decl.c (check_tag_decl): Use DECL_SOURCE_LOCATION.
[gcc.git] / gcc / cp / decl.c
1 /* Process declarations and variables for -*- C++ -*- compiler.
2 Copyright (C) 1988-2019 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
57 /* Possible cases of bad specifiers type used by bad_specifiers. */
58 enum bad_spec_place {
59 BSP_VAR, /* variable */
60 BSP_PARM, /* parameter */
61 BSP_TYPE, /* type */
62 BSP_FIELD /* field */
63 };
64
65 static const char *redeclaration_error_message (tree, tree);
66
67 static int decl_jump_unsafe (tree);
68 static void require_complete_types_for_parms (tree);
69 static tree grok_reference_init (tree, tree, tree, int);
70 static tree grokvardecl (tree, tree, tree, const cp_decl_specifier_seq *,
71 int, int, int, bool, int, tree, location_t);
72 static void check_static_variable_definition (tree, tree);
73 static void record_unknown_type (tree, const char *);
74 static tree builtin_function_1 (tree, tree, bool);
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 return true;
1229 if (TREE_CODE (old_decl) == FUNCTION_DECL)
1230 {
1231 if (fndecl_built_in_p (old_decl))
1232 {
1233 /* Hide a built-in declaration. */
1234 DECL_DECLARED_CONSTEXPR_P (old_decl)
1235 = DECL_DECLARED_CONSTEXPR_P (new_decl);
1236 return true;
1237 }
1238 /* 7.1.5 [dcl.constexpr]
1239 Note: An explicit specialization can differ from the template
1240 declaration with respect to the constexpr specifier. */
1241 if (! DECL_TEMPLATE_SPECIALIZATION (old_decl)
1242 && DECL_TEMPLATE_SPECIALIZATION (new_decl))
1243 return true;
1244
1245 error_at (DECL_SOURCE_LOCATION (new_decl),
1246 "redeclaration %qD differs in %<constexpr%> "
1247 "from previous declaration", new_decl);
1248 inform (DECL_SOURCE_LOCATION (old_decl),
1249 "previous declaration %qD", old_decl);
1250 return false;
1251 }
1252 return true;
1253 }
1254
1255 // If OLDDECL and NEWDECL are concept declarations with the same type
1256 // (i.e., and template parameters), but different requirements,
1257 // emit diagnostics and return true. Otherwise, return false.
1258 static inline bool
1259 check_concept_refinement (tree olddecl, tree newdecl)
1260 {
1261 if (!DECL_DECLARED_CONCEPT_P (olddecl) || !DECL_DECLARED_CONCEPT_P (newdecl))
1262 return false;
1263
1264 tree d1 = DECL_TEMPLATE_RESULT (olddecl);
1265 tree d2 = DECL_TEMPLATE_RESULT (newdecl);
1266 if (TREE_CODE (d1) != TREE_CODE (d2))
1267 return false;
1268
1269 tree t1 = TREE_TYPE (d1);
1270 tree t2 = TREE_TYPE (d2);
1271 if (TREE_CODE (d1) == FUNCTION_DECL)
1272 {
1273 if (compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2))
1274 && comp_template_parms (DECL_TEMPLATE_PARMS (olddecl),
1275 DECL_TEMPLATE_PARMS (newdecl))
1276 && !equivalently_constrained (olddecl, newdecl))
1277 {
1278 error ("cannot specialize concept %q#D", olddecl);
1279 return true;
1280 }
1281 }
1282 return false;
1283 }
1284
1285 /* DECL is a redeclaration of a function or function template. If
1286 it does have default arguments issue a diagnostic. Note: this
1287 function is used to enforce the requirements in C++11 8.3.6 about
1288 no default arguments in redeclarations. */
1289
1290 static void
1291 check_redeclaration_no_default_args (tree decl)
1292 {
1293 gcc_assert (DECL_DECLARES_FUNCTION_P (decl));
1294
1295 for (tree t = FUNCTION_FIRST_USER_PARMTYPE (decl);
1296 t && t != void_list_node; t = TREE_CHAIN (t))
1297 if (TREE_PURPOSE (t))
1298 {
1299 permerror (DECL_SOURCE_LOCATION (decl),
1300 "redeclaration of %q#D may not have default "
1301 "arguments", decl);
1302 return;
1303 }
1304 }
1305
1306 /* NEWDECL is a redeclaration of a function or function template OLDDECL,
1307 in any case represented as FUNCTION_DECLs (the DECL_TEMPLATE_RESULTs of
1308 the TEMPLATE_DECLs in case of function templates). This function is used
1309 to enforce the final part of C++17 11.3.6/4, about a single declaration:
1310 "If a friend declaration specifies a default argument expression, that
1311 declaration shall be a definition and shall be the only declaration of
1312 the function or function template in the translation unit." */
1313
1314 static void
1315 check_no_redeclaration_friend_default_args (tree olddecl, tree newdecl,
1316 bool olddecl_hidden_friend_p)
1317 {
1318 if (!olddecl_hidden_friend_p && !DECL_FRIEND_P (newdecl))
1319 return;
1320
1321 tree t1 = FUNCTION_FIRST_USER_PARMTYPE (olddecl);
1322 tree t2 = FUNCTION_FIRST_USER_PARMTYPE (newdecl);
1323
1324 for (; t1 && t1 != void_list_node;
1325 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1326 if ((olddecl_hidden_friend_p && TREE_PURPOSE (t1))
1327 || (DECL_FRIEND_P (newdecl) && TREE_PURPOSE (t2)))
1328 {
1329 auto_diagnostic_group d;
1330 if (permerror (DECL_SOURCE_LOCATION (newdecl),
1331 "friend declaration of %q#D specifies default "
1332 "arguments and isn%'t the only declaration", newdecl))
1333 inform (DECL_SOURCE_LOCATION (olddecl),
1334 "previous declaration of %q#D", olddecl);
1335 return;
1336 }
1337 }
1338
1339 /* Merge tree bits that correspond to attributes noreturn, nothrow,
1340 const, malloc, and pure from NEWDECL with those of OLDDECL. */
1341
1342 static void
1343 merge_attribute_bits (tree newdecl, tree olddecl)
1344 {
1345 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1346 TREE_THIS_VOLATILE (olddecl) |= TREE_THIS_VOLATILE (newdecl);
1347 TREE_NOTHROW (newdecl) |= TREE_NOTHROW (olddecl);
1348 TREE_NOTHROW (olddecl) |= TREE_NOTHROW (newdecl);
1349 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1350 TREE_READONLY (olddecl) |= TREE_READONLY (newdecl);
1351 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1352 DECL_IS_MALLOC (olddecl) |= DECL_IS_MALLOC (newdecl);
1353 DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
1354 DECL_PURE_P (olddecl) |= DECL_PURE_P (newdecl);
1355 DECL_UNINLINABLE (newdecl) |= DECL_UNINLINABLE (olddecl);
1356 DECL_UNINLINABLE (olddecl) |= DECL_UNINLINABLE (newdecl);
1357 }
1358
1359 #define GNU_INLINE_P(fn) (DECL_DECLARED_INLINE_P (fn) \
1360 && lookup_attribute ("gnu_inline", \
1361 DECL_ATTRIBUTES (fn)))
1362
1363 /* A subroutine of duplicate_decls. Emits a diagnostic when newdecl
1364 ambiguates olddecl. Returns true if an error occurs. */
1365
1366 static bool
1367 duplicate_function_template_decls (tree newdecl, tree olddecl)
1368 {
1369
1370 tree newres = DECL_TEMPLATE_RESULT (newdecl);
1371 tree oldres = DECL_TEMPLATE_RESULT (olddecl);
1372 /* Function template declarations can be differentiated by parameter
1373 and return type. */
1374 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (oldres)),
1375 TYPE_ARG_TYPES (TREE_TYPE (newres)))
1376 && same_type_p (TREE_TYPE (TREE_TYPE (newdecl)),
1377 TREE_TYPE (TREE_TYPE (olddecl))))
1378 {
1379 /* ... and also by their template-heads and requires-clauses. */
1380 if (template_heads_equivalent_p (newdecl, olddecl)
1381 && function_requirements_equivalent_p (newres, oldres))
1382 {
1383 error ("ambiguating new declaration %q+#D", newdecl);
1384 inform (DECL_SOURCE_LOCATION (olddecl),
1385 "old declaration %q#D", olddecl);
1386 return true;
1387 }
1388
1389 /* FIXME: The types are the same but the are differences
1390 in either the template heads or function requirements.
1391 We should be able to diagnose a set of common errors
1392 stemming from these declarations. For example:
1393
1394 template<typename T> requires C void f(...);
1395 template<typename T> void f(...) requires C;
1396
1397 These are functionally equivalent but not equivalent. */
1398 }
1399
1400 return false;
1401 }
1402
1403 /* If NEWDECL is a redeclaration of OLDDECL, merge the declarations.
1404 If the redeclaration is invalid, a diagnostic is issued, and the
1405 error_mark_node is returned. Otherwise, OLDDECL is returned.
1406
1407 If NEWDECL is not a redeclaration of OLDDECL, NULL_TREE is
1408 returned.
1409
1410 NEWDECL_IS_FRIEND is true if NEWDECL was declared as a friend. */
1411
1412 tree
1413 duplicate_decls (tree newdecl, tree olddecl, bool newdecl_is_friend)
1414 {
1415 unsigned olddecl_uid = DECL_UID (olddecl);
1416 int olddecl_friend = 0, types_match = 0, hidden_friend = 0;
1417 int olddecl_hidden_friend = 0;
1418 int new_defines_function = 0;
1419 tree new_template_info;
1420 location_t olddecl_loc = DECL_SOURCE_LOCATION (olddecl);
1421 location_t newdecl_loc = DECL_SOURCE_LOCATION (newdecl);
1422
1423 if (newdecl == olddecl)
1424 return olddecl;
1425
1426 types_match = decls_match (newdecl, olddecl);
1427
1428 /* If either the type of the new decl or the type of the old decl is an
1429 error_mark_node, then that implies that we have already issued an
1430 error (earlier) for some bogus type specification, and in that case,
1431 it is rather pointless to harass the user with yet more error message
1432 about the same declaration, so just pretend the types match here. */
1433 if (TREE_TYPE (newdecl) == error_mark_node
1434 || TREE_TYPE (olddecl) == error_mark_node)
1435 return error_mark_node;
1436
1437 /* Check for redeclaration and other discrepancies. */
1438 if (TREE_CODE (olddecl) == FUNCTION_DECL
1439 && DECL_ARTIFICIAL (olddecl))
1440 {
1441 gcc_assert (!DECL_HIDDEN_FRIEND_P (olddecl));
1442 if (TREE_CODE (newdecl) != FUNCTION_DECL)
1443 {
1444 /* Avoid warnings redeclaring built-ins which have not been
1445 explicitly declared. */
1446 if (DECL_ANTICIPATED (olddecl))
1447 {
1448 if (TREE_PUBLIC (newdecl)
1449 && CP_DECL_CONTEXT (newdecl) == global_namespace)
1450 warning_at (newdecl_loc,
1451 OPT_Wbuiltin_declaration_mismatch,
1452 "built-in function %qD declared as non-function",
1453 newdecl);
1454 return NULL_TREE;
1455 }
1456
1457 /* If you declare a built-in or predefined function name as static,
1458 the old definition is overridden, but optionally warn this was a
1459 bad choice of name. */
1460 if (! TREE_PUBLIC (newdecl))
1461 {
1462 warning_at (newdecl_loc,
1463 OPT_Wshadow,
1464 fndecl_built_in_p (olddecl)
1465 ? G_("shadowing built-in function %q#D")
1466 : G_("shadowing library function %q#D"), olddecl);
1467 /* Discard the old built-in function. */
1468 return NULL_TREE;
1469 }
1470 /* If the built-in is not ansi, then programs can override
1471 it even globally without an error. */
1472 else if (! fndecl_built_in_p (olddecl))
1473 warning_at (newdecl_loc, 0,
1474 "library function %q#D redeclared as non-function %q#D",
1475 olddecl, newdecl);
1476 else
1477 error_at (newdecl_loc,
1478 "declaration of %q#D conflicts with built-in "
1479 "declaration %q#D", newdecl, olddecl);
1480 return NULL_TREE;
1481 }
1482 else if (DECL_OMP_DECLARE_REDUCTION_P (olddecl))
1483 {
1484 gcc_assert (DECL_OMP_DECLARE_REDUCTION_P (newdecl));
1485 error_at (newdecl_loc,
1486 "redeclaration of %<pragma omp declare reduction%>");
1487 inform (olddecl_loc,
1488 "previous %<pragma omp declare reduction%> declaration");
1489 return error_mark_node;
1490 }
1491 else if (!types_match)
1492 {
1493 /* Avoid warnings redeclaring built-ins which have not been
1494 explicitly declared. */
1495 if (DECL_ANTICIPATED (olddecl))
1496 {
1497 tree t1, t2;
1498
1499 /* A new declaration doesn't match a built-in one unless it
1500 is also extern "C". */
1501 gcc_assert (DECL_IS_BUILTIN (olddecl));
1502 gcc_assert (DECL_EXTERN_C_P (olddecl));
1503 if (!DECL_EXTERN_C_P (newdecl))
1504 return NULL_TREE;
1505
1506 for (t1 = TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1507 t2 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1508 t1 || t2;
1509 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1510 {
1511 if (!t1 || !t2)
1512 break;
1513 /* FILE, tm types are not known at the time
1514 we create the builtins. */
1515 for (unsigned i = 0;
1516 i < sizeof (builtin_structptr_types)
1517 / sizeof (builtin_structptr_type);
1518 ++i)
1519 if (TREE_VALUE (t2) == builtin_structptr_types[i].node)
1520 {
1521 tree t = TREE_VALUE (t1);
1522
1523 if (TYPE_PTR_P (t)
1524 && TYPE_IDENTIFIER (TREE_TYPE (t))
1525 == get_identifier (builtin_structptr_types[i].str)
1526 && compparms (TREE_CHAIN (t1), TREE_CHAIN (t2)))
1527 {
1528 tree oldargs = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1529
1530 TYPE_ARG_TYPES (TREE_TYPE (olddecl))
1531 = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
1532 types_match = decls_match (newdecl, olddecl);
1533 if (types_match)
1534 return duplicate_decls (newdecl, olddecl,
1535 newdecl_is_friend);
1536 TYPE_ARG_TYPES (TREE_TYPE (olddecl)) = oldargs;
1537 }
1538 goto next_arg;
1539 }
1540
1541 if (! same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1542 break;
1543 next_arg:;
1544 }
1545
1546 warning_at (newdecl_loc,
1547 OPT_Wbuiltin_declaration_mismatch,
1548 "declaration of %q#D conflicts with built-in "
1549 "declaration %q#D", newdecl, olddecl);
1550 }
1551 else if ((DECL_EXTERN_C_P (newdecl)
1552 && DECL_EXTERN_C_P (olddecl))
1553 || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1554 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
1555 {
1556 /* Don't really override olddecl for __* prefixed builtins
1557 except for __[^b]*_chk, the compiler might be using those
1558 explicitly. */
1559 if (fndecl_built_in_p (olddecl))
1560 {
1561 tree id = DECL_NAME (olddecl);
1562 const char *name = IDENTIFIER_POINTER (id);
1563 size_t len;
1564
1565 if (name[0] == '_'
1566 && name[1] == '_'
1567 && (strncmp (name + 2, "builtin_",
1568 strlen ("builtin_")) == 0
1569 || (len = strlen (name)) <= strlen ("___chk")
1570 || memcmp (name + len - strlen ("_chk"),
1571 "_chk", strlen ("_chk") + 1) != 0))
1572 {
1573 if (DECL_INITIAL (newdecl))
1574 {
1575 error_at (newdecl_loc,
1576 "definition of %q#D ambiguates built-in "
1577 "declaration %q#D", newdecl, olddecl);
1578 return error_mark_node;
1579 }
1580 auto_diagnostic_group d;
1581 if (permerror (newdecl_loc,
1582 "new declaration %q#D ambiguates built-in"
1583 " declaration %q#D", newdecl, olddecl)
1584 && flag_permissive)
1585 inform (newdecl_loc,
1586 "ignoring the %q#D declaration", newdecl);
1587 return flag_permissive ? olddecl : error_mark_node;
1588 }
1589 }
1590
1591 /* A near match; override the builtin. */
1592
1593 if (TREE_PUBLIC (newdecl))
1594 warning_at (newdecl_loc,
1595 OPT_Wbuiltin_declaration_mismatch,
1596 "new declaration %q#D ambiguates built-in "
1597 "declaration %q#D", newdecl, olddecl);
1598 else
1599 warning (OPT_Wshadow,
1600 fndecl_built_in_p (olddecl)
1601 ? G_("shadowing built-in function %q#D")
1602 : G_("shadowing library function %q#D"), olddecl);
1603 }
1604 else
1605 /* Discard the old built-in function. */
1606 return NULL_TREE;
1607
1608 /* Replace the old RTL to avoid problems with inlining. */
1609 COPY_DECL_RTL (newdecl, olddecl);
1610 }
1611 /* Even if the types match, prefer the new declarations type for
1612 built-ins which have not been explicitly declared, for
1613 exception lists, etc... */
1614 else if (DECL_IS_BUILTIN (olddecl))
1615 {
1616 tree type = TREE_TYPE (newdecl);
1617 tree attribs = (*targetm.merge_type_attributes)
1618 (TREE_TYPE (olddecl), type);
1619
1620 type = cp_build_type_attribute_variant (type, attribs);
1621 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = type;
1622 }
1623
1624 /* If a function is explicitly declared "throw ()", propagate that to
1625 the corresponding builtin. */
1626 if (DECL_BUILT_IN_CLASS (olddecl) == BUILT_IN_NORMAL
1627 && DECL_ANTICIPATED (olddecl)
1628 && TREE_NOTHROW (newdecl)
1629 && !TREE_NOTHROW (olddecl))
1630 {
1631 enum built_in_function fncode = DECL_FUNCTION_CODE (olddecl);
1632 tree tmpdecl = builtin_decl_explicit (fncode);
1633 if (tmpdecl && tmpdecl != olddecl && types_match)
1634 TREE_NOTHROW (tmpdecl) = 1;
1635 }
1636
1637 /* Whether or not the builtin can throw exceptions has no
1638 bearing on this declarator. */
1639 TREE_NOTHROW (olddecl) = 0;
1640
1641 if (DECL_THIS_STATIC (newdecl) && !DECL_THIS_STATIC (olddecl))
1642 {
1643 /* If a builtin function is redeclared as `static', merge
1644 the declarations, but make the original one static. */
1645 DECL_THIS_STATIC (olddecl) = 1;
1646 TREE_PUBLIC (olddecl) = 0;
1647
1648 /* Make the old declaration consistent with the new one so
1649 that all remnants of the builtin-ness of this function
1650 will be banished. */
1651 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
1652 COPY_DECL_RTL (newdecl, olddecl);
1653 }
1654 }
1655 else if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1656 {
1657 /* C++ Standard, 3.3, clause 4:
1658 "[Note: a namespace name or a class template name must be unique
1659 in its declarative region (7.3.2, clause 14). ]" */
1660 if (TREE_CODE (olddecl) == NAMESPACE_DECL
1661 || TREE_CODE (newdecl) == NAMESPACE_DECL)
1662 /* Namespace conflicts with not namespace. */;
1663 else if (DECL_TYPE_TEMPLATE_P (olddecl)
1664 || DECL_TYPE_TEMPLATE_P (newdecl))
1665 /* Class template conflicts. */;
1666 else if ((TREE_CODE (newdecl) == FUNCTION_DECL
1667 && DECL_FUNCTION_TEMPLATE_P (olddecl))
1668 || (TREE_CODE (olddecl) == FUNCTION_DECL
1669 && DECL_FUNCTION_TEMPLATE_P (newdecl)))
1670 {
1671 /* One is a function and the other is a template
1672 function. */
1673 if (!UDLIT_OPER_P (DECL_NAME (newdecl)))
1674 return NULL_TREE;
1675
1676 /* There can only be one! */
1677 if (TREE_CODE (newdecl) == TEMPLATE_DECL
1678 && check_raw_literal_operator (olddecl))
1679 error_at (newdecl_loc,
1680 "literal operator %q#D conflicts with"
1681 " raw literal operator", newdecl);
1682 else if (check_raw_literal_operator (newdecl))
1683 error_at (newdecl_loc,
1684 "raw literal operator %q#D conflicts with"
1685 " literal operator template", newdecl);
1686 else
1687 return NULL_TREE;
1688
1689 inform (olddecl_loc, "previous declaration %q#D", olddecl);
1690 return error_mark_node;
1691 }
1692 else if (DECL_IMPLICIT_TYPEDEF_P (olddecl)
1693 || DECL_IMPLICIT_TYPEDEF_P (newdecl))
1694 /* One is an implicit typedef, that's ok. */
1695 return NULL_TREE;
1696
1697 error ("%q#D redeclared as different kind of entity", newdecl);
1698 inform (olddecl_loc, "previous declaration %q#D", olddecl);
1699
1700 return error_mark_node;
1701 }
1702 else if (!types_match)
1703 {
1704 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl))
1705 /* These are certainly not duplicate declarations; they're
1706 from different scopes. */
1707 return NULL_TREE;
1708
1709 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1710 {
1711 tree oldres = DECL_TEMPLATE_RESULT (olddecl);
1712 tree newres = DECL_TEMPLATE_RESULT (newdecl);
1713
1714 /* The name of a class template may not be declared to refer to
1715 any other template, class, function, object, namespace, value,
1716 or type in the same scope. */
1717 if (TREE_CODE (oldres) == TYPE_DECL
1718 || TREE_CODE (newres) == TYPE_DECL)
1719 {
1720 error_at (newdecl_loc,
1721 "conflicting declaration of template %q#D", newdecl);
1722 inform (olddecl_loc,
1723 "previous declaration %q#D", olddecl);
1724 return error_mark_node;
1725 }
1726
1727 else if (TREE_CODE (oldres) == FUNCTION_DECL
1728 && TREE_CODE (newres) == FUNCTION_DECL)
1729 {
1730 if (duplicate_function_template_decls (newdecl, olddecl))
1731 return error_mark_node;
1732 return NULL_TREE;
1733 }
1734 else if (check_concept_refinement (olddecl, newdecl))
1735 return error_mark_node;
1736 return NULL_TREE;
1737 }
1738 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1739 {
1740 if (DECL_EXTERN_C_P (newdecl) && DECL_EXTERN_C_P (olddecl))
1741 {
1742 error_at (newdecl_loc,
1743 "conflicting declaration of C function %q#D",
1744 newdecl);
1745 inform (olddecl_loc,
1746 "previous declaration %q#D", olddecl);
1747 return NULL_TREE;
1748 }
1749 /* For function versions, params and types match, but they
1750 are not ambiguous. */
1751 else if ((!DECL_FUNCTION_VERSIONED (newdecl)
1752 && !DECL_FUNCTION_VERSIONED (olddecl))
1753 // The functions have the same parameter types.
1754 && compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1755 TYPE_ARG_TYPES (TREE_TYPE (olddecl)))
1756 // And the same constraints.
1757 && equivalently_constrained (newdecl, olddecl))
1758 {
1759 error_at (newdecl_loc,
1760 "ambiguating new declaration of %q#D", newdecl);
1761 inform (olddecl_loc,
1762 "old declaration %q#D", olddecl);
1763 return error_mark_node;
1764 }
1765 else
1766 return NULL_TREE;
1767 }
1768 else
1769 {
1770 error_at (newdecl_loc, "conflicting declaration %q#D", newdecl);
1771 inform (olddecl_loc,
1772 "previous declaration as %q#D", olddecl);
1773 return error_mark_node;
1774 }
1775 }
1776 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1777 && ((DECL_TEMPLATE_SPECIALIZATION (olddecl)
1778 && (!DECL_TEMPLATE_INFO (newdecl)
1779 || (DECL_TI_TEMPLATE (newdecl)
1780 != DECL_TI_TEMPLATE (olddecl))))
1781 || (DECL_TEMPLATE_SPECIALIZATION (newdecl)
1782 && (!DECL_TEMPLATE_INFO (olddecl)
1783 || (DECL_TI_TEMPLATE (olddecl)
1784 != DECL_TI_TEMPLATE (newdecl))))))
1785 /* It's OK to have a template specialization and a non-template
1786 with the same type, or to have specializations of two
1787 different templates with the same type. Note that if one is a
1788 specialization, and the other is an instantiation of the same
1789 template, that we do not exit at this point. That situation
1790 can occur if we instantiate a template class, and then
1791 specialize one of its methods. This situation is valid, but
1792 the declarations must be merged in the usual way. */
1793 return NULL_TREE;
1794 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1795 && ((DECL_TEMPLATE_INSTANTIATION (olddecl)
1796 && !DECL_USE_TEMPLATE (newdecl))
1797 || (DECL_TEMPLATE_INSTANTIATION (newdecl)
1798 && !DECL_USE_TEMPLATE (olddecl))))
1799 /* One of the declarations is a template instantiation, and the
1800 other is not a template at all. That's OK. */
1801 return NULL_TREE;
1802 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
1803 {
1804 /* In [namespace.alias] we have:
1805
1806 In a declarative region, a namespace-alias-definition can be
1807 used to redefine a namespace-alias declared in that declarative
1808 region to refer only to the namespace to which it already
1809 refers.
1810
1811 Therefore, if we encounter a second alias directive for the same
1812 alias, we can just ignore the second directive. */
1813 if (DECL_NAMESPACE_ALIAS (newdecl)
1814 && (DECL_NAMESPACE_ALIAS (newdecl)
1815 == DECL_NAMESPACE_ALIAS (olddecl)))
1816 return olddecl;
1817
1818 /* Leave it to update_binding to merge or report error. */
1819 return NULL_TREE;
1820 }
1821 else
1822 {
1823 const char *errmsg = redeclaration_error_message (newdecl, olddecl);
1824 if (errmsg)
1825 {
1826 auto_diagnostic_group d;
1827 error_at (newdecl_loc, errmsg, newdecl);
1828 if (DECL_NAME (olddecl) != NULL_TREE)
1829 inform (olddecl_loc,
1830 (DECL_INITIAL (olddecl) && namespace_bindings_p ())
1831 ? G_("%q#D previously defined here")
1832 : G_("%q#D previously declared here"), olddecl);
1833 return error_mark_node;
1834 }
1835 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1836 && DECL_INITIAL (olddecl) != NULL_TREE
1837 && !prototype_p (TREE_TYPE (olddecl))
1838 && prototype_p (TREE_TYPE (newdecl)))
1839 {
1840 /* Prototype decl follows defn w/o prototype. */
1841 auto_diagnostic_group d;
1842 if (warning_at (newdecl_loc, 0,
1843 "prototype specified for %q#D", newdecl))
1844 inform (olddecl_loc,
1845 "previous non-prototype definition here");
1846 }
1847 else if (VAR_OR_FUNCTION_DECL_P (olddecl)
1848 && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl))
1849 {
1850 /* [dcl.link]
1851 If two declarations of the same function or object
1852 specify different linkage-specifications ..., the program
1853 is ill-formed.... Except for functions with C++ linkage,
1854 a function declaration without a linkage specification
1855 shall not precede the first linkage specification for
1856 that function. A function can be declared without a
1857 linkage specification after an explicit linkage
1858 specification has been seen; the linkage explicitly
1859 specified in the earlier declaration is not affected by
1860 such a function declaration.
1861
1862 DR 563 raises the question why the restrictions on
1863 functions should not also apply to objects. Older
1864 versions of G++ silently ignore the linkage-specification
1865 for this example:
1866
1867 namespace N {
1868 extern int i;
1869 extern "C" int i;
1870 }
1871
1872 which is clearly wrong. Therefore, we now treat objects
1873 like functions. */
1874 if (current_lang_depth () == 0)
1875 {
1876 /* There is no explicit linkage-specification, so we use
1877 the linkage from the previous declaration. */
1878 retrofit_lang_decl (newdecl);
1879 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
1880 }
1881 else
1882 {
1883 auto_diagnostic_group d;
1884 error_at (newdecl_loc,
1885 "conflicting declaration of %q#D with %qL linkage",
1886 newdecl, DECL_LANGUAGE (newdecl));
1887 inform (olddecl_loc,
1888 "previous declaration with %qL linkage",
1889 DECL_LANGUAGE (olddecl));
1890 }
1891 }
1892
1893 if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl))
1894 ;
1895 else if (TREE_CODE (olddecl) == FUNCTION_DECL)
1896 {
1897 /* Note: free functions, as TEMPLATE_DECLs, are handled below. */
1898 if (DECL_FUNCTION_MEMBER_P (olddecl)
1899 && (/* grokfndecl passes member function templates too
1900 as FUNCTION_DECLs. */
1901 DECL_TEMPLATE_INFO (olddecl)
1902 /* C++11 8.3.6/6.
1903 Default arguments for a member function of a class
1904 template shall be specified on the initial declaration
1905 of the member function within the class template. */
1906 || CLASSTYPE_TEMPLATE_INFO (CP_DECL_CONTEXT (olddecl))))
1907 check_redeclaration_no_default_args (newdecl);
1908 else
1909 {
1910 tree t1 = FUNCTION_FIRST_USER_PARMTYPE (olddecl);
1911 tree t2 = FUNCTION_FIRST_USER_PARMTYPE (newdecl);
1912 int i = 1;
1913
1914 for (; t1 && t1 != void_list_node;
1915 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++)
1916 if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
1917 {
1918 if (simple_cst_equal (TREE_PURPOSE (t1),
1919 TREE_PURPOSE (t2)) == 1)
1920 {
1921 auto_diagnostic_group d;
1922 if (permerror (newdecl_loc,
1923 "default argument given for parameter "
1924 "%d of %q#D", i, newdecl))
1925 inform (olddecl_loc,
1926 "previous specification in %q#D here",
1927 olddecl);
1928 }
1929 else
1930 {
1931 auto_diagnostic_group d;
1932 error_at (newdecl_loc,
1933 "default argument given for parameter %d "
1934 "of %q#D", i, newdecl);
1935 inform (olddecl_loc,
1936 "previous specification in %q#D here",
1937 olddecl);
1938 }
1939 }
1940
1941 /* C++17 11.3.6/4: "If a friend declaration specifies a default
1942 argument expression, that declaration... shall be the only
1943 declaration of the function or function template in the
1944 translation unit." */
1945 check_no_redeclaration_friend_default_args
1946 (olddecl, newdecl, DECL_HIDDEN_FRIEND_P (olddecl));
1947 }
1948 }
1949 }
1950
1951 /* Do not merge an implicit typedef with an explicit one. In:
1952
1953 class A;
1954 ...
1955 typedef class A A __attribute__ ((foo));
1956
1957 the attribute should apply only to the typedef. */
1958 if (TREE_CODE (olddecl) == TYPE_DECL
1959 && (DECL_IMPLICIT_TYPEDEF_P (olddecl)
1960 || DECL_IMPLICIT_TYPEDEF_P (newdecl)))
1961 return NULL_TREE;
1962
1963 if (!validate_constexpr_redeclaration (olddecl, newdecl))
1964 return error_mark_node;
1965
1966 /* We have committed to returning OLDDECL at this point. */
1967
1968 /* If new decl is `static' and an `extern' was seen previously,
1969 warn about it. */
1970 warn_extern_redeclared_static (newdecl, olddecl);
1971
1972 /* True to merge attributes between the declarations, false to
1973 set OLDDECL's attributes to those of NEWDECL (for template
1974 explicit specializations that specify their own attributes
1975 independent of those specified for the primary template). */
1976 const bool merge_attr = (TREE_CODE (newdecl) != FUNCTION_DECL
1977 || !DECL_TEMPLATE_SPECIALIZATION (newdecl)
1978 || DECL_TEMPLATE_SPECIALIZATION (olddecl));
1979
1980 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1981 {
1982 if (merge_attr)
1983 {
1984 if (diagnose_mismatched_attributes (olddecl, newdecl))
1985 inform (olddecl_loc, DECL_INITIAL (olddecl)
1986 ? G_("previous definition of %qD here")
1987 : G_("previous declaration of %qD here"), olddecl);
1988
1989 /* [dcl.attr.noreturn]: The first declaration of a function shall
1990 specify the noreturn attribute if any declaration of that function
1991 specifies the noreturn attribute. */
1992 tree a;
1993 if (TREE_THIS_VOLATILE (newdecl)
1994 && !TREE_THIS_VOLATILE (olddecl)
1995 /* This applies to [[noreturn]] only, not its GNU variants. */
1996 && (a = lookup_attribute ("noreturn", DECL_ATTRIBUTES (newdecl)))
1997 && cxx11_attribute_p (a)
1998 && get_attribute_namespace (a) == NULL_TREE)
1999 {
2000 error_at (newdecl_loc, "function %qD declared %<[[noreturn]]%> "
2001 "but its first declaration was not", newdecl);
2002 inform (olddecl_loc, "previous declaration of %qD", olddecl);
2003 }
2004 }
2005
2006 /* Now that functions must hold information normally held
2007 by field decls, there is extra work to do so that
2008 declaration information does not get destroyed during
2009 definition. */
2010 if (DECL_VINDEX (olddecl))
2011 DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl);
2012 if (DECL_CONTEXT (olddecl))
2013 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
2014 DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl);
2015 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
2016 DECL_PURE_VIRTUAL_P (newdecl) |= DECL_PURE_VIRTUAL_P (olddecl);
2017 DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl);
2018 DECL_INVALID_OVERRIDER_P (newdecl) |= DECL_INVALID_OVERRIDER_P (olddecl);
2019 DECL_FINAL_P (newdecl) |= DECL_FINAL_P (olddecl);
2020 DECL_OVERRIDE_P (newdecl) |= DECL_OVERRIDE_P (olddecl);
2021 DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl);
2022 if (DECL_OVERLOADED_OPERATOR_P (olddecl))
2023 DECL_OVERLOADED_OPERATOR_CODE_RAW (newdecl)
2024 = DECL_OVERLOADED_OPERATOR_CODE_RAW (olddecl);
2025 new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE;
2026
2027 /* Optionally warn about more than one declaration for the same
2028 name, but don't warn about a function declaration followed by a
2029 definition. */
2030 if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl)
2031 && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE)
2032 /* Don't warn about extern decl followed by definition. */
2033 && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl))
2034 /* Don't warn about friends, let add_friend take care of it. */
2035 && ! (newdecl_is_friend || DECL_FRIEND_P (olddecl))
2036 /* Don't warn about declaration followed by specialization. */
2037 && (! DECL_TEMPLATE_SPECIALIZATION (newdecl)
2038 || DECL_TEMPLATE_SPECIALIZATION (olddecl)))
2039 {
2040 auto_diagnostic_group d;
2041 if (warning_at (newdecl_loc,
2042 OPT_Wredundant_decls,
2043 "redundant redeclaration of %qD in same scope",
2044 newdecl))
2045 inform (olddecl_loc,
2046 "previous declaration of %qD", olddecl);
2047 }
2048
2049 if (!(DECL_TEMPLATE_INSTANTIATION (olddecl)
2050 && DECL_TEMPLATE_SPECIALIZATION (newdecl)))
2051 {
2052 if (DECL_DELETED_FN (newdecl))
2053 {
2054 auto_diagnostic_group d;
2055 error_at (newdecl_loc, "deleted definition of %qD", newdecl);
2056 inform (olddecl_loc,
2057 "previous declaration of %qD", olddecl);
2058 }
2059 DECL_DELETED_FN (newdecl) |= DECL_DELETED_FN (olddecl);
2060 }
2061 }
2062
2063 /* Deal with C++: must preserve virtual function table size. */
2064 if (TREE_CODE (olddecl) == TYPE_DECL)
2065 {
2066 tree newtype = TREE_TYPE (newdecl);
2067 tree oldtype = TREE_TYPE (olddecl);
2068
2069 if (newtype != error_mark_node && oldtype != error_mark_node
2070 && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype))
2071 CLASSTYPE_FRIEND_CLASSES (newtype)
2072 = CLASSTYPE_FRIEND_CLASSES (oldtype);
2073
2074 DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl);
2075 }
2076
2077 /* Copy all the DECL_... slots specified in the new decl except for
2078 any that we copy here from the old type. */
2079 if (merge_attr)
2080 DECL_ATTRIBUTES (newdecl)
2081 = (*targetm.merge_decl_attributes) (olddecl, newdecl);
2082 else
2083 DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
2084
2085 if (DECL_DECLARES_FUNCTION_P (olddecl))
2086 {
2087 olddecl_friend = DECL_FRIEND_P (olddecl);
2088 olddecl_hidden_friend = DECL_HIDDEN_FRIEND_P (olddecl);
2089 hidden_friend = (DECL_ANTICIPATED (olddecl)
2090 && DECL_HIDDEN_FRIEND_P (olddecl)
2091 && newdecl_is_friend);
2092 if (!hidden_friend)
2093 {
2094 DECL_ANTICIPATED (olddecl) = 0;
2095 DECL_HIDDEN_FRIEND_P (olddecl) = 0;
2096 }
2097 }
2098
2099 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2100 {
2101 tree old_result = DECL_TEMPLATE_RESULT (olddecl);
2102 tree new_result = DECL_TEMPLATE_RESULT (newdecl);
2103 TREE_TYPE (olddecl) = TREE_TYPE (old_result);
2104
2105 /* The new decl should not already have gathered any
2106 specializations. */
2107 gcc_assert (!DECL_TEMPLATE_SPECIALIZATIONS (newdecl));
2108
2109 DECL_ATTRIBUTES (old_result)
2110 = (*targetm.merge_decl_attributes) (old_result, new_result);
2111
2112 if (DECL_FUNCTION_TEMPLATE_P (newdecl))
2113 {
2114 if (DECL_SOURCE_LOCATION (newdecl)
2115 != DECL_SOURCE_LOCATION (olddecl))
2116 {
2117 /* Per C++11 8.3.6/4, default arguments cannot be added in
2118 later declarations of a function template. */
2119 check_redeclaration_no_default_args (newdecl);
2120 /* C++17 11.3.6/4: "If a friend declaration specifies a default
2121 argument expression, that declaration... shall be the only
2122 declaration of the function or function template in the
2123 translation unit." */
2124 check_no_redeclaration_friend_default_args
2125 (old_result, new_result, olddecl_hidden_friend);
2126 }
2127
2128 check_default_args (newdecl);
2129
2130 if (GNU_INLINE_P (old_result) != GNU_INLINE_P (new_result)
2131 && DECL_INITIAL (new_result))
2132 {
2133 if (DECL_INITIAL (old_result))
2134 DECL_UNINLINABLE (old_result) = 1;
2135 else
2136 DECL_UNINLINABLE (old_result) = DECL_UNINLINABLE (new_result);
2137 DECL_EXTERNAL (old_result) = DECL_EXTERNAL (new_result);
2138 DECL_NOT_REALLY_EXTERN (old_result)
2139 = DECL_NOT_REALLY_EXTERN (new_result);
2140 DECL_INTERFACE_KNOWN (old_result)
2141 = DECL_INTERFACE_KNOWN (new_result);
2142 DECL_DECLARED_INLINE_P (old_result)
2143 = DECL_DECLARED_INLINE_P (new_result);
2144 DECL_DISREGARD_INLINE_LIMITS (old_result)
2145 |= DECL_DISREGARD_INLINE_LIMITS (new_result);
2146
2147 }
2148 else
2149 {
2150 DECL_DECLARED_INLINE_P (old_result)
2151 |= DECL_DECLARED_INLINE_P (new_result);
2152 DECL_DISREGARD_INLINE_LIMITS (old_result)
2153 |= DECL_DISREGARD_INLINE_LIMITS (new_result);
2154 check_redeclaration_exception_specification (newdecl, olddecl);
2155
2156 merge_attribute_bits (new_result, old_result);
2157 }
2158 }
2159
2160 /* If the new declaration is a definition, update the file and
2161 line information on the declaration, and also make
2162 the old declaration the same definition. */
2163 if (DECL_INITIAL (new_result) != NULL_TREE)
2164 {
2165 DECL_SOURCE_LOCATION (olddecl)
2166 = DECL_SOURCE_LOCATION (old_result)
2167 = DECL_SOURCE_LOCATION (newdecl);
2168 DECL_INITIAL (old_result) = DECL_INITIAL (new_result);
2169 if (DECL_FUNCTION_TEMPLATE_P (newdecl))
2170 {
2171 tree parm;
2172 DECL_ARGUMENTS (old_result)
2173 = DECL_ARGUMENTS (new_result);
2174 for (parm = DECL_ARGUMENTS (old_result); parm;
2175 parm = DECL_CHAIN (parm))
2176 DECL_CONTEXT (parm) = old_result;
2177 }
2178 }
2179
2180 return olddecl;
2181 }
2182
2183 if (types_match)
2184 {
2185 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2186 check_redeclaration_exception_specification (newdecl, olddecl);
2187
2188 /* Automatically handles default parameters. */
2189 tree oldtype = TREE_TYPE (olddecl);
2190 tree newtype;
2191
2192 /* For typedefs use the old type, as the new type's DECL_NAME points
2193 at newdecl, which will be ggc_freed. */
2194 if (TREE_CODE (newdecl) == TYPE_DECL)
2195 {
2196 /* But NEWTYPE might have an attribute, honor that. */
2197 tree tem = TREE_TYPE (newdecl);
2198 newtype = oldtype;
2199
2200 if (TYPE_USER_ALIGN (tem))
2201 {
2202 if (TYPE_ALIGN (tem) > TYPE_ALIGN (newtype))
2203 SET_TYPE_ALIGN (newtype, TYPE_ALIGN (tem));
2204 TYPE_USER_ALIGN (newtype) = true;
2205 }
2206
2207 /* And remove the new type from the variants list. */
2208 if (TYPE_NAME (TREE_TYPE (newdecl)) == newdecl)
2209 {
2210 tree remove = TREE_TYPE (newdecl);
2211 if (TYPE_MAIN_VARIANT (remove) == remove)
2212 {
2213 gcc_assert (TYPE_NEXT_VARIANT (remove) == NULL_TREE);
2214 /* If remove is the main variant, no need to remove that
2215 from the list. One of the DECL_ORIGINAL_TYPE
2216 variants, e.g. created for aligned attribute, might still
2217 refer to the newdecl TYPE_DECL though, so remove that one
2218 in that case. */
2219 if (tree orig = DECL_ORIGINAL_TYPE (newdecl))
2220 if (orig != remove)
2221 for (tree t = TYPE_MAIN_VARIANT (orig); t;
2222 t = TYPE_MAIN_VARIANT (t))
2223 if (TYPE_NAME (TYPE_NEXT_VARIANT (t)) == newdecl)
2224 {
2225 TYPE_NEXT_VARIANT (t)
2226 = TYPE_NEXT_VARIANT (TYPE_NEXT_VARIANT (t));
2227 break;
2228 }
2229 }
2230 else
2231 for (tree t = TYPE_MAIN_VARIANT (remove); ;
2232 t = TYPE_NEXT_VARIANT (t))
2233 if (TYPE_NEXT_VARIANT (t) == remove)
2234 {
2235 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (remove);
2236 break;
2237 }
2238 }
2239 }
2240 else if (merge_attr)
2241 newtype = merge_types (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
2242 else
2243 newtype = TREE_TYPE (newdecl);
2244
2245 if (VAR_P (newdecl))
2246 {
2247 DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl);
2248 /* For already initialized vars, TREE_READONLY could have been
2249 cleared in cp_finish_decl, because the var needs runtime
2250 initialization or destruction. Make sure not to set
2251 TREE_READONLY on it again. */
2252 if (DECL_INITIALIZED_P (olddecl)
2253 && !DECL_EXTERNAL (olddecl)
2254 && !TREE_READONLY (olddecl))
2255 TREE_READONLY (newdecl) = 0;
2256 DECL_INITIALIZED_P (newdecl) |= DECL_INITIALIZED_P (olddecl);
2257 DECL_NONTRIVIALLY_INITIALIZED_P (newdecl)
2258 |= DECL_NONTRIVIALLY_INITIALIZED_P (olddecl);
2259 if (DECL_DEPENDENT_INIT_P (olddecl))
2260 SET_DECL_DEPENDENT_INIT_P (newdecl, true);
2261 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (newdecl)
2262 |= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (olddecl);
2263 DECL_DECLARED_CONSTEXPR_P (newdecl)
2264 |= DECL_DECLARED_CONSTEXPR_P (olddecl);
2265
2266 /* Merge the threadprivate attribute from OLDDECL into NEWDECL. */
2267 if (DECL_LANG_SPECIFIC (olddecl)
2268 && CP_DECL_THREADPRIVATE_P (olddecl))
2269 {
2270 /* Allocate a LANG_SPECIFIC structure for NEWDECL, if needed. */
2271 retrofit_lang_decl (newdecl);
2272 CP_DECL_THREADPRIVATE_P (newdecl) = 1;
2273 }
2274 }
2275
2276 /* An explicit specialization of a function template or of a member
2277 function of a class template can be declared transaction_safe
2278 independently of whether the corresponding template entity is declared
2279 transaction_safe. */
2280 if (flag_tm && TREE_CODE (newdecl) == FUNCTION_DECL
2281 && DECL_TEMPLATE_INSTANTIATION (olddecl)
2282 && DECL_TEMPLATE_SPECIALIZATION (newdecl)
2283 && tx_safe_fn_type_p (newtype)
2284 && !tx_safe_fn_type_p (TREE_TYPE (newdecl)))
2285 newtype = tx_unsafe_fn_variant (newtype);
2286
2287 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype;
2288
2289 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2290 check_default_args (newdecl);
2291
2292 /* Lay the type out, unless already done. */
2293 if (! same_type_p (newtype, oldtype)
2294 && TREE_TYPE (newdecl) != error_mark_node
2295 && !(processing_template_decl && uses_template_parms (newdecl)))
2296 layout_type (TREE_TYPE (newdecl));
2297
2298 if ((VAR_P (newdecl)
2299 || TREE_CODE (newdecl) == PARM_DECL
2300 || TREE_CODE (newdecl) == RESULT_DECL
2301 || TREE_CODE (newdecl) == FIELD_DECL
2302 || TREE_CODE (newdecl) == TYPE_DECL)
2303 && !(processing_template_decl && uses_template_parms (newdecl)))
2304 layout_decl (newdecl, 0);
2305
2306 /* Merge deprecatedness. */
2307 if (TREE_DEPRECATED (newdecl))
2308 TREE_DEPRECATED (olddecl) = 1;
2309
2310 /* Preserve function specific target and optimization options */
2311 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2312 {
2313 if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
2314 && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
2315 DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
2316 = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
2317
2318 if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
2319 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
2320 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
2321 = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
2322 }
2323 else
2324 {
2325 /* Merge the const type qualifier. */
2326 if (TREE_READONLY (newdecl))
2327 TREE_READONLY (olddecl) = 1;
2328 /* Merge the volatile type qualifier. */
2329 if (TREE_THIS_VOLATILE (newdecl))
2330 TREE_THIS_VOLATILE (olddecl) = 1;
2331 }
2332
2333 /* Merge the initialization information. */
2334 if (DECL_INITIAL (newdecl) == NULL_TREE
2335 && DECL_INITIAL (olddecl) != NULL_TREE)
2336 {
2337 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2338 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2339 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2340 {
2341 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
2342 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
2343 }
2344 }
2345
2346 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2347 {
2348 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
2349 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
2350 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
2351 if (DECL_IS_OPERATOR_NEW_P (olddecl))
2352 DECL_SET_IS_OPERATOR_NEW (newdecl, true);
2353 DECL_LOOPING_CONST_OR_PURE_P (newdecl)
2354 |= DECL_LOOPING_CONST_OR_PURE_P (olddecl);
2355
2356 if (merge_attr)
2357 merge_attribute_bits (newdecl, olddecl);
2358 else
2359 {
2360 /* Merge the noreturn bit. */
2361 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
2362 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
2363 TREE_NOTHROW (olddecl) = TREE_NOTHROW (newdecl);
2364 DECL_IS_MALLOC (olddecl) = DECL_IS_MALLOC (newdecl);
2365 DECL_PURE_P (olddecl) = DECL_PURE_P (newdecl);
2366 }
2367 /* Keep the old RTL. */
2368 COPY_DECL_RTL (olddecl, newdecl);
2369 }
2370 else if (VAR_P (newdecl)
2371 && (DECL_SIZE (olddecl) || !DECL_SIZE (newdecl)))
2372 {
2373 /* Keep the old RTL. We cannot keep the old RTL if the old
2374 declaration was for an incomplete object and the new
2375 declaration is not since many attributes of the RTL will
2376 change. */
2377 COPY_DECL_RTL (olddecl, newdecl);
2378 }
2379 }
2380 /* If cannot merge, then use the new type and qualifiers,
2381 and don't preserve the old rtl. */
2382 else
2383 {
2384 /* Clean out any memory we had of the old declaration. */
2385 tree oldstatic = value_member (olddecl, static_aggregates);
2386 if (oldstatic)
2387 TREE_VALUE (oldstatic) = error_mark_node;
2388
2389 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
2390 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
2391 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
2392 TREE_NOTHROW (olddecl) = TREE_NOTHROW (newdecl);
2393 TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
2394 }
2395
2396 /* Merge the storage class information. */
2397 merge_weak (newdecl, olddecl);
2398
2399 DECL_DEFER_OUTPUT (newdecl) |= DECL_DEFER_OUTPUT (olddecl);
2400 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
2401 TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl);
2402 if (! DECL_EXTERNAL (olddecl))
2403 DECL_EXTERNAL (newdecl) = 0;
2404 if (! DECL_COMDAT (olddecl))
2405 DECL_COMDAT (newdecl) = 0;
2406
2407 new_template_info = NULL_TREE;
2408 if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl))
2409 {
2410 bool new_redefines_gnu_inline = false;
2411
2412 if (new_defines_function
2413 && ((DECL_INTERFACE_KNOWN (olddecl)
2414 && TREE_CODE (olddecl) == FUNCTION_DECL)
2415 || (TREE_CODE (olddecl) == TEMPLATE_DECL
2416 && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl))
2417 == FUNCTION_DECL))))
2418 {
2419 tree fn = olddecl;
2420
2421 if (TREE_CODE (fn) == TEMPLATE_DECL)
2422 fn = DECL_TEMPLATE_RESULT (olddecl);
2423
2424 new_redefines_gnu_inline = GNU_INLINE_P (fn) && DECL_INITIAL (fn);
2425 }
2426
2427 if (!new_redefines_gnu_inline)
2428 {
2429 DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl);
2430 DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl);
2431 DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl);
2432 }
2433 DECL_TEMPLATE_INSTANTIATED (newdecl)
2434 |= DECL_TEMPLATE_INSTANTIATED (olddecl);
2435 DECL_ODR_USED (newdecl) |= DECL_ODR_USED (olddecl);
2436
2437 /* If the OLDDECL is an instantiation and/or specialization,
2438 then the NEWDECL must be too. But, it may not yet be marked
2439 as such if the caller has created NEWDECL, but has not yet
2440 figured out that it is a redeclaration. */
2441 if (!DECL_USE_TEMPLATE (newdecl))
2442 DECL_USE_TEMPLATE (newdecl) = DECL_USE_TEMPLATE (olddecl);
2443
2444 /* Don't really know how much of the language-specific
2445 values we should copy from old to new. */
2446 DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl);
2447 DECL_INITIALIZED_IN_CLASS_P (newdecl)
2448 |= DECL_INITIALIZED_IN_CLASS_P (olddecl);
2449
2450 if (LANG_DECL_HAS_MIN (newdecl))
2451 {
2452 DECL_ACCESS (newdecl) = DECL_ACCESS (olddecl);
2453 if (DECL_TEMPLATE_INFO (newdecl))
2454 {
2455 new_template_info = DECL_TEMPLATE_INFO (newdecl);
2456 if (DECL_TEMPLATE_INSTANTIATION (olddecl)
2457 && DECL_TEMPLATE_SPECIALIZATION (newdecl))
2458 /* Remember the presence of explicit specialization args. */
2459 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (olddecl))
2460 = TINFO_USED_TEMPLATE_ID (new_template_info);
2461 }
2462 DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl);
2463 }
2464
2465 if (DECL_DECLARES_FUNCTION_P (newdecl))
2466 {
2467 /* Only functions have these fields. */
2468 DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl);
2469 DECL_BEFRIENDING_CLASSES (newdecl)
2470 = chainon (DECL_BEFRIENDING_CLASSES (newdecl),
2471 DECL_BEFRIENDING_CLASSES (olddecl));
2472 /* DECL_THUNKS is only valid for virtual functions,
2473 otherwise it is a DECL_FRIEND_CONTEXT. */
2474 if (DECL_VIRTUAL_P (newdecl))
2475 SET_DECL_THUNKS (newdecl, DECL_THUNKS (olddecl));
2476 }
2477 else if (VAR_P (newdecl))
2478 {
2479 /* Only variables have this field. */
2480 if (VAR_HAD_UNKNOWN_BOUND (olddecl))
2481 SET_VAR_HAD_UNKNOWN_BOUND (newdecl);
2482 }
2483 }
2484
2485 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2486 {
2487 tree parm;
2488
2489 /* Merge parameter attributes. */
2490 tree oldarg, newarg;
2491 for (oldarg = DECL_ARGUMENTS(olddecl), newarg = DECL_ARGUMENTS(newdecl);
2492 oldarg && newarg;
2493 oldarg = DECL_CHAIN(oldarg), newarg = DECL_CHAIN(newarg))
2494 {
2495 DECL_ATTRIBUTES (newarg)
2496 = (*targetm.merge_decl_attributes) (oldarg, newarg);
2497 DECL_ATTRIBUTES (oldarg) = DECL_ATTRIBUTES (newarg);
2498 }
2499
2500 if (DECL_TEMPLATE_INSTANTIATION (olddecl)
2501 && !DECL_TEMPLATE_INSTANTIATION (newdecl))
2502 {
2503 /* If newdecl is not a specialization, then it is not a
2504 template-related function at all. And that means that we
2505 should have exited above, returning 0. */
2506 gcc_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl));
2507
2508 if (DECL_ODR_USED (olddecl))
2509 /* From [temp.expl.spec]:
2510
2511 If a template, a member template or the member of a class
2512 template is explicitly specialized then that
2513 specialization shall be declared before the first use of
2514 that specialization that would cause an implicit
2515 instantiation to take place, in every translation unit in
2516 which such a use occurs. */
2517 error ("explicit specialization of %qD after first use",
2518 olddecl);
2519
2520 SET_DECL_TEMPLATE_SPECIALIZATION (olddecl);
2521 DECL_COMDAT (newdecl) = (TREE_PUBLIC (newdecl)
2522 && DECL_DECLARED_INLINE_P (newdecl));
2523
2524 /* Don't propagate visibility from the template to the
2525 specialization here. We'll do that in determine_visibility if
2526 appropriate. */
2527 DECL_VISIBILITY_SPECIFIED (olddecl) = 0;
2528
2529 /* [temp.expl.spec/14] We don't inline explicit specialization
2530 just because the primary template says so. */
2531 gcc_assert (!merge_attr);
2532
2533 DECL_DECLARED_INLINE_P (olddecl)
2534 = DECL_DECLARED_INLINE_P (newdecl);
2535
2536 DECL_DISREGARD_INLINE_LIMITS (olddecl)
2537 = DECL_DISREGARD_INLINE_LIMITS (newdecl);
2538
2539 DECL_UNINLINABLE (olddecl) = DECL_UNINLINABLE (newdecl);
2540 }
2541 else if (new_defines_function && DECL_INITIAL (olddecl))
2542 {
2543 /* Never inline re-defined extern inline functions.
2544 FIXME: this could be better handled by keeping both
2545 function as separate declarations. */
2546 DECL_UNINLINABLE (newdecl) = 1;
2547 }
2548 else
2549 {
2550 if (DECL_PENDING_INLINE_P (olddecl))
2551 {
2552 DECL_PENDING_INLINE_P (newdecl) = 1;
2553 DECL_PENDING_INLINE_INFO (newdecl)
2554 = DECL_PENDING_INLINE_INFO (olddecl);
2555 }
2556 else if (DECL_PENDING_INLINE_P (newdecl))
2557 ;
2558 else if (DECL_SAVED_AUTO_RETURN_TYPE (newdecl) == NULL)
2559 DECL_SAVED_AUTO_RETURN_TYPE (newdecl)
2560 = DECL_SAVED_AUTO_RETURN_TYPE (olddecl);
2561
2562 DECL_DECLARED_INLINE_P (newdecl) |= DECL_DECLARED_INLINE_P (olddecl);
2563
2564 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
2565 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
2566
2567 DECL_DISREGARD_INLINE_LIMITS (newdecl)
2568 = DECL_DISREGARD_INLINE_LIMITS (olddecl)
2569 = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
2570 || DECL_DISREGARD_INLINE_LIMITS (olddecl));
2571 }
2572
2573 /* Preserve abstractness on cloned [cd]tors. */
2574 DECL_ABSTRACT_P (newdecl) = DECL_ABSTRACT_P (olddecl);
2575
2576 /* Update newdecl's parms to point at olddecl. */
2577 for (parm = DECL_ARGUMENTS (newdecl); parm;
2578 parm = DECL_CHAIN (parm))
2579 DECL_CONTEXT (parm) = olddecl;
2580
2581 if (! types_match)
2582 {
2583 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
2584 COPY_DECL_ASSEMBLER_NAME (newdecl, olddecl);
2585 COPY_DECL_RTL (newdecl, olddecl);
2586 }
2587 if (! types_match || new_defines_function)
2588 {
2589 /* These need to be copied so that the names are available.
2590 Note that if the types do match, we'll preserve inline
2591 info and other bits, but if not, we won't. */
2592 DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl);
2593 DECL_RESULT (olddecl) = DECL_RESULT (newdecl);
2594 }
2595 /* If redeclaring a builtin function, it stays built in
2596 if newdecl is a gnu_inline definition, or if newdecl is just
2597 a declaration. */
2598 if (fndecl_built_in_p (olddecl)
2599 && (new_defines_function ? GNU_INLINE_P (newdecl) : types_match))
2600 {
2601 copy_decl_built_in_function (newdecl, olddecl);
2602 /* If we're keeping the built-in definition, keep the rtl,
2603 regardless of declaration matches. */
2604 COPY_DECL_RTL (olddecl, newdecl);
2605 if (DECL_BUILT_IN_CLASS (newdecl) == BUILT_IN_NORMAL)
2606 {
2607 enum built_in_function fncode = DECL_FUNCTION_CODE (newdecl);
2608 switch (fncode)
2609 {
2610 /* If a compatible prototype of these builtin functions
2611 is seen, assume the runtime implements it with the
2612 expected semantics. */
2613 case BUILT_IN_STPCPY:
2614 if (builtin_decl_explicit_p (fncode))
2615 set_builtin_decl_implicit_p (fncode, true);
2616 break;
2617 default:
2618 if (builtin_decl_explicit_p (fncode))
2619 set_builtin_decl_declared_p (fncode, true);
2620 break;
2621 }
2622
2623 copy_attributes_to_builtin (newdecl);
2624 }
2625 }
2626 if (new_defines_function)
2627 /* If defining a function declared with other language
2628 linkage, use the previously declared language linkage. */
2629 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
2630 else if (types_match)
2631 {
2632 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
2633 /* Don't clear out the arguments if we're just redeclaring a
2634 function. */
2635 if (DECL_ARGUMENTS (olddecl))
2636 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
2637 }
2638 }
2639 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
2640 NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl);
2641
2642 /* Now preserve various other info from the definition. */
2643 TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl);
2644 TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl);
2645 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
2646 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
2647
2648 /* Warn about conflicting visibility specifications. */
2649 if (DECL_VISIBILITY_SPECIFIED (olddecl)
2650 && DECL_VISIBILITY_SPECIFIED (newdecl)
2651 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
2652 {
2653 auto_diagnostic_group d;
2654 if (warning_at (newdecl_loc, OPT_Wattributes,
2655 "%qD: visibility attribute ignored because it "
2656 "conflicts with previous declaration", newdecl))
2657 inform (olddecl_loc,
2658 "previous declaration of %qD", olddecl);
2659 }
2660 /* Choose the declaration which specified visibility. */
2661 if (DECL_VISIBILITY_SPECIFIED (olddecl))
2662 {
2663 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
2664 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
2665 }
2666 /* Init priority used to be merged from newdecl to olddecl by the memcpy,
2667 so keep this behavior. */
2668 if (VAR_P (newdecl) && DECL_HAS_INIT_PRIORITY_P (newdecl))
2669 {
2670 SET_DECL_INIT_PRIORITY (olddecl, DECL_INIT_PRIORITY (newdecl));
2671 DECL_HAS_INIT_PRIORITY_P (olddecl) = 1;
2672 }
2673 /* Likewise for DECL_ALIGN, DECL_USER_ALIGN and DECL_PACKED. */
2674 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
2675 {
2676 SET_DECL_ALIGN (newdecl, DECL_ALIGN (olddecl));
2677 DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
2678 }
2679 DECL_USER_ALIGN (olddecl) = DECL_USER_ALIGN (newdecl);
2680 if (DECL_WARN_IF_NOT_ALIGN (olddecl)
2681 > DECL_WARN_IF_NOT_ALIGN (newdecl))
2682 SET_DECL_WARN_IF_NOT_ALIGN (newdecl,
2683 DECL_WARN_IF_NOT_ALIGN (olddecl));
2684 if (TREE_CODE (newdecl) == FIELD_DECL)
2685 DECL_PACKED (olddecl) = DECL_PACKED (newdecl);
2686
2687 /* The DECL_LANG_SPECIFIC information in OLDDECL will be replaced
2688 with that from NEWDECL below. */
2689 if (DECL_LANG_SPECIFIC (olddecl))
2690 {
2691 gcc_assert (DECL_LANG_SPECIFIC (olddecl)
2692 != DECL_LANG_SPECIFIC (newdecl));
2693 ggc_free (DECL_LANG_SPECIFIC (olddecl));
2694 }
2695
2696 /* Merge the USED information. */
2697 if (TREE_USED (olddecl))
2698 TREE_USED (newdecl) = 1;
2699 else if (TREE_USED (newdecl))
2700 TREE_USED (olddecl) = 1;
2701 if (VAR_P (newdecl))
2702 {
2703 if (DECL_READ_P (olddecl))
2704 DECL_READ_P (newdecl) = 1;
2705 else if (DECL_READ_P (newdecl))
2706 DECL_READ_P (olddecl) = 1;
2707 }
2708 if (DECL_PRESERVE_P (olddecl))
2709 DECL_PRESERVE_P (newdecl) = 1;
2710 else if (DECL_PRESERVE_P (newdecl))
2711 DECL_PRESERVE_P (olddecl) = 1;
2712
2713 /* Merge the DECL_FUNCTION_VERSIONED information. newdecl will be copied
2714 to olddecl and deleted. */
2715 if (TREE_CODE (newdecl) == FUNCTION_DECL
2716 && DECL_FUNCTION_VERSIONED (olddecl))
2717 {
2718 /* Set the flag for newdecl so that it gets copied to olddecl. */
2719 DECL_FUNCTION_VERSIONED (newdecl) = 1;
2720 /* newdecl will be purged after copying to olddecl and is no longer
2721 a version. */
2722 cgraph_node::delete_function_version_by_decl (newdecl);
2723 }
2724
2725 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2726 {
2727 int function_size;
2728 struct symtab_node *snode = symtab_node::get (olddecl);
2729
2730 function_size = sizeof (struct tree_decl_common);
2731
2732 memcpy ((char *) olddecl + sizeof (struct tree_common),
2733 (char *) newdecl + sizeof (struct tree_common),
2734 function_size - sizeof (struct tree_common));
2735
2736 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2737 (char *) newdecl + sizeof (struct tree_decl_common),
2738 sizeof (struct tree_function_decl) - sizeof (struct tree_decl_common));
2739
2740 /* Preserve symtab node mapping. */
2741 olddecl->decl_with_vis.symtab_node = snode;
2742
2743 if (new_template_info)
2744 /* If newdecl is a template instantiation, it is possible that
2745 the following sequence of events has occurred:
2746
2747 o A friend function was declared in a class template. The
2748 class template was instantiated.
2749
2750 o The instantiation of the friend declaration was
2751 recorded on the instantiation list, and is newdecl.
2752
2753 o Later, however, instantiate_class_template called pushdecl
2754 on the newdecl to perform name injection. But, pushdecl in
2755 turn called duplicate_decls when it discovered that another
2756 declaration of a global function with the same name already
2757 existed.
2758
2759 o Here, in duplicate_decls, we decided to clobber newdecl.
2760
2761 If we're going to do that, we'd better make sure that
2762 olddecl, and not newdecl, is on the list of
2763 instantiations so that if we try to do the instantiation
2764 again we won't get the clobbered declaration. */
2765 reregister_specialization (newdecl,
2766 new_template_info,
2767 olddecl);
2768 }
2769 else
2770 {
2771 size_t size = tree_code_size (TREE_CODE (newdecl));
2772
2773 memcpy ((char *) olddecl + sizeof (struct tree_common),
2774 (char *) newdecl + sizeof (struct tree_common),
2775 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
2776 switch (TREE_CODE (newdecl))
2777 {
2778 case LABEL_DECL:
2779 case VAR_DECL:
2780 case RESULT_DECL:
2781 case PARM_DECL:
2782 case FIELD_DECL:
2783 case TYPE_DECL:
2784 case CONST_DECL:
2785 {
2786 struct symtab_node *snode = NULL;
2787
2788 if (VAR_P (newdecl)
2789 && (TREE_STATIC (olddecl) || TREE_PUBLIC (olddecl)
2790 || DECL_EXTERNAL (olddecl)))
2791 snode = symtab_node::get (olddecl);
2792 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2793 (char *) newdecl + sizeof (struct tree_decl_common),
2794 size - sizeof (struct tree_decl_common)
2795 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
2796 if (VAR_P (newdecl))
2797 olddecl->decl_with_vis.symtab_node = snode;
2798 }
2799 break;
2800 default:
2801 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2802 (char *) newdecl + sizeof (struct tree_decl_common),
2803 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common)
2804 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
2805 break;
2806 }
2807 }
2808
2809 if (VAR_OR_FUNCTION_DECL_P (newdecl))
2810 {
2811 if (DECL_EXTERNAL (olddecl)
2812 || TREE_PUBLIC (olddecl)
2813 || TREE_STATIC (olddecl))
2814 {
2815 /* Merge the section attribute.
2816 We want to issue an error if the sections conflict but that must be
2817 done later in decl_attributes since we are called before attributes
2818 are assigned. */
2819 if (DECL_SECTION_NAME (newdecl) != NULL)
2820 set_decl_section_name (olddecl, DECL_SECTION_NAME (newdecl));
2821
2822 if (DECL_ONE_ONLY (newdecl))
2823 {
2824 struct symtab_node *oldsym, *newsym;
2825 if (TREE_CODE (olddecl) == FUNCTION_DECL)
2826 oldsym = cgraph_node::get_create (olddecl);
2827 else
2828 oldsym = varpool_node::get_create (olddecl);
2829 newsym = symtab_node::get (newdecl);
2830 oldsym->set_comdat_group (newsym->get_comdat_group ());
2831 }
2832 }
2833
2834 if (VAR_P (newdecl)
2835 && CP_DECL_THREAD_LOCAL_P (newdecl))
2836 {
2837 CP_DECL_THREAD_LOCAL_P (olddecl) = true;
2838 if (!processing_template_decl)
2839 set_decl_tls_model (olddecl, DECL_TLS_MODEL (newdecl));
2840 }
2841 }
2842
2843 DECL_UID (olddecl) = olddecl_uid;
2844 if (olddecl_friend)
2845 DECL_FRIEND_P (olddecl) = 1;
2846 if (hidden_friend)
2847 {
2848 DECL_ANTICIPATED (olddecl) = 1;
2849 DECL_HIDDEN_FRIEND_P (olddecl) = 1;
2850 }
2851
2852 /* NEWDECL contains the merged attribute lists.
2853 Update OLDDECL to be the same. */
2854 DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
2855
2856 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
2857 so that encode_section_info has a chance to look at the new decl
2858 flags and attributes. */
2859 if (DECL_RTL_SET_P (olddecl)
2860 && (TREE_CODE (olddecl) == FUNCTION_DECL
2861 || (VAR_P (olddecl)
2862 && TREE_STATIC (olddecl))))
2863 make_decl_rtl (olddecl);
2864
2865 /* The NEWDECL will no longer be needed. Because every out-of-class
2866 declaration of a member results in a call to duplicate_decls,
2867 freeing these nodes represents in a significant savings.
2868
2869 Before releasing the node, be sore to remove function from symbol
2870 table that might have been inserted there to record comdat group.
2871 Be sure to however do not free DECL_STRUCT_FUNCTION because this
2872 structure is shared in between newdecl and oldecl. */
2873 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2874 DECL_STRUCT_FUNCTION (newdecl) = NULL;
2875 if (VAR_OR_FUNCTION_DECL_P (newdecl))
2876 {
2877 struct symtab_node *snode = symtab_node::get (newdecl);
2878 if (snode)
2879 snode->remove ();
2880 }
2881
2882 /* Remove the associated constraints for newdecl, if any, before
2883 reclaiming memory. */
2884 if (flag_concepts)
2885 remove_constraints (newdecl);
2886
2887 ggc_free (newdecl);
2888
2889 return olddecl;
2890 }
2891 \f
2892 /* Return zero if the declaration NEWDECL is valid
2893 when the declaration OLDDECL (assumed to be for the same name)
2894 has already been seen.
2895 Otherwise return an error message format string with a %s
2896 where the identifier should go. */
2897
2898 static const char *
2899 redeclaration_error_message (tree newdecl, tree olddecl)
2900 {
2901 if (TREE_CODE (newdecl) == TYPE_DECL)
2902 {
2903 /* Because C++ can put things into name space for free,
2904 constructs like "typedef struct foo { ... } foo"
2905 would look like an erroneous redeclaration. */
2906 if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl)))
2907 return NULL;
2908 else
2909 return G_("redefinition of %q#D");
2910 }
2911 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
2912 {
2913 /* If this is a pure function, its olddecl will actually be
2914 the original initialization to `0' (which we force to call
2915 abort()). Don't complain about redefinition in this case. */
2916 if (DECL_LANG_SPECIFIC (olddecl) && DECL_PURE_VIRTUAL_P (olddecl)
2917 && DECL_INITIAL (olddecl) == NULL_TREE)
2918 return NULL;
2919
2920 /* If both functions come from different namespaces, this is not
2921 a redeclaration - this is a conflict with a used function. */
2922 if (DECL_NAMESPACE_SCOPE_P (olddecl)
2923 && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl)
2924 && ! decls_match (olddecl, newdecl))
2925 return G_("%qD conflicts with used function");
2926
2927 /* We'll complain about linkage mismatches in
2928 warn_extern_redeclared_static. */
2929
2930 /* Defining the same name twice is no good. */
2931 if (decl_defined_p (olddecl)
2932 && decl_defined_p (newdecl))
2933 {
2934 if (DECL_NAME (olddecl) == NULL_TREE)
2935 return G_("%q#D not declared in class");
2936 else if (!GNU_INLINE_P (olddecl)
2937 || GNU_INLINE_P (newdecl))
2938 return G_("redefinition of %q#D");
2939 }
2940
2941 if (DECL_DECLARED_INLINE_P (olddecl) && DECL_DECLARED_INLINE_P (newdecl))
2942 {
2943 bool olda = GNU_INLINE_P (olddecl);
2944 bool newa = GNU_INLINE_P (newdecl);
2945
2946 if (olda != newa)
2947 {
2948 if (newa)
2949 return G_("%q+D redeclared inline with "
2950 "%<gnu_inline%> attribute");
2951 else
2952 return G_("%q+D redeclared inline without "
2953 "%<gnu_inline%> attribute");
2954 }
2955 }
2956
2957 check_abi_tag_redeclaration
2958 (olddecl, lookup_attribute ("abi_tag", DECL_ATTRIBUTES (olddecl)),
2959 lookup_attribute ("abi_tag", DECL_ATTRIBUTES (newdecl)));
2960
2961 return NULL;
2962 }
2963 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2964 {
2965 tree nt, ot;
2966
2967 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
2968 {
2969 if (COMPLETE_TYPE_P (TREE_TYPE (newdecl))
2970 && COMPLETE_TYPE_P (TREE_TYPE (olddecl)))
2971 return G_("redefinition of %q#D");
2972 return NULL;
2973 }
2974
2975 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == CONCEPT_DECL)
2976 return G_("redefinition of %q#D");
2977
2978 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) != FUNCTION_DECL
2979 || (DECL_TEMPLATE_RESULT (newdecl)
2980 == DECL_TEMPLATE_RESULT (olddecl)))
2981 return NULL;
2982
2983 nt = DECL_TEMPLATE_RESULT (newdecl);
2984 if (DECL_TEMPLATE_INFO (nt))
2985 nt = DECL_TEMPLATE_RESULT (template_for_substitution (nt));
2986 ot = DECL_TEMPLATE_RESULT (olddecl);
2987 if (DECL_TEMPLATE_INFO (ot))
2988 ot = DECL_TEMPLATE_RESULT (template_for_substitution (ot));
2989 if (DECL_INITIAL (nt) && DECL_INITIAL (ot)
2990 && (!GNU_INLINE_P (ot) || GNU_INLINE_P (nt)))
2991 return G_("redefinition of %q#D");
2992
2993 if (DECL_DECLARED_INLINE_P (ot) && DECL_DECLARED_INLINE_P (nt))
2994 {
2995 bool olda = GNU_INLINE_P (ot);
2996 bool newa = GNU_INLINE_P (nt);
2997
2998 if (olda != newa)
2999 {
3000 if (newa)
3001 return G_("%q+D redeclared inline with "
3002 "%<gnu_inline%> attribute");
3003 else
3004 return G_("%q+D redeclared inline without "
3005 "%<gnu_inline%> attribute");
3006 }
3007 }
3008
3009 /* Core issue #226 (C++0x):
3010
3011 If a friend function template declaration specifies a
3012 default template-argument, that declaration shall be a
3013 definition and shall be the only declaration of the
3014 function template in the translation unit. */
3015 if ((cxx_dialect != cxx98)
3016 && TREE_CODE (ot) == FUNCTION_DECL && DECL_FRIEND_P (ot)
3017 && !check_default_tmpl_args (nt, DECL_TEMPLATE_PARMS (newdecl),
3018 /*is_primary=*/true,
3019 /*is_partial=*/false,
3020 /*is_friend_decl=*/2))
3021 return G_("redeclaration of friend %q#D "
3022 "may not have default template arguments");
3023
3024 return NULL;
3025 }
3026 else if (VAR_P (newdecl)
3027 && CP_DECL_THREAD_LOCAL_P (newdecl) != CP_DECL_THREAD_LOCAL_P (olddecl)
3028 && (! DECL_LANG_SPECIFIC (olddecl)
3029 || ! CP_DECL_THREADPRIVATE_P (olddecl)
3030 || CP_DECL_THREAD_LOCAL_P (newdecl)))
3031 {
3032 /* Only variables can be thread-local, and all declarations must
3033 agree on this property. */
3034 if (CP_DECL_THREAD_LOCAL_P (newdecl))
3035 return G_("thread-local declaration of %q#D follows "
3036 "non-thread-local declaration");
3037 else
3038 return G_("non-thread-local declaration of %q#D follows "
3039 "thread-local declaration");
3040 }
3041 else if (toplevel_bindings_p () || DECL_NAMESPACE_SCOPE_P (newdecl))
3042 {
3043 /* The objects have been declared at namespace scope. If either
3044 is a member of an anonymous union, then this is an invalid
3045 redeclaration. For example:
3046
3047 int i;
3048 union { int i; };
3049
3050 is invalid. */
3051 if ((VAR_P (newdecl) && DECL_ANON_UNION_VAR_P (newdecl))
3052 || (VAR_P (olddecl) && DECL_ANON_UNION_VAR_P (olddecl)))
3053 return G_("redeclaration of %q#D");
3054 /* If at least one declaration is a reference, there is no
3055 conflict. For example:
3056
3057 int i = 3;
3058 extern int i;
3059
3060 is valid. */
3061 if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
3062 return NULL;
3063
3064 /* Static data member declared outside a class definition
3065 if the variable is defined within the class with constexpr
3066 specifier is declaration rather than definition (and
3067 deprecated). */
3068 if (cxx_dialect >= cxx17
3069 && VAR_P (olddecl)
3070 && DECL_CLASS_SCOPE_P (olddecl)
3071 && DECL_DECLARED_CONSTEXPR_P (olddecl)
3072 && !DECL_INITIAL (newdecl))
3073 {
3074 DECL_EXTERNAL (newdecl) = 1;
3075 /* For now, only warn with explicit -Wdeprecated. */
3076 if (global_options_set.x_warn_deprecated)
3077 {
3078 auto_diagnostic_group d;
3079 if (warning_at (DECL_SOURCE_LOCATION (newdecl), OPT_Wdeprecated,
3080 "redundant redeclaration of %<constexpr%> "
3081 "static data member %qD", newdecl))
3082 inform (DECL_SOURCE_LOCATION (olddecl),
3083 "previous declaration of %qD", olddecl);
3084 }
3085 return NULL;
3086 }
3087
3088 /* Reject two definitions. */
3089 return G_("redefinition of %q#D");
3090 }
3091 else
3092 {
3093 /* Objects declared with block scope: */
3094 /* Reject two definitions, and reject a definition
3095 together with an external reference. */
3096 if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl)))
3097 return G_("redeclaration of %q#D");
3098 return NULL;
3099 }
3100 }
3101 \f
3102
3103 /* Hash and equality functions for the named_label table. */
3104
3105 hashval_t
3106 named_label_hash::hash (const value_type entry)
3107 {
3108 return IDENTIFIER_HASH_VALUE (entry->name);
3109 }
3110
3111 bool
3112 named_label_hash::equal (const value_type entry, compare_type name)
3113 {
3114 return name == entry->name;
3115 }
3116
3117 /* Look for a label named ID in the current function. If one cannot
3118 be found, create one. Return the named_label_entry, or NULL on
3119 failure. */
3120
3121 static named_label_entry *
3122 lookup_label_1 (tree id, bool making_local_p)
3123 {
3124 /* You can't use labels at global scope. */
3125 if (current_function_decl == NULL_TREE)
3126 {
3127 error ("label %qE referenced outside of any function", id);
3128 return NULL;
3129 }
3130
3131 if (!named_labels)
3132 named_labels = hash_table<named_label_hash>::create_ggc (13);
3133
3134 hashval_t hash = IDENTIFIER_HASH_VALUE (id);
3135 named_label_entry **slot
3136 = named_labels->find_slot_with_hash (id, hash, INSERT);
3137 named_label_entry *old = *slot;
3138
3139 if (old && old->label_decl)
3140 {
3141 if (!making_local_p)
3142 return old;
3143
3144 if (old->binding_level == current_binding_level)
3145 {
3146 error ("local label %qE conflicts with existing label", id);
3147 inform (DECL_SOURCE_LOCATION (old->label_decl), "previous label");
3148 return NULL;
3149 }
3150 }
3151
3152 /* We are making a new decl, create or reuse the named_label_entry */
3153 named_label_entry *ent = NULL;
3154 if (old && !old->label_decl)
3155 ent = old;
3156 else
3157 {
3158 ent = ggc_cleared_alloc<named_label_entry> ();
3159 ent->name = id;
3160 ent->outer = old;
3161 *slot = ent;
3162 }
3163
3164 /* Now create the LABEL_DECL. */
3165 tree decl = build_decl (input_location, LABEL_DECL, id, void_type_node);
3166
3167 DECL_CONTEXT (decl) = current_function_decl;
3168 SET_DECL_MODE (decl, VOIDmode);
3169 if (making_local_p)
3170 {
3171 C_DECLARED_LABEL_FLAG (decl) = true;
3172 DECL_CHAIN (decl) = current_binding_level->names;
3173 current_binding_level->names = decl;
3174 }
3175
3176 ent->label_decl = decl;
3177
3178 return ent;
3179 }
3180
3181 /* Wrapper for lookup_label_1. */
3182
3183 tree
3184 lookup_label (tree id)
3185 {
3186 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3187 named_label_entry *ent = lookup_label_1 (id, false);
3188 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3189 return ent ? ent->label_decl : NULL_TREE;
3190 }
3191
3192 tree
3193 declare_local_label (tree id)
3194 {
3195 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3196 named_label_entry *ent = lookup_label_1 (id, true);
3197 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3198 return ent ? ent->label_decl : NULL_TREE;
3199 }
3200
3201 /* Returns nonzero if it is ill-formed to jump past the declaration of
3202 DECL. Returns 2 if it's also a real problem. */
3203
3204 static int
3205 decl_jump_unsafe (tree decl)
3206 {
3207 /* [stmt.dcl]/3: A program that jumps from a point where a local variable
3208 with automatic storage duration is not in scope to a point where it is
3209 in scope is ill-formed unless the variable has scalar type, class type
3210 with a trivial default constructor and a trivial destructor, a
3211 cv-qualified version of one of these types, or an array of one of the
3212 preceding types and is declared without an initializer (8.5). */
3213 tree type = TREE_TYPE (decl);
3214
3215 if (!VAR_P (decl) || TREE_STATIC (decl)
3216 || type == error_mark_node)
3217 return 0;
3218
3219 if (DECL_NONTRIVIALLY_INITIALIZED_P (decl)
3220 || variably_modified_type_p (type, NULL_TREE))
3221 return 2;
3222
3223 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3224 return 1;
3225
3226 return 0;
3227 }
3228
3229 /* A subroutine of check_previous_goto_1 and check_goto to identify a branch
3230 to the user. */
3231
3232 static bool
3233 identify_goto (tree decl, location_t loc, const location_t *locus,
3234 diagnostic_t diag_kind)
3235 {
3236 bool complained
3237 = emit_diagnostic (diag_kind, loc, 0,
3238 decl ? N_("jump to label %qD")
3239 : N_("jump to case label"), decl);
3240 if (complained && locus)
3241 inform (*locus, " from here");
3242 return complained;
3243 }
3244
3245 /* Check that a single previously seen jump to a newly defined label
3246 is OK. DECL is the LABEL_DECL or 0; LEVEL is the binding_level for
3247 the jump context; NAMES are the names in scope in LEVEL at the jump
3248 context; LOCUS is the source position of the jump or 0. Returns
3249 true if all is well. */
3250
3251 static bool
3252 check_previous_goto_1 (tree decl, cp_binding_level* level, tree names,
3253 bool exited_omp, const location_t *locus)
3254 {
3255 cp_binding_level *b;
3256 bool complained = false;
3257 int identified = 0;
3258 bool saw_eh = false, saw_omp = false, saw_tm = false, saw_cxif = false;
3259
3260 if (exited_omp)
3261 {
3262 complained = identify_goto (decl, input_location, locus, DK_ERROR);
3263 if (complained)
3264 inform (input_location, " exits OpenMP structured block");
3265 saw_omp = true;
3266 identified = 2;
3267 }
3268
3269 for (b = current_binding_level; b ; b = b->level_chain)
3270 {
3271 tree new_decls, old_decls = (b == level ? names : NULL_TREE);
3272
3273 for (new_decls = b->names; new_decls != old_decls;
3274 new_decls = (DECL_P (new_decls) ? DECL_CHAIN (new_decls)
3275 : TREE_CHAIN (new_decls)))
3276 {
3277 int problem = decl_jump_unsafe (new_decls);
3278 if (! problem)
3279 continue;
3280
3281 if (!identified)
3282 {
3283 complained = identify_goto (decl, input_location, locus,
3284 problem > 1
3285 ? DK_ERROR : DK_PERMERROR);
3286 identified = 1;
3287 }
3288 if (complained)
3289 {
3290 if (problem > 1)
3291 inform (DECL_SOURCE_LOCATION (new_decls),
3292 " crosses initialization of %q#D", new_decls);
3293 else
3294 inform (DECL_SOURCE_LOCATION (new_decls),
3295 " enters scope of %q#D, which has "
3296 "non-trivial destructor", new_decls);
3297 }
3298 }
3299
3300 if (b == level)
3301 break;
3302
3303 const char *inf = NULL;
3304 location_t loc = input_location;
3305 switch (b->kind)
3306 {
3307 case sk_try:
3308 if (!saw_eh)
3309 inf = G_(" enters %<try%> block");
3310 saw_eh = true;
3311 break;
3312
3313 case sk_catch:
3314 if (!saw_eh)
3315 inf = G_(" enters %<catch%> block");
3316 saw_eh = true;
3317 break;
3318
3319 case sk_omp:
3320 if (!saw_omp)
3321 inf = G_(" enters OpenMP structured block");
3322 saw_omp = true;
3323 break;
3324
3325 case sk_transaction:
3326 if (!saw_tm)
3327 inf = G_(" enters synchronized or atomic statement");
3328 saw_tm = true;
3329 break;
3330
3331 case sk_block:
3332 if (!saw_cxif && level_for_constexpr_if (b->level_chain))
3333 {
3334 inf = G_(" enters %<constexpr if%> statement");
3335 loc = EXPR_LOCATION (b->level_chain->this_entity);
3336 saw_cxif = true;
3337 }
3338 break;
3339
3340 default:
3341 break;
3342 }
3343
3344 if (inf)
3345 {
3346 if (identified < 2)
3347 complained = identify_goto (decl, input_location, locus, DK_ERROR);
3348 identified = 2;
3349 if (complained)
3350 inform (loc, inf);
3351 }
3352 }
3353
3354 return !identified;
3355 }
3356
3357 static void
3358 check_previous_goto (tree decl, struct named_label_use_entry *use)
3359 {
3360 check_previous_goto_1 (decl, use->binding_level,
3361 use->names_in_scope, use->in_omp_scope,
3362 &use->o_goto_locus);
3363 }
3364
3365 static bool
3366 check_switch_goto (cp_binding_level* level)
3367 {
3368 return check_previous_goto_1 (NULL_TREE, level, level->names, false, NULL);
3369 }
3370
3371 /* Check that a new jump to a label DECL is OK. Called by
3372 finish_goto_stmt. */
3373
3374 void
3375 check_goto (tree decl)
3376 {
3377 /* We can't know where a computed goto is jumping.
3378 So we assume that it's OK. */
3379 if (TREE_CODE (decl) != LABEL_DECL)
3380 return;
3381
3382 /* We didn't record any information about this label when we created it,
3383 and there's not much point since it's trivial to analyze as a return. */
3384 if (decl == cdtor_label)
3385 return;
3386
3387 hashval_t hash = IDENTIFIER_HASH_VALUE (DECL_NAME (decl));
3388 named_label_entry **slot
3389 = named_labels->find_slot_with_hash (DECL_NAME (decl), hash, NO_INSERT);
3390 named_label_entry *ent = *slot;
3391
3392 /* If the label hasn't been defined yet, defer checking. */
3393 if (! DECL_INITIAL (decl))
3394 {
3395 /* Don't bother creating another use if the last goto had the
3396 same data, and will therefore create the same set of errors. */
3397 if (ent->uses
3398 && ent->uses->names_in_scope == current_binding_level->names)
3399 return;
3400
3401 named_label_use_entry *new_use
3402 = ggc_alloc<named_label_use_entry> ();
3403 new_use->binding_level = current_binding_level;
3404 new_use->names_in_scope = current_binding_level->names;
3405 new_use->o_goto_locus = input_location;
3406 new_use->in_omp_scope = false;
3407
3408 new_use->next = ent->uses;
3409 ent->uses = new_use;
3410 return;
3411 }
3412
3413 bool saw_catch = false, complained = false;
3414 int identified = 0;
3415 tree bad;
3416 unsigned ix;
3417
3418 if (ent->in_try_scope || ent->in_catch_scope || ent->in_transaction_scope
3419 || ent->in_constexpr_if
3420 || ent->in_omp_scope || !vec_safe_is_empty (ent->bad_decls))
3421 {
3422 diagnostic_t diag_kind = DK_PERMERROR;
3423 if (ent->in_try_scope || ent->in_catch_scope || ent->in_constexpr_if
3424 || ent->in_transaction_scope || ent->in_omp_scope)
3425 diag_kind = DK_ERROR;
3426 complained = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
3427 &input_location, diag_kind);
3428 identified = 1 + (diag_kind == DK_ERROR);
3429 }
3430
3431 FOR_EACH_VEC_SAFE_ELT (ent->bad_decls, ix, bad)
3432 {
3433 int u = decl_jump_unsafe (bad);
3434
3435 if (u > 1 && DECL_ARTIFICIAL (bad))
3436 {
3437 /* Can't skip init of __exception_info. */
3438 if (identified == 1)
3439 {
3440 complained = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
3441 &input_location, DK_ERROR);
3442 identified = 2;
3443 }
3444 if (complained)
3445 inform (DECL_SOURCE_LOCATION (bad), " enters %<catch%> block");
3446 saw_catch = true;
3447 }
3448 else if (complained)
3449 {
3450 if (u > 1)
3451 inform (DECL_SOURCE_LOCATION (bad),
3452 " skips initialization of %q#D", bad);
3453 else
3454 inform (DECL_SOURCE_LOCATION (bad),
3455 " enters scope of %q#D which has "
3456 "non-trivial destructor", bad);
3457 }
3458 }
3459
3460 if (complained)
3461 {
3462 if (ent->in_try_scope)
3463 inform (input_location, " enters %<try%> block");
3464 else if (ent->in_catch_scope && !saw_catch)
3465 inform (input_location, " enters %<catch%> block");
3466 else if (ent->in_transaction_scope)
3467 inform (input_location, " enters synchronized or atomic statement");
3468 else if (ent->in_constexpr_if)
3469 inform (input_location, " enters %<constexpr if%> statement");
3470 }
3471
3472 if (ent->in_omp_scope)
3473 {
3474 if (complained)
3475 inform (input_location, " enters OpenMP structured block");
3476 }
3477 else if (flag_openmp)
3478 for (cp_binding_level *b = current_binding_level; b ; b = b->level_chain)
3479 {
3480 if (b == ent->binding_level)
3481 break;
3482 if (b->kind == sk_omp)
3483 {
3484 if (identified < 2)
3485 {
3486 complained = identify_goto (decl,
3487 DECL_SOURCE_LOCATION (decl),
3488 &input_location, DK_ERROR);
3489 identified = 2;
3490 }
3491 if (complained)
3492 inform (input_location, " exits OpenMP structured block");
3493 break;
3494 }
3495 }
3496 }
3497
3498 /* Check that a return is ok wrt OpenMP structured blocks.
3499 Called by finish_return_stmt. Returns true if all is well. */
3500
3501 bool
3502 check_omp_return (void)
3503 {
3504 for (cp_binding_level *b = current_binding_level; b ; b = b->level_chain)
3505 if (b->kind == sk_omp)
3506 {
3507 error ("invalid exit from OpenMP structured block");
3508 return false;
3509 }
3510 else if (b->kind == sk_function_parms)
3511 break;
3512 return true;
3513 }
3514
3515 /* Define a label, specifying the location in the source file.
3516 Return the LABEL_DECL node for the label. */
3517
3518 static tree
3519 define_label_1 (location_t location, tree name)
3520 {
3521 /* After labels, make any new cleanups in the function go into their
3522 own new (temporary) binding contour. */
3523 for (cp_binding_level *p = current_binding_level;
3524 p->kind != sk_function_parms;
3525 p = p->level_chain)
3526 p->more_cleanups_ok = 0;
3527
3528 named_label_entry *ent = lookup_label_1 (name, false);
3529 tree decl = ent->label_decl;
3530
3531 if (DECL_INITIAL (decl) != NULL_TREE)
3532 {
3533 error ("duplicate label %qD", decl);
3534 return error_mark_node;
3535 }
3536 else
3537 {
3538 /* Mark label as having been defined. */
3539 DECL_INITIAL (decl) = error_mark_node;
3540 /* Say where in the source. */
3541 DECL_SOURCE_LOCATION (decl) = location;
3542
3543 ent->binding_level = current_binding_level;
3544 ent->names_in_scope = current_binding_level->names;
3545
3546 for (named_label_use_entry *use = ent->uses; use; use = use->next)
3547 check_previous_goto (decl, use);
3548 ent->uses = NULL;
3549 }
3550
3551 return decl;
3552 }
3553
3554 /* Wrapper for define_label_1. */
3555
3556 tree
3557 define_label (location_t location, tree name)
3558 {
3559 bool running = timevar_cond_start (TV_NAME_LOOKUP);
3560 tree ret = define_label_1 (location, name);
3561 timevar_cond_stop (TV_NAME_LOOKUP, running);
3562 return ret;
3563 }
3564
3565
3566 struct cp_switch
3567 {
3568 cp_binding_level *level;
3569 struct cp_switch *next;
3570 /* The SWITCH_STMT being built. */
3571 tree switch_stmt;
3572 /* A splay-tree mapping the low element of a case range to the high
3573 element, or NULL_TREE if there is no high element. Used to
3574 determine whether or not a new case label duplicates an old case
3575 label. We need a tree, rather than simply a hash table, because
3576 of the GNU case range extension. */
3577 splay_tree cases;
3578 /* Remember whether a default: case label has been seen. */
3579 bool has_default_p;
3580 /* Remember whether a BREAK_STMT has been seen in this SWITCH_STMT. */
3581 bool break_stmt_seen_p;
3582 /* Set if inside of {FOR,DO,WHILE}_BODY nested inside of a switch,
3583 where BREAK_STMT doesn't belong to the SWITCH_STMT. */
3584 bool in_loop_body_p;
3585 };
3586
3587 /* A stack of the currently active switch statements. The innermost
3588 switch statement is on the top of the stack. There is no need to
3589 mark the stack for garbage collection because it is only active
3590 during the processing of the body of a function, and we never
3591 collect at that point. */
3592
3593 static struct cp_switch *switch_stack;
3594
3595 /* Called right after a switch-statement condition is parsed.
3596 SWITCH_STMT is the switch statement being parsed. */
3597
3598 void
3599 push_switch (tree switch_stmt)
3600 {
3601 struct cp_switch *p = XNEW (struct cp_switch);
3602 p->level = current_binding_level;
3603 p->next = switch_stack;
3604 p->switch_stmt = switch_stmt;
3605 p->cases = splay_tree_new (case_compare, NULL, NULL);
3606 p->has_default_p = false;
3607 p->break_stmt_seen_p = false;
3608 p->in_loop_body_p = false;
3609 switch_stack = p;
3610 }
3611
3612 void
3613 pop_switch (void)
3614 {
3615 struct cp_switch *cs = switch_stack;
3616 location_t switch_location;
3617
3618 /* Emit warnings as needed. */
3619 switch_location = cp_expr_loc_or_input_loc (cs->switch_stmt);
3620 const bool bool_cond_p
3621 = (SWITCH_STMT_TYPE (cs->switch_stmt)
3622 && TREE_CODE (SWITCH_STMT_TYPE (cs->switch_stmt)) == BOOLEAN_TYPE);
3623 if (!processing_template_decl)
3624 c_do_switch_warnings (cs->cases, switch_location,
3625 SWITCH_STMT_TYPE (cs->switch_stmt),
3626 SWITCH_STMT_COND (cs->switch_stmt), bool_cond_p);
3627
3628 /* For the benefit of block_may_fallthru remember if the switch body
3629 case labels cover all possible values and if there are break; stmts. */
3630 if (cs->has_default_p
3631 || (!processing_template_decl
3632 && c_switch_covers_all_cases_p (cs->cases,
3633 SWITCH_STMT_TYPE (cs->switch_stmt))))
3634 SWITCH_STMT_ALL_CASES_P (cs->switch_stmt) = 1;
3635 if (!cs->break_stmt_seen_p)
3636 SWITCH_STMT_NO_BREAK_P (cs->switch_stmt) = 1;
3637 gcc_assert (!cs->in_loop_body_p);
3638 splay_tree_delete (cs->cases);
3639 switch_stack = switch_stack->next;
3640 free (cs);
3641 }
3642
3643 /* Note that a BREAK_STMT is about to be added. If it is inside of
3644 a SWITCH_STMT and not inside of a loop body inside of it, note
3645 in switch_stack we've seen a BREAK_STMT. */
3646
3647 void
3648 note_break_stmt (void)
3649 {
3650 if (switch_stack && !switch_stack->in_loop_body_p)
3651 switch_stack->break_stmt_seen_p = true;
3652 }
3653
3654 /* Note the start of processing of an iteration statement's body.
3655 The note_break_stmt function will do nothing while processing it.
3656 Return a flag that should be passed to note_iteration_stmt_body_end. */
3657
3658 bool
3659 note_iteration_stmt_body_start (void)
3660 {
3661 if (!switch_stack)
3662 return false;
3663 bool ret = switch_stack->in_loop_body_p;
3664 switch_stack->in_loop_body_p = true;
3665 return ret;
3666 }
3667
3668 /* Note the end of processing of an iteration statement's body. */
3669
3670 void
3671 note_iteration_stmt_body_end (bool prev)
3672 {
3673 if (switch_stack)
3674 switch_stack->in_loop_body_p = prev;
3675 }
3676
3677 /* Convert a case constant VALUE in a switch to the type TYPE of the switch
3678 condition. Note that if TYPE and VALUE are already integral we don't
3679 really do the conversion because the language-independent
3680 warning/optimization code will work better that way. */
3681
3682 static tree
3683 case_conversion (tree type, tree value)
3684 {
3685 if (value == NULL_TREE)
3686 return value;
3687
3688 value = mark_rvalue_use (value);
3689
3690 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
3691 type = type_promotes_to (type);
3692
3693 tree ovalue = value;
3694 /* The constant-expression VALUE shall be a converted constant expression
3695 of the adjusted type of the switch condition, which doesn't allow
3696 narrowing conversions. */
3697 value = build_converted_constant_expr (type, value, tf_warning_or_error);
3698
3699 if (cxx_dialect >= cxx11
3700 && (SCOPED_ENUM_P (type)
3701 || !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (ovalue))))
3702 /* Use the converted value. */;
3703 else
3704 /* The already integral case. */
3705 value = ovalue;
3706
3707 return cxx_constant_value (value);
3708 }
3709
3710 /* Note that we've seen a definition of a case label, and complain if this
3711 is a bad place for one. */
3712
3713 tree
3714 finish_case_label (location_t loc, tree low_value, tree high_value)
3715 {
3716 tree cond, r;
3717 cp_binding_level *p;
3718 tree type;
3719
3720 if (low_value == NULL_TREE && high_value == NULL_TREE)
3721 switch_stack->has_default_p = true;
3722
3723 if (processing_template_decl)
3724 {
3725 tree label;
3726
3727 /* For templates, just add the case label; we'll do semantic
3728 analysis at instantiation-time. */
3729 label = build_decl (loc, LABEL_DECL, NULL_TREE, void_type_node);
3730 return add_stmt (build_case_label (low_value, high_value, label));
3731 }
3732
3733 /* Find the condition on which this switch statement depends. */
3734 cond = SWITCH_STMT_COND (switch_stack->switch_stmt);
3735 if (cond && TREE_CODE (cond) == TREE_LIST)
3736 cond = TREE_VALUE (cond);
3737
3738 if (!check_switch_goto (switch_stack->level))
3739 return error_mark_node;
3740
3741 type = SWITCH_STMT_TYPE (switch_stack->switch_stmt);
3742 if (type == error_mark_node)
3743 return error_mark_node;
3744
3745 low_value = case_conversion (type, low_value);
3746 high_value = case_conversion (type, high_value);
3747
3748 r = c_add_case_label (loc, switch_stack->cases, cond, low_value, high_value);
3749
3750 /* After labels, make any new cleanups in the function go into their
3751 own new (temporary) binding contour. */
3752 for (p = current_binding_level;
3753 p->kind != sk_function_parms;
3754 p = p->level_chain)
3755 p->more_cleanups_ok = 0;
3756
3757 return r;
3758 }
3759 \f
3760 struct typename_info {
3761 tree scope;
3762 tree name;
3763 tree template_id;
3764 bool enum_p;
3765 bool class_p;
3766 };
3767
3768 struct typename_hasher : ggc_ptr_hash<tree_node>
3769 {
3770 typedef typename_info *compare_type;
3771
3772 /* Hash a TYPENAME_TYPE. */
3773
3774 static hashval_t
3775 hash (tree t)
3776 {
3777 hashval_t hash;
3778
3779 hash = (htab_hash_pointer (TYPE_CONTEXT (t))
3780 ^ htab_hash_pointer (TYPE_IDENTIFIER (t)));
3781
3782 return hash;
3783 }
3784
3785 /* Compare two TYPENAME_TYPEs. */
3786
3787 static bool
3788 equal (tree t1, const typename_info *t2)
3789 {
3790 return (TYPE_IDENTIFIER (t1) == t2->name
3791 && TYPE_CONTEXT (t1) == t2->scope
3792 && TYPENAME_TYPE_FULLNAME (t1) == t2->template_id
3793 && TYPENAME_IS_ENUM_P (t1) == t2->enum_p
3794 && TYPENAME_IS_CLASS_P (t1) == t2->class_p);
3795 }
3796 };
3797
3798 /* Build a TYPENAME_TYPE. If the type is `typename T::t', CONTEXT is
3799 the type of `T', NAME is the IDENTIFIER_NODE for `t'.
3800
3801 Returns the new TYPENAME_TYPE. */
3802
3803 static GTY (()) hash_table<typename_hasher> *typename_htab;
3804
3805 tree
3806 build_typename_type (tree context, tree name, tree fullname,
3807 enum tag_types tag_type)
3808 {
3809 tree t;
3810 tree d;
3811 typename_info ti;
3812 tree *e;
3813 hashval_t hash;
3814
3815 if (typename_htab == NULL)
3816 typename_htab = hash_table<typename_hasher>::create_ggc (61);
3817
3818 ti.scope = FROB_CONTEXT (context);
3819 ti.name = name;
3820 ti.template_id = fullname;
3821 ti.enum_p = tag_type == enum_type;
3822 ti.class_p = (tag_type == class_type
3823 || tag_type == record_type
3824 || tag_type == union_type);
3825 hash = (htab_hash_pointer (ti.scope)
3826 ^ htab_hash_pointer (ti.name));
3827
3828 /* See if we already have this type. */
3829 e = typename_htab->find_slot_with_hash (&ti, hash, INSERT);
3830 if (*e)
3831 t = *e;
3832 else
3833 {
3834 /* Build the TYPENAME_TYPE. */
3835 t = cxx_make_type (TYPENAME_TYPE);
3836 TYPE_CONTEXT (t) = ti.scope;
3837 TYPENAME_TYPE_FULLNAME (t) = ti.template_id;
3838 TYPENAME_IS_ENUM_P (t) = ti.enum_p;
3839 TYPENAME_IS_CLASS_P (t) = ti.class_p;
3840
3841 /* Build the corresponding TYPE_DECL. */
3842 d = build_decl (input_location, TYPE_DECL, name, t);
3843 TYPE_NAME (TREE_TYPE (d)) = d;
3844 TYPE_STUB_DECL (TREE_TYPE (d)) = d;
3845 DECL_CONTEXT (d) = FROB_CONTEXT (context);
3846 DECL_ARTIFICIAL (d) = 1;
3847
3848 /* Store it in the hash table. */
3849 *e = t;
3850
3851 /* TYPENAME_TYPEs must always be compared structurally, because
3852 they may or may not resolve down to another type depending on
3853 the currently open classes. */
3854 SET_TYPE_STRUCTURAL_EQUALITY (t);
3855 }
3856
3857 return t;
3858 }
3859
3860 /* Resolve `typename CONTEXT::NAME'. TAG_TYPE indicates the tag
3861 provided to name the type. Returns an appropriate type, unless an
3862 error occurs, in which case error_mark_node is returned. If we
3863 locate a non-artificial TYPE_DECL and TF_KEEP_TYPE_DECL is set, we
3864 return that, rather than the _TYPE it corresponds to, in other
3865 cases we look through the type decl. If TF_ERROR is set, complain
3866 about errors, otherwise be quiet. */
3867
3868 tree
3869 make_typename_type (tree context, tree name, enum tag_types tag_type,
3870 tsubst_flags_t complain)
3871 {
3872 tree fullname;
3873 tree t;
3874 bool want_template;
3875
3876 if (name == error_mark_node
3877 || context == NULL_TREE
3878 || context == error_mark_node)
3879 return error_mark_node;
3880
3881 if (TYPE_P (name))
3882 {
3883 if (!(TYPE_LANG_SPECIFIC (name)
3884 && (CLASSTYPE_IS_TEMPLATE (name)
3885 || CLASSTYPE_USE_TEMPLATE (name))))
3886 name = TYPE_IDENTIFIER (name);
3887 else
3888 /* Create a TEMPLATE_ID_EXPR for the type. */
3889 name = build_nt (TEMPLATE_ID_EXPR,
3890 CLASSTYPE_TI_TEMPLATE (name),
3891 CLASSTYPE_TI_ARGS (name));
3892 }
3893 else if (TREE_CODE (name) == TYPE_DECL)
3894 name = DECL_NAME (name);
3895
3896 fullname = name;
3897
3898 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3899 {
3900 name = TREE_OPERAND (name, 0);
3901 if (DECL_TYPE_TEMPLATE_P (name))
3902 name = TREE_OPERAND (fullname, 0) = DECL_NAME (name);
3903 if (TREE_CODE (name) != IDENTIFIER_NODE)
3904 {
3905 if (complain & tf_error)
3906 error ("%qD is not a type", name);
3907 return error_mark_node;
3908 }
3909 }
3910 if (TREE_CODE (name) == TEMPLATE_DECL)
3911 {
3912 if (complain & tf_error)
3913 error ("%qD used without template arguments", name);
3914 return error_mark_node;
3915 }
3916 gcc_assert (identifier_p (name));
3917 gcc_assert (TYPE_P (context));
3918
3919 if (TREE_CODE (context) == TYPE_PACK_EXPANSION)
3920 /* This can happen for C++17 variadic using (c++/88986). */;
3921 else if (!MAYBE_CLASS_TYPE_P (context))
3922 {
3923 if (complain & tf_error)
3924 error ("%q#T is not a class", context);
3925 return error_mark_node;
3926 }
3927
3928 /* When the CONTEXT is a dependent type, NAME could refer to a
3929 dependent base class of CONTEXT. But look inside it anyway
3930 if CONTEXT is a currently open scope, in case it refers to a
3931 member of the current instantiation or a non-dependent base;
3932 lookup will stop when we hit a dependent base. */
3933 if (!dependent_scope_p (context))
3934 /* We should only set WANT_TYPE when we're a nested typename type.
3935 Then we can give better diagnostics if we find a non-type. */
3936 t = lookup_field (context, name, 2, /*want_type=*/true);
3937 else
3938 t = NULL_TREE;
3939
3940 if ((!t || TREE_CODE (t) == TREE_LIST) && dependent_type_p (context))
3941 return build_typename_type (context, name, fullname, tag_type);
3942
3943 want_template = TREE_CODE (fullname) == TEMPLATE_ID_EXPR;
3944
3945 if (!t)
3946 {
3947 if (complain & tf_error)
3948 {
3949 if (!COMPLETE_TYPE_P (context))
3950 cxx_incomplete_type_error (NULL_TREE, context);
3951 else
3952 error (want_template ? G_("no class template named %q#T in %q#T")
3953 : G_("no type named %q#T in %q#T"), name, context);
3954 }
3955 return error_mark_node;
3956 }
3957
3958 /* Pull out the template from an injected-class-name (or multiple). */
3959 if (want_template)
3960 t = maybe_get_template_decl_from_type_decl (t);
3961
3962 if (TREE_CODE (t) == TREE_LIST)
3963 {
3964 if (complain & tf_error)
3965 {
3966 error ("lookup of %qT in %qT is ambiguous", name, context);
3967 print_candidates (t);
3968 }
3969 return error_mark_node;
3970 }
3971
3972 if (want_template && !DECL_TYPE_TEMPLATE_P (t))
3973 {
3974 if (complain & tf_error)
3975 error ("%<typename %T::%D%> names %q#T, which is not a class template",
3976 context, name, t);
3977 return error_mark_node;
3978 }
3979 if (!want_template && TREE_CODE (t) != TYPE_DECL)
3980 {
3981 if (complain & tf_error)
3982 error ("%<typename %T::%D%> names %q#T, which is not a type",
3983 context, name, t);
3984 return error_mark_node;
3985 }
3986
3987 if (!perform_or_defer_access_check (TYPE_BINFO (context), t, t, complain))
3988 return error_mark_node;
3989
3990 /* If we are currently parsing a template and if T is a typedef accessed
3991 through CONTEXT then we need to remember and check access of T at
3992 template instantiation time. */
3993 add_typedef_to_current_template_for_access_check (t, context, input_location);
3994
3995 if (want_template)
3996 return lookup_template_class (t, TREE_OPERAND (fullname, 1),
3997 NULL_TREE, context,
3998 /*entering_scope=*/0,
3999 complain | tf_user);
4000
4001 if (DECL_ARTIFICIAL (t) || !(complain & tf_keep_type_decl))
4002 t = TREE_TYPE (t);
4003
4004 maybe_record_typedef_use (t);
4005
4006 return t;
4007 }
4008
4009 /* Resolve `CONTEXT::template NAME'. Returns a TEMPLATE_DECL if the name
4010 can be resolved or an UNBOUND_CLASS_TEMPLATE, unless an error occurs,
4011 in which case error_mark_node is returned.
4012
4013 If PARM_LIST is non-NULL, also make sure that the template parameter
4014 list of TEMPLATE_DECL matches.
4015
4016 If COMPLAIN zero, don't complain about any errors that occur. */
4017
4018 tree
4019 make_unbound_class_template (tree context, tree name, tree parm_list,
4020 tsubst_flags_t complain)
4021 {
4022 tree t;
4023 tree d;
4024
4025 if (TYPE_P (name))
4026 name = TYPE_IDENTIFIER (name);
4027 else if (DECL_P (name))
4028 name = DECL_NAME (name);
4029 gcc_assert (identifier_p (name));
4030
4031 if (!dependent_type_p (context)
4032 || currently_open_class (context))
4033 {
4034 tree tmpl = NULL_TREE;
4035
4036 if (MAYBE_CLASS_TYPE_P (context))
4037 tmpl = lookup_field (context, name, 0, false);
4038
4039 if (tmpl && TREE_CODE (tmpl) == TYPE_DECL)
4040 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
4041
4042 if (!tmpl || !DECL_TYPE_TEMPLATE_P (tmpl))
4043 {
4044 if (complain & tf_error)
4045 error ("no class template named %q#T in %q#T", name, context);
4046 return error_mark_node;
4047 }
4048
4049 if (parm_list
4050 && !comp_template_parms (DECL_TEMPLATE_PARMS (tmpl), parm_list))
4051 {
4052 if (complain & tf_error)
4053 {
4054 error ("template parameters do not match template %qD", tmpl);
4055 inform (DECL_SOURCE_LOCATION (tmpl),
4056 "%qD declared here", tmpl);
4057 }
4058 return error_mark_node;
4059 }
4060
4061 if (!perform_or_defer_access_check (TYPE_BINFO (context), tmpl, tmpl,
4062 complain))
4063 return error_mark_node;
4064
4065 return tmpl;
4066 }
4067
4068 /* Build the UNBOUND_CLASS_TEMPLATE. */
4069 t = cxx_make_type (UNBOUND_CLASS_TEMPLATE);
4070 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
4071 TREE_TYPE (t) = NULL_TREE;
4072 SET_TYPE_STRUCTURAL_EQUALITY (t);
4073
4074 /* Build the corresponding TEMPLATE_DECL. */
4075 d = build_decl (input_location, TEMPLATE_DECL, name, t);
4076 TYPE_NAME (TREE_TYPE (d)) = d;
4077 TYPE_STUB_DECL (TREE_TYPE (d)) = d;
4078 DECL_CONTEXT (d) = FROB_CONTEXT (context);
4079 DECL_ARTIFICIAL (d) = 1;
4080 DECL_TEMPLATE_PARMS (d) = parm_list;
4081
4082 return t;
4083 }
4084
4085 \f
4086
4087 /* Push the declarations of builtin types into the global namespace.
4088 RID_INDEX is the index of the builtin type in the array
4089 RID_POINTERS. NAME is the name used when looking up the builtin
4090 type. TYPE is the _TYPE node for the builtin type.
4091
4092 The calls to set_global_binding below should be
4093 eliminated. Built-in types should not be looked up name; their
4094 names are keywords that the parser can recognize. However, there
4095 is code in c-common.c that uses identifier_global_value to look up
4096 built-in types by name. */
4097
4098 void
4099 record_builtin_type (enum rid rid_index,
4100 const char* name,
4101 tree type)
4102 {
4103 tree decl = NULL_TREE;
4104
4105 if (name)
4106 {
4107 tree tname = get_identifier (name);
4108 tree tdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, tname, type);
4109 DECL_ARTIFICIAL (tdecl) = 1;
4110 set_global_binding (tdecl);
4111 decl = tdecl;
4112 }
4113
4114 if ((int) rid_index < (int) RID_MAX)
4115 if (tree rname = ridpointers[(int) rid_index])
4116 if (!decl || DECL_NAME (decl) != rname)
4117 {
4118 tree rdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, rname, type);
4119 DECL_ARTIFICIAL (rdecl) = 1;
4120 set_global_binding (rdecl);
4121 if (!decl)
4122 decl = rdecl;
4123 }
4124
4125 if (decl)
4126 {
4127 if (!TYPE_NAME (type))
4128 TYPE_NAME (type) = decl;
4129 debug_hooks->type_decl (decl, 0);
4130 }
4131 }
4132
4133 /* Push a type into the namespace so that the back ends ignore it. */
4134
4135 static void
4136 record_unknown_type (tree type, const char* name)
4137 {
4138 tree decl = pushdecl (build_decl (UNKNOWN_LOCATION,
4139 TYPE_DECL, get_identifier (name), type));
4140 /* Make sure the "unknown type" typedecl gets ignored for debug info. */
4141 DECL_IGNORED_P (decl) = 1;
4142 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
4143 TYPE_SIZE (type) = TYPE_SIZE (void_type_node);
4144 SET_TYPE_ALIGN (type, 1);
4145 TYPE_USER_ALIGN (type) = 0;
4146 SET_TYPE_MODE (type, TYPE_MODE (void_type_node));
4147 }
4148
4149 /* Create all the predefined identifiers. */
4150
4151 static void
4152 initialize_predefined_identifiers (void)
4153 {
4154 struct predefined_identifier
4155 {
4156 const char *name; /* Name. */
4157 tree *node; /* Node to store it in. */
4158 cp_identifier_kind kind; /* Kind of identifier. */
4159 };
4160
4161 /* A table of identifiers to create at startup. */
4162 static const predefined_identifier predefined_identifiers[] = {
4163 {"C++", &lang_name_cplusplus, cik_normal},
4164 {"C", &lang_name_c, cik_normal},
4165 /* Some of these names have a trailing space so that it is
4166 impossible for them to conflict with names written by users. */
4167 {"__ct ", &ctor_identifier, cik_ctor},
4168 {"__ct_base ", &base_ctor_identifier, cik_ctor},
4169 {"__ct_comp ", &complete_ctor_identifier, cik_ctor},
4170 {"__dt ", &dtor_identifier, cik_dtor},
4171 {"__dt_base ", &base_dtor_identifier, cik_dtor},
4172 {"__dt_comp ", &complete_dtor_identifier, cik_dtor},
4173 {"__dt_del ", &deleting_dtor_identifier, cik_dtor},
4174 {"__conv_op ", &conv_op_identifier, cik_conv_op},
4175 {"__in_chrg", &in_charge_identifier, cik_normal},
4176 {"this", &this_identifier, cik_normal},
4177 {"__delta", &delta_identifier, cik_normal},
4178 {"__pfn", &pfn_identifier, cik_normal},
4179 {"_vptr", &vptr_identifier, cik_normal},
4180 {"__vtt_parm", &vtt_parm_identifier, cik_normal},
4181 {"::", &global_identifier, cik_normal},
4182 {"std", &std_identifier, cik_normal},
4183 /* The demangler expects anonymous namespaces to be called
4184 something starting with '_GLOBAL__N_'. It no longer needs
4185 to be unique to the TU. */
4186 {"_GLOBAL__N_1", &anon_identifier, cik_normal},
4187 {"auto", &auto_identifier, cik_normal},
4188 {"decltype(auto)", &decltype_auto_identifier, cik_normal},
4189 {"initializer_list", &init_list_identifier, cik_normal},
4190 {"__for_range ", &for_range__identifier, cik_normal},
4191 {"__for_begin ", &for_begin__identifier, cik_normal},
4192 {"__for_end ", &for_end__identifier, cik_normal},
4193 {"__for_range", &for_range_identifier, cik_normal},
4194 {"__for_begin", &for_begin_identifier, cik_normal},
4195 {"__for_end", &for_end_identifier, cik_normal},
4196 {"abi_tag", &abi_tag_identifier, cik_normal},
4197 {"aligned", &aligned_identifier, cik_normal},
4198 {"begin", &begin_identifier, cik_normal},
4199 {"end", &end_identifier, cik_normal},
4200 {"get", &get__identifier, cik_normal},
4201 {"gnu", &gnu_identifier, cik_normal},
4202 {"tuple_element", &tuple_element_identifier, cik_normal},
4203 {"tuple_size", &tuple_size_identifier, cik_normal},
4204 {"type", &type_identifier, cik_normal},
4205 {"value", &value_identifier, cik_normal},
4206 {"_FUN", &fun_identifier, cik_normal},
4207 {"__closure", &closure_identifier, cik_normal},
4208 {"heap uninit", &heap_uninit_identifier, cik_normal},
4209 {"heap ", &heap_identifier, cik_normal},
4210 {"heap deleted", &heap_deleted_identifier, cik_normal},
4211 {NULL, NULL, cik_normal}
4212 };
4213
4214 for (const predefined_identifier *pid = predefined_identifiers;
4215 pid->name; ++pid)
4216 {
4217 *pid->node = get_identifier (pid->name);
4218 /* Some of these identifiers already have a special kind. */
4219 if (pid->kind != cik_normal)
4220 set_identifier_kind (*pid->node, pid->kind);
4221 }
4222 }
4223
4224 /* Create the predefined scalar types of C,
4225 and some nodes representing standard constants (0, 1, (void *)0).
4226 Initialize the global binding level.
4227 Make definitions for built-in primitive functions. */
4228
4229 void
4230 cxx_init_decl_processing (void)
4231 {
4232 tree void_ftype;
4233 tree void_ftype_ptr;
4234
4235 /* Create all the identifiers we need. */
4236 initialize_predefined_identifiers ();
4237
4238 /* Create the global variables. */
4239 push_to_top_level ();
4240
4241 current_function_decl = NULL_TREE;
4242 current_binding_level = NULL;
4243 /* Enter the global namespace. */
4244 gcc_assert (global_namespace == NULL_TREE);
4245 global_namespace = build_lang_decl (NAMESPACE_DECL, global_identifier,
4246 void_type_node);
4247 TREE_PUBLIC (global_namespace) = 1;
4248 DECL_CONTEXT (global_namespace)
4249 = build_translation_unit_decl (get_identifier (main_input_filename));
4250 /* Remember whether we want the empty class passing ABI change warning
4251 in this TU. */
4252 TRANSLATION_UNIT_WARN_EMPTY_P (DECL_CONTEXT (global_namespace))
4253 = warn_abi && abi_version_crosses (12);
4254 debug_hooks->register_main_translation_unit
4255 (DECL_CONTEXT (global_namespace));
4256 begin_scope (sk_namespace, global_namespace);
4257 current_namespace = global_namespace;
4258
4259 if (flag_visibility_ms_compat)
4260 default_visibility = VISIBILITY_HIDDEN;
4261
4262 /* Initially, C. */
4263 current_lang_name = lang_name_c;
4264
4265 /* Create the `std' namespace. */
4266 push_namespace (std_identifier);
4267 std_node = current_namespace;
4268 pop_namespace ();
4269
4270 flag_noexcept_type = (cxx_dialect >= cxx17);
4271
4272 c_common_nodes_and_builtins ();
4273
4274 tree bool_ftype = build_function_type_list (boolean_type_node, NULL_TREE);
4275 tree decl
4276 = add_builtin_function ("__builtin_is_constant_evaluated",
4277 bool_ftype, CP_BUILT_IN_IS_CONSTANT_EVALUATED,
4278 BUILT_IN_FRONTEND, NULL, NULL_TREE);
4279 set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
4280
4281 integer_two_node = build_int_cst (NULL_TREE, 2);
4282
4283 /* Guess at the initial static decls size. */
4284 vec_alloc (static_decls, 500);
4285
4286 /* ... and keyed classes. */
4287 vec_alloc (keyed_classes, 100);
4288
4289 record_builtin_type (RID_BOOL, "bool", boolean_type_node);
4290 truthvalue_type_node = boolean_type_node;
4291 truthvalue_false_node = boolean_false_node;
4292 truthvalue_true_node = boolean_true_node;
4293
4294 empty_except_spec = build_tree_list (NULL_TREE, NULL_TREE);
4295 noexcept_true_spec = build_tree_list (boolean_true_node, NULL_TREE);
4296 noexcept_false_spec = build_tree_list (boolean_false_node, NULL_TREE);
4297 noexcept_deferred_spec = build_tree_list (make_node (DEFERRED_NOEXCEPT),
4298 NULL_TREE);
4299
4300 #if 0
4301 record_builtin_type (RID_MAX, NULL, string_type_node);
4302 #endif
4303
4304 delta_type_node = ptrdiff_type_node;
4305 vtable_index_type = ptrdiff_type_node;
4306
4307 vtt_parm_type = build_pointer_type (const_ptr_type_node);
4308 void_ftype = build_function_type_list (void_type_node, NULL_TREE);
4309 void_ftype_ptr = build_function_type_list (void_type_node,
4310 ptr_type_node, NULL_TREE);
4311 void_ftype_ptr
4312 = build_exception_variant (void_ftype_ptr, empty_except_spec);
4313
4314 /* Create the conversion operator marker. This operator's DECL_NAME
4315 is in the identifier table, so we can use identifier equality to
4316 find it. */
4317 conv_op_marker = build_lang_decl (FUNCTION_DECL, conv_op_identifier,
4318 void_ftype);
4319
4320 /* C++ extensions */
4321
4322 unknown_type_node = make_node (LANG_TYPE);
4323 record_unknown_type (unknown_type_node, "unknown type");
4324
4325 /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node. */
4326 TREE_TYPE (unknown_type_node) = unknown_type_node;
4327
4328 /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same
4329 result. */
4330 TYPE_POINTER_TO (unknown_type_node) = unknown_type_node;
4331 TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node;
4332
4333 init_list_type_node = make_node (LANG_TYPE);
4334 record_unknown_type (init_list_type_node, "init list");
4335
4336 {
4337 /* Make sure we get a unique function type, so we can give
4338 its pointer type a name. (This wins for gdb.) */
4339 tree vfunc_type = make_node (FUNCTION_TYPE);
4340 TREE_TYPE (vfunc_type) = integer_type_node;
4341 TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
4342 layout_type (vfunc_type);
4343
4344 vtable_entry_type = build_pointer_type (vfunc_type);
4345 }
4346 record_builtin_type (RID_MAX, "__vtbl_ptr_type", vtable_entry_type);
4347
4348 vtbl_type_node
4349 = build_cplus_array_type (vtable_entry_type, NULL_TREE);
4350 layout_type (vtbl_type_node);
4351 vtbl_type_node = cp_build_qualified_type (vtbl_type_node, TYPE_QUAL_CONST);
4352 record_builtin_type (RID_MAX, NULL, vtbl_type_node);
4353 vtbl_ptr_type_node = build_pointer_type (vtable_entry_type);
4354 layout_type (vtbl_ptr_type_node);
4355 record_builtin_type (RID_MAX, NULL, vtbl_ptr_type_node);
4356
4357 push_namespace (get_identifier ("__cxxabiv1"));
4358 abi_node = current_namespace;
4359 pop_namespace ();
4360
4361 global_type_node = make_node (LANG_TYPE);
4362 record_unknown_type (global_type_node, "global type");
4363
4364 any_targ_node = make_node (LANG_TYPE);
4365 record_unknown_type (any_targ_node, "any type");
4366
4367 /* Now, C++. */
4368 current_lang_name = lang_name_cplusplus;
4369
4370 if (aligned_new_threshold > 1
4371 && !pow2p_hwi (aligned_new_threshold))
4372 {
4373 error ("%<-faligned-new=%d%> is not a power of two",
4374 aligned_new_threshold);
4375 aligned_new_threshold = 1;
4376 }
4377 if (aligned_new_threshold == -1)
4378 aligned_new_threshold = (cxx_dialect >= cxx17) ? 1 : 0;
4379 if (aligned_new_threshold == 1)
4380 aligned_new_threshold = malloc_alignment () / BITS_PER_UNIT;
4381
4382 {
4383 tree newattrs, extvisattr;
4384 tree newtype, deltype;
4385 tree ptr_ftype_sizetype;
4386 tree new_eh_spec;
4387
4388 ptr_ftype_sizetype
4389 = build_function_type_list (ptr_type_node, size_type_node, NULL_TREE);
4390 if (cxx_dialect == cxx98)
4391 {
4392 tree bad_alloc_id;
4393 tree bad_alloc_type_node;
4394 tree bad_alloc_decl;
4395
4396 push_namespace (std_identifier);
4397 bad_alloc_id = get_identifier ("bad_alloc");
4398 bad_alloc_type_node = make_class_type (RECORD_TYPE);
4399 TYPE_CONTEXT (bad_alloc_type_node) = current_namespace;
4400 bad_alloc_decl
4401 = create_implicit_typedef (bad_alloc_id, bad_alloc_type_node);
4402 DECL_CONTEXT (bad_alloc_decl) = current_namespace;
4403 pop_namespace ();
4404
4405 new_eh_spec
4406 = add_exception_specifier (NULL_TREE, bad_alloc_type_node, -1);
4407 }
4408 else
4409 new_eh_spec = noexcept_false_spec;
4410
4411 /* Ensure attribs.c is initialized. */
4412 init_attributes ();
4413
4414 /* Ensure constraint.cc is initialized. */
4415 init_constraint_processing ();
4416
4417 extvisattr = build_tree_list (get_identifier ("externally_visible"),
4418 NULL_TREE);
4419 newattrs = tree_cons (get_identifier ("alloc_size"),
4420 build_tree_list (NULL_TREE, integer_one_node),
4421 extvisattr);
4422 newtype = cp_build_type_attribute_variant (ptr_ftype_sizetype, newattrs);
4423 newtype = build_exception_variant (newtype, new_eh_spec);
4424 deltype = cp_build_type_attribute_variant (void_ftype_ptr, extvisattr);
4425 deltype = build_exception_variant (deltype, empty_except_spec);
4426 tree opnew = push_cp_library_fn (NEW_EXPR, newtype, 0);
4427 DECL_IS_MALLOC (opnew) = 1;
4428 DECL_SET_IS_OPERATOR_NEW (opnew, true);
4429 opnew = push_cp_library_fn (VEC_NEW_EXPR, newtype, 0);
4430 DECL_IS_MALLOC (opnew) = 1;
4431 DECL_SET_IS_OPERATOR_NEW (opnew, true);
4432 tree opdel = push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
4433 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4434 opdel = push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
4435 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4436 if (flag_sized_deallocation)
4437 {
4438 /* Also push the sized deallocation variants:
4439 void operator delete(void*, std::size_t) throw();
4440 void operator delete[](void*, std::size_t) throw(); */
4441 tree void_ftype_ptr_size
4442 = build_function_type_list (void_type_node, ptr_type_node,
4443 size_type_node, NULL_TREE);
4444 deltype = cp_build_type_attribute_variant (void_ftype_ptr_size,
4445 extvisattr);
4446 deltype = build_exception_variant (deltype, empty_except_spec);
4447 opdel = push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
4448 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4449 opdel = push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
4450 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4451 }
4452
4453 if (aligned_new_threshold)
4454 {
4455 push_namespace (std_identifier);
4456 tree align_id = get_identifier ("align_val_t");
4457 align_type_node = start_enum (align_id, NULL_TREE, size_type_node,
4458 NULL_TREE, /*scoped*/true, NULL);
4459 pop_namespace ();
4460
4461 /* operator new (size_t, align_val_t); */
4462 newtype = build_function_type_list (ptr_type_node, size_type_node,
4463 align_type_node, NULL_TREE);
4464 newtype = cp_build_type_attribute_variant (newtype, newattrs);
4465 newtype = build_exception_variant (newtype, new_eh_spec);
4466 opnew = push_cp_library_fn (NEW_EXPR, newtype, 0);
4467 DECL_IS_MALLOC (opnew) = 1;
4468 DECL_SET_IS_OPERATOR_NEW (opnew, true);
4469 opnew = push_cp_library_fn (VEC_NEW_EXPR, newtype, 0);
4470 DECL_IS_MALLOC (opnew) = 1;
4471 DECL_SET_IS_OPERATOR_NEW (opnew, true);
4472
4473 /* operator delete (void *, align_val_t); */
4474 deltype = build_function_type_list (void_type_node, ptr_type_node,
4475 align_type_node, NULL_TREE);
4476 deltype = cp_build_type_attribute_variant (deltype, extvisattr);
4477 deltype = build_exception_variant (deltype, empty_except_spec);
4478 opdel = push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
4479 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4480 opdel = push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
4481 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4482
4483 if (flag_sized_deallocation)
4484 {
4485 /* operator delete (void *, size_t, align_val_t); */
4486 deltype = build_function_type_list (void_type_node, ptr_type_node,
4487 size_type_node, align_type_node,
4488 NULL_TREE);
4489 deltype = cp_build_type_attribute_variant (deltype, extvisattr);
4490 deltype = build_exception_variant (deltype, empty_except_spec);
4491 opdel = push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
4492 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4493 opdel = push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
4494 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4495 }
4496 }
4497
4498 nullptr_type_node = make_node (NULLPTR_TYPE);
4499 TYPE_SIZE (nullptr_type_node) = bitsize_int (GET_MODE_BITSIZE (ptr_mode));
4500 TYPE_SIZE_UNIT (nullptr_type_node) = size_int (GET_MODE_SIZE (ptr_mode));
4501 TYPE_UNSIGNED (nullptr_type_node) = 1;
4502 TYPE_PRECISION (nullptr_type_node) = GET_MODE_BITSIZE (ptr_mode);
4503 if (abi_version_at_least (9))
4504 SET_TYPE_ALIGN (nullptr_type_node, GET_MODE_ALIGNMENT (ptr_mode));
4505 SET_TYPE_MODE (nullptr_type_node, ptr_mode);
4506 record_builtin_type (RID_MAX, "decltype(nullptr)", nullptr_type_node);
4507 nullptr_node = build_int_cst (nullptr_type_node, 0);
4508 }
4509
4510 abort_fndecl
4511 = build_library_fn_ptr ("__cxa_pure_virtual", void_ftype,
4512 ECF_NORETURN | ECF_NOTHROW | ECF_COLD);
4513
4514 /* Perform other language dependent initializations. */
4515 init_class_processing ();
4516 init_rtti_processing ();
4517 init_template_processing ();
4518
4519 if (flag_exceptions)
4520 init_exception_processing ();
4521
4522 if (! supports_one_only ())
4523 flag_weak = 0;
4524
4525 make_fname_decl = cp_make_fname_decl;
4526 start_fname_decls ();
4527
4528 /* Show we use EH for cleanups. */
4529 if (flag_exceptions)
4530 using_eh_for_cleanups ();
4531 }
4532
4533 /* Generate an initializer for a function naming variable from
4534 NAME. NAME may be NULL, to indicate a dependent name. TYPE_P is
4535 filled in with the type of the init. */
4536
4537 tree
4538 cp_fname_init (const char* name, tree *type_p)
4539 {
4540 tree domain = NULL_TREE;
4541 tree type;
4542 tree init = NULL_TREE;
4543 size_t length = 0;
4544
4545 if (name)
4546 {
4547 length = strlen (name);
4548 domain = build_index_type (size_int (length));
4549 init = build_string (length + 1, name);
4550 }
4551
4552 type = cp_build_qualified_type (char_type_node, TYPE_QUAL_CONST);
4553 type = build_cplus_array_type (type, domain);
4554
4555 *type_p = type;
4556
4557 if (init)
4558 TREE_TYPE (init) = type;
4559 else
4560 init = error_mark_node;
4561
4562 return init;
4563 }
4564
4565 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give
4566 the decl, LOC is the location to give the decl, NAME is the
4567 initialization string and TYPE_DEP indicates whether NAME depended
4568 on the type of the function. We make use of that to detect
4569 __PRETTY_FUNCTION__ inside a template fn. This is being done lazily
4570 at the point of first use, so we mustn't push the decl now. */
4571
4572 static tree
4573 cp_make_fname_decl (location_t loc, tree id, int type_dep)
4574 {
4575 const char * name = NULL;
4576 bool release_name = false;
4577 if (!(type_dep && in_template_function ()))
4578 {
4579 if (current_function_decl == NULL_TREE)
4580 name = "top level";
4581 else if (type_dep == 1) /* __PRETTY_FUNCTION__ */
4582 name = cxx_printable_name (current_function_decl, 2);
4583 else if (type_dep == 0) /* __FUNCTION__ */
4584 {
4585 name = fname_as_string (type_dep);
4586 release_name = true;
4587 }
4588 else
4589 gcc_unreachable ();
4590 }
4591 tree type;
4592 tree init = cp_fname_init (name, &type);
4593 tree decl = build_decl (loc, VAR_DECL, id, type);
4594
4595 if (release_name)
4596 free (CONST_CAST (char *, name));
4597
4598 /* As we're using pushdecl_with_scope, we must set the context. */
4599 DECL_CONTEXT (decl) = current_function_decl;
4600
4601 TREE_READONLY (decl) = 1;
4602 DECL_ARTIFICIAL (decl) = 1;
4603 DECL_DECLARED_CONSTEXPR_P (decl) = 1;
4604 TREE_STATIC (decl) = 1;
4605
4606 TREE_USED (decl) = 1;
4607
4608 if (init)
4609 {
4610 SET_DECL_VALUE_EXPR (decl, init);
4611 DECL_HAS_VALUE_EXPR_P (decl) = 1;
4612 /* For decl_constant_var_p. */
4613 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
4614 }
4615
4616 if (current_function_decl)
4617 {
4618 DECL_CONTEXT (decl) = current_function_decl;
4619 decl = pushdecl_outermost_localscope (decl);
4620 if (decl != error_mark_node)
4621 add_decl_expr (decl);
4622 }
4623 else
4624 {
4625 DECL_THIS_STATIC (decl) = true;
4626 pushdecl_top_level_and_finish (decl, NULL_TREE);
4627 }
4628
4629 return decl;
4630 }
4631
4632 static tree
4633 builtin_function_1 (tree decl, tree context, bool is_global)
4634 {
4635 tree id = DECL_NAME (decl);
4636 const char *name = IDENTIFIER_POINTER (id);
4637
4638 retrofit_lang_decl (decl);
4639
4640 DECL_ARTIFICIAL (decl) = 1;
4641 SET_DECL_LANGUAGE (decl, lang_c);
4642 /* Runtime library routines are, by definition, available in an
4643 external shared object. */
4644 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
4645 DECL_VISIBILITY_SPECIFIED (decl) = 1;
4646
4647 DECL_CONTEXT (decl) = context;
4648
4649 /* A function in the user's namespace should have an explicit
4650 declaration before it is used. Mark the built-in function as
4651 anticipated but not actually declared. */
4652 if (name[0] != '_' || name[1] != '_')
4653 DECL_ANTICIPATED (decl) = 1;
4654 else if (strncmp (name + 2, "builtin_", strlen ("builtin_")) != 0)
4655 {
4656 size_t len = strlen (name);
4657
4658 /* Treat __*_chk fortification functions as anticipated as well,
4659 unless they are __builtin_*. */
4660 if (len > strlen ("___chk")
4661 && memcmp (name + len - strlen ("_chk"),
4662 "_chk", strlen ("_chk") + 1) == 0)
4663 DECL_ANTICIPATED (decl) = 1;
4664 }
4665
4666 if (is_global)
4667 return pushdecl_top_level (decl);
4668 else
4669 return pushdecl (decl);
4670 }
4671
4672 tree
4673 cxx_builtin_function (tree decl)
4674 {
4675 tree id = DECL_NAME (decl);
4676 const char *name = IDENTIFIER_POINTER (id);
4677 /* All builtins that don't begin with an '_' should additionally
4678 go in the 'std' namespace. */
4679 if (name[0] != '_')
4680 {
4681 tree decl2 = copy_node(decl);
4682 push_namespace (std_identifier);
4683 builtin_function_1 (decl2, std_node, false);
4684 pop_namespace ();
4685 }
4686
4687 return builtin_function_1 (decl, NULL_TREE, false);
4688 }
4689
4690 /* Like cxx_builtin_function, but guarantee the function is added to the global
4691 scope. This is to allow function specific options to add new machine
4692 dependent builtins when the target ISA changes via attribute((target(...)))
4693 which saves space on program startup if the program does not use non-generic
4694 ISAs. */
4695
4696 tree
4697 cxx_builtin_function_ext_scope (tree decl)
4698 {
4699
4700 tree id = DECL_NAME (decl);
4701 const char *name = IDENTIFIER_POINTER (id);
4702 /* All builtins that don't begin with an '_' should additionally
4703 go in the 'std' namespace. */
4704 if (name[0] != '_')
4705 {
4706 tree decl2 = copy_node(decl);
4707 push_namespace (std_identifier);
4708 builtin_function_1 (decl2, std_node, true);
4709 pop_namespace ();
4710 }
4711
4712 return builtin_function_1 (decl, NULL_TREE, true);
4713 }
4714
4715 /* Generate a FUNCTION_DECL with the typical flags for a runtime library
4716 function. Not called directly. */
4717
4718 static tree
4719 build_library_fn (tree name, enum tree_code operator_code, tree type,
4720 int ecf_flags)
4721 {
4722 tree fn = build_lang_decl (FUNCTION_DECL, name, type);
4723 DECL_EXTERNAL (fn) = 1;
4724 TREE_PUBLIC (fn) = 1;
4725 DECL_ARTIFICIAL (fn) = 1;
4726 DECL_OVERLOADED_OPERATOR_CODE_RAW (fn)
4727 = OVL_OP_INFO (false, operator_code)->ovl_op_code;
4728 SET_DECL_LANGUAGE (fn, lang_c);
4729 /* Runtime library routines are, by definition, available in an
4730 external shared object. */
4731 DECL_VISIBILITY (fn) = VISIBILITY_DEFAULT;
4732 DECL_VISIBILITY_SPECIFIED (fn) = 1;
4733 set_call_expr_flags (fn, ecf_flags);
4734 return fn;
4735 }
4736
4737 /* Returns the _DECL for a library function with C++ linkage. */
4738
4739 static tree
4740 build_cp_library_fn (tree name, enum tree_code operator_code, tree type,
4741 int ecf_flags)
4742 {
4743 tree fn = build_library_fn (name, operator_code, type, ecf_flags);
4744 DECL_CONTEXT (fn) = FROB_CONTEXT (current_namespace);
4745 SET_DECL_LANGUAGE (fn, lang_cplusplus);
4746 return fn;
4747 }
4748
4749 /* Like build_library_fn, but takes a C string instead of an
4750 IDENTIFIER_NODE. */
4751
4752 tree
4753 build_library_fn_ptr (const char* name, tree type, int ecf_flags)
4754 {
4755 return build_library_fn (get_identifier (name), ERROR_MARK, type, ecf_flags);
4756 }
4757
4758 /* Like build_cp_library_fn, but takes a C string instead of an
4759 IDENTIFIER_NODE. */
4760
4761 tree
4762 build_cp_library_fn_ptr (const char* name, tree type, int ecf_flags)
4763 {
4764 return build_cp_library_fn (get_identifier (name), ERROR_MARK, type,
4765 ecf_flags);
4766 }
4767
4768 /* Like build_library_fn, but also pushes the function so that we will
4769 be able to find it via get_global_binding. Also, the function
4770 may throw exceptions listed in RAISES. */
4771
4772 tree
4773 push_library_fn (tree name, tree type, tree raises, int ecf_flags)
4774 {
4775 tree fn;
4776
4777 if (raises)
4778 type = build_exception_variant (type, raises);
4779
4780 fn = build_library_fn (name, ERROR_MARK, type, ecf_flags);
4781 pushdecl_top_level (fn);
4782 return fn;
4783 }
4784
4785 /* Like build_cp_library_fn, but also pushes the function so that it
4786 will be found by normal lookup. */
4787
4788 static tree
4789 push_cp_library_fn (enum tree_code operator_code, tree type,
4790 int ecf_flags)
4791 {
4792 tree fn = build_cp_library_fn (ovl_op_identifier (false, operator_code),
4793 operator_code, type, ecf_flags);
4794 pushdecl (fn);
4795 if (flag_tm)
4796 apply_tm_attr (fn, get_identifier ("transaction_safe"));
4797 return fn;
4798 }
4799
4800 /* Like push_library_fn, but takes a TREE_LIST of parm types rather than
4801 a FUNCTION_TYPE. */
4802
4803 tree
4804 push_void_library_fn (tree name, tree parmtypes, int ecf_flags)
4805 {
4806 tree type = build_function_type (void_type_node, parmtypes);
4807 return push_library_fn (name, type, NULL_TREE, ecf_flags);
4808 }
4809
4810 /* Like push_library_fn, but also note that this function throws
4811 and does not return. Used for __throw_foo and the like. */
4812
4813 tree
4814 push_throw_library_fn (tree name, tree type)
4815 {
4816 tree fn = push_library_fn (name, type, NULL_TREE, ECF_NORETURN | ECF_COLD);
4817 return fn;
4818 }
4819 \f
4820 /* When we call finish_struct for an anonymous union, we create
4821 default copy constructors and such. But, an anonymous union
4822 shouldn't have such things; this function undoes the damage to the
4823 anonymous union type T.
4824
4825 (The reason that we create the synthesized methods is that we don't
4826 distinguish `union { int i; }' from `typedef union { int i; } U'.
4827 The first is an anonymous union; the second is just an ordinary
4828 union type.) */
4829
4830 void
4831 fixup_anonymous_aggr (tree t)
4832 {
4833 /* Wipe out memory of synthesized methods. */
4834 TYPE_HAS_USER_CONSTRUCTOR (t) = 0;
4835 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0;
4836 TYPE_HAS_COPY_CTOR (t) = 0;
4837 TYPE_HAS_CONST_COPY_CTOR (t) = 0;
4838 TYPE_HAS_COPY_ASSIGN (t) = 0;
4839 TYPE_HAS_CONST_COPY_ASSIGN (t) = 0;
4840
4841 /* Splice the implicitly generated functions out of TYPE_FIELDS. */
4842 for (tree probe, *prev_p = &TYPE_FIELDS (t); (probe = *prev_p);)
4843 if (TREE_CODE (probe) == FUNCTION_DECL && DECL_ARTIFICIAL (probe))
4844 *prev_p = DECL_CHAIN (probe);
4845 else
4846 prev_p = &DECL_CHAIN (probe);
4847
4848 /* Anonymous aggregates cannot have fields with ctors, dtors or complex
4849 assignment operators (because they cannot have these methods themselves).
4850 For anonymous unions this is already checked because they are not allowed
4851 in any union, otherwise we have to check it. */
4852 if (TREE_CODE (t) != UNION_TYPE)
4853 {
4854 tree field, type;
4855
4856 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
4857 if (TREE_CODE (field) == FIELD_DECL)
4858 {
4859 type = TREE_TYPE (field);
4860 if (CLASS_TYPE_P (type))
4861 {
4862 if (TYPE_NEEDS_CONSTRUCTING (type))
4863 error ("member %q+#D with constructor not allowed "
4864 "in anonymous aggregate", field);
4865 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
4866 error ("member %q+#D with destructor not allowed "
4867 "in anonymous aggregate", field);
4868 if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type))
4869 error ("member %q+#D with copy assignment operator "
4870 "not allowed in anonymous aggregate", field);
4871 }
4872 }
4873 }
4874 }
4875
4876 /* Warn for an attribute located at LOCATION that appertains to the
4877 class type CLASS_TYPE that has not been properly placed after its
4878 class-key, in it class-specifier. */
4879
4880 void
4881 warn_misplaced_attr_for_class_type (location_t location,
4882 tree class_type)
4883 {
4884 gcc_assert (OVERLOAD_TYPE_P (class_type));
4885
4886 auto_diagnostic_group d;
4887 if (warning_at (location, OPT_Wattributes,
4888 "attribute ignored in declaration "
4889 "of %q#T", class_type))
4890 inform (location,
4891 "attribute for %q#T must follow the %qs keyword",
4892 class_type, class_key_or_enum_as_string (class_type));
4893 }
4894
4895 /* Returns the cv-qualifiers that apply to the type specified
4896 by the DECLSPECS. */
4897
4898 static int
4899 get_type_quals (const cp_decl_specifier_seq *declspecs)
4900 {
4901 int type_quals = TYPE_UNQUALIFIED;
4902
4903 if (decl_spec_seq_has_spec_p (declspecs, ds_const))
4904 type_quals |= TYPE_QUAL_CONST;
4905 if (decl_spec_seq_has_spec_p (declspecs, ds_volatile))
4906 type_quals |= TYPE_QUAL_VOLATILE;
4907 if (decl_spec_seq_has_spec_p (declspecs, ds_restrict))
4908 type_quals |= TYPE_QUAL_RESTRICT;
4909
4910 return type_quals;
4911 }
4912
4913 /* Make sure that a declaration with no declarator is well-formed, i.e.
4914 just declares a tagged type or anonymous union.
4915
4916 Returns the type declared; or NULL_TREE if none. */
4917
4918 tree
4919 check_tag_decl (cp_decl_specifier_seq *declspecs,
4920 bool explicit_type_instantiation_p)
4921 {
4922 int saw_friend = decl_spec_seq_has_spec_p (declspecs, ds_friend);
4923 int saw_typedef = decl_spec_seq_has_spec_p (declspecs, ds_typedef);
4924 /* If a class, struct, or enum type is declared by the DECLSPECS
4925 (i.e, if a class-specifier, enum-specifier, or non-typename
4926 elaborated-type-specifier appears in the DECLSPECS),
4927 DECLARED_TYPE is set to the corresponding type. */
4928 tree declared_type = NULL_TREE;
4929 bool error_p = false;
4930
4931 if (declspecs->multiple_types_p)
4932 error_at (smallest_type_location (declspecs),
4933 "multiple types in one declaration");
4934 else if (declspecs->redefined_builtin_type)
4935 {
4936 if (!in_system_header_at (input_location))
4937 permerror (declspecs->locations[ds_redefined_builtin_type_spec],
4938 "redeclaration of C++ built-in type %qT",
4939 declspecs->redefined_builtin_type);
4940 return NULL_TREE;
4941 }
4942
4943 if (declspecs->type
4944 && TYPE_P (declspecs->type)
4945 && ((TREE_CODE (declspecs->type) != TYPENAME_TYPE
4946 && MAYBE_CLASS_TYPE_P (declspecs->type))
4947 || TREE_CODE (declspecs->type) == ENUMERAL_TYPE))
4948 declared_type = declspecs->type;
4949 else if (declspecs->type == error_mark_node)
4950 error_p = true;
4951
4952 if (type_uses_auto (declared_type))
4953 {
4954 error_at (declspecs->locations[ds_type_spec],
4955 "%<auto%> can only be specified for variables "
4956 "or function declarations");
4957 return error_mark_node;
4958 }
4959
4960 if (declared_type && !OVERLOAD_TYPE_P (declared_type))
4961 declared_type = NULL_TREE;
4962
4963 if (!declared_type && !saw_friend && !error_p)
4964 permerror (input_location, "declaration does not declare anything");
4965 /* Check for an anonymous union. */
4966 else if (declared_type && RECORD_OR_UNION_CODE_P (TREE_CODE (declared_type))
4967 && TYPE_UNNAMED_P (declared_type))
4968 {
4969 /* 7/3 In a simple-declaration, the optional init-declarator-list
4970 can be omitted only when declaring a class (clause 9) or
4971 enumeration (7.2), that is, when the decl-specifier-seq contains
4972 either a class-specifier, an elaborated-type-specifier with
4973 a class-key (9.1), or an enum-specifier. In these cases and
4974 whenever a class-specifier or enum-specifier is present in the
4975 decl-specifier-seq, the identifiers in these specifiers are among
4976 the names being declared by the declaration (as class-name,
4977 enum-names, or enumerators, depending on the syntax). In such
4978 cases, and except for the declaration of an unnamed bit-field (9.6),
4979 the decl-specifier-seq shall introduce one or more names into the
4980 program, or shall redeclare a name introduced by a previous
4981 declaration. [Example:
4982 enum { }; // ill-formed
4983 typedef class { }; // ill-formed
4984 --end example] */
4985 if (saw_typedef)
4986 {
4987 error ("missing type-name in typedef-declaration");
4988 return NULL_TREE;
4989 }
4990 /* Anonymous unions are objects, so they can have specifiers. */;
4991 SET_ANON_AGGR_TYPE_P (declared_type);
4992
4993 if (TREE_CODE (declared_type) != UNION_TYPE)
4994 pedwarn (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (declared_type)),
4995 OPT_Wpedantic, "ISO C++ prohibits anonymous structs");
4996 }
4997
4998 else
4999 {
5000 if (decl_spec_seq_has_spec_p (declspecs, ds_inline))
5001 error_at (declspecs->locations[ds_inline],
5002 "%<inline%> can only be specified for functions");
5003 else if (decl_spec_seq_has_spec_p (declspecs, ds_virtual))
5004 error_at (declspecs->locations[ds_virtual],
5005 "%<virtual%> can only be specified for functions");
5006 else if (saw_friend
5007 && (!current_class_type
5008 || current_scope () != current_class_type))
5009 error_at (declspecs->locations[ds_friend],
5010 "%<friend%> can only be specified inside a class");
5011 else if (decl_spec_seq_has_spec_p (declspecs, ds_explicit))
5012 error_at (declspecs->locations[ds_explicit],
5013 "%<explicit%> can only be specified for constructors");
5014 else if (declspecs->storage_class)
5015 error_at (declspecs->locations[ds_storage_class],
5016 "a storage class can only be specified for objects "
5017 "and functions");
5018 else if (decl_spec_seq_has_spec_p (declspecs, ds_const))
5019 error_at (declspecs->locations[ds_const],
5020 "%<const%> can only be specified for objects and "
5021 "functions");
5022 else if (decl_spec_seq_has_spec_p (declspecs, ds_volatile))
5023 error_at (declspecs->locations[ds_volatile],
5024 "%<volatile%> can only be specified for objects and "
5025 "functions");
5026 else if (decl_spec_seq_has_spec_p (declspecs, ds_restrict))
5027 error_at (declspecs->locations[ds_restrict],
5028 "%<__restrict%> can only be specified for objects and "
5029 "functions");
5030 else if (decl_spec_seq_has_spec_p (declspecs, ds_thread))
5031 error_at (declspecs->locations[ds_thread],
5032 "%<__thread%> can only be specified for objects "
5033 "and functions");
5034 else if (saw_typedef)
5035 warning_at (declspecs->locations[ds_typedef], 0,
5036 "%<typedef%> was ignored in this declaration");
5037 else if (decl_spec_seq_has_spec_p (declspecs, ds_constexpr))
5038 error_at (declspecs->locations[ds_constexpr],
5039 "%<constexpr%> cannot be used for type declarations");
5040 else if (decl_spec_seq_has_spec_p (declspecs, ds_constinit))
5041 error_at (declspecs->locations[ds_constinit],
5042 "%<constinit%> cannot be used for type declarations");
5043 }
5044
5045 if (declspecs->attributes && warn_attributes && declared_type)
5046 {
5047 location_t loc;
5048 if (!CLASS_TYPE_P (declared_type)
5049 || !CLASSTYPE_TEMPLATE_INSTANTIATION (declared_type))
5050 /* For a non-template class, use the name location. */
5051 loc = location_of (declared_type);
5052 else
5053 /* For a template class (an explicit instantiation), use the
5054 current location. */
5055 loc = input_location;
5056
5057 if (explicit_type_instantiation_p)
5058 /* [dcl.attr.grammar]/4:
5059
5060 No attribute-specifier-seq shall appertain to an explicit
5061 instantiation. */
5062 {
5063 if (warning_at (loc, OPT_Wattributes,
5064 "attribute ignored in explicit instantiation %q#T",
5065 declared_type))
5066 inform (loc,
5067 "no attribute can be applied to "
5068 "an explicit instantiation");
5069 }
5070 else
5071 warn_misplaced_attr_for_class_type (loc, declared_type);
5072 }
5073
5074 return declared_type;
5075 }
5076
5077 /* Called when a declaration is seen that contains no names to declare.
5078 If its type is a reference to a structure, union or enum inherited
5079 from a containing scope, shadow that tag name for the current scope
5080 with a forward reference.
5081 If its type defines a new named structure or union
5082 or defines an enum, it is valid but we need not do anything here.
5083 Otherwise, it is an error.
5084
5085 C++: may have to grok the declspecs to learn about static,
5086 complain for anonymous unions.
5087
5088 Returns the TYPE declared -- or NULL_TREE if none. */
5089
5090 tree
5091 shadow_tag (cp_decl_specifier_seq *declspecs)
5092 {
5093 tree t = check_tag_decl (declspecs,
5094 /*explicit_type_instantiation_p=*/false);
5095
5096 if (!t)
5097 return NULL_TREE;
5098
5099 if (maybe_process_partial_specialization (t) == error_mark_node)
5100 return NULL_TREE;
5101
5102 /* This is where the variables in an anonymous union are
5103 declared. An anonymous union declaration looks like:
5104 union { ... } ;
5105 because there is no declarator after the union, the parser
5106 sends that declaration here. */
5107 if (ANON_AGGR_TYPE_P (t))
5108 {
5109 fixup_anonymous_aggr (t);
5110
5111 if (TYPE_FIELDS (t))
5112 {
5113 tree decl = grokdeclarator (/*declarator=*/NULL,
5114 declspecs, NORMAL, 0, NULL);
5115 finish_anon_union (decl);
5116 }
5117 }
5118
5119 return t;
5120 }
5121 \f
5122 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
5123
5124 tree
5125 groktypename (cp_decl_specifier_seq *type_specifiers,
5126 const cp_declarator *declarator,
5127 bool is_template_arg)
5128 {
5129 tree attrs;
5130 tree type;
5131 enum decl_context context
5132 = is_template_arg ? TEMPLATE_TYPE_ARG : TYPENAME;
5133 attrs = type_specifiers->attributes;
5134 type_specifiers->attributes = NULL_TREE;
5135 type = grokdeclarator (declarator, type_specifiers, context, 0, &attrs);
5136 if (attrs && type != error_mark_node)
5137 {
5138 if (CLASS_TYPE_P (type))
5139 warning (OPT_Wattributes, "ignoring attributes applied to class type %qT "
5140 "outside of definition", type);
5141 else if (MAYBE_CLASS_TYPE_P (type))
5142 /* A template type parameter or other dependent type. */
5143 warning (OPT_Wattributes, "ignoring attributes applied to dependent "
5144 "type %qT without an associated declaration", type);
5145 else
5146 cplus_decl_attributes (&type, attrs, 0);
5147 }
5148 return type;
5149 }
5150
5151 /* Process a DECLARATOR for a function-scope variable declaration,
5152 namespace-scope variable declaration, or function declaration.
5153 (Function definitions go through start_function; class member
5154 declarations appearing in the body of the class go through
5155 grokfield.) The DECL corresponding to the DECLARATOR is returned.
5156 If an error occurs, the error_mark_node is returned instead.
5157
5158 DECLSPECS are the decl-specifiers for the declaration. INITIALIZED is
5159 SD_INITIALIZED if an explicit initializer is present, or SD_DEFAULTED
5160 for an explicitly defaulted function, or SD_DELETED for an explicitly
5161 deleted function, but 0 (SD_UNINITIALIZED) if this is a variable
5162 implicitly initialized via a default constructor. ATTRIBUTES and
5163 PREFIX_ATTRIBUTES are GNU attributes associated with this declaration.
5164
5165 The scope represented by the context of the returned DECL is pushed
5166 (if it is not the global namespace) and is assigned to
5167 *PUSHED_SCOPE_P. The caller is then responsible for calling
5168 pop_scope on *PUSHED_SCOPE_P if it is set. */
5169
5170 tree
5171 start_decl (const cp_declarator *declarator,
5172 cp_decl_specifier_seq *declspecs,
5173 int initialized,
5174 tree attributes,
5175 tree prefix_attributes,
5176 tree *pushed_scope_p)
5177 {
5178 tree decl;
5179 tree context;
5180 bool was_public;
5181 int flags;
5182 bool alias;
5183
5184 *pushed_scope_p = NULL_TREE;
5185
5186 /* An object declared as __attribute__((deprecated)) suppresses
5187 warnings of uses of other deprecated items. */
5188 if (lookup_attribute ("deprecated", attributes))
5189 deprecated_state = DEPRECATED_SUPPRESS;
5190
5191 attributes = chainon (attributes, prefix_attributes);
5192
5193 decl = grokdeclarator (declarator, declspecs, NORMAL, initialized,
5194 &attributes);
5195
5196 deprecated_state = DEPRECATED_NORMAL;
5197
5198 if (decl == NULL_TREE || VOID_TYPE_P (decl)
5199 || decl == error_mark_node)
5200 return error_mark_node;
5201
5202 context = CP_DECL_CONTEXT (decl);
5203 if (context != global_namespace)
5204 *pushed_scope_p = push_scope (context);
5205
5206 /* Is it valid for this decl to have an initializer at all?
5207 If not, set INITIALIZED to zero, which will indirectly
5208 tell `cp_finish_decl' to ignore the initializer once it is parsed. */
5209 if (initialized
5210 && TREE_CODE (decl) == TYPE_DECL)
5211 {
5212 error_at (DECL_SOURCE_LOCATION (decl),
5213 "typedef %qD is initialized (use %qs instead)",
5214 decl, "decltype");
5215 return error_mark_node;
5216 }
5217
5218 if (initialized)
5219 {
5220 if (! toplevel_bindings_p ()
5221 && DECL_EXTERNAL (decl))
5222 warning (0, "declaration of %q#D has %<extern%> and is initialized",
5223 decl);
5224 DECL_EXTERNAL (decl) = 0;
5225 if (toplevel_bindings_p ())
5226 TREE_STATIC (decl) = 1;
5227 }
5228 alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl)) != 0;
5229
5230 if (alias && TREE_CODE (decl) == FUNCTION_DECL)
5231 record_key_method_defined (decl);
5232
5233 /* If this is a typedef that names the class for linkage purposes
5234 (7.1.3p8), apply any attributes directly to the type. */
5235 if (TREE_CODE (decl) == TYPE_DECL
5236 && OVERLOAD_TYPE_P (TREE_TYPE (decl))
5237 && decl == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (decl))))
5238 flags = ATTR_FLAG_TYPE_IN_PLACE;
5239 else
5240 flags = 0;
5241
5242 /* Set attributes here so if duplicate decl, will have proper attributes. */
5243 cplus_decl_attributes (&decl, attributes, flags);
5244
5245 /* Dllimported symbols cannot be defined. Static data members (which
5246 can be initialized in-class and dllimported) go through grokfield,
5247 not here, so we don't need to exclude those decls when checking for
5248 a definition. */
5249 if (initialized && DECL_DLLIMPORT_P (decl))
5250 {
5251 error_at (DECL_SOURCE_LOCATION (decl),
5252 "definition of %q#D is marked %<dllimport%>", decl);
5253 DECL_DLLIMPORT_P (decl) = 0;
5254 }
5255
5256 /* If #pragma weak was used, mark the decl weak now. */
5257 if (!processing_template_decl && !DECL_DECOMPOSITION_P (decl))
5258 maybe_apply_pragma_weak (decl);
5259
5260 if (TREE_CODE (decl) == FUNCTION_DECL
5261 && DECL_DECLARED_INLINE_P (decl)
5262 && DECL_UNINLINABLE (decl)
5263 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
5264 warning_at (DECL_SOURCE_LOCATION (decl), 0,
5265 "inline function %qD given attribute %qs", decl, "noinline");
5266
5267 if (TYPE_P (context) && COMPLETE_TYPE_P (complete_type (context)))
5268 {
5269 bool this_tmpl = (processing_template_decl
5270 > template_class_depth (context));
5271 if (VAR_P (decl))
5272 {
5273 tree field = lookup_field (context, DECL_NAME (decl), 0, false);
5274 if (field == NULL_TREE
5275 || !(VAR_P (field) || variable_template_p (field)))
5276 error ("%q+#D is not a static data member of %q#T", decl, context);
5277 else if (variable_template_p (field)
5278 && (DECL_LANG_SPECIFIC (decl)
5279 && DECL_TEMPLATE_SPECIALIZATION (decl)))
5280 /* OK, specialization was already checked. */;
5281 else if (variable_template_p (field) && !this_tmpl)
5282 {
5283 error_at (DECL_SOURCE_LOCATION (decl),
5284 "non-member-template declaration of %qD", decl);
5285 inform (DECL_SOURCE_LOCATION (field), "does not match "
5286 "member template declaration here");
5287 return error_mark_node;
5288 }
5289 else
5290 {
5291 if (variable_template_p (field))
5292 field = DECL_TEMPLATE_RESULT (field);
5293
5294 if (DECL_CONTEXT (field) != context)
5295 {
5296 if (!same_type_p (DECL_CONTEXT (field), context))
5297 permerror (input_location, "ISO C++ does not permit %<%T::%D%> "
5298 "to be defined as %<%T::%D%>",
5299 DECL_CONTEXT (field), DECL_NAME (decl),
5300 context, DECL_NAME (decl));
5301 DECL_CONTEXT (decl) = DECL_CONTEXT (field);
5302 }
5303 /* Static data member are tricky; an in-class initialization
5304 still doesn't provide a definition, so the in-class
5305 declaration will have DECL_EXTERNAL set, but will have an
5306 initialization. Thus, duplicate_decls won't warn
5307 about this situation, and so we check here. */
5308 if (initialized && DECL_INITIALIZED_IN_CLASS_P (field))
5309 error ("duplicate initialization of %qD", decl);
5310 field = duplicate_decls (decl, field,
5311 /*newdecl_is_friend=*/false);
5312 if (field == error_mark_node)
5313 return error_mark_node;
5314 else if (field)
5315 decl = field;
5316 }
5317 }
5318 else
5319 {
5320 tree field = check_classfn (context, decl,
5321 this_tmpl
5322 ? current_template_parms
5323 : NULL_TREE);
5324 if (field && field != error_mark_node
5325 && duplicate_decls (decl, field,
5326 /*newdecl_is_friend=*/false))
5327 decl = field;
5328 }
5329
5330 /* cp_finish_decl sets DECL_EXTERNAL if DECL_IN_AGGR_P is set. */
5331 DECL_IN_AGGR_P (decl) = 0;
5332 /* Do not mark DECL as an explicit specialization if it was not
5333 already marked as an instantiation; a declaration should
5334 never be marked as a specialization unless we know what
5335 template is being specialized. */
5336 if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
5337 {
5338 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
5339 if (TREE_CODE (decl) == FUNCTION_DECL)
5340 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
5341 && DECL_DECLARED_INLINE_P (decl));
5342 else
5343 DECL_COMDAT (decl) = false;
5344
5345 /* [temp.expl.spec] An explicit specialization of a static data
5346 member of a template is a definition if the declaration
5347 includes an initializer; otherwise, it is a declaration.
5348
5349 We check for processing_specialization so this only applies
5350 to the new specialization syntax. */
5351 if (!initialized && processing_specialization)
5352 DECL_EXTERNAL (decl) = 1;
5353 }
5354
5355 if (DECL_EXTERNAL (decl) && ! DECL_TEMPLATE_SPECIALIZATION (decl)
5356 /* Aliases are definitions. */
5357 && !alias)
5358 permerror (declarator->id_loc,
5359 "declaration of %q#D outside of class is not definition",
5360 decl);
5361 }
5362
5363 was_public = TREE_PUBLIC (decl);
5364
5365 /* Enter this declaration into the symbol table. Don't push the plain
5366 VAR_DECL for a variable template. */
5367 if (!template_parm_scope_p ()
5368 || !VAR_P (decl))
5369 decl = maybe_push_decl (decl);
5370
5371 if (processing_template_decl)
5372 {
5373 /* Make sure that for a `constinit' decl push_template_decl creates
5374 a DECL_TEMPLATE_INFO info for us, so that cp_finish_decl can then set
5375 TINFO_VAR_DECLARED_CONSTINIT. */
5376 if (decl_spec_seq_has_spec_p (declspecs, ds_constinit))
5377 retrofit_lang_decl (decl);
5378 decl = push_template_decl (decl);
5379 }
5380 if (decl == error_mark_node)
5381 return error_mark_node;
5382
5383 if (VAR_P (decl)
5384 && DECL_NAMESPACE_SCOPE_P (decl) && !TREE_PUBLIC (decl) && !was_public
5385 && !DECL_THIS_STATIC (decl) && !DECL_ARTIFICIAL (decl))
5386 {
5387 /* This is a const variable with implicit 'static'. Set
5388 DECL_THIS_STATIC so we can tell it from variables that are
5389 !TREE_PUBLIC because of the anonymous namespace. */
5390 gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (decl)) || errorcount);
5391 DECL_THIS_STATIC (decl) = 1;
5392 }
5393
5394 if (current_function_decl && VAR_P (decl)
5395 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
5396 {
5397 bool ok = false;
5398 if (CP_DECL_THREAD_LOCAL_P (decl))
5399 error_at (DECL_SOURCE_LOCATION (decl),
5400 "%qD declared %<thread_local%> in %<constexpr%> function",
5401 decl);
5402 else if (TREE_STATIC (decl))
5403 error_at (DECL_SOURCE_LOCATION (decl),
5404 "%qD declared %<static%> in %<constexpr%> function", decl);
5405 else
5406 ok = true;
5407 if (!ok)
5408 cp_function_chain->invalid_constexpr = true;
5409 }
5410
5411 if (!processing_template_decl && VAR_P (decl))
5412 start_decl_1 (decl, initialized);
5413
5414 return decl;
5415 }
5416
5417 /* Process the declaration of a variable DECL. INITIALIZED is true
5418 iff DECL is explicitly initialized. (INITIALIZED is false if the
5419 variable is initialized via an implicitly-called constructor.)
5420 This function must be called for ordinary variables (including, for
5421 example, implicit instantiations of templates), but must not be
5422 called for template declarations. */
5423
5424 void
5425 start_decl_1 (tree decl, bool initialized)
5426 {
5427 tree type;
5428 bool complete_p;
5429 bool aggregate_definition_p;
5430
5431 gcc_assert (!processing_template_decl);
5432
5433 if (error_operand_p (decl))
5434 return;
5435
5436 gcc_assert (VAR_P (decl));
5437
5438 type = TREE_TYPE (decl);
5439 complete_p = COMPLETE_TYPE_P (type);
5440 aggregate_definition_p = MAYBE_CLASS_TYPE_P (type) && !DECL_EXTERNAL (decl);
5441
5442 /* If an explicit initializer is present, or if this is a definition
5443 of an aggregate, then we need a complete type at this point.
5444 (Scalars are always complete types, so there is nothing to
5445 check.) This code just sets COMPLETE_P; errors (if necessary)
5446 are issued below. */
5447 if ((initialized || aggregate_definition_p)
5448 && !complete_p
5449 && COMPLETE_TYPE_P (complete_type (type)))
5450 {
5451 complete_p = true;
5452 /* We will not yet have set TREE_READONLY on DECL if the type
5453 was "const", but incomplete, before this point. But, now, we
5454 have a complete type, so we can try again. */
5455 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
5456 }
5457
5458 if (initialized)
5459 /* Is it valid for this decl to have an initializer at all? */
5460 {
5461 /* Don't allow initializations for incomplete types except for
5462 arrays which might be completed by the initialization. */
5463 if (complete_p)
5464 ; /* A complete type is ok. */
5465 else if (type_uses_auto (type))
5466 ; /* An auto type is ok. */
5467 else if (TREE_CODE (type) != ARRAY_TYPE)
5468 {
5469 error ("variable %q#D has initializer but incomplete type", decl);
5470 type = TREE_TYPE (decl) = error_mark_node;
5471 }
5472 else if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
5473 {
5474 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
5475 error ("elements of array %q#D have incomplete type", decl);
5476 /* else we already gave an error in start_decl. */
5477 }
5478 }
5479 else if (aggregate_definition_p && !complete_p)
5480 {
5481 if (type_uses_auto (type))
5482 gcc_assert (CLASS_PLACEHOLDER_TEMPLATE (type));
5483 else
5484 {
5485 error ("aggregate %q#D has incomplete type and cannot be defined",
5486 decl);
5487 /* Change the type so that assemble_variable will give
5488 DECL an rtl we can live with: (mem (const_int 0)). */
5489 type = TREE_TYPE (decl) = error_mark_node;
5490 }
5491 }
5492
5493 /* Create a new scope to hold this declaration if necessary.
5494 Whether or not a new scope is necessary cannot be determined
5495 until after the type has been completed; if the type is a
5496 specialization of a class template it is not until after
5497 instantiation has occurred that TYPE_HAS_NONTRIVIAL_DESTRUCTOR
5498 will be set correctly. */
5499 maybe_push_cleanup_level (type);
5500 }
5501
5502 /* Handle initialization of references. DECL, TYPE, and INIT have the
5503 same meaning as in cp_finish_decl. *CLEANUP must be NULL on entry,
5504 but will be set to a new CLEANUP_STMT if a temporary is created
5505 that must be destroyed subsequently.
5506
5507 Returns an initializer expression to use to initialize DECL, or
5508 NULL if the initialization can be performed statically.
5509
5510 Quotes on semantics can be found in ARM 8.4.3. */
5511
5512 static tree
5513 grok_reference_init (tree decl, tree type, tree init, int flags)
5514 {
5515 if (init == NULL_TREE)
5516 {
5517 if ((DECL_LANG_SPECIFIC (decl) == 0
5518 || DECL_IN_AGGR_P (decl) == 0)
5519 && ! DECL_THIS_EXTERN (decl))
5520 error_at (DECL_SOURCE_LOCATION (decl),
5521 "%qD declared as reference but not initialized", decl);
5522 return NULL_TREE;
5523 }
5524
5525 if (TREE_CODE (init) == TREE_LIST)
5526 init = build_x_compound_expr_from_list (init, ELK_INIT,
5527 tf_warning_or_error);
5528
5529 tree ttype = TREE_TYPE (type);
5530 if (TREE_CODE (ttype) != ARRAY_TYPE
5531 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
5532 /* Note: default conversion is only called in very special cases. */
5533 init = decay_conversion (init, tf_warning_or_error);
5534
5535 /* check_initializer handles this for non-reference variables, but for
5536 references we need to do it here or the initializer will get the
5537 incomplete array type and confuse later calls to
5538 cp_complete_array_type. */
5539 if (TREE_CODE (ttype) == ARRAY_TYPE
5540 && TYPE_DOMAIN (ttype) == NULL_TREE
5541 && (BRACE_ENCLOSED_INITIALIZER_P (init)
5542 || TREE_CODE (init) == STRING_CST))
5543 {
5544 cp_complete_array_type (&ttype, init, false);
5545 if (ttype != TREE_TYPE (type))
5546 type = cp_build_reference_type (ttype, TYPE_REF_IS_RVALUE (type));
5547 }
5548
5549 /* Convert INIT to the reference type TYPE. This may involve the
5550 creation of a temporary, whose lifetime must be the same as that
5551 of the reference. If so, a DECL_EXPR for the temporary will be
5552 added just after the DECL_EXPR for DECL. That's why we don't set
5553 DECL_INITIAL for local references (instead assigning to them
5554 explicitly); we need to allow the temporary to be initialized
5555 first. */
5556 return initialize_reference (type, init, flags,
5557 tf_warning_or_error);
5558 }
5559
5560 /* Designated initializers in arrays are not supported in GNU C++.
5561 The parser cannot detect this error since it does not know whether
5562 a given brace-enclosed initializer is for a class type or for an
5563 array. This function checks that CE does not use a designated
5564 initializer. If it does, an error is issued. Returns true if CE
5565 is valid, i.e., does not have a designated initializer. */
5566
5567 bool
5568 check_array_designated_initializer (constructor_elt *ce,
5569 unsigned HOST_WIDE_INT index)
5570 {
5571 /* Designated initializers for array elements are not supported. */
5572 if (ce->index)
5573 {
5574 /* The parser only allows identifiers as designated
5575 initializers. */
5576 if (ce->index == error_mark_node)
5577 {
5578 error ("name used in a GNU-style designated "
5579 "initializer for an array");
5580 return false;
5581 }
5582 else if (identifier_p (ce->index))
5583 {
5584 error ("name %qD used in a GNU-style designated "
5585 "initializer for an array", ce->index);
5586 return false;
5587 }
5588
5589 tree ce_index = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5590 ce->index, true);
5591 if (ce_index
5592 && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (ce_index))
5593 && (TREE_CODE (ce_index = fold_non_dependent_expr (ce_index))
5594 == INTEGER_CST))
5595 {
5596 /* A C99 designator is OK if it matches the current index. */
5597 if (wi::to_wide (ce_index) == index)
5598 {
5599 ce->index = ce_index;
5600 return true;
5601 }
5602 else
5603 sorry ("non-trivial designated initializers not supported");
5604 }
5605 else
5606 error_at (cp_expr_loc_or_input_loc (ce->index),
5607 "C99 designator %qE is not an integral constant-expression",
5608 ce->index);
5609
5610 return false;
5611 }
5612
5613 return true;
5614 }
5615
5616 /* When parsing `int a[] = {1, 2};' we don't know the size of the
5617 array until we finish parsing the initializer. If that's the
5618 situation we're in, update DECL accordingly. */
5619
5620 static void
5621 maybe_deduce_size_from_array_init (tree decl, tree init)
5622 {
5623 tree type = TREE_TYPE (decl);
5624
5625 if (TREE_CODE (type) == ARRAY_TYPE
5626 && TYPE_DOMAIN (type) == NULL_TREE
5627 && TREE_CODE (decl) != TYPE_DECL)
5628 {
5629 /* do_default is really a C-ism to deal with tentative definitions.
5630 But let's leave it here to ease the eventual merge. */
5631 int do_default = !DECL_EXTERNAL (decl);
5632 tree initializer = init ? init : DECL_INITIAL (decl);
5633 int failure = 0;
5634
5635 /* Check that there are no designated initializers in INIT, as
5636 those are not supported in GNU C++, and as the middle-end
5637 will crash if presented with a non-numeric designated
5638 initializer. */
5639 if (initializer && BRACE_ENCLOSED_INITIALIZER_P (initializer))
5640 {
5641 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initializer);
5642 constructor_elt *ce;
5643 HOST_WIDE_INT i;
5644 FOR_EACH_VEC_SAFE_ELT (v, i, ce)
5645 {
5646 if (instantiation_dependent_expression_p (ce->index))
5647 return;
5648 if (!check_array_designated_initializer (ce, i))
5649 failure = 1;
5650 }
5651 }
5652
5653 if (failure)
5654 TREE_TYPE (decl) = error_mark_node;
5655 else
5656 {
5657 failure = cp_complete_array_type (&TREE_TYPE (decl), initializer,
5658 do_default);
5659 if (failure == 1)
5660 {
5661 error_at (cp_expr_loc_or_loc (initializer,
5662 DECL_SOURCE_LOCATION (decl)),
5663 "initializer fails to determine size of %qD", decl);
5664 }
5665 else if (failure == 2)
5666 {
5667 if (do_default)
5668 {
5669 error_at (DECL_SOURCE_LOCATION (decl),
5670 "array size missing in %qD", decl);
5671 }
5672 /* If a `static' var's size isn't known, make it extern as
5673 well as static, so it does not get allocated. If it's not
5674 `static', then don't mark it extern; finish_incomplete_decl
5675 will give it a default size and it will get allocated. */
5676 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
5677 DECL_EXTERNAL (decl) = 1;
5678 }
5679 else if (failure == 3)
5680 {
5681 error_at (DECL_SOURCE_LOCATION (decl),
5682 "zero-size array %qD", decl);
5683 }
5684 }
5685
5686 cp_apply_type_quals_to_decl (cp_type_quals (TREE_TYPE (decl)), decl);
5687
5688 relayout_decl (decl);
5689 }
5690 }
5691
5692 /* Set DECL_SIZE, DECL_ALIGN, etc. for DECL (a VAR_DECL), and issue
5693 any appropriate error messages regarding the layout. */
5694
5695 static void
5696 layout_var_decl (tree decl)
5697 {
5698 tree type;
5699
5700 type = TREE_TYPE (decl);
5701 if (type == error_mark_node)
5702 return;
5703
5704 /* If we haven't already laid out this declaration, do so now.
5705 Note that we must not call complete type for an external object
5706 because it's type might involve templates that we are not
5707 supposed to instantiate yet. (And it's perfectly valid to say
5708 `extern X x' for some incomplete type `X'.) */
5709 if (!DECL_EXTERNAL (decl))
5710 complete_type (type);
5711 if (!DECL_SIZE (decl)
5712 && TREE_TYPE (decl) != error_mark_node
5713 && complete_or_array_type_p (type))
5714 layout_decl (decl, 0);
5715
5716 if (!DECL_EXTERNAL (decl) && DECL_SIZE (decl) == NULL_TREE)
5717 {
5718 /* An automatic variable with an incomplete type: that is an error.
5719 Don't talk about array types here, since we took care of that
5720 message in grokdeclarator. */
5721 error_at (DECL_SOURCE_LOCATION (decl),
5722 "storage size of %qD isn%'t known", decl);
5723 TREE_TYPE (decl) = error_mark_node;
5724 }
5725 #if 0
5726 /* Keep this code around in case we later want to control debug info
5727 based on whether a type is "used". (jason 1999-11-11) */
5728
5729 else if (!DECL_EXTERNAL (decl) && MAYBE_CLASS_TYPE_P (ttype))
5730 /* Let debugger know it should output info for this type. */
5731 note_debug_info_needed (ttype);
5732
5733 if (TREE_STATIC (decl) && DECL_CLASS_SCOPE_P (decl))
5734 note_debug_info_needed (DECL_CONTEXT (decl));
5735 #endif
5736
5737 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
5738 && DECL_SIZE (decl) != NULL_TREE
5739 && ! TREE_CONSTANT (DECL_SIZE (decl)))
5740 {
5741 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
5742 constant_expression_warning (DECL_SIZE (decl));
5743 else
5744 {
5745 error_at (DECL_SOURCE_LOCATION (decl),
5746 "storage size of %qD isn%'t constant", decl);
5747 TREE_TYPE (decl) = error_mark_node;
5748 }
5749 }
5750 }
5751
5752 /* If a local static variable is declared in an inline function, or if
5753 we have a weak definition, we must endeavor to create only one
5754 instance of the variable at link-time. */
5755
5756 void
5757 maybe_commonize_var (tree decl)
5758 {
5759 /* Don't mess with __FUNCTION__ and similar. */
5760 if (DECL_ARTIFICIAL (decl))
5761 return;
5762
5763 /* Static data in a function with comdat linkage also has comdat
5764 linkage. */
5765 if ((TREE_STATIC (decl)
5766 && DECL_FUNCTION_SCOPE_P (decl)
5767 && vague_linkage_p (DECL_CONTEXT (decl)))
5768 || (TREE_PUBLIC (decl) && DECL_INLINE_VAR_P (decl)))
5769 {
5770 if (flag_weak)
5771 {
5772 /* With weak symbols, we simply make the variable COMDAT;
5773 that will cause copies in multiple translations units to
5774 be merged. */
5775 comdat_linkage (decl);
5776 }
5777 else
5778 {
5779 if (DECL_INITIAL (decl) == NULL_TREE
5780 || DECL_INITIAL (decl) == error_mark_node)
5781 {
5782 /* Without weak symbols, we can use COMMON to merge
5783 uninitialized variables. */
5784 TREE_PUBLIC (decl) = 1;
5785 DECL_COMMON (decl) = 1;
5786 }
5787 else
5788 {
5789 /* While for initialized variables, we must use internal
5790 linkage -- which means that multiple copies will not
5791 be merged. */
5792 TREE_PUBLIC (decl) = 0;
5793 DECL_COMMON (decl) = 0;
5794 DECL_INTERFACE_KNOWN (decl) = 1;
5795 const char *msg;
5796 if (DECL_INLINE_VAR_P (decl))
5797 msg = G_("sorry: semantics of inline variable "
5798 "%q#D are wrong (you%'ll wind up with "
5799 "multiple copies)");
5800 else
5801 msg = G_("sorry: semantics of inline function "
5802 "static data %q#D are wrong (you%'ll wind "
5803 "up with multiple copies)");
5804 if (warning_at (DECL_SOURCE_LOCATION (decl), 0,
5805 msg, decl))
5806 inform (DECL_SOURCE_LOCATION (decl),
5807 "you can work around this by removing the initializer");
5808 }
5809 }
5810 }
5811 }
5812
5813 /* Issue an error message if DECL is an uninitialized const variable.
5814 CONSTEXPR_CONTEXT_P is true when the function is called in a constexpr
5815 context from potential_constant_expression. Returns true if all is well,
5816 false otherwise. */
5817
5818 bool
5819 check_for_uninitialized_const_var (tree decl, bool constexpr_context_p,
5820 tsubst_flags_t complain)
5821 {
5822 tree type = strip_array_types (TREE_TYPE (decl));
5823
5824 /* ``Unless explicitly declared extern, a const object does not have
5825 external linkage and must be initialized. ($8.4; $12.1)'' ARM
5826 7.1.6 */
5827 if (VAR_P (decl)
5828 && !TYPE_REF_P (type)
5829 && (constexpr_context_p
5830 || CP_TYPE_CONST_P (type) || var_in_constexpr_fn (decl))
5831 && !DECL_NONTRIVIALLY_INITIALIZED_P (decl))
5832 {
5833 tree field = default_init_uninitialized_part (type);
5834 if (!field)
5835 return true;
5836
5837 bool show_notes = true;
5838
5839 if (!constexpr_context_p)
5840 {
5841 if (CP_TYPE_CONST_P (type))
5842 {
5843 if (complain & tf_error)
5844 show_notes = permerror (DECL_SOURCE_LOCATION (decl),
5845 "uninitialized %<const %D%>", decl);
5846 }
5847 else
5848 {
5849 if (!is_instantiation_of_constexpr (current_function_decl)
5850 && (complain & tf_error))
5851 error_at (DECL_SOURCE_LOCATION (decl),
5852 "uninitialized variable %qD in %<constexpr%> "
5853 "function", decl);
5854 else
5855 show_notes = false;
5856 cp_function_chain->invalid_constexpr = true;
5857 }
5858 }
5859 else if (complain & tf_error)
5860 error_at (DECL_SOURCE_LOCATION (decl),
5861 "uninitialized variable %qD in %<constexpr%> context",
5862 decl);
5863
5864 if (show_notes && CLASS_TYPE_P (type) && (complain & tf_error))
5865 {
5866 tree defaulted_ctor;
5867
5868 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
5869 "%q#T has no user-provided default constructor", type);
5870 defaulted_ctor = in_class_defaulted_default_constructor (type);
5871 if (defaulted_ctor)
5872 inform (DECL_SOURCE_LOCATION (defaulted_ctor),
5873 "constructor is not user-provided because it is "
5874 "explicitly defaulted in the class body");
5875 inform (DECL_SOURCE_LOCATION (field),
5876 "and the implicitly-defined constructor does not "
5877 "initialize %q#D", field);
5878 }
5879
5880 return false;
5881 }
5882
5883 return true;
5884 }
5885 \f
5886 /* Structure holding the current initializer being processed by reshape_init.
5887 CUR is a pointer to the current element being processed, END is a pointer
5888 after the last element present in the initializer. */
5889 struct reshape_iter
5890 {
5891 constructor_elt *cur;
5892 constructor_elt *end;
5893 };
5894
5895 static tree reshape_init_r (tree, reshape_iter *, bool, tsubst_flags_t);
5896
5897 /* FIELD is a FIELD_DECL or NULL. In the former case, the value
5898 returned is the next FIELD_DECL (possibly FIELD itself) that can be
5899 initialized. If there are no more such fields, the return value
5900 will be NULL. */
5901
5902 tree
5903 next_initializable_field (tree field)
5904 {
5905 while (field
5906 && (TREE_CODE (field) != FIELD_DECL
5907 || DECL_UNNAMED_BIT_FIELD (field)
5908 || (DECL_ARTIFICIAL (field)
5909 && !(cxx_dialect >= cxx17 && DECL_FIELD_IS_BASE (field)))))
5910 field = DECL_CHAIN (field);
5911
5912 return field;
5913 }
5914
5915 /* Return true for [dcl.init.list] direct-list-initialization from
5916 single element of enumeration with a fixed underlying type. */
5917
5918 bool
5919 is_direct_enum_init (tree type, tree init)
5920 {
5921 if (cxx_dialect >= cxx17
5922 && TREE_CODE (type) == ENUMERAL_TYPE
5923 && ENUM_FIXED_UNDERLYING_TYPE_P (type)
5924 && TREE_CODE (init) == CONSTRUCTOR
5925 && CONSTRUCTOR_IS_DIRECT_INIT (init)
5926 && CONSTRUCTOR_NELTS (init) == 1)
5927 return true;
5928 return false;
5929 }
5930
5931 /* Subroutine of reshape_init_array and reshape_init_vector, which does
5932 the actual work. ELT_TYPE is the element type of the array. MAX_INDEX is an
5933 INTEGER_CST representing the size of the array minus one (the maximum index),
5934 or NULL_TREE if the array was declared without specifying the size. D is
5935 the iterator within the constructor. */
5936
5937 static tree
5938 reshape_init_array_1 (tree elt_type, tree max_index, reshape_iter *d,
5939 tsubst_flags_t complain)
5940 {
5941 tree new_init;
5942 bool sized_array_p = (max_index && TREE_CONSTANT (max_index));
5943 unsigned HOST_WIDE_INT max_index_cst = 0;
5944 unsigned HOST_WIDE_INT index;
5945
5946 /* The initializer for an array is always a CONSTRUCTOR. */
5947 new_init = build_constructor (init_list_type_node, NULL);
5948
5949 if (sized_array_p)
5950 {
5951 /* Minus 1 is used for zero sized arrays. */
5952 if (integer_all_onesp (max_index))
5953 return new_init;
5954
5955 if (tree_fits_uhwi_p (max_index))
5956 max_index_cst = tree_to_uhwi (max_index);
5957 /* sizetype is sign extended, not zero extended. */
5958 else
5959 max_index_cst = tree_to_uhwi (fold_convert (size_type_node, max_index));
5960 }
5961
5962 /* Set to the index of the last element with a non-zero initializer.
5963 Zero initializers for elements past this one can be dropped. */
5964 unsigned HOST_WIDE_INT last_nonzero = -1;
5965 /* Loop until there are no more initializers. */
5966 for (index = 0;
5967 d->cur != d->end && (!sized_array_p || index <= max_index_cst);
5968 ++index)
5969 {
5970 tree elt_init;
5971 constructor_elt *old_cur = d->cur;
5972
5973 check_array_designated_initializer (d->cur, index);
5974 elt_init = reshape_init_r (elt_type, d, /*first_initializer_p=*/false,
5975 complain);
5976 if (elt_init == error_mark_node)
5977 return error_mark_node;
5978 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init),
5979 size_int (index), elt_init);
5980 if (!TREE_CONSTANT (elt_init))
5981 TREE_CONSTANT (new_init) = false;
5982
5983 /* Pointers initialized to strings must be treated as non-zero
5984 even if the string is empty. */
5985 tree init_type = TREE_TYPE (elt_init);
5986 if ((POINTER_TYPE_P (elt_type) != POINTER_TYPE_P (init_type)))
5987 last_nonzero = index;
5988 else if (!type_initializer_zero_p (elt_type, elt_init))
5989 last_nonzero = index;
5990
5991 /* This can happen with an invalid initializer (c++/54501). */
5992 if (d->cur == old_cur && !sized_array_p)
5993 break;
5994 }
5995
5996 if (sized_array_p && trivial_type_p (elt_type))
5997 {
5998 /* Strip trailing zero-initializers from an array of a trivial
5999 type of known size. They are redundant and get in the way
6000 of telling them apart from those with implicit zero value. */
6001 unsigned HOST_WIDE_INT nelts = CONSTRUCTOR_NELTS (new_init);
6002 if (last_nonzero > nelts)
6003 nelts = 0;
6004 else if (last_nonzero < nelts - 1)
6005 nelts = last_nonzero + 1;
6006
6007 vec_safe_truncate (CONSTRUCTOR_ELTS (new_init), nelts);
6008 }
6009
6010 return new_init;
6011 }
6012
6013 /* Subroutine of reshape_init_r, processes the initializers for arrays.
6014 Parameters are the same of reshape_init_r. */
6015
6016 static tree
6017 reshape_init_array (tree type, reshape_iter *d, tsubst_flags_t complain)
6018 {
6019 tree max_index = NULL_TREE;
6020
6021 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
6022
6023 if (TYPE_DOMAIN (type))
6024 max_index = array_type_nelts (type);
6025
6026 return reshape_init_array_1 (TREE_TYPE (type), max_index, d, complain);
6027 }
6028
6029 /* Subroutine of reshape_init_r, processes the initializers for vectors.
6030 Parameters are the same of reshape_init_r. */
6031
6032 static tree
6033 reshape_init_vector (tree type, reshape_iter *d, tsubst_flags_t complain)
6034 {
6035 tree max_index = NULL_TREE;
6036
6037 gcc_assert (VECTOR_TYPE_P (type));
6038
6039 if (COMPOUND_LITERAL_P (d->cur->value))
6040 {
6041 tree value = d->cur->value;
6042 if (!same_type_p (TREE_TYPE (value), type))
6043 {
6044 if (complain & tf_error)
6045 error ("invalid type %qT as initializer for a vector of type %qT",
6046 TREE_TYPE (d->cur->value), type);
6047 value = error_mark_node;
6048 }
6049 ++d->cur;
6050 return value;
6051 }
6052
6053 /* For a vector, we initialize it as an array of the appropriate size. */
6054 if (VECTOR_TYPE_P (type))
6055 max_index = size_int (TYPE_VECTOR_SUBPARTS (type) - 1);
6056
6057 return reshape_init_array_1 (TREE_TYPE (type), max_index, d, complain);
6058 }
6059
6060 /* Subroutine of reshape_init_r, processes the initializers for classes
6061 or union. Parameters are the same of reshape_init_r. */
6062
6063 static tree
6064 reshape_init_class (tree type, reshape_iter *d, bool first_initializer_p,
6065 tsubst_flags_t complain)
6066 {
6067 tree field;
6068 tree new_init;
6069
6070 gcc_assert (CLASS_TYPE_P (type));
6071
6072 /* The initializer for a class is always a CONSTRUCTOR. */
6073 new_init = build_constructor (init_list_type_node, NULL);
6074 field = next_initializable_field (TYPE_FIELDS (type));
6075
6076 if (!field)
6077 {
6078 /* [dcl.init.aggr]
6079
6080 An initializer for an aggregate member that is an
6081 empty class shall have the form of an empty
6082 initializer-list {}. */
6083 if (!first_initializer_p)
6084 {
6085 if (complain & tf_error)
6086 error ("initializer for %qT must be brace-enclosed", type);
6087 return error_mark_node;
6088 }
6089 return new_init;
6090 }
6091
6092 /* Loop through the initializable fields, gathering initializers. */
6093 while (d->cur != d->end)
6094 {
6095 tree field_init;
6096 constructor_elt *old_cur = d->cur;
6097
6098 /* Handle designated initializers, as an extension. */
6099 if (d->cur->index)
6100 {
6101 if (d->cur->index == error_mark_node)
6102 return error_mark_node;
6103
6104 if (TREE_CODE (d->cur->index) == FIELD_DECL)
6105 {
6106 /* We already reshaped this. */
6107 if (field != d->cur->index)
6108 {
6109 tree id = DECL_NAME (d->cur->index);
6110 gcc_assert (id);
6111 gcc_checking_assert (d->cur->index
6112 == get_class_binding (type, id));
6113 field = d->cur->index;
6114 }
6115 }
6116 else if (TREE_CODE (d->cur->index) == IDENTIFIER_NODE)
6117 field = get_class_binding (type, d->cur->index);
6118 else
6119 {
6120 if (complain & tf_error)
6121 error ("%<[%E] =%> used in a GNU-style designated initializer"
6122 " for class %qT", d->cur->index, type);
6123 return error_mark_node;
6124 }
6125
6126 if (!field || TREE_CODE (field) != FIELD_DECL)
6127 {
6128 if (complain & tf_error)
6129 error ("%qT has no non-static data member named %qD", type,
6130 d->cur->index);
6131 return error_mark_node;
6132 }
6133 }
6134
6135 /* If we processed all the member of the class, we are done. */
6136 if (!field)
6137 break;
6138
6139 field_init = reshape_init_r (TREE_TYPE (field), d,
6140 /*first_initializer_p=*/false, complain);
6141 if (field_init == error_mark_node)
6142 return error_mark_node;
6143
6144 if (d->cur == old_cur && d->cur->index)
6145 {
6146 /* This can happen with an invalid initializer for a flexible
6147 array member (c++/54441). */
6148 if (complain & tf_error)
6149 error ("invalid initializer for %q#D", field);
6150 return error_mark_node;
6151 }
6152
6153 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init), field, field_init);
6154
6155 /* [dcl.init.aggr]
6156
6157 When a union is initialized with a brace-enclosed
6158 initializer, the braces shall only contain an
6159 initializer for the first member of the union. */
6160 if (TREE_CODE (type) == UNION_TYPE)
6161 break;
6162
6163 field = next_initializable_field (DECL_CHAIN (field));
6164 }
6165
6166 return new_init;
6167 }
6168
6169 /* Subroutine of reshape_init_r. We're in a context where C99 initializer
6170 designators are not valid; either complain or return true to indicate
6171 that reshape_init_r should return error_mark_node. */
6172
6173 static bool
6174 has_designator_problem (reshape_iter *d, tsubst_flags_t complain)
6175 {
6176 if (d->cur->index)
6177 {
6178 if (complain & tf_error)
6179 error_at (cp_expr_loc_or_input_loc (d->cur->index),
6180 "C99 designator %qE outside aggregate initializer",
6181 d->cur->index);
6182 else
6183 return true;
6184 }
6185 return false;
6186 }
6187
6188 /* Subroutine of reshape_init, which processes a single initializer (part of
6189 a CONSTRUCTOR). TYPE is the type of the variable being initialized, D is the
6190 iterator within the CONSTRUCTOR which points to the initializer to process.
6191 FIRST_INITIALIZER_P is true if this is the first initializer of the
6192 outermost CONSTRUCTOR node. */
6193
6194 static tree
6195 reshape_init_r (tree type, reshape_iter *d, bool first_initializer_p,
6196 tsubst_flags_t complain)
6197 {
6198 tree init = d->cur->value;
6199
6200 if (error_operand_p (init))
6201 return error_mark_node;
6202
6203 if (first_initializer_p && !CP_AGGREGATE_TYPE_P (type)
6204 && has_designator_problem (d, complain))
6205 return error_mark_node;
6206
6207 tree stripped_init = tree_strip_any_location_wrapper (init);
6208
6209 if (TREE_CODE (type) == COMPLEX_TYPE)
6210 {
6211 /* A complex type can be initialized from one or two initializers,
6212 but braces are not elided. */
6213 d->cur++;
6214 if (BRACE_ENCLOSED_INITIALIZER_P (stripped_init))
6215 {
6216 if (CONSTRUCTOR_NELTS (stripped_init) > 2)
6217 {
6218 if (complain & tf_error)
6219 error ("too many initializers for %qT", type);
6220 else
6221 return error_mark_node;
6222 }
6223 }
6224 else if (first_initializer_p && d->cur != d->end)
6225 {
6226 vec<constructor_elt, va_gc> *v = 0;
6227 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, init);
6228 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, d->cur->value);
6229 if (has_designator_problem (d, complain))
6230 return error_mark_node;
6231 d->cur++;
6232 init = build_constructor (init_list_type_node, v);
6233 }
6234 return init;
6235 }
6236
6237 /* A non-aggregate type is always initialized with a single
6238 initializer. */
6239 if (!CP_AGGREGATE_TYPE_P (type))
6240 {
6241 /* It is invalid to initialize a non-aggregate type with a
6242 brace-enclosed initializer before C++0x.
6243 We need to check for BRACE_ENCLOSED_INITIALIZER_P here because
6244 of g++.old-deja/g++.mike/p7626.C: a pointer-to-member constant is
6245 a CONSTRUCTOR (with a record type). */
6246 if (TREE_CODE (stripped_init) == CONSTRUCTOR
6247 /* Don't complain about a capture-init. */
6248 && !CONSTRUCTOR_IS_DIRECT_INIT (stripped_init)
6249 && BRACE_ENCLOSED_INITIALIZER_P (stripped_init)) /* p7626.C */
6250 {
6251 if (SCALAR_TYPE_P (type))
6252 {
6253 if (cxx_dialect < cxx11)
6254 {
6255 if (complain & tf_error)
6256 error ("braces around scalar initializer for type %qT",
6257 type);
6258 init = error_mark_node;
6259 }
6260 else if (first_initializer_p
6261 || (CONSTRUCTOR_NELTS (stripped_init) > 0
6262 && (BRACE_ENCLOSED_INITIALIZER_P
6263 (CONSTRUCTOR_ELT (stripped_init,0)->value))))
6264 {
6265 if (complain & tf_error)
6266 error ("too many braces around scalar initializer"
6267 "for type %qT", type);
6268 init = error_mark_node;
6269 }
6270 }
6271 else
6272 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6273 }
6274
6275 d->cur++;
6276 return init;
6277 }
6278
6279 /* "If T is a class type and the initializer list has a single element of
6280 type cv U, where U is T or a class derived from T, the object is
6281 initialized from that element." Even if T is an aggregate. */
6282 if (cxx_dialect >= cxx11 && (CLASS_TYPE_P (type) || VECTOR_TYPE_P (type))
6283 && first_initializer_p
6284 && d->end - d->cur == 1
6285 && reference_related_p (type, TREE_TYPE (init)))
6286 {
6287 d->cur++;
6288 return init;
6289 }
6290
6291 /* [dcl.init.aggr]
6292
6293 All implicit type conversions (clause _conv_) are considered when
6294 initializing the aggregate member with an initializer from an
6295 initializer-list. If the initializer can initialize a member,
6296 the member is initialized. Otherwise, if the member is itself a
6297 non-empty subaggregate, brace elision is assumed and the
6298 initializer is considered for the initialization of the first
6299 member of the subaggregate. */
6300 if (TREE_CODE (init) != CONSTRUCTOR
6301 /* But don't try this for the first initializer, since that would be
6302 looking through the outermost braces; A a2 = { a1 }; is not a
6303 valid aggregate initialization. */
6304 && !first_initializer_p
6305 && (same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (init))
6306 || can_convert_arg (type, TREE_TYPE (init), init, LOOKUP_NORMAL,
6307 complain)))
6308 {
6309 d->cur++;
6310 return init;
6311 }
6312
6313 /* [dcl.init.string]
6314
6315 A char array (whether plain char, signed char, or unsigned char)
6316 can be initialized by a string-literal (optionally enclosed in
6317 braces); a wchar_t array can be initialized by a wide
6318 string-literal (optionally enclosed in braces). */
6319 if (TREE_CODE (type) == ARRAY_TYPE
6320 && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type))))
6321 {
6322 tree str_init = init;
6323 tree stripped_str_init = stripped_init;
6324
6325 /* Strip one level of braces if and only if they enclose a single
6326 element (as allowed by [dcl.init.string]). */
6327 if (!first_initializer_p
6328 && TREE_CODE (stripped_str_init) == CONSTRUCTOR
6329 && CONSTRUCTOR_NELTS (stripped_str_init) == 1)
6330 {
6331 str_init = (*CONSTRUCTOR_ELTS (stripped_str_init))[0].value;
6332 stripped_str_init = tree_strip_any_location_wrapper (str_init);
6333 }
6334
6335 /* If it's a string literal, then it's the initializer for the array
6336 as a whole. Otherwise, continue with normal initialization for
6337 array types (one value per array element). */
6338 if (TREE_CODE (stripped_str_init) == STRING_CST)
6339 {
6340 if (has_designator_problem (d, complain))
6341 return error_mark_node;
6342 d->cur++;
6343 return str_init;
6344 }
6345 }
6346
6347 /* The following cases are about aggregates. If we are not within a full
6348 initializer already, and there is not a CONSTRUCTOR, it means that there
6349 is a missing set of braces (that is, we are processing the case for
6350 which reshape_init exists). */
6351 if (!first_initializer_p)
6352 {
6353 if (TREE_CODE (stripped_init) == CONSTRUCTOR)
6354 {
6355 tree init_type = TREE_TYPE (init);
6356 if (init_type && TYPE_PTRMEMFUNC_P (init_type))
6357 /* There is no need to call reshape_init for pointer-to-member
6358 function initializers, as they are always constructed correctly
6359 by the front end. Here we have e.g. {.__pfn=0B, .__delta=0},
6360 which is missing outermost braces. We should warn below, and
6361 one of the routines below will wrap it in additional { }. */;
6362 /* For a nested compound literal, there is no need to reshape since
6363 we called reshape_init in finish_compound_literal, before calling
6364 digest_init. */
6365 else if (COMPOUND_LITERAL_P (stripped_init)
6366 /* Similarly, a CONSTRUCTOR of the target's type is a
6367 previously digested initializer. */
6368 || same_type_ignoring_top_level_qualifiers_p (type,
6369 init_type))
6370 {
6371 ++d->cur;
6372 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (stripped_init));
6373 return init;
6374 }
6375 else
6376 {
6377 /* Something that hasn't been reshaped yet. */
6378 ++d->cur;
6379 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (stripped_init));
6380 return reshape_init (type, init, complain);
6381 }
6382 }
6383
6384 if (complain & tf_warning)
6385 warning (OPT_Wmissing_braces,
6386 "missing braces around initializer for %qT",
6387 type);
6388 }
6389
6390 /* Dispatch to specialized routines. */
6391 if (CLASS_TYPE_P (type))
6392 return reshape_init_class (type, d, first_initializer_p, complain);
6393 else if (TREE_CODE (type) == ARRAY_TYPE)
6394 return reshape_init_array (type, d, complain);
6395 else if (VECTOR_TYPE_P (type))
6396 return reshape_init_vector (type, d, complain);
6397 else
6398 gcc_unreachable();
6399 }
6400
6401 /* Undo the brace-elision allowed by [dcl.init.aggr] in a
6402 brace-enclosed aggregate initializer.
6403
6404 INIT is the CONSTRUCTOR containing the list of initializers describing
6405 a brace-enclosed initializer for an entity of the indicated aggregate TYPE.
6406 It may not presently match the shape of the TYPE; for example:
6407
6408 struct S { int a; int b; };
6409 struct S a[] = { 1, 2, 3, 4 };
6410
6411 Here INIT will hold a vector of four elements, rather than a
6412 vector of two elements, each itself a vector of two elements. This
6413 routine transforms INIT from the former form into the latter. The
6414 revised CONSTRUCTOR node is returned. */
6415
6416 tree
6417 reshape_init (tree type, tree init, tsubst_flags_t complain)
6418 {
6419 vec<constructor_elt, va_gc> *v;
6420 reshape_iter d;
6421 tree new_init;
6422
6423 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
6424
6425 v = CONSTRUCTOR_ELTS (init);
6426
6427 /* An empty constructor does not need reshaping, and it is always a valid
6428 initializer. */
6429 if (vec_safe_is_empty (v))
6430 return init;
6431
6432 /* Handle [dcl.init.list] direct-list-initialization from
6433 single element of enumeration with a fixed underlying type. */
6434 if (is_direct_enum_init (type, init))
6435 {
6436 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
6437 type = cv_unqualified (type);
6438 if (check_narrowing (ENUM_UNDERLYING_TYPE (type), elt, complain))
6439 {
6440 warning_sentinel w (warn_useless_cast);
6441 warning_sentinel w2 (warn_ignored_qualifiers);
6442 return cp_build_c_cast (type, elt, tf_warning_or_error);
6443 }
6444 else
6445 return error_mark_node;
6446 }
6447
6448 /* Recurse on this CONSTRUCTOR. */
6449 d.cur = &(*v)[0];
6450 d.end = d.cur + v->length ();
6451
6452 new_init = reshape_init_r (type, &d, true, complain);
6453 if (new_init == error_mark_node)
6454 return error_mark_node;
6455
6456 /* Make sure all the element of the constructor were used. Otherwise,
6457 issue an error about exceeding initializers. */
6458 if (d.cur != d.end)
6459 {
6460 if (complain & tf_error)
6461 error ("too many initializers for %qT", type);
6462 return error_mark_node;
6463 }
6464
6465 if (CONSTRUCTOR_IS_DIRECT_INIT (init)
6466 && BRACE_ENCLOSED_INITIALIZER_P (new_init))
6467 CONSTRUCTOR_IS_DIRECT_INIT (new_init) = true;
6468 if (CONSTRUCTOR_IS_DESIGNATED_INIT (init)
6469 && BRACE_ENCLOSED_INITIALIZER_P (new_init))
6470 CONSTRUCTOR_IS_DESIGNATED_INIT (new_init) = true;
6471
6472 return new_init;
6473 }
6474
6475 /* Verify array initializer. Returns true if errors have been reported. */
6476
6477 bool
6478 check_array_initializer (tree decl, tree type, tree init)
6479 {
6480 tree element_type = TREE_TYPE (type);
6481
6482 /* The array type itself need not be complete, because the
6483 initializer may tell us how many elements are in the array.
6484 But, the elements of the array must be complete. */
6485 if (!COMPLETE_TYPE_P (complete_type (element_type)))
6486 {
6487 if (decl)
6488 error_at (DECL_SOURCE_LOCATION (decl),
6489 "elements of array %q#D have incomplete type", decl);
6490 else
6491 error ("elements of array %q#T have incomplete type", type);
6492 return true;
6493 }
6494 /* A compound literal can't have variable size. */
6495 if (init && !decl
6496 && ((COMPLETE_TYPE_P (type) && !TREE_CONSTANT (TYPE_SIZE (type)))
6497 || !TREE_CONSTANT (TYPE_SIZE (element_type))))
6498 {
6499 error ("variable-sized compound literal");
6500 return true;
6501 }
6502 return false;
6503 }
6504
6505 /* Subroutine of check_initializer; args are passed down from that function.
6506 Set stmts_are_full_exprs_p to 1 across a call to build_aggr_init. */
6507
6508 static tree
6509 build_aggr_init_full_exprs (tree decl, tree init, int flags)
6510
6511 {
6512 gcc_assert (stmts_are_full_exprs_p ());
6513 return build_aggr_init (decl, init, flags, tf_warning_or_error);
6514 }
6515
6516 /* Verify INIT (the initializer for DECL), and record the
6517 initialization in DECL_INITIAL, if appropriate. CLEANUP is as for
6518 grok_reference_init.
6519
6520 If the return value is non-NULL, it is an expression that must be
6521 evaluated dynamically to initialize DECL. */
6522
6523 static tree
6524 check_initializer (tree decl, tree init, int flags, vec<tree, va_gc> **cleanups)
6525 {
6526 tree type;
6527 tree init_code = NULL;
6528 tree core_type;
6529
6530 /* Things that are going to be initialized need to have complete
6531 type. */
6532 TREE_TYPE (decl) = type = complete_type (TREE_TYPE (decl));
6533
6534 if (DECL_HAS_VALUE_EXPR_P (decl))
6535 {
6536 /* A variable with DECL_HAS_VALUE_EXPR_P set is just a placeholder,
6537 it doesn't have storage to be initialized. */
6538 gcc_assert (init == NULL_TREE);
6539 return NULL_TREE;
6540 }
6541
6542 if (type == error_mark_node)
6543 /* We will have already complained. */
6544 return NULL_TREE;
6545
6546 if (TREE_CODE (type) == ARRAY_TYPE)
6547 {
6548 if (check_array_initializer (decl, type, init))
6549 return NULL_TREE;
6550 }
6551 else if (!COMPLETE_TYPE_P (type))
6552 {
6553 error_at (DECL_SOURCE_LOCATION (decl),
6554 "%q#D has incomplete type", decl);
6555 TREE_TYPE (decl) = error_mark_node;
6556 return NULL_TREE;
6557 }
6558 else
6559 /* There is no way to make a variable-sized class type in GNU C++. */
6560 gcc_assert (TREE_CONSTANT (TYPE_SIZE (type)));
6561
6562 if (init && BRACE_ENCLOSED_INITIALIZER_P (init))
6563 {
6564 int init_len = CONSTRUCTOR_NELTS (init);
6565 if (SCALAR_TYPE_P (type))
6566 {
6567 if (init_len == 0)
6568 {
6569 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6570 init = build_zero_init (type, NULL_TREE, false);
6571 }
6572 else if (init_len != 1 && TREE_CODE (type) != COMPLEX_TYPE)
6573 {
6574 error_at (cp_expr_loc_or_loc (init, DECL_SOURCE_LOCATION (decl)),
6575 "scalar object %qD requires one element in "
6576 "initializer", decl);
6577 TREE_TYPE (decl) = error_mark_node;
6578 return NULL_TREE;
6579 }
6580 }
6581 }
6582
6583 if (TREE_CODE (decl) == CONST_DECL)
6584 {
6585 gcc_assert (!TYPE_REF_P (type));
6586
6587 DECL_INITIAL (decl) = init;
6588
6589 gcc_assert (init != NULL_TREE);
6590 init = NULL_TREE;
6591 }
6592 else if (!init && DECL_REALLY_EXTERN (decl))
6593 ;
6594 else if (init || type_build_ctor_call (type)
6595 || TYPE_REF_P (type))
6596 {
6597 if (TYPE_REF_P (type))
6598 {
6599 init = grok_reference_init (decl, type, init, flags);
6600 flags |= LOOKUP_ALREADY_DIGESTED;
6601 }
6602 else if (!init)
6603 check_for_uninitialized_const_var (decl, /*constexpr_context_p=*/false,
6604 tf_warning_or_error);
6605 /* Do not reshape constructors of vectors (they don't need to be
6606 reshaped. */
6607 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
6608 {
6609 if (is_std_init_list (type))
6610 {
6611 init = perform_implicit_conversion (type, init,
6612 tf_warning_or_error);
6613 flags |= LOOKUP_ALREADY_DIGESTED;
6614 }
6615 else if (TYPE_NON_AGGREGATE_CLASS (type))
6616 {
6617 /* Don't reshape if the class has constructors. */
6618 if (cxx_dialect == cxx98)
6619 error_at (cp_expr_loc_or_loc (init, DECL_SOURCE_LOCATION (decl)),
6620 "in C++98 %qD must be initialized by "
6621 "constructor, not by %<{...}%>",
6622 decl);
6623 }
6624 else if (VECTOR_TYPE_P (type) && TYPE_VECTOR_OPAQUE (type))
6625 {
6626 error ("opaque vector types cannot be initialized");
6627 init = error_mark_node;
6628 }
6629 else
6630 {
6631 init = reshape_init (type, init, tf_warning_or_error);
6632 flags |= LOOKUP_NO_NARROWING;
6633 }
6634 }
6635 else if (TREE_CODE (init) == TREE_LIST
6636 && TREE_TYPE (init) != unknown_type_node
6637 && !MAYBE_CLASS_TYPE_P (type))
6638 {
6639 gcc_assert (TREE_CODE (decl) != RESULT_DECL);
6640
6641 /* We get here with code like `int a (2);' */
6642 init = build_x_compound_expr_from_list (init, ELK_INIT,
6643 tf_warning_or_error);
6644 }
6645
6646 /* If DECL has an array type without a specific bound, deduce the
6647 array size from the initializer. */
6648 maybe_deduce_size_from_array_init (decl, init);
6649 type = TREE_TYPE (decl);
6650 if (type == error_mark_node)
6651 return NULL_TREE;
6652
6653 if (((type_build_ctor_call (type) || CLASS_TYPE_P (type))
6654 && !(flags & LOOKUP_ALREADY_DIGESTED)
6655 && !(init && BRACE_ENCLOSED_INITIALIZER_P (init)
6656 && CP_AGGREGATE_TYPE_P (type)
6657 && (CLASS_TYPE_P (type)
6658 || !TYPE_NEEDS_CONSTRUCTING (type)
6659 || type_has_extended_temps (type))))
6660 || (DECL_DECOMPOSITION_P (decl) && TREE_CODE (type) == ARRAY_TYPE))
6661 {
6662 init_code = build_aggr_init_full_exprs (decl, init, flags);
6663
6664 /* A constructor call is a non-trivial initializer even if
6665 it isn't explicitly written. */
6666 if (TREE_SIDE_EFFECTS (init_code))
6667 DECL_NONTRIVIALLY_INITIALIZED_P (decl) = true;
6668
6669 /* If this is a constexpr initializer, expand_default_init will
6670 have returned an INIT_EXPR rather than a CALL_EXPR. In that
6671 case, pull the initializer back out and pass it down into
6672 store_init_value. */
6673 while (TREE_CODE (init_code) == EXPR_STMT
6674 || TREE_CODE (init_code) == CONVERT_EXPR)
6675 init_code = TREE_OPERAND (init_code, 0);
6676 if (TREE_CODE (init_code) == INIT_EXPR)
6677 {
6678 init = TREE_OPERAND (init_code, 1);
6679 init_code = NULL_TREE;
6680 /* Don't call digest_init; it's unnecessary and will complain
6681 about aggregate initialization of non-aggregate classes. */
6682 flags |= LOOKUP_ALREADY_DIGESTED;
6683 }
6684 else if (DECL_DECLARED_CONSTEXPR_P (decl)
6685 || (flags & LOOKUP_CONSTINIT))
6686 {
6687 /* Declared constexpr or constinit, but no suitable initializer;
6688 massage init appropriately so we can pass it into
6689 store_init_value for the error. */
6690 if (CLASS_TYPE_P (type)
6691 && (!init || TREE_CODE (init) == TREE_LIST))
6692 {
6693 init = build_functional_cast (type, init, tf_none);
6694 if (TREE_CODE (init) == TARGET_EXPR)
6695 TARGET_EXPR_DIRECT_INIT_P (init) = true;
6696 }
6697 init_code = NULL_TREE;
6698 }
6699 else
6700 init = NULL_TREE;
6701 }
6702
6703 if (init && TREE_CODE (init) != TREE_VEC)
6704 {
6705 /* In aggregate initialization of a variable, each element
6706 initialization is a full-expression because there is no
6707 enclosing expression. */
6708 gcc_assert (stmts_are_full_exprs_p ());
6709
6710 init_code = store_init_value (decl, init, cleanups, flags);
6711
6712 if (DECL_INITIAL (decl)
6713 && TREE_CODE (DECL_INITIAL (decl)) == CONSTRUCTOR
6714 && !vec_safe_is_empty (CONSTRUCTOR_ELTS (DECL_INITIAL (decl))))
6715 {
6716 tree elt = CONSTRUCTOR_ELTS (DECL_INITIAL (decl))->last ().value;
6717 if (TREE_CODE (TREE_TYPE (elt)) == ARRAY_TYPE
6718 && TYPE_SIZE (TREE_TYPE (elt)) == NULL_TREE)
6719 cp_complete_array_type (&TREE_TYPE (elt), elt, false);
6720 }
6721
6722 if (pedantic && TREE_CODE (type) == ARRAY_TYPE
6723 && DECL_INITIAL (decl)
6724 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
6725 && PAREN_STRING_LITERAL_P (DECL_INITIAL (decl)))
6726 warning_at (cp_expr_loc_or_loc (DECL_INITIAL (decl),
6727 DECL_SOURCE_LOCATION (decl)),
6728 0, "array %qD initialized by parenthesized "
6729 "string literal %qE",
6730 decl, DECL_INITIAL (decl));
6731 init = NULL;
6732 }
6733 }
6734 else
6735 {
6736 if (CLASS_TYPE_P (core_type = strip_array_types (type))
6737 && (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type)
6738 || CLASSTYPE_REF_FIELDS_NEED_INIT (core_type)))
6739 diagnose_uninitialized_cst_or_ref_member (core_type, /*using_new=*/false,
6740 /*complain=*/true);
6741
6742 check_for_uninitialized_const_var (decl, /*constexpr_context_p=*/false,
6743 tf_warning_or_error);
6744 }
6745
6746 if (init && init != error_mark_node)
6747 init_code = build2 (INIT_EXPR, type, decl, init);
6748
6749 if (init_code)
6750 {
6751 /* We might have set these in cp_finish_decl. */
6752 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = false;
6753 TREE_CONSTANT (decl) = false;
6754 }
6755
6756 if (init_code
6757 && DECL_IN_AGGR_P (decl)
6758 && DECL_INITIALIZED_IN_CLASS_P (decl))
6759 {
6760 static int explained = 0;
6761
6762 if (cxx_dialect < cxx11)
6763 error ("initializer invalid for static member with constructor");
6764 else if (cxx_dialect < cxx17)
6765 error ("non-constant in-class initialization invalid for static "
6766 "member %qD", decl);
6767 else
6768 error ("non-constant in-class initialization invalid for non-inline "
6769 "static member %qD", decl);
6770 if (!explained)
6771 {
6772 inform (input_location,
6773 "(an out of class initialization is required)");
6774 explained = 1;
6775 }
6776 return NULL_TREE;
6777 }
6778
6779 return init_code;
6780 }
6781
6782 /* If DECL is not a local variable, give it RTL. */
6783
6784 static void
6785 make_rtl_for_nonlocal_decl (tree decl, tree init, const char* asmspec)
6786 {
6787 int toplev = toplevel_bindings_p ();
6788 int defer_p;
6789
6790 /* Set the DECL_ASSEMBLER_NAME for the object. */
6791 if (asmspec)
6792 {
6793 /* The `register' keyword, when used together with an
6794 asm-specification, indicates that the variable should be
6795 placed in a particular register. */
6796 if (VAR_P (decl) && DECL_REGISTER (decl))
6797 {
6798 set_user_assembler_name (decl, asmspec);
6799 DECL_HARD_REGISTER (decl) = 1;
6800 }
6801 else
6802 {
6803 if (TREE_CODE (decl) == FUNCTION_DECL
6804 && fndecl_built_in_p (decl, BUILT_IN_NORMAL))
6805 set_builtin_user_assembler_name (decl, asmspec);
6806 set_user_assembler_name (decl, asmspec);
6807 }
6808 }
6809
6810 /* Handle non-variables up front. */
6811 if (!VAR_P (decl))
6812 {
6813 rest_of_decl_compilation (decl, toplev, at_eof);
6814 return;
6815 }
6816
6817 /* If we see a class member here, it should be a static data
6818 member. */
6819 if (DECL_LANG_SPECIFIC (decl) && DECL_IN_AGGR_P (decl))
6820 {
6821 gcc_assert (TREE_STATIC (decl));
6822 /* An in-class declaration of a static data member should be
6823 external; it is only a declaration, and not a definition. */
6824 if (init == NULL_TREE)
6825 gcc_assert (DECL_EXTERNAL (decl)
6826 || !TREE_PUBLIC (decl));
6827 }
6828
6829 /* We don't create any RTL for local variables. */
6830 if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
6831 return;
6832
6833 /* We defer emission of local statics until the corresponding
6834 DECL_EXPR is expanded. But with constexpr its function might never
6835 be expanded, so go ahead and tell cgraph about the variable now. */
6836 defer_p = ((DECL_FUNCTION_SCOPE_P (decl)
6837 && !var_in_maybe_constexpr_fn (decl))
6838 || DECL_VIRTUAL_P (decl));
6839
6840 /* Defer template instantiations. */
6841 if (DECL_LANG_SPECIFIC (decl)
6842 && DECL_IMPLICIT_INSTANTIATION (decl))
6843 defer_p = 1;
6844
6845 /* If we're not deferring, go ahead and assemble the variable. */
6846 if (!defer_p)
6847 rest_of_decl_compilation (decl, toplev, at_eof);
6848 }
6849
6850 /* walk_tree helper for wrap_temporary_cleanups, below. */
6851
6852 static tree
6853 wrap_cleanups_r (tree *stmt_p, int *walk_subtrees, void *data)
6854 {
6855 /* Stop at types or full-expression boundaries. */
6856 if (TYPE_P (*stmt_p)
6857 || TREE_CODE (*stmt_p) == CLEANUP_POINT_EXPR)
6858 {
6859 *walk_subtrees = 0;
6860 return NULL_TREE;
6861 }
6862
6863 if (TREE_CODE (*stmt_p) == TARGET_EXPR)
6864 {
6865 tree guard = (tree)data;
6866 tree tcleanup = TARGET_EXPR_CLEANUP (*stmt_p);
6867
6868 tcleanup = build2 (TRY_CATCH_EXPR, void_type_node, tcleanup, guard);
6869 /* Tell honor_protect_cleanup_actions to handle this as a separate
6870 cleanup. */
6871 TRY_CATCH_IS_CLEANUP (tcleanup) = 1;
6872
6873 TARGET_EXPR_CLEANUP (*stmt_p) = tcleanup;
6874 }
6875
6876 return NULL_TREE;
6877 }
6878
6879 /* We're initializing a local variable which has a cleanup GUARD. If there
6880 are any temporaries used in the initializer INIT of this variable, we
6881 need to wrap their cleanups with TRY_CATCH_EXPR (, GUARD) so that the
6882 variable will be cleaned up properly if one of them throws.
6883
6884 Unfortunately, there's no way to express this properly in terms of
6885 nesting, as the regions for the temporaries overlap the region for the
6886 variable itself; if there are two temporaries, the variable needs to be
6887 the first thing destroyed if either of them throws. However, we only
6888 want to run the variable's cleanup if it actually got constructed. So
6889 we need to guard the temporary cleanups with the variable's cleanup if
6890 they are run on the normal path, but not if they are run on the
6891 exceptional path. We implement this by telling
6892 honor_protect_cleanup_actions to strip the variable cleanup from the
6893 exceptional path. */
6894
6895 static void
6896 wrap_temporary_cleanups (tree init, tree guard)
6897 {
6898 cp_walk_tree_without_duplicates (&init, wrap_cleanups_r, (void *)guard);
6899 }
6900
6901 /* Generate code to initialize DECL (a local variable). */
6902
6903 static void
6904 initialize_local_var (tree decl, tree init)
6905 {
6906 tree type = TREE_TYPE (decl);
6907 tree cleanup;
6908 int already_used;
6909
6910 gcc_assert (VAR_P (decl)
6911 || TREE_CODE (decl) == RESULT_DECL);
6912 gcc_assert (!TREE_STATIC (decl));
6913
6914 if (DECL_SIZE (decl) == NULL_TREE)
6915 {
6916 /* If we used it already as memory, it must stay in memory. */
6917 DECL_INITIAL (decl) = NULL_TREE;
6918 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
6919 return;
6920 }
6921
6922 if (type == error_mark_node)
6923 return;
6924
6925 /* Compute and store the initial value. */
6926 already_used = TREE_USED (decl) || TREE_USED (type);
6927 if (TREE_USED (type))
6928 DECL_READ_P (decl) = 1;
6929
6930 /* Generate a cleanup, if necessary. */
6931 cleanup = cxx_maybe_build_cleanup (decl, tf_warning_or_error);
6932
6933 /* Perform the initialization. */
6934 if (init)
6935 {
6936 tree rinit = (TREE_CODE (init) == INIT_EXPR
6937 ? TREE_OPERAND (init, 1) : NULL_TREE);
6938 if (rinit && !TREE_SIDE_EFFECTS (rinit))
6939 {
6940 /* Stick simple initializers in DECL_INITIAL so that
6941 -Wno-init-self works (c++/34772). */
6942 gcc_assert (TREE_OPERAND (init, 0) == decl);
6943 DECL_INITIAL (decl) = rinit;
6944
6945 if (warn_init_self && TYPE_REF_P (type))
6946 {
6947 STRIP_NOPS (rinit);
6948 if (rinit == decl)
6949 warning_at (DECL_SOURCE_LOCATION (decl),
6950 OPT_Winit_self,
6951 "reference %qD is initialized with itself", decl);
6952 }
6953 }
6954 else
6955 {
6956 int saved_stmts_are_full_exprs_p;
6957
6958 /* If we're only initializing a single object, guard the
6959 destructors of any temporaries used in its initializer with
6960 its destructor. This isn't right for arrays because each
6961 element initialization is a full-expression. */
6962 if (cleanup && TREE_CODE (type) != ARRAY_TYPE)
6963 wrap_temporary_cleanups (init, cleanup);
6964
6965 gcc_assert (building_stmt_list_p ());
6966 saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
6967 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
6968 finish_expr_stmt (init);
6969 current_stmt_tree ()->stmts_are_full_exprs_p =
6970 saved_stmts_are_full_exprs_p;
6971 }
6972 }
6973
6974 /* Set this to 0 so we can tell whether an aggregate which was
6975 initialized was ever used. Don't do this if it has a
6976 destructor, so we don't complain about the 'resource
6977 allocation is initialization' idiom. Now set
6978 attribute((unused)) on types so decls of that type will be
6979 marked used. (see TREE_USED, above.) */
6980 if (TYPE_NEEDS_CONSTRUCTING (type)
6981 && ! already_used
6982 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type)
6983 && DECL_NAME (decl))
6984 TREE_USED (decl) = 0;
6985 else if (already_used)
6986 TREE_USED (decl) = 1;
6987
6988 if (cleanup)
6989 finish_decl_cleanup (decl, cleanup);
6990 }
6991
6992 /* DECL is a VAR_DECL for a compiler-generated variable with static
6993 storage duration (like a virtual table) whose initializer is a
6994 compile-time constant. Initialize the variable and provide it to the
6995 back end. */
6996
6997 void
6998 initialize_artificial_var (tree decl, vec<constructor_elt, va_gc> *v)
6999 {
7000 tree init;
7001 gcc_assert (DECL_ARTIFICIAL (decl));
7002 init = build_constructor (TREE_TYPE (decl), v);
7003 gcc_assert (TREE_CODE (init) == CONSTRUCTOR);
7004 DECL_INITIAL (decl) = init;
7005 DECL_INITIALIZED_P (decl) = 1;
7006 /* Mark the decl as constexpr so that we can access its content
7007 at compile time. */
7008 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
7009 DECL_DECLARED_CONSTEXPR_P (decl) = true;
7010 determine_visibility (decl);
7011 layout_var_decl (decl);
7012 maybe_commonize_var (decl);
7013 make_rtl_for_nonlocal_decl (decl, init, /*asmspec=*/NULL);
7014 }
7015
7016 /* INIT is the initializer for a variable, as represented by the
7017 parser. Returns true iff INIT is value-dependent. */
7018
7019 static bool
7020 value_dependent_init_p (tree init)
7021 {
7022 if (TREE_CODE (init) == TREE_LIST)
7023 /* A parenthesized initializer, e.g.: int i (3, 2); ? */
7024 return any_value_dependent_elements_p (init);
7025 else if (TREE_CODE (init) == CONSTRUCTOR)
7026 /* A brace-enclosed initializer, e.g.: int i = { 3 }; ? */
7027 {
7028 if (dependent_type_p (TREE_TYPE (init)))
7029 return true;
7030
7031 vec<constructor_elt, va_gc> *elts;
7032 size_t nelts;
7033 size_t i;
7034
7035 elts = CONSTRUCTOR_ELTS (init);
7036 nelts = vec_safe_length (elts);
7037 for (i = 0; i < nelts; ++i)
7038 if (value_dependent_init_p ((*elts)[i].value))
7039 return true;
7040 }
7041 else
7042 /* It must be a simple expression, e.g., int i = 3; */
7043 return value_dependent_expression_p (init);
7044
7045 return false;
7046 }
7047
7048 // Returns true if a DECL is VAR_DECL with the concept specifier.
7049 static inline bool
7050 is_concept_var (tree decl)
7051 {
7052 return (VAR_P (decl)
7053 // Not all variables have DECL_LANG_SPECIFIC.
7054 && DECL_LANG_SPECIFIC (decl)
7055 && DECL_DECLARED_CONCEPT_P (decl));
7056 }
7057
7058 /* A helper function to be called via walk_tree. If any label exists
7059 under *TP, it is (going to be) forced. Set has_forced_label_in_static. */
7060
7061 static tree
7062 notice_forced_label_r (tree *tp, int *walk_subtrees, void *)
7063 {
7064 if (TYPE_P (*tp))
7065 *walk_subtrees = 0;
7066 if (TREE_CODE (*tp) == LABEL_DECL)
7067 cfun->has_forced_label_in_static = 1;
7068 return NULL_TREE;
7069 }
7070
7071 /* Return true if DECL has either a trivial destructor, or for C++2A
7072 is constexpr and has a constexpr destructor. */
7073
7074 static bool
7075 decl_maybe_constant_destruction (tree decl, tree type)
7076 {
7077 return (TYPE_HAS_TRIVIAL_DESTRUCTOR (type)
7078 || (cxx_dialect >= cxx2a
7079 && VAR_P (decl)
7080 && DECL_DECLARED_CONSTEXPR_P (decl)
7081 && type_has_constexpr_destructor (strip_array_types (type))));
7082 }
7083
7084 /* Finish processing of a declaration;
7085 install its line number and initial value.
7086 If the length of an array type is not known before,
7087 it must be determined now, from the initial value, or it is an error.
7088
7089 INIT is the initializer (if any) for DECL. If INIT_CONST_EXPR_P is
7090 true, then INIT is an integral constant expression.
7091
7092 FLAGS is LOOKUP_ONLYCONVERTING if the = init syntax was used, else 0
7093 if the (init) syntax was used. */
7094
7095 void
7096 cp_finish_decl (tree decl, tree init, bool init_const_expr_p,
7097 tree asmspec_tree, int flags)
7098 {
7099 tree type;
7100 vec<tree, va_gc> *cleanups = NULL;
7101 const char *asmspec = NULL;
7102 int was_readonly = 0;
7103 bool var_definition_p = false;
7104 tree auto_node;
7105
7106 if (decl == error_mark_node)
7107 return;
7108 else if (! decl)
7109 {
7110 if (init)
7111 error ("assignment (not initialization) in declaration");
7112 return;
7113 }
7114
7115 gcc_assert (TREE_CODE (decl) != RESULT_DECL);
7116 /* Parameters are handled by store_parm_decls, not cp_finish_decl. */
7117 gcc_assert (TREE_CODE (decl) != PARM_DECL);
7118
7119 type = TREE_TYPE (decl);
7120 if (type == error_mark_node)
7121 return;
7122
7123 /* Warn about register storage specifiers except when in GNU global
7124 or local register variable extension. */
7125 if (VAR_P (decl) && DECL_REGISTER (decl) && asmspec_tree == NULL_TREE)
7126 {
7127 if (cxx_dialect >= cxx17)
7128 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
7129 "ISO C++17 does not allow %<register%> storage "
7130 "class specifier");
7131 else
7132 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
7133 "%<register%> storage class specifier used");
7134 }
7135
7136 /* If a name was specified, get the string. */
7137 if (at_namespace_scope_p ())
7138 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
7139 if (asmspec_tree && asmspec_tree != error_mark_node)
7140 asmspec = TREE_STRING_POINTER (asmspec_tree);
7141
7142 if (current_class_type
7143 && CP_DECL_CONTEXT (decl) == current_class_type
7144 && TYPE_BEING_DEFINED (current_class_type)
7145 && !CLASSTYPE_TEMPLATE_INSTANTIATION (current_class_type)
7146 && (DECL_INITIAL (decl) || init))
7147 DECL_INITIALIZED_IN_CLASS_P (decl) = 1;
7148
7149 /* Do auto deduction unless decl is a function or an uninstantiated
7150 template specialization. */
7151 if (TREE_CODE (decl) != FUNCTION_DECL
7152 && !(init == NULL_TREE
7153 && DECL_LANG_SPECIFIC (decl)
7154 && DECL_TEMPLATE_INSTANTIATION (decl)
7155 && !DECL_TEMPLATE_INSTANTIATED (decl))
7156 && (auto_node = type_uses_auto (type)))
7157 {
7158 tree d_init;
7159 if (init == NULL_TREE)
7160 gcc_assert (CLASS_PLACEHOLDER_TEMPLATE (auto_node));
7161 d_init = init;
7162 if (d_init)
7163 {
7164 if (TREE_CODE (d_init) == TREE_LIST
7165 && !CLASS_PLACEHOLDER_TEMPLATE (auto_node))
7166 d_init = build_x_compound_expr_from_list (d_init, ELK_INIT,
7167 tf_warning_or_error);
7168 d_init = resolve_nondeduced_context (d_init, tf_warning_or_error);
7169 }
7170 enum auto_deduction_context adc = adc_variable_type;
7171 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
7172 adc = adc_decomp_type;
7173 type = TREE_TYPE (decl) = do_auto_deduction (type, d_init, auto_node,
7174 tf_warning_or_error, adc,
7175 NULL_TREE, flags);
7176 if (type == error_mark_node)
7177 return;
7178 if (TREE_CODE (type) == FUNCTION_TYPE)
7179 {
7180 error ("initializer for %<decltype(auto) %D%> has function type; "
7181 "did you forget the %<()%>?", decl);
7182 TREE_TYPE (decl) = error_mark_node;
7183 return;
7184 }
7185 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
7186 }
7187
7188 if (ensure_literal_type_for_constexpr_object (decl) == error_mark_node)
7189 {
7190 DECL_DECLARED_CONSTEXPR_P (decl) = 0;
7191 if (VAR_P (decl) && DECL_CLASS_SCOPE_P (decl))
7192 {
7193 init = NULL_TREE;
7194 DECL_EXTERNAL (decl) = 1;
7195 }
7196 }
7197
7198 if (VAR_P (decl)
7199 && DECL_CLASS_SCOPE_P (decl)
7200 && DECL_INITIALIZED_IN_CLASS_P (decl))
7201 check_static_variable_definition (decl, type);
7202
7203 if (init && TREE_CODE (decl) == FUNCTION_DECL)
7204 {
7205 tree clone;
7206 if (init == ridpointers[(int)RID_DELETE])
7207 {
7208 /* FIXME check this is 1st decl. */
7209 DECL_DELETED_FN (decl) = 1;
7210 DECL_DECLARED_INLINE_P (decl) = 1;
7211 DECL_INITIAL (decl) = error_mark_node;
7212 FOR_EACH_CLONE (clone, decl)
7213 {
7214 DECL_DELETED_FN (clone) = 1;
7215 DECL_DECLARED_INLINE_P (clone) = 1;
7216 DECL_INITIAL (clone) = error_mark_node;
7217 }
7218 init = NULL_TREE;
7219 }
7220 else if (init == ridpointers[(int)RID_DEFAULT])
7221 {
7222 if (defaultable_fn_check (decl))
7223 DECL_DEFAULTED_FN (decl) = 1;
7224 else
7225 DECL_INITIAL (decl) = NULL_TREE;
7226 }
7227 }
7228
7229 if (init && VAR_P (decl))
7230 {
7231 DECL_NONTRIVIALLY_INITIALIZED_P (decl) = 1;
7232 /* If DECL is a reference, then we want to know whether init is a
7233 reference constant; init_const_expr_p as passed tells us whether
7234 it's an rvalue constant. */
7235 if (TYPE_REF_P (type))
7236 init_const_expr_p = potential_constant_expression (init);
7237 if (init_const_expr_p)
7238 {
7239 /* Set these flags now for templates. We'll update the flags in
7240 store_init_value for instantiations. */
7241 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
7242 if (decl_maybe_constant_var_p (decl)
7243 /* FIXME setting TREE_CONSTANT on refs breaks the back end. */
7244 && !TYPE_REF_P (type))
7245 TREE_CONSTANT (decl) = 1;
7246 }
7247 }
7248
7249 if (processing_template_decl)
7250 {
7251 bool type_dependent_p;
7252
7253 /* Add this declaration to the statement-tree. */
7254 if (at_function_scope_p ())
7255 add_decl_expr (decl);
7256
7257 type_dependent_p = dependent_type_p (type);
7258
7259 if (check_for_bare_parameter_packs (init))
7260 {
7261 init = NULL_TREE;
7262 DECL_INITIAL (decl) = NULL_TREE;
7263 }
7264
7265 /* Handle `constinit' on variable templates. */
7266 if (flags & LOOKUP_CONSTINIT)
7267 TINFO_VAR_DECLARED_CONSTINIT (DECL_TEMPLATE_INFO (decl)) = true;
7268
7269 /* Generally, initializers in templates are expanded when the
7270 template is instantiated. But, if DECL is a variable constant
7271 then it can be used in future constant expressions, so its value
7272 must be available. */
7273
7274 bool dep_init = false;
7275
7276 if (!VAR_P (decl) || type_dependent_p)
7277 /* We can't do anything if the decl has dependent type. */;
7278 else if (!init && is_concept_var (decl))
7279 {
7280 error ("variable concept has no initializer");
7281 init = boolean_true_node;
7282 }
7283 else if (init
7284 && init_const_expr_p
7285 && !TYPE_REF_P (type)
7286 && decl_maybe_constant_var_p (decl)
7287 && !(dep_init = value_dependent_init_p (init)))
7288 {
7289 /* This variable seems to be a non-dependent constant, so process
7290 its initializer. If check_initializer returns non-null the
7291 initialization wasn't constant after all. */
7292 tree init_code;
7293 cleanups = make_tree_vector ();
7294 init_code = check_initializer (decl, init, flags, &cleanups);
7295 if (init_code == NULL_TREE)
7296 init = NULL_TREE;
7297 release_tree_vector (cleanups);
7298 }
7299 else
7300 {
7301 gcc_assert (!DECL_PRETTY_FUNCTION_P (decl));
7302 /* Deduce array size even if the initializer is dependent. */
7303 maybe_deduce_size_from_array_init (decl, init);
7304 /* And complain about multiple initializers. */
7305 if (init && TREE_CODE (init) == TREE_LIST && TREE_CHAIN (init)
7306 && !MAYBE_CLASS_TYPE_P (type))
7307 init = build_x_compound_expr_from_list (init, ELK_INIT,
7308 tf_warning_or_error);
7309 }
7310
7311 if (init)
7312 DECL_INITIAL (decl) = init;
7313
7314 if (dep_init)
7315 {
7316 retrofit_lang_decl (decl);
7317 SET_DECL_DEPENDENT_INIT_P (decl, true);
7318 }
7319 return;
7320 }
7321
7322 /* Just store non-static data member initializers for later. */
7323 if (init && TREE_CODE (decl) == FIELD_DECL)
7324 DECL_INITIAL (decl) = init;
7325
7326 /* Take care of TYPE_DECLs up front. */
7327 if (TREE_CODE (decl) == TYPE_DECL)
7328 {
7329 if (type != error_mark_node
7330 && MAYBE_CLASS_TYPE_P (type) && DECL_NAME (decl))
7331 {
7332 if (TREE_TYPE (DECL_NAME (decl)) && TREE_TYPE (decl) != type)
7333 warning (0, "shadowing previous type declaration of %q#D", decl);
7334 set_identifier_type_value (DECL_NAME (decl), decl);
7335 }
7336
7337 /* If we have installed this as the canonical typedef for this
7338 type, and that type has not been defined yet, delay emitting
7339 the debug information for it, as we will emit it later. */
7340 if (TYPE_MAIN_DECL (TREE_TYPE (decl)) == decl
7341 && !COMPLETE_TYPE_P (TREE_TYPE (decl)))
7342 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
7343
7344 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl),
7345 at_eof);
7346 return;
7347 }
7348
7349 /* A reference will be modified here, as it is initialized. */
7350 if (! DECL_EXTERNAL (decl)
7351 && TREE_READONLY (decl)
7352 && TYPE_REF_P (type))
7353 {
7354 was_readonly = 1;
7355 TREE_READONLY (decl) = 0;
7356 }
7357
7358 if (VAR_P (decl))
7359 {
7360 duration_kind dk = decl_storage_duration (decl);
7361 /* [dcl.constinit]/1 "The constinit specifier shall be applied
7362 only to a declaration of a variable with static or thread storage
7363 duration." */
7364 if ((flags & LOOKUP_CONSTINIT)
7365 && !(dk == dk_thread || dk == dk_static))
7366 {
7367 error_at (DECL_SOURCE_LOCATION (decl),
7368 "%<constinit%> can only be applied to a variable with "
7369 "static or thread storage duration");
7370 return;
7371 }
7372
7373 /* If this is a local variable that will need a mangled name,
7374 register it now. We must do this before processing the
7375 initializer for the variable, since the initialization might
7376 require a guard variable, and since the mangled name of the
7377 guard variable will depend on the mangled name of this
7378 variable. */
7379 if (DECL_FUNCTION_SCOPE_P (decl)
7380 && TREE_STATIC (decl)
7381 && !DECL_ARTIFICIAL (decl))
7382 {
7383 /* The variable holding an anonymous union will have had its
7384 discriminator set in finish_anon_union, after which it's
7385 NAME will have been cleared. */
7386 if (DECL_NAME (decl))
7387 determine_local_discriminator (decl);
7388 /* Normally has_forced_label_in_static is set during GIMPLE
7389 lowering, but [cd]tors are never actually compiled directly.
7390 We need to set this early so we can deal with the label
7391 address extension. */
7392 if ((DECL_CONSTRUCTOR_P (current_function_decl)
7393 || DECL_DESTRUCTOR_P (current_function_decl))
7394 && init)
7395 {
7396 walk_tree (&init, notice_forced_label_r, NULL, NULL);
7397 add_local_decl (cfun, decl);
7398 }
7399 /* And make sure it's in the symbol table for
7400 c_parse_final_cleanups to find. */
7401 varpool_node::get_create (decl);
7402 }
7403
7404 /* Convert the initializer to the type of DECL, if we have not
7405 already initialized DECL. */
7406 if (!DECL_INITIALIZED_P (decl)
7407 /* If !DECL_EXTERNAL then DECL is being defined. In the
7408 case of a static data member initialized inside the
7409 class-specifier, there can be an initializer even if DECL
7410 is *not* defined. */
7411 && (!DECL_EXTERNAL (decl) || init))
7412 {
7413 cleanups = make_tree_vector ();
7414 init = check_initializer (decl, init, flags, &cleanups);
7415
7416 /* Handle:
7417
7418 [dcl.init]
7419
7420 The memory occupied by any object of static storage
7421 duration is zero-initialized at program startup before
7422 any other initialization takes place.
7423
7424 We cannot create an appropriate initializer until after
7425 the type of DECL is finalized. If DECL_INITIAL is set,
7426 then the DECL is statically initialized, and any
7427 necessary zero-initialization has already been performed. */
7428 if (TREE_STATIC (decl) && !DECL_INITIAL (decl))
7429 DECL_INITIAL (decl) = build_zero_init (TREE_TYPE (decl),
7430 /*nelts=*/NULL_TREE,
7431 /*static_storage_p=*/true);
7432 /* Remember that the initialization for this variable has
7433 taken place. */
7434 DECL_INITIALIZED_P (decl) = 1;
7435 /* This declaration is the definition of this variable,
7436 unless we are initializing a static data member within
7437 the class specifier. */
7438 if (!DECL_EXTERNAL (decl))
7439 var_definition_p = true;
7440 }
7441 /* If the variable has an array type, lay out the type, even if
7442 there is no initializer. It is valid to index through the
7443 array, and we must get TYPE_ALIGN set correctly on the array
7444 type. */
7445 else if (TREE_CODE (type) == ARRAY_TYPE)
7446 layout_type (type);
7447
7448 if (TREE_STATIC (decl)
7449 && !at_function_scope_p ()
7450 && current_function_decl == NULL)
7451 /* So decl is a global variable or a static member of a
7452 non local class. Record the types it uses
7453 so that we can decide later to emit debug info for them. */
7454 record_types_used_by_current_var_decl (decl);
7455 }
7456
7457 /* Add this declaration to the statement-tree. This needs to happen
7458 after the call to check_initializer so that the DECL_EXPR for a
7459 reference temp is added before the DECL_EXPR for the reference itself. */
7460 if (DECL_FUNCTION_SCOPE_P (decl))
7461 {
7462 /* If we're building a variable sized type, and we might be
7463 reachable other than via the top of the current binding
7464 level, then create a new BIND_EXPR so that we deallocate
7465 the object at the right time. */
7466 if (VAR_P (decl)
7467 && DECL_SIZE (decl)
7468 && !TREE_CONSTANT (DECL_SIZE (decl))
7469 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
7470 {
7471 tree bind;
7472 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
7473 TREE_SIDE_EFFECTS (bind) = 1;
7474 add_stmt (bind);
7475 BIND_EXPR_BODY (bind) = push_stmt_list ();
7476 }
7477 add_decl_expr (decl);
7478 }
7479
7480 /* Let the middle end know about variables and functions -- but not
7481 static data members in uninstantiated class templates. */
7482 if (VAR_OR_FUNCTION_DECL_P (decl))
7483 {
7484 if (VAR_P (decl))
7485 {
7486 layout_var_decl (decl);
7487 maybe_commonize_var (decl);
7488 }
7489
7490 /* This needs to happen after the linkage is set. */
7491 determine_visibility (decl);
7492
7493 if (var_definition_p && TREE_STATIC (decl))
7494 {
7495 /* If a TREE_READONLY variable needs initialization
7496 at runtime, it is no longer readonly and we need to
7497 avoid MEM_READONLY_P being set on RTL created for it. */
7498 if (init)
7499 {
7500 if (TREE_READONLY (decl))
7501 TREE_READONLY (decl) = 0;
7502 was_readonly = 0;
7503 }
7504 else if (was_readonly)
7505 TREE_READONLY (decl) = 1;
7506
7507 /* Likewise if it needs destruction. */
7508 if (!decl_maybe_constant_destruction (decl, type))
7509 TREE_READONLY (decl) = 0;
7510 }
7511
7512 make_rtl_for_nonlocal_decl (decl, init, asmspec);
7513
7514 /* Check for abstractness of the type. Notice that there is no
7515 need to strip array types here since the check for those types
7516 is already done within create_array_type_for_decl. */
7517 abstract_virtuals_error (decl, type);
7518
7519 if (TREE_TYPE (decl) == error_mark_node)
7520 /* No initialization required. */
7521 ;
7522 else if (TREE_CODE (decl) == FUNCTION_DECL)
7523 {
7524 if (init)
7525 {
7526 if (init == ridpointers[(int)RID_DEFAULT])
7527 {
7528 /* An out-of-class default definition is defined at
7529 the point where it is explicitly defaulted. */
7530 if (DECL_DELETED_FN (decl))
7531 maybe_explain_implicit_delete (decl);
7532 else if (DECL_INITIAL (decl) == error_mark_node)
7533 synthesize_method (decl);
7534 }
7535 else
7536 error_at (cp_expr_loc_or_loc (init,
7537 DECL_SOURCE_LOCATION (decl)),
7538 "function %q#D is initialized like a variable",
7539 decl);
7540 }
7541 /* else no initialization required. */
7542 }
7543 else if (DECL_EXTERNAL (decl)
7544 && ! (DECL_LANG_SPECIFIC (decl)
7545 && DECL_NOT_REALLY_EXTERN (decl)))
7546 {
7547 /* check_initializer will have done any constant initialization. */
7548 }
7549 /* A variable definition. */
7550 else if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
7551 /* Initialize the local variable. */
7552 initialize_local_var (decl, init);
7553
7554 /* If a variable is defined, and then a subsequent
7555 definition with external linkage is encountered, we will
7556 get here twice for the same variable. We want to avoid
7557 calling expand_static_init more than once. For variables
7558 that are not static data members, we can call
7559 expand_static_init only when we actually process the
7560 initializer. It is not legal to redeclare a static data
7561 member, so this issue does not arise in that case. */
7562 else if (var_definition_p && TREE_STATIC (decl))
7563 expand_static_init (decl, init);
7564 }
7565
7566 /* If a CLEANUP_STMT was created to destroy a temporary bound to a
7567 reference, insert it in the statement-tree now. */
7568 if (cleanups)
7569 {
7570 unsigned i; tree t;
7571 FOR_EACH_VEC_ELT (*cleanups, i, t)
7572 push_cleanup (decl, t, false);
7573 release_tree_vector (cleanups);
7574 }
7575
7576 if (was_readonly)
7577 TREE_READONLY (decl) = 1;
7578
7579 if (flag_openmp
7580 && VAR_P (decl)
7581 && lookup_attribute ("omp declare target implicit",
7582 DECL_ATTRIBUTES (decl)))
7583 {
7584 DECL_ATTRIBUTES (decl)
7585 = remove_attribute ("omp declare target implicit",
7586 DECL_ATTRIBUTES (decl));
7587 complete_type (TREE_TYPE (decl));
7588 if (!cp_omp_mappable_type (TREE_TYPE (decl)))
7589 {
7590 error ("%q+D in declare target directive does not have mappable"
7591 " type", decl);
7592 cp_omp_emit_unmappable_type_notes (TREE_TYPE (decl));
7593 }
7594 else if (!lookup_attribute ("omp declare target",
7595 DECL_ATTRIBUTES (decl))
7596 && !lookup_attribute ("omp declare target link",
7597 DECL_ATTRIBUTES (decl)))
7598 DECL_ATTRIBUTES (decl)
7599 = tree_cons (get_identifier ("omp declare target"),
7600 NULL_TREE, DECL_ATTRIBUTES (decl));
7601 }
7602
7603 invoke_plugin_callbacks (PLUGIN_FINISH_DECL, decl);
7604 }
7605
7606 /* For class TYPE return itself or some its bases that contain
7607 any direct non-static data members. Return error_mark_node if an
7608 error has been diagnosed. */
7609
7610 static tree
7611 find_decomp_class_base (location_t loc, tree type, tree ret)
7612 {
7613 bool member_seen = false;
7614 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
7615 if (TREE_CODE (field) != FIELD_DECL
7616 || DECL_ARTIFICIAL (field)
7617 || DECL_UNNAMED_BIT_FIELD (field))
7618 continue;
7619 else if (ret)
7620 return type;
7621 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
7622 {
7623 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE)
7624 error_at (loc, "cannot decompose class type %qT because it has an "
7625 "anonymous struct member", type);
7626 else
7627 error_at (loc, "cannot decompose class type %qT because it has an "
7628 "anonymous union member", type);
7629 inform (DECL_SOURCE_LOCATION (field), "declared here");
7630 return error_mark_node;
7631 }
7632 else if (!accessible_p (type, field, true))
7633 {
7634 error_at (loc, "cannot decompose inaccessible member %qD of %qT",
7635 field, type);
7636 inform (DECL_SOURCE_LOCATION (field),
7637 TREE_PRIVATE (field)
7638 ? G_("declared private here")
7639 : G_("declared protected here"));
7640 return error_mark_node;
7641 }
7642 else
7643 member_seen = true;
7644
7645 tree base_binfo, binfo;
7646 tree orig_ret = ret;
7647 int i;
7648 if (member_seen)
7649 ret = type;
7650 for (binfo = TYPE_BINFO (type), i = 0;
7651 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
7652 {
7653 tree t = find_decomp_class_base (loc, TREE_TYPE (base_binfo), ret);
7654 if (t == error_mark_node)
7655 return error_mark_node;
7656 if (t != NULL_TREE && t != ret)
7657 {
7658 if (ret == type)
7659 {
7660 error_at (loc, "cannot decompose class type %qT: both it and "
7661 "its base class %qT have non-static data members",
7662 type, t);
7663 return error_mark_node;
7664 }
7665 else if (orig_ret != NULL_TREE)
7666 return t;
7667 else if (ret != NULL_TREE)
7668 {
7669 error_at (loc, "cannot decompose class type %qT: its base "
7670 "classes %qT and %qT have non-static data "
7671 "members", type, ret, t);
7672 return error_mark_node;
7673 }
7674 else
7675 ret = t;
7676 }
7677 }
7678 return ret;
7679 }
7680
7681 /* Return std::tuple_size<TYPE>::value. */
7682
7683 static tree
7684 get_tuple_size (tree type)
7685 {
7686 tree args = make_tree_vec (1);
7687 TREE_VEC_ELT (args, 0) = type;
7688 tree inst = lookup_template_class (tuple_size_identifier, args,
7689 /*in_decl*/NULL_TREE,
7690 /*context*/std_node,
7691 /*entering_scope*/false, tf_none);
7692 inst = complete_type (inst);
7693 if (inst == error_mark_node || !COMPLETE_TYPE_P (inst))
7694 return NULL_TREE;
7695 tree val = lookup_qualified_name (inst, value_identifier,
7696 /*type*/false, /*complain*/false);
7697 if (TREE_CODE (val) == VAR_DECL || TREE_CODE (val) == CONST_DECL)
7698 val = maybe_constant_value (val);
7699 if (TREE_CODE (val) == INTEGER_CST)
7700 return val;
7701 else
7702 return error_mark_node;
7703 }
7704
7705 /* Return std::tuple_element<I,TYPE>::type. */
7706
7707 static tree
7708 get_tuple_element_type (tree type, unsigned i)
7709 {
7710 tree args = make_tree_vec (2);
7711 TREE_VEC_ELT (args, 0) = build_int_cst (integer_type_node, i);
7712 TREE_VEC_ELT (args, 1) = type;
7713 tree inst = lookup_template_class (tuple_element_identifier, args,
7714 /*in_decl*/NULL_TREE,
7715 /*context*/std_node,
7716 /*entering_scope*/false,
7717 tf_warning_or_error);
7718 return make_typename_type (inst, type_identifier,
7719 none_type, tf_warning_or_error);
7720 }
7721
7722 /* Return e.get<i>() or get<i>(e). */
7723
7724 static tree
7725 get_tuple_decomp_init (tree decl, unsigned i)
7726 {
7727 tree targs = make_tree_vec (1);
7728 TREE_VEC_ELT (targs, 0) = build_int_cst (integer_type_node, i);
7729
7730 tree etype = TREE_TYPE (decl);
7731 tree e = convert_from_reference (decl);
7732
7733 /* [The id-expression] e is an lvalue if the type of the entity e is an
7734 lvalue reference and an xvalue otherwise. */
7735 if (!TYPE_REF_P (etype)
7736 || TYPE_REF_IS_RVALUE (etype))
7737 e = move (e);
7738
7739 tree fns = lookup_qualified_name (TREE_TYPE (e), get__identifier,
7740 /*type*/false, /*complain*/false);
7741 bool use_member_get = false;
7742
7743 /* To use a member get, member lookup must find at least one
7744 declaration that is a function template
7745 whose first template parameter is a non-type parameter. */
7746 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (fns)); iter; ++iter)
7747 {
7748 tree fn = *iter;
7749 if (TREE_CODE (fn) == TEMPLATE_DECL)
7750 {
7751 tree tparms = DECL_TEMPLATE_PARMS (fn);
7752 tree parm = TREE_VEC_ELT (INNERMOST_TEMPLATE_PARMS (tparms), 0);
7753 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL)
7754 {
7755 use_member_get = true;
7756 break;
7757 }
7758 }
7759 }
7760
7761 if (use_member_get)
7762 {
7763 fns = lookup_template_function (fns, targs);
7764 return build_new_method_call (e, fns, /*args*/NULL,
7765 /*path*/NULL_TREE, LOOKUP_NORMAL,
7766 /*fn_p*/NULL, tf_warning_or_error);
7767 }
7768 else
7769 {
7770 releasing_vec args (make_tree_vector_single (e));
7771 fns = lookup_template_function (get__identifier, targs);
7772 fns = perform_koenig_lookup (fns, args, tf_warning_or_error);
7773 return finish_call_expr (fns, &args, /*novirt*/false,
7774 /*koenig*/true, tf_warning_or_error);
7775 }
7776 }
7777
7778 /* It's impossible to recover the decltype of a tuple decomposition variable
7779 based on the actual type of the variable, so store it in a hash table. */
7780
7781 static GTY((cache)) tree_cache_map *decomp_type_table;
7782
7783 tree
7784 lookup_decomp_type (tree v)
7785 {
7786 return *decomp_type_table->get (v);
7787 }
7788
7789 /* Mangle a decomposition declaration if needed. Arguments like
7790 in cp_finish_decomp. */
7791
7792 void
7793 cp_maybe_mangle_decomp (tree decl, tree first, unsigned int count)
7794 {
7795 if (!processing_template_decl
7796 && !error_operand_p (decl)
7797 && TREE_STATIC (decl))
7798 {
7799 auto_vec<tree, 16> v;
7800 v.safe_grow (count);
7801 tree d = first;
7802 for (unsigned int i = 0; i < count; i++, d = DECL_CHAIN (d))
7803 v[count - i - 1] = d;
7804 SET_DECL_ASSEMBLER_NAME (decl, mangle_decomp (decl, v));
7805 maybe_apply_pragma_weak (decl);
7806 }
7807 }
7808
7809 /* Finish a decomposition declaration. DECL is the underlying declaration
7810 "e", FIRST is the head of a chain of decls for the individual identifiers
7811 chained through DECL_CHAIN in reverse order and COUNT is the number of
7812 those decls. */
7813
7814 void
7815 cp_finish_decomp (tree decl, tree first, unsigned int count)
7816 {
7817 if (error_operand_p (decl))
7818 {
7819 error_out:
7820 while (count--)
7821 {
7822 TREE_TYPE (first) = error_mark_node;
7823 if (DECL_HAS_VALUE_EXPR_P (first))
7824 {
7825 SET_DECL_VALUE_EXPR (first, NULL_TREE);
7826 DECL_HAS_VALUE_EXPR_P (first) = 0;
7827 }
7828 first = DECL_CHAIN (first);
7829 }
7830 if (DECL_P (decl) && DECL_NAMESPACE_SCOPE_P (decl))
7831 SET_DECL_ASSEMBLER_NAME (decl, get_identifier ("<decomp>"));
7832 return;
7833 }
7834
7835 location_t loc = DECL_SOURCE_LOCATION (decl);
7836 if (type_dependent_expression_p (decl)
7837 /* This happens for range for when not in templates.
7838 Still add the DECL_VALUE_EXPRs for later processing. */
7839 || (!processing_template_decl
7840 && type_uses_auto (TREE_TYPE (decl))))
7841 {
7842 for (unsigned int i = 0; i < count; i++)
7843 {
7844 if (!DECL_HAS_VALUE_EXPR_P (first))
7845 {
7846 tree v = build_nt (ARRAY_REF, decl,
7847 size_int (count - i - 1),
7848 NULL_TREE, NULL_TREE);
7849 SET_DECL_VALUE_EXPR (first, v);
7850 DECL_HAS_VALUE_EXPR_P (first) = 1;
7851 }
7852 if (processing_template_decl)
7853 fit_decomposition_lang_decl (first, decl);
7854 first = DECL_CHAIN (first);
7855 }
7856 return;
7857 }
7858
7859 auto_vec<tree, 16> v;
7860 v.safe_grow (count);
7861 tree d = first;
7862 for (unsigned int i = 0; i < count; i++, d = DECL_CHAIN (d))
7863 {
7864 v[count - i - 1] = d;
7865 fit_decomposition_lang_decl (d, decl);
7866 }
7867
7868 tree type = TREE_TYPE (decl);
7869 tree dexp = decl;
7870
7871 if (TYPE_REF_P (type))
7872 {
7873 dexp = convert_from_reference (dexp);
7874 type = complete_type (TREE_TYPE (type));
7875 if (type == error_mark_node)
7876 goto error_out;
7877 if (!COMPLETE_TYPE_P (type))
7878 {
7879 error_at (loc, "structured binding refers to incomplete type %qT",
7880 type);
7881 goto error_out;
7882 }
7883 }
7884
7885 tree eltype = NULL_TREE;
7886 unsigned HOST_WIDE_INT eltscnt = 0;
7887 if (TREE_CODE (type) == ARRAY_TYPE)
7888 {
7889 tree nelts;
7890 nelts = array_type_nelts_top (type);
7891 if (nelts == error_mark_node)
7892 goto error_out;
7893 if (!tree_fits_uhwi_p (nelts))
7894 {
7895 error_at (loc, "cannot decompose variable length array %qT", type);
7896 goto error_out;
7897 }
7898 eltscnt = tree_to_uhwi (nelts);
7899 if (count != eltscnt)
7900 {
7901 cnt_mismatch:
7902 if (count > eltscnt)
7903 error_n (loc, count,
7904 "%u name provided for structured binding",
7905 "%u names provided for structured binding", count);
7906 else
7907 error_n (loc, count,
7908 "only %u name provided for structured binding",
7909 "only %u names provided for structured binding", count);
7910 inform_n (loc, eltscnt,
7911 "while %qT decomposes into %wu element",
7912 "while %qT decomposes into %wu elements",
7913 type, eltscnt);
7914 goto error_out;
7915 }
7916 eltype = TREE_TYPE (type);
7917 for (unsigned int i = 0; i < count; i++)
7918 {
7919 TREE_TYPE (v[i]) = eltype;
7920 layout_decl (v[i], 0);
7921 if (processing_template_decl)
7922 continue;
7923 tree t = unshare_expr (dexp);
7924 t = build4_loc (DECL_SOURCE_LOCATION (v[i]), ARRAY_REF,
7925 eltype, t, size_int (i), NULL_TREE,
7926 NULL_TREE);
7927 SET_DECL_VALUE_EXPR (v[i], t);
7928 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
7929 }
7930 }
7931 /* 2 GNU extensions. */
7932 else if (TREE_CODE (type) == COMPLEX_TYPE)
7933 {
7934 eltscnt = 2;
7935 if (count != eltscnt)
7936 goto cnt_mismatch;
7937 eltype = cp_build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
7938 for (unsigned int i = 0; i < count; i++)
7939 {
7940 TREE_TYPE (v[i]) = eltype;
7941 layout_decl (v[i], 0);
7942 if (processing_template_decl)
7943 continue;
7944 tree t = unshare_expr (dexp);
7945 t = build1_loc (DECL_SOURCE_LOCATION (v[i]),
7946 i ? IMAGPART_EXPR : REALPART_EXPR, eltype,
7947 t);
7948 SET_DECL_VALUE_EXPR (v[i], t);
7949 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
7950 }
7951 }
7952 else if (TREE_CODE (type) == VECTOR_TYPE)
7953 {
7954 if (!TYPE_VECTOR_SUBPARTS (type).is_constant (&eltscnt))
7955 {
7956 error_at (loc, "cannot decompose variable length vector %qT", type);
7957 goto error_out;
7958 }
7959 if (count != eltscnt)
7960 goto cnt_mismatch;
7961 eltype = cp_build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
7962 for (unsigned int i = 0; i < count; i++)
7963 {
7964 TREE_TYPE (v[i]) = eltype;
7965 layout_decl (v[i], 0);
7966 if (processing_template_decl)
7967 continue;
7968 tree t = unshare_expr (dexp);
7969 convert_vector_to_array_for_subscript (DECL_SOURCE_LOCATION (v[i]),
7970 &t, size_int (i));
7971 t = build4_loc (DECL_SOURCE_LOCATION (v[i]), ARRAY_REF,
7972 eltype, t, size_int (i), NULL_TREE,
7973 NULL_TREE);
7974 SET_DECL_VALUE_EXPR (v[i], t);
7975 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
7976 }
7977 }
7978 else if (tree tsize = get_tuple_size (type))
7979 {
7980 if (tsize == error_mark_node)
7981 {
7982 error_at (loc, "%<std::tuple_size<%T>::value%> is not an integral "
7983 "constant expression", type);
7984 goto error_out;
7985 }
7986 if (!tree_fits_uhwi_p (tsize))
7987 {
7988 error_n (loc, count,
7989 "%u name provided for structured binding",
7990 "%u names provided for structured binding", count);
7991 inform (loc, "while %qT decomposes into %E elements",
7992 type, tsize);
7993 goto error_out;
7994 }
7995 eltscnt = tree_to_uhwi (tsize);
7996 if (count != eltscnt)
7997 goto cnt_mismatch;
7998 int save_read = DECL_READ_P (decl);
7999 for (unsigned i = 0; i < count; ++i)
8000 {
8001 location_t sloc = input_location;
8002 location_t dloc = DECL_SOURCE_LOCATION (v[i]);
8003
8004 input_location = dloc;
8005 tree init = get_tuple_decomp_init (decl, i);
8006 tree eltype = (init == error_mark_node ? error_mark_node
8007 : get_tuple_element_type (type, i));
8008 input_location = sloc;
8009
8010 if (init == error_mark_node || eltype == error_mark_node)
8011 {
8012 inform (dloc, "in initialization of structured binding "
8013 "variable %qD", v[i]);
8014 goto error_out;
8015 }
8016 /* Save the decltype away before reference collapse. */
8017 hash_map_safe_put<hm_ggc> (decomp_type_table, v[i], eltype);
8018 eltype = cp_build_reference_type (eltype, !lvalue_p (init));
8019 TREE_TYPE (v[i]) = eltype;
8020 layout_decl (v[i], 0);
8021 if (DECL_HAS_VALUE_EXPR_P (v[i]))
8022 {
8023 /* In this case the names are variables, not just proxies. */
8024 SET_DECL_VALUE_EXPR (v[i], NULL_TREE);
8025 DECL_HAS_VALUE_EXPR_P (v[i]) = 0;
8026 }
8027 if (!processing_template_decl)
8028 {
8029 TREE_PUBLIC (v[i]) = TREE_PUBLIC (decl);
8030 TREE_STATIC (v[i]) = TREE_STATIC (decl);
8031 DECL_COMMON (v[i]) = DECL_COMMON (decl);
8032 DECL_COMDAT (v[i]) = DECL_COMDAT (decl);
8033 if (TREE_STATIC (v[i]))
8034 {
8035 CP_DECL_THREAD_LOCAL_P (v[i])
8036 = CP_DECL_THREAD_LOCAL_P (decl);
8037 set_decl_tls_model (v[i], DECL_TLS_MODEL (decl));
8038 if (DECL_ONE_ONLY (decl))
8039 make_decl_one_only (v[i], cxx_comdat_group (v[i]));
8040 if (TREE_PUBLIC (decl))
8041 DECL_WEAK (v[i]) = DECL_WEAK (decl);
8042 DECL_VISIBILITY (v[i]) = DECL_VISIBILITY (decl);
8043 DECL_VISIBILITY_SPECIFIED (v[i])
8044 = DECL_VISIBILITY_SPECIFIED (decl);
8045 }
8046 cp_finish_decl (v[i], init, /*constexpr*/false,
8047 /*asm*/NULL_TREE, LOOKUP_NORMAL);
8048 }
8049 }
8050 /* Ignore reads from the underlying decl performed during initialization
8051 of the individual variables. If those will be read, we'll mark
8052 the underlying decl as read at that point. */
8053 DECL_READ_P (decl) = save_read;
8054 }
8055 else if (TREE_CODE (type) == UNION_TYPE)
8056 {
8057 error_at (loc, "cannot decompose union type %qT", type);
8058 goto error_out;
8059 }
8060 else if (!CLASS_TYPE_P (type))
8061 {
8062 error_at (loc, "cannot decompose non-array non-class type %qT", type);
8063 goto error_out;
8064 }
8065 else if (LAMBDA_TYPE_P (type))
8066 {
8067 error_at (loc, "cannot decompose lambda closure type %qT", type);
8068 goto error_out;
8069 }
8070 else if (processing_template_decl && !COMPLETE_TYPE_P (type))
8071 pedwarn (loc, 0, "structured binding refers to incomplete class type %qT",
8072 type);
8073 else
8074 {
8075 tree btype = find_decomp_class_base (loc, type, NULL_TREE);
8076 if (btype == error_mark_node)
8077 goto error_out;
8078 else if (btype == NULL_TREE)
8079 {
8080 error_at (loc, "cannot decompose class type %qT without non-static "
8081 "data members", type);
8082 goto error_out;
8083 }
8084 for (tree field = TYPE_FIELDS (btype); field; field = TREE_CHAIN (field))
8085 if (TREE_CODE (field) != FIELD_DECL
8086 || DECL_ARTIFICIAL (field)
8087 || DECL_UNNAMED_BIT_FIELD (field))
8088 continue;
8089 else
8090 eltscnt++;
8091 if (count != eltscnt)
8092 goto cnt_mismatch;
8093 tree t = dexp;
8094 if (type != btype)
8095 {
8096 t = convert_to_base (t, btype, /*check_access*/true,
8097 /*nonnull*/false, tf_warning_or_error);
8098 type = btype;
8099 }
8100 unsigned int i = 0;
8101 for (tree field = TYPE_FIELDS (btype); field; field = TREE_CHAIN (field))
8102 if (TREE_CODE (field) != FIELD_DECL
8103 || DECL_ARTIFICIAL (field)
8104 || DECL_UNNAMED_BIT_FIELD (field))
8105 continue;
8106 else
8107 {
8108 tree tt = finish_non_static_data_member (field, unshare_expr (t),
8109 NULL_TREE);
8110 if (REFERENCE_REF_P (tt))
8111 tt = TREE_OPERAND (tt, 0);
8112 TREE_TYPE (v[i]) = TREE_TYPE (tt);
8113 layout_decl (v[i], 0);
8114 if (!processing_template_decl)
8115 {
8116 SET_DECL_VALUE_EXPR (v[i], tt);
8117 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
8118 }
8119 i++;
8120 }
8121 }
8122 if (processing_template_decl)
8123 {
8124 for (unsigned int i = 0; i < count; i++)
8125 if (!DECL_HAS_VALUE_EXPR_P (v[i]))
8126 {
8127 tree a = build_nt (ARRAY_REF, decl, size_int (i),
8128 NULL_TREE, NULL_TREE);
8129 SET_DECL_VALUE_EXPR (v[i], a);
8130 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
8131 }
8132 }
8133 }
8134
8135 /* Returns a declaration for a VAR_DECL as if:
8136
8137 extern "C" TYPE NAME;
8138
8139 had been seen. Used to create compiler-generated global
8140 variables. */
8141
8142 static tree
8143 declare_global_var (tree name, tree type)
8144 {
8145 tree decl;
8146
8147 push_to_top_level ();
8148 decl = build_decl (input_location, VAR_DECL, name, type);
8149 TREE_PUBLIC (decl) = 1;
8150 DECL_EXTERNAL (decl) = 1;
8151 DECL_ARTIFICIAL (decl) = 1;
8152 DECL_CONTEXT (decl) = FROB_CONTEXT (global_namespace);
8153 /* If the user has explicitly declared this variable (perhaps
8154 because the code we are compiling is part of a low-level runtime
8155 library), then it is possible that our declaration will be merged
8156 with theirs by pushdecl. */
8157 decl = pushdecl (decl);
8158 cp_finish_decl (decl, NULL_TREE, false, NULL_TREE, 0);
8159 pop_from_top_level ();
8160
8161 return decl;
8162 }
8163
8164 /* Returns the type for the argument to "__cxa_atexit" (or "atexit",
8165 if "__cxa_atexit" is not being used) corresponding to the function
8166 to be called when the program exits. */
8167
8168 static tree
8169 get_atexit_fn_ptr_type (void)
8170 {
8171 tree fn_type;
8172
8173 if (!atexit_fn_ptr_type_node)
8174 {
8175 tree arg_type;
8176 if (flag_use_cxa_atexit
8177 && !targetm.cxx.use_atexit_for_cxa_atexit ())
8178 /* The parameter to "__cxa_atexit" is "void (*)(void *)". */
8179 arg_type = ptr_type_node;
8180 else
8181 /* The parameter to "atexit" is "void (*)(void)". */
8182 arg_type = NULL_TREE;
8183
8184 fn_type = build_function_type_list (void_type_node,
8185 arg_type, NULL_TREE);
8186 atexit_fn_ptr_type_node = build_pointer_type (fn_type);
8187 }
8188
8189 return atexit_fn_ptr_type_node;
8190 }
8191
8192 /* Returns a pointer to the `atexit' function. Note that if
8193 FLAG_USE_CXA_ATEXIT is nonzero, then this will actually be the new
8194 `__cxa_atexit' function specified in the IA64 C++ ABI. */
8195
8196 static tree
8197 get_atexit_node (void)
8198 {
8199 tree atexit_fndecl;
8200 tree fn_type;
8201 tree fn_ptr_type;
8202 const char *name;
8203 bool use_aeabi_atexit;
8204
8205 if (atexit_node)
8206 return atexit_node;
8207
8208 if (flag_use_cxa_atexit && !targetm.cxx.use_atexit_for_cxa_atexit ())
8209 {
8210 /* The declaration for `__cxa_atexit' is:
8211
8212 int __cxa_atexit (void (*)(void *), void *, void *)
8213
8214 We build up the argument types and then the function type
8215 itself. */
8216 tree argtype0, argtype1, argtype2;
8217
8218 use_aeabi_atexit = targetm.cxx.use_aeabi_atexit ();
8219 /* First, build the pointer-to-function type for the first
8220 argument. */
8221 fn_ptr_type = get_atexit_fn_ptr_type ();
8222 /* Then, build the rest of the argument types. */
8223 argtype2 = ptr_type_node;
8224 if (use_aeabi_atexit)
8225 {
8226 argtype1 = fn_ptr_type;
8227 argtype0 = ptr_type_node;
8228 }
8229 else
8230 {
8231 argtype1 = ptr_type_node;
8232 argtype0 = fn_ptr_type;
8233 }
8234 /* And the final __cxa_atexit type. */
8235 fn_type = build_function_type_list (integer_type_node,
8236 argtype0, argtype1, argtype2,
8237 NULL_TREE);
8238 if (use_aeabi_atexit)
8239 name = "__aeabi_atexit";
8240 else
8241 name = "__cxa_atexit";
8242 }
8243 else
8244 {
8245 /* The declaration for `atexit' is:
8246
8247 int atexit (void (*)());
8248
8249 We build up the argument types and then the function type
8250 itself. */
8251 fn_ptr_type = get_atexit_fn_ptr_type ();
8252 /* Build the final atexit type. */
8253 fn_type = build_function_type_list (integer_type_node,
8254 fn_ptr_type, NULL_TREE);
8255 name = "atexit";
8256 }
8257
8258 /* Now, build the function declaration. */
8259 push_lang_context (lang_name_c);
8260 atexit_fndecl = build_library_fn_ptr (name, fn_type, ECF_LEAF | ECF_NOTHROW);
8261 mark_used (atexit_fndecl);
8262 pop_lang_context ();
8263 atexit_node = decay_conversion (atexit_fndecl, tf_warning_or_error);
8264
8265 return atexit_node;
8266 }
8267
8268 /* Like get_atexit_node, but for thread-local cleanups. */
8269
8270 static tree
8271 get_thread_atexit_node (void)
8272 {
8273 /* The declaration for `__cxa_thread_atexit' is:
8274
8275 int __cxa_thread_atexit (void (*)(void *), void *, void *) */
8276 tree fn_type = build_function_type_list (integer_type_node,
8277 get_atexit_fn_ptr_type (),
8278 ptr_type_node, ptr_type_node,
8279 NULL_TREE);
8280
8281 /* Now, build the function declaration. */
8282 tree atexit_fndecl = build_library_fn_ptr ("__cxa_thread_atexit", fn_type,
8283 ECF_LEAF | ECF_NOTHROW);
8284 return decay_conversion (atexit_fndecl, tf_warning_or_error);
8285 }
8286
8287 /* Returns the __dso_handle VAR_DECL. */
8288
8289 static tree
8290 get_dso_handle_node (void)
8291 {
8292 if (dso_handle_node)
8293 return dso_handle_node;
8294
8295 /* Declare the variable. */
8296 dso_handle_node = declare_global_var (get_identifier ("__dso_handle"),
8297 ptr_type_node);
8298
8299 #ifdef HAVE_GAS_HIDDEN
8300 if (dso_handle_node != error_mark_node)
8301 {
8302 DECL_VISIBILITY (dso_handle_node) = VISIBILITY_HIDDEN;
8303 DECL_VISIBILITY_SPECIFIED (dso_handle_node) = 1;
8304 }
8305 #endif
8306
8307 return dso_handle_node;
8308 }
8309
8310 /* Begin a new function with internal linkage whose job will be simply
8311 to destroy some particular variable. */
8312
8313 static GTY(()) int start_cleanup_cnt;
8314
8315 static tree
8316 start_cleanup_fn (void)
8317 {
8318 char name[32];
8319 tree fntype;
8320 tree fndecl;
8321 bool use_cxa_atexit = flag_use_cxa_atexit
8322 && !targetm.cxx.use_atexit_for_cxa_atexit ();
8323
8324 push_to_top_level ();
8325
8326 /* No need to mangle this. */
8327 push_lang_context (lang_name_c);
8328
8329 /* Build the name of the function. */
8330 sprintf (name, "__tcf_%d", start_cleanup_cnt++);
8331 /* Build the function declaration. */
8332 fntype = TREE_TYPE (get_atexit_fn_ptr_type ());
8333 fndecl = build_lang_decl (FUNCTION_DECL, get_identifier (name), fntype);
8334 /* It's a function with internal linkage, generated by the
8335 compiler. */
8336 TREE_PUBLIC (fndecl) = 0;
8337 DECL_ARTIFICIAL (fndecl) = 1;
8338 /* Make the function `inline' so that it is only emitted if it is
8339 actually needed. It is unlikely that it will be inlined, since
8340 it is only called via a function pointer, but we avoid unnecessary
8341 emissions this way. */
8342 DECL_DECLARED_INLINE_P (fndecl) = 1;
8343 DECL_INTERFACE_KNOWN (fndecl) = 1;
8344 /* Build the parameter. */
8345 if (use_cxa_atexit)
8346 {
8347 tree parmdecl = cp_build_parm_decl (fndecl, NULL_TREE, ptr_type_node);
8348 TREE_USED (parmdecl) = 1;
8349 DECL_READ_P (parmdecl) = 1;
8350 DECL_ARGUMENTS (fndecl) = parmdecl;
8351 }
8352
8353 pushdecl (fndecl);
8354 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
8355
8356 pop_lang_context ();
8357
8358 return current_function_decl;
8359 }
8360
8361 /* Finish the cleanup function begun by start_cleanup_fn. */
8362
8363 static void
8364 end_cleanup_fn (void)
8365 {
8366 expand_or_defer_fn (finish_function (/*inline_p=*/false));
8367
8368 pop_from_top_level ();
8369 }
8370
8371 /* Generate code to handle the destruction of DECL, an object with
8372 static storage duration. */
8373
8374 tree
8375 register_dtor_fn (tree decl)
8376 {
8377 tree cleanup;
8378 tree addr;
8379 tree compound_stmt;
8380 tree fcall;
8381 tree type;
8382 bool ob_parm, dso_parm, use_dtor;
8383 tree arg0, arg1, arg2;
8384 tree atex_node;
8385
8386 type = TREE_TYPE (decl);
8387 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
8388 return void_node;
8389
8390 if (decl_maybe_constant_destruction (decl, type)
8391 && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
8392 {
8393 cxx_maybe_build_cleanup (decl, tf_warning_or_error);
8394 return void_node;
8395 }
8396
8397 /* If we're using "__cxa_atexit" (or "__cxa_thread_atexit" or
8398 "__aeabi_atexit"), and DECL is a class object, we can just pass the
8399 destructor to "__cxa_atexit"; we don't have to build a temporary
8400 function to do the cleanup. */
8401 dso_parm = (flag_use_cxa_atexit
8402 && !targetm.cxx.use_atexit_for_cxa_atexit ());
8403 ob_parm = (CP_DECL_THREAD_LOCAL_P (decl) || dso_parm);
8404 use_dtor = ob_parm && CLASS_TYPE_P (type);
8405 if (use_dtor)
8406 {
8407 cleanup = get_class_binding (type, complete_dtor_identifier);
8408
8409 /* Make sure it is accessible. */
8410 perform_or_defer_access_check (TYPE_BINFO (type), cleanup, cleanup,
8411 tf_warning_or_error);
8412 }
8413 else
8414 {
8415 /* Call build_cleanup before we enter the anonymous function so
8416 that any access checks will be done relative to the current
8417 scope, rather than the scope of the anonymous function. */
8418 build_cleanup (decl);
8419
8420 /* Now start the function. */
8421 cleanup = start_cleanup_fn ();
8422
8423 /* Now, recompute the cleanup. It may contain SAVE_EXPRs that refer
8424 to the original function, rather than the anonymous one. That
8425 will make the back end think that nested functions are in use,
8426 which causes confusion. */
8427 push_deferring_access_checks (dk_no_check);
8428 fcall = build_cleanup (decl);
8429 pop_deferring_access_checks ();
8430
8431 /* Create the body of the anonymous function. */
8432 compound_stmt = begin_compound_stmt (BCS_FN_BODY);
8433 finish_expr_stmt (fcall);
8434 finish_compound_stmt (compound_stmt);
8435 end_cleanup_fn ();
8436 }
8437
8438 /* Call atexit with the cleanup function. */
8439 mark_used (cleanup);
8440 cleanup = build_address (cleanup);
8441
8442 if (CP_DECL_THREAD_LOCAL_P (decl))
8443 atex_node = get_thread_atexit_node ();
8444 else
8445 atex_node = get_atexit_node ();
8446
8447 if (use_dtor)
8448 {
8449 /* We must convert CLEANUP to the type that "__cxa_atexit"
8450 expects. */
8451 cleanup = build_nop (get_atexit_fn_ptr_type (), cleanup);
8452 /* "__cxa_atexit" will pass the address of DECL to the
8453 cleanup function. */
8454 mark_used (decl);
8455 addr = build_address (decl);
8456 /* The declared type of the parameter to "__cxa_atexit" is
8457 "void *". For plain "T*", we could just let the
8458 machinery in cp_build_function_call convert it -- but if the
8459 type is "cv-qualified T *", then we need to convert it
8460 before passing it in, to avoid spurious errors. */
8461 addr = build_nop (ptr_type_node, addr);
8462 }
8463 else
8464 /* Since the cleanup functions we build ignore the address
8465 they're given, there's no reason to pass the actual address
8466 in, and, in general, it's cheaper to pass NULL than any
8467 other value. */
8468 addr = null_pointer_node;
8469
8470 if (dso_parm)
8471 arg2 = cp_build_addr_expr (get_dso_handle_node (),
8472 tf_warning_or_error);
8473 else if (ob_parm)
8474 /* Just pass NULL to the dso handle parm if we don't actually
8475 have a DSO handle on this target. */
8476 arg2 = null_pointer_node;
8477 else
8478 arg2 = NULL_TREE;
8479
8480 if (ob_parm)
8481 {
8482 if (!CP_DECL_THREAD_LOCAL_P (decl)
8483 && targetm.cxx.use_aeabi_atexit ())
8484 {
8485 arg1 = cleanup;
8486 arg0 = addr;
8487 }
8488 else
8489 {
8490 arg1 = addr;
8491 arg0 = cleanup;
8492 }
8493 }
8494 else
8495 {
8496 arg0 = cleanup;
8497 arg1 = NULL_TREE;
8498 }
8499 return cp_build_function_call_nary (atex_node, tf_warning_or_error,
8500 arg0, arg1, arg2, NULL_TREE);
8501 }
8502
8503 /* DECL is a VAR_DECL with static storage duration. INIT, if present,
8504 is its initializer. Generate code to handle the construction
8505 and destruction of DECL. */
8506
8507 static void
8508 expand_static_init (tree decl, tree init)
8509 {
8510 gcc_assert (VAR_P (decl));
8511 gcc_assert (TREE_STATIC (decl));
8512
8513 /* Some variables require no dynamic initialization. */
8514 if (decl_maybe_constant_destruction (decl, TREE_TYPE (decl)))
8515 {
8516 /* Make sure the destructor is callable. */
8517 cxx_maybe_build_cleanup (decl, tf_warning_or_error);
8518 if (!init)
8519 return;
8520 }
8521
8522 if (CP_DECL_THREAD_LOCAL_P (decl) && DECL_GNU_TLS_P (decl)
8523 && !DECL_FUNCTION_SCOPE_P (decl))
8524 {
8525 location_t dloc = DECL_SOURCE_LOCATION (decl);
8526 if (init)
8527 error_at (dloc, "non-local variable %qD declared %<__thread%> "
8528 "needs dynamic initialization", decl);
8529 else
8530 error_at (dloc, "non-local variable %qD declared %<__thread%> "
8531 "has a non-trivial destructor", decl);
8532 static bool informed;
8533 if (!informed)
8534 {
8535 inform (dloc, "C++11 %<thread_local%> allows dynamic "
8536 "initialization and destruction");
8537 informed = true;
8538 }
8539 return;
8540 }
8541
8542 if (DECL_FUNCTION_SCOPE_P (decl))
8543 {
8544 /* Emit code to perform this initialization but once. */
8545 tree if_stmt = NULL_TREE, inner_if_stmt = NULL_TREE;
8546 tree then_clause = NULL_TREE, inner_then_clause = NULL_TREE;
8547 tree guard, guard_addr;
8548 tree flag, begin;
8549 /* We don't need thread-safety code for thread-local vars. */
8550 bool thread_guard = (flag_threadsafe_statics
8551 && !CP_DECL_THREAD_LOCAL_P (decl));
8552
8553 /* Emit code to perform this initialization but once. This code
8554 looks like:
8555
8556 static <type> guard;
8557 if (!__atomic_load (guard.first_byte)) {
8558 if (__cxa_guard_acquire (&guard)) {
8559 bool flag = false;
8560 try {
8561 // Do initialization.
8562 flag = true; __cxa_guard_release (&guard);
8563 // Register variable for destruction at end of program.
8564 } catch {
8565 if (!flag) __cxa_guard_abort (&guard);
8566 }
8567 }
8568 }
8569
8570 Note that the `flag' variable is only set to 1 *after* the
8571 initialization is complete. This ensures that an exception,
8572 thrown during the construction, will cause the variable to
8573 reinitialized when we pass through this code again, as per:
8574
8575 [stmt.dcl]
8576
8577 If the initialization exits by throwing an exception, the
8578 initialization is not complete, so it will be tried again
8579 the next time control enters the declaration.
8580
8581 This process should be thread-safe, too; multiple threads
8582 should not be able to initialize the variable more than
8583 once. */
8584
8585 /* Create the guard variable. */
8586 guard = get_guard (decl);
8587
8588 /* Begin the conditional initialization. */
8589 if_stmt = begin_if_stmt ();
8590
8591 finish_if_stmt_cond (get_guard_cond (guard, thread_guard), if_stmt);
8592 then_clause = begin_compound_stmt (BCS_NO_SCOPE);
8593
8594 if (thread_guard)
8595 {
8596 tree vfntype = NULL_TREE;
8597 tree acquire_name, release_name, abort_name;
8598 tree acquire_fn, release_fn, abort_fn;
8599 guard_addr = build_address (guard);
8600
8601 acquire_name = get_identifier ("__cxa_guard_acquire");
8602 release_name = get_identifier ("__cxa_guard_release");
8603 abort_name = get_identifier ("__cxa_guard_abort");
8604 acquire_fn = get_global_binding (acquire_name);
8605 release_fn = get_global_binding (release_name);
8606 abort_fn = get_global_binding (abort_name);
8607 if (!acquire_fn)
8608 acquire_fn = push_library_fn
8609 (acquire_name, build_function_type_list (integer_type_node,
8610 TREE_TYPE (guard_addr),
8611 NULL_TREE),
8612 NULL_TREE, ECF_NOTHROW | ECF_LEAF);
8613 if (!release_fn || !abort_fn)
8614 vfntype = build_function_type_list (void_type_node,
8615 TREE_TYPE (guard_addr),
8616 NULL_TREE);
8617 if (!release_fn)
8618 release_fn = push_library_fn (release_name, vfntype, NULL_TREE,
8619 ECF_NOTHROW | ECF_LEAF);
8620 if (!abort_fn)
8621 abort_fn = push_library_fn (abort_name, vfntype, NULL_TREE,
8622 ECF_NOTHROW | ECF_LEAF);
8623
8624 inner_if_stmt = begin_if_stmt ();
8625 finish_if_stmt_cond (build_call_n (acquire_fn, 1, guard_addr),
8626 inner_if_stmt);
8627
8628 inner_then_clause = begin_compound_stmt (BCS_NO_SCOPE);
8629 begin = get_target_expr (boolean_false_node);
8630 flag = TARGET_EXPR_SLOT (begin);
8631
8632 TARGET_EXPR_CLEANUP (begin)
8633 = build3 (COND_EXPR, void_type_node, flag,
8634 void_node,
8635 build_call_n (abort_fn, 1, guard_addr));
8636 CLEANUP_EH_ONLY (begin) = 1;
8637
8638 /* Do the initialization itself. */
8639 init = add_stmt_to_compound (begin, init);
8640 init = add_stmt_to_compound
8641 (init, build2 (MODIFY_EXPR, void_type_node, flag, boolean_true_node));
8642 init = add_stmt_to_compound
8643 (init, build_call_n (release_fn, 1, guard_addr));
8644 }
8645 else
8646 init = add_stmt_to_compound (init, set_guard (guard));
8647
8648 /* Use atexit to register a function for destroying this static
8649 variable. */
8650 init = add_stmt_to_compound (init, register_dtor_fn (decl));
8651
8652 finish_expr_stmt (init);
8653
8654 if (thread_guard)
8655 {
8656 finish_compound_stmt (inner_then_clause);
8657 finish_then_clause (inner_if_stmt);
8658 finish_if_stmt (inner_if_stmt);
8659 }
8660
8661 finish_compound_stmt (then_clause);
8662 finish_then_clause (if_stmt);
8663 finish_if_stmt (if_stmt);
8664 }
8665 else if (CP_DECL_THREAD_LOCAL_P (decl))
8666 tls_aggregates = tree_cons (init, decl, tls_aggregates);
8667 else
8668 static_aggregates = tree_cons (init, decl, static_aggregates);
8669 }
8670
8671 \f
8672 /* Make TYPE a complete type based on INITIAL_VALUE.
8673 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
8674 2 if there was no information (in which case assume 0 if DO_DEFAULT),
8675 3 if the initializer list is empty (in pedantic mode). */
8676
8677 int
8678 cp_complete_array_type (tree *ptype, tree initial_value, bool do_default)
8679 {
8680 int failure;
8681 tree type, elt_type;
8682
8683 /* Don't get confused by a CONSTRUCTOR for some other type. */
8684 if (initial_value && TREE_CODE (initial_value) == CONSTRUCTOR
8685 && !BRACE_ENCLOSED_INITIALIZER_P (initial_value)
8686 && TREE_CODE (TREE_TYPE (initial_value)) != ARRAY_TYPE)
8687 return 1;
8688
8689 if (initial_value)
8690 {
8691 unsigned HOST_WIDE_INT i;
8692 tree value;
8693
8694 /* An array of character type can be initialized from a
8695 brace-enclosed string constant.
8696
8697 FIXME: this code is duplicated from reshape_init. Probably
8698 we should just call reshape_init here? */
8699 if (char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (*ptype)))
8700 && TREE_CODE (initial_value) == CONSTRUCTOR
8701 && !vec_safe_is_empty (CONSTRUCTOR_ELTS (initial_value)))
8702 {
8703 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
8704 tree value = (*v)[0].value;
8705 STRIP_ANY_LOCATION_WRAPPER (value);
8706
8707 if (TREE_CODE (value) == STRING_CST
8708 && v->length () == 1)
8709 initial_value = value;
8710 }
8711
8712 /* If any of the elements are parameter packs, we can't actually
8713 complete this type now because the array size is dependent. */
8714 if (TREE_CODE (initial_value) == CONSTRUCTOR)
8715 {
8716 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (initial_value),
8717 i, value)
8718 {
8719 if (PACK_EXPANSION_P (value))
8720 return 0;
8721 }
8722 }
8723 }
8724
8725 failure = complete_array_type (ptype, initial_value, do_default);
8726
8727 /* We can create the array before the element type is complete, which
8728 means that we didn't have these two bits set in the original type
8729 either. In completing the type, we are expected to propagate these
8730 bits. See also complete_type which does the same thing for arrays
8731 of fixed size. */
8732 type = *ptype;
8733 if (type != error_mark_node && TYPE_DOMAIN (type))
8734 {
8735 elt_type = TREE_TYPE (type);
8736 TYPE_NEEDS_CONSTRUCTING (type) = TYPE_NEEDS_CONSTRUCTING (elt_type);
8737 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
8738 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (elt_type);
8739 }
8740
8741 return failure;
8742 }
8743
8744 /* As above, but either give an error or reject zero-size arrays, depending
8745 on COMPLAIN. */
8746
8747 int
8748 cp_complete_array_type_or_error (tree *ptype, tree initial_value,
8749 bool do_default, tsubst_flags_t complain)
8750 {
8751 int failure;
8752 bool sfinae = !(complain & tf_error);
8753 /* In SFINAE context we can't be lenient about zero-size arrays. */
8754 if (sfinae)
8755 ++pedantic;
8756 failure = cp_complete_array_type (ptype, initial_value, do_default);
8757 if (sfinae)
8758 --pedantic;
8759 if (failure)
8760 {
8761 if (sfinae)
8762 /* Not an error. */;
8763 else if (failure == 1)
8764 error ("initializer fails to determine size of %qT", *ptype);
8765 else if (failure == 2)
8766 {
8767 if (do_default)
8768 error ("array size missing in %qT", *ptype);
8769 }
8770 else if (failure == 3)
8771 error ("zero-size array %qT", *ptype);
8772 *ptype = error_mark_node;
8773 }
8774 return failure;
8775 }
8776 \f
8777 /* Return zero if something is declared to be a member of type
8778 CTYPE when in the context of CUR_TYPE. STRING is the error
8779 message to print in that case. Otherwise, quietly return 1. */
8780
8781 static int
8782 member_function_or_else (tree ctype, tree cur_type, enum overload_flags flags)
8783 {
8784 if (ctype && ctype != cur_type)
8785 {
8786 if (flags == DTOR_FLAG)
8787 error ("destructor for alien class %qT cannot be a member", ctype);
8788 else
8789 error ("constructor for alien class %qT cannot be a member", ctype);
8790 return 0;
8791 }
8792 return 1;
8793 }
8794 \f
8795 /* Subroutine of `grokdeclarator'. */
8796
8797 /* Generate errors possibly applicable for a given set of specifiers.
8798 This is for ARM $7.1.2. */
8799
8800 static void
8801 bad_specifiers (tree object,
8802 enum bad_spec_place type,
8803 int virtualp,
8804 int quals,
8805 int inlinep,
8806 int friendp,
8807 int raises,
8808 const location_t* locations)
8809 {
8810 switch (type)
8811 {
8812 case BSP_VAR:
8813 if (virtualp)
8814 error_at (locations[ds_virtual],
8815 "%qD declared as a %<virtual%> variable", object);
8816 if (quals)
8817 error ("%<const%> and %<volatile%> function specifiers on "
8818 "%qD invalid in variable declaration", object);
8819 break;
8820 case BSP_PARM:
8821 if (virtualp)
8822 error_at (locations[ds_virtual],
8823 "%qD declared as a %<virtual%> parameter", object);
8824 if (inlinep)
8825 error_at (locations[ds_inline],
8826 "%qD declared as an %<inline%> parameter", object);
8827 if (quals)
8828 error ("%<const%> and %<volatile%> function specifiers on "
8829 "%qD invalid in parameter declaration", object);
8830 break;
8831 case BSP_TYPE:
8832 if (virtualp)
8833 error_at (locations[ds_virtual],
8834 "%qD declared as a %<virtual%> type", object);
8835 if (inlinep)
8836 error_at (locations[ds_inline],
8837 "%qD declared as an %<inline%> type", object);
8838 if (quals)
8839 error ("%<const%> and %<volatile%> function specifiers on "
8840 "%qD invalid in type declaration", object);
8841 break;
8842 case BSP_FIELD:
8843 if (virtualp)
8844 error_at (locations[ds_virtual],
8845 "%qD declared as a %<virtual%> field", object);
8846 if (inlinep)
8847 error_at (locations[ds_inline],
8848 "%qD declared as an %<inline%> field", object);
8849 if (quals)
8850 error ("%<const%> and %<volatile%> function specifiers on "
8851 "%qD invalid in field declaration", object);
8852 break;
8853 default:
8854 gcc_unreachable();
8855 }
8856 if (friendp)
8857 error ("%q+D declared as a friend", object);
8858 if (raises
8859 && !flag_noexcept_type
8860 && (TREE_CODE (object) == TYPE_DECL
8861 || (!TYPE_PTRFN_P (TREE_TYPE (object))
8862 && !TYPE_REFFN_P (TREE_TYPE (object))
8863 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (object)))))
8864 error ("%q+D declared with an exception specification", object);
8865 }
8866
8867 /* DECL is a member function or static data member and is presently
8868 being defined. Check that the definition is taking place in a
8869 valid namespace. */
8870
8871 static void
8872 check_class_member_definition_namespace (tree decl)
8873 {
8874 /* These checks only apply to member functions and static data
8875 members. */
8876 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
8877 /* We check for problems with specializations in pt.c in
8878 check_specialization_namespace, where we can issue better
8879 diagnostics. */
8880 if (processing_specialization)
8881 return;
8882 /* We check this in check_explicit_instantiation_namespace. */
8883 if (processing_explicit_instantiation)
8884 return;
8885 /* [class.mfct]
8886
8887 A member function definition that appears outside of the
8888 class definition shall appear in a namespace scope enclosing
8889 the class definition.
8890
8891 [class.static.data]
8892
8893 The definition for a static data member shall appear in a
8894 namespace scope enclosing the member's class definition. */
8895 if (!is_ancestor (current_namespace, DECL_CONTEXT (decl)))
8896 permerror (input_location, "definition of %qD is not in namespace enclosing %qT",
8897 decl, DECL_CONTEXT (decl));
8898 }
8899
8900 /* Build a PARM_DECL for the "this" parameter of FN. TYPE is the
8901 METHOD_TYPE for a non-static member function; QUALS are the
8902 cv-qualifiers that apply to the function. */
8903
8904 tree
8905 build_this_parm (tree fn, tree type, cp_cv_quals quals)
8906 {
8907 tree this_type;
8908 tree qual_type;
8909 tree parm;
8910 cp_cv_quals this_quals;
8911
8912 if (CLASS_TYPE_P (type))
8913 {
8914 this_type
8915 = cp_build_qualified_type (type, quals & ~TYPE_QUAL_RESTRICT);
8916 this_type = build_pointer_type (this_type);
8917 }
8918 else
8919 this_type = type_of_this_parm (type);
8920 /* The `this' parameter is implicitly `const'; it cannot be
8921 assigned to. */
8922 this_quals = (quals & TYPE_QUAL_RESTRICT) | TYPE_QUAL_CONST;
8923 qual_type = cp_build_qualified_type (this_type, this_quals);
8924 parm = build_artificial_parm (fn, this_identifier, qual_type);
8925 cp_apply_type_quals_to_decl (this_quals, parm);
8926 return parm;
8927 }
8928
8929 /* DECL is a static member function. Complain if it was declared
8930 with function-cv-quals. */
8931
8932 static void
8933 check_static_quals (tree decl, cp_cv_quals quals)
8934 {
8935 if (quals != TYPE_UNQUALIFIED)
8936 error ("static member function %q#D declared with type qualifiers",
8937 decl);
8938 }
8939
8940 // Check that FN takes no arguments and returns bool.
8941 static void
8942 check_concept_fn (tree fn)
8943 {
8944 // A constraint is nullary.
8945 if (DECL_ARGUMENTS (fn))
8946 error_at (DECL_SOURCE_LOCATION (fn),
8947 "concept %q#D declared with function parameters", fn);
8948
8949 // The declared return type of the concept shall be bool, and
8950 // it shall not be deduced from it definition.
8951 tree type = TREE_TYPE (TREE_TYPE (fn));
8952 if (is_auto (type))
8953 error_at (DECL_SOURCE_LOCATION (fn),
8954 "concept %q#D declared with a deduced return type", fn);
8955 else if (type != boolean_type_node)
8956 error_at (DECL_SOURCE_LOCATION (fn),
8957 "concept %q#D with non-%<bool%> return type %qT", fn, type);
8958 }
8959
8960 /* Helper function. Replace the temporary this parameter injected
8961 during cp_finish_omp_declare_simd with the real this parameter. */
8962
8963 static tree
8964 declare_simd_adjust_this (tree *tp, int *walk_subtrees, void *data)
8965 {
8966 tree this_parm = (tree) data;
8967 if (TREE_CODE (*tp) == PARM_DECL
8968 && DECL_NAME (*tp) == this_identifier
8969 && *tp != this_parm)
8970 *tp = this_parm;
8971 else if (TYPE_P (*tp))
8972 *walk_subtrees = 0;
8973 return NULL_TREE;
8974 }
8975
8976 /* CTYPE is class type, or null if non-class.
8977 TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE
8978 or METHOD_TYPE.
8979 DECLARATOR is the function's name.
8980 PARMS is a chain of PARM_DECLs for the function.
8981 VIRTUALP is truthvalue of whether the function is virtual or not.
8982 FLAGS are to be passed through to `grokclassfn'.
8983 QUALS are qualifiers indicating whether the function is `const'
8984 or `volatile'.
8985 RAISES is a list of exceptions that this function can raise.
8986 CHECK is 1 if we must find this method in CTYPE, 0 if we should
8987 not look, and -1 if we should not call `grokclassfn' at all.
8988
8989 SFK is the kind of special function (if any) for the new function.
8990
8991 Returns `NULL_TREE' if something goes wrong, after issuing
8992 applicable error messages. */
8993
8994 static tree
8995 grokfndecl (tree ctype,
8996 tree type,
8997 tree declarator,
8998 tree parms,
8999 tree orig_declarator,
9000 const cp_decl_specifier_seq *declspecs,
9001 tree decl_reqs,
9002 int virtualp,
9003 enum overload_flags flags,
9004 cp_cv_quals quals,
9005 cp_ref_qualifier rqual,
9006 tree raises,
9007 int check,
9008 int friendp,
9009 int publicp,
9010 int inlinep,
9011 bool deletedp,
9012 special_function_kind sfk,
9013 bool funcdef_flag,
9014 bool late_return_type_p,
9015 int template_count,
9016 tree in_namespace,
9017 tree* attrlist,
9018 location_t location)
9019 {
9020 tree decl;
9021 int staticp = ctype && TREE_CODE (type) == FUNCTION_TYPE;
9022 tree t;
9023
9024 if (location == UNKNOWN_LOCATION)
9025 location = input_location;
9026
9027 /* Was the concept specifier present? */
9028 bool concept_p = inlinep & 4;
9029
9030 /* Concept declarations must have a corresponding definition. */
9031 if (concept_p && !funcdef_flag)
9032 {
9033 error_at (location, "concept %qD has no definition", declarator);
9034 return NULL_TREE;
9035 }
9036
9037 type = build_cp_fntype_variant (type, rqual, raises, late_return_type_p);
9038
9039 decl = build_lang_decl_loc (location, FUNCTION_DECL, declarator, type);
9040
9041 /* Set the constraints on the declaration. */
9042 if (flag_concepts)
9043 {
9044 tree tmpl_reqs = NULL_TREE;
9045 if (processing_template_decl > template_class_depth (ctype))
9046 tmpl_reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
9047 tree ci = build_constraints (tmpl_reqs, decl_reqs);
9048 if (concept_p && ci)
9049 {
9050 error_at (location, "a function concept cannot be constrained");
9051 ci = NULL_TREE;
9052 }
9053 set_constraints (decl, ci);
9054 }
9055
9056 if (TREE_CODE (type) == METHOD_TYPE)
9057 {
9058 tree parm = build_this_parm (decl, type, quals);
9059 DECL_CHAIN (parm) = parms;
9060 parms = parm;
9061
9062 /* Allocate space to hold the vptr bit if needed. */
9063 SET_DECL_ALIGN (decl, MINIMUM_METHOD_BOUNDARY);
9064 }
9065
9066 DECL_ARGUMENTS (decl) = parms;
9067 for (t = parms; t; t = DECL_CHAIN (t))
9068 DECL_CONTEXT (t) = decl;
9069
9070 /* Propagate volatile out from type to decl. */
9071 if (TYPE_VOLATILE (type))
9072 TREE_THIS_VOLATILE (decl) = 1;
9073
9074 /* Setup decl according to sfk. */
9075 switch (sfk)
9076 {
9077 case sfk_constructor:
9078 case sfk_copy_constructor:
9079 case sfk_move_constructor:
9080 DECL_CXX_CONSTRUCTOR_P (decl) = 1;
9081 DECL_NAME (decl) = ctor_identifier;
9082 break;
9083 case sfk_destructor:
9084 DECL_CXX_DESTRUCTOR_P (decl) = 1;
9085 DECL_NAME (decl) = dtor_identifier;
9086 break;
9087 default:
9088 break;
9089 }
9090
9091 if (friendp && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)
9092 {
9093 if (funcdef_flag)
9094 error_at (location,
9095 "defining explicit specialization %qD in friend declaration",
9096 orig_declarator);
9097 else
9098 {
9099 tree fns = TREE_OPERAND (orig_declarator, 0);
9100 tree args = TREE_OPERAND (orig_declarator, 1);
9101
9102 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
9103 {
9104 /* Something like `template <class T> friend void f<T>()'. */
9105 error_at (location,
9106 "invalid use of template-id %qD in declaration "
9107 "of primary template",
9108 orig_declarator);
9109 return NULL_TREE;
9110 }
9111
9112
9113 /* A friend declaration of the form friend void f<>(). Record
9114 the information in the TEMPLATE_ID_EXPR. */
9115 SET_DECL_IMPLICIT_INSTANTIATION (decl);
9116
9117 gcc_assert (identifier_p (fns) || OVL_P (fns));
9118 DECL_TEMPLATE_INFO (decl) = build_template_info (fns, args);
9119
9120 for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t))
9121 if (TREE_PURPOSE (t)
9122 && TREE_CODE (TREE_PURPOSE (t)) == DEFERRED_PARSE)
9123 {
9124 error_at (defparse_location (TREE_PURPOSE (t)),
9125 "default arguments are not allowed in declaration "
9126 "of friend template specialization %qD",
9127 decl);
9128 return NULL_TREE;
9129 }
9130
9131 if (inlinep & 1)
9132 {
9133 error_at (declspecs->locations[ds_inline],
9134 "%<inline%> is not allowed in declaration of friend "
9135 "template specialization %qD",
9136 decl);
9137 return NULL_TREE;
9138 }
9139 }
9140 }
9141
9142 /* C++17 11.3.6/4: "If a friend declaration specifies a default argument
9143 expression, that declaration shall be a definition..." */
9144 if (friendp && !funcdef_flag)
9145 {
9146 for (tree t = FUNCTION_FIRST_USER_PARMTYPE (decl);
9147 t && t != void_list_node; t = TREE_CHAIN (t))
9148 if (TREE_PURPOSE (t))
9149 {
9150 permerror (DECL_SOURCE_LOCATION (decl),
9151 "friend declaration of %qD specifies default "
9152 "arguments and isn%'t a definition", decl);
9153 break;
9154 }
9155 }
9156
9157 /* If this decl has namespace scope, set that up. */
9158 if (in_namespace)
9159 set_decl_namespace (decl, in_namespace, friendp);
9160 else if (!ctype)
9161 DECL_CONTEXT (decl) = FROB_CONTEXT (current_decl_namespace ());
9162
9163 /* `main' and builtins have implicit 'C' linkage. */
9164 if (ctype == NULL_TREE
9165 && DECL_FILE_SCOPE_P (decl)
9166 && current_lang_name == lang_name_cplusplus
9167 && (MAIN_NAME_P (declarator)
9168 || (IDENTIFIER_LENGTH (declarator) > 10
9169 && IDENTIFIER_POINTER (declarator)[0] == '_'
9170 && IDENTIFIER_POINTER (declarator)[1] == '_'
9171 && strncmp (IDENTIFIER_POINTER (declarator)+2,
9172 "builtin_", 8) == 0)
9173 || (targetcm.cxx_implicit_extern_c
9174 && (targetcm.cxx_implicit_extern_c
9175 (IDENTIFIER_POINTER (declarator))))))
9176 SET_DECL_LANGUAGE (decl, lang_c);
9177
9178 /* Should probably propagate const out from type to decl I bet (mrs). */
9179 if (staticp)
9180 {
9181 DECL_STATIC_FUNCTION_P (decl) = 1;
9182 DECL_CONTEXT (decl) = ctype;
9183 }
9184
9185 if (deletedp)
9186 DECL_DELETED_FN (decl) = 1;
9187
9188 if (ctype)
9189 {
9190 DECL_CONTEXT (decl) = ctype;
9191 if (funcdef_flag)
9192 check_class_member_definition_namespace (decl);
9193 }
9194
9195 if (ctype == NULL_TREE && DECL_MAIN_P (decl))
9196 {
9197 if (PROCESSING_REAL_TEMPLATE_DECL_P())
9198 error_at (location, "cannot declare %<::main%> to be a template");
9199 if (inlinep & 1)
9200 error_at (declspecs->locations[ds_inline],
9201 "cannot declare %<::main%> to be inline");
9202 if (inlinep & 2)
9203 error_at (declspecs->locations[ds_constexpr],
9204 "cannot declare %<::main%> to be %<constexpr%>");
9205 if (!publicp)
9206 error_at (location, "cannot declare %<::main%> to be static");
9207 inlinep = 0;
9208 publicp = 1;
9209 }
9210
9211 /* Members of anonymous types and local classes have no linkage; make
9212 them internal. If a typedef is made later, this will be changed. */
9213 if (ctype && (!TREE_PUBLIC (TYPE_MAIN_DECL (ctype))
9214 || decl_function_context (TYPE_MAIN_DECL (ctype))))
9215 publicp = 0;
9216
9217 if (publicp && cxx_dialect == cxx98)
9218 {
9219 /* [basic.link]: A name with no linkage (notably, the name of a class
9220 or enumeration declared in a local scope) shall not be used to
9221 declare an entity with linkage.
9222
9223 DR 757 relaxes this restriction for C++0x. */
9224 no_linkage_error (decl);
9225 }
9226
9227 TREE_PUBLIC (decl) = publicp;
9228 if (! publicp)
9229 {
9230 DECL_INTERFACE_KNOWN (decl) = 1;
9231 DECL_NOT_REALLY_EXTERN (decl) = 1;
9232 }
9233
9234 /* If the declaration was declared inline, mark it as such. */
9235 if (inlinep)
9236 {
9237 DECL_DECLARED_INLINE_P (decl) = 1;
9238 if (publicp)
9239 DECL_COMDAT (decl) = 1;
9240 }
9241 if (inlinep & 2)
9242 DECL_DECLARED_CONSTEXPR_P (decl) = true;
9243
9244 // If the concept declaration specifier was found, check
9245 // that the declaration satisfies the necessary requirements.
9246 if (concept_p)
9247 {
9248 DECL_DECLARED_CONCEPT_P (decl) = true;
9249 check_concept_fn (decl);
9250 }
9251
9252 DECL_EXTERNAL (decl) = 1;
9253 if (TREE_CODE (type) == FUNCTION_TYPE)
9254 {
9255 if (quals || rqual)
9256 TREE_TYPE (decl) = apply_memfn_quals (TREE_TYPE (decl),
9257 TYPE_UNQUALIFIED,
9258 REF_QUAL_NONE);
9259
9260 if (quals)
9261 {
9262 error (ctype
9263 ? G_("static member function %qD cannot have cv-qualifier")
9264 : G_("non-member function %qD cannot have cv-qualifier"),
9265 decl);
9266 quals = TYPE_UNQUALIFIED;
9267 }
9268
9269 if (rqual)
9270 {
9271 error (ctype
9272 ? G_("static member function %qD cannot have ref-qualifier")
9273 : G_("non-member function %qD cannot have ref-qualifier"),
9274 decl);
9275 rqual = REF_QUAL_NONE;
9276 }
9277 }
9278
9279 if (deduction_guide_p (decl))
9280 {
9281 if (!DECL_NAMESPACE_SCOPE_P (decl))
9282 {
9283 error_at (location, "deduction guide %qD must be declared at "
9284 "namespace scope", decl);
9285 return NULL_TREE;
9286 }
9287 if (funcdef_flag)
9288 error_at (location,
9289 "deduction guide %qD must not have a function body", decl);
9290 }
9291 else if (IDENTIFIER_ANY_OP_P (DECL_NAME (decl))
9292 && !grok_op_properties (decl, /*complain=*/true))
9293 return NULL_TREE;
9294 else if (UDLIT_OPER_P (DECL_NAME (decl)))
9295 {
9296 bool long_long_unsigned_p;
9297 bool long_double_p;
9298 const char *suffix = NULL;
9299 /* [over.literal]/6: Literal operators shall not have C linkage. */
9300 if (DECL_LANGUAGE (decl) == lang_c)
9301 {
9302 error_at (location, "literal operator with C linkage");
9303 maybe_show_extern_c_location ();
9304 return NULL_TREE;
9305 }
9306
9307 if (DECL_NAMESPACE_SCOPE_P (decl))
9308 {
9309 if (!check_literal_operator_args (decl, &long_long_unsigned_p,
9310 &long_double_p))
9311 {
9312 error_at (location, "%qD has invalid argument list", decl);
9313 return NULL_TREE;
9314 }
9315
9316 suffix = UDLIT_OP_SUFFIX (DECL_NAME (decl));
9317 if (long_long_unsigned_p)
9318 {
9319 if (cpp_interpret_int_suffix (parse_in, suffix, strlen (suffix)))
9320 warning_at (location, 0, "integer suffix %qs"
9321 " shadowed by implementation", suffix);
9322 }
9323 else if (long_double_p)
9324 {
9325 if (cpp_interpret_float_suffix (parse_in, suffix, strlen (suffix)))
9326 warning_at (location, 0, "floating-point suffix %qs"
9327 " shadowed by implementation", suffix);
9328 }
9329 /* 17.6.3.3.5 */
9330 if (suffix[0] != '_'
9331 && !in_system_header_at (location)
9332 && !current_function_decl && !(friendp && !funcdef_flag))
9333 warning_at (location, OPT_Wliteral_suffix,
9334 "literal operator suffixes not preceded by %<_%>"
9335 " are reserved for future standardization");
9336 }
9337 else
9338 {
9339 error_at (location, "%qD must be a non-member function", decl);
9340 return NULL_TREE;
9341 }
9342 }
9343
9344 if (funcdef_flag)
9345 /* Make the init_value nonzero so pushdecl knows this is not
9346 tentative. error_mark_node is replaced later with the BLOCK. */
9347 DECL_INITIAL (decl) = error_mark_node;
9348
9349 if (TYPE_NOTHROW_P (type) || nothrow_libfn_p (decl))
9350 TREE_NOTHROW (decl) = 1;
9351
9352 if (flag_openmp || flag_openmp_simd)
9353 {
9354 /* Adjust "omp declare simd" attributes. */
9355 tree ods = lookup_attribute ("omp declare simd", *attrlist);
9356 if (ods)
9357 {
9358 tree attr;
9359 for (attr = ods; attr;
9360 attr = lookup_attribute ("omp declare simd", TREE_CHAIN (attr)))
9361 {
9362 if (TREE_CODE (type) == METHOD_TYPE)
9363 walk_tree (&TREE_VALUE (attr), declare_simd_adjust_this,
9364 DECL_ARGUMENTS (decl), NULL);
9365 if (TREE_VALUE (attr) != NULL_TREE)
9366 {
9367 tree cl = TREE_VALUE (TREE_VALUE (attr));
9368 cl = c_omp_declare_simd_clauses_to_numbers
9369 (DECL_ARGUMENTS (decl), cl);
9370 if (cl)
9371 TREE_VALUE (TREE_VALUE (attr)) = cl;
9372 else
9373 TREE_VALUE (attr) = NULL_TREE;
9374 }
9375 }
9376 }
9377 }
9378
9379 /* Caller will do the rest of this. */
9380 if (check < 0)
9381 return decl;
9382
9383 if (ctype != NULL_TREE)
9384 grokclassfn (ctype, decl, flags);
9385
9386 /* 12.4/3 */
9387 if (cxx_dialect >= cxx11
9388 && DECL_DESTRUCTOR_P (decl)
9389 && !TYPE_BEING_DEFINED (DECL_CONTEXT (decl))
9390 && !processing_template_decl)
9391 deduce_noexcept_on_destructor (decl);
9392
9393 decl = check_explicit_specialization (orig_declarator, decl,
9394 template_count,
9395 2 * funcdef_flag +
9396 4 * (friendp != 0) +
9397 8 * concept_p,
9398 *attrlist);
9399 if (decl == error_mark_node)
9400 return NULL_TREE;
9401
9402 if (DECL_STATIC_FUNCTION_P (decl))
9403 check_static_quals (decl, quals);
9404
9405 if (attrlist)
9406 {
9407 cplus_decl_attributes (&decl, *attrlist, 0);
9408 *attrlist = NULL_TREE;
9409 }
9410
9411 /* Check main's type after attributes have been applied. */
9412 if (ctype == NULL_TREE && DECL_MAIN_P (decl))
9413 {
9414 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
9415 integer_type_node))
9416 {
9417 tree oldtypeargs = TYPE_ARG_TYPES (TREE_TYPE (decl));
9418 tree newtype;
9419 error_at (declspecs->locations[ds_type_spec],
9420 "%<::main%> must return %<int%>");
9421 newtype = build_function_type (integer_type_node, oldtypeargs);
9422 TREE_TYPE (decl) = newtype;
9423 }
9424 if (warn_main)
9425 check_main_parameter_types (decl);
9426 }
9427
9428 if (ctype != NULL_TREE && check)
9429 {
9430 tree old_decl = check_classfn (ctype, decl,
9431 (processing_template_decl
9432 > template_class_depth (ctype))
9433 ? current_template_parms
9434 : NULL_TREE);
9435
9436 if (old_decl == error_mark_node)
9437 return NULL_TREE;
9438
9439 if (old_decl)
9440 {
9441 tree ok;
9442 tree pushed_scope;
9443
9444 if (TREE_CODE (old_decl) == TEMPLATE_DECL)
9445 /* Because grokfndecl is always supposed to return a
9446 FUNCTION_DECL, we pull out the DECL_TEMPLATE_RESULT
9447 here. We depend on our callers to figure out that its
9448 really a template that's being returned. */
9449 old_decl = DECL_TEMPLATE_RESULT (old_decl);
9450
9451 if (DECL_STATIC_FUNCTION_P (old_decl)
9452 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
9453 {
9454 /* Remove the `this' parm added by grokclassfn. */
9455 revert_static_member_fn (decl);
9456 check_static_quals (decl, quals);
9457 }
9458 if (DECL_ARTIFICIAL (old_decl))
9459 {
9460 error ("definition of implicitly-declared %qD", old_decl);
9461 return NULL_TREE;
9462 }
9463 else if (DECL_DEFAULTED_FN (old_decl))
9464 {
9465 error ("definition of explicitly-defaulted %q+D", decl);
9466 inform (DECL_SOURCE_LOCATION (old_decl),
9467 "%q#D explicitly defaulted here", old_decl);
9468 return NULL_TREE;
9469 }
9470
9471 /* Since we've smashed OLD_DECL to its
9472 DECL_TEMPLATE_RESULT, we must do the same to DECL. */
9473 if (TREE_CODE (decl) == TEMPLATE_DECL)
9474 decl = DECL_TEMPLATE_RESULT (decl);
9475
9476 /* Attempt to merge the declarations. This can fail, in
9477 the case of some invalid specialization declarations. */
9478 pushed_scope = push_scope (ctype);
9479 ok = duplicate_decls (decl, old_decl, friendp);
9480 if (pushed_scope)
9481 pop_scope (pushed_scope);
9482 if (!ok)
9483 {
9484 error ("no %q#D member function declared in class %qT",
9485 decl, ctype);
9486 return NULL_TREE;
9487 }
9488 if (ok == error_mark_node)
9489 return NULL_TREE;
9490 return old_decl;
9491 }
9492 }
9493
9494 if (DECL_CONSTRUCTOR_P (decl) && !grok_ctor_properties (ctype, decl))
9495 return NULL_TREE;
9496
9497 if (ctype == NULL_TREE || check)
9498 return decl;
9499
9500 if (virtualp)
9501 DECL_VIRTUAL_P (decl) = 1;
9502
9503 return decl;
9504 }
9505
9506 /* decl is a FUNCTION_DECL.
9507 specifiers are the parsed virt-specifiers.
9508
9509 Set flags to reflect the virt-specifiers.
9510
9511 Returns decl. */
9512
9513 static tree
9514 set_virt_specifiers (tree decl, cp_virt_specifiers specifiers)
9515 {
9516 if (decl == NULL_TREE)
9517 return decl;
9518 if (specifiers & VIRT_SPEC_OVERRIDE)
9519 DECL_OVERRIDE_P (decl) = 1;
9520 if (specifiers & VIRT_SPEC_FINAL)
9521 DECL_FINAL_P (decl) = 1;
9522 return decl;
9523 }
9524
9525 /* DECL is a VAR_DECL for a static data member. Set flags to reflect
9526 the linkage that DECL will receive in the object file. */
9527
9528 static void
9529 set_linkage_for_static_data_member (tree decl)
9530 {
9531 /* A static data member always has static storage duration and
9532 external linkage. Note that static data members are forbidden in
9533 local classes -- the only situation in which a class has
9534 non-external linkage. */
9535 TREE_PUBLIC (decl) = 1;
9536 TREE_STATIC (decl) = 1;
9537 /* For non-template classes, static data members are always put
9538 out in exactly those files where they are defined, just as
9539 with ordinary namespace-scope variables. */
9540 if (!processing_template_decl)
9541 DECL_INTERFACE_KNOWN (decl) = 1;
9542 }
9543
9544 /* Create a VAR_DECL named NAME with the indicated TYPE.
9545
9546 If SCOPE is non-NULL, it is the class type or namespace containing
9547 the variable. If SCOPE is NULL, the variable should is created in
9548 the innermost enclosing scope. */
9549
9550 static tree
9551 grokvardecl (tree type,
9552 tree name,
9553 tree orig_declarator,
9554 const cp_decl_specifier_seq *declspecs,
9555 int initialized,
9556 int type_quals,
9557 int inlinep,
9558 bool conceptp,
9559 int template_count,
9560 tree scope,
9561 location_t location)
9562 {
9563 tree decl;
9564 tree explicit_scope;
9565
9566 gcc_assert (!name || identifier_p (name));
9567
9568 bool constp = (type_quals & TYPE_QUAL_CONST) != 0;
9569 bool volatilep = (type_quals & TYPE_QUAL_VOLATILE) != 0;
9570
9571 /* Compute the scope in which to place the variable, but remember
9572 whether or not that scope was explicitly specified by the user. */
9573 explicit_scope = scope;
9574 if (!scope)
9575 {
9576 /* An explicit "extern" specifier indicates a namespace-scope
9577 variable. */
9578 if (declspecs->storage_class == sc_extern)
9579 scope = current_decl_namespace ();
9580 else if (!at_function_scope_p ())
9581 scope = current_scope ();
9582 }
9583
9584 if (scope
9585 && (/* If the variable is a namespace-scope variable declared in a
9586 template, we need DECL_LANG_SPECIFIC. */
9587 (TREE_CODE (scope) == NAMESPACE_DECL && processing_template_decl)
9588 /* Similarly for namespace-scope variables with language linkage
9589 other than C++. */
9590 || (TREE_CODE (scope) == NAMESPACE_DECL
9591 && current_lang_name != lang_name_cplusplus)
9592 /* Similarly for static data members. */
9593 || TYPE_P (scope)
9594 /* Similarly for explicit specializations. */
9595 || (orig_declarator
9596 && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)))
9597 decl = build_lang_decl_loc (location, VAR_DECL, name, type);
9598 else
9599 decl = build_decl (location, VAR_DECL, name, type);
9600
9601 if (explicit_scope && TREE_CODE (explicit_scope) == NAMESPACE_DECL)
9602 set_decl_namespace (decl, explicit_scope, 0);
9603 else
9604 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
9605
9606 if (declspecs->storage_class == sc_extern)
9607 {
9608 DECL_THIS_EXTERN (decl) = 1;
9609 DECL_EXTERNAL (decl) = !initialized;
9610 }
9611
9612 if (DECL_CLASS_SCOPE_P (decl))
9613 {
9614 set_linkage_for_static_data_member (decl);
9615 /* This function is only called with out-of-class definitions. */
9616 DECL_EXTERNAL (decl) = 0;
9617 check_class_member_definition_namespace (decl);
9618 }
9619 /* At top level, either `static' or no s.c. makes a definition
9620 (perhaps tentative), and absence of `static' makes it public. */
9621 else if (toplevel_bindings_p ())
9622 {
9623 TREE_PUBLIC (decl) = (declspecs->storage_class != sc_static
9624 && (DECL_THIS_EXTERN (decl)
9625 || ! constp
9626 || volatilep
9627 || inlinep));
9628 TREE_STATIC (decl) = ! DECL_EXTERNAL (decl);
9629 }
9630 /* Not at top level, only `static' makes a static definition. */
9631 else
9632 {
9633 TREE_STATIC (decl) = declspecs->storage_class == sc_static;
9634 TREE_PUBLIC (decl) = DECL_EXTERNAL (decl);
9635 }
9636
9637 if (decl_spec_seq_has_spec_p (declspecs, ds_thread))
9638 {
9639 if (DECL_EXTERNAL (decl) || TREE_STATIC (decl))
9640 {
9641 CP_DECL_THREAD_LOCAL_P (decl) = true;
9642 if (!processing_template_decl)
9643 set_decl_tls_model (decl, decl_default_tls_model (decl));
9644 }
9645 if (declspecs->gnu_thread_keyword_p)
9646 SET_DECL_GNU_TLS_P (decl);
9647 }
9648
9649 /* If the type of the decl has no linkage, make sure that we'll
9650 notice that in mark_used. */
9651 if (cxx_dialect > cxx98
9652 && decl_linkage (decl) != lk_none
9653 && DECL_LANG_SPECIFIC (decl) == NULL
9654 && !DECL_EXTERN_C_P (decl)
9655 && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
9656 retrofit_lang_decl (decl);
9657
9658 if (TREE_PUBLIC (decl))
9659 {
9660 /* [basic.link]: A name with no linkage (notably, the name of a class
9661 or enumeration declared in a local scope) shall not be used to
9662 declare an entity with linkage.
9663
9664 DR 757 relaxes this restriction for C++0x. */
9665 if (cxx_dialect < cxx11)
9666 no_linkage_error (decl);
9667 }
9668 else
9669 DECL_INTERFACE_KNOWN (decl) = 1;
9670
9671 if (DECL_NAME (decl)
9672 && MAIN_NAME_P (DECL_NAME (decl))
9673 && scope == global_namespace)
9674 error_at (DECL_SOURCE_LOCATION (decl),
9675 "cannot declare %<::main%> to be a global variable");
9676
9677 /* Check that the variable can be safely declared as a concept.
9678 Note that this also forbids explicit specializations. */
9679 if (conceptp)
9680 {
9681 if (!processing_template_decl)
9682 {
9683 error_at (declspecs->locations[ds_concept],
9684 "a non-template variable cannot be %<concept%>");
9685 return NULL_TREE;
9686 }
9687 else
9688 DECL_DECLARED_CONCEPT_P (decl) = true;
9689 if (!same_type_ignoring_top_level_qualifiers_p (type, boolean_type_node))
9690 error_at (declspecs->locations[ds_type_spec],
9691 "concept must have type %<bool%>");
9692 if (TEMPLATE_PARMS_CONSTRAINTS (current_template_parms))
9693 {
9694 error_at (location, "a variable concept cannot be constrained");
9695 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = NULL_TREE;
9696 }
9697 }
9698 else if (flag_concepts
9699 && processing_template_decl > template_class_depth (scope))
9700 {
9701 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
9702 tree ci = build_constraints (reqs, NULL_TREE);
9703
9704 set_constraints (decl, ci);
9705 }
9706
9707 // Handle explicit specializations and instantiations of variable templates.
9708 if (orig_declarator)
9709 decl = check_explicit_specialization (orig_declarator, decl,
9710 template_count, conceptp * 8);
9711
9712 return decl != error_mark_node ? decl : NULL_TREE;
9713 }
9714
9715 /* Create and return a canonical pointer to member function type, for
9716 TYPE, which is a POINTER_TYPE to a METHOD_TYPE. */
9717
9718 tree
9719 build_ptrmemfunc_type (tree type)
9720 {
9721 tree field, fields;
9722 tree t;
9723
9724 if (type == error_mark_node)
9725 return type;
9726
9727 /* Make sure that we always have the unqualified pointer-to-member
9728 type first. */
9729 if (cp_cv_quals quals = cp_type_quals (type))
9730 {
9731 tree unqual = build_ptrmemfunc_type (TYPE_MAIN_VARIANT (type));
9732 return cp_build_qualified_type (unqual, quals);
9733 }
9734
9735 /* If a canonical type already exists for this type, use it. We use
9736 this method instead of type_hash_canon, because it only does a
9737 simple equality check on the list of field members. */
9738
9739 t = TYPE_PTRMEMFUNC_TYPE (type);
9740 if (t)
9741 return t;
9742
9743 t = make_node (RECORD_TYPE);
9744
9745 /* Let the front end know this is a pointer to member function. */
9746 TYPE_PTRMEMFUNC_FLAG (t) = 1;
9747
9748 field = build_decl (input_location, FIELD_DECL, pfn_identifier, type);
9749 DECL_NONADDRESSABLE_P (field) = 1;
9750 fields = field;
9751
9752 field = build_decl (input_location, FIELD_DECL, delta_identifier,
9753 delta_type_node);
9754 DECL_NONADDRESSABLE_P (field) = 1;
9755 DECL_CHAIN (field) = fields;
9756 fields = field;
9757
9758 finish_builtin_struct (t, "__ptrmemfunc_type", fields, ptr_type_node);
9759
9760 /* Zap out the name so that the back end will give us the debugging
9761 information for this anonymous RECORD_TYPE. */
9762 TYPE_NAME (t) = NULL_TREE;
9763
9764 /* Cache this pointer-to-member type so that we can find it again
9765 later. */
9766 TYPE_PTRMEMFUNC_TYPE (type) = t;
9767
9768 if (TYPE_STRUCTURAL_EQUALITY_P (type))
9769 SET_TYPE_STRUCTURAL_EQUALITY (t);
9770 else if (TYPE_CANONICAL (type) != type)
9771 TYPE_CANONICAL (t) = build_ptrmemfunc_type (TYPE_CANONICAL (type));
9772
9773 return t;
9774 }
9775
9776 /* Create and return a pointer to data member type. */
9777
9778 tree
9779 build_ptrmem_type (tree class_type, tree member_type)
9780 {
9781 if (TREE_CODE (member_type) == METHOD_TYPE)
9782 {
9783 cp_cv_quals quals = type_memfn_quals (member_type);
9784 cp_ref_qualifier rqual = type_memfn_rqual (member_type);
9785 member_type = build_memfn_type (member_type, class_type, quals, rqual);
9786 return build_ptrmemfunc_type (build_pointer_type (member_type));
9787 }
9788 else
9789 {
9790 gcc_assert (TREE_CODE (member_type) != FUNCTION_TYPE);
9791 return build_offset_type (class_type, member_type);
9792 }
9793 }
9794
9795 /* DECL is a VAR_DECL defined in-class, whose TYPE is also given.
9796 Check to see that the definition is valid. Issue appropriate error
9797 messages. */
9798
9799 static void
9800 check_static_variable_definition (tree decl, tree type)
9801 {
9802 /* Avoid redundant diagnostics on out-of-class definitions. */
9803 if (!current_class_type || !TYPE_BEING_DEFINED (current_class_type))
9804 ;
9805 /* Can't check yet if we don't know the type. */
9806 else if (dependent_type_p (type))
9807 ;
9808 /* If DECL is declared constexpr, we'll do the appropriate checks
9809 in check_initializer. Similarly for inline static data members. */
9810 else if (DECL_P (decl)
9811 && (DECL_DECLARED_CONSTEXPR_P (decl)
9812 || undeduced_auto_decl (decl)
9813 || DECL_VAR_DECLARED_INLINE_P (decl)))
9814 ;
9815 else if (cxx_dialect >= cxx11 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
9816 {
9817 if (!COMPLETE_TYPE_P (type))
9818 error_at (DECL_SOURCE_LOCATION (decl),
9819 "in-class initialization of static data member %q#D of "
9820 "incomplete type", decl);
9821 else if (literal_type_p (type))
9822 permerror (DECL_SOURCE_LOCATION (decl),
9823 "%<constexpr%> needed for in-class initialization of "
9824 "static data member %q#D of non-integral type", decl);
9825 else
9826 error_at (DECL_SOURCE_LOCATION (decl),
9827 "in-class initialization of static data member %q#D of "
9828 "non-literal type", decl);
9829 }
9830 /* Motion 10 at San Diego: If a static const integral data member is
9831 initialized with an integral constant expression, the initializer
9832 may appear either in the declaration (within the class), or in
9833 the definition, but not both. If it appears in the class, the
9834 member is a member constant. The file-scope definition is always
9835 required. */
9836 else if (!ARITHMETIC_TYPE_P (type) && TREE_CODE (type) != ENUMERAL_TYPE)
9837 error_at (DECL_SOURCE_LOCATION (decl),
9838 "invalid in-class initialization of static data member "
9839 "of non-integral type %qT",
9840 type);
9841 else if (!CP_TYPE_CONST_P (type))
9842 error_at (DECL_SOURCE_LOCATION (decl),
9843 "ISO C++ forbids in-class initialization of non-const "
9844 "static member %qD",
9845 decl);
9846 else if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type))
9847 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
9848 "ISO C++ forbids initialization of member constant "
9849 "%qD of non-integral type %qT", decl, type);
9850 }
9851
9852 /* *expr_p is part of the TYPE_SIZE of a variably-sized array. If any
9853 SAVE_EXPRs in *expr_p wrap expressions with side-effects, break those
9854 expressions out into temporary variables so that walk_tree doesn't
9855 step into them (c++/15764). */
9856
9857 static tree
9858 stabilize_save_expr_r (tree *expr_p, int *walk_subtrees, void *data)
9859 {
9860 hash_set<tree> *pset = (hash_set<tree> *)data;
9861 tree expr = *expr_p;
9862 if (TREE_CODE (expr) == SAVE_EXPR)
9863 {
9864 tree op = TREE_OPERAND (expr, 0);
9865 cp_walk_tree (&op, stabilize_save_expr_r, data, pset);
9866 if (TREE_SIDE_EFFECTS (op))
9867 TREE_OPERAND (expr, 0) = get_temp_regvar (TREE_TYPE (op), op);
9868 *walk_subtrees = 0;
9869 }
9870 else if (!EXPR_P (expr) || !TREE_SIDE_EFFECTS (expr))
9871 *walk_subtrees = 0;
9872 return NULL;
9873 }
9874
9875 /* Entry point for the above. */
9876
9877 static void
9878 stabilize_vla_size (tree size)
9879 {
9880 hash_set<tree> pset;
9881 /* Break out any function calls into temporary variables. */
9882 cp_walk_tree (&size, stabilize_save_expr_r, &pset, &pset);
9883 }
9884
9885 /* Reduce a SIZEOF_EXPR to its value. */
9886
9887 tree
9888 fold_sizeof_expr (tree t)
9889 {
9890 tree r;
9891 if (SIZEOF_EXPR_TYPE_P (t))
9892 r = cxx_sizeof_or_alignof_type (TREE_TYPE (TREE_OPERAND (t, 0)),
9893 SIZEOF_EXPR, false, false);
9894 else if (TYPE_P (TREE_OPERAND (t, 0)))
9895 r = cxx_sizeof_or_alignof_type (TREE_OPERAND (t, 0), SIZEOF_EXPR,
9896 false, false);
9897 else
9898 r = cxx_sizeof_or_alignof_expr (TREE_OPERAND (t, 0), SIZEOF_EXPR,
9899 false);
9900 if (r == error_mark_node)
9901 r = size_one_node;
9902 return r;
9903 }
9904
9905 /* Given the SIZE (i.e., number of elements) in an array, compute
9906 an appropriate index type for the array. If non-NULL, NAME is
9907 the name of the entity being declared. */
9908
9909 static tree
9910 compute_array_index_type_loc (location_t name_loc, tree name, tree size,
9911 tsubst_flags_t complain)
9912 {
9913 if (error_operand_p (size))
9914 return error_mark_node;
9915
9916 /* The type of the index being computed. */
9917 tree itype;
9918
9919 /* The original numeric size as seen in the source code before
9920 conversion to size_t. */
9921 tree origsize = size;
9922
9923 location_t loc = cp_expr_loc_or_loc (size, name ? name_loc : input_location);
9924
9925 if (!type_dependent_expression_p (size))
9926 {
9927 origsize = size = mark_rvalue_use (size);
9928
9929 if (cxx_dialect < cxx11 && TREE_CODE (size) == NOP_EXPR
9930 && TREE_SIDE_EFFECTS (size))
9931 /* In C++98, we mark a non-constant array bound with a magic
9932 NOP_EXPR with TREE_SIDE_EFFECTS; don't fold in that case. */;
9933 else
9934 {
9935 size = instantiate_non_dependent_expr_sfinae (size, complain);
9936 size = build_converted_constant_expr (size_type_node, size, complain);
9937 /* Pedantically a constant expression is required here and so
9938 __builtin_is_constant_evaluated () should fold to true if it
9939 is successfully folded into a constant. */
9940 size = maybe_constant_value (size, NULL_TREE,
9941 /*manifestly_const_eval=*/true);
9942
9943 if (!TREE_CONSTANT (size))
9944 size = origsize;
9945 }
9946
9947 if (error_operand_p (size))
9948 return error_mark_node;
9949
9950 /* The array bound must be an integer type. */
9951 tree type = TREE_TYPE (size);
9952 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
9953 {
9954 if (!(complain & tf_error))
9955 return error_mark_node;
9956 if (name)
9957 error_at (loc, "size of array %qD has non-integral type %qT",
9958 name, type);
9959 else
9960 error_at (loc, "size of array has non-integral type %qT", type);
9961 size = integer_one_node;
9962 }
9963 }
9964
9965 /* A type is dependent if it is...an array type constructed from any
9966 dependent type or whose size is specified by a constant expression
9967 that is value-dependent. */
9968 /* We can only call value_dependent_expression_p on integral constant
9969 expressions; treat non-constant expressions as dependent, too. */
9970 if (processing_template_decl
9971 && (type_dependent_expression_p (size)
9972 || !TREE_CONSTANT (size) || value_dependent_expression_p (size)))
9973 {
9974 /* We cannot do any checking for a SIZE that isn't known to be
9975 constant. Just build the index type and mark that it requires
9976 structural equality checks. */
9977 itype = build_index_type (build_min (MINUS_EXPR, sizetype,
9978 size, size_one_node));
9979 TYPE_DEPENDENT_P (itype) = 1;
9980 TYPE_DEPENDENT_P_VALID (itype) = 1;
9981 SET_TYPE_STRUCTURAL_EQUALITY (itype);
9982 return itype;
9983 }
9984
9985 if (TREE_CODE (size) != INTEGER_CST)
9986 {
9987 tree folded = cp_fully_fold (size);
9988 if (TREE_CODE (folded) == INTEGER_CST)
9989 {
9990 if (name)
9991 pedwarn (loc, OPT_Wpedantic, "size of array %qD is not an "
9992 "integral constant-expression", name);
9993 else
9994 pedwarn (loc, OPT_Wpedantic,
9995 "size of array is not an integral constant-expression");
9996 }
9997 /* Use the folded result for VLAs, too; it will have resolved
9998 SIZEOF_EXPR. */
9999 size = folded;
10000 }
10001
10002 /* Normally, the array-bound will be a constant. */
10003 if (TREE_CODE (size) == INTEGER_CST)
10004 {
10005 /* The size to use in diagnostics that reflects the constant
10006 size used in the source, rather than SIZE massaged above. */
10007 tree diagsize = size;
10008
10009 /* If the original size before conversion to size_t was signed
10010 and negative, convert it to ssizetype to restore the sign. */
10011 if (!TYPE_UNSIGNED (TREE_TYPE (origsize))
10012 && TREE_CODE (size) == INTEGER_CST
10013 && tree_int_cst_sign_bit (size))
10014 {
10015 diagsize = fold_convert (ssizetype, size);
10016
10017 /* Clear the overflow bit that may have been set as a result
10018 of the conversion from the sizetype of the new size to
10019 ssizetype. */
10020 TREE_OVERFLOW (diagsize) = false;
10021 }
10022
10023 /* Verify that the array has a positive number of elements
10024 and issue the appropriate diagnostic if it doesn't. */
10025 if (!valid_array_size_p (loc, diagsize, name, (complain & tf_error)))
10026 {
10027 if (!(complain & tf_error))
10028 return error_mark_node;
10029 size = integer_one_node;
10030 }
10031 /* As an extension we allow zero-sized arrays. */
10032 else if (integer_zerop (size))
10033 {
10034 if (!(complain & tf_error))
10035 /* We must fail if performing argument deduction (as
10036 indicated by the state of complain), so that
10037 another substitution can be found. */
10038 return error_mark_node;
10039 else if (in_system_header_at (input_location))
10040 /* Allow them in system headers because glibc uses them. */;
10041 else if (name)
10042 pedwarn (loc, OPT_Wpedantic,
10043 "ISO C++ forbids zero-size array %qD", name);
10044 else
10045 pedwarn (loc, OPT_Wpedantic,
10046 "ISO C++ forbids zero-size array");
10047 }
10048 }
10049 else if (TREE_CONSTANT (size)
10050 /* We don't allow VLAs at non-function scopes, or during
10051 tentative template substitution. */
10052 || !at_function_scope_p ()
10053 || !(complain & tf_error))
10054 {
10055 if (!(complain & tf_error))
10056 return error_mark_node;
10057 /* `(int) &fn' is not a valid array bound. */
10058 if (name)
10059 error_at (loc,
10060 "size of array %qD is not an integral constant-expression",
10061 name);
10062 else
10063 error_at (loc, "size of array is not an integral constant-expression");
10064 size = integer_one_node;
10065 }
10066 else if (pedantic && warn_vla != 0)
10067 {
10068 if (name)
10069 pedwarn (name_loc, OPT_Wvla,
10070 "ISO C++ forbids variable length array %qD", name);
10071 else
10072 pedwarn (input_location, OPT_Wvla,
10073 "ISO C++ forbids variable length array");
10074 }
10075 else if (warn_vla > 0)
10076 {
10077 if (name)
10078 warning_at (name_loc, OPT_Wvla,
10079 "variable length array %qD is used", name);
10080 else
10081 warning (OPT_Wvla,
10082 "variable length array is used");
10083 }
10084
10085 if (processing_template_decl && !TREE_CONSTANT (size))
10086 /* A variable sized array. */
10087 itype = build_min (MINUS_EXPR, sizetype, size, integer_one_node);
10088 else
10089 {
10090 /* Compute the index of the largest element in the array. It is
10091 one less than the number of elements in the array. We save
10092 and restore PROCESSING_TEMPLATE_DECL so that computations in
10093 cp_build_binary_op will be appropriately folded. */
10094 {
10095 processing_template_decl_sentinel s;
10096 itype = cp_build_binary_op (input_location,
10097 MINUS_EXPR,
10098 cp_convert (ssizetype, size, complain),
10099 cp_convert (ssizetype, integer_one_node,
10100 complain),
10101 complain);
10102 itype = maybe_constant_value (itype);
10103 }
10104
10105 if (!TREE_CONSTANT (itype))
10106 {
10107 /* A variable sized array. */
10108 itype = variable_size (itype);
10109
10110 stabilize_vla_size (itype);
10111
10112 if (sanitize_flags_p (SANITIZE_VLA)
10113 && current_function_decl != NULL_TREE)
10114 {
10115 /* We have to add 1 -- in the ubsan routine we generate
10116 LE_EXPR rather than LT_EXPR. */
10117 tree t = fold_build2 (PLUS_EXPR, TREE_TYPE (itype), itype,
10118 build_one_cst (TREE_TYPE (itype)));
10119 t = ubsan_instrument_vla (input_location, t);
10120 finish_expr_stmt (t);
10121 }
10122 }
10123 /* Make sure that there was no overflow when creating to a signed
10124 index type. (For example, on a 32-bit machine, an array with
10125 size 2^32 - 1 is too big.) */
10126 else if (TREE_CODE (itype) == INTEGER_CST
10127 && TREE_OVERFLOW (itype))
10128 {
10129 if (!(complain & tf_error))
10130 return error_mark_node;
10131 error ("overflow in array dimension");
10132 TREE_OVERFLOW (itype) = 0;
10133 }
10134 }
10135
10136 /* Create and return the appropriate index type. */
10137 itype = build_index_type (itype);
10138
10139 /* If the index type were dependent, we would have returned early, so
10140 remember that it isn't. */
10141 TYPE_DEPENDENT_P (itype) = 0;
10142 TYPE_DEPENDENT_P_VALID (itype) = 1;
10143 return itype;
10144 }
10145
10146 tree
10147 compute_array_index_type (tree name, tree size, tsubst_flags_t complain)
10148 {
10149 return compute_array_index_type_loc (input_location, name, size, complain);
10150 }
10151
10152 /* Returns the scope (if any) in which the entity declared by
10153 DECLARATOR will be located. If the entity was declared with an
10154 unqualified name, NULL_TREE is returned. */
10155
10156 tree
10157 get_scope_of_declarator (const cp_declarator *declarator)
10158 {
10159 while (declarator && declarator->kind != cdk_id)
10160 declarator = declarator->declarator;
10161
10162 /* If the declarator-id is a SCOPE_REF, the scope in which the
10163 declaration occurs is the first operand. */
10164 if (declarator
10165 && declarator->u.id.qualifying_scope)
10166 return declarator->u.id.qualifying_scope;
10167
10168 /* Otherwise, the declarator is not a qualified name; the entity will
10169 be declared in the current scope. */
10170 return NULL_TREE;
10171 }
10172
10173 /* Returns an ARRAY_TYPE for an array with SIZE elements of the
10174 indicated TYPE. If non-NULL, NAME is the NAME of the declaration
10175 with this type. */
10176
10177 static tree
10178 create_array_type_for_decl (tree name, tree type, tree size, location_t loc)
10179 {
10180 tree itype = NULL_TREE;
10181
10182 /* If things have already gone awry, bail now. */
10183 if (type == error_mark_node || size == error_mark_node)
10184 return error_mark_node;
10185
10186 /* 8.3.4/1: If the type of the identifier of D contains the auto
10187 type-specifier, the program is ill-formed. */
10188 if (type_uses_auto (type))
10189 {
10190 if (name)
10191 error_at (loc, "%qD declared as array of %qT", name, type);
10192 else
10193 error ("creating array of %qT", type);
10194 return error_mark_node;
10195 }
10196
10197 /* If there are some types which cannot be array elements,
10198 issue an error-message and return. */
10199 switch (TREE_CODE (type))
10200 {
10201 case VOID_TYPE:
10202 if (name)
10203 error_at (loc, "declaration of %qD as array of void", name);
10204 else
10205 error ("creating array of void");
10206 return error_mark_node;
10207
10208 case FUNCTION_TYPE:
10209 if (name)
10210 error_at (loc, "declaration of %qD as array of functions", name);
10211 else
10212 error ("creating array of functions");
10213 return error_mark_node;
10214
10215 case REFERENCE_TYPE:
10216 if (name)
10217 error_at (loc, "declaration of %qD as array of references", name);
10218 else
10219 error ("creating array of references");
10220 return error_mark_node;
10221
10222 case METHOD_TYPE:
10223 if (name)
10224 error_at (loc, "declaration of %qD as array of function members",
10225 name);
10226 else
10227 error ("creating array of function members");
10228 return error_mark_node;
10229
10230 default:
10231 break;
10232 }
10233
10234 /* [dcl.array]
10235
10236 The constant expressions that specify the bounds of the arrays
10237 can be omitted only for the first member of the sequence. */
10238 if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type))
10239 {
10240 if (name)
10241 error_at (loc, "declaration of %qD as multidimensional array must "
10242 "have bounds for all dimensions except the first",
10243 name);
10244 else
10245 error ("multidimensional array must have bounds for all "
10246 "dimensions except the first");
10247
10248 return error_mark_node;
10249 }
10250
10251 /* Figure out the index type for the array. */
10252 if (size)
10253 itype = compute_array_index_type_loc (loc, name, size,
10254 tf_warning_or_error);
10255
10256 /* [dcl.array]
10257 T is called the array element type; this type shall not be [...] an
10258 abstract class type. */
10259 abstract_virtuals_error (name, type);
10260
10261 return build_cplus_array_type (type, itype);
10262 }
10263
10264 /* Returns the smallest location that is not UNKNOWN_LOCATION. */
10265
10266 static location_t
10267 min_location (location_t loca, location_t locb)
10268 {
10269 if (loca == UNKNOWN_LOCATION
10270 || (locb != UNKNOWN_LOCATION
10271 && linemap_location_before_p (line_table, locb, loca)))
10272 return locb;
10273 return loca;
10274 }
10275
10276 /* Returns the smallest location != UNKNOWN_LOCATION among the
10277 three stored in LOCATIONS[ds_const], LOCATIONS[ds_volatile],
10278 and LOCATIONS[ds_restrict]. */
10279
10280 static location_t
10281 smallest_type_quals_location (int type_quals, const location_t* locations)
10282 {
10283 location_t loc = UNKNOWN_LOCATION;
10284
10285 if (type_quals & TYPE_QUAL_CONST)
10286 loc = locations[ds_const];
10287
10288 if (type_quals & TYPE_QUAL_VOLATILE)
10289 loc = min_location (loc, locations[ds_volatile]);
10290
10291 if (type_quals & TYPE_QUAL_RESTRICT)
10292 loc = min_location (loc, locations[ds_restrict]);
10293
10294 return loc;
10295 }
10296
10297 /* Returns the smallest among the latter and locations[ds_type_spec]. */
10298
10299 static location_t
10300 smallest_type_location (int type_quals, const location_t* locations)
10301 {
10302 location_t loc = smallest_type_quals_location (type_quals, locations);
10303 return min_location (loc, locations[ds_type_spec]);
10304 }
10305
10306 static location_t
10307 smallest_type_location (const cp_decl_specifier_seq *declspecs)
10308 {
10309 int type_quals = get_type_quals (declspecs);
10310 return smallest_type_location (type_quals, declspecs->locations);
10311 }
10312
10313 /* Check that it's OK to declare a function with the indicated TYPE
10314 and TYPE_QUALS. SFK indicates the kind of special function (if any)
10315 that this function is. OPTYPE is the type given in a conversion
10316 operator declaration, or the class type for a constructor/destructor.
10317 Returns the actual return type of the function; that may be different
10318 than TYPE if an error occurs, or for certain special functions. */
10319
10320 static tree
10321 check_special_function_return_type (special_function_kind sfk,
10322 tree type,
10323 tree optype,
10324 int type_quals,
10325 const location_t* locations)
10326 {
10327 switch (sfk)
10328 {
10329 case sfk_constructor:
10330 if (type)
10331 error_at (smallest_type_location (type_quals, locations),
10332 "return type specification for constructor invalid");
10333 else if (type_quals != TYPE_UNQUALIFIED)
10334 error_at (smallest_type_quals_location (type_quals, locations),
10335 "qualifiers are not allowed on constructor declaration");
10336
10337 if (targetm.cxx.cdtor_returns_this ())
10338 type = build_pointer_type (optype);
10339 else
10340 type = void_type_node;
10341 break;
10342
10343 case sfk_destructor:
10344 if (type)
10345 error_at (smallest_type_location (type_quals, locations),
10346 "return type specification for destructor invalid");
10347 else if (type_quals != TYPE_UNQUALIFIED)
10348 error_at (smallest_type_quals_location (type_quals, locations),
10349 "qualifiers are not allowed on destructor declaration");
10350
10351 /* We can't use the proper return type here because we run into
10352 problems with ambiguous bases and covariant returns. */
10353 if (targetm.cxx.cdtor_returns_this ())
10354 type = build_pointer_type (void_type_node);
10355 else
10356 type = void_type_node;
10357 break;
10358
10359 case sfk_conversion:
10360 if (type)
10361 error_at (smallest_type_location (type_quals, locations),
10362 "return type specified for %<operator %T%>", optype);
10363 else if (type_quals != TYPE_UNQUALIFIED)
10364 error_at (smallest_type_quals_location (type_quals, locations),
10365 "qualifiers are not allowed on declaration of "
10366 "%<operator %T%>", optype);
10367
10368 type = optype;
10369 break;
10370
10371 case sfk_deduction_guide:
10372 if (type)
10373 error_at (smallest_type_location (type_quals, locations),
10374 "return type specified for deduction guide");
10375 else if (type_quals != TYPE_UNQUALIFIED)
10376 error_at (smallest_type_quals_location (type_quals, locations),
10377 "qualifiers are not allowed on declaration of "
10378 "deduction guide");
10379 if (TREE_CODE (optype) == TEMPLATE_TEMPLATE_PARM)
10380 {
10381 error ("template template parameter %qT in declaration of "
10382 "deduction guide", optype);
10383 type = error_mark_node;
10384 }
10385 else
10386 type = make_template_placeholder (CLASSTYPE_TI_TEMPLATE (optype));
10387 for (int i = 0; i < ds_last; ++i)
10388 if (i != ds_explicit && locations[i])
10389 error_at (locations[i],
10390 "%<decl-specifier%> in declaration of deduction guide");
10391 break;
10392
10393 default:
10394 gcc_unreachable ();
10395 }
10396
10397 return type;
10398 }
10399
10400 /* A variable or data member (whose unqualified name is IDENTIFIER)
10401 has been declared with the indicated TYPE. If the TYPE is not
10402 acceptable, issue an error message and return a type to use for
10403 error-recovery purposes. */
10404
10405 tree
10406 check_var_type (tree identifier, tree type, location_t loc)
10407 {
10408 if (VOID_TYPE_P (type))
10409 {
10410 if (!identifier)
10411 error_at (loc, "unnamed variable or field declared void");
10412 else if (identifier_p (identifier))
10413 {
10414 gcc_assert (!IDENTIFIER_ANY_OP_P (identifier));
10415 error_at (loc, "variable or field %qE declared void",
10416 identifier);
10417 }
10418 else
10419 error_at (loc, "variable or field declared void");
10420 type = error_mark_node;
10421 }
10422
10423 return type;
10424 }
10425
10426 /* Handle declaring DECL as an inline variable. */
10427
10428 static void
10429 mark_inline_variable (tree decl, location_t loc)
10430 {
10431 bool inlinep = true;
10432 if (! toplevel_bindings_p ())
10433 {
10434 error_at (loc, "%<inline%> specifier invalid for variable "
10435 "%qD declared at block scope", decl);
10436 inlinep = false;
10437 }
10438 else if (cxx_dialect < cxx17)
10439 pedwarn (loc, 0, "inline variables are only available "
10440 "with %<-std=c++17%> or %<-std=gnu++17%>");
10441 if (inlinep)
10442 {
10443 retrofit_lang_decl (decl);
10444 SET_DECL_VAR_DECLARED_INLINE_P (decl);
10445 }
10446 }
10447
10448
10449 /* Assign a typedef-given name to a class or enumeration type declared
10450 as anonymous at first. This was split out of grokdeclarator
10451 because it is also used in libcc1. */
10452
10453 void
10454 name_unnamed_type (tree type, tree decl)
10455 {
10456 gcc_assert (TYPE_UNNAMED_P (type));
10457
10458 /* Replace the anonymous name with the real name everywhere. */
10459 for (tree t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
10460 if (IDENTIFIER_ANON_P (TYPE_IDENTIFIER (t)))
10461 /* We do not rename the debug info representing the unnamed
10462 tagged type because the standard says in [dcl.typedef] that
10463 the naming applies only for linkage purposes. */
10464 /*debug_hooks->set_name (t, decl);*/
10465 TYPE_NAME (t) = decl;
10466
10467 if (TYPE_LANG_SPECIFIC (type))
10468 TYPE_WAS_UNNAMED (type) = 1;
10469
10470 /* If this is a typedef within a template class, the nested
10471 type is a (non-primary) template. The name for the
10472 template needs updating as well. */
10473 if (TYPE_LANG_SPECIFIC (type) && CLASSTYPE_TEMPLATE_INFO (type))
10474 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type))
10475 = TYPE_IDENTIFIER (type);
10476
10477 /* Adjust linkage now that we aren't unnamed anymore. */
10478 reset_type_linkage (type);
10479
10480 /* FIXME remangle member functions; member functions of a
10481 type with external linkage have external linkage. */
10482
10483 /* Check that our job is done, and that it would fail if we
10484 attempted to do it again. */
10485 gcc_assert (!TYPE_UNNAMED_P (type));
10486 }
10487
10488 /* Given declspecs and a declarator (abstract or otherwise), determine
10489 the name and type of the object declared and construct a DECL node
10490 for it.
10491
10492 DECLSPECS points to the representation of declaration-specifier
10493 sequence that precedes declarator.
10494
10495 DECL_CONTEXT says which syntactic context this declaration is in:
10496 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
10497 FUNCDEF for a function definition. Like NORMAL but a few different
10498 error messages in each case. Return value may be zero meaning
10499 this definition is too screwy to try to parse.
10500 MEMFUNCDEF for a function definition. Like FUNCDEF but prepares to
10501 handle member functions (which have FIELD context).
10502 Return value may be zero meaning this definition is too screwy to
10503 try to parse.
10504 PARM for a parameter declaration (either within a function prototype
10505 or before a function body). Make a PARM_DECL, or return void_type_node.
10506 TPARM for a template parameter declaration.
10507 CATCHPARM for a parameter declaration before a catch clause.
10508 TYPENAME if for a typename (in a cast or sizeof).
10509 Don't make a DECL node; just return the ..._TYPE node.
10510 FIELD for a struct or union field; make a FIELD_DECL.
10511 BITFIELD for a field with specified width.
10512
10513 INITIALIZED is as for start_decl.
10514
10515 ATTRLIST is a pointer to the list of attributes, which may be NULL
10516 if there are none; *ATTRLIST may be modified if attributes from inside
10517 the declarator should be applied to the declaration.
10518
10519 When this function is called, scoping variables (such as
10520 CURRENT_CLASS_TYPE) should reflect the scope in which the
10521 declaration occurs, not the scope in which the new declaration will
10522 be placed. For example, on:
10523
10524 void S::f() { ... }
10525
10526 when grokdeclarator is called for `S::f', the CURRENT_CLASS_TYPE
10527 should not be `S'.
10528
10529 Returns a DECL (if a declarator is present), a TYPE (if there is no
10530 declarator, in cases like "struct S;"), or the ERROR_MARK_NODE if an
10531 error occurs. */
10532
10533 tree
10534 grokdeclarator (const cp_declarator *declarator,
10535 cp_decl_specifier_seq *declspecs,
10536 enum decl_context decl_context,
10537 int initialized,
10538 tree* attrlist)
10539 {
10540 tree type = NULL_TREE;
10541 int longlong = 0;
10542 int explicit_intN = 0;
10543 int int_n_alt = 0;
10544 int virtualp, explicitp, friendp, inlinep, staticp;
10545 int explicit_int = 0;
10546 int explicit_char = 0;
10547 int defaulted_int = 0;
10548
10549 tree typedef_decl = NULL_TREE;
10550 const char *name = NULL;
10551 tree typedef_type = NULL_TREE;
10552 /* True if this declarator is a function definition. */
10553 bool funcdef_flag = false;
10554 cp_declarator_kind innermost_code = cdk_error;
10555 int bitfield = 0;
10556 #if 0
10557 /* See the code below that used this. */
10558 tree decl_attr = NULL_TREE;
10559 #endif
10560
10561 /* Keep track of what sort of function is being processed
10562 so that we can warn about default return values, or explicit
10563 return values which do not match prescribed defaults. */
10564 special_function_kind sfk = sfk_none;
10565
10566 tree dname = NULL_TREE;
10567 tree ctor_return_type = NULL_TREE;
10568 enum overload_flags flags = NO_SPECIAL;
10569 /* cv-qualifiers that apply to the declarator, for a declaration of
10570 a member function. */
10571 cp_cv_quals memfn_quals = TYPE_UNQUALIFIED;
10572 /* virt-specifiers that apply to the declarator, for a declaration of
10573 a member function. */
10574 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
10575 /* ref-qualifier that applies to the declarator, for a declaration of
10576 a member function. */
10577 cp_ref_qualifier rqual = REF_QUAL_NONE;
10578 /* cv-qualifiers that apply to the type specified by the DECLSPECS. */
10579 int type_quals = get_type_quals (declspecs);
10580 tree raises = NULL_TREE;
10581 int template_count = 0;
10582 tree returned_attrs = NULL_TREE;
10583 tree parms = NULL_TREE;
10584 const cp_declarator *id_declarator;
10585 /* The unqualified name of the declarator; either an
10586 IDENTIFIER_NODE, BIT_NOT_EXPR, or TEMPLATE_ID_EXPR. */
10587 tree unqualified_id;
10588 /* The class type, if any, in which this entity is located,
10589 or NULL_TREE if none. Note that this value may be different from
10590 the current class type; for example if an attempt is made to declare
10591 "A::f" inside "B", this value will be "A". */
10592 tree ctype = current_class_type;
10593 /* The NAMESPACE_DECL for the namespace in which this entity is
10594 located. If an unqualified name is used to declare the entity,
10595 this value will be NULL_TREE, even if the entity is located at
10596 namespace scope. */
10597 tree in_namespace = NULL_TREE;
10598 cp_storage_class storage_class;
10599 bool unsigned_p, signed_p, short_p, long_p, thread_p;
10600 bool type_was_error_mark_node = false;
10601 bool parameter_pack_p = declarator ? declarator->parameter_pack_p : false;
10602 bool template_type_arg = false;
10603 bool template_parm_flag = false;
10604 bool typedef_p = decl_spec_seq_has_spec_p (declspecs, ds_typedef);
10605 bool constexpr_p = decl_spec_seq_has_spec_p (declspecs, ds_constexpr);
10606 bool constinit_p = decl_spec_seq_has_spec_p (declspecs, ds_constinit);
10607 bool late_return_type_p = false;
10608 bool array_parameter_p = false;
10609 tree reqs = NULL_TREE;
10610
10611 signed_p = decl_spec_seq_has_spec_p (declspecs, ds_signed);
10612 unsigned_p = decl_spec_seq_has_spec_p (declspecs, ds_unsigned);
10613 short_p = decl_spec_seq_has_spec_p (declspecs, ds_short);
10614 long_p = decl_spec_seq_has_spec_p (declspecs, ds_long);
10615 longlong = decl_spec_seq_has_spec_p (declspecs, ds_long_long);
10616 explicit_intN = declspecs->explicit_intN_p;
10617 int_n_alt = declspecs->int_n_alt;
10618 thread_p = decl_spec_seq_has_spec_p (declspecs, ds_thread);
10619
10620 // Was concept_p specified? Note that ds_concept
10621 // implies ds_constexpr!
10622 bool concept_p = decl_spec_seq_has_spec_p (declspecs, ds_concept);
10623 if (concept_p)
10624 constexpr_p = true;
10625
10626 if (decl_context == FUNCDEF)
10627 funcdef_flag = true, decl_context = NORMAL;
10628 else if (decl_context == MEMFUNCDEF)
10629 funcdef_flag = true, decl_context = FIELD;
10630 else if (decl_context == BITFIELD)
10631 bitfield = 1, decl_context = FIELD;
10632 else if (decl_context == TEMPLATE_TYPE_ARG)
10633 template_type_arg = true, decl_context = TYPENAME;
10634 else if (decl_context == TPARM)
10635 template_parm_flag = true, decl_context = PARM;
10636
10637 if (initialized > 1)
10638 funcdef_flag = true;
10639
10640 location_t typespec_loc = smallest_type_location (type_quals,
10641 declspecs->locations);
10642 if (typespec_loc == UNKNOWN_LOCATION)
10643 typespec_loc = input_location;
10644
10645 location_t id_loc = declarator ? declarator->id_loc : input_location;
10646 if (id_loc == UNKNOWN_LOCATION)
10647 id_loc = input_location;
10648
10649 /* Look inside a declarator for the name being declared
10650 and get it as a string, for an error message. */
10651 for (id_declarator = declarator;
10652 id_declarator;
10653 id_declarator = id_declarator->declarator)
10654 {
10655 if (id_declarator->kind != cdk_id)
10656 innermost_code = id_declarator->kind;
10657
10658 switch (id_declarator->kind)
10659 {
10660 case cdk_function:
10661 if (id_declarator->declarator
10662 && id_declarator->declarator->kind == cdk_id)
10663 {
10664 sfk = id_declarator->declarator->u.id.sfk;
10665 if (sfk == sfk_destructor)
10666 flags = DTOR_FLAG;
10667 }
10668 break;
10669
10670 case cdk_id:
10671 {
10672 tree qualifying_scope = id_declarator->u.id.qualifying_scope;
10673 tree decl = id_declarator->u.id.unqualified_name;
10674 if (!decl)
10675 break;
10676 if (qualifying_scope)
10677 {
10678 if (check_for_bare_parameter_packs (qualifying_scope,
10679 id_declarator->id_loc))
10680 return error_mark_node;
10681 if (at_function_scope_p ())
10682 {
10683 /* [dcl.meaning]
10684
10685 A declarator-id shall not be qualified except
10686 for ...
10687
10688 None of the cases are permitted in block
10689 scope. */
10690 if (qualifying_scope == global_namespace)
10691 error ("invalid use of qualified-name %<::%D%>",
10692 decl);
10693 else if (TYPE_P (qualifying_scope))
10694 error ("invalid use of qualified-name %<%T::%D%>",
10695 qualifying_scope, decl);
10696 else
10697 error ("invalid use of qualified-name %<%D::%D%>",
10698 qualifying_scope, decl);
10699 return error_mark_node;
10700 }
10701 else if (TYPE_P (qualifying_scope))
10702 {
10703 ctype = qualifying_scope;
10704 if (!MAYBE_CLASS_TYPE_P (ctype))
10705 {
10706 error_at (id_declarator->id_loc,
10707 "%q#T is not a class or namespace", ctype);
10708 ctype = NULL_TREE;
10709 }
10710 else if (innermost_code != cdk_function
10711 && current_class_type
10712 && !uniquely_derived_from_p (ctype,
10713 current_class_type))
10714 {
10715 error_at (id_declarator->id_loc,
10716 "invalid use of qualified-name %<%T::%D%>",
10717 qualifying_scope, decl);
10718 return error_mark_node;
10719 }
10720 }
10721 else if (TREE_CODE (qualifying_scope) == NAMESPACE_DECL)
10722 in_namespace = qualifying_scope;
10723 }
10724 switch (TREE_CODE (decl))
10725 {
10726 case BIT_NOT_EXPR:
10727 {
10728 if (innermost_code != cdk_function)
10729 {
10730 error_at (EXPR_LOCATION (decl),
10731 "declaration of %qE as non-function", decl);
10732 return error_mark_node;
10733 }
10734 else if (!qualifying_scope
10735 && !(current_class_type && at_class_scope_p ()))
10736 {
10737 error_at (EXPR_LOCATION (decl),
10738 "declaration of %qE as non-member", decl);
10739 return error_mark_node;
10740 }
10741
10742 tree type = TREE_OPERAND (decl, 0);
10743 if (TYPE_P (type))
10744 type = constructor_name (type);
10745 name = identifier_to_locale (IDENTIFIER_POINTER (type));
10746 dname = decl;
10747 }
10748 break;
10749
10750 case TEMPLATE_ID_EXPR:
10751 {
10752 tree fns = TREE_OPERAND (decl, 0);
10753
10754 dname = fns;
10755 if (!identifier_p (dname))
10756 dname = OVL_NAME (dname);
10757 }
10758 /* Fall through. */
10759
10760 case IDENTIFIER_NODE:
10761 if (identifier_p (decl))
10762 dname = decl;
10763
10764 if (IDENTIFIER_KEYWORD_P (dname))
10765 {
10766 error ("declarator-id missing; using reserved word %qD",
10767 dname);
10768 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
10769 }
10770 else if (!IDENTIFIER_CONV_OP_P (dname))
10771 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
10772 else
10773 {
10774 gcc_assert (flags == NO_SPECIAL);
10775 flags = TYPENAME_FLAG;
10776 sfk = sfk_conversion;
10777 tree glob = get_global_binding (dname);
10778 if (glob && TREE_CODE (glob) == TYPE_DECL)
10779 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
10780 else
10781 name = "<invalid operator>";
10782 }
10783 break;
10784
10785 default:
10786 gcc_unreachable ();
10787 }
10788 break;
10789 }
10790
10791 case cdk_array:
10792 case cdk_pointer:
10793 case cdk_reference:
10794 case cdk_ptrmem:
10795 break;
10796
10797 case cdk_decomp:
10798 name = "structured binding";
10799 break;
10800
10801 case cdk_error:
10802 return error_mark_node;
10803
10804 default:
10805 gcc_unreachable ();
10806 }
10807 if (id_declarator->kind == cdk_id)
10808 break;
10809 }
10810
10811 /* [dcl.fct.edf]
10812
10813 The declarator in a function-definition shall have the form
10814 D1 ( parameter-declaration-clause) ... */
10815 if (funcdef_flag && innermost_code != cdk_function)
10816 {
10817 error_at (id_loc, "function definition does not declare parameters");
10818 return error_mark_node;
10819 }
10820
10821 if (flags == TYPENAME_FLAG
10822 && innermost_code != cdk_function
10823 && ! (ctype && !declspecs->any_specifiers_p))
10824 {
10825 error_at (id_loc, "declaration of %qD as non-function", dname);
10826 return error_mark_node;
10827 }
10828
10829 if (dname && identifier_p (dname))
10830 {
10831 if (UDLIT_OPER_P (dname)
10832 && innermost_code != cdk_function)
10833 {
10834 error_at (id_loc, "declaration of %qD as non-function", dname);
10835 return error_mark_node;
10836 }
10837
10838 if (IDENTIFIER_ANY_OP_P (dname))
10839 {
10840 if (typedef_p)
10841 {
10842 error_at (id_loc, "declaration of %qD as %<typedef%>", dname);
10843 return error_mark_node;
10844 }
10845 else if (decl_context == PARM || decl_context == CATCHPARM)
10846 {
10847 error_at (id_loc, "declaration of %qD as parameter", dname);
10848 return error_mark_node;
10849 }
10850 }
10851 }
10852
10853 /* Anything declared one level down from the top level
10854 must be one of the parameters of a function
10855 (because the body is at least two levels down). */
10856
10857 /* This heuristic cannot be applied to C++ nodes! Fixed, however,
10858 by not allowing C++ class definitions to specify their parameters
10859 with xdecls (must be spec.d in the parmlist).
10860
10861 Since we now wait to push a class scope until we are sure that
10862 we are in a legitimate method context, we must set oldcname
10863 explicitly (since current_class_name is not yet alive).
10864
10865 We also want to avoid calling this a PARM if it is in a namespace. */
10866
10867 if (decl_context == NORMAL && !toplevel_bindings_p ())
10868 {
10869 cp_binding_level *b = current_binding_level;
10870 current_binding_level = b->level_chain;
10871 if (current_binding_level != 0 && toplevel_bindings_p ())
10872 decl_context = PARM;
10873 current_binding_level = b;
10874 }
10875
10876 if (name == NULL)
10877 name = decl_context == PARM ? "parameter" : "type name";
10878
10879 if (concept_p && typedef_p)
10880 {
10881 error_at (declspecs->locations[ds_concept],
10882 "%<concept%> cannot appear in a typedef declaration");
10883 return error_mark_node;
10884 }
10885
10886 if (constexpr_p && typedef_p)
10887 {
10888 error_at (declspecs->locations[ds_constexpr],
10889 "%<constexpr%> cannot appear in a typedef declaration");
10890 return error_mark_node;
10891 }
10892
10893 if (constinit_p && typedef_p)
10894 {
10895 error_at (declspecs->locations[ds_constinit],
10896 "%<constinit%> cannot appear in a typedef declaration");
10897 return error_mark_node;
10898 }
10899
10900 /* [dcl.spec]/2 "At most one of the constexpr, consteval, and constinit
10901 keywords shall appear in a decl-specifier-seq." */
10902 if (constinit_p && constexpr_p)
10903 {
10904 gcc_rich_location richloc (declspecs->locations[ds_constinit]);
10905 richloc.add_range (declspecs->locations[ds_constexpr]);
10906 error_at (&richloc,
10907 "can use at most one of the %<constinit%> and %<constexpr%> "
10908 "specifiers");
10909 return error_mark_node;
10910 }
10911
10912 /* If there were multiple types specified in the decl-specifier-seq,
10913 issue an error message. */
10914 if (declspecs->multiple_types_p)
10915 {
10916 error_at (typespec_loc,
10917 "two or more data types in declaration of %qs", name);
10918 return error_mark_node;
10919 }
10920
10921 if (declspecs->conflicting_specifiers_p)
10922 {
10923 error_at (min_location (declspecs->locations[ds_typedef],
10924 declspecs->locations[ds_storage_class]),
10925 "conflicting specifiers in declaration of %qs", name);
10926 return error_mark_node;
10927 }
10928
10929 /* Extract the basic type from the decl-specifier-seq. */
10930 type = declspecs->type;
10931 if (type == error_mark_node)
10932 {
10933 type = NULL_TREE;
10934 type_was_error_mark_node = true;
10935 }
10936 cp_warn_deprecated_use (type);
10937 if (type && TREE_CODE (type) == TYPE_DECL)
10938 {
10939 cp_warn_deprecated_use_scopes (CP_DECL_CONTEXT (type));
10940 typedef_decl = type;
10941 type = TREE_TYPE (typedef_decl);
10942 if (DECL_ARTIFICIAL (typedef_decl))
10943 cp_warn_deprecated_use (type);
10944 }
10945 /* No type at all: default to `int', and set DEFAULTED_INT
10946 because it was not a user-defined typedef. */
10947 if (type == NULL_TREE)
10948 {
10949 if (signed_p || unsigned_p || long_p || short_p)
10950 {
10951 /* These imply 'int'. */
10952 type = integer_type_node;
10953 defaulted_int = 1;
10954 }
10955 /* If we just have "complex", it is equivalent to "complex double". */
10956 else if (!longlong && !explicit_intN
10957 && decl_spec_seq_has_spec_p (declspecs, ds_complex))
10958 {
10959 type = double_type_node;
10960 pedwarn (declspecs->locations[ds_complex], OPT_Wpedantic,
10961 "ISO C++ does not support plain %<complex%> meaning "
10962 "%<double complex%>");
10963 }
10964 }
10965 /* Gather flags. */
10966 explicit_int = declspecs->explicit_int_p;
10967 explicit_char = declspecs->explicit_char_p;
10968
10969 #if 0
10970 /* See the code below that used this. */
10971 if (typedef_decl)
10972 decl_attr = DECL_ATTRIBUTES (typedef_decl);
10973 #endif
10974 typedef_type = type;
10975
10976 if (sfk == sfk_conversion || sfk == sfk_deduction_guide)
10977 ctor_return_type = TREE_TYPE (dname);
10978 else
10979 ctor_return_type = ctype;
10980
10981 if (sfk != sfk_none)
10982 {
10983 type = check_special_function_return_type (sfk, type,
10984 ctor_return_type,
10985 type_quals,
10986 declspecs->locations);
10987 type_quals = TYPE_UNQUALIFIED;
10988 }
10989 else if (type == NULL_TREE)
10990 {
10991 int is_main;
10992
10993 explicit_int = -1;
10994
10995 /* We handle `main' specially here, because 'main () { }' is so
10996 common. With no options, it is allowed. With -Wreturn-type,
10997 it is a warning. It is only an error with -pedantic-errors. */
10998 is_main = (funcdef_flag
10999 && dname && identifier_p (dname)
11000 && MAIN_NAME_P (dname)
11001 && ctype == NULL_TREE
11002 && in_namespace == NULL_TREE
11003 && current_namespace == global_namespace);
11004
11005 if (type_was_error_mark_node)
11006 /* We've already issued an error, don't complain more. */;
11007 else if (in_system_header_at (input_location) || flag_ms_extensions)
11008 /* Allow it, sigh. */;
11009 else if (! is_main)
11010 permerror (id_loc, "ISO C++ forbids declaration of %qs with no type",
11011 name);
11012 else if (pedantic)
11013 pedwarn (id_loc, OPT_Wpedantic,
11014 "ISO C++ forbids declaration of %qs with no type", name);
11015 else
11016 warning_at (id_loc, OPT_Wreturn_type,
11017 "ISO C++ forbids declaration of %qs with no type", name);
11018
11019 if (type_was_error_mark_node && template_parm_flag)
11020 /* FIXME we should be able to propagate the error_mark_node as is
11021 for other contexts too. */
11022 type = error_mark_node;
11023 else
11024 type = integer_type_node;
11025 }
11026
11027 ctype = NULL_TREE;
11028
11029 if (explicit_intN)
11030 {
11031 if (! int_n_enabled_p[declspecs->int_n_idx])
11032 {
11033 error_at (declspecs->locations[ds_type_spec],
11034 "%<__int%d%> is not supported by this target",
11035 int_n_data[declspecs->int_n_idx].bitsize);
11036 explicit_intN = false;
11037 }
11038 /* Don't pedwarn if the alternate "__intN__" form has been used instead
11039 of "__intN". */
11040 else if (!int_n_alt && pedantic && ! in_system_header_at (input_location))
11041 pedwarn (declspecs->locations[ds_type_spec], OPT_Wpedantic,
11042 "ISO C++ does not support %<__int%d%> for %qs",
11043 int_n_data[declspecs->int_n_idx].bitsize, name);
11044 }
11045
11046 /* Now process the modifiers that were specified
11047 and check for invalid combinations. */
11048
11049 /* Long double is a special combination. */
11050 if (long_p && !longlong && TYPE_MAIN_VARIANT (type) == double_type_node)
11051 {
11052 long_p = false;
11053 type = cp_build_qualified_type (long_double_type_node,
11054 cp_type_quals (type));
11055 }
11056
11057 /* Check all other uses of type modifiers. */
11058
11059 if (unsigned_p || signed_p || long_p || short_p)
11060 {
11061 location_t loc;
11062 const char *key;
11063 if (unsigned_p)
11064 {
11065 key = "unsigned";
11066 loc = declspecs->locations[ds_unsigned];
11067 }
11068 else if (signed_p)
11069 {
11070 key = "signed";
11071 loc = declspecs->locations[ds_signed];
11072 }
11073 else if (longlong)
11074 {
11075 key = "long long";
11076 loc = declspecs->locations[ds_long_long];
11077 }
11078 else if (long_p)
11079 {
11080 key = "long";
11081 loc = declspecs->locations[ds_long];
11082 }
11083 else /* if (short_p) */
11084 {
11085 key = "short";
11086 loc = declspecs->locations[ds_short];
11087 }
11088
11089 int ok = 0;
11090
11091 if (signed_p && unsigned_p)
11092 {
11093 gcc_rich_location richloc (declspecs->locations[ds_signed]);
11094 richloc.add_range (declspecs->locations[ds_unsigned]);
11095 error_at (&richloc,
11096 "%<signed%> and %<unsigned%> specified together");
11097 }
11098 else if (long_p && short_p)
11099 {
11100 gcc_rich_location richloc (declspecs->locations[ds_long]);
11101 richloc.add_range (declspecs->locations[ds_short]);
11102 error_at (&richloc, "%<long%> and %<short%> specified together");
11103 }
11104 else if (TREE_CODE (type) != INTEGER_TYPE
11105 || type == char8_type_node
11106 || type == char16_type_node
11107 || type == char32_type_node
11108 || ((long_p || short_p)
11109 && (explicit_char || explicit_intN)))
11110 error_at (loc, "%qs specified with %qT", key, type);
11111 else if (!explicit_int && !defaulted_int
11112 && !explicit_char && !explicit_intN)
11113 {
11114 if (typedef_decl)
11115 {
11116 pedwarn (loc, OPT_Wpedantic, "%qs specified with %qT",
11117 key, type);
11118 ok = !flag_pedantic_errors;
11119 }
11120 else if (declspecs->decltype_p)
11121 error_at (loc, "%qs specified with %<decltype%>", key);
11122 else
11123 error_at (loc, "%qs specified with %<typeof%>", key);
11124 }
11125 else
11126 ok = 1;
11127
11128 /* Discard the type modifiers if they are invalid. */
11129 if (! ok)
11130 {
11131 unsigned_p = false;
11132 signed_p = false;
11133 long_p = false;
11134 short_p = false;
11135 longlong = 0;
11136 }
11137 }
11138
11139 /* Decide whether an integer type is signed or not.
11140 Optionally treat bitfields as signed by default. */
11141 if (unsigned_p
11142 /* [class.bit]
11143
11144 It is implementation-defined whether a plain (neither
11145 explicitly signed or unsigned) char, short, int, or long
11146 bit-field is signed or unsigned.
11147
11148 Naturally, we extend this to long long as well. Note that
11149 this does not include wchar_t. */
11150 || (bitfield && !flag_signed_bitfields
11151 && !signed_p
11152 /* A typedef for plain `int' without `signed' can be
11153 controlled just like plain `int', but a typedef for
11154 `signed int' cannot be so controlled. */
11155 && !(typedef_decl
11156 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl))
11157 && TREE_CODE (type) == INTEGER_TYPE
11158 && !same_type_p (TYPE_MAIN_VARIANT (type), wchar_type_node)))
11159 {
11160 if (explicit_intN)
11161 type = int_n_trees[declspecs->int_n_idx].unsigned_type;
11162 else if (longlong)
11163 type = long_long_unsigned_type_node;
11164 else if (long_p)
11165 type = long_unsigned_type_node;
11166 else if (short_p)
11167 type = short_unsigned_type_node;
11168 else if (type == char_type_node)
11169 type = unsigned_char_type_node;
11170 else if (typedef_decl)
11171 type = unsigned_type_for (type);
11172 else
11173 type = unsigned_type_node;
11174 }
11175 else if (signed_p && type == char_type_node)
11176 type = signed_char_type_node;
11177 else if (explicit_intN)
11178 type = int_n_trees[declspecs->int_n_idx].signed_type;
11179 else if (longlong)
11180 type = long_long_integer_type_node;
11181 else if (long_p)
11182 type = long_integer_type_node;
11183 else if (short_p)
11184 type = short_integer_type_node;
11185
11186 if (decl_spec_seq_has_spec_p (declspecs, ds_complex))
11187 {
11188 if (TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
11189 error_at (declspecs->locations[ds_complex],
11190 "complex invalid for %qs", name);
11191 /* If a modifier is specified, the resulting complex is the complex
11192 form of TYPE. E.g, "complex short" is "complex short int". */
11193 else if (type == integer_type_node)
11194 type = complex_integer_type_node;
11195 else if (type == float_type_node)
11196 type = complex_float_type_node;
11197 else if (type == double_type_node)
11198 type = complex_double_type_node;
11199 else if (type == long_double_type_node)
11200 type = complex_long_double_type_node;
11201 else
11202 type = build_complex_type (type);
11203 }
11204
11205 /* If we're using the injected-class-name to form a compound type or a
11206 declaration, replace it with the underlying class so we don't get
11207 redundant typedefs in the debug output. But if we are returning the
11208 type unchanged, leave it alone so that it's available to
11209 maybe_get_template_decl_from_type_decl. */
11210 if (CLASS_TYPE_P (type)
11211 && DECL_SELF_REFERENCE_P (TYPE_NAME (type))
11212 && type == TREE_TYPE (TYPE_NAME (type))
11213 && (declarator || type_quals))
11214 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
11215
11216 type_quals |= cp_type_quals (type);
11217 type = cp_build_qualified_type_real
11218 (type, type_quals, ((((typedef_decl && !DECL_ARTIFICIAL (typedef_decl))
11219 || declspecs->decltype_p)
11220 ? tf_ignore_bad_quals : 0) | tf_warning_or_error));
11221 /* We might have ignored or rejected some of the qualifiers. */
11222 type_quals = cp_type_quals (type);
11223
11224 if (cxx_dialect >= cxx17 && type && is_auto (type)
11225 && innermost_code != cdk_function
11226 && id_declarator && declarator != id_declarator)
11227 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (type))
11228 {
11229 error_at (typespec_loc, "template placeholder type %qT must be followed "
11230 "by a simple declarator-id", type);
11231 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here", tmpl);
11232 type = error_mark_node;
11233 }
11234
11235 staticp = 0;
11236 inlinep = decl_spec_seq_has_spec_p (declspecs, ds_inline);
11237 virtualp = decl_spec_seq_has_spec_p (declspecs, ds_virtual);
11238 explicitp = decl_spec_seq_has_spec_p (declspecs, ds_explicit);
11239
11240 storage_class = declspecs->storage_class;
11241 if (storage_class == sc_static)
11242 staticp = 1 + (decl_context == FIELD);
11243
11244 if (virtualp)
11245 {
11246 if (staticp == 2)
11247 {
11248 gcc_rich_location richloc (declspecs->locations[ds_virtual]);
11249 richloc.add_range (declspecs->locations[ds_storage_class]);
11250 error_at (&richloc, "member %qD cannot be declared both %<virtual%> "
11251 "and %<static%>", dname);
11252 storage_class = sc_none;
11253 staticp = 0;
11254 }
11255 if (constexpr_p && cxx_dialect < cxx2a)
11256 {
11257 gcc_rich_location richloc (declspecs->locations[ds_virtual]);
11258 richloc.add_range (declspecs->locations[ds_constexpr]);
11259 pedwarn (&richloc, OPT_Wpedantic, "member %qD can be declared both "
11260 "%<virtual%> and %<constexpr%> only in %<-std=c++2a%> or "
11261 "%<-std=gnu++2a%>", dname);
11262 }
11263 }
11264 friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
11265
11266 /* Issue errors about use of storage classes for parameters. */
11267 if (decl_context == PARM)
11268 {
11269 if (typedef_p)
11270 {
11271 error_at (declspecs->locations[ds_typedef],
11272 "typedef declaration invalid in parameter declaration");
11273 return error_mark_node;
11274 }
11275 else if (template_parm_flag && storage_class != sc_none)
11276 {
11277 error_at (min_location (declspecs->locations[ds_thread],
11278 declspecs->locations[ds_storage_class]),
11279 "storage class specified for template parameter %qs",
11280 name);
11281 return error_mark_node;
11282 }
11283 else if (storage_class == sc_static
11284 || storage_class == sc_extern
11285 || thread_p)
11286 {
11287 error_at (min_location (declspecs->locations[ds_thread],
11288 declspecs->locations[ds_storage_class]),
11289 "storage class specified for parameter %qs", name);
11290 return error_mark_node;
11291 }
11292
11293 /* Function parameters cannot be concept. */
11294 if (concept_p)
11295 error_at (declspecs->locations[ds_concept],
11296 "a parameter cannot be declared %<concept%>");
11297 /* Function parameters cannot be constexpr. If we saw one, moan
11298 and pretend it wasn't there. */
11299 else if (constexpr_p)
11300 {
11301 error_at (declspecs->locations[ds_constexpr],
11302 "a parameter cannot be declared %<constexpr%>");
11303 constexpr_p = 0;
11304 }
11305 else if (constinit_p)
11306 {
11307 error_at (declspecs->locations[ds_constinit],
11308 "a parameter cannot be declared %<constinit%>");
11309 constexpr_p = 0;
11310 }
11311 }
11312
11313 /* Give error if `virtual' is used outside of class declaration. */
11314 if (virtualp
11315 && (current_class_name == NULL_TREE || decl_context != FIELD))
11316 {
11317 error_at (declspecs->locations[ds_virtual],
11318 "%<virtual%> outside class declaration");
11319 virtualp = 0;
11320 }
11321
11322 if (innermost_code == cdk_decomp)
11323 {
11324 location_t loc = (declarator->kind == cdk_reference
11325 ? declarator->declarator->id_loc : declarator->id_loc);
11326 if (inlinep)
11327 error_at (declspecs->locations[ds_inline],
11328 "structured binding declaration cannot be %qs", "inline");
11329 if (typedef_p)
11330 error_at (declspecs->locations[ds_typedef],
11331 "structured binding declaration cannot be %qs", "typedef");
11332 if (constexpr_p)
11333 error_at (declspecs->locations[ds_constexpr], "structured "
11334 "binding declaration cannot be %qs", "constexpr");
11335 if (thread_p && cxx_dialect < cxx2a)
11336 pedwarn (declspecs->locations[ds_thread], 0,
11337 "structured binding declaration can be %qs only in "
11338 "%<-std=c++2a%> or %<-std=gnu++2a%>",
11339 declspecs->gnu_thread_keyword_p
11340 ? "__thread" : "thread_local");
11341 if (concept_p)
11342 error_at (declspecs->locations[ds_concept],
11343 "structured binding declaration cannot be %qs", "concept");
11344 /* [dcl.struct.bind] "A cv that includes volatile is deprecated." */
11345 if (type_quals & TYPE_QUAL_VOLATILE)
11346 warning_at (declspecs->locations[ds_volatile], OPT_Wvolatile,
11347 "%<volatile%>-qualified structured binding is deprecated");
11348 switch (storage_class)
11349 {
11350 case sc_none:
11351 break;
11352 case sc_register:
11353 error_at (loc, "structured binding declaration cannot be %qs",
11354 "register");
11355 break;
11356 case sc_static:
11357 if (cxx_dialect < cxx2a)
11358 pedwarn (loc, 0,
11359 "structured binding declaration can be %qs only in "
11360 "%<-std=c++2a%> or %<-std=gnu++2a%>", "static");
11361 break;
11362 case sc_extern:
11363 error_at (loc, "structured binding declaration cannot be %qs",
11364 "extern");
11365 break;
11366 case sc_mutable:
11367 error_at (loc, "structured binding declaration cannot be %qs",
11368 "mutable");
11369 break;
11370 case sc_auto:
11371 error_at (loc, "structured binding declaration cannot be "
11372 "C++98 %<auto%>");
11373 break;
11374 default:
11375 gcc_unreachable ();
11376 }
11377 if (TREE_CODE (type) != TEMPLATE_TYPE_PARM
11378 || TYPE_IDENTIFIER (type) != auto_identifier)
11379 {
11380 if (type != error_mark_node)
11381 {
11382 error_at (loc, "structured binding declaration cannot have "
11383 "type %qT", type);
11384 inform (loc,
11385 "type must be cv-qualified %<auto%> or reference to "
11386 "cv-qualified %<auto%>");
11387 }
11388 type = build_qualified_type (make_auto (), type_quals);
11389 declspecs->type = type;
11390 }
11391 inlinep = 0;
11392 typedef_p = 0;
11393 constexpr_p = 0;
11394 concept_p = 0;
11395 if (storage_class != sc_static)
11396 {
11397 storage_class = sc_none;
11398 declspecs->storage_class = sc_none;
11399 }
11400 }
11401
11402 /* Static anonymous unions are dealt with here. */
11403 if (staticp && decl_context == TYPENAME
11404 && declspecs->type
11405 && ANON_AGGR_TYPE_P (declspecs->type))
11406 decl_context = FIELD;
11407
11408 /* Warn about storage classes that are invalid for certain
11409 kinds of declarations (parameters, typenames, etc.). */
11410 if (thread_p
11411 && ((storage_class
11412 && storage_class != sc_extern
11413 && storage_class != sc_static)
11414 || typedef_p))
11415 {
11416 location_t loc
11417 = min_location (declspecs->locations[ds_thread],
11418 declspecs->locations[ds_storage_class]);
11419 error_at (loc, "multiple storage classes in declaration of %qs", name);
11420 thread_p = false;
11421 }
11422 if (decl_context != NORMAL
11423 && ((storage_class != sc_none
11424 && storage_class != sc_mutable)
11425 || thread_p))
11426 {
11427 if ((decl_context == PARM || decl_context == CATCHPARM)
11428 && (storage_class == sc_register
11429 || storage_class == sc_auto))
11430 ;
11431 else if (typedef_p)
11432 ;
11433 else if (decl_context == FIELD
11434 /* C++ allows static class elements. */
11435 && storage_class == sc_static)
11436 /* C++ also allows inlines and signed and unsigned elements,
11437 but in those cases we don't come in here. */
11438 ;
11439 else
11440 {
11441 location_t loc
11442 = min_location (declspecs->locations[ds_thread],
11443 declspecs->locations[ds_storage_class]);
11444 if (decl_context == FIELD)
11445 error_at (loc, "storage class specified for %qs", name);
11446 else if (decl_context == PARM || decl_context == CATCHPARM)
11447 error_at (loc, "storage class specified for parameter %qs", name);
11448 else
11449 error_at (loc, "storage class specified for typename");
11450 if (storage_class == sc_register
11451 || storage_class == sc_auto
11452 || storage_class == sc_extern
11453 || thread_p)
11454 storage_class = sc_none;
11455 }
11456 }
11457 else if (storage_class == sc_extern && funcdef_flag
11458 && ! toplevel_bindings_p ())
11459 error ("nested function %qs declared %<extern%>", name);
11460 else if (toplevel_bindings_p ())
11461 {
11462 if (storage_class == sc_auto)
11463 error_at (declspecs->locations[ds_storage_class],
11464 "top-level declaration of %qs specifies %<auto%>", name);
11465 }
11466 else if (thread_p
11467 && storage_class != sc_extern
11468 && storage_class != sc_static)
11469 {
11470 if (declspecs->gnu_thread_keyword_p)
11471 pedwarn (declspecs->locations[ds_thread],
11472 0, "function-scope %qs implicitly auto and "
11473 "declared %<__thread%>", name);
11474
11475 /* When thread_local is applied to a variable of block scope the
11476 storage-class-specifier static is implied if it does not appear
11477 explicitly. */
11478 storage_class = declspecs->storage_class = sc_static;
11479 staticp = 1;
11480 }
11481
11482 if (storage_class && friendp)
11483 {
11484 error_at (min_location (declspecs->locations[ds_thread],
11485 declspecs->locations[ds_storage_class]),
11486 "storage class specifiers invalid in friend function "
11487 "declarations");
11488 storage_class = sc_none;
11489 staticp = 0;
11490 }
11491
11492 if (!id_declarator)
11493 unqualified_id = NULL_TREE;
11494 else
11495 {
11496 unqualified_id = id_declarator->u.id.unqualified_name;
11497 switch (TREE_CODE (unqualified_id))
11498 {
11499 case BIT_NOT_EXPR:
11500 unqualified_id = TREE_OPERAND (unqualified_id, 0);
11501 if (TYPE_P (unqualified_id))
11502 unqualified_id = constructor_name (unqualified_id);
11503 break;
11504
11505 case IDENTIFIER_NODE:
11506 case TEMPLATE_ID_EXPR:
11507 break;
11508
11509 default:
11510 gcc_unreachable ();
11511 }
11512 }
11513
11514 if (declspecs->std_attributes)
11515 {
11516 location_t attr_loc = declspecs->locations[ds_std_attribute];
11517 if (warning_at (attr_loc, OPT_Wattributes, "attribute ignored"))
11518 inform (attr_loc, "an attribute that appertains to a type-specifier "
11519 "is ignored");
11520 }
11521
11522 /* Determine the type of the entity declared by recurring on the
11523 declarator. */
11524 for (; declarator; declarator = declarator->declarator)
11525 {
11526 const cp_declarator *inner_declarator;
11527 tree attrs;
11528
11529 if (type == error_mark_node)
11530 return error_mark_node;
11531
11532 attrs = declarator->attributes;
11533 if (attrs)
11534 {
11535 int attr_flags;
11536
11537 attr_flags = 0;
11538 if (declarator == NULL || declarator->kind == cdk_id)
11539 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
11540 if (declarator->kind == cdk_function)
11541 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
11542 if (declarator->kind == cdk_array)
11543 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
11544 returned_attrs = decl_attributes (&type,
11545 chainon (returned_attrs, attrs),
11546 attr_flags);
11547 }
11548
11549 inner_declarator = declarator->declarator;
11550
11551 /* We don't want to warn in parameter context because we don't
11552 yet know if the parse will succeed, and this might turn out
11553 to be a constructor call. */
11554 if (decl_context != PARM
11555 && decl_context != TYPENAME
11556 && !typedef_p
11557 && declarator->parenthesized != UNKNOWN_LOCATION
11558 /* If the type is class-like and the inner name used a
11559 global namespace qualifier, we need the parens.
11560 Unfortunately all we can tell is whether a qualified name
11561 was used or not. */
11562 && !(inner_declarator
11563 && inner_declarator->kind == cdk_id
11564 && inner_declarator->u.id.qualifying_scope
11565 && (MAYBE_CLASS_TYPE_P (type)
11566 || TREE_CODE (type) == ENUMERAL_TYPE)))
11567 warning_at (declarator->parenthesized, OPT_Wparentheses,
11568 "unnecessary parentheses in declaration of %qs", name);
11569 if (declarator->kind == cdk_id || declarator->kind == cdk_decomp)
11570 break;
11571
11572 switch (declarator->kind)
11573 {
11574 case cdk_array:
11575 type = create_array_type_for_decl (dname, type,
11576 declarator->u.array.bounds,
11577 declarator->id_loc);
11578 if (!valid_array_size_p (dname
11579 ? declarator->id_loc : input_location,
11580 type, dname))
11581 type = error_mark_node;
11582
11583 if (declarator->std_attributes)
11584 /* [dcl.array]/1:
11585
11586 The optional attribute-specifier-seq appertains to the
11587 array. */
11588 returned_attrs = chainon (returned_attrs,
11589 declarator->std_attributes);
11590 break;
11591
11592 case cdk_function:
11593 {
11594 tree arg_types;
11595 int funcdecl_p;
11596
11597 /* Declaring a function type. */
11598
11599 {
11600 iloc_sentinel ils (declspecs->locations[ds_type_spec]);
11601 abstract_virtuals_error (ACU_RETURN, type);
11602 }
11603
11604 /* Pick up type qualifiers which should be applied to `this'. */
11605 memfn_quals = declarator->u.function.qualifiers;
11606 /* Pick up virt-specifiers. */
11607 virt_specifiers = declarator->u.function.virt_specifiers;
11608 /* And ref-qualifier, too */
11609 rqual = declarator->u.function.ref_qualifier;
11610 /* And tx-qualifier. */
11611 tree tx_qual = declarator->u.function.tx_qualifier;
11612 /* Pick up the exception specifications. */
11613 raises = declarator->u.function.exception_specification;
11614 /* If the exception-specification is ill-formed, let's pretend
11615 there wasn't one. */
11616 if (raises == error_mark_node)
11617 raises = NULL_TREE;
11618
11619 if (reqs)
11620 error_at (location_of (reqs), "requires-clause on return type");
11621 reqs = declarator->u.function.requires_clause;
11622
11623 /* Say it's a definition only for the CALL_EXPR
11624 closest to the identifier. */
11625 funcdecl_p = inner_declarator && inner_declarator->kind == cdk_id;
11626
11627 /* Handle a late-specified return type. */
11628 tree late_return_type = declarator->u.function.late_return_type;
11629 if (funcdecl_p
11630 /* This is the case e.g. for
11631 using T = auto () -> int. */
11632 || inner_declarator == NULL)
11633 {
11634 if (tree auto_node = type_uses_auto (type))
11635 {
11636 if (!late_return_type)
11637 {
11638 if (current_class_type
11639 && LAMBDA_TYPE_P (current_class_type))
11640 /* OK for C++11 lambdas. */;
11641 else if (cxx_dialect < cxx14)
11642 {
11643 error_at (typespec_loc, "%qs function uses "
11644 "%<auto%> type specifier without "
11645 "trailing return type", name);
11646 inform (typespec_loc,
11647 "deduced return type only available "
11648 "with %<-std=c++14%> or %<-std=gnu++14%>");
11649 }
11650 else if (virtualp)
11651 {
11652 error_at (typespec_loc, "virtual function "
11653 "cannot have deduced return type");
11654 virtualp = false;
11655 }
11656 }
11657 else if (!is_auto (type) && sfk != sfk_conversion)
11658 {
11659 error_at (typespec_loc, "%qs function with trailing "
11660 "return type has %qT as its type rather "
11661 "than plain %<auto%>", name, type);
11662 return error_mark_node;
11663 }
11664 else if (is_auto (type) && AUTO_IS_DECLTYPE (type))
11665 {
11666 if (funcdecl_p)
11667 error_at (typespec_loc,
11668 "%qs function with trailing return type "
11669 "has %<decltype(auto)%> as its type "
11670 "rather than plain %<auto%>", name);
11671 else
11672 error_at (typespec_loc,
11673 "invalid use of %<decltype(auto)%>");
11674 return error_mark_node;
11675 }
11676 tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node);
11677 if (!tmpl)
11678 if (tree late_auto = type_uses_auto (late_return_type))
11679 tmpl = CLASS_PLACEHOLDER_TEMPLATE (late_auto);
11680 if (tmpl && funcdecl_p)
11681 {
11682 if (!dguide_name_p (unqualified_id))
11683 {
11684 error_at (declarator->id_loc, "deduced class "
11685 "type %qD in function return type",
11686 DECL_NAME (tmpl));
11687 inform (DECL_SOURCE_LOCATION (tmpl),
11688 "%qD declared here", tmpl);
11689 return error_mark_node;
11690 }
11691 else if (!late_return_type)
11692 {
11693 error_at (declarator->id_loc, "deduction guide "
11694 "for %qT must have trailing return "
11695 "type", TREE_TYPE (tmpl));
11696 inform (DECL_SOURCE_LOCATION (tmpl),
11697 "%qD declared here", tmpl);
11698 return error_mark_node;
11699 }
11700 else if (CLASS_TYPE_P (late_return_type)
11701 && CLASSTYPE_TEMPLATE_INFO (late_return_type)
11702 && (CLASSTYPE_TI_TEMPLATE (late_return_type)
11703 == tmpl))
11704 /* OK */;
11705 else
11706 error ("trailing return type %qT of deduction guide "
11707 "is not a specialization of %qT",
11708 late_return_type, TREE_TYPE (tmpl));
11709 }
11710 }
11711 else if (late_return_type
11712 && sfk != sfk_conversion)
11713 {
11714 if (late_return_type == error_mark_node)
11715 return error_mark_node;
11716 if (cxx_dialect < cxx11)
11717 /* Not using maybe_warn_cpp0x because this should
11718 always be an error. */
11719 error_at (typespec_loc,
11720 "trailing return type only available "
11721 "with %<-std=c++11%> or %<-std=gnu++11%>");
11722 else
11723 error_at (typespec_loc, "%qs function with trailing "
11724 "return type not declared with %<auto%> "
11725 "type specifier", name);
11726 return error_mark_node;
11727 }
11728 }
11729 type = splice_late_return_type (type, late_return_type);
11730 if (type == error_mark_node)
11731 return error_mark_node;
11732
11733 if (late_return_type)
11734 {
11735 late_return_type_p = true;
11736 type_quals = cp_type_quals (type);
11737 }
11738
11739 if (type_quals != TYPE_UNQUALIFIED)
11740 {
11741 if (SCALAR_TYPE_P (type) || VOID_TYPE_P (type))
11742 warning_at (typespec_loc, OPT_Wignored_qualifiers, "type "
11743 "qualifiers ignored on function return type");
11744 /* [dcl.fct] "A volatile-qualified return type is
11745 deprecated." */
11746 if (type_quals & TYPE_QUAL_VOLATILE)
11747 warning_at (typespec_loc, OPT_Wvolatile,
11748 "%<volatile%>-qualified return type is "
11749 "deprecated");
11750
11751 /* We now know that the TYPE_QUALS don't apply to the
11752 decl, but to its return type. */
11753 type_quals = TYPE_UNQUALIFIED;
11754 }
11755
11756 /* Error about some types functions can't return. */
11757
11758 if (TREE_CODE (type) == FUNCTION_TYPE)
11759 {
11760 error_at (typespec_loc, "%qs declared as function returning "
11761 "a function", name);
11762 return error_mark_node;
11763 }
11764 if (TREE_CODE (type) == ARRAY_TYPE)
11765 {
11766 error_at (typespec_loc, "%qs declared as function returning "
11767 "an array", name);
11768 return error_mark_node;
11769 }
11770 if (constinit_p)
11771 {
11772 error_at (declspecs->locations[ds_constinit],
11773 "%<constinit%> on function return type is not "
11774 "allowed");
11775 return error_mark_node;
11776 }
11777 /* Only plain decltype(auto) is allowed. */
11778 if (tree a = type_uses_auto (type))
11779 {
11780 if (AUTO_IS_DECLTYPE (a) && a != type)
11781 {
11782 error_at (typespec_loc, "%qT as type rather than "
11783 "plain %<decltype(auto)%>", type);
11784 return error_mark_node;
11785 }
11786 }
11787
11788 if (ctype == NULL_TREE
11789 && decl_context == FIELD
11790 && funcdecl_p
11791 && friendp == 0)
11792 ctype = current_class_type;
11793
11794 if (ctype && (sfk == sfk_constructor
11795 || sfk == sfk_destructor))
11796 {
11797 /* We are within a class's scope. If our declarator name
11798 is the same as the class name, and we are defining
11799 a function, then it is a constructor/destructor, and
11800 therefore returns a void type. */
11801
11802 /* ISO C++ 12.4/2. A destructor may not be declared
11803 const or volatile. A destructor may not be static.
11804 A destructor may not be declared with ref-qualifier.
11805
11806 ISO C++ 12.1. A constructor may not be declared
11807 const or volatile. A constructor may not be
11808 virtual. A constructor may not be static.
11809 A constructor may not be declared with ref-qualifier. */
11810 if (staticp == 2)
11811 error_at (declspecs->locations[ds_storage_class],
11812 (flags == DTOR_FLAG)
11813 ? G_("destructor cannot be static member "
11814 "function")
11815 : G_("constructor cannot be static member "
11816 "function"));
11817 if (memfn_quals)
11818 {
11819 error ((flags == DTOR_FLAG)
11820 ? G_("destructors may not be cv-qualified")
11821 : G_("constructors may not be cv-qualified"));
11822 memfn_quals = TYPE_UNQUALIFIED;
11823 }
11824
11825 if (rqual)
11826 {
11827 maybe_warn_cpp0x (CPP0X_REF_QUALIFIER);
11828 error ((flags == DTOR_FLAG)
11829 ? G_("destructors may not be ref-qualified")
11830 : G_("constructors may not be ref-qualified"));
11831 rqual = REF_QUAL_NONE;
11832 }
11833
11834 if (decl_context == FIELD
11835 && !member_function_or_else (ctype,
11836 current_class_type,
11837 flags))
11838 return error_mark_node;
11839
11840 if (flags != DTOR_FLAG)
11841 {
11842 /* It's a constructor. */
11843 if (explicitp == 1)
11844 explicitp = 2;
11845 if (virtualp)
11846 {
11847 permerror (declspecs->locations[ds_virtual],
11848 "constructors cannot be declared %<virtual%>");
11849 virtualp = 0;
11850 }
11851 if (decl_context == FIELD
11852 && sfk != sfk_constructor)
11853 return error_mark_node;
11854 }
11855 if (decl_context == FIELD)
11856 staticp = 0;
11857 }
11858 else if (friendp)
11859 {
11860 if (virtualp)
11861 {
11862 /* Cannot be both friend and virtual. */
11863 gcc_rich_location richloc (declspecs->locations[ds_virtual]);
11864 richloc.add_range (declspecs->locations[ds_friend]);
11865 error_at (&richloc, "virtual functions cannot be friends");
11866 friendp = 0;
11867 }
11868 if (decl_context == NORMAL)
11869 error_at (declarator->id_loc,
11870 "friend declaration not in class definition");
11871 if (current_function_decl && funcdef_flag)
11872 {
11873 error_at (declarator->id_loc,
11874 "cannot define friend function %qs in a local "
11875 "class definition", name);
11876 friendp = 0;
11877 }
11878 /* [class.friend]/6: A function can be defined in a friend
11879 declaration if the function name is unqualified. */
11880 if (funcdef_flag && in_namespace)
11881 {
11882 if (in_namespace == global_namespace)
11883 error_at (declarator->id_loc,
11884 "friend function definition %qs cannot have "
11885 "a name qualified with %<::%>", name);
11886 else
11887 error_at (declarator->id_loc,
11888 "friend function definition %qs cannot have "
11889 "a name qualified with %<%D::%>", name,
11890 in_namespace);
11891 }
11892 }
11893 else if (ctype && sfk == sfk_conversion)
11894 {
11895 if (explicitp == 1)
11896 {
11897 maybe_warn_cpp0x (CPP0X_EXPLICIT_CONVERSION);
11898 explicitp = 2;
11899 }
11900 if (late_return_type_p)
11901 error ("a conversion function cannot have a trailing return type");
11902 }
11903 else if (sfk == sfk_deduction_guide)
11904 {
11905 if (explicitp == 1)
11906 explicitp = 2;
11907 }
11908
11909 tree pushed_scope = NULL_TREE;
11910 if (funcdecl_p
11911 && decl_context != FIELD
11912 && inner_declarator->u.id.qualifying_scope
11913 && CLASS_TYPE_P (inner_declarator->u.id.qualifying_scope))
11914 pushed_scope
11915 = push_scope (inner_declarator->u.id.qualifying_scope);
11916
11917 arg_types = grokparms (declarator->u.function.parameters, &parms);
11918
11919 if (pushed_scope)
11920 pop_scope (pushed_scope);
11921
11922 if (inner_declarator
11923 && inner_declarator->kind == cdk_id
11924 && inner_declarator->u.id.sfk == sfk_destructor
11925 && arg_types != void_list_node)
11926 {
11927 error_at (declarator->id_loc,
11928 "destructors may not have parameters");
11929 arg_types = void_list_node;
11930 parms = NULL_TREE;
11931 }
11932
11933 type = build_function_type (type, arg_types);
11934
11935 tree attrs = declarator->std_attributes;
11936 if (tx_qual)
11937 {
11938 tree att = build_tree_list (tx_qual, NULL_TREE);
11939 /* transaction_safe applies to the type, but
11940 transaction_safe_dynamic applies to the function. */
11941 if (is_attribute_p ("transaction_safe", tx_qual))
11942 attrs = chainon (attrs, att);
11943 else
11944 returned_attrs = chainon (returned_attrs, att);
11945 }
11946 if (attrs)
11947 /* [dcl.fct]/2:
11948
11949 The optional attribute-specifier-seq appertains to
11950 the function type. */
11951 decl_attributes (&type, attrs, 0);
11952
11953 if (raises)
11954 type = build_exception_variant (type, raises);
11955 }
11956 break;
11957
11958 case cdk_pointer:
11959 case cdk_reference:
11960 case cdk_ptrmem:
11961 /* Filter out pointers-to-references and references-to-references.
11962 We can get these if a TYPE_DECL is used. */
11963
11964 if (TYPE_REF_P (type))
11965 {
11966 if (declarator->kind != cdk_reference)
11967 {
11968 error ("cannot declare pointer to %q#T", type);
11969 type = TREE_TYPE (type);
11970 }
11971
11972 /* In C++0x, we allow reference to reference declarations
11973 that occur indirectly through typedefs [7.1.3/8 dcl.typedef]
11974 and template type arguments [14.3.1/4 temp.arg.type]. The
11975 check for direct reference to reference declarations, which
11976 are still forbidden, occurs below. Reasoning behind the change
11977 can be found in DR106, DR540, and the rvalue reference
11978 proposals. */
11979 else if (cxx_dialect == cxx98)
11980 {
11981 error ("cannot declare reference to %q#T", type);
11982 type = TREE_TYPE (type);
11983 }
11984 }
11985 else if (VOID_TYPE_P (type))
11986 {
11987 if (declarator->kind == cdk_reference)
11988 error ("cannot declare reference to %q#T", type);
11989 else if (declarator->kind == cdk_ptrmem)
11990 error ("cannot declare pointer to %q#T member", type);
11991 }
11992
11993 /* We now know that the TYPE_QUALS don't apply to the decl,
11994 but to the target of the pointer. */
11995 type_quals = TYPE_UNQUALIFIED;
11996
11997 /* This code used to handle METHOD_TYPE, but I don't think it's
11998 possible to get it here anymore. */
11999 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
12000 if (declarator->kind == cdk_ptrmem
12001 && TREE_CODE (type) == FUNCTION_TYPE)
12002 {
12003 memfn_quals |= type_memfn_quals (type);
12004 type = build_memfn_type (type,
12005 declarator->u.pointer.class_type,
12006 memfn_quals,
12007 rqual);
12008 if (type == error_mark_node)
12009 return error_mark_node;
12010
12011 rqual = REF_QUAL_NONE;
12012 memfn_quals = TYPE_UNQUALIFIED;
12013 }
12014
12015 if (TREE_CODE (type) == FUNCTION_TYPE
12016 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
12017 || type_memfn_rqual (type) != REF_QUAL_NONE))
12018 error (declarator->kind == cdk_reference
12019 ? G_("cannot declare reference to qualified function type %qT")
12020 : G_("cannot declare pointer to qualified function type %qT"),
12021 type);
12022
12023 /* When the pointed-to type involves components of variable size,
12024 care must be taken to ensure that the size evaluation code is
12025 emitted early enough to dominate all the possible later uses
12026 and late enough for the variables on which it depends to have
12027 been assigned.
12028
12029 This is expected to happen automatically when the pointed-to
12030 type has a name/declaration of it's own, but special attention
12031 is required if the type is anonymous.
12032
12033 We handle the NORMAL and FIELD contexts here by inserting a
12034 dummy statement that just evaluates the size at a safe point
12035 and ensures it is not deferred until e.g. within a deeper
12036 conditional context (c++/43555).
12037
12038 We expect nothing to be needed here for PARM or TYPENAME.
12039 Evaluating the size at this point for TYPENAME would
12040 actually be incorrect, as we might be in the middle of an
12041 expression with side effects on the pointed-to type size
12042 "arguments" prior to the pointer declaration point and the
12043 size evaluation could end up prior to the side effects. */
12044
12045 if (!TYPE_NAME (type)
12046 && (decl_context == NORMAL || decl_context == FIELD)
12047 && at_function_scope_p ()
12048 && variably_modified_type_p (type, NULL_TREE))
12049 {
12050 TYPE_NAME (type) = build_decl (UNKNOWN_LOCATION, TYPE_DECL,
12051 NULL_TREE, type);
12052 add_decl_expr (TYPE_NAME (type));
12053 }
12054
12055 if (declarator->kind == cdk_reference)
12056 {
12057 /* In C++0x, the type we are creating a reference to might be
12058 a typedef which is itself a reference type. In that case,
12059 we follow the reference collapsing rules in
12060 [7.1.3/8 dcl.typedef] to create the final reference type:
12061
12062 "If a typedef TD names a type that is a reference to a type
12063 T, an attempt to create the type 'lvalue reference to cv TD'
12064 creates the type 'lvalue reference to T,' while an attempt
12065 to create the type "rvalue reference to cv TD' creates the
12066 type TD."
12067 */
12068 if (VOID_TYPE_P (type))
12069 /* We already gave an error. */;
12070 else if (TYPE_REF_P (type))
12071 {
12072 if (declarator->u.reference.rvalue_ref)
12073 /* Leave type alone. */;
12074 else
12075 type = cp_build_reference_type (TREE_TYPE (type), false);
12076 }
12077 else
12078 type = cp_build_reference_type
12079 (type, declarator->u.reference.rvalue_ref);
12080
12081 /* In C++0x, we need this check for direct reference to
12082 reference declarations, which are forbidden by
12083 [8.3.2/5 dcl.ref]. Reference to reference declarations
12084 are only allowed indirectly through typedefs and template
12085 type arguments. Example:
12086
12087 void foo(int & &); // invalid ref-to-ref decl
12088
12089 typedef int & int_ref;
12090 void foo(int_ref &); // valid ref-to-ref decl
12091 */
12092 if (inner_declarator && inner_declarator->kind == cdk_reference)
12093 error ("cannot declare reference to %q#T, which is not "
12094 "a typedef or a template type argument", type);
12095 }
12096 else if (TREE_CODE (type) == METHOD_TYPE)
12097 type = build_ptrmemfunc_type (build_pointer_type (type));
12098 else if (declarator->kind == cdk_ptrmem)
12099 {
12100 gcc_assert (TREE_CODE (declarator->u.pointer.class_type)
12101 != NAMESPACE_DECL);
12102 if (declarator->u.pointer.class_type == error_mark_node)
12103 /* We will already have complained. */
12104 type = error_mark_node;
12105 else
12106 type = build_ptrmem_type (declarator->u.pointer.class_type,
12107 type);
12108 }
12109 else
12110 type = build_pointer_type (type);
12111
12112 /* Process a list of type modifier keywords (such as
12113 const or volatile) that were given inside the `*' or `&'. */
12114
12115 if (declarator->u.pointer.qualifiers)
12116 {
12117 type
12118 = cp_build_qualified_type (type,
12119 declarator->u.pointer.qualifiers);
12120 type_quals = cp_type_quals (type);
12121 }
12122
12123 /* Apply C++11 attributes to the pointer, and not to the
12124 type pointed to. This is unlike what is done for GNU
12125 attributes above. It is to comply with [dcl.ptr]/1:
12126
12127 [the optional attribute-specifier-seq (7.6.1) appertains
12128 to the pointer and not to the object pointed to]. */
12129 if (declarator->std_attributes)
12130 decl_attributes (&type, declarator->std_attributes,
12131 0);
12132
12133 ctype = NULL_TREE;
12134 break;
12135
12136 case cdk_error:
12137 break;
12138
12139 default:
12140 gcc_unreachable ();
12141 }
12142 }
12143
12144 id_loc = declarator ? declarator->id_loc : input_location;
12145
12146 /* A `constexpr' specifier used in an object declaration declares
12147 the object as `const'. */
12148 if (constexpr_p && innermost_code != cdk_function)
12149 {
12150 /* DR1688 says that a `constexpr' specifier in combination with
12151 `volatile' is valid. */
12152
12153 if (!TYPE_REF_P (type))
12154 {
12155 type_quals |= TYPE_QUAL_CONST;
12156 type = cp_build_qualified_type (type, type_quals);
12157 }
12158 }
12159
12160 if (unqualified_id && TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR
12161 && !FUNC_OR_METHOD_TYPE_P (type)
12162 && !variable_template_p (TREE_OPERAND (unqualified_id, 0)))
12163 {
12164 error ("template-id %qD used as a declarator",
12165 unqualified_id);
12166 unqualified_id = dname;
12167 }
12168
12169 /* If TYPE is a FUNCTION_TYPE, but the function name was explicitly
12170 qualified with a class-name, turn it into a METHOD_TYPE, unless
12171 we know that the function is static. We take advantage of this
12172 opportunity to do other processing that pertains to entities
12173 explicitly declared to be class members. Note that if DECLARATOR
12174 is non-NULL, we know it is a cdk_id declarator; otherwise, we
12175 would not have exited the loop above. */
12176 if (declarator
12177 && declarator->kind == cdk_id
12178 && declarator->u.id.qualifying_scope
12179 && MAYBE_CLASS_TYPE_P (declarator->u.id.qualifying_scope))
12180 {
12181 ctype = declarator->u.id.qualifying_scope;
12182 ctype = TYPE_MAIN_VARIANT (ctype);
12183 template_count = num_template_headers_for_class (ctype);
12184
12185 if (ctype == current_class_type)
12186 {
12187 if (friendp)
12188 {
12189 permerror (declspecs->locations[ds_friend],
12190 "member functions are implicitly "
12191 "friends of their class");
12192 friendp = 0;
12193 }
12194 else
12195 permerror (id_loc, "extra qualification %<%T::%> on member %qs",
12196 ctype, name);
12197 }
12198 else if (/* If the qualifying type is already complete, then we
12199 can skip the following checks. */
12200 !COMPLETE_TYPE_P (ctype)
12201 && (/* If the function is being defined, then
12202 qualifying type must certainly be complete. */
12203 funcdef_flag
12204 /* A friend declaration of "T::f" is OK, even if
12205 "T" is a template parameter. But, if this
12206 function is not a friend, the qualifying type
12207 must be a class. */
12208 || (!friendp && !CLASS_TYPE_P (ctype))
12209 /* For a declaration, the type need not be
12210 complete, if either it is dependent (since there
12211 is no meaningful definition of complete in that
12212 case) or the qualifying class is currently being
12213 defined. */
12214 || !(dependent_type_p (ctype)
12215 || currently_open_class (ctype)))
12216 /* Check that the qualifying type is complete. */
12217 && !complete_type_or_else (ctype, NULL_TREE))
12218 return error_mark_node;
12219 else if (TREE_CODE (type) == FUNCTION_TYPE)
12220 {
12221 if (current_class_type
12222 && (!friendp || funcdef_flag || initialized))
12223 {
12224 error_at (id_loc, funcdef_flag || initialized
12225 ? G_("cannot define member function %<%T::%s%> "
12226 "within %qT")
12227 : G_("cannot declare member function %<%T::%s%> "
12228 "within %qT"),
12229 ctype, name, current_class_type);
12230 return error_mark_node;
12231 }
12232 }
12233 else if (typedef_p && current_class_type)
12234 {
12235 error_at (id_loc, "cannot declare member %<%T::%s%> within %qT",
12236 ctype, name, current_class_type);
12237 return error_mark_node;
12238 }
12239 }
12240
12241 if (ctype == NULL_TREE && decl_context == FIELD && friendp == 0)
12242 ctype = current_class_type;
12243
12244 /* Now TYPE has the actual type. */
12245
12246 if (returned_attrs)
12247 {
12248 if (attrlist)
12249 *attrlist = chainon (returned_attrs, *attrlist);
12250 else
12251 attrlist = &returned_attrs;
12252 }
12253
12254 if (declarator
12255 && declarator->kind == cdk_id
12256 && declarator->std_attributes
12257 && attrlist != NULL)
12258 {
12259 /* [dcl.meaning]/1: The optional attribute-specifier-seq following
12260 a declarator-id appertains to the entity that is declared. */
12261 if (declarator->std_attributes != error_mark_node)
12262 *attrlist = chainon (*attrlist, declarator->std_attributes);
12263 else
12264 /* We should have already diagnosed the issue (c++/78344). */
12265 gcc_assert (seen_error ());
12266 }
12267
12268 /* Handle parameter packs. */
12269 if (parameter_pack_p)
12270 {
12271 if (decl_context == PARM)
12272 /* Turn the type into a pack expansion.*/
12273 type = make_pack_expansion (type);
12274 else
12275 error ("non-parameter %qs cannot be a parameter pack", name);
12276 }
12277
12278 if ((decl_context == FIELD || decl_context == PARM)
12279 && !processing_template_decl
12280 && variably_modified_type_p (type, NULL_TREE))
12281 {
12282 if (decl_context == FIELD)
12283 error_at (id_loc,
12284 "data member may not have variably modified type %qT", type);
12285 else
12286 error_at (id_loc,
12287 "parameter may not have variably modified type %qT", type);
12288 type = error_mark_node;
12289 }
12290
12291 if (explicitp == 1 || (explicitp && friendp))
12292 {
12293 /* [dcl.fct.spec] (C++11) The explicit specifier shall be used only
12294 in the declaration of a constructor or conversion function within
12295 a class definition. */
12296 if (!current_class_type)
12297 error_at (declspecs->locations[ds_explicit],
12298 "%<explicit%> outside class declaration");
12299 else if (friendp)
12300 error_at (declspecs->locations[ds_explicit],
12301 "%<explicit%> in friend declaration");
12302 else
12303 error_at (declspecs->locations[ds_explicit],
12304 "only declarations of constructors and conversion operators "
12305 "can be %<explicit%>");
12306 explicitp = 0;
12307 }
12308
12309 if (storage_class == sc_mutable)
12310 {
12311 location_t sloc = declspecs->locations[ds_storage_class];
12312 if (decl_context != FIELD || friendp)
12313 {
12314 error_at (sloc, "non-member %qs cannot be declared %<mutable%>",
12315 name);
12316 storage_class = sc_none;
12317 }
12318 else if (decl_context == TYPENAME || typedef_p)
12319 {
12320 error_at (sloc,
12321 "non-object member %qs cannot be declared %<mutable%>",
12322 name);
12323 storage_class = sc_none;
12324 }
12325 else if (FUNC_OR_METHOD_TYPE_P (type))
12326 {
12327 error_at (sloc, "function %qs cannot be declared %<mutable%>",
12328 name);
12329 storage_class = sc_none;
12330 }
12331 else if (staticp)
12332 {
12333 error_at (sloc, "%<static%> %qs cannot be declared %<mutable%>",
12334 name);
12335 storage_class = sc_none;
12336 }
12337 else if (type_quals & TYPE_QUAL_CONST)
12338 {
12339 error_at (sloc, "%<const%> %qs cannot be declared %<mutable%>",
12340 name);
12341 storage_class = sc_none;
12342 }
12343 else if (TYPE_REF_P (type))
12344 {
12345 permerror (sloc, "reference %qs cannot be declared %<mutable%>",
12346 name);
12347 storage_class = sc_none;
12348 }
12349 }
12350
12351 /* If this is declaring a typedef name, return a TYPE_DECL. */
12352 if (typedef_p && decl_context != TYPENAME)
12353 {
12354 bool alias_p = decl_spec_seq_has_spec_p (declspecs, ds_alias);
12355 tree decl;
12356
12357 if (funcdef_flag)
12358 {
12359 if (decl_context == NORMAL)
12360 error_at (id_loc,
12361 "typedef may not be a function definition");
12362 else
12363 error_at (id_loc,
12364 "typedef may not be a member function definition");
12365 return error_mark_node;
12366 }
12367
12368 /* This declaration:
12369
12370 typedef void f(int) const;
12371
12372 declares a function type which is not a member of any
12373 particular class, but which is cv-qualified; for
12374 example "f S::*" declares a pointer to a const-qualified
12375 member function of S. We record the cv-qualification in the
12376 function type. */
12377 if ((rqual || memfn_quals) && TREE_CODE (type) == FUNCTION_TYPE)
12378 {
12379 type = apply_memfn_quals (type, memfn_quals, rqual);
12380
12381 /* We have now dealt with these qualifiers. */
12382 memfn_quals = TYPE_UNQUALIFIED;
12383 rqual = REF_QUAL_NONE;
12384 }
12385
12386 if (type_uses_auto (type))
12387 {
12388 if (alias_p)
12389 error_at (declspecs->locations[ds_type_spec],
12390 "%<auto%> not allowed in alias declaration");
12391 else
12392 error_at (declspecs->locations[ds_type_spec],
12393 "typedef declared %<auto%>");
12394 type = error_mark_node;
12395 }
12396
12397 if (reqs)
12398 error_at (location_of (reqs), "requires-clause on typedef");
12399
12400 if (id_declarator && declarator->u.id.qualifying_scope)
12401 {
12402 error_at (id_loc, "typedef name may not be a nested-name-specifier");
12403 type = error_mark_node;
12404 }
12405
12406 if (decl_context == FIELD)
12407 decl = build_lang_decl_loc (id_loc, TYPE_DECL, unqualified_id, type);
12408 else
12409 decl = build_decl (id_loc, TYPE_DECL, unqualified_id, type);
12410
12411 if (decl_context != FIELD)
12412 {
12413 if (!current_function_decl)
12414 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
12415 else if (DECL_MAYBE_IN_CHARGE_CDTOR_P (current_function_decl))
12416 /* The TYPE_DECL is "abstract" because there will be
12417 clones of this constructor/destructor, and there will
12418 be copies of this TYPE_DECL generated in those
12419 clones. The decloning optimization (for space) may
12420 revert this subsequently if it determines that
12421 the clones should share a common implementation. */
12422 DECL_ABSTRACT_P (decl) = true;
12423 }
12424 else if (current_class_type
12425 && constructor_name_p (unqualified_id, current_class_type))
12426 permerror (id_loc, "ISO C++ forbids nested type %qD with same name "
12427 "as enclosing class",
12428 unqualified_id);
12429
12430 /* If the user declares "typedef struct {...} foo" then the
12431 struct will have an anonymous name. Fill that name in now.
12432 Nothing can refer to it, so nothing needs know about the name
12433 change. */
12434 if (type != error_mark_node
12435 && unqualified_id
12436 && TYPE_NAME (type)
12437 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12438 && TYPE_UNNAMED_P (type)
12439 && declspecs->type_definition_p
12440 && attributes_naming_typedef_ok (*attrlist)
12441 && cp_type_quals (type) == TYPE_UNQUALIFIED)
12442 name_unnamed_type (type, decl);
12443
12444 if (signed_p
12445 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
12446 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
12447
12448 bad_specifiers (decl, BSP_TYPE, virtualp,
12449 memfn_quals != TYPE_UNQUALIFIED,
12450 inlinep, friendp, raises != NULL_TREE,
12451 declspecs->locations);
12452
12453 if (alias_p)
12454 /* Acknowledge that this was written:
12455 `using analias = atype;'. */
12456 TYPE_DECL_ALIAS_P (decl) = 1;
12457
12458 return decl;
12459 }
12460
12461 /* Detect the case of an array type of unspecified size
12462 which came, as such, direct from a typedef name.
12463 We must copy the type, so that the array's domain can be
12464 individually set by the object's initializer. */
12465
12466 if (type && typedef_type
12467 && TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
12468 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
12469 type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
12470
12471 /* Detect where we're using a typedef of function type to declare a
12472 function. PARMS will not be set, so we must create it now. */
12473
12474 if (type == typedef_type && TREE_CODE (type) == FUNCTION_TYPE)
12475 {
12476 tree decls = NULL_TREE;
12477 tree args;
12478
12479 for (args = TYPE_ARG_TYPES (type);
12480 args && args != void_list_node;
12481 args = TREE_CHAIN (args))
12482 {
12483 tree decl = cp_build_parm_decl (NULL_TREE, NULL_TREE,
12484 TREE_VALUE (args));
12485
12486 DECL_CHAIN (decl) = decls;
12487 decls = decl;
12488 }
12489
12490 parms = nreverse (decls);
12491
12492 if (decl_context != TYPENAME)
12493 {
12494 /* The qualifiers on the function type become the qualifiers on
12495 the non-static member function. */
12496 memfn_quals |= type_memfn_quals (type);
12497 rqual = type_memfn_rqual (type);
12498 type_quals = TYPE_UNQUALIFIED;
12499 }
12500 }
12501
12502 /* If this is a type name (such as, in a cast or sizeof),
12503 compute the type and return it now. */
12504
12505 if (decl_context == TYPENAME)
12506 {
12507 /* Note that here we don't care about type_quals. */
12508
12509 /* Special case: "friend class foo" looks like a TYPENAME context. */
12510 if (friendp)
12511 {
12512 if (inlinep)
12513 {
12514 error ("%<inline%> specified for friend class declaration");
12515 inlinep = 0;
12516 }
12517
12518 if (!current_aggr)
12519 {
12520 /* Don't allow friend declaration without a class-key. */
12521 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
12522 permerror (input_location, "template parameters cannot be friends");
12523 else if (TREE_CODE (type) == TYPENAME_TYPE)
12524 permerror (input_location, "friend declaration requires class-key, "
12525 "i.e. %<friend class %T::%D%>",
12526 TYPE_CONTEXT (type), TYPENAME_TYPE_FULLNAME (type));
12527 else
12528 permerror (input_location, "friend declaration requires class-key, "
12529 "i.e. %<friend %#T%>",
12530 type);
12531 }
12532
12533 /* Only try to do this stuff if we didn't already give up. */
12534 if (type != integer_type_node)
12535 {
12536 /* A friendly class? */
12537 if (current_class_type)
12538 make_friend_class (current_class_type, TYPE_MAIN_VARIANT (type),
12539 /*complain=*/true);
12540 else
12541 error ("trying to make class %qT a friend of global scope",
12542 type);
12543
12544 type = void_type_node;
12545 }
12546 }
12547 else if (memfn_quals || rqual)
12548 {
12549 if (ctype == NULL_TREE
12550 && TREE_CODE (type) == METHOD_TYPE)
12551 ctype = TYPE_METHOD_BASETYPE (type);
12552
12553 if (ctype)
12554 type = build_memfn_type (type, ctype, memfn_quals, rqual);
12555 /* Core issue #547: need to allow this in template type args.
12556 Allow it in general in C++11 for alias-declarations. */
12557 else if ((template_type_arg || cxx_dialect >= cxx11)
12558 && TREE_CODE (type) == FUNCTION_TYPE)
12559 type = apply_memfn_quals (type, memfn_quals, rqual);
12560 else
12561 error ("invalid qualifiers on non-member function type");
12562 }
12563
12564 if (reqs)
12565 error_at (location_of (reqs), "requires-clause on type-id");
12566
12567 return type;
12568 }
12569 else if (unqualified_id == NULL_TREE && decl_context != PARM
12570 && decl_context != CATCHPARM
12571 && TREE_CODE (type) != UNION_TYPE
12572 && ! bitfield
12573 && innermost_code != cdk_decomp)
12574 {
12575 error ("abstract declarator %qT used as declaration", type);
12576 return error_mark_node;
12577 }
12578
12579 if (!FUNC_OR_METHOD_TYPE_P (type))
12580 {
12581 /* Only functions may be declared using an operator-function-id. */
12582 if (dname && IDENTIFIER_ANY_OP_P (dname))
12583 {
12584 error_at (id_loc, "declaration of %qD as non-function", dname);
12585 return error_mark_node;
12586 }
12587
12588 if (reqs)
12589 error_at (location_of (reqs),
12590 "requires-clause on declaration of non-function type %qT",
12591 type);
12592 }
12593
12594 /* We don't check parameter types here because we can emit a better
12595 error message later. */
12596 if (decl_context != PARM)
12597 {
12598 type = check_var_type (unqualified_id, type, id_loc);
12599 if (type == error_mark_node)
12600 return error_mark_node;
12601 }
12602
12603 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
12604 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
12605
12606 if (decl_context == PARM || decl_context == CATCHPARM)
12607 {
12608 if (ctype || in_namespace)
12609 error ("cannot use %<::%> in parameter declaration");
12610
12611 tree auto_node = type_uses_auto (type);
12612 if (auto_node && !(cxx_dialect >= cxx17 && template_parm_flag))
12613 {
12614 if (cxx_dialect >= cxx14)
12615 {
12616 if (decl_context == PARM && AUTO_IS_DECLTYPE (auto_node))
12617 error_at (typespec_loc,
12618 "cannot declare a parameter with %<decltype(auto)%>");
12619 else
12620 error_at (typespec_loc,
12621 "%<auto%> parameter not permitted in this context");
12622 }
12623 else
12624 error_at (typespec_loc, "parameter declared %<auto%>");
12625 type = error_mark_node;
12626 }
12627
12628 /* A parameter declared as an array of T is really a pointer to T.
12629 One declared as a function is really a pointer to a function.
12630 One declared as a member is really a pointer to member. */
12631
12632 if (TREE_CODE (type) == ARRAY_TYPE)
12633 {
12634 /* Transfer const-ness of array into that of type pointed to. */
12635 type = build_pointer_type (TREE_TYPE (type));
12636 type_quals = TYPE_UNQUALIFIED;
12637 array_parameter_p = true;
12638 }
12639 else if (TREE_CODE (type) == FUNCTION_TYPE)
12640 type = build_pointer_type (type);
12641 }
12642
12643 if (ctype && TREE_CODE (type) == FUNCTION_TYPE && staticp < 2
12644 && !(unqualified_id
12645 && identifier_p (unqualified_id)
12646 && IDENTIFIER_NEWDEL_OP_P (unqualified_id)))
12647 {
12648 cp_cv_quals real_quals = memfn_quals;
12649 if (cxx_dialect < cxx14 && constexpr_p
12650 && sfk != sfk_constructor && sfk != sfk_destructor)
12651 real_quals |= TYPE_QUAL_CONST;
12652 type = build_memfn_type (type, ctype, real_quals, rqual);
12653 }
12654
12655 {
12656 tree decl = NULL_TREE;
12657
12658 if (decl_context == PARM)
12659 {
12660 decl = cp_build_parm_decl (NULL_TREE, unqualified_id, type);
12661 DECL_ARRAY_PARAMETER_P (decl) = array_parameter_p;
12662
12663 bad_specifiers (decl, BSP_PARM, virtualp,
12664 memfn_quals != TYPE_UNQUALIFIED,
12665 inlinep, friendp, raises != NULL_TREE,
12666 declspecs->locations);
12667 }
12668 else if (decl_context == FIELD)
12669 {
12670 if (!staticp && !friendp && TREE_CODE (type) != METHOD_TYPE)
12671 if (tree auto_node = type_uses_auto (type))
12672 {
12673 location_t tloc = declspecs->locations[ds_type_spec];
12674 if (CLASS_PLACEHOLDER_TEMPLATE (auto_node))
12675 error_at (tloc, "invalid use of template-name %qE without an "
12676 "argument list",
12677 CLASS_PLACEHOLDER_TEMPLATE (auto_node));
12678 else
12679 error_at (tloc, "non-static data member declared with "
12680 "placeholder %qT", auto_node);
12681 type = error_mark_node;
12682 }
12683
12684 /* The C99 flexible array extension. */
12685 if (!staticp && TREE_CODE (type) == ARRAY_TYPE
12686 && TYPE_DOMAIN (type) == NULL_TREE)
12687 {
12688 if (ctype
12689 && (TREE_CODE (ctype) == UNION_TYPE
12690 || TREE_CODE (ctype) == QUAL_UNION_TYPE))
12691 {
12692 error_at (id_loc, "flexible array member in union");
12693 type = error_mark_node;
12694 }
12695 else
12696 {
12697 /* Array is a flexible member. */
12698 if (in_system_header_at (input_location))
12699 /* Do not warn on flexible array members in system
12700 headers because glibc uses them. */;
12701 else if (name)
12702 pedwarn (id_loc, OPT_Wpedantic,
12703 "ISO C++ forbids flexible array member %qs", name);
12704 else
12705 pedwarn (input_location, OPT_Wpedantic,
12706 "ISO C++ forbids flexible array members");
12707
12708 /* Flexible array member has a null domain. */
12709 type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
12710 }
12711 }
12712
12713 if (type == error_mark_node)
12714 {
12715 /* Happens when declaring arrays of sizes which
12716 are error_mark_node, for example. */
12717 decl = NULL_TREE;
12718 }
12719 else if (in_namespace && !friendp)
12720 {
12721 /* Something like struct S { int N::j; }; */
12722 error_at (id_loc, "invalid use of %<::%>");
12723 return error_mark_node;
12724 }
12725 else if (FUNC_OR_METHOD_TYPE_P (type) && unqualified_id)
12726 {
12727 int publicp = 0;
12728 tree function_context;
12729
12730 if (friendp == 0)
12731 {
12732 /* This should never happen in pure C++ (the check
12733 could be an assert). It could happen in
12734 Objective-C++ if someone writes invalid code that
12735 uses a function declaration for an instance
12736 variable or property (instance variables and
12737 properties are parsed as FIELD_DECLs, but they are
12738 part of an Objective-C class, not a C++ class).
12739 That code is invalid and is caught by this
12740 check. */
12741 if (!ctype)
12742 {
12743 error ("declaration of function %qD in invalid context",
12744 unqualified_id);
12745 return error_mark_node;
12746 }
12747
12748 /* ``A union may [ ... ] not [ have ] virtual functions.''
12749 ARM 9.5 */
12750 if (virtualp && TREE_CODE (ctype) == UNION_TYPE)
12751 {
12752 error_at (declspecs->locations[ds_virtual],
12753 "function %qD declared %<virtual%> inside a union",
12754 unqualified_id);
12755 return error_mark_node;
12756 }
12757
12758 if (virtualp
12759 && identifier_p (unqualified_id)
12760 && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
12761 {
12762 error_at (declspecs->locations[ds_virtual],
12763 "%qD cannot be declared %<virtual%>, since it "
12764 "is always static", unqualified_id);
12765 virtualp = 0;
12766 }
12767 }
12768
12769 /* Check that the name used for a destructor makes sense. */
12770 if (sfk == sfk_destructor)
12771 {
12772 tree uqname = id_declarator->u.id.unqualified_name;
12773
12774 if (!ctype)
12775 {
12776 gcc_assert (friendp);
12777 error_at (id_loc, "expected qualified name in friend "
12778 "declaration for destructor %qD", uqname);
12779 return error_mark_node;
12780 }
12781
12782 if (!check_dtor_name (ctype, TREE_OPERAND (uqname, 0)))
12783 {
12784 error_at (id_loc, "declaration of %qD as member of %qT",
12785 uqname, ctype);
12786 return error_mark_node;
12787 }
12788 if (concept_p)
12789 {
12790 error_at (declspecs->locations[ds_concept],
12791 "a destructor cannot be %<concept%>");
12792 return error_mark_node;
12793 }
12794 if (constexpr_p && cxx_dialect < cxx2a)
12795 {
12796 error_at (declspecs->locations[ds_constexpr],
12797 "%<constexpr%> destructors only available"
12798 " with %<-std=c++2a%> or %<-std=gnu++2a%>");
12799 return error_mark_node;
12800 }
12801 }
12802 else if (sfk == sfk_constructor && friendp && !ctype)
12803 {
12804 error ("expected qualified name in friend declaration "
12805 "for constructor %qD",
12806 id_declarator->u.id.unqualified_name);
12807 return error_mark_node;
12808 }
12809 if (sfk == sfk_constructor)
12810 if (concept_p)
12811 {
12812 error_at (declspecs->locations[ds_concept],
12813 "a constructor cannot be %<concept%>");
12814 return error_mark_node;
12815 }
12816 if (concept_p)
12817 {
12818 error_at (declspecs->locations[ds_concept],
12819 "a concept cannot be a member function");
12820 concept_p = false;
12821 }
12822
12823 if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
12824 {
12825 tree tmpl = TREE_OPERAND (unqualified_id, 0);
12826 if (variable_template_p (tmpl))
12827 {
12828 error_at (id_loc, "specialization of variable template "
12829 "%qD declared as function", tmpl);
12830 inform (DECL_SOURCE_LOCATION (tmpl),
12831 "variable template declared here");
12832 return error_mark_node;
12833 }
12834 }
12835
12836 /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node. */
12837 function_context
12838 = (ctype != NULL_TREE
12839 ? decl_function_context (TYPE_MAIN_DECL (ctype)) : NULL_TREE);
12840 publicp = ((! friendp || ! staticp)
12841 && function_context == NULL_TREE);
12842
12843 decl = grokfndecl (ctype, type,
12844 TREE_CODE (unqualified_id) != TEMPLATE_ID_EXPR
12845 ? unqualified_id : dname,
12846 parms,
12847 unqualified_id,
12848 declspecs,
12849 reqs,
12850 virtualp, flags, memfn_quals, rqual, raises,
12851 friendp ? -1 : 0, friendp, publicp,
12852 inlinep | (2 * constexpr_p) | (4 * concept_p),
12853 initialized == SD_DELETED, sfk,
12854 funcdef_flag, late_return_type_p,
12855 template_count, in_namespace,
12856 attrlist, id_loc);
12857 decl = set_virt_specifiers (decl, virt_specifiers);
12858 if (decl == NULL_TREE)
12859 return error_mark_node;
12860 #if 0
12861 /* This clobbers the attrs stored in `decl' from `attrlist'. */
12862 /* The decl and setting of decl_attr is also turned off. */
12863 decl = build_decl_attribute_variant (decl, decl_attr);
12864 #endif
12865
12866 /* [class.conv.ctor]
12867
12868 A constructor declared without the function-specifier
12869 explicit that can be called with a single parameter
12870 specifies a conversion from the type of its first
12871 parameter to the type of its class. Such a constructor
12872 is called a converting constructor. */
12873 if (explicitp == 2)
12874 DECL_NONCONVERTING_P (decl) = 1;
12875
12876 if (declspecs->explicit_specifier)
12877 store_explicit_specifier (decl, declspecs->explicit_specifier);
12878 }
12879 else if (!staticp && !dependent_type_p (type)
12880 && !COMPLETE_TYPE_P (complete_type (type))
12881 && (!complete_or_array_type_p (type)
12882 || initialized == 0))
12883 {
12884 if (TREE_CODE (type) != ARRAY_TYPE
12885 || !COMPLETE_TYPE_P (TREE_TYPE (type)))
12886 {
12887 if (unqualified_id)
12888 {
12889 error_at (id_loc, "field %qD has incomplete type %qT",
12890 unqualified_id, type);
12891 cxx_incomplete_type_inform (strip_array_types (type));
12892 }
12893 else
12894 error ("name %qT has incomplete type", type);
12895
12896 type = error_mark_node;
12897 decl = NULL_TREE;
12898 }
12899 }
12900 else
12901 {
12902 if (friendp)
12903 {
12904 if (unqualified_id)
12905 error_at (id_loc,
12906 "%qE is neither function nor member function; "
12907 "cannot be declared friend", unqualified_id);
12908 else
12909 error ("unnamed field is neither function nor member "
12910 "function; cannot be declared friend");
12911 return error_mark_node;
12912 }
12913 decl = NULL_TREE;
12914 }
12915
12916 if (friendp)
12917 {
12918 /* Friends are treated specially. */
12919 if (ctype == current_class_type)
12920 ; /* We already issued a permerror. */
12921 else if (decl && DECL_NAME (decl))
12922 {
12923 if (template_class_depth (current_class_type) == 0)
12924 {
12925 decl = check_explicit_specialization
12926 (unqualified_id, decl, template_count,
12927 2 * funcdef_flag + 4);
12928 if (decl == error_mark_node)
12929 return error_mark_node;
12930 }
12931
12932 decl = do_friend (ctype, unqualified_id, decl,
12933 *attrlist, flags,
12934 funcdef_flag);
12935 return decl;
12936 }
12937 else
12938 return error_mark_node;
12939 }
12940
12941 /* Structure field. It may not be a function, except for C++. */
12942
12943 if (decl == NULL_TREE)
12944 {
12945 if (staticp)
12946 {
12947 /* C++ allows static class members. All other work
12948 for this is done by grokfield. */
12949 decl = build_lang_decl_loc (id_loc, VAR_DECL,
12950 unqualified_id, type);
12951 set_linkage_for_static_data_member (decl);
12952 if (concept_p)
12953 error_at (declspecs->locations[ds_concept],
12954 "static data member %qE declared %<concept%>",
12955 unqualified_id);
12956 else if (constexpr_p && !initialized)
12957 {
12958 error_at (DECL_SOURCE_LOCATION (decl),
12959 "%<constexpr%> static data member %qD must "
12960 "have an initializer", decl);
12961 constexpr_p = false;
12962 }
12963
12964 if (inlinep)
12965 mark_inline_variable (decl, declspecs->locations[ds_inline]);
12966
12967 if (!DECL_VAR_DECLARED_INLINE_P (decl)
12968 && !(cxx_dialect >= cxx17 && constexpr_p))
12969 /* Even if there is an in-class initialization, DECL
12970 is considered undefined until an out-of-class
12971 definition is provided, unless this is an inline
12972 variable. */
12973 DECL_EXTERNAL (decl) = 1;
12974
12975 if (thread_p)
12976 {
12977 CP_DECL_THREAD_LOCAL_P (decl) = true;
12978 if (!processing_template_decl)
12979 set_decl_tls_model (decl, decl_default_tls_model (decl));
12980 if (declspecs->gnu_thread_keyword_p)
12981 SET_DECL_GNU_TLS_P (decl);
12982 }
12983 }
12984 else
12985 {
12986 if (concept_p)
12987 error_at (declspecs->locations[ds_concept],
12988 "non-static data member %qE declared %<concept%>",
12989 unqualified_id);
12990 else if (constexpr_p)
12991 {
12992 error_at (declspecs->locations[ds_constexpr],
12993 "non-static data member %qE declared "
12994 "%<constexpr%>", unqualified_id);
12995 constexpr_p = false;
12996 }
12997 else if (constinit_p)
12998 {
12999 error_at (declspecs->locations[ds_constinit],
13000 "non-static data member %qE declared "
13001 "%<constinit%>", unqualified_id);
13002 constinit_p = false;
13003 }
13004 decl = build_decl (id_loc, FIELD_DECL, unqualified_id, type);
13005 DECL_NONADDRESSABLE_P (decl) = bitfield;
13006 if (bitfield && !unqualified_id)
13007 {
13008 TREE_NO_WARNING (decl) = 1;
13009 DECL_PADDING_P (decl) = 1;
13010 }
13011
13012 if (storage_class == sc_mutable)
13013 {
13014 DECL_MUTABLE_P (decl) = 1;
13015 storage_class = sc_none;
13016 }
13017
13018 if (initialized)
13019 {
13020 /* An attempt is being made to initialize a non-static
13021 member. This is new in C++11. */
13022 maybe_warn_cpp0x (CPP0X_NSDMI);
13023
13024 /* If this has been parsed with static storage class, but
13025 errors forced staticp to be cleared, ensure NSDMI is
13026 not present. */
13027 if (declspecs->storage_class == sc_static)
13028 DECL_INITIAL (decl) = error_mark_node;
13029 }
13030 }
13031
13032 bad_specifiers (decl, BSP_FIELD, virtualp,
13033 memfn_quals != TYPE_UNQUALIFIED,
13034 staticp ? false : inlinep, friendp,
13035 raises != NULL_TREE,
13036 declspecs->locations);
13037 }
13038 }
13039 else if (FUNC_OR_METHOD_TYPE_P (type))
13040 {
13041 tree original_name;
13042 int publicp = 0;
13043
13044 if (!unqualified_id)
13045 return error_mark_node;
13046
13047 if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
13048 original_name = dname;
13049 else
13050 original_name = unqualified_id;
13051 // FIXME:gcc_assert (original_name == dname);
13052
13053 if (storage_class == sc_auto)
13054 error_at (declspecs->locations[ds_storage_class],
13055 "storage class %<auto%> invalid for function %qs", name);
13056 else if (storage_class == sc_register)
13057 error_at (declspecs->locations[ds_storage_class],
13058 "storage class %<register%> invalid for function %qs",
13059 name);
13060 else if (thread_p)
13061 {
13062 if (declspecs->gnu_thread_keyword_p)
13063 error_at (declspecs->locations[ds_thread],
13064 "storage class %<__thread%> invalid for function %qs",
13065 name);
13066 else
13067 error_at (declspecs->locations[ds_thread],
13068 "storage class %<thread_local%> invalid for "
13069 "function %qs", name);
13070 }
13071
13072 if (virt_specifiers)
13073 error ("virt-specifiers in %qs not allowed outside a class "
13074 "definition", name);
13075 /* Function declaration not at top level.
13076 Storage classes other than `extern' are not allowed
13077 and `extern' makes no difference. */
13078 if (! toplevel_bindings_p ()
13079 && (storage_class == sc_static
13080 || decl_spec_seq_has_spec_p (declspecs, ds_inline))
13081 && pedantic)
13082 {
13083 if (storage_class == sc_static)
13084 pedwarn (declspecs->locations[ds_storage_class], OPT_Wpedantic,
13085 "%<static%> specifier invalid for function %qs "
13086 "declared out of global scope", name);
13087 else
13088 pedwarn (declspecs->locations[ds_inline], OPT_Wpedantic,
13089 "%<inline%> specifier invalid for function %qs "
13090 "declared out of global scope", name);
13091 }
13092
13093 if (ctype == NULL_TREE)
13094 {
13095 if (virtualp)
13096 {
13097 error ("virtual non-class function %qs", name);
13098 virtualp = 0;
13099 }
13100 else if (sfk == sfk_constructor
13101 || sfk == sfk_destructor)
13102 {
13103 error (funcdef_flag
13104 ? G_("%qs defined in a non-class scope")
13105 : G_("%qs declared in a non-class scope"), name);
13106 sfk = sfk_none;
13107 }
13108 }
13109
13110 /* Record whether the function is public. */
13111 publicp = (ctype != NULL_TREE
13112 || storage_class != sc_static);
13113
13114 decl = grokfndecl (ctype, type, original_name, parms, unqualified_id,
13115 declspecs,
13116 reqs, virtualp, flags, memfn_quals, rqual, raises,
13117 1, friendp,
13118 publicp,
13119 inlinep | (2 * constexpr_p) | (4 * concept_p),
13120 initialized == SD_DELETED,
13121 sfk,
13122 funcdef_flag,
13123 late_return_type_p,
13124 template_count, in_namespace, attrlist,
13125 id_loc);
13126 if (decl == NULL_TREE)
13127 return error_mark_node;
13128
13129 if (explicitp == 2)
13130 DECL_NONCONVERTING_P (decl) = 1;
13131 if (staticp == 1)
13132 {
13133 int invalid_static = 0;
13134
13135 /* Don't allow a static member function in a class, and forbid
13136 declaring main to be static. */
13137 if (TREE_CODE (type) == METHOD_TYPE)
13138 {
13139 permerror (input_location, "cannot declare member function %qD to have "
13140 "static linkage", decl);
13141 invalid_static = 1;
13142 }
13143 else if (current_function_decl)
13144 {
13145 /* 7.1.1: There can be no static function declarations within a
13146 block. */
13147 error_at (declspecs->locations[ds_storage_class],
13148 "cannot declare static function inside another function");
13149 invalid_static = 1;
13150 }
13151
13152 if (invalid_static)
13153 {
13154 staticp = 0;
13155 storage_class = sc_none;
13156 }
13157 }
13158 }
13159 else
13160 {
13161 /* It's a variable. */
13162
13163 /* An uninitialized decl with `extern' is a reference. */
13164 decl = grokvardecl (type, dname, unqualified_id,
13165 declspecs,
13166 initialized,
13167 type_quals,
13168 inlinep,
13169 concept_p,
13170 template_count,
13171 ctype ? ctype : in_namespace,
13172 id_loc);
13173 if (decl == NULL_TREE)
13174 return error_mark_node;
13175
13176 bad_specifiers (decl, BSP_VAR, virtualp,
13177 memfn_quals != TYPE_UNQUALIFIED,
13178 inlinep, friendp, raises != NULL_TREE,
13179 declspecs->locations);
13180
13181 if (ctype)
13182 {
13183 DECL_CONTEXT (decl) = ctype;
13184 if (staticp == 1)
13185 {
13186 permerror (declspecs->locations[ds_storage_class],
13187 "%<static%> may not be used when defining "
13188 "(as opposed to declaring) a static data member");
13189 staticp = 0;
13190 storage_class = sc_none;
13191 }
13192 if (storage_class == sc_register && TREE_STATIC (decl))
13193 {
13194 error ("static member %qD declared %<register%>", decl);
13195 storage_class = sc_none;
13196 }
13197 if (storage_class == sc_extern && pedantic)
13198 {
13199 pedwarn (input_location, OPT_Wpedantic,
13200 "cannot explicitly declare member %q#D to have "
13201 "extern linkage", decl);
13202 storage_class = sc_none;
13203 }
13204 }
13205 else if (constexpr_p && DECL_EXTERNAL (decl))
13206 {
13207 error_at (DECL_SOURCE_LOCATION (decl),
13208 "declaration of %<constexpr%> variable %qD "
13209 "is not a definition", decl);
13210 constexpr_p = false;
13211 }
13212
13213 if (inlinep)
13214 mark_inline_variable (decl, declspecs->locations[ds_inline]);
13215 if (innermost_code == cdk_decomp)
13216 {
13217 gcc_assert (declarator && declarator->kind == cdk_decomp);
13218 DECL_SOURCE_LOCATION (decl) = id_loc;
13219 DECL_ARTIFICIAL (decl) = 1;
13220 fit_decomposition_lang_decl (decl, NULL_TREE);
13221 }
13222 }
13223
13224 if (VAR_P (decl) && !initialized)
13225 if (tree auto_node = type_uses_auto (type))
13226 if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node))
13227 {
13228 location_t loc = declspecs->locations[ds_type_spec];
13229 error_at (loc, "declaration of %q#D has no initializer", decl);
13230 TREE_TYPE (decl) = error_mark_node;
13231 }
13232
13233 if (storage_class == sc_extern && initialized && !funcdef_flag)
13234 {
13235 if (toplevel_bindings_p ())
13236 {
13237 /* It's common practice (and completely valid) to have a const
13238 be initialized and declared extern. */
13239 if (!(type_quals & TYPE_QUAL_CONST))
13240 warning_at (DECL_SOURCE_LOCATION (decl), 0,
13241 "%qs initialized and declared %<extern%>", name);
13242 }
13243 else
13244 {
13245 error_at (DECL_SOURCE_LOCATION (decl),
13246 "%qs has both %<extern%> and initializer", name);
13247 return error_mark_node;
13248 }
13249 }
13250
13251 /* Record `register' declaration for warnings on &
13252 and in case doing stupid register allocation. */
13253
13254 if (storage_class == sc_register)
13255 {
13256 DECL_REGISTER (decl) = 1;
13257 /* Warn about register storage specifiers on PARM_DECLs. */
13258 if (TREE_CODE (decl) == PARM_DECL)
13259 {
13260 if (cxx_dialect >= cxx17)
13261 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
13262 "ISO C++17 does not allow %<register%> storage "
13263 "class specifier");
13264 else
13265 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
13266 "%<register%> storage class specifier used");
13267 }
13268 }
13269 else if (storage_class == sc_extern)
13270 DECL_THIS_EXTERN (decl) = 1;
13271 else if (storage_class == sc_static)
13272 DECL_THIS_STATIC (decl) = 1;
13273
13274 /* Set constexpr flag on vars (functions got it in grokfndecl). */
13275 if (constexpr_p && VAR_P (decl))
13276 DECL_DECLARED_CONSTEXPR_P (decl) = true;
13277
13278 /* Record constancy and volatility on the DECL itself . There's
13279 no need to do this when processing a template; we'll do this
13280 for the instantiated declaration based on the type of DECL. */
13281 if (!processing_template_decl)
13282 cp_apply_type_quals_to_decl (type_quals, decl);
13283
13284 return decl;
13285 }
13286 }
13287 \f
13288 /* Subroutine of start_function. Ensure that each of the parameter
13289 types (as listed in PARMS) is complete, as is required for a
13290 function definition. */
13291
13292 static void
13293 require_complete_types_for_parms (tree parms)
13294 {
13295 for (; parms; parms = DECL_CHAIN (parms))
13296 {
13297 if (dependent_type_p (TREE_TYPE (parms)))
13298 continue;
13299 if (!VOID_TYPE_P (TREE_TYPE (parms))
13300 && complete_type_or_else (TREE_TYPE (parms), parms))
13301 {
13302 relayout_decl (parms);
13303 DECL_ARG_TYPE (parms) = type_passed_as (TREE_TYPE (parms));
13304
13305 maybe_warn_parm_abi (TREE_TYPE (parms),
13306 DECL_SOURCE_LOCATION (parms));
13307 }
13308 else
13309 /* grokparms or complete_type_or_else will have already issued
13310 an error. */
13311 TREE_TYPE (parms) = error_mark_node;
13312 }
13313 }
13314
13315 /* Returns nonzero if T is a local variable. */
13316
13317 int
13318 local_variable_p (const_tree t)
13319 {
13320 if ((VAR_P (t)
13321 /* A VAR_DECL with a context that is a _TYPE is a static data
13322 member. */
13323 && !TYPE_P (CP_DECL_CONTEXT (t))
13324 /* Any other non-local variable must be at namespace scope. */
13325 && !DECL_NAMESPACE_SCOPE_P (t))
13326 || (TREE_CODE (t) == PARM_DECL))
13327 return 1;
13328
13329 return 0;
13330 }
13331
13332 /* Like local_variable_p, but suitable for use as a tree-walking
13333 function. */
13334
13335 static tree
13336 local_variable_p_walkfn (tree *tp, int *walk_subtrees,
13337 void * /*data*/)
13338 {
13339 if (local_variable_p (*tp)
13340 && (!DECL_ARTIFICIAL (*tp) || DECL_NAME (*tp) == this_identifier))
13341 return *tp;
13342 else if (TYPE_P (*tp))
13343 *walk_subtrees = 0;
13344
13345 return NULL_TREE;
13346 }
13347
13348 /* Check that ARG, which is a default-argument expression for a
13349 parameter DECL, is valid. Returns ARG, or ERROR_MARK_NODE, if
13350 something goes wrong. DECL may also be a _TYPE node, rather than a
13351 DECL, if there is no DECL available. */
13352
13353 tree
13354 check_default_argument (tree decl, tree arg, tsubst_flags_t complain)
13355 {
13356 tree var;
13357 tree decl_type;
13358
13359 if (TREE_CODE (arg) == DEFERRED_PARSE)
13360 /* We get a DEFERRED_PARSE when looking at an in-class declaration
13361 with a default argument. Ignore the argument for now; we'll
13362 deal with it after the class is complete. */
13363 return arg;
13364
13365 if (TYPE_P (decl))
13366 {
13367 decl_type = decl;
13368 decl = NULL_TREE;
13369 }
13370 else
13371 decl_type = TREE_TYPE (decl);
13372
13373 if (arg == error_mark_node
13374 || decl == error_mark_node
13375 || TREE_TYPE (arg) == error_mark_node
13376 || decl_type == error_mark_node)
13377 /* Something already went wrong. There's no need to check
13378 further. */
13379 return error_mark_node;
13380
13381 /* [dcl.fct.default]
13382
13383 A default argument expression is implicitly converted to the
13384 parameter type. */
13385 ++cp_unevaluated_operand;
13386 /* Avoid digest_init clobbering the initializer. */
13387 tree carg = BRACE_ENCLOSED_INITIALIZER_P (arg) ? unshare_expr (arg): arg;
13388 perform_implicit_conversion_flags (decl_type, carg, complain,
13389 LOOKUP_IMPLICIT);
13390 --cp_unevaluated_operand;
13391
13392 /* Avoid redundant -Wzero-as-null-pointer-constant warnings at
13393 the call sites. */
13394 if (TYPE_PTR_OR_PTRMEM_P (decl_type)
13395 && null_ptr_cst_p (arg)
13396 /* Don't lose side-effects as in PR90473. */
13397 && !TREE_SIDE_EFFECTS (arg))
13398 return nullptr_node;
13399
13400 /* [dcl.fct.default]
13401
13402 Local variables shall not be used in default argument
13403 expressions.
13404
13405 The keyword `this' shall not be used in a default argument of a
13406 member function. */
13407 var = cp_walk_tree_without_duplicates (&arg, local_variable_p_walkfn, NULL);
13408 if (var)
13409 {
13410 if (complain & tf_warning_or_error)
13411 {
13412 if (DECL_NAME (var) == this_identifier)
13413 permerror (input_location, "default argument %qE uses %qD",
13414 arg, var);
13415 else
13416 error ("default argument %qE uses local variable %qD", arg, var);
13417 }
13418 return error_mark_node;
13419 }
13420
13421 /* All is well. */
13422 return arg;
13423 }
13424
13425 /* Returns a deprecated type used within TYPE, or NULL_TREE if none. */
13426
13427 static tree
13428 type_is_deprecated (tree type)
13429 {
13430 enum tree_code code;
13431 if (TREE_DEPRECATED (type))
13432 return type;
13433 if (TYPE_NAME (type))
13434 {
13435 if (TREE_DEPRECATED (TYPE_NAME (type)))
13436 return type;
13437 else
13438 {
13439 cp_warn_deprecated_use_scopes (CP_DECL_CONTEXT (TYPE_NAME (type)));
13440 return NULL_TREE;
13441 }
13442 }
13443
13444 /* Do warn about using typedefs to a deprecated class. */
13445 if (OVERLOAD_TYPE_P (type) && type != TYPE_MAIN_VARIANT (type))
13446 return type_is_deprecated (TYPE_MAIN_VARIANT (type));
13447
13448 code = TREE_CODE (type);
13449
13450 if (code == POINTER_TYPE || code == REFERENCE_TYPE
13451 || code == OFFSET_TYPE || code == FUNCTION_TYPE
13452 || code == METHOD_TYPE || code == ARRAY_TYPE)
13453 return type_is_deprecated (TREE_TYPE (type));
13454
13455 if (TYPE_PTRMEMFUNC_P (type))
13456 return type_is_deprecated
13457 (TREE_TYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type))));
13458
13459 return NULL_TREE;
13460 }
13461
13462 /* Decode the list of parameter types for a function type.
13463 Given the list of things declared inside the parens,
13464 return a list of types.
13465
13466 If this parameter does not end with an ellipsis, we append
13467 void_list_node.
13468
13469 *PARMS is set to the chain of PARM_DECLs created. */
13470
13471 tree
13472 grokparms (tree parmlist, tree *parms)
13473 {
13474 tree result = NULL_TREE;
13475 tree decls = NULL_TREE;
13476 tree parm;
13477 int any_error = 0;
13478
13479 for (parm = parmlist; parm != NULL_TREE; parm = TREE_CHAIN (parm))
13480 {
13481 tree type = NULL_TREE;
13482 tree init = TREE_PURPOSE (parm);
13483 tree decl = TREE_VALUE (parm);
13484
13485 if (parm == void_list_node)
13486 break;
13487
13488 if (! decl || TREE_TYPE (decl) == error_mark_node)
13489 continue;
13490
13491 type = TREE_TYPE (decl);
13492 if (VOID_TYPE_P (type))
13493 {
13494 if (same_type_p (type, void_type_node)
13495 && !init
13496 && !DECL_NAME (decl) && !result
13497 && TREE_CHAIN (parm) == void_list_node)
13498 /* DR 577: A parameter list consisting of a single
13499 unnamed parameter of non-dependent type 'void'. */
13500 break;
13501 else if (cv_qualified_p (type))
13502 error_at (DECL_SOURCE_LOCATION (decl),
13503 "invalid use of cv-qualified type %qT in "
13504 "parameter declaration", type);
13505 else
13506 error_at (DECL_SOURCE_LOCATION (decl),
13507 "invalid use of type %<void%> in parameter "
13508 "declaration");
13509 /* It's not a good idea to actually create parameters of
13510 type `void'; other parts of the compiler assume that a
13511 void type terminates the parameter list. */
13512 type = error_mark_node;
13513 TREE_TYPE (decl) = error_mark_node;
13514 }
13515
13516 if (type != error_mark_node)
13517 {
13518 if (deprecated_state != DEPRECATED_SUPPRESS)
13519 {
13520 tree deptype = type_is_deprecated (type);
13521 if (deptype)
13522 cp_warn_deprecated_use (deptype);
13523 }
13524
13525 /* [dcl.fct] "A parameter with volatile-qualified type is
13526 deprecated." */
13527 if (CP_TYPE_VOLATILE_P (type))
13528 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wvolatile,
13529 "%<volatile%>-qualified parameter is "
13530 "deprecated");
13531
13532 /* Top-level qualifiers on the parameters are
13533 ignored for function types. */
13534 type = cp_build_qualified_type (type, 0);
13535 if (TREE_CODE (type) == METHOD_TYPE)
13536 {
13537 error ("parameter %qD invalidly declared method type", decl);
13538 type = build_pointer_type (type);
13539 TREE_TYPE (decl) = type;
13540 }
13541 else if (abstract_virtuals_error (decl, type))
13542 any_error = 1; /* Seems like a good idea. */
13543 else if (cxx_dialect < cxx17 && INDIRECT_TYPE_P (type))
13544 {
13545 /* Before C++17 DR 393:
13546 [dcl.fct]/6, parameter types cannot contain pointers
13547 (references) to arrays of unknown bound. */
13548 tree t = TREE_TYPE (type);
13549 int ptr = TYPE_PTR_P (type);
13550
13551 while (1)
13552 {
13553 if (TYPE_PTR_P (t))
13554 ptr = 1;
13555 else if (TREE_CODE (t) != ARRAY_TYPE)
13556 break;
13557 else if (!TYPE_DOMAIN (t))
13558 break;
13559 t = TREE_TYPE (t);
13560 }
13561 if (TREE_CODE (t) == ARRAY_TYPE)
13562 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
13563 ptr
13564 ? G_("parameter %qD includes pointer to array of "
13565 "unknown bound %qT")
13566 : G_("parameter %qD includes reference to array of "
13567 "unknown bound %qT"),
13568 decl, t);
13569 }
13570
13571 if (any_error)
13572 init = NULL_TREE;
13573 else if (init && !processing_template_decl)
13574 init = check_default_argument (decl, init, tf_warning_or_error);
13575 }
13576
13577 DECL_CHAIN (decl) = decls;
13578 decls = decl;
13579 result = tree_cons (init, type, result);
13580 }
13581 decls = nreverse (decls);
13582 result = nreverse (result);
13583 if (parm)
13584 result = chainon (result, void_list_node);
13585 *parms = decls;
13586
13587 return result;
13588 }
13589
13590 \f
13591 /* D is a constructor or overloaded `operator='.
13592
13593 Let T be the class in which D is declared. Then, this function
13594 returns:
13595
13596 -1 if D's is an ill-formed constructor or copy assignment operator
13597 whose first parameter is of type `T'.
13598 0 if D is not a copy constructor or copy assignment
13599 operator.
13600 1 if D is a copy constructor or copy assignment operator whose
13601 first parameter is a reference to non-const qualified T.
13602 2 if D is a copy constructor or copy assignment operator whose
13603 first parameter is a reference to const qualified T.
13604
13605 This function can be used as a predicate. Positive values indicate
13606 a copy constructor and nonzero values indicate a copy assignment
13607 operator. */
13608
13609 int
13610 copy_fn_p (const_tree d)
13611 {
13612 tree args;
13613 tree arg_type;
13614 int result = 1;
13615
13616 gcc_assert (DECL_FUNCTION_MEMBER_P (d));
13617
13618 if (TREE_CODE (d) == TEMPLATE_DECL
13619 || (DECL_TEMPLATE_INFO (d)
13620 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
13621 /* Instantiations of template member functions are never copy
13622 functions. Note that member functions of templated classes are
13623 represented as template functions internally, and we must
13624 accept those as copy functions. */
13625 return 0;
13626
13627 args = FUNCTION_FIRST_USER_PARMTYPE (d);
13628 if (!args)
13629 return 0;
13630
13631 arg_type = TREE_VALUE (args);
13632 if (arg_type == error_mark_node)
13633 return 0;
13634
13635 if (TYPE_MAIN_VARIANT (arg_type) == DECL_CONTEXT (d))
13636 {
13637 /* Pass by value copy assignment operator. */
13638 result = -1;
13639 }
13640 else if (TYPE_REF_P (arg_type)
13641 && !TYPE_REF_IS_RVALUE (arg_type)
13642 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)) == DECL_CONTEXT (d))
13643 {
13644 if (CP_TYPE_CONST_P (TREE_TYPE (arg_type)))
13645 result = 2;
13646 }
13647 else
13648 return 0;
13649
13650 args = TREE_CHAIN (args);
13651
13652 if (args && args != void_list_node && !TREE_PURPOSE (args))
13653 /* There are more non-optional args. */
13654 return 0;
13655
13656 return result;
13657 }
13658
13659 /* D is a constructor or overloaded `operator='.
13660
13661 Let T be the class in which D is declared. Then, this function
13662 returns true when D is a move constructor or move assignment
13663 operator, false otherwise. */
13664
13665 bool
13666 move_fn_p (const_tree d)
13667 {
13668 gcc_assert (DECL_FUNCTION_MEMBER_P (d));
13669
13670 if (cxx_dialect == cxx98)
13671 /* There are no move constructors if we are in C++98 mode. */
13672 return false;
13673
13674 if (TREE_CODE (d) == TEMPLATE_DECL
13675 || (DECL_TEMPLATE_INFO (d)
13676 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
13677 /* Instantiations of template member functions are never move
13678 functions. Note that member functions of templated classes are
13679 represented as template functions internally, and we must
13680 accept those as move functions. */
13681 return 0;
13682
13683 return move_signature_fn_p (d);
13684 }
13685
13686 /* D is a constructor or overloaded `operator='.
13687
13688 Then, this function returns true when D has the same signature as a move
13689 constructor or move assignment operator (because either it is such a
13690 ctor/op= or it is a template specialization with the same signature),
13691 false otherwise. */
13692
13693 bool
13694 move_signature_fn_p (const_tree d)
13695 {
13696 tree args;
13697 tree arg_type;
13698 bool result = false;
13699
13700 args = FUNCTION_FIRST_USER_PARMTYPE (d);
13701 if (!args)
13702 return 0;
13703
13704 arg_type = TREE_VALUE (args);
13705 if (arg_type == error_mark_node)
13706 return 0;
13707
13708 if (TYPE_REF_P (arg_type)
13709 && TYPE_REF_IS_RVALUE (arg_type)
13710 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)),
13711 DECL_CONTEXT (d)))
13712 result = true;
13713
13714 args = TREE_CHAIN (args);
13715
13716 if (args && args != void_list_node && !TREE_PURPOSE (args))
13717 /* There are more non-optional args. */
13718 return false;
13719
13720 return result;
13721 }
13722
13723 /* Remember any special properties of member function DECL. */
13724
13725 void
13726 grok_special_member_properties (tree decl)
13727 {
13728 tree class_type;
13729
13730 if (TREE_CODE (decl) == USING_DECL
13731 || !DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
13732 return;
13733
13734 class_type = DECL_CONTEXT (decl);
13735 if (IDENTIFIER_CTOR_P (DECL_NAME (decl)))
13736 {
13737 int ctor = copy_fn_p (decl);
13738
13739 if (!DECL_ARTIFICIAL (decl))
13740 TYPE_HAS_USER_CONSTRUCTOR (class_type) = 1;
13741
13742 if (ctor > 0)
13743 {
13744 /* [class.copy]
13745
13746 A non-template constructor for class X is a copy
13747 constructor if its first parameter is of type X&, const
13748 X&, volatile X& or const volatile X&, and either there
13749 are no other parameters or else all other parameters have
13750 default arguments. */
13751 TYPE_HAS_COPY_CTOR (class_type) = 1;
13752 if (ctor > 1)
13753 TYPE_HAS_CONST_COPY_CTOR (class_type) = 1;
13754 }
13755 else if (sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (decl)))
13756 TYPE_HAS_DEFAULT_CONSTRUCTOR (class_type) = 1;
13757 else if (is_list_ctor (decl))
13758 TYPE_HAS_LIST_CTOR (class_type) = 1;
13759
13760 if (DECL_DECLARED_CONSTEXPR_P (decl)
13761 && !ctor && !move_fn_p (decl))
13762 TYPE_HAS_CONSTEXPR_CTOR (class_type) = 1;
13763 }
13764 else if (DECL_NAME (decl) == assign_op_identifier)
13765 {
13766 /* [class.copy]
13767
13768 A non-template assignment operator for class X is a copy
13769 assignment operator if its parameter is of type X, X&, const
13770 X&, volatile X& or const volatile X&. */
13771
13772 int assop = copy_fn_p (decl);
13773
13774 if (assop)
13775 {
13776 TYPE_HAS_COPY_ASSIGN (class_type) = 1;
13777 if (assop != 1)
13778 TYPE_HAS_CONST_COPY_ASSIGN (class_type) = 1;
13779 }
13780 }
13781 else if (IDENTIFIER_CONV_OP_P (DECL_NAME (decl)))
13782 TYPE_HAS_CONVERSION (class_type) = true;
13783
13784 /* Destructors are handled in check_methods. */
13785 }
13786
13787 /* Check a constructor DECL has the correct form. Complains
13788 if the class has a constructor of the form X(X). */
13789
13790 bool
13791 grok_ctor_properties (const_tree ctype, const_tree decl)
13792 {
13793 int ctor_parm = copy_fn_p (decl);
13794
13795 if (ctor_parm < 0)
13796 {
13797 /* [class.copy]
13798
13799 A declaration of a constructor for a class X is ill-formed if
13800 its first parameter is of type (optionally cv-qualified) X
13801 and either there are no other parameters or else all other
13802 parameters have default arguments.
13803
13804 We *don't* complain about member template instantiations that
13805 have this form, though; they can occur as we try to decide
13806 what constructor to use during overload resolution. Since
13807 overload resolution will never prefer such a constructor to
13808 the non-template copy constructor (which is either explicitly
13809 or implicitly defined), there's no need to worry about their
13810 existence. Theoretically, they should never even be
13811 instantiated, but that's hard to forestall. */
13812 error_at (DECL_SOURCE_LOCATION (decl),
13813 "invalid constructor; you probably meant %<%T (const %T&)%>",
13814 ctype, ctype);
13815 return false;
13816 }
13817
13818 return true;
13819 }
13820
13821 /* DECL is a declaration for an overloaded or conversion operator. If
13822 COMPLAIN is true, errors are issued for invalid declarations. */
13823
13824 bool
13825 grok_op_properties (tree decl, bool complain)
13826 {
13827 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
13828 bool methodp = TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE;
13829 tree name = DECL_NAME (decl);
13830 location_t loc = DECL_SOURCE_LOCATION (decl);
13831
13832 tree class_type = DECL_CONTEXT (decl);
13833 if (class_type && !CLASS_TYPE_P (class_type))
13834 class_type = NULL_TREE;
13835
13836 tree_code operator_code;
13837 unsigned op_flags;
13838 if (IDENTIFIER_CONV_OP_P (name))
13839 {
13840 /* Conversion operators are TYPE_EXPR for the purposes of this
13841 function. */
13842 operator_code = TYPE_EXPR;
13843 op_flags = OVL_OP_FLAG_UNARY;
13844 }
13845 else
13846 {
13847 const ovl_op_info_t *ovl_op = IDENTIFIER_OVL_OP_INFO (name);
13848
13849 operator_code = ovl_op->tree_code;
13850 op_flags = ovl_op->flags;
13851 gcc_checking_assert (operator_code != ERROR_MARK);
13852 DECL_OVERLOADED_OPERATOR_CODE_RAW (decl) = ovl_op->ovl_op_code;
13853 }
13854
13855 if (op_flags & OVL_OP_FLAG_ALLOC)
13856 {
13857 /* operator new and operator delete are quite special. */
13858 if (class_type)
13859 switch (op_flags)
13860 {
13861 case OVL_OP_FLAG_ALLOC:
13862 TYPE_HAS_NEW_OPERATOR (class_type) = 1;
13863 break;
13864
13865 case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_DELETE:
13866 TYPE_GETS_DELETE (class_type) |= 1;
13867 break;
13868
13869 case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_VEC:
13870 TYPE_HAS_ARRAY_NEW_OPERATOR (class_type) = 1;
13871 break;
13872
13873 case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_DELETE | OVL_OP_FLAG_VEC:
13874 TYPE_GETS_DELETE (class_type) |= 2;
13875 break;
13876
13877 default:
13878 gcc_unreachable ();
13879 }
13880
13881 /* [basic.std.dynamic.allocation]/1:
13882
13883 A program is ill-formed if an allocation function is declared
13884 in a namespace scope other than global scope or declared
13885 static in global scope.
13886
13887 The same also holds true for deallocation functions. */
13888 if (DECL_NAMESPACE_SCOPE_P (decl))
13889 {
13890 if (CP_DECL_CONTEXT (decl) != global_namespace)
13891 {
13892 error_at (loc, "%qD may not be declared within a namespace",
13893 decl);
13894 return false;
13895 }
13896
13897 if (!TREE_PUBLIC (decl))
13898 {
13899 error_at (loc, "%qD may not be declared as static", decl);
13900 return false;
13901 }
13902 }
13903
13904 if (op_flags & OVL_OP_FLAG_DELETE)
13905 {
13906 DECL_SET_IS_OPERATOR_DELETE (decl, true);
13907 coerce_delete_type (decl, loc);
13908 }
13909 else
13910 {
13911 DECL_SET_IS_OPERATOR_NEW (decl, true);
13912 TREE_TYPE (decl) = coerce_new_type (TREE_TYPE (decl), loc);
13913 }
13914
13915 return true;
13916 }
13917
13918 /* An operator function must either be a non-static member function
13919 or have at least one parameter of a class, a reference to a class,
13920 an enumeration, or a reference to an enumeration. 13.4.0.6 */
13921 if (! methodp || DECL_STATIC_FUNCTION_P (decl))
13922 {
13923 if (operator_code == TYPE_EXPR
13924 || operator_code == CALL_EXPR
13925 || operator_code == COMPONENT_REF
13926 || operator_code == ARRAY_REF
13927 || operator_code == NOP_EXPR)
13928 {
13929 error_at (loc, "%qD must be a nonstatic member function", decl);
13930 return false;
13931 }
13932
13933 if (DECL_STATIC_FUNCTION_P (decl))
13934 {
13935 error_at (loc, "%qD must be either a non-static member "
13936 "function or a non-member function", decl);
13937 return false;
13938 }
13939
13940 for (tree arg = argtypes; ; arg = TREE_CHAIN (arg))
13941 {
13942 if (!arg || arg == void_list_node)
13943 {
13944 if (complain)
13945 error_at(loc, "%qD must have an argument of class or "
13946 "enumerated type", decl);
13947 return false;
13948 }
13949
13950 tree type = non_reference (TREE_VALUE (arg));
13951 if (type == error_mark_node)
13952 return false;
13953
13954 /* MAYBE_CLASS_TYPE_P, rather than CLASS_TYPE_P, is used
13955 because these checks are performed even on template
13956 functions. */
13957 if (MAYBE_CLASS_TYPE_P (type)
13958 || TREE_CODE (type) == ENUMERAL_TYPE)
13959 break;
13960 }
13961 }
13962
13963 if (operator_code == CALL_EXPR)
13964 /* There are no further restrictions on the arguments to an overloaded
13965 "operator ()". */
13966 return true;
13967
13968 if (operator_code == COND_EXPR)
13969 {
13970 /* 13.4.0.3 */
13971 error_at (loc, "ISO C++ prohibits overloading %<operator ?:%>");
13972 return false;
13973 }
13974
13975 /* Count the number of arguments and check for ellipsis. */
13976 int arity = 0;
13977 for (tree arg = argtypes; arg != void_list_node; arg = TREE_CHAIN (arg))
13978 {
13979 if (!arg)
13980 {
13981 /* Variadic. */
13982 error_at (loc, "%qD must not have variable number of arguments",
13983 decl);
13984 return false;
13985 }
13986 ++arity;
13987 }
13988
13989 /* Verify correct number of arguments. */
13990 switch (op_flags)
13991 {
13992 case OVL_OP_FLAG_AMBIARY:
13993 if (arity == 1)
13994 {
13995 /* We have a unary instance of an ambi-ary op. Remap to the
13996 unary one. */
13997 unsigned alt = ovl_op_alternate[ovl_op_mapping [operator_code]];
13998 const ovl_op_info_t *ovl_op = &ovl_op_info[false][alt];
13999 gcc_checking_assert (ovl_op->flags == OVL_OP_FLAG_UNARY);
14000 operator_code = ovl_op->tree_code;
14001 DECL_OVERLOADED_OPERATOR_CODE_RAW (decl) = ovl_op->ovl_op_code;
14002 }
14003 else if (arity != 2)
14004 {
14005 /* This was an ambiguous operator but is invalid. */
14006 error_at (loc,
14007 methodp
14008 ? G_("%qD must have either zero or one argument")
14009 : G_("%qD must have either one or two arguments"), decl);
14010 return false;
14011 }
14012 else if ((operator_code == POSTINCREMENT_EXPR
14013 || operator_code == POSTDECREMENT_EXPR)
14014 && ! processing_template_decl
14015 /* x++ and x--'s second argument must be an int. */
14016 && ! same_type_p (TREE_VALUE (TREE_CHAIN (argtypes)),
14017 integer_type_node))
14018 {
14019 error_at (loc,
14020 methodp
14021 ? G_("postfix %qD must have %<int%> as its argument")
14022 : G_("postfix %qD must have %<int%> as its second argument"),
14023 decl);
14024 return false;
14025 }
14026 break;
14027
14028 case OVL_OP_FLAG_UNARY:
14029 if (arity != 1)
14030 {
14031 error_at (loc,
14032 methodp
14033 ? G_("%qD must have no arguments")
14034 : G_("%qD must have exactly one argument"), decl);
14035 return false;
14036 }
14037 break;
14038
14039 case OVL_OP_FLAG_BINARY:
14040 if (arity != 2)
14041 {
14042 error_at (loc,
14043 methodp
14044 ? G_("%qD must have exactly one argument")
14045 : G_("%qD must have exactly two arguments"), decl);
14046 return false;
14047 }
14048 break;
14049
14050 default:
14051 gcc_unreachable ();
14052 }
14053
14054 /* There can be no default arguments. */
14055 for (tree arg = argtypes; arg != void_list_node; arg = TREE_CHAIN (arg))
14056 if (TREE_PURPOSE (arg))
14057 {
14058 TREE_PURPOSE (arg) = NULL_TREE;
14059 error_at (loc, "%qD cannot have default arguments", decl);
14060 return false;
14061 }
14062
14063 /* At this point the declaration is well-formed. It may not be
14064 sensible though. */
14065
14066 /* Check member function warnings only on the in-class declaration.
14067 There's no point warning on an out-of-class definition. */
14068 if (class_type && class_type != current_class_type)
14069 return true;
14070
14071 /* Warn about conversion operators that will never be used. */
14072 if (IDENTIFIER_CONV_OP_P (name)
14073 && ! DECL_TEMPLATE_INFO (decl)
14074 && warn_class_conversion)
14075 {
14076 tree t = TREE_TYPE (name);
14077 int ref = TYPE_REF_P (t);
14078
14079 if (ref)
14080 t = TYPE_MAIN_VARIANT (TREE_TYPE (t));
14081
14082 if (VOID_TYPE_P (t))
14083 warning_at (loc, OPT_Wclass_conversion, "converting %qT to %<void%> "
14084 "will never use a type conversion operator", class_type);
14085 else if (class_type)
14086 {
14087 if (same_type_ignoring_top_level_qualifiers_p (t, class_type))
14088 warning_at (loc, OPT_Wclass_conversion,
14089 ref
14090 ? G_("converting %qT to a reference to the same type "
14091 "will never use a type conversion operator")
14092 : G_("converting %qT to the same type "
14093 "will never use a type conversion operator"),
14094 class_type);
14095 /* Don't force t to be complete here. */
14096 else if (MAYBE_CLASS_TYPE_P (t)
14097 && COMPLETE_TYPE_P (t)
14098 && DERIVED_FROM_P (t, class_type))
14099 warning_at (loc, OPT_Wclass_conversion,
14100 ref
14101 ? G_("converting %qT to a reference to a base class "
14102 "%qT will never use a type conversion operator")
14103 : G_("converting %qT to a base class %qT "
14104 "will never use a type conversion operator"),
14105 class_type, t);
14106 }
14107 }
14108
14109 if (!warn_ecpp)
14110 return true;
14111
14112 /* Effective C++ rules below. */
14113
14114 /* More Effective C++ rule 7. */
14115 if (operator_code == TRUTH_ANDIF_EXPR
14116 || operator_code == TRUTH_ORIF_EXPR
14117 || operator_code == COMPOUND_EXPR)
14118 warning_at (loc, OPT_Weffc__,
14119 "user-defined %qD always evaluates both arguments", decl);
14120
14121 /* More Effective C++ rule 6. */
14122 if (operator_code == POSTINCREMENT_EXPR
14123 || operator_code == POSTDECREMENT_EXPR
14124 || operator_code == PREINCREMENT_EXPR
14125 || operator_code == PREDECREMENT_EXPR)
14126 {
14127 tree arg = TREE_VALUE (argtypes);
14128 tree ret = TREE_TYPE (TREE_TYPE (decl));
14129 if (methodp || TYPE_REF_P (arg))
14130 arg = TREE_TYPE (arg);
14131 arg = TYPE_MAIN_VARIANT (arg);
14132
14133 if (operator_code == PREINCREMENT_EXPR
14134 || operator_code == PREDECREMENT_EXPR)
14135 {
14136 if (!TYPE_REF_P (ret)
14137 || !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (ret)), arg))
14138 warning_at (loc, OPT_Weffc__, "prefix %qD should return %qT", decl,
14139 build_reference_type (arg));
14140 }
14141 else
14142 {
14143 if (!same_type_p (TYPE_MAIN_VARIANT (ret), arg))
14144 warning_at (loc, OPT_Weffc__,
14145 "postfix %qD should return %qT", decl, arg);
14146 }
14147 }
14148
14149 /* Effective C++ rule 23. */
14150 if (!DECL_ASSIGNMENT_OPERATOR_P (decl)
14151 && (operator_code == PLUS_EXPR
14152 || operator_code == MINUS_EXPR
14153 || operator_code == TRUNC_DIV_EXPR
14154 || operator_code == MULT_EXPR
14155 || operator_code == TRUNC_MOD_EXPR)
14156 && TYPE_REF_P (TREE_TYPE (TREE_TYPE (decl))))
14157 warning_at (loc, OPT_Weffc__, "%qD should return by value", decl);
14158
14159 return true;
14160 }
14161 \f
14162 /* Return a string giving the keyword associate with CODE. */
14163
14164 static const char *
14165 tag_name (enum tag_types code)
14166 {
14167 switch (code)
14168 {
14169 case record_type:
14170 return "struct";
14171 case class_type:
14172 return "class";
14173 case union_type:
14174 return "union";
14175 case enum_type:
14176 return "enum";
14177 case typename_type:
14178 return "typename";
14179 default:
14180 gcc_unreachable ();
14181 }
14182 }
14183
14184 /* Name lookup in an elaborated-type-specifier (after the keyword
14185 indicated by TAG_CODE) has found the TYPE_DECL DECL. If the
14186 elaborated-type-specifier is invalid, issue a diagnostic and return
14187 error_mark_node; otherwise, return the *_TYPE to which it referred.
14188 If ALLOW_TEMPLATE_P is true, TYPE may be a class template. */
14189
14190 tree
14191 check_elaborated_type_specifier (enum tag_types tag_code,
14192 tree decl,
14193 bool allow_template_p)
14194 {
14195 tree type;
14196
14197 /* In the case of:
14198
14199 struct S { struct S *p; };
14200
14201 name lookup will find the TYPE_DECL for the implicit "S::S"
14202 typedef. Adjust for that here. */
14203 if (DECL_SELF_REFERENCE_P (decl))
14204 decl = TYPE_NAME (TREE_TYPE (decl));
14205
14206 type = TREE_TYPE (decl);
14207
14208 /* Check TEMPLATE_TYPE_PARM first because DECL_IMPLICIT_TYPEDEF_P
14209 is false for this case as well. */
14210 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
14211 {
14212 error ("using template type parameter %qT after %qs",
14213 type, tag_name (tag_code));
14214 return error_mark_node;
14215 }
14216 /* Accept template template parameters. */
14217 else if (allow_template_p
14218 && (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM
14219 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM))
14220 ;
14221 /* [dcl.type.elab]
14222
14223 If the identifier resolves to a typedef-name or the
14224 simple-template-id resolves to an alias template
14225 specialization, the elaborated-type-specifier is ill-formed.
14226
14227 In other words, the only legitimate declaration to use in the
14228 elaborated type specifier is the implicit typedef created when
14229 the type is declared. */
14230 else if (!DECL_IMPLICIT_TYPEDEF_P (decl)
14231 && !DECL_SELF_REFERENCE_P (decl)
14232 && tag_code != typename_type)
14233 {
14234 if (alias_template_specialization_p (type))
14235 error ("using alias template specialization %qT after %qs",
14236 type, tag_name (tag_code));
14237 else
14238 error ("using typedef-name %qD after %qs", decl, tag_name (tag_code));
14239 inform (DECL_SOURCE_LOCATION (decl),
14240 "%qD has a previous declaration here", decl);
14241 return error_mark_node;
14242 }
14243 else if (TREE_CODE (type) != RECORD_TYPE
14244 && TREE_CODE (type) != UNION_TYPE
14245 && tag_code != enum_type
14246 && tag_code != typename_type)
14247 {
14248 error ("%qT referred to as %qs", type, tag_name (tag_code));
14249 inform (location_of (type), "%qT has a previous declaration here", type);
14250 return error_mark_node;
14251 }
14252 else if (TREE_CODE (type) != ENUMERAL_TYPE
14253 && tag_code == enum_type)
14254 {
14255 error ("%qT referred to as enum", type);
14256 inform (location_of (type), "%qT has a previous declaration here", type);
14257 return error_mark_node;
14258 }
14259 else if (!allow_template_p
14260 && TREE_CODE (type) == RECORD_TYPE
14261 && CLASSTYPE_IS_TEMPLATE (type))
14262 {
14263 /* If a class template appears as elaborated type specifier
14264 without a template header such as:
14265
14266 template <class T> class C {};
14267 void f(class C); // No template header here
14268
14269 then the required template argument is missing. */
14270 error ("template argument required for %<%s %T%>",
14271 tag_name (tag_code),
14272 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)));
14273 return error_mark_node;
14274 }
14275
14276 return type;
14277 }
14278
14279 /* Lookup NAME in elaborate type specifier in scope according to
14280 SCOPE and issue diagnostics if necessary.
14281 Return *_TYPE node upon success, NULL_TREE when the NAME is not
14282 found, and ERROR_MARK_NODE for type error. */
14283
14284 static tree
14285 lookup_and_check_tag (enum tag_types tag_code, tree name,
14286 tag_scope scope, bool template_header_p)
14287 {
14288 tree t;
14289 tree decl;
14290 if (scope == ts_global)
14291 {
14292 /* First try ordinary name lookup, ignoring hidden class name
14293 injected via friend declaration. */
14294 decl = lookup_name_prefer_type (name, 2);
14295 decl = strip_using_decl (decl);
14296 /* If that fails, the name will be placed in the smallest
14297 non-class, non-function-prototype scope according to 3.3.1/5.
14298 We may already have a hidden name declared as friend in this
14299 scope. So lookup again but not ignoring hidden names.
14300 If we find one, that name will be made visible rather than
14301 creating a new tag. */
14302 if (!decl)
14303 decl = lookup_type_scope (name, ts_within_enclosing_non_class);
14304 }
14305 else
14306 decl = lookup_type_scope (name, scope);
14307
14308 if (decl
14309 && (DECL_CLASS_TEMPLATE_P (decl)
14310 /* If scope is ts_current we're defining a class, so ignore a
14311 template template parameter. */
14312 || (scope != ts_current
14313 && DECL_TEMPLATE_TEMPLATE_PARM_P (decl))))
14314 decl = DECL_TEMPLATE_RESULT (decl);
14315
14316 if (decl && TREE_CODE (decl) == TYPE_DECL)
14317 {
14318 /* Look for invalid nested type:
14319 class C {
14320 class C {};
14321 }; */
14322 if (scope == ts_current && DECL_SELF_REFERENCE_P (decl))
14323 {
14324 error ("%qD has the same name as the class in which it is "
14325 "declared",
14326 decl);
14327 return error_mark_node;
14328 }
14329
14330 /* Two cases we need to consider when deciding if a class
14331 template is allowed as an elaborated type specifier:
14332 1. It is a self reference to its own class.
14333 2. It comes with a template header.
14334
14335 For example:
14336
14337 template <class T> class C {
14338 class C *c1; // DECL_SELF_REFERENCE_P is true
14339 class D;
14340 };
14341 template <class U> class C; // template_header_p is true
14342 template <class T> class C<T>::D {
14343 class C *c2; // DECL_SELF_REFERENCE_P is true
14344 }; */
14345
14346 t = check_elaborated_type_specifier (tag_code,
14347 decl,
14348 template_header_p
14349 | DECL_SELF_REFERENCE_P (decl));
14350 if (template_header_p && t && CLASS_TYPE_P (t)
14351 && (!CLASSTYPE_TEMPLATE_INFO (t)
14352 || (!PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)))))
14353 {
14354 error ("%qT is not a template", t);
14355 inform (location_of (t), "previous declaration here");
14356 if (TYPE_CLASS_SCOPE_P (t)
14357 && CLASSTYPE_TEMPLATE_INFO (TYPE_CONTEXT (t)))
14358 inform (input_location,
14359 "perhaps you want to explicitly add %<%T::%>",
14360 TYPE_CONTEXT (t));
14361 t = error_mark_node;
14362 }
14363
14364 return t;
14365 }
14366 else if (decl && TREE_CODE (decl) == TREE_LIST)
14367 {
14368 error ("reference to %qD is ambiguous", name);
14369 print_candidates (decl);
14370 return error_mark_node;
14371 }
14372 else
14373 return NULL_TREE;
14374 }
14375
14376 /* Get the struct, enum or union (TAG_CODE says which) with tag NAME.
14377 Define the tag as a forward-reference if it is not defined.
14378
14379 If a declaration is given, process it here, and report an error if
14380 multiple declarations are not identical.
14381
14382 SCOPE is TS_CURRENT when this is also a definition. Only look in
14383 the current frame for the name (since C++ allows new names in any
14384 scope.) It is TS_WITHIN_ENCLOSING_NON_CLASS if this is a friend
14385 declaration. Only look beginning from the current scope outward up
14386 till the nearest non-class scope. Otherwise it is TS_GLOBAL.
14387
14388 TEMPLATE_HEADER_P is true when this declaration is preceded by
14389 a set of template parameters. */
14390
14391 static tree
14392 xref_tag_1 (enum tag_types tag_code, tree name,
14393 tag_scope scope, bool template_header_p)
14394 {
14395 enum tree_code code;
14396 tree context = NULL_TREE;
14397
14398 gcc_assert (identifier_p (name));
14399
14400 switch (tag_code)
14401 {
14402 case record_type:
14403 case class_type:
14404 code = RECORD_TYPE;
14405 break;
14406 case union_type:
14407 code = UNION_TYPE;
14408 break;
14409 case enum_type:
14410 code = ENUMERAL_TYPE;
14411 break;
14412 default:
14413 gcc_unreachable ();
14414 }
14415
14416 /* In case of anonymous name, xref_tag is only called to
14417 make type node and push name. Name lookup is not required. */
14418 tree t = NULL_TREE;
14419 if (scope != ts_lambda && !IDENTIFIER_ANON_P (name))
14420 t = lookup_and_check_tag (tag_code, name, scope, template_header_p);
14421
14422 if (t == error_mark_node)
14423 return error_mark_node;
14424
14425 if (scope != ts_current && t && current_class_type
14426 && template_class_depth (current_class_type)
14427 && template_header_p)
14428 {
14429 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
14430 return t;
14431
14432 /* Since SCOPE is not TS_CURRENT, we are not looking at a
14433 definition of this tag. Since, in addition, we are currently
14434 processing a (member) template declaration of a template
14435 class, we must be very careful; consider:
14436
14437 template <class X> struct S1
14438
14439 template <class U> struct S2
14440 {
14441 template <class V> friend struct S1;
14442 };
14443
14444 Here, the S2::S1 declaration should not be confused with the
14445 outer declaration. In particular, the inner version should
14446 have a template parameter of level 2, not level 1.
14447
14448 On the other hand, when presented with:
14449
14450 template <class T> struct S1
14451 {
14452 template <class U> struct S2 {};
14453 template <class U> friend struct S2;
14454 };
14455
14456 the friend must find S1::S2 eventually. We accomplish this
14457 by making sure that the new type we create to represent this
14458 declaration has the right TYPE_CONTEXT. */
14459 context = TYPE_CONTEXT (t);
14460 t = NULL_TREE;
14461 }
14462
14463 if (! t)
14464 {
14465 /* If no such tag is yet defined, create a forward-reference node
14466 and record it as the "definition".
14467 When a real declaration of this type is found,
14468 the forward-reference will be altered into a real type. */
14469 if (code == ENUMERAL_TYPE)
14470 {
14471 error ("use of enum %q#D without previous declaration", name);
14472 return error_mark_node;
14473 }
14474 else
14475 {
14476 t = make_class_type (code);
14477 TYPE_CONTEXT (t) = context;
14478 if (scope == ts_lambda)
14479 {
14480 /* Mark it as a lambda type. */
14481 CLASSTYPE_LAMBDA_EXPR (t) = error_mark_node;
14482 /* And push it into current scope. */
14483 scope = ts_current;
14484 }
14485 t = pushtag (name, t, scope);
14486 }
14487 }
14488 else
14489 {
14490 if (template_header_p && MAYBE_CLASS_TYPE_P (t))
14491 {
14492 /* Check that we aren't trying to overload a class with different
14493 constraints. */
14494 tree constr = NULL_TREE;
14495 if (current_template_parms)
14496 {
14497 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
14498 constr = build_constraints (reqs, NULL_TREE);
14499 }
14500 if (!redeclare_class_template (t, current_template_parms, constr))
14501 return error_mark_node;
14502 }
14503 else if (!processing_template_decl
14504 && CLASS_TYPE_P (t)
14505 && CLASSTYPE_IS_TEMPLATE (t))
14506 {
14507 error ("redeclaration of %qT as a non-template", t);
14508 inform (location_of (t), "previous declaration %qD", t);
14509 return error_mark_node;
14510 }
14511
14512 if (scope != ts_within_enclosing_non_class && TYPE_HIDDEN_P (t))
14513 {
14514 /* This is no longer an invisible friend. Make it
14515 visible. */
14516 tree decl = TYPE_NAME (t);
14517
14518 DECL_ANTICIPATED (decl) = false;
14519 DECL_FRIEND_P (decl) = false;
14520
14521 if (TYPE_TEMPLATE_INFO (t))
14522 {
14523 tree tmpl = TYPE_TI_TEMPLATE (t);
14524 DECL_ANTICIPATED (tmpl) = false;
14525 DECL_FRIEND_P (tmpl) = false;
14526 }
14527 }
14528 }
14529
14530 return t;
14531 }
14532
14533 /* Wrapper for xref_tag_1. */
14534
14535 tree
14536 xref_tag (enum tag_types tag_code, tree name,
14537 tag_scope scope, bool template_header_p)
14538 {
14539 tree ret;
14540 bool subtime;
14541 subtime = timevar_cond_start (TV_NAME_LOOKUP);
14542 ret = xref_tag_1 (tag_code, name, scope, template_header_p);
14543 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
14544 return ret;
14545 }
14546
14547
14548 tree
14549 xref_tag_from_type (tree old, tree id, tag_scope scope)
14550 {
14551 enum tag_types tag_kind;
14552
14553 if (TREE_CODE (old) == RECORD_TYPE)
14554 tag_kind = (CLASSTYPE_DECLARED_CLASS (old) ? class_type : record_type);
14555 else
14556 tag_kind = union_type;
14557
14558 if (id == NULL_TREE)
14559 id = TYPE_IDENTIFIER (old);
14560
14561 return xref_tag (tag_kind, id, scope, false);
14562 }
14563
14564 /* Create the binfo hierarchy for REF with (possibly NULL) base list
14565 BASE_LIST. For each element on BASE_LIST the TREE_PURPOSE is an
14566 access_* node, and the TREE_VALUE is the type of the base-class.
14567 Non-NULL TREE_TYPE indicates virtual inheritance. */
14568
14569 void
14570 xref_basetypes (tree ref, tree base_list)
14571 {
14572 tree *basep;
14573 tree binfo, base_binfo;
14574 unsigned max_vbases = 0; /* Maximum direct & indirect virtual bases. */
14575 unsigned max_bases = 0; /* Maximum direct bases. */
14576 unsigned max_dvbases = 0; /* Maximum direct virtual bases. */
14577 int i;
14578 tree default_access;
14579 tree igo_prev; /* Track Inheritance Graph Order. */
14580
14581 if (ref == error_mark_node)
14582 return;
14583
14584 /* The base of a derived class is private by default, all others are
14585 public. */
14586 default_access = (TREE_CODE (ref) == RECORD_TYPE
14587 && CLASSTYPE_DECLARED_CLASS (ref)
14588 ? access_private_node : access_public_node);
14589
14590 /* First, make sure that any templates in base-classes are
14591 instantiated. This ensures that if we call ourselves recursively
14592 we do not get confused about which classes are marked and which
14593 are not. */
14594 basep = &base_list;
14595 while (*basep)
14596 {
14597 tree basetype = TREE_VALUE (*basep);
14598
14599 /* The dependent_type_p call below should really be dependent_scope_p
14600 so that we give a hard error about using an incomplete type as a
14601 base, but we allow it with a pedwarn for backward
14602 compatibility. */
14603 if (processing_template_decl
14604 && CLASS_TYPE_P (basetype) && TYPE_BEING_DEFINED (basetype))
14605 cxx_incomplete_type_diagnostic (NULL_TREE, basetype, DK_PEDWARN);
14606 if (!dependent_type_p (basetype)
14607 && !complete_type_or_else (basetype, NULL))
14608 /* An incomplete type. Remove it from the list. */
14609 *basep = TREE_CHAIN (*basep);
14610 else
14611 {
14612 max_bases++;
14613 if (TREE_TYPE (*basep))
14614 max_dvbases++;
14615 if (CLASS_TYPE_P (basetype))
14616 max_vbases += vec_safe_length (CLASSTYPE_VBASECLASSES (basetype));
14617 basep = &TREE_CHAIN (*basep);
14618 }
14619 }
14620 max_vbases += max_dvbases;
14621
14622 TYPE_MARKED_P (ref) = 1;
14623
14624 /* The binfo slot should be empty, unless this is an (ill-formed)
14625 redefinition. */
14626 gcc_assert (!TYPE_BINFO (ref) || TYPE_SIZE (ref));
14627
14628 gcc_assert (TYPE_MAIN_VARIANT (ref) == ref);
14629
14630 binfo = make_tree_binfo (max_bases);
14631
14632 TYPE_BINFO (ref) = binfo;
14633 BINFO_OFFSET (binfo) = size_zero_node;
14634 BINFO_TYPE (binfo) = ref;
14635
14636 /* Apply base-class info set up to the variants of this type. */
14637 fixup_type_variants (ref);
14638
14639 if (max_bases)
14640 {
14641 vec_alloc (BINFO_BASE_ACCESSES (binfo), max_bases);
14642 /* A C++98 POD cannot have base classes. */
14643 CLASSTYPE_NON_LAYOUT_POD_P (ref) = true;
14644
14645 if (TREE_CODE (ref) == UNION_TYPE)
14646 {
14647 error ("derived union %qT invalid", ref);
14648 return;
14649 }
14650 }
14651
14652 if (max_bases > 1)
14653 warning (OPT_Wmultiple_inheritance,
14654 "%qT defined with multiple direct bases", ref);
14655
14656 if (max_vbases)
14657 {
14658 /* An aggregate can't have virtual base classes. */
14659 CLASSTYPE_NON_AGGREGATE (ref) = true;
14660
14661 vec_alloc (CLASSTYPE_VBASECLASSES (ref), max_vbases);
14662
14663 if (max_dvbases)
14664 warning (OPT_Wvirtual_inheritance,
14665 "%qT defined with direct virtual base", ref);
14666 }
14667
14668 for (igo_prev = binfo; base_list; base_list = TREE_CHAIN (base_list))
14669 {
14670 tree access = TREE_PURPOSE (base_list);
14671 int via_virtual = TREE_TYPE (base_list) != NULL_TREE;
14672 tree basetype = TREE_VALUE (base_list);
14673
14674 if (access == access_default_node)
14675 access = default_access;
14676
14677 /* Before C++17, an aggregate cannot have base classes. In C++17, an
14678 aggregate can't have virtual, private, or protected base classes. */
14679 if (cxx_dialect < cxx17
14680 || access != access_public_node
14681 || via_virtual)
14682 CLASSTYPE_NON_AGGREGATE (ref) = true;
14683
14684 if (PACK_EXPANSION_P (basetype))
14685 basetype = PACK_EXPANSION_PATTERN (basetype);
14686 if (TREE_CODE (basetype) == TYPE_DECL)
14687 basetype = TREE_TYPE (basetype);
14688 if (!MAYBE_CLASS_TYPE_P (basetype) || TREE_CODE (basetype) == UNION_TYPE)
14689 {
14690 error ("base type %qT fails to be a struct or class type",
14691 basetype);
14692 goto dropped_base;
14693 }
14694
14695 base_binfo = NULL_TREE;
14696 if (CLASS_TYPE_P (basetype) && !dependent_scope_p (basetype))
14697 {
14698 base_binfo = TYPE_BINFO (basetype);
14699 /* The original basetype could have been a typedef'd type. */
14700 basetype = BINFO_TYPE (base_binfo);
14701
14702 /* Inherit flags from the base. */
14703 TYPE_HAS_NEW_OPERATOR (ref)
14704 |= TYPE_HAS_NEW_OPERATOR (basetype);
14705 TYPE_HAS_ARRAY_NEW_OPERATOR (ref)
14706 |= TYPE_HAS_ARRAY_NEW_OPERATOR (basetype);
14707 TYPE_GETS_DELETE (ref) |= TYPE_GETS_DELETE (basetype);
14708 TYPE_HAS_CONVERSION (ref) |= TYPE_HAS_CONVERSION (basetype);
14709 CLASSTYPE_DIAMOND_SHAPED_P (ref)
14710 |= CLASSTYPE_DIAMOND_SHAPED_P (basetype);
14711 CLASSTYPE_REPEATED_BASE_P (ref)
14712 |= CLASSTYPE_REPEATED_BASE_P (basetype);
14713 }
14714
14715 /* We must do this test after we've seen through a typedef
14716 type. */
14717 if (TYPE_MARKED_P (basetype))
14718 {
14719 if (basetype == ref)
14720 error ("recursive type %qT undefined", basetype);
14721 else
14722 error ("duplicate base type %qT invalid", basetype);
14723 goto dropped_base;
14724 }
14725
14726 if (PACK_EXPANSION_P (TREE_VALUE (base_list)))
14727 /* Regenerate the pack expansion for the bases. */
14728 basetype = make_pack_expansion (basetype);
14729
14730 TYPE_MARKED_P (basetype) = 1;
14731
14732 base_binfo = copy_binfo (base_binfo, basetype, ref,
14733 &igo_prev, via_virtual);
14734 if (!BINFO_INHERITANCE_CHAIN (base_binfo))
14735 BINFO_INHERITANCE_CHAIN (base_binfo) = binfo;
14736
14737 BINFO_BASE_APPEND (binfo, base_binfo);
14738 BINFO_BASE_ACCESS_APPEND (binfo, access);
14739 continue;
14740
14741 dropped_base:
14742 /* Update max_vbases to reflect the reality that we are dropping
14743 this base: if it reaches zero we want to undo the vec_alloc
14744 above to avoid inconsistencies during error-recovery: eg, in
14745 build_special_member_call, CLASSTYPE_VBASECLASSES non null
14746 and vtt null (c++/27952). */
14747 if (via_virtual)
14748 max_vbases--;
14749 if (CLASS_TYPE_P (basetype))
14750 max_vbases
14751 -= vec_safe_length (CLASSTYPE_VBASECLASSES (basetype));
14752 }
14753
14754 if (CLASSTYPE_VBASECLASSES (ref)
14755 && max_vbases == 0)
14756 vec_free (CLASSTYPE_VBASECLASSES (ref));
14757
14758 if (vec_safe_length (CLASSTYPE_VBASECLASSES (ref)) < max_vbases)
14759 /* If we didn't get max_vbases vbases, we must have shared at
14760 least one of them, and are therefore diamond shaped. */
14761 CLASSTYPE_DIAMOND_SHAPED_P (ref) = 1;
14762
14763 /* Unmark all the types. */
14764 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
14765 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
14766 TYPE_MARKED_P (ref) = 0;
14767
14768 /* Now see if we have a repeated base type. */
14769 if (!CLASSTYPE_REPEATED_BASE_P (ref))
14770 {
14771 for (base_binfo = binfo; base_binfo;
14772 base_binfo = TREE_CHAIN (base_binfo))
14773 {
14774 if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
14775 {
14776 CLASSTYPE_REPEATED_BASE_P (ref) = 1;
14777 break;
14778 }
14779 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 1;
14780 }
14781 for (base_binfo = binfo; base_binfo;
14782 base_binfo = TREE_CHAIN (base_binfo))
14783 if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
14784 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
14785 else
14786 break;
14787 }
14788 }
14789
14790 \f
14791 /* Copies the enum-related properties from type SRC to type DST.
14792 Used with the underlying type of an enum and the enum itself. */
14793 static void
14794 copy_type_enum (tree dst, tree src)
14795 {
14796 tree t;
14797 for (t = dst; t; t = TYPE_NEXT_VARIANT (t))
14798 {
14799 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (src);
14800 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (src);
14801 TYPE_SIZE (t) = TYPE_SIZE (src);
14802 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (src);
14803 SET_TYPE_MODE (dst, TYPE_MODE (src));
14804 TYPE_PRECISION (t) = TYPE_PRECISION (src);
14805 unsigned valign = TYPE_ALIGN (src);
14806 if (TYPE_USER_ALIGN (t))
14807 valign = MAX (valign, TYPE_ALIGN (t));
14808 else
14809 TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (src);
14810 SET_TYPE_ALIGN (t, valign);
14811 TYPE_UNSIGNED (t) = TYPE_UNSIGNED (src);
14812 }
14813 }
14814
14815 /* Begin compiling the definition of an enumeration type.
14816 NAME is its name,
14817
14818 if ENUMTYPE is not NULL_TREE then the type has alredy been found.
14819
14820 UNDERLYING_TYPE is the type that will be used as the storage for
14821 the enumeration type. This should be NULL_TREE if no storage type
14822 was specified.
14823
14824 ATTRIBUTES are any attributes specified after the enum-key.
14825
14826 SCOPED_ENUM_P is true if this is a scoped enumeration type.
14827
14828 if IS_NEW is not NULL, gets TRUE iff a new type is created.
14829
14830 Returns the type object, as yet incomplete.
14831 Also records info about it so that build_enumerator
14832 may be used to declare the individual values as they are read. */
14833
14834 tree
14835 start_enum (tree name, tree enumtype, tree underlying_type,
14836 tree attributes, bool scoped_enum_p, bool *is_new)
14837 {
14838 tree prevtype = NULL_TREE;
14839 gcc_assert (identifier_p (name));
14840
14841 if (is_new)
14842 *is_new = false;
14843 /* [C++0x dcl.enum]p5:
14844
14845 If not explicitly specified, the underlying type of a scoped
14846 enumeration type is int. */
14847 if (!underlying_type && scoped_enum_p)
14848 underlying_type = integer_type_node;
14849
14850 if (underlying_type)
14851 underlying_type = cv_unqualified (underlying_type);
14852
14853 /* If this is the real definition for a previous forward reference,
14854 fill in the contents in the same object that used to be the
14855 forward reference. */
14856 if (!enumtype)
14857 enumtype = lookup_and_check_tag (enum_type, name,
14858 /*tag_scope=*/ts_current,
14859 /*template_header_p=*/false);
14860
14861 /* In case of a template_decl, the only check that should be deferred
14862 to instantiation time is the comparison of underlying types. */
14863 if (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE)
14864 {
14865 if (scoped_enum_p != SCOPED_ENUM_P (enumtype))
14866 {
14867 error_at (input_location, "scoped/unscoped mismatch "
14868 "in enum %q#T", enumtype);
14869 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
14870 "previous definition here");
14871 enumtype = error_mark_node;
14872 }
14873 else if (ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) != !! underlying_type)
14874 {
14875 error_at (input_location, "underlying type mismatch "
14876 "in enum %q#T", enumtype);
14877 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
14878 "previous definition here");
14879 enumtype = error_mark_node;
14880 }
14881 else if (underlying_type && ENUM_UNDERLYING_TYPE (enumtype)
14882 && !same_type_p (underlying_type,
14883 ENUM_UNDERLYING_TYPE (enumtype)))
14884 {
14885 error_at (input_location, "different underlying type "
14886 "in enum %q#T", enumtype);
14887 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
14888 "previous definition here");
14889 underlying_type = NULL_TREE;
14890 }
14891 }
14892
14893 if (!enumtype || TREE_CODE (enumtype) != ENUMERAL_TYPE
14894 || processing_template_decl)
14895 {
14896 /* In case of error, make a dummy enum to allow parsing to
14897 continue. */
14898 if (enumtype == error_mark_node)
14899 {
14900 name = make_anon_name ();
14901 enumtype = NULL_TREE;
14902 }
14903
14904 /* enumtype may be an ENUMERAL_TYPE if this is a redefinition
14905 of an opaque enum, or an opaque enum of an already defined
14906 enumeration (C++11).
14907 In any other case, it'll be NULL_TREE. */
14908 if (!enumtype)
14909 {
14910 if (is_new)
14911 *is_new = true;
14912 }
14913 prevtype = enumtype;
14914
14915 /* Do not push the decl more than once. */
14916 if (!enumtype
14917 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
14918 {
14919 enumtype = cxx_make_type (ENUMERAL_TYPE);
14920 enumtype = pushtag (name, enumtype, /*tag_scope=*/ts_current);
14921
14922 /* std::byte aliases anything. */
14923 if (enumtype != error_mark_node
14924 && TYPE_CONTEXT (enumtype) == std_node
14925 && !strcmp ("byte", TYPE_NAME_STRING (enumtype)))
14926 TYPE_ALIAS_SET (enumtype) = 0;
14927 }
14928 else
14929 enumtype = xref_tag (enum_type, name, /*tag_scope=*/ts_current,
14930 false);
14931
14932 if (enumtype == error_mark_node)
14933 return error_mark_node;
14934
14935 /* The enum is considered opaque until the opening '{' of the
14936 enumerator list. */
14937 SET_OPAQUE_ENUM_P (enumtype, true);
14938 ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) = !! underlying_type;
14939 }
14940
14941 SET_SCOPED_ENUM_P (enumtype, scoped_enum_p);
14942
14943 cplus_decl_attributes (&enumtype, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
14944
14945 if (underlying_type)
14946 {
14947 if (ENUM_UNDERLYING_TYPE (enumtype))
14948 /* We already checked that it matches, don't change it to a different
14949 typedef variant. */;
14950 else if (CP_INTEGRAL_TYPE_P (underlying_type))
14951 {
14952 copy_type_enum (enumtype, underlying_type);
14953 ENUM_UNDERLYING_TYPE (enumtype) = underlying_type;
14954 }
14955 else if (dependent_type_p (underlying_type))
14956 ENUM_UNDERLYING_TYPE (enumtype) = underlying_type;
14957 else
14958 error ("underlying type %qT of %qT must be an integral type",
14959 underlying_type, enumtype);
14960 }
14961
14962 /* If into a template class, the returned enum is always the first
14963 declaration (opaque or not) seen. This way all the references to
14964 this type will be to the same declaration. The following ones are used
14965 only to check for definition errors. */
14966 if (prevtype && processing_template_decl)
14967 return prevtype;
14968 else
14969 return enumtype;
14970 }
14971
14972 /* After processing and defining all the values of an enumeration type,
14973 install their decls in the enumeration type.
14974 ENUMTYPE is the type object. */
14975
14976 void
14977 finish_enum_value_list (tree enumtype)
14978 {
14979 tree values;
14980 tree underlying_type;
14981 tree decl;
14982 tree value;
14983 tree minnode, maxnode;
14984 tree t;
14985
14986 bool fixed_underlying_type_p
14987 = ENUM_UNDERLYING_TYPE (enumtype) != NULL_TREE;
14988
14989 /* We built up the VALUES in reverse order. */
14990 TYPE_VALUES (enumtype) = nreverse (TYPE_VALUES (enumtype));
14991
14992 /* For an enum defined in a template, just set the type of the values;
14993 all further processing is postponed until the template is
14994 instantiated. We need to set the type so that tsubst of a CONST_DECL
14995 works. */
14996 if (processing_template_decl)
14997 {
14998 for (values = TYPE_VALUES (enumtype);
14999 values;
15000 values = TREE_CHAIN (values))
15001 TREE_TYPE (TREE_VALUE (values)) = enumtype;
15002 return;
15003 }
15004
15005 /* Determine the minimum and maximum values of the enumerators. */
15006 if (TYPE_VALUES (enumtype))
15007 {
15008 minnode = maxnode = NULL_TREE;
15009
15010 for (values = TYPE_VALUES (enumtype);
15011 values;
15012 values = TREE_CHAIN (values))
15013 {
15014 decl = TREE_VALUE (values);
15015
15016 /* [dcl.enum]: Following the closing brace of an enum-specifier,
15017 each enumerator has the type of its enumeration. Prior to the
15018 closing brace, the type of each enumerator is the type of its
15019 initializing value. */
15020 TREE_TYPE (decl) = enumtype;
15021
15022 /* Update the minimum and maximum values, if appropriate. */
15023 value = DECL_INITIAL (decl);
15024 if (value == error_mark_node)
15025 value = integer_zero_node;
15026 /* Figure out what the minimum and maximum values of the
15027 enumerators are. */
15028 if (!minnode)
15029 minnode = maxnode = value;
15030 else if (tree_int_cst_lt (maxnode, value))
15031 maxnode = value;
15032 else if (tree_int_cst_lt (value, minnode))
15033 minnode = value;
15034 }
15035 }
15036 else
15037 /* [dcl.enum]
15038
15039 If the enumerator-list is empty, the underlying type is as if
15040 the enumeration had a single enumerator with value 0. */
15041 minnode = maxnode = integer_zero_node;
15042
15043 if (!fixed_underlying_type_p)
15044 {
15045 /* Compute the number of bits require to represent all values of the
15046 enumeration. We must do this before the type of MINNODE and
15047 MAXNODE are transformed, since tree_int_cst_min_precision relies
15048 on the TREE_TYPE of the value it is passed. */
15049 signop sgn = tree_int_cst_sgn (minnode) >= 0 ? UNSIGNED : SIGNED;
15050 int lowprec = tree_int_cst_min_precision (minnode, sgn);
15051 int highprec = tree_int_cst_min_precision (maxnode, sgn);
15052 int precision = MAX (lowprec, highprec);
15053 unsigned int itk;
15054 bool use_short_enum;
15055
15056 /* Determine the underlying type of the enumeration.
15057
15058 [dcl.enum]
15059
15060 The underlying type of an enumeration is an integral type that
15061 can represent all the enumerator values defined in the
15062 enumeration. It is implementation-defined which integral type is
15063 used as the underlying type for an enumeration except that the
15064 underlying type shall not be larger than int unless the value of
15065 an enumerator cannot fit in an int or unsigned int.
15066
15067 We use "int" or an "unsigned int" as the underlying type, even if
15068 a smaller integral type would work, unless the user has
15069 explicitly requested that we use the smallest possible type. The
15070 user can request that for all enumerations with a command line
15071 flag, or for just one enumeration with an attribute. */
15072
15073 use_short_enum = flag_short_enums
15074 || lookup_attribute ("packed", TYPE_ATTRIBUTES (enumtype));
15075
15076 /* If the precision of the type was specified with an attribute and it
15077 was too small, give an error. Otherwise, use it. */
15078 if (TYPE_PRECISION (enumtype))
15079 {
15080 if (precision > TYPE_PRECISION (enumtype))
15081 error ("specified mode too small for enumerated values");
15082 else
15083 {
15084 use_short_enum = true;
15085 precision = TYPE_PRECISION (enumtype);
15086 }
15087 }
15088
15089 for (itk = (use_short_enum ? itk_char : itk_int);
15090 itk != itk_none;
15091 itk++)
15092 {
15093 underlying_type = integer_types[itk];
15094 if (underlying_type != NULL_TREE
15095 && TYPE_PRECISION (underlying_type) >= precision
15096 && TYPE_SIGN (underlying_type) == sgn)
15097 break;
15098 }
15099 if (itk == itk_none)
15100 {
15101 /* DR 377
15102
15103 IF no integral type can represent all the enumerator values, the
15104 enumeration is ill-formed. */
15105 error ("no integral type can represent all of the enumerator values "
15106 "for %qT", enumtype);
15107 precision = TYPE_PRECISION (long_long_integer_type_node);
15108 underlying_type = integer_types[itk_unsigned_long_long];
15109 }
15110
15111 /* [dcl.enum]
15112
15113 The value of sizeof() applied to an enumeration type, an object
15114 of an enumeration type, or an enumerator, is the value of sizeof()
15115 applied to the underlying type. */
15116 copy_type_enum (enumtype, underlying_type);
15117
15118 /* Compute the minimum and maximum values for the type.
15119
15120 [dcl.enum]
15121
15122 For an enumeration where emin is the smallest enumerator and emax
15123 is the largest, the values of the enumeration are the values of the
15124 underlying type in the range bmin to bmax, where bmin and bmax are,
15125 respectively, the smallest and largest values of the smallest bit-
15126 field that can store emin and emax. */
15127
15128 /* The middle-end currently assumes that types with TYPE_PRECISION
15129 narrower than their underlying type are suitably zero or sign
15130 extended to fill their mode. Similarly, it assumes that the front
15131 end assures that a value of a particular type must be within
15132 TYPE_MIN_VALUE and TYPE_MAX_VALUE.
15133
15134 We used to set these fields based on bmin and bmax, but that led
15135 to invalid assumptions like optimizing away bounds checking. So
15136 now we just set the TYPE_PRECISION, TYPE_MIN_VALUE, and
15137 TYPE_MAX_VALUE to the values for the mode above and only restrict
15138 the ENUM_UNDERLYING_TYPE for the benefit of diagnostics. */
15139 ENUM_UNDERLYING_TYPE (enumtype)
15140 = build_distinct_type_copy (underlying_type);
15141 TYPE_PRECISION (ENUM_UNDERLYING_TYPE (enumtype)) = precision;
15142 set_min_and_max_values_for_integral_type
15143 (ENUM_UNDERLYING_TYPE (enumtype), precision, sgn);
15144
15145 /* If -fstrict-enums, still constrain TYPE_MIN/MAX_VALUE. */
15146 if (flag_strict_enums)
15147 set_min_and_max_values_for_integral_type (enumtype, precision, sgn);
15148 }
15149 else
15150 underlying_type = ENUM_UNDERLYING_TYPE (enumtype);
15151
15152 /* Convert each of the enumerators to the type of the underlying
15153 type of the enumeration. */
15154 for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values))
15155 {
15156 decl = TREE_VALUE (values);
15157 iloc_sentinel ils (DECL_SOURCE_LOCATION (decl));
15158 if (fixed_underlying_type_p)
15159 /* If the enumeration type has a fixed underlying type, we
15160 already checked all of the enumerator values. */
15161 value = DECL_INITIAL (decl);
15162 else
15163 value = perform_implicit_conversion (underlying_type,
15164 DECL_INITIAL (decl),
15165 tf_warning_or_error);
15166 /* Do not clobber shared ints. */
15167 if (value != error_mark_node)
15168 {
15169 value = copy_node (value);
15170
15171 TREE_TYPE (value) = enumtype;
15172 }
15173 DECL_INITIAL (decl) = value;
15174 }
15175
15176 /* Fix up all variant types of this enum type. */
15177 for (t = TYPE_MAIN_VARIANT (enumtype); t; t = TYPE_NEXT_VARIANT (t))
15178 TYPE_VALUES (t) = TYPE_VALUES (enumtype);
15179
15180 if (at_class_scope_p ()
15181 && COMPLETE_TYPE_P (current_class_type)
15182 && UNSCOPED_ENUM_P (enumtype))
15183 {
15184 insert_late_enum_def_bindings (current_class_type, enumtype);
15185 /* TYPE_FIELDS needs fixup. */
15186 fixup_type_variants (current_class_type);
15187 }
15188
15189 /* Finish debugging output for this type. */
15190 rest_of_type_compilation (enumtype, namespace_bindings_p ());
15191
15192 /* Each enumerator now has the type of its enumeration. Clear the cache
15193 so that this change in types doesn't confuse us later on. */
15194 clear_cv_and_fold_caches ();
15195 }
15196
15197 /* Finishes the enum type. This is called only the first time an
15198 enumeration is seen, be it opaque or odinary.
15199 ENUMTYPE is the type object. */
15200
15201 void
15202 finish_enum (tree enumtype)
15203 {
15204 if (processing_template_decl)
15205 {
15206 if (at_function_scope_p ())
15207 add_stmt (build_min (TAG_DEFN, enumtype));
15208 return;
15209 }
15210
15211 /* If this is a forward declaration, there should not be any variants,
15212 though we can get a variant in the middle of an enum-specifier with
15213 wacky code like 'enum E { e = sizeof(const E*) };' */
15214 gcc_assert (enumtype == TYPE_MAIN_VARIANT (enumtype)
15215 && (TYPE_VALUES (enumtype)
15216 || !TYPE_NEXT_VARIANT (enumtype)));
15217 }
15218
15219 /* Build and install a CONST_DECL for an enumeration constant of the
15220 enumeration type ENUMTYPE whose NAME and VALUE (if any) are provided.
15221 Apply ATTRIBUTES if available. LOC is the location of NAME.
15222 Assignment of sequential values by default is handled here. */
15223
15224 void
15225 build_enumerator (tree name, tree value, tree enumtype, tree attributes,
15226 location_t loc)
15227 {
15228 tree decl;
15229 tree context;
15230 tree type;
15231
15232 /* scalar_constant_value will pull out this expression, so make sure
15233 it's folded as appropriate. */
15234 if (processing_template_decl)
15235 value = fold_non_dependent_expr (value);
15236
15237 /* If the VALUE was erroneous, pretend it wasn't there; that will
15238 result in the enum being assigned the next value in sequence. */
15239 if (value == error_mark_node)
15240 value = NULL_TREE;
15241
15242 /* Remove no-op casts from the value. */
15243 if (value)
15244 STRIP_TYPE_NOPS (value);
15245
15246 if (! processing_template_decl)
15247 {
15248 /* Validate and default VALUE. */
15249 if (value != NULL_TREE)
15250 {
15251 if (!ENUM_UNDERLYING_TYPE (enumtype))
15252 {
15253 tree tmp_value = build_expr_type_conversion (WANT_INT | WANT_ENUM,
15254 value, true);
15255 if (tmp_value)
15256 value = tmp_value;
15257 }
15258 else if (! INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P
15259 (TREE_TYPE (value)))
15260 value = perform_implicit_conversion_flags
15261 (ENUM_UNDERLYING_TYPE (enumtype), value, tf_warning_or_error,
15262 LOOKUP_IMPLICIT | LOOKUP_NO_NARROWING);
15263
15264 if (value == error_mark_node)
15265 value = NULL_TREE;
15266
15267 if (value != NULL_TREE)
15268 {
15269 if (! INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P
15270 (TREE_TYPE (value)))
15271 {
15272 error_at (cp_expr_loc_or_input_loc (value),
15273 "enumerator value for %qD must have integral or "
15274 "unscoped enumeration type", name);
15275 value = NULL_TREE;
15276 }
15277 else
15278 {
15279 value = cxx_constant_value (value);
15280
15281 if (TREE_CODE (value) != INTEGER_CST)
15282 {
15283 error ("enumerator value for %qD is not an integer "
15284 "constant", name);
15285 value = NULL_TREE;
15286 }
15287 }
15288 }
15289 }
15290
15291 /* Default based on previous value. */
15292 if (value == NULL_TREE)
15293 {
15294 if (TYPE_VALUES (enumtype))
15295 {
15296 tree prev_value;
15297
15298 /* C++03 7.2/4: If no initializer is specified for the first
15299 enumerator, the type is an unspecified integral
15300 type. Otherwise the type is the same as the type of the
15301 initializing value of the preceding enumerator unless the
15302 incremented value is not representable in that type, in
15303 which case the type is an unspecified integral type
15304 sufficient to contain the incremented value. */
15305 prev_value = DECL_INITIAL (TREE_VALUE (TYPE_VALUES (enumtype)));
15306 if (error_operand_p (prev_value))
15307 value = error_mark_node;
15308 else
15309 {
15310 wi::overflow_type overflowed;
15311 tree type = TREE_TYPE (prev_value);
15312 signop sgn = TYPE_SIGN (type);
15313 widest_int wi = wi::add (wi::to_widest (prev_value), 1, sgn,
15314 &overflowed);
15315 if (!overflowed)
15316 {
15317 bool pos = !wi::neg_p (wi, sgn);
15318 if (!wi::fits_to_tree_p (wi, type))
15319 {
15320 unsigned int itk;
15321 for (itk = itk_int; itk != itk_none; itk++)
15322 {
15323 type = integer_types[itk];
15324 if (type != NULL_TREE
15325 && (pos || !TYPE_UNSIGNED (type))
15326 && wi::fits_to_tree_p (wi, type))
15327 break;
15328 }
15329 if (type && cxx_dialect < cxx11
15330 && itk > itk_unsigned_long)
15331 pedwarn (input_location, OPT_Wlong_long,
15332 pos ? G_("\
15333 incremented enumerator value is too large for %<unsigned long%>") : G_("\
15334 incremented enumerator value is too large for %<long%>"));
15335 }
15336 if (type == NULL_TREE)
15337 overflowed = wi::OVF_UNKNOWN;
15338 else
15339 value = wide_int_to_tree (type, wi);
15340 }
15341
15342 if (overflowed)
15343 {
15344 error ("overflow in enumeration values at %qD", name);
15345 value = error_mark_node;
15346 }
15347 }
15348 }
15349 else
15350 value = integer_zero_node;
15351 }
15352
15353 /* Remove no-op casts from the value. */
15354 STRIP_TYPE_NOPS (value);
15355
15356 /* If the underlying type of the enum is fixed, check whether
15357 the enumerator values fits in the underlying type. If it
15358 does not fit, the program is ill-formed [C++0x dcl.enum]. */
15359 if (ENUM_UNDERLYING_TYPE (enumtype)
15360 && value
15361 && TREE_CODE (value) == INTEGER_CST)
15362 {
15363 if (!int_fits_type_p (value, ENUM_UNDERLYING_TYPE (enumtype)))
15364 error ("enumerator value %qE is outside the range of underlying "
15365 "type %qT", value, ENUM_UNDERLYING_TYPE (enumtype));
15366
15367 /* Convert the value to the appropriate type. */
15368 value = fold_convert (ENUM_UNDERLYING_TYPE (enumtype), value);
15369 }
15370 }
15371
15372 /* C++ associates enums with global, function, or class declarations. */
15373 context = current_scope ();
15374
15375 /* Build the actual enumeration constant. Note that the enumeration
15376 constants have the underlying type of the enum (if it is fixed)
15377 or the type of their initializer (if the underlying type of the
15378 enum is not fixed):
15379
15380 [ C++0x dcl.enum ]
15381
15382 If the underlying type is fixed, the type of each enumerator
15383 prior to the closing brace is the underlying type; if the
15384 initializing value of an enumerator cannot be represented by
15385 the underlying type, the program is ill-formed. If the
15386 underlying type is not fixed, the type of each enumerator is
15387 the type of its initializing value.
15388
15389 If the underlying type is not fixed, it will be computed by
15390 finish_enum and we will reset the type of this enumerator. Of
15391 course, if we're processing a template, there may be no value. */
15392 type = value ? TREE_TYPE (value) : NULL_TREE;
15393
15394 decl = build_decl (loc, CONST_DECL, name, type);
15395
15396 DECL_CONTEXT (decl) = enumtype;
15397 TREE_CONSTANT (decl) = 1;
15398 TREE_READONLY (decl) = 1;
15399 DECL_INITIAL (decl) = value;
15400
15401 if (attributes)
15402 cplus_decl_attributes (&decl, attributes, 0);
15403
15404 if (context && context == current_class_type && !SCOPED_ENUM_P (enumtype))
15405 {
15406 /* In something like `struct S { enum E { i = 7 }; };' we put `i'
15407 on the TYPE_FIELDS list for `S'. (That's so that you can say
15408 things like `S::i' later.) */
15409
15410 /* The enumerator may be getting declared outside of its enclosing
15411 class, like so:
15412
15413 class S { public: enum E : int; }; enum S::E : int { i = 7; };
15414
15415 For which case we need to make sure that the access of `S::i'
15416 matches the access of `S::E'. */
15417 tree saved_cas = current_access_specifier;
15418 if (TREE_PRIVATE (TYPE_NAME (enumtype)))
15419 current_access_specifier = access_private_node;
15420 else if (TREE_PROTECTED (TYPE_NAME (enumtype)))
15421 current_access_specifier = access_protected_node;
15422 else
15423 current_access_specifier = access_public_node;
15424
15425 finish_member_declaration (decl);
15426
15427 current_access_specifier = saved_cas;
15428 }
15429 else
15430 pushdecl (decl);
15431
15432 /* Add this enumeration constant to the list for this type. */
15433 TYPE_VALUES (enumtype) = tree_cons (name, decl, TYPE_VALUES (enumtype));
15434 }
15435
15436 /* Look for an enumerator with the given NAME within the enumeration
15437 type ENUMTYPE. This routine is used primarily for qualified name
15438 lookup into an enumerator in C++0x, e.g.,
15439
15440 enum class Color { Red, Green, Blue };
15441
15442 Color color = Color::Red;
15443
15444 Returns the value corresponding to the enumerator, or
15445 NULL_TREE if no such enumerator was found. */
15446 tree
15447 lookup_enumerator (tree enumtype, tree name)
15448 {
15449 tree e;
15450 gcc_assert (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE);
15451
15452 e = purpose_member (name, TYPE_VALUES (enumtype));
15453 return e? TREE_VALUE (e) : NULL_TREE;
15454 }
15455
15456 \f
15457 /* We're defining DECL. Make sure that its type is OK. */
15458
15459 static void
15460 check_function_type (tree decl, tree current_function_parms)
15461 {
15462 tree fntype = TREE_TYPE (decl);
15463 tree return_type = complete_type (TREE_TYPE (fntype));
15464
15465 /* In a function definition, arg types must be complete. */
15466 require_complete_types_for_parms (current_function_parms);
15467
15468 if (dependent_type_p (return_type)
15469 || type_uses_auto (return_type))
15470 return;
15471 if (!COMPLETE_OR_VOID_TYPE_P (return_type))
15472 {
15473 tree args = TYPE_ARG_TYPES (fntype);
15474
15475 error ("return type %q#T is incomplete", return_type);
15476
15477 /* Make it return void instead. */
15478 if (TREE_CODE (fntype) == METHOD_TYPE)
15479 fntype = build_method_type_directly (TREE_TYPE (TREE_VALUE (args)),
15480 void_type_node,
15481 TREE_CHAIN (args));
15482 else
15483 fntype = build_function_type (void_type_node, args);
15484 fntype = (cp_build_type_attribute_variant
15485 (fntype, TYPE_ATTRIBUTES (TREE_TYPE (decl))));
15486 fntype = cxx_copy_lang_qualifiers (fntype, TREE_TYPE (decl));
15487 TREE_TYPE (decl) = fntype;
15488 }
15489 else
15490 {
15491 abstract_virtuals_error (decl, TREE_TYPE (fntype));
15492 maybe_warn_parm_abi (TREE_TYPE (fntype),
15493 DECL_SOURCE_LOCATION (decl));
15494 }
15495 }
15496
15497 /* True iff FN is an implicitly-defined default constructor. */
15498
15499 static bool
15500 implicit_default_ctor_p (tree fn)
15501 {
15502 return (DECL_CONSTRUCTOR_P (fn)
15503 && !user_provided_p (fn)
15504 && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn)));
15505 }
15506
15507 /* Clobber the contents of *this to let the back end know that the object
15508 storage is dead when we enter the constructor or leave the destructor. */
15509
15510 static tree
15511 build_clobber_this ()
15512 {
15513 /* Clobbering an empty base is pointless, and harmful if its one byte
15514 TYPE_SIZE overlays real data. */
15515 if (is_empty_class (current_class_type))
15516 return void_node;
15517
15518 /* If we have virtual bases, clobber the whole object, but only if we're in
15519 charge. If we don't have virtual bases, clobber the as-base type so we
15520 don't mess with tail padding. */
15521 bool vbases = CLASSTYPE_VBASECLASSES (current_class_type);
15522
15523 tree ctype = current_class_type;
15524 if (!vbases)
15525 ctype = CLASSTYPE_AS_BASE (ctype);
15526
15527 tree clobber = build_clobber (ctype);
15528
15529 tree thisref = current_class_ref;
15530 if (ctype != current_class_type)
15531 {
15532 thisref = build_nop (build_reference_type (ctype), current_class_ptr);
15533 thisref = convert_from_reference (thisref);
15534 }
15535
15536 tree exprstmt = build2 (MODIFY_EXPR, void_type_node, thisref, clobber);
15537 if (vbases)
15538 exprstmt = build_if_in_charge (exprstmt);
15539
15540 return exprstmt;
15541 }
15542
15543 /* Create the FUNCTION_DECL for a function definition.
15544 DECLSPECS and DECLARATOR are the parts of the declaration;
15545 they describe the function's name and the type it returns,
15546 but twisted together in a fashion that parallels the syntax of C.
15547
15548 FLAGS is a bitwise or of SF_PRE_PARSED (indicating that the
15549 DECLARATOR is really the DECL for the function we are about to
15550 process and that DECLSPECS should be ignored), SF_INCLASS_INLINE
15551 indicating that the function is an inline defined in-class.
15552
15553 This function creates a binding context for the function body
15554 as well as setting up the FUNCTION_DECL in current_function_decl.
15555
15556 For C++, we must first check whether that datum makes any sense.
15557 For example, "class A local_a(1,2);" means that variable local_a
15558 is an aggregate of type A, which should have a constructor
15559 applied to it with the argument list [1, 2].
15560
15561 On entry, DECL_INITIAL (decl1) should be NULL_TREE or error_mark_node,
15562 or may be a BLOCK if the function has been defined previously
15563 in this translation unit. On exit, DECL_INITIAL (decl1) will be
15564 error_mark_node if the function has never been defined, or
15565 a BLOCK if the function has been defined somewhere. */
15566
15567 bool
15568 start_preparsed_function (tree decl1, tree attrs, int flags)
15569 {
15570 tree ctype = NULL_TREE;
15571 tree fntype;
15572 tree restype;
15573 int doing_friend = 0;
15574 cp_binding_level *bl;
15575 tree current_function_parms;
15576 struct c_fileinfo *finfo
15577 = get_fileinfo (LOCATION_FILE (DECL_SOURCE_LOCATION (decl1)));
15578 bool honor_interface;
15579
15580 /* Sanity check. */
15581 gcc_assert (VOID_TYPE_P (TREE_VALUE (void_list_node)));
15582 gcc_assert (TREE_CHAIN (void_list_node) == NULL_TREE);
15583
15584 fntype = TREE_TYPE (decl1);
15585 if (TREE_CODE (fntype) == METHOD_TYPE)
15586 ctype = TYPE_METHOD_BASETYPE (fntype);
15587
15588 /* ISO C++ 11.4/5. A friend function defined in a class is in
15589 the (lexical) scope of the class in which it is defined. */
15590 if (!ctype && DECL_FRIEND_P (decl1))
15591 {
15592 ctype = DECL_FRIEND_CONTEXT (decl1);
15593
15594 /* CTYPE could be null here if we're dealing with a template;
15595 for example, `inline friend float foo()' inside a template
15596 will have no CTYPE set. */
15597 if (ctype && TREE_CODE (ctype) != RECORD_TYPE)
15598 ctype = NULL_TREE;
15599 else
15600 doing_friend = 1;
15601 }
15602
15603 if (DECL_DECLARED_INLINE_P (decl1)
15604 && lookup_attribute ("noinline", attrs))
15605 warning_at (DECL_SOURCE_LOCATION (decl1), 0,
15606 "inline function %qD given attribute %qs", decl1, "noinline");
15607
15608 /* Handle gnu_inline attribute. */
15609 if (GNU_INLINE_P (decl1))
15610 {
15611 DECL_EXTERNAL (decl1) = 1;
15612 DECL_NOT_REALLY_EXTERN (decl1) = 0;
15613 DECL_INTERFACE_KNOWN (decl1) = 1;
15614 DECL_DISREGARD_INLINE_LIMITS (decl1) = 1;
15615 }
15616
15617 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl1))
15618 /* This is a constructor, we must ensure that any default args
15619 introduced by this definition are propagated to the clones
15620 now. The clones are used directly in overload resolution. */
15621 adjust_clone_args (decl1);
15622
15623 /* Sometimes we don't notice that a function is a static member, and
15624 build a METHOD_TYPE for it. Fix that up now. */
15625 gcc_assert (!(ctype != NULL_TREE && DECL_STATIC_FUNCTION_P (decl1)
15626 && TREE_CODE (TREE_TYPE (decl1)) == METHOD_TYPE));
15627
15628 /* Set up current_class_type, and enter the scope of the class, if
15629 appropriate. */
15630 if (ctype)
15631 push_nested_class (ctype);
15632 else if (DECL_STATIC_FUNCTION_P (decl1))
15633 push_nested_class (DECL_CONTEXT (decl1));
15634
15635 /* Now that we have entered the scope of the class, we must restore
15636 the bindings for any template parameters surrounding DECL1, if it
15637 is an inline member template. (Order is important; consider the
15638 case where a template parameter has the same name as a field of
15639 the class.) It is not until after this point that
15640 PROCESSING_TEMPLATE_DECL is guaranteed to be set up correctly. */
15641 if (flags & SF_INCLASS_INLINE)
15642 maybe_begin_member_template_processing (decl1);
15643
15644 /* Effective C++ rule 15. */
15645 if (warn_ecpp
15646 && DECL_ASSIGNMENT_OPERATOR_P (decl1)
15647 && DECL_OVERLOADED_OPERATOR_IS (decl1, NOP_EXPR)
15648 && VOID_TYPE_P (TREE_TYPE (fntype)))
15649 warning (OPT_Weffc__,
15650 "%<operator=%> should return a reference to %<*this%>");
15651
15652 /* Make the init_value nonzero so pushdecl knows this is not tentative.
15653 error_mark_node is replaced below (in poplevel) with the BLOCK. */
15654 if (!DECL_INITIAL (decl1))
15655 DECL_INITIAL (decl1) = error_mark_node;
15656
15657 /* This function exists in static storage.
15658 (This does not mean `static' in the C sense!) */
15659 TREE_STATIC (decl1) = 1;
15660
15661 /* We must call push_template_decl after current_class_type is set
15662 up. (If we are processing inline definitions after exiting a
15663 class scope, current_class_type will be NULL_TREE until set above
15664 by push_nested_class.) */
15665 if (processing_template_decl)
15666 {
15667 tree newdecl1 = push_template_decl (decl1);
15668 if (newdecl1 == error_mark_node)
15669 {
15670 if (ctype || DECL_STATIC_FUNCTION_P (decl1))
15671 pop_nested_class ();
15672 return false;
15673 }
15674 decl1 = newdecl1;
15675 }
15676
15677 /* Make sure the parameter and return types are reasonable. When
15678 you declare a function, these types can be incomplete, but they
15679 must be complete when you define the function. */
15680 check_function_type (decl1, DECL_ARGUMENTS (decl1));
15681
15682 /* Build the return declaration for the function. */
15683 restype = TREE_TYPE (fntype);
15684
15685 if (DECL_RESULT (decl1) == NULL_TREE)
15686 {
15687 tree resdecl;
15688
15689 resdecl = build_decl (input_location, RESULT_DECL, 0, restype);
15690 DECL_ARTIFICIAL (resdecl) = 1;
15691 DECL_IGNORED_P (resdecl) = 1;
15692 DECL_RESULT (decl1) = resdecl;
15693
15694 cp_apply_type_quals_to_decl (cp_type_quals (restype), resdecl);
15695 }
15696
15697 /* Record the decl so that the function name is defined.
15698 If we already have a decl for this name, and it is a FUNCTION_DECL,
15699 use the old decl. */
15700 if (!processing_template_decl && !(flags & SF_PRE_PARSED))
15701 {
15702 /* A specialization is not used to guide overload resolution. */
15703 if (!DECL_FUNCTION_MEMBER_P (decl1)
15704 && !(DECL_USE_TEMPLATE (decl1) &&
15705 PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl1))))
15706 {
15707 tree olddecl = pushdecl (decl1);
15708
15709 if (olddecl == error_mark_node)
15710 /* If something went wrong when registering the declaration,
15711 use DECL1; we have to have a FUNCTION_DECL to use when
15712 parsing the body of the function. */
15713 ;
15714 else
15715 {
15716 /* Otherwise, OLDDECL is either a previous declaration
15717 of the same function or DECL1 itself. */
15718
15719 if (warn_missing_declarations
15720 && olddecl == decl1
15721 && !DECL_MAIN_P (decl1)
15722 && TREE_PUBLIC (decl1)
15723 && !DECL_DECLARED_INLINE_P (decl1))
15724 {
15725 tree context;
15726
15727 /* Check whether DECL1 is in an anonymous
15728 namespace. */
15729 for (context = DECL_CONTEXT (decl1);
15730 context;
15731 context = DECL_CONTEXT (context))
15732 {
15733 if (TREE_CODE (context) == NAMESPACE_DECL
15734 && DECL_NAME (context) == NULL_TREE)
15735 break;
15736 }
15737
15738 if (context == NULL)
15739 warning_at (DECL_SOURCE_LOCATION (decl1),
15740 OPT_Wmissing_declarations,
15741 "no previous declaration for %qD", decl1);
15742 }
15743
15744 decl1 = olddecl;
15745 }
15746 }
15747 else
15748 {
15749 /* We need to set the DECL_CONTEXT. */
15750 if (!DECL_CONTEXT (decl1) && DECL_TEMPLATE_INFO (decl1))
15751 DECL_CONTEXT (decl1) = DECL_CONTEXT (DECL_TI_TEMPLATE (decl1));
15752 }
15753 fntype = TREE_TYPE (decl1);
15754 restype = TREE_TYPE (fntype);
15755
15756 /* If #pragma weak applies, mark the decl appropriately now.
15757 The pragma only applies to global functions. Because
15758 determining whether or not the #pragma applies involves
15759 computing the mangled name for the declaration, we cannot
15760 apply the pragma until after we have merged this declaration
15761 with any previous declarations; if the original declaration
15762 has a linkage specification, that specification applies to
15763 the definition as well, and may affect the mangled name. */
15764 if (DECL_FILE_SCOPE_P (decl1))
15765 maybe_apply_pragma_weak (decl1);
15766 }
15767
15768 /* We are now in the scope of the function being defined. */
15769 current_function_decl = decl1;
15770
15771 /* Save the parm names or decls from this function's declarator
15772 where store_parm_decls will find them. */
15773 current_function_parms = DECL_ARGUMENTS (decl1);
15774
15775 /* Let the user know we're compiling this function. */
15776 announce_function (decl1);
15777
15778 gcc_assert (DECL_INITIAL (decl1));
15779
15780 /* This function may already have been parsed, in which case just
15781 return; our caller will skip over the body without parsing. */
15782 if (DECL_INITIAL (decl1) != error_mark_node)
15783 return true;
15784
15785 /* Initialize RTL machinery. We cannot do this until
15786 CURRENT_FUNCTION_DECL and DECL_RESULT are set up. We do this
15787 even when processing a template; this is how we get
15788 CFUN set up, and our per-function variables initialized.
15789 FIXME factor out the non-RTL stuff. */
15790 bl = current_binding_level;
15791 allocate_struct_function (decl1, processing_template_decl);
15792
15793 /* Initialize the language data structures. Whenever we start
15794 a new function, we destroy temporaries in the usual way. */
15795 cfun->language = ggc_cleared_alloc<language_function> ();
15796 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
15797 current_binding_level = bl;
15798
15799 /* If we are (erroneously) defining a function that we have already
15800 defined before, wipe out what we knew before. */
15801 gcc_checking_assert (!DECL_PENDING_INLINE_P (decl1));
15802 FNDECL_USED_AUTO (decl1) = false;
15803 DECL_SAVED_AUTO_RETURN_TYPE (decl1) = NULL;
15804
15805 if (!processing_template_decl && type_uses_auto (restype))
15806 {
15807 FNDECL_USED_AUTO (decl1) = true;
15808 DECL_SAVED_AUTO_RETURN_TYPE (decl1) = restype;
15809 }
15810
15811 /* Start the statement-tree, start the tree now. */
15812 DECL_SAVED_TREE (decl1) = push_stmt_list ();
15813
15814 if (ctype && !doing_friend && !DECL_STATIC_FUNCTION_P (decl1))
15815 {
15816 /* We know that this was set up by `grokclassfn'. We do not
15817 wait until `store_parm_decls', since evil parse errors may
15818 never get us to that point. Here we keep the consistency
15819 between `current_class_type' and `current_class_ptr'. */
15820 tree t = DECL_ARGUMENTS (decl1);
15821
15822 gcc_assert (t != NULL_TREE && TREE_CODE (t) == PARM_DECL);
15823 gcc_assert (TYPE_PTR_P (TREE_TYPE (t)));
15824
15825 cp_function_chain->x_current_class_ref
15826 = cp_build_fold_indirect_ref (t);
15827 /* Set this second to avoid shortcut in cp_build_indirect_ref. */
15828 cp_function_chain->x_current_class_ptr = t;
15829
15830 /* Constructors and destructors need to know whether they're "in
15831 charge" of initializing virtual base classes. */
15832 t = DECL_CHAIN (t);
15833 if (DECL_HAS_IN_CHARGE_PARM_P (decl1))
15834 {
15835 current_in_charge_parm = t;
15836 t = DECL_CHAIN (t);
15837 }
15838 if (DECL_HAS_VTT_PARM_P (decl1))
15839 {
15840 gcc_assert (DECL_NAME (t) == vtt_parm_identifier);
15841 current_vtt_parm = t;
15842 }
15843 }
15844
15845 honor_interface = (!DECL_TEMPLATE_INSTANTIATION (decl1)
15846 /* Implicitly-defined methods (like the
15847 destructor for a class in which no destructor
15848 is explicitly declared) must not be defined
15849 until their definition is needed. So, we
15850 ignore interface specifications for
15851 compiler-generated functions. */
15852 && !DECL_ARTIFICIAL (decl1));
15853
15854 if (processing_template_decl)
15855 /* Don't mess with interface flags. */;
15856 else if (DECL_INTERFACE_KNOWN (decl1))
15857 {
15858 tree ctx = decl_function_context (decl1);
15859
15860 if (DECL_NOT_REALLY_EXTERN (decl1))
15861 DECL_EXTERNAL (decl1) = 0;
15862
15863 if (ctx != NULL_TREE && vague_linkage_p (ctx))
15864 /* This is a function in a local class in an extern inline
15865 or template function. */
15866 comdat_linkage (decl1);
15867 }
15868 /* If this function belongs to an interface, it is public.
15869 If it belongs to someone else's interface, it is also external.
15870 This only affects inlines and template instantiations. */
15871 else if (!finfo->interface_unknown && honor_interface)
15872 {
15873 if (DECL_DECLARED_INLINE_P (decl1)
15874 || DECL_TEMPLATE_INSTANTIATION (decl1))
15875 {
15876 DECL_EXTERNAL (decl1)
15877 = (finfo->interface_only
15878 || (DECL_DECLARED_INLINE_P (decl1)
15879 && ! flag_implement_inlines
15880 && !DECL_VINDEX (decl1)));
15881
15882 /* For WIN32 we also want to put these in linkonce sections. */
15883 maybe_make_one_only (decl1);
15884 }
15885 else
15886 DECL_EXTERNAL (decl1) = 0;
15887 DECL_INTERFACE_KNOWN (decl1) = 1;
15888 /* If this function is in an interface implemented in this file,
15889 make sure that the back end knows to emit this function
15890 here. */
15891 if (!DECL_EXTERNAL (decl1))
15892 mark_needed (decl1);
15893 }
15894 else if (finfo->interface_unknown && finfo->interface_only
15895 && honor_interface)
15896 {
15897 /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma
15898 interface, we will have both finfo->interface_unknown and
15899 finfo->interface_only set. In that case, we don't want to
15900 use the normal heuristics because someone will supply a
15901 #pragma implementation elsewhere, and deducing it here would
15902 produce a conflict. */
15903 comdat_linkage (decl1);
15904 DECL_EXTERNAL (decl1) = 0;
15905 DECL_INTERFACE_KNOWN (decl1) = 1;
15906 DECL_DEFER_OUTPUT (decl1) = 1;
15907 }
15908 else
15909 {
15910 /* This is a definition, not a reference.
15911 So clear DECL_EXTERNAL, unless this is a GNU extern inline. */
15912 if (!GNU_INLINE_P (decl1))
15913 DECL_EXTERNAL (decl1) = 0;
15914
15915 if ((DECL_DECLARED_INLINE_P (decl1)
15916 || DECL_TEMPLATE_INSTANTIATION (decl1))
15917 && ! DECL_INTERFACE_KNOWN (decl1))
15918 DECL_DEFER_OUTPUT (decl1) = 1;
15919 else
15920 DECL_INTERFACE_KNOWN (decl1) = 1;
15921 }
15922
15923 /* Determine the ELF visibility attribute for the function. We must not
15924 do this before calling "pushdecl", as we must allow "duplicate_decls"
15925 to merge any attributes appropriately. We also need to wait until
15926 linkage is set. */
15927 if (!DECL_CLONED_FUNCTION_P (decl1))
15928 determine_visibility (decl1);
15929
15930 if (!processing_template_decl)
15931 maybe_instantiate_noexcept (decl1);
15932
15933 begin_scope (sk_function_parms, decl1);
15934
15935 ++function_depth;
15936
15937 if (DECL_DESTRUCTOR_P (decl1)
15938 || (DECL_CONSTRUCTOR_P (decl1)
15939 && targetm.cxx.cdtor_returns_this ()))
15940 {
15941 cdtor_label = create_artificial_label (input_location);
15942 LABEL_DECL_CDTOR (cdtor_label) = true;
15943 }
15944
15945 start_fname_decls ();
15946
15947 store_parm_decls (current_function_parms);
15948
15949 push_operator_bindings ();
15950
15951 if (!processing_template_decl
15952 && (flag_lifetime_dse > 1)
15953 && DECL_CONSTRUCTOR_P (decl1)
15954 && !DECL_CLONED_FUNCTION_P (decl1)
15955 /* Clobbering an empty base is harmful if it overlays real data. */
15956 && !is_empty_class (current_class_type)
15957 /* We can't clobber safely for an implicitly-defined default constructor
15958 because part of the initialization might happen before we enter the
15959 constructor, via AGGR_INIT_ZERO_FIRST (c++/68006). */
15960 && !implicit_default_ctor_p (decl1))
15961 finish_expr_stmt (build_clobber_this ());
15962
15963 if (!processing_template_decl
15964 && DECL_CONSTRUCTOR_P (decl1)
15965 && sanitize_flags_p (SANITIZE_VPTR)
15966 && !DECL_CLONED_FUNCTION_P (decl1)
15967 && !implicit_default_ctor_p (decl1))
15968 cp_ubsan_maybe_initialize_vtbl_ptrs (current_class_ptr);
15969
15970 start_lambda_scope (decl1);
15971
15972 return true;
15973 }
15974
15975
15976 /* Like start_preparsed_function, except that instead of a
15977 FUNCTION_DECL, this function takes DECLSPECS and DECLARATOR.
15978
15979 Returns true on success. If the DECLARATOR is not suitable
15980 for a function, we return false, which tells the parser to
15981 skip the entire function. */
15982
15983 bool
15984 start_function (cp_decl_specifier_seq *declspecs,
15985 const cp_declarator *declarator,
15986 tree attrs)
15987 {
15988 tree decl1;
15989
15990 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, &attrs);
15991 invoke_plugin_callbacks (PLUGIN_START_PARSE_FUNCTION, decl1);
15992 if (decl1 == error_mark_node)
15993 return false;
15994
15995 if (DECL_MAIN_P (decl1))
15996 /* main must return int. grokfndecl should have corrected it
15997 (and issued a diagnostic) if the user got it wrong. */
15998 gcc_assert (same_type_p (TREE_TYPE (TREE_TYPE (decl1)),
15999 integer_type_node));
16000
16001 return start_preparsed_function (decl1, attrs, /*flags=*/SF_DEFAULT);
16002 }
16003 \f
16004 /* Returns true iff an EH_SPEC_BLOCK should be created in the body of
16005 FN. */
16006
16007 static bool
16008 use_eh_spec_block (tree fn)
16009 {
16010 return (flag_exceptions && flag_enforce_eh_specs
16011 && !processing_template_decl
16012 && !type_throw_all_p (TREE_TYPE (fn))
16013 /* We insert the EH_SPEC_BLOCK only in the original
16014 function; then, it is copied automatically to the
16015 clones. */
16016 && !DECL_CLONED_FUNCTION_P (fn)
16017 /* Implicitly-generated constructors and destructors have
16018 exception specifications. However, those specifications
16019 are the union of the possible exceptions specified by the
16020 constructors/destructors for bases and members, so no
16021 unallowed exception will ever reach this function. By
16022 not creating the EH_SPEC_BLOCK we save a little memory,
16023 and we avoid spurious warnings about unreachable
16024 code. */
16025 && !DECL_DEFAULTED_FN (fn));
16026 }
16027
16028 /* Helper function to push ARGS into the current lexical scope. DECL
16029 is the function declaration. NONPARMS is used to handle enum
16030 constants. */
16031
16032 void
16033 do_push_parm_decls (tree decl, tree args, tree *nonparms)
16034 {
16035 /* If we're doing semantic analysis, then we'll call pushdecl
16036 for each of these. We must do them in reverse order so that
16037 they end in the correct forward order. */
16038 args = nreverse (args);
16039
16040 tree next;
16041 for (tree parm = args; parm; parm = next)
16042 {
16043 next = DECL_CHAIN (parm);
16044 if (TREE_CODE (parm) == PARM_DECL)
16045 pushdecl (parm);
16046 else if (nonparms)
16047 {
16048 /* If we find an enum constant or a type tag, put it aside for
16049 the moment. */
16050 TREE_CHAIN (parm) = NULL_TREE;
16051 *nonparms = chainon (*nonparms, parm);
16052 }
16053 }
16054
16055 /* Get the decls in their original chain order and record in the
16056 function. This is all and only the PARM_DECLs that were
16057 pushed into scope by the loop above. */
16058 DECL_ARGUMENTS (decl) = get_local_decls ();
16059 }
16060
16061 /* Store the parameter declarations into the current function declaration.
16062 This is called after parsing the parameter declarations, before
16063 digesting the body of the function.
16064
16065 Also install to binding contour return value identifier, if any. */
16066
16067 static void
16068 store_parm_decls (tree current_function_parms)
16069 {
16070 tree fndecl = current_function_decl;
16071
16072 /* This is a chain of any other decls that came in among the parm
16073 declarations. If a parm is declared with enum {foo, bar} x;
16074 then CONST_DECLs for foo and bar are put here. */
16075 tree nonparms = NULL_TREE;
16076
16077 if (current_function_parms)
16078 {
16079 /* This case is when the function was defined with an ANSI prototype.
16080 The parms already have decls, so we need not do anything here
16081 except record them as in effect
16082 and complain if any redundant old-style parm decls were written. */
16083
16084 tree specparms = current_function_parms;
16085
16086 /* Must clear this because it might contain TYPE_DECLs declared
16087 at class level. */
16088 current_binding_level->names = NULL;
16089
16090 do_push_parm_decls (fndecl, specparms, &nonparms);
16091 }
16092 else
16093 DECL_ARGUMENTS (fndecl) = NULL_TREE;
16094
16095 /* Now store the final chain of decls for the arguments
16096 as the decl-chain of the current lexical scope.
16097 Put the enumerators in as well, at the front so that
16098 DECL_ARGUMENTS is not modified. */
16099 current_binding_level->names = chainon (nonparms, DECL_ARGUMENTS (fndecl));
16100
16101 if (use_eh_spec_block (current_function_decl))
16102 current_eh_spec_block = begin_eh_spec_block ();
16103 }
16104
16105 \f
16106 /* Set the return value of the constructor (if present). */
16107
16108 static void
16109 finish_constructor_body (void)
16110 {
16111 tree val;
16112 tree exprstmt;
16113
16114 if (targetm.cxx.cdtor_returns_this ())
16115 {
16116 /* Any return from a constructor will end up here. */
16117 add_stmt (build_stmt (input_location, LABEL_EXPR, cdtor_label));
16118
16119 val = DECL_ARGUMENTS (current_function_decl);
16120 val = build2 (MODIFY_EXPR, TREE_TYPE (val),
16121 DECL_RESULT (current_function_decl), val);
16122 /* Return the address of the object. */
16123 exprstmt = build_stmt (input_location, RETURN_EXPR, val);
16124 add_stmt (exprstmt);
16125 }
16126 }
16127
16128 /* Do all the processing for the beginning of a destructor; set up the
16129 vtable pointers and cleanups for bases and members. */
16130
16131 static void
16132 begin_destructor_body (void)
16133 {
16134 tree compound_stmt;
16135
16136 /* If the CURRENT_CLASS_TYPE is incomplete, we will have already
16137 issued an error message. We still want to try to process the
16138 body of the function, but initialize_vtbl_ptrs will crash if
16139 TYPE_BINFO is NULL. */
16140 if (COMPLETE_TYPE_P (current_class_type))
16141 {
16142 compound_stmt = begin_compound_stmt (0);
16143 /* Make all virtual function table pointers in non-virtual base
16144 classes point to CURRENT_CLASS_TYPE's virtual function
16145 tables. */
16146 initialize_vtbl_ptrs (current_class_ptr);
16147 finish_compound_stmt (compound_stmt);
16148
16149 if (flag_lifetime_dse
16150 /* Clobbering an empty base is harmful if it overlays real data. */
16151 && !is_empty_class (current_class_type))
16152 {
16153 if (sanitize_flags_p (SANITIZE_VPTR)
16154 && (flag_sanitize_recover & SANITIZE_VPTR) == 0
16155 && TYPE_CONTAINS_VPTR_P (current_class_type))
16156 {
16157 tree binfo = TYPE_BINFO (current_class_type);
16158 tree ref
16159 = cp_build_fold_indirect_ref (current_class_ptr);
16160
16161 tree vtbl_ptr = build_vfield_ref (ref, TREE_TYPE (binfo));
16162 tree vtbl = build_zero_cst (TREE_TYPE (vtbl_ptr));
16163 tree stmt = cp_build_modify_expr (input_location, vtbl_ptr,
16164 NOP_EXPR, vtbl,
16165 tf_warning_or_error);
16166 /* If the vptr is shared with some virtual nearly empty base,
16167 don't clear it if not in charge, the dtor of the virtual
16168 nearly empty base will do that later. */
16169 if (CLASSTYPE_VBASECLASSES (current_class_type)
16170 && CLASSTYPE_PRIMARY_BINFO (current_class_type)
16171 && BINFO_VIRTUAL_P
16172 (CLASSTYPE_PRIMARY_BINFO (current_class_type)))
16173 {
16174 stmt = convert_to_void (stmt, ICV_STATEMENT,
16175 tf_warning_or_error);
16176 stmt = build_if_in_charge (stmt);
16177 }
16178 finish_decl_cleanup (NULL_TREE, stmt);
16179 }
16180 else
16181 finish_decl_cleanup (NULL_TREE, build_clobber_this ());
16182 }
16183
16184 /* And insert cleanups for our bases and members so that they
16185 will be properly destroyed if we throw. */
16186 push_base_cleanups ();
16187 }
16188 }
16189
16190 /* At the end of every destructor we generate code to delete the object if
16191 necessary. Do that now. */
16192
16193 static void
16194 finish_destructor_body (void)
16195 {
16196 tree exprstmt;
16197
16198 /* Any return from a destructor will end up here; that way all base
16199 and member cleanups will be run when the function returns. */
16200 add_stmt (build_stmt (input_location, LABEL_EXPR, cdtor_label));
16201
16202 if (targetm.cxx.cdtor_returns_this ())
16203 {
16204 tree val;
16205
16206 val = DECL_ARGUMENTS (current_function_decl);
16207 val = build2 (MODIFY_EXPR, TREE_TYPE (val),
16208 DECL_RESULT (current_function_decl), val);
16209 /* Return the address of the object. */
16210 exprstmt = build_stmt (input_location, RETURN_EXPR, val);
16211 add_stmt (exprstmt);
16212 }
16213 }
16214
16215 /* Do the necessary processing for the beginning of a function body, which
16216 in this case includes member-initializers, but not the catch clauses of
16217 a function-try-block. Currently, this means opening a binding level
16218 for the member-initializers (in a ctor), member cleanups (in a dtor),
16219 and capture proxies (in a lambda operator()). */
16220
16221 tree
16222 begin_function_body (void)
16223 {
16224 tree stmt;
16225
16226 if (! FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
16227 return NULL_TREE;
16228
16229 if (processing_template_decl)
16230 /* Do nothing now. */;
16231 else
16232 /* Always keep the BLOCK node associated with the outermost pair of
16233 curly braces of a function. These are needed for correct
16234 operation of dwarfout.c. */
16235 keep_next_level (true);
16236
16237 stmt = begin_compound_stmt (BCS_FN_BODY);
16238
16239 if (processing_template_decl)
16240 /* Do nothing now. */;
16241 else if (DECL_DESTRUCTOR_P (current_function_decl))
16242 begin_destructor_body ();
16243
16244 return stmt;
16245 }
16246
16247 /* Do the processing for the end of a function body. Currently, this means
16248 closing out the cleanups for fully-constructed bases and members, and in
16249 the case of the destructor, deleting the object if desired. Again, this
16250 is only meaningful for [cd]tors, since they are the only functions where
16251 there is a significant distinction between the main body and any
16252 function catch clauses. Handling, say, main() return semantics here
16253 would be wrong, as flowing off the end of a function catch clause for
16254 main() would also need to return 0. */
16255
16256 void
16257 finish_function_body (tree compstmt)
16258 {
16259 if (compstmt == NULL_TREE)
16260 return;
16261
16262 /* Close the block. */
16263 finish_compound_stmt (compstmt);
16264
16265 if (processing_template_decl)
16266 /* Do nothing now. */;
16267 else if (DECL_CONSTRUCTOR_P (current_function_decl))
16268 finish_constructor_body ();
16269 else if (DECL_DESTRUCTOR_P (current_function_decl))
16270 finish_destructor_body ();
16271 }
16272
16273 /* Given a function, returns the BLOCK corresponding to the outermost level
16274 of curly braces, skipping the artificial block created for constructor
16275 initializers. */
16276
16277 tree
16278 outer_curly_brace_block (tree fndecl)
16279 {
16280 tree block = DECL_INITIAL (fndecl);
16281 if (BLOCK_OUTER_CURLY_BRACE_P (block))
16282 return block;
16283 block = BLOCK_SUBBLOCKS (block);
16284 if (BLOCK_OUTER_CURLY_BRACE_P (block))
16285 return block;
16286 block = BLOCK_SUBBLOCKS (block);
16287 gcc_assert (BLOCK_OUTER_CURLY_BRACE_P (block));
16288 return block;
16289 }
16290
16291 /* If FNDECL is a class's key method, add the class to the list of
16292 keyed classes that should be emitted. */
16293
16294 static void
16295 record_key_method_defined (tree fndecl)
16296 {
16297 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fndecl)
16298 && DECL_VIRTUAL_P (fndecl)
16299 && !processing_template_decl)
16300 {
16301 tree fnclass = DECL_CONTEXT (fndecl);
16302 if (fndecl == CLASSTYPE_KEY_METHOD (fnclass))
16303 vec_safe_push (keyed_classes, fnclass);
16304 }
16305 }
16306
16307 /* Subroutine of finish_function.
16308 Save the body of constexpr functions for possible
16309 future compile time evaluation. */
16310
16311 static void
16312 maybe_save_function_definition (tree fun)
16313 {
16314 if (!processing_template_decl
16315 && DECL_DECLARED_CONSTEXPR_P (fun)
16316 && !cp_function_chain->invalid_constexpr
16317 && !DECL_CLONED_FUNCTION_P (fun))
16318 register_constexpr_fundef (fun, DECL_SAVED_TREE (fun));
16319 }
16320
16321 /* Attempt to add a fix-it hint to RICHLOC suggesting the insertion
16322 of "return *this;" immediately before its location, using FNDECL's
16323 first statement (if any) to give the indentation, if appropriate. */
16324
16325 static void
16326 add_return_star_this_fixit (gcc_rich_location *richloc, tree fndecl)
16327 {
16328 location_t indent = UNKNOWN_LOCATION;
16329 tree stmts = expr_first (DECL_SAVED_TREE (fndecl));
16330 if (stmts)
16331 indent = EXPR_LOCATION (stmts);
16332 richloc->add_fixit_insert_formatted ("return *this;",
16333 richloc->get_loc (),
16334 indent);
16335 }
16336
16337 /* Finish up a function declaration and compile that function
16338 all the way to assembler language output. The free the storage
16339 for the function definition. INLINE_P is TRUE if we just
16340 finished processing the body of an in-class inline function
16341 definition. (This processing will have taken place after the
16342 class definition is complete.) */
16343
16344 tree
16345 finish_function (bool inline_p)
16346 {
16347 tree fndecl = current_function_decl;
16348 tree fntype, ctype = NULL_TREE;
16349
16350 /* When we get some parse errors, we can end up without a
16351 current_function_decl, so cope. */
16352 if (fndecl == NULL_TREE)
16353 return error_mark_node;
16354
16355 finish_lambda_scope ();
16356
16357 if (c_dialect_objc ())
16358 objc_finish_function ();
16359
16360 record_key_method_defined (fndecl);
16361
16362 fntype = TREE_TYPE (fndecl);
16363
16364 /* TREE_READONLY (fndecl) = 1;
16365 This caused &foo to be of type ptr-to-const-function
16366 which then got a warning when stored in a ptr-to-function variable. */
16367
16368 gcc_assert (building_stmt_list_p ());
16369 /* The current function is being defined, so its DECL_INITIAL should
16370 be set, and unless there's a multiple definition, it should be
16371 error_mark_node. */
16372 gcc_assert (DECL_INITIAL (fndecl) == error_mark_node);
16373
16374 /* For a cloned function, we've already got all the code we need;
16375 there's no need to add any extra bits. */
16376 if (!DECL_CLONED_FUNCTION_P (fndecl))
16377 {
16378 /* Make it so that `main' always returns 0 by default. */
16379 if (DECL_MAIN_P (current_function_decl))
16380 finish_return_stmt (integer_zero_node);
16381
16382 if (use_eh_spec_block (current_function_decl))
16383 finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
16384 (TREE_TYPE (current_function_decl)),
16385 current_eh_spec_block);
16386 }
16387
16388 /* If we're saving up tree structure, tie off the function now. */
16389 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
16390
16391 finish_fname_decls ();
16392
16393 /* If this function can't throw any exceptions, remember that. */
16394 if (!processing_template_decl
16395 && !cp_function_chain->can_throw
16396 && !flag_non_call_exceptions
16397 && !decl_replaceable_p (fndecl))
16398 TREE_NOTHROW (fndecl) = 1;
16399
16400 /* This must come after expand_function_end because cleanups might
16401 have declarations (from inline functions) that need to go into
16402 this function's blocks. */
16403
16404 /* If the current binding level isn't the outermost binding level
16405 for this function, either there is a bug, or we have experienced
16406 syntax errors and the statement tree is malformed. */
16407 if (current_binding_level->kind != sk_function_parms)
16408 {
16409 /* Make sure we have already experienced errors. */
16410 gcc_assert (errorcount);
16411
16412 /* Throw away the broken statement tree and extra binding
16413 levels. */
16414 DECL_SAVED_TREE (fndecl) = alloc_stmt_list ();
16415
16416 while (current_binding_level->kind != sk_function_parms)
16417 {
16418 if (current_binding_level->kind == sk_class)
16419 pop_nested_class ();
16420 else
16421 poplevel (0, 0, 0);
16422 }
16423 }
16424 poplevel (1, 0, 1);
16425
16426 /* Statements should always be full-expressions at the outermost set
16427 of curly braces for a function. */
16428 gcc_assert (stmts_are_full_exprs_p ());
16429
16430 /* If there are no return statements in a function with auto return type,
16431 the return type is void. But if the declared type is something like
16432 auto*, this is an error. */
16433 if (!processing_template_decl && FNDECL_USED_AUTO (fndecl)
16434 && TREE_TYPE (fntype) == DECL_SAVED_AUTO_RETURN_TYPE (fndecl))
16435 {
16436 if (is_auto (DECL_SAVED_AUTO_RETURN_TYPE (fndecl))
16437 && !current_function_returns_value
16438 && !current_function_returns_null)
16439 {
16440 /* We haven't applied return type deduction because we haven't
16441 seen any return statements. Do that now. */
16442 tree node = type_uses_auto (DECL_SAVED_AUTO_RETURN_TYPE (fndecl));
16443 do_auto_deduction (DECL_SAVED_AUTO_RETURN_TYPE (fndecl),
16444 void_node, node, tf_warning_or_error,
16445 adc_return_type);
16446
16447 apply_deduced_return_type (fndecl, void_type_node);
16448 fntype = TREE_TYPE (fndecl);
16449 }
16450 else if (!current_function_returns_value
16451 && !current_function_returns_null)
16452 {
16453 error ("no return statements in function returning %qT",
16454 DECL_SAVED_AUTO_RETURN_TYPE (fndecl));
16455 inform (input_location, "only plain %<auto%> return type can be "
16456 "deduced to %<void%>");
16457 }
16458 }
16459
16460 // If this is a concept, check that the definition is reasonable.
16461 if (DECL_DECLARED_CONCEPT_P (fndecl))
16462 check_function_concept (fndecl);
16463
16464 /* Lambda closure members are implicitly constexpr if possible. */
16465 if (cxx_dialect >= cxx17
16466 && LAMBDA_TYPE_P (CP_DECL_CONTEXT (fndecl)))
16467 DECL_DECLARED_CONSTEXPR_P (fndecl)
16468 = ((processing_template_decl
16469 || is_valid_constexpr_fn (fndecl, /*complain*/false))
16470 && potential_constant_expression (DECL_SAVED_TREE (fndecl)));
16471
16472 /* Save constexpr function body before it gets munged by
16473 the NRV transformation. */
16474 maybe_save_function_definition (fndecl);
16475
16476 /* Invoke the pre-genericize plugin before we start munging things. */
16477 if (!processing_template_decl)
16478 invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE, fndecl);
16479
16480 /* Perform delayed folding before NRV transformation. */
16481 if (!processing_template_decl)
16482 cp_fold_function (fndecl);
16483
16484 /* Set up the named return value optimization, if we can. Candidate
16485 variables are selected in check_return_expr. */
16486 if (current_function_return_value)
16487 {
16488 tree r = current_function_return_value;
16489 tree outer;
16490
16491 if (r != error_mark_node
16492 /* This is only worth doing for fns that return in memory--and
16493 simpler, since we don't have to worry about promoted modes. */
16494 && aggregate_value_p (TREE_TYPE (TREE_TYPE (fndecl)), fndecl)
16495 /* Only allow this for variables declared in the outer scope of
16496 the function so we know that their lifetime always ends with a
16497 return; see g++.dg/opt/nrv6.C. We could be more flexible if
16498 we were to do this optimization in tree-ssa. */
16499 && (outer = outer_curly_brace_block (fndecl))
16500 && chain_member (r, BLOCK_VARS (outer)))
16501 finalize_nrv (&DECL_SAVED_TREE (fndecl), r, DECL_RESULT (fndecl));
16502
16503 current_function_return_value = NULL_TREE;
16504 }
16505
16506 /* Remember that we were in class scope. */
16507 if (current_class_name)
16508 ctype = current_class_type;
16509
16510 /* Must mark the RESULT_DECL as being in this function. */
16511 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
16512
16513 /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point
16514 to the FUNCTION_DECL node itself. */
16515 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
16516
16517 /* Complain if there's just no return statement. */
16518 if (warn_return_type
16519 && !VOID_TYPE_P (TREE_TYPE (fntype))
16520 && !dependent_type_p (TREE_TYPE (fntype))
16521 && !current_function_returns_value && !current_function_returns_null
16522 /* Don't complain if we abort or throw. */
16523 && !current_function_returns_abnormally
16524 /* Don't complain if there's an infinite loop. */
16525 && !current_function_infinite_loop
16526 /* Don't complain if we are declared noreturn. */
16527 && !TREE_THIS_VOLATILE (fndecl)
16528 && !DECL_NAME (DECL_RESULT (fndecl))
16529 && !TREE_NO_WARNING (fndecl)
16530 /* Structor return values (if any) are set by the compiler. */
16531 && !DECL_CONSTRUCTOR_P (fndecl)
16532 && !DECL_DESTRUCTOR_P (fndecl)
16533 && targetm.warn_func_return (fndecl))
16534 {
16535 gcc_rich_location richloc (input_location);
16536 /* Potentially add a "return *this;" fix-it hint for
16537 assignment operators. */
16538 if (IDENTIFIER_ASSIGN_OP_P (DECL_NAME (fndecl)))
16539 {
16540 tree valtype = TREE_TYPE (DECL_RESULT (fndecl));
16541 if (TREE_CODE (valtype) == REFERENCE_TYPE
16542 && current_class_ref
16543 && same_type_ignoring_top_level_qualifiers_p
16544 (TREE_TYPE (valtype), TREE_TYPE (current_class_ref))
16545 && global_dc->option_enabled (OPT_Wreturn_type,
16546 global_dc->lang_mask,
16547 global_dc->option_state))
16548 add_return_star_this_fixit (&richloc, fndecl);
16549 }
16550 if (warning_at (&richloc, OPT_Wreturn_type,
16551 "no return statement in function returning non-void"))
16552 TREE_NO_WARNING (fndecl) = 1;
16553 }
16554
16555 /* Store the end of the function, so that we get good line number
16556 info for the epilogue. */
16557 cfun->function_end_locus = input_location;
16558
16559 /* Complain about parameters that are only set, but never otherwise used. */
16560 if (warn_unused_but_set_parameter
16561 && !processing_template_decl
16562 && errorcount == unused_but_set_errorcount
16563 && !DECL_CLONED_FUNCTION_P (fndecl))
16564 {
16565 tree decl;
16566
16567 for (decl = DECL_ARGUMENTS (fndecl);
16568 decl;
16569 decl = DECL_CHAIN (decl))
16570 if (TREE_USED (decl)
16571 && TREE_CODE (decl) == PARM_DECL
16572 && !DECL_READ_P (decl)
16573 && DECL_NAME (decl)
16574 && !DECL_ARTIFICIAL (decl)
16575 && !TREE_NO_WARNING (decl)
16576 && !DECL_IN_SYSTEM_HEADER (decl)
16577 && TREE_TYPE (decl) != error_mark_node
16578 && !TYPE_REF_P (TREE_TYPE (decl))
16579 && (!CLASS_TYPE_P (TREE_TYPE (decl))
16580 || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl))))
16581 warning_at (DECL_SOURCE_LOCATION (decl),
16582 OPT_Wunused_but_set_parameter,
16583 "parameter %qD set but not used", decl);
16584 unused_but_set_errorcount = errorcount;
16585 }
16586
16587 /* Complain about locally defined typedefs that are not used in this
16588 function. */
16589 maybe_warn_unused_local_typedefs ();
16590
16591 /* Possibly warn about unused parameters. */
16592 if (warn_unused_parameter
16593 && !processing_template_decl
16594 && !DECL_CLONED_FUNCTION_P (fndecl))
16595 do_warn_unused_parameter (fndecl);
16596
16597 /* Genericize before inlining. */
16598 if (!processing_template_decl)
16599 cp_genericize (fndecl);
16600
16601 /* We're leaving the context of this function, so zap cfun. It's still in
16602 DECL_STRUCT_FUNCTION, and we'll restore it in tree_rest_of_compilation. */
16603 set_cfun (NULL);
16604 current_function_decl = NULL;
16605
16606 /* If this is an in-class inline definition, we may have to pop the
16607 bindings for the template parameters that we added in
16608 maybe_begin_member_template_processing when start_function was
16609 called. */
16610 if (inline_p)
16611 maybe_end_member_template_processing ();
16612
16613 /* Leave the scope of the class. */
16614 if (ctype)
16615 pop_nested_class ();
16616
16617 --function_depth;
16618
16619 /* Clean up. */
16620 current_function_decl = NULL_TREE;
16621
16622 invoke_plugin_callbacks (PLUGIN_FINISH_PARSE_FUNCTION, fndecl);
16623 return fndecl;
16624 }
16625 \f
16626 /* Create the FUNCTION_DECL for a function definition.
16627 DECLSPECS and DECLARATOR are the parts of the declaration;
16628 they describe the return type and the name of the function,
16629 but twisted together in a fashion that parallels the syntax of C.
16630
16631 This function creates a binding context for the function body
16632 as well as setting up the FUNCTION_DECL in current_function_decl.
16633
16634 Returns a FUNCTION_DECL on success.
16635
16636 If the DECLARATOR is not suitable for a function (it defines a datum
16637 instead), we return 0, which tells yyparse to report a parse error.
16638
16639 May return void_type_node indicating that this method is actually
16640 a friend. See grokfield for more details.
16641
16642 Came here with a `.pushlevel' .
16643
16644 DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
16645 CHANGES TO CODE IN `grokfield'. */
16646
16647 tree
16648 grokmethod (cp_decl_specifier_seq *declspecs,
16649 const cp_declarator *declarator, tree attrlist)
16650 {
16651 tree fndecl = grokdeclarator (declarator, declspecs, MEMFUNCDEF, 0,
16652 &attrlist);
16653
16654 if (fndecl == error_mark_node)
16655 return error_mark_node;
16656
16657 if (attrlist)
16658 cplus_decl_attributes (&fndecl, attrlist, 0);
16659
16660 /* Pass friends other than inline friend functions back. */
16661 if (fndecl == void_type_node)
16662 return fndecl;
16663
16664 if (DECL_IN_AGGR_P (fndecl))
16665 {
16666 if (DECL_CLASS_SCOPE_P (fndecl))
16667 error ("%qD is already defined in class %qT", fndecl,
16668 DECL_CONTEXT (fndecl));
16669 return error_mark_node;
16670 }
16671
16672 check_template_shadow (fndecl);
16673
16674 if (TREE_PUBLIC (fndecl))
16675 DECL_COMDAT (fndecl) = 1;
16676 DECL_DECLARED_INLINE_P (fndecl) = 1;
16677 DECL_NO_INLINE_WARNING_P (fndecl) = 1;
16678
16679 /* We process method specializations in finish_struct_1. */
16680 if (processing_template_decl && !DECL_TEMPLATE_SPECIALIZATION (fndecl))
16681 {
16682 fndecl = push_template_decl (fndecl);
16683 if (fndecl == error_mark_node)
16684 return fndecl;
16685 }
16686
16687 if (! DECL_FRIEND_P (fndecl))
16688 {
16689 if (DECL_CHAIN (fndecl))
16690 {
16691 fndecl = copy_node (fndecl);
16692 TREE_CHAIN (fndecl) = NULL_TREE;
16693 }
16694 }
16695
16696 cp_finish_decl (fndecl, NULL_TREE, false, NULL_TREE, 0);
16697
16698 DECL_IN_AGGR_P (fndecl) = 1;
16699 return fndecl;
16700 }
16701 \f
16702
16703 /* VAR is a VAR_DECL. If its type is incomplete, remember VAR so that
16704 we can lay it out later, when and if its type becomes complete.
16705
16706 Also handle constexpr variables where the initializer involves
16707 an unlowered PTRMEM_CST because the class isn't complete yet. */
16708
16709 void
16710 maybe_register_incomplete_var (tree var)
16711 {
16712 gcc_assert (VAR_P (var));
16713
16714 /* Keep track of variables with incomplete types. */
16715 if (!processing_template_decl && TREE_TYPE (var) != error_mark_node
16716 && DECL_EXTERNAL (var))
16717 {
16718 tree inner_type = TREE_TYPE (var);
16719
16720 while (TREE_CODE (inner_type) == ARRAY_TYPE)
16721 inner_type = TREE_TYPE (inner_type);
16722 inner_type = TYPE_MAIN_VARIANT (inner_type);
16723
16724 if ((!COMPLETE_TYPE_P (inner_type) && CLASS_TYPE_P (inner_type))
16725 /* RTTI TD entries are created while defining the type_info. */
16726 || (TYPE_LANG_SPECIFIC (inner_type)
16727 && TYPE_BEING_DEFINED (inner_type)))
16728 {
16729 incomplete_var iv = {var, inner_type};
16730 vec_safe_push (incomplete_vars, iv);
16731 }
16732 else if (!(DECL_LANG_SPECIFIC (var) && DECL_TEMPLATE_INFO (var))
16733 && decl_constant_var_p (var)
16734 && (TYPE_PTRMEM_P (inner_type) || CLASS_TYPE_P (inner_type)))
16735 {
16736 /* When the outermost open class is complete we can resolve any
16737 pointers-to-members. */
16738 tree context = outermost_open_class ();
16739 incomplete_var iv = {var, context};
16740 vec_safe_push (incomplete_vars, iv);
16741 }
16742 }
16743 }
16744
16745 /* Called when a class type (given by TYPE) is defined. If there are
16746 any existing VAR_DECLs whose type has been completed by this
16747 declaration, update them now. */
16748
16749 void
16750 complete_vars (tree type)
16751 {
16752 unsigned ix;
16753 incomplete_var *iv;
16754
16755 for (ix = 0; vec_safe_iterate (incomplete_vars, ix, &iv); )
16756 {
16757 if (same_type_p (type, iv->incomplete_type))
16758 {
16759 tree var = iv->decl;
16760 tree type = TREE_TYPE (var);
16761
16762 if (type != error_mark_node
16763 && (TYPE_MAIN_VARIANT (strip_array_types (type))
16764 == iv->incomplete_type))
16765 {
16766 /* Complete the type of the variable. The VAR_DECL itself
16767 will be laid out in expand_expr. */
16768 complete_type (type);
16769 cp_apply_type_quals_to_decl (cp_type_quals (type), var);
16770 }
16771
16772 /* Remove this entry from the list. */
16773 incomplete_vars->unordered_remove (ix);
16774 }
16775 else
16776 ix++;
16777 }
16778
16779 /* Check for pending declarations which may have abstract type. */
16780 complete_type_check_abstract (type);
16781 }
16782
16783 /* If DECL is of a type which needs a cleanup, build and return an
16784 expression to perform that cleanup here. Return NULL_TREE if no
16785 cleanup need be done. DECL can also be a _REF when called from
16786 split_nonconstant_init_1. */
16787
16788 tree
16789 cxx_maybe_build_cleanup (tree decl, tsubst_flags_t complain)
16790 {
16791 tree type;
16792 tree attr;
16793 tree cleanup;
16794
16795 /* Assume no cleanup is required. */
16796 cleanup = NULL_TREE;
16797
16798 if (error_operand_p (decl))
16799 return cleanup;
16800
16801 /* Handle "__attribute__((cleanup))". We run the cleanup function
16802 before the destructor since the destructor is what actually
16803 terminates the lifetime of the object. */
16804 if (DECL_P (decl))
16805 attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
16806 else
16807 attr = NULL_TREE;
16808 if (attr)
16809 {
16810 tree id;
16811 tree fn;
16812 tree arg;
16813
16814 /* Get the name specified by the user for the cleanup function. */
16815 id = TREE_VALUE (TREE_VALUE (attr));
16816 /* Look up the name to find the cleanup function to call. It is
16817 important to use lookup_name here because that is what is
16818 used in c-common.c:handle_cleanup_attribute when performing
16819 initial checks on the attribute. Note that those checks
16820 include ensuring that the function found is not an overloaded
16821 function, or an object with an overloaded call operator,
16822 etc.; we can rely on the fact that the function found is an
16823 ordinary FUNCTION_DECL. */
16824 fn = lookup_name (id);
16825 arg = build_address (decl);
16826 if (!mark_used (decl, complain) && !(complain & tf_error))
16827 return error_mark_node;
16828 cleanup = cp_build_function_call_nary (fn, complain, arg, NULL_TREE);
16829 if (cleanup == error_mark_node)
16830 return error_mark_node;
16831 }
16832 /* Handle ordinary C++ destructors. */
16833 type = TREE_TYPE (decl);
16834 if (type_build_dtor_call (type))
16835 {
16836 int flags = LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR;
16837 tree addr;
16838 tree call;
16839
16840 if (TREE_CODE (type) == ARRAY_TYPE)
16841 addr = decl;
16842 else
16843 addr = build_address (decl);
16844
16845 call = build_delete (TREE_TYPE (addr), addr,
16846 sfk_complete_destructor, flags, 0, complain);
16847 if (call == error_mark_node)
16848 cleanup = error_mark_node;
16849 else if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
16850 /* Discard the call. */;
16851 else if (decl_maybe_constant_destruction (decl, type)
16852 && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
16853 cxx_constant_dtor (call, decl);
16854 else if (cleanup)
16855 cleanup = cp_build_compound_expr (cleanup, call, complain);
16856 else
16857 cleanup = call;
16858 }
16859
16860 /* build_delete sets the location of the destructor call to the
16861 current location, even though the destructor is going to be
16862 called later, at the end of the current scope. This can lead to
16863 a "jumpy" behavior for users of debuggers when they step around
16864 the end of the block. So let's unset the location of the
16865 destructor call instead. */
16866 protected_set_expr_location (cleanup, UNKNOWN_LOCATION);
16867
16868 if (cleanup
16869 && DECL_P (decl)
16870 && !lookup_attribute ("warn_unused", TYPE_ATTRIBUTES (TREE_TYPE (decl)))
16871 /* Treat objects with destructors as used; the destructor may do
16872 something substantive. */
16873 && !mark_used (decl, complain) && !(complain & tf_error))
16874 return error_mark_node;
16875
16876 return cleanup;
16877 }
16878
16879 \f
16880 /* Return the FUNCTION_TYPE that corresponds to MEMFNTYPE, which can be a
16881 FUNCTION_DECL, METHOD_TYPE, FUNCTION_TYPE, pointer or reference to
16882 METHOD_TYPE or FUNCTION_TYPE, or pointer to member function. */
16883
16884 tree
16885 static_fn_type (tree memfntype)
16886 {
16887 tree fntype;
16888 tree args;
16889
16890 if (TYPE_PTRMEMFUNC_P (memfntype))
16891 memfntype = TYPE_PTRMEMFUNC_FN_TYPE (memfntype);
16892 if (INDIRECT_TYPE_P (memfntype)
16893 || TREE_CODE (memfntype) == FUNCTION_DECL)
16894 memfntype = TREE_TYPE (memfntype);
16895 if (TREE_CODE (memfntype) == FUNCTION_TYPE)
16896 return memfntype;
16897 gcc_assert (TREE_CODE (memfntype) == METHOD_TYPE);
16898 args = TYPE_ARG_TYPES (memfntype);
16899 fntype = build_function_type (TREE_TYPE (memfntype), TREE_CHAIN (args));
16900 fntype = apply_memfn_quals (fntype, type_memfn_quals (memfntype));
16901 fntype = (cp_build_type_attribute_variant
16902 (fntype, TYPE_ATTRIBUTES (memfntype)));
16903 fntype = cxx_copy_lang_qualifiers (fntype, memfntype);
16904 return fntype;
16905 }
16906
16907 /* DECL was originally constructed as a non-static member function,
16908 but turned out to be static. Update it accordingly. */
16909
16910 void
16911 revert_static_member_fn (tree decl)
16912 {
16913 tree stype = static_fn_type (decl);
16914 cp_cv_quals quals = type_memfn_quals (stype);
16915 cp_ref_qualifier rqual = type_memfn_rqual (stype);
16916
16917 if (quals != TYPE_UNQUALIFIED || rqual != REF_QUAL_NONE)
16918 stype = apply_memfn_quals (stype, TYPE_UNQUALIFIED, REF_QUAL_NONE);
16919
16920 TREE_TYPE (decl) = stype;
16921
16922 if (DECL_ARGUMENTS (decl))
16923 DECL_ARGUMENTS (decl) = DECL_CHAIN (DECL_ARGUMENTS (decl));
16924 DECL_STATIC_FUNCTION_P (decl) = 1;
16925 }
16926
16927 /* Return which tree structure is used by T, or TS_CP_GENERIC if T is
16928 one of the language-independent trees. */
16929
16930 enum cp_tree_node_structure_enum
16931 cp_tree_node_structure (union lang_tree_node * t)
16932 {
16933 switch (TREE_CODE (&t->generic))
16934 {
16935 case ARGUMENT_PACK_SELECT: return TS_CP_ARGUMENT_PACK_SELECT;
16936 case BASELINK: return TS_CP_BASELINK;
16937 case CONSTRAINT_INFO: return TS_CP_CONSTRAINT_INFO;
16938 case DEFERRED_NOEXCEPT: return TS_CP_DEFERRED_NOEXCEPT;
16939 case DEFERRED_PARSE: return TS_CP_DEFERRED_PARSE;
16940 case IDENTIFIER_NODE: return TS_CP_IDENTIFIER;
16941 case LAMBDA_EXPR: return TS_CP_LAMBDA_EXPR;
16942 case OVERLOAD: return TS_CP_OVERLOAD;
16943 case PTRMEM_CST: return TS_CP_PTRMEM;
16944 case STATIC_ASSERT: return TS_CP_STATIC_ASSERT;
16945 case TEMPLATE_DECL: return TS_CP_TEMPLATE_DECL;
16946 case TEMPLATE_INFO: return TS_CP_TEMPLATE_INFO;
16947 case TEMPLATE_PARM_INDEX: return TS_CP_TPI;
16948 case TRAIT_EXPR: return TS_CP_TRAIT_EXPR;
16949 case USERDEF_LITERAL: return TS_CP_USERDEF_LITERAL;
16950 default: return TS_CP_GENERIC;
16951 }
16952 }
16953
16954 /* Build the void_list_node (void_type_node having been created). */
16955 tree
16956 build_void_list_node (void)
16957 {
16958 tree t = build_tree_list (NULL_TREE, void_type_node);
16959 return t;
16960 }
16961
16962 bool
16963 cp_missing_noreturn_ok_p (tree decl)
16964 {
16965 /* A missing noreturn is ok for the `main' function. */
16966 return DECL_MAIN_P (decl);
16967 }
16968
16969 /* Return the decl used to identify the COMDAT group into which DECL should
16970 be placed. */
16971
16972 tree
16973 cxx_comdat_group (tree decl)
16974 {
16975 /* Virtual tables, construction virtual tables, and virtual table
16976 tables all go in a single COMDAT group, named after the primary
16977 virtual table. */
16978 if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl))
16979 decl = CLASSTYPE_VTABLES (DECL_CONTEXT (decl));
16980 /* For all other DECLs, the COMDAT group is the mangled name of the
16981 declaration itself. */
16982 else
16983 {
16984 while (DECL_THUNK_P (decl))
16985 {
16986 /* If TARGET_USE_LOCAL_THUNK_ALIAS_P, use_thunk puts the thunk
16987 into the same section as the target function. In that case
16988 we must return target's name. */
16989 tree target = THUNK_TARGET (decl);
16990 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (target)
16991 && DECL_SECTION_NAME (target) != NULL
16992 && DECL_ONE_ONLY (target))
16993 decl = target;
16994 else
16995 break;
16996 }
16997 }
16998
16999 return decl;
17000 }
17001
17002 /* Returns the return type for FN as written by the user, which may include
17003 a placeholder for a deduced return type. */
17004
17005 tree
17006 fndecl_declared_return_type (tree fn)
17007 {
17008 fn = STRIP_TEMPLATE (fn);
17009 if (FNDECL_USED_AUTO (fn))
17010 return DECL_SAVED_AUTO_RETURN_TYPE (fn);
17011
17012 return TREE_TYPE (TREE_TYPE (fn));
17013 }
17014
17015 /* Returns true iff DECL is a variable or function declared with an auto type
17016 that has not yet been deduced to a real type. */
17017
17018 bool
17019 undeduced_auto_decl (tree decl)
17020 {
17021 if (cxx_dialect < cxx11)
17022 return false;
17023 STRIP_ANY_LOCATION_WRAPPER (decl);
17024 return ((VAR_OR_FUNCTION_DECL_P (decl)
17025 || TREE_CODE (decl) == TEMPLATE_DECL)
17026 && type_uses_auto (TREE_TYPE (decl)));
17027 }
17028
17029 /* Complain if DECL has an undeduced return type. */
17030
17031 bool
17032 require_deduced_type (tree decl, tsubst_flags_t complain)
17033 {
17034 if (undeduced_auto_decl (decl))
17035 {
17036 if (TREE_NO_WARNING (decl) && seen_error ())
17037 /* We probably already complained about deduction failure. */;
17038 else if (complain & tf_error)
17039 error ("use of %qD before deduction of %<auto%>", decl);
17040 return false;
17041 }
17042 return true;
17043 }
17044
17045 /* Create a representation of the explicit-specifier with
17046 constant-expression of EXPR. COMPLAIN is as for tsubst. */
17047
17048 tree
17049 build_explicit_specifier (tree expr, tsubst_flags_t complain)
17050 {
17051 if (instantiation_dependent_expression_p (expr))
17052 /* Wait for instantiation, tsubst_function_decl will handle it. */
17053 return expr;
17054
17055 expr = instantiate_non_dependent_expr_sfinae (expr, complain);
17056 /* Don't let convert_like_real create more template codes. */
17057 processing_template_decl_sentinel s;
17058 expr = build_converted_constant_bool_expr (expr, complain);
17059 expr = cxx_constant_value (expr);
17060 return expr;
17061 }
17062
17063 #include "gt-cp-decl.h"