1 /* Process declarations and variables for -*- C++ -*- compiler.
2 Copyright (C) 1988-2022 Free Software Foundation, Inc.
3 Contributed by Michael Tiemann (tiemann@cygnus.com)
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
20
21
22 /* Process declarations and symbol lookup for C++ front end.
23 Also constructs types; the standard scalar types at initialization,
24 and structure, union, array and enum types when they are declared. */
25
26 /* ??? not all decl nodes are given the most useful possible
27 line numbers. For example, the CONST_DECLs for enum values. */
28
29 #include "config.h"
30 #include "system.h"
31 #include "coretypes.h"
32 #include "target.h"
33 #include "c-family/c-target.h"
34 #include "cp-tree.h"
35 #include "timevar.h"
36 #include "stringpool.h"
37 #include "cgraph.h"
38 #include "stor-layout.h"
39 #include "varasm.h"
40 #include "attribs.h"
41 #include "flags.h"
42 #include "tree-iterator.h"
43 #include "decl.h"
44 #include "intl.h"
45 #include "toplev.h"
46 #include "c-family/c-objc.h"
47 #include "c-family/c-pragma.h"
48 #include "c-family/c-ubsan.h"
49 #include "debug.h"
50 #include "plugin.h"
51 #include "builtins.h"
52 #include "gimplify.h"
53 #include "asan.h"
54 #include "gcc-rich-location.h"
55 #include "langhooks.h"
56 #include "context.h" /* For 'g'. */
57 #include "omp-general.h"
58 #include "omp-offload.h" /* For offload_vars. */
59 #include "opts.h"
60 #include "langhooks-def.h" /* For lhd_simulate_record_decl */
61
62 /* Possible cases of bad specifiers type used by bad_specifiers. */
63 enum bad_spec_place {
64 BSP_VAR, /* variable */
65 BSP_PARM, /* parameter */
66 BSP_TYPE, /* type */
67 BSP_FIELD /* field */
68 };
69
70 static const char *redeclaration_error_message (tree, tree);
71
72 static int decl_jump_unsafe (tree);
73 static void require_complete_types_for_parms (tree);
74 static tree grok_reference_init (tree, tree, tree, int);
75 static tree grokvardecl (tree, tree, tree, const cp_decl_specifier_seq *,
76 int, int, int, bool, int, tree, location_t);
77 static void check_static_variable_definition (tree, tree);
78 static void record_unknown_type (tree, const char *);
79 static int member_function_or_else (tree, tree, enum overload_flags);
80 static tree local_variable_p_walkfn (tree *, int *, void *);
81 static const char *tag_name (enum tag_types);
82 static tree lookup_and_check_tag (enum tag_types, tree, TAG_how, bool);
83 static void maybe_deduce_size_from_array_init (tree, tree);
84 static void layout_var_decl (tree);
85 static tree check_initializer (tree, tree, int, vec<tree, va_gc> **);
86 static void make_rtl_for_nonlocal_decl (tree, tree, const char *);
87 static void copy_type_enum (tree , tree);
88 static void check_function_type (tree, tree);
89 static void finish_constructor_body (void);
90 static void begin_destructor_body (void);
91 static void finish_destructor_body (void);
92 static void record_key_method_defined (tree);
93 static tree create_array_type_for_decl (tree, tree, tree, location_t);
94 static tree get_atexit_node (void);
95 static tree get_dso_handle_node (void);
96 static tree start_cleanup_fn (void);
97 static void end_cleanup_fn (void);
98 static tree cp_make_fname_decl (location_t, tree, int);
99 static void initialize_predefined_identifiers (void);
100 static tree check_special_function_return_type
101 (special_function_kind, tree, tree, int, const location_t*);
102 static tree push_cp_library_fn (enum tree_code, tree, int);
103 static tree build_cp_library_fn (tree, enum tree_code, tree, int);
104 static void store_parm_decls (tree);
105 static void initialize_local_var (tree, tree);
106 static void expand_static_init (tree, tree);
107 static location_t smallest_type_location (const cp_decl_specifier_seq*);
108
109 /* The following symbols are subsumed in the cp_global_trees array, and
110 listed here individually for documentation purposes.
111
112 C++ extensions
113 tree wchar_decl_node;
114
115 tree vtable_entry_type;
116 tree delta_type_node;
117 tree __t_desc_type_node;
118
119 tree class_type_node;
120 tree unknown_type_node;
121
122 Array type `vtable_entry_type[]'
123
124 tree vtbl_type_node;
125 tree vtbl_ptr_type_node;
126
127 Namespaces,
128
129 tree std_node;
130 tree abi_node;
131
132 A FUNCTION_DECL which can call `abort'. Not necessarily the
133 one that the user will declare, but sufficient to be called
134 by routines that want to abort the program.
135
136 tree abort_fndecl;
137
138 Used by RTTI
139 tree type_info_type_node, tinfo_decl_id, tinfo_decl_type;
140 tree tinfo_var_id; */
141
142 tree cp_global_trees[CPTI_MAX];
143
144 /* A list of objects which have constructors or destructors
145 which reside in namespace scope. The decl is stored in
146 the TREE_VALUE slot and the initializer is stored
147 in the TREE_PURPOSE slot. */
148 tree static_aggregates;
149
150 /* Like static_aggregates, but for thread_local variables. */
151 tree tls_aggregates;
152
153 /* A hash-map mapping from variable decls to the dynamic initializer for
154 the decl. This is currently only used by OpenMP. */
155 decl_tree_map *dynamic_initializers;
156
157 /* -- end of C++ */
158
159 /* A node for the integer constant 2. */
160
161 tree integer_two_node;
162
163 /* vector of static decls. */
164 vec<tree, va_gc> *static_decls;
165
166 /* vector of keyed classes. */
167 vec<tree, va_gc> *keyed_classes;
168
169 /* Used only for jumps to as-yet undefined labels, since jumps to
170 defined labels can have their validity checked immediately. */
171
172 struct GTY((chain_next ("%h.next"))) named_label_use_entry {
173 struct named_label_use_entry *next;
174 /* The binding level to which this entry is *currently* attached.
175 This is initially the binding level in which the goto appeared,
176 but is modified as scopes are closed. */
177 cp_binding_level *binding_level;
178 /* The head of the names list that was current when the goto appeared,
179 or the inner scope popped. These are the decls that will *not* be
180 skipped when jumping to the label. */
181 tree names_in_scope;
182 /* The location of the goto, for error reporting. */
183 location_t o_goto_locus;
184 /* True if an OpenMP structured block scope has been closed since
185 the goto appeared. This means that the branch from the label will
186 illegally exit an OpenMP scope. */
187 bool in_omp_scope;
188 };
189
190 /* A list of all LABEL_DECLs in the function that have names. Here so
191 we can clear out their names' definitions at the end of the
192 function, and so we can check the validity of jumps to these labels. */
193
194 struct GTY((for_user)) named_label_entry {
195
196 tree name; /* Name of decl. */
197
198 tree label_decl; /* LABEL_DECL, unless deleted local label. */
199
200 named_label_entry *outer; /* Outer shadowed chain. */
201
202 /* The binding level to which the label is *currently* attached.
203 This is initially set to the binding level in which the label
204 is defined, but is modified as scopes are closed. */
205 cp_binding_level *binding_level;
206
207 /* The head of the names list that was current when the label was
208 defined, or the inner scope popped. These are the decls that will
209 be skipped when jumping to the label. */
210 tree names_in_scope;
211
212 /* A vector of all decls from all binding levels that would be
213 crossed by a backward branch to the label. */
214 vec<tree, va_gc> *bad_decls;
215
216 /* A list of uses of the label, before the label is defined. */
217 named_label_use_entry *uses;
218
219 /* The following bits are set after the label is defined, and are
220 updated as scopes are popped. They indicate that a jump to the
221 label will illegally enter a scope of the given flavor. */
222 bool in_try_scope;
223 bool in_catch_scope;
224 bool in_omp_scope;
225 bool in_transaction_scope;
226 bool in_constexpr_if;
227 bool in_consteval_if;
228 };
229
230 #define named_labels cp_function_chain->x_named_labels
231
232 /* The number of function bodies which we are currently processing.
233 (Zero if we are at namespace scope, one inside the body of a
234 function, two inside the body of a function in a local class, etc.) */
235 int function_depth;
236
237 /* Whether the exception-specifier is part of a function type (i.e. C++17). */
238 bool flag_noexcept_type;
239
240 /* States indicating how grokdeclarator() should handle declspecs marked
241 with __attribute__((deprecated)). An object declared as
242 __attribute__((deprecated)) suppresses warnings of uses of other
243 deprecated items. */
244 enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
245
246
247 /* A list of VAR_DECLs whose type was incomplete at the time the
248 variable was declared. */
249
250 struct GTY(()) incomplete_var {
251 tree decl;
252 tree incomplete_type;
253 };
254
255
256 static GTY(()) vec<incomplete_var, va_gc> *incomplete_vars;
257
258 /* Returns the kind of template specialization we are currently
259 processing, given that it's declaration contained N_CLASS_SCOPES
260 explicit scope qualifications. */
261
262 tmpl_spec_kind
current_tmpl_spec_kind(int n_class_scopes)263 current_tmpl_spec_kind (int n_class_scopes)
264 {
265 int n_template_parm_scopes = 0;
266 int seen_specialization_p = 0;
267 int innermost_specialization_p = 0;
268 cp_binding_level *b;
269
270 /* Scan through the template parameter scopes. */
271 for (b = current_binding_level;
272 b->kind == sk_template_parms;
273 b = b->level_chain)
274 {
275 /* If we see a specialization scope inside a parameter scope,
276 then something is wrong. That corresponds to a declaration
277 like:
278
279 template <class T> template <> ...
280
281 which is always invalid since [temp.expl.spec] forbids the
282 specialization of a class member template if the enclosing
283 class templates are not explicitly specialized as well. */
284 if (b->explicit_spec_p)
285 {
286 if (n_template_parm_scopes == 0)
287 innermost_specialization_p = 1;
288 else
289 seen_specialization_p = 1;
290 }
291 else if (seen_specialization_p == 1)
292 return tsk_invalid_member_spec;
293
294 ++n_template_parm_scopes;
295 }
296
297 /* Handle explicit instantiations. */
298 if (processing_explicit_instantiation)
299 {
300 if (n_template_parm_scopes != 0)
301 /* We've seen a template parameter list during an explicit
302 instantiation. For example:
303
304 template <class T> template void f(int);
305
306 This is erroneous. */
307 return tsk_invalid_expl_inst;
308 else
309 return tsk_expl_inst;
310 }
311
312 if (n_template_parm_scopes < n_class_scopes)
313 /* We've not seen enough template headers to match all the
314 specialized classes present. For example:
315
316 template <class T> void R<T>::S<T>::f(int);
317
318 This is invalid; there needs to be one set of template
319 parameters for each class. */
320 return tsk_insufficient_parms;
321 else if (n_template_parm_scopes == n_class_scopes)
322 /* We're processing a non-template declaration (even though it may
323 be a member of a template class.) For example:
324
325 template <class T> void S<T>::f(int);
326
327 The `class T' matches the `S<T>', leaving no template headers
328 corresponding to the `f'. */
329 return tsk_none;
330 else if (n_template_parm_scopes > n_class_scopes + 1)
331 /* We've got too many template headers. For example:
332
333 template <> template <class T> void f (T);
334
335 There need to be more enclosing classes. */
336 return tsk_excessive_parms;
337 else
338 /* This must be a template. It's of the form:
339
340 template <class T> template <class U> void S<T>::f(U);
341
342 This is a specialization if the innermost level was a
343 specialization; otherwise it's just a definition of the
344 template. */
345 return innermost_specialization_p ? tsk_expl_spec : tsk_template;
346 }
347
348 /* Exit the current scope. */
349
350 void
finish_scope(void)351 finish_scope (void)
352 {
353 poplevel (0, 0, 0);
354 }
355
356 /* When a label goes out of scope, check to see if that label was used
357 in a valid manner, and issue any appropriate warnings or errors. */
358
359 static void
check_label_used(tree label)360 check_label_used (tree label)
361 {
362 if (!processing_template_decl)
363 {
364 if (DECL_INITIAL (label) == NULL_TREE)
365 {
366 location_t location;
367
368 error ("label %q+D used but not defined", label);
369 location = input_location;
370 /* FIXME want (LOCATION_FILE (input_location), (line)0) */
371 /* Avoid crashing later. */
372 define_label (location, DECL_NAME (label));
373 }
374 else
375 warn_for_unused_label (label);
376 }
377 }
378
379 /* Helper function to sort named label entries in a vector by DECL_UID. */
380
381 static int
sort_labels(const void * a,const void * b)382 sort_labels (const void *a, const void *b)
383 {
384 tree label1 = *(tree const *) a;
385 tree label2 = *(tree const *) b;
386
387 /* DECL_UIDs can never be equal. */
388 return DECL_UID (label1) > DECL_UID (label2) ? -1 : +1;
389 }
390
391 /* At the end of a function, all labels declared within the function
392 go out of scope. BLOCK is the top-level block for the
393 function. */
394
395 static void
pop_labels(tree block)396 pop_labels (tree block)
397 {
398 if (!named_labels)
399 return;
400
401 /* We need to add the labels to the block chain, so debug
402 information is emitted. But, we want the order to be stable so
403 need to sort them first. Otherwise the debug output could be
404 randomly ordered. I guess it's mostly stable, unless the hash
405 table implementation changes. */
406 auto_vec<tree, 32> labels (named_labels->elements ());
407 hash_table<named_label_hash>::iterator end (named_labels->end ());
408 for (hash_table<named_label_hash>::iterator iter
409 (named_labels->begin ()); iter != end; ++iter)
410 {
411 named_label_entry *ent = *iter;
412
413 gcc_checking_assert (!ent->outer);
414 if (ent->label_decl)
415 labels.quick_push (ent->label_decl);
416 ggc_free (ent);
417 }
418 named_labels = NULL;
419 labels.qsort (sort_labels);
420
421 while (labels.length ())
422 {
423 tree label = labels.pop ();
424
425 DECL_CHAIN (label) = BLOCK_VARS (block);
426 BLOCK_VARS (block) = label;
427
428 check_label_used (label);
429 }
430 }
431
432 /* At the end of a block with local labels, restore the outer definition. */
433
434 static void
pop_local_label(tree id,tree label)435 pop_local_label (tree id, tree label)
436 {
437 check_label_used (label);
438 named_label_entry **slot = named_labels->find_slot_with_hash
439 (id, IDENTIFIER_HASH_VALUE (id), NO_INSERT);
440 named_label_entry *ent = *slot;
441
442 if (ent->outer)
443 ent = ent->outer;
444 else
445 {
446 ent = ggc_cleared_alloc<named_label_entry> ();
447 ent->name = id;
448 }
449 *slot = ent;
450 }
451
452 /* The following two routines are used to interface to Objective-C++.
453 The binding level is purposely treated as an opaque type. */
454
455 void *
objc_get_current_scope(void)456 objc_get_current_scope (void)
457 {
458 return current_binding_level;
459 }
460
461 /* The following routine is used by the NeXT-style SJLJ exceptions;
462 variables get marked 'volatile' so as to not be clobbered by
463 _setjmp()/_longjmp() calls. All variables in the current scope,
464 as well as parent scopes up to (but not including) ENCLOSING_BLK
465 shall be thusly marked. */
466
467 void
objc_mark_locals_volatile(void * enclosing_blk)468 objc_mark_locals_volatile (void *enclosing_blk)
469 {
470 cp_binding_level *scope;
471
472 for (scope = current_binding_level;
473 scope && scope != enclosing_blk;
474 scope = scope->level_chain)
475 {
476 tree decl;
477
478 for (decl = scope->names; decl; decl = TREE_CHAIN (decl))
479 objc_volatilize_decl (decl);
480
481 /* Do not climb up past the current function. */
482 if (scope->kind == sk_function_parms)
483 break;
484 }
485 }
486
487 /* True if B is the level for the condition of a constexpr if. */
488
489 static bool
level_for_constexpr_if(cp_binding_level * b)490 level_for_constexpr_if (cp_binding_level *b)
491 {
492 return (b->kind == sk_cond && b->this_entity
493 && TREE_CODE (b->this_entity) == IF_STMT
494 && IF_STMT_CONSTEXPR_P (b->this_entity));
495 }
496
497 /* True if B is the level for the condition of a consteval if. */
498
499 static bool
level_for_consteval_if(cp_binding_level * b)500 level_for_consteval_if (cp_binding_level *b)
501 {
502 return (b->kind == sk_cond && b->this_entity
503 && TREE_CODE (b->this_entity) == IF_STMT
504 && IF_STMT_CONSTEVAL_P (b->this_entity));
505 }
506
507 /* Update data for defined and undefined labels when leaving a scope. */
508
509 int
poplevel_named_label_1(named_label_entry ** slot,cp_binding_level * bl)510 poplevel_named_label_1 (named_label_entry **slot, cp_binding_level *bl)
511 {
512 named_label_entry *ent = *slot;
513 cp_binding_level *obl = bl->level_chain;
514
515 if (ent->binding_level == bl)
516 {
517 tree decl;
518
519 /* ENT->NAMES_IN_SCOPE may contain a mixture of DECLs and
520 TREE_LISTs representing OVERLOADs, so be careful. */
521 for (decl = ent->names_in_scope; decl; decl = (DECL_P (decl)
522 ? DECL_CHAIN (decl)
523 : TREE_CHAIN (decl)))
524 if (decl_jump_unsafe (decl))
525 vec_safe_push (ent->bad_decls, decl);
526
527 ent->binding_level = obl;
528 ent->names_in_scope = obl->names;
529 switch (bl->kind)
530 {
531 case sk_try:
532 ent->in_try_scope = true;
533 break;
534 case sk_catch:
535 ent->in_catch_scope = true;
536 break;
537 case sk_omp:
538 ent->in_omp_scope = true;
539 break;
540 case sk_transaction:
541 ent->in_transaction_scope = true;
542 break;
543 case sk_block:
544 if (level_for_constexpr_if (bl->level_chain))
545 ent->in_constexpr_if = true;
546 else if (level_for_consteval_if (bl->level_chain))
547 ent->in_consteval_if = true;
548 break;
549 default:
550 break;
551 }
552 }
553 else if (ent->uses)
554 {
555 struct named_label_use_entry *use;
556
557 for (use = ent->uses; use ; use = use->next)
558 if (use->binding_level == bl)
559 {
560 use->binding_level = obl;
561 use->names_in_scope = obl->names;
562 if (bl->kind == sk_omp)
563 use->in_omp_scope = true;
564 }
565 }
566
567 return 1;
568 }
569
570 /* Saved errorcount to avoid -Wunused-but-set-{parameter,variable} warnings
571 when errors were reported, except for -Werror-unused-but-set-*. */
572 static int unused_but_set_errorcount;
573
574 /* Exit a binding level.
575 Pop the level off, and restore the state of the identifier-decl mappings
576 that were in effect when this level was entered.
577
578 If KEEP == 1, this level had explicit declarations, so
579 and create a "block" (a BLOCK node) for the level
580 to record its declarations and subblocks for symbol table output.
581
582 If FUNCTIONBODY is nonzero, this level is the body of a function,
583 so create a block as if KEEP were set and also clear out all
584 label names.
585
586 If REVERSE is nonzero, reverse the order of decls before putting
587 them into the BLOCK. */
588
589 tree
poplevel(int keep,int reverse,int functionbody)590 poplevel (int keep, int reverse, int functionbody)
591 {
592 tree link;
593 /* The chain of decls was accumulated in reverse order.
594 Put it into forward order, just for cleanliness. */
595 tree decls;
596 tree subblocks;
597 tree block;
598 tree decl;
599 scope_kind kind;
600
601 auto_cond_timevar tv (TV_NAME_LOOKUP);
602 restart:
603
604 block = NULL_TREE;
605
606 gcc_assert (current_binding_level->kind != sk_class
607 && current_binding_level->kind != sk_namespace);
608
609 if (current_binding_level->kind == sk_cleanup)
610 functionbody = 0;
611 subblocks = functionbody >= 0 ? current_binding_level->blocks : 0;
612
613 gcc_assert (!vec_safe_length (current_binding_level->class_shadowed));
614
615 /* We used to use KEEP == 2 to indicate that the new block should go
616 at the beginning of the list of blocks at this binding level,
617 rather than the end. This hack is no longer used. */
618 gcc_assert (keep == 0 || keep == 1);
619
620 if (current_binding_level->keep)
621 keep = 1;
622
623 /* Any uses of undefined labels, and any defined labels, now operate
624 under constraints of next binding contour. */
625 if (cfun && !functionbody && named_labels)
626 named_labels->traverse<cp_binding_level *, poplevel_named_label_1>
627 (current_binding_level);
628
629 /* Get the decls in the order they were written.
630 Usually current_binding_level->names is in reverse order.
631 But parameter decls were previously put in forward order. */
632
633 decls = current_binding_level->names;
634 if (reverse)
635 {
636 decls = nreverse (decls);
637 current_binding_level->names = decls;
638 }
639
640 /* If there were any declarations or structure tags in that level,
641 or if this level is a function body,
642 create a BLOCK to record them for the life of this function. */
643 block = NULL_TREE;
644 /* Avoid function body block if possible. */
645 if (functionbody && subblocks && BLOCK_CHAIN (subblocks) == NULL_TREE)
646 keep = 0;
647 else if (keep == 1 || functionbody)
648 block = make_node (BLOCK);
649 if (block != NULL_TREE)
650 {
651 BLOCK_VARS (block) = decls;
652 BLOCK_SUBBLOCKS (block) = subblocks;
653 }
654
655 /* In each subblock, record that this is its superior. */
656 if (keep >= 0)
657 for (link = subblocks; link; link = BLOCK_CHAIN (link))
658 BLOCK_SUPERCONTEXT (link) = block;
659
660 /* Before we remove the declarations first check for unused variables. */
661 if ((warn_unused_variable || warn_unused_but_set_variable)
662 && current_binding_level->kind != sk_template_parms
663 && !processing_template_decl)
664 for (tree d = get_local_decls (); d; d = TREE_CHAIN (d))
665 {
666 /* There are cases where D itself is a TREE_LIST. See in
667 push_local_binding where the list of decls returned by
668 getdecls is built. */
669 decl = TREE_CODE (d) == TREE_LIST ? TREE_VALUE (d) : d;
670
671 tree type = TREE_TYPE (decl);
672 if (VAR_P (decl)
673 && (! TREE_USED (decl) || !DECL_READ_P (decl))
674 && ! DECL_IN_SYSTEM_HEADER (decl)
675 /* For structured bindings, consider only real variables, not
676 subobjects. */
677 && (DECL_DECOMPOSITION_P (decl) ? !DECL_DECOMP_BASE (decl)
678 : (DECL_NAME (decl) && !DECL_ARTIFICIAL (decl)))
679 && type != error_mark_node
680 && (!CLASS_TYPE_P (type)
681 || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
682 || lookup_attribute ("warn_unused",
683 TYPE_ATTRIBUTES (TREE_TYPE (decl)))))
684 {
685 if (! TREE_USED (decl))
686 {
687 if (!DECL_NAME (decl) && DECL_DECOMPOSITION_P (decl))
688 warning_at (DECL_SOURCE_LOCATION (decl),
689 OPT_Wunused_variable,
690 "unused structured binding declaration");
691 else
692 warning_at (DECL_SOURCE_LOCATION (decl),
693 OPT_Wunused_variable, "unused variable %qD", decl);
694 suppress_warning (decl, OPT_Wunused_variable);
695 }
696 else if (DECL_CONTEXT (decl) == current_function_decl
697 // For -Wunused-but-set-variable leave references alone.
698 && !TYPE_REF_P (TREE_TYPE (decl))
699 && errorcount == unused_but_set_errorcount)
700 {
701 if (!DECL_NAME (decl) && DECL_DECOMPOSITION_P (decl))
702 warning_at (DECL_SOURCE_LOCATION (decl),
703 OPT_Wunused_but_set_variable, "structured "
704 "binding declaration set but not used");
705 else
706 warning_at (DECL_SOURCE_LOCATION (decl),
707 OPT_Wunused_but_set_variable,
708 "variable %qD set but not used", decl);
709 unused_but_set_errorcount = errorcount;
710 }
711 }
712 }
713
714 /* Remove declarations for all the DECLs in this level. */
715 for (link = decls; link; link = TREE_CHAIN (link))
716 {
717 tree name;
718 if (TREE_CODE (link) == TREE_LIST)
719 {
720 decl = TREE_VALUE (link);
721 name = TREE_PURPOSE (link);
722 gcc_checking_assert (name);
723 }
724 else
725 {
726 decl = link;
727 name = DECL_NAME (decl);
728 }
729
730 /* Remove the binding. */
731 if (TREE_CODE (decl) == LABEL_DECL)
732 pop_local_label (name, decl);
733 else
734 pop_local_binding (name, decl);
735 }
736
737 /* Restore the IDENTIFIER_TYPE_VALUEs. */
738 for (link = current_binding_level->type_shadowed;
739 link; link = TREE_CHAIN (link))
740 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link), TREE_VALUE (link));
741
742 /* There may be OVERLOADs (wrapped in TREE_LISTs) on the BLOCK_VARs
743 list if a `using' declaration put them there. The debugging
744 back ends won't understand OVERLOAD, so we remove them here.
745 Because the BLOCK_VARS are (temporarily) shared with
746 CURRENT_BINDING_LEVEL->NAMES we must do this fixup after we have
747 popped all the bindings. Also remove undeduced 'auto' decls,
748 which LTO doesn't understand, and can't have been used by anything. */
749 if (block)
750 {
751 tree* d;
752
753 for (d = &BLOCK_VARS (block); *d; )
754 {
755 if (TREE_CODE (*d) == TREE_LIST
756 || (!processing_template_decl
757 && undeduced_auto_decl (*d)))
758 *d = TREE_CHAIN (*d);
759 else
760 d = &DECL_CHAIN (*d);
761 }
762 }
763
764 /* If the level being exited is the top level of a function,
765 check over all the labels. */
766 if (functionbody)
767 {
768 if (block)
769 {
770 /* Since this is the top level block of a function, the vars are
771 the function's parameters. Don't leave them in the BLOCK
772 because they are found in the FUNCTION_DECL instead. */
773 BLOCK_VARS (block) = 0;
774 pop_labels (block);
775 }
776 else
777 pop_labels (subblocks);
778 }
779
780 kind = current_binding_level->kind;
781 if (kind == sk_cleanup)
782 {
783 tree stmt;
784
785 /* If this is a temporary binding created for a cleanup, then we'll
786 have pushed a statement list level. Pop that, create a new
787 BIND_EXPR for the block, and insert it into the stream. */
788 stmt = pop_stmt_list (current_binding_level->statement_list);
789 stmt = c_build_bind_expr (input_location, block, stmt);
790 add_stmt (stmt);
791 }
792
793 leave_scope ();
794 if (functionbody)
795 {
796 /* The current function is being defined, so its DECL_INITIAL
797 should be error_mark_node. */
798 gcc_assert (DECL_INITIAL (current_function_decl) == error_mark_node);
799 DECL_INITIAL (current_function_decl) = block ? block : subblocks;
800 if (subblocks)
801 {
802 if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
803 {
804 if (BLOCK_SUBBLOCKS (subblocks))
805 BLOCK_OUTER_CURLY_BRACE_P (BLOCK_SUBBLOCKS (subblocks)) = 1;
806 }
807 else
808 BLOCK_OUTER_CURLY_BRACE_P (subblocks) = 1;
809 }
810 }
811 else if (block)
812 current_binding_level->blocks
813 = block_chainon (current_binding_level->blocks, block);
814
815 /* If we did not make a block for the level just exited,
816 any blocks made for inner levels
817 (since they cannot be recorded as subblocks in that level)
818 must be carried forward so they will later become subblocks
819 of something else. */
820 else if (subblocks)
821 current_binding_level->blocks
822 = block_chainon (current_binding_level->blocks, subblocks);
823
824 /* Each and every BLOCK node created here in `poplevel' is important
825 (e.g. for proper debugging information) so if we created one
826 earlier, mark it as "used". */
827 if (block)
828 TREE_USED (block) = 1;
829
830 /* All temporary bindings created for cleanups are popped silently. */
831 if (kind == sk_cleanup)
832 goto restart;
833
834 return block;
835 }
836
837 /* Call wrapup_globals_declarations for the globals in NAMESPACE. */
838 /* Diagnose odr-used extern inline variables without definitions
839 in the current TU. */
840
841 int
wrapup_namespace_globals()842 wrapup_namespace_globals ()
843 {
844 if (vec<tree, va_gc> *statics = static_decls)
845 {
846 for (tree decl : *statics)
847 {
848 if (warn_unused_function
849 && TREE_CODE (decl) == FUNCTION_DECL
850 && DECL_INITIAL (decl) == 0
851 && DECL_EXTERNAL (decl)
852 && !TREE_PUBLIC (decl)
853 && !DECL_ARTIFICIAL (decl)
854 && !DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
855 && !warning_suppressed_p (decl, OPT_Wunused_function))
856 warning_at (DECL_SOURCE_LOCATION (decl),
857 OPT_Wunused_function,
858 "%qF declared %<static%> but never defined", decl);
859
860 if (VAR_P (decl)
861 && DECL_EXTERNAL (decl)
862 && DECL_INLINE_VAR_P (decl)
863 && DECL_ODR_USED (decl))
864 error_at (DECL_SOURCE_LOCATION (decl),
865 "odr-used inline variable %qD is not defined", decl);
866 }
867
868 /* Clear out the list, so we don't rescan next time. */
869 static_decls = NULL;
870
871 /* Write out any globals that need to be output. */
872 return wrapup_global_declarations (statics->address (),
873 statics->length ());
874 }
875 return 0;
876 }
877
878 /* In C++, you don't have to write `struct S' to refer to `S'; you
879 can just use `S'. We accomplish this by creating a TYPE_DECL as
880 if the user had written `typedef struct S S'. Create and return
881 the TYPE_DECL for TYPE. */
882
883 tree
create_implicit_typedef(tree name,tree type)884 create_implicit_typedef (tree name, tree type)
885 {
886 tree decl;
887
888 decl = build_decl (input_location, TYPE_DECL, name, type);
889 DECL_ARTIFICIAL (decl) = 1;
890 /* There are other implicit type declarations, like the one *within*
891 a class that allows you to write `S::S'. We must distinguish
892 amongst these. */
893 SET_DECL_IMPLICIT_TYPEDEF_P (decl);
894 TYPE_NAME (type) = decl;
895 TYPE_STUB_DECL (type) = decl;
896
897 return decl;
898 }
899
900 /* Function-scope local entities that need discriminators. Each entry
901 is a {decl,name} pair. VAR_DECLs for anon unions get their name
902 smashed, so we cannot rely on DECL_NAME. */
903
904 static GTY((deletable)) vec<tree, va_gc> *local_entities;
905
906 /* Determine the mangling discriminator of local DECL. There are
907 generally very few of these in any particular function. */
908
909 void
determine_local_discriminator(tree decl)910 determine_local_discriminator (tree decl)
911 {
912 auto_cond_timevar tv (TV_NAME_LOOKUP);
913 retrofit_lang_decl (decl);
914 tree ctx = DECL_CONTEXT (decl);
915 tree name = (TREE_CODE (decl) == TYPE_DECL
916 && TYPE_UNNAMED_P (TREE_TYPE (decl))
917 ? NULL_TREE : DECL_NAME (decl));
918 size_t nelts = vec_safe_length (local_entities);
919 for (size_t i = 0; i < nelts; i += 2)
920 {
921 tree *pair = &(*local_entities)[i];
922 tree d = pair[0];
923 tree n = pair[1];
924 gcc_checking_assert (d != decl);
925 if (name == n
926 && TREE_CODE (decl) == TREE_CODE (d)
927 && ctx == DECL_CONTEXT (d))
928 {
929 tree disc = integer_one_node;
930 if (DECL_DISCRIMINATOR (d))
931 disc = build_int_cst (TREE_TYPE (disc),
932 TREE_INT_CST_LOW (DECL_DISCRIMINATOR (d)) + 1);
933 DECL_DISCRIMINATOR (decl) = disc;
934 /* Replace the saved decl. */
935 pair[0] = decl;
936 decl = NULL_TREE;
937 break;
938 }
939 }
940
941 if (decl)
942 {
943 vec_safe_reserve (local_entities, 2);
944 local_entities->quick_push (decl);
945 local_entities->quick_push (name);
946 }
947 }
948
949
950
951 /* Returns true if functions FN1 and FN2 have equivalent trailing
952 requires clauses. */
953
954 static bool
function_requirements_equivalent_p(tree newfn,tree oldfn)955 function_requirements_equivalent_p (tree newfn, tree oldfn)
956 {
957 /* In the concepts TS, the combined constraints are compared. */
958 if (cxx_dialect < cxx20)
959 {
960 tree ci1 = get_constraints (oldfn);
961 tree ci2 = get_constraints (newfn);
962 tree req1 = ci1 ? CI_ASSOCIATED_CONSTRAINTS (ci1) : NULL_TREE;
963 tree req2 = ci2 ? CI_ASSOCIATED_CONSTRAINTS (ci2) : NULL_TREE;
964 return cp_tree_equal (req1, req2);
965 }
966
967 /* Compare only trailing requirements. */
968 tree reqs1 = get_trailing_function_requirements (newfn);
969 tree reqs2 = get_trailing_function_requirements (oldfn);
970 if ((reqs1 != NULL_TREE) != (reqs2 != NULL_TREE))
971 return false;
972
973 /* Substitution is needed when friends are involved. */
974 reqs1 = maybe_substitute_reqs_for (reqs1, newfn);
975 reqs2 = maybe_substitute_reqs_for (reqs2, oldfn);
976
977 return cp_tree_equal (reqs1, reqs2);
978 }
979
980 /* Subroutine of duplicate_decls: return truthvalue of whether
981 or not types of these decls match.
982
983 For C++, we must compare the parameter list so that `int' can match
984 `int&' in a parameter position, but `int&' is not confused with
985 `const int&'. */
986
987 int
decls_match(tree newdecl,tree olddecl,bool record_versions)988 decls_match (tree newdecl, tree olddecl, bool record_versions /* = true */)
989 {
990 int types_match;
991
992 if (newdecl == olddecl)
993 return 1;
994
995 if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
996 /* If the two DECLs are not even the same kind of thing, we're not
997 interested in their types. */
998 return 0;
999
1000 gcc_assert (DECL_P (newdecl));
1001
1002 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1003 {
1004 /* Specializations of different templates are different functions
1005 even if they have the same type. */
1006 tree t1 = (DECL_USE_TEMPLATE (newdecl)
1007 ? DECL_TI_TEMPLATE (newdecl)
1008 : NULL_TREE);
1009 tree t2 = (DECL_USE_TEMPLATE (olddecl)
1010 ? DECL_TI_TEMPLATE (olddecl)
1011 : NULL_TREE);
1012 if (t1 != t2)
1013 return 0;
1014
1015 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
1016 && ! (DECL_EXTERN_C_P (newdecl)
1017 && DECL_EXTERN_C_P (olddecl)))
1018 return 0;
1019
1020 /* A new declaration doesn't match a built-in one unless it
1021 is also extern "C". */
1022 if (DECL_IS_UNDECLARED_BUILTIN (olddecl)
1023 && DECL_EXTERN_C_P (olddecl) && !DECL_EXTERN_C_P (newdecl))
1024 return 0;
1025
1026 tree f1 = TREE_TYPE (newdecl);
1027 tree f2 = TREE_TYPE (olddecl);
1028 if (TREE_CODE (f1) != TREE_CODE (f2))
1029 return 0;
1030
1031 /* A declaration with deduced return type should use its pre-deduction
1032 type for declaration matching. */
1033 tree r2 = fndecl_declared_return_type (olddecl);
1034 tree r1 = fndecl_declared_return_type (newdecl);
1035
1036 tree p1 = TYPE_ARG_TYPES (f1);
1037 tree p2 = TYPE_ARG_TYPES (f2);
1038
1039 if (same_type_p (r1, r2))
1040 {
1041 if (!prototype_p (f2) && DECL_EXTERN_C_P (olddecl)
1042 && fndecl_built_in_p (olddecl))
1043 {
1044 types_match = self_promoting_args_p (p1);
1045 if (p1 == void_list_node)
1046 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
1047 }
1048 else
1049 types_match =
1050 compparms (p1, p2)
1051 && type_memfn_rqual (f1) == type_memfn_rqual (f2)
1052 && (TYPE_ATTRIBUTES (TREE_TYPE (newdecl)) == NULL_TREE
1053 || comp_type_attributes (TREE_TYPE (newdecl),
1054 TREE_TYPE (olddecl)) != 0);
1055 }
1056 else
1057 types_match = 0;
1058
1059 /* Two function declarations match if either has a requires-clause
1060 then both have a requires-clause and their constraints-expressions
1061 are equivalent. */
1062 if (types_match && flag_concepts)
1063 types_match = function_requirements_equivalent_p (newdecl, olddecl);
1064
1065 /* The decls dont match if they correspond to two different versions
1066 of the same function. Disallow extern "C" functions to be
1067 versions for now. */
1068 if (types_match
1069 && !DECL_EXTERN_C_P (newdecl)
1070 && !DECL_EXTERN_C_P (olddecl)
1071 && targetm.target_option.function_versions (newdecl, olddecl))
1072 {
1073 if (record_versions)
1074 maybe_version_functions (newdecl, olddecl,
1075 (!DECL_FUNCTION_VERSIONED (newdecl)
1076 || !DECL_FUNCTION_VERSIONED (olddecl)));
1077 return 0;
1078 }
1079 }
1080 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1081 {
1082 if (!template_heads_equivalent_p (newdecl, olddecl))
1083 return 0;
1084
1085 tree oldres = DECL_TEMPLATE_RESULT (olddecl);
1086 tree newres = DECL_TEMPLATE_RESULT (newdecl);
1087
1088 if (TREE_CODE (newres) != TREE_CODE (oldres))
1089 return 0;
1090
1091 /* Two template types match if they are the same. Otherwise, compare
1092 the underlying declarations. */
1093 if (TREE_CODE (newres) == TYPE_DECL)
1094 types_match = same_type_p (TREE_TYPE (newres), TREE_TYPE (oldres));
1095 else
1096 types_match = decls_match (newres, oldres);
1097 }
1098 else
1099 {
1100 /* Need to check scope for variable declaration (VAR_DECL).
1101 For typedef (TYPE_DECL), scope is ignored. */
1102 if (VAR_P (newdecl)
1103 && CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
1104 /* [dcl.link]
1105 Two declarations for an object with C language linkage
1106 with the same name (ignoring the namespace that qualify
1107 it) that appear in different namespace scopes refer to
1108 the same object. */
1109 && !(DECL_EXTERN_C_P (olddecl) && DECL_EXTERN_C_P (newdecl)))
1110 return 0;
1111
1112 if (TREE_TYPE (newdecl) == error_mark_node)
1113 types_match = TREE_TYPE (olddecl) == error_mark_node;
1114 else if (TREE_TYPE (olddecl) == NULL_TREE)
1115 types_match = TREE_TYPE (newdecl) == NULL_TREE;
1116 else if (TREE_TYPE (newdecl) == NULL_TREE)
1117 types_match = 0;
1118 else
1119 types_match = comptypes (TREE_TYPE (newdecl),
1120 TREE_TYPE (olddecl),
1121 COMPARE_REDECLARATION);
1122 }
1123
1124 return types_match;
1125 }
1126
1127 /* Mark DECL as versioned if it isn't already. */
1128
1129 static void
maybe_mark_function_versioned(tree decl)1130 maybe_mark_function_versioned (tree decl)
1131 {
1132 if (!DECL_FUNCTION_VERSIONED (decl))
1133 {
1134 DECL_FUNCTION_VERSIONED (decl) = 1;
1135 /* If DECL_ASSEMBLER_NAME has already been set, re-mangle
1136 to include the version marker. */
1137 if (DECL_ASSEMBLER_NAME_SET_P (decl))
1138 mangle_decl (decl);
1139 }
1140 }
1141
1142 /* NEWDECL and OLDDECL have identical signatures. If they are
1143 different versions adjust them and return true.
1144 If RECORD is set to true, record function versions. */
1145
1146 bool
maybe_version_functions(tree newdecl,tree olddecl,bool record)1147 maybe_version_functions (tree newdecl, tree olddecl, bool record)
1148 {
1149 if (!targetm.target_option.function_versions (newdecl, olddecl))
1150 return false;
1151
1152 maybe_mark_function_versioned (olddecl);
1153 if (DECL_LOCAL_DECL_P (olddecl))
1154 {
1155 olddecl = DECL_LOCAL_DECL_ALIAS (olddecl);
1156 maybe_mark_function_versioned (olddecl);
1157 }
1158
1159 maybe_mark_function_versioned (newdecl);
1160 if (DECL_LOCAL_DECL_P (newdecl))
1161 {
1162 /* Unfortunately, we can get here before pushdecl naturally calls
1163 push_local_extern_decl_alias, so we need to call it directly. */
1164 if (!DECL_LOCAL_DECL_ALIAS (newdecl))
1165 push_local_extern_decl_alias (newdecl);
1166 newdecl = DECL_LOCAL_DECL_ALIAS (newdecl);
1167 maybe_mark_function_versioned (newdecl);
1168 }
1169
1170 if (record)
1171 cgraph_node::record_function_versions (olddecl, newdecl);
1172
1173 return true;
1174 }
1175
1176 /* If NEWDECL is `static' and an `extern' was seen previously,
1177 warn about it. OLDDECL is the previous declaration.
1178
1179 Note that this does not apply to the C++ case of declaring
1180 a variable `extern const' and then later `const'.
1181
1182 Don't complain about built-in functions, since they are beyond
1183 the user's control. */
1184
1185 void
warn_extern_redeclared_static(tree newdecl,tree olddecl)1186 warn_extern_redeclared_static (tree newdecl, tree olddecl)
1187 {
1188 if (TREE_CODE (newdecl) == TYPE_DECL
1189 || TREE_CODE (newdecl) == TEMPLATE_DECL
1190 || TREE_CODE (newdecl) == CONST_DECL
1191 || TREE_CODE (newdecl) == NAMESPACE_DECL)
1192 return;
1193
1194 /* Don't get confused by static member functions; that's a different
1195 use of `static'. */
1196 if (TREE_CODE (newdecl) == FUNCTION_DECL
1197 && DECL_STATIC_FUNCTION_P (newdecl))
1198 return;
1199
1200 /* If the old declaration was `static', or the new one isn't, then
1201 everything is OK. */
1202 if (DECL_THIS_STATIC (olddecl) || !DECL_THIS_STATIC (newdecl))
1203 return;
1204
1205 /* It's OK to declare a builtin function as `static'. */
1206 if (TREE_CODE (olddecl) == FUNCTION_DECL
1207 && DECL_ARTIFICIAL (olddecl))
1208 return;
1209
1210 auto_diagnostic_group d;
1211 if (permerror (DECL_SOURCE_LOCATION (newdecl),
1212 "%qD was declared %<extern%> and later %<static%>", newdecl))
1213 inform (DECL_SOURCE_LOCATION (olddecl),
1214 "previous declaration of %qD", olddecl);
1215 }
1216
1217 /* NEW_DECL is a redeclaration of OLD_DECL; both are functions or
1218 function templates. If their exception specifications do not
1219 match, issue a diagnostic. */
1220
1221 static void
check_redeclaration_exception_specification(tree new_decl,tree old_decl)1222 check_redeclaration_exception_specification (tree new_decl,
1223 tree old_decl)
1224 {
1225 tree new_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (new_decl));
1226 tree old_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old_decl));
1227
1228 /* Two default specs are equivalent, don't force evaluation. */
1229 if (UNEVALUATED_NOEXCEPT_SPEC_P (new_exceptions)
1230 && UNEVALUATED_NOEXCEPT_SPEC_P (old_exceptions))
1231 return;
1232
1233 if (!type_dependent_expression_p (old_decl))
1234 {
1235 maybe_instantiate_noexcept (new_decl);
1236 maybe_instantiate_noexcept (old_decl);
1237 }
1238 new_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (new_decl));
1239 old_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old_decl));
1240
1241 /* [except.spec]
1242
1243 If any declaration of a function has an exception-specification,
1244 all declarations, including the definition and an explicit
1245 specialization, of that function shall have an
1246 exception-specification with the same set of type-ids. */
1247 if (!DECL_IS_UNDECLARED_BUILTIN (old_decl)
1248 && !DECL_IS_UNDECLARED_BUILTIN (new_decl)
1249 && !comp_except_specs (new_exceptions, old_exceptions, ce_normal))
1250 {
1251 const char *const msg
1252 = G_("declaration of %qF has a different exception specifier");
1253 bool complained = true;
1254 location_t new_loc = DECL_SOURCE_LOCATION (new_decl);
1255 auto_diagnostic_group d;
1256 if (DECL_IN_SYSTEM_HEADER (old_decl))
1257 complained = pedwarn (new_loc, OPT_Wsystem_headers, msg, new_decl);
1258 else if (!flag_exceptions)
1259 /* We used to silently permit mismatched eh specs with
1260 -fno-exceptions, so make them a pedwarn now. */
1261 complained = pedwarn (new_loc, OPT_Wpedantic, msg, new_decl);
1262 else
1263 error_at (new_loc, msg, new_decl);
1264 if (complained)
1265 inform (DECL_SOURCE_LOCATION (old_decl),
1266 "from previous declaration %qF", old_decl);
1267 }
1268 }
1269
1270 /* Return true if OLD_DECL and NEW_DECL agree on constexprness.
1271 Otherwise issue diagnostics. */
1272
1273 static bool
validate_constexpr_redeclaration(tree old_decl,tree new_decl)1274 validate_constexpr_redeclaration (tree old_decl, tree new_decl)
1275 {
1276 old_decl = STRIP_TEMPLATE (old_decl);
1277 new_decl = STRIP_TEMPLATE (new_decl);
1278 if (!VAR_OR_FUNCTION_DECL_P (old_decl)
1279 || !VAR_OR_FUNCTION_DECL_P (new_decl))
1280 return true;
1281 if (DECL_DECLARED_CONSTEXPR_P (old_decl)
1282 == DECL_DECLARED_CONSTEXPR_P (new_decl))
1283 {
1284 if (TREE_CODE (old_decl) != FUNCTION_DECL)
1285 return true;
1286 if (DECL_IMMEDIATE_FUNCTION_P (old_decl)
1287 == DECL_IMMEDIATE_FUNCTION_P (new_decl))
1288 return true;
1289 }
1290 if (TREE_CODE (old_decl) == FUNCTION_DECL)
1291 {
1292 /* With -fimplicit-constexpr, ignore changes in the constexpr
1293 keyword. */
1294 if (flag_implicit_constexpr
1295 && (DECL_IMMEDIATE_FUNCTION_P (new_decl)
1296 == DECL_IMMEDIATE_FUNCTION_P (old_decl)))
1297 return true;
1298 if (fndecl_built_in_p (old_decl))
1299 {
1300 /* Hide a built-in declaration. */
1301 DECL_DECLARED_CONSTEXPR_P (old_decl)
1302 = DECL_DECLARED_CONSTEXPR_P (new_decl);
1303 if (DECL_IMMEDIATE_FUNCTION_P (new_decl))
1304 SET_DECL_IMMEDIATE_FUNCTION_P (old_decl);
1305 return true;
1306 }
1307 /* 7.1.5 [dcl.constexpr]
1308 Note: An explicit specialization can differ from the template
1309 declaration with respect to the constexpr specifier. */
1310 if (! DECL_TEMPLATE_SPECIALIZATION (old_decl)
1311 && DECL_TEMPLATE_SPECIALIZATION (new_decl))
1312 return true;
1313
1314 const char *kind = "constexpr";
1315 if (DECL_IMMEDIATE_FUNCTION_P (old_decl)
1316 || DECL_IMMEDIATE_FUNCTION_P (new_decl))
1317 kind = "consteval";
1318 error_at (DECL_SOURCE_LOCATION (new_decl),
1319 "redeclaration %qD differs in %qs "
1320 "from previous declaration", new_decl,
1321 kind);
1322 inform (DECL_SOURCE_LOCATION (old_decl),
1323 "previous declaration %qD", old_decl);
1324 return false;
1325 }
1326 return true;
1327 }
1328
1329 // If OLDDECL and NEWDECL are concept declarations with the same type
1330 // (i.e., and template parameters), but different requirements,
1331 // emit diagnostics and return true. Otherwise, return false.
1332 static inline bool
check_concept_refinement(tree olddecl,tree newdecl)1333 check_concept_refinement (tree olddecl, tree newdecl)
1334 {
1335 if (!DECL_DECLARED_CONCEPT_P (olddecl) || !DECL_DECLARED_CONCEPT_P (newdecl))
1336 return false;
1337
1338 tree d1 = DECL_TEMPLATE_RESULT (olddecl);
1339 tree d2 = DECL_TEMPLATE_RESULT (newdecl);
1340 if (TREE_CODE (d1) != TREE_CODE (d2))
1341 return false;
1342
1343 tree t1 = TREE_TYPE (d1);
1344 tree t2 = TREE_TYPE (d2);
1345 if (TREE_CODE (d1) == FUNCTION_DECL)
1346 {
1347 if (compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2))
1348 && comp_template_parms (DECL_TEMPLATE_PARMS (olddecl),
1349 DECL_TEMPLATE_PARMS (newdecl))
1350 && !equivalently_constrained (olddecl, newdecl))
1351 {
1352 error ("cannot specialize concept %q#D", olddecl);
1353 return true;
1354 }
1355 }
1356 return false;
1357 }
1358
1359 /* DECL is a redeclaration of a function or function template. If
1360 it does have default arguments issue a diagnostic. Note: this
1361 function is used to enforce the requirements in C++11 8.3.6 about
1362 no default arguments in redeclarations. */
1363
1364 static void
check_redeclaration_no_default_args(tree decl)1365 check_redeclaration_no_default_args (tree decl)
1366 {
1367 gcc_assert (DECL_DECLARES_FUNCTION_P (decl));
1368
1369 for (tree t = FUNCTION_FIRST_USER_PARMTYPE (decl);
1370 t && t != void_list_node; t = TREE_CHAIN (t))
1371 if (TREE_PURPOSE (t))
1372 {
1373 permerror (DECL_SOURCE_LOCATION (decl),
1374 "redeclaration of %q#D may not have default "
1375 "arguments", decl);
1376 return;
1377 }
1378 }
1379
1380 /* NEWDECL is a redeclaration of a function or function template OLDDECL,
1381 in any case represented as FUNCTION_DECLs (the DECL_TEMPLATE_RESULTs of
1382 the TEMPLATE_DECLs in case of function templates). This function is used
1383 to enforce the final part of C++17 11.3.6/4, about a single declaration:
1384 "If a friend declaration specifies a default argument expression, that
1385 declaration shall be a definition and shall be the only declaration of
1386 the function or function template in the translation unit." */
1387
1388 static void
check_no_redeclaration_friend_default_args(tree olddecl,tree newdecl)1389 check_no_redeclaration_friend_default_args (tree olddecl, tree newdecl)
1390 {
1391 if (!DECL_UNIQUE_FRIEND_P (olddecl) && !DECL_UNIQUE_FRIEND_P (newdecl))
1392 return;
1393
1394 for (tree t1 = FUNCTION_FIRST_USER_PARMTYPE (olddecl),
1395 t2 = FUNCTION_FIRST_USER_PARMTYPE (newdecl);
1396 t1 && t1 != void_list_node;
1397 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1398 if ((DECL_UNIQUE_FRIEND_P (olddecl) && TREE_PURPOSE (t1))
1399 || (DECL_UNIQUE_FRIEND_P (newdecl) && TREE_PURPOSE (t2)))
1400 {
1401 auto_diagnostic_group d;
1402 if (permerror (DECL_SOURCE_LOCATION (newdecl),
1403 "friend declaration of %q#D specifies default "
1404 "arguments and isn%'t the only declaration", newdecl))
1405 inform (DECL_SOURCE_LOCATION (olddecl),
1406 "previous declaration of %q#D", olddecl);
1407 return;
1408 }
1409 }
1410
1411 /* Merge tree bits that correspond to attributes noreturn, nothrow,
1412 const, malloc, and pure from NEWDECL with those of OLDDECL. */
1413
1414 static void
merge_attribute_bits(tree newdecl,tree olddecl)1415 merge_attribute_bits (tree newdecl, tree olddecl)
1416 {
1417 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1418 TREE_THIS_VOLATILE (olddecl) |= TREE_THIS_VOLATILE (newdecl);
1419 TREE_NOTHROW (newdecl) |= TREE_NOTHROW (olddecl);
1420 TREE_NOTHROW (olddecl) |= TREE_NOTHROW (newdecl);
1421 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1422 TREE_READONLY (olddecl) |= TREE_READONLY (newdecl);
1423 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1424 DECL_IS_MALLOC (olddecl) |= DECL_IS_MALLOC (newdecl);
1425 DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
1426 DECL_PURE_P (olddecl) |= DECL_PURE_P (newdecl);
1427 DECL_UNINLINABLE (newdecl) |= DECL_UNINLINABLE (olddecl);
1428 DECL_UNINLINABLE (olddecl) |= DECL_UNINLINABLE (newdecl);
1429 }
1430
1431 #define GNU_INLINE_P(fn) (DECL_DECLARED_INLINE_P (fn) \
1432 && lookup_attribute ("gnu_inline", \
1433 DECL_ATTRIBUTES (fn)))
1434
1435 /* A subroutine of duplicate_decls. Emits a diagnostic when newdecl
1436 ambiguates olddecl. Returns true if an error occurs. */
1437
1438 static bool
duplicate_function_template_decls(tree newdecl,tree olddecl)1439 duplicate_function_template_decls (tree newdecl, tree olddecl)
1440 {
1441
1442 tree newres = DECL_TEMPLATE_RESULT (newdecl);
1443 tree oldres = DECL_TEMPLATE_RESULT (olddecl);
1444 /* Function template declarations can be differentiated by parameter
1445 and return type. */
1446 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (oldres)),
1447 TYPE_ARG_TYPES (TREE_TYPE (newres)))
1448 && same_type_p (TREE_TYPE (TREE_TYPE (newdecl)),
1449 TREE_TYPE (TREE_TYPE (olddecl))))
1450 {
1451 /* ... and also by their template-heads and requires-clauses. */
1452 if (template_heads_equivalent_p (newdecl, olddecl)
1453 && function_requirements_equivalent_p (newres, oldres))
1454 {
1455 error ("ambiguating new declaration %q+#D", newdecl);
1456 inform (DECL_SOURCE_LOCATION (olddecl),
1457 "old declaration %q#D", olddecl);
1458 return true;
1459 }
1460
1461 /* FIXME: The types are the same but the are differences
1462 in either the template heads or function requirements.
1463 We should be able to diagnose a set of common errors
1464 stemming from these declarations. For example:
1465
1466 template<typename T> requires C void f(...);
1467 template<typename T> void f(...) requires C;
1468
1469 These are functionally equivalent but not equivalent. */
1470 }
1471
1472 return false;
1473 }
1474
1475 /* OLD_PARMS is the innermost set of template parameters for some template
1476 declaration, and NEW_PARMS is the corresponding set of template parameters
1477 for a redeclaration of that template. Merge the default arguments within
1478 these two sets of parameters. CLASS_P is true iff the template in
1479 question is a class template. */
1480
1481 bool
merge_default_template_args(tree new_parms,tree old_parms,bool class_p)1482 merge_default_template_args (tree new_parms, tree old_parms, bool class_p)
1483 {
1484 gcc_checking_assert (TREE_VEC_LENGTH (new_parms)
1485 == TREE_VEC_LENGTH (old_parms));
1486 for (int i = 0; i < TREE_VEC_LENGTH (new_parms); i++)
1487 {
1488 tree new_parm = TREE_VALUE (TREE_VEC_ELT (new_parms, i));
1489 tree old_parm = TREE_VALUE (TREE_VEC_ELT (old_parms, i));
1490 tree& new_default = TREE_PURPOSE (TREE_VEC_ELT (new_parms, i));
1491 tree& old_default = TREE_PURPOSE (TREE_VEC_ELT (old_parms, i));
1492 if (error_operand_p (new_parm) || error_operand_p (old_parm))
1493 return false;
1494 if (new_default != NULL_TREE && old_default != NULL_TREE)
1495 {
1496 auto_diagnostic_group d;
1497 error ("redefinition of default argument for %q+#D", new_parm);
1498 inform (DECL_SOURCE_LOCATION (old_parm),
1499 "original definition appeared here");
1500 return false;
1501 }
1502 else if (new_default != NULL_TREE)
1503 /* Update the previous template parameters (which are the ones
1504 that will really count) with the new default value. */
1505 old_default = new_default;
1506 else if (class_p && old_default != NULL_TREE)
1507 /* Update the new parameters, too; they'll be used as the
1508 parameters for any members. */
1509 new_default = old_default;
1510 }
1511 return true;
1512 }
1513
1514 /* If NEWDECL is a redeclaration of OLDDECL, merge the declarations.
1515 If the redeclaration is invalid, a diagnostic is issued, and the
1516 error_mark_node is returned. Otherwise, OLDDECL is returned.
1517
1518 If NEWDECL is not a redeclaration of OLDDECL, NULL_TREE is
1519 returned.
1520
1521 HIDING is true if the new decl is being hidden. WAS_HIDDEN is true
1522 if the old decl was hidden.
1523
1524 Hidden decls can be anticipated builtins, injected friends, or
1525 (coming soon) injected from a local-extern decl. */
1526
1527 tree
duplicate_decls(tree newdecl,tree olddecl,bool hiding,bool was_hidden)1528 duplicate_decls (tree newdecl, tree olddecl, bool hiding, bool was_hidden)
1529 {
1530 unsigned olddecl_uid = DECL_UID (olddecl);
1531 int types_match = 0;
1532 int new_defines_function = 0;
1533 tree new_template_info;
1534 location_t olddecl_loc = DECL_SOURCE_LOCATION (olddecl);
1535 location_t newdecl_loc = DECL_SOURCE_LOCATION (newdecl);
1536
1537 if (newdecl == olddecl)
1538 return olddecl;
1539
1540 types_match = decls_match (newdecl, olddecl);
1541
1542 /* If either the type of the new decl or the type of the old decl is an
1543 error_mark_node, then that implies that we have already issued an
1544 error (earlier) for some bogus type specification, and in that case,
1545 it is rather pointless to harass the user with yet more error message
1546 about the same declaration, so just pretend the types match here. */
1547 if (TREE_TYPE (newdecl) == error_mark_node
1548 || TREE_TYPE (olddecl) == error_mark_node)
1549 return error_mark_node;
1550
1551 /* Check for redeclaration and other discrepancies. */
1552 if (TREE_CODE (olddecl) == FUNCTION_DECL
1553 && DECL_IS_UNDECLARED_BUILTIN (olddecl))
1554 {
1555 if (TREE_CODE (newdecl) != FUNCTION_DECL)
1556 {
1557 /* Avoid warnings redeclaring built-ins which have not been
1558 explicitly declared. */
1559 if (was_hidden)
1560 {
1561 if (TREE_PUBLIC (newdecl)
1562 && CP_DECL_CONTEXT (newdecl) == global_namespace)
1563 warning_at (newdecl_loc,
1564 OPT_Wbuiltin_declaration_mismatch,
1565 "built-in function %qD declared as non-function",
1566 newdecl);
1567 return NULL_TREE;
1568 }
1569
1570 /* If you declare a built-in or predefined function name as static,
1571 the old definition is overridden, but optionally warn this was a
1572 bad choice of name. */
1573 if (! TREE_PUBLIC (newdecl))
1574 {
1575 warning_at (newdecl_loc,
1576 OPT_Wshadow,
1577 fndecl_built_in_p (olddecl)
1578 ? G_("shadowing built-in function %q#D")
1579 : G_("shadowing library function %q#D"), olddecl);
1580 /* Discard the old built-in function. */
1581 return NULL_TREE;
1582 }
1583 /* If the built-in is not ansi, then programs can override
1584 it even globally without an error. */
1585 else if (! fndecl_built_in_p (olddecl))
1586 warning_at (newdecl_loc, 0,
1587 "library function %q#D redeclared as non-function %q#D",
1588 olddecl, newdecl);
1589 else
1590 error_at (newdecl_loc,
1591 "declaration of %q#D conflicts with built-in "
1592 "declaration %q#D", newdecl, olddecl);
1593 return NULL_TREE;
1594 }
1595 else if (!types_match)
1596 {
1597 /* Avoid warnings redeclaring built-ins which have not been
1598 explicitly declared. */
1599 if (was_hidden)
1600 {
1601 tree t1, t2;
1602
1603 /* A new declaration doesn't match a built-in one unless it
1604 is also extern "C". */
1605 gcc_assert (DECL_IS_UNDECLARED_BUILTIN (olddecl));
1606 gcc_assert (DECL_EXTERN_C_P (olddecl));
1607 if (!DECL_EXTERN_C_P (newdecl))
1608 return NULL_TREE;
1609
1610 for (t1 = TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1611 t2 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1612 t1 || t2;
1613 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1614 {
1615 if (!t1 || !t2)
1616 break;
1617 /* FILE, tm types are not known at the time
1618 we create the builtins. */
1619 for (unsigned i = 0;
1620 i < sizeof (builtin_structptr_types)
1621 / sizeof (builtin_structptr_type);
1622 ++i)
1623 if (TREE_VALUE (t2) == builtin_structptr_types[i].node)
1624 {
1625 tree t = TREE_VALUE (t1);
1626
1627 if (TYPE_PTR_P (t)
1628 && TYPE_IDENTIFIER (TREE_TYPE (t))
1629 == get_identifier (builtin_structptr_types[i].str)
1630 && compparms (TREE_CHAIN (t1), TREE_CHAIN (t2)))
1631 {
1632 tree oldargs = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1633
1634 TYPE_ARG_TYPES (TREE_TYPE (olddecl))
1635 = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
1636 types_match = decls_match (newdecl, olddecl);
1637 if (types_match)
1638 return duplicate_decls (newdecl, olddecl,
1639 hiding, was_hidden);
1640 TYPE_ARG_TYPES (TREE_TYPE (olddecl)) = oldargs;
1641 }
1642 goto next_arg;
1643 }
1644
1645 if (! same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1646 break;
1647 next_arg:;
1648 }
1649
1650 warning_at (newdecl_loc,
1651 OPT_Wbuiltin_declaration_mismatch,
1652 "declaration of %q#D conflicts with built-in "
1653 "declaration %q#D", newdecl, olddecl);
1654 }
1655 else if ((DECL_EXTERN_C_P (newdecl)
1656 && DECL_EXTERN_C_P (olddecl))
1657 || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1658 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
1659 {
1660 /* Don't really override olddecl for __* prefixed builtins
1661 except for __[^b]*_chk, the compiler might be using those
1662 explicitly. */
1663 if (fndecl_built_in_p (olddecl))
1664 {
1665 tree id = DECL_NAME (olddecl);
1666 const char *name = IDENTIFIER_POINTER (id);
1667 size_t len;
1668
1669 if (name[0] == '_'
1670 && name[1] == '_'
1671 && (startswith (name + 2, "builtin_")
1672 || (len = strlen (name)) <= strlen ("___chk")
1673 || memcmp (name + len - strlen ("_chk"),
1674 "_chk", strlen ("_chk") + 1) != 0))
1675 {
1676 if (DECL_INITIAL (newdecl))
1677 {
1678 error_at (newdecl_loc,
1679 "definition of %q#D ambiguates built-in "
1680 "declaration %q#D", newdecl, olddecl);
1681 return error_mark_node;
1682 }
1683 auto_diagnostic_group d;
1684 if (permerror (newdecl_loc,
1685 "new declaration %q#D ambiguates built-in"
1686 " declaration %q#D", newdecl, olddecl)
1687 && flag_permissive)
1688 inform (newdecl_loc,
1689 "ignoring the %q#D declaration", newdecl);
1690 return flag_permissive ? olddecl : error_mark_node;
1691 }
1692 }
1693
1694 /* A near match; override the builtin. */
1695
1696 if (TREE_PUBLIC (newdecl))
1697 warning_at (newdecl_loc,
1698 OPT_Wbuiltin_declaration_mismatch,
1699 "new declaration %q#D ambiguates built-in "
1700 "declaration %q#D", newdecl, olddecl);
1701 else
1702 warning (OPT_Wshadow,
1703 fndecl_built_in_p (olddecl)
1704 ? G_("shadowing built-in function %q#D")
1705 : G_("shadowing library function %q#D"), olddecl);
1706 }
1707 else
1708 /* Discard the old built-in function. */
1709 return NULL_TREE;
1710
1711 /* Replace the old RTL to avoid problems with inlining. */
1712 COPY_DECL_RTL (newdecl, olddecl);
1713 }
1714 else
1715 {
1716 /* Even if the types match, prefer the new declarations type
1717 for built-ins which have not been explicitly declared,
1718 for exception lists, etc... */
1719 tree type = TREE_TYPE (newdecl);
1720 tree attribs = (*targetm.merge_type_attributes)
1721 (TREE_TYPE (olddecl), type);
1722
1723 type = cp_build_type_attribute_variant (type, attribs);
1724 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = type;
1725 }
1726
1727 /* If a function is explicitly declared "throw ()", propagate that to
1728 the corresponding builtin. */
1729 if (DECL_BUILT_IN_CLASS (olddecl) == BUILT_IN_NORMAL
1730 && was_hidden
1731 && TREE_NOTHROW (newdecl)
1732 && !TREE_NOTHROW (olddecl))
1733 {
1734 enum built_in_function fncode = DECL_FUNCTION_CODE (olddecl);
1735 tree tmpdecl = builtin_decl_explicit (fncode);
1736 if (tmpdecl && tmpdecl != olddecl && types_match)
1737 TREE_NOTHROW (tmpdecl) = 1;
1738 }
1739
1740 /* Whether or not the builtin can throw exceptions has no
1741 bearing on this declarator. */
1742 TREE_NOTHROW (olddecl) = 0;
1743
1744 if (DECL_THIS_STATIC (newdecl) && !DECL_THIS_STATIC (olddecl))
1745 {
1746 /* If a builtin function is redeclared as `static', merge
1747 the declarations, but make the original one static. */
1748 DECL_THIS_STATIC (olddecl) = 1;
1749 TREE_PUBLIC (olddecl) = 0;
1750
1751 /* Make the old declaration consistent with the new one so
1752 that all remnants of the builtin-ness of this function
1753 will be banished. */
1754 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
1755 COPY_DECL_RTL (newdecl, olddecl);
1756 }
1757 }
1758 else if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1759 {
1760 /* C++ Standard, 3.3, clause 4:
1761 "[Note: a namespace name or a class template name must be unique
1762 in its declarative region (7.3.2, clause 14). ]" */
1763 if (TREE_CODE (olddecl) == NAMESPACE_DECL
1764 || TREE_CODE (newdecl) == NAMESPACE_DECL)
1765 /* Namespace conflicts with not namespace. */;
1766 else if (DECL_TYPE_TEMPLATE_P (olddecl)
1767 || DECL_TYPE_TEMPLATE_P (newdecl))
1768 /* Class template conflicts. */;
1769 else if ((TREE_CODE (olddecl) == TEMPLATE_DECL
1770 && DECL_TEMPLATE_RESULT (olddecl)
1771 && TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == VAR_DECL)
1772 || (TREE_CODE (newdecl) == TEMPLATE_DECL
1773 && DECL_TEMPLATE_RESULT (newdecl)
1774 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == VAR_DECL))
1775 /* Variable template conflicts. */;
1776 else if (concept_definition_p (olddecl)
1777 || concept_definition_p (newdecl))
1778 /* Concept conflicts. */;
1779 else if ((TREE_CODE (newdecl) == FUNCTION_DECL
1780 && DECL_FUNCTION_TEMPLATE_P (olddecl))
1781 || (TREE_CODE (olddecl) == FUNCTION_DECL
1782 && DECL_FUNCTION_TEMPLATE_P (newdecl)))
1783 {
1784 /* One is a function and the other is a template
1785 function. */
1786 if (!UDLIT_OPER_P (DECL_NAME (newdecl)))
1787 return NULL_TREE;
1788
1789 /* There can only be one! */
1790 if (TREE_CODE (newdecl) == TEMPLATE_DECL
1791 && check_raw_literal_operator (olddecl))
1792 error_at (newdecl_loc,
1793 "literal operator %q#D conflicts with"
1794 " raw literal operator", newdecl);
1795 else if (check_raw_literal_operator (newdecl))
1796 error_at (newdecl_loc,
1797 "raw literal operator %q#D conflicts with"
1798 " literal operator template", newdecl);
1799 else
1800 return NULL_TREE;
1801
1802 inform (olddecl_loc, "previous declaration %q#D", olddecl);
1803 return error_mark_node;
1804 }
1805 else if ((VAR_P (olddecl) && DECL_DECOMPOSITION_P (olddecl))
1806 || (VAR_P (newdecl) && DECL_DECOMPOSITION_P (newdecl)))
1807 /* A structured binding must be unique in its declarative region. */;
1808 else if (DECL_IMPLICIT_TYPEDEF_P (olddecl)
1809 || DECL_IMPLICIT_TYPEDEF_P (newdecl))
1810 /* One is an implicit typedef, that's ok. */
1811 return NULL_TREE;
1812
1813 error ("%q#D redeclared as different kind of entity", newdecl);
1814 inform (olddecl_loc, "previous declaration %q#D", olddecl);
1815
1816 return error_mark_node;
1817 }
1818 else if (!types_match)
1819 {
1820 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl))
1821 /* These are certainly not duplicate declarations; they're
1822 from different scopes. */
1823 return NULL_TREE;
1824
1825 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1826 {
1827 tree oldres = DECL_TEMPLATE_RESULT (olddecl);
1828 tree newres = DECL_TEMPLATE_RESULT (newdecl);
1829
1830 /* The name of a class template may not be declared to refer to
1831 any other template, class, function, object, namespace, value,
1832 or type in the same scope. */
1833 if (TREE_CODE (oldres) == TYPE_DECL
1834 || TREE_CODE (newres) == TYPE_DECL)
1835 {
1836 error_at (newdecl_loc,
1837 "conflicting declaration of template %q#D", newdecl);
1838 inform (olddecl_loc,
1839 "previous declaration %q#D", olddecl);
1840 return error_mark_node;
1841 }
1842
1843 else if (TREE_CODE (oldres) == FUNCTION_DECL
1844 && TREE_CODE (newres) == FUNCTION_DECL)
1845 {
1846 if (duplicate_function_template_decls (newdecl, olddecl))
1847 return error_mark_node;
1848 return NULL_TREE;
1849 }
1850 else if (check_concept_refinement (olddecl, newdecl))
1851 return error_mark_node;
1852 return NULL_TREE;
1853 }
1854 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1855 {
1856 if (DECL_EXTERN_C_P (newdecl) && DECL_EXTERN_C_P (olddecl))
1857 {
1858 error_at (newdecl_loc,
1859 "conflicting declaration of C function %q#D",
1860 newdecl);
1861 inform (olddecl_loc,
1862 "previous declaration %q#D", olddecl);
1863 return error_mark_node;
1864 }
1865 /* For function versions, params and types match, but they
1866 are not ambiguous. */
1867 else if ((!DECL_FUNCTION_VERSIONED (newdecl)
1868 && !DECL_FUNCTION_VERSIONED (olddecl))
1869 // The functions have the same parameter types.
1870 && compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1871 TYPE_ARG_TYPES (TREE_TYPE (olddecl)))
1872 // And the same constraints.
1873 && equivalently_constrained (newdecl, olddecl))
1874 {
1875 error_at (newdecl_loc,
1876 "ambiguating new declaration of %q#D", newdecl);
1877 inform (olddecl_loc,
1878 "old declaration %q#D", olddecl);
1879 return error_mark_node;
1880 }
1881 else
1882 return NULL_TREE;
1883 }
1884 else
1885 {
1886 error_at (newdecl_loc, "conflicting declaration %q#D", newdecl);
1887 inform (olddecl_loc,
1888 "previous declaration as %q#D", olddecl);
1889 return error_mark_node;
1890 }
1891 }
1892 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1893 && DECL_OMP_DECLARE_REDUCTION_P (newdecl))
1894 {
1895 /* OMP UDRs are never duplicates. */
1896 gcc_assert (DECL_OMP_DECLARE_REDUCTION_P (olddecl));
1897 error_at (newdecl_loc,
1898 "redeclaration of %<pragma omp declare reduction%>");
1899 inform (olddecl_loc,
1900 "previous %<pragma omp declare reduction%> declaration");
1901 return error_mark_node;
1902 }
1903 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1904 && ((DECL_TEMPLATE_SPECIALIZATION (olddecl)
1905 && (!DECL_TEMPLATE_INFO (newdecl)
1906 || (DECL_TI_TEMPLATE (newdecl)
1907 != DECL_TI_TEMPLATE (olddecl))))
1908 || (DECL_TEMPLATE_SPECIALIZATION (newdecl)
1909 && (!DECL_TEMPLATE_INFO (olddecl)
1910 || (DECL_TI_TEMPLATE (olddecl)
1911 != DECL_TI_TEMPLATE (newdecl))))))
1912 /* It's OK to have a template specialization and a non-template
1913 with the same type, or to have specializations of two
1914 different templates with the same type. Note that if one is a
1915 specialization, and the other is an instantiation of the same
1916 template, that we do not exit at this point. That situation
1917 can occur if we instantiate a template class, and then
1918 specialize one of its methods. This situation is valid, but
1919 the declarations must be merged in the usual way. */
1920 return NULL_TREE;
1921 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1922 && ((DECL_TEMPLATE_INSTANTIATION (olddecl)
1923 && !DECL_USE_TEMPLATE (newdecl))
1924 || (DECL_TEMPLATE_INSTANTIATION (newdecl)
1925 && !DECL_USE_TEMPLATE (olddecl))))
1926 /* One of the declarations is a template instantiation, and the
1927 other is not a template at all. That's OK. */
1928 return NULL_TREE;
1929 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
1930 {
1931 /* In [namespace.alias] we have:
1932
1933 In a declarative region, a namespace-alias-definition can be
1934 used to redefine a namespace-alias declared in that declarative
1935 region to refer only to the namespace to which it already
1936 refers.
1937
1938 Therefore, if we encounter a second alias directive for the same
1939 alias, we can just ignore the second directive. */
1940 if (DECL_NAMESPACE_ALIAS (newdecl)
1941 && (DECL_NAMESPACE_ALIAS (newdecl)
1942 == DECL_NAMESPACE_ALIAS (olddecl)))
1943 return olddecl;
1944
1945 /* Leave it to update_binding to merge or report error. */
1946 return NULL_TREE;
1947 }
1948 else
1949 {
1950 const char *errmsg = redeclaration_error_message (newdecl, olddecl);
1951 if (errmsg)
1952 {
1953 auto_diagnostic_group d;
1954 error_at (newdecl_loc, errmsg, newdecl);
1955 if (DECL_NAME (olddecl) != NULL_TREE)
1956 inform (olddecl_loc,
1957 (DECL_INITIAL (olddecl) && namespace_bindings_p ())
1958 ? G_("%q#D previously defined here")
1959 : G_("%q#D previously declared here"), olddecl);
1960 return error_mark_node;
1961 }
1962 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1963 && DECL_INITIAL (olddecl) != NULL_TREE
1964 && !prototype_p (TREE_TYPE (olddecl))
1965 && prototype_p (TREE_TYPE (newdecl)))
1966 {
1967 /* Prototype decl follows defn w/o prototype. */
1968 auto_diagnostic_group d;
1969 if (warning_at (newdecl_loc, 0,
1970 "prototype specified for %q#D", newdecl))
1971 inform (olddecl_loc,
1972 "previous non-prototype definition here");
1973 }
1974 else if (VAR_OR_FUNCTION_DECL_P (olddecl)
1975 && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl))
1976 {
1977 /* [dcl.link]
1978 If two declarations of the same function or object
1979 specify different linkage-specifications ..., the program
1980 is ill-formed.... Except for functions with C++ linkage,
1981 a function declaration without a linkage specification
1982 shall not precede the first linkage specification for
1983 that function. A function can be declared without a
1984 linkage specification after an explicit linkage
1985 specification has been seen; the linkage explicitly
1986 specified in the earlier declaration is not affected by
1987 such a function declaration.
1988
1989 DR 563 raises the question why the restrictions on
1990 functions should not also apply to objects. Older
1991 versions of G++ silently ignore the linkage-specification
1992 for this example:
1993
1994 namespace N {
1995 extern int i;
1996 extern "C" int i;
1997 }
1998
1999 which is clearly wrong. Therefore, we now treat objects
2000 like functions. */
2001 if (current_lang_depth () == 0)
2002 {
2003 /* There is no explicit linkage-specification, so we use
2004 the linkage from the previous declaration. */
2005 retrofit_lang_decl (newdecl);
2006 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
2007 }
2008 else
2009 {
2010 auto_diagnostic_group d;
2011 error_at (newdecl_loc,
2012 "conflicting declaration of %q#D with %qL linkage",
2013 newdecl, DECL_LANGUAGE (newdecl));
2014 inform (olddecl_loc,
2015 "previous declaration with %qL linkage",
2016 DECL_LANGUAGE (olddecl));
2017 }
2018 }
2019
2020 if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl))
2021 ;
2022 else if (TREE_CODE (olddecl) == FUNCTION_DECL)
2023 {
2024 /* Note: free functions, as TEMPLATE_DECLs, are handled below. */
2025 if (DECL_FUNCTION_MEMBER_P (olddecl)
2026 && (/* grokfndecl passes member function templates too
2027 as FUNCTION_DECLs. */
2028 DECL_TEMPLATE_INFO (olddecl)
2029 /* C++11 8.3.6/6.
2030 Default arguments for a member function of a class
2031 template shall be specified on the initial declaration
2032 of the member function within the class template. */
2033 || CLASSTYPE_TEMPLATE_INFO (CP_DECL_CONTEXT (olddecl))))
2034 {
2035 check_redeclaration_no_default_args (newdecl);
2036
2037 if (DECL_TEMPLATE_INFO (olddecl)
2038 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (olddecl)))
2039 {
2040 tree new_parms = DECL_TEMPLATE_INFO (newdecl)
2041 ? DECL_INNERMOST_TEMPLATE_PARMS (DECL_TI_TEMPLATE (newdecl))
2042 : INNERMOST_TEMPLATE_PARMS (current_template_parms);
2043 tree old_parms
2044 = DECL_INNERMOST_TEMPLATE_PARMS (DECL_TI_TEMPLATE (olddecl));
2045 merge_default_template_args (new_parms, old_parms,
2046 /*class_p=*/false);
2047 }
2048 }
2049 else
2050 {
2051 tree t1 = FUNCTION_FIRST_USER_PARMTYPE (olddecl);
2052 tree t2 = FUNCTION_FIRST_USER_PARMTYPE (newdecl);
2053 int i = 1;
2054
2055 for (; t1 && t1 != void_list_node;
2056 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++)
2057 if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
2058 {
2059 if (simple_cst_equal (TREE_PURPOSE (t1),
2060 TREE_PURPOSE (t2)) == 1)
2061 {
2062 auto_diagnostic_group d;
2063 if (permerror (newdecl_loc,
2064 "default argument given for parameter "
2065 "%d of %q#D", i, newdecl))
2066 inform (olddecl_loc,
2067 "previous specification in %q#D here",
2068 olddecl);
2069 }
2070 else
2071 {
2072 auto_diagnostic_group d;
2073 error_at (newdecl_loc,
2074 "default argument given for parameter %d "
2075 "of %q#D", i, newdecl);
2076 inform (olddecl_loc,
2077 "previous specification in %q#D here",
2078 olddecl);
2079 }
2080 }
2081
2082 /* C++17 11.3.6/4: "If a friend declaration specifies a default
2083 argument expression, that declaration... shall be the only
2084 declaration of the function or function template in the
2085 translation unit." */
2086 check_no_redeclaration_friend_default_args (olddecl, newdecl);
2087 }
2088 }
2089 }
2090
2091 /* Do not merge an implicit typedef with an explicit one. In:
2092
2093 class A;
2094 ...
2095 typedef class A A __attribute__ ((foo));
2096
2097 the attribute should apply only to the typedef. */
2098 if (TREE_CODE (olddecl) == TYPE_DECL
2099 && (DECL_IMPLICIT_TYPEDEF_P (olddecl)
2100 || DECL_IMPLICIT_TYPEDEF_P (newdecl)))
2101 return NULL_TREE;
2102
2103 if (DECL_TEMPLATE_PARM_P (olddecl) != DECL_TEMPLATE_PARM_P (newdecl))
2104 return NULL_TREE;
2105
2106 if (!validate_constexpr_redeclaration (olddecl, newdecl))
2107 return error_mark_node;
2108
2109 if (modules_p ()
2110 && TREE_CODE (CP_DECL_CONTEXT (olddecl)) == NAMESPACE_DECL
2111 && TREE_CODE (olddecl) != NAMESPACE_DECL
2112 && !hiding)
2113 {
2114 if (DECL_ARTIFICIAL (olddecl))
2115 {
2116 if (!(global_purview_p () || not_module_p ()))
2117 error ("declaration %qD conflicts with builtin", newdecl);
2118 else
2119 DECL_MODULE_EXPORT_P (olddecl) = DECL_MODULE_EXPORT_P (newdecl);
2120 }
2121 else
2122 {
2123 if (!module_may_redeclare (olddecl))
2124 {
2125 error ("declaration %qD conflicts with import", newdecl);
2126 inform (olddecl_loc, "import declared %q#D here", olddecl);
2127
2128 return error_mark_node;
2129 }
2130
2131 if (DECL_MODULE_EXPORT_P (newdecl)
2132 && !DECL_MODULE_EXPORT_P (olddecl))
2133 {
2134 error ("conflicting exporting declaration %qD", newdecl);
2135 inform (olddecl_loc, "previous declaration %q#D here", olddecl);
2136 }
2137 }
2138 }
2139
2140 /* We have committed to returning OLDDECL at this point. */
2141
2142 /* If new decl is `static' and an `extern' was seen previously,
2143 warn about it. */
2144 warn_extern_redeclared_static (newdecl, olddecl);
2145
2146 /* True to merge attributes between the declarations, false to
2147 set OLDDECL's attributes to those of NEWDECL (for template
2148 explicit specializations that specify their own attributes
2149 independent of those specified for the primary template). */
2150 const bool merge_attr = (TREE_CODE (newdecl) != FUNCTION_DECL
2151 || !DECL_TEMPLATE_SPECIALIZATION (newdecl)
2152 || DECL_TEMPLATE_SPECIALIZATION (olddecl));
2153
2154 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2155 {
2156 if (merge_attr)
2157 {
2158 if (diagnose_mismatched_attributes (olddecl, newdecl))
2159 inform (olddecl_loc, DECL_INITIAL (olddecl)
2160 ? G_("previous definition of %qD here")
2161 : G_("previous declaration of %qD here"), olddecl);
2162
2163 /* [dcl.attr.noreturn]: The first declaration of a function shall
2164 specify the noreturn attribute if any declaration of that function
2165 specifies the noreturn attribute. */
2166 tree a;
2167 if (TREE_THIS_VOLATILE (newdecl)
2168 && !TREE_THIS_VOLATILE (olddecl)
2169 /* This applies to [[noreturn]] only, not its GNU variants. */
2170 && (a = lookup_attribute ("noreturn", DECL_ATTRIBUTES (newdecl)))
2171 && cxx11_attribute_p (a)
2172 && get_attribute_namespace (a) == NULL_TREE)
2173 {
2174 error_at (newdecl_loc, "function %qD declared %<[[noreturn]]%> "
2175 "but its first declaration was not", newdecl);
2176 inform (olddecl_loc, "previous declaration of %qD", olddecl);
2177 }
2178 }
2179
2180 /* Now that functions must hold information normally held
2181 by field decls, there is extra work to do so that
2182 declaration information does not get destroyed during
2183 definition. */
2184 if (DECL_VINDEX (olddecl))
2185 DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl);
2186 if (DECL_CONTEXT (olddecl))
2187 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
2188 DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl);
2189 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
2190 DECL_PURE_VIRTUAL_P (newdecl) |= DECL_PURE_VIRTUAL_P (olddecl);
2191 DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl);
2192 DECL_INVALID_OVERRIDER_P (newdecl) |= DECL_INVALID_OVERRIDER_P (olddecl);
2193 DECL_FINAL_P (newdecl) |= DECL_FINAL_P (olddecl);
2194 DECL_OVERRIDE_P (newdecl) |= DECL_OVERRIDE_P (olddecl);
2195 DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl);
2196 DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (newdecl)
2197 |= DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (olddecl);
2198 if (DECL_OVERLOADED_OPERATOR_P (olddecl))
2199 DECL_OVERLOADED_OPERATOR_CODE_RAW (newdecl)
2200 = DECL_OVERLOADED_OPERATOR_CODE_RAW (olddecl);
2201 new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE;
2202
2203 /* Optionally warn about more than one declaration for the same
2204 name, but don't warn about a function declaration followed by a
2205 definition. */
2206 if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl)
2207 && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE)
2208 /* Don't warn about extern decl followed by definition. */
2209 && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl))
2210 /* Don't warn if at least one is/was hidden. */
2211 && !(hiding || was_hidden)
2212 /* Don't warn about declaration followed by specialization. */
2213 && (! DECL_TEMPLATE_SPECIALIZATION (newdecl)
2214 || DECL_TEMPLATE_SPECIALIZATION (olddecl)))
2215 {
2216 auto_diagnostic_group d;
2217 if (warning_at (newdecl_loc,
2218 OPT_Wredundant_decls,
2219 "redundant redeclaration of %qD in same scope",
2220 newdecl))
2221 inform (olddecl_loc,
2222 "previous declaration of %qD", olddecl);
2223 }
2224
2225 /* [dcl.fct.def.delete] A deleted definition of a function shall be the
2226 first declaration of the function or, for an explicit specialization
2227 of a function template, the first declaration of that
2228 specialization. */
2229 if (!(DECL_TEMPLATE_INSTANTIATION (olddecl)
2230 && DECL_TEMPLATE_SPECIALIZATION (newdecl)))
2231 {
2232 if (DECL_DELETED_FN (newdecl))
2233 {
2234 auto_diagnostic_group d;
2235 if (pedwarn (newdecl_loc, 0, "deleted definition of %qD "
2236 "is not first declaration", newdecl))
2237 inform (olddecl_loc,
2238 "previous declaration of %qD", olddecl);
2239 }
2240 DECL_DELETED_FN (newdecl) |= DECL_DELETED_FN (olddecl);
2241 }
2242 }
2243
2244 /* Deal with C++: must preserve virtual function table size. */
2245 if (TREE_CODE (olddecl) == TYPE_DECL)
2246 {
2247 tree newtype = TREE_TYPE (newdecl);
2248 tree oldtype = TREE_TYPE (olddecl);
2249
2250 if (newtype != error_mark_node && oldtype != error_mark_node
2251 && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype))
2252 CLASSTYPE_FRIEND_CLASSES (newtype)
2253 = CLASSTYPE_FRIEND_CLASSES (oldtype);
2254
2255 DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl);
2256 }
2257
2258 /* Copy all the DECL_... slots specified in the new decl except for
2259 any that we copy here from the old type. */
2260 if (merge_attr)
2261 DECL_ATTRIBUTES (newdecl)
2262 = (*targetm.merge_decl_attributes) (olddecl, newdecl);
2263 else
2264 DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
2265
2266 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2267 {
2268 tree old_result = DECL_TEMPLATE_RESULT (olddecl);
2269 tree new_result = DECL_TEMPLATE_RESULT (newdecl);
2270 TREE_TYPE (olddecl) = TREE_TYPE (old_result);
2271
2272 /* The new decl should not already have gathered any
2273 specializations. */
2274 gcc_assert (!DECL_TEMPLATE_SPECIALIZATIONS (newdecl));
2275
2276 DECL_ATTRIBUTES (old_result)
2277 = (*targetm.merge_decl_attributes) (old_result, new_result);
2278
2279 if (DECL_FUNCTION_TEMPLATE_P (newdecl))
2280 {
2281 if (DECL_SOURCE_LOCATION (newdecl)
2282 != DECL_SOURCE_LOCATION (olddecl))
2283 {
2284 /* Per C++11 8.3.6/4, default arguments cannot be added in
2285 later declarations of a function template. */
2286 check_redeclaration_no_default_args (newdecl);
2287 /* C++17 11.3.6/4: "If a friend declaration specifies a default
2288 argument expression, that declaration... shall be the only
2289 declaration of the function or function template in the
2290 translation unit." */
2291 check_no_redeclaration_friend_default_args
2292 (old_result, new_result);
2293
2294 tree new_parms = DECL_INNERMOST_TEMPLATE_PARMS (newdecl);
2295 tree old_parms = DECL_INNERMOST_TEMPLATE_PARMS (olddecl);
2296 merge_default_template_args (new_parms, old_parms,
2297 /*class_p=*/false);
2298 }
2299 if (!DECL_UNIQUE_FRIEND_P (new_result))
2300 DECL_UNIQUE_FRIEND_P (old_result) = false;
2301
2302 check_default_args (newdecl);
2303
2304 if (GNU_INLINE_P (old_result) != GNU_INLINE_P (new_result)
2305 && DECL_INITIAL (new_result))
2306 {
2307 if (DECL_INITIAL (old_result))
2308 DECL_UNINLINABLE (old_result) = 1;
2309 else
2310 DECL_UNINLINABLE (old_result) = DECL_UNINLINABLE (new_result);
2311 DECL_EXTERNAL (old_result) = DECL_EXTERNAL (new_result);
2312 DECL_NOT_REALLY_EXTERN (old_result)
2313 = DECL_NOT_REALLY_EXTERN (new_result);
2314 DECL_INTERFACE_KNOWN (old_result)
2315 = DECL_INTERFACE_KNOWN (new_result);
2316 DECL_DECLARED_INLINE_P (old_result)
2317 = DECL_DECLARED_INLINE_P (new_result);
2318 DECL_DISREGARD_INLINE_LIMITS (old_result)
2319 |= DECL_DISREGARD_INLINE_LIMITS (new_result);
2320
2321 }
2322 else
2323 {
2324 DECL_DECLARED_INLINE_P (old_result)
2325 |= DECL_DECLARED_INLINE_P (new_result);
2326 DECL_DISREGARD_INLINE_LIMITS (old_result)
2327 |= DECL_DISREGARD_INLINE_LIMITS (new_result);
2328 check_redeclaration_exception_specification (newdecl, olddecl);
2329
2330 merge_attribute_bits (new_result, old_result);
2331 }
2332 }
2333
2334 /* If the new declaration is a definition, update the file and
2335 line information on the declaration, and also make
2336 the old declaration the same definition. */
2337 if (DECL_INITIAL (new_result) != NULL_TREE)
2338 {
2339 DECL_SOURCE_LOCATION (olddecl)
2340 = DECL_SOURCE_LOCATION (old_result)
2341 = DECL_SOURCE_LOCATION (newdecl);
2342 DECL_INITIAL (old_result) = DECL_INITIAL (new_result);
2343 if (DECL_FUNCTION_TEMPLATE_P (newdecl))
2344 {
2345 tree parm;
2346 DECL_ARGUMENTS (old_result)
2347 = DECL_ARGUMENTS (new_result);
2348 for (parm = DECL_ARGUMENTS (old_result); parm;
2349 parm = DECL_CHAIN (parm))
2350 DECL_CONTEXT (parm) = old_result;
2351
2352 if (tree fc = DECL_FRIEND_CONTEXT (new_result))
2353 SET_DECL_FRIEND_CONTEXT (old_result, fc);
2354 }
2355 }
2356
2357 return olddecl;
2358 }
2359
2360 if (types_match)
2361 {
2362 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2363 check_redeclaration_exception_specification (newdecl, olddecl);
2364
2365 /* Automatically handles default parameters. */
2366 tree oldtype = TREE_TYPE (olddecl);
2367 tree newtype;
2368
2369 /* For typedefs use the old type, as the new type's DECL_NAME points
2370 at newdecl, which will be ggc_freed. */
2371 if (TREE_CODE (newdecl) == TYPE_DECL)
2372 {
2373 /* But NEWTYPE might have an attribute, honor that. */
2374 tree tem = TREE_TYPE (newdecl);
2375 newtype = oldtype;
2376
2377 if (TYPE_USER_ALIGN (tem))
2378 {
2379 if (TYPE_ALIGN (tem) > TYPE_ALIGN (newtype))
2380 SET_TYPE_ALIGN (newtype, TYPE_ALIGN (tem));
2381 TYPE_USER_ALIGN (newtype) = true;
2382 }
2383
2384 /* And remove the new type from the variants list. */
2385 if (TYPE_NAME (TREE_TYPE (newdecl)) == newdecl)
2386 {
2387 tree remove = TREE_TYPE (newdecl);
2388 if (TYPE_MAIN_VARIANT (remove) == remove)
2389 {
2390 gcc_assert (TYPE_NEXT_VARIANT (remove) == NULL_TREE);
2391 /* If remove is the main variant, no need to remove that
2392 from the list. One of the DECL_ORIGINAL_TYPE
2393 variants, e.g. created for aligned attribute, might still
2394 refer to the newdecl TYPE_DECL though, so remove that one
2395 in that case. */
2396 if (tree orig = DECL_ORIGINAL_TYPE (newdecl))
2397 if (orig != remove)
2398 for (tree t = TYPE_MAIN_VARIANT (orig); t;
2399 t = TYPE_MAIN_VARIANT (t))
2400 if (TYPE_NAME (TYPE_NEXT_VARIANT (t)) == newdecl)
2401 {
2402 TYPE_NEXT_VARIANT (t)
2403 = TYPE_NEXT_VARIANT (TYPE_NEXT_VARIANT (t));
2404 break;
2405 }
2406 }
2407 else
2408 for (tree t = TYPE_MAIN_VARIANT (remove); ;
2409 t = TYPE_NEXT_VARIANT (t))
2410 if (TYPE_NEXT_VARIANT (t) == remove)
2411 {
2412 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (remove);
2413 break;
2414 }
2415 }
2416 }
2417 else if (merge_attr)
2418 newtype = merge_types (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
2419 else
2420 newtype = TREE_TYPE (newdecl);
2421
2422 if (VAR_P (newdecl))
2423 {
2424 DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl);
2425 /* For already initialized vars, TREE_READONLY could have been
2426 cleared in cp_finish_decl, because the var needs runtime
2427 initialization or destruction. Make sure not to set
2428 TREE_READONLY on it again. */
2429 if (DECL_INITIALIZED_P (olddecl)
2430 && !DECL_EXTERNAL (olddecl)
2431 && !TREE_READONLY (olddecl))
2432 TREE_READONLY (newdecl) = 0;
2433 DECL_INITIALIZED_P (newdecl) |= DECL_INITIALIZED_P (olddecl);
2434 DECL_NONTRIVIALLY_INITIALIZED_P (newdecl)
2435 |= DECL_NONTRIVIALLY_INITIALIZED_P (olddecl);
2436 if (DECL_DEPENDENT_INIT_P (olddecl))
2437 SET_DECL_DEPENDENT_INIT_P (newdecl, true);
2438 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (newdecl)
2439 |= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (olddecl);
2440 DECL_DECLARED_CONSTEXPR_P (newdecl)
2441 |= DECL_DECLARED_CONSTEXPR_P (olddecl);
2442 DECL_DECLARED_CONSTINIT_P (newdecl)
2443 |= DECL_DECLARED_CONSTINIT_P (olddecl);
2444
2445 /* Merge the threadprivate attribute from OLDDECL into NEWDECL. */
2446 if (DECL_LANG_SPECIFIC (olddecl)
2447 && CP_DECL_THREADPRIVATE_P (olddecl))
2448 {
2449 /* Allocate a LANG_SPECIFIC structure for NEWDECL, if needed. */
2450 retrofit_lang_decl (newdecl);
2451 CP_DECL_THREADPRIVATE_P (newdecl) = 1;
2452 }
2453 }
2454
2455 /* An explicit specialization of a function template or of a member
2456 function of a class template can be declared transaction_safe
2457 independently of whether the corresponding template entity is declared
2458 transaction_safe. */
2459 if (flag_tm && TREE_CODE (newdecl) == FUNCTION_DECL
2460 && DECL_TEMPLATE_INSTANTIATION (olddecl)
2461 && DECL_TEMPLATE_SPECIALIZATION (newdecl)
2462 && tx_safe_fn_type_p (newtype)
2463 && !tx_safe_fn_type_p (TREE_TYPE (newdecl)))
2464 newtype = tx_unsafe_fn_variant (newtype);
2465
2466 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype;
2467
2468 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2469 check_default_args (newdecl);
2470
2471 /* Lay the type out, unless already done. */
2472 if (! same_type_p (newtype, oldtype)
2473 && TREE_TYPE (newdecl) != error_mark_node
2474 && !(processing_template_decl && uses_template_parms (newdecl)))
2475 layout_type (TREE_TYPE (newdecl));
2476
2477 if ((VAR_P (newdecl)
2478 || TREE_CODE (newdecl) == PARM_DECL
2479 || TREE_CODE (newdecl) == RESULT_DECL
2480 || TREE_CODE (newdecl) == FIELD_DECL
2481 || TREE_CODE (newdecl) == TYPE_DECL)
2482 && !(processing_template_decl && uses_template_parms (newdecl)))
2483 layout_decl (newdecl, 0);
2484
2485 /* Merge deprecatedness. */
2486 if (TREE_DEPRECATED (newdecl))
2487 TREE_DEPRECATED (olddecl) = 1;
2488
2489 /* Merge unavailability. */
2490 if (TREE_UNAVAILABLE (newdecl))
2491 TREE_UNAVAILABLE (olddecl) = 1;
2492
2493 /* Preserve function specific target and optimization options */
2494 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2495 {
2496 if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
2497 && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
2498 DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
2499 = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
2500
2501 if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
2502 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
2503 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
2504 = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
2505
2506 if (!DECL_UNIQUE_FRIEND_P (olddecl))
2507 DECL_UNIQUE_FRIEND_P (newdecl) = false;
2508 }
2509 else
2510 {
2511 /* Merge the const type qualifier. */
2512 if (TREE_READONLY (newdecl))
2513 TREE_READONLY (olddecl) = 1;
2514 /* Merge the volatile type qualifier. */
2515 if (TREE_THIS_VOLATILE (newdecl))
2516 TREE_THIS_VOLATILE (olddecl) = 1;
2517 }
2518
2519 /* Merge the initialization information. */
2520 if (DECL_INITIAL (newdecl) == NULL_TREE
2521 && DECL_INITIAL (olddecl) != NULL_TREE)
2522 {
2523 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2524 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2525 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2526 {
2527 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
2528 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
2529 }
2530 }
2531
2532 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2533 {
2534 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
2535 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
2536 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
2537 if (DECL_IS_OPERATOR_NEW_P (olddecl))
2538 DECL_SET_IS_OPERATOR_NEW (newdecl, true);
2539 DECL_LOOPING_CONST_OR_PURE_P (newdecl)
2540 |= DECL_LOOPING_CONST_OR_PURE_P (olddecl);
2541 DECL_IS_REPLACEABLE_OPERATOR (newdecl)
2542 |= DECL_IS_REPLACEABLE_OPERATOR (olddecl);
2543
2544 if (merge_attr)
2545 merge_attribute_bits (newdecl, olddecl);
2546 else
2547 {
2548 /* Merge the noreturn bit. */
2549 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
2550 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
2551 TREE_NOTHROW (olddecl) = TREE_NOTHROW (newdecl);
2552 DECL_IS_MALLOC (olddecl) = DECL_IS_MALLOC (newdecl);
2553 DECL_PURE_P (olddecl) = DECL_PURE_P (newdecl);
2554 }
2555 /* Keep the old RTL. */
2556 COPY_DECL_RTL (olddecl, newdecl);
2557 }
2558 else if (VAR_P (newdecl)
2559 && (DECL_SIZE (olddecl) || !DECL_SIZE (newdecl)))
2560 {
2561 /* Keep the old RTL. We cannot keep the old RTL if the old
2562 declaration was for an incomplete object and the new
2563 declaration is not since many attributes of the RTL will
2564 change. */
2565 COPY_DECL_RTL (olddecl, newdecl);
2566 }
2567 }
2568 /* If cannot merge, then use the new type and qualifiers,
2569 and don't preserve the old rtl. */
2570 else
2571 {
2572 /* Clean out any memory we had of the old declaration. */
2573 tree oldstatic = value_member (olddecl, static_aggregates);
2574 if (oldstatic)
2575 TREE_VALUE (oldstatic) = error_mark_node;
2576
2577 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
2578 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
2579 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
2580 TREE_NOTHROW (olddecl) = TREE_NOTHROW (newdecl);
2581 TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
2582 }
2583
2584 /* Merge the storage class information. */
2585 merge_weak (newdecl, olddecl);
2586
2587 DECL_DEFER_OUTPUT (newdecl) |= DECL_DEFER_OUTPUT (olddecl);
2588 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
2589 TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl);
2590 if (! DECL_EXTERNAL (olddecl))
2591 DECL_EXTERNAL (newdecl) = 0;
2592 if (! DECL_COMDAT (olddecl))
2593 DECL_COMDAT (newdecl) = 0;
2594
2595 if (VAR_OR_FUNCTION_DECL_P (newdecl) && DECL_LOCAL_DECL_P (newdecl))
2596 {
2597 if (!DECL_LOCAL_DECL_P (olddecl))
2598 /* This can happen if olddecl was brought in from the
2599 enclosing namespace via a using-decl. The new decl is
2600 then not a block-scope extern at all. */
2601 DECL_LOCAL_DECL_P (newdecl) = false;
2602 else
2603 {
2604 retrofit_lang_decl (newdecl);
2605 tree alias = DECL_LOCAL_DECL_ALIAS (newdecl)
2606 = DECL_LOCAL_DECL_ALIAS (olddecl);
2607 DECL_ATTRIBUTES (alias)
2608 = (*targetm.merge_decl_attributes) (alias, newdecl);
2609 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2610 merge_attribute_bits (newdecl, alias);
2611 }
2612 }
2613
2614 new_template_info = NULL_TREE;
2615 if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl))
2616 {
2617 bool new_redefines_gnu_inline = false;
2618
2619 if (new_defines_function
2620 && ((DECL_INTERFACE_KNOWN (olddecl)
2621 && TREE_CODE (olddecl) == FUNCTION_DECL)
2622 || (TREE_CODE (olddecl) == TEMPLATE_DECL
2623 && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl))
2624 == FUNCTION_DECL))))
2625 new_redefines_gnu_inline = GNU_INLINE_P (STRIP_TEMPLATE (olddecl));
2626
2627 if (!new_redefines_gnu_inline)
2628 {
2629 DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl);
2630 DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl);
2631 DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl);
2632 }
2633
2634 if (TREE_CODE (newdecl) != TYPE_DECL)
2635 {
2636 DECL_TEMPLATE_INSTANTIATED (newdecl)
2637 |= DECL_TEMPLATE_INSTANTIATED (olddecl);
2638 DECL_ODR_USED (newdecl) |= DECL_ODR_USED (olddecl);
2639
2640 /* If the OLDDECL is an instantiation and/or specialization,
2641 then the NEWDECL must be too. But, it may not yet be marked
2642 as such if the caller has created NEWDECL, but has not yet
2643 figured out that it is a redeclaration. */
2644 if (!DECL_USE_TEMPLATE (newdecl))
2645 DECL_USE_TEMPLATE (newdecl) = DECL_USE_TEMPLATE (olddecl);
2646
2647 if (!DECL_TEMPLATE_SPECIALIZATION (newdecl))
2648 DECL_INITIALIZED_IN_CLASS_P (newdecl)
2649 |= DECL_INITIALIZED_IN_CLASS_P (olddecl);
2650 }
2651
2652 /* Don't really know how much of the language-specific
2653 values we should copy from old to new. */
2654 DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl);
2655
2656 if (LANG_DECL_HAS_MIN (newdecl))
2657 {
2658 DECL_ACCESS (newdecl) = DECL_ACCESS (olddecl);
2659 if (DECL_TEMPLATE_INFO (newdecl))
2660 {
2661 new_template_info = DECL_TEMPLATE_INFO (newdecl);
2662 if (DECL_TEMPLATE_INSTANTIATION (olddecl)
2663 && DECL_TEMPLATE_SPECIALIZATION (newdecl))
2664 /* Remember the presence of explicit specialization args. */
2665 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (olddecl))
2666 = TINFO_USED_TEMPLATE_ID (new_template_info);
2667 }
2668
2669 /* We don't want to copy template info from a non-templated friend
2670 (PR105761), but these shouldn't have DECL_TEMPLATE_INFO now. */
2671 gcc_checking_assert (!DECL_TEMPLATE_INFO (olddecl)
2672 || !non_templated_friend_p (olddecl));
2673 DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl);
2674 }
2675
2676 if (DECL_DECLARES_FUNCTION_P (newdecl))
2677 {
2678 /* Only functions have these fields. */
2679 DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl);
2680 DECL_BEFRIENDING_CLASSES (newdecl)
2681 = chainon (DECL_BEFRIENDING_CLASSES (newdecl),
2682 DECL_BEFRIENDING_CLASSES (olddecl));
2683 /* DECL_THUNKS is only valid for virtual functions,
2684 otherwise it is a DECL_FRIEND_CONTEXT. */
2685 if (DECL_VIRTUAL_P (newdecl))
2686 SET_DECL_THUNKS (newdecl, DECL_THUNKS (olddecl));
2687 else if (tree fc = DECL_FRIEND_CONTEXT (newdecl))
2688 SET_DECL_FRIEND_CONTEXT (olddecl, fc);
2689 }
2690 else if (VAR_P (newdecl))
2691 {
2692 /* Only variables have this field. */
2693 if (VAR_HAD_UNKNOWN_BOUND (olddecl))
2694 SET_VAR_HAD_UNKNOWN_BOUND (newdecl);
2695 }
2696 }
2697
2698 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2699 {
2700 tree parm;
2701
2702 /* Merge parameter attributes. */
2703 tree oldarg, newarg;
2704 for (oldarg = DECL_ARGUMENTS(olddecl), newarg = DECL_ARGUMENTS(newdecl);
2705 oldarg && newarg;
2706 oldarg = DECL_CHAIN(oldarg), newarg = DECL_CHAIN(newarg))
2707 {
2708 DECL_ATTRIBUTES (newarg)
2709 = (*targetm.merge_decl_attributes) (oldarg, newarg);
2710 DECL_ATTRIBUTES (oldarg) = DECL_ATTRIBUTES (newarg);
2711 }
2712
2713 if (DECL_TEMPLATE_INSTANTIATION (olddecl)
2714 && !DECL_TEMPLATE_INSTANTIATION (newdecl))
2715 {
2716 /* If newdecl is not a specialization, then it is not a
2717 template-related function at all. And that means that we
2718 should have exited above, returning 0. */
2719 gcc_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl));
2720
2721 if (DECL_ODR_USED (olddecl))
2722 /* From [temp.expl.spec]:
2723
2724 If a template, a member template or the member of a class
2725 template is explicitly specialized then that
2726 specialization shall be declared before the first use of
2727 that specialization that would cause an implicit
2728 instantiation to take place, in every translation unit in
2729 which such a use occurs. */
2730 error ("explicit specialization of %qD after first use",
2731 olddecl);
2732
2733 SET_DECL_TEMPLATE_SPECIALIZATION (olddecl);
2734 DECL_COMDAT (newdecl) = (TREE_PUBLIC (newdecl)
2735 && DECL_DECLARED_INLINE_P (newdecl));
2736
2737 /* Don't propagate visibility from the template to the
2738 specialization here. We'll do that in determine_visibility if
2739 appropriate. */
2740 DECL_VISIBILITY_SPECIFIED (olddecl) = 0;
2741
2742 /* [temp.expl.spec/14] We don't inline explicit specialization
2743 just because the primary template says so. */
2744 gcc_assert (!merge_attr);
2745
2746 DECL_DECLARED_INLINE_P (olddecl)
2747 = DECL_DECLARED_INLINE_P (newdecl);
2748
2749 DECL_DISREGARD_INLINE_LIMITS (olddecl)
2750 = DECL_DISREGARD_INLINE_LIMITS (newdecl);
2751
2752 DECL_UNINLINABLE (olddecl) = DECL_UNINLINABLE (newdecl);
2753 }
2754 else if (new_defines_function && DECL_INITIAL (olddecl))
2755 {
2756 /* Never inline re-defined extern inline functions.
2757 FIXME: this could be better handled by keeping both
2758 function as separate declarations. */
2759 DECL_UNINLINABLE (newdecl) = 1;
2760 }
2761 else
2762 {
2763 if (DECL_PENDING_INLINE_P (olddecl))
2764 {
2765 DECL_PENDING_INLINE_P (newdecl) = 1;
2766 DECL_PENDING_INLINE_INFO (newdecl)
2767 = DECL_PENDING_INLINE_INFO (olddecl);
2768 }
2769 else if (DECL_PENDING_INLINE_P (newdecl))
2770 ;
2771 else if (DECL_SAVED_AUTO_RETURN_TYPE (newdecl) == NULL)
2772 DECL_SAVED_AUTO_RETURN_TYPE (newdecl)
2773 = DECL_SAVED_AUTO_RETURN_TYPE (olddecl);
2774
2775 DECL_DECLARED_INLINE_P (newdecl) |= DECL_DECLARED_INLINE_P (olddecl);
2776
2777 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
2778 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
2779
2780 DECL_DISREGARD_INLINE_LIMITS (newdecl)
2781 = DECL_DISREGARD_INLINE_LIMITS (olddecl)
2782 = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
2783 || DECL_DISREGARD_INLINE_LIMITS (olddecl));
2784 }
2785
2786 /* Preserve abstractness on cloned [cd]tors. */
2787 DECL_ABSTRACT_P (newdecl) = DECL_ABSTRACT_P (olddecl);
2788
2789 /* Update newdecl's parms to point at olddecl. */
2790 for (parm = DECL_ARGUMENTS (newdecl); parm;
2791 parm = DECL_CHAIN (parm))
2792 DECL_CONTEXT (parm) = olddecl;
2793
2794 if (! types_match)
2795 {
2796 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
2797 COPY_DECL_ASSEMBLER_NAME (newdecl, olddecl);
2798 COPY_DECL_RTL (newdecl, olddecl);
2799 }
2800 if (! types_match || new_defines_function)
2801 {
2802 /* These need to be copied so that the names are available.
2803 Note that if the types do match, we'll preserve inline
2804 info and other bits, but if not, we won't. */
2805 DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl);
2806 DECL_RESULT (olddecl) = DECL_RESULT (newdecl);
2807 }
2808 /* If redeclaring a builtin function, it stays built in
2809 if newdecl is a gnu_inline definition, or if newdecl is just
2810 a declaration. */
2811 if (fndecl_built_in_p (olddecl)
2812 && (new_defines_function ? GNU_INLINE_P (newdecl) : types_match))
2813 {
2814 copy_decl_built_in_function (newdecl, olddecl);
2815 /* If we're keeping the built-in definition, keep the rtl,
2816 regardless of declaration matches. */
2817 COPY_DECL_RTL (olddecl, newdecl);
2818 if (DECL_BUILT_IN_CLASS (newdecl) == BUILT_IN_NORMAL)
2819 {
2820 enum built_in_function fncode = DECL_FUNCTION_CODE (newdecl);
2821 if (builtin_decl_explicit_p (fncode))
2822 {
2823 /* A compatible prototype of these builtin functions
2824 is seen, assume the runtime implements it with
2825 the expected semantics. */
2826 switch (fncode)
2827 {
2828 case BUILT_IN_STPCPY:
2829 set_builtin_decl_implicit_p (fncode, true);
2830 break;
2831 default:
2832 set_builtin_decl_declared_p (fncode, true);
2833 break;
2834 }
2835 }
2836
2837 copy_attributes_to_builtin (newdecl);
2838 }
2839 }
2840 if (new_defines_function)
2841 /* If defining a function declared with other language
2842 linkage, use the previously declared language linkage. */
2843 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
2844 else if (types_match)
2845 {
2846 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
2847 /* Don't clear out the arguments if we're just redeclaring a
2848 function. */
2849 if (DECL_ARGUMENTS (olddecl))
2850 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
2851 }
2852 }
2853 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
2854 NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl);
2855
2856 /* Now preserve various other info from the definition. */
2857 TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl);
2858 TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl);
2859 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
2860 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
2861
2862 /* Warn about conflicting visibility specifications. */
2863 if (DECL_VISIBILITY_SPECIFIED (olddecl)
2864 && DECL_VISIBILITY_SPECIFIED (newdecl)
2865 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
2866 {
2867 auto_diagnostic_group d;
2868 if (warning_at (newdecl_loc, OPT_Wattributes,
2869 "%qD: visibility attribute ignored because it "
2870 "conflicts with previous declaration", newdecl))
2871 inform (olddecl_loc,
2872 "previous declaration of %qD", olddecl);
2873 }
2874 /* Choose the declaration which specified visibility. */
2875 if (DECL_VISIBILITY_SPECIFIED (olddecl))
2876 {
2877 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
2878 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
2879 }
2880 /* Init priority used to be merged from newdecl to olddecl by the memcpy,
2881 so keep this behavior. */
2882 if (VAR_P (newdecl) && DECL_HAS_INIT_PRIORITY_P (newdecl))
2883 {
2884 SET_DECL_INIT_PRIORITY (olddecl, DECL_INIT_PRIORITY (newdecl));
2885 DECL_HAS_INIT_PRIORITY_P (olddecl) = 1;
2886 }
2887 /* Likewise for DECL_ALIGN, DECL_USER_ALIGN and DECL_PACKED. */
2888 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
2889 {
2890 SET_DECL_ALIGN (newdecl, DECL_ALIGN (olddecl));
2891 DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
2892 }
2893 else if (DECL_ALIGN (olddecl) == DECL_ALIGN (newdecl)
2894 && DECL_USER_ALIGN (olddecl) != DECL_USER_ALIGN (newdecl))
2895 DECL_USER_ALIGN (newdecl) = 1;
2896
2897 DECL_USER_ALIGN (olddecl) = DECL_USER_ALIGN (newdecl);
2898 if (DECL_WARN_IF_NOT_ALIGN (olddecl)
2899 > DECL_WARN_IF_NOT_ALIGN (newdecl))
2900 SET_DECL_WARN_IF_NOT_ALIGN (newdecl,
2901 DECL_WARN_IF_NOT_ALIGN (olddecl));
2902 if (TREE_CODE (newdecl) == FIELD_DECL)
2903 DECL_PACKED (olddecl) = DECL_PACKED (newdecl);
2904
2905 /* The DECL_LANG_SPECIFIC information in OLDDECL will be replaced
2906 with that from NEWDECL below. */
2907 if (DECL_LANG_SPECIFIC (olddecl))
2908 {
2909 gcc_checking_assert (DECL_LANG_SPECIFIC (olddecl)
2910 != DECL_LANG_SPECIFIC (newdecl));
2911 ggc_free (DECL_LANG_SPECIFIC (olddecl));
2912 }
2913
2914 /* Merge the USED information. */
2915 if (TREE_USED (olddecl))
2916 TREE_USED (newdecl) = 1;
2917 else if (TREE_USED (newdecl))
2918 TREE_USED (olddecl) = 1;
2919
2920 if (VAR_P (newdecl))
2921 {
2922 if (DECL_READ_P (olddecl))
2923 DECL_READ_P (newdecl) = 1;
2924 else if (DECL_READ_P (newdecl))
2925 DECL_READ_P (olddecl) = 1;
2926 }
2927
2928 if (DECL_PRESERVE_P (olddecl))
2929 DECL_PRESERVE_P (newdecl) = 1;
2930 else if (DECL_PRESERVE_P (newdecl))
2931 DECL_PRESERVE_P (olddecl) = 1;
2932
2933 /* Merge the DECL_FUNCTION_VERSIONED information. newdecl will be copied
2934 to olddecl and deleted. */
2935 if (TREE_CODE (newdecl) == FUNCTION_DECL
2936 && DECL_FUNCTION_VERSIONED (olddecl))
2937 {
2938 /* Set the flag for newdecl so that it gets copied to olddecl. */
2939 DECL_FUNCTION_VERSIONED (newdecl) = 1;
2940 /* newdecl will be purged after copying to olddecl and is no longer
2941 a version. */
2942 cgraph_node::delete_function_version_by_decl (newdecl);
2943 }
2944
2945 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2946 {
2947 int function_size;
2948 struct symtab_node *snode = symtab_node::get (olddecl);
2949
2950 function_size = sizeof (struct tree_decl_common);
2951
2952 memcpy ((char *) olddecl + sizeof (struct tree_common),
2953 (char *) newdecl + sizeof (struct tree_common),
2954 function_size - sizeof (struct tree_common));
2955
2956 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2957 (char *) newdecl + sizeof (struct tree_decl_common),
2958 sizeof (struct tree_function_decl) - sizeof (struct tree_decl_common));
2959
2960 /* Preserve symtab node mapping. */
2961 olddecl->decl_with_vis.symtab_node = snode;
2962
2963 if (new_template_info)
2964 /* If newdecl is a template instantiation, it is possible that
2965 the following sequence of events has occurred:
2966
2967 o A friend function was declared in a class template. The
2968 class template was instantiated.
2969
2970 o The instantiation of the friend declaration was
2971 recorded on the instantiation list, and is newdecl.
2972
2973 o Later, however, instantiate_class_template called pushdecl
2974 on the newdecl to perform name injection. But, pushdecl in
2975 turn called duplicate_decls when it discovered that another
2976 declaration of a global function with the same name already
2977 existed.
2978
2979 o Here, in duplicate_decls, we decided to clobber newdecl.
2980
2981 If we're going to do that, we'd better make sure that
2982 olddecl, and not newdecl, is on the list of
2983 instantiations so that if we try to do the instantiation
2984 again we won't get the clobbered declaration. */
2985 reregister_specialization (newdecl,
2986 new_template_info,
2987 olddecl);
2988 }
2989 else
2990 {
2991 size_t size = tree_code_size (TREE_CODE (newdecl));
2992
2993 memcpy ((char *) olddecl + sizeof (struct tree_common),
2994 (char *) newdecl + sizeof (struct tree_common),
2995 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
2996
2997 switch (TREE_CODE (newdecl))
2998 {
2999 case LABEL_DECL:
3000 case VAR_DECL:
3001 case RESULT_DECL:
3002 case PARM_DECL:
3003 case FIELD_DECL:
3004 case TYPE_DECL:
3005 case CONST_DECL:
3006 {
3007 struct symtab_node *snode = NULL;
3008
3009 if (VAR_P (newdecl)
3010 && (TREE_STATIC (olddecl) || TREE_PUBLIC (olddecl)
3011 || DECL_EXTERNAL (olddecl)))
3012 snode = symtab_node::get (olddecl);
3013 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
3014 (char *) newdecl + sizeof (struct tree_decl_common),
3015 size - sizeof (struct tree_decl_common)
3016 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
3017 if (VAR_P (newdecl))
3018 olddecl->decl_with_vis.symtab_node = snode;
3019 }
3020 break;
3021 default:
3022 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
3023 (char *) newdecl + sizeof (struct tree_decl_common),
3024 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common)
3025 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
3026 break;
3027 }
3028 }
3029
3030 if (VAR_OR_FUNCTION_DECL_P (newdecl))
3031 {
3032 if (DECL_EXTERNAL (olddecl)
3033 || TREE_PUBLIC (olddecl)
3034 || TREE_STATIC (olddecl))
3035 {
3036 /* Merge the section attribute.
3037 We want to issue an error if the sections conflict but that must be
3038 done later in decl_attributes since we are called before attributes
3039 are assigned. */
3040 if (DECL_SECTION_NAME (newdecl) != NULL)
3041 set_decl_section_name (olddecl, newdecl);
3042
3043 if (DECL_ONE_ONLY (newdecl))
3044 {
3045 struct symtab_node *oldsym, *newsym;
3046 if (TREE_CODE (olddecl) == FUNCTION_DECL)
3047 oldsym = cgraph_node::get_create (olddecl);
3048 else
3049 oldsym = varpool_node::get_create (olddecl);
3050 newsym = symtab_node::get (newdecl);
3051 oldsym->set_comdat_group (newsym->get_comdat_group ());
3052 }
3053 }
3054
3055 if (VAR_P (newdecl)
3056 && CP_DECL_THREAD_LOCAL_P (newdecl))
3057 {
3058 CP_DECL_THREAD_LOCAL_P (olddecl) = true;
3059 if (!processing_template_decl)
3060 set_decl_tls_model (olddecl, DECL_TLS_MODEL (newdecl));
3061 }
3062 }
3063
3064 DECL_UID (olddecl) = olddecl_uid;
3065
3066 /* NEWDECL contains the merged attribute lists.
3067 Update OLDDECL to be the same. */
3068 DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
3069
3070 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
3071 so that encode_section_info has a chance to look at the new decl
3072 flags and attributes. */
3073 if (DECL_RTL_SET_P (olddecl)
3074 && (TREE_CODE (olddecl) == FUNCTION_DECL
3075 || (VAR_P (olddecl)
3076 && TREE_STATIC (olddecl))))
3077 make_decl_rtl (olddecl);
3078
3079 /* The NEWDECL will no longer be needed. Because every out-of-class
3080 declaration of a member results in a call to duplicate_decls,
3081 freeing these nodes represents in a significant savings.
3082
3083 Before releasing the node, be sore to remove function from symbol
3084 table that might have been inserted there to record comdat group.
3085 Be sure to however do not free DECL_STRUCT_FUNCTION because this
3086 structure is shared in between newdecl and oldecl. */
3087 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3088 DECL_STRUCT_FUNCTION (newdecl) = NULL;
3089 if (VAR_OR_FUNCTION_DECL_P (newdecl))
3090 {
3091 struct symtab_node *snode = symtab_node::get (newdecl);
3092 if (snode)
3093 snode->remove ();
3094 }
3095
3096 if (TREE_CODE (olddecl) == FUNCTION_DECL)
3097 {
3098 tree clone;
3099 FOR_EACH_CLONE (clone, olddecl)
3100 {
3101 DECL_ATTRIBUTES (clone) = DECL_ATTRIBUTES (olddecl);
3102 DECL_PRESERVE_P (clone) |= DECL_PRESERVE_P (olddecl);
3103 }
3104 }
3105
3106 /* Remove the associated constraints for newdecl, if any, before
3107 reclaiming memory. */
3108 if (flag_concepts)
3109 remove_constraints (newdecl);
3110
3111 ggc_free (newdecl);
3112
3113 return olddecl;
3114 }
3115
3116 /* Return zero if the declaration NEWDECL is valid
3117 when the declaration OLDDECL (assumed to be for the same name)
3118 has already been seen.
3119 Otherwise return an error message format string with a %s
3120 where the identifier should go. */
3121
3122 static const char *
redeclaration_error_message(tree newdecl,tree olddecl)3123 redeclaration_error_message (tree newdecl, tree olddecl)
3124 {
3125 if (TREE_CODE (newdecl) == TYPE_DECL)
3126 {
3127 /* Because C++ can put things into name space for free,
3128 constructs like "typedef struct foo { ... } foo"
3129 would look like an erroneous redeclaration. */
3130 if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl)))
3131 return NULL;
3132 else
3133 return G_("redefinition of %q#D");
3134 }
3135 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
3136 {
3137 /* If this is a pure function, its olddecl will actually be
3138 the original initialization to `0' (which we force to call
3139 abort()). Don't complain about redefinition in this case. */
3140 if (DECL_LANG_SPECIFIC (olddecl) && DECL_PURE_VIRTUAL_P (olddecl)
3141 && DECL_INITIAL (olddecl) == NULL_TREE)
3142 return NULL;
3143
3144 /* If both functions come from different namespaces, this is not
3145 a redeclaration - this is a conflict with a used function. */
3146 if (DECL_NAMESPACE_SCOPE_P (olddecl)
3147 && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl)
3148 && ! decls_match (olddecl, newdecl))
3149 return G_("%qD conflicts with used function");
3150
3151 /* We'll complain about linkage mismatches in
3152 warn_extern_redeclared_static. */
3153
3154 /* Defining the same name twice is no good. */
3155 if (decl_defined_p (olddecl)
3156 && decl_defined_p (newdecl))
3157 {
3158 if (DECL_NAME (olddecl) == NULL_TREE)
3159 return G_("%q#D not declared in class");
3160 else if (!GNU_INLINE_P (olddecl)
3161 || GNU_INLINE_P (newdecl))
3162 return G_("redefinition of %q#D");
3163 }
3164
3165 if (DECL_DECLARED_INLINE_P (olddecl) && DECL_DECLARED_INLINE_P (newdecl))
3166 {
3167 bool olda = GNU_INLINE_P (olddecl);
3168 bool newa = GNU_INLINE_P (newdecl);
3169
3170 if (olda != newa)
3171 {
3172 if (newa)
3173 return G_("%q+D redeclared inline with "
3174 "%<gnu_inline%> attribute");
3175 else
3176 return G_("%q+D redeclared inline without "
3177 "%<gnu_inline%> attribute");
3178 }
3179 }
3180
3181 if (deduction_guide_p (olddecl)
3182 && deduction_guide_p (newdecl))
3183 return G_("deduction guide %q+D redeclared");
3184
3185 /* [class.compare.default]: A definition of a comparison operator as
3186 defaulted that appears in a class shall be the first declaration of
3187 that function. */
3188 special_function_kind sfk = special_function_p (olddecl);
3189 if (sfk == sfk_comparison && DECL_DEFAULTED_FN (newdecl))
3190 return G_("comparison operator %q+D defaulted after "
3191 "its first declaration");
3192
3193 check_abi_tag_redeclaration
3194 (olddecl, lookup_attribute ("abi_tag", DECL_ATTRIBUTES (olddecl)),
3195 lookup_attribute ("abi_tag", DECL_ATTRIBUTES (newdecl)));
3196
3197 return NULL;
3198 }
3199 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
3200 {
3201 tree nt, ot;
3202
3203 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == CONCEPT_DECL)
3204 return G_("redefinition of %q#D");
3205
3206 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) != FUNCTION_DECL)
3207 return redeclaration_error_message (DECL_TEMPLATE_RESULT (newdecl),
3208 DECL_TEMPLATE_RESULT (olddecl));
3209
3210 if (DECL_TEMPLATE_RESULT (newdecl) == DECL_TEMPLATE_RESULT (olddecl))
3211 return NULL;
3212
3213 nt = DECL_TEMPLATE_RESULT (newdecl);
3214 if (DECL_TEMPLATE_INFO (nt))
3215 nt = DECL_TEMPLATE_RESULT (template_for_substitution (nt));
3216 ot = DECL_TEMPLATE_RESULT (olddecl);
3217 if (DECL_TEMPLATE_INFO (ot))
3218 ot = DECL_TEMPLATE_RESULT (template_for_substitution (ot));
3219 if (DECL_INITIAL (nt) && DECL_INITIAL (ot)
3220 && (!GNU_INLINE_P (ot) || GNU_INLINE_P (nt)))
3221 return G_("redefinition of %q#D");
3222
3223 if (DECL_DECLARED_INLINE_P (ot) && DECL_DECLARED_INLINE_P (nt))
3224 {
3225 bool olda = GNU_INLINE_P (ot);
3226 bool newa = GNU_INLINE_P (nt);
3227
3228 if (olda != newa)
3229 {
3230 if (newa)
3231 return G_("%q+D redeclared inline with "
3232 "%<gnu_inline%> attribute");
3233 else
3234 return G_("%q+D redeclared inline without "
3235 "%<gnu_inline%> attribute");
3236 }
3237 }
3238
3239 if (deduction_guide_p (olddecl)
3240 && deduction_guide_p (newdecl))
3241 return G_("deduction guide %q+D redeclared");
3242
3243 /* Core issue #226 (C++11):
3244
3245 If a friend function template declaration specifies a
3246 default template-argument, that declaration shall be a
3247 definition and shall be the only declaration of the
3248 function template in the translation unit. */
3249 if ((cxx_dialect != cxx98)
3250 && TREE_CODE (ot) == FUNCTION_DECL && DECL_UNIQUE_FRIEND_P (ot)
3251 && !check_default_tmpl_args (nt, DECL_TEMPLATE_PARMS (newdecl),
3252 /*is_primary=*/true,
3253 /*is_partial=*/false,
3254 /*is_friend_decl=*/2))
3255 return G_("redeclaration of friend %q#D "
3256 "may not have default template arguments");
3257
3258 return NULL;
3259 }
3260 else if (VAR_P (newdecl)
3261 && (CP_DECL_THREAD_LOCAL_P (newdecl)
3262 != CP_DECL_THREAD_LOCAL_P (olddecl))
3263 && (! DECL_LANG_SPECIFIC (olddecl)
3264 || ! CP_DECL_THREADPRIVATE_P (olddecl)
3265 || CP_DECL_THREAD_LOCAL_P (newdecl)))
3266 {
3267 /* Only variables can be thread-local, and all declarations must
3268 agree on this property. */
3269 if (CP_DECL_THREAD_LOCAL_P (newdecl))
3270 return G_("thread-local declaration of %q#D follows "
3271 "non-thread-local declaration");
3272 else
3273 return G_("non-thread-local declaration of %q#D follows "
3274 "thread-local declaration");
3275 }
3276 else if (toplevel_bindings_p () || DECL_NAMESPACE_SCOPE_P (newdecl))
3277 {
3278 /* The objects have been declared at namespace scope. If either
3279 is a member of an anonymous union, then this is an invalid
3280 redeclaration. For example:
3281
3282 int i;
3283 union { int i; };
3284
3285 is invalid. */
3286 if ((VAR_P (newdecl) && DECL_ANON_UNION_VAR_P (newdecl))
3287 || (VAR_P (olddecl) && DECL_ANON_UNION_VAR_P (olddecl)))
3288 return G_("redeclaration of %q#D");
3289 /* If at least one declaration is a reference, there is no
3290 conflict. For example:
3291
3292 int i = 3;
3293 extern int i;
3294
3295 is valid. */
3296 if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
3297 return NULL;
3298
3299 /* Static data member declared outside a class definition
3300 if the variable is defined within the class with constexpr
3301 specifier is declaration rather than definition (and
3302 deprecated). */
3303 if (cxx_dialect >= cxx17
3304 && VAR_P (olddecl)
3305 && DECL_CLASS_SCOPE_P (olddecl)
3306 && DECL_DECLARED_CONSTEXPR_P (olddecl)
3307 && !DECL_INITIAL (newdecl))
3308 {
3309 DECL_EXTERNAL (newdecl) = 1;
3310 /* For now, only warn with explicit -Wdeprecated. */
3311 if (OPTION_SET_P (warn_deprecated))
3312 {
3313 auto_diagnostic_group d;
3314 if (warning_at (DECL_SOURCE_LOCATION (newdecl), OPT_Wdeprecated,
3315 "redundant redeclaration of %<constexpr%> "
3316 "static data member %qD", newdecl))
3317 inform (DECL_SOURCE_LOCATION (olddecl),
3318 "previous declaration of %qD", olddecl);
3319 }
3320 return NULL;
3321 }
3322
3323 /* Reject two definitions. */
3324 return G_("redefinition of %q#D");
3325 }
3326 else
3327 {
3328 /* Objects declared with block scope: */
3329 /* Reject two definitions, and reject a definition
3330 together with an external reference. */
3331 if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl)))
3332 return G_("redeclaration of %q#D");
3333 return NULL;
3334 }
3335 }
3336
3337
3338 /* Hash and equality functions for the named_label table. */
3339
3340 hashval_t
hash(const value_type entry)3341 named_label_hash::hash (const value_type entry)
3342 {
3343 return IDENTIFIER_HASH_VALUE (entry->name);
3344 }
3345
3346 bool
equal(const value_type entry,compare_type name)3347 named_label_hash::equal (const value_type entry, compare_type name)
3348 {
3349 return name == entry->name;
3350 }
3351
3352 /* Look for a label named ID in the current function. If one cannot
3353 be found, create one. Return the named_label_entry, or NULL on
3354 failure. */
3355
3356 static named_label_entry *
lookup_label_1(tree id,bool making_local_p)3357 lookup_label_1 (tree id, bool making_local_p)
3358 {
3359 auto_cond_timevar tv (TV_NAME_LOOKUP);
3360
3361 /* You can't use labels at global scope. */
3362 if (current_function_decl == NULL_TREE)
3363 {
3364 error ("label %qE referenced outside of any function", id);
3365 return NULL;
3366 }
3367
3368 if (!named_labels)
3369 named_labels = hash_table<named_label_hash>::create_ggc (13);
3370
3371 hashval_t hash = IDENTIFIER_HASH_VALUE (id);
3372 named_label_entry **slot
3373 = named_labels->find_slot_with_hash (id, hash, INSERT);
3374 named_label_entry *old = *slot;
3375
3376 if (old && old->label_decl)
3377 {
3378 if (!making_local_p)
3379 return old;
3380
3381 if (old->binding_level == current_binding_level)
3382 {
3383 error ("local label %qE conflicts with existing label", id);
3384 inform (DECL_SOURCE_LOCATION (old->label_decl), "previous label");
3385 return NULL;
3386 }
3387 }
3388
3389 /* We are making a new decl, create or reuse the named_label_entry */
3390 named_label_entry *ent = NULL;
3391 if (old && !old->label_decl)
3392 ent = old;
3393 else
3394 {
3395 ent = ggc_cleared_alloc<named_label_entry> ();
3396 ent->name = id;
3397 ent->outer = old;
3398 *slot = ent;
3399 }
3400
3401 /* Now create the LABEL_DECL. */
3402 tree decl = build_decl (input_location, LABEL_DECL, id, void_type_node);
3403
3404 DECL_CONTEXT (decl) = current_function_decl;
3405 SET_DECL_MODE (decl, VOIDmode);
3406 if (making_local_p)
3407 {
3408 C_DECLARED_LABEL_FLAG (decl) = true;
3409 DECL_CHAIN (decl) = current_binding_level->names;
3410 current_binding_level->names = decl;
3411 }
3412
3413 ent->label_decl = decl;
3414
3415 return ent;
3416 }
3417
3418 /* Wrapper for lookup_label_1. */
3419
3420 tree
lookup_label(tree id)3421 lookup_label (tree id)
3422 {
3423 named_label_entry *ent = lookup_label_1 (id, false);
3424 return ent ? ent->label_decl : NULL_TREE;
3425 }
3426
3427 tree
declare_local_label(tree id)3428 declare_local_label (tree id)
3429 {
3430 named_label_entry *ent = lookup_label_1 (id, true);
3431 return ent ? ent->label_decl : NULL_TREE;
3432 }
3433
3434 /* Returns nonzero if it is ill-formed to jump past the declaration of
3435 DECL. Returns 2 if it's also a real problem. */
3436
3437 static int
decl_jump_unsafe(tree decl)3438 decl_jump_unsafe (tree decl)
3439 {
3440 /* [stmt.dcl]/3: A program that jumps from a point where a local variable
3441 with automatic storage duration is not in scope to a point where it is
3442 in scope is ill-formed unless the variable has scalar type, class type
3443 with a trivial default constructor and a trivial destructor, a
3444 cv-qualified version of one of these types, or an array of one of the
3445 preceding types and is declared without an initializer (8.5). */
3446 tree type = TREE_TYPE (decl);
3447
3448 if (!VAR_P (decl) || TREE_STATIC (decl)
3449 || type == error_mark_node)
3450 return 0;
3451
3452 if (DECL_NONTRIVIALLY_INITIALIZED_P (decl)
3453 || variably_modified_type_p (type, NULL_TREE))
3454 return 2;
3455
3456 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3457 return 1;
3458
3459 return 0;
3460 }
3461
3462 /* A subroutine of check_previous_goto_1 and check_goto to identify a branch
3463 to the user. */
3464
3465 static bool
identify_goto(tree decl,location_t loc,const location_t * locus,diagnostic_t diag_kind)3466 identify_goto (tree decl, location_t loc, const location_t *locus,
3467 diagnostic_t diag_kind)
3468 {
3469 bool complained
3470 = emit_diagnostic (diag_kind, loc, 0,
3471 decl ? N_("jump to label %qD")
3472 : N_("jump to case label"), decl);
3473 if (complained && locus)
3474 inform (*locus, " from here");
3475 return complained;
3476 }
3477
3478 /* Check that a single previously seen jump to a newly defined label
3479 is OK. DECL is the LABEL_DECL or 0; LEVEL is the binding_level for
3480 the jump context; NAMES are the names in scope in LEVEL at the jump
3481 context; LOCUS is the source position of the jump or 0. Returns
3482 true if all is well. */
3483
3484 static bool
check_previous_goto_1(tree decl,cp_binding_level * level,tree names,bool exited_omp,const location_t * locus)3485 check_previous_goto_1 (tree decl, cp_binding_level* level, tree names,
3486 bool exited_omp, const location_t *locus)
3487 {
3488 cp_binding_level *b;
3489 bool complained = false;
3490 int identified = 0;
3491 bool saw_eh = false, saw_omp = false, saw_tm = false, saw_cxif = false;
3492 bool saw_ceif = false;
3493
3494 if (exited_omp)
3495 {
3496 complained = identify_goto (decl, input_location, locus, DK_ERROR);
3497 if (complained)
3498 inform (input_location, " exits OpenMP structured block");
3499 saw_omp = true;
3500 identified = 2;
3501 }
3502
3503 for (b = current_binding_level; b ; b = b->level_chain)
3504 {
3505 tree new_decls, old_decls = (b == level ? names : NULL_TREE);
3506
3507 for (new_decls = b->names; new_decls != old_decls;
3508 new_decls = (DECL_P (new_decls) ? DECL_CHAIN (new_decls)
3509 : TREE_CHAIN (new_decls)))
3510 {
3511 int problem = decl_jump_unsafe (new_decls);
3512 if (! problem)
3513 continue;
3514
3515 if (!identified)
3516 {
3517 complained = identify_goto (decl, input_location, locus,
3518 problem > 1
3519 ? DK_ERROR : DK_PERMERROR);
3520 identified = 1;
3521 }
3522 if (complained)
3523 {
3524 if (problem > 1)
3525 inform (DECL_SOURCE_LOCATION (new_decls),
3526 " crosses initialization of %q#D", new_decls);
3527 else
3528 inform (DECL_SOURCE_LOCATION (new_decls),
3529 " enters scope of %q#D, which has "
3530 "non-trivial destructor", new_decls);
3531 }
3532 }
3533
3534 if (b == level)
3535 break;
3536
3537 const char *inf = NULL;
3538 location_t loc = input_location;
3539 switch (b->kind)
3540 {
3541 case sk_try:
3542 if (!saw_eh)
3543 inf = G_(" enters %<try%> block");
3544 saw_eh = true;
3545 break;
3546
3547 case sk_catch:
3548 if (!saw_eh)
3549 inf = G_(" enters %<catch%> block");
3550 saw_eh = true;
3551 break;
3552
3553 case sk_omp:
3554 if (!saw_omp)
3555 inf = G_(" enters OpenMP structured block");
3556 saw_omp = true;
3557 break;
3558
3559 case sk_transaction:
3560 if (!saw_tm)
3561 inf = G_(" enters synchronized or atomic statement");
3562 saw_tm = true;
3563 break;
3564
3565 case sk_block:
3566 if (!saw_cxif && level_for_constexpr_if (b->level_chain))
3567 {
3568 inf = G_(" enters %<constexpr if%> statement");
3569 loc = EXPR_LOCATION (b->level_chain->this_entity);
3570 saw_cxif = true;
3571 }
3572 else if (!saw_ceif && level_for_consteval_if (b->level_chain))
3573 {
3574 inf = G_(" enters %<consteval if%> statement");
3575 loc = EXPR_LOCATION (b->level_chain->this_entity);
3576 saw_ceif = true;
3577 }
3578 break;
3579
3580 default:
3581 break;
3582 }
3583
3584 if (inf)
3585 {
3586 if (identified < 2)
3587 complained = identify_goto (decl, input_location, locus, DK_ERROR);
3588 identified = 2;
3589 if (complained)
3590 inform (loc, inf);
3591 }
3592 }
3593
3594 return !identified;
3595 }
3596
3597 static void
check_previous_goto(tree decl,struct named_label_use_entry * use)3598 check_previous_goto (tree decl, struct named_label_use_entry *use)
3599 {
3600 check_previous_goto_1 (decl, use->binding_level,
3601 use->names_in_scope, use->in_omp_scope,
3602 &use->o_goto_locus);
3603 }
3604
3605 static bool
check_switch_goto(cp_binding_level * level)3606 check_switch_goto (cp_binding_level* level)
3607 {
3608 return check_previous_goto_1 (NULL_TREE, level, level->names, false, NULL);
3609 }
3610
3611 /* Check that a new jump to a label DECL is OK. Called by
3612 finish_goto_stmt. */
3613
3614 void
check_goto(tree decl)3615 check_goto (tree decl)
3616 {
3617 /* We can't know where a computed goto is jumping.
3618 So we assume that it's OK. */
3619 if (TREE_CODE (decl) != LABEL_DECL)
3620 return;
3621
3622 /* We didn't record any information about this label when we created it,
3623 and there's not much point since it's trivial to analyze as a return. */
3624 if (decl == cdtor_label)
3625 return;
3626
3627 hashval_t hash = IDENTIFIER_HASH_VALUE (DECL_NAME (decl));
3628 named_label_entry **slot
3629 = named_labels->find_slot_with_hash (DECL_NAME (decl), hash, NO_INSERT);
3630 named_label_entry *ent = *slot;
3631
3632 /* If the label hasn't been defined yet, defer checking. */
3633 if (! DECL_INITIAL (decl))
3634 {
3635 /* Don't bother creating another use if the last goto had the
3636 same data, and will therefore create the same set of errors. */
3637 if (ent->uses
3638 && ent->uses->names_in_scope == current_binding_level->names)
3639 return;
3640
3641 named_label_use_entry *new_use
3642 = ggc_alloc<named_label_use_entry> ();
3643 new_use->binding_level = current_binding_level;
3644 new_use->names_in_scope = current_binding_level->names;
3645 new_use->o_goto_locus = input_location;
3646 new_use->in_omp_scope = false;
3647
3648 new_use->next = ent->uses;
3649 ent->uses = new_use;
3650 return;
3651 }
3652
3653 cp_function_chain->backward_goto = true;
3654
3655 bool saw_catch = false, complained = false;
3656 int identified = 0;
3657 tree bad;
3658 unsigned ix;
3659
3660 if (ent->in_try_scope || ent->in_catch_scope || ent->in_transaction_scope
3661 || ent->in_constexpr_if || ent->in_consteval_if
3662 || ent->in_omp_scope || !vec_safe_is_empty (ent->bad_decls))
3663 {
3664 diagnostic_t diag_kind = DK_PERMERROR;
3665 if (ent->in_try_scope || ent->in_catch_scope || ent->in_constexpr_if
3666 || ent->in_consteval_if || ent->in_transaction_scope
3667 || ent->in_omp_scope)
3668 diag_kind = DK_ERROR;
3669 complained = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
3670 &input_location, diag_kind);
3671 identified = 1 + (diag_kind == DK_ERROR);
3672 }
3673
3674 FOR_EACH_VEC_SAFE_ELT (ent->bad_decls, ix, bad)
3675 {
3676 int u = decl_jump_unsafe (bad);
3677
3678 if (u > 1 && DECL_ARTIFICIAL (bad))
3679 {
3680 /* Can't skip init of __exception_info. */
3681 if (identified == 1)
3682 {
3683 complained = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
3684 &input_location, DK_ERROR);
3685 identified = 2;
3686 }
3687 if (complained)
3688 inform (DECL_SOURCE_LOCATION (bad), " enters %<catch%> block");
3689 saw_catch = true;
3690 }
3691 else if (complained)
3692 {
3693 if (u > 1)
3694 inform (DECL_SOURCE_LOCATION (bad),
3695 " skips initialization of %q#D", bad);
3696 else
3697 inform (DECL_SOURCE_LOCATION (bad),
3698 " enters scope of %q#D which has "
3699 "non-trivial destructor", bad);
3700 }
3701 }
3702
3703 if (complained)
3704 {
3705 if (ent->in_try_scope)
3706 inform (input_location, " enters %<try%> block");
3707 else if (ent->in_catch_scope && !saw_catch)
3708 inform (input_location, " enters %<catch%> block");
3709 else if (ent->in_transaction_scope)
3710 inform (input_location, " enters synchronized or atomic statement");
3711 else if (ent->in_constexpr_if)
3712 inform (input_location, " enters %<constexpr if%> statement");
3713 else if (ent->in_consteval_if)
3714 inform (input_location, " enters %<consteval if%> statement");
3715 }
3716
3717 if (ent->in_omp_scope)
3718 {
3719 if (complained)
3720 inform (input_location, " enters OpenMP structured block");
3721 }
3722 else if (flag_openmp)
3723 for (cp_binding_level *b = current_binding_level; b ; b = b->level_chain)
3724 {
3725 if (b == ent->binding_level)
3726 break;
3727 if (b->kind == sk_omp)
3728 {
3729 if (identified < 2)
3730 {
3731 complained = identify_goto (decl,
3732 DECL_SOURCE_LOCATION (decl),
3733 &input_location, DK_ERROR);
3734 identified = 2;
3735 }
3736 if (complained)
3737 inform (input_location, " exits OpenMP structured block");
3738 break;
3739 }
3740 }
3741 }
3742
3743 /* Check that a return is ok wrt OpenMP structured blocks.
3744 Called by finish_return_stmt. Returns true if all is well. */
3745
3746 bool
check_omp_return(void)3747 check_omp_return (void)
3748 {
3749 for (cp_binding_level *b = current_binding_level; b ; b = b->level_chain)
3750 if (b->kind == sk_omp)
3751 {
3752 error ("invalid exit from OpenMP structured block");
3753 return false;
3754 }
3755 else if (b->kind == sk_function_parms)
3756 break;
3757 return true;
3758 }
3759
3760 /* Define a label, specifying the location in the source file.
3761 Return the LABEL_DECL node for the label. */
3762
3763 tree
define_label(location_t location,tree name)3764 define_label (location_t location, tree name)
3765 {
3766 auto_cond_timevar tv (TV_NAME_LOOKUP);
3767
3768 /* After labels, make any new cleanups in the function go into their
3769 own new (temporary) binding contour. */
3770 for (cp_binding_level *p = current_binding_level;
3771 p->kind != sk_function_parms;
3772 p = p->level_chain)
3773 p->more_cleanups_ok = 0;
3774
3775 named_label_entry *ent = lookup_label_1 (name, false);
3776 tree decl = ent->label_decl;
3777
3778 if (DECL_INITIAL (decl) != NULL_TREE)
3779 {
3780 error ("duplicate label %qD", decl);
3781 return error_mark_node;
3782 }
3783 else
3784 {
3785 /* Mark label as having been defined. */
3786 DECL_INITIAL (decl) = error_mark_node;
3787 /* Say where in the source. */
3788 DECL_SOURCE_LOCATION (decl) = location;
3789
3790 ent->binding_level = current_binding_level;
3791 ent->names_in_scope = current_binding_level->names;
3792
3793 for (named_label_use_entry *use = ent->uses; use; use = use->next)
3794 check_previous_goto (decl, use);
3795 ent->uses = NULL;
3796 }
3797
3798 return decl;
3799 }
3800
3801 struct cp_switch
3802 {
3803 cp_binding_level *level;
3804 struct cp_switch *next;
3805 /* The SWITCH_STMT being built. */
3806 tree switch_stmt;
3807 /* A splay-tree mapping the low element of a case range to the high
3808 element, or NULL_TREE if there is no high element. Used to
3809 determine whether or not a new case label duplicates an old case
3810 label. We need a tree, rather than simply a hash table, because
3811 of the GNU case range extension. */
3812 splay_tree cases;
3813 /* Remember whether a default: case label has been seen. */
3814 bool has_default_p;
3815 /* Remember whether a BREAK_STMT has been seen in this SWITCH_STMT. */
3816 bool break_stmt_seen_p;
3817 /* Set if inside of {FOR,DO,WHILE}_BODY nested inside of a switch,
3818 where BREAK_STMT doesn't belong to the SWITCH_STMT. */
3819 bool in_loop_body_p;
3820 };
3821
3822 /* A stack of the currently active switch statements. The innermost
3823 switch statement is on the top of the stack. There is no need to
3824 mark the stack for garbage collection because it is only active
3825 during the processing of the body of a function, and we never
3826 collect at that point. */
3827
3828 static struct cp_switch *switch_stack;
3829
3830 /* Called right after a switch-statement condition is parsed.
3831 SWITCH_STMT is the switch statement being parsed. */
3832
3833 void
push_switch(tree switch_stmt)3834 push_switch (tree switch_stmt)
3835 {
3836 struct cp_switch *p = XNEW (struct cp_switch);
3837 p->level = current_binding_level;
3838 p->next = switch_stack;
3839 p->switch_stmt = switch_stmt;
3840 p->cases = splay_tree_new (case_compare, NULL, NULL);
3841 p->has_default_p = false;
3842 p->break_stmt_seen_p = false;
3843 p->in_loop_body_p = false;
3844 switch_stack = p;
3845 }
3846
3847 void
pop_switch(void)3848 pop_switch (void)
3849 {
3850 struct cp_switch *cs = switch_stack;
3851
3852 /* Emit warnings as needed. */
3853 location_t switch_location = cp_expr_loc_or_input_loc (cs->switch_stmt);
3854 tree cond = SWITCH_STMT_COND (cs->switch_stmt);
3855 const bool bool_cond_p
3856 = (SWITCH_STMT_TYPE (cs->switch_stmt)
3857 && TREE_CODE (SWITCH_STMT_TYPE (cs->switch_stmt)) == BOOLEAN_TYPE);
3858 if (!processing_template_decl)
3859 c_do_switch_warnings (cs->cases, switch_location,
3860 SWITCH_STMT_TYPE (cs->switch_stmt), cond,
3861 bool_cond_p);
3862
3863 /* For the benefit of block_may_fallthru remember if the switch body
3864 case labels cover all possible values and if there are break; stmts. */
3865 if (cs->has_default_p
3866 || (!processing_template_decl
3867 && c_switch_covers_all_cases_p (cs->cases,
3868 SWITCH_STMT_TYPE (cs->switch_stmt))))
3869 SWITCH_STMT_ALL_CASES_P (cs->switch_stmt) = 1;
3870 if (!cs->break_stmt_seen_p)
3871 SWITCH_STMT_NO_BREAK_P (cs->switch_stmt) = 1;
3872 /* Now that we're done with the switch warnings, set the switch type
3873 to the type of the condition if the index type was of scoped enum type.
3874 (Such types don't participate in the integer promotions.) We do this
3875 because of bit-fields whose declared type is a scoped enum type:
3876 gimplification will use the lowered index type, but convert the
3877 case values to SWITCH_STMT_TYPE, which would have been the declared type
3878 and verify_gimple_switch doesn't accept that. */
3879 if (is_bitfield_expr_with_lowered_type (cond))
3880 SWITCH_STMT_TYPE (cs->switch_stmt) = TREE_TYPE (cond);
3881 gcc_assert (!cs->in_loop_body_p);
3882 splay_tree_delete (cs->cases);
3883 switch_stack = switch_stack->next;
3884 free (cs);
3885 }
3886
3887 /* Note that a BREAK_STMT is about to be added. If it is inside of
3888 a SWITCH_STMT and not inside of a loop body inside of it, note
3889 in switch_stack we've seen a BREAK_STMT. */
3890
3891 void
note_break_stmt(void)3892 note_break_stmt (void)
3893 {
3894 if (switch_stack && !switch_stack->in_loop_body_p)
3895 switch_stack->break_stmt_seen_p = true;
3896 }
3897
3898 /* Note the start of processing of an iteration statement's body.
3899 The note_break_stmt function will do nothing while processing it.
3900 Return a flag that should be passed to note_iteration_stmt_body_end. */
3901
3902 bool
note_iteration_stmt_body_start(void)3903 note_iteration_stmt_body_start (void)
3904 {
3905 if (!switch_stack)
3906 return false;
3907 bool ret = switch_stack->in_loop_body_p;
3908 switch_stack->in_loop_body_p = true;
3909 return ret;
3910 }
3911
3912 /* Note the end of processing of an iteration statement's body. */
3913
3914 void
note_iteration_stmt_body_end(bool prev)3915 note_iteration_stmt_body_end (bool prev)
3916 {
3917 if (switch_stack)
3918 switch_stack->in_loop_body_p = prev;
3919 }
3920
3921 /* Convert a case constant VALUE in a switch to the type TYPE of the switch
3922 condition. Note that if TYPE and VALUE are already integral we don't
3923 really do the conversion because the language-independent
3924 warning/optimization code will work better that way. */
3925
3926 static tree
case_conversion(tree type,tree value)3927 case_conversion (tree type, tree value)
3928 {
3929 if (value == NULL_TREE)
3930 return value;
3931
3932 value = mark_rvalue_use (value);
3933
3934 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
3935 type = type_promotes_to (type);
3936
3937 tree ovalue = value;
3938 /* The constant-expression VALUE shall be a converted constant expression
3939 of the adjusted type of the switch condition, which doesn't allow
3940 narrowing conversions. */
3941 value = build_converted_constant_expr (type, value, tf_warning_or_error);
3942
3943 if (cxx_dialect >= cxx11
3944 && (SCOPED_ENUM_P (type)
3945 || !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (ovalue))))
3946 /* Use the converted value. */;
3947 else
3948 /* The already integral case. */
3949 value = ovalue;
3950
3951 return cxx_constant_value (value);
3952 }
3953
3954 /* Note that we've seen a definition of a case label, and complain if this
3955 is a bad place for one. */
3956
3957 tree
finish_case_label(location_t loc,tree low_value,tree high_value)3958 finish_case_label (location_t loc, tree low_value, tree high_value)
3959 {
3960 tree cond, r;
3961 cp_binding_level *p;
3962 tree type;
3963
3964 if (low_value == NULL_TREE && high_value == NULL_TREE)
3965 switch_stack->has_default_p = true;
3966
3967 if (processing_template_decl)
3968 {
3969 tree label;
3970
3971 /* For templates, just add the case label; we'll do semantic
3972 analysis at instantiation-time. */
3973 label = build_decl (loc, LABEL_DECL, NULL_TREE, void_type_node);
3974 return add_stmt (build_case_label (low_value, high_value, label));
3975 }
3976
3977 /* Find the condition on which this switch statement depends. */
3978 cond = SWITCH_STMT_COND (switch_stack->switch_stmt);
3979 if (cond && TREE_CODE (cond) == TREE_LIST)
3980 cond = TREE_VALUE (cond);
3981
3982 if (!check_switch_goto (switch_stack->level))
3983 return error_mark_node;
3984
3985 type = SWITCH_STMT_TYPE (switch_stack->switch_stmt);
3986 if (type == error_mark_node)
3987 return error_mark_node;
3988
3989 low_value = case_conversion (type, low_value);
3990 high_value = case_conversion (type, high_value);
3991
3992 r = c_add_case_label (loc, switch_stack->cases, cond, low_value, high_value);
3993
3994 /* After labels, make any new cleanups in the function go into their
3995 own new (temporary) binding contour. */
3996 for (p = current_binding_level;
3997 p->kind != sk_function_parms;
3998 p = p->level_chain)
3999 p->more_cleanups_ok = 0;
4000
4001 return r;
4002 }
4003
4004 struct typename_info {
4005 tree scope;
4006 tree name;
4007 tree template_id;
4008 bool enum_p;
4009 bool class_p;
4010 };
4011
4012 struct typename_hasher : ggc_ptr_hash<tree_node>
4013 {
4014 typedef typename_info *compare_type;
4015
4016 /* Hash a TYPENAME_TYPE. */
4017
4018 static hashval_t
hashtypename_hasher4019 hash (tree t)
4020 {
4021 hashval_t hash;
4022
4023 hash = (htab_hash_pointer (TYPE_CONTEXT (t))
4024 ^ htab_hash_pointer (TYPE_IDENTIFIER (t)));
4025
4026 return hash;
4027 }
4028
4029 /* Compare two TYPENAME_TYPEs. */
4030
4031 static bool
equaltypename_hasher4032 equal (tree t1, const typename_info *t2)
4033 {
4034 return (TYPE_IDENTIFIER (t1) == t2->name
4035 && TYPE_CONTEXT (t1) == t2->scope
4036 && TYPENAME_TYPE_FULLNAME (t1) == t2->template_id
4037 && TYPENAME_IS_ENUM_P (t1) == t2->enum_p
4038 && TYPENAME_IS_CLASS_P (t1) == t2->class_p);
4039 }
4040 };
4041
4042 /* Build a TYPENAME_TYPE. If the type is `typename T::t', CONTEXT is
4043 the type of `T', NAME is the IDENTIFIER_NODE for `t'.
4044
4045 Returns the new TYPENAME_TYPE. */
4046
4047 static GTY (()) hash_table<typename_hasher> *typename_htab;
4048
4049 tree
build_typename_type(tree context,tree name,tree fullname,enum tag_types tag_type)4050 build_typename_type (tree context, tree name, tree fullname,
4051 enum tag_types tag_type)
4052 {
4053 typename_info ti;
4054
4055 if (typename_htab == NULL)
4056 typename_htab = hash_table<typename_hasher>::create_ggc (61);
4057
4058 ti.scope = FROB_CONTEXT (context);
4059 ti.name = name;
4060 ti.template_id = fullname;
4061 ti.enum_p = tag_type == enum_type;
4062 ti.class_p = (tag_type == class_type
4063 || tag_type == record_type
4064 || tag_type == union_type);
4065 hashval_t hash = (htab_hash_pointer (ti.scope)
4066 ^ htab_hash_pointer (ti.name));
4067
4068 /* See if we already have this type. */
4069 tree *e = typename_htab->find_slot_with_hash (&ti, hash, INSERT);
4070 tree t = *e;
4071 if (*e)
4072 t = *e;
4073 else
4074 {
4075 /* Build the TYPENAME_TYPE. */
4076 t = cxx_make_type (TYPENAME_TYPE);
4077 TYPE_CONTEXT (t) = ti.scope;
4078 TYPENAME_TYPE_FULLNAME (t) = ti.template_id;
4079 TYPENAME_IS_ENUM_P (t) = ti.enum_p;
4080 TYPENAME_IS_CLASS_P (t) = ti.class_p;
4081
4082 /* Build the corresponding TYPE_DECL. */
4083 tree d = build_decl (input_location, TYPE_DECL, name, t);
4084 TYPE_NAME (t) = d;
4085 TYPE_STUB_DECL (t) = d;
4086 DECL_CONTEXT (d) = ti.scope;
4087 DECL_ARTIFICIAL (d) = 1;
4088
4089 /* Store it in the hash table. */
4090 *e = t;
4091
4092 /* TYPENAME_TYPEs must always be compared structurally, because
4093 they may or may not resolve down to another type depending on
4094 the currently open classes. */
4095 SET_TYPE_STRUCTURAL_EQUALITY (t);
4096 }
4097
4098 return t;
4099 }
4100
4101 /* Resolve `typename CONTEXT::NAME'. TAG_TYPE indicates the tag
4102 provided to name the type. Returns an appropriate type, unless an
4103 error occurs, in which case error_mark_node is returned. If we
4104 locate a non-artificial TYPE_DECL and TF_KEEP_TYPE_DECL is set, we
4105 return that, rather than the _TYPE it corresponds to, in other
4106 cases we look through the type decl. If TF_ERROR is set, complain
4107 about errors, otherwise be quiet. */
4108
4109 tree
make_typename_type(tree context,tree name,enum tag_types tag_type,tsubst_flags_t complain)4110 make_typename_type (tree context, tree name, enum tag_types tag_type,
4111 tsubst_flags_t complain)
4112 {
4113 tree fullname;
4114 tree t;
4115 bool want_template;
4116
4117 if (name == error_mark_node
4118 || context == NULL_TREE
4119 || context == error_mark_node)
4120 return error_mark_node;
4121
4122 if (TYPE_P (name))
4123 {
4124 if (!(TYPE_LANG_SPECIFIC (name)
4125 && (CLASSTYPE_IS_TEMPLATE (name)
4126 || CLASSTYPE_USE_TEMPLATE (name))))
4127 name = TYPE_IDENTIFIER (name);
4128 else
4129 /* Create a TEMPLATE_ID_EXPR for the type. */
4130 name = build_nt (TEMPLATE_ID_EXPR,
4131 CLASSTYPE_TI_TEMPLATE (name),
4132 CLASSTYPE_TI_ARGS (name));
4133 }
4134 else if (TREE_CODE (name) == TYPE_DECL)
4135 name = DECL_NAME (name);
4136
4137 fullname = name;
4138
4139 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
4140 {
4141 name = TREE_OPERAND (name, 0);
4142 if (DECL_TYPE_TEMPLATE_P (name))
4143 name = TREE_OPERAND (fullname, 0) = DECL_NAME (name);
4144 if (TREE_CODE (name) != IDENTIFIER_NODE)
4145 {
4146 if (complain & tf_error)
4147 error ("%qD is not a type", name);
4148 return error_mark_node;
4149 }
4150 }
4151 if (TREE_CODE (name) == TEMPLATE_DECL)
4152 {
4153 if (complain & tf_error)
4154 error ("%qD used without template arguments", name);
4155 return error_mark_node;
4156 }
4157 else if (is_overloaded_fn (name))
4158 {
4159 if (complain & tf_error)
4160 error ("%qD is a function, not a type", name);
4161 return error_mark_node;
4162 }
4163 gcc_assert (identifier_p (name));
4164 gcc_assert (TYPE_P (context));
4165
4166 if (TREE_CODE (context) == TYPE_PACK_EXPANSION)
4167 /* This can happen for C++17 variadic using (c++/88986). */;
4168 else if (!MAYBE_CLASS_TYPE_P (context))
4169 {
4170 if (complain & tf_error)
4171 error ("%q#T is not a class", context);
4172 return error_mark_node;
4173 }
4174
4175 /* When the CONTEXT is a dependent type, NAME could refer to a
4176 dependent base class of CONTEXT. But look inside it anyway
4177 if CONTEXT is a currently open scope, in case it refers to a
4178 member of the current instantiation or a non-dependent base;
4179 lookup will stop when we hit a dependent base. */
4180 if (!dependent_scope_p (context))
4181 /* We should only set WANT_TYPE when we're a nested typename type.
4182 Then we can give better diagnostics if we find a non-type. */
4183 t = lookup_field (context, name, 2, /*want_type=*/true);
4184 else
4185 t = NULL_TREE;
4186
4187 if ((!t || TREE_CODE (t) == TREE_LIST) && dependent_type_p (context))
4188 return build_typename_type (context, name, fullname, tag_type);
4189
4190 want_template = TREE_CODE (fullname) == TEMPLATE_ID_EXPR;
4191
4192 if (!t)
4193 {
4194 if (complain & tf_error)
4195 {
4196 if (!COMPLETE_TYPE_P (context))
4197 cxx_incomplete_type_error (NULL_TREE, context);
4198 else
4199 error (want_template ? G_("no class template named %q#T in %q#T")
4200 : G_("no type named %q#T in %q#T"), name, context);
4201 }
4202 return error_mark_node;
4203 }
4204
4205 /* Pull out the template from an injected-class-name (or multiple). */
4206 if (want_template)
4207 t = maybe_get_template_decl_from_type_decl (t);
4208
4209 if (TREE_CODE (t) == TREE_LIST)
4210 {
4211 if (complain & tf_error)
4212 {
4213 error ("lookup of %qT in %qT is ambiguous", name, context);
4214 print_candidates (t);
4215 }
4216 return error_mark_node;
4217 }
4218
4219 if (want_template && !DECL_TYPE_TEMPLATE_P (t))
4220 {
4221 if (complain & tf_error)
4222 error ("%<typename %T::%D%> names %q#T, which is not a class template",
4223 context, name, t);
4224 return error_mark_node;
4225 }
4226 if (!want_template && TREE_CODE (t) != TYPE_DECL)
4227 {
4228 if ((complain & tf_tst_ok) && cxx_dialect >= cxx17
4229 && DECL_TYPE_TEMPLATE_P (t))
4230 /* The caller permits this typename-specifier to name a template
4231 (because it appears in a CTAD-enabled context). */;
4232 else
4233 {
4234 if (complain & tf_error)
4235 error ("%<typename %T::%D%> names %q#T, which is not a type",
4236 context, name, t);
4237 return error_mark_node;
4238 }
4239 }
4240
4241 if (!check_accessibility_of_qualified_id (t, /*object_type=*/NULL_TREE,
4242 context, complain))
4243 return error_mark_node;
4244
4245 if (!want_template && DECL_TYPE_TEMPLATE_P (t))
4246 return make_template_placeholder (t);
4247
4248 if (want_template)
4249 {
4250 t = lookup_template_class (t, TREE_OPERAND (fullname, 1),
4251 NULL_TREE, context,
4252 /*entering_scope=*/0,
4253 complain | tf_user);
4254 if (t == error_mark_node)
4255 return error_mark_node;
4256 t = TYPE_NAME (t);
4257 }
4258
4259 if (DECL_ARTIFICIAL (t) || !(complain & tf_keep_type_decl))
4260 t = TREE_TYPE (t);
4261
4262 maybe_record_typedef_use (t);
4263
4264 return t;
4265 }
4266
4267 /* Resolve `CONTEXT::template NAME'. Returns a TEMPLATE_DECL if the name
4268 can be resolved or an UNBOUND_CLASS_TEMPLATE, unless an error occurs,
4269 in which case error_mark_node is returned.
4270
4271 If PARM_LIST is non-NULL, also make sure that the template parameter
4272 list of TEMPLATE_DECL matches.
4273
4274 If COMPLAIN zero, don't complain about any errors that occur. */
4275
4276 tree
make_unbound_class_template(tree context,tree name,tree parm_list,tsubst_flags_t complain)4277 make_unbound_class_template (tree context, tree name, tree parm_list,
4278 tsubst_flags_t complain)
4279 {
4280 if (TYPE_P (name))
4281 name = TYPE_IDENTIFIER (name);
4282 else if (DECL_P (name))
4283 name = DECL_NAME (name);
4284 gcc_assert (identifier_p (name));
4285
4286 if (!dependent_type_p (context)
4287 || currently_open_class (context))
4288 {
4289 tree tmpl = NULL_TREE;
4290
4291 if (MAYBE_CLASS_TYPE_P (context))
4292 tmpl = lookup_field (context, name, 0, false);
4293
4294 if (tmpl && TREE_CODE (tmpl) == TYPE_DECL)
4295 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
4296
4297 if (!tmpl || !DECL_TYPE_TEMPLATE_P (tmpl))
4298 {
4299 if (complain & tf_error)
4300 error ("no class template named %q#T in %q#T", name, context);
4301 return error_mark_node;
4302 }
4303
4304 if (parm_list
4305 && !comp_template_parms (DECL_TEMPLATE_PARMS (tmpl), parm_list))
4306 {
4307 if (complain & tf_error)
4308 {
4309 error ("template parameters do not match template %qD", tmpl);
4310 inform (DECL_SOURCE_LOCATION (tmpl),
4311 "%qD declared here", tmpl);
4312 }
4313 return error_mark_node;
4314 }
4315
4316 if (!perform_or_defer_access_check (TYPE_BINFO (context), tmpl, tmpl,
4317 complain))
4318 return error_mark_node;
4319
4320 return tmpl;
4321 }
4322
4323 return make_unbound_class_template_raw (context, name, parm_list);
4324 }
4325
4326 /* Build an UNBOUND_CLASS_TEMPLATE. */
4327
4328 tree
make_unbound_class_template_raw(tree context,tree name,tree parm_list)4329 make_unbound_class_template_raw (tree context, tree name, tree parm_list)
4330 {
4331 /* Build the UNBOUND_CLASS_TEMPLATE. */
4332 tree t = cxx_make_type (UNBOUND_CLASS_TEMPLATE);
4333 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
4334 TREE_TYPE (t) = NULL_TREE;
4335 SET_TYPE_STRUCTURAL_EQUALITY (t);
4336
4337 /* Build the corresponding TEMPLATE_DECL. */
4338 tree d = build_decl (input_location, TEMPLATE_DECL, name, t);
4339 TYPE_NAME (t) = d;
4340 TYPE_STUB_DECL (t) = d;
4341 DECL_CONTEXT (d) = TYPE_CONTEXT (t);
4342 DECL_ARTIFICIAL (d) = 1;
4343 DECL_TEMPLATE_PARMS (d) = parm_list;
4344
4345 return t;
4346 }
4347
4348
4349
4350 /* Push the declarations of builtin types into the global namespace.
4351 RID_INDEX is the index of the builtin type in the array
4352 RID_POINTERS. NAME is the name used when looking up the builtin
4353 type. TYPE is the _TYPE node for the builtin type.
4354
4355 The calls to set_global_binding below should be
4356 eliminated. Built-in types should not be looked up name; their
4357 names are keywords that the parser can recognize. However, there
4358 is code in c-common.cc that uses identifier_global_value to look up
4359 built-in types by name. */
4360
4361 void
record_builtin_type(enum rid rid_index,const char * name,tree type)4362 record_builtin_type (enum rid rid_index,
4363 const char* name,
4364 tree type)
4365 {
4366 tree decl = NULL_TREE;
4367
4368 if (name)
4369 {
4370 tree tname = get_identifier (name);
4371 tree tdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, tname, type);
4372 DECL_ARTIFICIAL (tdecl) = 1;
4373 set_global_binding (tdecl);
4374 decl = tdecl;
4375 }
4376
4377 if ((int) rid_index < (int) RID_MAX)
4378 if (tree rname = ridpointers[(int) rid_index])
4379 if (!decl || DECL_NAME (decl) != rname)
4380 {
4381 tree rdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, rname, type);
4382 DECL_ARTIFICIAL (rdecl) = 1;
4383 set_global_binding (rdecl);
4384 if (!decl)
4385 decl = rdecl;
4386 }
4387
4388 if (decl)
4389 {
4390 if (!TYPE_NAME (type))
4391 TYPE_NAME (type) = decl;
4392 debug_hooks->type_decl (decl, 0);
4393 }
4394 }
4395
4396 /* Push a type into the namespace so that the back ends ignore it. */
4397
4398 static void
record_unknown_type(tree type,const char * name)4399 record_unknown_type (tree type, const char* name)
4400 {
4401 tree decl = pushdecl (build_decl (UNKNOWN_LOCATION,
4402 TYPE_DECL, get_identifier (name), type));
4403 /* Make sure the "unknown type" typedecl gets ignored for debug info. */
4404 DECL_IGNORED_P (decl) = 1;
4405 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
4406 TYPE_SIZE (type) = TYPE_SIZE (void_type_node);
4407 SET_TYPE_ALIGN (type, 1);
4408 TYPE_USER_ALIGN (type) = 0;
4409 SET_TYPE_MODE (type, TYPE_MODE (void_type_node));
4410 }
4411
4412 /* Create all the predefined identifiers. */
4413
4414 static void
initialize_predefined_identifiers(void)4415 initialize_predefined_identifiers (void)
4416 {
4417 struct predefined_identifier
4418 {
4419 const char *name; /* Name. */
4420 tree *node; /* Node to store it in. */
4421 cp_identifier_kind kind; /* Kind of identifier. */
4422 };
4423
4424 /* A table of identifiers to create at startup. */
4425 static const predefined_identifier predefined_identifiers[] = {
4426 {"C++", &lang_name_cplusplus, cik_normal},
4427 {"C", &lang_name_c, cik_normal},
4428 /* Some of these names have a trailing space so that it is
4429 impossible for them to conflict with names written by users. */
4430 {"__ct ", &ctor_identifier, cik_ctor},
4431 {"__ct_base ", &base_ctor_identifier, cik_ctor},
4432 {"__ct_comp ", &complete_ctor_identifier, cik_ctor},
4433 {"__dt ", &dtor_identifier, cik_dtor},
4434 {"__dt_base ", &base_dtor_identifier, cik_dtor},
4435 {"__dt_comp ", &complete_dtor_identifier, cik_dtor},
4436 {"__dt_del ", &deleting_dtor_identifier, cik_dtor},
4437 {"__conv_op ", &conv_op_identifier, cik_conv_op},
4438 {"__in_chrg", &in_charge_identifier, cik_normal},
4439 {"__as_base ", &as_base_identifier, cik_normal},
4440 {"this", &this_identifier, cik_normal},
4441 {"__delta", &delta_identifier, cik_normal},
4442 {"__pfn", &pfn_identifier, cik_normal},
4443 {"_vptr", &vptr_identifier, cik_normal},
4444 {"__vtt_parm", &vtt_parm_identifier, cik_normal},
4445 {"::", &global_identifier, cik_normal},
4446 /* The demangler expects anonymous namespaces to be called
4447 something starting with '_GLOBAL__N_'. It no longer needs
4448 to be unique to the TU. */
4449 {"_GLOBAL__N_1", &anon_identifier, cik_normal},
4450 {"auto", &auto_identifier, cik_normal},
4451 {"decltype(auto)", &decltype_auto_identifier, cik_normal},
4452 {"initializer_list", &init_list_identifier, cik_normal},
4453 {"__for_range ", &for_range__identifier, cik_normal},
4454 {"__for_begin ", &for_begin__identifier, cik_normal},
4455 {"__for_end ", &for_end__identifier, cik_normal},
4456 {"__for_range", &for_range_identifier, cik_normal},
4457 {"__for_begin", &for_begin_identifier, cik_normal},
4458 {"__for_end", &for_end_identifier, cik_normal},
4459 {"abi_tag", &abi_tag_identifier, cik_normal},
4460 {"aligned", &aligned_identifier, cik_normal},
4461 {"begin", &begin_identifier, cik_normal},
4462 {"end", &end_identifier, cik_normal},
4463 {"get", &get__identifier, cik_normal},
4464 {"gnu", &gnu_identifier, cik_normal},
4465 {"tuple_element", &tuple_element_identifier, cik_normal},
4466 {"tuple_size", &tuple_size_identifier, cik_normal},
4467 {"type", &type_identifier, cik_normal},
4468 {"value", &value_identifier, cik_normal},
4469 {"_FUN", &fun_identifier, cik_normal},
4470 {"__closure", &closure_identifier, cik_normal},
4471 {"heap uninit", &heap_uninit_identifier, cik_normal},
4472 {"heap ", &heap_identifier, cik_normal},
4473 {"heap deleted", &heap_deleted_identifier, cik_normal},
4474 {"heap [] uninit", &heap_vec_uninit_identifier, cik_normal},
4475 {"heap []", &heap_vec_identifier, cik_normal},
4476 {"omp", &omp_identifier, cik_normal},
4477 {NULL, NULL, cik_normal}
4478 };
4479
4480 for (const predefined_identifier *pid = predefined_identifiers;
4481 pid->name; ++pid)
4482 {
4483 *pid->node = get_identifier (pid->name);
4484 /* Some of these identifiers already have a special kind. */
4485 if (pid->kind != cik_normal)
4486 set_identifier_kind (*pid->node, pid->kind);
4487 }
4488 }
4489
4490 /* Create the predefined scalar types of C,
4491 and some nodes representing standard constants (0, 1, (void *)0).
4492 Initialize the global binding level.
4493 Make definitions for built-in primitive functions. */
4494
4495 void
cxx_init_decl_processing(void)4496 cxx_init_decl_processing (void)
4497 {
4498 tree void_ftype;
4499 tree void_ftype_ptr;
4500
4501 /* Create all the identifiers we need. */
4502 initialize_predefined_identifiers ();
4503
4504 /* Create the global variables. */
4505 push_to_top_level ();
4506
4507 current_function_decl = NULL_TREE;
4508 current_binding_level = NULL;
4509 /* Enter the global namespace. */
4510 gcc_assert (global_namespace == NULL_TREE);
4511 global_namespace = build_lang_decl (NAMESPACE_DECL, global_identifier,
4512 void_type_node);
4513 TREE_PUBLIC (global_namespace) = true;
4514 DECL_MODULE_EXPORT_P (global_namespace) = true;
4515 DECL_CONTEXT (global_namespace)
4516 = build_translation_unit_decl (get_identifier (main_input_filename));
4517 /* Remember whether we want the empty class passing ABI change warning
4518 in this TU. */
4519 TRANSLATION_UNIT_WARN_EMPTY_P (DECL_CONTEXT (global_namespace))
4520 = warn_abi && abi_version_crosses (12);
4521 debug_hooks->register_main_translation_unit
4522 (DECL_CONTEXT (global_namespace));
4523 begin_scope (sk_namespace, global_namespace);
4524 current_namespace = global_namespace;
4525
4526 if (flag_visibility_ms_compat)
4527 default_visibility = VISIBILITY_HIDDEN;
4528
4529 /* Initially, C. */
4530 current_lang_name = lang_name_c;
4531
4532 /* Create the `std' namespace. */
4533 push_namespace (get_identifier ("std"));
4534 std_node = current_namespace;
4535 pop_namespace ();
4536
4537 flag_noexcept_type = (cxx_dialect >= cxx17);
4538
4539 c_common_nodes_and_builtins ();
4540
4541 tree bool_ftype = build_function_type_list (boolean_type_node, NULL_TREE);
4542 tree decl
4543 = add_builtin_function ("__builtin_is_constant_evaluated",
4544 bool_ftype, CP_BUILT_IN_IS_CONSTANT_EVALUATED,
4545 BUILT_IN_FRONTEND, NULL, NULL_TREE);
4546 set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
4547
4548 tree cptr_ftype = build_function_type_list (const_ptr_type_node, NULL_TREE);
4549 decl = add_builtin_function ("__builtin_source_location",
4550 cptr_ftype, CP_BUILT_IN_SOURCE_LOCATION,
4551 BUILT_IN_FRONTEND, NULL, NULL_TREE);
4552 set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
4553
4554 tree bool_vaftype = build_varargs_function_type_list (boolean_type_node,
4555 NULL_TREE);
4556 decl
4557 = add_builtin_function ("__builtin_is_corresponding_member",
4558 bool_vaftype,
4559 CP_BUILT_IN_IS_CORRESPONDING_MEMBER,
4560 BUILT_IN_FRONTEND, NULL, NULL_TREE);
4561 set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
4562
4563 decl
4564 = add_builtin_function ("__builtin_is_pointer_interconvertible_with_class",
4565 bool_vaftype,
4566 CP_BUILT_IN_IS_POINTER_INTERCONVERTIBLE_WITH_CLASS,
4567 BUILT_IN_FRONTEND, NULL, NULL_TREE);
4568 set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
4569
4570 integer_two_node = build_int_cst (NULL_TREE, 2);
4571
4572 /* Guess at the initial static decls size. */
4573 vec_alloc (static_decls, 500);
4574
4575 /* ... and keyed classes. */
4576 vec_alloc (keyed_classes, 100);
4577
4578 record_builtin_type (RID_BOOL, "bool", boolean_type_node);
4579 truthvalue_type_node = boolean_type_node;
4580 truthvalue_false_node = boolean_false_node;
4581 truthvalue_true_node = boolean_true_node;
4582
4583 empty_except_spec = build_tree_list (NULL_TREE, NULL_TREE);
4584 noexcept_true_spec = build_tree_list (boolean_true_node, NULL_TREE);
4585 noexcept_false_spec = build_tree_list (boolean_false_node, NULL_TREE);
4586 noexcept_deferred_spec = build_tree_list (make_node (DEFERRED_NOEXCEPT),
4587 NULL_TREE);
4588
4589 #if 0
4590 record_builtin_type (RID_MAX, NULL, string_type_node);
4591 #endif
4592
4593 delta_type_node = ptrdiff_type_node;
4594 vtable_index_type = ptrdiff_type_node;
4595
4596 vtt_parm_type = build_pointer_type (const_ptr_type_node);
4597 void_ftype = build_function_type_list (void_type_node, NULL_TREE);
4598 void_ftype_ptr = build_function_type_list (void_type_node,
4599 ptr_type_node, NULL_TREE);
4600 void_ftype_ptr
4601 = build_exception_variant (void_ftype_ptr, empty_except_spec);
4602
4603 /* Create the conversion operator marker. This operator's DECL_NAME
4604 is in the identifier table, so we can use identifier equality to
4605 find it. */
4606 conv_op_marker = build_lang_decl (FUNCTION_DECL, conv_op_identifier,
4607 void_ftype);
4608
4609 /* C++ extensions */
4610
4611 unknown_type_node = make_node (LANG_TYPE);
4612 record_unknown_type (unknown_type_node, "unknown type");
4613
4614 /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node. */
4615 TREE_TYPE (unknown_type_node) = unknown_type_node;
4616
4617 /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same
4618 result. */
4619 TYPE_POINTER_TO (unknown_type_node) = unknown_type_node;
4620 TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node;
4621
4622 init_list_type_node = make_node (LANG_TYPE);
4623 record_unknown_type (init_list_type_node, "init list");
4624
4625 /* Used when parsing to distinguish parameter-lists () and (void). */
4626 explicit_void_list_node = build_void_list_node ();
4627
4628 {
4629 /* Make sure we get a unique function type, so we can give
4630 its pointer type a name. (This wins for gdb.) */
4631 tree vfunc_type = make_node (FUNCTION_TYPE);
4632 TREE_TYPE (vfunc_type) = integer_type_node;
4633 TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
4634 layout_type (vfunc_type);
4635
4636 vtable_entry_type = build_pointer_type (vfunc_type);
4637 }
4638 record_builtin_type (RID_MAX, "__vtbl_ptr_type", vtable_entry_type);
4639
4640 vtbl_type_node
4641 = build_cplus_array_type (vtable_entry_type, NULL_TREE);
4642 layout_type (vtbl_type_node);
4643 vtbl_type_node = cp_build_qualified_type (vtbl_type_node, TYPE_QUAL_CONST);
4644 record_builtin_type (RID_MAX, NULL, vtbl_type_node);
4645 vtbl_ptr_type_node = build_pointer_type (vtable_entry_type);
4646 layout_type (vtbl_ptr_type_node);
4647 record_builtin_type (RID_MAX, NULL, vtbl_ptr_type_node);
4648
4649 push_namespace (get_identifier ("__cxxabiv1"));
4650 abi_node = current_namespace;
4651 pop_namespace ();
4652
4653 any_targ_node = make_node (LANG_TYPE);
4654 record_unknown_type (any_targ_node, "any type");
4655
4656 /* Now, C++. */
4657 current_lang_name = lang_name_cplusplus;
4658
4659 if (aligned_new_threshold > 1
4660 && !pow2p_hwi (aligned_new_threshold))
4661 {
4662 error ("%<-faligned-new=%d%> is not a power of two",
4663 aligned_new_threshold);
4664 aligned_new_threshold = 1;
4665 }
4666 if (aligned_new_threshold == -1)
4667 aligned_new_threshold = (cxx_dialect >= cxx17) ? 1 : 0;
4668 if (aligned_new_threshold == 1)
4669 aligned_new_threshold = malloc_alignment () / BITS_PER_UNIT;
4670
4671 {
4672 tree newattrs, extvisattr;
4673 tree newtype, deltype;
4674 tree ptr_ftype_sizetype;
4675 tree new_eh_spec;
4676
4677 ptr_ftype_sizetype
4678 = build_function_type_list (ptr_type_node, size_type_node, NULL_TREE);
4679 if (cxx_dialect == cxx98)
4680 {
4681 tree bad_alloc_id;
4682 tree bad_alloc_type_node;
4683 tree bad_alloc_decl;
4684
4685 push_nested_namespace (std_node);
4686 bad_alloc_id = get_identifier ("bad_alloc");
4687 bad_alloc_type_node = make_class_type (RECORD_TYPE);
4688 TYPE_CONTEXT (bad_alloc_type_node) = current_namespace;
4689 bad_alloc_decl
4690 = create_implicit_typedef (bad_alloc_id, bad_alloc_type_node);
4691 DECL_CONTEXT (bad_alloc_decl) = current_namespace;
4692 pop_nested_namespace (std_node);
4693
4694 new_eh_spec
4695 = add_exception_specifier (NULL_TREE, bad_alloc_type_node, -1);
4696 }
4697 else
4698 new_eh_spec = noexcept_false_spec;
4699
4700 /* Ensure attribs.cc is initialized. */
4701 init_attributes ();
4702
4703 extvisattr = build_tree_list (get_identifier ("externally_visible"),
4704 NULL_TREE);
4705 newattrs = tree_cons (get_identifier ("alloc_size"),
4706 build_tree_list (NULL_TREE, integer_one_node),
4707 extvisattr);
4708 newtype = cp_build_type_attribute_variant (ptr_ftype_sizetype, newattrs);
4709 newtype = build_exception_variant (newtype, new_eh_spec);
4710 deltype = cp_build_type_attribute_variant (void_ftype_ptr, extvisattr);
4711 deltype = build_exception_variant (deltype, empty_except_spec);
4712 tree opnew = push_cp_library_fn (NEW_EXPR, newtype, 0);
4713 DECL_IS_MALLOC (opnew) = 1;
4714 DECL_SET_IS_OPERATOR_NEW (opnew, true);
4715 DECL_IS_REPLACEABLE_OPERATOR (opnew) = 1;
4716 opnew = push_cp_library_fn (VEC_NEW_EXPR, newtype, 0);
4717 DECL_IS_MALLOC (opnew) = 1;
4718 DECL_SET_IS_OPERATOR_NEW (opnew, true);
4719 DECL_IS_REPLACEABLE_OPERATOR (opnew) = 1;
4720 tree opdel = push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
4721 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4722 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
4723 opdel = push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
4724 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4725 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
4726 if (flag_sized_deallocation)
4727 {
4728 /* Also push the sized deallocation variants:
4729 void operator delete(void*, std::size_t) throw();
4730 void operator delete[](void*, std::size_t) throw(); */
4731 tree void_ftype_ptr_size
4732 = build_function_type_list (void_type_node, ptr_type_node,
4733 size_type_node, NULL_TREE);
4734 deltype = cp_build_type_attribute_variant (void_ftype_ptr_size,
4735 extvisattr);
4736 deltype = build_exception_variant (deltype, empty_except_spec);
4737 opdel = push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
4738 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4739 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
4740 opdel = push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
4741 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4742 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
4743 }
4744
4745 if (aligned_new_threshold)
4746 {
4747 push_nested_namespace (std_node);
4748 tree align_id = get_identifier ("align_val_t");
4749 align_type_node = start_enum (align_id, NULL_TREE, size_type_node,
4750 NULL_TREE, /*scoped*/true, NULL);
4751 pop_nested_namespace (std_node);
4752
4753 /* operator new (size_t, align_val_t); */
4754 newtype = build_function_type_list (ptr_type_node, size_type_node,
4755 align_type_node, NULL_TREE);
4756 newtype = cp_build_type_attribute_variant (newtype, newattrs);
4757 newtype = build_exception_variant (newtype, new_eh_spec);
4758 opnew = push_cp_library_fn (NEW_EXPR, newtype, 0);
4759 DECL_IS_MALLOC (opnew) = 1;
4760 DECL_SET_IS_OPERATOR_NEW (opnew, true);
4761 DECL_IS_REPLACEABLE_OPERATOR (opnew) = 1;
4762 opnew = push_cp_library_fn (VEC_NEW_EXPR, newtype, 0);
4763 DECL_IS_MALLOC (opnew) = 1;
4764 DECL_SET_IS_OPERATOR_NEW (opnew, true);
4765 DECL_IS_REPLACEABLE_OPERATOR (opnew) = 1;
4766
4767 /* operator delete (void *, align_val_t); */
4768 deltype = build_function_type_list (void_type_node, ptr_type_node,
4769 align_type_node, NULL_TREE);
4770 deltype = cp_build_type_attribute_variant (deltype, extvisattr);
4771 deltype = build_exception_variant (deltype, empty_except_spec);
4772 opdel = push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
4773 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4774 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
4775 opdel = push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
4776 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4777 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
4778
4779 if (flag_sized_deallocation)
4780 {
4781 /* operator delete (void *, size_t, align_val_t); */
4782 deltype = build_function_type_list (void_type_node, ptr_type_node,
4783 size_type_node, align_type_node,
4784 NULL_TREE);
4785 deltype = cp_build_type_attribute_variant (deltype, extvisattr);
4786 deltype = build_exception_variant (deltype, empty_except_spec);
4787 opdel = push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
4788 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4789 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
4790 opdel = push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
4791 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4792 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
4793 }
4794 }
4795
4796 nullptr_type_node = make_node (NULLPTR_TYPE);
4797 TYPE_SIZE (nullptr_type_node) = bitsize_int (GET_MODE_BITSIZE (ptr_mode));
4798 TYPE_SIZE_UNIT (nullptr_type_node) = size_int (GET_MODE_SIZE (ptr_mode));
4799 TYPE_UNSIGNED (nullptr_type_node) = 1;
4800 TYPE_PRECISION (nullptr_type_node) = GET_MODE_BITSIZE (ptr_mode);
4801 if (abi_version_at_least (9))
4802 SET_TYPE_ALIGN (nullptr_type_node, GET_MODE_ALIGNMENT (ptr_mode));
4803 SET_TYPE_MODE (nullptr_type_node, ptr_mode);
4804 record_builtin_type (RID_MAX, "decltype(nullptr)", nullptr_type_node);
4805 nullptr_node = build_int_cst (nullptr_type_node, 0);
4806 }
4807
4808 if (! supports_one_only ())
4809 flag_weak = 0;
4810
4811 abort_fndecl
4812 = build_library_fn_ptr ("__cxa_pure_virtual", void_ftype,
4813 ECF_NORETURN | ECF_NOTHROW | ECF_COLD);
4814 if (flag_weak)
4815 /* If no definition is available, resolve references to NULL. */
4816 declare_weak (abort_fndecl);
4817
4818 /* Perform other language dependent initializations. */
4819 init_class_processing ();
4820 init_rtti_processing ();
4821 init_template_processing ();
4822
4823 if (flag_exceptions)
4824 init_exception_processing ();
4825
4826 if (modules_p ())
4827 init_modules (parse_in);
4828
4829 make_fname_decl = cp_make_fname_decl;
4830 start_fname_decls ();
4831
4832 /* Show we use EH for cleanups. */
4833 if (flag_exceptions)
4834 using_eh_for_cleanups ();
4835
4836 /* Check that the hardware interference sizes are at least
4837 alignof(max_align_t), as required by the standard. */
4838 const int max_align = max_align_t_align () / BITS_PER_UNIT;
4839 if (OPTION_SET_P (param_destruct_interfere_size))
4840 {
4841 if (param_destruct_interfere_size < max_align)
4842 error ("%<--param destructive-interference-size=%d%> is less than "
4843 "%d", param_destruct_interfere_size, max_align);
4844 else if (param_destruct_interfere_size < param_l1_cache_line_size)
4845 warning (OPT_Winterference_size,
4846 "%<--param destructive-interference-size=%d%> "
4847 "is less than %<--param l1-cache-line-size=%d%>",
4848 param_destruct_interfere_size, param_l1_cache_line_size);
4849 }
4850 else if (param_destruct_interfere_size)
4851 /* Assume the internal value is OK. */;
4852 else if (param_l1_cache_line_size >= max_align)
4853 param_destruct_interfere_size = param_l1_cache_line_size;
4854 /* else leave it unset. */
4855
4856 if (OPTION_SET_P (param_construct_interfere_size))
4857 {
4858 if (param_construct_interfere_size < max_align)
4859 error ("%<--param constructive-interference-size=%d%> is less than "
4860 "%d", param_construct_interfere_size, max_align);
4861 else if (param_construct_interfere_size > param_l1_cache_line_size
4862 && param_l1_cache_line_size >= max_align)
4863 warning (OPT_Winterference_size,
4864 "%<--param constructive-interference-size=%d%> "
4865 "is greater than %<--param l1-cache-line-size=%d%>",
4866 param_construct_interfere_size, param_l1_cache_line_size);
4867 }
4868 else if (param_construct_interfere_size)
4869 /* Assume the internal value is OK. */;
4870 else if (param_l1_cache_line_size >= max_align)
4871 param_construct_interfere_size = param_l1_cache_line_size;
4872 }
4873
4874 /* Enter an abi node in global-module context. returns a cookie to
4875 give to pop_abi_namespace. */
4876
4877 unsigned
push_abi_namespace(tree node)4878 push_abi_namespace (tree node)
4879 {
4880 push_nested_namespace (node);
4881 push_visibility ("default", 2);
4882 unsigned flags = module_kind;
4883 module_kind = 0;
4884 return flags;
4885 }
4886
4887 /* Pop an abi namespace, FLAGS is the cookie push_abi_namespace gave
4888 you. */
4889
4890 void
pop_abi_namespace(unsigned flags,tree node)4891 pop_abi_namespace (unsigned flags, tree node)
4892 {
4893 module_kind = flags;
4894 pop_visibility (2);
4895 pop_nested_namespace (node);
4896 }
4897
4898 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give
4899 the decl, LOC is the location to give the decl, NAME is the
4900 initialization string and TYPE_DEP indicates whether NAME depended
4901 on the type of the function. We make use of that to detect
4902 __PRETTY_FUNCTION__ inside a template fn. This is being done lazily
4903 at the point of first use, so we mustn't push the decl now. */
4904
4905 static tree
cp_make_fname_decl(location_t loc,tree id,int type_dep)4906 cp_make_fname_decl (location_t loc, tree id, int type_dep)
4907 {
4908 tree domain = NULL_TREE;
4909 tree init = NULL_TREE;
4910
4911 if (!(type_dep && in_template_function ()))
4912 {
4913 const char *name = NULL;
4914 bool release_name = false;
4915
4916 if (current_function_decl == NULL_TREE)
4917 name = "top level";
4918 else if (type_dep == 0)
4919 {
4920 /* __FUNCTION__ */
4921 name = fname_as_string (type_dep);
4922 release_name = true;
4923 }
4924 else
4925 {
4926 /* __PRETTY_FUNCTION__ */
4927 gcc_checking_assert (type_dep == 1);
4928 name = cxx_printable_name (current_function_decl, 2);
4929 }
4930
4931 size_t length = strlen (name);
4932 domain = build_index_type (size_int (length));
4933 init = build_string (length + 1, name);
4934 if (release_name)
4935 free (const_cast<char *> (name));
4936 }
4937
4938 tree type = cp_build_qualified_type (char_type_node, TYPE_QUAL_CONST);
4939 type = build_cplus_array_type (type, domain);
4940
4941 if (init)
4942 TREE_TYPE (init) = type;
4943 else
4944 init = error_mark_node;
4945
4946 tree decl = build_decl (loc, VAR_DECL, id, type);
4947
4948 TREE_READONLY (decl) = 1;
4949 DECL_ARTIFICIAL (decl) = 1;
4950 DECL_DECLARED_CONSTEXPR_P (decl) = 1;
4951 TREE_STATIC (decl) = 1;
4952
4953 TREE_USED (decl) = 1;
4954
4955 SET_DECL_VALUE_EXPR (decl, init);
4956 DECL_HAS_VALUE_EXPR_P (decl) = 1;
4957 /* For decl_constant_var_p. */
4958 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
4959
4960 if (current_function_decl)
4961 {
4962 DECL_CONTEXT (decl) = current_function_decl;
4963 decl = pushdecl_outermost_localscope (decl);
4964 if (decl != error_mark_node)
4965 add_decl_expr (decl);
4966 }
4967 else
4968 {
4969 DECL_THIS_STATIC (decl) = true;
4970 decl = pushdecl_top_level_and_finish (decl, NULL_TREE);
4971 }
4972
4973 return decl;
4974 }
4975
4976 /* Install DECL as a builtin function at current global scope. Return
4977 the new decl (if we found an existing version). Also installs it
4978 into ::std, if it's not '_*'. */
4979
4980 tree
cxx_builtin_function(tree decl)4981 cxx_builtin_function (tree decl)
4982 {
4983 retrofit_lang_decl (decl);
4984
4985 DECL_ARTIFICIAL (decl) = 1;
4986 SET_DECL_LANGUAGE (decl, lang_c);
4987 /* Runtime library routines are, by definition, available in an
4988 external shared object. */
4989 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
4990 DECL_VISIBILITY_SPECIFIED (decl) = 1;
4991
4992 tree id = DECL_NAME (decl);
4993 const char *name = IDENTIFIER_POINTER (id);
4994 bool hiding = false;
4995 if (name[0] != '_' || name[1] != '_')
4996 /* In the user's namespace, it must be declared before use. */
4997 hiding = true;
4998 else if (IDENTIFIER_LENGTH (id) > strlen ("___chk")
4999 && !startswith (name + 2, "builtin_")
5000 && 0 == memcmp (name + IDENTIFIER_LENGTH (id) - strlen ("_chk"),
5001 "_chk", strlen ("_chk") + 1))
5002 /* Treat __*_chk fortification functions as anticipated as well,
5003 unless they are __builtin_*_chk. */
5004 hiding = true;
5005
5006 /* All builtins that don't begin with an '_' should additionally
5007 go in the 'std' namespace. */
5008 if (name[0] != '_')
5009 {
5010 tree std_decl = copy_decl (decl);
5011
5012 push_nested_namespace (std_node);
5013 DECL_CONTEXT (std_decl) = FROB_CONTEXT (std_node);
5014 pushdecl (std_decl, hiding);
5015 pop_nested_namespace (std_node);
5016 }
5017
5018 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5019 decl = pushdecl (decl, hiding);
5020
5021 return decl;
5022 }
5023
5024 /* Like cxx_builtin_function, but guarantee the function is added to the global
5025 scope. This is to allow function specific options to add new machine
5026 dependent builtins when the target ISA changes via attribute((target(...)))
5027 which saves space on program startup if the program does not use non-generic
5028 ISAs. */
5029
5030 tree
cxx_builtin_function_ext_scope(tree decl)5031 cxx_builtin_function_ext_scope (tree decl)
5032 {
5033 push_nested_namespace (global_namespace);
5034 decl = cxx_builtin_function (decl);
5035 pop_nested_namespace (global_namespace);
5036
5037 return decl;
5038 }
5039
5040 /* Implement LANG_HOOKS_SIMULATE_BUILTIN_FUNCTION_DECL. */
5041
5042 tree
cxx_simulate_builtin_function_decl(tree decl)5043 cxx_simulate_builtin_function_decl (tree decl)
5044 {
5045 retrofit_lang_decl (decl);
5046
5047 DECL_ARTIFICIAL (decl) = 1;
5048 SET_DECL_LANGUAGE (decl, lang_cplusplus);
5049 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5050 return pushdecl (decl);
5051 }
5052
5053 /* Generate a FUNCTION_DECL with the typical flags for a runtime library
5054 function. Not called directly. */
5055
5056 static tree
build_library_fn(tree name,enum tree_code operator_code,tree type,int ecf_flags)5057 build_library_fn (tree name, enum tree_code operator_code, tree type,
5058 int ecf_flags)
5059 {
5060 tree fn = build_lang_decl (FUNCTION_DECL, name, type);
5061 DECL_EXTERNAL (fn) = 1;
5062 TREE_PUBLIC (fn) = 1;
5063 DECL_ARTIFICIAL (fn) = 1;
5064 DECL_OVERLOADED_OPERATOR_CODE_RAW (fn)
5065 = OVL_OP_INFO (false, operator_code)->ovl_op_code;
5066 SET_DECL_LANGUAGE (fn, lang_c);
5067 /* Runtime library routines are, by definition, available in an
5068 external shared object. */
5069 DECL_VISIBILITY (fn) = VISIBILITY_DEFAULT;
5070 DECL_VISIBILITY_SPECIFIED (fn) = 1;
5071 set_call_expr_flags (fn, ecf_flags);
5072 return fn;
5073 }
5074
5075 /* Returns the _DECL for a library function with C++ linkage. */
5076
5077 static tree
build_cp_library_fn(tree name,enum tree_code operator_code,tree type,int ecf_flags)5078 build_cp_library_fn (tree name, enum tree_code operator_code, tree type,
5079 int ecf_flags)
5080 {
5081 tree fn = build_library_fn (name, operator_code, type, ecf_flags);
5082 DECL_CONTEXT (fn) = FROB_CONTEXT (current_namespace);
5083 SET_DECL_LANGUAGE (fn, lang_cplusplus);
5084 return fn;
5085 }
5086
5087 /* Like build_library_fn, but takes a C string instead of an
5088 IDENTIFIER_NODE. */
5089
5090 tree
build_library_fn_ptr(const char * name,tree type,int ecf_flags)5091 build_library_fn_ptr (const char* name, tree type, int ecf_flags)
5092 {
5093 return build_library_fn (get_identifier (name), ERROR_MARK, type, ecf_flags);
5094 }
5095
5096 /* Like build_cp_library_fn, but takes a C string instead of an
5097 IDENTIFIER_NODE. */
5098
5099 tree
build_cp_library_fn_ptr(const char * name,tree type,int ecf_flags)5100 build_cp_library_fn_ptr (const char* name, tree type, int ecf_flags)
5101 {
5102 return build_cp_library_fn (get_identifier (name), ERROR_MARK, type,
5103 ecf_flags);
5104 }
5105
5106 /* Like build_library_fn, but also pushes the function so that we will
5107 be able to find it via get_global_binding. Also, the function
5108 may throw exceptions listed in RAISES. */
5109
5110 tree
push_library_fn(tree name,tree type,tree raises,int ecf_flags)5111 push_library_fn (tree name, tree type, tree raises, int ecf_flags)
5112 {
5113 if (raises)
5114 type = build_exception_variant (type, raises);
5115
5116 tree fn = build_library_fn (name, ERROR_MARK, type, ecf_flags);
5117 return pushdecl_top_level (fn);
5118 }
5119
5120 /* Like build_cp_library_fn, but also pushes the function so that it
5121 will be found by normal lookup. */
5122
5123 static tree
push_cp_library_fn(enum tree_code operator_code,tree type,int ecf_flags)5124 push_cp_library_fn (enum tree_code operator_code, tree type,
5125 int ecf_flags)
5126 {
5127 tree fn = build_cp_library_fn (ovl_op_identifier (false, operator_code),
5128 operator_code, type, ecf_flags);
5129 pushdecl (fn);
5130 if (flag_tm)
5131 apply_tm_attr (fn, get_identifier ("transaction_safe"));
5132 return fn;
5133 }
5134
5135 /* Like push_library_fn, but also note that this function throws
5136 and does not return. Used for __throw_foo and the like. */
5137
5138 tree
push_throw_library_fn(tree name,tree type)5139 push_throw_library_fn (tree name, tree type)
5140 {
5141 tree fn = push_library_fn (name, type, NULL_TREE, ECF_NORETURN | ECF_COLD);
5142 return fn;
5143 }
5144
5145 /* When we call finish_struct for an anonymous union, we create
5146 default copy constructors and such. But, an anonymous union
5147 shouldn't have such things; this function undoes the damage to the
5148 anonymous union type T.
5149
5150 (The reason that we create the synthesized methods is that we don't
5151 distinguish `union { int i; }' from `typedef union { int i; } U'.
5152 The first is an anonymous union; the second is just an ordinary
5153 union type.) */
5154
5155 void
fixup_anonymous_aggr(tree t)5156 fixup_anonymous_aggr (tree t)
5157 {
5158 /* Wipe out memory of synthesized methods. */
5159 TYPE_HAS_USER_CONSTRUCTOR (t) = 0;
5160 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0;
5161 TYPE_HAS_COPY_CTOR (t) = 0;
5162 TYPE_HAS_CONST_COPY_CTOR (t) = 0;
5163 TYPE_HAS_COPY_ASSIGN (t) = 0;
5164 TYPE_HAS_CONST_COPY_ASSIGN (t) = 0;
5165
5166 /* Splice the implicitly generated functions out of TYPE_FIELDS and diagnose
5167 invalid members. */
5168 for (tree probe, *prev_p = &TYPE_FIELDS (t); (probe = *prev_p);)
5169 {
5170 if (TREE_CODE (probe) == FUNCTION_DECL && DECL_ARTIFICIAL (probe))
5171 *prev_p = DECL_CHAIN (probe);
5172 else
5173 prev_p = &DECL_CHAIN (probe);
5174
5175 if (DECL_ARTIFICIAL (probe)
5176 && (!DECL_IMPLICIT_TYPEDEF_P (probe)
5177 || TYPE_ANON_P (TREE_TYPE (probe))))
5178 continue;
5179
5180 if (TREE_CODE (probe) != FIELD_DECL
5181 || (TREE_PRIVATE (probe) || TREE_PROTECTED (probe)))
5182 {
5183 /* We already complained about static data members in
5184 finish_static_data_member_decl. */
5185 if (!VAR_P (probe))
5186 {
5187 auto_diagnostic_group d;
5188 if (permerror (DECL_SOURCE_LOCATION (probe),
5189 TREE_CODE (t) == UNION_TYPE
5190 ? "%q#D invalid; an anonymous union may "
5191 "only have public non-static data members"
5192 : "%q#D invalid; an anonymous struct may "
5193 "only have public non-static data members", probe))
5194 {
5195 static bool hint;
5196 if (flag_permissive && !hint)
5197 {
5198 hint = true;
5199 inform (DECL_SOURCE_LOCATION (probe),
5200 "this flexibility is deprecated and will be "
5201 "removed");
5202 }
5203 }
5204 }
5205 }
5206 }
5207
5208 /* Splice all functions out of CLASSTYPE_MEMBER_VEC. */
5209 vec<tree,va_gc>* vec = CLASSTYPE_MEMBER_VEC (t);
5210 unsigned store = 0;
5211 for (tree elt : vec)
5212 if (!is_overloaded_fn (elt))
5213 (*vec)[store++] = elt;
5214 vec_safe_truncate (vec, store);
5215
5216 /* Wipe RTTI info. */
5217 CLASSTYPE_TYPEINFO_VAR (t) = NULL_TREE;
5218
5219 /* Anonymous aggregates cannot have fields with ctors, dtors or complex
5220 assignment operators (because they cannot have these methods themselves).
5221 For anonymous unions this is already checked because they are not allowed
5222 in any union, otherwise we have to check it. */
5223 if (TREE_CODE (t) != UNION_TYPE)
5224 {
5225 tree field, type;
5226
5227 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)))
5228 {
5229 error_at (location_of (t), "anonymous struct with base classes");
5230 /* Avoid ICE after error on anon-struct9.C. */
5231 TYPE_NEEDS_CONSTRUCTING (t) = false;
5232 }
5233
5234 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
5235 if (TREE_CODE (field) == FIELD_DECL)
5236 {
5237 type = TREE_TYPE (field);
5238 if (CLASS_TYPE_P (type))
5239 {
5240 if (TYPE_NEEDS_CONSTRUCTING (type))
5241 error ("member %q+#D with constructor not allowed "
5242 "in anonymous aggregate", field);
5243 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
5244 error ("member %q+#D with destructor not allowed "
5245 "in anonymous aggregate", field);
5246 if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type))
5247 error ("member %q+#D with copy assignment operator "
5248 "not allowed in anonymous aggregate", field);
5249 }
5250 }
5251 }
5252 }
5253
5254 /* Warn for an attribute located at LOCATION that appertains to the
5255 class type CLASS_TYPE that has not been properly placed after its
5256 class-key, in it class-specifier. */
5257
5258 void
warn_misplaced_attr_for_class_type(location_t location,tree class_type)5259 warn_misplaced_attr_for_class_type (location_t location,
5260 tree class_type)
5261 {
5262 gcc_assert (OVERLOAD_TYPE_P (class_type));
5263
5264 auto_diagnostic_group d;
5265 if (warning_at (location, OPT_Wattributes,
5266 "attribute ignored in declaration "
5267 "of %q#T", class_type))
5268 inform (location,
5269 "attribute for %q#T must follow the %qs keyword",
5270 class_type, class_key_or_enum_as_string (class_type));
5271 }
5272
5273 /* Returns the cv-qualifiers that apply to the type specified
5274 by the DECLSPECS. */
5275
5276 static int
get_type_quals(const cp_decl_specifier_seq * declspecs)5277 get_type_quals (const cp_decl_specifier_seq *declspecs)
5278 {
5279 int type_quals = TYPE_UNQUALIFIED;
5280
5281 if (decl_spec_seq_has_spec_p (declspecs, ds_const))
5282 type_quals |= TYPE_QUAL_CONST;
5283 if (decl_spec_seq_has_spec_p (declspecs, ds_volatile))
5284 type_quals |= TYPE_QUAL_VOLATILE;
5285 if (decl_spec_seq_has_spec_p (declspecs, ds_restrict))
5286 type_quals |= TYPE_QUAL_RESTRICT;
5287
5288 return type_quals;
5289 }
5290
5291 /* Make sure that a declaration with no declarator is well-formed, i.e.
5292 just declares a tagged type or anonymous union.
5293
5294 Returns the type declared; or NULL_TREE if none. */
5295
5296 tree
check_tag_decl(cp_decl_specifier_seq * declspecs,bool explicit_type_instantiation_p)5297 check_tag_decl (cp_decl_specifier_seq *declspecs,
5298 bool explicit_type_instantiation_p)
5299 {
5300 int saw_friend = decl_spec_seq_has_spec_p (declspecs, ds_friend);
5301 int saw_typedef = decl_spec_seq_has_spec_p (declspecs, ds_typedef);
5302 /* If a class, struct, or enum type is declared by the DECLSPECS
5303 (i.e, if a class-specifier, enum-specifier, or non-typename
5304 elaborated-type-specifier appears in the DECLSPECS),
5305 DECLARED_TYPE is set to the corresponding type. */
5306 tree declared_type = NULL_TREE;
5307 bool error_p = false;
5308
5309 if (declspecs->multiple_types_p)
5310 error_at (smallest_type_location (declspecs),
5311 "multiple types in one declaration");
5312 else if (declspecs->redefined_builtin_type)
5313 {
5314 location_t loc = declspecs->locations[ds_redefined_builtin_type_spec];
5315 if (!in_system_header_at (loc))
5316 permerror (loc, "redeclaration of C++ built-in type %qT",
5317 declspecs->redefined_builtin_type);
5318 return NULL_TREE;
5319 }
5320
5321 if (declspecs->type
5322 && TYPE_P (declspecs->type)
5323 && ((TREE_CODE (declspecs->type) != TYPENAME_TYPE
5324 && MAYBE_CLASS_TYPE_P (declspecs->type))
5325 || TREE_CODE (declspecs->type) == ENUMERAL_TYPE))
5326 declared_type = declspecs->type;
5327 else if (declspecs->type == error_mark_node)
5328 error_p = true;
5329
5330 if (type_uses_auto (declared_type))
5331 {
5332 error_at (declspecs->locations[ds_type_spec],
5333 "%<auto%> can only be specified for variables "
5334 "or function declarations");
5335 return error_mark_node;
5336 }
5337
5338 if (declared_type && !OVERLOAD_TYPE_P (declared_type))
5339 declared_type = NULL_TREE;
5340
5341 if (!declared_type && !saw_friend && !error_p)
5342 permerror (input_location, "declaration does not declare anything");
5343 /* Check for an anonymous union. */
5344 else if (declared_type && RECORD_OR_UNION_CODE_P (TREE_CODE (declared_type))
5345 && TYPE_UNNAMED_P (declared_type))
5346 {
5347 /* 7/3 In a simple-declaration, the optional init-declarator-list
5348 can be omitted only when declaring a class (clause 9) or
5349 enumeration (7.2), that is, when the decl-specifier-seq contains
5350 either a class-specifier, an elaborated-type-specifier with
5351 a class-key (9.1), or an enum-specifier. In these cases and
5352 whenever a class-specifier or enum-specifier is present in the
5353 decl-specifier-seq, the identifiers in these specifiers are among
5354 the names being declared by the declaration (as class-name,
5355 enum-names, or enumerators, depending on the syntax). In such
5356 cases, and except for the declaration of an unnamed bit-field (9.6),
5357 the decl-specifier-seq shall introduce one or more names into the
5358 program, or shall redeclare a name introduced by a previous
5359 declaration. [Example:
5360 enum { }; // ill-formed
5361 typedef class { }; // ill-formed
5362 --end example] */
5363 if (saw_typedef)
5364 {
5365 error_at (declspecs->locations[ds_typedef],
5366 "missing type-name in typedef-declaration");
5367 return NULL_TREE;
5368 }
5369 /* Anonymous unions are objects, so they can have specifiers. */;
5370 SET_ANON_AGGR_TYPE_P (declared_type);
5371
5372 if (TREE_CODE (declared_type) != UNION_TYPE)
5373 pedwarn (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (declared_type)),
5374 OPT_Wpedantic, "ISO C++ prohibits anonymous structs");
5375 }
5376
5377 else
5378 {
5379 if (decl_spec_seq_has_spec_p (declspecs, ds_inline))
5380 error_at (declspecs->locations[ds_inline],
5381 "%<inline%> can only be specified for functions");
5382 else if (decl_spec_seq_has_spec_p (declspecs, ds_virtual))
5383 error_at (declspecs->locations[ds_virtual],
5384 "%<virtual%> can only be specified for functions");
5385 else if (saw_friend
5386 && (!current_class_type
5387 || current_scope () != current_class_type))
5388 error_at (declspecs->locations[ds_friend],
5389 "%<friend%> can only be specified inside a class");
5390 else if (decl_spec_seq_has_spec_p (declspecs, ds_explicit))
5391 error_at (declspecs->locations[ds_explicit],
5392 "%<explicit%> can only be specified for constructors");
5393 else if (declspecs->storage_class)
5394 error_at (declspecs->locations[ds_storage_class],
5395 "a storage class can only be specified for objects "
5396 "and functions");
5397 else if (decl_spec_seq_has_spec_p (declspecs, ds_const))
5398 error_at (declspecs->locations[ds_const],
5399 "%<const%> can only be specified for objects and "
5400 "functions");
5401 else if (decl_spec_seq_has_spec_p (declspecs, ds_volatile))
5402 error_at (declspecs->locations[ds_volatile],
5403 "%<volatile%> can only be specified for objects and "
5404 "functions");
5405 else if (decl_spec_seq_has_spec_p (declspecs, ds_restrict))
5406 error_at (declspecs->locations[ds_restrict],
5407 "%<__restrict%> can only be specified for objects and "
5408 "functions");
5409 else if (decl_spec_seq_has_spec_p (declspecs, ds_thread))
5410 error_at (declspecs->locations[ds_thread],
5411 "%<__thread%> can only be specified for objects "
5412 "and functions");
5413 else if (saw_typedef)
5414 warning_at (declspecs->locations[ds_typedef], 0,
5415 "%<typedef%> was ignored in this declaration");
5416 else if (decl_spec_seq_has_spec_p (declspecs, ds_constexpr))
5417 error_at (declspecs->locations[ds_constexpr],
5418 "%qs cannot be used for type declarations", "constexpr");
5419 else if (decl_spec_seq_has_spec_p (declspecs, ds_constinit))
5420 error_at (declspecs->locations[ds_constinit],
5421 "%qs cannot be used for type declarations", "constinit");
5422 else if (decl_spec_seq_has_spec_p (declspecs, ds_consteval))
5423 error_at (declspecs->locations[ds_consteval],
5424 "%qs cannot be used for type declarations", "consteval");
5425 }
5426
5427 if (declspecs->attributes && warn_attributes && declared_type)
5428 {
5429 location_t loc;
5430 if (!CLASS_TYPE_P (declared_type)
5431 || !CLASSTYPE_TEMPLATE_INSTANTIATION (declared_type))
5432 /* For a non-template class, use the name location. */
5433 loc = location_of (declared_type);
5434 else
5435 /* For a template class (an explicit instantiation), use the
5436 current location. */
5437 loc = input_location;
5438
5439 if (explicit_type_instantiation_p)
5440 /* [dcl.attr.grammar]/4:
5441
5442 No attribute-specifier-seq shall appertain to an explicit
5443 instantiation. */
5444 {
5445 if (warning_at (loc, OPT_Wattributes,
5446 "attribute ignored in explicit instantiation %q#T",
5447 declared_type))
5448 inform (loc,
5449 "no attribute can be applied to "
5450 "an explicit instantiation");
5451 }
5452 else
5453 warn_misplaced_attr_for_class_type (loc, declared_type);
5454 }
5455
5456 return declared_type;
5457 }
5458
5459 /* Called when a declaration is seen that contains no names to declare.
5460 If its type is a reference to a structure, union or enum inherited
5461 from a containing scope, shadow that tag name for the current scope
5462 with a forward reference.
5463 If its type defines a new named structure or union
5464 or defines an enum, it is valid but we need not do anything here.
5465 Otherwise, it is an error.
5466
5467 C++: may have to grok the declspecs to learn about static,
5468 complain for anonymous unions.
5469
5470 Returns the TYPE declared -- or NULL_TREE if none. */
5471
5472 tree
shadow_tag(cp_decl_specifier_seq * declspecs)5473 shadow_tag (cp_decl_specifier_seq *declspecs)
5474 {
5475 tree t = check_tag_decl (declspecs,
5476 /*explicit_type_instantiation_p=*/false);
5477
5478 if (!t)
5479 return NULL_TREE;
5480
5481 t = maybe_process_partial_specialization (t);
5482 if (t == error_mark_node)
5483 return NULL_TREE;
5484
5485 /* This is where the variables in an anonymous union are
5486 declared. An anonymous union declaration looks like:
5487 union { ... } ;
5488 because there is no declarator after the union, the parser
5489 sends that declaration here. */
5490 if (ANON_AGGR_TYPE_P (t))
5491 {
5492 fixup_anonymous_aggr (t);
5493
5494 if (TYPE_FIELDS (t))
5495 {
5496 tree decl = grokdeclarator (/*declarator=*/NULL,
5497 declspecs, NORMAL, 0, NULL);
5498 finish_anon_union (decl);
5499 }
5500 }
5501
5502 return t;
5503 }
5504
5505 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
5506
5507 tree
groktypename(cp_decl_specifier_seq * type_specifiers,const cp_declarator * declarator,bool is_template_arg)5508 groktypename (cp_decl_specifier_seq *type_specifiers,
5509 const cp_declarator *declarator,
5510 bool is_template_arg)
5511 {
5512 tree attrs;
5513 tree type;
5514 enum decl_context context
5515 = is_template_arg ? TEMPLATE_TYPE_ARG : TYPENAME;
5516 attrs = type_specifiers->attributes;
5517 type_specifiers->attributes = NULL_TREE;
5518 type = grokdeclarator (declarator, type_specifiers, context, 0, &attrs);
5519 if (attrs && type != error_mark_node)
5520 {
5521 if (CLASS_TYPE_P (type))
5522 warning (OPT_Wattributes, "ignoring attributes applied to class type %qT "
5523 "outside of definition", type);
5524 else if (MAYBE_CLASS_TYPE_P (type))
5525 /* A template type parameter or other dependent type. */
5526 warning (OPT_Wattributes, "ignoring attributes applied to dependent "
5527 "type %qT without an associated declaration", type);
5528 else
5529 cplus_decl_attributes (&type, attrs, 0);
5530 }
5531 return type;
5532 }
5533
5534 /* Process a DECLARATOR for a function-scope or namespace-scope
5535 variable or function declaration.
5536 (Function definitions go through start_function; class member
5537 declarations appearing in the body of the class go through
5538 grokfield.) The DECL corresponding to the DECLARATOR is returned.
5539 If an error occurs, the error_mark_node is returned instead.
5540
5541 DECLSPECS are the decl-specifiers for the declaration. INITIALIZED is
5542 SD_INITIALIZED if an explicit initializer is present, or SD_DEFAULTED
5543 for an explicitly defaulted function, or SD_DELETED for an explicitly
5544 deleted function, but 0 (SD_UNINITIALIZED) if this is a variable
5545 implicitly initialized via a default constructor. It can also be
5546 SD_DECOMPOSITION which behaves much like SD_INITIALIZED, but we also
5547 mark the new decl as DECL_DECOMPOSITION_P.
5548
5549 ATTRIBUTES and PREFIX_ATTRIBUTES are GNU attributes associated with this
5550 declaration.
5551
5552 The scope represented by the context of the returned DECL is pushed
5553 (if it is not the global namespace) and is assigned to
5554 *PUSHED_SCOPE_P. The caller is then responsible for calling
5555 pop_scope on *PUSHED_SCOPE_P if it is set. */
5556
5557 tree
start_decl(const cp_declarator * declarator,cp_decl_specifier_seq * declspecs,int initialized,tree attributes,tree prefix_attributes,tree * pushed_scope_p)5558 start_decl (const cp_declarator *declarator,
5559 cp_decl_specifier_seq *declspecs,
5560 int initialized,
5561 tree attributes,
5562 tree prefix_attributes,
5563 tree *pushed_scope_p)
5564 {
5565 tree decl;
5566 tree context;
5567 bool was_public;
5568 int flags;
5569 bool alias;
5570 tree initial;
5571
5572 *pushed_scope_p = NULL_TREE;
5573
5574 if (prefix_attributes != error_mark_node)
5575 attributes = chainon (attributes, prefix_attributes);
5576
5577 decl = grokdeclarator (declarator, declspecs, NORMAL, initialized,
5578 &attributes);
5579
5580 if (decl == NULL_TREE || VOID_TYPE_P (decl)
5581 || decl == error_mark_node
5582 || prefix_attributes == error_mark_node)
5583 return error_mark_node;
5584
5585 context = CP_DECL_CONTEXT (decl);
5586 if (context != global_namespace)
5587 *pushed_scope_p = push_scope (context);
5588
5589 if (initialized && TREE_CODE (decl) == TYPE_DECL)
5590 {
5591 error_at (DECL_SOURCE_LOCATION (decl),
5592 "typedef %qD is initialized (use %qs instead)",
5593 decl, "decltype");
5594 return error_mark_node;
5595 }
5596
5597 /* Save the DECL_INITIAL value in case it gets clobbered to assist
5598 with attribute validation. */
5599 initial = DECL_INITIAL (decl);
5600
5601 if (initialized)
5602 {
5603 if (! toplevel_bindings_p ()
5604 && DECL_EXTERNAL (decl))
5605 warning (0, "declaration of %q#D has %<extern%> and is initialized",
5606 decl);
5607 DECL_EXTERNAL (decl) = 0;
5608 if (toplevel_bindings_p ())
5609 TREE_STATIC (decl) = 1;
5610 /* Tell 'cplus_decl_attributes' this is an initialized decl,
5611 even though we might not yet have the initializer expression. */
5612 if (!DECL_INITIAL (decl))
5613 DECL_INITIAL (decl) = error_mark_node;
5614 }
5615 alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl)) != 0;
5616
5617 if (alias && TREE_CODE (decl) == FUNCTION_DECL)
5618 record_key_method_defined (decl);
5619
5620 /* If this is a typedef that names the class for linkage purposes
5621 (7.1.3p8), apply any attributes directly to the type. */
5622 if (TREE_CODE (decl) == TYPE_DECL
5623 && OVERLOAD_TYPE_P (TREE_TYPE (decl))
5624 && decl == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (decl))))
5625 flags = ATTR_FLAG_TYPE_IN_PLACE;
5626 else
5627 flags = 0;
5628
5629 /* Set attributes here so if duplicate decl, will have proper attributes. */
5630 cplus_decl_attributes (&decl, attributes, flags);
5631
5632 /* Restore the original DECL_INITIAL that we may have clobbered earlier to
5633 assist with attribute validation. */
5634 DECL_INITIAL (decl) = initial;
5635
5636 /* Dllimported symbols cannot be defined. Static data members (which
5637 can be initialized in-class and dllimported) go through grokfield,
5638 not here, so we don't need to exclude those decls when checking for
5639 a definition. */
5640 if (initialized && DECL_DLLIMPORT_P (decl))
5641 {
5642 error_at (DECL_SOURCE_LOCATION (decl),
5643 "definition of %q#D is marked %<dllimport%>", decl);
5644 DECL_DLLIMPORT_P (decl) = 0;
5645 }
5646
5647 /* If #pragma weak was used, mark the decl weak now. */
5648 if (!processing_template_decl && !DECL_DECOMPOSITION_P (decl))
5649 maybe_apply_pragma_weak (decl);
5650
5651 if (TREE_CODE (decl) == FUNCTION_DECL
5652 && DECL_DECLARED_INLINE_P (decl)
5653 && DECL_UNINLINABLE (decl)
5654 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
5655 warning_at (DECL_SOURCE_LOCATION (decl), 0,
5656 "inline function %qD given attribute %qs", decl, "noinline");
5657
5658 if (TYPE_P (context) && COMPLETE_TYPE_P (complete_type (context)))
5659 {
5660 bool this_tmpl = (current_template_depth
5661 > template_class_depth (context));
5662 if (VAR_P (decl))
5663 {
5664 tree field = lookup_field (context, DECL_NAME (decl), 0, false);
5665 if (field == NULL_TREE
5666 || !(VAR_P (field) || variable_template_p (field)))
5667 error ("%q+#D is not a static data member of %q#T", decl, context);
5668 else if (variable_template_p (field)
5669 && (DECL_LANG_SPECIFIC (decl)
5670 && DECL_TEMPLATE_SPECIALIZATION (decl)))
5671 /* OK, specialization was already checked. */;
5672 else if (variable_template_p (field) && !this_tmpl)
5673 {
5674 error_at (DECL_SOURCE_LOCATION (decl),
5675 "non-member-template declaration of %qD", decl);
5676 inform (DECL_SOURCE_LOCATION (field), "does not match "
5677 "member template declaration here");
5678 return error_mark_node;
5679 }
5680 else
5681 {
5682 if (variable_template_p (field))
5683 field = DECL_TEMPLATE_RESULT (field);
5684
5685 if (DECL_CONTEXT (field) != context)
5686 {
5687 if (!same_type_p (DECL_CONTEXT (field), context))
5688 permerror (input_location, "ISO C++ does not permit %<%T::%D%> "
5689 "to be defined as %<%T::%D%>",
5690 DECL_CONTEXT (field), DECL_NAME (decl),
5691 context, DECL_NAME (decl));
5692 DECL_CONTEXT (decl) = DECL_CONTEXT (field);
5693 }
5694 /* Static data member are tricky; an in-class initialization
5695 still doesn't provide a definition, so the in-class
5696 declaration will have DECL_EXTERNAL set, but will have an
5697 initialization. Thus, duplicate_decls won't warn
5698 about this situation, and so we check here. */
5699 if (initialized && DECL_INITIALIZED_IN_CLASS_P (field))
5700 error ("duplicate initialization of %qD", decl);
5701 field = duplicate_decls (decl, field);
5702 if (field == error_mark_node)
5703 return error_mark_node;
5704 else if (field)
5705 decl = field;
5706 }
5707 }
5708 else
5709 {
5710 tree field = check_classfn (context, decl,
5711 this_tmpl
5712 ? current_template_parms
5713 : NULL_TREE);
5714 if (field && field != error_mark_node
5715 && duplicate_decls (decl, field))
5716 decl = field;
5717 }
5718
5719 /* cp_finish_decl sets DECL_EXTERNAL if DECL_IN_AGGR_P is set. */
5720 DECL_IN_AGGR_P (decl) = 0;
5721 /* Do not mark DECL as an explicit specialization if it was not
5722 already marked as an instantiation; a declaration should
5723 never be marked as a specialization unless we know what
5724 template is being specialized. */
5725 if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
5726 {
5727 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
5728 if (TREE_CODE (decl) == FUNCTION_DECL)
5729 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
5730 && DECL_DECLARED_INLINE_P (decl));
5731 else
5732 DECL_COMDAT (decl) = false;
5733
5734 /* [temp.expl.spec] An explicit specialization of a static data
5735 member of a template is a definition if the declaration
5736 includes an initializer; otherwise, it is a declaration.
5737
5738 We check for processing_specialization so this only applies
5739 to the new specialization syntax. */
5740 if (!initialized && processing_specialization)
5741 DECL_EXTERNAL (decl) = 1;
5742 }
5743
5744 if (DECL_EXTERNAL (decl) && ! DECL_TEMPLATE_SPECIALIZATION (decl)
5745 /* Aliases are definitions. */
5746 && !alias)
5747 permerror (declarator->id_loc,
5748 "declaration of %q#D outside of class is not definition",
5749 decl);
5750 }
5751
5752 /* Create a DECL_LANG_SPECIFIC so that DECL_DECOMPOSITION_P works. */
5753 if (initialized == SD_DECOMPOSITION)
5754 fit_decomposition_lang_decl (decl, NULL_TREE);
5755
5756 was_public = TREE_PUBLIC (decl);
5757
5758 if ((DECL_EXTERNAL (decl) || TREE_CODE (decl) == FUNCTION_DECL)
5759 && current_function_decl)
5760 {
5761 /* A function-scope decl of some namespace-scope decl. */
5762 DECL_LOCAL_DECL_P (decl) = true;
5763 if (named_module_purview_p ())
5764 error_at (declarator->id_loc,
5765 "block-scope extern declaration %q#D not permitted"
5766 " in module purview", decl);
5767 }
5768
5769 /* Enter this declaration into the symbol table. Don't push the plain
5770 VAR_DECL for a variable template. */
5771 if (!template_parm_scope_p ()
5772 || !VAR_P (decl))
5773 decl = maybe_push_decl (decl);
5774
5775 if (processing_template_decl)
5776 decl = push_template_decl (decl);
5777
5778 if (decl == error_mark_node)
5779 return error_mark_node;
5780
5781 if (VAR_P (decl)
5782 && DECL_NAMESPACE_SCOPE_P (decl) && !TREE_PUBLIC (decl) && !was_public
5783 && !DECL_THIS_STATIC (decl) && !DECL_ARTIFICIAL (decl)
5784 /* But not templated variables. */
5785 && !(DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)))
5786 {
5787 /* This is a const variable with implicit 'static'. Set
5788 DECL_THIS_STATIC so we can tell it from variables that are
5789 !TREE_PUBLIC because of the anonymous namespace. */
5790 gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (decl)) || errorcount);
5791 DECL_THIS_STATIC (decl) = 1;
5792 }
5793
5794 if (current_function_decl && VAR_P (decl)
5795 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
5796 && cxx_dialect < cxx23)
5797 {
5798 bool ok = false;
5799 if (CP_DECL_THREAD_LOCAL_P (decl) && !DECL_REALLY_EXTERN (decl))
5800 error_at (DECL_SOURCE_LOCATION (decl),
5801 "%qD defined %<thread_local%> in %qs function only "
5802 "available with %<-std=c++2b%> or %<-std=gnu++2b%>", decl,
5803 DECL_IMMEDIATE_FUNCTION_P (current_function_decl)
5804 ? "consteval" : "constexpr");
5805 else if (TREE_STATIC (decl))
5806 error_at (DECL_SOURCE_LOCATION (decl),
5807 "%qD defined %<static%> in %qs function only available "
5808 "with %<-std=c++2b%> or %<-std=gnu++2b%>", decl,
5809 DECL_IMMEDIATE_FUNCTION_P (current_function_decl)
5810 ? "consteval" : "constexpr");
5811 else
5812 ok = true;
5813 if (!ok)
5814 cp_function_chain->invalid_constexpr = true;
5815 }
5816
5817 if (!processing_template_decl && VAR_P (decl))
5818 start_decl_1 (decl, initialized);
5819
5820 return decl;
5821 }
5822
5823 /* Process the declaration of a variable DECL. INITIALIZED is true
5824 iff DECL is explicitly initialized. (INITIALIZED is false if the
5825 variable is initialized via an implicitly-called constructor.)
5826 This function must be called for ordinary variables (including, for
5827 example, implicit instantiations of templates), but must not be
5828 called for template declarations. */
5829
5830 void
start_decl_1(tree decl,bool initialized)5831 start_decl_1 (tree decl, bool initialized)
5832 {
5833 gcc_checking_assert (!processing_template_decl);
5834
5835 if (error_operand_p (decl))
5836 return;
5837
5838 gcc_checking_assert (VAR_P (decl));
5839
5840 tree type = TREE_TYPE (decl);
5841 bool complete_p = COMPLETE_TYPE_P (type);
5842 bool aggregate_definition_p
5843 = MAYBE_CLASS_TYPE_P (type) && !DECL_EXTERNAL (decl);
5844
5845 /* If an explicit initializer is present, or if this is a definition
5846 of an aggregate, then we need a complete type at this point.
5847 (Scalars are always complete types, so there is nothing to
5848 check.) This code just sets COMPLETE_P; errors (if necessary)
5849 are issued below. */
5850 if ((initialized || aggregate_definition_p)
5851 && !complete_p
5852 && COMPLETE_TYPE_P (complete_type (type)))
5853 {
5854 complete_p = true;
5855 /* We will not yet have set TREE_READONLY on DECL if the type
5856 was "const", but incomplete, before this point. But, now, we
5857 have a complete type, so we can try again. */
5858 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
5859 }
5860
5861 if (initialized)
5862 /* Is it valid for this decl to have an initializer at all? */
5863 {
5864 /* Don't allow initializations for incomplete types except for
5865 arrays which might be completed by the initialization. */
5866 if (complete_p)
5867 ; /* A complete type is ok. */
5868 else if (type_uses_auto (type))
5869 ; /* An auto type is ok. */
5870 else if (TREE_CODE (type) != ARRAY_TYPE)
5871 {
5872 error ("variable %q#D has initializer but incomplete type", decl);
5873 type = TREE_TYPE (decl) = error_mark_node;
5874 }
5875 else if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
5876 {
5877 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
5878 error ("elements of array %q#D have incomplete type", decl);
5879 /* else we already gave an error in start_decl. */
5880 }
5881 }
5882 else if (aggregate_definition_p && !complete_p)
5883 {
5884 if (type_uses_auto (type))
5885 gcc_assert (CLASS_PLACEHOLDER_TEMPLATE (type));
5886 else
5887 {
5888 error ("aggregate %q#D has incomplete type and cannot be defined",
5889 decl);
5890 /* Change the type so that assemble_variable will give
5891 DECL an rtl we can live with: (mem (const_int 0)). */
5892 type = TREE_TYPE (decl) = error_mark_node;
5893 }
5894 }
5895
5896 /* Create a new scope to hold this declaration if necessary.
5897 Whether or not a new scope is necessary cannot be determined
5898 until after the type has been completed; if the type is a
5899 specialization of a class template it is not until after
5900 instantiation has occurred that TYPE_HAS_NONTRIVIAL_DESTRUCTOR
5901 will be set correctly. */
5902 maybe_push_cleanup_level (type);
5903 }
5904
5905 /* Given a parenthesized list of values INIT, create a CONSTRUCTOR to handle
5906 C++20 P0960. TYPE is the type of the object we're initializing. */
5907
5908 tree
do_aggregate_paren_init(tree init,tree type)5909 do_aggregate_paren_init (tree init, tree type)
5910 {
5911 tree val = TREE_VALUE (init);
5912
5913 if (TREE_CHAIN (init) == NULL_TREE)
5914 {
5915 /* If the list has a single element and it's a string literal,
5916 then it's the initializer for the array as a whole. */
5917 if (TREE_CODE (type) == ARRAY_TYPE
5918 && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type)))
5919 && TREE_CODE (tree_strip_any_location_wrapper (val))
5920 == STRING_CST)
5921 return val;
5922 /* Handle non-standard extensions like compound literals. This also
5923 prevents triggering aggregate parenthesized-initialization in
5924 compiler-generated code for =default. */
5925 else if (same_type_ignoring_top_level_qualifiers_p (type,
5926 TREE_TYPE (val)))
5927 return val;
5928 }
5929
5930 init = build_constructor_from_list (init_list_type_node, init);
5931 CONSTRUCTOR_IS_DIRECT_INIT (init) = true;
5932 CONSTRUCTOR_IS_PAREN_INIT (init) = true;
5933 return init;
5934 }
5935
5936 /* Handle initialization of references. DECL, TYPE, and INIT have the
5937 same meaning as in cp_finish_decl. *CLEANUP must be NULL on entry,
5938 but will be set to a new CLEANUP_STMT if a temporary is created
5939 that must be destroyed subsequently.
5940
5941 Returns an initializer expression to use to initialize DECL, or
5942 NULL if the initialization can be performed statically.
5943
5944 Quotes on semantics can be found in ARM 8.4.3. */
5945
5946 static tree
grok_reference_init(tree decl,tree type,tree init,int flags)5947 grok_reference_init (tree decl, tree type, tree init, int flags)
5948 {
5949 if (init == NULL_TREE)
5950 {
5951 if ((DECL_LANG_SPECIFIC (decl) == 0
5952 || DECL_IN_AGGR_P (decl) == 0)
5953 && ! DECL_THIS_EXTERN (decl))
5954 error_at (DECL_SOURCE_LOCATION (decl),
5955 "%qD declared as reference but not initialized", decl);
5956 return NULL_TREE;
5957 }
5958
5959 tree ttype = TREE_TYPE (type);
5960 if (TREE_CODE (init) == TREE_LIST)
5961 {
5962 /* This handles (C++20 only) code like
5963
5964 const A& r(1, 2, 3);
5965
5966 where we treat the parenthesized list as a CONSTRUCTOR. */
5967 if (TREE_TYPE (init) == NULL_TREE
5968 && CP_AGGREGATE_TYPE_P (ttype)
5969 && !DECL_DECOMPOSITION_P (decl)
5970 && (cxx_dialect >= cxx20))
5971 {
5972 /* We don't know yet if we should treat const A& r(1) as
5973 const A& r{1}. */
5974 if (list_length (init) == 1)
5975 {
5976 flags |= LOOKUP_AGGREGATE_PAREN_INIT;
5977 init = build_x_compound_expr_from_list (init, ELK_INIT,
5978 tf_warning_or_error);
5979 }
5980 /* If the list had more than one element, the code is ill-formed
5981 pre-C++20, so we can build a constructor right away. */
5982 else
5983 init = do_aggregate_paren_init (init, ttype);
5984 }
5985 else
5986 init = build_x_compound_expr_from_list (init, ELK_INIT,
5987 tf_warning_or_error);
5988 }
5989
5990 if (TREE_CODE (ttype) != ARRAY_TYPE
5991 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
5992 /* Note: default conversion is only called in very special cases. */
5993 init = decay_conversion (init, tf_warning_or_error);
5994
5995 /* check_initializer handles this for non-reference variables, but for
5996 references we need to do it here or the initializer will get the
5997 incomplete array type and confuse later calls to
5998 cp_complete_array_type. */
5999 if (TREE_CODE (ttype) == ARRAY_TYPE
6000 && TYPE_DOMAIN (ttype) == NULL_TREE
6001 && (BRACE_ENCLOSED_INITIALIZER_P (init)
6002 || TREE_CODE (init) == STRING_CST))
6003 {
6004 cp_complete_array_type (&ttype, init, false);
6005 if (ttype != TREE_TYPE (type))
6006 type = cp_build_reference_type (ttype, TYPE_REF_IS_RVALUE (type));
6007 }
6008
6009 /* Convert INIT to the reference type TYPE. This may involve the
6010 creation of a temporary, whose lifetime must be the same as that
6011 of the reference. If so, a DECL_EXPR for the temporary will be
6012 added just after the DECL_EXPR for DECL. That's why we don't set
6013 DECL_INITIAL for local references (instead assigning to them
6014 explicitly); we need to allow the temporary to be initialized
6015 first. */
6016 return initialize_reference (type, init, flags,
6017 tf_warning_or_error);
6018 }
6019
6020 /* Designated initializers in arrays are not supported in GNU C++.
6021 The parser cannot detect this error since it does not know whether
6022 a given brace-enclosed initializer is for a class type or for an
6023 array. This function checks that CE does not use a designated
6024 initializer. If it does, an error is issued. Returns true if CE
6025 is valid, i.e., does not have a designated initializer. */
6026
6027 bool
check_array_designated_initializer(constructor_elt * ce,unsigned HOST_WIDE_INT index)6028 check_array_designated_initializer (constructor_elt *ce,
6029 unsigned HOST_WIDE_INT index)
6030 {
6031 /* Designated initializers for array elements are not supported. */
6032 if (ce->index)
6033 {
6034 /* The parser only allows identifiers as designated
6035 initializers. */
6036 if (ce->index == error_mark_node)
6037 {
6038 error ("name used in a GNU-style designated "
6039 "initializer for an array");
6040 return false;
6041 }
6042 else if (identifier_p (ce->index))
6043 {
6044 error ("name %qD used in a GNU-style designated "
6045 "initializer for an array", ce->index);
6046 return false;
6047 }
6048
6049 tree ce_index = build_expr_type_conversion (WANT_INT | WANT_ENUM,
6050 ce->index, true);
6051 if (ce_index
6052 && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (ce_index))
6053 && (TREE_CODE (ce_index = fold_non_dependent_expr (ce_index))
6054 == INTEGER_CST))
6055 {
6056 /* A C99 designator is OK if it matches the current index. */
6057 if (wi::to_wide (ce_index) == index)
6058 {
6059 ce->index = ce_index;
6060 return true;
6061 }
6062 else
6063 sorry ("non-trivial designated initializers not supported");
6064 }
6065 else
6066 error_at (cp_expr_loc_or_input_loc (ce->index),
6067 "C99 designator %qE is not an integral constant-expression",
6068 ce->index);
6069
6070 return false;
6071 }
6072
6073 return true;
6074 }
6075
6076 /* When parsing `int a[] = {1, 2};' we don't know the size of the
6077 array until we finish parsing the initializer. If that's the
6078 situation we're in, update DECL accordingly. */
6079
6080 static void
maybe_deduce_size_from_array_init(tree decl,tree init)6081 maybe_deduce_size_from_array_init (tree decl, tree init)
6082 {
6083 tree type = TREE_TYPE (decl);
6084
6085 if (TREE_CODE (type) == ARRAY_TYPE
6086 && TYPE_DOMAIN (type) == NULL_TREE
6087 && TREE_CODE (decl) != TYPE_DECL)
6088 {
6089 /* do_default is really a C-ism to deal with tentative definitions.
6090 But let's leave it here to ease the eventual merge. */
6091 int do_default = !DECL_EXTERNAL (decl);
6092 tree initializer = init ? init : DECL_INITIAL (decl);
6093 int failure = 0;
6094
6095 /* Check that there are no designated initializers in INIT, as
6096 those are not supported in GNU C++, and as the middle-end
6097 will crash if presented with a non-numeric designated
6098 initializer. */
6099 if (initializer && BRACE_ENCLOSED_INITIALIZER_P (initializer))
6100 {
6101 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initializer);
6102 constructor_elt *ce;
6103 HOST_WIDE_INT i;
6104 FOR_EACH_VEC_SAFE_ELT (v, i, ce)
6105 {
6106 if (instantiation_dependent_expression_p (ce->index))
6107 return;
6108 if (!check_array_designated_initializer (ce, i))
6109 failure = 1;
6110 /* If an un-designated initializer is type-dependent, we can't
6111 check brace elision yet. */
6112 if (ce->index == NULL_TREE
6113 && type_dependent_expression_p (ce->value))
6114 return;
6115 }
6116 }
6117
6118 if (failure)
6119 TREE_TYPE (decl) = error_mark_node;
6120 else
6121 {
6122 failure = cp_complete_array_type (&TREE_TYPE (decl), initializer,
6123 do_default);
6124 if (failure == 1)
6125 {
6126 error_at (cp_expr_loc_or_loc (initializer,
6127 DECL_SOURCE_LOCATION (decl)),
6128 "initializer fails to determine size of %qD", decl);
6129 }
6130 else if (failure == 2)
6131 {
6132 if (do_default)
6133 {
6134 error_at (DECL_SOURCE_LOCATION (decl),
6135 "array size missing in %qD", decl);
6136 }
6137 /* If a `static' var's size isn't known, make it extern as
6138 well as static, so it does not get allocated. If it's not
6139 `static', then don't mark it extern; finish_incomplete_decl
6140 will give it a default size and it will get allocated. */
6141 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
6142 DECL_EXTERNAL (decl) = 1;
6143 }
6144 else if (failure == 3)
6145 {
6146 error_at (DECL_SOURCE_LOCATION (decl),
6147 "zero-size array %qD", decl);
6148 }
6149 }
6150
6151 cp_apply_type_quals_to_decl (cp_type_quals (TREE_TYPE (decl)), decl);
6152
6153 relayout_decl (decl);
6154 }
6155 }
6156
6157 /* Set DECL_SIZE, DECL_ALIGN, etc. for DECL (a VAR_DECL), and issue
6158 any appropriate error messages regarding the layout. */
6159
6160 static void
layout_var_decl(tree decl)6161 layout_var_decl (tree decl)
6162 {
6163 tree type;
6164
6165 type = TREE_TYPE (decl);
6166 if (type == error_mark_node)
6167 return;
6168
6169 /* If we haven't already laid out this declaration, do so now.
6170 Note that we must not call complete type for an external object
6171 because it's type might involve templates that we are not
6172 supposed to instantiate yet. (And it's perfectly valid to say
6173 `extern X x' for some incomplete type `X'.) */
6174 if (!DECL_EXTERNAL (decl))
6175 complete_type (type);
6176 if (!DECL_SIZE (decl)
6177 && TREE_TYPE (decl) != error_mark_node
6178 && complete_or_array_type_p (type))
6179 layout_decl (decl, 0);
6180
6181 if (!DECL_EXTERNAL (decl) && DECL_SIZE (decl) == NULL_TREE)
6182 {
6183 /* An automatic variable with an incomplete type: that is an error.
6184 Don't talk about array types here, since we took care of that
6185 message in grokdeclarator. */
6186 error_at (DECL_SOURCE_LOCATION (decl),
6187 "storage size of %qD isn%'t known", decl);
6188 TREE_TYPE (decl) = error_mark_node;
6189 }
6190 #if 0
6191 /* Keep this code around in case we later want to control debug info
6192 based on whether a type is "used". (jason 1999-11-11) */
6193
6194 else if (!DECL_EXTERNAL (decl) && MAYBE_CLASS_TYPE_P (ttype))
6195 /* Let debugger know it should output info for this type. */
6196 note_debug_info_needed (ttype);
6197
6198 if (TREE_STATIC (decl) && DECL_CLASS_SCOPE_P (decl))
6199 note_debug_info_needed (DECL_CONTEXT (decl));
6200 #endif
6201
6202 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
6203 && DECL_SIZE (decl) != NULL_TREE
6204 && ! TREE_CONSTANT (DECL_SIZE (decl)))
6205 {
6206 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST
6207 && !DECL_LOCAL_DECL_P (decl))
6208 constant_expression_warning (DECL_SIZE (decl));
6209 else
6210 {
6211 error_at (DECL_SOURCE_LOCATION (decl),
6212 "storage size of %qD isn%'t constant", decl);
6213 TREE_TYPE (decl) = error_mark_node;
6214 type = error_mark_node;
6215 }
6216 }
6217
6218 /* If the final element initializes a flexible array field, add the size of
6219 that initializer to DECL's size. */
6220 if (type != error_mark_node
6221 && DECL_INITIAL (decl)
6222 && TREE_CODE (DECL_INITIAL (decl)) == CONSTRUCTOR
6223 && !vec_safe_is_empty (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)))
6224 && DECL_SIZE (decl) != NULL_TREE
6225 && TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST
6226 && TYPE_SIZE (type) != NULL_TREE
6227 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
6228 && tree_int_cst_equal (DECL_SIZE (decl), TYPE_SIZE (type)))
6229 {
6230 constructor_elt &elt = CONSTRUCTOR_ELTS (DECL_INITIAL (decl))->last ();
6231 if (elt.index)
6232 {
6233 tree itype = TREE_TYPE (elt.index);
6234 tree vtype = TREE_TYPE (elt.value);
6235 if (TREE_CODE (itype) == ARRAY_TYPE
6236 && TYPE_DOMAIN (itype) == NULL
6237 && TREE_CODE (vtype) == ARRAY_TYPE
6238 && COMPLETE_TYPE_P (vtype))
6239 {
6240 DECL_SIZE (decl)
6241 = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (vtype));
6242 DECL_SIZE_UNIT (decl)
6243 = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl),
6244 TYPE_SIZE_UNIT (vtype));
6245 }
6246 }
6247 }
6248 }
6249
6250 /* If a local static variable is declared in an inline function, or if
6251 we have a weak definition, we must endeavor to create only one
6252 instance of the variable at link-time. */
6253
6254 void
maybe_commonize_var(tree decl)6255 maybe_commonize_var (tree decl)
6256 {
6257 /* Don't mess with __FUNCTION__ and similar. */
6258 if (DECL_ARTIFICIAL (decl))
6259 return;
6260
6261 /* Static data in a function with comdat linkage also has comdat
6262 linkage. */
6263 if ((TREE_STATIC (decl)
6264 && DECL_FUNCTION_SCOPE_P (decl)
6265 && vague_linkage_p (DECL_CONTEXT (decl)))
6266 || (TREE_PUBLIC (decl) && DECL_INLINE_VAR_P (decl)))
6267 {
6268 if (flag_weak)
6269 {
6270 /* With weak symbols, we simply make the variable COMDAT;
6271 that will cause copies in multiple translations units to
6272 be merged. */
6273 comdat_linkage (decl);
6274 }
6275 else
6276 {
6277 if (DECL_INITIAL (decl) == NULL_TREE
6278 || DECL_INITIAL (decl) == error_mark_node)
6279 {
6280 /* Without weak symbols, we can use COMMON to merge
6281 uninitialized variables. */
6282 TREE_PUBLIC (decl) = 1;
6283 DECL_COMMON (decl) = 1;
6284 }
6285 else
6286 {
6287 /* While for initialized variables, we must use internal
6288 linkage -- which means that multiple copies will not
6289 be merged. */
6290 TREE_PUBLIC (decl) = 0;
6291 DECL_COMMON (decl) = 0;
6292 DECL_INTERFACE_KNOWN (decl) = 1;
6293 const char *msg;
6294 if (DECL_INLINE_VAR_P (decl))
6295 msg = G_("sorry: semantics of inline variable "
6296 "%q#D are wrong (you%'ll wind up with "
6297 "multiple copies)");
6298 else
6299 msg = G_("sorry: semantics of inline function "
6300 "static data %q#D are wrong (you%'ll wind "
6301 "up with multiple copies)");
6302 if (warning_at (DECL_SOURCE_LOCATION (decl), 0,
6303 msg, decl))
6304 inform (DECL_SOURCE_LOCATION (decl),
6305 "you can work around this by removing the initializer");
6306 }
6307 }
6308 }
6309 }
6310
6311 /* Issue an error message if DECL is an uninitialized const variable.
6312 CONSTEXPR_CONTEXT_P is true when the function is called in a constexpr
6313 context from potential_constant_expression. Returns true if all is well,
6314 false otherwise. */
6315
6316 bool
check_for_uninitialized_const_var(tree decl,bool constexpr_context_p,tsubst_flags_t complain)6317 check_for_uninitialized_const_var (tree decl, bool constexpr_context_p,
6318 tsubst_flags_t complain)
6319 {
6320 tree type = strip_array_types (TREE_TYPE (decl));
6321
6322 /* ``Unless explicitly declared extern, a const object does not have
6323 external linkage and must be initialized. ($8.4; $12.1)'' ARM
6324 7.1.6 */
6325 if (VAR_P (decl)
6326 && !TYPE_REF_P (type)
6327 && (CP_TYPE_CONST_P (type)
6328 /* C++20 permits trivial default initialization in constexpr
6329 context (P1331R2). */
6330 || (cxx_dialect < cxx20
6331 && (constexpr_context_p
6332 || var_in_constexpr_fn (decl))))
6333 && !DECL_NONTRIVIALLY_INITIALIZED_P (decl))
6334 {
6335 tree field = default_init_uninitialized_part (type);
6336 if (!field)
6337 return true;
6338
6339 bool show_notes = true;
6340
6341 if (!constexpr_context_p || cxx_dialect >= cxx20)
6342 {
6343 if (CP_TYPE_CONST_P (type))
6344 {
6345 if (complain & tf_error)
6346 show_notes = permerror (DECL_SOURCE_LOCATION (decl),
6347 "uninitialized %<const %D%>", decl);
6348 }
6349 else
6350 {
6351 if (!is_instantiation_of_constexpr (current_function_decl)
6352 && (complain & tf_error))
6353 error_at (DECL_SOURCE_LOCATION (decl),
6354 "uninitialized variable %qD in %<constexpr%> "
6355 "function", decl);
6356 else
6357 show_notes = false;
6358 cp_function_chain->invalid_constexpr = true;
6359 }
6360 }
6361 else if (complain & tf_error)
6362 error_at (DECL_SOURCE_LOCATION (decl),
6363 "uninitialized variable %qD in %<constexpr%> context",
6364 decl);
6365
6366 if (show_notes && CLASS_TYPE_P (type) && (complain & tf_error))
6367 {
6368 tree defaulted_ctor;
6369
6370 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
6371 "%q#T has no user-provided default constructor", type);
6372 defaulted_ctor = in_class_defaulted_default_constructor (type);
6373 if (defaulted_ctor)
6374 inform (DECL_SOURCE_LOCATION (defaulted_ctor),
6375 "constructor is not user-provided because it is "
6376 "explicitly defaulted in the class body");
6377 inform (DECL_SOURCE_LOCATION (field),
6378 "and the implicitly-defined constructor does not "
6379 "initialize %q#D", field);
6380 }
6381
6382 return false;
6383 }
6384
6385 return true;
6386 }
6387
6388 /* Structure holding the current initializer being processed by reshape_init.
6389 CUR is a pointer to the current element being processed, END is a pointer
6390 after the last element present in the initializer. */
6391 struct reshape_iter
6392 {
6393 constructor_elt *cur;
6394 constructor_elt *end;
6395 };
6396
6397 static tree reshape_init_r (tree, reshape_iter *, tree, tsubst_flags_t);
6398
6399 /* FIELD is an element of TYPE_FIELDS or NULL. In the former case, the value
6400 returned is the next FIELD_DECL (possibly FIELD itself) that can be
6401 initialized. If there are no more such fields, the return value
6402 will be NULL. */
6403
6404 tree
next_initializable_field(tree field)6405 next_initializable_field (tree field)
6406 {
6407 while (field
6408 && (TREE_CODE (field) != FIELD_DECL
6409 || DECL_UNNAMED_BIT_FIELD (field)
6410 || (DECL_ARTIFICIAL (field)
6411 /* In C++17, don't skip base class fields. */
6412 && !(cxx_dialect >= cxx17 && DECL_FIELD_IS_BASE (field))
6413 /* Don't skip vptr fields. We might see them when we're
6414 called from reduced_constant_expression_p. */
6415 && !DECL_VIRTUAL_P (field))))
6416 field = DECL_CHAIN (field);
6417
6418 return field;
6419 }
6420
6421 /* FIELD is an element of TYPE_FIELDS or NULL. In the former case, the value
6422 returned is the next FIELD_DECL (possibly FIELD itself) that corresponds
6423 to a subobject. If there are no more such fields, the return value will be
6424 NULL. */
6425
6426 tree
next_subobject_field(tree field)6427 next_subobject_field (tree field)
6428 {
6429 while (field
6430 && (TREE_CODE (field) != FIELD_DECL
6431 || DECL_UNNAMED_BIT_FIELD (field)
6432 || (DECL_ARTIFICIAL (field)
6433 && !DECL_FIELD_IS_BASE (field)
6434 && !DECL_VIRTUAL_P (field))))
6435 field = DECL_CHAIN (field);
6436
6437 return field;
6438 }
6439
6440 /* Return true for [dcl.init.list] direct-list-initialization from
6441 single element of enumeration with a fixed underlying type. */
6442
6443 bool
is_direct_enum_init(tree type,tree init)6444 is_direct_enum_init (tree type, tree init)
6445 {
6446 if (cxx_dialect >= cxx17
6447 && TREE_CODE (type) == ENUMERAL_TYPE
6448 && ENUM_FIXED_UNDERLYING_TYPE_P (type)
6449 && TREE_CODE (init) == CONSTRUCTOR
6450 && CONSTRUCTOR_IS_DIRECT_INIT (init)
6451 && CONSTRUCTOR_NELTS (init) == 1
6452 /* DR 2374: The single element needs to be implicitly
6453 convertible to the underlying type of the enum. */
6454 && can_convert_arg (ENUM_UNDERLYING_TYPE (type),
6455 TREE_TYPE (CONSTRUCTOR_ELT (init, 0)->value),
6456 CONSTRUCTOR_ELT (init, 0)->value,
6457 LOOKUP_IMPLICIT, tf_none))
6458 return true;
6459 return false;
6460 }
6461
6462 /* Subroutine of reshape_init_array and reshape_init_vector, which does
6463 the actual work. ELT_TYPE is the element type of the array. MAX_INDEX is an
6464 INTEGER_CST representing the size of the array minus one (the maximum index),
6465 or NULL_TREE if the array was declared without specifying the size. D is
6466 the iterator within the constructor. */
6467
6468 static tree
reshape_init_array_1(tree elt_type,tree max_index,reshape_iter * d,tree first_initializer_p,tsubst_flags_t complain)6469 reshape_init_array_1 (tree elt_type, tree max_index, reshape_iter *d,
6470 tree first_initializer_p, tsubst_flags_t complain)
6471 {
6472 tree new_init;
6473 bool sized_array_p = (max_index && TREE_CONSTANT (max_index));
6474 unsigned HOST_WIDE_INT max_index_cst = 0;
6475 unsigned HOST_WIDE_INT index;
6476
6477 /* The initializer for an array is always a CONSTRUCTOR. If this is the
6478 outermost CONSTRUCTOR and the element type is non-aggregate, we don't need
6479 to build a new one. But don't reuse if not complaining; if this is
6480 tentative, we might also reshape to another type (95319). */
6481 bool reuse = (first_initializer_p
6482 && (complain & tf_error)
6483 && !CP_AGGREGATE_TYPE_P (elt_type)
6484 && !TREE_SIDE_EFFECTS (first_initializer_p));
6485 if (reuse)
6486 new_init = first_initializer_p;
6487 else
6488 new_init = build_constructor (init_list_type_node, NULL);
6489
6490 if (sized_array_p)
6491 {
6492 /* Minus 1 is used for zero sized arrays. */
6493 if (integer_all_onesp (max_index))
6494 return new_init;
6495
6496 if (tree_fits_uhwi_p (max_index))
6497 max_index_cst = tree_to_uhwi (max_index);
6498 /* sizetype is sign extended, not zero extended. */
6499 else
6500 max_index_cst = tree_to_uhwi (fold_convert (size_type_node, max_index));
6501 }
6502
6503 /* Loop until there are no more initializers. */
6504 for (index = 0;
6505 d->cur != d->end && (!sized_array_p || index <= max_index_cst);
6506 ++index)
6507 {
6508 tree elt_init;
6509 constructor_elt *old_cur = d->cur;
6510
6511 if (d->cur->index)
6512 CONSTRUCTOR_IS_DESIGNATED_INIT (new_init) = true;
6513 check_array_designated_initializer (d->cur, index);
6514 elt_init = reshape_init_r (elt_type, d,
6515 /*first_initializer_p=*/NULL_TREE,
6516 complain);
6517 if (elt_init == error_mark_node)
6518 return error_mark_node;
6519 tree idx = size_int (index);
6520 if (reuse)
6521 {
6522 old_cur->index = idx;
6523 old_cur->value = elt_init;
6524 }
6525 else
6526 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init),
6527 idx, elt_init);
6528 if (!TREE_CONSTANT (elt_init))
6529 TREE_CONSTANT (new_init) = false;
6530
6531 /* This can happen with an invalid initializer (c++/54501). */
6532 if (d->cur == old_cur && !sized_array_p)
6533 break;
6534 }
6535
6536 return new_init;
6537 }
6538
6539 /* Subroutine of reshape_init_r, processes the initializers for arrays.
6540 Parameters are the same of reshape_init_r. */
6541
6542 static tree
reshape_init_array(tree type,reshape_iter * d,tree first_initializer_p,tsubst_flags_t complain)6543 reshape_init_array (tree type, reshape_iter *d, tree first_initializer_p,
6544 tsubst_flags_t complain)
6545 {
6546 tree max_index = NULL_TREE;
6547
6548 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
6549
6550 if (TYPE_DOMAIN (type))
6551 max_index = array_type_nelts (type);
6552
6553 return reshape_init_array_1 (TREE_TYPE (type), max_index, d,
6554 first_initializer_p, complain);
6555 }
6556
6557 /* Subroutine of reshape_init_r, processes the initializers for vectors.
6558 Parameters are the same of reshape_init_r. */
6559
6560 static tree
reshape_init_vector(tree type,reshape_iter * d,tsubst_flags_t complain)6561 reshape_init_vector (tree type, reshape_iter *d, tsubst_flags_t complain)
6562 {
6563 tree max_index = NULL_TREE;
6564
6565 gcc_assert (VECTOR_TYPE_P (type));
6566
6567 if (COMPOUND_LITERAL_P (d->cur->value))
6568 {
6569 tree value = d->cur->value;
6570 if (!same_type_p (TREE_TYPE (value), type))
6571 {
6572 if (complain & tf_error)
6573 error ("invalid type %qT as initializer for a vector of type %qT",
6574 TREE_TYPE (d->cur->value), type);
6575 value = error_mark_node;
6576 }
6577 ++d->cur;
6578 return value;
6579 }
6580
6581 /* For a vector, we initialize it as an array of the appropriate size. */
6582 if (VECTOR_TYPE_P (type))
6583 max_index = size_int (TYPE_VECTOR_SUBPARTS (type) - 1);
6584
6585 return reshape_init_array_1 (TREE_TYPE (type), max_index, d,
6586 NULL_TREE, complain);
6587 }
6588
6589 /* Subroutine of reshape_init*: We're initializing an element with TYPE from
6590 INIT, in isolation from any designator or other initializers. */
6591
6592 static tree
reshape_single_init(tree type,tree init,tsubst_flags_t complain)6593 reshape_single_init (tree type, tree init, tsubst_flags_t complain)
6594 {
6595 /* We could also implement this by wrapping init in a new CONSTRUCTOR and
6596 calling reshape_init, but this way can just live on the stack. */
6597 constructor_elt elt = { /*index=*/NULL_TREE, init };
6598 reshape_iter iter = { &elt, &elt + 1 };
6599 return reshape_init_r (type, &iter,
6600 /*first_initializer_p=*/NULL_TREE,
6601 complain);
6602 }
6603
6604 /* Subroutine of reshape_init_r, processes the initializers for classes
6605 or union. Parameters are the same of reshape_init_r. */
6606
6607 static tree
reshape_init_class(tree type,reshape_iter * d,bool first_initializer_p,tsubst_flags_t complain)6608 reshape_init_class (tree type, reshape_iter *d, bool first_initializer_p,
6609 tsubst_flags_t complain)
6610 {
6611 tree field;
6612 tree new_init;
6613
6614 gcc_assert (CLASS_TYPE_P (type));
6615
6616 /* The initializer for a class is always a CONSTRUCTOR. */
6617 new_init = build_constructor (init_list_type_node, NULL);
6618
6619 int binfo_idx = -1;
6620 tree binfo = TYPE_BINFO (type);
6621 tree base_binfo = NULL_TREE;
6622 if (cxx_dialect >= cxx17 && uses_template_parms (type))
6623 {
6624 /* We get here from maybe_aggr_guide for C++20 class template argument
6625 deduction. In this case we need to look through the binfo because a
6626 template doesn't have base fields. */
6627 binfo_idx = 0;
6628 BINFO_BASE_ITERATE (binfo, binfo_idx, base_binfo);
6629 }
6630 if (base_binfo)
6631 field = base_binfo;
6632 else
6633 field = next_initializable_field (TYPE_FIELDS (type));
6634
6635 if (!field)
6636 {
6637 /* [dcl.init.aggr]
6638
6639 An initializer for an aggregate member that is an
6640 empty class shall have the form of an empty
6641 initializer-list {}. */
6642 if (!first_initializer_p)
6643 {
6644 if (complain & tf_error)
6645 error ("initializer for %qT must be brace-enclosed", type);
6646 return error_mark_node;
6647 }
6648 return new_init;
6649 }
6650
6651 /* For C++20 CTAD, handle pack expansions in the base list. */
6652 tree last_was_pack_expansion = NULL_TREE;
6653
6654 /* Loop through the initializable fields, gathering initializers. */
6655 while (d->cur != d->end)
6656 {
6657 tree field_init;
6658 constructor_elt *old_cur = d->cur;
6659 bool direct_desig = false;
6660
6661 /* Handle C++20 designated initializers. */
6662 if (d->cur->index)
6663 {
6664 if (d->cur->index == error_mark_node)
6665 return error_mark_node;
6666
6667 if (TREE_CODE (d->cur->index) == FIELD_DECL)
6668 {
6669 /* We already reshaped this. */
6670 if (field != d->cur->index)
6671 {
6672 if (tree id = DECL_NAME (d->cur->index))
6673 gcc_checking_assert (d->cur->index
6674 == get_class_binding (type, id));
6675 field = d->cur->index;
6676 }
6677 }
6678 else if (TREE_CODE (d->cur->index) == IDENTIFIER_NODE)
6679 {
6680 CONSTRUCTOR_IS_DESIGNATED_INIT (new_init) = true;
6681 field = get_class_binding (type, d->cur->index);
6682 direct_desig = true;
6683 }
6684 else
6685 {
6686 if (complain & tf_error)
6687 error ("%<[%E] =%> used in a GNU-style designated initializer"
6688 " for class %qT", d->cur->index, type);
6689 return error_mark_node;
6690 }
6691
6692 if (!field && ANON_AGGR_TYPE_P (type))
6693 /* Apparently the designator isn't for a member of this anonymous
6694 struct, so head back to the enclosing class. */
6695 break;
6696
6697 if (!field || TREE_CODE (field) != FIELD_DECL)
6698 {
6699 if (complain & tf_error)
6700 error ("%qT has no non-static data member named %qD", type,
6701 d->cur->index);
6702 return error_mark_node;
6703 }
6704
6705 /* If the element is an anonymous union object and the initializer
6706 list is a designated-initializer-list, the anonymous union object
6707 is initialized by the designated-initializer-list { D }, where D
6708 is the designated-initializer-clause naming a member of the
6709 anonymous union object. */
6710 tree ictx = DECL_CONTEXT (field);
6711 if (!same_type_ignoring_top_level_qualifiers_p (ictx, type))
6712 {
6713 /* Find the anon aggr that is a direct member of TYPE. */
6714 while (ANON_AGGR_TYPE_P (ictx))
6715 {
6716 tree cctx = TYPE_CONTEXT (ictx);
6717 if (same_type_ignoring_top_level_qualifiers_p (cctx, type))
6718 goto found;
6719 ictx = cctx;
6720 }
6721
6722 /* Not found, e.g. FIELD is a member of a base class. */
6723 if (complain & tf_error)
6724 error ("%qD is not a direct member of %qT", field, type);
6725 return error_mark_node;
6726
6727 found:
6728 /* Now find the TYPE member with that anon aggr type. */
6729 tree aafield = TYPE_FIELDS (type);
6730 for (; aafield; aafield = TREE_CHAIN (aafield))
6731 if (TREE_TYPE (aafield) == ictx)
6732 break;
6733 gcc_assert (aafield);
6734 field = aafield;
6735 direct_desig = false;
6736 }
6737 }
6738
6739 /* If we processed all the member of the class, we are done. */
6740 if (!field)
6741 break;
6742
6743 last_was_pack_expansion = (PACK_EXPANSION_P (TREE_TYPE (field))
6744 ? field : NULL_TREE);
6745 if (last_was_pack_expansion)
6746 /* Each non-trailing aggregate element that is a pack expansion is
6747 assumed to correspond to no elements of the initializer list. */
6748 goto continue_;
6749
6750 if (direct_desig)
6751 {
6752 /* The designated field F is initialized from this one element.
6753
6754 Note that we don't want to do this if we found the designator
6755 inside an anon aggr above; we use the normal code to implement:
6756
6757 "If the element is an anonymous union member and the initializer
6758 list is a brace-enclosed designated- initializer-list, the element
6759 is initialized by the designated-initializer-list { D }, where D
6760 is the designated- initializer-clause naming a member of the
6761 anonymous union member." */
6762 field_init = reshape_single_init (TREE_TYPE (field),
6763 d->cur->value, complain);
6764 d->cur++;
6765 }
6766 else
6767 field_init = reshape_init_r (TREE_TYPE (field), d,
6768 /*first_initializer_p=*/NULL_TREE,
6769 complain);
6770
6771 if (field_init == error_mark_node)
6772 return error_mark_node;
6773
6774 if (d->cur == old_cur && d->cur->index)
6775 {
6776 /* This can happen with an invalid initializer for a flexible
6777 array member (c++/54441). */
6778 if (complain & tf_error)
6779 error ("invalid initializer for %q#D", field);
6780 return error_mark_node;
6781 }
6782
6783 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init), field, field_init);
6784
6785 /* [dcl.init.aggr]
6786
6787 When a union is initialized with a brace-enclosed
6788 initializer, the braces shall only contain an
6789 initializer for the first member of the union. */
6790 if (TREE_CODE (type) == UNION_TYPE)
6791 break;
6792
6793 continue_:
6794 if (base_binfo)
6795 {
6796 if (BINFO_BASE_ITERATE (binfo, ++binfo_idx, base_binfo))
6797 field = base_binfo;
6798 else
6799 field = next_initializable_field (TYPE_FIELDS (type));
6800 }
6801 else
6802 field = next_initializable_field (DECL_CHAIN (field));
6803 }
6804
6805 /* A trailing aggregate element that is a pack expansion is assumed to
6806 correspond to all remaining elements of the initializer list (if any). */
6807 if (last_was_pack_expansion)
6808 {
6809 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init),
6810 last_was_pack_expansion, d->cur->value);
6811 while (d->cur != d->end)
6812 d->cur++;
6813 }
6814
6815 return new_init;
6816 }
6817
6818 /* Subroutine of reshape_init_r. We're in a context where C99 initializer
6819 designators are not valid; either complain or return true to indicate
6820 that reshape_init_r should return error_mark_node. */
6821
6822 static bool
has_designator_problem(reshape_iter * d,tsubst_flags_t complain)6823 has_designator_problem (reshape_iter *d, tsubst_flags_t complain)
6824 {
6825 if (d->cur->index)
6826 {
6827 if (complain & tf_error)
6828 error_at (cp_expr_loc_or_input_loc (d->cur->index),
6829 "C99 designator %qE outside aggregate initializer",
6830 d->cur->index);
6831 else
6832 return true;
6833 }
6834 return false;
6835 }
6836
6837 /* Subroutine of reshape_init, which processes a single initializer (part of
6838 a CONSTRUCTOR). TYPE is the type of the variable being initialized, D is the
6839 iterator within the CONSTRUCTOR which points to the initializer to process.
6840 If this is the first initializer of the outermost CONSTRUCTOR node,
6841 FIRST_INITIALIZER_P is that CONSTRUCTOR; otherwise, it is NULL_TREE. */
6842
6843 static tree
reshape_init_r(tree type,reshape_iter * d,tree first_initializer_p,tsubst_flags_t complain)6844 reshape_init_r (tree type, reshape_iter *d, tree first_initializer_p,
6845 tsubst_flags_t complain)
6846 {
6847 tree init = d->cur->value;
6848
6849 if (error_operand_p (init))
6850 return error_mark_node;
6851
6852 if (first_initializer_p && !CP_AGGREGATE_TYPE_P (type)
6853 && has_designator_problem (d, complain))
6854 return error_mark_node;
6855
6856 tree stripped_init = tree_strip_any_location_wrapper (init);
6857
6858 if (TREE_CODE (type) == COMPLEX_TYPE)
6859 {
6860 /* A complex type can be initialized from one or two initializers,
6861 but braces are not elided. */
6862 d->cur++;
6863 if (BRACE_ENCLOSED_INITIALIZER_P (stripped_init))
6864 {
6865 if (CONSTRUCTOR_NELTS (stripped_init) > 2)
6866 {
6867 if (complain & tf_error)
6868 error ("too many initializers for %qT", type);
6869 else
6870 return error_mark_node;
6871 }
6872 }
6873 else if (first_initializer_p && d->cur != d->end)
6874 {
6875 vec<constructor_elt, va_gc> *v = 0;
6876 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, init);
6877 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, d->cur->value);
6878 if (has_designator_problem (d, complain))
6879 return error_mark_node;
6880 d->cur++;
6881 init = build_constructor (init_list_type_node, v);
6882 }
6883 return init;
6884 }
6885
6886 /* A non-aggregate type is always initialized with a single
6887 initializer. */
6888 if (!CP_AGGREGATE_TYPE_P (type)
6889 /* As is an array with dependent bound, which we can see
6890 during C++20 aggregate CTAD. */
6891 || (cxx_dialect >= cxx20
6892 && TREE_CODE (type) == ARRAY_TYPE
6893 && uses_template_parms (TYPE_DOMAIN (type))))
6894 {
6895 /* It is invalid to initialize a non-aggregate type with a
6896 brace-enclosed initializer before C++0x.
6897 We need to check for BRACE_ENCLOSED_INITIALIZER_P here because
6898 of g++.old-deja/g++.mike/p7626.C: a pointer-to-member constant is
6899 a CONSTRUCTOR (with a record type). */
6900 if (TREE_CODE (stripped_init) == CONSTRUCTOR
6901 /* Don't complain about a capture-init. */
6902 && !CONSTRUCTOR_IS_DIRECT_INIT (stripped_init)
6903 && BRACE_ENCLOSED_INITIALIZER_P (stripped_init)) /* p7626.C */
6904 {
6905 if (SCALAR_TYPE_P (type))
6906 {
6907 if (cxx_dialect < cxx11)
6908 {
6909 if (complain & tf_error)
6910 error ("braces around scalar initializer for type %qT",
6911 type);
6912 init = error_mark_node;
6913 }
6914 else if (first_initializer_p
6915 || (CONSTRUCTOR_NELTS (stripped_init) > 0
6916 && (BRACE_ENCLOSED_INITIALIZER_P
6917 (CONSTRUCTOR_ELT (stripped_init,0)->value))))
6918 {
6919 if (complain & tf_error)
6920 error ("too many braces around scalar initializer "
6921 "for type %qT", type);
6922 init = error_mark_node;
6923 }
6924 }
6925 else
6926 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6927 }
6928
6929 d->cur++;
6930 return init;
6931 }
6932
6933 /* "If T is a class type and the initializer list has a single element of
6934 type cv U, where U is T or a class derived from T, the object is
6935 initialized from that element." Even if T is an aggregate. */
6936 if (cxx_dialect >= cxx11 && (CLASS_TYPE_P (type) || VECTOR_TYPE_P (type))
6937 && first_initializer_p
6938 /* But not if it's a designated init. */
6939 && !d->cur->index
6940 && d->end - d->cur == 1
6941 && reference_related_p (type, TREE_TYPE (init)))
6942 {
6943 d->cur++;
6944 return init;
6945 }
6946
6947 /* [dcl.init.aggr]
6948
6949 All implicit type conversions (clause _conv_) are considered when
6950 initializing the aggregate member with an initializer from an
6951 initializer-list. If the initializer can initialize a member,
6952 the member is initialized. Otherwise, if the member is itself a
6953 non-empty subaggregate, brace elision is assumed and the
6954 initializer is considered for the initialization of the first
6955 member of the subaggregate. */
6956 if ((TREE_CODE (init) != CONSTRUCTOR || COMPOUND_LITERAL_P (init))
6957 /* But don't try this for the first initializer, since that would be
6958 looking through the outermost braces; A a2 = { a1 }; is not a
6959 valid aggregate initialization. */
6960 && !first_initializer_p
6961 && (same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (init))
6962 || can_convert_arg (type, TREE_TYPE (init), init, LOOKUP_NORMAL,
6963 complain)))
6964 {
6965 d->cur++;
6966 return init;
6967 }
6968
6969 /* [dcl.init.string]
6970
6971 A char array (whether plain char, signed char, or unsigned char)
6972 can be initialized by a string-literal (optionally enclosed in
6973 braces); a wchar_t array can be initialized by a wide
6974 string-literal (optionally enclosed in braces). */
6975 if (TREE_CODE (type) == ARRAY_TYPE
6976 && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type))))
6977 {
6978 tree str_init = init;
6979 tree stripped_str_init = stripped_init;
6980 reshape_iter stripd = {};
6981
6982 /* Strip one level of braces if and only if they enclose a single
6983 element (as allowed by [dcl.init.string]). */
6984 if (!first_initializer_p
6985 && TREE_CODE (stripped_str_init) == CONSTRUCTOR
6986 && CONSTRUCTOR_NELTS (stripped_str_init) == 1)
6987 {
6988 stripd.cur = CONSTRUCTOR_ELT (stripped_str_init, 0);
6989 str_init = stripd.cur->value;
6990 stripped_str_init = tree_strip_any_location_wrapper (str_init);
6991 }
6992
6993 /* If it's a string literal, then it's the initializer for the array
6994 as a whole. Otherwise, continue with normal initialization for
6995 array types (one value per array element). */
6996 if (TREE_CODE (stripped_str_init) == STRING_CST)
6997 {
6998 if ((first_initializer_p && has_designator_problem (d, complain))
6999 || (stripd.cur && has_designator_problem (&stripd, complain)))
7000 return error_mark_node;
7001 d->cur++;
7002 return str_init;
7003 }
7004 }
7005
7006 /* The following cases are about aggregates. If we are not within a full
7007 initializer already, and there is not a CONSTRUCTOR, it means that there
7008 is a missing set of braces (that is, we are processing the case for
7009 which reshape_init exists). */
7010 bool braces_elided_p = false;
7011 if (!first_initializer_p)
7012 {
7013 if (TREE_CODE (stripped_init) == CONSTRUCTOR)
7014 {
7015 tree init_type = TREE_TYPE (init);
7016 if (init_type && TYPE_PTRMEMFUNC_P (init_type))
7017 /* There is no need to call reshape_init for pointer-to-member
7018 function initializers, as they are always constructed correctly
7019 by the front end. Here we have e.g. {.__pfn=0B, .__delta=0},
7020 which is missing outermost braces. We should warn below, and
7021 one of the routines below will wrap it in additional { }. */;
7022 /* For a nested compound literal, proceed to specialized routines,
7023 to handle initialization of arrays and similar. */
7024 else if (COMPOUND_LITERAL_P (stripped_init))
7025 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (stripped_init));
7026 /* If we have an unresolved designator, we need to find the member it
7027 designates within TYPE, so proceed to the routines below. For
7028 FIELD_DECL or INTEGER_CST designators, we're already initializing
7029 the designated element. */
7030 else if (d->cur->index
7031 && TREE_CODE (d->cur->index) == IDENTIFIER_NODE)
7032 /* Brace elision with designators is only permitted for anonymous
7033 aggregates. */
7034 gcc_checking_assert (ANON_AGGR_TYPE_P (type));
7035 /* A CONSTRUCTOR of the target's type is a previously
7036 digested initializer. */
7037 else if (same_type_ignoring_top_level_qualifiers_p (type, init_type))
7038 {
7039 ++d->cur;
7040 return init;
7041 }
7042 else
7043 {
7044 /* Something that hasn't been reshaped yet. */
7045 ++d->cur;
7046 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (stripped_init));
7047 return reshape_init (type, init, complain);
7048 }
7049 }
7050
7051 if (complain & tf_warning)
7052 warning (OPT_Wmissing_braces,
7053 "missing braces around initializer for %qT",
7054 type);
7055 braces_elided_p = true;
7056 }
7057
7058 /* Dispatch to specialized routines. */
7059 tree new_init;
7060 if (CLASS_TYPE_P (type))
7061 new_init = reshape_init_class (type, d, first_initializer_p, complain);
7062 else if (TREE_CODE (type) == ARRAY_TYPE)
7063 new_init = reshape_init_array (type, d, first_initializer_p, complain);
7064 else if (VECTOR_TYPE_P (type))
7065 new_init = reshape_init_vector (type, d, complain);
7066 else
7067 gcc_unreachable();
7068
7069 if (braces_elided_p
7070 && TREE_CODE (new_init) == CONSTRUCTOR)
7071 CONSTRUCTOR_BRACES_ELIDED_P (new_init) = true;
7072
7073 return new_init;
7074 }
7075
7076 /* Undo the brace-elision allowed by [dcl.init.aggr] in a
7077 brace-enclosed aggregate initializer.
7078
7079 INIT is the CONSTRUCTOR containing the list of initializers describing
7080 a brace-enclosed initializer for an entity of the indicated aggregate TYPE.
7081 It may not presently match the shape of the TYPE; for example:
7082
7083 struct S { int a; int b; };
7084 struct S a[] = { 1, 2, 3, 4 };
7085
7086 Here INIT will hold a vector of four elements, rather than a
7087 vector of two elements, each itself a vector of two elements. This
7088 routine transforms INIT from the former form into the latter. The
7089 revised CONSTRUCTOR node is returned. */
7090
7091 tree
reshape_init(tree type,tree init,tsubst_flags_t complain)7092 reshape_init (tree type, tree init, tsubst_flags_t complain)
7093 {
7094 vec<constructor_elt, va_gc> *v;
7095 reshape_iter d;
7096 tree new_init;
7097
7098 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
7099
7100 v = CONSTRUCTOR_ELTS (init);
7101
7102 /* An empty constructor does not need reshaping, and it is always a valid
7103 initializer. */
7104 if (vec_safe_is_empty (v))
7105 return init;
7106
7107 /* Brace elision is not performed for a CONSTRUCTOR representing
7108 parenthesized aggregate initialization. */
7109 if (CONSTRUCTOR_IS_PAREN_INIT (init))
7110 {
7111 tree elt = (*v)[0].value;
7112 /* If we're initializing a char array from a string-literal that is
7113 enclosed in braces, unwrap it here. */
7114 if (TREE_CODE (type) == ARRAY_TYPE
7115 && vec_safe_length (v) == 1
7116 && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type)))
7117 && TREE_CODE (tree_strip_any_location_wrapper (elt)) == STRING_CST)
7118 return elt;
7119 return init;
7120 }
7121
7122 /* Handle [dcl.init.list] direct-list-initialization from
7123 single element of enumeration with a fixed underlying type. */
7124 if (is_direct_enum_init (type, init))
7125 {
7126 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
7127 type = cv_unqualified (type);
7128 if (check_narrowing (ENUM_UNDERLYING_TYPE (type), elt, complain))
7129 {
7130 warning_sentinel w (warn_useless_cast);
7131 warning_sentinel w2 (warn_ignored_qualifiers);
7132 return cp_build_c_cast (input_location, type, elt,
7133 tf_warning_or_error);
7134 }
7135 else
7136 return error_mark_node;
7137 }
7138
7139 /* Recurse on this CONSTRUCTOR. */
7140 d.cur = &(*v)[0];
7141 d.end = d.cur + v->length ();
7142
7143 new_init = reshape_init_r (type, &d, init, complain);
7144 if (new_init == error_mark_node)
7145 return error_mark_node;
7146
7147 /* Make sure all the element of the constructor were used. Otherwise,
7148 issue an error about exceeding initializers. */
7149 if (d.cur != d.end)
7150 {
7151 if (complain & tf_error)
7152 error ("too many initializers for %qT", type);
7153 return error_mark_node;
7154 }
7155
7156 if (CONSTRUCTOR_IS_DIRECT_INIT (init)
7157 && BRACE_ENCLOSED_INITIALIZER_P (new_init))
7158 CONSTRUCTOR_IS_DIRECT_INIT (new_init) = true;
7159 if (CONSTRUCTOR_IS_DESIGNATED_INIT (init)
7160 && BRACE_ENCLOSED_INITIALIZER_P (new_init))
7161 CONSTRUCTOR_IS_DESIGNATED_INIT (new_init) = true;
7162
7163 return new_init;
7164 }
7165
7166 /* Verify array initializer. Returns true if errors have been reported. */
7167
7168 bool
check_array_initializer(tree decl,tree type,tree init)7169 check_array_initializer (tree decl, tree type, tree init)
7170 {
7171 tree element_type = TREE_TYPE (type);
7172
7173 /* Structured binding when initialized with an array type needs
7174 to have complete type. */
7175 if (decl
7176 && DECL_DECOMPOSITION_P (decl)
7177 && !DECL_DECOMP_BASE (decl)
7178 && !COMPLETE_TYPE_P (type))
7179 {
7180 error_at (DECL_SOURCE_LOCATION (decl),
7181 "structured binding has incomplete type %qT", type);
7182 TREE_TYPE (decl) = error_mark_node;
7183 return true;
7184 }
7185
7186 /* The array type itself need not be complete, because the
7187 initializer may tell us how many elements are in the array.
7188 But, the elements of the array must be complete. */
7189 if (!COMPLETE_TYPE_P (complete_type (element_type)))
7190 {
7191 if (decl)
7192 error_at (DECL_SOURCE_LOCATION (decl),
7193 "elements of array %q#D have incomplete type", decl);
7194 else
7195 error ("elements of array %q#T have incomplete type", type);
7196 return true;
7197 }
7198
7199 location_t loc = (decl ? location_of (decl) : input_location);
7200 if (!verify_type_context (loc, TCTX_ARRAY_ELEMENT, element_type))
7201 return true;
7202
7203 /* A compound literal can't have variable size. */
7204 if (init && !decl
7205 && ((COMPLETE_TYPE_P (type) && !TREE_CONSTANT (TYPE_SIZE (type)))
7206 || !TREE_CONSTANT (TYPE_SIZE (element_type))))
7207 {
7208 error ("variable-sized compound literal");
7209 return true;
7210 }
7211 return false;
7212 }
7213
7214 /* Subroutine of check_initializer; args are passed down from that function.
7215 Set stmts_are_full_exprs_p to 1 across a call to build_aggr_init. */
7216
7217 static tree
build_aggr_init_full_exprs(tree decl,tree init,int flags)7218 build_aggr_init_full_exprs (tree decl, tree init, int flags)
7219
7220 {
7221 gcc_assert (stmts_are_full_exprs_p ());
7222 return build_aggr_init (decl, init, flags, tf_warning_or_error);
7223 }
7224
7225 /* Verify INIT (the initializer for DECL), and record the
7226 initialization in DECL_INITIAL, if appropriate. CLEANUP is as for
7227 grok_reference_init.
7228
7229 If the return value is non-NULL, it is an expression that must be
7230 evaluated dynamically to initialize DECL. */
7231
7232 static tree
check_initializer(tree decl,tree init,int flags,vec<tree,va_gc> ** cleanups)7233 check_initializer (tree decl, tree init, int flags, vec<tree, va_gc> **cleanups)
7234 {
7235 tree type;
7236 tree init_code = NULL;
7237 tree core_type;
7238
7239 /* Things that are going to be initialized need to have complete
7240 type. */
7241 TREE_TYPE (decl) = type = complete_type (TREE_TYPE (decl));
7242
7243 if (DECL_HAS_VALUE_EXPR_P (decl))
7244 {
7245 /* A variable with DECL_HAS_VALUE_EXPR_P set is just a placeholder,
7246 it doesn't have storage to be initialized. */
7247 gcc_assert (init == NULL_TREE);
7248 return NULL_TREE;
7249 }
7250
7251 if (type == error_mark_node)
7252 /* We will have already complained. */
7253 return NULL_TREE;
7254
7255 if (TREE_CODE (type) == ARRAY_TYPE)
7256 {
7257 if (check_array_initializer (decl, type, init))
7258 return NULL_TREE;
7259 }
7260 else if (!COMPLETE_TYPE_P (type))
7261 {
7262 error_at (DECL_SOURCE_LOCATION (decl),
7263 "%q#D has incomplete type", decl);
7264 TREE_TYPE (decl) = error_mark_node;
7265 return NULL_TREE;
7266 }
7267 else
7268 /* There is no way to make a variable-sized class type in GNU C++. */
7269 gcc_assert (TREE_CONSTANT (TYPE_SIZE (type)));
7270
7271 if (init && BRACE_ENCLOSED_INITIALIZER_P (init))
7272 {
7273 int init_len = CONSTRUCTOR_NELTS (init);
7274 if (SCALAR_TYPE_P (type))
7275 {
7276 if (init_len == 0)
7277 {
7278 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7279 init = build_zero_init (type, NULL_TREE, false);
7280 }
7281 else if (init_len != 1 && TREE_CODE (type) != COMPLEX_TYPE)
7282 {
7283 error_at (cp_expr_loc_or_loc (init, DECL_SOURCE_LOCATION (decl)),
7284 "scalar object %qD requires one element in "
7285 "initializer", decl);
7286 TREE_TYPE (decl) = error_mark_node;
7287 return NULL_TREE;
7288 }
7289 }
7290 }
7291
7292 if (TREE_CODE (decl) == CONST_DECL)
7293 {
7294 gcc_assert (!TYPE_REF_P (type));
7295
7296 DECL_INITIAL (decl) = init;
7297
7298 gcc_assert (init != NULL_TREE);
7299 init = NULL_TREE;
7300 }
7301 else if (!init && DECL_REALLY_EXTERN (decl))
7302 ;
7303 else if (init || type_build_ctor_call (type)
7304 || TYPE_REF_P (type))
7305 {
7306 if (TYPE_REF_P (type))
7307 {
7308 init = grok_reference_init (decl, type, init, flags);
7309 flags |= LOOKUP_ALREADY_DIGESTED;
7310 }
7311 else if (!init)
7312 check_for_uninitialized_const_var (decl, /*constexpr_context_p=*/false,
7313 tf_warning_or_error);
7314 /* Do not reshape constructors of vectors (they don't need to be
7315 reshaped. */
7316 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
7317 {
7318 if (is_std_init_list (type))
7319 {
7320 init = perform_implicit_conversion (type, init,
7321 tf_warning_or_error);
7322 flags |= LOOKUP_ALREADY_DIGESTED;
7323 }
7324 else if (TYPE_NON_AGGREGATE_CLASS (type))
7325 {
7326 /* Don't reshape if the class has constructors. */
7327 if (cxx_dialect == cxx98)
7328 error_at (cp_expr_loc_or_loc (init, DECL_SOURCE_LOCATION (decl)),
7329 "in C++98 %qD must be initialized by "
7330 "constructor, not by %<{...}%>",
7331 decl);
7332 }
7333 else if (VECTOR_TYPE_P (type) && TYPE_VECTOR_OPAQUE (type))
7334 {
7335 error ("opaque vector types cannot be initialized");
7336 init = error_mark_node;
7337 }
7338 else
7339 {
7340 init = reshape_init (type, init, tf_warning_or_error);
7341 flags |= LOOKUP_NO_NARROWING;
7342 }
7343 }
7344 /* [dcl.init] "Otherwise, if the destination type is an array, the object
7345 is initialized as follows..." So handle things like
7346
7347 int a[](1, 2, 3);
7348
7349 which is permitted in C++20 by P0960. */
7350 else if (TREE_CODE (init) == TREE_LIST
7351 && TREE_TYPE (init) == NULL_TREE
7352 && TREE_CODE (type) == ARRAY_TYPE
7353 && !DECL_DECOMPOSITION_P (decl)
7354 && (cxx_dialect >= cxx20))
7355 init = do_aggregate_paren_init (init, type);
7356 else if (TREE_CODE (init) == TREE_LIST
7357 && TREE_TYPE (init) != unknown_type_node
7358 && !MAYBE_CLASS_TYPE_P (type))
7359 {
7360 gcc_assert (TREE_CODE (decl) != RESULT_DECL);
7361
7362 /* We get here with code like `int a (2);' */
7363 init = build_x_compound_expr_from_list (init, ELK_INIT,
7364 tf_warning_or_error);
7365 }
7366
7367 /* If DECL has an array type without a specific bound, deduce the
7368 array size from the initializer. */
7369 maybe_deduce_size_from_array_init (decl, init);
7370 type = TREE_TYPE (decl);
7371 if (type == error_mark_node)
7372 return NULL_TREE;
7373
7374 if (((type_build_ctor_call (type) || CLASS_TYPE_P (type))
7375 && !(flags & LOOKUP_ALREADY_DIGESTED)
7376 && !(init && BRACE_ENCLOSED_INITIALIZER_P (init)
7377 && CP_AGGREGATE_TYPE_P (type)
7378 && (CLASS_TYPE_P (type)
7379 /* The call to build_aggr_init below could end up
7380 calling build_vec_init, which may break when we
7381 are processing a template. */
7382 || processing_template_decl
7383 || !TYPE_NEEDS_CONSTRUCTING (type)
7384 || type_has_extended_temps (type))))
7385 || (DECL_DECOMPOSITION_P (decl) && TREE_CODE (type) == ARRAY_TYPE))
7386 {
7387 init_code = build_aggr_init_full_exprs (decl, init, flags);
7388
7389 /* A constructor call is a non-trivial initializer even if
7390 it isn't explicitly written. */
7391 if (TREE_SIDE_EFFECTS (init_code))
7392 DECL_NONTRIVIALLY_INITIALIZED_P (decl) = true;
7393
7394 /* If this is a constexpr initializer, expand_default_init will
7395 have returned an INIT_EXPR rather than a CALL_EXPR. In that
7396 case, pull the initializer back out and pass it down into
7397 store_init_value. */
7398 while (true)
7399 {
7400 if (TREE_CODE (init_code) == EXPR_STMT
7401 || TREE_CODE (init_code) == STMT_EXPR
7402 || TREE_CODE (init_code) == CONVERT_EXPR)
7403 init_code = TREE_OPERAND (init_code, 0);
7404 else if (TREE_CODE (init_code) == BIND_EXPR)
7405 init_code = BIND_EXPR_BODY (init_code);
7406 else
7407 break;
7408 }
7409 if (TREE_CODE (init_code) == INIT_EXPR)
7410 {
7411 /* In C++20, the call to build_aggr_init could have created
7412 an INIT_EXPR with a CONSTRUCTOR as the RHS to handle
7413 A(1, 2). */
7414 tree rhs = TREE_OPERAND (init_code, 1);
7415 if (processing_template_decl && TREE_CODE (rhs) == TARGET_EXPR)
7416 /* Avoid leaking TARGET_EXPR into template trees. */
7417 rhs = build_implicit_conv_flags (type, init, flags);
7418 init = rhs;
7419
7420 init_code = NULL_TREE;
7421 /* Don't call digest_init; it's unnecessary and will complain
7422 about aggregate initialization of non-aggregate classes. */
7423 flags |= LOOKUP_ALREADY_DIGESTED;
7424 }
7425 else if (DECL_DECLARED_CONSTEXPR_P (decl)
7426 || DECL_DECLARED_CONSTINIT_P (decl))
7427 {
7428 /* Declared constexpr or constinit, but no suitable initializer;
7429 massage init appropriately so we can pass it into
7430 store_init_value for the error. */
7431 tree new_init = NULL_TREE;
7432 if (!processing_template_decl
7433 && TREE_CODE (init_code) == CALL_EXPR)
7434 new_init = build_cplus_new (type, init_code, tf_none);
7435 else if (CLASS_TYPE_P (type)
7436 && (!init || TREE_CODE (init) == TREE_LIST))
7437 new_init = build_functional_cast (input_location, type,
7438 init, tf_none);
7439 if (new_init)
7440 {
7441 init = new_init;
7442 if (TREE_CODE (init) == TARGET_EXPR
7443 && !(flags & LOOKUP_ONLYCONVERTING))
7444 TARGET_EXPR_DIRECT_INIT_P (init) = true;
7445 }
7446 init_code = NULL_TREE;
7447 }
7448 else
7449 init = NULL_TREE;
7450 }
7451
7452 if (init && TREE_CODE (init) != TREE_VEC)
7453 {
7454 init_code = store_init_value (decl, init, cleanups, flags);
7455
7456 if (DECL_INITIAL (decl)
7457 && TREE_CODE (DECL_INITIAL (decl)) == CONSTRUCTOR
7458 && !vec_safe_is_empty (CONSTRUCTOR_ELTS (DECL_INITIAL (decl))))
7459 {
7460 tree elt = CONSTRUCTOR_ELTS (DECL_INITIAL (decl))->last ().value;
7461 if (TREE_CODE (TREE_TYPE (elt)) == ARRAY_TYPE
7462 && TYPE_SIZE (TREE_TYPE (elt)) == NULL_TREE)
7463 cp_complete_array_type (&TREE_TYPE (elt), elt, false);
7464 }
7465
7466 if (pedantic && TREE_CODE (type) == ARRAY_TYPE
7467 && DECL_INITIAL (decl)
7468 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
7469 && PAREN_STRING_LITERAL_P (DECL_INITIAL (decl)))
7470 warning_at (cp_expr_loc_or_loc (DECL_INITIAL (decl),
7471 DECL_SOURCE_LOCATION (decl)),
7472 0, "array %qD initialized by parenthesized "
7473 "string literal %qE",
7474 decl, DECL_INITIAL (decl));
7475 init = NULL_TREE;
7476 }
7477 }
7478 else
7479 {
7480 if (CLASS_TYPE_P (core_type = strip_array_types (type))
7481 && (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type)
7482 || CLASSTYPE_REF_FIELDS_NEED_INIT (core_type)))
7483 diagnose_uninitialized_cst_or_ref_member (core_type, /*using_new=*/false,
7484 /*complain=*/true);
7485
7486 check_for_uninitialized_const_var (decl, /*constexpr_context_p=*/false,
7487 tf_warning_or_error);
7488 }
7489
7490 if (init && init != error_mark_node)
7491 init_code = build2 (INIT_EXPR, type, decl, init);
7492
7493 if (init_code && !TREE_SIDE_EFFECTS (init_code)
7494 && init_code != error_mark_node)
7495 init_code = NULL_TREE;
7496
7497 if (init_code)
7498 {
7499 /* We might have set these in cp_finish_decl. */
7500 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = false;
7501 TREE_CONSTANT (decl) = false;
7502 }
7503
7504 if (init_code
7505 && DECL_IN_AGGR_P (decl)
7506 && DECL_INITIALIZED_IN_CLASS_P (decl))
7507 {
7508 static int explained = 0;
7509
7510 if (cxx_dialect < cxx11)
7511 error ("initializer invalid for static member with constructor");
7512 else if (cxx_dialect < cxx17)
7513 error ("non-constant in-class initialization invalid for static "
7514 "member %qD", decl);
7515 else
7516 error ("non-constant in-class initialization invalid for non-inline "
7517 "static member %qD", decl);
7518 if (!explained)
7519 {
7520 inform (input_location,
7521 "(an out of class initialization is required)");
7522 explained = 1;
7523 }
7524 return NULL_TREE;
7525 }
7526
7527 return init_code;
7528 }
7529
7530 /* If DECL is not a local variable, give it RTL. */
7531
7532 static void
make_rtl_for_nonlocal_decl(tree decl,tree init,const char * asmspec)7533 make_rtl_for_nonlocal_decl (tree decl, tree init, const char* asmspec)
7534 {
7535 int toplev = toplevel_bindings_p ();
7536 int defer_p;
7537
7538 /* Set the DECL_ASSEMBLER_NAME for the object. */
7539 if (asmspec)
7540 {
7541 /* The `register' keyword, when used together with an
7542 asm-specification, indicates that the variable should be
7543 placed in a particular register. */
7544 if (VAR_P (decl) && DECL_REGISTER (decl))
7545 {
7546 set_user_assembler_name (decl, asmspec);
7547 DECL_HARD_REGISTER (decl) = 1;
7548 }
7549 else
7550 {
7551 if (TREE_CODE (decl) == FUNCTION_DECL
7552 && fndecl_built_in_p (decl, BUILT_IN_NORMAL))
7553 set_builtin_user_assembler_name (decl, asmspec);
7554 set_user_assembler_name (decl, asmspec);
7555 if (DECL_LOCAL_DECL_P (decl))
7556 if (auto ns_decl = DECL_LOCAL_DECL_ALIAS (decl))
7557 /* We have to propagate the name to the ns-alias.
7558 This is horrible, as we're affecting a
7559 possibly-shared decl. Again, a one-true-decl
7560 model breaks down. */
7561 if (ns_decl != error_mark_node)
7562 set_user_assembler_name (ns_decl, asmspec);
7563 }
7564 }
7565
7566 /* Handle non-variables up front. */
7567 if (!VAR_P (decl))
7568 {
7569 rest_of_decl_compilation (decl, toplev, at_eof);
7570 return;
7571 }
7572
7573 /* If we see a class member here, it should be a static data
7574 member. */
7575 if (DECL_LANG_SPECIFIC (decl) && DECL_IN_AGGR_P (decl))
7576 {
7577 gcc_assert (TREE_STATIC (decl));
7578 /* An in-class declaration of a static data member should be
7579 external; it is only a declaration, and not a definition. */
7580 if (init == NULL_TREE)
7581 gcc_assert (DECL_EXTERNAL (decl)
7582 || !TREE_PUBLIC (decl));
7583 }
7584
7585 /* We don't create any RTL for local variables. */
7586 if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
7587 return;
7588
7589 /* We defer emission of local statics until the corresponding
7590 DECL_EXPR is expanded. But with constexpr its function might never
7591 be expanded, so go ahead and tell cgraph about the variable now. */
7592 defer_p = ((DECL_FUNCTION_SCOPE_P (decl)
7593 && !var_in_maybe_constexpr_fn (decl))
7594 || DECL_VIRTUAL_P (decl));
7595
7596 /* Defer template instantiations. */
7597 if (DECL_LANG_SPECIFIC (decl)
7598 && DECL_IMPLICIT_INSTANTIATION (decl))
7599 defer_p = 1;
7600
7601 /* If we're not deferring, go ahead and assemble the variable. */
7602 if (!defer_p)
7603 rest_of_decl_compilation (decl, toplev, at_eof);
7604 }
7605
7606 /* walk_tree helper for wrap_temporary_cleanups, below. */
7607
7608 static tree
wrap_cleanups_r(tree * stmt_p,int * walk_subtrees,void * data)7609 wrap_cleanups_r (tree *stmt_p, int *walk_subtrees, void *data)
7610 {
7611 /* Stop at types or full-expression boundaries. */
7612 if (TYPE_P (*stmt_p)
7613 || TREE_CODE (*stmt_p) == CLEANUP_POINT_EXPR)
7614 {
7615 *walk_subtrees = 0;
7616 return NULL_TREE;
7617 }
7618
7619 if (TREE_CODE (*stmt_p) == TARGET_EXPR)
7620 {
7621 tree guard = (tree)data;
7622 tree tcleanup = TARGET_EXPR_CLEANUP (*stmt_p);
7623
7624 if (tcleanup && !CLEANUP_EH_ONLY (*stmt_p)
7625 && !expr_noexcept_p (tcleanup, tf_none))
7626 {
7627 tcleanup = build2 (TRY_CATCH_EXPR, void_type_node, tcleanup, guard);
7628 /* Tell honor_protect_cleanup_actions to handle this as a separate
7629 cleanup. */
7630 TRY_CATCH_IS_CLEANUP (tcleanup) = 1;
7631 TARGET_EXPR_CLEANUP (*stmt_p) = tcleanup;
7632 }
7633 }
7634
7635 return NULL_TREE;
7636 }
7637
7638 /* We're initializing a local variable which has a cleanup GUARD. If there
7639 are any temporaries used in the initializer INIT of this variable, we
7640 need to wrap their cleanups with TRY_CATCH_EXPR (, GUARD) so that the
7641 variable will be cleaned up properly if one of them throws.
7642
7643 Unfortunately, there's no way to express this properly in terms of
7644 nesting, as the regions for the temporaries overlap the region for the
7645 variable itself; if there are two temporaries, the variable needs to be
7646 the first thing destroyed if either of them throws. However, we only
7647 want to run the variable's cleanup if it actually got constructed. So
7648 we need to guard the temporary cleanups with the variable's cleanup if
7649 they are run on the normal path, but not if they are run on the
7650 exceptional path. We implement this by telling
7651 honor_protect_cleanup_actions to strip the variable cleanup from the
7652 exceptional path.
7653
7654 Another approach could be to make the variable cleanup region enclose
7655 initialization, but depend on a flag to indicate that the variable is
7656 initialized; that's effectively what we do for arrays. But the current
7657 approach works fine for non-arrays, and has no code overhead in the usual
7658 case where the temporary destructors are noexcept. */
7659
7660 static void
wrap_temporary_cleanups(tree init,tree guard)7661 wrap_temporary_cleanups (tree init, tree guard)
7662 {
7663 if (TREE_CODE (guard) == BIND_EXPR)
7664 {
7665 /* An array cleanup region already encloses any temporary cleanups,
7666 don't wrap it around them again. */
7667 gcc_checking_assert (BIND_EXPR_VEC_DTOR (guard));
7668 return;
7669 }
7670 cp_walk_tree_without_duplicates (&init, wrap_cleanups_r, (void *)guard);
7671 }
7672
7673 /* Generate code to initialize DECL (a local variable). */
7674
7675 static void
initialize_local_var(tree decl,tree init)7676 initialize_local_var (tree decl, tree init)
7677 {
7678 tree type = TREE_TYPE (decl);
7679 tree cleanup;
7680 int already_used;
7681
7682 gcc_assert (VAR_P (decl)
7683 || TREE_CODE (decl) == RESULT_DECL);
7684 gcc_assert (!TREE_STATIC (decl));
7685
7686 if (DECL_SIZE (decl) == NULL_TREE)
7687 {
7688 /* If we used it already as memory, it must stay in memory. */
7689 DECL_INITIAL (decl) = NULL_TREE;
7690 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
7691 return;
7692 }
7693
7694 if (type == error_mark_node)
7695 return;
7696
7697 /* Compute and store the initial value. */
7698 already_used = TREE_USED (decl) || TREE_USED (type);
7699 if (TREE_USED (type))
7700 DECL_READ_P (decl) = 1;
7701
7702 /* Generate a cleanup, if necessary. */
7703 cleanup = cxx_maybe_build_cleanup (decl, tf_warning_or_error);
7704
7705 /* Perform the initialization. */
7706 if (init)
7707 {
7708 tree rinit = (TREE_CODE (init) == INIT_EXPR
7709 ? TREE_OPERAND (init, 1) : NULL_TREE);
7710 if (rinit && !TREE_SIDE_EFFECTS (rinit)
7711 && TREE_OPERAND (init, 0) == decl)
7712 {
7713 /* Stick simple initializers in DECL_INITIAL so that
7714 -Wno-init-self works (c++/34772). */
7715 DECL_INITIAL (decl) = rinit;
7716
7717 if (warn_init_self && TYPE_REF_P (type))
7718 {
7719 STRIP_NOPS (rinit);
7720 if (rinit == decl)
7721 warning_at (DECL_SOURCE_LOCATION (decl),
7722 OPT_Winit_self,
7723 "reference %qD is initialized with itself", decl);
7724 }
7725 }
7726 else
7727 {
7728 int saved_stmts_are_full_exprs_p;
7729
7730 /* If we're only initializing a single object, guard the
7731 destructors of any temporaries used in its initializer with
7732 its destructor. */
7733 if (cleanup)
7734 wrap_temporary_cleanups (init, cleanup);
7735
7736 gcc_assert (building_stmt_list_p ());
7737 saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
7738 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
7739 finish_expr_stmt (init);
7740 current_stmt_tree ()->stmts_are_full_exprs_p =
7741 saved_stmts_are_full_exprs_p;
7742 }
7743 }
7744
7745 /* Set this to 0 so we can tell whether an aggregate which was
7746 initialized was ever used. Don't do this if it has a
7747 destructor, so we don't complain about the 'resource
7748 allocation is initialization' idiom. Now set
7749 attribute((unused)) on types so decls of that type will be
7750 marked used. (see TREE_USED, above.) */
7751 if (TYPE_NEEDS_CONSTRUCTING (type)
7752 && ! already_used
7753 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type)
7754 && DECL_NAME (decl))
7755 TREE_USED (decl) = 0;
7756 else if (already_used)
7757 TREE_USED (decl) = 1;
7758
7759 if (cleanup)
7760 finish_decl_cleanup (decl, cleanup);
7761 }
7762
7763 /* DECL is a VAR_DECL for a compiler-generated variable with static
7764 storage duration (like a virtual table) whose initializer is a
7765 compile-time constant. Initialize the variable and provide it to the
7766 back end. */
7767
7768 void
initialize_artificial_var(tree decl,vec<constructor_elt,va_gc> * v)7769 initialize_artificial_var (tree decl, vec<constructor_elt, va_gc> *v)
7770 {
7771 tree init;
7772 gcc_assert (DECL_ARTIFICIAL (decl));
7773 init = build_constructor (TREE_TYPE (decl), v);
7774 gcc_assert (TREE_CODE (init) == CONSTRUCTOR);
7775 DECL_INITIAL (decl) = init;
7776 DECL_INITIALIZED_P (decl) = 1;
7777 /* Mark the decl as constexpr so that we can access its content
7778 at compile time. */
7779 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
7780 DECL_DECLARED_CONSTEXPR_P (decl) = true;
7781 determine_visibility (decl);
7782 layout_var_decl (decl);
7783 maybe_commonize_var (decl);
7784 make_rtl_for_nonlocal_decl (decl, init, /*asmspec=*/NULL);
7785 }
7786
7787 /* INIT is the initializer for a variable, as represented by the
7788 parser. Returns true iff INIT is value-dependent. */
7789
7790 static bool
value_dependent_init_p(tree init)7791 value_dependent_init_p (tree init)
7792 {
7793 if (TREE_CODE (init) == TREE_LIST)
7794 /* A parenthesized initializer, e.g.: int i (3, 2); ? */
7795 return any_value_dependent_elements_p (init);
7796 else if (TREE_CODE (init) == CONSTRUCTOR)
7797 /* A brace-enclosed initializer, e.g.: int i = { 3 }; ? */
7798 {
7799 if (dependent_type_p (TREE_TYPE (init)))
7800 return true;
7801
7802 vec<constructor_elt, va_gc> *elts;
7803 size_t nelts;
7804 size_t i;
7805
7806 elts = CONSTRUCTOR_ELTS (init);
7807 nelts = vec_safe_length (elts);
7808 for (i = 0; i < nelts; ++i)
7809 if (value_dependent_init_p ((*elts)[i].value))
7810 return true;
7811 }
7812 else
7813 /* It must be a simple expression, e.g., int i = 3; */
7814 return value_dependent_expression_p (init);
7815
7816 return false;
7817 }
7818
7819 // Returns true if a DECL is VAR_DECL with the concept specifier.
7820 static inline bool
is_concept_var(tree decl)7821 is_concept_var (tree decl)
7822 {
7823 return (VAR_P (decl)
7824 // Not all variables have DECL_LANG_SPECIFIC.
7825 && DECL_LANG_SPECIFIC (decl)
7826 && DECL_DECLARED_CONCEPT_P (decl));
7827 }
7828
7829 /* A helper function to be called via walk_tree. If any label exists
7830 under *TP, it is (going to be) forced. Set has_forced_label_in_static. */
7831
7832 static tree
notice_forced_label_r(tree * tp,int * walk_subtrees,void *)7833 notice_forced_label_r (tree *tp, int *walk_subtrees, void *)
7834 {
7835 if (TYPE_P (*tp))
7836 *walk_subtrees = 0;
7837 if (TREE_CODE (*tp) == LABEL_DECL)
7838 cfun->has_forced_label_in_static = 1;
7839 return NULL_TREE;
7840 }
7841
7842 /* Return true if DECL has either a trivial destructor, or for C++20
7843 is constexpr and has a constexpr destructor. */
7844
7845 static bool
decl_maybe_constant_destruction(tree decl,tree type)7846 decl_maybe_constant_destruction (tree decl, tree type)
7847 {
7848 return (TYPE_HAS_TRIVIAL_DESTRUCTOR (type)
7849 || (cxx_dialect >= cxx20
7850 && VAR_P (decl)
7851 && DECL_DECLARED_CONSTEXPR_P (decl)
7852 && type_has_constexpr_destructor (strip_array_types (type))));
7853 }
7854
7855 static tree declare_simd_adjust_this (tree *, int *, void *);
7856
7857 /* Helper function of omp_declare_variant_finalize. Finalize one
7858 "omp declare variant base" attribute. Return true if it should be
7859 removed. */
7860
7861 static bool
omp_declare_variant_finalize_one(tree decl,tree attr)7862 omp_declare_variant_finalize_one (tree decl, tree attr)
7863 {
7864 if (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
7865 {
7866 walk_tree (&TREE_VALUE (TREE_VALUE (attr)), declare_simd_adjust_this,
7867 DECL_ARGUMENTS (decl), NULL);
7868 walk_tree (&TREE_PURPOSE (TREE_VALUE (attr)), declare_simd_adjust_this,
7869 DECL_ARGUMENTS (decl), NULL);
7870 }
7871
7872 tree ctx = TREE_VALUE (TREE_VALUE (attr));
7873 tree simd = omp_get_context_selector (ctx, "construct", "simd");
7874 if (simd)
7875 {
7876 TREE_VALUE (simd)
7877 = c_omp_declare_simd_clauses_to_numbers (DECL_ARGUMENTS (decl),
7878 TREE_VALUE (simd));
7879 /* FIXME, adjusting simd args unimplemented. */
7880 return true;
7881 }
7882
7883 tree chain = TREE_CHAIN (TREE_VALUE (attr));
7884 location_t varid_loc
7885 = cp_expr_loc_or_input_loc (TREE_PURPOSE (TREE_CHAIN (chain)));
7886 location_t match_loc = cp_expr_loc_or_input_loc (TREE_PURPOSE (chain));
7887 cp_id_kind idk = (cp_id_kind) tree_to_uhwi (TREE_VALUE (chain));
7888 tree variant = TREE_PURPOSE (TREE_VALUE (attr));
7889
7890 location_t save_loc = input_location;
7891 input_location = varid_loc;
7892
7893 releasing_vec args;
7894 tree parm = DECL_ARGUMENTS (decl);
7895 if (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
7896 parm = DECL_CHAIN (parm);
7897 for (; parm; parm = DECL_CHAIN (parm))
7898 if (type_dependent_expression_p (parm))
7899 vec_safe_push (args, build_constructor (TREE_TYPE (parm), NULL));
7900 else if (MAYBE_CLASS_TYPE_P (TREE_TYPE (parm)))
7901 vec_safe_push (args, build_local_temp (TREE_TYPE (parm)));
7902 else
7903 vec_safe_push (args, build_zero_cst (TREE_TYPE (parm)));
7904
7905 bool koenig_p = false;
7906 if (idk == CP_ID_KIND_UNQUALIFIED || idk == CP_ID_KIND_TEMPLATE_ID)
7907 {
7908 if (identifier_p (variant)
7909 /* In C++20, we may need to perform ADL for a template
7910 name. */
7911 || (TREE_CODE (variant) == TEMPLATE_ID_EXPR
7912 && identifier_p (TREE_OPERAND (variant, 0))))
7913 {
7914 if (!args->is_empty ())
7915 {
7916 koenig_p = true;
7917 if (!any_type_dependent_arguments_p (args))
7918 variant = perform_koenig_lookup (variant, args,
7919 tf_warning_or_error);
7920 }
7921 else
7922 variant = unqualified_fn_lookup_error (variant);
7923 }
7924 else if (!args->is_empty () && is_overloaded_fn (variant))
7925 {
7926 tree fn = get_first_fn (variant);
7927 fn = STRIP_TEMPLATE (fn);
7928 if (!((TREE_CODE (fn) == USING_DECL && DECL_DEPENDENT_P (fn))
7929 || DECL_FUNCTION_MEMBER_P (fn)
7930 || DECL_LOCAL_DECL_P (fn)))
7931 {
7932 koenig_p = true;
7933 if (!any_type_dependent_arguments_p (args))
7934 variant = perform_koenig_lookup (variant, args,
7935 tf_warning_or_error);
7936 }
7937 }
7938 }
7939
7940 if (idk == CP_ID_KIND_QUALIFIED)
7941 variant = finish_call_expr (variant, &args, /*disallow_virtual=*/true,
7942 koenig_p, tf_warning_or_error);
7943 else
7944 variant = finish_call_expr (variant, &args, /*disallow_virtual=*/false,
7945 koenig_p, tf_warning_or_error);
7946 if (variant == error_mark_node && !processing_template_decl)
7947 return true;
7948
7949 variant = cp_get_callee_fndecl_nofold (variant);
7950 input_location = save_loc;
7951
7952 if (variant)
7953 {
7954 const char *varname = IDENTIFIER_POINTER (DECL_NAME (variant));
7955 if (!comptypes (TREE_TYPE (decl), TREE_TYPE (variant), 0))
7956 {
7957 error_at (varid_loc, "variant %qD and base %qD have incompatible "
7958 "types", variant, decl);
7959 return true;
7960 }
7961 if (fndecl_built_in_p (variant)
7962 && (startswith (varname, "__builtin_")
7963 || startswith (varname, "__sync_")
7964 || startswith (varname, "__atomic_")))
7965 {
7966 error_at (varid_loc, "variant %qD is a built-in", variant);
7967 return true;
7968 }
7969 else
7970 {
7971 tree construct = omp_get_context_selector (ctx, "construct", NULL);
7972 omp_mark_declare_variant (match_loc, variant, construct);
7973 if (!omp_context_selector_matches (ctx))
7974 return true;
7975 TREE_PURPOSE (TREE_VALUE (attr)) = variant;
7976 }
7977 }
7978 else if (!processing_template_decl)
7979 {
7980 error_at (varid_loc, "could not find variant declaration");
7981 return true;
7982 }
7983
7984 return false;
7985 }
7986
7987 /* Helper function, finish up "omp declare variant base" attribute
7988 now that there is a DECL. ATTR is the first "omp declare variant base"
7989 attribute. */
7990
7991 void
omp_declare_variant_finalize(tree decl,tree attr)7992 omp_declare_variant_finalize (tree decl, tree attr)
7993 {
7994 size_t attr_len = strlen ("omp declare variant base");
7995 tree *list = &DECL_ATTRIBUTES (decl);
7996 bool remove_all = false;
7997 location_t match_loc = DECL_SOURCE_LOCATION (decl);
7998 if (TREE_CHAIN (TREE_VALUE (attr))
7999 && TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr)))
8000 && EXPR_HAS_LOCATION (TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr)))))
8001 match_loc = EXPR_LOCATION (TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr))));
8002 if (DECL_CONSTRUCTOR_P (decl))
8003 {
8004 error_at (match_loc, "%<declare variant%> on constructor %qD", decl);
8005 remove_all = true;
8006 }
8007 else if (DECL_DESTRUCTOR_P (decl))
8008 {
8009 error_at (match_loc, "%<declare variant%> on destructor %qD", decl);
8010 remove_all = true;
8011 }
8012 else if (DECL_DEFAULTED_FN (decl))
8013 {
8014 error_at (match_loc, "%<declare variant%> on defaulted %qD", decl);
8015 remove_all = true;
8016 }
8017 else if (DECL_DELETED_FN (decl))
8018 {
8019 error_at (match_loc, "%<declare variant%> on deleted %qD", decl);
8020 remove_all = true;
8021 }
8022 else if (DECL_VIRTUAL_P (decl))
8023 {
8024 error_at (match_loc, "%<declare variant%> on virtual %qD", decl);
8025 remove_all = true;
8026 }
8027 /* This loop is like private_lookup_attribute, except that it works
8028 with tree * rather than tree, as we might want to remove the
8029 attributes that are diagnosed as errorneous. */
8030 while (*list)
8031 {
8032 tree attr = get_attribute_name (*list);
8033 size_t ident_len = IDENTIFIER_LENGTH (attr);
8034 if (cmp_attribs ("omp declare variant base", attr_len,
8035 IDENTIFIER_POINTER (attr), ident_len))
8036 {
8037 if (remove_all || omp_declare_variant_finalize_one (decl, *list))
8038 {
8039 *list = TREE_CHAIN (*list);
8040 continue;
8041 }
8042 }
8043 list = &TREE_CHAIN (*list);
8044 }
8045 }
8046
8047 /* Finish processing of a declaration;
8048 install its line number and initial value.
8049 If the length of an array type is not known before,
8050 it must be determined now, from the initial value, or it is an error.
8051
8052 INIT is the initializer (if any) for DECL. If INIT_CONST_EXPR_P is
8053 true, then INIT is an integral constant expression.
8054
8055 FLAGS is LOOKUP_ONLYCONVERTING if the = init syntax was used, else 0
8056 if the (init) syntax was used. */
8057
8058 void
cp_finish_decl(tree decl,tree init,bool init_const_expr_p,tree asmspec_tree,int flags)8059 cp_finish_decl (tree decl, tree init, bool init_const_expr_p,
8060 tree asmspec_tree, int flags)
8061 {
8062 tree type;
8063 vec<tree, va_gc> *cleanups = NULL;
8064 const char *asmspec = NULL;
8065 int was_readonly = 0;
8066 bool var_definition_p = false;
8067 tree auto_node;
8068
8069 if (decl == error_mark_node)
8070 return;
8071 else if (! decl)
8072 {
8073 if (init)
8074 error ("assignment (not initialization) in declaration");
8075 return;
8076 }
8077
8078 gcc_assert (TREE_CODE (decl) != RESULT_DECL);
8079 /* Parameters are handled by store_parm_decls, not cp_finish_decl. */
8080 gcc_assert (TREE_CODE (decl) != PARM_DECL);
8081
8082 type = TREE_TYPE (decl);
8083 if (type == error_mark_node)
8084 return;
8085
8086 if (VAR_P (decl) && is_copy_initialization (init))
8087 flags |= LOOKUP_ONLYCONVERTING;
8088
8089 /* Warn about register storage specifiers except when in GNU global
8090 or local register variable extension. */
8091 if (VAR_P (decl) && DECL_REGISTER (decl) && asmspec_tree == NULL_TREE)
8092 {
8093 if (cxx_dialect >= cxx17)
8094 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
8095 "ISO C++17 does not allow %<register%> storage "
8096 "class specifier");
8097 else
8098 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
8099 "%<register%> storage class specifier used");
8100 }
8101
8102 /* If a name was specified, get the string. */
8103 if (at_namespace_scope_p ())
8104 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
8105 if (asmspec_tree && asmspec_tree != error_mark_node)
8106 asmspec = TREE_STRING_POINTER (asmspec_tree);
8107
8108 bool in_class_decl
8109 = (current_class_type
8110 && CP_DECL_CONTEXT (decl) == current_class_type
8111 && TYPE_BEING_DEFINED (current_class_type)
8112 && !CLASSTYPE_TEMPLATE_INSTANTIATION (current_class_type));
8113
8114 if (in_class_decl
8115 && (DECL_INITIAL (decl) || init))
8116 DECL_INITIALIZED_IN_CLASS_P (decl) = 1;
8117
8118 if (TREE_CODE (decl) != FUNCTION_DECL
8119 && (auto_node = type_uses_auto (type)))
8120 {
8121 tree d_init;
8122 if (init == NULL_TREE)
8123 {
8124 if (DECL_LANG_SPECIFIC (decl)
8125 && DECL_TEMPLATE_INSTANTIATION (decl)
8126 && !DECL_TEMPLATE_INSTANTIATED (decl))
8127 {
8128 /* init is null because we're deferring instantiating the
8129 initializer until we need it. Well, we need it now. */
8130 instantiate_decl (decl, /*defer_ok*/true, /*expl*/false);
8131 return;
8132 }
8133
8134 gcc_assert (CLASS_PLACEHOLDER_TEMPLATE (auto_node));
8135 }
8136 d_init = init;
8137 if (d_init)
8138 {
8139 if (TREE_CODE (d_init) == TREE_LIST
8140 && !CLASS_PLACEHOLDER_TEMPLATE (auto_node))
8141 d_init = build_x_compound_expr_from_list (d_init, ELK_INIT,
8142 tf_warning_or_error);
8143 d_init = resolve_nondeduced_context (d_init, tf_warning_or_error);
8144 /* Force auto deduction now. Use tf_none to avoid redundant warnings
8145 on deprecated-14.C. */
8146 mark_single_function (d_init, tf_none);
8147 }
8148 enum auto_deduction_context adc = adc_variable_type;
8149 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
8150 adc = adc_decomp_type;
8151 tree outer_targs = NULL_TREE;
8152 if (PLACEHOLDER_TYPE_CONSTRAINTS_INFO (auto_node)
8153 && VAR_P (decl)
8154 && DECL_LANG_SPECIFIC (decl)
8155 && DECL_TEMPLATE_INFO (decl)
8156 && !DECL_FUNCTION_SCOPE_P (decl))
8157 /* The outer template arguments might be needed for satisfaction.
8158 (For function scope variables, do_auto_deduction will obtain the
8159 outer template arguments from current_function_decl.) */
8160 outer_targs = DECL_TI_ARGS (decl);
8161 type = TREE_TYPE (decl) = do_auto_deduction (type, d_init, auto_node,
8162 tf_warning_or_error, adc,
8163 outer_targs, flags);
8164 if (type == error_mark_node)
8165 return;
8166 if (TREE_CODE (type) == FUNCTION_TYPE)
8167 {
8168 error ("initializer for %<decltype(auto) %D%> has function type; "
8169 "did you forget the %<()%>?", decl);
8170 TREE_TYPE (decl) = error_mark_node;
8171 return;
8172 }
8173 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
8174 }
8175
8176 if (ensure_literal_type_for_constexpr_object (decl) == error_mark_node)
8177 {
8178 DECL_DECLARED_CONSTEXPR_P (decl) = 0;
8179 if (VAR_P (decl) && DECL_CLASS_SCOPE_P (decl))
8180 {
8181 init = NULL_TREE;
8182 DECL_EXTERNAL (decl) = 1;
8183 }
8184 }
8185
8186 if (VAR_P (decl)
8187 && DECL_CLASS_SCOPE_P (decl)
8188 && verify_type_context (DECL_SOURCE_LOCATION (decl),
8189 TCTX_STATIC_STORAGE, type)
8190 && DECL_INITIALIZED_IN_CLASS_P (decl))
8191 check_static_variable_definition (decl, type);
8192
8193 if (!processing_template_decl && VAR_P (decl) && is_global_var (decl))
8194 {
8195 type_context_kind context = (DECL_THREAD_LOCAL_P (decl)
8196 ? TCTX_THREAD_STORAGE
8197 : TCTX_STATIC_STORAGE);
8198 verify_type_context (input_location, context, TREE_TYPE (decl));
8199 }
8200
8201 if (init && TREE_CODE (decl) == FUNCTION_DECL)
8202 {
8203 tree clone;
8204 if (init == ridpointers[(int)RID_DELETE])
8205 {
8206 /* FIXME check this is 1st decl. */
8207 DECL_DELETED_FN (decl) = 1;
8208 DECL_DECLARED_INLINE_P (decl) = 1;
8209 DECL_INITIAL (decl) = error_mark_node;
8210 FOR_EACH_CLONE (clone, decl)
8211 {
8212 DECL_DELETED_FN (clone) = 1;
8213 DECL_DECLARED_INLINE_P (clone) = 1;
8214 DECL_INITIAL (clone) = error_mark_node;
8215 }
8216 init = NULL_TREE;
8217 }
8218 else if (init == ridpointers[(int)RID_DEFAULT])
8219 {
8220 if (defaultable_fn_check (decl))
8221 DECL_DEFAULTED_FN (decl) = 1;
8222 else
8223 DECL_INITIAL (decl) = NULL_TREE;
8224 }
8225 }
8226
8227 if (init && VAR_P (decl))
8228 {
8229 DECL_NONTRIVIALLY_INITIALIZED_P (decl) = 1;
8230 /* If DECL is a reference, then we want to know whether init is a
8231 reference constant; init_const_expr_p as passed tells us whether
8232 it's an rvalue constant. */
8233 if (TYPE_REF_P (type))
8234 init_const_expr_p = potential_constant_expression (init);
8235 if (init_const_expr_p)
8236 {
8237 /* Set these flags now for templates. We'll update the flags in
8238 store_init_value for instantiations. */
8239 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
8240 if (decl_maybe_constant_var_p (decl)
8241 /* FIXME setting TREE_CONSTANT on refs breaks the back end. */
8242 && !TYPE_REF_P (type))
8243 TREE_CONSTANT (decl) = 1;
8244 }
8245 /* This is handled mostly by gimplify.cc, but we have to deal with
8246 not warning about int x = x; as it is a GCC extension to turn off
8247 this warning but only if warn_init_self is zero. */
8248 if (!DECL_EXTERNAL (decl)
8249 && !TREE_STATIC (decl)
8250 && decl == tree_strip_any_location_wrapper (init)
8251 && !warning_enabled_at (DECL_SOURCE_LOCATION (decl), OPT_Winit_self))
8252 suppress_warning (decl, OPT_Winit_self);
8253 }
8254
8255 if (flag_openmp
8256 && TREE_CODE (decl) == FUNCTION_DECL
8257 /* #pragma omp declare variant on methods handled in finish_struct
8258 instead. */
8259 && (!DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
8260 || COMPLETE_TYPE_P (DECL_CONTEXT (decl))))
8261 if (tree attr = lookup_attribute ("omp declare variant base",
8262 DECL_ATTRIBUTES (decl)))
8263 omp_declare_variant_finalize (decl, attr);
8264
8265 if (processing_template_decl)
8266 {
8267 bool type_dependent_p;
8268
8269 /* Add this declaration to the statement-tree. */
8270 if (at_function_scope_p ())
8271 add_decl_expr (decl);
8272
8273 type_dependent_p = dependent_type_p (type);
8274
8275 if (check_for_bare_parameter_packs (init))
8276 {
8277 init = NULL_TREE;
8278 DECL_INITIAL (decl) = NULL_TREE;
8279 }
8280
8281 /* Generally, initializers in templates are expanded when the
8282 template is instantiated. But, if DECL is a variable constant
8283 then it can be used in future constant expressions, so its value
8284 must be available. */
8285
8286 bool dep_init = false;
8287
8288 if (!VAR_P (decl) || type_dependent_p)
8289 /* We can't do anything if the decl has dependent type. */;
8290 else if (!init && is_concept_var (decl))
8291 {
8292 error ("variable concept has no initializer");
8293 init = boolean_true_node;
8294 }
8295 else if (init
8296 && (init_const_expr_p || DECL_DECLARED_CONSTEXPR_P (decl))
8297 && !TYPE_REF_P (type)
8298 && decl_maybe_constant_var_p (decl)
8299 && !(dep_init = value_dependent_init_p (init)))
8300 {
8301 /* This variable seems to be a non-dependent constant, so process
8302 its initializer. If check_initializer returns non-null the
8303 initialization wasn't constant after all. */
8304 tree init_code;
8305 cleanups = make_tree_vector ();
8306 init_code = check_initializer (decl, init, flags, &cleanups);
8307 if (init_code == NULL_TREE)
8308 init = NULL_TREE;
8309 release_tree_vector (cleanups);
8310 }
8311 else
8312 {
8313 gcc_assert (!DECL_PRETTY_FUNCTION_P (decl));
8314 /* Try to deduce array size. */
8315 maybe_deduce_size_from_array_init (decl, init);
8316 /* And complain about multiple initializers. */
8317 if (init && TREE_CODE (init) == TREE_LIST && TREE_CHAIN (init)
8318 && !MAYBE_CLASS_TYPE_P (type))
8319 init = build_x_compound_expr_from_list (init, ELK_INIT,
8320 tf_warning_or_error);
8321 }
8322
8323 if (init)
8324 DECL_INITIAL (decl) = init;
8325
8326 if (dep_init)
8327 {
8328 retrofit_lang_decl (decl);
8329 SET_DECL_DEPENDENT_INIT_P (decl, true);
8330 }
8331
8332 if (VAR_P (decl) && DECL_REGISTER (decl) && asmspec)
8333 {
8334 set_user_assembler_name (decl, asmspec);
8335 DECL_HARD_REGISTER (decl) = 1;
8336 }
8337 return;
8338 }
8339
8340 /* Just store non-static data member initializers for later. */
8341 if (init && TREE_CODE (decl) == FIELD_DECL)
8342 DECL_INITIAL (decl) = init;
8343
8344 /* Take care of TYPE_DECLs up front. */
8345 if (TREE_CODE (decl) == TYPE_DECL)
8346 {
8347 if (type != error_mark_node
8348 && MAYBE_CLASS_TYPE_P (type) && DECL_NAME (decl))
8349 {
8350 if (TREE_TYPE (DECL_NAME (decl)) && TREE_TYPE (decl) != type)
8351 warning (0, "shadowing previous type declaration of %q#D", decl);
8352 set_identifier_type_value (DECL_NAME (decl), decl);
8353 }
8354
8355 /* If we have installed this as the canonical typedef for this
8356 type, and that type has not been defined yet, delay emitting
8357 the debug information for it, as we will emit it later. */
8358 if (TYPE_MAIN_DECL (TREE_TYPE (decl)) == decl
8359 && !COMPLETE_TYPE_P (TREE_TYPE (decl)))
8360 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
8361
8362 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl),
8363 at_eof);
8364 return;
8365 }
8366
8367 /* A reference will be modified here, as it is initialized. */
8368 if (! DECL_EXTERNAL (decl)
8369 && TREE_READONLY (decl)
8370 && TYPE_REF_P (type))
8371 {
8372 was_readonly = 1;
8373 TREE_READONLY (decl) = 0;
8374 }
8375
8376 /* This needs to happen before extend_ref_init_temps. */
8377 if (VAR_OR_FUNCTION_DECL_P (decl))
8378 {
8379 if (VAR_P (decl))
8380 maybe_commonize_var (decl);
8381 determine_visibility (decl);
8382 }
8383
8384 if (VAR_P (decl))
8385 {
8386 duration_kind dk = decl_storage_duration (decl);
8387 /* [dcl.constinit]/1 "The constinit specifier shall be applied
8388 only to a declaration of a variable with static or thread storage
8389 duration." */
8390 if (DECL_DECLARED_CONSTINIT_P (decl)
8391 && !(dk == dk_thread || dk == dk_static))
8392 {
8393 error_at (DECL_SOURCE_LOCATION (decl),
8394 "%<constinit%> can only be applied to a variable with "
8395 "static or thread storage duration");
8396 return;
8397 }
8398
8399 /* If this is a local variable that will need a mangled name,
8400 register it now. We must do this before processing the
8401 initializer for the variable, since the initialization might
8402 require a guard variable, and since the mangled name of the
8403 guard variable will depend on the mangled name of this
8404 variable. */
8405 if (DECL_FUNCTION_SCOPE_P (decl)
8406 && TREE_STATIC (decl)
8407 && !DECL_ARTIFICIAL (decl))
8408 {
8409 /* The variable holding an anonymous union will have had its
8410 discriminator set in finish_anon_union, after which it's
8411 NAME will have been cleared. */
8412 if (DECL_NAME (decl))
8413 determine_local_discriminator (decl);
8414 /* Normally has_forced_label_in_static is set during GIMPLE
8415 lowering, but [cd]tors are never actually compiled directly.
8416 We need to set this early so we can deal with the label
8417 address extension. */
8418 if ((DECL_CONSTRUCTOR_P (current_function_decl)
8419 || DECL_DESTRUCTOR_P (current_function_decl))
8420 && init)
8421 {
8422 walk_tree (&init, notice_forced_label_r, NULL, NULL);
8423 add_local_decl (cfun, decl);
8424 }
8425 /* And make sure it's in the symbol table for
8426 c_parse_final_cleanups to find. */
8427 varpool_node::get_create (decl);
8428 }
8429
8430 /* Convert the initializer to the type of DECL, if we have not
8431 already initialized DECL. */
8432 if (!DECL_INITIALIZED_P (decl)
8433 /* If !DECL_EXTERNAL then DECL is being defined. In the
8434 case of a static data member initialized inside the
8435 class-specifier, there can be an initializer even if DECL
8436 is *not* defined. */
8437 && (!DECL_EXTERNAL (decl) || init))
8438 {
8439 cleanups = make_tree_vector ();
8440 init = check_initializer (decl, init, flags, &cleanups);
8441
8442 /* Handle:
8443
8444 [dcl.init]
8445
8446 The memory occupied by any object of static storage
8447 duration is zero-initialized at program startup before
8448 any other initialization takes place.
8449
8450 We cannot create an appropriate initializer until after
8451 the type of DECL is finalized. If DECL_INITIAL is set,
8452 then the DECL is statically initialized, and any
8453 necessary zero-initialization has already been performed. */
8454 if (TREE_STATIC (decl) && !DECL_INITIAL (decl))
8455 DECL_INITIAL (decl) = build_zero_init (TREE_TYPE (decl),
8456 /*nelts=*/NULL_TREE,
8457 /*static_storage_p=*/true);
8458 /* Remember that the initialization for this variable has
8459 taken place. */
8460 DECL_INITIALIZED_P (decl) = 1;
8461 /* This declaration is the definition of this variable,
8462 unless we are initializing a static data member within
8463 the class specifier. */
8464 if (!DECL_EXTERNAL (decl))
8465 var_definition_p = true;
8466 }
8467 /* If the variable has an array type, lay out the type, even if
8468 there is no initializer. It is valid to index through the
8469 array, and we must get TYPE_ALIGN set correctly on the array
8470 type. */
8471 else if (TREE_CODE (type) == ARRAY_TYPE)
8472 layout_type (type);
8473
8474 if (TREE_STATIC (decl)
8475 && !at_function_scope_p ()
8476 && current_function_decl == NULL)
8477 /* So decl is a global variable or a static member of a
8478 non local class. Record the types it uses
8479 so that we can decide later to emit debug info for them. */
8480 record_types_used_by_current_var_decl (decl);
8481 }
8482
8483 /* Add this declaration to the statement-tree. This needs to happen
8484 after the call to check_initializer so that the DECL_EXPR for a
8485 reference temp is added before the DECL_EXPR for the reference itself. */
8486 if (DECL_FUNCTION_SCOPE_P (decl))
8487 {
8488 /* If we're building a variable sized type, and we might be
8489 reachable other than via the top of the current binding
8490 level, then create a new BIND_EXPR so that we deallocate
8491 the object at the right time. */
8492 if (VAR_P (decl)
8493 && DECL_SIZE (decl)
8494 && !TREE_CONSTANT (DECL_SIZE (decl))
8495 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
8496 {
8497 tree bind;
8498 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
8499 TREE_SIDE_EFFECTS (bind) = 1;
8500 add_stmt (bind);
8501 BIND_EXPR_BODY (bind) = push_stmt_list ();
8502 }
8503 add_decl_expr (decl);
8504 }
8505
8506 /* Let the middle end know about variables and functions -- but not
8507 static data members in uninstantiated class templates. */
8508 if (VAR_OR_FUNCTION_DECL_P (decl))
8509 {
8510 if (VAR_P (decl))
8511 {
8512 layout_var_decl (decl);
8513 if (!flag_weak)
8514 /* Check again now that we have an initializer. */
8515 maybe_commonize_var (decl);
8516 /* A class-scope constexpr variable with an out-of-class declaration.
8517 C++17 makes them implicitly inline, but still force it out. */
8518 if (DECL_INLINE_VAR_P (decl)
8519 && !DECL_VAR_DECLARED_INLINE_P (decl)
8520 && !DECL_TEMPLATE_INSTANTIATION (decl)
8521 && !in_class_decl)
8522 mark_needed (decl);
8523 }
8524
8525 if (var_definition_p
8526 /* With -fmerge-all-constants, gimplify_init_constructor
8527 might add TREE_STATIC to aggregate variables. */
8528 && (TREE_STATIC (decl)
8529 || (flag_merge_constants >= 2
8530 && AGGREGATE_TYPE_P (type))))
8531 {
8532 /* If a TREE_READONLY variable needs initialization
8533 at runtime, it is no longer readonly and we need to
8534 avoid MEM_READONLY_P being set on RTL created for it. */
8535 if (init)
8536 {
8537 if (TREE_READONLY (decl))
8538 TREE_READONLY (decl) = 0;
8539 was_readonly = 0;
8540 }
8541 else if (was_readonly)
8542 TREE_READONLY (decl) = 1;
8543
8544 /* Likewise if it needs destruction. */
8545 if (!decl_maybe_constant_destruction (decl, type))
8546 TREE_READONLY (decl) = 0;
8547 }
8548 else if (VAR_P (decl)
8549 && CP_DECL_THREAD_LOCAL_P (decl)
8550 && (!DECL_EXTERNAL (decl) || flag_extern_tls_init)
8551 && (was_readonly || TREE_READONLY (decl))
8552 && var_needs_tls_wrapper (decl))
8553 {
8554 /* TLS variables need dynamic initialization by the TLS wrapper
8555 function, we don't want to hoist accesses to it before the
8556 wrapper. */
8557 was_readonly = 0;
8558 TREE_READONLY (decl) = 0;
8559 }
8560
8561 make_rtl_for_nonlocal_decl (decl, init, asmspec);
8562
8563 /* Check for abstractness of the type. */
8564 if (var_definition_p)
8565 abstract_virtuals_error (decl, type);
8566
8567 if (TREE_TYPE (decl) == error_mark_node)
8568 /* No initialization required. */
8569 ;
8570 else if (TREE_CODE (decl) == FUNCTION_DECL)
8571 {
8572 if (init)
8573 {
8574 if (init == ridpointers[(int)RID_DEFAULT])
8575 {
8576 /* An out-of-class default definition is defined at
8577 the point where it is explicitly defaulted. */
8578 if (DECL_DELETED_FN (decl))
8579 maybe_explain_implicit_delete (decl);
8580 else if (DECL_INITIAL (decl) == error_mark_node)
8581 synthesize_method (decl);
8582 }
8583 else
8584 error_at (cp_expr_loc_or_loc (init,
8585 DECL_SOURCE_LOCATION (decl)),
8586 "function %q#D is initialized like a variable",
8587 decl);
8588 }
8589 /* else no initialization required. */
8590 }
8591 else if (DECL_EXTERNAL (decl)
8592 && ! (DECL_LANG_SPECIFIC (decl)
8593 && DECL_NOT_REALLY_EXTERN (decl)))
8594 {
8595 /* check_initializer will have done any constant initialization. */
8596 }
8597 /* A variable definition. */
8598 else if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
8599 /* Initialize the local variable. */
8600 initialize_local_var (decl, init);
8601
8602 /* If a variable is defined, and then a subsequent
8603 definition with external linkage is encountered, we will
8604 get here twice for the same variable. We want to avoid
8605 calling expand_static_init more than once. For variables
8606 that are not static data members, we can call
8607 expand_static_init only when we actually process the
8608 initializer. It is not legal to redeclare a static data
8609 member, so this issue does not arise in that case. */
8610 else if (var_definition_p && TREE_STATIC (decl))
8611 expand_static_init (decl, init);
8612 }
8613
8614 /* If a CLEANUP_STMT was created to destroy a temporary bound to a
8615 reference, insert it in the statement-tree now. */
8616 if (cleanups)
8617 {
8618 for (tree t : *cleanups)
8619 {
8620 push_cleanup (NULL_TREE, t, false);
8621 /* As in initialize_local_var. */
8622 wrap_temporary_cleanups (init, t);
8623 }
8624 release_tree_vector (cleanups);
8625 }
8626
8627 if (was_readonly)
8628 TREE_READONLY (decl) = 1;
8629
8630 if (flag_openmp
8631 && VAR_P (decl)
8632 && lookup_attribute ("omp declare target implicit",
8633 DECL_ATTRIBUTES (decl)))
8634 {
8635 DECL_ATTRIBUTES (decl)
8636 = remove_attribute ("omp declare target implicit",
8637 DECL_ATTRIBUTES (decl));
8638 complete_type (TREE_TYPE (decl));
8639 if (!cp_omp_mappable_type (TREE_TYPE (decl)))
8640 {
8641 error ("%q+D in declare target directive does not have mappable"
8642 " type", decl);
8643 cp_omp_emit_unmappable_type_notes (TREE_TYPE (decl));
8644 }
8645 else if (!lookup_attribute ("omp declare target",
8646 DECL_ATTRIBUTES (decl))
8647 && !lookup_attribute ("omp declare target link",
8648 DECL_ATTRIBUTES (decl)))
8649 {
8650 DECL_ATTRIBUTES (decl)
8651 = tree_cons (get_identifier ("omp declare target"),
8652 NULL_TREE, DECL_ATTRIBUTES (decl));
8653 symtab_node *node = symtab_node::get (decl);
8654 if (node != NULL)
8655 {
8656 node->offloadable = 1;
8657 if (ENABLE_OFFLOADING)
8658 {
8659 g->have_offload = true;
8660 if (is_a <varpool_node *> (node))
8661 vec_safe_push (offload_vars, decl);
8662 }
8663 }
8664 }
8665 }
8666
8667 /* This is the last point we can lower alignment so give the target the
8668 chance to do so. */
8669 if (VAR_P (decl)
8670 && !is_global_var (decl)
8671 && !DECL_HARD_REGISTER (decl))
8672 targetm.lower_local_decl_alignment (decl);
8673
8674 invoke_plugin_callbacks (PLUGIN_FINISH_DECL, decl);
8675 }
8676
8677 /* For class TYPE return itself or some its bases that contain
8678 any direct non-static data members. Return error_mark_node if an
8679 error has been diagnosed. */
8680
8681 static tree
find_decomp_class_base(location_t loc,tree type,tree ret)8682 find_decomp_class_base (location_t loc, tree type, tree ret)
8683 {
8684 bool member_seen = false;
8685 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
8686 if (TREE_CODE (field) != FIELD_DECL
8687 || DECL_ARTIFICIAL (field)
8688 || DECL_UNNAMED_BIT_FIELD (field))
8689 continue;
8690 else if (ret)
8691 return type;
8692 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
8693 {
8694 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE)
8695 error_at (loc, "cannot decompose class type %qT because it has an "
8696 "anonymous struct member", type);
8697 else
8698 error_at (loc, "cannot decompose class type %qT because it has an "
8699 "anonymous union member", type);
8700 inform (DECL_SOURCE_LOCATION (field), "declared here");
8701 return error_mark_node;
8702 }
8703 else if (!accessible_p (type, field, true))
8704 {
8705 error_at (loc, "cannot decompose inaccessible member %qD of %qT",
8706 field, type);
8707 inform (DECL_SOURCE_LOCATION (field),
8708 TREE_PRIVATE (field)
8709 ? G_("declared private here")
8710 : G_("declared protected here"));
8711 return error_mark_node;
8712 }
8713 else
8714 member_seen = true;
8715
8716 tree base_binfo, binfo;
8717 tree orig_ret = ret;
8718 int i;
8719 if (member_seen)
8720 ret = type;
8721 for (binfo = TYPE_BINFO (type), i = 0;
8722 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
8723 {
8724 tree t = find_decomp_class_base (loc, TREE_TYPE (base_binfo), ret);
8725 if (t == error_mark_node)
8726 return error_mark_node;
8727 if (t != NULL_TREE && t != ret)
8728 {
8729 if (ret == type)
8730 {
8731 error_at (loc, "cannot decompose class type %qT: both it and "
8732 "its base class %qT have non-static data members",
8733 type, t);
8734 return error_mark_node;
8735 }
8736 else if (orig_ret != NULL_TREE)
8737 return t;
8738 else if (ret != NULL_TREE)
8739 {
8740 error_at (loc, "cannot decompose class type %qT: its base "
8741 "classes %qT and %qT have non-static data "
8742 "members", type, ret, t);
8743 return error_mark_node;
8744 }
8745 else
8746 ret = t;
8747 }
8748 }
8749 return ret;
8750 }
8751
8752 /* Return std::tuple_size<TYPE>::value. */
8753
8754 static tree
get_tuple_size(tree type)8755 get_tuple_size (tree type)
8756 {
8757 tree args = make_tree_vec (1);
8758 TREE_VEC_ELT (args, 0) = type;
8759 tree inst = lookup_template_class (tuple_size_identifier, args,
8760 /*in_decl*/NULL_TREE,
8761 /*context*/std_node,
8762 /*entering_scope*/false, tf_none);
8763 inst = complete_type (inst);
8764 if (inst == error_mark_node || !COMPLETE_TYPE_P (inst))
8765 return NULL_TREE;
8766 tree val = lookup_qualified_name (inst, value_identifier,
8767 LOOK_want::NORMAL, /*complain*/false);
8768 if (TREE_CODE (val) == VAR_DECL || TREE_CODE (val) == CONST_DECL)
8769 val = maybe_constant_value (val);
8770 if (TREE_CODE (val) == INTEGER_CST)
8771 return val;
8772 else
8773 return error_mark_node;
8774 }
8775
8776 /* Return std::tuple_element<I,TYPE>::type. */
8777
8778 static tree
get_tuple_element_type(tree type,unsigned i)8779 get_tuple_element_type (tree type, unsigned i)
8780 {
8781 tree args = make_tree_vec (2);
8782 TREE_VEC_ELT (args, 0) = build_int_cst (integer_type_node, i);
8783 TREE_VEC_ELT (args, 1) = type;
8784 tree inst = lookup_template_class (tuple_element_identifier, args,
8785 /*in_decl*/NULL_TREE,
8786 /*context*/std_node,
8787 /*entering_scope*/false,
8788 tf_warning_or_error);
8789 return make_typename_type (inst, type_identifier,
8790 none_type, tf_warning_or_error);
8791 }
8792
8793 /* Return e.get<i>() or get<i>(e). */
8794
8795 static tree
get_tuple_decomp_init(tree decl,unsigned i)8796 get_tuple_decomp_init (tree decl, unsigned i)
8797 {
8798 tree targs = make_tree_vec (1);
8799 TREE_VEC_ELT (targs, 0) = build_int_cst (integer_type_node, i);
8800
8801 tree etype = TREE_TYPE (decl);
8802 tree e = convert_from_reference (decl);
8803
8804 /* [The id-expression] e is an lvalue if the type of the entity e is an
8805 lvalue reference and an xvalue otherwise. */
8806 if (!TYPE_REF_P (etype)
8807 || TYPE_REF_IS_RVALUE (etype))
8808 e = move (e);
8809
8810 tree fns = lookup_qualified_name (TREE_TYPE (e), get__identifier,
8811 LOOK_want::NORMAL, /*complain*/false);
8812 bool use_member_get = false;
8813
8814 /* To use a member get, member lookup must find at least one
8815 declaration that is a function template
8816 whose first template parameter is a non-type parameter. */
8817 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (fns)); iter; ++iter)
8818 {
8819 tree fn = *iter;
8820 if (TREE_CODE (fn) == TEMPLATE_DECL)
8821 {
8822 tree tparms = DECL_TEMPLATE_PARMS (fn);
8823 tree parm = TREE_VEC_ELT (INNERMOST_TEMPLATE_PARMS (tparms), 0);
8824 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL)
8825 {
8826 use_member_get = true;
8827 break;
8828 }
8829 }
8830 }
8831
8832 if (use_member_get)
8833 {
8834 fns = lookup_template_function (fns, targs);
8835 return build_new_method_call (e, fns, /*args*/NULL,
8836 /*path*/NULL_TREE, LOOKUP_NORMAL,
8837 /*fn_p*/NULL, tf_warning_or_error);
8838 }
8839 else
8840 {
8841 releasing_vec args (make_tree_vector_single (e));
8842 fns = lookup_template_function (get__identifier, targs);
8843 fns = perform_koenig_lookup (fns, args, tf_warning_or_error);
8844 return finish_call_expr (fns, &args, /*novirt*/false,
8845 /*koenig*/true, tf_warning_or_error);
8846 }
8847 }
8848
8849 /* It's impossible to recover the decltype of a tuple decomposition variable
8850 based on the actual type of the variable, so store it in a hash table. */
8851
8852 static GTY((cache)) decl_tree_cache_map *decomp_type_table;
8853
8854 tree
lookup_decomp_type(tree v)8855 lookup_decomp_type (tree v)
8856 {
8857 return *decomp_type_table->get (v);
8858 }
8859
8860 /* Mangle a decomposition declaration if needed. Arguments like
8861 in cp_finish_decomp. */
8862
8863 void
cp_maybe_mangle_decomp(tree decl,tree first,unsigned int count)8864 cp_maybe_mangle_decomp (tree decl, tree first, unsigned int count)
8865 {
8866 if (!processing_template_decl
8867 && !error_operand_p (decl)
8868 && TREE_STATIC (decl))
8869 {
8870 auto_vec<tree, 16> v;
8871 v.safe_grow (count, true);
8872 tree d = first;
8873 for (unsigned int i = 0; i < count; i++, d = DECL_CHAIN (d))
8874 v[count - i - 1] = d;
8875 SET_DECL_ASSEMBLER_NAME (decl, mangle_decomp (decl, v));
8876 maybe_apply_pragma_weak (decl);
8877 }
8878 }
8879
8880 /* Finish a decomposition declaration. DECL is the underlying declaration
8881 "e", FIRST is the head of a chain of decls for the individual identifiers
8882 chained through DECL_CHAIN in reverse order and COUNT is the number of
8883 those decls. */
8884
8885 void
cp_finish_decomp(tree decl,tree first,unsigned int count)8886 cp_finish_decomp (tree decl, tree first, unsigned int count)
8887 {
8888 if (error_operand_p (decl))
8889 {
8890 error_out:
8891 while (count--)
8892 {
8893 TREE_TYPE (first) = error_mark_node;
8894 if (DECL_HAS_VALUE_EXPR_P (first))
8895 {
8896 SET_DECL_VALUE_EXPR (first, NULL_TREE);
8897 DECL_HAS_VALUE_EXPR_P (first) = 0;
8898 }
8899 first = DECL_CHAIN (first);
8900 }
8901 if (DECL_P (decl) && DECL_NAMESPACE_SCOPE_P (decl))
8902 SET_DECL_ASSEMBLER_NAME (decl, get_identifier ("<decomp>"));
8903 return;
8904 }
8905
8906 location_t loc = DECL_SOURCE_LOCATION (decl);
8907 if (type_dependent_expression_p (decl)
8908 /* This happens for range for when not in templates.
8909 Still add the DECL_VALUE_EXPRs for later processing. */
8910 || (!processing_template_decl
8911 && type_uses_auto (TREE_TYPE (decl))))
8912 {
8913 for (unsigned int i = 0; i < count; i++)
8914 {
8915 if (!DECL_HAS_VALUE_EXPR_P (first))
8916 {
8917 tree v = build_nt (ARRAY_REF, decl,
8918 size_int (count - i - 1),
8919 NULL_TREE, NULL_TREE);
8920 SET_DECL_VALUE_EXPR (first, v);
8921 DECL_HAS_VALUE_EXPR_P (first) = 1;
8922 }
8923 if (processing_template_decl)
8924 fit_decomposition_lang_decl (first, decl);
8925 first = DECL_CHAIN (first);
8926 }
8927 return;
8928 }
8929
8930 auto_vec<tree, 16> v;
8931 v.safe_grow (count, true);
8932 tree d = first;
8933 for (unsigned int i = 0; i < count; i++, d = DECL_CHAIN (d))
8934 {
8935 v[count - i - 1] = d;
8936 fit_decomposition_lang_decl (d, decl);
8937 }
8938
8939 tree type = TREE_TYPE (decl);
8940 tree dexp = decl;
8941
8942 if (TYPE_REF_P (type))
8943 {
8944 dexp = convert_from_reference (dexp);
8945 type = complete_type (TREE_TYPE (type));
8946 if (type == error_mark_node)
8947 goto error_out;
8948 if (!COMPLETE_TYPE_P (type))
8949 {
8950 error_at (loc, "structured binding refers to incomplete type %qT",
8951 type);
8952 goto error_out;
8953 }
8954 }
8955
8956 tree eltype = NULL_TREE;
8957 unsigned HOST_WIDE_INT eltscnt = 0;
8958 if (TREE_CODE (type) == ARRAY_TYPE)
8959 {
8960 tree nelts;
8961 nelts = array_type_nelts_top (type);
8962 if (nelts == error_mark_node)
8963 goto error_out;
8964 if (!tree_fits_uhwi_p (nelts))
8965 {
8966 error_at (loc, "cannot decompose variable length array %qT", type);
8967 goto error_out;
8968 }
8969 eltscnt = tree_to_uhwi (nelts);
8970 if (count != eltscnt)
8971 {
8972 cnt_mismatch:
8973 if (count > eltscnt)
8974 error_n (loc, count,
8975 "%u name provided for structured binding",
8976 "%u names provided for structured binding", count);
8977 else
8978 error_n (loc, count,
8979 "only %u name provided for structured binding",
8980 "only %u names provided for structured binding", count);
8981 inform_n (loc, eltscnt,
8982 "while %qT decomposes into %wu element",
8983 "while %qT decomposes into %wu elements",
8984 type, eltscnt);
8985 goto error_out;
8986 }
8987 eltype = TREE_TYPE (type);
8988 for (unsigned int i = 0; i < count; i++)
8989 {
8990 TREE_TYPE (v[i]) = eltype;
8991 layout_decl (v[i], 0);
8992 if (processing_template_decl)
8993 continue;
8994 tree t = unshare_expr (dexp);
8995 t = build4_loc (DECL_SOURCE_LOCATION (v[i]), ARRAY_REF,
8996 eltype, t, size_int (i), NULL_TREE,
8997 NULL_TREE);
8998 SET_DECL_VALUE_EXPR (v[i], t);
8999 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
9000 }
9001 }
9002 /* 2 GNU extensions. */
9003 else if (TREE_CODE (type) == COMPLEX_TYPE)
9004 {
9005 eltscnt = 2;
9006 if (count != eltscnt)
9007 goto cnt_mismatch;
9008 eltype = cp_build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
9009 for (unsigned int i = 0; i < count; i++)
9010 {
9011 TREE_TYPE (v[i]) = eltype;
9012 layout_decl (v[i], 0);
9013 if (processing_template_decl)
9014 continue;
9015 tree t = unshare_expr (dexp);
9016 t = build1_loc (DECL_SOURCE_LOCATION (v[i]),
9017 i ? IMAGPART_EXPR : REALPART_EXPR, eltype,
9018 t);
9019 SET_DECL_VALUE_EXPR (v[i], t);
9020 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
9021 }
9022 }
9023 else if (TREE_CODE (type) == VECTOR_TYPE)
9024 {
9025 if (!TYPE_VECTOR_SUBPARTS (type).is_constant (&eltscnt))
9026 {
9027 error_at (loc, "cannot decompose variable length vector %qT", type);
9028 goto error_out;
9029 }
9030 if (count != eltscnt)
9031 goto cnt_mismatch;
9032 eltype = cp_build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
9033 for (unsigned int i = 0; i < count; i++)
9034 {
9035 TREE_TYPE (v[i]) = eltype;
9036 layout_decl (v[i], 0);
9037 if (processing_template_decl)
9038 continue;
9039 tree t = unshare_expr (dexp);
9040 convert_vector_to_array_for_subscript (DECL_SOURCE_LOCATION (v[i]),
9041 &t, size_int (i));
9042 t = build4_loc (DECL_SOURCE_LOCATION (v[i]), ARRAY_REF,
9043 eltype, t, size_int (i), NULL_TREE,
9044 NULL_TREE);
9045 SET_DECL_VALUE_EXPR (v[i], t);
9046 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
9047 }
9048 }
9049 else if (tree tsize = get_tuple_size (type))
9050 {
9051 if (tsize == error_mark_node)
9052 {
9053 error_at (loc, "%<std::tuple_size<%T>::value%> is not an integral "
9054 "constant expression", type);
9055 goto error_out;
9056 }
9057 if (!tree_fits_uhwi_p (tsize))
9058 {
9059 error_n (loc, count,
9060 "%u name provided for structured binding",
9061 "%u names provided for structured binding", count);
9062 inform (loc, "while %qT decomposes into %E elements",
9063 type, tsize);
9064 goto error_out;
9065 }
9066 eltscnt = tree_to_uhwi (tsize);
9067 if (count != eltscnt)
9068 goto cnt_mismatch;
9069 int save_read = DECL_READ_P (decl);
9070 for (unsigned i = 0; i < count; ++i)
9071 {
9072 location_t sloc = input_location;
9073 location_t dloc = DECL_SOURCE_LOCATION (v[i]);
9074
9075 input_location = dloc;
9076 tree init = get_tuple_decomp_init (decl, i);
9077 tree eltype = (init == error_mark_node ? error_mark_node
9078 : get_tuple_element_type (type, i));
9079 input_location = sloc;
9080
9081 if (VOID_TYPE_P (eltype))
9082 {
9083 error ("%<std::tuple_element<%u, %T>::type%> is %<void%>",
9084 i, type);
9085 eltype = error_mark_node;
9086 }
9087 if (init == error_mark_node || eltype == error_mark_node)
9088 {
9089 inform (dloc, "in initialization of structured binding "
9090 "variable %qD", v[i]);
9091 goto error_out;
9092 }
9093 /* Save the decltype away before reference collapse. */
9094 hash_map_safe_put<hm_ggc> (decomp_type_table, v[i], eltype);
9095 eltype = cp_build_reference_type (eltype, !lvalue_p (init));
9096 TREE_TYPE (v[i]) = eltype;
9097 layout_decl (v[i], 0);
9098 if (DECL_HAS_VALUE_EXPR_P (v[i]))
9099 {
9100 /* In this case the names are variables, not just proxies. */
9101 SET_DECL_VALUE_EXPR (v[i], NULL_TREE);
9102 DECL_HAS_VALUE_EXPR_P (v[i]) = 0;
9103 }
9104 if (!processing_template_decl)
9105 {
9106 copy_linkage (v[i], decl);
9107 cp_finish_decl (v[i], init, /*constexpr*/false,
9108 /*asm*/NULL_TREE, LOOKUP_NORMAL);
9109 }
9110 }
9111 /* Ignore reads from the underlying decl performed during initialization
9112 of the individual variables. If those will be read, we'll mark
9113 the underlying decl as read at that point. */
9114 DECL_READ_P (decl) = save_read;
9115 }
9116 else if (TREE_CODE (type) == UNION_TYPE)
9117 {
9118 error_at (loc, "cannot decompose union type %qT", type);
9119 goto error_out;
9120 }
9121 else if (!CLASS_TYPE_P (type))
9122 {
9123 error_at (loc, "cannot decompose non-array non-class type %qT", type);
9124 goto error_out;
9125 }
9126 else if (LAMBDA_TYPE_P (type))
9127 {
9128 error_at (loc, "cannot decompose lambda closure type %qT", type);
9129 goto error_out;
9130 }
9131 else if (processing_template_decl && complete_type (type) == error_mark_node)
9132 goto error_out;
9133 else if (processing_template_decl && !COMPLETE_TYPE_P (type))
9134 pedwarn (loc, 0, "structured binding refers to incomplete class type %qT",
9135 type);
9136 else
9137 {
9138 tree btype = find_decomp_class_base (loc, type, NULL_TREE);
9139 if (btype == error_mark_node)
9140 goto error_out;
9141 else if (btype == NULL_TREE)
9142 {
9143 error_at (loc, "cannot decompose class type %qT without non-static "
9144 "data members", type);
9145 goto error_out;
9146 }
9147 for (tree field = TYPE_FIELDS (btype); field; field = TREE_CHAIN (field))
9148 if (TREE_CODE (field) != FIELD_DECL
9149 || DECL_ARTIFICIAL (field)
9150 || DECL_UNNAMED_BIT_FIELD (field))
9151 continue;
9152 else
9153 eltscnt++;
9154 if (count != eltscnt)
9155 goto cnt_mismatch;
9156 tree t = dexp;
9157 if (type != btype)
9158 {
9159 t = convert_to_base (t, btype, /*check_access*/true,
9160 /*nonnull*/false, tf_warning_or_error);
9161 type = btype;
9162 }
9163 unsigned int i = 0;
9164 for (tree field = TYPE_FIELDS (btype); field; field = TREE_CHAIN (field))
9165 if (TREE_CODE (field) != FIELD_DECL
9166 || DECL_ARTIFICIAL (field)
9167 || DECL_UNNAMED_BIT_FIELD (field))
9168 continue;
9169 else
9170 {
9171 tree tt = finish_non_static_data_member (field, unshare_expr (t),
9172 NULL_TREE);
9173 if (REFERENCE_REF_P (tt))
9174 tt = TREE_OPERAND (tt, 0);
9175 TREE_TYPE (v[i]) = TREE_TYPE (tt);
9176 layout_decl (v[i], 0);
9177 if (!processing_template_decl)
9178 {
9179 SET_DECL_VALUE_EXPR (v[i], tt);
9180 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
9181 }
9182 i++;
9183 }
9184 }
9185 if (processing_template_decl)
9186 {
9187 for (unsigned int i = 0; i < count; i++)
9188 if (!DECL_HAS_VALUE_EXPR_P (v[i]))
9189 {
9190 tree a = build_nt (ARRAY_REF, decl, size_int (i),
9191 NULL_TREE, NULL_TREE);
9192 SET_DECL_VALUE_EXPR (v[i], a);
9193 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
9194 }
9195 }
9196 }
9197
9198 /* Returns a declaration for a VAR_DECL as if:
9199
9200 extern "C" TYPE NAME;
9201
9202 had been seen. Used to create compiler-generated global
9203 variables. */
9204
9205 static tree
declare_global_var(tree name,tree type)9206 declare_global_var (tree name, tree type)
9207 {
9208 auto cookie = push_abi_namespace (global_namespace);
9209 tree decl = build_decl (input_location, VAR_DECL, name, type);
9210 TREE_PUBLIC (decl) = 1;
9211 DECL_EXTERNAL (decl) = 1;
9212 DECL_ARTIFICIAL (decl) = 1;
9213 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
9214 /* If the user has explicitly declared this variable (perhaps
9215 because the code we are compiling is part of a low-level runtime
9216 library), then it is possible that our declaration will be merged
9217 with theirs by pushdecl. */
9218 decl = pushdecl (decl);
9219 cp_finish_decl (decl, NULL_TREE, false, NULL_TREE, 0);
9220 pop_abi_namespace (cookie, global_namespace);
9221
9222 return decl;
9223 }
9224
9225 /* Returns the type for the argument to "__cxa_atexit" (or "atexit",
9226 if "__cxa_atexit" is not being used) corresponding to the function
9227 to be called when the program exits. */
9228
9229 static tree
get_atexit_fn_ptr_type(void)9230 get_atexit_fn_ptr_type (void)
9231 {
9232 tree fn_type;
9233
9234 if (!atexit_fn_ptr_type_node)
9235 {
9236 tree arg_type;
9237 if (flag_use_cxa_atexit
9238 && !targetm.cxx.use_atexit_for_cxa_atexit ())
9239 /* The parameter to "__cxa_atexit" is "void (*)(void *)". */
9240 arg_type = ptr_type_node;
9241 else
9242 /* The parameter to "atexit" is "void (*)(void)". */
9243 arg_type = NULL_TREE;
9244
9245 fn_type = build_function_type_list (void_type_node,
9246 arg_type, NULL_TREE);
9247 atexit_fn_ptr_type_node = build_pointer_type (fn_type);
9248 }
9249
9250 return atexit_fn_ptr_type_node;
9251 }
9252
9253 /* Returns a pointer to the `atexit' function. Note that if
9254 FLAG_USE_CXA_ATEXIT is nonzero, then this will actually be the new
9255 `__cxa_atexit' function specified in the IA64 C++ ABI. */
9256
9257 static tree
get_atexit_node(void)9258 get_atexit_node (void)
9259 {
9260 tree atexit_fndecl;
9261 tree fn_type;
9262 tree fn_ptr_type;
9263 const char *name;
9264 bool use_aeabi_atexit;
9265 tree ctx = global_namespace;
9266
9267 if (atexit_node)
9268 return atexit_node;
9269
9270 if (flag_use_cxa_atexit && !targetm.cxx.use_atexit_for_cxa_atexit ())
9271 {
9272 /* The declaration for `__cxa_atexit' is:
9273
9274 int __cxa_atexit (void (*)(void *), void *, void *)
9275
9276 We build up the argument types and then the function type
9277 itself. */
9278 tree argtype0, argtype1, argtype2;
9279
9280 use_aeabi_atexit = targetm.cxx.use_aeabi_atexit ();
9281 /* First, build the pointer-to-function type for the first
9282 argument. */
9283 fn_ptr_type = get_atexit_fn_ptr_type ();
9284 /* Then, build the rest of the argument types. */
9285 argtype2 = ptr_type_node;
9286 if (use_aeabi_atexit)
9287 {
9288 argtype1 = fn_ptr_type;
9289 argtype0 = ptr_type_node;
9290 }
9291 else
9292 {
9293 argtype1 = ptr_type_node;
9294 argtype0 = fn_ptr_type;
9295 }
9296 /* And the final __cxa_atexit type. */
9297 fn_type = build_function_type_list (integer_type_node,
9298 argtype0, argtype1, argtype2,
9299 NULL_TREE);
9300 /* ... which needs noexcept. */
9301 fn_type = build_exception_variant (fn_type, noexcept_true_spec);
9302 if (use_aeabi_atexit)
9303 {
9304 name = "__aeabi_atexit";
9305 push_to_top_level ();
9306 int n = push_namespace (get_identifier ("__aeabiv1"), false);
9307 ctx = current_namespace;
9308 while (n--)
9309 pop_namespace ();
9310 pop_from_top_level ();
9311 }
9312 else
9313 {
9314 name = "__cxa_atexit";
9315 ctx = abi_node;
9316 }
9317 }
9318 else
9319 {
9320 /* The declaration for `atexit' is:
9321
9322 int atexit (void (*)());
9323
9324 We build up the argument types and then the function type
9325 itself. */
9326 fn_ptr_type = get_atexit_fn_ptr_type ();
9327 /* Build the final atexit type. */
9328 fn_type = build_function_type_list (integer_type_node,
9329 fn_ptr_type, NULL_TREE);
9330 /* ... which needs noexcept. */
9331 fn_type = build_exception_variant (fn_type, noexcept_true_spec);
9332 name = "atexit";
9333 }
9334
9335 /* Now, build the function declaration. */
9336 push_lang_context (lang_name_c);
9337 auto cookie = push_abi_namespace (ctx);
9338 atexit_fndecl = build_library_fn_ptr (name, fn_type, ECF_LEAF | ECF_NOTHROW);
9339 DECL_CONTEXT (atexit_fndecl) = FROB_CONTEXT (current_namespace);
9340 /* Install as hidden builtin so we're (a) more relaxed about
9341 exception spec matching and (b) will not give a confusing location
9342 in diagnostic and (c) won't magically appear in user-visible name
9343 lookups. */
9344 DECL_SOURCE_LOCATION (atexit_fndecl) = BUILTINS_LOCATION;
9345 atexit_fndecl = pushdecl (atexit_fndecl, /*hiding=*/true);
9346 pop_abi_namespace (cookie, ctx);
9347 mark_used (atexit_fndecl);
9348 pop_lang_context ();
9349 atexit_node = decay_conversion (atexit_fndecl, tf_warning_or_error);
9350
9351 return atexit_node;
9352 }
9353
9354 /* Like get_atexit_node, but for thread-local cleanups. */
9355
9356 static tree
get_thread_atexit_node(void)9357 get_thread_atexit_node (void)
9358 {
9359 /* The declaration for `__cxa_thread_atexit' is:
9360
9361 int __cxa_thread_atexit (void (*)(void *), void *, void *) */
9362 tree fn_type = build_function_type_list (integer_type_node,
9363 get_atexit_fn_ptr_type (),
9364 ptr_type_node, ptr_type_node,
9365 NULL_TREE);
9366
9367 /* Now, build the function declaration. */
9368 tree atexit_fndecl = build_library_fn_ptr ("__cxa_thread_atexit", fn_type,
9369 ECF_LEAF | ECF_NOTHROW);
9370 return decay_conversion (atexit_fndecl, tf_warning_or_error);
9371 }
9372
9373 /* Returns the __dso_handle VAR_DECL. */
9374
9375 static tree
get_dso_handle_node(void)9376 get_dso_handle_node (void)
9377 {
9378 if (dso_handle_node)
9379 return dso_handle_node;
9380
9381 /* Declare the variable. */
9382 dso_handle_node = declare_global_var (get_identifier ("__dso_handle"),
9383 ptr_type_node);
9384
9385 #ifdef HAVE_GAS_HIDDEN
9386 if (dso_handle_node != error_mark_node)
9387 {
9388 DECL_VISIBILITY (dso_handle_node) = VISIBILITY_HIDDEN;
9389 DECL_VISIBILITY_SPECIFIED (dso_handle_node) = 1;
9390 }
9391 #endif
9392
9393 return dso_handle_node;
9394 }
9395
9396 /* Begin a new function with internal linkage whose job will be simply
9397 to destroy some particular variable. */
9398
9399 static GTY(()) int start_cleanup_cnt;
9400
9401 static tree
start_cleanup_fn(void)9402 start_cleanup_fn (void)
9403 {
9404 char name[32];
9405
9406 push_to_top_level ();
9407
9408 /* No need to mangle this. */
9409 push_lang_context (lang_name_c);
9410
9411 /* Build the name of the function. */
9412 sprintf (name, "__tcf_%d", start_cleanup_cnt++);
9413 /* Build the function declaration. */
9414 tree fntype = TREE_TYPE (get_atexit_fn_ptr_type ());
9415 tree fndecl = build_lang_decl (FUNCTION_DECL, get_identifier (name), fntype);
9416 DECL_CONTEXT (fndecl) = FROB_CONTEXT (current_namespace);
9417 /* It's a function with internal linkage, generated by the
9418 compiler. */
9419 TREE_PUBLIC (fndecl) = 0;
9420 DECL_ARTIFICIAL (fndecl) = 1;
9421 /* Make the function `inline' so that it is only emitted if it is
9422 actually needed. It is unlikely that it will be inlined, since
9423 it is only called via a function pointer, but we avoid unnecessary
9424 emissions this way. */
9425 DECL_DECLARED_INLINE_P (fndecl) = 1;
9426 DECL_INTERFACE_KNOWN (fndecl) = 1;
9427 if (flag_use_cxa_atexit && !targetm.cxx.use_atexit_for_cxa_atexit ())
9428 {
9429 /* Build the parameter. */
9430 tree parmdecl = cp_build_parm_decl (fndecl, NULL_TREE, ptr_type_node);
9431 TREE_USED (parmdecl) = 1;
9432 DECL_READ_P (parmdecl) = 1;
9433 DECL_ARGUMENTS (fndecl) = parmdecl;
9434 }
9435
9436 fndecl = pushdecl (fndecl, /*hidden=*/true);
9437 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
9438
9439 pop_lang_context ();
9440
9441 return current_function_decl;
9442 }
9443
9444 /* Finish the cleanup function begun by start_cleanup_fn. */
9445
9446 static void
end_cleanup_fn(void)9447 end_cleanup_fn (void)
9448 {
9449 expand_or_defer_fn (finish_function (/*inline_p=*/false));
9450
9451 pop_from_top_level ();
9452 }
9453
9454 /* Generate code to handle the destruction of DECL, an object with
9455 static storage duration. */
9456
9457 tree
register_dtor_fn(tree decl)9458 register_dtor_fn (tree decl)
9459 {
9460 tree cleanup;
9461 tree addr;
9462 tree compound_stmt;
9463 tree fcall;
9464 tree type;
9465 bool ob_parm, dso_parm, use_dtor;
9466 tree arg0, arg1, arg2;
9467 tree atex_node;
9468
9469 type = TREE_TYPE (decl);
9470 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
9471 return void_node;
9472
9473 if (decl_maybe_constant_destruction (decl, type)
9474 && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
9475 {
9476 cxx_maybe_build_cleanup (decl, tf_warning_or_error);
9477 return void_node;
9478 }
9479
9480 /* If we're using "__cxa_atexit" (or "__cxa_thread_atexit" or
9481 "__aeabi_atexit"), and DECL is a class object, we can just pass the
9482 destructor to "__cxa_atexit"; we don't have to build a temporary
9483 function to do the cleanup. */
9484 dso_parm = (flag_use_cxa_atexit
9485 && !targetm.cxx.use_atexit_for_cxa_atexit ());
9486 ob_parm = (CP_DECL_THREAD_LOCAL_P (decl) || dso_parm);
9487 use_dtor = ob_parm && CLASS_TYPE_P (type);
9488 if (use_dtor)
9489 {
9490 cleanup = get_class_binding (type, complete_dtor_identifier);
9491
9492 /* Make sure it is accessible. */
9493 perform_or_defer_access_check (TYPE_BINFO (type), cleanup, cleanup,
9494 tf_warning_or_error);
9495 }
9496 else
9497 {
9498 /* Call build_cleanup before we enter the anonymous function so
9499 that any access checks will be done relative to the current
9500 scope, rather than the scope of the anonymous function. */
9501 build_cleanup (decl);
9502
9503 /* Now start the function. */
9504 cleanup = start_cleanup_fn ();
9505
9506 /* Now, recompute the cleanup. It may contain SAVE_EXPRs that refer
9507 to the original function, rather than the anonymous one. That
9508 will make the back end think that nested functions are in use,
9509 which causes confusion. */
9510 push_deferring_access_checks (dk_no_check);
9511 fcall = build_cleanup (decl);
9512 pop_deferring_access_checks ();
9513
9514 /* Create the body of the anonymous function. */
9515 compound_stmt = begin_compound_stmt (BCS_FN_BODY);
9516 finish_expr_stmt (fcall);
9517 finish_compound_stmt (compound_stmt);
9518 end_cleanup_fn ();
9519 }
9520
9521 /* Call atexit with the cleanup function. */
9522 mark_used (cleanup);
9523 cleanup = build_address (cleanup);
9524
9525 if (CP_DECL_THREAD_LOCAL_P (decl))
9526 atex_node = get_thread_atexit_node ();
9527 else
9528 atex_node = get_atexit_node ();
9529
9530 if (use_dtor)
9531 {
9532 /* We must convert CLEANUP to the type that "__cxa_atexit"
9533 expects. */
9534 cleanup = build_nop (get_atexit_fn_ptr_type (), cleanup);
9535 /* "__cxa_atexit" will pass the address of DECL to the
9536 cleanup function. */
9537 mark_used (decl);
9538 addr = build_address (decl);
9539 /* The declared type of the parameter to "__cxa_atexit" is
9540 "void *". For plain "T*", we could just let the
9541 machinery in cp_build_function_call convert it -- but if the
9542 type is "cv-qualified T *", then we need to convert it
9543 before passing it in, to avoid spurious errors. */
9544 addr = build_nop (ptr_type_node, addr);
9545 }
9546 else
9547 /* Since the cleanup functions we build ignore the address
9548 they're given, there's no reason to pass the actual address
9549 in, and, in general, it's cheaper to pass NULL than any
9550 other value. */
9551 addr = null_pointer_node;
9552
9553 if (dso_parm)
9554 arg2 = cp_build_addr_expr (get_dso_handle_node (),
9555 tf_warning_or_error);
9556 else if (ob_parm)
9557 /* Just pass NULL to the dso handle parm if we don't actually
9558 have a DSO handle on this target. */
9559 arg2 = null_pointer_node;
9560 else
9561 arg2 = NULL_TREE;
9562
9563 if (ob_parm)
9564 {
9565 if (!CP_DECL_THREAD_LOCAL_P (decl)
9566 && targetm.cxx.use_aeabi_atexit ())
9567 {
9568 arg1 = cleanup;
9569 arg0 = addr;
9570 }
9571 else
9572 {
9573 arg1 = addr;
9574 arg0 = cleanup;
9575 }
9576 }
9577 else
9578 {
9579 arg0 = cleanup;
9580 arg1 = NULL_TREE;
9581 }
9582 return cp_build_function_call_nary (atex_node, tf_warning_or_error,
9583 arg0, arg1, arg2, NULL_TREE);
9584 }
9585
9586 /* DECL is a VAR_DECL with static storage duration. INIT, if present,
9587 is its initializer. Generate code to handle the construction
9588 and destruction of DECL. */
9589
9590 static void
expand_static_init(tree decl,tree init)9591 expand_static_init (tree decl, tree init)
9592 {
9593 gcc_assert (VAR_P (decl));
9594 gcc_assert (TREE_STATIC (decl));
9595
9596 /* Some variables require no dynamic initialization. */
9597 if (decl_maybe_constant_destruction (decl, TREE_TYPE (decl)))
9598 {
9599 /* Make sure the destructor is callable. */
9600 cxx_maybe_build_cleanup (decl, tf_warning_or_error);
9601 if (!init)
9602 return;
9603 }
9604
9605 if (CP_DECL_THREAD_LOCAL_P (decl) && DECL_GNU_TLS_P (decl)
9606 && !DECL_FUNCTION_SCOPE_P (decl))
9607 {
9608 location_t dloc = DECL_SOURCE_LOCATION (decl);
9609 if (init)
9610 error_at (dloc, "non-local variable %qD declared %<__thread%> "
9611 "needs dynamic initialization", decl);
9612 else
9613 error_at (dloc, "non-local variable %qD declared %<__thread%> "
9614 "has a non-trivial destructor", decl);
9615 static bool informed;
9616 if (!informed)
9617 {
9618 inform (dloc, "C++11 %<thread_local%> allows dynamic "
9619 "initialization and destruction");
9620 informed = true;
9621 }
9622 return;
9623 }
9624
9625 if (DECL_FUNCTION_SCOPE_P (decl))
9626 {
9627 /* Emit code to perform this initialization but once. */
9628 tree if_stmt = NULL_TREE, inner_if_stmt = NULL_TREE;
9629 tree then_clause = NULL_TREE, inner_then_clause = NULL_TREE;
9630 tree guard, guard_addr;
9631 tree flag, begin;
9632 /* We don't need thread-safety code for thread-local vars. */
9633 bool thread_guard = (flag_threadsafe_statics
9634 && !CP_DECL_THREAD_LOCAL_P (decl));
9635
9636 /* Emit code to perform this initialization but once. This code
9637 looks like:
9638
9639 static <type> guard;
9640 if (!__atomic_load (guard.first_byte)) {
9641 if (__cxa_guard_acquire (&guard)) {
9642 bool flag = false;
9643 try {
9644 // Do initialization.
9645 flag = true; __cxa_guard_release (&guard);
9646 // Register variable for destruction at end of program.
9647 } catch {
9648 if (!flag) __cxa_guard_abort (&guard);
9649 }
9650 }
9651 }
9652
9653 Note that the `flag' variable is only set to 1 *after* the
9654 initialization is complete. This ensures that an exception,
9655 thrown during the construction, will cause the variable to
9656 reinitialized when we pass through this code again, as per:
9657
9658 [stmt.dcl]
9659
9660 If the initialization exits by throwing an exception, the
9661 initialization is not complete, so it will be tried again
9662 the next time control enters the declaration.
9663
9664 This process should be thread-safe, too; multiple threads
9665 should not be able to initialize the variable more than
9666 once. */
9667
9668 /* Create the guard variable. */
9669 guard = get_guard (decl);
9670
9671 /* Begin the conditional initialization. */
9672 if_stmt = begin_if_stmt ();
9673
9674 finish_if_stmt_cond (get_guard_cond (guard, thread_guard), if_stmt);
9675 then_clause = begin_compound_stmt (BCS_NO_SCOPE);
9676
9677 if (thread_guard)
9678 {
9679 tree vfntype = NULL_TREE;
9680 tree acquire_name, release_name, abort_name;
9681 tree acquire_fn, release_fn, abort_fn;
9682 guard_addr = build_address (guard);
9683
9684 acquire_name = get_identifier ("__cxa_guard_acquire");
9685 release_name = get_identifier ("__cxa_guard_release");
9686 abort_name = get_identifier ("__cxa_guard_abort");
9687 acquire_fn = get_global_binding (acquire_name);
9688 release_fn = get_global_binding (release_name);
9689 abort_fn = get_global_binding (abort_name);
9690 if (!acquire_fn)
9691 acquire_fn = push_library_fn
9692 (acquire_name, build_function_type_list (integer_type_node,
9693 TREE_TYPE (guard_addr),
9694 NULL_TREE),
9695 NULL_TREE, ECF_NOTHROW);
9696 if (!release_fn || !abort_fn)
9697 vfntype = build_function_type_list (void_type_node,
9698 TREE_TYPE (guard_addr),
9699 NULL_TREE);
9700 if (!release_fn)
9701 release_fn = push_library_fn (release_name, vfntype, NULL_TREE,
9702 ECF_NOTHROW);
9703 if (!abort_fn)
9704 abort_fn = push_library_fn (abort_name, vfntype, NULL_TREE,
9705 ECF_NOTHROW | ECF_LEAF);
9706
9707 inner_if_stmt = begin_if_stmt ();
9708 finish_if_stmt_cond (build_call_n (acquire_fn, 1, guard_addr),
9709 inner_if_stmt);
9710
9711 inner_then_clause = begin_compound_stmt (BCS_NO_SCOPE);
9712 begin = get_target_expr (boolean_false_node);
9713 flag = TARGET_EXPR_SLOT (begin);
9714
9715 TARGET_EXPR_CLEANUP (begin)
9716 = build3 (COND_EXPR, void_type_node, flag,
9717 void_node,
9718 build_call_n (abort_fn, 1, guard_addr));
9719 CLEANUP_EH_ONLY (begin) = 1;
9720
9721 /* Do the initialization itself. */
9722 init = add_stmt_to_compound (begin, init);
9723 init = add_stmt_to_compound (init,
9724 build2 (MODIFY_EXPR, void_type_node,
9725 flag, boolean_true_node));
9726
9727 /* Use atexit to register a function for destroying this static
9728 variable. Do this before calling __cxa_guard_release. */
9729 init = add_stmt_to_compound (init, register_dtor_fn (decl));
9730
9731 init = add_stmt_to_compound (init, build_call_n (release_fn, 1,
9732 guard_addr));
9733 }
9734 else
9735 {
9736 init = add_stmt_to_compound (init, set_guard (guard));
9737
9738 /* Use atexit to register a function for destroying this static
9739 variable. */
9740 init = add_stmt_to_compound (init, register_dtor_fn (decl));
9741 }
9742
9743 finish_expr_stmt (init);
9744
9745 if (thread_guard)
9746 {
9747 finish_compound_stmt (inner_then_clause);
9748 finish_then_clause (inner_if_stmt);
9749 finish_if_stmt (inner_if_stmt);
9750 }
9751
9752 finish_compound_stmt (then_clause);
9753 finish_then_clause (if_stmt);
9754 finish_if_stmt (if_stmt);
9755 }
9756 else if (CP_DECL_THREAD_LOCAL_P (decl))
9757 tls_aggregates = tree_cons (init, decl, tls_aggregates);
9758 else
9759 static_aggregates = tree_cons (init, decl, static_aggregates);
9760 }
9761
9762
9763 /* Make TYPE a complete type based on INITIAL_VALUE.
9764 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
9765 2 if there was no information (in which case assume 0 if DO_DEFAULT),
9766 3 if the initializer list is empty (in pedantic mode). */
9767
9768 int
cp_complete_array_type(tree * ptype,tree initial_value,bool do_default)9769 cp_complete_array_type (tree *ptype, tree initial_value, bool do_default)
9770 {
9771 int failure;
9772 tree type, elt_type;
9773
9774 /* Don't get confused by a CONSTRUCTOR for some other type. */
9775 if (initial_value && TREE_CODE (initial_value) == CONSTRUCTOR
9776 && !BRACE_ENCLOSED_INITIALIZER_P (initial_value)
9777 && TREE_CODE (TREE_TYPE (initial_value)) != ARRAY_TYPE)
9778 return 1;
9779
9780 if (initial_value)
9781 {
9782 /* An array of character type can be initialized from a
9783 brace-enclosed string constant so call reshape_init to
9784 remove the optional braces from a braced string literal. */
9785 if (char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (*ptype)))
9786 && BRACE_ENCLOSED_INITIALIZER_P (initial_value))
9787 initial_value = reshape_init (*ptype, initial_value,
9788 tf_warning_or_error);
9789
9790 /* If any of the elements are parameter packs, we can't actually
9791 complete this type now because the array size is dependent. */
9792 if (TREE_CODE (initial_value) == CONSTRUCTOR)
9793 for (auto &e: CONSTRUCTOR_ELTS (initial_value))
9794 if (PACK_EXPANSION_P (e.value))
9795 return 0;
9796 }
9797
9798 failure = complete_array_type (ptype, initial_value, do_default);
9799
9800 /* We can create the array before the element type is complete, which
9801 means that we didn't have these two bits set in the original type
9802 either. In completing the type, we are expected to propagate these
9803 bits. See also complete_type which does the same thing for arrays
9804 of fixed size. */
9805 type = *ptype;
9806 if (type != error_mark_node && TYPE_DOMAIN (type))
9807 {
9808 elt_type = TREE_TYPE (type);
9809 TYPE_NEEDS_CONSTRUCTING (type) = TYPE_NEEDS_CONSTRUCTING (elt_type);
9810 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
9811 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (elt_type);
9812 }
9813
9814 return failure;
9815 }
9816
9817 /* As above, but either give an error or reject zero-size arrays, depending
9818 on COMPLAIN. */
9819
9820 int
cp_complete_array_type_or_error(tree * ptype,tree initial_value,bool do_default,tsubst_flags_t complain)9821 cp_complete_array_type_or_error (tree *ptype, tree initial_value,
9822 bool do_default, tsubst_flags_t complain)
9823 {
9824 int failure;
9825 bool sfinae = !(complain & tf_error);
9826 /* In SFINAE context we can't be lenient about zero-size arrays. */
9827 if (sfinae)
9828 ++pedantic;
9829 failure = cp_complete_array_type (ptype, initial_value, do_default);
9830 if (sfinae)
9831 --pedantic;
9832 if (failure)
9833 {
9834 if (sfinae)
9835 /* Not an error. */;
9836 else if (failure == 1)
9837 error ("initializer fails to determine size of %qT", *ptype);
9838 else if (failure == 2)
9839 {
9840 if (do_default)
9841 error ("array size missing in %qT", *ptype);
9842 }
9843 else if (failure == 3)
9844 error ("zero-size array %qT", *ptype);
9845 *ptype = error_mark_node;
9846 }
9847 return failure;
9848 }
9849
9850 /* Return zero if something is declared to be a member of type
9851 CTYPE when in the context of CUR_TYPE. STRING is the error
9852 message to print in that case. Otherwise, quietly return 1. */
9853
9854 static int
member_function_or_else(tree ctype,tree cur_type,enum overload_flags flags)9855 member_function_or_else (tree ctype, tree cur_type, enum overload_flags flags)
9856 {
9857 if (ctype && ctype != cur_type)
9858 {
9859 if (flags == DTOR_FLAG)
9860 error ("destructor for alien class %qT cannot be a member", ctype);
9861 else
9862 error ("constructor for alien class %qT cannot be a member", ctype);
9863 return 0;
9864 }
9865 return 1;
9866 }
9867
9868 /* Subroutine of `grokdeclarator'. */
9869
9870 /* Generate errors possibly applicable for a given set of specifiers.
9871 This is for ARM $7.1.2. */
9872
9873 static void
bad_specifiers(tree object,enum bad_spec_place type,int virtualp,int quals,int inlinep,int friendp,int raises,const location_t * locations)9874 bad_specifiers (tree object,
9875 enum bad_spec_place type,
9876 int virtualp,
9877 int quals,
9878 int inlinep,
9879 int friendp,
9880 int raises,
9881 const location_t* locations)
9882 {
9883 switch (type)
9884 {
9885 case BSP_VAR:
9886 if (virtualp)
9887 error_at (locations[ds_virtual],
9888 "%qD declared as a %<virtual%> variable", object);
9889 if (quals)
9890 error ("%<const%> and %<volatile%> function specifiers on "
9891 "%qD invalid in variable declaration", object);
9892 break;
9893 case BSP_PARM:
9894 if (virtualp)
9895 error_at (locations[ds_virtual],
9896 "%qD declared as a %<virtual%> parameter", object);
9897 if (inlinep)
9898 error_at (locations[ds_inline],
9899 "%qD declared as an %<inline%> parameter", object);
9900 if (quals)
9901 error ("%<const%> and %<volatile%> function specifiers on "
9902 "%qD invalid in parameter declaration", object);
9903 break;
9904 case BSP_TYPE:
9905 if (virtualp)
9906 error_at (locations[ds_virtual],
9907 "%qD declared as a %<virtual%> type", object);
9908 if (inlinep)
9909 error_at (locations[ds_inline],
9910 "%qD declared as an %<inline%> type", object);
9911 if (quals)
9912 error ("%<const%> and %<volatile%> function specifiers on "
9913 "%qD invalid in type declaration", object);
9914 break;
9915 case BSP_FIELD:
9916 if (virtualp)
9917 error_at (locations[ds_virtual],
9918 "%qD declared as a %<virtual%> field", object);
9919 if (inlinep)
9920 error_at (locations[ds_inline],
9921 "%qD declared as an %<inline%> field", object);
9922 if (quals)
9923 error ("%<const%> and %<volatile%> function specifiers on "
9924 "%qD invalid in field declaration", object);
9925 break;
9926 default:
9927 gcc_unreachable();
9928 }
9929 if (friendp)
9930 error ("%q+D declared as a friend", object);
9931 if (raises
9932 && !flag_noexcept_type
9933 && (TREE_CODE (object) == TYPE_DECL
9934 || (!TYPE_PTRFN_P (TREE_TYPE (object))
9935 && !TYPE_REFFN_P (TREE_TYPE (object))
9936 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (object)))))
9937 error ("%q+D declared with an exception specification", object);
9938 }
9939
9940 /* DECL is a member function or static data member and is presently
9941 being defined. Check that the definition is taking place in a
9942 valid namespace. */
9943
9944 static void
check_class_member_definition_namespace(tree decl)9945 check_class_member_definition_namespace (tree decl)
9946 {
9947 /* These checks only apply to member functions and static data
9948 members. */
9949 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
9950 /* We check for problems with specializations in pt.cc in
9951 check_specialization_namespace, where we can issue better
9952 diagnostics. */
9953 if (processing_specialization)
9954 return;
9955 /* We check this in check_explicit_instantiation_namespace. */
9956 if (processing_explicit_instantiation)
9957 return;
9958 /* [class.mfct]
9959
9960 A member function definition that appears outside of the
9961 class definition shall appear in a namespace scope enclosing
9962 the class definition.
9963
9964 [class.static.data]
9965
9966 The definition for a static data member shall appear in a
9967 namespace scope enclosing the member's class definition. */
9968 if (!is_ancestor (current_namespace, DECL_CONTEXT (decl)))
9969 permerror (input_location, "definition of %qD is not in namespace enclosing %qT",
9970 decl, DECL_CONTEXT (decl));
9971 }
9972
9973 /* Build a PARM_DECL for the "this" parameter of FN. TYPE is the
9974 METHOD_TYPE for a non-static member function; QUALS are the
9975 cv-qualifiers that apply to the function. */
9976
9977 tree
build_this_parm(tree fn,tree type,cp_cv_quals quals)9978 build_this_parm (tree fn, tree type, cp_cv_quals quals)
9979 {
9980 tree this_type;
9981 tree qual_type;
9982 tree parm;
9983 cp_cv_quals this_quals;
9984
9985 if (CLASS_TYPE_P (type))
9986 {
9987 this_type
9988 = cp_build_qualified_type (type, quals & ~TYPE_QUAL_RESTRICT);
9989 this_type = build_pointer_type (this_type);
9990 }
9991 else
9992 this_type = type_of_this_parm (type);
9993 /* The `this' parameter is implicitly `const'; it cannot be
9994 assigned to. */
9995 this_quals = (quals & TYPE_QUAL_RESTRICT) | TYPE_QUAL_CONST;
9996 qual_type = cp_build_qualified_type (this_type, this_quals);
9997 parm = build_artificial_parm (fn, this_identifier, qual_type);
9998 cp_apply_type_quals_to_decl (this_quals, parm);
9999 return parm;
10000 }
10001
10002 /* DECL is a static member function. Complain if it was declared
10003 with function-cv-quals. */
10004
10005 static void
check_static_quals(tree decl,cp_cv_quals quals)10006 check_static_quals (tree decl, cp_cv_quals quals)
10007 {
10008 if (quals != TYPE_UNQUALIFIED)
10009 error ("static member function %q#D declared with type qualifiers",
10010 decl);
10011 }
10012
10013 // Check that FN takes no arguments and returns bool.
10014 static void
check_concept_fn(tree fn)10015 check_concept_fn (tree fn)
10016 {
10017 // A constraint is nullary.
10018 if (DECL_ARGUMENTS (fn))
10019 error_at (DECL_SOURCE_LOCATION (fn),
10020 "concept %q#D declared with function parameters", fn);
10021
10022 // The declared return type of the concept shall be bool, and
10023 // it shall not be deduced from it definition.
10024 tree type = TREE_TYPE (TREE_TYPE (fn));
10025 if (is_auto (type))
10026 error_at (DECL_SOURCE_LOCATION (fn),
10027 "concept %q#D declared with a deduced return type", fn);
10028 else if (type != boolean_type_node)
10029 error_at (DECL_SOURCE_LOCATION (fn),
10030 "concept %q#D with non-%<bool%> return type %qT", fn, type);
10031 }
10032
10033 /* Helper function. Replace the temporary this parameter injected
10034 during cp_finish_omp_declare_simd with the real this parameter. */
10035
10036 static tree
declare_simd_adjust_this(tree * tp,int * walk_subtrees,void * data)10037 declare_simd_adjust_this (tree *tp, int *walk_subtrees, void *data)
10038 {
10039 tree this_parm = (tree) data;
10040 if (TREE_CODE (*tp) == PARM_DECL
10041 && DECL_NAME (*tp) == this_identifier
10042 && *tp != this_parm)
10043 *tp = this_parm;
10044 else if (TYPE_P (*tp))
10045 *walk_subtrees = 0;
10046 return NULL_TREE;
10047 }
10048
10049 /* CTYPE is class type, or null if non-class.
10050 TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE
10051 or METHOD_TYPE.
10052 DECLARATOR is the function's name.
10053 PARMS is a chain of PARM_DECLs for the function.
10054 VIRTUALP is truthvalue of whether the function is virtual or not.
10055 FLAGS are to be passed through to `grokclassfn'.
10056 QUALS are qualifiers indicating whether the function is `const'
10057 or `volatile'.
10058 RAISES is a list of exceptions that this function can raise.
10059 CHECK is 1 if we must find this method in CTYPE, 0 if we should
10060 not look, and -1 if we should not call `grokclassfn' at all.
10061
10062 SFK is the kind of special function (if any) for the new function.
10063
10064 Returns `NULL_TREE' if something goes wrong, after issuing
10065 applicable error messages. */
10066
10067 static tree
grokfndecl(tree ctype,tree type,tree declarator,tree parms,tree orig_declarator,const cp_decl_specifier_seq * declspecs,tree decl_reqs,int virtualp,enum overload_flags flags,cp_cv_quals quals,cp_ref_qualifier rqual,tree raises,int check,int friendp,int publicp,int inlinep,bool deletedp,special_function_kind sfk,bool funcdef_flag,bool late_return_type_p,int template_count,tree in_namespace,tree * attrlist,location_t location)10068 grokfndecl (tree ctype,
10069 tree type,
10070 tree declarator,
10071 tree parms,
10072 tree orig_declarator,
10073 const cp_decl_specifier_seq *declspecs,
10074 tree decl_reqs,
10075 int virtualp,
10076 enum overload_flags flags,
10077 cp_cv_quals quals,
10078 cp_ref_qualifier rqual,
10079 tree raises,
10080 int check,
10081 int friendp,
10082 int publicp,
10083 int inlinep,
10084 bool deletedp,
10085 special_function_kind sfk,
10086 bool funcdef_flag,
10087 bool late_return_type_p,
10088 int template_count,
10089 tree in_namespace,
10090 tree* attrlist,
10091 location_t location)
10092 {
10093 tree decl;
10094 int staticp = ctype && TREE_CODE (type) == FUNCTION_TYPE;
10095 tree t;
10096
10097 if (location == UNKNOWN_LOCATION)
10098 location = input_location;
10099
10100 /* Was the concept specifier present? */
10101 bool concept_p = inlinep & 4;
10102
10103 /* Concept declarations must have a corresponding definition. */
10104 if (concept_p && !funcdef_flag)
10105 {
10106 error_at (location, "concept %qD has no definition", declarator);
10107 return NULL_TREE;
10108 }
10109
10110 type = build_cp_fntype_variant (type, rqual, raises, late_return_type_p);
10111
10112 decl = build_lang_decl_loc (location, FUNCTION_DECL, declarator, type);
10113
10114 /* Set the constraints on the declaration. */
10115 if (flag_concepts)
10116 {
10117 tree tmpl_reqs = NULL_TREE;
10118 tree ctx = friendp ? current_class_type : ctype;
10119 bool block_local = TREE_CODE (current_scope ()) == FUNCTION_DECL;
10120 bool memtmpl = (!block_local
10121 && (current_template_depth
10122 > template_class_depth (ctx)));
10123 if (memtmpl)
10124 {
10125 if (!current_template_parms)
10126 /* If there are no template parameters, something must have
10127 gone wrong. */
10128 gcc_assert (seen_error ());
10129 else
10130 tmpl_reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
10131 }
10132 tree ci = build_constraints (tmpl_reqs, decl_reqs);
10133 if (concept_p && ci)
10134 {
10135 error_at (location, "a function concept cannot be constrained");
10136 ci = NULL_TREE;
10137 }
10138 /* C++20 CA378: Remove non-templated constrained functions. */
10139 if (ci
10140 && (block_local
10141 || (!flag_concepts_ts
10142 && (!processing_template_decl
10143 || (friendp && !memtmpl && !funcdef_flag)))))
10144 {
10145 error_at (location, "constraints on a non-templated function");
10146 ci = NULL_TREE;
10147 }
10148 set_constraints (decl, ci);
10149 }
10150
10151 if (TREE_CODE (type) == METHOD_TYPE)
10152 {
10153 tree parm = build_this_parm (decl, type, quals);
10154 DECL_CHAIN (parm) = parms;
10155 parms = parm;
10156
10157 /* Allocate space to hold the vptr bit if needed. */
10158 SET_DECL_ALIGN (decl, MINIMUM_METHOD_BOUNDARY);
10159 }
10160
10161 DECL_ARGUMENTS (decl) = parms;
10162 for (t = parms; t; t = DECL_CHAIN (t))
10163 DECL_CONTEXT (t) = decl;
10164
10165 /* Propagate volatile out from type to decl. */
10166 if (TYPE_VOLATILE (type))
10167 TREE_THIS_VOLATILE (decl) = 1;
10168
10169 /* Setup decl according to sfk. */
10170 switch (sfk)
10171 {
10172 case sfk_constructor:
10173 case sfk_copy_constructor:
10174 case sfk_move_constructor:
10175 DECL_CXX_CONSTRUCTOR_P (decl) = 1;
10176 DECL_NAME (decl) = ctor_identifier;
10177 break;
10178 case sfk_destructor:
10179 DECL_CXX_DESTRUCTOR_P (decl) = 1;
10180 DECL_NAME (decl) = dtor_identifier;
10181 break;
10182 default:
10183 break;
10184 }
10185
10186 if (friendp && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)
10187 {
10188 if (funcdef_flag)
10189 error_at (location,
10190 "defining explicit specialization %qD in friend declaration",
10191 orig_declarator);
10192 else
10193 {
10194 tree fns = TREE_OPERAND (orig_declarator, 0);
10195 tree args = TREE_OPERAND (orig_declarator, 1);
10196
10197 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
10198 {
10199 /* Something like `template <class T> friend void f<T>()'. */
10200 error_at (location,
10201 "invalid use of template-id %qD in declaration "
10202 "of primary template",
10203 orig_declarator);
10204 return NULL_TREE;
10205 }
10206
10207
10208 /* A friend declaration of the form friend void f<>(). Record
10209 the information in the TEMPLATE_ID_EXPR. */
10210 SET_DECL_IMPLICIT_INSTANTIATION (decl);
10211
10212 gcc_assert (identifier_p (fns) || OVL_P (fns));
10213 DECL_TEMPLATE_INFO (decl) = build_template_info (fns, args);
10214
10215 for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t))
10216 if (TREE_PURPOSE (t)
10217 && TREE_CODE (TREE_PURPOSE (t)) == DEFERRED_PARSE)
10218 {
10219 error_at (defparse_location (TREE_PURPOSE (t)),
10220 "default arguments are not allowed in declaration "
10221 "of friend template specialization %qD",
10222 decl);
10223 return NULL_TREE;
10224 }
10225
10226 if (inlinep & 1)
10227 {
10228 error_at (declspecs->locations[ds_inline],
10229 "%<inline%> is not allowed in declaration of friend "
10230 "template specialization %qD",
10231 decl);
10232 return NULL_TREE;
10233 }
10234 }
10235 }
10236
10237 /* C++17 11.3.6/4: "If a friend declaration specifies a default argument
10238 expression, that declaration shall be a definition..." */
10239 if (friendp && !funcdef_flag)
10240 {
10241 for (tree t = FUNCTION_FIRST_USER_PARMTYPE (decl);
10242 t && t != void_list_node; t = TREE_CHAIN (t))
10243 if (TREE_PURPOSE (t))
10244 {
10245 permerror (DECL_SOURCE_LOCATION (decl),
10246 "friend declaration of %qD specifies default "
10247 "arguments and isn%'t a definition", decl);
10248 break;
10249 }
10250 }
10251
10252 /* If this decl has namespace scope, set that up. */
10253 if (in_namespace)
10254 set_decl_namespace (decl, in_namespace, friendp);
10255 else if (ctype)
10256 DECL_CONTEXT (decl) = ctype;
10257 else
10258 DECL_CONTEXT (decl) = FROB_CONTEXT (current_decl_namespace ());
10259
10260 /* `main' and builtins have implicit 'C' linkage. */
10261 if (ctype == NULL_TREE
10262 && DECL_FILE_SCOPE_P (decl)
10263 && current_lang_name == lang_name_cplusplus
10264 && (MAIN_NAME_P (declarator)
10265 || (IDENTIFIER_LENGTH (declarator) > 10
10266 && IDENTIFIER_POINTER (declarator)[0] == '_'
10267 && IDENTIFIER_POINTER (declarator)[1] == '_'
10268 && startswith (IDENTIFIER_POINTER (declarator) + 2,
10269 "builtin_"))
10270 || (targetcm.cxx_implicit_extern_c
10271 && (targetcm.cxx_implicit_extern_c
10272 (IDENTIFIER_POINTER (declarator))))))
10273 SET_DECL_LANGUAGE (decl, lang_c);
10274
10275 /* Should probably propagate const out from type to decl I bet (mrs). */
10276 if (staticp)
10277 {
10278 DECL_STATIC_FUNCTION_P (decl) = 1;
10279 DECL_CONTEXT (decl) = ctype;
10280 }
10281
10282 if (deletedp)
10283 DECL_DELETED_FN (decl) = 1;
10284
10285 if (ctype && funcdef_flag)
10286 check_class_member_definition_namespace (decl);
10287
10288 if (ctype == NULL_TREE && DECL_MAIN_P (decl))
10289 {
10290 if (PROCESSING_REAL_TEMPLATE_DECL_P())
10291 error_at (location, "cannot declare %<::main%> to be a template");
10292 if (inlinep & 1)
10293 error_at (declspecs->locations[ds_inline],
10294 "cannot declare %<::main%> to be inline");
10295 if (inlinep & 2)
10296 error_at (declspecs->locations[ds_constexpr],
10297 "cannot declare %<::main%> to be %qs", "constexpr");
10298 if (inlinep & 8)
10299 error_at (declspecs->locations[ds_consteval],
10300 "cannot declare %<::main%> to be %qs", "consteval");
10301 if (!publicp)
10302 error_at (location, "cannot declare %<::main%> to be static");
10303 inlinep = 0;
10304 publicp = 1;
10305 }
10306
10307 /* Members of anonymous types and local classes have no linkage; make
10308 them internal. If a typedef is made later, this will be changed. */
10309 if (ctype && (!TREE_PUBLIC (TYPE_MAIN_DECL (ctype))
10310 || decl_function_context (TYPE_MAIN_DECL (ctype))))
10311 publicp = 0;
10312
10313 if (publicp && cxx_dialect == cxx98)
10314 {
10315 /* [basic.link]: A name with no linkage (notably, the name of a class
10316 or enumeration declared in a local scope) shall not be used to
10317 declare an entity with linkage.
10318
10319 DR 757 relaxes this restriction for C++0x. */
10320 no_linkage_error (decl);
10321 }
10322
10323 TREE_PUBLIC (decl) = publicp;
10324 if (! publicp)
10325 {
10326 DECL_INTERFACE_KNOWN (decl) = 1;
10327 DECL_NOT_REALLY_EXTERN (decl) = 1;
10328 }
10329
10330 /* If the declaration was declared inline, mark it as such. */
10331 if (inlinep)
10332 {
10333 DECL_DECLARED_INLINE_P (decl) = 1;
10334 if (publicp)
10335 DECL_COMDAT (decl) = 1;
10336 }
10337 if (inlinep & 2)
10338 DECL_DECLARED_CONSTEXPR_P (decl) = true;
10339 else if (inlinep & 8)
10340 {
10341 DECL_DECLARED_CONSTEXPR_P (decl) = true;
10342 SET_DECL_IMMEDIATE_FUNCTION_P (decl);
10343 }
10344
10345 // If the concept declaration specifier was found, check
10346 // that the declaration satisfies the necessary requirements.
10347 if (concept_p)
10348 {
10349 DECL_DECLARED_CONCEPT_P (decl) = true;
10350 check_concept_fn (decl);
10351 }
10352
10353 DECL_EXTERNAL (decl) = 1;
10354 if (TREE_CODE (type) == FUNCTION_TYPE)
10355 {
10356 if (quals || rqual)
10357 TREE_TYPE (decl) = apply_memfn_quals (TREE_TYPE (decl),
10358 TYPE_UNQUALIFIED,
10359 REF_QUAL_NONE);
10360
10361 if (quals)
10362 {
10363 error (ctype
10364 ? G_("static member function %qD cannot have cv-qualifier")
10365 : G_("non-member function %qD cannot have cv-qualifier"),
10366 decl);
10367 quals = TYPE_UNQUALIFIED;
10368 }
10369
10370 if (rqual)
10371 {
10372 error (ctype
10373 ? G_("static member function %qD cannot have ref-qualifier")
10374 : G_("non-member function %qD cannot have ref-qualifier"),
10375 decl);
10376 rqual = REF_QUAL_NONE;
10377 }
10378 }
10379
10380 if (deduction_guide_p (decl))
10381 {
10382 tree type = TREE_TYPE (DECL_NAME (decl));
10383 if (in_namespace == NULL_TREE
10384 && CP_DECL_CONTEXT (decl) != CP_TYPE_CONTEXT (type))
10385 {
10386 error_at (location, "deduction guide %qD must be declared in the "
10387 "same scope as %qT", decl, type);
10388 inform (location_of (type), " declared here");
10389 return NULL_TREE;
10390 }
10391 if (DECL_CLASS_SCOPE_P (decl)
10392 && current_access_specifier != declared_access (TYPE_NAME (type)))
10393 {
10394 error_at (location, "deduction guide %qD must have the same access "
10395 "as %qT", decl, type);
10396 inform (location_of (type), " declared here");
10397 }
10398 if (funcdef_flag)
10399 error_at (location,
10400 "deduction guide %qD must not have a function body", decl);
10401 }
10402 else if (IDENTIFIER_ANY_OP_P (DECL_NAME (decl))
10403 && !grok_op_properties (decl, /*complain=*/true))
10404 return NULL_TREE;
10405 else if (UDLIT_OPER_P (DECL_NAME (decl)))
10406 {
10407 bool long_long_unsigned_p;
10408 bool long_double_p;
10409 const char *suffix = NULL;
10410 /* [over.literal]/6: Literal operators shall not have C linkage. */
10411 if (DECL_LANGUAGE (decl) == lang_c)
10412 {
10413 error_at (location, "literal operator with C linkage");
10414 maybe_show_extern_c_location ();
10415 return NULL_TREE;
10416 }
10417
10418 if (DECL_NAMESPACE_SCOPE_P (decl))
10419 {
10420 if (!check_literal_operator_args (decl, &long_long_unsigned_p,
10421 &long_double_p))
10422 {
10423 error_at (location, "%qD has invalid argument list", decl);
10424 return NULL_TREE;
10425 }
10426
10427 suffix = UDLIT_OP_SUFFIX (DECL_NAME (decl));
10428 if (long_long_unsigned_p)
10429 {
10430 if (cpp_interpret_int_suffix (parse_in, suffix, strlen (suffix)))
10431 warning_at (location, 0, "integer suffix %qs"
10432 " shadowed by implementation", suffix);
10433 }
10434 else if (long_double_p)
10435 {
10436 if (cpp_interpret_float_suffix (parse_in, suffix, strlen (suffix)))
10437 warning_at (location, 0, "floating-point suffix %qs"
10438 " shadowed by implementation", suffix);
10439 }
10440 /* 17.6.3.3.5 */
10441 if (suffix[0] != '_'
10442 && !current_function_decl && !(friendp && !funcdef_flag))
10443 warning_at (location, OPT_Wliteral_suffix,
10444 "literal operator suffixes not preceded by %<_%>"
10445 " are reserved for future standardization");
10446 }
10447 else
10448 {
10449 error_at (location, "%qD must be a non-member function", decl);
10450 return NULL_TREE;
10451 }
10452 }
10453
10454 if (funcdef_flag)
10455 /* Make the init_value nonzero so pushdecl knows this is not
10456 tentative. error_mark_node is replaced later with the BLOCK. */
10457 DECL_INITIAL (decl) = error_mark_node;
10458
10459 if (TYPE_NOTHROW_P (type) || nothrow_libfn_p (decl))
10460 TREE_NOTHROW (decl) = 1;
10461
10462 if (flag_openmp || flag_openmp_simd)
10463 {
10464 /* Adjust "omp declare simd" attributes. */
10465 tree ods = lookup_attribute ("omp declare simd", *attrlist);
10466 if (ods)
10467 {
10468 tree attr;
10469 for (attr = ods; attr;
10470 attr = lookup_attribute ("omp declare simd", TREE_CHAIN (attr)))
10471 {
10472 if (TREE_CODE (type) == METHOD_TYPE)
10473 walk_tree (&TREE_VALUE (attr), declare_simd_adjust_this,
10474 DECL_ARGUMENTS (decl), NULL);
10475 if (TREE_VALUE (attr) != NULL_TREE)
10476 {
10477 tree cl = TREE_VALUE (TREE_VALUE (attr));
10478 cl = c_omp_declare_simd_clauses_to_numbers
10479 (DECL_ARGUMENTS (decl), cl);
10480 if (cl)
10481 TREE_VALUE (TREE_VALUE (attr)) = cl;
10482 else
10483 TREE_VALUE (attr) = NULL_TREE;
10484 }
10485 }
10486 }
10487 }
10488
10489 /* Caller will do the rest of this. */
10490 if (check < 0)
10491 return decl;
10492
10493 if (ctype != NULL_TREE)
10494 grokclassfn (ctype, decl, flags);
10495
10496 /* 12.4/3 */
10497 if (cxx_dialect >= cxx11
10498 && DECL_DESTRUCTOR_P (decl)
10499 && !TYPE_BEING_DEFINED (DECL_CONTEXT (decl))
10500 && !processing_template_decl)
10501 deduce_noexcept_on_destructor (decl);
10502
10503 set_originating_module (decl);
10504
10505 decl = check_explicit_specialization (orig_declarator, decl,
10506 template_count,
10507 2 * funcdef_flag +
10508 4 * (friendp != 0) +
10509 8 * concept_p,
10510 *attrlist);
10511 if (decl == error_mark_node)
10512 return NULL_TREE;
10513
10514 if (DECL_STATIC_FUNCTION_P (decl))
10515 check_static_quals (decl, quals);
10516
10517 if (attrlist)
10518 {
10519 cplus_decl_attributes (&decl, *attrlist, 0);
10520 *attrlist = NULL_TREE;
10521 }
10522
10523 /* Check main's type after attributes have been applied. */
10524 if (ctype == NULL_TREE && DECL_MAIN_P (decl))
10525 {
10526 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
10527 integer_type_node))
10528 {
10529 tree oldtypeargs = TYPE_ARG_TYPES (TREE_TYPE (decl));
10530 tree newtype;
10531 error_at (declspecs->locations[ds_type_spec],
10532 "%<::main%> must return %<int%>");
10533 newtype = build_function_type (integer_type_node, oldtypeargs);
10534 TREE_TYPE (decl) = newtype;
10535 }
10536 if (warn_main)
10537 check_main_parameter_types (decl);
10538 }
10539
10540 if (ctype != NULL_TREE && check)
10541 {
10542 tree old_decl = check_classfn (ctype, decl,
10543 (current_template_depth
10544 > template_class_depth (ctype))
10545 ? current_template_parms
10546 : NULL_TREE);
10547
10548 if (old_decl == error_mark_node)
10549 return NULL_TREE;
10550
10551 if (old_decl)
10552 {
10553 tree ok;
10554 tree pushed_scope;
10555
10556 if (TREE_CODE (old_decl) == TEMPLATE_DECL)
10557 /* Because grokfndecl is always supposed to return a
10558 FUNCTION_DECL, we pull out the DECL_TEMPLATE_RESULT
10559 here. We depend on our callers to figure out that its
10560 really a template that's being returned. */
10561 old_decl = DECL_TEMPLATE_RESULT (old_decl);
10562
10563 if (DECL_STATIC_FUNCTION_P (old_decl)
10564 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
10565 {
10566 /* Remove the `this' parm added by grokclassfn. */
10567 revert_static_member_fn (decl);
10568 check_static_quals (decl, quals);
10569 }
10570 if (DECL_ARTIFICIAL (old_decl))
10571 {
10572 error ("definition of implicitly-declared %qD", old_decl);
10573 return NULL_TREE;
10574 }
10575 else if (DECL_DEFAULTED_FN (old_decl))
10576 {
10577 error ("definition of explicitly-defaulted %q+D", decl);
10578 inform (DECL_SOURCE_LOCATION (old_decl),
10579 "%q#D explicitly defaulted here", old_decl);
10580 return NULL_TREE;
10581 }
10582
10583 /* Since we've smashed OLD_DECL to its
10584 DECL_TEMPLATE_RESULT, we must do the same to DECL. */
10585 if (TREE_CODE (decl) == TEMPLATE_DECL)
10586 decl = DECL_TEMPLATE_RESULT (decl);
10587
10588 /* Attempt to merge the declarations. This can fail, in
10589 the case of some invalid specialization declarations. */
10590 pushed_scope = push_scope (ctype);
10591 ok = duplicate_decls (decl, old_decl);
10592 if (pushed_scope)
10593 pop_scope (pushed_scope);
10594 if (!ok)
10595 {
10596 error ("no %q#D member function declared in class %qT",
10597 decl, ctype);
10598 return NULL_TREE;
10599 }
10600 if (ok == error_mark_node)
10601 return NULL_TREE;
10602 return old_decl;
10603 }
10604 }
10605
10606 if (DECL_CONSTRUCTOR_P (decl) && !grok_ctor_properties (ctype, decl))
10607 return NULL_TREE;
10608
10609 if (ctype == NULL_TREE || check)
10610 return decl;
10611
10612 if (virtualp)
10613 DECL_VIRTUAL_P (decl) = 1;
10614
10615 return decl;
10616 }
10617
10618 /* decl is a FUNCTION_DECL.
10619 specifiers are the parsed virt-specifiers.
10620
10621 Set flags to reflect the virt-specifiers.
10622
10623 Returns decl. */
10624
10625 static tree
set_virt_specifiers(tree decl,cp_virt_specifiers specifiers)10626 set_virt_specifiers (tree decl, cp_virt_specifiers specifiers)
10627 {
10628 if (decl == NULL_TREE)
10629 return decl;
10630 if (specifiers & VIRT_SPEC_OVERRIDE)
10631 DECL_OVERRIDE_P (decl) = 1;
10632 if (specifiers & VIRT_SPEC_FINAL)
10633 DECL_FINAL_P (decl) = 1;
10634 return decl;
10635 }
10636
10637 /* DECL is a VAR_DECL for a static data member. Set flags to reflect
10638 the linkage that DECL will receive in the object file. */
10639
10640 static void
set_linkage_for_static_data_member(tree decl)10641 set_linkage_for_static_data_member (tree decl)
10642 {
10643 /* A static data member always has static storage duration and
10644 external linkage. Note that static data members are forbidden in
10645 local classes -- the only situation in which a class has
10646 non-external linkage. */
10647 TREE_PUBLIC (decl) = 1;
10648 TREE_STATIC (decl) = 1;
10649 /* For non-template classes, static data members are always put
10650 out in exactly those files where they are defined, just as
10651 with ordinary namespace-scope variables. */
10652 if (!processing_template_decl)
10653 DECL_INTERFACE_KNOWN (decl) = 1;
10654 }
10655
10656 /* Create a VAR_DECL named NAME with the indicated TYPE.
10657
10658 If SCOPE is non-NULL, it is the class type or namespace containing
10659 the variable. If SCOPE is NULL, the variable should is created in
10660 the innermost enclosing scope. */
10661
10662 static tree
grokvardecl(tree type,tree name,tree orig_declarator,const cp_decl_specifier_seq * declspecs,int initialized,int type_quals,int inlinep,bool conceptp,int template_count,tree scope,location_t location)10663 grokvardecl (tree type,
10664 tree name,
10665 tree orig_declarator,
10666 const cp_decl_specifier_seq *declspecs,
10667 int initialized,
10668 int type_quals,
10669 int inlinep,
10670 bool conceptp,
10671 int template_count,
10672 tree scope,
10673 location_t location)
10674 {
10675 tree decl;
10676 tree explicit_scope;
10677
10678 gcc_assert (!name || identifier_p (name));
10679
10680 bool constp = (type_quals & TYPE_QUAL_CONST) != 0;
10681 bool volatilep = (type_quals & TYPE_QUAL_VOLATILE) != 0;
10682
10683 /* Compute the scope in which to place the variable, but remember
10684 whether or not that scope was explicitly specified by the user. */
10685 explicit_scope = scope;
10686 if (!scope)
10687 {
10688 /* An explicit "extern" specifier indicates a namespace-scope
10689 variable. */
10690 if (declspecs->storage_class == sc_extern)
10691 scope = current_decl_namespace ();
10692 else if (!at_function_scope_p ())
10693 scope = current_scope ();
10694 }
10695
10696 if (scope
10697 && (/* If the variable is a namespace-scope variable declared in a
10698 template, we need DECL_LANG_SPECIFIC. */
10699 (TREE_CODE (scope) == NAMESPACE_DECL && processing_template_decl)
10700 /* Similarly for namespace-scope variables with language linkage
10701 other than C++. */
10702 || (TREE_CODE (scope) == NAMESPACE_DECL
10703 && current_lang_name != lang_name_cplusplus)
10704 /* Similarly for static data members. */
10705 || TYPE_P (scope)
10706 /* Similarly for explicit specializations. */
10707 || (orig_declarator
10708 && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)))
10709 decl = build_lang_decl_loc (location, VAR_DECL, name, type);
10710 else
10711 decl = build_decl (location, VAR_DECL, name, type);
10712
10713 if (explicit_scope && TREE_CODE (explicit_scope) == NAMESPACE_DECL)
10714 set_decl_namespace (decl, explicit_scope, 0);
10715 else
10716 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
10717
10718 if (declspecs->storage_class == sc_extern)
10719 {
10720 DECL_THIS_EXTERN (decl) = 1;
10721 DECL_EXTERNAL (decl) = !initialized;
10722 }
10723
10724 if (DECL_CLASS_SCOPE_P (decl))
10725 {
10726 set_linkage_for_static_data_member (decl);
10727 /* This function is only called with out-of-class definitions. */
10728 DECL_EXTERNAL (decl) = 0;
10729 check_class_member_definition_namespace (decl);
10730 }
10731 /* At top level, either `static' or no s.c. makes a definition
10732 (perhaps tentative), and absence of `static' makes it public. */
10733 else if (toplevel_bindings_p ())
10734 {
10735 TREE_PUBLIC (decl) = (declspecs->storage_class != sc_static
10736 && (DECL_THIS_EXTERN (decl)
10737 || ! constp
10738 || volatilep
10739 || inlinep));
10740 TREE_STATIC (decl) = ! DECL_EXTERNAL (decl);
10741 }
10742 /* Not at top level, only `static' makes a static definition. */
10743 else
10744 {
10745 TREE_STATIC (decl) = declspecs->storage_class == sc_static;
10746 TREE_PUBLIC (decl) = DECL_EXTERNAL (decl);
10747 }
10748
10749 set_originating_module (decl);
10750
10751 if (decl_spec_seq_has_spec_p (declspecs, ds_thread))
10752 {
10753 if (DECL_EXTERNAL (decl) || TREE_STATIC (decl))
10754 {
10755 CP_DECL_THREAD_LOCAL_P (decl) = true;
10756 if (!processing_template_decl)
10757 set_decl_tls_model (decl, decl_default_tls_model (decl));
10758 }
10759 if (declspecs->gnu_thread_keyword_p)
10760 SET_DECL_GNU_TLS_P (decl);
10761 }
10762
10763 /* If the type of the decl has no linkage, make sure that we'll
10764 notice that in mark_used. */
10765 if (cxx_dialect > cxx98
10766 && decl_linkage (decl) != lk_none
10767 && DECL_LANG_SPECIFIC (decl) == NULL
10768 && !DECL_EXTERN_C_P (decl)
10769 && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
10770 retrofit_lang_decl (decl);
10771
10772 if (TREE_PUBLIC (decl))
10773 {
10774 /* [basic.link]: A name with no linkage (notably, the name of a class
10775 or enumeration declared in a local scope) shall not be used to
10776 declare an entity with linkage.
10777
10778 DR 757 relaxes this restriction for C++0x. */
10779 if (cxx_dialect < cxx11)
10780 no_linkage_error (decl);
10781 }
10782 else
10783 DECL_INTERFACE_KNOWN (decl) = 1;
10784
10785 if (DECL_NAME (decl)
10786 && MAIN_NAME_P (DECL_NAME (decl))
10787 && scope == global_namespace)
10788 error_at (DECL_SOURCE_LOCATION (decl),
10789 "cannot declare %<::main%> to be a global variable");
10790
10791 /* Check that the variable can be safely declared as a concept.
10792 Note that this also forbids explicit specializations. */
10793 if (conceptp)
10794 {
10795 if (!processing_template_decl)
10796 {
10797 error_at (declspecs->locations[ds_concept],
10798 "a non-template variable cannot be %<concept%>");
10799 return NULL_TREE;
10800 }
10801 else if (!at_namespace_scope_p ())
10802 {
10803 error_at (declspecs->locations[ds_concept],
10804 "concept must be defined at namespace scope");
10805 return NULL_TREE;
10806 }
10807 else
10808 DECL_DECLARED_CONCEPT_P (decl) = true;
10809 if (!same_type_ignoring_top_level_qualifiers_p (type, boolean_type_node))
10810 error_at (declspecs->locations[ds_type_spec],
10811 "concept must have type %<bool%>");
10812 if (TEMPLATE_PARMS_CONSTRAINTS (current_template_parms))
10813 {
10814 error_at (location, "a variable concept cannot be constrained");
10815 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = NULL_TREE;
10816 }
10817 }
10818 else if (flag_concepts
10819 && current_template_depth > template_class_depth (scope))
10820 {
10821 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
10822 tree ci = build_constraints (reqs, NULL_TREE);
10823
10824 set_constraints (decl, ci);
10825 }
10826
10827 // Handle explicit specializations and instantiations of variable templates.
10828 if (orig_declarator)
10829 decl = check_explicit_specialization (orig_declarator, decl,
10830 template_count, conceptp * 8);
10831
10832 return decl != error_mark_node ? decl : NULL_TREE;
10833 }
10834
10835 /* Create and return a canonical pointer to member function type, for
10836 TYPE, which is a POINTER_TYPE to a METHOD_TYPE. */
10837
10838 tree
build_ptrmemfunc_type(tree type)10839 build_ptrmemfunc_type (tree type)
10840 {
10841 tree field, fields;
10842 tree t;
10843
10844 if (type == error_mark_node)
10845 return type;
10846
10847 /* Make sure that we always have the unqualified pointer-to-member
10848 type first. */
10849 if (cp_cv_quals quals = cp_type_quals (type))
10850 {
10851 tree unqual = build_ptrmemfunc_type (TYPE_MAIN_VARIANT (type));
10852 return cp_build_qualified_type (unqual, quals);
10853 }
10854
10855 /* If a canonical type already exists for this type, use it. We use
10856 this method instead of type_hash_canon, because it only does a
10857 simple equality check on the list of field members. */
10858
10859 t = TYPE_PTRMEMFUNC_TYPE (type);
10860 if (t)
10861 return t;
10862
10863 t = make_node (RECORD_TYPE);
10864
10865 /* Let the front end know this is a pointer to member function. */
10866 TYPE_PTRMEMFUNC_FLAG (t) = 1;
10867
10868 field = build_decl (input_location, FIELD_DECL, pfn_identifier, type);
10869 DECL_NONADDRESSABLE_P (field) = 1;
10870 fields = field;
10871
10872 field = build_decl (input_location, FIELD_DECL, delta_identifier,
10873 delta_type_node);
10874 DECL_NONADDRESSABLE_P (field) = 1;
10875 DECL_CHAIN (field) = fields;
10876 fields = field;
10877
10878 finish_builtin_struct (t, "__ptrmemfunc_type", fields, ptr_type_node);
10879
10880 /* Zap out the name so that the back end will give us the debugging
10881 information for this anonymous RECORD_TYPE. */
10882 TYPE_NAME (t) = NULL_TREE;
10883
10884 /* Cache this pointer-to-member type so that we can find it again
10885 later. */
10886 TYPE_PTRMEMFUNC_TYPE (type) = t;
10887
10888 if (TYPE_STRUCTURAL_EQUALITY_P (type))
10889 SET_TYPE_STRUCTURAL_EQUALITY (t);
10890 else if (TYPE_CANONICAL (type) != type)
10891 TYPE_CANONICAL (t) = build_ptrmemfunc_type (TYPE_CANONICAL (type));
10892
10893 return t;
10894 }
10895
10896 /* Create and return a pointer to data member type. */
10897
10898 tree
build_ptrmem_type(tree class_type,tree member_type)10899 build_ptrmem_type (tree class_type, tree member_type)
10900 {
10901 if (TREE_CODE (member_type) == METHOD_TYPE)
10902 {
10903 cp_cv_quals quals = type_memfn_quals (member_type);
10904 cp_ref_qualifier rqual = type_memfn_rqual (member_type);
10905 member_type = build_memfn_type (member_type, class_type, quals, rqual);
10906 return build_ptrmemfunc_type (build_pointer_type (member_type));
10907 }
10908 else
10909 {
10910 gcc_assert (TREE_CODE (member_type) != FUNCTION_TYPE);
10911 return build_offset_type (class_type, member_type);
10912 }
10913 }
10914
10915 /* DECL is a VAR_DECL defined in-class, whose TYPE is also given.
10916 Check to see that the definition is valid. Issue appropriate error
10917 messages. */
10918
10919 static void
check_static_variable_definition(tree decl,tree type)10920 check_static_variable_definition (tree decl, tree type)
10921 {
10922 /* Avoid redundant diagnostics on out-of-class definitions. */
10923 if (!current_class_type || !TYPE_BEING_DEFINED (current_class_type))
10924 ;
10925 /* Can't check yet if we don't know the type. */
10926 else if (dependent_type_p (type))
10927 ;
10928 /* If DECL is declared constexpr, we'll do the appropriate checks
10929 in check_initializer. Similarly for inline static data members. */
10930 else if (DECL_P (decl)
10931 && (DECL_DECLARED_CONSTEXPR_P (decl)
10932 || DECL_VAR_DECLARED_INLINE_P (decl)))
10933 ;
10934 else if (cxx_dialect >= cxx11 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
10935 {
10936 if (!COMPLETE_TYPE_P (type))
10937 error_at (DECL_SOURCE_LOCATION (decl),
10938 "in-class initialization of static data member %q#D of "
10939 "incomplete type", decl);
10940 else if (literal_type_p (type))
10941 permerror (DECL_SOURCE_LOCATION (decl),
10942 "%<constexpr%> needed for in-class initialization of "
10943 "static data member %q#D of non-integral type", decl);
10944 else
10945 error_at (DECL_SOURCE_LOCATION (decl),
10946 "in-class initialization of static data member %q#D of "
10947 "non-literal type", decl);
10948 }
10949 /* Motion 10 at San Diego: If a static const integral data member is
10950 initialized with an integral constant expression, the initializer
10951 may appear either in the declaration (within the class), or in
10952 the definition, but not both. If it appears in the class, the
10953 member is a member constant. The file-scope definition is always
10954 required. */
10955 else if (!ARITHMETIC_TYPE_P (type) && TREE_CODE (type) != ENUMERAL_TYPE)
10956 error_at (DECL_SOURCE_LOCATION (decl),
10957 "invalid in-class initialization of static data member "
10958 "of non-integral type %qT",
10959 type);
10960 else if (!CP_TYPE_CONST_P (type))
10961 error_at (DECL_SOURCE_LOCATION (decl),
10962 "ISO C++ forbids in-class initialization of non-const "
10963 "static member %qD",
10964 decl);
10965 else if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type))
10966 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
10967 "ISO C++ forbids initialization of member constant "
10968 "%qD of non-integral type %qT", decl, type);
10969 }
10970
10971 /* *expr_p is part of the TYPE_SIZE of a variably-sized array. If any
10972 SAVE_EXPRs in *expr_p wrap expressions with side-effects, break those
10973 expressions out into temporary variables so that walk_tree doesn't
10974 step into them (c++/15764). */
10975
10976 static tree
stabilize_save_expr_r(tree * expr_p,int * walk_subtrees,void * data)10977 stabilize_save_expr_r (tree *expr_p, int *walk_subtrees, void *data)
10978 {
10979 hash_set<tree> *pset = (hash_set<tree> *)data;
10980 tree expr = *expr_p;
10981 if (TREE_CODE (expr) == SAVE_EXPR)
10982 {
10983 tree op = TREE_OPERAND (expr, 0);
10984 cp_walk_tree (&op, stabilize_save_expr_r, data, pset);
10985 if (TREE_SIDE_EFFECTS (op))
10986 TREE_OPERAND (expr, 0) = get_temp_regvar (TREE_TYPE (op), op);
10987 *walk_subtrees = 0;
10988 }
10989 else if (!EXPR_P (expr) || !TREE_SIDE_EFFECTS (expr))
10990 *walk_subtrees = 0;
10991 return NULL;
10992 }
10993
10994 /* Entry point for the above. */
10995
10996 static void
stabilize_vla_size(tree size)10997 stabilize_vla_size (tree size)
10998 {
10999 hash_set<tree> pset;
11000 /* Break out any function calls into temporary variables. */
11001 cp_walk_tree (&size, stabilize_save_expr_r, &pset, &pset);
11002 }
11003
11004 /* Reduce a SIZEOF_EXPR to its value. */
11005
11006 tree
fold_sizeof_expr(tree t)11007 fold_sizeof_expr (tree t)
11008 {
11009 tree r;
11010 if (SIZEOF_EXPR_TYPE_P (t))
11011 r = cxx_sizeof_or_alignof_type (EXPR_LOCATION (t),
11012 TREE_TYPE (TREE_OPERAND (t, 0)),
11013 SIZEOF_EXPR, false, false);
11014 else if (TYPE_P (TREE_OPERAND (t, 0)))
11015 r = cxx_sizeof_or_alignof_type (EXPR_LOCATION (t),
11016 TREE_OPERAND (t, 0), SIZEOF_EXPR,
11017 false, false);
11018 else
11019 r = cxx_sizeof_or_alignof_expr (EXPR_LOCATION (t),
11020 TREE_OPERAND (t, 0), SIZEOF_EXPR,
11021 false, false);
11022 if (r == error_mark_node)
11023 r = size_one_node;
11024 return r;
11025 }
11026
11027 /* Given the SIZE (i.e., number of elements) in an array, compute
11028 an appropriate index type for the array. If non-NULL, NAME is
11029 the name of the entity being declared. */
11030
11031 static tree
compute_array_index_type_loc(location_t name_loc,tree name,tree size,tsubst_flags_t complain)11032 compute_array_index_type_loc (location_t name_loc, tree name, tree size,
11033 tsubst_flags_t complain)
11034 {
11035 if (error_operand_p (size))
11036 return error_mark_node;
11037
11038 /* The type of the index being computed. */
11039 tree itype;
11040
11041 /* The original numeric size as seen in the source code before
11042 conversion to size_t. */
11043 tree origsize = size;
11044
11045 location_t loc = cp_expr_loc_or_loc (size, name ? name_loc : input_location);
11046
11047 if (!type_dependent_expression_p (size))
11048 {
11049 origsize = size = mark_rvalue_use (size);
11050
11051 if (cxx_dialect < cxx11 && TREE_CODE (size) == NOP_EXPR
11052 && TREE_SIDE_EFFECTS (size))
11053 /* In C++98, we mark a non-constant array bound with a magic
11054 NOP_EXPR with TREE_SIDE_EFFECTS; don't fold in that case. */;
11055 else
11056 {
11057 size = build_converted_constant_expr (size_type_node, size, complain);
11058 /* Pedantically a constant expression is required here and so
11059 __builtin_is_constant_evaluated () should fold to true if it
11060 is successfully folded into a constant. */
11061 size = fold_non_dependent_expr (size, complain,
11062 /*manifestly_const_eval=*/true);
11063
11064 if (!TREE_CONSTANT (size))
11065 size = origsize;
11066 }
11067
11068 if (error_operand_p (size))
11069 return error_mark_node;
11070
11071 /* The array bound must be an integer type. */
11072 tree type = TREE_TYPE (size);
11073 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
11074 {
11075 if (!(complain & tf_error))
11076 return error_mark_node;
11077 if (name)
11078 error_at (loc, "size of array %qD has non-integral type %qT",
11079 name, type);
11080 else
11081 error_at (loc, "size of array has non-integral type %qT", type);
11082 size = integer_one_node;
11083 }
11084 }
11085
11086 /* A type is dependent if it is...an array type constructed from any
11087 dependent type or whose size is specified by a constant expression
11088 that is value-dependent. */
11089 /* We can only call value_dependent_expression_p on integral constant
11090 expressions. */
11091 if (processing_template_decl
11092 && potential_constant_expression (size)
11093 && value_dependent_expression_p (size))
11094 {
11095 /* Just build the index type and mark that it requires
11096 structural equality checks. */
11097 in_template:
11098 itype = build_index_type (build_min (MINUS_EXPR, sizetype,
11099 size, size_one_node));
11100 TYPE_DEPENDENT_P (itype) = 1;
11101 TYPE_DEPENDENT_P_VALID (itype) = 1;
11102 SET_TYPE_STRUCTURAL_EQUALITY (itype);
11103 return itype;
11104 }
11105
11106 if (TREE_CODE (size) != INTEGER_CST)
11107 {
11108 tree folded = cp_fully_fold (size);
11109 if (TREE_CODE (folded) == INTEGER_CST)
11110 {
11111 if (name)
11112 pedwarn (loc, OPT_Wpedantic, "size of array %qD is not an "
11113 "integral constant-expression", name);
11114 else
11115 pedwarn (loc, OPT_Wpedantic,
11116 "size of array is not an integral constant-expression");
11117 }
11118 if (TREE_CONSTANT (size) && !TREE_CONSTANT (folded))
11119 /* We might have lost the TREE_CONSTANT flag e.g. when we are
11120 folding a conversion from a pointer to integral type. In that
11121 case issue an error below and don't treat this as a VLA. */;
11122 else
11123 /* Use the folded result for VLAs, too; it will have resolved
11124 SIZEOF_EXPR. */
11125 size = folded;
11126 }
11127
11128 /* Normally, the array-bound will be a constant. */
11129 if (TREE_CODE (size) == INTEGER_CST)
11130 {
11131 /* The size to use in diagnostics that reflects the constant
11132 size used in the source, rather than SIZE massaged above. */
11133 tree diagsize = size;
11134
11135 /* If the original size before conversion to size_t was signed
11136 and negative, convert it to ssizetype to restore the sign. */
11137 if (!TYPE_UNSIGNED (TREE_TYPE (origsize))
11138 && TREE_CODE (size) == INTEGER_CST
11139 && tree_int_cst_sign_bit (size))
11140 {
11141 diagsize = fold_convert (ssizetype, size);
11142
11143 /* Clear the overflow bit that may have been set as a result
11144 of the conversion from the sizetype of the new size to
11145 ssizetype. */
11146 TREE_OVERFLOW (diagsize) = false;
11147 }
11148
11149 /* Verify that the array has a positive number of elements
11150 and issue the appropriate diagnostic if it doesn't. */
11151 if (!valid_array_size_p (loc, diagsize, name, (complain & tf_error)))
11152 {
11153 if (!(complain & tf_error))
11154 return error_mark_node;
11155 size = integer_one_node;
11156 }
11157 /* As an extension we allow zero-sized arrays. */
11158 else if (integer_zerop (size))
11159 {
11160 if (!(complain & tf_error))
11161 /* We must fail if performing argument deduction (as
11162 indicated by the state of complain), so that
11163 another substitution can be found. */
11164 return error_mark_node;
11165 else if (name)
11166 pedwarn (loc, OPT_Wpedantic,
11167 "ISO C++ forbids zero-size array %qD", name);
11168 else
11169 pedwarn (loc, OPT_Wpedantic,
11170 "ISO C++ forbids zero-size array");
11171 }
11172 }
11173 else if (TREE_CONSTANT (size)
11174 /* We don't allow VLAs at non-function scopes, or during
11175 tentative template substitution. */
11176 || !at_function_scope_p ()
11177 || !(complain & tf_error))
11178 {
11179 if (!(complain & tf_error))
11180 return error_mark_node;
11181 /* `(int) &fn' is not a valid array bound. */
11182 if (name)
11183 error_at (loc,
11184 "size of array %qD is not an integral constant-expression",
11185 name);
11186 else
11187 error_at (loc, "size of array is not an integral constant-expression");
11188 size = integer_one_node;
11189 }
11190 else if (pedantic && warn_vla != 0)
11191 {
11192 if (name)
11193 pedwarn (name_loc, OPT_Wvla,
11194 "ISO C++ forbids variable length array %qD", name);
11195 else
11196 pedwarn (input_location, OPT_Wvla,
11197 "ISO C++ forbids variable length array");
11198 }
11199 else if (warn_vla > 0)
11200 {
11201 if (name)
11202 warning_at (name_loc, OPT_Wvla,
11203 "variable length array %qD is used", name);
11204 else
11205 warning (OPT_Wvla,
11206 "variable length array is used");
11207 }
11208
11209 if (processing_template_decl && !TREE_CONSTANT (size))
11210 goto in_template;
11211 else
11212 {
11213 if (!TREE_CONSTANT (size))
11214 {
11215 /* A variable sized array. Arrange for the SAVE_EXPR on the inside
11216 of the MINUS_EXPR, which allows the -1 to get folded with the +1
11217 that happens when building TYPE_SIZE. */
11218 size = variable_size (size);
11219 stabilize_vla_size (size);
11220 }
11221
11222 /* Compute the index of the largest element in the array. It is
11223 one less than the number of elements in the array. We save
11224 and restore PROCESSING_TEMPLATE_DECL so that computations in
11225 cp_build_binary_op will be appropriately folded. */
11226 {
11227 processing_template_decl_sentinel s;
11228 itype = cp_build_binary_op (input_location,
11229 MINUS_EXPR,
11230 cp_convert (ssizetype, size, complain),
11231 cp_convert (ssizetype, integer_one_node,
11232 complain),
11233 complain);
11234 itype = maybe_constant_value (itype, NULL_TREE, true);
11235 }
11236
11237 if (!TREE_CONSTANT (itype))
11238 {
11239 if (sanitize_flags_p (SANITIZE_VLA)
11240 && current_function_decl != NULL_TREE)
11241 {
11242 /* We have to add 1 -- in the ubsan routine we generate
11243 LE_EXPR rather than LT_EXPR. */
11244 tree t = fold_build2 (PLUS_EXPR, TREE_TYPE (itype), itype,
11245 build_one_cst (TREE_TYPE (itype)));
11246 t = ubsan_instrument_vla (input_location, t);
11247 finish_expr_stmt (t);
11248 }
11249 }
11250 /* Make sure that there was no overflow when creating to a signed
11251 index type. (For example, on a 32-bit machine, an array with
11252 size 2^32 - 1 is too big.) */
11253 else if (TREE_CODE (itype) == INTEGER_CST
11254 && TREE_OVERFLOW (itype))
11255 {
11256 if (!(complain & tf_error))
11257 return error_mark_node;
11258 error ("overflow in array dimension");
11259 TREE_OVERFLOW (itype) = 0;
11260 }
11261 }
11262
11263 /* Create and return the appropriate index type. */
11264 itype = build_index_type (itype);
11265
11266 /* If the index type were dependent, we would have returned early, so
11267 remember that it isn't. */
11268 TYPE_DEPENDENT_P (itype) = 0;
11269 TYPE_DEPENDENT_P_VALID (itype) = 1;
11270 return itype;
11271 }
11272
11273 tree
compute_array_index_type(tree name,tree size,tsubst_flags_t complain)11274 compute_array_index_type (tree name, tree size, tsubst_flags_t complain)
11275 {
11276 return compute_array_index_type_loc (input_location, name, size, complain);
11277 }
11278
11279 /* Returns the scope (if any) in which the entity declared by
11280 DECLARATOR will be located. If the entity was declared with an
11281 unqualified name, NULL_TREE is returned. */
11282
11283 tree
get_scope_of_declarator(const cp_declarator * declarator)11284 get_scope_of_declarator (const cp_declarator *declarator)
11285 {
11286 while (declarator && declarator->kind != cdk_id)
11287 declarator = declarator->declarator;
11288
11289 /* If the declarator-id is a SCOPE_REF, the scope in which the
11290 declaration occurs is the first operand. */
11291 if (declarator
11292 && declarator->u.id.qualifying_scope)
11293 return declarator->u.id.qualifying_scope;
11294
11295 /* Otherwise, the declarator is not a qualified name; the entity will
11296 be declared in the current scope. */
11297 return NULL_TREE;
11298 }
11299
11300 /* Returns an ARRAY_TYPE for an array with SIZE elements of the
11301 indicated TYPE. If non-NULL, NAME is the NAME of the declaration
11302 with this type. */
11303
11304 static tree
create_array_type_for_decl(tree name,tree type,tree size,location_t loc)11305 create_array_type_for_decl (tree name, tree type, tree size, location_t loc)
11306 {
11307 tree itype = NULL_TREE;
11308
11309 /* If things have already gone awry, bail now. */
11310 if (type == error_mark_node || size == error_mark_node)
11311 return error_mark_node;
11312
11313 /* [dcl.type.class.deduct] prohibits forming an array of placeholder
11314 for a deduced class type. */
11315 if (template_placeholder_p (type))
11316 {
11317 if (name)
11318 error_at (loc, "%qD declared as array of template placeholder "
11319 "type %qT", name, type);
11320 else
11321 error ("creating array of template placeholder type %qT", type);
11322 return error_mark_node;
11323 }
11324
11325 /* If there are some types which cannot be array elements,
11326 issue an error-message and return. */
11327 switch (TREE_CODE (type))
11328 {
11329 case VOID_TYPE:
11330 if (name)
11331 error_at (loc, "declaration of %qD as array of void", name);
11332 else
11333 error ("creating array of void");
11334 return error_mark_node;
11335
11336 case FUNCTION_TYPE:
11337 if (name)
11338 error_at (loc, "declaration of %qD as array of functions", name);
11339 else
11340 error ("creating array of functions");
11341 return error_mark_node;
11342
11343 case REFERENCE_TYPE:
11344 if (name)
11345 error_at (loc, "declaration of %qD as array of references", name);
11346 else
11347 error ("creating array of references");
11348 return error_mark_node;
11349
11350 case METHOD_TYPE:
11351 if (name)
11352 error_at (loc, "declaration of %qD as array of function members",
11353 name);
11354 else
11355 error ("creating array of function members");
11356 return error_mark_node;
11357
11358 default:
11359 break;
11360 }
11361
11362 if (!verify_type_context (name ? loc : input_location,
11363 TCTX_ARRAY_ELEMENT, type))
11364 return error_mark_node;
11365
11366 /* [dcl.array]
11367
11368 The constant expressions that specify the bounds of the arrays
11369 can be omitted only for the first member of the sequence. */
11370 if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type))
11371 {
11372 if (name)
11373 error_at (loc, "declaration of %qD as multidimensional array must "
11374 "have bounds for all dimensions except the first",
11375 name);
11376 else
11377 error ("multidimensional array must have bounds for all "
11378 "dimensions except the first");
11379
11380 return error_mark_node;
11381 }
11382
11383 /* Figure out the index type for the array. */
11384 if (size)
11385 {
11386 itype = compute_array_index_type_loc (loc, name, size,
11387 tf_warning_or_error);
11388 if (type_uses_auto (type)
11389 && variably_modified_type_p (itype, /*fn=*/NULL_TREE))
11390 {
11391 sorry_at (loc, "variable-length array of %<auto%>");
11392 return error_mark_node;
11393 }
11394 }
11395
11396 return build_cplus_array_type (type, itype);
11397 }
11398
11399 /* Returns the smallest location that is not UNKNOWN_LOCATION. */
11400
11401 static location_t
min_location(location_t loca,location_t locb)11402 min_location (location_t loca, location_t locb)
11403 {
11404 if (loca == UNKNOWN_LOCATION
11405 || (locb != UNKNOWN_LOCATION
11406 && linemap_location_before_p (line_table, locb, loca)))
11407 return locb;
11408 return loca;
11409 }
11410
11411 /* Returns the smallest location != UNKNOWN_LOCATION among the
11412 three stored in LOCATIONS[ds_const], LOCATIONS[ds_volatile],
11413 and LOCATIONS[ds_restrict]. */
11414
11415 static location_t
smallest_type_quals_location(int type_quals,const location_t * locations)11416 smallest_type_quals_location (int type_quals, const location_t* locations)
11417 {
11418 location_t loc = UNKNOWN_LOCATION;
11419
11420 if (type_quals & TYPE_QUAL_CONST)
11421 loc = locations[ds_const];
11422
11423 if (type_quals & TYPE_QUAL_VOLATILE)
11424 loc = min_location (loc, locations[ds_volatile]);
11425
11426 if (type_quals & TYPE_QUAL_RESTRICT)
11427 loc = min_location (loc, locations[ds_restrict]);
11428
11429 return loc;
11430 }
11431
11432 /* Returns the smallest among the latter and locations[ds_type_spec]. */
11433
11434 static location_t
smallest_type_location(int type_quals,const location_t * locations)11435 smallest_type_location (int type_quals, const location_t* locations)
11436 {
11437 location_t loc = smallest_type_quals_location (type_quals, locations);
11438 return min_location (loc, locations[ds_type_spec]);
11439 }
11440
11441 static location_t
smallest_type_location(const cp_decl_specifier_seq * declspecs)11442 smallest_type_location (const cp_decl_specifier_seq *declspecs)
11443 {
11444 int type_quals = get_type_quals (declspecs);
11445 return smallest_type_location (type_quals, declspecs->locations);
11446 }
11447
11448 /* Check that it's OK to declare a function with the indicated TYPE
11449 and TYPE_QUALS. SFK indicates the kind of special function (if any)
11450 that this function is. OPTYPE is the type given in a conversion
11451 operator declaration, or the class type for a constructor/destructor.
11452 Returns the actual return type of the function; that may be different
11453 than TYPE if an error occurs, or for certain special functions. */
11454
11455 static tree
check_special_function_return_type(special_function_kind sfk,tree type,tree optype,int type_quals,const location_t * locations)11456 check_special_function_return_type (special_function_kind sfk,
11457 tree type,
11458 tree optype,
11459 int type_quals,
11460 const location_t* locations)
11461 {
11462 switch (sfk)
11463 {
11464 case sfk_constructor:
11465 if (type)
11466 error_at (smallest_type_location (type_quals, locations),
11467 "return type specification for constructor invalid");
11468 else if (type_quals != TYPE_UNQUALIFIED)
11469 error_at (smallest_type_quals_location (type_quals, locations),
11470 "qualifiers are not allowed on constructor declaration");
11471
11472 if (targetm.cxx.cdtor_returns_this ())
11473 type = build_pointer_type (optype);
11474 else
11475 type = void_type_node;
11476 break;
11477
11478 case sfk_destructor:
11479 if (type)
11480 error_at (smallest_type_location (type_quals, locations),
11481 "return type specification for destructor invalid");
11482 else if (type_quals != TYPE_UNQUALIFIED)
11483 error_at (smallest_type_quals_location (type_quals, locations),
11484 "qualifiers are not allowed on destructor declaration");
11485
11486 /* We can't use the proper return type here because we run into
11487 problems with ambiguous bases and covariant returns. */
11488 if (targetm.cxx.cdtor_returns_this ())
11489 type = build_pointer_type (void_type_node);
11490 else
11491 type = void_type_node;
11492 break;
11493
11494 case sfk_conversion:
11495 if (type)
11496 error_at (smallest_type_location (type_quals, locations),
11497 "return type specified for %<operator %T%>", optype);
11498 else if (type_quals != TYPE_UNQUALIFIED)
11499 error_at (smallest_type_quals_location (type_quals, locations),
11500 "qualifiers are not allowed on declaration of "
11501 "%<operator %T%>", optype);
11502
11503 type = optype;
11504 break;
11505
11506 case sfk_deduction_guide:
11507 if (type)
11508 error_at (smallest_type_location (type_quals, locations),
11509 "return type specified for deduction guide");
11510 else if (type_quals != TYPE_UNQUALIFIED)
11511 error_at (smallest_type_quals_location (type_quals, locations),
11512 "qualifiers are not allowed on declaration of "
11513 "deduction guide");
11514 if (TREE_CODE (optype) == TEMPLATE_TEMPLATE_PARM)
11515 {
11516 error ("template template parameter %qT in declaration of "
11517 "deduction guide", optype);
11518 type = error_mark_node;
11519 }
11520 else
11521 type = make_template_placeholder (CLASSTYPE_TI_TEMPLATE (optype));
11522 for (int i = 0; i < ds_last; ++i)
11523 if (i != ds_explicit && locations[i])
11524 error_at (locations[i],
11525 "%<decl-specifier%> in declaration of deduction guide");
11526 break;
11527
11528 default:
11529 gcc_unreachable ();
11530 }
11531
11532 return type;
11533 }
11534
11535 /* A variable or data member (whose unqualified name is IDENTIFIER)
11536 has been declared with the indicated TYPE. If the TYPE is not
11537 acceptable, issue an error message and return a type to use for
11538 error-recovery purposes. */
11539
11540 tree
check_var_type(tree identifier,tree type,location_t loc)11541 check_var_type (tree identifier, tree type, location_t loc)
11542 {
11543 if (VOID_TYPE_P (type))
11544 {
11545 if (!identifier)
11546 error_at (loc, "unnamed variable or field declared void");
11547 else if (identifier_p (identifier))
11548 {
11549 gcc_assert (!IDENTIFIER_ANY_OP_P (identifier));
11550 error_at (loc, "variable or field %qE declared void",
11551 identifier);
11552 }
11553 else
11554 error_at (loc, "variable or field declared void");
11555 type = error_mark_node;
11556 }
11557
11558 return type;
11559 }
11560
11561 /* Handle declaring DECL as an inline variable. */
11562
11563 static void
mark_inline_variable(tree decl,location_t loc)11564 mark_inline_variable (tree decl, location_t loc)
11565 {
11566 bool inlinep = true;
11567 if (! toplevel_bindings_p ())
11568 {
11569 error_at (loc, "%<inline%> specifier invalid for variable "
11570 "%qD declared at block scope", decl);
11571 inlinep = false;
11572 }
11573 else if (cxx_dialect < cxx17)
11574 pedwarn (loc, OPT_Wc__17_extensions, "inline variables are only available "
11575 "with %<-std=c++17%> or %<-std=gnu++17%>");
11576 if (inlinep)
11577 {
11578 retrofit_lang_decl (decl);
11579 SET_DECL_VAR_DECLARED_INLINE_P (decl);
11580 }
11581 }
11582
11583
11584 /* Assign a typedef-given name to a class or enumeration type declared
11585 as anonymous at first. This was split out of grokdeclarator
11586 because it is also used in libcc1. */
11587
11588 void
name_unnamed_type(tree type,tree decl)11589 name_unnamed_type (tree type, tree decl)
11590 {
11591 gcc_assert (TYPE_UNNAMED_P (type));
11592
11593 /* Replace the anonymous decl with the real decl. Be careful not to
11594 rename other typedefs (such as the self-reference) of type. */
11595 tree orig = TYPE_NAME (type);
11596 for (tree t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
11597 if (TYPE_NAME (t) == orig)
11598 TYPE_NAME (t) = decl;
11599
11600 /* If this is a typedef within a template class, the nested
11601 type is a (non-primary) template. The name for the
11602 template needs updating as well. */
11603 if (TYPE_LANG_SPECIFIC (type) && CLASSTYPE_TEMPLATE_INFO (type))
11604 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)) = DECL_NAME (decl);
11605
11606 /* Adjust linkage now that we aren't unnamed anymore. */
11607 reset_type_linkage (type);
11608
11609 /* FIXME remangle member functions; member functions of a
11610 type with external linkage have external linkage. */
11611
11612 /* Check that our job is done, and that it would fail if we
11613 attempted to do it again. */
11614 gcc_assert (!TYPE_UNNAMED_P (type));
11615 }
11616
11617 /* Check that decltype(auto) was well-formed: only plain decltype(auto)
11618 is allowed. TYPE might contain a decltype(auto). Returns true if
11619 there was a problem, false otherwise. */
11620
11621 static bool
check_decltype_auto(location_t loc,tree type)11622 check_decltype_auto (location_t loc, tree type)
11623 {
11624 if (tree a = type_uses_auto (type))
11625 {
11626 if (AUTO_IS_DECLTYPE (a))
11627 {
11628 if (a != type)
11629 {
11630 error_at (loc, "%qT as type rather than plain "
11631 "%<decltype(auto)%>", type);
11632 return true;
11633 }
11634 else if (TYPE_QUALS (type) != TYPE_UNQUALIFIED)
11635 {
11636 error_at (loc, "%<decltype(auto)%> cannot be cv-qualified");
11637 return true;
11638 }
11639 }
11640 }
11641 return false;
11642 }
11643
11644 /* Given declspecs and a declarator (abstract or otherwise), determine
11645 the name and type of the object declared and construct a DECL node
11646 for it.
11647
11648 DECLSPECS points to the representation of declaration-specifier
11649 sequence that precedes declarator.
11650
11651 DECL_CONTEXT says which syntactic context this declaration is in:
11652 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
11653 FUNCDEF for a function definition. Like NORMAL but a few different
11654 error messages in each case. Return value may be zero meaning
11655 this definition is too screwy to try to parse.
11656 MEMFUNCDEF for a function definition. Like FUNCDEF but prepares to
11657 handle member functions (which have FIELD context).
11658 Return value may be zero meaning this definition is too screwy to
11659 try to parse.
11660 PARM for a parameter declaration (either within a function prototype
11661 or before a function body). Make a PARM_DECL, or return void_type_node.
11662 TPARM for a template parameter declaration.
11663 CATCHPARM for a parameter declaration before a catch clause.
11664 TYPENAME if for a typename (in a cast or sizeof).
11665 Don't make a DECL node; just return the ..._TYPE node.
11666 FIELD for a struct or union field; make a FIELD_DECL.
11667 BITFIELD for a field with specified width.
11668
11669 INITIALIZED is as for start_decl.
11670
11671 ATTRLIST is a pointer to the list of attributes, which may be NULL
11672 if there are none; *ATTRLIST may be modified if attributes from inside
11673 the declarator should be applied to the declaration.
11674
11675 When this function is called, scoping variables (such as
11676 CURRENT_CLASS_TYPE) should reflect the scope in which the
11677 declaration occurs, not the scope in which the new declaration will
11678 be placed. For example, on:
11679
11680 void S::f() { ... }
11681
11682 when grokdeclarator is called for `S::f', the CURRENT_CLASS_TYPE
11683 should not be `S'.
11684
11685 Returns a DECL (if a declarator is present), a TYPE (if there is no
11686 declarator, in cases like "struct S;"), or the ERROR_MARK_NODE if an
11687 error occurs. */
11688
11689 tree
grokdeclarator(const cp_declarator * declarator,cp_decl_specifier_seq * declspecs,enum decl_context decl_context,int initialized,tree * attrlist)11690 grokdeclarator (const cp_declarator *declarator,
11691 cp_decl_specifier_seq *declspecs,
11692 enum decl_context decl_context,
11693 int initialized,
11694 tree* attrlist)
11695 {
11696 tree type = NULL_TREE;
11697 int longlong = 0;
11698 int explicit_intN = 0;
11699 int int_n_alt = 0;
11700 int virtualp, explicitp, friendp, inlinep, staticp;
11701 int explicit_int = 0;
11702 int explicit_char = 0;
11703 int defaulted_int = 0;
11704
11705 tree typedef_decl = NULL_TREE;
11706 const char *name = NULL;
11707 tree typedef_type = NULL_TREE;
11708 /* True if this declarator is a function definition. */
11709 bool funcdef_flag = false;
11710 cp_declarator_kind innermost_code = cdk_error;
11711 int bitfield = 0;
11712 #if 0
11713 /* See the code below that used this. */
11714 tree decl_attr = NULL_TREE;
11715 #endif
11716
11717 /* Keep track of what sort of function is being processed
11718 so that we can warn about default return values, or explicit
11719 return values which do not match prescribed defaults. */
11720 special_function_kind sfk = sfk_none;
11721
11722 tree dname = NULL_TREE;
11723 tree ctor_return_type = NULL_TREE;
11724 enum overload_flags flags = NO_SPECIAL;
11725 /* cv-qualifiers that apply to the declarator, for a declaration of
11726 a member function. */
11727 cp_cv_quals memfn_quals = TYPE_UNQUALIFIED;
11728 /* virt-specifiers that apply to the declarator, for a declaration of
11729 a member function. */
11730 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
11731 /* ref-qualifier that applies to the declarator, for a declaration of
11732 a member function. */
11733 cp_ref_qualifier rqual = REF_QUAL_NONE;
11734 /* cv-qualifiers that apply to the type specified by the DECLSPECS. */
11735 int type_quals = get_type_quals (declspecs);
11736 tree raises = NULL_TREE;
11737 int template_count = 0;
11738 tree returned_attrs = NULL_TREE;
11739 tree parms = NULL_TREE;
11740 const cp_declarator *id_declarator;
11741 /* The unqualified name of the declarator; either an
11742 IDENTIFIER_NODE, BIT_NOT_EXPR, or TEMPLATE_ID_EXPR. */
11743 tree unqualified_id;
11744 /* The class type, if any, in which this entity is located,
11745 or NULL_TREE if none. Note that this value may be different from
11746 the current class type; for example if an attempt is made to declare
11747 "A::f" inside "B", this value will be "A". */
11748 tree ctype = current_class_type;
11749 /* The NAMESPACE_DECL for the namespace in which this entity is
11750 located. If an unqualified name is used to declare the entity,
11751 this value will be NULL_TREE, even if the entity is located at
11752 namespace scope. */
11753 tree in_namespace = NULL_TREE;
11754 cp_storage_class storage_class;
11755 bool unsigned_p, signed_p, short_p, long_p, thread_p;
11756 bool type_was_error_mark_node = false;
11757 bool parameter_pack_p = declarator ? declarator->parameter_pack_p : false;
11758 bool template_type_arg = false;
11759 bool template_parm_flag = false;
11760 bool typedef_p = decl_spec_seq_has_spec_p (declspecs, ds_typedef);
11761 bool constexpr_p = decl_spec_seq_has_spec_p (declspecs, ds_constexpr);
11762 bool constinit_p = decl_spec_seq_has_spec_p (declspecs, ds_constinit);
11763 bool consteval_p = decl_spec_seq_has_spec_p (declspecs, ds_consteval);
11764 bool late_return_type_p = false;
11765 bool array_parameter_p = false;
11766 tree reqs = NULL_TREE;
11767
11768 signed_p = decl_spec_seq_has_spec_p (declspecs, ds_signed);
11769 unsigned_p = decl_spec_seq_has_spec_p (declspecs, ds_unsigned);
11770 short_p = decl_spec_seq_has_spec_p (declspecs, ds_short);
11771 long_p = decl_spec_seq_has_spec_p (declspecs, ds_long);
11772 longlong = decl_spec_seq_has_spec_p (declspecs, ds_long_long);
11773 explicit_intN = declspecs->explicit_intN_p;
11774 int_n_alt = declspecs->int_n_alt;
11775 thread_p = decl_spec_seq_has_spec_p (declspecs, ds_thread);
11776
11777 // Was concept_p specified? Note that ds_concept
11778 // implies ds_constexpr!
11779 bool concept_p = decl_spec_seq_has_spec_p (declspecs, ds_concept);
11780 if (concept_p)
11781 constexpr_p = true;
11782
11783 if (decl_context == FUNCDEF)
11784 funcdef_flag = true, decl_context = NORMAL;
11785 else if (decl_context == MEMFUNCDEF)
11786 funcdef_flag = true, decl_context = FIELD;
11787 else if (decl_context == BITFIELD)
11788 bitfield = 1, decl_context = FIELD;
11789 else if (decl_context == TEMPLATE_TYPE_ARG)
11790 template_type_arg = true, decl_context = TYPENAME;
11791 else if (decl_context == TPARM)
11792 template_parm_flag = true, decl_context = PARM;
11793
11794 if (initialized == SD_DEFAULTED || initialized == SD_DELETED)
11795 funcdef_flag = true;
11796
11797 location_t typespec_loc = loc_or_input_loc (smallest_type_location
11798 (type_quals,
11799 declspecs->locations));
11800 location_t id_loc;
11801 location_t init_loc;
11802 if (declarator)
11803 {
11804 id_loc = loc_or_input_loc (declarator->id_loc);
11805 init_loc = loc_or_input_loc (declarator->init_loc);
11806 }
11807 else
11808 init_loc = id_loc = input_location;
11809
11810 /* Look inside a declarator for the name being declared
11811 and get it as a string, for an error message. */
11812 for (id_declarator = declarator;
11813 id_declarator;
11814 id_declarator = id_declarator->declarator)
11815 {
11816 if (id_declarator->kind != cdk_id)
11817 innermost_code = id_declarator->kind;
11818
11819 switch (id_declarator->kind)
11820 {
11821 case cdk_function:
11822 if (id_declarator->declarator
11823 && id_declarator->declarator->kind == cdk_id)
11824 {
11825 sfk = id_declarator->declarator->u.id.sfk;
11826 if (sfk == sfk_destructor)
11827 flags = DTOR_FLAG;
11828 }
11829 break;
11830
11831 case cdk_id:
11832 {
11833 tree qualifying_scope = id_declarator->u.id.qualifying_scope;
11834 tree decl = id_declarator->u.id.unqualified_name;
11835 if (!decl)
11836 break;
11837 if (qualifying_scope)
11838 {
11839 if (check_for_bare_parameter_packs (qualifying_scope,
11840 id_declarator->id_loc))
11841 return error_mark_node;
11842 if (at_function_scope_p ())
11843 {
11844 /* [dcl.meaning]
11845
11846 A declarator-id shall not be qualified except
11847 for ...
11848
11849 None of the cases are permitted in block
11850 scope. */
11851 if (qualifying_scope == global_namespace)
11852 error ("invalid use of qualified-name %<::%D%>",
11853 decl);
11854 else if (TYPE_P (qualifying_scope))
11855 error ("invalid use of qualified-name %<%T::%D%>",
11856 qualifying_scope, decl);
11857 else
11858 error ("invalid use of qualified-name %<%D::%D%>",
11859 qualifying_scope, decl);
11860 return error_mark_node;
11861 }
11862 else if (TYPE_P (qualifying_scope))
11863 {
11864 ctype = qualifying_scope;
11865 if (!MAYBE_CLASS_TYPE_P (ctype))
11866 {
11867 error_at (id_declarator->id_loc,
11868 "%q#T is not a class or namespace", ctype);
11869 ctype = NULL_TREE;
11870 }
11871 else if (innermost_code != cdk_function
11872 && current_class_type
11873 && !uniquely_derived_from_p (ctype,
11874 current_class_type))
11875 {
11876 error_at (id_declarator->id_loc,
11877 "invalid use of qualified-name %<%T::%D%>",
11878 qualifying_scope, decl);
11879 return error_mark_node;
11880 }
11881 }
11882 else if (TREE_CODE (qualifying_scope) == NAMESPACE_DECL)
11883 in_namespace = qualifying_scope;
11884 }
11885 switch (TREE_CODE (decl))
11886 {
11887 case BIT_NOT_EXPR:
11888 {
11889 if (innermost_code != cdk_function)
11890 {
11891 error_at (EXPR_LOCATION (decl),
11892 "declaration of %qE as non-function", decl);
11893 return error_mark_node;
11894 }
11895 else if (!qualifying_scope
11896 && !(current_class_type && at_class_scope_p ()))
11897 {
11898 error_at (EXPR_LOCATION (decl),
11899 "declaration of %qE as non-member", decl);
11900 return error_mark_node;
11901 }
11902
11903 tree type = TREE_OPERAND (decl, 0);
11904 if (TYPE_P (type))
11905 type = constructor_name (type);
11906 name = identifier_to_locale (IDENTIFIER_POINTER (type));
11907 dname = decl;
11908 }
11909 break;
11910
11911 case TEMPLATE_ID_EXPR:
11912 {
11913 tree fns = TREE_OPERAND (decl, 0);
11914
11915 dname = fns;
11916 if (!identifier_p (dname))
11917 dname = OVL_NAME (dname);
11918 }
11919 /* Fall through. */
11920
11921 case IDENTIFIER_NODE:
11922 if (identifier_p (decl))
11923 dname = decl;
11924
11925 if (IDENTIFIER_KEYWORD_P (dname))
11926 {
11927 error ("declarator-id missing; using reserved word %qD",
11928 dname);
11929 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
11930 }
11931 else if (!IDENTIFIER_CONV_OP_P (dname))
11932 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
11933 else
11934 {
11935 gcc_assert (flags == NO_SPECIAL);
11936 flags = TYPENAME_FLAG;
11937 sfk = sfk_conversion;
11938 tree glob = get_global_binding (dname);
11939 if (glob && TREE_CODE (glob) == TYPE_DECL)
11940 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
11941 else
11942 name = "<invalid operator>";
11943 }
11944 break;
11945
11946 default:
11947 gcc_unreachable ();
11948 }
11949 break;
11950 }
11951
11952 case cdk_array:
11953 case cdk_pointer:
11954 case cdk_reference:
11955 case cdk_ptrmem:
11956 break;
11957
11958 case cdk_decomp:
11959 name = "structured binding";
11960 break;
11961
11962 case cdk_error:
11963 return error_mark_node;
11964
11965 default:
11966 gcc_unreachable ();
11967 }
11968 if (id_declarator->kind == cdk_id)
11969 break;
11970 }
11971
11972 /* [dcl.fct.edf]
11973
11974 The declarator in a function-definition shall have the form
11975 D1 ( parameter-declaration-clause) ... */
11976 if (funcdef_flag && innermost_code != cdk_function)
11977 {
11978 error_at (id_loc, "function definition does not declare parameters");
11979 return error_mark_node;
11980 }
11981
11982 if (flags == TYPENAME_FLAG
11983 && innermost_code != cdk_function
11984 && ! (ctype && !declspecs->any_specifiers_p))
11985 {
11986 error_at (id_loc, "declaration of %qD as non-function", dname);
11987 return error_mark_node;
11988 }
11989
11990 if (dname && identifier_p (dname))
11991 {
11992 if (UDLIT_OPER_P (dname)
11993 && innermost_code != cdk_function)
11994 {
11995 error_at (id_loc, "declaration of %qD as non-function", dname);
11996 return error_mark_node;
11997 }
11998
11999 if (IDENTIFIER_ANY_OP_P (dname))
12000 {
12001 if (typedef_p)
12002 {
12003 error_at (id_loc, "declaration of %qD as %<typedef%>", dname);
12004 return error_mark_node;
12005 }
12006 else if (decl_context == PARM || decl_context == CATCHPARM)
12007 {
12008 error_at (id_loc, "declaration of %qD as parameter", dname);
12009 return error_mark_node;
12010 }
12011 }
12012 }
12013
12014 /* Anything declared one level down from the top level
12015 must be one of the parameters of a function
12016 (because the body is at least two levels down). */
12017
12018 /* This heuristic cannot be applied to C++ nodes! Fixed, however,
12019 by not allowing C++ class definitions to specify their parameters
12020 with xdecls (must be spec.d in the parmlist).
12021
12022 Since we now wait to push a class scope until we are sure that
12023 we are in a legitimate method context, we must set oldcname
12024 explicitly (since current_class_name is not yet alive).
12025
12026 We also want to avoid calling this a PARM if it is in a namespace. */
12027
12028 if (decl_context == NORMAL && !toplevel_bindings_p ())
12029 {
12030 cp_binding_level *b = current_binding_level;
12031 current_binding_level = b->level_chain;
12032 if (current_binding_level != 0 && toplevel_bindings_p ())
12033 decl_context = PARM;
12034 current_binding_level = b;
12035 }
12036
12037 if (name == NULL)
12038 name = decl_context == PARM ? "parameter" : "type name";
12039
12040 if (consteval_p && constexpr_p)
12041 {
12042 error_at (declspecs->locations[ds_consteval],
12043 "both %qs and %qs specified", "constexpr", "consteval");
12044 return error_mark_node;
12045 }
12046
12047 if (concept_p && typedef_p)
12048 {
12049 error_at (declspecs->locations[ds_concept],
12050 "%qs cannot appear in a typedef declaration", "concept");
12051 return error_mark_node;
12052 }
12053
12054 if (constexpr_p && typedef_p)
12055 {
12056 error_at (declspecs->locations[ds_constexpr],
12057 "%qs cannot appear in a typedef declaration", "constexpr");
12058 return error_mark_node;
12059 }
12060
12061 if (consteval_p && typedef_p)
12062 {
12063 error_at (declspecs->locations[ds_consteval],
12064 "%qs cannot appear in a typedef declaration", "consteval");
12065 return error_mark_node;
12066 }
12067
12068 if (constinit_p && typedef_p)
12069 {
12070 error_at (declspecs->locations[ds_constinit],
12071 "%qs cannot appear in a typedef declaration", "constinit");
12072 return error_mark_node;
12073 }
12074
12075 /* [dcl.spec]/2 "At most one of the constexpr, consteval, and constinit
12076 keywords shall appear in a decl-specifier-seq." */
12077 if (constinit_p && constexpr_p)
12078 {
12079 gcc_rich_location richloc (declspecs->locations[ds_constinit]);
12080 richloc.add_range (declspecs->locations[ds_constexpr]);
12081 error_at (&richloc,
12082 "can use at most one of the %<constinit%> and %<constexpr%> "
12083 "specifiers");
12084 return error_mark_node;
12085 }
12086
12087 /* If there were multiple types specified in the decl-specifier-seq,
12088 issue an error message. */
12089 if (declspecs->multiple_types_p)
12090 {
12091 error_at (typespec_loc,
12092 "two or more data types in declaration of %qs", name);
12093 return error_mark_node;
12094 }
12095
12096 if (declspecs->conflicting_specifiers_p)
12097 {
12098 error_at (min_location (declspecs->locations[ds_typedef],
12099 declspecs->locations[ds_storage_class]),
12100 "conflicting specifiers in declaration of %qs", name);
12101 return error_mark_node;
12102 }
12103
12104 /* Extract the basic type from the decl-specifier-seq. */
12105 type = declspecs->type;
12106 if (type == error_mark_node)
12107 {
12108 type = NULL_TREE;
12109 type_was_error_mark_node = true;
12110 }
12111
12112 /* Ignore erroneous attributes. */
12113 if (attrlist && *attrlist == error_mark_node)
12114 *attrlist = NULL_TREE;
12115
12116 /* An object declared as __attribute__((unavailable)) suppresses
12117 any reports of being declared with unavailable or deprecated
12118 items. An object declared as __attribute__((deprecated))
12119 suppresses warnings of uses of other deprecated items. */
12120 auto ds = make_temp_override (deprecated_state);
12121 if (attrlist && lookup_attribute ("unavailable", *attrlist))
12122 deprecated_state = UNAVAILABLE_DEPRECATED_SUPPRESS;
12123 else if (attrlist && lookup_attribute ("deprecated", *attrlist))
12124 deprecated_state = DEPRECATED_SUPPRESS;
12125
12126 cp_handle_deprecated_or_unavailable (type);
12127 if (type && TREE_CODE (type) == TYPE_DECL)
12128 {
12129 cp_warn_deprecated_use_scopes (CP_DECL_CONTEXT (type));
12130 typedef_decl = type;
12131 type = TREE_TYPE (typedef_decl);
12132 if (DECL_ARTIFICIAL (typedef_decl))
12133 cp_handle_deprecated_or_unavailable (type);
12134 }
12135 /* No type at all: default to `int', and set DEFAULTED_INT
12136 because it was not a user-defined typedef. */
12137 if (type == NULL_TREE)
12138 {
12139 if (signed_p || unsigned_p || long_p || short_p)
12140 {
12141 /* These imply 'int'. */
12142 type = integer_type_node;
12143 defaulted_int = 1;
12144 }
12145 /* If we just have "complex", it is equivalent to "complex double". */
12146 else if (!longlong && !explicit_intN
12147 && decl_spec_seq_has_spec_p (declspecs, ds_complex))
12148 {
12149 type = double_type_node;
12150 pedwarn (declspecs->locations[ds_complex], OPT_Wpedantic,
12151 "ISO C++ does not support plain %<complex%> meaning "
12152 "%<double complex%>");
12153 }
12154 }
12155 /* Gather flags. */
12156 explicit_int = declspecs->explicit_int_p;
12157 explicit_char = declspecs->explicit_char_p;
12158
12159 #if 0
12160 /* See the code below that used this. */
12161 if (typedef_decl)
12162 decl_attr = DECL_ATTRIBUTES (typedef_decl);
12163 #endif
12164 typedef_type = type;
12165
12166 if (sfk == sfk_conversion || sfk == sfk_deduction_guide)
12167 ctor_return_type = TREE_TYPE (dname);
12168 else
12169 ctor_return_type = ctype;
12170
12171 if (sfk != sfk_none)
12172 {
12173 type = check_special_function_return_type (sfk, type,
12174 ctor_return_type,
12175 type_quals,
12176 declspecs->locations);
12177 type_quals = TYPE_UNQUALIFIED;
12178 }
12179 else if (type == NULL_TREE)
12180 {
12181 int is_main;
12182
12183 explicit_int = -1;
12184
12185 /* We handle `main' specially here, because 'main () { }' is so
12186 common. With no options, it is allowed. With -Wreturn-type,
12187 it is a warning. It is only an error with -pedantic-errors. */
12188 is_main = (funcdef_flag
12189 && dname && identifier_p (dname)
12190 && MAIN_NAME_P (dname)
12191 && ctype == NULL_TREE
12192 && in_namespace == NULL_TREE
12193 && current_namespace == global_namespace);
12194
12195 if (type_was_error_mark_node)
12196 /* We've already issued an error, don't complain more. */;
12197 else if (in_system_header_at (id_loc) || flag_ms_extensions)
12198 /* Allow it, sigh. */;
12199 else if (! is_main)
12200 permerror (id_loc, "ISO C++ forbids declaration of %qs with no type",
12201 name);
12202 else if (pedantic)
12203 pedwarn (id_loc, OPT_Wpedantic,
12204 "ISO C++ forbids declaration of %qs with no type", name);
12205 else
12206 warning_at (id_loc, OPT_Wreturn_type,
12207 "ISO C++ forbids declaration of %qs with no type", name);
12208
12209 if (type_was_error_mark_node && template_parm_flag)
12210 /* FIXME we should be able to propagate the error_mark_node as is
12211 for other contexts too. */
12212 type = error_mark_node;
12213 else
12214 type = integer_type_node;
12215 }
12216
12217 ctype = NULL_TREE;
12218
12219 if (explicit_intN)
12220 {
12221 if (! int_n_enabled_p[declspecs->int_n_idx])
12222 {
12223 error_at (declspecs->locations[ds_type_spec],
12224 "%<__int%d%> is not supported by this target",
12225 int_n_data[declspecs->int_n_idx].bitsize);
12226 explicit_intN = false;
12227 }
12228 /* Don't pedwarn if the alternate "__intN__" form has been used instead
12229 of "__intN". */
12230 else if (!int_n_alt && pedantic)
12231 pedwarn (declspecs->locations[ds_type_spec], OPT_Wpedantic,
12232 "ISO C++ does not support %<__int%d%> for %qs",
12233 int_n_data[declspecs->int_n_idx].bitsize, name);
12234 }
12235
12236 /* Now process the modifiers that were specified
12237 and check for invalid combinations. */
12238
12239 /* Long double is a special combination. */
12240 if (long_p && !longlong && TYPE_MAIN_VARIANT (type) == double_type_node)
12241 {
12242 long_p = false;
12243 type = cp_build_qualified_type (long_double_type_node,
12244 cp_type_quals (type));
12245 }
12246
12247 /* Check all other uses of type modifiers. */
12248
12249 if (unsigned_p || signed_p || long_p || short_p)
12250 {
12251 location_t loc;
12252 const char *key;
12253 if (unsigned_p)
12254 {
12255 key = "unsigned";
12256 loc = declspecs->locations[ds_unsigned];
12257 }
12258 else if (signed_p)
12259 {
12260 key = "signed";
12261 loc = declspecs->locations[ds_signed];
12262 }
12263 else if (longlong)
12264 {
12265 key = "long long";
12266 loc = declspecs->locations[ds_long_long];
12267 }
12268 else if (long_p)
12269 {
12270 key = "long";
12271 loc = declspecs->locations[ds_long];
12272 }
12273 else /* if (short_p) */
12274 {
12275 key = "short";
12276 loc = declspecs->locations[ds_short];
12277 }
12278
12279 int ok = 0;
12280
12281 if (signed_p && unsigned_p)
12282 {
12283 gcc_rich_location richloc (declspecs->locations[ds_signed]);
12284 richloc.add_range (declspecs->locations[ds_unsigned]);
12285 error_at (&richloc,
12286 "%<signed%> and %<unsigned%> specified together");
12287 }
12288 else if (long_p && short_p)
12289 {
12290 gcc_rich_location richloc (declspecs->locations[ds_long]);
12291 richloc.add_range (declspecs->locations[ds_short]);
12292 error_at (&richloc, "%<long%> and %<short%> specified together");
12293 }
12294 else if (TREE_CODE (type) != INTEGER_TYPE
12295 || type == char8_type_node
12296 || type == char16_type_node
12297 || type == char32_type_node
12298 || ((long_p || short_p)
12299 && (explicit_char || explicit_intN)))
12300 error_at (loc, "%qs specified with %qT", key, type);
12301 else if (!explicit_int && !defaulted_int
12302 && !explicit_char && !explicit_intN)
12303 {
12304 if (typedef_decl)
12305 {
12306 pedwarn (loc, OPT_Wpedantic,
12307 "%qs specified with typedef-name %qD",
12308 key, typedef_decl);
12309 ok = !flag_pedantic_errors;
12310 /* PR108099: __int128_t comes from c_common_nodes_and_builtins,
12311 and is not built as a typedef. */
12312 if (is_typedef_decl (typedef_decl))
12313 type = DECL_ORIGINAL_TYPE (typedef_decl);
12314 }
12315 else if (declspecs->decltype_p)
12316 error_at (loc, "%qs specified with %<decltype%>", key);
12317 else
12318 error_at (loc, "%qs specified with %<typeof%>", key);
12319 }
12320 else
12321 ok = 1;
12322
12323 /* Discard the type modifiers if they are invalid. */
12324 if (! ok)
12325 {
12326 unsigned_p = false;
12327 signed_p = false;
12328 long_p = false;
12329 short_p = false;
12330 longlong = 0;
12331 }
12332 }
12333
12334 /* Decide whether an integer type is signed or not.
12335 Optionally treat bitfields as signed by default. */
12336 if (unsigned_p
12337 /* [class.bit]
12338
12339 It is implementation-defined whether a plain (neither
12340 explicitly signed or unsigned) char, short, int, or long
12341 bit-field is signed or unsigned.
12342
12343 Naturally, we extend this to long long as well. Note that
12344 this does not include wchar_t. */
12345 || (bitfield && !flag_signed_bitfields
12346 && !signed_p
12347 /* A typedef for plain `int' without `signed' can be
12348 controlled just like plain `int', but a typedef for
12349 `signed int' cannot be so controlled. */
12350 && !(typedef_decl
12351 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl))
12352 && TREE_CODE (type) == INTEGER_TYPE
12353 && !same_type_p (TYPE_MAIN_VARIANT (type), wchar_type_node)))
12354 {
12355 if (explicit_intN)
12356 type = int_n_trees[declspecs->int_n_idx].unsigned_type;
12357 else if (longlong)
12358 type = long_long_unsigned_type_node;
12359 else if (long_p)
12360 type = long_unsigned_type_node;
12361 else if (short_p)
12362 type = short_unsigned_type_node;
12363 else if (type == char_type_node)
12364 type = unsigned_char_type_node;
12365 else if (typedef_decl)
12366 type = c_common_unsigned_type (type);
12367 else
12368 type = unsigned_type_node;
12369 }
12370 else if (signed_p && type == char_type_node)
12371 type = signed_char_type_node;
12372 else if (explicit_intN)
12373 type = int_n_trees[declspecs->int_n_idx].signed_type;
12374 else if (longlong)
12375 type = long_long_integer_type_node;
12376 else if (long_p)
12377 type = long_integer_type_node;
12378 else if (short_p)
12379 type = short_integer_type_node;
12380 else if (signed_p && typedef_decl)
12381 type = c_common_signed_type (type);
12382
12383 if (decl_spec_seq_has_spec_p (declspecs, ds_complex))
12384 {
12385 if (TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
12386 error_at (declspecs->locations[ds_complex],
12387 "complex invalid for %qs", name);
12388 /* If a modifier is specified, the resulting complex is the complex
12389 form of TYPE. E.g, "complex short" is "complex short int". */
12390 else if (type == integer_type_node)
12391 type = complex_integer_type_node;
12392 else if (type == float_type_node)
12393 type = complex_float_type_node;
12394 else if (type == double_type_node)
12395 type = complex_double_type_node;
12396 else if (type == long_double_type_node)
12397 type = complex_long_double_type_node;
12398 else
12399 type = build_complex_type (type);
12400 }
12401
12402 /* If we're using the injected-class-name to form a compound type or a
12403 declaration, replace it with the underlying class so we don't get
12404 redundant typedefs in the debug output. But if we are returning the
12405 type unchanged, leave it alone so that it's available to
12406 maybe_get_template_decl_from_type_decl. */
12407 if (CLASS_TYPE_P (type)
12408 && DECL_SELF_REFERENCE_P (TYPE_NAME (type))
12409 && type == TREE_TYPE (TYPE_NAME (type))
12410 && (declarator || type_quals))
12411 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
12412
12413 type_quals |= cp_type_quals (type);
12414 type = cp_build_qualified_type_real
12415 (type, type_quals, ((((typedef_decl && !DECL_ARTIFICIAL (typedef_decl))
12416 || declspecs->decltype_p)
12417 ? tf_ignore_bad_quals : 0) | tf_warning_or_error));
12418 /* We might have ignored or rejected some of the qualifiers. */
12419 type_quals = cp_type_quals (type);
12420
12421 if (cxx_dialect >= cxx17 && type && is_auto (type)
12422 && innermost_code != cdk_function
12423 && id_declarator && declarator != id_declarator)
12424 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (type))
12425 {
12426 error_at (typespec_loc, "template placeholder type %qT must be followed "
12427 "by a simple declarator-id", type);
12428 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here", tmpl);
12429 type = error_mark_node;
12430 }
12431
12432 staticp = 0;
12433 inlinep = decl_spec_seq_has_spec_p (declspecs, ds_inline);
12434 virtualp = decl_spec_seq_has_spec_p (declspecs, ds_virtual);
12435 explicitp = decl_spec_seq_has_spec_p (declspecs, ds_explicit);
12436
12437 storage_class = declspecs->storage_class;
12438 if (storage_class == sc_static)
12439 staticp = 1 + (decl_context == FIELD);
12440 else if (decl_context == FIELD && sfk == sfk_deduction_guide)
12441 /* Treat class-scope deduction guides as static member functions
12442 so that they get a FUNCTION_TYPE instead of a METHOD_TYPE. */
12443 staticp = 2;
12444
12445 if (virtualp)
12446 {
12447 if (staticp == 2)
12448 {
12449 gcc_rich_location richloc (declspecs->locations[ds_virtual]);
12450 richloc.add_range (declspecs->locations[ds_storage_class]);
12451 error_at (&richloc, "member %qD cannot be declared both %<virtual%> "
12452 "and %<static%>", dname);
12453 storage_class = sc_none;
12454 staticp = 0;
12455 }
12456 if (constexpr_p && pedantic && cxx_dialect < cxx20)
12457 {
12458 gcc_rich_location richloc (declspecs->locations[ds_virtual]);
12459 richloc.add_range (declspecs->locations[ds_constexpr]);
12460 pedwarn (&richloc, OPT_Wc__20_extensions, "member %qD can be "
12461 "declared both %<virtual%> and %<constexpr%> only in "
12462 "%<-std=c++20%> or %<-std=gnu++20%>", dname);
12463 }
12464 }
12465 friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
12466
12467 /* Issue errors about use of storage classes for parameters. */
12468 if (decl_context == PARM)
12469 {
12470 if (typedef_p)
12471 {
12472 error_at (declspecs->locations[ds_typedef],
12473 "typedef declaration invalid in parameter declaration");
12474 return error_mark_node;
12475 }
12476 else if (template_parm_flag && storage_class != sc_none)
12477 {
12478 error_at (min_location (declspecs->locations[ds_thread],
12479 declspecs->locations[ds_storage_class]),
12480 "storage class specified for template parameter %qs",
12481 name);
12482 return error_mark_node;
12483 }
12484 else if (storage_class == sc_static
12485 || storage_class == sc_extern
12486 || thread_p)
12487 {
12488 error_at (min_location (declspecs->locations[ds_thread],
12489 declspecs->locations[ds_storage_class]),
12490 "storage class specified for parameter %qs", name);
12491 return error_mark_node;
12492 }
12493
12494 /* Function parameters cannot be concept. */
12495 if (concept_p)
12496 {
12497 error_at (declspecs->locations[ds_concept],
12498 "a parameter cannot be declared %qs", "concept");
12499 concept_p = 0;
12500 constexpr_p = 0;
12501 }
12502 /* Function parameters cannot be constexpr. If we saw one, moan
12503 and pretend it wasn't there. */
12504 else if (constexpr_p)
12505 {
12506 error_at (declspecs->locations[ds_constexpr],
12507 "a parameter cannot be declared %qs", "constexpr");
12508 constexpr_p = 0;
12509 }
12510 if (constinit_p)
12511 {
12512 error_at (declspecs->locations[ds_constinit],
12513 "a parameter cannot be declared %qs", "constinit");
12514 constinit_p = 0;
12515 }
12516 if (consteval_p)
12517 {
12518 error_at (declspecs->locations[ds_consteval],
12519 "a parameter cannot be declared %qs", "consteval");
12520 consteval_p = 0;
12521 }
12522 }
12523
12524 /* Give error if `virtual' is used outside of class declaration. */
12525 if (virtualp
12526 && (current_class_name == NULL_TREE || decl_context != FIELD))
12527 {
12528 error_at (declspecs->locations[ds_virtual],
12529 "%<virtual%> outside class declaration");
12530 virtualp = 0;
12531 }
12532
12533 if (innermost_code == cdk_decomp)
12534 {
12535 location_t loc = (declarator->kind == cdk_reference
12536 ? declarator->declarator->id_loc : declarator->id_loc);
12537 if (inlinep)
12538 error_at (declspecs->locations[ds_inline],
12539 "structured binding declaration cannot be %qs", "inline");
12540 if (typedef_p)
12541 error_at (declspecs->locations[ds_typedef],
12542 "structured binding declaration cannot be %qs", "typedef");
12543 if (constexpr_p && !concept_p)
12544 error_at (declspecs->locations[ds_constexpr], "structured "
12545 "binding declaration cannot be %qs", "constexpr");
12546 if (consteval_p)
12547 error_at (declspecs->locations[ds_consteval], "structured "
12548 "binding declaration cannot be %qs", "consteval");
12549 if (thread_p && cxx_dialect < cxx20)
12550 pedwarn (declspecs->locations[ds_thread], OPT_Wc__20_extensions,
12551 "structured binding declaration can be %qs only in "
12552 "%<-std=c++20%> or %<-std=gnu++20%>",
12553 declspecs->gnu_thread_keyword_p
12554 ? "__thread" : "thread_local");
12555 if (concept_p)
12556 error_at (declspecs->locations[ds_concept],
12557 "structured binding declaration cannot be %qs", "concept");
12558 /* [dcl.struct.bind] "A cv that includes volatile is deprecated." */
12559 if (type_quals & TYPE_QUAL_VOLATILE)
12560 warning_at (declspecs->locations[ds_volatile], OPT_Wvolatile,
12561 "%<volatile%>-qualified structured binding is deprecated");
12562 switch (storage_class)
12563 {
12564 case sc_none:
12565 break;
12566 case sc_register:
12567 error_at (loc, "structured binding declaration cannot be %qs",
12568 "register");
12569 break;
12570 case sc_static:
12571 if (cxx_dialect < cxx20)
12572 pedwarn (loc, OPT_Wc__20_extensions,
12573 "structured binding declaration can be %qs only in "
12574 "%<-std=c++20%> or %<-std=gnu++20%>", "static");
12575 break;
12576 case sc_extern:
12577 error_at (loc, "structured binding declaration cannot be %qs",
12578 "extern");
12579 break;
12580 case sc_mutable:
12581 error_at (loc, "structured binding declaration cannot be %qs",
12582 "mutable");
12583 break;
12584 case sc_auto:
12585 error_at (loc, "structured binding declaration cannot be "
12586 "C++98 %<auto%>");
12587 break;
12588 default:
12589 gcc_unreachable ();
12590 }
12591 if (TREE_CODE (type) != TEMPLATE_TYPE_PARM
12592 || TYPE_IDENTIFIER (type) != auto_identifier)
12593 {
12594 if (type != error_mark_node)
12595 {
12596 error_at (loc, "structured binding declaration cannot have "
12597 "type %qT", type);
12598 inform (loc,
12599 "type must be cv-qualified %<auto%> or reference to "
12600 "cv-qualified %<auto%>");
12601 }
12602 type = build_qualified_type (make_auto (), type_quals);
12603 declspecs->type = type;
12604 }
12605 inlinep = 0;
12606 typedef_p = 0;
12607 constexpr_p = 0;
12608 consteval_p = 0;
12609 concept_p = 0;
12610 if (storage_class != sc_static)
12611 {
12612 storage_class = sc_none;
12613 declspecs->storage_class = sc_none;
12614 }
12615 }
12616
12617 /* Static anonymous unions are dealt with here. */
12618 if (staticp && decl_context == TYPENAME
12619 && declspecs->type
12620 && ANON_AGGR_TYPE_P (declspecs->type))
12621 decl_context = FIELD;
12622
12623 /* Warn about storage classes that are invalid for certain
12624 kinds of declarations (parameters, typenames, etc.). */
12625 if (thread_p
12626 && ((storage_class
12627 && storage_class != sc_extern
12628 && storage_class != sc_static)
12629 || typedef_p))
12630 {
12631 location_t loc
12632 = min_location (declspecs->locations[ds_thread],
12633 declspecs->locations[ds_storage_class]);
12634 error_at (loc, "multiple storage classes in declaration of %qs", name);
12635 thread_p = false;
12636 }
12637 if (decl_context != NORMAL
12638 && ((storage_class != sc_none
12639 && storage_class != sc_mutable)
12640 || thread_p))
12641 {
12642 if ((decl_context == PARM || decl_context == CATCHPARM)
12643 && (storage_class == sc_register
12644 || storage_class == sc_auto))
12645 ;
12646 else if (typedef_p)
12647 ;
12648 else if (decl_context == FIELD
12649 /* C++ allows static class elements. */
12650 && storage_class == sc_static)
12651 /* C++ also allows inlines and signed and unsigned elements,
12652 but in those cases we don't come in here. */
12653 ;
12654 else
12655 {
12656 location_t loc
12657 = min_location (declspecs->locations[ds_thread],
12658 declspecs->locations[ds_storage_class]);
12659 if (decl_context == FIELD)
12660 error_at (loc, "storage class specified for %qs", name);
12661 else if (decl_context == PARM || decl_context == CATCHPARM)
12662 error_at (loc, "storage class specified for parameter %qs", name);
12663 else
12664 error_at (loc, "storage class specified for typename");
12665 if (storage_class == sc_register
12666 || storage_class == sc_auto
12667 || storage_class == sc_extern
12668 || thread_p)
12669 storage_class = sc_none;
12670 }
12671 }
12672 else if (storage_class == sc_extern && funcdef_flag
12673 && ! toplevel_bindings_p ())
12674 error ("nested function %qs declared %<extern%>", name);
12675 else if (toplevel_bindings_p ())
12676 {
12677 if (storage_class == sc_auto)
12678 error_at (declspecs->locations[ds_storage_class],
12679 "top-level declaration of %qs specifies %<auto%>", name);
12680 }
12681 else if (thread_p
12682 && storage_class != sc_extern
12683 && storage_class != sc_static)
12684 {
12685 if (declspecs->gnu_thread_keyword_p)
12686 pedwarn (declspecs->locations[ds_thread],
12687 0, "function-scope %qs implicitly auto and "
12688 "declared %<__thread%>", name);
12689
12690 /* When thread_local is applied to a variable of block scope the
12691 storage-class-specifier static is implied if it does not appear
12692 explicitly. */
12693 storage_class = declspecs->storage_class = sc_static;
12694 staticp = 1;
12695 }
12696
12697 if (storage_class && friendp)
12698 {
12699 error_at (min_location (declspecs->locations[ds_thread],
12700 declspecs->locations[ds_storage_class]),
12701 "storage class specifiers invalid in friend function "
12702 "declarations");
12703 storage_class = sc_none;
12704 staticp = 0;
12705 }
12706
12707 if (!id_declarator)
12708 unqualified_id = NULL_TREE;
12709 else
12710 {
12711 unqualified_id = id_declarator->u.id.unqualified_name;
12712 switch (TREE_CODE (unqualified_id))
12713 {
12714 case BIT_NOT_EXPR:
12715 unqualified_id = TREE_OPERAND (unqualified_id, 0);
12716 if (TYPE_P (unqualified_id))
12717 unqualified_id = constructor_name (unqualified_id);
12718 break;
12719
12720 case IDENTIFIER_NODE:
12721 case TEMPLATE_ID_EXPR:
12722 break;
12723
12724 default:
12725 gcc_unreachable ();
12726 }
12727 }
12728
12729 if (declspecs->std_attributes)
12730 {
12731 location_t attr_loc = declspecs->locations[ds_std_attribute];
12732 if (warning_at (attr_loc, OPT_Wattributes, "attribute ignored"))
12733 inform (attr_loc, "an attribute that appertains to a type-specifier "
12734 "is ignored");
12735 }
12736
12737 /* Determine the type of the entity declared by recurring on the
12738 declarator. */
12739 for (; declarator; declarator = declarator->declarator)
12740 {
12741 const cp_declarator *inner_declarator;
12742 tree attrs;
12743
12744 if (type == error_mark_node)
12745 return error_mark_node;
12746
12747 attrs = declarator->attributes;
12748 if (attrs)
12749 {
12750 int attr_flags;
12751
12752 attr_flags = 0;
12753 if (declarator->kind == cdk_id)
12754 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
12755 if (declarator->kind == cdk_function)
12756 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
12757 if (declarator->kind == cdk_array)
12758 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
12759 tree late_attrs = NULL_TREE;
12760 if (decl_context != PARM && decl_context != TYPENAME)
12761 /* Assume that any attributes that get applied late to
12762 templates will DTRT when applied to the declaration
12763 as a whole. */
12764 late_attrs = splice_template_attributes (&attrs, type);
12765 returned_attrs = decl_attributes (&type,
12766 chainon (returned_attrs, attrs),
12767 attr_flags);
12768 returned_attrs = chainon (late_attrs, returned_attrs);
12769 }
12770
12771 inner_declarator = declarator->declarator;
12772
12773 /* We don't want to warn in parameter context because we don't
12774 yet know if the parse will succeed, and this might turn out
12775 to be a constructor call. */
12776 if (decl_context != PARM
12777 && decl_context != TYPENAME
12778 && !typedef_p
12779 && declarator->parenthesized != UNKNOWN_LOCATION
12780 /* If the type is class-like and the inner name used a
12781 global namespace qualifier, we need the parens.
12782 Unfortunately all we can tell is whether a qualified name
12783 was used or not. */
12784 && !(inner_declarator
12785 && inner_declarator->kind == cdk_id
12786 && inner_declarator->u.id.qualifying_scope
12787 && (MAYBE_CLASS_TYPE_P (type)
12788 || TREE_CODE (type) == ENUMERAL_TYPE)))
12789 {
12790 if (warning_at (declarator->parenthesized, OPT_Wparentheses,
12791 "unnecessary parentheses in declaration of %qs",
12792 name))
12793 {
12794 gcc_rich_location iloc (declarator->parenthesized);
12795 iloc.add_fixit_remove (get_start (declarator->parenthesized));
12796 iloc.add_fixit_remove (get_finish (declarator->parenthesized));
12797 inform (&iloc, "remove parentheses");
12798 }
12799 }
12800 if (declarator->kind == cdk_id || declarator->kind == cdk_decomp)
12801 break;
12802
12803 switch (declarator->kind)
12804 {
12805 case cdk_array:
12806 type = create_array_type_for_decl (dname, type,
12807 declarator->u.array.bounds,
12808 declarator->id_loc);
12809 if (!valid_array_size_p (dname
12810 ? declarator->id_loc : input_location,
12811 type, dname))
12812 type = error_mark_node;
12813
12814 if (declarator->std_attributes)
12815 /* [dcl.array]/1:
12816
12817 The optional attribute-specifier-seq appertains to the
12818 array. */
12819 returned_attrs = chainon (returned_attrs,
12820 declarator->std_attributes);
12821 break;
12822
12823 case cdk_function:
12824 {
12825 tree arg_types;
12826 int funcdecl_p;
12827
12828 /* Declaring a function type. */
12829
12830 /* Pick up type qualifiers which should be applied to `this'. */
12831 memfn_quals = declarator->u.function.qualifiers;
12832 /* Pick up virt-specifiers. */
12833 virt_specifiers = declarator->u.function.virt_specifiers;
12834 /* And ref-qualifier, too */
12835 rqual = declarator->u.function.ref_qualifier;
12836 /* And tx-qualifier. */
12837 tree tx_qual = declarator->u.function.tx_qualifier;
12838 /* Pick up the exception specifications. */
12839 raises = declarator->u.function.exception_specification;
12840 /* If the exception-specification is ill-formed, let's pretend
12841 there wasn't one. */
12842 if (raises == error_mark_node)
12843 raises = NULL_TREE;
12844
12845 if (reqs)
12846 error_at (location_of (reqs), "requires-clause on return type");
12847 reqs = declarator->u.function.requires_clause;
12848
12849 /* Say it's a definition only for the CALL_EXPR
12850 closest to the identifier. */
12851 funcdecl_p = inner_declarator && inner_declarator->kind == cdk_id;
12852
12853 /* Handle a late-specified return type. */
12854 tree late_return_type = declarator->u.function.late_return_type;
12855 if (tree auto_node = type_uses_auto (type))
12856 {
12857 if (!late_return_type)
12858 {
12859 if (!funcdecl_p)
12860 /* auto (*fp)() = f; is OK. */;
12861 else if (current_class_type
12862 && LAMBDA_TYPE_P (current_class_type))
12863 /* OK for C++11 lambdas. */;
12864 else if (cxx_dialect < cxx14)
12865 {
12866 error_at (typespec_loc, "%qs function uses "
12867 "%<auto%> type specifier without "
12868 "trailing return type", name);
12869 inform (typespec_loc,
12870 "deduced return type only available "
12871 "with %<-std=c++14%> or %<-std=gnu++14%>");
12872 }
12873 else if (virtualp)
12874 {
12875 error_at (typespec_loc, "virtual function "
12876 "cannot have deduced return type");
12877 virtualp = false;
12878 }
12879 }
12880 else if (!is_auto (type) && sfk != sfk_conversion)
12881 {
12882 error_at (typespec_loc, "%qs function with trailing "
12883 "return type has %qT as its type rather "
12884 "than plain %<auto%>", name, type);
12885 return error_mark_node;
12886 }
12887 else if (is_auto (type) && AUTO_IS_DECLTYPE (type))
12888 {
12889 if (funcdecl_p)
12890 error_at (typespec_loc,
12891 "%qs function with trailing return type "
12892 "has %<decltype(auto)%> as its type "
12893 "rather than plain %<auto%>", name);
12894 else
12895 error_at (typespec_loc,
12896 "invalid use of %<decltype(auto)%>");
12897 return error_mark_node;
12898 }
12899 tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node);
12900 if (!tmpl)
12901 if (tree late_auto = type_uses_auto (late_return_type))
12902 tmpl = CLASS_PLACEHOLDER_TEMPLATE (late_auto);
12903 if (tmpl)
12904 {
12905 if (!funcdecl_p || !dguide_name_p (unqualified_id))
12906 {
12907 error_at (typespec_loc, "deduced class "
12908 "type %qD in function return type",
12909 DECL_NAME (tmpl));
12910 inform (DECL_SOURCE_LOCATION (tmpl),
12911 "%qD declared here", tmpl);
12912 return error_mark_node;
12913 }
12914 else if (!late_return_type)
12915 {
12916 error_at (declarator->id_loc, "deduction guide "
12917 "for %qT must have trailing return "
12918 "type", TREE_TYPE (tmpl));
12919 inform (DECL_SOURCE_LOCATION (tmpl),
12920 "%qD declared here", tmpl);
12921 return error_mark_node;
12922 }
12923 else if (CLASS_TYPE_P (late_return_type)
12924 && CLASSTYPE_TEMPLATE_INFO (late_return_type)
12925 && (CLASSTYPE_TI_TEMPLATE (late_return_type)
12926 == tmpl))
12927 /* OK */;
12928 else
12929 error ("trailing return type %qT of deduction guide "
12930 "is not a specialization of %qT",
12931 late_return_type, TREE_TYPE (tmpl));
12932 }
12933 }
12934 else if (late_return_type
12935 && sfk != sfk_conversion)
12936 {
12937 if (late_return_type == error_mark_node)
12938 return error_mark_node;
12939 if (cxx_dialect < cxx11)
12940 /* Not using maybe_warn_cpp0x because this should
12941 always be an error. */
12942 error_at (typespec_loc,
12943 "trailing return type only available "
12944 "with %<-std=c++11%> or %<-std=gnu++11%>");
12945 else
12946 error_at (typespec_loc, "%qs function with trailing "
12947 "return type not declared with %<auto%> "
12948 "type specifier", name);
12949 return error_mark_node;
12950 }
12951 if (late_return_type && sfk == sfk_conversion)
12952 {
12953 error ("a conversion function cannot have a trailing return type");
12954 return error_mark_node;
12955 }
12956 type = splice_late_return_type (type, late_return_type);
12957 if (type == error_mark_node)
12958 return error_mark_node;
12959
12960 if (late_return_type)
12961 {
12962 late_return_type_p = true;
12963 type_quals = cp_type_quals (type);
12964 }
12965
12966 if (type_quals != TYPE_UNQUALIFIED)
12967 {
12968 if (SCALAR_TYPE_P (type) || VOID_TYPE_P (type))
12969 warning_at (typespec_loc, OPT_Wignored_qualifiers, "type "
12970 "qualifiers ignored on function return type");
12971 /* [dcl.fct] "A volatile-qualified return type is
12972 deprecated." */
12973 if (type_quals & TYPE_QUAL_VOLATILE)
12974 warning_at (typespec_loc, OPT_Wvolatile,
12975 "%<volatile%>-qualified return type is "
12976 "deprecated");
12977
12978 /* We now know that the TYPE_QUALS don't apply to the
12979 decl, but to its return type. */
12980 type_quals = TYPE_UNQUALIFIED;
12981 }
12982
12983 /* Error about some types functions can't return. */
12984
12985 if (TREE_CODE (type) == FUNCTION_TYPE)
12986 {
12987 error_at (typespec_loc, "%qs declared as function returning "
12988 "a function", name);
12989 return error_mark_node;
12990 }
12991 if (TREE_CODE (type) == ARRAY_TYPE)
12992 {
12993 error_at (typespec_loc, "%qs declared as function returning "
12994 "an array", name);
12995 return error_mark_node;
12996 }
12997 if (constinit_p && funcdecl_p)
12998 {
12999 error_at (declspecs->locations[ds_constinit],
13000 "%<constinit%> on function return type is not "
13001 "allowed");
13002 return error_mark_node;
13003 }
13004
13005 if (check_decltype_auto (typespec_loc, type))
13006 return error_mark_node;
13007
13008 if (ctype == NULL_TREE
13009 && decl_context == FIELD
13010 && funcdecl_p
13011 && friendp == 0)
13012 ctype = current_class_type;
13013
13014 if (ctype && (sfk == sfk_constructor
13015 || sfk == sfk_destructor))
13016 {
13017 /* We are within a class's scope. If our declarator name
13018 is the same as the class name, and we are defining
13019 a function, then it is a constructor/destructor, and
13020 therefore returns a void type. */
13021
13022 /* ISO C++ 12.4/2. A destructor may not be declared
13023 const or volatile. A destructor may not be static.
13024 A destructor may not be declared with ref-qualifier.
13025
13026 ISO C++ 12.1. A constructor may not be declared
13027 const or volatile. A constructor may not be
13028 virtual. A constructor may not be static.
13029 A constructor may not be declared with ref-qualifier. */
13030 if (staticp == 2)
13031 error_at (declspecs->locations[ds_storage_class],
13032 (flags == DTOR_FLAG)
13033 ? G_("destructor cannot be static member "
13034 "function")
13035 : G_("constructor cannot be static member "
13036 "function"));
13037 if (memfn_quals)
13038 {
13039 error ((flags == DTOR_FLAG)
13040 ? G_("destructors may not be cv-qualified")
13041 : G_("constructors may not be cv-qualified"));
13042 memfn_quals = TYPE_UNQUALIFIED;
13043 }
13044
13045 if (rqual)
13046 {
13047 maybe_warn_cpp0x (CPP0X_REF_QUALIFIER);
13048 error ((flags == DTOR_FLAG)
13049 ? G_("destructors may not be ref-qualified")
13050 : G_("constructors may not be ref-qualified"));
13051 rqual = REF_QUAL_NONE;
13052 }
13053
13054 if (decl_context == FIELD
13055 && !member_function_or_else (ctype,
13056 current_class_type,
13057 flags))
13058 return error_mark_node;
13059
13060 if (flags != DTOR_FLAG)
13061 {
13062 /* It's a constructor. */
13063 if (explicitp == 1)
13064 explicitp = 2;
13065 if (virtualp)
13066 {
13067 permerror (declspecs->locations[ds_virtual],
13068 "constructors cannot be declared %<virtual%>");
13069 virtualp = 0;
13070 }
13071 if (decl_context == FIELD
13072 && sfk != sfk_constructor)
13073 return error_mark_node;
13074 }
13075 if (decl_context == FIELD)
13076 staticp = 0;
13077 }
13078 else if (friendp)
13079 {
13080 if (virtualp)
13081 {
13082 /* Cannot be both friend and virtual. */
13083 gcc_rich_location richloc (declspecs->locations[ds_virtual]);
13084 richloc.add_range (declspecs->locations[ds_friend]);
13085 error_at (&richloc, "virtual functions cannot be friends");
13086 friendp = 0;
13087 }
13088 if (decl_context == NORMAL)
13089 error_at (declarator->id_loc,
13090 "friend declaration not in class definition");
13091 if (current_function_decl && funcdef_flag)
13092 {
13093 error_at (declarator->id_loc,
13094 "cannot define friend function %qs in a local "
13095 "class definition", name);
13096 friendp = 0;
13097 }
13098 /* [class.friend]/6: A function can be defined in a friend
13099 declaration if the function name is unqualified. */
13100 if (funcdef_flag && in_namespace)
13101 {
13102 if (in_namespace == global_namespace)
13103 error_at (declarator->id_loc,
13104 "friend function definition %qs cannot have "
13105 "a name qualified with %<::%>", name);
13106 else
13107 error_at (declarator->id_loc,
13108 "friend function definition %qs cannot have "
13109 "a name qualified with %<%D::%>", name,
13110 in_namespace);
13111 }
13112 }
13113 else if (ctype && sfk == sfk_conversion)
13114 {
13115 if (explicitp == 1)
13116 {
13117 maybe_warn_cpp0x (CPP0X_EXPLICIT_CONVERSION);
13118 explicitp = 2;
13119 }
13120 }
13121 else if (sfk == sfk_deduction_guide)
13122 {
13123 if (explicitp == 1)
13124 explicitp = 2;
13125 }
13126
13127 tree pushed_scope = NULL_TREE;
13128 if (funcdecl_p
13129 && decl_context != FIELD
13130 && inner_declarator->u.id.qualifying_scope
13131 && CLASS_TYPE_P (inner_declarator->u.id.qualifying_scope))
13132 pushed_scope
13133 = push_scope (inner_declarator->u.id.qualifying_scope);
13134
13135 arg_types = grokparms (declarator->u.function.parameters, &parms);
13136
13137 if (pushed_scope)
13138 pop_scope (pushed_scope);
13139
13140 if (inner_declarator
13141 && inner_declarator->kind == cdk_id
13142 && inner_declarator->u.id.sfk == sfk_destructor
13143 && arg_types != void_list_node)
13144 {
13145 error_at (declarator->id_loc,
13146 "destructors may not have parameters");
13147 arg_types = void_list_node;
13148 parms = NULL_TREE;
13149 }
13150
13151 type = build_function_type (type, arg_types);
13152
13153 tree attrs = declarator->std_attributes;
13154 if (tx_qual)
13155 {
13156 tree att = build_tree_list (tx_qual, NULL_TREE);
13157 /* transaction_safe applies to the type, but
13158 transaction_safe_dynamic applies to the function. */
13159 if (is_attribute_p ("transaction_safe", tx_qual))
13160 attrs = chainon (attrs, att);
13161 else
13162 returned_attrs = chainon (returned_attrs, att);
13163 }
13164 if (attrs)
13165 /* [dcl.fct]/2:
13166
13167 The optional attribute-specifier-seq appertains to
13168 the function type. */
13169 cplus_decl_attributes (&type, attrs, 0);
13170
13171 if (raises)
13172 type = build_exception_variant (type, raises);
13173 }
13174 break;
13175
13176 case cdk_pointer:
13177 case cdk_reference:
13178 case cdk_ptrmem:
13179 /* Filter out pointers-to-references and references-to-references.
13180 We can get these if a TYPE_DECL is used. */
13181
13182 if (TYPE_REF_P (type))
13183 {
13184 if (declarator->kind != cdk_reference)
13185 {
13186 error ("cannot declare pointer to %q#T", type);
13187 type = TREE_TYPE (type);
13188 }
13189
13190 /* In C++0x, we allow reference to reference declarations
13191 that occur indirectly through typedefs [7.1.3/8 dcl.typedef]
13192 and template type arguments [14.3.1/4 temp.arg.type]. The
13193 check for direct reference to reference declarations, which
13194 are still forbidden, occurs below. Reasoning behind the change
13195 can be found in DR106, DR540, and the rvalue reference
13196 proposals. */
13197 else if (cxx_dialect == cxx98)
13198 {
13199 error ("cannot declare reference to %q#T", type);
13200 type = TREE_TYPE (type);
13201 }
13202 }
13203 else if (VOID_TYPE_P (type))
13204 {
13205 if (declarator->kind == cdk_reference)
13206 error ("cannot declare reference to %q#T", type);
13207 else if (declarator->kind == cdk_ptrmem)
13208 error ("cannot declare pointer to %q#T member", type);
13209 }
13210
13211 /* We now know that the TYPE_QUALS don't apply to the decl,
13212 but to the target of the pointer. */
13213 type_quals = TYPE_UNQUALIFIED;
13214
13215 /* This code used to handle METHOD_TYPE, but I don't think it's
13216 possible to get it here anymore. */
13217 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
13218 if (declarator->kind == cdk_ptrmem
13219 && TREE_CODE (type) == FUNCTION_TYPE)
13220 {
13221 memfn_quals |= type_memfn_quals (type);
13222 type = build_memfn_type (type,
13223 declarator->u.pointer.class_type,
13224 memfn_quals,
13225 rqual);
13226 if (type == error_mark_node)
13227 return error_mark_node;
13228
13229 rqual = REF_QUAL_NONE;
13230 memfn_quals = TYPE_UNQUALIFIED;
13231 }
13232
13233 if (TREE_CODE (type) == FUNCTION_TYPE
13234 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
13235 || type_memfn_rqual (type) != REF_QUAL_NONE))
13236 error (declarator->kind == cdk_reference
13237 ? G_("cannot declare reference to qualified function type %qT")
13238 : G_("cannot declare pointer to qualified function type %qT"),
13239 type);
13240
13241 /* When the pointed-to type involves components of variable size,
13242 care must be taken to ensure that the size evaluation code is
13243 emitted early enough to dominate all the possible later uses
13244 and late enough for the variables on which it depends to have
13245 been assigned.
13246
13247 This is expected to happen automatically when the pointed-to
13248 type has a name/declaration of it's own, but special attention
13249 is required if the type is anonymous.
13250
13251 We handle the NORMAL and FIELD contexts here by inserting a
13252 dummy statement that just evaluates the size at a safe point
13253 and ensures it is not deferred until e.g. within a deeper
13254 conditional context (c++/43555).
13255
13256 We expect nothing to be needed here for PARM or TYPENAME.
13257 Evaluating the size at this point for TYPENAME would
13258 actually be incorrect, as we might be in the middle of an
13259 expression with side effects on the pointed-to type size
13260 "arguments" prior to the pointer declaration point and the
13261 size evaluation could end up prior to the side effects. */
13262
13263 if (!TYPE_NAME (type)
13264 && (decl_context == NORMAL || decl_context == FIELD)
13265 && at_function_scope_p ()
13266 && variably_modified_type_p (type, NULL_TREE))
13267 {
13268 TYPE_NAME (type) = build_decl (UNKNOWN_LOCATION, TYPE_DECL,
13269 NULL_TREE, type);
13270 add_decl_expr (TYPE_NAME (type));
13271 }
13272
13273 if (declarator->kind == cdk_reference)
13274 {
13275 /* In C++0x, the type we are creating a reference to might be
13276 a typedef which is itself a reference type. In that case,
13277 we follow the reference collapsing rules in
13278 [7.1.3/8 dcl.typedef] to create the final reference type:
13279
13280 "If a typedef TD names a type that is a reference to a type
13281 T, an attempt to create the type 'lvalue reference to cv TD'
13282 creates the type 'lvalue reference to T,' while an attempt
13283 to create the type "rvalue reference to cv TD' creates the
13284 type TD."
13285 */
13286 if (VOID_TYPE_P (type))
13287 /* We already gave an error. */;
13288 else if (TYPE_REF_P (type))
13289 {
13290 if (declarator->u.reference.rvalue_ref)
13291 /* Leave type alone. */;
13292 else
13293 type = cp_build_reference_type (TREE_TYPE (type), false);
13294 }
13295 else
13296 type = cp_build_reference_type
13297 (type, declarator->u.reference.rvalue_ref);
13298
13299 /* In C++0x, we need this check for direct reference to
13300 reference declarations, which are forbidden by
13301 [8.3.2/5 dcl.ref]. Reference to reference declarations
13302 are only allowed indirectly through typedefs and template
13303 type arguments. Example:
13304
13305 void foo(int & &); // invalid ref-to-ref decl
13306
13307 typedef int & int_ref;
13308 void foo(int_ref &); // valid ref-to-ref decl
13309 */
13310 if (inner_declarator && inner_declarator->kind == cdk_reference)
13311 error ("cannot declare reference to %q#T, which is not "
13312 "a typedef or a template type argument", type);
13313 }
13314 else if (TREE_CODE (type) == METHOD_TYPE)
13315 type = build_ptrmemfunc_type (build_pointer_type (type));
13316 else if (declarator->kind == cdk_ptrmem)
13317 {
13318 gcc_assert (TREE_CODE (declarator->u.pointer.class_type)
13319 != NAMESPACE_DECL);
13320 if (declarator->u.pointer.class_type == error_mark_node)
13321 /* We will already have complained. */
13322 type = error_mark_node;
13323 else
13324 type = build_ptrmem_type (declarator->u.pointer.class_type,
13325 type);
13326 }
13327 else
13328 type = build_pointer_type (type);
13329
13330 /* Process a list of type modifier keywords (such as
13331 const or volatile) that were given inside the `*' or `&'. */
13332
13333 if (declarator->u.pointer.qualifiers)
13334 {
13335 type
13336 = cp_build_qualified_type (type,
13337 declarator->u.pointer.qualifiers);
13338 type_quals = cp_type_quals (type);
13339 }
13340
13341 /* Apply C++11 attributes to the pointer, and not to the
13342 type pointed to. This is unlike what is done for GNU
13343 attributes above. It is to comply with [dcl.ptr]/1:
13344
13345 [the optional attribute-specifier-seq (7.6.1) appertains
13346 to the pointer and not to the object pointed to]. */
13347 if (declarator->std_attributes)
13348 decl_attributes (&type, declarator->std_attributes,
13349 0);
13350
13351 ctype = NULL_TREE;
13352 break;
13353
13354 case cdk_error:
13355 break;
13356
13357 default:
13358 gcc_unreachable ();
13359 }
13360 }
13361
13362 id_loc = declarator ? declarator->id_loc : input_location;
13363
13364 if (innermost_code != cdk_function
13365 /* Don't check this if it can be the artifical decltype(auto)
13366 we created when building a constraint in a compound-requirement:
13367 that the type-constraint is plain is going to be checked in
13368 cp_parser_compound_requirement. */
13369 && decl_context != TYPENAME
13370 && check_decltype_auto (id_loc, type))
13371 return error_mark_node;
13372
13373 /* A `constexpr' specifier used in an object declaration declares
13374 the object as `const'. */
13375 if (constexpr_p && innermost_code != cdk_function)
13376 {
13377 /* DR1688 says that a `constexpr' specifier in combination with
13378 `volatile' is valid. */
13379
13380 if (!TYPE_REF_P (type))
13381 {
13382 type_quals |= TYPE_QUAL_CONST;
13383 type = cp_build_qualified_type (type, type_quals);
13384 }
13385 }
13386
13387 if (unqualified_id && TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR
13388 && !FUNC_OR_METHOD_TYPE_P (type)
13389 && !variable_template_p (TREE_OPERAND (unqualified_id, 0)))
13390 {
13391 error ("template-id %qD used as a declarator",
13392 unqualified_id);
13393 unqualified_id = dname;
13394 }
13395
13396 /* If TYPE is a FUNCTION_TYPE, but the function name was explicitly
13397 qualified with a class-name, turn it into a METHOD_TYPE, unless
13398 we know that the function is static. We take advantage of this
13399 opportunity to do other processing that pertains to entities
13400 explicitly declared to be class members. Note that if DECLARATOR
13401 is non-NULL, we know it is a cdk_id declarator; otherwise, we
13402 would not have exited the loop above. */
13403 if (declarator
13404 && declarator->kind == cdk_id
13405 && declarator->u.id.qualifying_scope
13406 && MAYBE_CLASS_TYPE_P (declarator->u.id.qualifying_scope))
13407 {
13408 ctype = declarator->u.id.qualifying_scope;
13409 ctype = TYPE_MAIN_VARIANT (ctype);
13410 template_count = num_template_headers_for_class (ctype);
13411
13412 if (ctype == current_class_type)
13413 {
13414 if (friendp)
13415 {
13416 permerror (declspecs->locations[ds_friend],
13417 "member functions are implicitly "
13418 "friends of their class");
13419 friendp = 0;
13420 }
13421 else
13422 permerror (id_loc, "extra qualification %<%T::%> on member %qs",
13423 ctype, name);
13424 }
13425 else if (/* If the qualifying type is already complete, then we
13426 can skip the following checks. */
13427 !COMPLETE_TYPE_P (ctype)
13428 && (/* If the function is being defined, then
13429 qualifying type must certainly be complete. */
13430 funcdef_flag
13431 /* A friend declaration of "T::f" is OK, even if
13432 "T" is a template parameter. But, if this
13433 function is not a friend, the qualifying type
13434 must be a class. */
13435 || (!friendp && !CLASS_TYPE_P (ctype))
13436 /* For a declaration, the type need not be
13437 complete, if either it is dependent (since there
13438 is no meaningful definition of complete in that
13439 case) or the qualifying class is currently being
13440 defined. */
13441 || !(dependent_type_p (ctype)
13442 || currently_open_class (ctype)))
13443 /* Check that the qualifying type is complete. */
13444 && !complete_type_or_else (ctype, NULL_TREE))
13445 return error_mark_node;
13446 else if (TREE_CODE (type) == FUNCTION_TYPE)
13447 {
13448 if (current_class_type
13449 && (!friendp || funcdef_flag || initialized))
13450 {
13451 error_at (id_loc, funcdef_flag || initialized
13452 ? G_("cannot define member function %<%T::%s%> "
13453 "within %qT")
13454 : G_("cannot declare member function %<%T::%s%> "
13455 "within %qT"),
13456 ctype, name, current_class_type);
13457 return error_mark_node;
13458 }
13459 }
13460 else if (typedef_p && current_class_type)
13461 {
13462 error_at (id_loc, "cannot declare member %<%T::%s%> within %qT",
13463 ctype, name, current_class_type);
13464 return error_mark_node;
13465 }
13466 }
13467
13468 if (ctype == NULL_TREE && decl_context == FIELD && friendp == 0)
13469 ctype = current_class_type;
13470
13471 /* Now TYPE has the actual type. */
13472
13473 if (returned_attrs)
13474 {
13475 if (attrlist)
13476 *attrlist = chainon (returned_attrs, *attrlist);
13477 else
13478 attrlist = &returned_attrs;
13479 }
13480
13481 if (declarator
13482 && declarator->kind == cdk_id
13483 && declarator->std_attributes
13484 && attrlist != NULL)
13485 {
13486 /* [dcl.meaning]/1: The optional attribute-specifier-seq following
13487 a declarator-id appertains to the entity that is declared. */
13488 if (declarator->std_attributes != error_mark_node)
13489 *attrlist = chainon (*attrlist, declarator->std_attributes);
13490 else
13491 /* We should have already diagnosed the issue (c++/78344). */
13492 gcc_assert (seen_error ());
13493 }
13494
13495 /* Handle parameter packs. */
13496 if (parameter_pack_p)
13497 {
13498 if (decl_context == PARM)
13499 /* Turn the type into a pack expansion.*/
13500 type = make_pack_expansion (type);
13501 else
13502 error ("non-parameter %qs cannot be a parameter pack", name);
13503 }
13504
13505 if ((decl_context == FIELD || decl_context == PARM)
13506 && !processing_template_decl
13507 && variably_modified_type_p (type, NULL_TREE))
13508 {
13509 if (decl_context == FIELD)
13510 error_at (id_loc,
13511 "data member may not have variably modified type %qT", type);
13512 else
13513 error_at (id_loc,
13514 "parameter may not have variably modified type %qT", type);
13515 type = error_mark_node;
13516 }
13517
13518 if (explicitp == 1 || (explicitp && friendp))
13519 {
13520 /* [dcl.fct.spec] (C++11) The explicit specifier shall be used only
13521 in the declaration of a constructor or conversion function within
13522 a class definition. */
13523 if (!current_class_type)
13524 error_at (declspecs->locations[ds_explicit],
13525 "%<explicit%> outside class declaration");
13526 else if (friendp)
13527 error_at (declspecs->locations[ds_explicit],
13528 "%<explicit%> in friend declaration");
13529 else
13530 error_at (declspecs->locations[ds_explicit],
13531 "only declarations of constructors and conversion operators "
13532 "can be %<explicit%>");
13533 explicitp = 0;
13534 }
13535
13536 if (storage_class == sc_mutable)
13537 {
13538 location_t sloc = declspecs->locations[ds_storage_class];
13539 if (decl_context != FIELD || friendp)
13540 {
13541 error_at (sloc, "non-member %qs cannot be declared %<mutable%>",
13542 name);
13543 storage_class = sc_none;
13544 }
13545 else if (decl_context == TYPENAME || typedef_p)
13546 {
13547 error_at (sloc,
13548 "non-object member %qs cannot be declared %<mutable%>",
13549 name);
13550 storage_class = sc_none;
13551 }
13552 else if (FUNC_OR_METHOD_TYPE_P (type))
13553 {
13554 error_at (sloc, "function %qs cannot be declared %<mutable%>",
13555 name);
13556 storage_class = sc_none;
13557 }
13558 else if (staticp)
13559 {
13560 error_at (sloc, "%<static%> %qs cannot be declared %<mutable%>",
13561 name);
13562 storage_class = sc_none;
13563 }
13564 else if (type_quals & TYPE_QUAL_CONST)
13565 {
13566 error_at (sloc, "%<const%> %qs cannot be declared %<mutable%>",
13567 name);
13568 storage_class = sc_none;
13569 }
13570 else if (TYPE_REF_P (type))
13571 {
13572 permerror (sloc, "reference %qs cannot be declared %<mutable%>",
13573 name);
13574 storage_class = sc_none;
13575 }
13576 }
13577
13578 /* If this is declaring a typedef name, return a TYPE_DECL. */
13579 if (typedef_p && decl_context != TYPENAME)
13580 {
13581 bool alias_p = decl_spec_seq_has_spec_p (declspecs, ds_alias);
13582 tree decl;
13583
13584 if (funcdef_flag)
13585 {
13586 if (decl_context == NORMAL)
13587 error_at (id_loc,
13588 "typedef may not be a function definition");
13589 else
13590 error_at (id_loc,
13591 "typedef may not be a member function definition");
13592 return error_mark_node;
13593 }
13594
13595 /* This declaration:
13596
13597 typedef void f(int) const;
13598
13599 declares a function type which is not a member of any
13600 particular class, but which is cv-qualified; for
13601 example "f S::*" declares a pointer to a const-qualified
13602 member function of S. We record the cv-qualification in the
13603 function type. */
13604 if ((rqual || memfn_quals) && TREE_CODE (type) == FUNCTION_TYPE)
13605 {
13606 type = apply_memfn_quals (type, memfn_quals, rqual);
13607
13608 /* We have now dealt with these qualifiers. */
13609 memfn_quals = TYPE_UNQUALIFIED;
13610 rqual = REF_QUAL_NONE;
13611 }
13612
13613 if (type_uses_auto (type))
13614 {
13615 if (alias_p)
13616 error_at (declspecs->locations[ds_type_spec],
13617 "%<auto%> not allowed in alias declaration");
13618 else
13619 error_at (declspecs->locations[ds_type_spec],
13620 "typedef declared %<auto%>");
13621 type = error_mark_node;
13622 }
13623
13624 if (reqs)
13625 error_at (location_of (reqs), "requires-clause on typedef");
13626
13627 if (id_declarator && declarator->u.id.qualifying_scope)
13628 {
13629 error_at (id_loc, "typedef name may not be a nested-name-specifier");
13630 type = error_mark_node;
13631 }
13632
13633 if (decl_context == FIELD)
13634 decl = build_lang_decl_loc (id_loc, TYPE_DECL, unqualified_id, type);
13635 else
13636 decl = build_decl (id_loc, TYPE_DECL, unqualified_id, type);
13637
13638 if (decl_context != FIELD)
13639 {
13640 if (!current_function_decl)
13641 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
13642 else if (DECL_MAYBE_IN_CHARGE_CDTOR_P (current_function_decl))
13643 /* The TYPE_DECL is "abstract" because there will be
13644 clones of this constructor/destructor, and there will
13645 be copies of this TYPE_DECL generated in those
13646 clones. The decloning optimization (for space) may
13647 revert this subsequently if it determines that
13648 the clones should share a common implementation. */
13649 DECL_ABSTRACT_P (decl) = true;
13650
13651 set_originating_module (decl);
13652 }
13653 else if (current_class_type
13654 && constructor_name_p (unqualified_id, current_class_type))
13655 permerror (id_loc, "ISO C++ forbids nested type %qD with same name "
13656 "as enclosing class",
13657 unqualified_id);
13658
13659 /* If the user declares "typedef struct {...} foo" then the
13660 struct will have an anonymous name. Fill that name in now.
13661 Nothing can refer to it, so nothing needs know about the name
13662 change. */
13663 if (type != error_mark_node
13664 && unqualified_id
13665 && TYPE_NAME (type)
13666 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
13667 && TYPE_UNNAMED_P (type)
13668 && declspecs->type_definition_p
13669 && attributes_naming_typedef_ok (*attrlist)
13670 && cp_type_quals (type) == TYPE_UNQUALIFIED)
13671 name_unnamed_type (type, decl);
13672
13673 if (signed_p
13674 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
13675 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
13676
13677 bad_specifiers (decl, BSP_TYPE, virtualp,
13678 memfn_quals != TYPE_UNQUALIFIED,
13679 inlinep, friendp, raises != NULL_TREE,
13680 declspecs->locations);
13681
13682 if (alias_p)
13683 /* Acknowledge that this was written:
13684 `using analias = atype;'. */
13685 TYPE_DECL_ALIAS_P (decl) = 1;
13686
13687 return decl;
13688 }
13689
13690 /* Detect the case of an array type of unspecified size
13691 which came, as such, direct from a typedef name.
13692 We must copy the type, so that the array's domain can be
13693 individually set by the object's initializer. */
13694
13695 if (type && typedef_type
13696 && TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
13697 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
13698 type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
13699
13700 /* Detect where we're using a typedef of function type to declare a
13701 function. PARMS will not be set, so we must create it now. */
13702
13703 if (type == typedef_type && TREE_CODE (type) == FUNCTION_TYPE)
13704 {
13705 tree decls = NULL_TREE;
13706 tree args;
13707
13708 for (args = TYPE_ARG_TYPES (type);
13709 args && args != void_list_node;
13710 args = TREE_CHAIN (args))
13711 {
13712 tree decl = cp_build_parm_decl (NULL_TREE, NULL_TREE,
13713 TREE_VALUE (args));
13714
13715 DECL_CHAIN (decl) = decls;
13716 decls = decl;
13717 }
13718
13719 parms = nreverse (decls);
13720
13721 if (decl_context != TYPENAME)
13722 {
13723 /* The qualifiers on the function type become the qualifiers on
13724 the non-static member function. */
13725 memfn_quals |= type_memfn_quals (type);
13726 rqual = type_memfn_rqual (type);
13727 type_quals = TYPE_UNQUALIFIED;
13728 raises = TYPE_RAISES_EXCEPTIONS (type);
13729 }
13730 }
13731
13732 /* If this is a type name (such as, in a cast or sizeof),
13733 compute the type and return it now. */
13734
13735 if (decl_context == TYPENAME)
13736 {
13737 /* Note that here we don't care about type_quals. */
13738
13739 /* Special case: "friend class foo" looks like a TYPENAME context. */
13740 if (friendp)
13741 {
13742 if (inlinep)
13743 {
13744 error ("%<inline%> specified for friend class declaration");
13745 inlinep = 0;
13746 }
13747
13748 if (!current_aggr)
13749 {
13750 /* Don't allow friend declaration without a class-key. */
13751 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
13752 permerror (input_location, "template parameters cannot be friends");
13753 else if (TREE_CODE (type) == TYPENAME_TYPE)
13754 permerror (input_location, "friend declaration requires class-key, "
13755 "i.e. %<friend class %T::%D%>",
13756 TYPE_CONTEXT (type), TYPENAME_TYPE_FULLNAME (type));
13757 else
13758 permerror (input_location, "friend declaration requires class-key, "
13759 "i.e. %<friend %#T%>",
13760 type);
13761 }
13762
13763 /* Only try to do this stuff if we didn't already give up. */
13764 if (type != integer_type_node)
13765 {
13766 /* A friendly class? */
13767 if (current_class_type)
13768 make_friend_class (current_class_type, TYPE_MAIN_VARIANT (type),
13769 /*complain=*/true);
13770 else
13771 error ("trying to make class %qT a friend of global scope",
13772 type);
13773
13774 type = void_type_node;
13775 }
13776 }
13777 else if (memfn_quals || rqual)
13778 {
13779 if (ctype == NULL_TREE
13780 && TREE_CODE (type) == METHOD_TYPE)
13781 ctype = TYPE_METHOD_BASETYPE (type);
13782
13783 if (ctype)
13784 type = build_memfn_type (type, ctype, memfn_quals, rqual);
13785 /* Core issue #547: need to allow this in template type args.
13786 Allow it in general in C++11 for alias-declarations. */
13787 else if ((template_type_arg || cxx_dialect >= cxx11)
13788 && TREE_CODE (type) == FUNCTION_TYPE)
13789 type = apply_memfn_quals (type, memfn_quals, rqual);
13790 else
13791 error ("invalid qualifiers on non-member function type");
13792 }
13793
13794 if (reqs)
13795 error_at (location_of (reqs), "requires-clause on type-id");
13796
13797 return type;
13798 }
13799 else if (unqualified_id == NULL_TREE && decl_context != PARM
13800 && decl_context != CATCHPARM
13801 && TREE_CODE (type) != UNION_TYPE
13802 && ! bitfield
13803 && innermost_code != cdk_decomp)
13804 {
13805 error ("abstract declarator %qT used as declaration", type);
13806 return error_mark_node;
13807 }
13808
13809 if (!FUNC_OR_METHOD_TYPE_P (type))
13810 {
13811 /* Only functions may be declared using an operator-function-id. */
13812 if (dname && IDENTIFIER_ANY_OP_P (dname))
13813 {
13814 error_at (id_loc, "declaration of %qD as non-function", dname);
13815 return error_mark_node;
13816 }
13817
13818 if (reqs)
13819 error_at (location_of (reqs),
13820 "requires-clause on declaration of non-function type %qT",
13821 type);
13822 }
13823
13824 /* We don't check parameter types here because we can emit a better
13825 error message later. */
13826 if (decl_context != PARM)
13827 {
13828 type = check_var_type (unqualified_id, type, id_loc);
13829 if (type == error_mark_node)
13830 return error_mark_node;
13831 }
13832
13833 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
13834 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
13835
13836 if (decl_context == PARM || decl_context == CATCHPARM)
13837 {
13838 if (ctype || in_namespace)
13839 error ("cannot use %<::%> in parameter declaration");
13840
13841 tree auto_node = type_uses_auto (type);
13842 if (auto_node && !(cxx_dialect >= cxx17 && template_parm_flag))
13843 {
13844 if (cxx_dialect >= cxx14)
13845 {
13846 if (decl_context == PARM && AUTO_IS_DECLTYPE (auto_node))
13847 error_at (typespec_loc,
13848 "cannot declare a parameter with %<decltype(auto)%>");
13849 else if (tree c = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
13850 {
13851 auto_diagnostic_group g;
13852 error_at (typespec_loc,
13853 "class template placeholder %qE not permitted "
13854 "in this context", c);
13855 if (decl_context == PARM && cxx_dialect >= cxx20)
13856 inform (typespec_loc, "use %<auto%> for an "
13857 "abbreviated function template");
13858 }
13859 else
13860 error_at (typespec_loc,
13861 "%<auto%> parameter not permitted in this context");
13862 }
13863 else
13864 error_at (typespec_loc, "parameter declared %<auto%>");
13865 type = error_mark_node;
13866 }
13867
13868 /* A parameter declared as an array of T is really a pointer to T.
13869 One declared as a function is really a pointer to a function.
13870 One declared as a member is really a pointer to member. */
13871
13872 if (TREE_CODE (type) == ARRAY_TYPE)
13873 {
13874 /* Transfer const-ness of array into that of type pointed to. */
13875 type = build_pointer_type (TREE_TYPE (type));
13876 type_quals = TYPE_UNQUALIFIED;
13877 array_parameter_p = true;
13878 }
13879 else if (TREE_CODE (type) == FUNCTION_TYPE)
13880 type = build_pointer_type (type);
13881 }
13882
13883 if (ctype && TREE_CODE (type) == FUNCTION_TYPE && staticp < 2
13884 && !(unqualified_id
13885 && identifier_p (unqualified_id)
13886 && IDENTIFIER_NEWDEL_OP_P (unqualified_id)))
13887 {
13888 cp_cv_quals real_quals = memfn_quals;
13889 if (cxx_dialect < cxx14 && constexpr_p
13890 && sfk != sfk_constructor && sfk != sfk_destructor)
13891 real_quals |= TYPE_QUAL_CONST;
13892 type = build_memfn_type (type, ctype, real_quals, rqual);
13893 }
13894
13895 {
13896 tree decl = NULL_TREE;
13897
13898 if (decl_context == PARM)
13899 {
13900 decl = cp_build_parm_decl (NULL_TREE, unqualified_id, type);
13901 DECL_ARRAY_PARAMETER_P (decl) = array_parameter_p;
13902
13903 bad_specifiers (decl, BSP_PARM, virtualp,
13904 memfn_quals != TYPE_UNQUALIFIED,
13905 inlinep, friendp, raises != NULL_TREE,
13906 declspecs->locations);
13907 }
13908 else if (decl_context == FIELD)
13909 {
13910 if (!staticp && !friendp && !FUNC_OR_METHOD_TYPE_P (type))
13911 if (tree auto_node = type_uses_auto (type))
13912 {
13913 location_t tloc = declspecs->locations[ds_type_spec];
13914 if (CLASS_PLACEHOLDER_TEMPLATE (auto_node))
13915 error_at (tloc, "invalid use of template-name %qE without an "
13916 "argument list",
13917 CLASS_PLACEHOLDER_TEMPLATE (auto_node));
13918 else
13919 error_at (tloc, "non-static data member declared with "
13920 "placeholder %qT", auto_node);
13921 type = error_mark_node;
13922 }
13923
13924 /* The C99 flexible array extension. */
13925 if (!staticp && TREE_CODE (type) == ARRAY_TYPE
13926 && TYPE_DOMAIN (type) == NULL_TREE)
13927 {
13928 if (ctype
13929 && (TREE_CODE (ctype) == UNION_TYPE
13930 || TREE_CODE (ctype) == QUAL_UNION_TYPE))
13931 {
13932 error_at (id_loc, "flexible array member in union");
13933 type = error_mark_node;
13934 }
13935 else
13936 {
13937 /* Array is a flexible member. */
13938 if (name)
13939 pedwarn (id_loc, OPT_Wpedantic,
13940 "ISO C++ forbids flexible array member %qs", name);
13941 else
13942 pedwarn (input_location, OPT_Wpedantic,
13943 "ISO C++ forbids flexible array members");
13944
13945 /* Flexible array member has a null domain. */
13946 type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
13947 }
13948 }
13949
13950 if (type == error_mark_node)
13951 {
13952 /* Happens when declaring arrays of sizes which
13953 are error_mark_node, for example. */
13954 decl = NULL_TREE;
13955 }
13956 else if (in_namespace && !friendp)
13957 {
13958 /* Something like struct S { int N::j; }; */
13959 error_at (id_loc, "invalid use of %<::%>");
13960 return error_mark_node;
13961 }
13962 else if (FUNC_OR_METHOD_TYPE_P (type) && unqualified_id)
13963 {
13964 int publicp = 0;
13965 tree function_context;
13966
13967 if (friendp == 0)
13968 {
13969 /* This should never happen in pure C++ (the check
13970 could be an assert). It could happen in
13971 Objective-C++ if someone writes invalid code that
13972 uses a function declaration for an instance
13973 variable or property (instance variables and
13974 properties are parsed as FIELD_DECLs, but they are
13975 part of an Objective-C class, not a C++ class).
13976 That code is invalid and is caught by this
13977 check. */
13978 if (!ctype)
13979 {
13980 error ("declaration of function %qD in invalid context",
13981 unqualified_id);
13982 return error_mark_node;
13983 }
13984
13985 /* ``A union may [ ... ] not [ have ] virtual functions.''
13986 ARM 9.5 */
13987 if (virtualp && TREE_CODE (ctype) == UNION_TYPE)
13988 {
13989 error_at (declspecs->locations[ds_virtual],
13990 "function %qD declared %<virtual%> inside a union",
13991 unqualified_id);
13992 return error_mark_node;
13993 }
13994
13995 if (virtualp
13996 && identifier_p (unqualified_id)
13997 && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
13998 {
13999 error_at (declspecs->locations[ds_virtual],
14000 "%qD cannot be declared %<virtual%>, since it "
14001 "is always static", unqualified_id);
14002 virtualp = 0;
14003 }
14004 }
14005
14006 /* Check that the name used for a destructor makes sense. */
14007 if (sfk == sfk_destructor)
14008 {
14009 tree uqname = id_declarator->u.id.unqualified_name;
14010
14011 if (!ctype)
14012 {
14013 gcc_assert (friendp);
14014 error_at (id_loc, "expected qualified name in friend "
14015 "declaration for destructor %qD", uqname);
14016 return error_mark_node;
14017 }
14018
14019 if (!check_dtor_name (ctype, TREE_OPERAND (uqname, 0)))
14020 {
14021 error_at (id_loc, "declaration of %qD as member of %qT",
14022 uqname, ctype);
14023 return error_mark_node;
14024 }
14025 if (concept_p)
14026 {
14027 error_at (declspecs->locations[ds_concept],
14028 "a destructor cannot be %qs", "concept");
14029 return error_mark_node;
14030 }
14031 if (constexpr_p && cxx_dialect < cxx20)
14032 {
14033 error_at (declspecs->locations[ds_constexpr],
14034 "%<constexpr%> destructors only available"
14035 " with %<-std=c++20%> or %<-std=gnu++20%>");
14036 return error_mark_node;
14037 }
14038 if (consteval_p)
14039 {
14040 error_at (declspecs->locations[ds_consteval],
14041 "a destructor cannot be %qs", "consteval");
14042 return error_mark_node;
14043 }
14044 }
14045 else if (sfk == sfk_constructor && friendp && !ctype)
14046 {
14047 error ("expected qualified name in friend declaration "
14048 "for constructor %qD",
14049 id_declarator->u.id.unqualified_name);
14050 return error_mark_node;
14051 }
14052 if (sfk == sfk_constructor)
14053 if (concept_p)
14054 {
14055 error_at (declspecs->locations[ds_concept],
14056 "a constructor cannot be %<concept%>");
14057 return error_mark_node;
14058 }
14059 if (concept_p)
14060 {
14061 error_at (declspecs->locations[ds_concept],
14062 "a concept cannot be a member function");
14063 concept_p = false;
14064 }
14065 else if (consteval_p
14066 && identifier_p (unqualified_id)
14067 && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
14068 {
14069 error_at (declspecs->locations[ds_consteval],
14070 "%qD cannot be %qs", unqualified_id, "consteval");
14071 consteval_p = false;
14072 }
14073
14074 if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
14075 {
14076 tree tmpl = TREE_OPERAND (unqualified_id, 0);
14077 if (variable_template_p (tmpl))
14078 {
14079 error_at (id_loc, "specialization of variable template "
14080 "%qD declared as function", tmpl);
14081 inform (DECL_SOURCE_LOCATION (tmpl),
14082 "variable template declared here");
14083 return error_mark_node;
14084 }
14085 }
14086
14087 /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node. */
14088 function_context
14089 = (ctype != NULL_TREE
14090 ? decl_function_context (TYPE_MAIN_DECL (ctype)) : NULL_TREE);
14091 publicp = ((! friendp || ! staticp)
14092 && function_context == NULL_TREE);
14093
14094 decl = grokfndecl (ctype, type,
14095 TREE_CODE (unqualified_id) != TEMPLATE_ID_EXPR
14096 ? unqualified_id : dname,
14097 parms,
14098 unqualified_id,
14099 declspecs,
14100 reqs,
14101 virtualp, flags, memfn_quals, rqual, raises,
14102 friendp ? -1 : 0, friendp, publicp,
14103 inlinep | (2 * constexpr_p) | (4 * concept_p)
14104 | (8 * consteval_p),
14105 initialized == SD_DELETED, sfk,
14106 funcdef_flag, late_return_type_p,
14107 template_count, in_namespace,
14108 attrlist, id_loc);
14109 decl = set_virt_specifiers (decl, virt_specifiers);
14110 if (decl == NULL_TREE)
14111 return error_mark_node;
14112 #if 0
14113 /* This clobbers the attrs stored in `decl' from `attrlist'. */
14114 /* The decl and setting of decl_attr is also turned off. */
14115 decl = build_decl_attribute_variant (decl, decl_attr);
14116 #endif
14117
14118 /* [class.conv.ctor]
14119
14120 A constructor declared without the function-specifier
14121 explicit that can be called with a single parameter
14122 specifies a conversion from the type of its first
14123 parameter to the type of its class. Such a constructor
14124 is called a converting constructor. */
14125 if (explicitp == 2)
14126 DECL_NONCONVERTING_P (decl) = 1;
14127
14128 if (declspecs->explicit_specifier)
14129 store_explicit_specifier (decl, declspecs->explicit_specifier);
14130 }
14131 else if (!staticp
14132 && ((current_class_type
14133 && same_type_p (type, current_class_type))
14134 || (!dependent_type_p (type)
14135 && !COMPLETE_TYPE_P (complete_type (type))
14136 && (!complete_or_array_type_p (type)
14137 || initialized == SD_UNINITIALIZED))))
14138 {
14139 if (TREE_CODE (type) != ARRAY_TYPE
14140 || !COMPLETE_TYPE_P (TREE_TYPE (type)))
14141 {
14142 if (unqualified_id)
14143 {
14144 error_at (id_loc, "field %qD has incomplete type %qT",
14145 unqualified_id, type);
14146 cxx_incomplete_type_inform (strip_array_types (type));
14147 }
14148 else
14149 error ("name %qT has incomplete type", type);
14150
14151 type = error_mark_node;
14152 decl = NULL_TREE;
14153 }
14154 }
14155 else if (!verify_type_context (input_location,
14156 staticp
14157 ? TCTX_STATIC_STORAGE
14158 : TCTX_FIELD, type))
14159 {
14160 type = error_mark_node;
14161 decl = NULL_TREE;
14162 }
14163 else
14164 {
14165 if (friendp)
14166 {
14167 if (unqualified_id)
14168 error_at (id_loc,
14169 "%qE is neither function nor member function; "
14170 "cannot be declared friend", unqualified_id);
14171 else
14172 error ("unnamed field is neither function nor member "
14173 "function; cannot be declared friend");
14174 return error_mark_node;
14175 }
14176 decl = NULL_TREE;
14177 }
14178
14179 if (friendp)
14180 {
14181 /* Packages tend to use GNU attributes on friends, so we only
14182 warn for standard attributes. */
14183 if (attrlist && !funcdef_flag && cxx11_attribute_p (*attrlist))
14184 {
14185 *attrlist = NULL_TREE;
14186 if (warning_at (id_loc, OPT_Wattributes, "attribute ignored"))
14187 inform (id_loc, "an attribute that appertains to a friend "
14188 "declaration that is not a definition is ignored");
14189 }
14190 /* Friends are treated specially. */
14191 if (ctype == current_class_type)
14192 ; /* We already issued a permerror. */
14193 else if (decl && DECL_NAME (decl))
14194 {
14195 set_originating_module (decl, true);
14196
14197 if (initialized)
14198 /* Kludge: We need funcdef_flag to be true in do_friend for
14199 in-class defaulted functions, but that breaks grokfndecl.
14200 So set it here. */
14201 funcdef_flag = true;
14202
14203 if (template_class_depth (current_class_type) == 0)
14204 {
14205 decl = check_explicit_specialization
14206 (unqualified_id, decl, template_count,
14207 2 * funcdef_flag + 4);
14208 if (decl == error_mark_node)
14209 return error_mark_node;
14210 }
14211
14212 tree scope = ctype ? ctype : in_namespace;
14213 decl = do_friend (scope, unqualified_id, decl,
14214 flags, funcdef_flag);
14215 return decl;
14216 }
14217 else
14218 return error_mark_node;
14219 }
14220
14221 /* Structure field. It may not be a function, except for C++. */
14222
14223 if (decl == NULL_TREE)
14224 {
14225 if (staticp)
14226 {
14227 /* C++ allows static class members. All other work
14228 for this is done by grokfield. */
14229 decl = build_lang_decl_loc (id_loc, VAR_DECL,
14230 unqualified_id, type);
14231 set_linkage_for_static_data_member (decl);
14232 if (concept_p)
14233 error_at (declspecs->locations[ds_concept],
14234 "static data member %qE declared %qs",
14235 unqualified_id, "concept");
14236 else if (constexpr_p && !initialized)
14237 {
14238 error_at (DECL_SOURCE_LOCATION (decl),
14239 "%<constexpr%> static data member %qD must "
14240 "have an initializer", decl);
14241 constexpr_p = false;
14242 }
14243 if (consteval_p)
14244 error_at (declspecs->locations[ds_consteval],
14245 "static data member %qE declared %qs",
14246 unqualified_id, "consteval");
14247
14248 if (inlinep)
14249 mark_inline_variable (decl, declspecs->locations[ds_inline]);
14250
14251 if (!DECL_VAR_DECLARED_INLINE_P (decl)
14252 && !(cxx_dialect >= cxx17 && constexpr_p))
14253 /* Even if there is an in-class initialization, DECL
14254 is considered undefined until an out-of-class
14255 definition is provided, unless this is an inline
14256 variable. */
14257 DECL_EXTERNAL (decl) = 1;
14258
14259 if (thread_p)
14260 {
14261 CP_DECL_THREAD_LOCAL_P (decl) = true;
14262 if (!processing_template_decl)
14263 set_decl_tls_model (decl, decl_default_tls_model (decl));
14264 if (declspecs->gnu_thread_keyword_p)
14265 SET_DECL_GNU_TLS_P (decl);
14266 }
14267
14268 /* Set the constraints on the declaration. */
14269 bool memtmpl = (current_template_depth
14270 > template_class_depth (current_class_type));
14271 if (memtmpl)
14272 {
14273 tree tmpl_reqs
14274 = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
14275 tree ci = build_constraints (tmpl_reqs, NULL_TREE);
14276 set_constraints (decl, ci);
14277 }
14278 }
14279 else
14280 {
14281 if (concept_p)
14282 {
14283 error_at (declspecs->locations[ds_concept],
14284 "non-static data member %qE declared %qs",
14285 unqualified_id, "concept");
14286 concept_p = false;
14287 constexpr_p = false;
14288 }
14289 else if (constexpr_p)
14290 {
14291 error_at (declspecs->locations[ds_constexpr],
14292 "non-static data member %qE declared %qs",
14293 unqualified_id, "constexpr");
14294 constexpr_p = false;
14295 }
14296 if (constinit_p)
14297 {
14298 error_at (declspecs->locations[ds_constinit],
14299 "non-static data member %qE declared %qs",
14300 unqualified_id, "constinit");
14301 constinit_p = false;
14302 }
14303 if (consteval_p)
14304 {
14305 error_at (declspecs->locations[ds_consteval],
14306 "non-static data member %qE declared %qs",
14307 unqualified_id, "consteval");
14308 consteval_p = false;
14309 }
14310 decl = build_decl (id_loc, FIELD_DECL, unqualified_id, type);
14311 DECL_NONADDRESSABLE_P (decl) = bitfield;
14312 if (bitfield && !unqualified_id)
14313 DECL_PADDING_P (decl) = 1;
14314
14315 if (storage_class == sc_mutable)
14316 {
14317 DECL_MUTABLE_P (decl) = 1;
14318 storage_class = sc_none;
14319 }
14320
14321 if (initialized)
14322 {
14323 /* An attempt is being made to initialize a non-static
14324 member. This is new in C++11. */
14325 maybe_warn_cpp0x (CPP0X_NSDMI, init_loc);
14326
14327 /* If this has been parsed with static storage class, but
14328 errors forced staticp to be cleared, ensure NSDMI is
14329 not present. */
14330 if (declspecs->storage_class == sc_static)
14331 DECL_INITIAL (decl) = error_mark_node;
14332 }
14333 }
14334
14335 bad_specifiers (decl, BSP_FIELD, virtualp,
14336 memfn_quals != TYPE_UNQUALIFIED,
14337 staticp ? false : inlinep, friendp,
14338 raises != NULL_TREE,
14339 declspecs->locations);
14340 }
14341 }
14342 else if (FUNC_OR_METHOD_TYPE_P (type))
14343 {
14344 tree original_name;
14345 int publicp = 0;
14346
14347 if (!unqualified_id)
14348 return error_mark_node;
14349
14350 if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
14351 original_name = dname;
14352 else
14353 original_name = unqualified_id;
14354 // FIXME:gcc_assert (original_name == dname);
14355
14356 if (storage_class == sc_auto)
14357 error_at (declspecs->locations[ds_storage_class],
14358 "storage class %<auto%> invalid for function %qs", name);
14359 else if (storage_class == sc_register)
14360 error_at (declspecs->locations[ds_storage_class],
14361 "storage class %<register%> invalid for function %qs",
14362 name);
14363 else if (thread_p)
14364 {
14365 if (declspecs->gnu_thread_keyword_p)
14366 error_at (declspecs->locations[ds_thread],
14367 "storage class %<__thread%> invalid for function %qs",
14368 name);
14369 else
14370 error_at (declspecs->locations[ds_thread],
14371 "storage class %<thread_local%> invalid for "
14372 "function %qs", name);
14373 }
14374
14375 if (virt_specifiers)
14376 error ("virt-specifiers in %qs not allowed outside a class "
14377 "definition", name);
14378 /* Function declaration not at top level.
14379 Storage classes other than `extern' are not allowed
14380 and `extern' makes no difference. */
14381 if (! toplevel_bindings_p ()
14382 && (storage_class == sc_static
14383 || decl_spec_seq_has_spec_p (declspecs, ds_inline))
14384 && pedantic)
14385 {
14386 if (storage_class == sc_static)
14387 pedwarn (declspecs->locations[ds_storage_class], OPT_Wpedantic,
14388 "%<static%> specifier invalid for function %qs "
14389 "declared out of global scope", name);
14390 else
14391 pedwarn (declspecs->locations[ds_inline], OPT_Wpedantic,
14392 "%<inline%> specifier invalid for function %qs "
14393 "declared out of global scope", name);
14394 }
14395
14396 if (ctype == NULL_TREE)
14397 {
14398 if (virtualp)
14399 {
14400 error ("virtual non-class function %qs", name);
14401 virtualp = 0;
14402 }
14403 else if (sfk == sfk_constructor
14404 || sfk == sfk_destructor)
14405 {
14406 error (funcdef_flag
14407 ? G_("%qs defined in a non-class scope")
14408 : G_("%qs declared in a non-class scope"), name);
14409 sfk = sfk_none;
14410 }
14411 }
14412 if (consteval_p
14413 && identifier_p (unqualified_id)
14414 && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
14415 {
14416 error_at (declspecs->locations[ds_consteval],
14417 "%qD cannot be %qs", unqualified_id, "consteval");
14418 consteval_p = false;
14419 }
14420
14421 /* Record whether the function is public. */
14422 publicp = (ctype != NULL_TREE
14423 || storage_class != sc_static);
14424
14425 decl = grokfndecl (ctype, type, original_name, parms, unqualified_id,
14426 declspecs,
14427 reqs, virtualp, flags, memfn_quals, rqual, raises,
14428 1, friendp,
14429 publicp,
14430 inlinep | (2 * constexpr_p) | (4 * concept_p)
14431 | (8 * consteval_p),
14432 initialized == SD_DELETED,
14433 sfk,
14434 funcdef_flag,
14435 late_return_type_p,
14436 template_count, in_namespace, attrlist,
14437 id_loc);
14438 if (decl == NULL_TREE)
14439 return error_mark_node;
14440
14441 if (explicitp == 2)
14442 DECL_NONCONVERTING_P (decl) = 1;
14443 if (staticp == 1)
14444 {
14445 int invalid_static = 0;
14446
14447 /* Don't allow a static member function in a class, and forbid
14448 declaring main to be static. */
14449 if (TREE_CODE (type) == METHOD_TYPE)
14450 {
14451 permerror (input_location, "cannot declare member function %qD to have "
14452 "static linkage", decl);
14453 invalid_static = 1;
14454 }
14455 else if (current_function_decl)
14456 {
14457 /* 7.1.1: There can be no static function declarations within a
14458 block. */
14459 error_at (declspecs->locations[ds_storage_class],
14460 "cannot declare static function inside another function");
14461 invalid_static = 1;
14462 }
14463
14464 if (invalid_static)
14465 {
14466 staticp = 0;
14467 storage_class = sc_none;
14468 }
14469 }
14470 if (declspecs->explicit_specifier)
14471 store_explicit_specifier (decl, declspecs->explicit_specifier);
14472 }
14473 else
14474 {
14475 /* It's a variable. */
14476
14477 /* An uninitialized decl with `extern' is a reference. */
14478 decl = grokvardecl (type, dname, unqualified_id,
14479 declspecs,
14480 initialized,
14481 type_quals,
14482 inlinep,
14483 concept_p,
14484 template_count,
14485 ctype ? ctype : in_namespace,
14486 id_loc);
14487 if (decl == NULL_TREE)
14488 return error_mark_node;
14489
14490 bad_specifiers (decl, BSP_VAR, virtualp,
14491 memfn_quals != TYPE_UNQUALIFIED,
14492 inlinep, friendp, raises != NULL_TREE,
14493 declspecs->locations);
14494
14495 if (ctype)
14496 {
14497 DECL_CONTEXT (decl) = ctype;
14498 if (staticp == 1)
14499 {
14500 permerror (declspecs->locations[ds_storage_class],
14501 "%<static%> may not be used when defining "
14502 "(as opposed to declaring) a static data member");
14503 staticp = 0;
14504 storage_class = sc_none;
14505 }
14506 if (storage_class == sc_register && TREE_STATIC (decl))
14507 {
14508 error ("static member %qD declared %<register%>", decl);
14509 storage_class = sc_none;
14510 }
14511 if (storage_class == sc_extern && pedantic)
14512 {
14513 pedwarn (input_location, OPT_Wpedantic,
14514 "cannot explicitly declare member %q#D to have "
14515 "extern linkage", decl);
14516 storage_class = sc_none;
14517 }
14518 }
14519 else if (constexpr_p && DECL_EXTERNAL (decl))
14520 {
14521 error_at (DECL_SOURCE_LOCATION (decl),
14522 "declaration of %<constexpr%> variable %qD "
14523 "is not a definition", decl);
14524 constexpr_p = false;
14525 }
14526 if (consteval_p)
14527 {
14528 error_at (DECL_SOURCE_LOCATION (decl),
14529 "a variable cannot be declared %<consteval%>");
14530 consteval_p = false;
14531 }
14532
14533 if (inlinep)
14534 mark_inline_variable (decl, declspecs->locations[ds_inline]);
14535 if (innermost_code == cdk_decomp)
14536 {
14537 gcc_assert (declarator && declarator->kind == cdk_decomp);
14538 DECL_SOURCE_LOCATION (decl) = id_loc;
14539 DECL_ARTIFICIAL (decl) = 1;
14540 fit_decomposition_lang_decl (decl, NULL_TREE);
14541 }
14542 }
14543
14544 if (VAR_P (decl) && !initialized)
14545 if (tree auto_node = type_uses_auto (type))
14546 if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node))
14547 {
14548 location_t loc = declspecs->locations[ds_type_spec];
14549 error_at (loc, "declaration of %q#D has no initializer", decl);
14550 TREE_TYPE (decl) = error_mark_node;
14551 }
14552
14553 if (storage_class == sc_extern && initialized && !funcdef_flag)
14554 {
14555 if (toplevel_bindings_p ())
14556 {
14557 /* It's common practice (and completely valid) to have a const
14558 be initialized and declared extern. */
14559 if (!(type_quals & TYPE_QUAL_CONST))
14560 warning_at (DECL_SOURCE_LOCATION (decl), 0,
14561 "%qs initialized and declared %<extern%>", name);
14562 }
14563 else
14564 {
14565 error_at (DECL_SOURCE_LOCATION (decl),
14566 "%qs has both %<extern%> and initializer", name);
14567 return error_mark_node;
14568 }
14569 }
14570
14571 /* Record `register' declaration for warnings on &
14572 and in case doing stupid register allocation. */
14573
14574 if (storage_class == sc_register)
14575 {
14576 DECL_REGISTER (decl) = 1;
14577 /* Warn about register storage specifiers on PARM_DECLs. */
14578 if (TREE_CODE (decl) == PARM_DECL)
14579 {
14580 if (cxx_dialect >= cxx17)
14581 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
14582 "ISO C++17 does not allow %<register%> storage "
14583 "class specifier");
14584 else
14585 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
14586 "%<register%> storage class specifier used");
14587 }
14588 }
14589 else if (storage_class == sc_extern)
14590 DECL_THIS_EXTERN (decl) = 1;
14591 else if (storage_class == sc_static)
14592 DECL_THIS_STATIC (decl) = 1;
14593
14594 if (VAR_P (decl))
14595 {
14596 /* Set constexpr flag on vars (functions got it in grokfndecl). */
14597 if (constexpr_p)
14598 DECL_DECLARED_CONSTEXPR_P (decl) = true;
14599 /* And the constinit flag (which only applies to variables). */
14600 else if (constinit_p)
14601 DECL_DECLARED_CONSTINIT_P (decl) = true;
14602 }
14603
14604 /* Record constancy and volatility on the DECL itself . There's
14605 no need to do this when processing a template; we'll do this
14606 for the instantiated declaration based on the type of DECL. */
14607 if (!processing_template_decl)
14608 cp_apply_type_quals_to_decl (type_quals, decl);
14609
14610 return decl;
14611 }
14612 }
14613
14614 /* Subroutine of start_function. Ensure that each of the parameter
14615 types (as listed in PARMS) is complete, as is required for a
14616 function definition. */
14617
14618 static void
require_complete_types_for_parms(tree parms)14619 require_complete_types_for_parms (tree parms)
14620 {
14621 for (; parms; parms = DECL_CHAIN (parms))
14622 {
14623 if (dependent_type_p (TREE_TYPE (parms)))
14624 continue;
14625 if (!VOID_TYPE_P (TREE_TYPE (parms))
14626 && complete_type_or_else (TREE_TYPE (parms), parms))
14627 {
14628 relayout_decl (parms);
14629 DECL_ARG_TYPE (parms) = type_passed_as (TREE_TYPE (parms));
14630
14631 abstract_virtuals_error (parms, TREE_TYPE (parms));
14632 maybe_warn_parm_abi (TREE_TYPE (parms),
14633 DECL_SOURCE_LOCATION (parms));
14634 }
14635 else
14636 /* grokparms or complete_type_or_else will have already issued
14637 an error. */
14638 TREE_TYPE (parms) = error_mark_node;
14639 }
14640 }
14641
14642 /* Returns nonzero if T is a local variable. */
14643
14644 int
local_variable_p(const_tree t)14645 local_variable_p (const_tree t)
14646 {
14647 if ((VAR_P (t)
14648 && (DECL_LOCAL_DECL_P (t)
14649 || !DECL_CONTEXT (t)
14650 || TREE_CODE (DECL_CONTEXT (t)) == FUNCTION_DECL))
14651 || (TREE_CODE (t) == PARM_DECL))
14652 return 1;
14653
14654 return 0;
14655 }
14656
14657 /* Like local_variable_p, but suitable for use as a tree-walking
14658 function. */
14659
14660 static tree
local_variable_p_walkfn(tree * tp,int * walk_subtrees,void *)14661 local_variable_p_walkfn (tree *tp, int *walk_subtrees,
14662 void * /*data*/)
14663 {
14664 if (unevaluated_p (TREE_CODE (*tp)))
14665 {
14666 /* DR 2082 permits local variables in unevaluated contexts
14667 within a default argument. */
14668 *walk_subtrees = 0;
14669 return NULL_TREE;
14670 }
14671
14672 if (local_variable_p (*tp)
14673 && (!DECL_ARTIFICIAL (*tp) || DECL_NAME (*tp) == this_identifier))
14674 return *tp;
14675 else if (TYPE_P (*tp))
14676 *walk_subtrees = 0;
14677
14678 return NULL_TREE;
14679 }
14680
14681 /* Check that ARG, which is a default-argument expression for a
14682 parameter DECL, is valid. Returns ARG, or ERROR_MARK_NODE, if
14683 something goes wrong. DECL may also be a _TYPE node, rather than a
14684 DECL, if there is no DECL available. */
14685
14686 tree
check_default_argument(tree decl,tree arg,tsubst_flags_t complain)14687 check_default_argument (tree decl, tree arg, tsubst_flags_t complain)
14688 {
14689 tree var;
14690 tree decl_type;
14691
14692 if (TREE_CODE (arg) == DEFERRED_PARSE)
14693 /* We get a DEFERRED_PARSE when looking at an in-class declaration
14694 with a default argument. Ignore the argument for now; we'll
14695 deal with it after the class is complete. */
14696 return arg;
14697
14698 if (TYPE_P (decl))
14699 {
14700 decl_type = decl;
14701 decl = NULL_TREE;
14702 }
14703 else
14704 decl_type = TREE_TYPE (decl);
14705
14706 if (arg == error_mark_node
14707 || decl == error_mark_node
14708 || TREE_TYPE (arg) == error_mark_node
14709 || decl_type == error_mark_node)
14710 /* Something already went wrong. There's no need to check
14711 further. */
14712 return error_mark_node;
14713
14714 /* [dcl.fct.default]
14715
14716 A default argument expression is implicitly converted to the
14717 parameter type. */
14718 ++cp_unevaluated_operand;
14719 /* Avoid digest_init clobbering the initializer. */
14720 tree carg = BRACE_ENCLOSED_INITIALIZER_P (arg) ? unshare_expr (arg): arg;
14721 perform_implicit_conversion_flags (decl_type, carg, complain,
14722 LOOKUP_IMPLICIT);
14723 --cp_unevaluated_operand;
14724
14725 /* Avoid redundant -Wzero-as-null-pointer-constant warnings at
14726 the call sites. */
14727 if (TYPE_PTR_OR_PTRMEM_P (decl_type)
14728 && null_ptr_cst_p (arg)
14729 /* Don't lose side-effects as in PR90473. */
14730 && !TREE_SIDE_EFFECTS (arg))
14731 return nullptr_node;
14732
14733 /* [dcl.fct.default]
14734
14735 Local variables shall not be used in default argument
14736 expressions.
14737
14738 The keyword `this' shall not be used in a default argument of a
14739 member function. */
14740 var = cp_walk_tree_without_duplicates (&arg, local_variable_p_walkfn, NULL);
14741 if (var)
14742 {
14743 if (complain & tf_warning_or_error)
14744 {
14745 if (DECL_NAME (var) == this_identifier)
14746 permerror (input_location, "default argument %qE uses %qD",
14747 arg, var);
14748 else
14749 error ("default argument %qE uses local variable %qD", arg, var);
14750 }
14751 return error_mark_node;
14752 }
14753
14754 /* All is well. */
14755 return arg;
14756 }
14757
14758 /* Returns a deprecated type used within TYPE, or NULL_TREE if none. */
14759
14760 static tree
type_is_deprecated(tree type)14761 type_is_deprecated (tree type)
14762 {
14763 enum tree_code code;
14764 if (TREE_DEPRECATED (type))
14765 return type;
14766 if (TYPE_NAME (type))
14767 {
14768 if (TREE_DEPRECATED (TYPE_NAME (type)))
14769 return type;
14770 else
14771 {
14772 cp_warn_deprecated_use_scopes (CP_DECL_CONTEXT (TYPE_NAME (type)));
14773 return NULL_TREE;
14774 }
14775 }
14776
14777 /* Do warn about using typedefs to a deprecated class. */
14778 if (OVERLOAD_TYPE_P (type) && type != TYPE_MAIN_VARIANT (type))
14779 return type_is_deprecated (TYPE_MAIN_VARIANT (type));
14780
14781 code = TREE_CODE (type);
14782
14783 if (code == POINTER_TYPE || code == REFERENCE_TYPE
14784 || code == OFFSET_TYPE || code == FUNCTION_TYPE
14785 || code == METHOD_TYPE || code == ARRAY_TYPE)
14786 return type_is_deprecated (TREE_TYPE (type));
14787
14788 if (TYPE_PTRMEMFUNC_P (type))
14789 return type_is_deprecated
14790 (TREE_TYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type))));
14791
14792 return NULL_TREE;
14793 }
14794
14795 /* Returns an unavailable type used within TYPE, or NULL_TREE if none. */
14796
14797 static tree
type_is_unavailable(tree type)14798 type_is_unavailable (tree type)
14799 {
14800 enum tree_code code;
14801 if (TREE_UNAVAILABLE (type))
14802 return type;
14803 if (TYPE_NAME (type))
14804 {
14805 if (TREE_UNAVAILABLE (TYPE_NAME (type)))
14806 return type;
14807 else
14808 {
14809 cp_warn_deprecated_use_scopes (CP_DECL_CONTEXT (TYPE_NAME (type)));
14810 return NULL_TREE;
14811 }
14812 }
14813
14814 /* Do warn about using typedefs to a deprecated class. */
14815 if (OVERLOAD_TYPE_P (type) && type != TYPE_MAIN_VARIANT (type))
14816 return type_is_deprecated (TYPE_MAIN_VARIANT (type));
14817
14818 code = TREE_CODE (type);
14819
14820 if (code == POINTER_TYPE || code == REFERENCE_TYPE
14821 || code == OFFSET_TYPE || code == FUNCTION_TYPE
14822 || code == METHOD_TYPE || code == ARRAY_TYPE)
14823 return type_is_unavailable (TREE_TYPE (type));
14824
14825 if (TYPE_PTRMEMFUNC_P (type))
14826 return type_is_unavailable
14827 (TREE_TYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type))));
14828
14829 return NULL_TREE;
14830 }
14831
14832 /* Decode the list of parameter types for a function type.
14833 Given the list of things declared inside the parens,
14834 return a list of types.
14835
14836 If this parameter does not end with an ellipsis, we append
14837 void_list_node.
14838
14839 *PARMS is set to the chain of PARM_DECLs created. */
14840
14841 tree
grokparms(tree parmlist,tree * parms)14842 grokparms (tree parmlist, tree *parms)
14843 {
14844 tree result = NULL_TREE;
14845 tree decls = NULL_TREE;
14846 tree parm;
14847 int any_error = 0;
14848
14849 for (parm = parmlist; parm != NULL_TREE; parm = TREE_CHAIN (parm))
14850 {
14851 tree type = NULL_TREE;
14852 tree init = TREE_PURPOSE (parm);
14853 tree decl = TREE_VALUE (parm);
14854
14855 if (parm == void_list_node || parm == explicit_void_list_node)
14856 break;
14857
14858 if (! decl || TREE_TYPE (decl) == error_mark_node)
14859 {
14860 any_error = 1;
14861 continue;
14862 }
14863
14864 type = TREE_TYPE (decl);
14865 if (VOID_TYPE_P (type))
14866 {
14867 if (same_type_p (type, void_type_node)
14868 && !init
14869 && !DECL_NAME (decl) && !result
14870 && TREE_CHAIN (parm) == void_list_node)
14871 /* DR 577: A parameter list consisting of a single
14872 unnamed parameter of non-dependent type 'void'. */
14873 break;
14874 else if (cv_qualified_p (type))
14875 error_at (DECL_SOURCE_LOCATION (decl),
14876 "invalid use of cv-qualified type %qT in "
14877 "parameter declaration", type);
14878 else
14879 error_at (DECL_SOURCE_LOCATION (decl),
14880 "invalid use of type %<void%> in parameter "
14881 "declaration");
14882 /* It's not a good idea to actually create parameters of
14883 type `void'; other parts of the compiler assume that a
14884 void type terminates the parameter list. */
14885 type = error_mark_node;
14886 TREE_TYPE (decl) = error_mark_node;
14887 }
14888
14889 if (type != error_mark_node)
14890 {
14891 if (deprecated_state != UNAVAILABLE_DEPRECATED_SUPPRESS)
14892 {
14893 tree unavailtype = type_is_unavailable (type);
14894 if (unavailtype)
14895 cp_handle_deprecated_or_unavailable (unavailtype);
14896 }
14897 if (deprecated_state != DEPRECATED_SUPPRESS
14898 && deprecated_state != UNAVAILABLE_DEPRECATED_SUPPRESS)
14899 {
14900 tree deptype = type_is_deprecated (type);
14901 if (deptype)
14902 cp_handle_deprecated_or_unavailable (deptype);
14903 }
14904
14905 /* [dcl.fct] "A parameter with volatile-qualified type is
14906 deprecated." */
14907 if (CP_TYPE_VOLATILE_P (type))
14908 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wvolatile,
14909 "%<volatile%>-qualified parameter is "
14910 "deprecated");
14911
14912 /* Top-level qualifiers on the parameters are
14913 ignored for function types. */
14914 type = cp_build_qualified_type (type, 0);
14915 if (TREE_CODE (type) == METHOD_TYPE)
14916 {
14917 error ("parameter %qD invalidly declared method type", decl);
14918 type = build_pointer_type (type);
14919 TREE_TYPE (decl) = type;
14920 }
14921 else if (cxx_dialect < cxx17 && INDIRECT_TYPE_P (type))
14922 {
14923 /* Before C++17 DR 393:
14924 [dcl.fct]/6, parameter types cannot contain pointers
14925 (references) to arrays of unknown bound. */
14926 tree t = TREE_TYPE (type);
14927 int ptr = TYPE_PTR_P (type);
14928
14929 while (1)
14930 {
14931 if (TYPE_PTR_P (t))
14932 ptr = 1;
14933 else if (TREE_CODE (t) != ARRAY_TYPE)
14934 break;
14935 else if (!TYPE_DOMAIN (t))
14936 break;
14937 t = TREE_TYPE (t);
14938 }
14939 if (TREE_CODE (t) == ARRAY_TYPE)
14940 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
14941 ptr
14942 ? G_("parameter %qD includes pointer to array of "
14943 "unknown bound %qT")
14944 : G_("parameter %qD includes reference to array of "
14945 "unknown bound %qT"),
14946 decl, t);
14947 }
14948
14949 if (init && !processing_template_decl)
14950 init = check_default_argument (decl, init, tf_warning_or_error);
14951 }
14952
14953 DECL_CHAIN (decl) = decls;
14954 decls = decl;
14955 result = tree_cons (init, type, result);
14956 }
14957 decls = nreverse (decls);
14958 result = nreverse (result);
14959 if (parm)
14960 result = chainon (result, void_list_node);
14961 *parms = decls;
14962 if (any_error)
14963 result = NULL_TREE;
14964
14965 if (any_error)
14966 /* We had parm errors, recover by giving the function (...) type. */
14967 result = NULL_TREE;
14968
14969 return result;
14970 }
14971
14972
14973 /* D is a constructor or overloaded `operator='.
14974
14975 Let T be the class in which D is declared. Then, this function
14976 returns:
14977
14978 -1 if D's is an ill-formed constructor or copy assignment operator
14979 whose first parameter is of type `T'.
14980 0 if D is not a copy constructor or copy assignment
14981 operator.
14982 1 if D is a copy constructor or copy assignment operator whose
14983 first parameter is a reference to non-const qualified T.
14984 2 if D is a copy constructor or copy assignment operator whose
14985 first parameter is a reference to const qualified T.
14986
14987 This function can be used as a predicate. Positive values indicate
14988 a copy constructor and nonzero values indicate a copy assignment
14989 operator. */
14990
14991 int
copy_fn_p(const_tree d)14992 copy_fn_p (const_tree d)
14993 {
14994 tree args;
14995 tree arg_type;
14996 int result = 1;
14997
14998 gcc_assert (DECL_FUNCTION_MEMBER_P (d));
14999
15000 if (TREE_CODE (d) == TEMPLATE_DECL
15001 || (DECL_TEMPLATE_INFO (d)
15002 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
15003 /* Instantiations of template member functions are never copy
15004 functions. Note that member functions of templated classes are
15005 represented as template functions internally, and we must
15006 accept those as copy functions. */
15007 return 0;
15008
15009 if (!DECL_CONSTRUCTOR_P (d)
15010 && DECL_NAME (d) != assign_op_identifier)
15011 return 0;
15012
15013 args = FUNCTION_FIRST_USER_PARMTYPE (d);
15014 if (!args)
15015 return 0;
15016
15017 arg_type = TREE_VALUE (args);
15018 if (arg_type == error_mark_node)
15019 return 0;
15020
15021 if (TYPE_MAIN_VARIANT (arg_type) == DECL_CONTEXT (d))
15022 {
15023 /* Pass by value copy assignment operator. */
15024 result = -1;
15025 }
15026 else if (TYPE_REF_P (arg_type)
15027 && !TYPE_REF_IS_RVALUE (arg_type)
15028 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)) == DECL_CONTEXT (d))
15029 {
15030 if (CP_TYPE_CONST_P (TREE_TYPE (arg_type)))
15031 result = 2;
15032 }
15033 else
15034 return 0;
15035
15036 args = TREE_CHAIN (args);
15037
15038 if (args && args != void_list_node && !TREE_PURPOSE (args))
15039 /* There are more non-optional args. */
15040 return 0;
15041
15042 return result;
15043 }
15044
15045 /* D is a constructor or overloaded `operator='.
15046
15047 Let T be the class in which D is declared. Then, this function
15048 returns true when D is a move constructor or move assignment
15049 operator, false otherwise. */
15050
15051 bool
move_fn_p(const_tree d)15052 move_fn_p (const_tree d)
15053 {
15054 if (cxx_dialect == cxx98)
15055 /* There are no move constructors if we are in C++98 mode. */
15056 return false;
15057
15058 if (TREE_CODE (d) == TEMPLATE_DECL
15059 || (DECL_TEMPLATE_INFO (d)
15060 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
15061 /* Instantiations of template member functions are never move
15062 functions. Note that member functions of templated classes are
15063 represented as template functions internally, and we must
15064 accept those as move functions. */
15065 return 0;
15066
15067 return move_signature_fn_p (d);
15068 }
15069
15070 /* D is a constructor or overloaded `operator='.
15071
15072 Then, this function returns true when D has the same signature as a move
15073 constructor or move assignment operator (because either it is such a
15074 ctor/op= or it is a template specialization with the same signature),
15075 false otherwise. */
15076
15077 bool
move_signature_fn_p(const_tree d)15078 move_signature_fn_p (const_tree d)
15079 {
15080 tree args;
15081 tree arg_type;
15082 bool result = false;
15083
15084 if (!DECL_CONSTRUCTOR_P (d)
15085 && DECL_NAME (d) != assign_op_identifier)
15086 return 0;
15087
15088 args = FUNCTION_FIRST_USER_PARMTYPE (d);
15089 if (!args)
15090 return 0;
15091
15092 arg_type = TREE_VALUE (args);
15093 if (arg_type == error_mark_node)
15094 return 0;
15095
15096 if (TYPE_REF_P (arg_type)
15097 && TYPE_REF_IS_RVALUE (arg_type)
15098 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)),
15099 DECL_CONTEXT (d)))
15100 result = true;
15101
15102 args = TREE_CHAIN (args);
15103
15104 if (args && args != void_list_node && !TREE_PURPOSE (args))
15105 /* There are more non-optional args. */
15106 return false;
15107
15108 return result;
15109 }
15110
15111 /* Remember any special properties of member function DECL. */
15112
15113 void
grok_special_member_properties(tree decl)15114 grok_special_member_properties (tree decl)
15115 {
15116 tree class_type;
15117
15118 if (TREE_CODE (decl) == USING_DECL
15119 || !DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
15120 return;
15121
15122 class_type = DECL_CONTEXT (decl);
15123 if (IDENTIFIER_CTOR_P (DECL_NAME (decl)))
15124 {
15125 int ctor = copy_fn_p (decl);
15126
15127 if (!DECL_ARTIFICIAL (decl))
15128 TYPE_HAS_USER_CONSTRUCTOR (class_type) = 1;
15129
15130 if (ctor > 0)
15131 {
15132 /* [class.copy]
15133
15134 A non-template constructor for class X is a copy
15135 constructor if its first parameter is of type X&, const
15136 X&, volatile X& or const volatile X&, and either there
15137 are no other parameters or else all other parameters have
15138 default arguments. */
15139 TYPE_HAS_COPY_CTOR (class_type) = 1;
15140 if (ctor > 1)
15141 TYPE_HAS_CONST_COPY_CTOR (class_type) = 1;
15142 }
15143
15144 if (sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (decl)))
15145 TYPE_HAS_DEFAULT_CONSTRUCTOR (class_type) = 1;
15146
15147 if (is_list_ctor (decl))
15148 TYPE_HAS_LIST_CTOR (class_type) = 1;
15149
15150 if (maybe_constexpr_fn (decl)
15151 && !ctor && !move_fn_p (decl))
15152 TYPE_HAS_CONSTEXPR_CTOR (class_type) = 1;
15153 }
15154 else if (DECL_NAME (decl) == assign_op_identifier)
15155 {
15156 /* [class.copy]
15157
15158 A non-template assignment operator for class X is a copy
15159 assignment operator if its parameter is of type X, X&, const
15160 X&, volatile X& or const volatile X&. */
15161
15162 int assop = copy_fn_p (decl);
15163
15164 if (assop)
15165 {
15166 TYPE_HAS_COPY_ASSIGN (class_type) = 1;
15167 if (assop != 1)
15168 TYPE_HAS_CONST_COPY_ASSIGN (class_type) = 1;
15169 }
15170 }
15171 else if (IDENTIFIER_CONV_OP_P (DECL_NAME (decl)))
15172 TYPE_HAS_CONVERSION (class_type) = true;
15173
15174 /* Destructors are handled in check_methods. */
15175 }
15176
15177 /* Check a constructor DECL has the correct form. Complains
15178 if the class has a constructor of the form X(X). */
15179
15180 bool
grok_ctor_properties(const_tree ctype,const_tree decl)15181 grok_ctor_properties (const_tree ctype, const_tree decl)
15182 {
15183 int ctor_parm = copy_fn_p (decl);
15184
15185 if (ctor_parm < 0)
15186 {
15187 /* [class.copy]
15188
15189 A declaration of a constructor for a class X is ill-formed if
15190 its first parameter is of type (optionally cv-qualified) X
15191 and either there are no other parameters or else all other
15192 parameters have default arguments.
15193
15194 We *don't* complain about member template instantiations that
15195 have this form, though; they can occur as we try to decide
15196 what constructor to use during overload resolution. Since
15197 overload resolution will never prefer such a constructor to
15198 the non-template copy constructor (which is either explicitly
15199 or implicitly defined), there's no need to worry about their
15200 existence. Theoretically, they should never even be
15201 instantiated, but that's hard to forestall. */
15202 error_at (DECL_SOURCE_LOCATION (decl),
15203 "invalid constructor; you probably meant %<%T (const %T&)%>",
15204 ctype, ctype);
15205 return false;
15206 }
15207
15208 return true;
15209 }
15210
15211 /* DECL is a declaration for an overloaded or conversion operator. If
15212 COMPLAIN is true, errors are issued for invalid declarations. */
15213
15214 bool
grok_op_properties(tree decl,bool complain)15215 grok_op_properties (tree decl, bool complain)
15216 {
15217 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
15218 bool methodp = TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE;
15219 tree name = DECL_NAME (decl);
15220 location_t loc = DECL_SOURCE_LOCATION (decl);
15221
15222 tree class_type = DECL_CONTEXT (decl);
15223 if (class_type && !CLASS_TYPE_P (class_type))
15224 class_type = NULL_TREE;
15225
15226 tree_code operator_code;
15227 unsigned op_flags;
15228 if (IDENTIFIER_CONV_OP_P (name))
15229 {
15230 /* Conversion operators are TYPE_EXPR for the purposes of this
15231 function. */
15232 operator_code = TYPE_EXPR;
15233 op_flags = OVL_OP_FLAG_UNARY;
15234 }
15235 else
15236 {
15237 const ovl_op_info_t *ovl_op = IDENTIFIER_OVL_OP_INFO (name);
15238
15239 operator_code = ovl_op->tree_code;
15240 op_flags = ovl_op->flags;
15241 gcc_checking_assert (operator_code != ERROR_MARK);
15242 DECL_OVERLOADED_OPERATOR_CODE_RAW (decl) = ovl_op->ovl_op_code;
15243 }
15244
15245 if (op_flags & OVL_OP_FLAG_ALLOC)
15246 {
15247 /* operator new and operator delete are quite special. */
15248 if (class_type)
15249 switch (op_flags)
15250 {
15251 case OVL_OP_FLAG_ALLOC:
15252 TYPE_HAS_NEW_OPERATOR (class_type) = 1;
15253 break;
15254
15255 case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_DELETE:
15256 TYPE_GETS_DELETE (class_type) |= 1;
15257 break;
15258
15259 case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_VEC:
15260 TYPE_HAS_ARRAY_NEW_OPERATOR (class_type) = 1;
15261 break;
15262
15263 case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_DELETE | OVL_OP_FLAG_VEC:
15264 TYPE_GETS_DELETE (class_type) |= 2;
15265 break;
15266
15267 default:
15268 gcc_unreachable ();
15269 }
15270
15271 /* [basic.std.dynamic.allocation]/1:
15272
15273 A program is ill-formed if an allocation function is declared
15274 in a namespace scope other than global scope or declared
15275 static in global scope.
15276
15277 The same also holds true for deallocation functions. */
15278 if (DECL_NAMESPACE_SCOPE_P (decl))
15279 {
15280 if (CP_DECL_CONTEXT (decl) != global_namespace)
15281 {
15282 error_at (loc, "%qD may not be declared within a namespace",
15283 decl);
15284 return false;
15285 }
15286
15287 if (!TREE_PUBLIC (decl))
15288 {
15289 error_at (loc, "%qD may not be declared as static", decl);
15290 return false;
15291 }
15292 }
15293
15294 if (op_flags & OVL_OP_FLAG_DELETE)
15295 {
15296 DECL_SET_IS_OPERATOR_DELETE (decl, true);
15297 coerce_delete_type (decl, loc);
15298 }
15299 else
15300 {
15301 DECL_SET_IS_OPERATOR_NEW (decl, true);
15302 TREE_TYPE (decl) = coerce_new_type (TREE_TYPE (decl), loc);
15303 }
15304
15305 return true;
15306 }
15307
15308 /* An operator function must either be a non-static member function
15309 or have at least one parameter of a class, a reference to a class,
15310 an enumeration, or a reference to an enumeration. 13.4.0.6 */
15311 if (! methodp || DECL_STATIC_FUNCTION_P (decl))
15312 {
15313 if (operator_code == TYPE_EXPR
15314 || operator_code == CALL_EXPR
15315 || operator_code == COMPONENT_REF
15316 || operator_code == ARRAY_REF
15317 || operator_code == NOP_EXPR)
15318 {
15319 error_at (loc, "%qD must be a non-static member function", decl);
15320 return false;
15321 }
15322
15323 if (DECL_STATIC_FUNCTION_P (decl))
15324 {
15325 error_at (loc, "%qD must be either a non-static member "
15326 "function or a non-member function", decl);
15327 return false;
15328 }
15329
15330 for (tree arg = argtypes; ; arg = TREE_CHAIN (arg))
15331 {
15332 if (!arg || arg == void_list_node)
15333 {
15334 if (complain)
15335 error_at(loc, "%qD must have an argument of class or "
15336 "enumerated type", decl);
15337 return false;
15338 }
15339
15340 tree type = non_reference (TREE_VALUE (arg));
15341 if (type == error_mark_node)
15342 return false;
15343
15344 /* MAYBE_CLASS_TYPE_P, rather than CLASS_TYPE_P, is used
15345 because these checks are performed even on template
15346 functions. */
15347 if (MAYBE_CLASS_TYPE_P (type)
15348 || TREE_CODE (type) == ENUMERAL_TYPE)
15349 break;
15350 }
15351 }
15352
15353 if (operator_code == CALL_EXPR)
15354 /* There are no further restrictions on the arguments to an overloaded
15355 "operator ()". */
15356 return true;
15357
15358 if (operator_code == COND_EXPR)
15359 {
15360 /* 13.4.0.3 */
15361 error_at (loc, "ISO C++ prohibits overloading %<operator ?:%>");
15362 return false;
15363 }
15364
15365 /* Count the number of arguments and check for ellipsis. */
15366 int arity = 0;
15367 for (tree arg = argtypes; arg != void_list_node; arg = TREE_CHAIN (arg))
15368 {
15369 if (!arg)
15370 {
15371 /* Variadic. */
15372 if (operator_code == ARRAY_REF && cxx_dialect >= cxx23)
15373 break;
15374
15375 error_at (loc, "%qD must not have variable number of arguments",
15376 decl);
15377 return false;
15378 }
15379 ++arity;
15380 }
15381
15382 /* Verify correct number of arguments. */
15383 switch (op_flags)
15384 {
15385 case OVL_OP_FLAG_AMBIARY:
15386 if (arity == 1)
15387 {
15388 /* We have a unary instance of an ambi-ary op. Remap to the
15389 unary one. */
15390 unsigned alt = ovl_op_alternate[ovl_op_mapping [operator_code]];
15391 const ovl_op_info_t *ovl_op = &ovl_op_info[false][alt];
15392 gcc_checking_assert (ovl_op->flags == OVL_OP_FLAG_UNARY);
15393 operator_code = ovl_op->tree_code;
15394 DECL_OVERLOADED_OPERATOR_CODE_RAW (decl) = ovl_op->ovl_op_code;
15395 }
15396 else if (arity != 2)
15397 {
15398 /* This was an ambiguous operator but is invalid. */
15399 error_at (loc,
15400 methodp
15401 ? G_("%qD must have either zero or one argument")
15402 : G_("%qD must have either one or two arguments"), decl);
15403 return false;
15404 }
15405 else if ((operator_code == POSTINCREMENT_EXPR
15406 || operator_code == POSTDECREMENT_EXPR)
15407 && ! processing_template_decl
15408 /* x++ and x--'s second argument must be an int. */
15409 && ! same_type_p (TREE_VALUE (TREE_CHAIN (argtypes)),
15410 integer_type_node))
15411 {
15412 error_at (loc,
15413 methodp
15414 ? G_("postfix %qD must have %<int%> as its argument")
15415 : G_("postfix %qD must have %<int%> as its second argument"),
15416 decl);
15417 return false;
15418 }
15419 break;
15420
15421 case OVL_OP_FLAG_UNARY:
15422 if (arity != 1)
15423 {
15424 error_at (loc,
15425 methodp
15426 ? G_("%qD must have no arguments")
15427 : G_("%qD must have exactly one argument"), decl);
15428 return false;
15429 }
15430 break;
15431
15432 case OVL_OP_FLAG_BINARY:
15433 if (arity != 2)
15434 {
15435 if (operator_code == ARRAY_REF && cxx_dialect >= cxx23)
15436 break;
15437 error_at (loc,
15438 methodp
15439 ? G_("%qD must have exactly one argument")
15440 : G_("%qD must have exactly two arguments"), decl);
15441 return false;
15442 }
15443 break;
15444
15445 default:
15446 gcc_unreachable ();
15447 }
15448
15449 /* There can be no default arguments. */
15450 for (tree arg = argtypes; arg && arg != void_list_node;
15451 arg = TREE_CHAIN (arg))
15452 if (TREE_PURPOSE (arg))
15453 {
15454 TREE_PURPOSE (arg) = NULL_TREE;
15455 error_at (loc, "%qD cannot have default arguments", decl);
15456 return false;
15457 }
15458
15459 /* At this point the declaration is well-formed. It may not be
15460 sensible though. */
15461
15462 /* Check member function warnings only on the in-class declaration.
15463 There's no point warning on an out-of-class definition. */
15464 if (class_type && class_type != current_class_type)
15465 return true;
15466
15467 /* Warn about conversion operators that will never be used. */
15468 if (IDENTIFIER_CONV_OP_P (name)
15469 && ! DECL_TEMPLATE_INFO (decl)
15470 && warn_class_conversion)
15471 {
15472 tree t = TREE_TYPE (name);
15473 int ref = TYPE_REF_P (t);
15474
15475 if (ref)
15476 t = TYPE_MAIN_VARIANT (TREE_TYPE (t));
15477
15478 if (VOID_TYPE_P (t))
15479 warning_at (loc, OPT_Wclass_conversion, "converting %qT to %<void%> "
15480 "will never use a type conversion operator", class_type);
15481 else if (class_type)
15482 {
15483 if (same_type_ignoring_top_level_qualifiers_p (t, class_type))
15484 warning_at (loc, OPT_Wclass_conversion,
15485 ref
15486 ? G_("converting %qT to a reference to the same type "
15487 "will never use a type conversion operator")
15488 : G_("converting %qT to the same type "
15489 "will never use a type conversion operator"),
15490 class_type);
15491 /* Don't force t to be complete here. */
15492 else if (MAYBE_CLASS_TYPE_P (t)
15493 && COMPLETE_TYPE_P (t)
15494 && DERIVED_FROM_P (t, class_type))
15495 warning_at (loc, OPT_Wclass_conversion,
15496 ref
15497 ? G_("converting %qT to a reference to a base class "
15498 "%qT will never use a type conversion operator")
15499 : G_("converting %qT to a base class %qT "
15500 "will never use a type conversion operator"),
15501 class_type, t);
15502 }
15503 }
15504
15505 if (!warn_ecpp)
15506 return true;
15507
15508 /* Effective C++ rules below. */
15509
15510 /* More Effective C++ rule 7. */
15511 if (operator_code == TRUTH_ANDIF_EXPR
15512 || operator_code == TRUTH_ORIF_EXPR
15513 || operator_code == COMPOUND_EXPR)
15514 warning_at (loc, OPT_Weffc__,
15515 "user-defined %qD always evaluates both arguments", decl);
15516
15517 /* More Effective C++ rule 6. */
15518 if (operator_code == POSTINCREMENT_EXPR
15519 || operator_code == POSTDECREMENT_EXPR
15520 || operator_code == PREINCREMENT_EXPR
15521 || operator_code == PREDECREMENT_EXPR)
15522 {
15523 tree arg = TREE_VALUE (argtypes);
15524 tree ret = TREE_TYPE (TREE_TYPE (decl));
15525 if (methodp || TYPE_REF_P (arg))
15526 arg = TREE_TYPE (arg);
15527 arg = TYPE_MAIN_VARIANT (arg);
15528
15529 if (operator_code == PREINCREMENT_EXPR
15530 || operator_code == PREDECREMENT_EXPR)
15531 {
15532 if (!TYPE_REF_P (ret)
15533 || !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (ret)), arg))
15534 warning_at (loc, OPT_Weffc__, "prefix %qD should return %qT", decl,
15535 build_reference_type (arg));
15536 }
15537 else
15538 {
15539 if (!same_type_p (TYPE_MAIN_VARIANT (ret), arg))
15540 warning_at (loc, OPT_Weffc__,
15541 "postfix %qD should return %qT", decl, arg);
15542 }
15543 }
15544
15545 /* Effective C++ rule 23. */
15546 if (!DECL_ASSIGNMENT_OPERATOR_P (decl)
15547 && (operator_code == PLUS_EXPR
15548 || operator_code == MINUS_EXPR
15549 || operator_code == TRUNC_DIV_EXPR
15550 || operator_code == MULT_EXPR
15551 || operator_code == TRUNC_MOD_EXPR)
15552 && TYPE_REF_P (TREE_TYPE (TREE_TYPE (decl))))
15553 warning_at (loc, OPT_Weffc__, "%qD should return by value", decl);
15554
15555 return true;
15556 }
15557
15558 /* Return a string giving the keyword associate with CODE. */
15559
15560 static const char *
tag_name(enum tag_types code)15561 tag_name (enum tag_types code)
15562 {
15563 switch (code)
15564 {
15565 case record_type:
15566 return "struct";
15567 case class_type:
15568 return "class";
15569 case union_type:
15570 return "union";
15571 case enum_type:
15572 return "enum";
15573 case typename_type:
15574 return "typename";
15575 default:
15576 gcc_unreachable ();
15577 }
15578 }
15579
15580 /* Name lookup in an elaborated-type-specifier (after the keyword
15581 indicated by TAG_CODE) has found the TYPE_DECL DECL. If the
15582 elaborated-type-specifier is invalid, issue a diagnostic and return
15583 error_mark_node; otherwise, return the *_TYPE to which it referred.
15584 If ALLOW_TEMPLATE_P is true, TYPE may be a class template. */
15585
15586 tree
check_elaborated_type_specifier(enum tag_types tag_code,tree decl,bool allow_template_p)15587 check_elaborated_type_specifier (enum tag_types tag_code,
15588 tree decl,
15589 bool allow_template_p)
15590 {
15591 tree type;
15592
15593 /* In the case of:
15594
15595 struct S { struct S *p; };
15596
15597 name lookup will find the TYPE_DECL for the implicit "S::S"
15598 typedef. Adjust for that here. */
15599 if (DECL_SELF_REFERENCE_P (decl))
15600 decl = TYPE_NAME (TREE_TYPE (decl));
15601
15602 type = TREE_TYPE (decl);
15603
15604 /* Check TEMPLATE_TYPE_PARM first because DECL_IMPLICIT_TYPEDEF_P
15605 is false for this case as well. */
15606 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
15607 {
15608 error ("using template type parameter %qT after %qs",
15609 type, tag_name (tag_code));
15610 return error_mark_node;
15611 }
15612 /* Accept template template parameters. */
15613 else if (allow_template_p
15614 && (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM
15615 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM))
15616 ;
15617 /* [dcl.type.elab]
15618
15619 If the identifier resolves to a typedef-name or the
15620 simple-template-id resolves to an alias template
15621 specialization, the elaborated-type-specifier is ill-formed.
15622
15623 In other words, the only legitimate declaration to use in the
15624 elaborated type specifier is the implicit typedef created when
15625 the type is declared. */
15626 else if (!DECL_IMPLICIT_TYPEDEF_P (decl)
15627 && !DECL_SELF_REFERENCE_P (decl)
15628 && tag_code != typename_type)
15629 {
15630 if (alias_template_specialization_p (type, nt_opaque))
15631 error ("using alias template specialization %qT after %qs",
15632 type, tag_name (tag_code));
15633 else
15634 error ("using typedef-name %qD after %qs", decl, tag_name (tag_code));
15635 inform (DECL_SOURCE_LOCATION (decl),
15636 "%qD has a previous declaration here", decl);
15637 return error_mark_node;
15638 }
15639 else if (TREE_CODE (type) != RECORD_TYPE
15640 && TREE_CODE (type) != UNION_TYPE
15641 && tag_code != enum_type
15642 && tag_code != typename_type)
15643 {
15644 error ("%qT referred to as %qs", type, tag_name (tag_code));
15645 inform (location_of (type), "%qT has a previous declaration here", type);
15646 return error_mark_node;
15647 }
15648 else if (TREE_CODE (type) != ENUMERAL_TYPE
15649 && tag_code == enum_type)
15650 {
15651 error ("%qT referred to as enum", type);
15652 inform (location_of (type), "%qT has a previous declaration here", type);
15653 return error_mark_node;
15654 }
15655 else if (!allow_template_p
15656 && TREE_CODE (type) == RECORD_TYPE
15657 && CLASSTYPE_IS_TEMPLATE (type))
15658 {
15659 /* If a class template appears as elaborated type specifier
15660 without a template header such as:
15661
15662 template <class T> class C {};
15663 void f(class C); // No template header here
15664
15665 then the required template argument is missing. */
15666 error ("template argument required for %<%s %T%>",
15667 tag_name (tag_code),
15668 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)));
15669 return error_mark_node;
15670 }
15671
15672 return type;
15673 }
15674
15675 /* Lookup NAME of an elaborated type specifier according to SCOPE and
15676 issue diagnostics if necessary. Return *_TYPE node upon success,
15677 NULL_TREE when the NAME is not found, and ERROR_MARK_NODE for type
15678 error. */
15679
15680 static tree
lookup_and_check_tag(enum tag_types tag_code,tree name,TAG_how how,bool template_header_p)15681 lookup_and_check_tag (enum tag_types tag_code, tree name,
15682 TAG_how how, bool template_header_p)
15683 {
15684 tree decl;
15685 if (how == TAG_how::GLOBAL)
15686 {
15687 /* First try ordinary name lookup, ignoring hidden class name
15688 injected via friend declaration. */
15689 decl = lookup_name (name, LOOK_want::TYPE);
15690 decl = strip_using_decl (decl);
15691 /* If that fails, the name will be placed in the smallest
15692 non-class, non-function-prototype scope according to 3.3.1/5.
15693 We may already have a hidden name declared as friend in this
15694 scope. So lookup again but not ignoring hidden names.
15695 If we find one, that name will be made visible rather than
15696 creating a new tag. */
15697 if (!decl)
15698 decl = lookup_elaborated_type (name, TAG_how::INNERMOST_NON_CLASS);
15699 }
15700 else
15701 decl = lookup_elaborated_type (name, how);
15702
15703 if (!decl)
15704 /* We found nothing. */
15705 return NULL_TREE;
15706
15707 if (TREE_CODE (decl) == TREE_LIST)
15708 {
15709 error ("reference to %qD is ambiguous", name);
15710 print_candidates (decl);
15711 return error_mark_node;
15712 }
15713
15714 if (DECL_CLASS_TEMPLATE_P (decl)
15715 && !template_header_p
15716 && how == TAG_how::CURRENT_ONLY)
15717 {
15718 error ("class template %qD redeclared as non-template", name);
15719 inform (location_of (decl), "previous declaration here");
15720 CLASSTYPE_ERRONEOUS (TREE_TYPE (decl)) = true;
15721 return error_mark_node;
15722 }
15723
15724 if (DECL_CLASS_TEMPLATE_P (decl)
15725 /* If scope is TAG_how::CURRENT_ONLY we're defining a class,
15726 so ignore a template template parameter. */
15727 || (how != TAG_how::CURRENT_ONLY && DECL_TEMPLATE_TEMPLATE_PARM_P (decl)))
15728 decl = DECL_TEMPLATE_RESULT (decl);
15729
15730 if (TREE_CODE (decl) != TYPE_DECL)
15731 /* Found not-a-type. */
15732 return NULL_TREE;
15733
15734 /* Look for invalid nested type:
15735 class C {
15736 class C {};
15737 }; */
15738 if (how == TAG_how::CURRENT_ONLY && DECL_SELF_REFERENCE_P (decl))
15739 {
15740 error ("%qD has the same name as the class in which it is "
15741 "declared", decl);
15742 return error_mark_node;
15743 }
15744
15745 /* Two cases we need to consider when deciding if a class
15746 template is allowed as an elaborated type specifier:
15747 1. It is a self reference to its own class.
15748 2. It comes with a template header.
15749
15750 For example:
15751
15752 template <class T> class C {
15753 class C *c1; // DECL_SELF_REFERENCE_P is true
15754 class D;
15755 };
15756 template <class U> class C; // template_header_p is true
15757 template <class T> class C<T>::D {
15758 class C *c2; // DECL_SELF_REFERENCE_P is true
15759 }; */
15760
15761 tree t = check_elaborated_type_specifier (tag_code, decl,
15762 template_header_p
15763 | DECL_SELF_REFERENCE_P (decl));
15764 if (template_header_p && t && CLASS_TYPE_P (t)
15765 && (!CLASSTYPE_TEMPLATE_INFO (t)
15766 || (!PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)))))
15767 {
15768 error ("%qT is not a template", t);
15769 inform (location_of (t), "previous declaration here");
15770 if (TYPE_CLASS_SCOPE_P (t)
15771 && CLASSTYPE_TEMPLATE_INFO (TYPE_CONTEXT (t)))
15772 inform (input_location,
15773 "perhaps you want to explicitly add %<%T::%>",
15774 TYPE_CONTEXT (t));
15775 return error_mark_node;
15776 }
15777
15778 return t;
15779 }
15780
15781 /* Get the struct, enum or union (TAG_CODE says which) with tag NAME.
15782 Define the tag as a forward-reference if it is not defined.
15783
15784 If a declaration is given, process it here, and report an error if
15785 multiple declarations are not identical.
15786
15787 SCOPE is TS_CURRENT when this is also a definition. Only look in
15788 the current frame for the name (since C++ allows new names in any
15789 scope.) It is TS_WITHIN_ENCLOSING_NON_CLASS if this is a friend
15790 declaration. Only look beginning from the current scope outward up
15791 till the nearest non-class scope. Otherwise it is TS_GLOBAL.
15792
15793 TEMPLATE_HEADER_P is true when this declaration is preceded by
15794 a set of template parameters. */
15795
15796 tree
xref_tag(enum tag_types tag_code,tree name,TAG_how how,bool template_header_p)15797 xref_tag (enum tag_types tag_code, tree name,
15798 TAG_how how, bool template_header_p)
15799 {
15800 enum tree_code code;
15801 tree context = NULL_TREE;
15802
15803 auto_cond_timevar tv (TV_NAME_LOOKUP);
15804
15805 gcc_assert (identifier_p (name));
15806
15807 switch (tag_code)
15808 {
15809 case record_type:
15810 case class_type:
15811 code = RECORD_TYPE;
15812 break;
15813 case union_type:
15814 code = UNION_TYPE;
15815 break;
15816 case enum_type:
15817 code = ENUMERAL_TYPE;
15818 break;
15819 default:
15820 gcc_unreachable ();
15821 }
15822
15823 /* In case of anonymous name, xref_tag is only called to
15824 make type node and push name. Name lookup is not required. */
15825 tree t = NULL_TREE;
15826 if (!IDENTIFIER_ANON_P (name))
15827 t = lookup_and_check_tag (tag_code, name, how, template_header_p);
15828
15829 if (t == error_mark_node)
15830 return error_mark_node;
15831
15832 if (how != TAG_how::CURRENT_ONLY && t && current_class_type
15833 && template_class_depth (current_class_type)
15834 && template_header_p)
15835 {
15836 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
15837 return t;
15838
15839 /* Since HOW is not TAG_how::CURRENT_ONLY, we are not looking at
15840 a definition of this tag. Since, in addition, we are
15841 currently processing a (member) template declaration of a
15842 template class, we must be very careful; consider:
15843
15844 template <class X> struct S1
15845
15846 template <class U> struct S2
15847 {
15848 template <class V> friend struct S1;
15849 };
15850
15851 Here, the S2::S1 declaration should not be confused with the
15852 outer declaration. In particular, the inner version should
15853 have a template parameter of level 2, not level 1.
15854
15855 On the other hand, when presented with:
15856
15857 template <class T> struct S1
15858 {
15859 template <class U> struct S2 {};
15860 template <class U> friend struct S2;
15861 };
15862
15863 the friend must find S1::S2 eventually. We accomplish this
15864 by making sure that the new type we create to represent this
15865 declaration has the right TYPE_CONTEXT. */
15866 context = TYPE_CONTEXT (t);
15867 t = NULL_TREE;
15868 }
15869
15870 if (! t)
15871 {
15872 /* If no such tag is yet defined, create a forward-reference node
15873 and record it as the "definition".
15874 When a real declaration of this type is found,
15875 the forward-reference will be altered into a real type. */
15876 if (code == ENUMERAL_TYPE)
15877 {
15878 error ("use of enum %q#D without previous declaration", name);
15879 return error_mark_node;
15880 }
15881
15882 t = make_class_type (code);
15883 TYPE_CONTEXT (t) = context;
15884 if (IDENTIFIER_LAMBDA_P (name))
15885 /* Mark it as a lambda type right now. Our caller will
15886 correct the value. */
15887 CLASSTYPE_LAMBDA_EXPR (t) = error_mark_node;
15888 t = pushtag (name, t, how);
15889 }
15890 else
15891 {
15892 if (template_header_p && MAYBE_CLASS_TYPE_P (t))
15893 {
15894 /* Check that we aren't trying to overload a class with different
15895 constraints. */
15896 tree constr = NULL_TREE;
15897 if (current_template_parms)
15898 {
15899 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
15900 constr = build_constraints (reqs, NULL_TREE);
15901 }
15902 if (!redeclare_class_template (t, current_template_parms, constr))
15903 return error_mark_node;
15904 }
15905 else if (!processing_template_decl
15906 && CLASS_TYPE_P (t)
15907 && CLASSTYPE_IS_TEMPLATE (t))
15908 {
15909 error ("redeclaration of %qT as a non-template", t);
15910 inform (location_of (t), "previous declaration %qD", t);
15911 return error_mark_node;
15912 }
15913
15914 if (modules_p ()
15915 && how == TAG_how::CURRENT_ONLY)
15916 {
15917 tree decl = TYPE_NAME (t);
15918 if (!module_may_redeclare (decl))
15919 {
15920 error ("cannot declare %qD in a different module", decl);
15921 inform (DECL_SOURCE_LOCATION (decl), "declared here");
15922 return error_mark_node;
15923 }
15924
15925 tree maybe_tmpl = decl;
15926 if (CLASS_TYPE_P (t) && CLASSTYPE_IS_TEMPLATE (t))
15927 maybe_tmpl = CLASSTYPE_TI_TEMPLATE (t);
15928
15929 if (DECL_LANG_SPECIFIC (decl)
15930 && DECL_MODULE_IMPORT_P (decl)
15931 && TREE_CODE (CP_DECL_CONTEXT (decl)) == NAMESPACE_DECL)
15932 {
15933 /* Push it into this TU's symbol slot. */
15934 gcc_checking_assert (current_namespace == CP_DECL_CONTEXT (decl));
15935 if (maybe_tmpl != decl)
15936 /* We're in the template parm binding level.
15937 Pushtag has logic to slide under that, but we're
15938 not pushing a *new* type. */
15939 push_nested_namespace (CP_DECL_CONTEXT (decl));
15940
15941 pushdecl (maybe_tmpl);
15942 if (maybe_tmpl != decl)
15943 pop_nested_namespace (CP_DECL_CONTEXT (decl));
15944 }
15945
15946 set_instantiating_module (maybe_tmpl);
15947 }
15948 }
15949
15950 return t;
15951 }
15952
15953 /* Create the binfo hierarchy for REF with (possibly NULL) base list
15954 BASE_LIST. For each element on BASE_LIST the TREE_PURPOSE is an
15955 access_* node, and the TREE_VALUE is the type of the base-class.
15956 Non-NULL TREE_TYPE indicates virtual inheritance. */
15957
15958 void
xref_basetypes(tree ref,tree base_list)15959 xref_basetypes (tree ref, tree base_list)
15960 {
15961 tree *basep;
15962 tree binfo, base_binfo;
15963 unsigned max_vbases = 0; /* Maximum direct & indirect virtual bases. */
15964 unsigned max_bases = 0; /* Maximum direct bases. */
15965 unsigned max_dvbases = 0; /* Maximum direct virtual bases. */
15966 int i;
15967 tree default_access;
15968 tree igo_prev; /* Track Inheritance Graph Order. */
15969
15970 if (ref == error_mark_node)
15971 return;
15972
15973 /* The base of a derived class is private by default, all others are
15974 public. */
15975 default_access = (TREE_CODE (ref) == RECORD_TYPE
15976 && CLASSTYPE_DECLARED_CLASS (ref)
15977 ? access_private_node : access_public_node);
15978
15979 /* First, make sure that any templates in base-classes are
15980 instantiated. This ensures that if we call ourselves recursively
15981 we do not get confused about which classes are marked and which
15982 are not. */
15983 basep = &base_list;
15984 while (*basep)
15985 {
15986 tree basetype = TREE_VALUE (*basep);
15987
15988 /* The dependent_type_p call below should really be dependent_scope_p
15989 so that we give a hard error about using an incomplete type as a
15990 base, but we allow it with a pedwarn for backward
15991 compatibility. */
15992 if (processing_template_decl
15993 && CLASS_TYPE_P (basetype) && TYPE_BEING_DEFINED (basetype))
15994 cxx_incomplete_type_diagnostic (NULL_TREE, basetype, DK_PEDWARN);
15995 if (!dependent_type_p (basetype)
15996 && !complete_type_or_else (basetype, NULL))
15997 /* An incomplete type. Remove it from the list. */
15998 *basep = TREE_CHAIN (*basep);
15999 else
16000 {
16001 max_bases++;
16002 if (TREE_TYPE (*basep))
16003 max_dvbases++;
16004 if (CLASS_TYPE_P (basetype))
16005 max_vbases += vec_safe_length (CLASSTYPE_VBASECLASSES (basetype));
16006 basep = &TREE_CHAIN (*basep);
16007 }
16008 }
16009 max_vbases += max_dvbases;
16010
16011 TYPE_MARKED_P (ref) = 1;
16012
16013 /* The binfo slot should be empty, unless this is an (ill-formed)
16014 redefinition. */
16015 gcc_assert (!TYPE_BINFO (ref) || TYPE_SIZE (ref));
16016
16017 gcc_assert (TYPE_MAIN_VARIANT (ref) == ref);
16018
16019 binfo = make_tree_binfo (max_bases);
16020
16021 TYPE_BINFO (ref) = binfo;
16022 BINFO_OFFSET (binfo) = size_zero_node;
16023 BINFO_TYPE (binfo) = ref;
16024
16025 /* Apply base-class info set up to the variants of this type. */
16026 fixup_type_variants (ref);
16027
16028 if (max_bases)
16029 {
16030 vec_alloc (BINFO_BASE_ACCESSES (binfo), max_bases);
16031 /* A C++98 POD cannot have base classes. */
16032 CLASSTYPE_NON_LAYOUT_POD_P (ref) = true;
16033
16034 if (TREE_CODE (ref) == UNION_TYPE)
16035 {
16036 error ("derived union %qT invalid", ref);
16037 return;
16038 }
16039 }
16040
16041 if (max_bases > 1)
16042 warning (OPT_Wmultiple_inheritance,
16043 "%qT defined with multiple direct bases", ref);
16044
16045 if (max_vbases)
16046 {
16047 /* An aggregate can't have virtual base classes. */
16048 CLASSTYPE_NON_AGGREGATE (ref) = true;
16049
16050 vec_alloc (CLASSTYPE_VBASECLASSES (ref), max_vbases);
16051
16052 if (max_dvbases)
16053 warning (OPT_Wvirtual_inheritance,
16054 "%qT defined with direct virtual base", ref);
16055 }
16056
16057 for (igo_prev = binfo; base_list; base_list = TREE_CHAIN (base_list))
16058 {
16059 tree access = TREE_PURPOSE (base_list);
16060 int via_virtual = TREE_TYPE (base_list) != NULL_TREE;
16061 tree basetype = TREE_VALUE (base_list);
16062
16063 if (access == access_default_node)
16064 access = default_access;
16065
16066 /* Before C++17, an aggregate cannot have base classes. In C++17, an
16067 aggregate can't have virtual, private, or protected base classes. */
16068 if (cxx_dialect < cxx17
16069 || access != access_public_node
16070 || via_virtual)
16071 CLASSTYPE_NON_AGGREGATE (ref) = true;
16072
16073 if (PACK_EXPANSION_P (basetype))
16074 basetype = PACK_EXPANSION_PATTERN (basetype);
16075 if (TREE_CODE (basetype) == TYPE_DECL)
16076 basetype = TREE_TYPE (basetype);
16077 if (!MAYBE_CLASS_TYPE_P (basetype) || TREE_CODE (basetype) == UNION_TYPE)
16078 {
16079 error ("base type %qT fails to be a struct or class type",
16080 basetype);
16081 goto dropped_base;
16082 }
16083
16084 base_binfo = NULL_TREE;
16085 if (CLASS_TYPE_P (basetype) && !dependent_scope_p (basetype))
16086 {
16087 base_binfo = TYPE_BINFO (basetype);
16088 /* The original basetype could have been a typedef'd type. */
16089 basetype = BINFO_TYPE (base_binfo);
16090
16091 /* Inherit flags from the base. */
16092 TYPE_HAS_NEW_OPERATOR (ref)
16093 |= TYPE_HAS_NEW_OPERATOR (basetype);
16094 TYPE_HAS_ARRAY_NEW_OPERATOR (ref)
16095 |= TYPE_HAS_ARRAY_NEW_OPERATOR (basetype);
16096 TYPE_GETS_DELETE (ref) |= TYPE_GETS_DELETE (basetype);
16097 TYPE_HAS_CONVERSION (ref) |= TYPE_HAS_CONVERSION (basetype);
16098 CLASSTYPE_DIAMOND_SHAPED_P (ref)
16099 |= CLASSTYPE_DIAMOND_SHAPED_P (basetype);
16100 CLASSTYPE_REPEATED_BASE_P (ref)
16101 |= CLASSTYPE_REPEATED_BASE_P (basetype);
16102 }
16103
16104 /* We must do this test after we've seen through a typedef
16105 type. */
16106 if (TYPE_MARKED_P (basetype))
16107 {
16108 if (basetype == ref)
16109 error ("recursive type %qT undefined", basetype);
16110 else
16111 error ("duplicate base type %qT invalid", basetype);
16112 goto dropped_base;
16113 }
16114
16115 if (PACK_EXPANSION_P (TREE_VALUE (base_list)))
16116 /* Regenerate the pack expansion for the bases. */
16117 basetype = make_pack_expansion (basetype);
16118
16119 TYPE_MARKED_P (basetype) = 1;
16120
16121 base_binfo = copy_binfo (base_binfo, basetype, ref,
16122 &igo_prev, via_virtual);
16123 if (!BINFO_INHERITANCE_CHAIN (base_binfo))
16124 BINFO_INHERITANCE_CHAIN (base_binfo) = binfo;
16125
16126 BINFO_BASE_APPEND (binfo, base_binfo);
16127 BINFO_BASE_ACCESS_APPEND (binfo, access);
16128 continue;
16129
16130 dropped_base:
16131 /* Update max_vbases to reflect the reality that we are dropping
16132 this base: if it reaches zero we want to undo the vec_alloc
16133 above to avoid inconsistencies during error-recovery: eg, in
16134 build_special_member_call, CLASSTYPE_VBASECLASSES non null
16135 and vtt null (c++/27952). */
16136 if (via_virtual)
16137 max_vbases--;
16138 if (CLASS_TYPE_P (basetype))
16139 max_vbases
16140 -= vec_safe_length (CLASSTYPE_VBASECLASSES (basetype));
16141 }
16142
16143 if (CLASSTYPE_VBASECLASSES (ref)
16144 && max_vbases == 0)
16145 vec_free (CLASSTYPE_VBASECLASSES (ref));
16146
16147 if (vec_safe_length (CLASSTYPE_VBASECLASSES (ref)) < max_vbases)
16148 /* If we didn't get max_vbases vbases, we must have shared at
16149 least one of them, and are therefore diamond shaped. */
16150 CLASSTYPE_DIAMOND_SHAPED_P (ref) = 1;
16151
16152 /* Unmark all the types. */
16153 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
16154 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
16155 TYPE_MARKED_P (ref) = 0;
16156
16157 /* Now see if we have a repeated base type. */
16158 if (!CLASSTYPE_REPEATED_BASE_P (ref))
16159 {
16160 for (base_binfo = binfo; base_binfo;
16161 base_binfo = TREE_CHAIN (base_binfo))
16162 {
16163 if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
16164 {
16165 CLASSTYPE_REPEATED_BASE_P (ref) = 1;
16166 break;
16167 }
16168 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 1;
16169 }
16170 for (base_binfo = binfo; base_binfo;
16171 base_binfo = TREE_CHAIN (base_binfo))
16172 if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
16173 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
16174 else
16175 break;
16176 }
16177 }
16178
16179
16180 /* Copies the enum-related properties from type SRC to type DST.
16181 Used with the underlying type of an enum and the enum itself. */
16182 static void
copy_type_enum(tree dst,tree src)16183 copy_type_enum (tree dst, tree src)
16184 {
16185 tree t;
16186 for (t = dst; t; t = TYPE_NEXT_VARIANT (t))
16187 {
16188 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (src);
16189 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (src);
16190 TYPE_SIZE (t) = TYPE_SIZE (src);
16191 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (src);
16192 SET_TYPE_MODE (dst, TYPE_MODE (src));
16193 TYPE_PRECISION (t) = TYPE_PRECISION (src);
16194 unsigned valign = TYPE_ALIGN (src);
16195 if (TYPE_USER_ALIGN (t))
16196 valign = MAX (valign, TYPE_ALIGN (t));
16197 else
16198 TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (src);
16199 SET_TYPE_ALIGN (t, valign);
16200 TYPE_UNSIGNED (t) = TYPE_UNSIGNED (src);
16201 }
16202 }
16203
16204 /* Begin compiling the definition of an enumeration type.
16205 NAME is its name,
16206
16207 if ENUMTYPE is not NULL_TREE then the type has alredy been found.
16208
16209 UNDERLYING_TYPE is the type that will be used as the storage for
16210 the enumeration type. This should be NULL_TREE if no storage type
16211 was specified.
16212
16213 ATTRIBUTES are any attributes specified after the enum-key.
16214
16215 SCOPED_ENUM_P is true if this is a scoped enumeration type.
16216
16217 if IS_NEW is not NULL, gets TRUE iff a new type is created.
16218
16219 Returns the type object, as yet incomplete.
16220 Also records info about it so that build_enumerator
16221 may be used to declare the individual values as they are read. */
16222
16223 tree
start_enum(tree name,tree enumtype,tree underlying_type,tree attributes,bool scoped_enum_p,bool * is_new)16224 start_enum (tree name, tree enumtype, tree underlying_type,
16225 tree attributes, bool scoped_enum_p, bool *is_new)
16226 {
16227 tree prevtype = NULL_TREE;
16228 gcc_assert (identifier_p (name));
16229
16230 if (is_new)
16231 *is_new = false;
16232 /* [C++0x dcl.enum]p5:
16233
16234 If not explicitly specified, the underlying type of a scoped
16235 enumeration type is int. */
16236 if (!underlying_type && scoped_enum_p)
16237 underlying_type = integer_type_node;
16238
16239 if (underlying_type)
16240 underlying_type = cv_unqualified (underlying_type);
16241
16242 /* If this is the real definition for a previous forward reference,
16243 fill in the contents in the same object that used to be the
16244 forward reference. */
16245 if (!enumtype)
16246 enumtype = lookup_and_check_tag (enum_type, name,
16247 /*tag_scope=*/TAG_how::CURRENT_ONLY,
16248 /*template_header_p=*/false);
16249
16250 /* In case of a template_decl, the only check that should be deferred
16251 to instantiation time is the comparison of underlying types. */
16252 if (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE)
16253 {
16254 if (scoped_enum_p != SCOPED_ENUM_P (enumtype))
16255 {
16256 error_at (input_location, "scoped/unscoped mismatch "
16257 "in enum %q#T", enumtype);
16258 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
16259 "previous definition here");
16260 enumtype = error_mark_node;
16261 }
16262 else if (ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) != !! underlying_type)
16263 {
16264 error_at (input_location, "underlying type mismatch "
16265 "in enum %q#T", enumtype);
16266 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
16267 "previous definition here");
16268 enumtype = error_mark_node;
16269 }
16270 else if (underlying_type && ENUM_UNDERLYING_TYPE (enumtype)
16271 && !same_type_p (underlying_type,
16272 ENUM_UNDERLYING_TYPE (enumtype)))
16273 {
16274 error_at (input_location, "different underlying type "
16275 "in enum %q#T", enumtype);
16276 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
16277 "previous definition here");
16278 underlying_type = NULL_TREE;
16279 }
16280
16281 if (modules_p ())
16282 {
16283 if (!module_may_redeclare (TYPE_NAME (enumtype)))
16284 {
16285 error ("cannot define %qD in different module",
16286 TYPE_NAME (enumtype));
16287 inform (DECL_SOURCE_LOCATION (TYPE_NAME (enumtype)),
16288 "declared here");
16289 enumtype = error_mark_node;
16290 }
16291 set_instantiating_module (TYPE_NAME (enumtype));
16292 }
16293 }
16294
16295 if (!enumtype || TREE_CODE (enumtype) != ENUMERAL_TYPE
16296 || processing_template_decl)
16297 {
16298 /* In case of error, make a dummy enum to allow parsing to
16299 continue. */
16300 if (enumtype == error_mark_node)
16301 {
16302 name = make_anon_name ();
16303 enumtype = NULL_TREE;
16304 }
16305
16306 /* enumtype may be an ENUMERAL_TYPE if this is a redefinition
16307 of an opaque enum, or an opaque enum of an already defined
16308 enumeration (C++11).
16309 In any other case, it'll be NULL_TREE. */
16310 if (!enumtype)
16311 {
16312 if (is_new)
16313 *is_new = true;
16314 }
16315 prevtype = enumtype;
16316
16317 /* Do not push the decl more than once. */
16318 if (!enumtype
16319 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
16320 {
16321 enumtype = cxx_make_type (ENUMERAL_TYPE);
16322 enumtype = pushtag (name, enumtype);
16323
16324 /* std::byte aliases anything. */
16325 if (enumtype != error_mark_node
16326 && TYPE_CONTEXT (enumtype) == std_node
16327 && !strcmp ("byte", TYPE_NAME_STRING (enumtype)))
16328 TYPE_ALIAS_SET (enumtype) = 0;
16329 }
16330 else
16331 enumtype = xref_tag (enum_type, name);
16332
16333 if (enumtype == error_mark_node)
16334 return error_mark_node;
16335
16336 /* The enum is considered opaque until the opening '{' of the
16337 enumerator list. */
16338 SET_OPAQUE_ENUM_P (enumtype, true);
16339 ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) = !! underlying_type;
16340 }
16341
16342 SET_SCOPED_ENUM_P (enumtype, scoped_enum_p);
16343
16344 cplus_decl_attributes (&enumtype, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
16345
16346 if (underlying_type)
16347 {
16348 if (ENUM_UNDERLYING_TYPE (enumtype))
16349 /* We already checked that it matches, don't change it to a different
16350 typedef variant. */;
16351 else if (CP_INTEGRAL_TYPE_P (underlying_type))
16352 {
16353 copy_type_enum (enumtype, underlying_type);
16354 ENUM_UNDERLYING_TYPE (enumtype) = underlying_type;
16355 }
16356 else if (dependent_type_p (underlying_type))
16357 ENUM_UNDERLYING_TYPE (enumtype) = underlying_type;
16358 else
16359 error ("underlying type %qT of %qT must be an integral type",
16360 underlying_type, enumtype);
16361 }
16362
16363 /* If into a template class, the returned enum is always the first
16364 declaration (opaque or not) seen. This way all the references to
16365 this type will be to the same declaration. The following ones are used
16366 only to check for definition errors. */
16367 if (prevtype && processing_template_decl)
16368 return prevtype;
16369 else
16370 return enumtype;
16371 }
16372
16373 /* After processing and defining all the values of an enumeration type,
16374 install their decls in the enumeration type.
16375 ENUMTYPE is the type object. */
16376
16377 void
finish_enum_value_list(tree enumtype)16378 finish_enum_value_list (tree enumtype)
16379 {
16380 tree values;
16381 tree underlying_type;
16382 tree decl;
16383 tree value;
16384 tree minnode, maxnode;
16385 tree t;
16386
16387 bool fixed_underlying_type_p
16388 = ENUM_UNDERLYING_TYPE (enumtype) != NULL_TREE;
16389
16390 /* We built up the VALUES in reverse order. */
16391 TYPE_VALUES (enumtype) = nreverse (TYPE_VALUES (enumtype));
16392
16393 /* For an enum defined in a template, just set the type of the values;
16394 all further processing is postponed until the template is
16395 instantiated. We need to set the type so that tsubst of a CONST_DECL
16396 works. */
16397 if (processing_template_decl)
16398 {
16399 for (values = TYPE_VALUES (enumtype);
16400 values;
16401 values = TREE_CHAIN (values))
16402 TREE_TYPE (TREE_VALUE (values)) = enumtype;
16403 return;
16404 }
16405
16406 /* Determine the minimum and maximum values of the enumerators. */
16407 if (TYPE_VALUES (enumtype))
16408 {
16409 minnode = maxnode = NULL_TREE;
16410
16411 for (values = TYPE_VALUES (enumtype);
16412 values;
16413 values = TREE_CHAIN (values))
16414 {
16415 decl = TREE_VALUE (values);
16416
16417 /* [dcl.enum]: Following the closing brace of an enum-specifier,
16418 each enumerator has the type of its enumeration. Prior to the
16419 closing brace, the type of each enumerator is the type of its
16420 initializing value. */
16421 TREE_TYPE (decl) = enumtype;
16422
16423 /* Update the minimum and maximum values, if appropriate. */
16424 value = DECL_INITIAL (decl);
16425 if (TREE_CODE (value) != INTEGER_CST)
16426 value = integer_zero_node;
16427 /* Figure out what the minimum and maximum values of the
16428 enumerators are. */
16429 if (!minnode)
16430 minnode = maxnode = value;
16431 else if (tree_int_cst_lt (maxnode, value))
16432 maxnode = value;
16433 else if (tree_int_cst_lt (value, minnode))
16434 minnode = value;
16435 }
16436 }
16437 else
16438 /* [dcl.enum]
16439
16440 If the enumerator-list is empty, the underlying type is as if
16441 the enumeration had a single enumerator with value 0. */
16442 minnode = maxnode = integer_zero_node;
16443
16444 if (!fixed_underlying_type_p)
16445 {
16446 /* Compute the number of bits require to represent all values of the
16447 enumeration. We must do this before the type of MINNODE and
16448 MAXNODE are transformed, since tree_int_cst_min_precision relies
16449 on the TREE_TYPE of the value it is passed. */
16450 signop sgn = tree_int_cst_sgn (minnode) >= 0 ? UNSIGNED : SIGNED;
16451 int lowprec = tree_int_cst_min_precision (minnode, sgn);
16452 int highprec = tree_int_cst_min_precision (maxnode, sgn);
16453 int precision = MAX (lowprec, highprec);
16454 unsigned int itk;
16455 bool use_short_enum;
16456
16457 /* Determine the underlying type of the enumeration.
16458
16459 [dcl.enum]
16460
16461 The underlying type of an enumeration is an integral type that
16462 can represent all the enumerator values defined in the
16463 enumeration. It is implementation-defined which integral type is
16464 used as the underlying type for an enumeration except that the
16465 underlying type shall not be larger than int unless the value of
16466 an enumerator cannot fit in an int or unsigned int.
16467
16468 We use "int" or an "unsigned int" as the underlying type, even if
16469 a smaller integral type would work, unless the user has
16470 explicitly requested that we use the smallest possible type. The
16471 user can request that for all enumerations with a command line
16472 flag, or for just one enumeration with an attribute. */
16473
16474 use_short_enum = flag_short_enums
16475 || lookup_attribute ("packed", TYPE_ATTRIBUTES (enumtype));
16476
16477 /* If the precision of the type was specified with an attribute and it
16478 was too small, give an error. Otherwise, use it. */
16479 if (TYPE_PRECISION (enumtype))
16480 {
16481 if (precision > TYPE_PRECISION (enumtype))
16482 error ("specified mode too small for enumerated values");
16483 else
16484 {
16485 use_short_enum = true;
16486 precision = TYPE_PRECISION (enumtype);
16487 }
16488 }
16489
16490 for (itk = (use_short_enum ? itk_char : itk_int);
16491 itk != itk_none;
16492 itk++)
16493 {
16494 underlying_type = integer_types[itk];
16495 if (underlying_type != NULL_TREE
16496 && TYPE_PRECISION (underlying_type) >= precision
16497 && TYPE_SIGN (underlying_type) == sgn)
16498 break;
16499 }
16500 if (itk == itk_none)
16501 {
16502 /* DR 377
16503
16504 IF no integral type can represent all the enumerator values, the
16505 enumeration is ill-formed. */
16506 error ("no integral type can represent all of the enumerator values "
16507 "for %qT", enumtype);
16508 precision = TYPE_PRECISION (long_long_integer_type_node);
16509 underlying_type = integer_types[itk_unsigned_long_long];
16510 }
16511
16512 /* [dcl.enum]
16513
16514 The value of sizeof() applied to an enumeration type, an object
16515 of an enumeration type, or an enumerator, is the value of sizeof()
16516 applied to the underlying type. */
16517 copy_type_enum (enumtype, underlying_type);
16518
16519 /* Compute the minimum and maximum values for the type.
16520
16521 [dcl.enum]
16522
16523 For an enumeration where emin is the smallest enumerator and emax
16524 is the largest, the values of the enumeration are the values of the
16525 underlying type in the range bmin to bmax, where bmin and bmax are,
16526 respectively, the smallest and largest values of the smallest bit-
16527 field that can store emin and emax. */
16528
16529 /* The middle-end currently assumes that types with TYPE_PRECISION
16530 narrower than their underlying type are suitably zero or sign
16531 extended to fill their mode. Similarly, it assumes that the front
16532 end assures that a value of a particular type must be within
16533 TYPE_MIN_VALUE and TYPE_MAX_VALUE.
16534
16535 We used to set these fields based on bmin and bmax, but that led
16536 to invalid assumptions like optimizing away bounds checking. So
16537 now we just set the TYPE_PRECISION, TYPE_MIN_VALUE, and
16538 TYPE_MAX_VALUE to the values for the mode above and only restrict
16539 the ENUM_UNDERLYING_TYPE for the benefit of diagnostics. */
16540 ENUM_UNDERLYING_TYPE (enumtype)
16541 = build_distinct_type_copy (underlying_type);
16542 TYPE_PRECISION (ENUM_UNDERLYING_TYPE (enumtype)) = precision;
16543 set_min_and_max_values_for_integral_type
16544 (ENUM_UNDERLYING_TYPE (enumtype), precision, sgn);
16545
16546 /* If -fstrict-enums, still constrain TYPE_MIN/MAX_VALUE. */
16547 if (flag_strict_enums)
16548 set_min_and_max_values_for_integral_type (enumtype, precision, sgn);
16549 }
16550 else
16551 underlying_type = ENUM_UNDERLYING_TYPE (enumtype);
16552
16553 /* If the enum is exported, mark the consts too. */
16554 bool export_p = (UNSCOPED_ENUM_P (enumtype)
16555 && DECL_MODULE_EXPORT_P (TYPE_STUB_DECL (enumtype))
16556 && at_namespace_scope_p ());
16557
16558 /* Convert each of the enumerators to the type of the underlying
16559 type of the enumeration. */
16560 for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values))
16561 {
16562 decl = TREE_VALUE (values);
16563 iloc_sentinel ils (DECL_SOURCE_LOCATION (decl));
16564 if (fixed_underlying_type_p)
16565 /* If the enumeration type has a fixed underlying type, we
16566 already checked all of the enumerator values. */
16567 value = DECL_INITIAL (decl);
16568 else
16569 value = perform_implicit_conversion (underlying_type,
16570 DECL_INITIAL (decl),
16571 tf_warning_or_error);
16572 /* Do not clobber shared ints. */
16573 if (value != error_mark_node)
16574 {
16575 value = copy_node (value);
16576
16577 TREE_TYPE (value) = enumtype;
16578 }
16579 DECL_INITIAL (decl) = value;
16580 if (export_p)
16581 DECL_MODULE_EXPORT_P (decl) = true;
16582 }
16583
16584 /* Fix up all variant types of this enum type. */
16585 for (t = TYPE_MAIN_VARIANT (enumtype); t; t = TYPE_NEXT_VARIANT (t))
16586 TYPE_VALUES (t) = TYPE_VALUES (enumtype);
16587
16588 if (at_class_scope_p ()
16589 && COMPLETE_TYPE_P (current_class_type)
16590 && UNSCOPED_ENUM_P (enumtype))
16591 {
16592 insert_late_enum_def_bindings (current_class_type, enumtype);
16593 /* TYPE_FIELDS needs fixup. */
16594 fixup_type_variants (current_class_type);
16595 }
16596
16597 /* Finish debugging output for this type. */
16598 rest_of_type_compilation (enumtype, namespace_bindings_p ());
16599
16600 /* Each enumerator now has the type of its enumeration. Clear the cache
16601 so that this change in types doesn't confuse us later on. */
16602 clear_cv_and_fold_caches ();
16603 }
16604
16605 /* Finishes the enum type. This is called only the first time an
16606 enumeration is seen, be it opaque or odinary.
16607 ENUMTYPE is the type object. */
16608
16609 void
finish_enum(tree enumtype)16610 finish_enum (tree enumtype)
16611 {
16612 if (processing_template_decl)
16613 {
16614 if (at_function_scope_p ())
16615 add_stmt (build_min (TAG_DEFN, enumtype));
16616 return;
16617 }
16618
16619 /* If this is a forward declaration, there should not be any variants,
16620 though we can get a variant in the middle of an enum-specifier with
16621 wacky code like 'enum E { e = sizeof(const E*) };' */
16622 gcc_assert (enumtype == TYPE_MAIN_VARIANT (enumtype)
16623 && (TYPE_VALUES (enumtype)
16624 || !TYPE_NEXT_VARIANT (enumtype)));
16625 }
16626
16627 /* Build and install a CONST_DECL for an enumeration constant of the
16628 enumeration type ENUMTYPE whose NAME and VALUE (if any) are provided.
16629 Apply ATTRIBUTES if available. LOC is the location of NAME.
16630 Assignment of sequential values by default is handled here. */
16631
16632 tree
build_enumerator(tree name,tree value,tree enumtype,tree attributes,location_t loc)16633 build_enumerator (tree name, tree value, tree enumtype, tree attributes,
16634 location_t loc)
16635 {
16636 tree decl;
16637 tree context;
16638 tree type;
16639
16640 /* scalar_constant_value will pull out this expression, so make sure
16641 it's folded as appropriate. */
16642 if (processing_template_decl)
16643 value = fold_non_dependent_expr (value);
16644
16645 /* If the VALUE was erroneous, pretend it wasn't there; that will
16646 result in the enum being assigned the next value in sequence. */
16647 if (value == error_mark_node)
16648 value = NULL_TREE;
16649
16650 /* Remove no-op casts from the value. */
16651 if (value)
16652 STRIP_TYPE_NOPS (value);
16653
16654 if (! processing_template_decl)
16655 {
16656 /* Validate and default VALUE. */
16657 if (value != NULL_TREE)
16658 {
16659 if (!ENUM_UNDERLYING_TYPE (enumtype))
16660 {
16661 tree tmp_value = build_expr_type_conversion (WANT_INT | WANT_ENUM,
16662 value, true);
16663 if (tmp_value)
16664 value = tmp_value;
16665 }
16666 else if (! INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P
16667 (TREE_TYPE (value)))
16668 value = perform_implicit_conversion_flags
16669 (ENUM_UNDERLYING_TYPE (enumtype), value, tf_warning_or_error,
16670 LOOKUP_IMPLICIT | LOOKUP_NO_NARROWING);
16671
16672 if (value == error_mark_node)
16673 value = NULL_TREE;
16674
16675 if (value != NULL_TREE)
16676 {
16677 if (! INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P
16678 (TREE_TYPE (value)))
16679 {
16680 error_at (cp_expr_loc_or_input_loc (value),
16681 "enumerator value for %qD must have integral or "
16682 "unscoped enumeration type", name);
16683 value = NULL_TREE;
16684 }
16685 else
16686 {
16687 value = cxx_constant_value (value);
16688
16689 if (TREE_CODE (value) != INTEGER_CST)
16690 {
16691 error ("enumerator value for %qD is not an integer "
16692 "constant", name);
16693 value = NULL_TREE;
16694 }
16695 }
16696 }
16697 }
16698
16699 /* Default based on previous value. */
16700 if (value == NULL_TREE)
16701 {
16702 if (TYPE_VALUES (enumtype))
16703 {
16704 tree prev_value;
16705
16706 /* C++03 7.2/4: If no initializer is specified for the first
16707 enumerator, the type is an unspecified integral
16708 type. Otherwise the type is the same as the type of the
16709 initializing value of the preceding enumerator unless the
16710 incremented value is not representable in that type, in
16711 which case the type is an unspecified integral type
16712 sufficient to contain the incremented value. */
16713 prev_value = DECL_INITIAL (TREE_VALUE (TYPE_VALUES (enumtype)));
16714 if (TREE_CODE (prev_value) != INTEGER_CST)
16715 value = error_mark_node;
16716 else
16717 {
16718 wi::overflow_type overflowed;
16719 tree type = TREE_TYPE (prev_value);
16720 signop sgn = TYPE_SIGN (type);
16721 widest_int wi = wi::add (wi::to_widest (prev_value), 1, sgn,
16722 &overflowed);
16723 if (!overflowed)
16724 {
16725 bool pos = !wi::neg_p (wi, sgn);
16726 if (!wi::fits_to_tree_p (wi, type))
16727 {
16728 unsigned int itk;
16729 for (itk = itk_int; itk != itk_none; itk++)
16730 {
16731 type = integer_types[itk];
16732 if (type != NULL_TREE
16733 && (pos || !TYPE_UNSIGNED (type))
16734 && wi::fits_to_tree_p (wi, type))
16735 break;
16736 }
16737 if (type && cxx_dialect < cxx11
16738 && itk > itk_unsigned_long)
16739 pedwarn (input_location, OPT_Wlong_long,
16740 pos ? G_("\
16741 incremented enumerator value is too large for %<unsigned long%>") : G_("\
16742 incremented enumerator value is too large for %<long%>"));
16743 }
16744 if (type == NULL_TREE)
16745 overflowed = wi::OVF_UNKNOWN;
16746 else
16747 value = wide_int_to_tree (type, wi);
16748 }
16749
16750 if (overflowed)
16751 {
16752 error ("overflow in enumeration values at %qD", name);
16753 value = error_mark_node;
16754 }
16755 }
16756 }
16757 else
16758 value = integer_zero_node;
16759 }
16760
16761 /* Remove no-op casts from the value. */
16762 STRIP_TYPE_NOPS (value);
16763
16764 /* If the underlying type of the enum is fixed, check whether
16765 the enumerator values fits in the underlying type. If it
16766 does not fit, the program is ill-formed [C++0x dcl.enum]. */
16767 if (ENUM_UNDERLYING_TYPE (enumtype)
16768 && value
16769 && TREE_CODE (value) == INTEGER_CST)
16770 {
16771 if (!int_fits_type_p (value, ENUM_UNDERLYING_TYPE (enumtype)))
16772 error ("enumerator value %qE is outside the range of underlying "
16773 "type %qT", value, ENUM_UNDERLYING_TYPE (enumtype));
16774
16775 /* Convert the value to the appropriate type. */
16776 value = fold_convert (ENUM_UNDERLYING_TYPE (enumtype), value);
16777 }
16778 }
16779
16780 /* C++ associates enums with global, function, or class declarations. */
16781 context = current_scope ();
16782
16783 /* Build the actual enumeration constant. Note that the enumeration
16784 constants have the underlying type of the enum (if it is fixed)
16785 or the type of their initializer (if the underlying type of the
16786 enum is not fixed):
16787
16788 [ C++0x dcl.enum ]
16789
16790 If the underlying type is fixed, the type of each enumerator
16791 prior to the closing brace is the underlying type; if the
16792 initializing value of an enumerator cannot be represented by
16793 the underlying type, the program is ill-formed. If the
16794 underlying type is not fixed, the type of each enumerator is
16795 the type of its initializing value.
16796
16797 If the underlying type is not fixed, it will be computed by
16798 finish_enum and we will reset the type of this enumerator. Of
16799 course, if we're processing a template, there may be no value. */
16800 type = value ? TREE_TYPE (value) : NULL_TREE;
16801
16802 decl = build_decl (loc, CONST_DECL, name, type);
16803
16804 DECL_CONTEXT (decl) = enumtype;
16805 TREE_CONSTANT (decl) = 1;
16806 TREE_READONLY (decl) = 1;
16807 DECL_INITIAL (decl) = value;
16808
16809 if (attributes)
16810 cplus_decl_attributes (&decl, attributes, 0);
16811
16812 if (context && context == current_class_type && !SCOPED_ENUM_P (enumtype))
16813 {
16814 /* In something like `struct S { enum E { i = 7 }; };' we put `i'
16815 on the TYPE_FIELDS list for `S'. (That's so that you can say
16816 things like `S::i' later.) */
16817
16818 /* The enumerator may be getting declared outside of its enclosing
16819 class, like so:
16820
16821 class S { public: enum E : int; }; enum S::E : int { i = 7; };
16822
16823 For which case we need to make sure that the access of `S::i'
16824 matches the access of `S::E'. */
16825 auto cas = make_temp_override (current_access_specifier);
16826 set_current_access_from_decl (TYPE_NAME (enumtype));
16827 finish_member_declaration (decl);
16828 }
16829 else
16830 pushdecl (decl);
16831
16832 /* Add this enumeration constant to the list for this type. */
16833 TYPE_VALUES (enumtype) = tree_cons (name, decl, TYPE_VALUES (enumtype));
16834
16835 return decl;
16836 }
16837
16838 /* Look for an enumerator with the given NAME within the enumeration
16839 type ENUMTYPE. This routine is used primarily for qualified name
16840 lookup into an enumerator in C++0x, e.g.,
16841
16842 enum class Color { Red, Green, Blue };
16843
16844 Color color = Color::Red;
16845
16846 Returns the value corresponding to the enumerator, or
16847 NULL_TREE if no such enumerator was found. */
16848 tree
lookup_enumerator(tree enumtype,tree name)16849 lookup_enumerator (tree enumtype, tree name)
16850 {
16851 tree e;
16852 gcc_assert (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE);
16853
16854 e = purpose_member (name, TYPE_VALUES (enumtype));
16855 return e? TREE_VALUE (e) : NULL_TREE;
16856 }
16857
16858 /* Implement LANG_HOOKS_SIMULATE_ENUM_DECL. */
16859
16860 tree
cxx_simulate_enum_decl(location_t loc,const char * name,vec<string_int_pair> * values)16861 cxx_simulate_enum_decl (location_t loc, const char *name,
16862 vec<string_int_pair> *values)
16863 {
16864 location_t saved_loc = input_location;
16865 input_location = loc;
16866
16867 tree enumtype = start_enum (get_identifier (name), NULL_TREE, NULL_TREE,
16868 NULL_TREE, false, NULL);
16869 if (!OPAQUE_ENUM_P (enumtype))
16870 {
16871 error_at (loc, "multiple definition of %q#T", enumtype);
16872 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
16873 "previous definition here");
16874 return enumtype;
16875 }
16876 SET_OPAQUE_ENUM_P (enumtype, false);
16877 DECL_SOURCE_LOCATION (TYPE_NAME (enumtype)) = loc;
16878
16879 for (const string_int_pair &value : values)
16880 build_enumerator (get_identifier (value.first),
16881 build_int_cst (integer_type_node, value.second),
16882 enumtype, NULL_TREE, loc);
16883
16884 finish_enum_value_list (enumtype);
16885 finish_enum (enumtype);
16886
16887 input_location = saved_loc;
16888 return enumtype;
16889 }
16890
16891 /* Implement LANG_HOOKS_SIMULATE_RECORD_DECL. */
16892
16893 tree
cxx_simulate_record_decl(location_t loc,const char * name,array_slice<const tree> fields)16894 cxx_simulate_record_decl (location_t loc, const char *name,
16895 array_slice<const tree> fields)
16896 {
16897 iloc_sentinel ils (loc);
16898
16899 tree ident = get_identifier (name);
16900 tree type = xref_tag (/*tag_code=*/record_type, ident);
16901 if (type != error_mark_node
16902 && (TREE_CODE (type) != RECORD_TYPE || COMPLETE_TYPE_P (type)))
16903 {
16904 error ("redefinition of %q#T", type);
16905 type = error_mark_node;
16906 }
16907 if (type == error_mark_node)
16908 return lhd_simulate_record_decl (loc, name, fields);
16909
16910 xref_basetypes (type, NULL_TREE);
16911 type = begin_class_definition (type);
16912 if (type == error_mark_node)
16913 return lhd_simulate_record_decl (loc, name, fields);
16914
16915 for (tree field : fields)
16916 finish_member_declaration (field);
16917
16918 type = finish_struct (type, NULL_TREE);
16919
16920 tree decl = build_decl (loc, TYPE_DECL, ident, type);
16921 set_underlying_type (decl);
16922 lang_hooks.decls.pushdecl (decl);
16923
16924 return type;
16925 }
16926
16927 /* We're defining DECL. Make sure that its type is OK. */
16928
16929 static void
check_function_type(tree decl,tree current_function_parms)16930 check_function_type (tree decl, tree current_function_parms)
16931 {
16932 tree fntype = TREE_TYPE (decl);
16933 tree return_type = complete_type (TREE_TYPE (fntype));
16934
16935 /* In a function definition, arg types must be complete. */
16936 require_complete_types_for_parms (current_function_parms);
16937
16938 if (dependent_type_p (return_type)
16939 || type_uses_auto (return_type))
16940 return;
16941 if (!COMPLETE_OR_VOID_TYPE_P (return_type))
16942 {
16943 tree args = TYPE_ARG_TYPES (fntype);
16944
16945 error ("return type %q#T is incomplete", return_type);
16946
16947 /* Make it return void instead. */
16948 if (TREE_CODE (fntype) == METHOD_TYPE)
16949 fntype = build_method_type_directly (TREE_TYPE (TREE_VALUE (args)),
16950 void_type_node,
16951 TREE_CHAIN (args));
16952 else
16953 fntype = build_function_type (void_type_node, args);
16954 fntype = (cp_build_type_attribute_variant
16955 (fntype, TYPE_ATTRIBUTES (TREE_TYPE (decl))));
16956 fntype = cxx_copy_lang_qualifiers (fntype, TREE_TYPE (decl));
16957 TREE_TYPE (decl) = fntype;
16958 }
16959 else
16960 {
16961 abstract_virtuals_error (decl, TREE_TYPE (fntype));
16962 maybe_warn_parm_abi (TREE_TYPE (fntype),
16963 DECL_SOURCE_LOCATION (decl));
16964 }
16965 }
16966
16967 /* True iff FN is an implicitly-defined default constructor. */
16968
16969 static bool
implicit_default_ctor_p(tree fn)16970 implicit_default_ctor_p (tree fn)
16971 {
16972 return (DECL_CONSTRUCTOR_P (fn)
16973 && !user_provided_p (fn)
16974 && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn)));
16975 }
16976
16977 /* Clobber the contents of *this to let the back end know that the object
16978 storage is dead when we enter the constructor or leave the destructor. */
16979
16980 static tree
build_clobber_this()16981 build_clobber_this ()
16982 {
16983 /* Clobbering an empty base is pointless, and harmful if its one byte
16984 TYPE_SIZE overlays real data. */
16985 if (is_empty_class (current_class_type))
16986 return void_node;
16987
16988 /* If we have virtual bases, clobber the whole object, but only if we're in
16989 charge. If we don't have virtual bases, clobber the as-base type so we
16990 don't mess with tail padding. */
16991 bool vbases = CLASSTYPE_VBASECLASSES (current_class_type);
16992
16993 tree ctype = current_class_type;
16994 if (!vbases)
16995 ctype = CLASSTYPE_AS_BASE (ctype);
16996
16997 tree clobber = build_clobber (ctype);
16998
16999 tree thisref = current_class_ref;
17000 if (ctype != current_class_type)
17001 {
17002 thisref = build_nop (build_reference_type (ctype), current_class_ptr);
17003 thisref = convert_from_reference (thisref);
17004 }
17005
17006 tree exprstmt = build2 (MODIFY_EXPR, void_type_node, thisref, clobber);
17007 if (vbases)
17008 exprstmt = build_if_in_charge (exprstmt);
17009
17010 return exprstmt;
17011 }
17012
17013 /* Create the FUNCTION_DECL for a function definition.
17014 DECLSPECS and DECLARATOR are the parts of the declaration;
17015 they describe the function's name and the type it returns,
17016 but twisted together in a fashion that parallels the syntax of C.
17017
17018 FLAGS is a bitwise or of SF_PRE_PARSED (indicating that the
17019 DECLARATOR is really the DECL for the function we are about to
17020 process and that DECLSPECS should be ignored), SF_INCLASS_INLINE
17021 indicating that the function is an inline defined in-class.
17022
17023 This function creates a binding context for the function body
17024 as well as setting up the FUNCTION_DECL in current_function_decl.
17025
17026 For C++, we must first check whether that datum makes any sense.
17027 For example, "class A local_a(1,2);" means that variable local_a
17028 is an aggregate of type A, which should have a constructor
17029 applied to it with the argument list [1, 2].
17030
17031 On entry, DECL_INITIAL (decl1) should be NULL_TREE or error_mark_node,
17032 or may be a BLOCK if the function has been defined previously
17033 in this translation unit. On exit, DECL_INITIAL (decl1) will be
17034 error_mark_node if the function has never been defined, or
17035 a BLOCK if the function has been defined somewhere. */
17036
17037 bool
start_preparsed_function(tree decl1,tree attrs,int flags)17038 start_preparsed_function (tree decl1, tree attrs, int flags)
17039 {
17040 tree ctype = NULL_TREE;
17041 bool doing_friend = false;
17042
17043 /* Sanity check. */
17044 gcc_assert (VOID_TYPE_P (TREE_VALUE (void_list_node)));
17045 gcc_assert (TREE_CHAIN (void_list_node) == NULL_TREE);
17046
17047 tree fntype = TREE_TYPE (decl1);
17048 if (TREE_CODE (fntype) == METHOD_TYPE)
17049 ctype = TYPE_METHOD_BASETYPE (fntype);
17050 else
17051 {
17052 ctype = DECL_FRIEND_CONTEXT (decl1);
17053
17054 if (ctype)
17055 doing_friend = true;
17056 }
17057
17058 if (DECL_DECLARED_INLINE_P (decl1)
17059 && lookup_attribute ("noinline", attrs))
17060 warning_at (DECL_SOURCE_LOCATION (decl1), 0,
17061 "inline function %qD given attribute %qs", decl1, "noinline");
17062
17063 /* Handle gnu_inline attribute. */
17064 if (GNU_INLINE_P (decl1))
17065 {
17066 DECL_EXTERNAL (decl1) = 1;
17067 DECL_NOT_REALLY_EXTERN (decl1) = 0;
17068 DECL_INTERFACE_KNOWN (decl1) = 1;
17069 DECL_DISREGARD_INLINE_LIMITS (decl1) = 1;
17070 }
17071
17072 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl1))
17073 /* This is a constructor, we must ensure that any default args
17074 introduced by this definition are propagated to the clones
17075 now. The clones are used directly in overload resolution. */
17076 adjust_clone_args (decl1);
17077
17078 /* Sometimes we don't notice that a function is a static member, and
17079 build a METHOD_TYPE for it. Fix that up now. */
17080 gcc_assert (!(ctype != NULL_TREE && DECL_STATIC_FUNCTION_P (decl1)
17081 && TREE_CODE (TREE_TYPE (decl1)) == METHOD_TYPE));
17082
17083 /* Set up current_class_type, and enter the scope of the class, if
17084 appropriate. */
17085 if (ctype)
17086 push_nested_class (ctype);
17087 else if (DECL_STATIC_FUNCTION_P (decl1))
17088 push_nested_class (DECL_CONTEXT (decl1));
17089
17090 /* Now that we have entered the scope of the class, we must restore
17091 the bindings for any template parameters surrounding DECL1, if it
17092 is an inline member template. (Order is important; consider the
17093 case where a template parameter has the same name as a field of
17094 the class.) It is not until after this point that
17095 PROCESSING_TEMPLATE_DECL is guaranteed to be set up correctly. */
17096 if (flags & SF_INCLASS_INLINE)
17097 maybe_begin_member_template_processing (decl1);
17098
17099 /* Effective C++ rule 15. */
17100 if (warn_ecpp
17101 && DECL_ASSIGNMENT_OPERATOR_P (decl1)
17102 && DECL_OVERLOADED_OPERATOR_IS (decl1, NOP_EXPR)
17103 && VOID_TYPE_P (TREE_TYPE (fntype)))
17104 warning (OPT_Weffc__,
17105 "%<operator=%> should return a reference to %<*this%>");
17106
17107 /* Make the init_value nonzero so pushdecl knows this is not tentative.
17108 error_mark_node is replaced below (in poplevel) with the BLOCK. */
17109 if (!DECL_INITIAL (decl1))
17110 DECL_INITIAL (decl1) = error_mark_node;
17111
17112 /* This function exists in static storage.
17113 (This does not mean `static' in the C sense!) */
17114 TREE_STATIC (decl1) = 1;
17115
17116 /* We must call push_template_decl after current_class_type is set
17117 up. (If we are processing inline definitions after exiting a
17118 class scope, current_class_type will be NULL_TREE until set above
17119 by push_nested_class.) */
17120 if (processing_template_decl)
17121 {
17122 tree newdecl1 = push_template_decl (decl1, doing_friend);
17123 if (newdecl1 == error_mark_node)
17124 {
17125 if (ctype || DECL_STATIC_FUNCTION_P (decl1))
17126 pop_nested_class ();
17127 return false;
17128 }
17129 decl1 = newdecl1;
17130 }
17131
17132 /* Make sure the parameter and return types are reasonable. When
17133 you declare a function, these types can be incomplete, but they
17134 must be complete when you define the function. */
17135 check_function_type (decl1, DECL_ARGUMENTS (decl1));
17136
17137 /* Build the return declaration for the function. */
17138 tree restype = TREE_TYPE (fntype);
17139
17140 if (DECL_RESULT (decl1) == NULL_TREE)
17141 {
17142 tree resdecl;
17143
17144 resdecl = build_decl (input_location, RESULT_DECL, 0, restype);
17145 DECL_ARTIFICIAL (resdecl) = 1;
17146 DECL_IGNORED_P (resdecl) = 1;
17147 DECL_RESULT (decl1) = resdecl;
17148
17149 cp_apply_type_quals_to_decl (cp_type_quals (restype), resdecl);
17150 }
17151
17152 /* Record the decl so that the function name is defined.
17153 If we already have a decl for this name, and it is a FUNCTION_DECL,
17154 use the old decl. */
17155 if (!processing_template_decl && !(flags & SF_PRE_PARSED))
17156 {
17157 /* A specialization is not used to guide overload resolution. */
17158 if (!DECL_FUNCTION_MEMBER_P (decl1)
17159 && !(DECL_USE_TEMPLATE (decl1) &&
17160 PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl1))))
17161 {
17162 tree olddecl = pushdecl (decl1);
17163
17164 if (olddecl == error_mark_node)
17165 /* If something went wrong when registering the declaration,
17166 use DECL1; we have to have a FUNCTION_DECL to use when
17167 parsing the body of the function. */
17168 ;
17169 else
17170 {
17171 /* Otherwise, OLDDECL is either a previous declaration
17172 of the same function or DECL1 itself. */
17173
17174 if (warn_missing_declarations
17175 && olddecl == decl1
17176 && !DECL_MAIN_P (decl1)
17177 && TREE_PUBLIC (decl1)
17178 && !DECL_DECLARED_INLINE_P (decl1))
17179 {
17180 tree context;
17181
17182 /* Check whether DECL1 is in an anonymous
17183 namespace. */
17184 for (context = DECL_CONTEXT (decl1);
17185 context;
17186 context = DECL_CONTEXT (context))
17187 {
17188 if (TREE_CODE (context) == NAMESPACE_DECL
17189 && DECL_NAME (context) == NULL_TREE)
17190 break;
17191 }
17192
17193 if (context == NULL)
17194 warning_at (DECL_SOURCE_LOCATION (decl1),
17195 OPT_Wmissing_declarations,
17196 "no previous declaration for %qD", decl1);
17197 }
17198
17199 decl1 = olddecl;
17200 }
17201 }
17202 else
17203 {
17204 /* We need to set the DECL_CONTEXT. */
17205 if (!DECL_CONTEXT (decl1) && DECL_TEMPLATE_INFO (decl1))
17206 DECL_CONTEXT (decl1) = DECL_CONTEXT (DECL_TI_TEMPLATE (decl1));
17207 }
17208 fntype = TREE_TYPE (decl1);
17209 restype = TREE_TYPE (fntype);
17210
17211 /* If #pragma weak applies, mark the decl appropriately now.
17212 The pragma only applies to global functions. Because
17213 determining whether or not the #pragma applies involves
17214 computing the mangled name for the declaration, we cannot
17215 apply the pragma until after we have merged this declaration
17216 with any previous declarations; if the original declaration
17217 has a linkage specification, that specification applies to
17218 the definition as well, and may affect the mangled name. */
17219 if (DECL_FILE_SCOPE_P (decl1))
17220 maybe_apply_pragma_weak (decl1);
17221 }
17222
17223 /* We are now in the scope of the function being defined. */
17224 current_function_decl = decl1;
17225
17226 /* Save the parm names or decls from this function's declarator
17227 where store_parm_decls will find them. */
17228 tree current_function_parms = DECL_ARGUMENTS (decl1);
17229
17230 /* Let the user know we're compiling this function. */
17231 announce_function (decl1);
17232
17233 gcc_assert (DECL_INITIAL (decl1));
17234
17235 /* This function may already have been parsed, in which case just
17236 return; our caller will skip over the body without parsing. */
17237 if (DECL_INITIAL (decl1) != error_mark_node)
17238 return true;
17239
17240 /* Initialize RTL machinery. We cannot do this until
17241 CURRENT_FUNCTION_DECL and DECL_RESULT are set up. We do this
17242 even when processing a template; this is how we get
17243 CFUN set up, and our per-function variables initialized.
17244 FIXME factor out the non-RTL stuff. */
17245 cp_binding_level *bl = current_binding_level;
17246 allocate_struct_function (decl1, processing_template_decl);
17247
17248 /* Initialize the language data structures. Whenever we start
17249 a new function, we destroy temporaries in the usual way. */
17250 cfun->language = ggc_cleared_alloc<language_function> ();
17251 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
17252 current_binding_level = bl;
17253
17254 /* If we are (erroneously) defining a function that we have already
17255 defined before, wipe out what we knew before. */
17256 gcc_checking_assert (!DECL_PENDING_INLINE_P (decl1));
17257 FNDECL_USED_AUTO (decl1) = false;
17258 DECL_SAVED_AUTO_RETURN_TYPE (decl1) = NULL;
17259
17260 if (!processing_template_decl && type_uses_auto (restype))
17261 {
17262 FNDECL_USED_AUTO (decl1) = true;
17263 DECL_SAVED_AUTO_RETURN_TYPE (decl1) = restype;
17264 }
17265
17266 /* Start the statement-tree, start the tree now. */
17267 DECL_SAVED_TREE (decl1) = push_stmt_list ();
17268
17269 if (ctype && !doing_friend && !DECL_STATIC_FUNCTION_P (decl1))
17270 {
17271 /* We know that this was set up by `grokclassfn'. We do not
17272 wait until `store_parm_decls', since evil parse errors may
17273 never get us to that point. Here we keep the consistency
17274 between `current_class_type' and `current_class_ptr'. */
17275 tree t = DECL_ARGUMENTS (decl1);
17276
17277 gcc_assert (t != NULL_TREE && TREE_CODE (t) == PARM_DECL);
17278 gcc_assert (TYPE_PTR_P (TREE_TYPE (t)));
17279
17280 cp_function_chain->x_current_class_ref
17281 = cp_build_fold_indirect_ref (t);
17282 /* Set this second to avoid shortcut in cp_build_indirect_ref. */
17283 cp_function_chain->x_current_class_ptr = t;
17284
17285 /* Constructors and destructors need to know whether they're "in
17286 charge" of initializing virtual base classes. */
17287 t = DECL_CHAIN (t);
17288 if (DECL_HAS_IN_CHARGE_PARM_P (decl1))
17289 {
17290 current_in_charge_parm = t;
17291 t = DECL_CHAIN (t);
17292 }
17293 if (DECL_HAS_VTT_PARM_P (decl1))
17294 {
17295 gcc_assert (DECL_NAME (t) == vtt_parm_identifier);
17296 current_vtt_parm = t;
17297 }
17298 }
17299
17300 bool honor_interface = (!DECL_TEMPLATE_INSTANTIATION (decl1)
17301 /* Implicitly-defined methods (like the
17302 destructor for a class in which no destructor
17303 is explicitly declared) must not be defined
17304 until their definition is needed. So, we
17305 ignore interface specifications for
17306 compiler-generated functions. */
17307 && !DECL_ARTIFICIAL (decl1));
17308 struct c_fileinfo *finfo
17309 = get_fileinfo (LOCATION_FILE (DECL_SOURCE_LOCATION (decl1)));
17310
17311 if (processing_template_decl)
17312 /* Don't mess with interface flags. */;
17313 else if (DECL_INTERFACE_KNOWN (decl1))
17314 {
17315 tree ctx = decl_function_context (decl1);
17316
17317 if (DECL_NOT_REALLY_EXTERN (decl1))
17318 DECL_EXTERNAL (decl1) = 0;
17319
17320 if (ctx != NULL_TREE && vague_linkage_p (ctx))
17321 /* This is a function in a local class in an extern inline
17322 or template function. */
17323 comdat_linkage (decl1);
17324 }
17325 /* If this function belongs to an interface, it is public.
17326 If it belongs to someone else's interface, it is also external.
17327 This only affects inlines and template instantiations. */
17328 else if (!finfo->interface_unknown && honor_interface)
17329 {
17330 if (DECL_DECLARED_INLINE_P (decl1)
17331 || DECL_TEMPLATE_INSTANTIATION (decl1))
17332 {
17333 DECL_EXTERNAL (decl1)
17334 = (finfo->interface_only
17335 || (DECL_DECLARED_INLINE_P (decl1)
17336 && ! flag_implement_inlines
17337 && !DECL_VINDEX (decl1)));
17338
17339 /* For WIN32 we also want to put these in linkonce sections. */
17340 maybe_make_one_only (decl1);
17341 }
17342 else
17343 DECL_EXTERNAL (decl1) = 0;
17344 DECL_INTERFACE_KNOWN (decl1) = 1;
17345 /* If this function is in an interface implemented in this file,
17346 make sure that the back end knows to emit this function
17347 here. */
17348 if (!DECL_EXTERNAL (decl1))
17349 mark_needed (decl1);
17350 }
17351 else if (finfo->interface_unknown && finfo->interface_only
17352 && honor_interface)
17353 {
17354 /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma
17355 interface, we will have both finfo->interface_unknown and
17356 finfo->interface_only set. In that case, we don't want to
17357 use the normal heuristics because someone will supply a
17358 #pragma implementation elsewhere, and deducing it here would
17359 produce a conflict. */
17360 comdat_linkage (decl1);
17361 DECL_EXTERNAL (decl1) = 0;
17362 DECL_INTERFACE_KNOWN (decl1) = 1;
17363 DECL_DEFER_OUTPUT (decl1) = 1;
17364 }
17365 else
17366 {
17367 /* This is a definition, not a reference.
17368 So clear DECL_EXTERNAL, unless this is a GNU extern inline. */
17369 if (!GNU_INLINE_P (decl1))
17370 DECL_EXTERNAL (decl1) = 0;
17371
17372 if ((DECL_DECLARED_INLINE_P (decl1)
17373 || DECL_TEMPLATE_INSTANTIATION (decl1))
17374 && ! DECL_INTERFACE_KNOWN (decl1))
17375 DECL_DEFER_OUTPUT (decl1) = 1;
17376 else
17377 DECL_INTERFACE_KNOWN (decl1) = 1;
17378 }
17379
17380 /* Determine the ELF visibility attribute for the function. We must not
17381 do this before calling "pushdecl", as we must allow "duplicate_decls"
17382 to merge any attributes appropriately. We also need to wait until
17383 linkage is set. */
17384 if (!DECL_CLONED_FUNCTION_P (decl1))
17385 determine_visibility (decl1);
17386
17387 if (!processing_template_decl)
17388 maybe_instantiate_noexcept (decl1);
17389
17390 begin_scope (sk_function_parms, decl1);
17391
17392 ++function_depth;
17393
17394 if (DECL_DESTRUCTOR_P (decl1)
17395 || (DECL_CONSTRUCTOR_P (decl1)
17396 && targetm.cxx.cdtor_returns_this ()))
17397 {
17398 cdtor_label = create_artificial_label (input_location);
17399 LABEL_DECL_CDTOR (cdtor_label) = true;
17400 }
17401
17402 start_fname_decls ();
17403
17404 store_parm_decls (current_function_parms);
17405
17406 if (!processing_template_decl
17407 && (flag_lifetime_dse > 1)
17408 && DECL_CONSTRUCTOR_P (decl1)
17409 && !DECL_CLONED_FUNCTION_P (decl1)
17410 /* Clobbering an empty base is harmful if it overlays real data. */
17411 && !is_empty_class (current_class_type)
17412 /* We can't clobber safely for an implicitly-defined default constructor
17413 because part of the initialization might happen before we enter the
17414 constructor, via AGGR_INIT_ZERO_FIRST (c++/68006). */
17415 && !implicit_default_ctor_p (decl1))
17416 finish_expr_stmt (build_clobber_this ());
17417
17418 if (!processing_template_decl
17419 && DECL_CONSTRUCTOR_P (decl1)
17420 && sanitize_flags_p (SANITIZE_VPTR)
17421 && !DECL_CLONED_FUNCTION_P (decl1)
17422 && !implicit_default_ctor_p (decl1))
17423 cp_ubsan_maybe_initialize_vtbl_ptrs (current_class_ptr);
17424
17425 if (!DECL_OMP_DECLARE_REDUCTION_P (decl1))
17426 start_lambda_scope (decl1);
17427
17428 return true;
17429 }
17430
17431
17432 /* Like start_preparsed_function, except that instead of a
17433 FUNCTION_DECL, this function takes DECLSPECS and DECLARATOR.
17434
17435 Returns true on success. If the DECLARATOR is not suitable
17436 for a function, we return false, which tells the parser to
17437 skip the entire function. */
17438
17439 bool
start_function(cp_decl_specifier_seq * declspecs,const cp_declarator * declarator,tree attrs)17440 start_function (cp_decl_specifier_seq *declspecs,
17441 const cp_declarator *declarator,
17442 tree attrs)
17443 {
17444 tree decl1;
17445
17446 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, &attrs);
17447 invoke_plugin_callbacks (PLUGIN_START_PARSE_FUNCTION, decl1);
17448 if (decl1 == error_mark_node)
17449 return false;
17450
17451 if (DECL_MAIN_P (decl1))
17452 /* main must return int. grokfndecl should have corrected it
17453 (and issued a diagnostic) if the user got it wrong. */
17454 gcc_assert (same_type_p (TREE_TYPE (TREE_TYPE (decl1)),
17455 integer_type_node));
17456
17457 return start_preparsed_function (decl1, attrs, /*flags=*/SF_DEFAULT);
17458 }
17459
17460 /* Returns true iff an EH_SPEC_BLOCK should be created in the body of
17461 FN. */
17462
17463 static bool
use_eh_spec_block(tree fn)17464 use_eh_spec_block (tree fn)
17465 {
17466 return (flag_exceptions && flag_enforce_eh_specs
17467 && !processing_template_decl
17468 /* We insert the EH_SPEC_BLOCK only in the original
17469 function; then, it is copied automatically to the
17470 clones. */
17471 && !DECL_CLONED_FUNCTION_P (fn)
17472 /* Implicitly-generated constructors and destructors have
17473 exception specifications. However, those specifications
17474 are the union of the possible exceptions specified by the
17475 constructors/destructors for bases and members, so no
17476 unallowed exception will ever reach this function. By
17477 not creating the EH_SPEC_BLOCK we save a little memory,
17478 and we avoid spurious warnings about unreachable
17479 code. */
17480 && !DECL_DEFAULTED_FN (fn)
17481 && !type_throw_all_p (TREE_TYPE (fn)));
17482 }
17483
17484 /* Helper function to push ARGS into the current lexical scope. DECL
17485 is the function declaration. NONPARMS is used to handle enum
17486 constants. */
17487
17488 void
do_push_parm_decls(tree decl,tree args,tree * nonparms)17489 do_push_parm_decls (tree decl, tree args, tree *nonparms)
17490 {
17491 /* If we're doing semantic analysis, then we'll call pushdecl
17492 for each of these. We must do them in reverse order so that
17493 they end in the correct forward order. */
17494 args = nreverse (args);
17495
17496 tree next;
17497 for (tree parm = args; parm; parm = next)
17498 {
17499 next = DECL_CHAIN (parm);
17500 if (TREE_CODE (parm) == PARM_DECL)
17501 pushdecl (parm);
17502 else if (nonparms)
17503 {
17504 /* If we find an enum constant or a type tag, put it aside for
17505 the moment. */
17506 TREE_CHAIN (parm) = NULL_TREE;
17507 *nonparms = chainon (*nonparms, parm);
17508 }
17509 }
17510
17511 /* Get the decls in their original chain order and record in the
17512 function. This is all and only the PARM_DECLs that were
17513 pushed into scope by the loop above. */
17514 DECL_ARGUMENTS (decl) = get_local_decls ();
17515 }
17516
17517 /* Store the parameter declarations into the current function declaration.
17518 This is called after parsing the parameter declarations, before
17519 digesting the body of the function.
17520
17521 Also install to binding contour return value identifier, if any. */
17522
17523 static void
store_parm_decls(tree current_function_parms)17524 store_parm_decls (tree current_function_parms)
17525 {
17526 tree fndecl = current_function_decl;
17527
17528 /* This is a chain of any other decls that came in among the parm
17529 declarations. If a parm is declared with enum {foo, bar} x;
17530 then CONST_DECLs for foo and bar are put here. */
17531 tree nonparms = NULL_TREE;
17532
17533 if (current_function_parms)
17534 {
17535 /* This case is when the function was defined with an ANSI prototype.
17536 The parms already have decls, so we need not do anything here
17537 except record them as in effect
17538 and complain if any redundant old-style parm decls were written. */
17539
17540 tree specparms = current_function_parms;
17541
17542 /* Must clear this because it might contain TYPE_DECLs declared
17543 at class level. */
17544 current_binding_level->names = NULL;
17545
17546 do_push_parm_decls (fndecl, specparms, &nonparms);
17547 }
17548 else
17549 DECL_ARGUMENTS (fndecl) = NULL_TREE;
17550
17551 /* Now store the final chain of decls for the arguments
17552 as the decl-chain of the current lexical scope.
17553 Put the enumerators in as well, at the front so that
17554 DECL_ARGUMENTS is not modified. */
17555 current_binding_level->names = chainon (nonparms, DECL_ARGUMENTS (fndecl));
17556
17557 if (use_eh_spec_block (current_function_decl))
17558 current_eh_spec_block = begin_eh_spec_block ();
17559 }
17560
17561
17562 /* Set the return value of the constructor (if present). */
17563
17564 static void
finish_constructor_body(void)17565 finish_constructor_body (void)
17566 {
17567 tree val;
17568 tree exprstmt;
17569
17570 if (targetm.cxx.cdtor_returns_this ())
17571 {
17572 /* Any return from a constructor will end up here. */
17573 add_stmt (build_stmt (input_location, LABEL_EXPR, cdtor_label));
17574
17575 val = DECL_ARGUMENTS (current_function_decl);
17576 suppress_warning (val, OPT_Wuse_after_free);
17577 val = build2 (MODIFY_EXPR, TREE_TYPE (val),
17578 DECL_RESULT (current_function_decl), val);
17579 /* Return the address of the object. */
17580 exprstmt = build_stmt (input_location, RETURN_EXPR, val);
17581 add_stmt (exprstmt);
17582 }
17583 }
17584
17585 /* Do all the processing for the beginning of a destructor; set up the
17586 vtable pointers and cleanups for bases and members. */
17587
17588 static void
begin_destructor_body(void)17589 begin_destructor_body (void)
17590 {
17591 tree compound_stmt;
17592
17593 /* If the CURRENT_CLASS_TYPE is incomplete, we will have already
17594 issued an error message. We still want to try to process the
17595 body of the function, but initialize_vtbl_ptrs will crash if
17596 TYPE_BINFO is NULL. */
17597 if (COMPLETE_TYPE_P (current_class_type))
17598 {
17599 compound_stmt = begin_compound_stmt (0);
17600 /* Make all virtual function table pointers in non-virtual base
17601 classes point to CURRENT_CLASS_TYPE's virtual function
17602 tables. */
17603 initialize_vtbl_ptrs (current_class_ptr);
17604 finish_compound_stmt (compound_stmt);
17605
17606 if (flag_lifetime_dse
17607 /* Clobbering an empty base is harmful if it overlays real data. */
17608 && !is_empty_class (current_class_type))
17609 {
17610 if (sanitize_flags_p (SANITIZE_VPTR)
17611 && (flag_sanitize_recover & SANITIZE_VPTR) == 0
17612 && TYPE_CONTAINS_VPTR_P (current_class_type))
17613 {
17614 tree binfo = TYPE_BINFO (current_class_type);
17615 tree ref
17616 = cp_build_fold_indirect_ref (current_class_ptr);
17617
17618 tree vtbl_ptr = build_vfield_ref (ref, TREE_TYPE (binfo));
17619 tree vtbl = build_zero_cst (TREE_TYPE (vtbl_ptr));
17620 tree stmt = cp_build_modify_expr (input_location, vtbl_ptr,
17621 NOP_EXPR, vtbl,
17622 tf_warning_or_error);
17623 /* If the vptr is shared with some virtual nearly empty base,
17624 don't clear it if not in charge, the dtor of the virtual
17625 nearly empty base will do that later. */
17626 if (CLASSTYPE_VBASECLASSES (current_class_type))
17627 {
17628 tree c = current_class_type;
17629 while (CLASSTYPE_PRIMARY_BINFO (c))
17630 {
17631 if (BINFO_VIRTUAL_P (CLASSTYPE_PRIMARY_BINFO (c)))
17632 {
17633 stmt = convert_to_void (stmt, ICV_STATEMENT,
17634 tf_warning_or_error);
17635 stmt = build_if_in_charge (stmt);
17636 break;
17637 }
17638 c = BINFO_TYPE (CLASSTYPE_PRIMARY_BINFO (c));
17639 }
17640 }
17641 finish_decl_cleanup (NULL_TREE, stmt);
17642 }
17643 else
17644 finish_decl_cleanup (NULL_TREE, build_clobber_this ());
17645 }
17646
17647 /* And insert cleanups for our bases and members so that they
17648 will be properly destroyed if we throw. */
17649 push_base_cleanups ();
17650 }
17651 }
17652
17653 /* At the end of every destructor we generate code to delete the object if
17654 necessary. Do that now. */
17655
17656 static void
finish_destructor_body(void)17657 finish_destructor_body (void)
17658 {
17659 tree exprstmt;
17660
17661 /* Any return from a destructor will end up here; that way all base
17662 and member cleanups will be run when the function returns. */
17663 add_stmt (build_stmt (input_location, LABEL_EXPR, cdtor_label));
17664
17665 if (targetm.cxx.cdtor_returns_this ())
17666 {
17667 tree val;
17668
17669 val = DECL_ARGUMENTS (current_function_decl);
17670 suppress_warning (val, OPT_Wuse_after_free);
17671 val = build2 (MODIFY_EXPR, TREE_TYPE (val),
17672 DECL_RESULT (current_function_decl), val);
17673 /* Return the address of the object. */
17674 exprstmt = build_stmt (input_location, RETURN_EXPR, val);
17675 add_stmt (exprstmt);
17676 }
17677 }
17678
17679 /* Do the necessary processing for the beginning of a function body, which
17680 in this case includes member-initializers, but not the catch clauses of
17681 a function-try-block. Currently, this means opening a binding level
17682 for the member-initializers (in a ctor), member cleanups (in a dtor),
17683 and capture proxies (in a lambda operator()). */
17684
17685 tree
begin_function_body(void)17686 begin_function_body (void)
17687 {
17688 if (! FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
17689 return NULL_TREE;
17690
17691 if (processing_template_decl)
17692 /* Do nothing now. */;
17693 else
17694 /* Always keep the BLOCK node associated with the outermost pair of
17695 curly braces of a function. These are needed for correct
17696 operation of dwarfout.c. */
17697 keep_next_level (true);
17698
17699 tree stmt = begin_compound_stmt (BCS_FN_BODY);
17700
17701 if (processing_template_decl)
17702 /* Do nothing now. */;
17703 else if (DECL_DESTRUCTOR_P (current_function_decl))
17704 begin_destructor_body ();
17705
17706 return stmt;
17707 }
17708
17709 /* Do the processing for the end of a function body. Currently, this means
17710 closing out the cleanups for fully-constructed bases and members, and in
17711 the case of the destructor, deleting the object if desired. Again, this
17712 is only meaningful for [cd]tors, since they are the only functions where
17713 there is a significant distinction between the main body and any
17714 function catch clauses. Handling, say, main() return semantics here
17715 would be wrong, as flowing off the end of a function catch clause for
17716 main() would also need to return 0. */
17717
17718 void
finish_function_body(tree compstmt)17719 finish_function_body (tree compstmt)
17720 {
17721 if (compstmt == NULL_TREE)
17722 return;
17723
17724 /* Close the block. */
17725 finish_compound_stmt (compstmt);
17726
17727 if (processing_template_decl)
17728 /* Do nothing now. */;
17729 else if (DECL_CONSTRUCTOR_P (current_function_decl))
17730 finish_constructor_body ();
17731 else if (DECL_DESTRUCTOR_P (current_function_decl))
17732 finish_destructor_body ();
17733 }
17734
17735 /* Given a function, returns the BLOCK corresponding to the outermost level
17736 of curly braces, skipping the artificial block created for constructor
17737 initializers. */
17738
17739 tree
outer_curly_brace_block(tree fndecl)17740 outer_curly_brace_block (tree fndecl)
17741 {
17742 tree block = DECL_INITIAL (fndecl);
17743 if (BLOCK_OUTER_CURLY_BRACE_P (block))
17744 return block;
17745 block = BLOCK_SUBBLOCKS (block);
17746 if (BLOCK_OUTER_CURLY_BRACE_P (block))
17747 return block;
17748 block = BLOCK_SUBBLOCKS (block);
17749 gcc_assert (BLOCK_OUTER_CURLY_BRACE_P (block));
17750 return block;
17751 }
17752
17753 /* If FNDECL is a class's key method, add the class to the list of
17754 keyed classes that should be emitted. */
17755
17756 static void
record_key_method_defined(tree fndecl)17757 record_key_method_defined (tree fndecl)
17758 {
17759 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fndecl)
17760 && DECL_VIRTUAL_P (fndecl)
17761 && !processing_template_decl)
17762 {
17763 tree fnclass = DECL_CONTEXT (fndecl);
17764 if (fndecl == CLASSTYPE_KEY_METHOD (fnclass))
17765 vec_safe_push (keyed_classes, fnclass);
17766 }
17767 }
17768
17769 /* Attempt to add a fix-it hint to RICHLOC suggesting the insertion
17770 of "return *this;" immediately before its location, using FNDECL's
17771 first statement (if any) to give the indentation, if appropriate. */
17772
17773 static void
add_return_star_this_fixit(gcc_rich_location * richloc,tree fndecl)17774 add_return_star_this_fixit (gcc_rich_location *richloc, tree fndecl)
17775 {
17776 location_t indent = UNKNOWN_LOCATION;
17777 tree stmts = expr_first (DECL_SAVED_TREE (fndecl));
17778 if (stmts)
17779 indent = EXPR_LOCATION (stmts);
17780 richloc->add_fixit_insert_formatted ("return *this;",
17781 richloc->get_loc (),
17782 indent);
17783 }
17784
17785 /* This function carries out the subset of finish_function operations needed
17786 to emit the compiler-generated outlined helper functions used by the
17787 coroutines implementation. */
17788
17789 static void
emit_coro_helper(tree helper)17790 emit_coro_helper (tree helper)
17791 {
17792 /* This is a partial set of the operations done by finish_function()
17793 plus emitting the result. */
17794 set_cfun (NULL);
17795 current_function_decl = helper;
17796 begin_scope (sk_function_parms, NULL);
17797 store_parm_decls (DECL_ARGUMENTS (helper));
17798 announce_function (helper);
17799 allocate_struct_function (helper, false);
17800 cfun->language = ggc_cleared_alloc<language_function> ();
17801 poplevel (1, 0, 1);
17802 maybe_save_constexpr_fundef (helper);
17803 /* We must start each function with a clear fold cache. */
17804 clear_fold_cache ();
17805 cp_fold_function (helper);
17806 DECL_CONTEXT (DECL_RESULT (helper)) = helper;
17807 BLOCK_SUPERCONTEXT (DECL_INITIAL (helper)) = helper;
17808 /* This function has coroutine IFNs that we should handle in middle
17809 end lowering. */
17810 cfun->coroutine_component = true;
17811 cp_genericize (helper);
17812 expand_or_defer_fn (helper);
17813 }
17814
17815 /* Finish up a function declaration and compile that function
17816 all the way to assembler language output. The free the storage
17817 for the function definition. INLINE_P is TRUE if we just
17818 finished processing the body of an in-class inline function
17819 definition. (This processing will have taken place after the
17820 class definition is complete.) */
17821
17822 tree
finish_function(bool inline_p)17823 finish_function (bool inline_p)
17824 {
17825 tree fndecl = current_function_decl;
17826 tree fntype, ctype = NULL_TREE;
17827 tree resumer = NULL_TREE, destroyer = NULL_TREE;
17828 bool coro_p = flag_coroutines
17829 && !processing_template_decl
17830 && DECL_COROUTINE_P (fndecl);
17831 bool coro_emit_helpers = false;
17832
17833 /* When we get some parse errors, we can end up without a
17834 current_function_decl, so cope. */
17835 if (fndecl == NULL_TREE)
17836 return error_mark_node;
17837
17838 if (!DECL_OMP_DECLARE_REDUCTION_P (fndecl))
17839 finish_lambda_scope ();
17840
17841 if (c_dialect_objc ())
17842 objc_finish_function ();
17843
17844 record_key_method_defined (fndecl);
17845
17846 fntype = TREE_TYPE (fndecl);
17847
17848 /* TREE_READONLY (fndecl) = 1;
17849 This caused &foo to be of type ptr-to-const-function
17850 which then got a warning when stored in a ptr-to-function variable. */
17851
17852 gcc_assert (building_stmt_list_p ());
17853 /* The current function is being defined, so its DECL_INITIAL should
17854 be set, and unless there's a multiple definition, it should be
17855 error_mark_node. */
17856 gcc_assert (DECL_INITIAL (fndecl) == error_mark_node);
17857
17858 if (coro_p)
17859 {
17860 /* Only try to emit the coroutine outlined helper functions if the
17861 transforms succeeded. Otherwise, treat errors in the same way as
17862 a regular function. */
17863 coro_emit_helpers = morph_fn_to_coro (fndecl, &resumer, &destroyer);
17864
17865 /* We should handle coroutine IFNs in middle end lowering. */
17866 cfun->coroutine_component = true;
17867
17868 /* Do not try to process the ramp's EH unless outlining succeeded. */
17869 if (coro_emit_helpers && use_eh_spec_block (fndecl))
17870 finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
17871 (TREE_TYPE (fndecl)),
17872 current_eh_spec_block);
17873 }
17874 else
17875 /* For a cloned function, we've already got all the code we need;
17876 there's no need to add any extra bits. */
17877 if (!DECL_CLONED_FUNCTION_P (fndecl))
17878 {
17879 /* Make it so that `main' always returns 0 by default. */
17880 if (DECL_MAIN_P (current_function_decl))
17881 finish_return_stmt (integer_zero_node);
17882
17883 if (use_eh_spec_block (current_function_decl))
17884 finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
17885 (TREE_TYPE (current_function_decl)),
17886 current_eh_spec_block);
17887 }
17888
17889 /* If we're saving up tree structure, tie off the function now. */
17890 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
17891
17892 finish_fname_decls ();
17893
17894 /* If this function can't throw any exceptions, remember that. */
17895 if (!processing_template_decl
17896 && !cp_function_chain->can_throw
17897 && !flag_non_call_exceptions
17898 && !decl_replaceable_p (fndecl,
17899 opt_for_fn (fndecl, flag_semantic_interposition)))
17900 TREE_NOTHROW (fndecl) = 1;
17901
17902 /* This must come after expand_function_end because cleanups might
17903 have declarations (from inline functions) that need to go into
17904 this function's blocks. */
17905
17906 /* If the current binding level isn't the outermost binding level
17907 for this function, either there is a bug, or we have experienced
17908 syntax errors and the statement tree is malformed. */
17909 if (current_binding_level->kind != sk_function_parms)
17910 {
17911 /* Make sure we have already experienced errors. */
17912 gcc_assert (errorcount);
17913
17914 /* Throw away the broken statement tree and extra binding
17915 levels. */
17916 DECL_SAVED_TREE (fndecl) = alloc_stmt_list ();
17917
17918 while (current_binding_level->kind != sk_function_parms)
17919 {
17920 if (current_binding_level->kind == sk_class)
17921 pop_nested_class ();
17922 else
17923 poplevel (0, 0, 0);
17924 }
17925 }
17926 poplevel (1, 0, 1);
17927
17928 /* Statements should always be full-expressions at the outermost set
17929 of curly braces for a function. */
17930 gcc_assert (stmts_are_full_exprs_p ());
17931
17932 /* If there are no return statements in a function with auto return type,
17933 the return type is void. But if the declared type is something like
17934 auto*, this is an error. */
17935 if (!processing_template_decl && FNDECL_USED_AUTO (fndecl)
17936 && TREE_TYPE (fntype) == DECL_SAVED_AUTO_RETURN_TYPE (fndecl))
17937 {
17938 if (is_auto (DECL_SAVED_AUTO_RETURN_TYPE (fndecl))
17939 && !current_function_returns_value
17940 && !current_function_returns_null)
17941 {
17942 /* We haven't applied return type deduction because we haven't
17943 seen any return statements. Do that now. */
17944 tree node = type_uses_auto (DECL_SAVED_AUTO_RETURN_TYPE (fndecl));
17945 do_auto_deduction (DECL_SAVED_AUTO_RETURN_TYPE (fndecl),
17946 void_node, node, tf_warning_or_error,
17947 adc_return_type);
17948
17949 apply_deduced_return_type (fndecl, void_type_node);
17950 fntype = TREE_TYPE (fndecl);
17951 }
17952 else if (!current_function_returns_value
17953 && !current_function_returns_null)
17954 {
17955 error ("no return statements in function returning %qT",
17956 DECL_SAVED_AUTO_RETURN_TYPE (fndecl));
17957 inform (input_location, "only plain %<auto%> return type can be "
17958 "deduced to %<void%>");
17959 }
17960 }
17961
17962 /* Remember that we were in class scope. */
17963 if (current_class_name)
17964 ctype = current_class_type;
17965
17966 if (DECL_DELETED_FN (fndecl))
17967 {
17968 DECL_INITIAL (fndecl) = error_mark_node;
17969 DECL_SAVED_TREE (fndecl) = NULL_TREE;
17970 goto cleanup;
17971 }
17972
17973 // If this is a concept, check that the definition is reasonable.
17974 if (DECL_DECLARED_CONCEPT_P (fndecl))
17975 check_function_concept (fndecl);
17976
17977 if (flag_openmp)
17978 if (tree attr = lookup_attribute ("omp declare variant base",
17979 DECL_ATTRIBUTES (fndecl)))
17980 omp_declare_variant_finalize (fndecl, attr);
17981
17982 /* Complain if there's just no return statement. */
17983 if ((warn_return_type
17984 || (cxx_dialect >= cxx14
17985 && DECL_DECLARED_CONSTEXPR_P (fndecl)))
17986 && !VOID_TYPE_P (TREE_TYPE (fntype))
17987 && !dependent_type_p (TREE_TYPE (fntype))
17988 && !current_function_returns_value && !current_function_returns_null
17989 /* Don't complain if we abort or throw. */
17990 && !current_function_returns_abnormally
17991 /* Don't complain if there's an infinite loop. */
17992 && !current_function_infinite_loop
17993 /* Don't complain if we are declared noreturn. */
17994 && !TREE_THIS_VOLATILE (fndecl)
17995 && !DECL_NAME (DECL_RESULT (fndecl))
17996 && !warning_suppressed_p (fndecl, OPT_Wreturn_type)
17997 /* Structor return values (if any) are set by the compiler. */
17998 && !DECL_CONSTRUCTOR_P (fndecl)
17999 && !DECL_DESTRUCTOR_P (fndecl)
18000 && targetm.warn_func_return (fndecl))
18001 {
18002 gcc_rich_location richloc (input_location);
18003 /* Potentially add a "return *this;" fix-it hint for
18004 assignment operators. */
18005 if (IDENTIFIER_ASSIGN_OP_P (DECL_NAME (fndecl)))
18006 {
18007 tree valtype = TREE_TYPE (DECL_RESULT (fndecl));
18008 if (TREE_CODE (valtype) == REFERENCE_TYPE
18009 && current_class_ref
18010 && same_type_ignoring_top_level_qualifiers_p
18011 (TREE_TYPE (valtype), TREE_TYPE (current_class_ref))
18012 && global_dc->option_enabled (OPT_Wreturn_type,
18013 global_dc->lang_mask,
18014 global_dc->option_state))
18015 add_return_star_this_fixit (&richloc, fndecl);
18016 }
18017 if (cxx_dialect >= cxx14
18018 && DECL_DECLARED_CONSTEXPR_P (fndecl))
18019 error_at (&richloc, "no return statement in %<constexpr%> function "
18020 "returning non-void");
18021 else if (warning_at (&richloc, OPT_Wreturn_type,
18022 "no return statement in function returning "
18023 "non-void"))
18024 suppress_warning (fndecl, OPT_Wreturn_type);
18025 }
18026
18027 /* Lambda closure members are implicitly constexpr if possible. */
18028 if (cxx_dialect >= cxx17
18029 && LAMBDA_TYPE_P (CP_DECL_CONTEXT (fndecl)))
18030 DECL_DECLARED_CONSTEXPR_P (fndecl)
18031 = ((processing_template_decl
18032 || is_valid_constexpr_fn (fndecl, /*complain*/false))
18033 && potential_constant_expression (DECL_SAVED_TREE (fndecl)));
18034
18035 /* Save constexpr function body before it gets munged by
18036 the NRV transformation. */
18037 maybe_save_constexpr_fundef (fndecl);
18038
18039 /* Invoke the pre-genericize plugin before we start munging things. */
18040 if (!processing_template_decl)
18041 invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE, fndecl);
18042
18043 /* Perform delayed folding before NRV transformation. */
18044 if (!processing_template_decl
18045 && !DECL_IMMEDIATE_FUNCTION_P (fndecl)
18046 && !DECL_OMP_DECLARE_REDUCTION_P (fndecl))
18047 cp_fold_function (fndecl);
18048
18049 /* Set up the named return value optimization, if we can. Candidate
18050 variables are selected in check_return_expr. */
18051 if (current_function_return_value)
18052 {
18053 tree r = current_function_return_value;
18054 tree outer;
18055
18056 if (r != error_mark_node
18057 /* This is only worth doing for fns that return in memory--and
18058 simpler, since we don't have to worry about promoted modes. */
18059 && aggregate_value_p (TREE_TYPE (TREE_TYPE (fndecl)), fndecl)
18060 /* Only allow this for variables declared in the outer scope of
18061 the function so we know that their lifetime always ends with a
18062 return; see g++.dg/opt/nrv6.C. We could be more flexible if
18063 we were to do this optimization in tree-ssa. */
18064 && (outer = outer_curly_brace_block (fndecl))
18065 && chain_member (r, BLOCK_VARS (outer)))
18066 finalize_nrv (&DECL_SAVED_TREE (fndecl), r, DECL_RESULT (fndecl));
18067
18068 current_function_return_value = NULL_TREE;
18069 }
18070
18071 /* Must mark the RESULT_DECL as being in this function. */
18072 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
18073
18074 /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point
18075 to the FUNCTION_DECL node itself. */
18076 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
18077
18078 /* Store the end of the function, so that we get good line number
18079 info for the epilogue. */
18080 cfun->function_end_locus = input_location;
18081
18082 /* Complain about parameters that are only set, but never otherwise used. */
18083 if (warn_unused_but_set_parameter
18084 && !processing_template_decl
18085 && errorcount == unused_but_set_errorcount
18086 && !DECL_CLONED_FUNCTION_P (fndecl))
18087 {
18088 tree decl;
18089
18090 for (decl = DECL_ARGUMENTS (fndecl);
18091 decl;
18092 decl = DECL_CHAIN (decl))
18093 if (TREE_USED (decl)
18094 && TREE_CODE (decl) == PARM_DECL
18095 && !DECL_READ_P (decl)
18096 && DECL_NAME (decl)
18097 && !DECL_ARTIFICIAL (decl)
18098 && !warning_suppressed_p (decl,OPT_Wunused_but_set_parameter)
18099 && !DECL_IN_SYSTEM_HEADER (decl)
18100 && TREE_TYPE (decl) != error_mark_node
18101 && !TYPE_REF_P (TREE_TYPE (decl))
18102 && (!CLASS_TYPE_P (TREE_TYPE (decl))
18103 || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl))))
18104 warning_at (DECL_SOURCE_LOCATION (decl),
18105 OPT_Wunused_but_set_parameter,
18106 "parameter %qD set but not used", decl);
18107 unused_but_set_errorcount = errorcount;
18108 }
18109
18110 /* Complain about locally defined typedefs that are not used in this
18111 function. */
18112 maybe_warn_unused_local_typedefs ();
18113
18114 /* Possibly warn about unused parameters. */
18115 if (warn_unused_parameter
18116 && !processing_template_decl
18117 && !DECL_CLONED_FUNCTION_P (fndecl))
18118 do_warn_unused_parameter (fndecl);
18119
18120 /* Genericize before inlining. */
18121 if (!processing_template_decl
18122 && !DECL_IMMEDIATE_FUNCTION_P (fndecl)
18123 && !DECL_OMP_DECLARE_REDUCTION_P (fndecl))
18124 cp_genericize (fndecl);
18125
18126 /* Emit the resumer and destroyer functions now, providing that we have
18127 not encountered some fatal error. */
18128 if (coro_emit_helpers)
18129 {
18130 emit_coro_helper (resumer);
18131 emit_coro_helper (destroyer);
18132 }
18133
18134 cleanup:
18135 /* We're leaving the context of this function, so zap cfun. It's still in
18136 DECL_STRUCT_FUNCTION, and we'll restore it in tree_rest_of_compilation. */
18137 set_cfun (NULL);
18138 current_function_decl = NULL;
18139
18140 /* If this is an in-class inline definition, we may have to pop the
18141 bindings for the template parameters that we added in
18142 maybe_begin_member_template_processing when start_function was
18143 called. */
18144 if (inline_p)
18145 maybe_end_member_template_processing ();
18146
18147 /* Leave the scope of the class. */
18148 if (ctype)
18149 pop_nested_class ();
18150
18151 --function_depth;
18152
18153 /* Clean up. */
18154 current_function_decl = NULL_TREE;
18155
18156 invoke_plugin_callbacks (PLUGIN_FINISH_PARSE_FUNCTION, fndecl);
18157 return fndecl;
18158 }
18159
18160 /* Create the FUNCTION_DECL for a function definition.
18161 DECLSPECS and DECLARATOR are the parts of the declaration;
18162 they describe the return type and the name of the function,
18163 but twisted together in a fashion that parallels the syntax of C.
18164
18165 This function creates a binding context for the function body
18166 as well as setting up the FUNCTION_DECL in current_function_decl.
18167
18168 Returns a FUNCTION_DECL on success.
18169
18170 If the DECLARATOR is not suitable for a function (it defines a datum
18171 instead), we return 0, which tells yyparse to report a parse error.
18172
18173 May return void_type_node indicating that this method is actually
18174 a friend. See grokfield for more details.
18175
18176 Came here with a `.pushlevel' .
18177
18178 DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
18179 CHANGES TO CODE IN `grokfield'. */
18180
18181 tree
grokmethod(cp_decl_specifier_seq * declspecs,const cp_declarator * declarator,tree attrlist)18182 grokmethod (cp_decl_specifier_seq *declspecs,
18183 const cp_declarator *declarator, tree attrlist)
18184 {
18185 tree fndecl = grokdeclarator (declarator, declspecs, MEMFUNCDEF, 0,
18186 &attrlist);
18187
18188 if (fndecl == error_mark_node)
18189 return error_mark_node;
18190
18191 if (attrlist)
18192 cplus_decl_attributes (&fndecl, attrlist, 0);
18193
18194 /* Pass friends other than inline friend functions back. */
18195 if (fndecl == void_type_node)
18196 return fndecl;
18197
18198 if (DECL_IN_AGGR_P (fndecl))
18199 {
18200 if (DECL_CLASS_SCOPE_P (fndecl))
18201 error ("%qD is already defined in class %qT", fndecl,
18202 DECL_CONTEXT (fndecl));
18203 return error_mark_node;
18204 }
18205
18206 check_template_shadow (fndecl);
18207
18208 /* p1779 ABI-Isolation makes inline not a default for in-class
18209 definitions in named module purview. If the user explicitly
18210 made it inline, grokdeclarator will already have done the right
18211 things. */
18212 if ((!named_module_purview_p ()
18213 || flag_module_implicit_inline
18214 /* Lambda's operator function remains inline. */
18215 || LAMBDA_TYPE_P (DECL_CONTEXT (fndecl)))
18216 /* If the user explicitly asked for this to be inline, we don't
18217 need to do more, but more importantly we want to warn if we
18218 can't inline it. */
18219 && !DECL_DECLARED_INLINE_P (fndecl))
18220 {
18221 if (TREE_PUBLIC (fndecl))
18222 DECL_COMDAT (fndecl) = 1;
18223 DECL_DECLARED_INLINE_P (fndecl) = 1;
18224 /* It's ok if we can't inline this. */
18225 DECL_NO_INLINE_WARNING_P (fndecl) = 1;
18226 }
18227
18228 /* We process method specializations in finish_struct_1. */
18229 if (processing_template_decl && !DECL_TEMPLATE_SPECIALIZATION (fndecl))
18230 {
18231 /* Avoid calling decl_spec_seq... until we have to. */
18232 bool friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
18233 fndecl = push_template_decl (fndecl, friendp);
18234 if (fndecl == error_mark_node)
18235 return fndecl;
18236 }
18237
18238 if (DECL_CHAIN (fndecl) && !decl_spec_seq_has_spec_p (declspecs, ds_friend))
18239 {
18240 fndecl = copy_node (fndecl);
18241 TREE_CHAIN (fndecl) = NULL_TREE;
18242 }
18243
18244 cp_finish_decl (fndecl, NULL_TREE, false, NULL_TREE, 0);
18245
18246 DECL_IN_AGGR_P (fndecl) = 1;
18247 return fndecl;
18248 }
18249
18250
18251 /* VAR is a VAR_DECL. If its type is incomplete, remember VAR so that
18252 we can lay it out later, when and if its type becomes complete.
18253
18254 Also handle constexpr variables where the initializer involves
18255 an unlowered PTRMEM_CST because the class isn't complete yet. */
18256
18257 void
maybe_register_incomplete_var(tree var)18258 maybe_register_incomplete_var (tree var)
18259 {
18260 gcc_assert (VAR_P (var));
18261
18262 /* Keep track of variables with incomplete types. */
18263 if (!processing_template_decl && TREE_TYPE (var) != error_mark_node
18264 && DECL_EXTERNAL (var))
18265 {
18266 tree inner_type = TREE_TYPE (var);
18267
18268 while (TREE_CODE (inner_type) == ARRAY_TYPE)
18269 inner_type = TREE_TYPE (inner_type);
18270 inner_type = TYPE_MAIN_VARIANT (inner_type);
18271
18272 if ((!COMPLETE_TYPE_P (inner_type) && CLASS_TYPE_P (inner_type))
18273 /* RTTI TD entries are created while defining the type_info. */
18274 || (TYPE_LANG_SPECIFIC (inner_type)
18275 && TYPE_BEING_DEFINED (inner_type)))
18276 {
18277 incomplete_var iv = {var, inner_type};
18278 vec_safe_push (incomplete_vars, iv);
18279 }
18280 else if (!(DECL_LANG_SPECIFIC (var) && DECL_TEMPLATE_INFO (var))
18281 && decl_constant_var_p (var)
18282 && (TYPE_PTRMEM_P (inner_type) || CLASS_TYPE_P (inner_type)))
18283 {
18284 /* When the outermost open class is complete we can resolve any
18285 pointers-to-members. */
18286 tree context = outermost_open_class ();
18287 incomplete_var iv = {var, context};
18288 vec_safe_push (incomplete_vars, iv);
18289 }
18290 }
18291 }
18292
18293 /* Called when a class type (given by TYPE) is defined. If there are
18294 any existing VAR_DECLs whose type has been completed by this
18295 declaration, update them now. */
18296
18297 void
complete_vars(tree type)18298 complete_vars (tree type)
18299 {
18300 unsigned ix;
18301 incomplete_var *iv;
18302
18303 for (ix = 0; vec_safe_iterate (incomplete_vars, ix, &iv); )
18304 {
18305 if (same_type_p (type, iv->incomplete_type))
18306 {
18307 tree var = iv->decl;
18308 tree type = TREE_TYPE (var);
18309
18310 if (type != error_mark_node
18311 && (TYPE_MAIN_VARIANT (strip_array_types (type))
18312 == iv->incomplete_type))
18313 {
18314 /* Complete the type of the variable. */
18315 complete_type (type);
18316 cp_apply_type_quals_to_decl (cp_type_quals (type), var);
18317 if (COMPLETE_TYPE_P (type))
18318 layout_var_decl (var);
18319 }
18320
18321 /* Remove this entry from the list. */
18322 incomplete_vars->unordered_remove (ix);
18323 }
18324 else
18325 ix++;
18326 }
18327 }
18328
18329 /* If DECL is of a type which needs a cleanup, build and return an
18330 expression to perform that cleanup here. Return NULL_TREE if no
18331 cleanup need be done. DECL can also be a _REF when called from
18332 split_nonconstant_init_1. */
18333
18334 tree
cxx_maybe_build_cleanup(tree decl,tsubst_flags_t complain)18335 cxx_maybe_build_cleanup (tree decl, tsubst_flags_t complain)
18336 {
18337 tree type;
18338 tree attr;
18339 tree cleanup;
18340
18341 /* Assume no cleanup is required. */
18342 cleanup = NULL_TREE;
18343
18344 if (error_operand_p (decl))
18345 return cleanup;
18346
18347 /* Handle "__attribute__((cleanup))". We run the cleanup function
18348 before the destructor since the destructor is what actually
18349 terminates the lifetime of the object. */
18350 if (DECL_P (decl))
18351 attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
18352 else
18353 attr = NULL_TREE;
18354 if (attr)
18355 {
18356 tree id;
18357 tree fn;
18358 tree arg;
18359
18360 /* Get the name specified by the user for the cleanup function. */
18361 id = TREE_VALUE (TREE_VALUE (attr));
18362 /* Look up the name to find the cleanup function to call. It is
18363 important to use lookup_name here because that is what is
18364 used in c-common.cc:handle_cleanup_attribute when performing
18365 initial checks on the attribute. Note that those checks
18366 include ensuring that the function found is not an overloaded
18367 function, or an object with an overloaded call operator,
18368 etc.; we can rely on the fact that the function found is an
18369 ordinary FUNCTION_DECL. */
18370 fn = lookup_name (id);
18371 arg = build_address (decl);
18372 if (!mark_used (decl, complain) && !(complain & tf_error))
18373 return error_mark_node;
18374 cleanup = cp_build_function_call_nary (fn, complain, arg, NULL_TREE);
18375 if (cleanup == error_mark_node)
18376 return error_mark_node;
18377 }
18378 /* Handle ordinary C++ destructors. */
18379 type = TREE_TYPE (decl);
18380 if (type_build_dtor_call (type))
18381 {
18382 int flags = LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR;
18383 tree addr;
18384 tree call;
18385
18386 if (TREE_CODE (type) == ARRAY_TYPE)
18387 addr = decl;
18388 else
18389 addr = build_address (decl);
18390
18391 call = build_delete (input_location, TREE_TYPE (addr), addr,
18392 sfk_complete_destructor, flags, 0, complain);
18393 if (call == error_mark_node)
18394 cleanup = error_mark_node;
18395 else if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
18396 /* Discard the call. */;
18397 else if (decl_maybe_constant_destruction (decl, type)
18398 && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
18399 cxx_constant_dtor (call, decl);
18400 else if (cleanup)
18401 cleanup = cp_build_compound_expr (cleanup, call, complain);
18402 else
18403 cleanup = call;
18404 }
18405
18406 /* build_delete sets the location of the destructor call to the
18407 current location, even though the destructor is going to be
18408 called later, at the end of the current scope. This can lead to
18409 a "jumpy" behavior for users of debuggers when they step around
18410 the end of the block. So let's unset the location of the
18411 destructor call instead. */
18412 protected_set_expr_location (cleanup, UNKNOWN_LOCATION);
18413 if (cleanup && CONVERT_EXPR_P (cleanup))
18414 protected_set_expr_location (TREE_OPERAND (cleanup, 0), UNKNOWN_LOCATION);
18415
18416 if (cleanup
18417 && DECL_P (decl)
18418 && !lookup_attribute ("warn_unused", TYPE_ATTRIBUTES (TREE_TYPE (decl)))
18419 /* Treat objects with destructors as used; the destructor may do
18420 something substantive. */
18421 && !mark_used (decl, complain) && !(complain & tf_error))
18422 return error_mark_node;
18423
18424 if (cleanup && cfun && !processing_template_decl
18425 && !expr_noexcept_p (cleanup, tf_none))
18426 cp_function_chain->throwing_cleanup = true;
18427
18428 return cleanup;
18429 }
18430
18431
18432 /* Return the FUNCTION_TYPE that corresponds to MEMFNTYPE, which can be a
18433 FUNCTION_DECL, METHOD_TYPE, FUNCTION_TYPE, pointer or reference to
18434 METHOD_TYPE or FUNCTION_TYPE, or pointer to member function. */
18435
18436 tree
static_fn_type(tree memfntype)18437 static_fn_type (tree memfntype)
18438 {
18439 tree fntype;
18440 tree args;
18441
18442 if (TYPE_PTRMEMFUNC_P (memfntype))
18443 memfntype = TYPE_PTRMEMFUNC_FN_TYPE (memfntype);
18444 if (INDIRECT_TYPE_P (memfntype)
18445 || TREE_CODE (memfntype) == FUNCTION_DECL)
18446 memfntype = TREE_TYPE (memfntype);
18447 if (TREE_CODE (memfntype) == FUNCTION_TYPE)
18448 return memfntype;
18449 gcc_assert (TREE_CODE (memfntype) == METHOD_TYPE);
18450 args = TYPE_ARG_TYPES (memfntype);
18451 fntype = build_function_type (TREE_TYPE (memfntype), TREE_CHAIN (args));
18452 fntype = apply_memfn_quals (fntype, type_memfn_quals (memfntype));
18453 fntype = (cp_build_type_attribute_variant
18454 (fntype, TYPE_ATTRIBUTES (memfntype)));
18455 fntype = cxx_copy_lang_qualifiers (fntype, memfntype);
18456 return fntype;
18457 }
18458
18459 /* DECL was originally constructed as a non-static member function,
18460 but turned out to be static. Update it accordingly. */
18461
18462 void
revert_static_member_fn(tree decl)18463 revert_static_member_fn (tree decl)
18464 {
18465 tree stype = static_fn_type (decl);
18466 cp_cv_quals quals = type_memfn_quals (stype);
18467 cp_ref_qualifier rqual = type_memfn_rqual (stype);
18468
18469 if (quals != TYPE_UNQUALIFIED || rqual != REF_QUAL_NONE)
18470 stype = apply_memfn_quals (stype, TYPE_UNQUALIFIED, REF_QUAL_NONE);
18471
18472 TREE_TYPE (decl) = stype;
18473
18474 if (DECL_ARGUMENTS (decl))
18475 DECL_ARGUMENTS (decl) = DECL_CHAIN (DECL_ARGUMENTS (decl));
18476 DECL_STATIC_FUNCTION_P (decl) = 1;
18477 }
18478
18479 /* Return which tree structure is used by T, or TS_CP_GENERIC if T is
18480 one of the language-independent trees. */
18481
18482 enum cp_tree_node_structure_enum
cp_tree_node_structure(union lang_tree_node * t)18483 cp_tree_node_structure (union lang_tree_node * t)
18484 {
18485 switch (TREE_CODE (&t->generic))
18486 {
18487 case ARGUMENT_PACK_SELECT: return TS_CP_ARGUMENT_PACK_SELECT;
18488 case BASELINK: return TS_CP_BASELINK;
18489 case CONSTRAINT_INFO: return TS_CP_CONSTRAINT_INFO;
18490 case DEFERRED_NOEXCEPT: return TS_CP_DEFERRED_NOEXCEPT;
18491 case DEFERRED_PARSE: return TS_CP_DEFERRED_PARSE;
18492 case IDENTIFIER_NODE: return TS_CP_IDENTIFIER;
18493 case LAMBDA_EXPR: return TS_CP_LAMBDA_EXPR;
18494 case BINDING_VECTOR: return TS_CP_BINDING_VECTOR;
18495 case OVERLOAD: return TS_CP_OVERLOAD;
18496 case PTRMEM_CST: return TS_CP_PTRMEM;
18497 case STATIC_ASSERT: return TS_CP_STATIC_ASSERT;
18498 case TEMPLATE_DECL: return TS_CP_TEMPLATE_DECL;
18499 case TEMPLATE_INFO: return TS_CP_TEMPLATE_INFO;
18500 case TEMPLATE_PARM_INDEX: return TS_CP_TPI;
18501 case TRAIT_EXPR: return TS_CP_TRAIT_EXPR;
18502 case USERDEF_LITERAL: return TS_CP_USERDEF_LITERAL;
18503 default: return TS_CP_GENERIC;
18504 }
18505 }
18506
18507 /* Build the void_list_node (void_type_node having been created). */
18508 tree
build_void_list_node(void)18509 build_void_list_node (void)
18510 {
18511 tree t = build_tree_list (NULL_TREE, void_type_node);
18512 return t;
18513 }
18514
18515 bool
cp_missing_noreturn_ok_p(tree decl)18516 cp_missing_noreturn_ok_p (tree decl)
18517 {
18518 /* A missing noreturn is ok for the `main' function. */
18519 return DECL_MAIN_P (decl);
18520 }
18521
18522 /* Return the decl used to identify the COMDAT group into which DECL should
18523 be placed. */
18524
18525 tree
cxx_comdat_group(tree decl)18526 cxx_comdat_group (tree decl)
18527 {
18528 /* Virtual tables, construction virtual tables, and virtual table
18529 tables all go in a single COMDAT group, named after the primary
18530 virtual table. */
18531 if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl))
18532 decl = CLASSTYPE_VTABLES (DECL_CONTEXT (decl));
18533 /* For all other DECLs, the COMDAT group is the mangled name of the
18534 declaration itself. */
18535 else
18536 {
18537 while (DECL_THUNK_P (decl))
18538 {
18539 /* If TARGET_USE_LOCAL_THUNK_ALIAS_P, use_thunk puts the thunk
18540 into the same section as the target function. In that case
18541 we must return target's name. */
18542 tree target = THUNK_TARGET (decl);
18543 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (target)
18544 && DECL_SECTION_NAME (target) != NULL
18545 && DECL_ONE_ONLY (target))
18546 decl = target;
18547 else
18548 break;
18549 }
18550 }
18551
18552 return decl;
18553 }
18554
18555 /* Returns the return type for FN as written by the user, which may include
18556 a placeholder for a deduced return type. */
18557
18558 tree
fndecl_declared_return_type(tree fn)18559 fndecl_declared_return_type (tree fn)
18560 {
18561 fn = STRIP_TEMPLATE (fn);
18562 if (FNDECL_USED_AUTO (fn))
18563 return DECL_SAVED_AUTO_RETURN_TYPE (fn);
18564
18565 return TREE_TYPE (TREE_TYPE (fn));
18566 }
18567
18568 /* Returns true iff DECL is a variable or function declared with an auto type
18569 that has not yet been deduced to a real type. */
18570
18571 bool
undeduced_auto_decl(tree decl)18572 undeduced_auto_decl (tree decl)
18573 {
18574 if (cxx_dialect < cxx11)
18575 return false;
18576 STRIP_ANY_LOCATION_WRAPPER (decl);
18577 return ((VAR_OR_FUNCTION_DECL_P (decl)
18578 || TREE_CODE (decl) == TEMPLATE_DECL)
18579 && type_uses_auto (TREE_TYPE (decl)));
18580 }
18581
18582 /* Complain if DECL has an undeduced return type. */
18583
18584 bool
require_deduced_type(tree decl,tsubst_flags_t complain)18585 require_deduced_type (tree decl, tsubst_flags_t complain)
18586 {
18587 if (undeduced_auto_decl (decl))
18588 {
18589 if (warning_suppressed_p (decl) && seen_error ())
18590 /* We probably already complained about deduction failure. */;
18591 else if (complain & tf_error)
18592 error ("use of %qD before deduction of %<auto%>", decl);
18593 note_failed_type_completion_for_satisfaction (decl);
18594 return false;
18595 }
18596 return true;
18597 }
18598
18599 /* Create a representation of the explicit-specifier with
18600 constant-expression of EXPR. COMPLAIN is as for tsubst. */
18601
18602 tree
build_explicit_specifier(tree expr,tsubst_flags_t complain)18603 build_explicit_specifier (tree expr, tsubst_flags_t complain)
18604 {
18605 if (check_for_bare_parameter_packs (expr))
18606 return error_mark_node;
18607
18608 if (instantiation_dependent_expression_p (expr))
18609 /* Wait for instantiation, tsubst_function_decl will handle it. */
18610 return expr;
18611
18612 expr = build_converted_constant_bool_expr (expr, complain);
18613 expr = instantiate_non_dependent_expr_sfinae (expr, complain);
18614 expr = cxx_constant_value (expr);
18615 return expr;
18616 }
18617
18618 #include "gt-cp-decl.h"
18619