xref: /netbsd/external/gpl3/gcc/dist/gcc/cp/decl.c (revision 1695e785)
1 /* Process declarations and variables for -*- C++ -*- compiler.
2    Copyright (C) 1988-2020 Free Software Foundation, Inc.
3    Contributed by Michael Tiemann (tiemann@cygnus.com)
4 
5 This file is part of GCC.
6 
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
11 
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3.  If not see
19 <http://www.gnu.org/licenses/>.  */
20 
21 
22 /* Process declarations and symbol lookup for C++ front end.
23    Also constructs types; the standard scalar types at initialization,
24    and structure, union, array and enum types when they are declared.  */
25 
26 /* ??? not all decl nodes are given the most useful possible
27    line numbers.  For example, the CONST_DECLs for enum values.  */
28 
29 #include "config.h"
30 #include "system.h"
31 #include "coretypes.h"
32 #include "target.h"
33 #include "c-family/c-target.h"
34 #include "cp-tree.h"
35 #include "timevar.h"
36 #include "stringpool.h"
37 #include "cgraph.h"
38 #include "stor-layout.h"
39 #include "varasm.h"
40 #include "attribs.h"
41 #include "flags.h"
42 #include "tree-iterator.h"
43 #include "decl.h"
44 #include "intl.h"
45 #include "toplev.h"
46 #include "c-family/c-objc.h"
47 #include "c-family/c-pragma.h"
48 #include "c-family/c-ubsan.h"
49 #include "debug.h"
50 #include "plugin.h"
51 #include "builtins.h"
52 #include "gimplify.h"
53 #include "asan.h"
54 #include "gcc-rich-location.h"
55 #include "langhooks.h"
56 #include "omp-general.h"
57 
58 /* Possible cases of bad specifiers type used by bad_specifiers. */
59 enum bad_spec_place {
60   BSP_VAR,    /* variable */
61   BSP_PARM,   /* parameter */
62   BSP_TYPE,   /* type */
63   BSP_FIELD   /* field */
64 };
65 
66 static const char *redeclaration_error_message (tree, tree);
67 
68 static int decl_jump_unsafe (tree);
69 static void require_complete_types_for_parms (tree);
70 static tree grok_reference_init (tree, tree, tree, int);
71 static tree grokvardecl (tree, tree, tree, const cp_decl_specifier_seq *,
72 			 int, int, int, bool, int, tree, location_t);
73 static void check_static_variable_definition (tree, tree);
74 static void record_unknown_type (tree, const char *);
75 static int member_function_or_else (tree, tree, enum overload_flags);
76 static tree local_variable_p_walkfn (tree *, int *, void *);
77 static const char *tag_name (enum tag_types);
78 static tree lookup_and_check_tag (enum tag_types, tree, tag_scope, bool);
79 static void maybe_deduce_size_from_array_init (tree, tree);
80 static void layout_var_decl (tree);
81 static tree check_initializer (tree, tree, int, vec<tree, va_gc> **);
82 static void make_rtl_for_nonlocal_decl (tree, tree, const char *);
83 static void copy_type_enum (tree , tree);
84 static void check_function_type (tree, tree);
85 static void finish_constructor_body (void);
86 static void begin_destructor_body (void);
87 static void finish_destructor_body (void);
88 static void record_key_method_defined (tree);
89 static tree create_array_type_for_decl (tree, tree, tree, location_t);
90 static tree get_atexit_node (void);
91 static tree get_dso_handle_node (void);
92 static tree start_cleanup_fn (void);
93 static void end_cleanup_fn (void);
94 static tree cp_make_fname_decl (location_t, tree, int);
95 static void initialize_predefined_identifiers (void);
96 static tree check_special_function_return_type
97        (special_function_kind, tree, tree, int, const location_t*);
98 static tree push_cp_library_fn (enum tree_code, tree, int);
99 static tree build_cp_library_fn (tree, enum tree_code, tree, int);
100 static void store_parm_decls (tree);
101 static void initialize_local_var (tree, tree);
102 static void expand_static_init (tree, tree);
103 static location_t smallest_type_location (const cp_decl_specifier_seq*);
104 
105 /* The following symbols are subsumed in the cp_global_trees array, and
106    listed here individually for documentation purposes.
107 
108    C++ extensions
109 	tree wchar_decl_node;
110 
111 	tree vtable_entry_type;
112 	tree delta_type_node;
113 	tree __t_desc_type_node;
114 
115 	tree class_type_node;
116 	tree unknown_type_node;
117 
118    Array type `vtable_entry_type[]'
119 
120 	tree vtbl_type_node;
121 	tree vtbl_ptr_type_node;
122 
123    Namespaces,
124 
125 	tree std_node;
126 	tree abi_node;
127 
128    A FUNCTION_DECL which can call `abort'.  Not necessarily the
129    one that the user will declare, but sufficient to be called
130    by routines that want to abort the program.
131 
132 	tree abort_fndecl;
133 
134    Used by RTTI
135 	tree type_info_type_node, tinfo_decl_id, tinfo_decl_type;
136 	tree tinfo_var_id;  */
137 
138 tree cp_global_trees[CPTI_MAX];
139 
140 /* A list of objects which have constructors or destructors
141    which reside in the global scope.  The decl is stored in
142    the TREE_VALUE slot and the initializer is stored
143    in the TREE_PURPOSE slot.  */
144 tree static_aggregates;
145 
146 /* Like static_aggregates, but for thread_local variables.  */
147 tree tls_aggregates;
148 
149 /* -- end of C++ */
150 
151 /* A node for the integer constant 2.  */
152 
153 tree integer_two_node;
154 
155 /* vector of static decls.  */
156 vec<tree, va_gc> *static_decls;
157 
158 /* vector of keyed classes.  */
159 vec<tree, va_gc> *keyed_classes;
160 
161 /* Used only for jumps to as-yet undefined labels, since jumps to
162    defined labels can have their validity checked immediately.  */
163 
164 struct GTY((chain_next ("%h.next"))) named_label_use_entry {
165   struct named_label_use_entry *next;
166   /* The binding level to which this entry is *currently* attached.
167      This is initially the binding level in which the goto appeared,
168      but is modified as scopes are closed.  */
169   cp_binding_level *binding_level;
170   /* The head of the names list that was current when the goto appeared,
171      or the inner scope popped.  These are the decls that will *not* be
172      skipped when jumping to the label.  */
173   tree names_in_scope;
174   /* The location of the goto, for error reporting.  */
175   location_t o_goto_locus;
176   /* True if an OpenMP structured block scope has been closed since
177      the goto appeared.  This means that the branch from the label will
178      illegally exit an OpenMP scope.  */
179   bool in_omp_scope;
180 };
181 
182 /* A list of all LABEL_DECLs in the function that have names.  Here so
183    we can clear out their names' definitions at the end of the
184    function, and so we can check the validity of jumps to these labels.  */
185 
186 struct GTY((for_user)) named_label_entry {
187 
188   tree name;  /* Name of decl. */
189 
190   tree label_decl; /* LABEL_DECL, unless deleted local label. */
191 
192   named_label_entry *outer; /* Outer shadowed chain.  */
193 
194   /* The binding level to which the label is *currently* attached.
195      This is initially set to the binding level in which the label
196      is defined, but is modified as scopes are closed.  */
197   cp_binding_level *binding_level;
198 
199   /* The head of the names list that was current when the label was
200      defined, or the inner scope popped.  These are the decls that will
201      be skipped when jumping to the label.  */
202   tree names_in_scope;
203 
204   /* A vector of all decls from all binding levels that would be
205      crossed by a backward branch to the label.  */
206   vec<tree, va_gc> *bad_decls;
207 
208   /* A list of uses of the label, before the label is defined.  */
209   named_label_use_entry *uses;
210 
211   /* The following bits are set after the label is defined, and are
212      updated as scopes are popped.  They indicate that a jump to the
213      label will illegally enter a scope of the given flavor.  */
214   bool in_try_scope;
215   bool in_catch_scope;
216   bool in_omp_scope;
217   bool in_transaction_scope;
218   bool in_constexpr_if;
219 };
220 
221 #define named_labels cp_function_chain->x_named_labels
222 
223 /* The number of function bodies which we are currently processing.
224    (Zero if we are at namespace scope, one inside the body of a
225    function, two inside the body of a function in a local class, etc.)  */
226 int function_depth;
227 
228 /* Whether the exception-specifier is part of a function type (i.e. C++17).  */
229 bool flag_noexcept_type;
230 
231 /* States indicating how grokdeclarator() should handle declspecs marked
232    with __attribute__((deprecated)).  An object declared as
233    __attribute__((deprecated)) suppresses warnings of uses of other
234    deprecated items.  */
235 enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
236 
237 
238 /* A list of VAR_DECLs whose type was incomplete at the time the
239    variable was declared.  */
240 
241 struct GTY(()) incomplete_var {
242   tree decl;
243   tree incomplete_type;
244 };
245 
246 
247 static GTY(()) vec<incomplete_var, va_gc> *incomplete_vars;
248 
249 /* Returns the kind of template specialization we are currently
250    processing, given that it's declaration contained N_CLASS_SCOPES
251    explicit scope qualifications.  */
252 
253 tmpl_spec_kind
current_tmpl_spec_kind(int n_class_scopes)254 current_tmpl_spec_kind (int n_class_scopes)
255 {
256   int n_template_parm_scopes = 0;
257   int seen_specialization_p = 0;
258   int innermost_specialization_p = 0;
259   cp_binding_level *b;
260 
261   /* Scan through the template parameter scopes.  */
262   for (b = current_binding_level;
263        b->kind == sk_template_parms;
264        b = b->level_chain)
265     {
266       /* If we see a specialization scope inside a parameter scope,
267 	 then something is wrong.  That corresponds to a declaration
268 	 like:
269 
270 	    template <class T> template <> ...
271 
272 	 which is always invalid since [temp.expl.spec] forbids the
273 	 specialization of a class member template if the enclosing
274 	 class templates are not explicitly specialized as well.  */
275       if (b->explicit_spec_p)
276 	{
277 	  if (n_template_parm_scopes == 0)
278 	    innermost_specialization_p = 1;
279 	  else
280 	    seen_specialization_p = 1;
281 	}
282       else if (seen_specialization_p == 1)
283 	return tsk_invalid_member_spec;
284 
285       ++n_template_parm_scopes;
286     }
287 
288   /* Handle explicit instantiations.  */
289   if (processing_explicit_instantiation)
290     {
291       if (n_template_parm_scopes != 0)
292 	/* We've seen a template parameter list during an explicit
293 	   instantiation.  For example:
294 
295 	     template <class T> template void f(int);
296 
297 	   This is erroneous.  */
298 	return tsk_invalid_expl_inst;
299       else
300 	return tsk_expl_inst;
301     }
302 
303   if (n_template_parm_scopes < n_class_scopes)
304     /* We've not seen enough template headers to match all the
305        specialized classes present.  For example:
306 
307 	 template <class T> void R<T>::S<T>::f(int);
308 
309        This is invalid; there needs to be one set of template
310        parameters for each class.  */
311     return tsk_insufficient_parms;
312   else if (n_template_parm_scopes == n_class_scopes)
313     /* We're processing a non-template declaration (even though it may
314        be a member of a template class.)  For example:
315 
316 	 template <class T> void S<T>::f(int);
317 
318        The `class T' matches the `S<T>', leaving no template headers
319        corresponding to the `f'.  */
320     return tsk_none;
321   else if (n_template_parm_scopes > n_class_scopes + 1)
322     /* We've got too many template headers.  For example:
323 
324 	 template <> template <class T> void f (T);
325 
326        There need to be more enclosing classes.  */
327     return tsk_excessive_parms;
328   else
329     /* This must be a template.  It's of the form:
330 
331 	 template <class T> template <class U> void S<T>::f(U);
332 
333        This is a specialization if the innermost level was a
334        specialization; otherwise it's just a definition of the
335        template.  */
336     return innermost_specialization_p ? tsk_expl_spec : tsk_template;
337 }
338 
339 /* Exit the current scope.  */
340 
341 void
finish_scope(void)342 finish_scope (void)
343 {
344   poplevel (0, 0, 0);
345 }
346 
347 /* When a label goes out of scope, check to see if that label was used
348    in a valid manner, and issue any appropriate warnings or errors.  */
349 
350 static void
check_label_used(tree label)351 check_label_used (tree label)
352 {
353   if (!processing_template_decl)
354     {
355       if (DECL_INITIAL (label) == NULL_TREE)
356 	{
357 	  location_t location;
358 
359 	  error ("label %q+D used but not defined", label);
360 	  location = input_location;
361 	    /* FIXME want (LOCATION_FILE (input_location), (line)0) */
362 	  /* Avoid crashing later.  */
363 	  define_label (location, DECL_NAME (label));
364 	}
365       else
366 	warn_for_unused_label (label);
367     }
368 }
369 
370 /* Helper function to sort named label entries in a vector by DECL_UID.  */
371 
372 static int
sort_labels(const void * a,const void * b)373 sort_labels (const void *a, const void *b)
374 {
375   tree label1 = *(tree const *) a;
376   tree label2 = *(tree const *) b;
377 
378   /* DECL_UIDs can never be equal.  */
379   return DECL_UID (label1) > DECL_UID (label2) ? -1 : +1;
380 }
381 
382 /* At the end of a function, all labels declared within the function
383    go out of scope.  BLOCK is the top-level block for the
384    function.  */
385 
386 static void
pop_labels(tree block)387 pop_labels (tree block)
388 {
389   if (!named_labels)
390     return;
391 
392   /* We need to add the labels to the block chain, so debug
393      information is emitted.  But, we want the order to be stable so
394      need to sort them first.  Otherwise the debug output could be
395      randomly ordered.  I guess it's mostly stable, unless the hash
396      table implementation changes.  */
397   auto_vec<tree, 32> labels (named_labels->elements ());
398   hash_table<named_label_hash>::iterator end (named_labels->end ());
399   for (hash_table<named_label_hash>::iterator iter
400 	 (named_labels->begin ()); iter != end; ++iter)
401     {
402       named_label_entry *ent = *iter;
403 
404       gcc_checking_assert (!ent->outer);
405       if (ent->label_decl)
406 	labels.quick_push (ent->label_decl);
407       ggc_free (ent);
408     }
409   named_labels = NULL;
410   labels.qsort (sort_labels);
411 
412   while (labels.length ())
413     {
414       tree label = labels.pop ();
415 
416       DECL_CHAIN (label) = BLOCK_VARS (block);
417       BLOCK_VARS (block) = label;
418 
419       check_label_used (label);
420     }
421 }
422 
423 /* At the end of a block with local labels, restore the outer definition.  */
424 
425 static void
pop_local_label(tree id,tree label)426 pop_local_label (tree id, tree label)
427 {
428   check_label_used (label);
429   named_label_entry **slot = named_labels->find_slot_with_hash
430     (id, IDENTIFIER_HASH_VALUE (id), NO_INSERT);
431   named_label_entry *ent = *slot;
432 
433   if (ent->outer)
434     ent = ent->outer;
435   else
436     {
437       ent = ggc_cleared_alloc<named_label_entry> ();
438       ent->name = id;
439     }
440   *slot = ent;
441 }
442 
443 /* The following two routines are used to interface to Objective-C++.
444    The binding level is purposely treated as an opaque type.  */
445 
446 void *
objc_get_current_scope(void)447 objc_get_current_scope (void)
448 {
449   return current_binding_level;
450 }
451 
452 /* The following routine is used by the NeXT-style SJLJ exceptions;
453    variables get marked 'volatile' so as to not be clobbered by
454    _setjmp()/_longjmp() calls.  All variables in the current scope,
455    as well as parent scopes up to (but not including) ENCLOSING_BLK
456    shall be thusly marked.  */
457 
458 void
objc_mark_locals_volatile(void * enclosing_blk)459 objc_mark_locals_volatile (void *enclosing_blk)
460 {
461   cp_binding_level *scope;
462 
463   for (scope = current_binding_level;
464        scope && scope != enclosing_blk;
465        scope = scope->level_chain)
466     {
467       tree decl;
468 
469       for (decl = scope->names; decl; decl = TREE_CHAIN (decl))
470 	objc_volatilize_decl (decl);
471 
472       /* Do not climb up past the current function.  */
473       if (scope->kind == sk_function_parms)
474 	break;
475     }
476 }
477 
478 /* True if B is the level for the condition of a constexpr if.  */
479 
480 static bool
level_for_constexpr_if(cp_binding_level * b)481 level_for_constexpr_if (cp_binding_level *b)
482 {
483   return (b->kind == sk_cond && b->this_entity
484 	  && TREE_CODE (b->this_entity) == IF_STMT
485 	  && IF_STMT_CONSTEXPR_P (b->this_entity));
486 }
487 
488 /* Update data for defined and undefined labels when leaving a scope.  */
489 
490 int
poplevel_named_label_1(named_label_entry ** slot,cp_binding_level * bl)491 poplevel_named_label_1 (named_label_entry **slot, cp_binding_level *bl)
492 {
493   named_label_entry *ent = *slot;
494   cp_binding_level *obl = bl->level_chain;
495 
496   if (ent->binding_level == bl)
497     {
498       tree decl;
499 
500       /* ENT->NAMES_IN_SCOPE may contain a mixture of DECLs and
501 	 TREE_LISTs representing OVERLOADs, so be careful.  */
502       for (decl = ent->names_in_scope; decl; decl = (DECL_P (decl)
503 						     ? DECL_CHAIN (decl)
504 						     : TREE_CHAIN (decl)))
505 	if (decl_jump_unsafe (decl))
506 	  vec_safe_push (ent->bad_decls, decl);
507 
508       ent->binding_level = obl;
509       ent->names_in_scope = obl->names;
510       switch (bl->kind)
511 	{
512 	case sk_try:
513 	  ent->in_try_scope = true;
514 	  break;
515 	case sk_catch:
516 	  ent->in_catch_scope = true;
517 	  break;
518 	case sk_omp:
519 	  ent->in_omp_scope = true;
520 	  break;
521 	case sk_transaction:
522 	  ent->in_transaction_scope = true;
523 	  break;
524 	case sk_block:
525 	  if (level_for_constexpr_if (bl->level_chain))
526 	    ent->in_constexpr_if = true;
527 	  break;
528 	default:
529 	  break;
530 	}
531     }
532   else if (ent->uses)
533     {
534       struct named_label_use_entry *use;
535 
536       for (use = ent->uses; use ; use = use->next)
537 	if (use->binding_level == bl)
538 	  {
539 	    use->binding_level = obl;
540 	    use->names_in_scope = obl->names;
541 	    if (bl->kind == sk_omp)
542 	      use->in_omp_scope = true;
543 	  }
544     }
545 
546   return 1;
547 }
548 
549 /* Saved errorcount to avoid -Wunused-but-set-{parameter,variable} warnings
550    when errors were reported, except for -Werror-unused-but-set-*.  */
551 static int unused_but_set_errorcount;
552 
553 /* Exit a binding level.
554    Pop the level off, and restore the state of the identifier-decl mappings
555    that were in effect when this level was entered.
556 
557    If KEEP == 1, this level had explicit declarations, so
558    and create a "block" (a BLOCK node) for the level
559    to record its declarations and subblocks for symbol table output.
560 
561    If FUNCTIONBODY is nonzero, this level is the body of a function,
562    so create a block as if KEEP were set and also clear out all
563    label names.
564 
565    If REVERSE is nonzero, reverse the order of decls before putting
566    them into the BLOCK.  */
567 
568 tree
poplevel(int keep,int reverse,int functionbody)569 poplevel (int keep, int reverse, int functionbody)
570 {
571   tree link;
572   /* The chain of decls was accumulated in reverse order.
573      Put it into forward order, just for cleanliness.  */
574   tree decls;
575   tree subblocks;
576   tree block;
577   tree decl;
578   scope_kind kind;
579 
580   bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
581  restart:
582 
583   block = NULL_TREE;
584 
585   gcc_assert (current_binding_level->kind != sk_class
586 	      && current_binding_level->kind != sk_namespace);
587 
588   if (current_binding_level->kind == sk_cleanup)
589     functionbody = 0;
590   subblocks = functionbody >= 0 ? current_binding_level->blocks : 0;
591 
592   gcc_assert (!vec_safe_length (current_binding_level->class_shadowed));
593 
594   /* We used to use KEEP == 2 to indicate that the new block should go
595      at the beginning of the list of blocks at this binding level,
596      rather than the end.  This hack is no longer used.  */
597   gcc_assert (keep == 0 || keep == 1);
598 
599   if (current_binding_level->keep)
600     keep = 1;
601 
602   /* Any uses of undefined labels, and any defined labels, now operate
603      under constraints of next binding contour.  */
604   if (cfun && !functionbody && named_labels)
605     named_labels->traverse<cp_binding_level *, poplevel_named_label_1>
606 		   (current_binding_level);
607 
608   /* Get the decls in the order they were written.
609      Usually current_binding_level->names is in reverse order.
610      But parameter decls were previously put in forward order.  */
611 
612   decls = current_binding_level->names;
613   if (reverse)
614     {
615       decls = nreverse (decls);
616       current_binding_level->names = decls;
617     }
618 
619   /* If there were any declarations or structure tags in that level,
620      or if this level is a function body,
621      create a BLOCK to record them for the life of this function.  */
622   block = NULL_TREE;
623   /* Avoid function body block if possible.  */
624   if (functionbody && subblocks && BLOCK_CHAIN (subblocks) == NULL_TREE)
625     keep = 0;
626   else if (keep == 1 || functionbody)
627     block = make_node (BLOCK);
628   if (block != NULL_TREE)
629     {
630       BLOCK_VARS (block) = decls;
631       BLOCK_SUBBLOCKS (block) = subblocks;
632     }
633 
634   /* In each subblock, record that this is its superior.  */
635   if (keep >= 0)
636     for (link = subblocks; link; link = BLOCK_CHAIN (link))
637       BLOCK_SUPERCONTEXT (link) = block;
638 
639   /* Before we remove the declarations first check for unused variables.  */
640   if ((warn_unused_variable || warn_unused_but_set_variable)
641       && current_binding_level->kind != sk_template_parms
642       && !processing_template_decl)
643     for (tree d = get_local_decls (); d; d = TREE_CHAIN (d))
644       {
645 	/* There are cases where D itself is a TREE_LIST.  See in
646 	   push_local_binding where the list of decls returned by
647 	   getdecls is built.  */
648 	decl = TREE_CODE (d) == TREE_LIST ? TREE_VALUE (d) : d;
649 
650 	tree type = TREE_TYPE (decl);
651 	if (VAR_P (decl)
652 	    && (! TREE_USED (decl) || !DECL_READ_P (decl))
653 	    && ! DECL_IN_SYSTEM_HEADER (decl)
654 	    /* For structured bindings, consider only real variables, not
655 	       subobjects.  */
656 	    && (DECL_DECOMPOSITION_P (decl) ? !DECL_DECOMP_BASE (decl)
657 		: (DECL_NAME (decl) && !DECL_ARTIFICIAL (decl)))
658 	    && type != error_mark_node
659 	    && (!CLASS_TYPE_P (type)
660 		|| !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
661 		|| lookup_attribute ("warn_unused",
662 				     TYPE_ATTRIBUTES (TREE_TYPE (decl)))))
663 	  {
664 	    if (! TREE_USED (decl))
665 	      {
666 		if (!DECL_NAME (decl) && DECL_DECOMPOSITION_P (decl))
667 		  warning_at (DECL_SOURCE_LOCATION (decl),
668 			      OPT_Wunused_variable,
669 			      "unused structured binding declaration");
670 		else
671 		  warning_at (DECL_SOURCE_LOCATION (decl),
672 			      OPT_Wunused_variable, "unused variable %qD", decl);
673 	      }
674 	    else if (DECL_CONTEXT (decl) == current_function_decl
675 		     // For -Wunused-but-set-variable leave references alone.
676 		     && !TYPE_REF_P (TREE_TYPE (decl))
677 		     && errorcount == unused_but_set_errorcount)
678 	      {
679 		if (!DECL_NAME (decl) && DECL_DECOMPOSITION_P (decl))
680 		  warning_at (DECL_SOURCE_LOCATION (decl),
681 			      OPT_Wunused_but_set_variable, "structured "
682 			      "binding declaration set but not used");
683 		else
684 		  warning_at (DECL_SOURCE_LOCATION (decl),
685 			      OPT_Wunused_but_set_variable,
686 			      "variable %qD set but not used", decl);
687 		unused_but_set_errorcount = errorcount;
688 	      }
689 	  }
690       }
691 
692   /* Remove declarations for all the DECLs in this level.  */
693   for (link = decls; link; link = TREE_CHAIN (link))
694     {
695       decl = TREE_CODE (link) == TREE_LIST ? TREE_VALUE (link) : link;
696       tree name = OVL_NAME (decl);
697 
698       /* Remove the binding.  */
699       if (TREE_CODE (decl) == LABEL_DECL)
700 	pop_local_label (name, decl);
701       else
702 	pop_local_binding (name, decl);
703     }
704 
705   /* Restore the IDENTIFIER_TYPE_VALUEs.  */
706   for (link = current_binding_level->type_shadowed;
707        link; link = TREE_CHAIN (link))
708     SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link), TREE_VALUE (link));
709 
710   /* There may be OVERLOADs (wrapped in TREE_LISTs) on the BLOCK_VARs
711      list if a `using' declaration put them there.  The debugging
712      back ends won't understand OVERLOAD, so we remove them here.
713      Because the BLOCK_VARS are (temporarily) shared with
714      CURRENT_BINDING_LEVEL->NAMES we must do this fixup after we have
715      popped all the bindings.  Also remove undeduced 'auto' decls,
716      which LTO doesn't understand, and can't have been used by anything.  */
717   if (block)
718     {
719       tree* d;
720 
721       for (d = &BLOCK_VARS (block); *d; )
722 	{
723 	  if (TREE_CODE (*d) == TREE_LIST
724 	      || (!processing_template_decl
725 		  && undeduced_auto_decl (*d)))
726 	    *d = TREE_CHAIN (*d);
727 	  else
728 	    d = &DECL_CHAIN (*d);
729 	}
730     }
731 
732   /* If the level being exited is the top level of a function,
733      check over all the labels.  */
734   if (functionbody)
735     {
736       if (block)
737 	{
738 	  /* Since this is the top level block of a function, the vars are
739 	     the function's parameters.  Don't leave them in the BLOCK
740 	     because they are found in the FUNCTION_DECL instead.  */
741 	  BLOCK_VARS (block) = 0;
742 	  pop_labels (block);
743 	}
744       else
745 	pop_labels (subblocks);
746     }
747 
748   kind = current_binding_level->kind;
749   if (kind == sk_cleanup)
750     {
751       tree stmt;
752 
753       /* If this is a temporary binding created for a cleanup, then we'll
754 	 have pushed a statement list level.  Pop that, create a new
755 	 BIND_EXPR for the block, and insert it into the stream.  */
756       stmt = pop_stmt_list (current_binding_level->statement_list);
757       stmt = c_build_bind_expr (input_location, block, stmt);
758       add_stmt (stmt);
759     }
760 
761   leave_scope ();
762   if (functionbody)
763     {
764       /* The current function is being defined, so its DECL_INITIAL
765 	 should be error_mark_node.  */
766       gcc_assert (DECL_INITIAL (current_function_decl) == error_mark_node);
767       DECL_INITIAL (current_function_decl) = block ? block : subblocks;
768       if (subblocks)
769 	{
770 	  if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
771 	    {
772 	      if (BLOCK_SUBBLOCKS (subblocks))
773 		BLOCK_OUTER_CURLY_BRACE_P (BLOCK_SUBBLOCKS (subblocks)) = 1;
774 	    }
775 	  else
776 	    BLOCK_OUTER_CURLY_BRACE_P (subblocks) = 1;
777 	}
778     }
779   else if (block)
780     current_binding_level->blocks
781       = block_chainon (current_binding_level->blocks, block);
782 
783   /* If we did not make a block for the level just exited,
784      any blocks made for inner levels
785      (since they cannot be recorded as subblocks in that level)
786      must be carried forward so they will later become subblocks
787      of something else.  */
788   else if (subblocks)
789     current_binding_level->blocks
790       = block_chainon (current_binding_level->blocks, subblocks);
791 
792   /* Each and every BLOCK node created here in `poplevel' is important
793      (e.g. for proper debugging information) so if we created one
794      earlier, mark it as "used".  */
795   if (block)
796     TREE_USED (block) = 1;
797 
798   /* All temporary bindings created for cleanups are popped silently.  */
799   if (kind == sk_cleanup)
800     goto restart;
801 
802   timevar_cond_stop (TV_NAME_LOOKUP, subtime);
803   return block;
804 }
805 
806 /* Call wrapup_globals_declarations for the globals in NAMESPACE.  */
807 /* Diagnose odr-used extern inline variables without definitions
808    in the current TU.  */
809 
810 int
wrapup_namespace_globals()811 wrapup_namespace_globals ()
812 {
813   if (vec<tree, va_gc> *statics = static_decls)
814     {
815       tree decl;
816       unsigned int i;
817       FOR_EACH_VEC_ELT (*statics, i, decl)
818 	{
819 	  if (warn_unused_function
820 	      && TREE_CODE (decl) == FUNCTION_DECL
821 	      && DECL_INITIAL (decl) == 0
822 	      && DECL_EXTERNAL (decl)
823 	      && !TREE_PUBLIC (decl)
824 	      && !DECL_ARTIFICIAL (decl)
825 	      && !DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
826 	      && !TREE_NO_WARNING (decl))
827 	    warning_at (DECL_SOURCE_LOCATION (decl),
828 			OPT_Wunused_function,
829 			"%qF declared %<static%> but never defined", decl);
830 
831 	  if (VAR_P (decl)
832 	      && DECL_EXTERNAL (decl)
833 	      && DECL_INLINE_VAR_P (decl)
834 	      && DECL_ODR_USED (decl))
835 	    error_at (DECL_SOURCE_LOCATION (decl),
836 		      "odr-used inline variable %qD is not defined", decl);
837 	}
838 
839       /* Clear out the list, so we don't rescan next time.  */
840       static_decls = NULL;
841 
842       /* Write out any globals that need to be output.  */
843       return wrapup_global_declarations (statics->address (),
844 					 statics->length ());
845     }
846   return 0;
847 }
848 
849 /* In C++, you don't have to write `struct S' to refer to `S'; you
850    can just use `S'.  We accomplish this by creating a TYPE_DECL as
851    if the user had written `typedef struct S S'.  Create and return
852    the TYPE_DECL for TYPE.  */
853 
854 tree
create_implicit_typedef(tree name,tree type)855 create_implicit_typedef (tree name, tree type)
856 {
857   tree decl;
858 
859   decl = build_decl (input_location, TYPE_DECL, name, type);
860   DECL_ARTIFICIAL (decl) = 1;
861   /* There are other implicit type declarations, like the one *within*
862      a class that allows you to write `S::S'.  We must distinguish
863      amongst these.  */
864   SET_DECL_IMPLICIT_TYPEDEF_P (decl);
865   TYPE_NAME (type) = decl;
866   TYPE_STUB_DECL (type) = decl;
867 
868   return decl;
869 }
870 
871 /* Function-scope local entities that need discriminators.  Each entry
872    is a {decl,name} pair.  VAR_DECLs for anon unions get their name
873    smashed, so we cannot rely on DECL_NAME.  */
874 
875 static GTY((deletable)) vec<tree, va_gc> *local_entities;
876 
877 /* Determine the mangling discriminator of local DECL.  There are
878    generally very few of these in any particular function.  */
879 
880 void
determine_local_discriminator(tree decl)881 determine_local_discriminator (tree decl)
882 {
883   bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
884   retrofit_lang_decl (decl);
885   tree ctx = DECL_CONTEXT (decl);
886   tree name = (TREE_CODE (decl) == TYPE_DECL
887 	       && TYPE_UNNAMED_P (TREE_TYPE (decl))
888 	       ? NULL_TREE : DECL_NAME (decl));
889   size_t nelts = vec_safe_length (local_entities);
890   for (size_t i = 0; i < nelts; i += 2)
891     {
892       tree *pair = &(*local_entities)[i];
893       tree d = pair[0];
894       tree n = pair[1];
895       gcc_checking_assert (d != decl);
896       if (name == n
897 	  && TREE_CODE (decl) == TREE_CODE (d)
898 	  && ctx == DECL_CONTEXT (d))
899 	{
900 	  tree disc = integer_one_node;
901 	  if (DECL_DISCRIMINATOR (d))
902 	    disc = build_int_cst (TREE_TYPE (disc),
903 				  TREE_INT_CST_LOW (DECL_DISCRIMINATOR (d)) + 1);
904 	  DECL_DISCRIMINATOR (decl) = disc;
905 	  /* Replace the saved decl.  */
906 	  pair[0] = decl;
907 	  decl = NULL_TREE;
908 	  break;
909 	}
910     }
911 
912   if (decl)
913     {
914       vec_safe_reserve (local_entities, 2);
915       local_entities->quick_push (decl);
916       local_entities->quick_push (name);
917     }
918 
919   timevar_cond_stop (TV_NAME_LOOKUP, subtime);
920 }
921 
922 
923 
924 /* Returns true if functions FN1 and FN2 have equivalent trailing
925    requires clauses.  */
926 
927 static bool
function_requirements_equivalent_p(tree newfn,tree oldfn)928 function_requirements_equivalent_p (tree newfn, tree oldfn)
929 {
930   /* In the concepts TS, the combined constraints are compared.  */
931   if (cxx_dialect < cxx2a)
932     {
933       tree ci1 = get_constraints (oldfn);
934       tree ci2 = get_constraints (newfn);
935       tree req1 = ci1 ? CI_ASSOCIATED_CONSTRAINTS (ci1) : NULL_TREE;
936       tree req2 = ci2 ? CI_ASSOCIATED_CONSTRAINTS (ci2) : NULL_TREE;
937       return cp_tree_equal (req1, req2);
938     }
939 
940   /* Compare only trailing requirements.  */
941   tree reqs1 = get_trailing_function_requirements (newfn);
942   tree reqs2 = get_trailing_function_requirements (oldfn);
943   if ((reqs1 != NULL_TREE) != (reqs2 != NULL_TREE))
944     return false;
945   reqs1 = maybe_substitute_reqs_for (reqs1, newfn);
946   reqs2 = maybe_substitute_reqs_for (reqs2, oldfn);
947   return cp_tree_equal (reqs1, reqs2);
948 }
949 
950 /* Subroutine of duplicate_decls: return truthvalue of whether
951    or not types of these decls match.
952 
953    For C++, we must compare the parameter list so that `int' can match
954    `int&' in a parameter position, but `int&' is not confused with
955    `const int&'.  */
956 
957 int
decls_match(tree newdecl,tree olddecl,bool record_versions)958 decls_match (tree newdecl, tree olddecl, bool record_versions /* = true */)
959 {
960   int types_match;
961 
962   if (newdecl == olddecl)
963     return 1;
964 
965   if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
966     /* If the two DECLs are not even the same kind of thing, we're not
967        interested in their types.  */
968     return 0;
969 
970   gcc_assert (DECL_P (newdecl));
971 
972   if (TREE_CODE (newdecl) == FUNCTION_DECL)
973     {
974       tree f1 = TREE_TYPE (newdecl);
975       tree f2 = TREE_TYPE (olddecl);
976       tree p1 = TYPE_ARG_TYPES (f1);
977       tree p2 = TYPE_ARG_TYPES (f2);
978       tree r2;
979 
980       /* Specializations of different templates are different functions
981 	 even if they have the same type.  */
982       tree t1 = (DECL_USE_TEMPLATE (newdecl)
983 		 ? DECL_TI_TEMPLATE (newdecl)
984 		 : NULL_TREE);
985       tree t2 = (DECL_USE_TEMPLATE (olddecl)
986 		 ? DECL_TI_TEMPLATE (olddecl)
987 		 : NULL_TREE);
988       if (t1 != t2)
989 	return 0;
990 
991       if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
992 	  && ! (DECL_EXTERN_C_P (newdecl)
993 		&& DECL_EXTERN_C_P (olddecl)))
994 	return 0;
995 
996       /* A new declaration doesn't match a built-in one unless it
997 	 is also extern "C".  */
998       if (DECL_IS_BUILTIN (olddecl)
999 	  && DECL_EXTERN_C_P (olddecl) && !DECL_EXTERN_C_P (newdecl))
1000 	return 0;
1001 
1002       if (TREE_CODE (f1) != TREE_CODE (f2))
1003 	return 0;
1004 
1005       /* A declaration with deduced return type should use its pre-deduction
1006 	 type for declaration matching.  */
1007       r2 = fndecl_declared_return_type (olddecl);
1008 
1009       if (same_type_p (TREE_TYPE (f1), r2))
1010 	{
1011 	  if (!prototype_p (f2) && DECL_EXTERN_C_P (olddecl)
1012 	      && fndecl_built_in_p (olddecl))
1013 	    {
1014 	      types_match = self_promoting_args_p (p1);
1015 	      if (p1 == void_list_node)
1016 		TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
1017 	    }
1018 	  else
1019 	    types_match =
1020 	      compparms (p1, p2)
1021 	      && type_memfn_rqual (f1) == type_memfn_rqual (f2)
1022 	      && (TYPE_ATTRIBUTES (TREE_TYPE (newdecl)) == NULL_TREE
1023 	          || comp_type_attributes (TREE_TYPE (newdecl),
1024 					   TREE_TYPE (olddecl)) != 0);
1025 	}
1026       else
1027 	types_match = 0;
1028 
1029       /* Two function declarations match if either has a requires-clause
1030          then both have a requires-clause and their constraints-expressions
1031          are equivalent.  */
1032       if (types_match && flag_concepts)
1033 	types_match = function_requirements_equivalent_p (newdecl, olddecl);
1034 
1035       /* The decls dont match if they correspond to two different versions
1036 	 of the same function.   Disallow extern "C" functions to be
1037 	 versions for now.  */
1038       if (types_match
1039 	  && !DECL_EXTERN_C_P (newdecl)
1040 	  && !DECL_EXTERN_C_P (olddecl)
1041 	  && record_versions
1042 	  && maybe_version_functions (newdecl, olddecl,
1043 				      (!DECL_FUNCTION_VERSIONED (newdecl)
1044 				       || !DECL_FUNCTION_VERSIONED (olddecl))))
1045 	return 0;
1046     }
1047   else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1048     {
1049       if (!template_heads_equivalent_p (newdecl, olddecl))
1050 	return 0;
1051 
1052       tree oldres = DECL_TEMPLATE_RESULT (olddecl);
1053       tree newres = DECL_TEMPLATE_RESULT (newdecl);
1054 
1055       if (TREE_CODE (newres) != TREE_CODE (oldres))
1056 	return 0;
1057 
1058       /* Two template types match if they are the same. Otherwise, compare
1059          the underlying declarations.  */
1060       if (TREE_CODE (newres) == TYPE_DECL)
1061         types_match = same_type_p (TREE_TYPE (newres), TREE_TYPE (oldres));
1062       else
1063 	types_match = decls_match (newres, oldres);
1064     }
1065   else
1066     {
1067       /* Need to check scope for variable declaration (VAR_DECL).
1068 	 For typedef (TYPE_DECL), scope is ignored.  */
1069       if (VAR_P (newdecl)
1070 	  && CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
1071 	  /* [dcl.link]
1072 	     Two declarations for an object with C language linkage
1073 	     with the same name (ignoring the namespace that qualify
1074 	     it) that appear in different namespace scopes refer to
1075 	     the same object.  */
1076 	  && !(DECL_EXTERN_C_P (olddecl) && DECL_EXTERN_C_P (newdecl)))
1077 	return 0;
1078 
1079       if (TREE_TYPE (newdecl) == error_mark_node)
1080 	types_match = TREE_TYPE (olddecl) == error_mark_node;
1081       else if (TREE_TYPE (olddecl) == NULL_TREE)
1082 	types_match = TREE_TYPE (newdecl) == NULL_TREE;
1083       else if (TREE_TYPE (newdecl) == NULL_TREE)
1084 	types_match = 0;
1085       else
1086 	types_match = comptypes (TREE_TYPE (newdecl),
1087 				 TREE_TYPE (olddecl),
1088 				 COMPARE_REDECLARATION);
1089     }
1090 
1091   return types_match;
1092 }
1093 
1094 /* NEWDECL and OLDDECL have identical signatures.  If they are
1095    different versions adjust them and return true.
1096    If RECORD is set to true, record function versions.  */
1097 
1098 bool
maybe_version_functions(tree newdecl,tree olddecl,bool record)1099 maybe_version_functions (tree newdecl, tree olddecl, bool record)
1100 {
1101   if (!targetm.target_option.function_versions (newdecl, olddecl))
1102     return false;
1103 
1104   if (!DECL_FUNCTION_VERSIONED (olddecl))
1105     {
1106       DECL_FUNCTION_VERSIONED (olddecl) = 1;
1107       if (DECL_ASSEMBLER_NAME_SET_P (olddecl))
1108 	mangle_decl (olddecl);
1109     }
1110 
1111   if (!DECL_FUNCTION_VERSIONED (newdecl))
1112     {
1113       DECL_FUNCTION_VERSIONED (newdecl) = 1;
1114       if (DECL_ASSEMBLER_NAME_SET_P (newdecl))
1115 	mangle_decl (newdecl);
1116     }
1117 
1118   if (record)
1119     cgraph_node::record_function_versions (olddecl, newdecl);
1120 
1121   return true;
1122 }
1123 
1124 /* If NEWDECL is `static' and an `extern' was seen previously,
1125    warn about it.  OLDDECL is the previous declaration.
1126 
1127    Note that this does not apply to the C++ case of declaring
1128    a variable `extern const' and then later `const'.
1129 
1130    Don't complain about built-in functions, since they are beyond
1131    the user's control.  */
1132 
1133 void
warn_extern_redeclared_static(tree newdecl,tree olddecl)1134 warn_extern_redeclared_static (tree newdecl, tree olddecl)
1135 {
1136   if (TREE_CODE (newdecl) == TYPE_DECL
1137       || TREE_CODE (newdecl) == TEMPLATE_DECL
1138       || TREE_CODE (newdecl) == CONST_DECL
1139       || TREE_CODE (newdecl) == NAMESPACE_DECL)
1140     return;
1141 
1142   /* Don't get confused by static member functions; that's a different
1143      use of `static'.  */
1144   if (TREE_CODE (newdecl) == FUNCTION_DECL
1145       && DECL_STATIC_FUNCTION_P (newdecl))
1146     return;
1147 
1148   /* If the old declaration was `static', or the new one isn't, then
1149      everything is OK.  */
1150   if (DECL_THIS_STATIC (olddecl) || !DECL_THIS_STATIC (newdecl))
1151     return;
1152 
1153   /* It's OK to declare a builtin function as `static'.  */
1154   if (TREE_CODE (olddecl) == FUNCTION_DECL
1155       && DECL_ARTIFICIAL (olddecl))
1156     return;
1157 
1158   auto_diagnostic_group d;
1159   if (permerror (DECL_SOURCE_LOCATION (newdecl),
1160 		 "%qD was declared %<extern%> and later %<static%>", newdecl))
1161     inform (DECL_SOURCE_LOCATION (olddecl),
1162 	    "previous declaration of %qD", olddecl);
1163 }
1164 
1165 /* NEW_DECL is a redeclaration of OLD_DECL; both are functions or
1166    function templates.  If their exception specifications do not
1167    match, issue a diagnostic.  */
1168 
1169 static void
check_redeclaration_exception_specification(tree new_decl,tree old_decl)1170 check_redeclaration_exception_specification (tree new_decl,
1171 					     tree old_decl)
1172 {
1173   tree new_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (new_decl));
1174   tree old_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old_decl));
1175 
1176   /* Two default specs are equivalent, don't force evaluation.  */
1177   if (UNEVALUATED_NOEXCEPT_SPEC_P (new_exceptions)
1178       && UNEVALUATED_NOEXCEPT_SPEC_P (old_exceptions))
1179     return;
1180 
1181   if (!type_dependent_expression_p (old_decl))
1182     {
1183       maybe_instantiate_noexcept (new_decl);
1184       maybe_instantiate_noexcept (old_decl);
1185     }
1186   new_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (new_decl));
1187   old_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old_decl));
1188 
1189   /* [except.spec]
1190 
1191      If any declaration of a function has an exception-specification,
1192      all declarations, including the definition and an explicit
1193      specialization, of that function shall have an
1194      exception-specification with the same set of type-ids.  */
1195   if (! DECL_IS_BUILTIN (old_decl)
1196       && !comp_except_specs (new_exceptions, old_exceptions, ce_normal))
1197     {
1198       const char *const msg
1199 	= G_("declaration of %qF has a different exception specifier");
1200       bool complained = true;
1201       location_t new_loc = DECL_SOURCE_LOCATION (new_decl);
1202       auto_diagnostic_group d;
1203       if (DECL_IN_SYSTEM_HEADER (old_decl))
1204 	complained = pedwarn (new_loc, OPT_Wsystem_headers, msg, new_decl);
1205       else if (!flag_exceptions)
1206 	/* We used to silently permit mismatched eh specs with
1207 	   -fno-exceptions, so make them a pedwarn now.  */
1208 	complained = pedwarn (new_loc, OPT_Wpedantic, msg, new_decl);
1209       else
1210 	error_at (new_loc, msg, new_decl);
1211       if (complained)
1212 	inform (DECL_SOURCE_LOCATION (old_decl),
1213 		"from previous declaration %qF", old_decl);
1214     }
1215 }
1216 
1217 /* Return true if OLD_DECL and NEW_DECL agree on constexprness.
1218    Otherwise issue diagnostics.  */
1219 
1220 static bool
validate_constexpr_redeclaration(tree old_decl,tree new_decl)1221 validate_constexpr_redeclaration (tree old_decl, tree new_decl)
1222 {
1223   old_decl = STRIP_TEMPLATE (old_decl);
1224   new_decl = STRIP_TEMPLATE (new_decl);
1225   if (!VAR_OR_FUNCTION_DECL_P (old_decl)
1226       || !VAR_OR_FUNCTION_DECL_P (new_decl))
1227     return true;
1228   if (DECL_DECLARED_CONSTEXPR_P (old_decl)
1229       == DECL_DECLARED_CONSTEXPR_P (new_decl))
1230     {
1231       if (TREE_CODE (old_decl) != FUNCTION_DECL)
1232 	return true;
1233       if (DECL_IMMEDIATE_FUNCTION_P (old_decl)
1234 	  == DECL_IMMEDIATE_FUNCTION_P (new_decl))
1235 	return true;
1236     }
1237   if (TREE_CODE (old_decl) == FUNCTION_DECL)
1238     {
1239       if (fndecl_built_in_p (old_decl))
1240 	{
1241 	  /* Hide a built-in declaration.  */
1242 	  DECL_DECLARED_CONSTEXPR_P (old_decl)
1243 	    = DECL_DECLARED_CONSTEXPR_P (new_decl);
1244 	  if (DECL_IMMEDIATE_FUNCTION_P (new_decl))
1245 	    SET_DECL_IMMEDIATE_FUNCTION_P (old_decl);
1246 	  return true;
1247 	}
1248       /* 7.1.5 [dcl.constexpr]
1249 	 Note: An explicit specialization can differ from the template
1250 	 declaration with respect to the constexpr specifier.  */
1251       if (! DECL_TEMPLATE_SPECIALIZATION (old_decl)
1252 	  && DECL_TEMPLATE_SPECIALIZATION (new_decl))
1253 	return true;
1254 
1255       const char *kind = "constexpr";
1256       if (DECL_IMMEDIATE_FUNCTION_P (old_decl)
1257 	  || DECL_IMMEDIATE_FUNCTION_P (new_decl))
1258 	kind = "consteval";
1259       error_at (DECL_SOURCE_LOCATION (new_decl),
1260 		"redeclaration %qD differs in %qs "
1261 		"from previous declaration", new_decl,
1262 		kind);
1263       inform (DECL_SOURCE_LOCATION (old_decl),
1264 	      "previous declaration %qD", old_decl);
1265       return false;
1266     }
1267   return true;
1268 }
1269 
1270 // If OLDDECL and NEWDECL are concept declarations with the same type
1271 // (i.e., and template parameters), but different requirements,
1272 // emit diagnostics and return true. Otherwise, return false.
1273 static inline bool
check_concept_refinement(tree olddecl,tree newdecl)1274 check_concept_refinement (tree olddecl, tree newdecl)
1275 {
1276   if (!DECL_DECLARED_CONCEPT_P (olddecl) || !DECL_DECLARED_CONCEPT_P (newdecl))
1277     return false;
1278 
1279   tree d1 = DECL_TEMPLATE_RESULT (olddecl);
1280   tree d2 = DECL_TEMPLATE_RESULT (newdecl);
1281   if (TREE_CODE (d1) != TREE_CODE (d2))
1282     return false;
1283 
1284   tree t1 = TREE_TYPE (d1);
1285   tree t2 = TREE_TYPE (d2);
1286   if (TREE_CODE (d1) == FUNCTION_DECL)
1287     {
1288       if (compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2))
1289           && comp_template_parms (DECL_TEMPLATE_PARMS (olddecl),
1290                                   DECL_TEMPLATE_PARMS (newdecl))
1291           && !equivalently_constrained (olddecl, newdecl))
1292         {
1293           error ("cannot specialize concept %q#D", olddecl);
1294           return true;
1295         }
1296     }
1297   return false;
1298 }
1299 
1300 /* DECL is a redeclaration of a function or function template.  If
1301    it does have default arguments issue a diagnostic.  Note: this
1302    function is used to enforce the requirements in C++11 8.3.6 about
1303    no default arguments in redeclarations.  */
1304 
1305 static void
check_redeclaration_no_default_args(tree decl)1306 check_redeclaration_no_default_args (tree decl)
1307 {
1308   gcc_assert (DECL_DECLARES_FUNCTION_P (decl));
1309 
1310   for (tree t = FUNCTION_FIRST_USER_PARMTYPE (decl);
1311        t && t != void_list_node; t = TREE_CHAIN (t))
1312     if (TREE_PURPOSE (t))
1313       {
1314 	permerror (DECL_SOURCE_LOCATION (decl),
1315 		   "redeclaration of %q#D may not have default "
1316 		   "arguments", decl);
1317 	return;
1318       }
1319 }
1320 
1321 /* NEWDECL is a redeclaration of a function or function template OLDDECL,
1322    in any case represented as FUNCTION_DECLs (the DECL_TEMPLATE_RESULTs of
1323    the TEMPLATE_DECLs in case of function templates).  This function is used
1324    to enforce the final part of C++17 11.3.6/4, about a single declaration:
1325    "If a friend declaration specifies a default argument expression, that
1326    declaration shall be a definition and shall be the only declaration of
1327    the function or function template in the translation unit."  */
1328 
1329 static void
check_no_redeclaration_friend_default_args(tree olddecl,tree newdecl,bool olddecl_hidden_friend_p)1330 check_no_redeclaration_friend_default_args (tree olddecl, tree newdecl,
1331 					    bool olddecl_hidden_friend_p)
1332 {
1333   if (!olddecl_hidden_friend_p && !DECL_FRIEND_P (newdecl))
1334     return;
1335 
1336   tree t1 = FUNCTION_FIRST_USER_PARMTYPE (olddecl);
1337   tree t2 = FUNCTION_FIRST_USER_PARMTYPE (newdecl);
1338 
1339   for (; t1 && t1 != void_list_node;
1340        t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1341     if ((olddecl_hidden_friend_p && TREE_PURPOSE (t1))
1342 	|| (DECL_FRIEND_P (newdecl) && TREE_PURPOSE (t2)))
1343       {
1344 	auto_diagnostic_group d;
1345 	if (permerror (DECL_SOURCE_LOCATION (newdecl),
1346 		       "friend declaration of %q#D specifies default "
1347 		       "arguments and isn%'t the only declaration", newdecl))
1348 	  inform (DECL_SOURCE_LOCATION (olddecl),
1349 		  "previous declaration of %q#D", olddecl);
1350 	return;
1351       }
1352 }
1353 
1354 /* Merge tree bits that correspond to attributes noreturn, nothrow,
1355    const,  malloc, and pure from NEWDECL with those of OLDDECL.  */
1356 
1357 static void
merge_attribute_bits(tree newdecl,tree olddecl)1358 merge_attribute_bits (tree newdecl, tree olddecl)
1359 {
1360   TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1361   TREE_THIS_VOLATILE (olddecl) |= TREE_THIS_VOLATILE (newdecl);
1362   TREE_NOTHROW (newdecl) |= TREE_NOTHROW (olddecl);
1363   TREE_NOTHROW (olddecl) |= TREE_NOTHROW (newdecl);
1364   TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1365   TREE_READONLY (olddecl) |= TREE_READONLY (newdecl);
1366   DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1367   DECL_IS_MALLOC (olddecl) |= DECL_IS_MALLOC (newdecl);
1368   DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
1369   DECL_PURE_P (olddecl) |= DECL_PURE_P (newdecl);
1370   DECL_UNINLINABLE (newdecl) |= DECL_UNINLINABLE (olddecl);
1371   DECL_UNINLINABLE (olddecl) |= DECL_UNINLINABLE (newdecl);
1372 }
1373 
1374 #define GNU_INLINE_P(fn) (DECL_DECLARED_INLINE_P (fn)			\
1375 			  && lookup_attribute ("gnu_inline",		\
1376 					       DECL_ATTRIBUTES (fn)))
1377 
1378 /* A subroutine of duplicate_decls. Emits a diagnostic when newdecl
1379    ambiguates olddecl.  Returns true if an error occurs.  */
1380 
1381 static bool
duplicate_function_template_decls(tree newdecl,tree olddecl)1382 duplicate_function_template_decls (tree newdecl, tree olddecl)
1383 {
1384 
1385   tree newres = DECL_TEMPLATE_RESULT (newdecl);
1386   tree oldres = DECL_TEMPLATE_RESULT (olddecl);
1387   /* Function template declarations can be differentiated by parameter
1388      and return type.  */
1389   if (compparms (TYPE_ARG_TYPES (TREE_TYPE (oldres)),
1390 		 TYPE_ARG_TYPES (TREE_TYPE (newres)))
1391        && same_type_p (TREE_TYPE (TREE_TYPE (newdecl)),
1392 		       TREE_TYPE (TREE_TYPE (olddecl))))
1393     {
1394       /* ... and also by their template-heads and requires-clauses.  */
1395       if (template_heads_equivalent_p (newdecl, olddecl)
1396 	  && function_requirements_equivalent_p (newres, oldres))
1397 	{
1398 	  error ("ambiguating new declaration %q+#D", newdecl);
1399 	  inform (DECL_SOURCE_LOCATION (olddecl),
1400 		  "old declaration %q#D", olddecl);
1401 	  return true;
1402 	}
1403 
1404       /* FIXME: The types are the same but the are differences
1405 	 in either the template heads or function requirements.
1406 	 We should be able to diagnose a set of common errors
1407 	 stemming from these declarations. For example:
1408 
1409 	   template<typename T> requires C void f(...);
1410 	   template<typename T> void f(...) requires C;
1411 
1412 	 These are functionally equivalent but not equivalent.  */
1413     }
1414 
1415   return false;
1416 }
1417 
1418 /* If NEWDECL is a redeclaration of OLDDECL, merge the declarations.
1419    If the redeclaration is invalid, a diagnostic is issued, and the
1420    error_mark_node is returned.  Otherwise, OLDDECL is returned.
1421 
1422    If NEWDECL is not a redeclaration of OLDDECL, NULL_TREE is
1423    returned.
1424 
1425    NEWDECL_IS_FRIEND is true if NEWDECL was declared as a friend.  */
1426 
1427 tree
duplicate_decls(tree newdecl,tree olddecl,bool newdecl_is_friend)1428 duplicate_decls (tree newdecl, tree olddecl, bool newdecl_is_friend)
1429 {
1430   unsigned olddecl_uid = DECL_UID (olddecl);
1431   int olddecl_friend = 0, types_match = 0, hidden_friend = 0;
1432   int olddecl_hidden_friend = 0;
1433   int new_defines_function = 0;
1434   tree new_template_info;
1435   location_t olddecl_loc = DECL_SOURCE_LOCATION (olddecl);
1436   location_t newdecl_loc = DECL_SOURCE_LOCATION (newdecl);
1437 
1438   if (newdecl == olddecl)
1439     return olddecl;
1440 
1441   types_match = decls_match (newdecl, olddecl);
1442 
1443   /* If either the type of the new decl or the type of the old decl is an
1444      error_mark_node, then that implies that we have already issued an
1445      error (earlier) for some bogus type specification, and in that case,
1446      it is rather pointless to harass the user with yet more error message
1447      about the same declaration, so just pretend the types match here.  */
1448   if (TREE_TYPE (newdecl) == error_mark_node
1449       || TREE_TYPE (olddecl) == error_mark_node)
1450     return error_mark_node;
1451 
1452   /* Check for redeclaration and other discrepancies.  */
1453   if (TREE_CODE (olddecl) == FUNCTION_DECL
1454       && DECL_ARTIFICIAL (olddecl)
1455       /* A C++20 implicit friend operator== uses the normal path (94462).  */
1456       && !DECL_HIDDEN_FRIEND_P (olddecl))
1457     {
1458       if (TREE_CODE (newdecl) != FUNCTION_DECL)
1459 	{
1460 	  /* Avoid warnings redeclaring built-ins which have not been
1461 	     explicitly declared.  */
1462 	  if (DECL_ANTICIPATED (olddecl))
1463 	    {
1464 	      if (TREE_PUBLIC (newdecl)
1465 		  && CP_DECL_CONTEXT (newdecl) == global_namespace)
1466 		warning_at (newdecl_loc,
1467 			    OPT_Wbuiltin_declaration_mismatch,
1468 			    "built-in function %qD declared as non-function",
1469 			    newdecl);
1470 	      return NULL_TREE;
1471 	    }
1472 
1473 	  /* If you declare a built-in or predefined function name as static,
1474 	     the old definition is overridden, but optionally warn this was a
1475 	     bad choice of name.  */
1476 	  if (! TREE_PUBLIC (newdecl))
1477 	    {
1478 	      warning_at (newdecl_loc,
1479 			  OPT_Wshadow,
1480 			  fndecl_built_in_p (olddecl)
1481 			  ? G_("shadowing built-in function %q#D")
1482 			  : G_("shadowing library function %q#D"), olddecl);
1483 	      /* Discard the old built-in function.  */
1484 	      return NULL_TREE;
1485 	    }
1486 	  /* If the built-in is not ansi, then programs can override
1487 	     it even globally without an error.  */
1488 	  else if (! fndecl_built_in_p (olddecl))
1489 	    warning_at (newdecl_loc, 0,
1490 			"library function %q#D redeclared as non-function %q#D",
1491 			olddecl, newdecl);
1492 	  else
1493 	    error_at (newdecl_loc,
1494 		      "declaration of %q#D conflicts with built-in "
1495 		      "declaration %q#D", newdecl, olddecl);
1496 	  return NULL_TREE;
1497 	}
1498       else if (DECL_OMP_DECLARE_REDUCTION_P (olddecl))
1499 	{
1500 	  gcc_assert (DECL_OMP_DECLARE_REDUCTION_P (newdecl));
1501 	  error_at (newdecl_loc,
1502 		    "redeclaration of %<pragma omp declare reduction%>");
1503 	  inform (olddecl_loc,
1504 		  "previous %<pragma omp declare reduction%> declaration");
1505 	  return error_mark_node;
1506 	}
1507       else if (!types_match)
1508 	{
1509 	  /* Avoid warnings redeclaring built-ins which have not been
1510 	     explicitly declared.  */
1511 	  if (DECL_ANTICIPATED (olddecl))
1512 	    {
1513 	      tree t1, t2;
1514 
1515 	      /* A new declaration doesn't match a built-in one unless it
1516 		 is also extern "C".  */
1517 	      gcc_assert (DECL_IS_BUILTIN (olddecl));
1518 	      gcc_assert (DECL_EXTERN_C_P (olddecl));
1519 	      if (!DECL_EXTERN_C_P (newdecl))
1520 		return NULL_TREE;
1521 
1522 	      for (t1 = TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1523 		   t2 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1524 		   t1 || t2;
1525 		   t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1526 		{
1527 		  if (!t1 || !t2)
1528 		    break;
1529 		  /* FILE, tm types are not known at the time
1530 		     we create the builtins.  */
1531 		  for (unsigned i = 0;
1532 		       i < sizeof (builtin_structptr_types)
1533 			   / sizeof (builtin_structptr_type);
1534 		       ++i)
1535 		    if (TREE_VALUE (t2) == builtin_structptr_types[i].node)
1536 		      {
1537 			tree t = TREE_VALUE (t1);
1538 
1539 			if (TYPE_PTR_P (t)
1540 			    && TYPE_IDENTIFIER (TREE_TYPE (t))
1541 			    == get_identifier (builtin_structptr_types[i].str)
1542 			    && compparms (TREE_CHAIN (t1), TREE_CHAIN (t2)))
1543 			  {
1544 			    tree oldargs = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1545 
1546 			    TYPE_ARG_TYPES (TREE_TYPE (olddecl))
1547 			      = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
1548 			    types_match = decls_match (newdecl, olddecl);
1549 			    if (types_match)
1550 			      return duplicate_decls (newdecl, olddecl,
1551 						      newdecl_is_friend);
1552 			    TYPE_ARG_TYPES (TREE_TYPE (olddecl)) = oldargs;
1553 			  }
1554 			goto next_arg;
1555 		      }
1556 
1557 		  if (! same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1558 		    break;
1559 		next_arg:;
1560 		}
1561 
1562 	      warning_at (newdecl_loc,
1563 			  OPT_Wbuiltin_declaration_mismatch,
1564 			  "declaration of %q#D conflicts with built-in "
1565 			  "declaration %q#D", newdecl, olddecl);
1566 	    }
1567 	  else if ((DECL_EXTERN_C_P (newdecl)
1568 		    && DECL_EXTERN_C_P (olddecl))
1569 		   || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1570 				 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
1571 	    {
1572 	      /* Don't really override olddecl for __* prefixed builtins
1573 		 except for __[^b]*_chk, the compiler might be using those
1574 		 explicitly.  */
1575 	      if (fndecl_built_in_p (olddecl))
1576 		{
1577 		  tree id = DECL_NAME (olddecl);
1578 		  const char *name = IDENTIFIER_POINTER (id);
1579 		  size_t len;
1580 
1581 		  if (name[0] == '_'
1582 		      && name[1] == '_'
1583 		      && (strncmp (name + 2, "builtin_",
1584 				   strlen ("builtin_")) == 0
1585 			  || (len = strlen (name)) <= strlen ("___chk")
1586 			  || memcmp (name + len - strlen ("_chk"),
1587 				     "_chk", strlen ("_chk") + 1) != 0))
1588 		    {
1589 		      if (DECL_INITIAL (newdecl))
1590 			{
1591 			  error_at (newdecl_loc,
1592 				    "definition of %q#D ambiguates built-in "
1593 				    "declaration %q#D", newdecl, olddecl);
1594 			  return error_mark_node;
1595 			}
1596 		      auto_diagnostic_group d;
1597 		      if (permerror (newdecl_loc,
1598 				     "new declaration %q#D ambiguates built-in"
1599 				     " declaration %q#D", newdecl, olddecl)
1600 			  && flag_permissive)
1601 			inform (newdecl_loc,
1602 				"ignoring the %q#D declaration", newdecl);
1603 		      return flag_permissive ? olddecl : error_mark_node;
1604 		    }
1605 		}
1606 
1607 	      /* A near match; override the builtin.  */
1608 
1609 	      if (TREE_PUBLIC (newdecl))
1610 		warning_at (newdecl_loc,
1611 			    OPT_Wbuiltin_declaration_mismatch,
1612 			    "new declaration %q#D ambiguates built-in "
1613 			    "declaration %q#D", newdecl, olddecl);
1614 	      else
1615 		warning (OPT_Wshadow,
1616 			 fndecl_built_in_p (olddecl)
1617 			 ? G_("shadowing built-in function %q#D")
1618 			 : G_("shadowing library function %q#D"), olddecl);
1619 	    }
1620 	  else
1621 	    /* Discard the old built-in function.  */
1622 	    return NULL_TREE;
1623 
1624 	  /* Replace the old RTL to avoid problems with inlining.  */
1625 	  COPY_DECL_RTL (newdecl, olddecl);
1626 	}
1627       /* Even if the types match, prefer the new declarations type for
1628 	 built-ins which have not been explicitly declared, for
1629 	 exception lists, etc...  */
1630       else if (DECL_IS_BUILTIN (olddecl))
1631 	{
1632 	  tree type = TREE_TYPE (newdecl);
1633 	  tree attribs = (*targetm.merge_type_attributes)
1634 	    (TREE_TYPE (olddecl), type);
1635 
1636 	  type = cp_build_type_attribute_variant (type, attribs);
1637 	  TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = type;
1638 	}
1639 
1640       /* If a function is explicitly declared "throw ()", propagate that to
1641 	 the corresponding builtin.  */
1642       if (DECL_BUILT_IN_CLASS (olddecl) == BUILT_IN_NORMAL
1643 	  && DECL_ANTICIPATED (olddecl)
1644 	  && TREE_NOTHROW (newdecl)
1645 	  && !TREE_NOTHROW (olddecl))
1646 	{
1647 	  enum built_in_function fncode = DECL_FUNCTION_CODE (olddecl);
1648 	  tree tmpdecl = builtin_decl_explicit (fncode);
1649 	  if (tmpdecl && tmpdecl != olddecl && types_match)
1650 	    TREE_NOTHROW (tmpdecl)  = 1;
1651 	}
1652 
1653       /* Whether or not the builtin can throw exceptions has no
1654 	 bearing on this declarator.  */
1655       TREE_NOTHROW (olddecl) = 0;
1656 
1657       if (DECL_THIS_STATIC (newdecl) && !DECL_THIS_STATIC (olddecl))
1658 	{
1659 	  /* If a builtin function is redeclared as `static', merge
1660 	     the declarations, but make the original one static.  */
1661 	  DECL_THIS_STATIC (olddecl) = 1;
1662 	  TREE_PUBLIC (olddecl) = 0;
1663 
1664 	  /* Make the old declaration consistent with the new one so
1665 	     that all remnants of the builtin-ness of this function
1666 	     will be banished.  */
1667 	  SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
1668 	  COPY_DECL_RTL (newdecl, olddecl);
1669 	}
1670     }
1671   else if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1672     {
1673       /* C++ Standard, 3.3, clause 4:
1674 	 "[Note: a namespace name or a class template name must be unique
1675 	 in its declarative region (7.3.2, clause 14). ]"  */
1676       if (TREE_CODE (olddecl) == NAMESPACE_DECL
1677 	  || TREE_CODE (newdecl) == NAMESPACE_DECL)
1678 	/* Namespace conflicts with not namespace.  */;
1679       else if (DECL_TYPE_TEMPLATE_P (olddecl)
1680 	       || DECL_TYPE_TEMPLATE_P (newdecl))
1681 	/* Class template conflicts.  */;
1682       else if ((TREE_CODE (newdecl) == FUNCTION_DECL
1683 		&& DECL_FUNCTION_TEMPLATE_P (olddecl))
1684 	       || (TREE_CODE (olddecl) == FUNCTION_DECL
1685 		   && DECL_FUNCTION_TEMPLATE_P (newdecl)))
1686 	{
1687 	  /* One is a function and the other is a template
1688 	     function.  */
1689 	  if (!UDLIT_OPER_P (DECL_NAME (newdecl)))
1690 	    return NULL_TREE;
1691 
1692 	  /* There can only be one!  */
1693 	  if (TREE_CODE (newdecl) == TEMPLATE_DECL
1694 	      && check_raw_literal_operator (olddecl))
1695 	    error_at (newdecl_loc,
1696 		      "literal operator %q#D conflicts with"
1697 		      " raw literal operator", newdecl);
1698 	  else if (check_raw_literal_operator (newdecl))
1699 	    error_at (newdecl_loc,
1700 		      "raw literal operator %q#D conflicts with"
1701 		      " literal operator template", newdecl);
1702 	  else
1703 	    return NULL_TREE;
1704 
1705 	  inform (olddecl_loc, "previous declaration %q#D", olddecl);
1706 	  return error_mark_node;
1707 	}
1708       else if (DECL_IMPLICIT_TYPEDEF_P (olddecl)
1709 	       || DECL_IMPLICIT_TYPEDEF_P (newdecl))
1710 	/* One is an implicit typedef, that's ok.  */
1711 	return NULL_TREE;
1712 
1713       error ("%q#D redeclared as different kind of entity", newdecl);
1714       inform (olddecl_loc, "previous declaration %q#D", olddecl);
1715 
1716       return error_mark_node;
1717     }
1718   else if (!types_match)
1719     {
1720       if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl))
1721 	/* These are certainly not duplicate declarations; they're
1722 	   from different scopes.  */
1723 	return NULL_TREE;
1724 
1725       if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1726 	{
1727 	  tree oldres = DECL_TEMPLATE_RESULT (olddecl);
1728 	  tree newres = DECL_TEMPLATE_RESULT (newdecl);
1729 
1730 	  /* The name of a class template may not be declared to refer to
1731 	     any other template, class, function, object, namespace, value,
1732 	     or type in the same scope.  */
1733 	  if (TREE_CODE (oldres) == TYPE_DECL
1734 	      || TREE_CODE (newres) == TYPE_DECL)
1735 	    {
1736 	      error_at (newdecl_loc,
1737 			"conflicting declaration of template %q#D", newdecl);
1738 	      inform (olddecl_loc,
1739 		      "previous declaration %q#D", olddecl);
1740 	      return error_mark_node;
1741 	    }
1742 
1743 	  else if (TREE_CODE (oldres) == FUNCTION_DECL
1744 		   && TREE_CODE (newres) == FUNCTION_DECL)
1745 	    {
1746 	      if (duplicate_function_template_decls (newdecl, olddecl))
1747 		return error_mark_node;
1748 	      return NULL_TREE;
1749 	    }
1750           else if (check_concept_refinement (olddecl, newdecl))
1751 	    return error_mark_node;
1752 	  return NULL_TREE;
1753 	}
1754       if (TREE_CODE (newdecl) == FUNCTION_DECL)
1755 	{
1756 	  if (DECL_EXTERN_C_P (newdecl) && DECL_EXTERN_C_P (olddecl))
1757 	    {
1758 	      error_at (newdecl_loc,
1759 			"conflicting declaration of C function %q#D",
1760 			newdecl);
1761 	      inform (olddecl_loc,
1762 		      "previous declaration %q#D", olddecl);
1763 	      return NULL_TREE;
1764 	    }
1765 	  /* For function versions, params and types match, but they
1766 	     are not ambiguous.  */
1767 	  else if ((!DECL_FUNCTION_VERSIONED (newdecl)
1768 		    && !DECL_FUNCTION_VERSIONED (olddecl))
1769                    // The functions have the same parameter types.
1770 		   && compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1771 				 TYPE_ARG_TYPES (TREE_TYPE (olddecl)))
1772                    // And the same constraints.
1773                    && equivalently_constrained (newdecl, olddecl))
1774 	    {
1775 	      error_at (newdecl_loc,
1776 			"ambiguating new declaration of %q#D", newdecl);
1777 	      inform (olddecl_loc,
1778 		      "old declaration %q#D", olddecl);
1779               return error_mark_node;
1780 	    }
1781 	  else
1782 	    return NULL_TREE;
1783 	}
1784       else
1785 	{
1786 	  error_at (newdecl_loc, "conflicting declaration %q#D", newdecl);
1787 	  inform (olddecl_loc,
1788 		  "previous declaration as %q#D", olddecl);
1789 	  return error_mark_node;
1790 	}
1791     }
1792   else if (TREE_CODE (newdecl) == FUNCTION_DECL
1793 	    && ((DECL_TEMPLATE_SPECIALIZATION (olddecl)
1794 		 && (!DECL_TEMPLATE_INFO (newdecl)
1795 		     || (DECL_TI_TEMPLATE (newdecl)
1796 			 != DECL_TI_TEMPLATE (olddecl))))
1797 		|| (DECL_TEMPLATE_SPECIALIZATION (newdecl)
1798 		    && (!DECL_TEMPLATE_INFO (olddecl)
1799 			|| (DECL_TI_TEMPLATE (olddecl)
1800 			    != DECL_TI_TEMPLATE (newdecl))))))
1801     /* It's OK to have a template specialization and a non-template
1802        with the same type, or to have specializations of two
1803        different templates with the same type.  Note that if one is a
1804        specialization, and the other is an instantiation of the same
1805        template, that we do not exit at this point.  That situation
1806        can occur if we instantiate a template class, and then
1807        specialize one of its methods.  This situation is valid, but
1808        the declarations must be merged in the usual way.  */
1809     return NULL_TREE;
1810   else if (TREE_CODE (newdecl) == FUNCTION_DECL
1811 	   && ((DECL_TEMPLATE_INSTANTIATION (olddecl)
1812 		&& !DECL_USE_TEMPLATE (newdecl))
1813 	       || (DECL_TEMPLATE_INSTANTIATION (newdecl)
1814 		   && !DECL_USE_TEMPLATE (olddecl))))
1815     /* One of the declarations is a template instantiation, and the
1816        other is not a template at all.  That's OK.  */
1817     return NULL_TREE;
1818   else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
1819     {
1820       /* In [namespace.alias] we have:
1821 
1822 	   In a declarative region, a namespace-alias-definition can be
1823 	   used to redefine a namespace-alias declared in that declarative
1824 	   region to refer only to the namespace to which it already
1825 	   refers.
1826 
1827 	 Therefore, if we encounter a second alias directive for the same
1828 	 alias, we can just ignore the second directive.  */
1829       if (DECL_NAMESPACE_ALIAS (newdecl)
1830 	  && (DECL_NAMESPACE_ALIAS (newdecl)
1831 	      == DECL_NAMESPACE_ALIAS (olddecl)))
1832 	return olddecl;
1833 
1834       /* Leave it to update_binding to merge or report error.  */
1835       return NULL_TREE;
1836     }
1837   else
1838     {
1839       const char *errmsg = redeclaration_error_message (newdecl, olddecl);
1840       if (errmsg)
1841 	{
1842 	  auto_diagnostic_group d;
1843 	  error_at (newdecl_loc, errmsg, newdecl);
1844 	  if (DECL_NAME (olddecl) != NULL_TREE)
1845 	    inform (olddecl_loc,
1846 		    (DECL_INITIAL (olddecl) && namespace_bindings_p ())
1847 		    ? G_("%q#D previously defined here")
1848 		    : G_("%q#D previously declared here"), olddecl);
1849 	  return error_mark_node;
1850 	}
1851       else if (TREE_CODE (olddecl) == FUNCTION_DECL
1852 	       && DECL_INITIAL (olddecl) != NULL_TREE
1853 	       && !prototype_p (TREE_TYPE (olddecl))
1854 	       && prototype_p (TREE_TYPE (newdecl)))
1855 	{
1856 	  /* Prototype decl follows defn w/o prototype.  */
1857 	  auto_diagnostic_group d;
1858 	  if (warning_at (newdecl_loc, 0,
1859 			  "prototype specified for %q#D", newdecl))
1860 	    inform (olddecl_loc,
1861 		    "previous non-prototype definition here");
1862 	}
1863       else if (VAR_OR_FUNCTION_DECL_P (olddecl)
1864 	       && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl))
1865 	{
1866 	  /* [dcl.link]
1867 	     If two declarations of the same function or object
1868 	     specify different linkage-specifications ..., the program
1869 	     is ill-formed.... Except for functions with C++ linkage,
1870 	     a function declaration without a linkage specification
1871 	     shall not precede the first linkage specification for
1872 	     that function.  A function can be declared without a
1873 	     linkage specification after an explicit linkage
1874 	     specification has been seen; the linkage explicitly
1875 	     specified in the earlier declaration is not affected by
1876 	     such a function declaration.
1877 
1878 	     DR 563 raises the question why the restrictions on
1879 	     functions should not also apply to objects.  Older
1880 	     versions of G++ silently ignore the linkage-specification
1881 	     for this example:
1882 
1883 	       namespace N {
1884                  extern int i;
1885    	         extern "C" int i;
1886                }
1887 
1888              which is clearly wrong.  Therefore, we now treat objects
1889 	     like functions.  */
1890 	  if (current_lang_depth () == 0)
1891 	    {
1892 	      /* There is no explicit linkage-specification, so we use
1893 		 the linkage from the previous declaration.  */
1894 	      retrofit_lang_decl (newdecl);
1895 	      SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
1896 	    }
1897 	  else
1898 	    {
1899 	      auto_diagnostic_group d;
1900 	      error_at (newdecl_loc,
1901 			"conflicting declaration of %q#D with %qL linkage",
1902 			newdecl, DECL_LANGUAGE (newdecl));
1903 	      inform (olddecl_loc,
1904 		      "previous declaration with %qL linkage",
1905 		      DECL_LANGUAGE (olddecl));
1906 	    }
1907 	}
1908 
1909       if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl))
1910 	;
1911       else if (TREE_CODE (olddecl) == FUNCTION_DECL)
1912 	{
1913 	  /* Note: free functions, as TEMPLATE_DECLs, are handled below.  */
1914 	  if (DECL_FUNCTION_MEMBER_P (olddecl)
1915 	      && (/* grokfndecl passes member function templates too
1916 		     as FUNCTION_DECLs.  */
1917 		  DECL_TEMPLATE_INFO (olddecl)
1918 		  /* C++11 8.3.6/6.
1919 		     Default arguments for a member function of a class
1920 		     template shall be specified on the initial declaration
1921 		     of the member function within the class template.  */
1922 		  || CLASSTYPE_TEMPLATE_INFO (CP_DECL_CONTEXT (olddecl))))
1923 	    check_redeclaration_no_default_args (newdecl);
1924 	  else
1925 	    {
1926 	      tree t1 = FUNCTION_FIRST_USER_PARMTYPE (olddecl);
1927 	      tree t2 = FUNCTION_FIRST_USER_PARMTYPE (newdecl);
1928 	      int i = 1;
1929 
1930 	      for (; t1 && t1 != void_list_node;
1931 		   t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++)
1932 		if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
1933 		  {
1934 		    if (simple_cst_equal (TREE_PURPOSE (t1),
1935 					  TREE_PURPOSE (t2)) == 1)
1936 		      {
1937 			auto_diagnostic_group d;
1938 			if (permerror (newdecl_loc,
1939 				       "default argument given for parameter "
1940 				       "%d of %q#D", i, newdecl))
1941 			  inform (olddecl_loc,
1942 				  "previous specification in %q#D here",
1943 				  olddecl);
1944 		      }
1945 		    else
1946 		      {
1947 			auto_diagnostic_group d;
1948 			error_at (newdecl_loc,
1949 				  "default argument given for parameter %d "
1950 				  "of %q#D", i, newdecl);
1951 			inform (olddecl_loc,
1952 				"previous specification in %q#D here",
1953 				olddecl);
1954 		      }
1955 		  }
1956 
1957 	      /* C++17 11.3.6/4: "If a friend declaration specifies a default
1958 		 argument expression, that declaration... shall be the only
1959 		 declaration of the function or function template in the
1960 		 translation unit."  */
1961 	      check_no_redeclaration_friend_default_args
1962 		(olddecl, newdecl, DECL_HIDDEN_FRIEND_P (olddecl));
1963 	    }
1964 	}
1965     }
1966 
1967   /* Do not merge an implicit typedef with an explicit one.  In:
1968 
1969        class A;
1970        ...
1971        typedef class A A __attribute__ ((foo));
1972 
1973      the attribute should apply only to the typedef.  */
1974   if (TREE_CODE (olddecl) == TYPE_DECL
1975       && (DECL_IMPLICIT_TYPEDEF_P (olddecl)
1976 	  || DECL_IMPLICIT_TYPEDEF_P (newdecl)))
1977     return NULL_TREE;
1978 
1979   if (!validate_constexpr_redeclaration (olddecl, newdecl))
1980     return error_mark_node;
1981 
1982   /* We have committed to returning OLDDECL at this point.  */
1983 
1984   /* If new decl is `static' and an `extern' was seen previously,
1985      warn about it.  */
1986   warn_extern_redeclared_static (newdecl, olddecl);
1987 
1988   /* True to merge attributes between the declarations, false to
1989      set OLDDECL's attributes to those of NEWDECL (for template
1990      explicit specializations that specify their own attributes
1991      independent of those specified for the primary template).  */
1992   const bool merge_attr = (TREE_CODE (newdecl) != FUNCTION_DECL
1993 			   || !DECL_TEMPLATE_SPECIALIZATION (newdecl)
1994 			   || DECL_TEMPLATE_SPECIALIZATION (olddecl));
1995 
1996   if (TREE_CODE (newdecl) == FUNCTION_DECL)
1997     {
1998       if (merge_attr)
1999 	{
2000 	  if (diagnose_mismatched_attributes (olddecl, newdecl))
2001 	    inform (olddecl_loc, DECL_INITIAL (olddecl)
2002 		    ? G_("previous definition of %qD here")
2003 		    : G_("previous declaration of %qD here"), olddecl);
2004 
2005 	  /* [dcl.attr.noreturn]: The first declaration of a function shall
2006 	     specify the noreturn attribute if any declaration of that function
2007 	     specifies the noreturn attribute.  */
2008 	  tree a;
2009 	  if (TREE_THIS_VOLATILE (newdecl)
2010 	      && !TREE_THIS_VOLATILE (olddecl)
2011 	      /* This applies to [[noreturn]] only, not its GNU variants.  */
2012 	      && (a = lookup_attribute ("noreturn", DECL_ATTRIBUTES (newdecl)))
2013 	      && cxx11_attribute_p (a)
2014 	      && get_attribute_namespace (a) == NULL_TREE)
2015 	    {
2016 	      error_at (newdecl_loc, "function %qD declared %<[[noreturn]]%> "
2017 			"but its first declaration was not", newdecl);
2018 	      inform (olddecl_loc, "previous declaration of %qD", olddecl);
2019 	    }
2020 	}
2021 
2022       /* Now that functions must hold information normally held
2023 	 by field decls, there is extra work to do so that
2024 	 declaration information does not get destroyed during
2025 	 definition.  */
2026       if (DECL_VINDEX (olddecl))
2027 	DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl);
2028       if (DECL_CONTEXT (olddecl))
2029 	DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
2030       DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl);
2031       DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
2032       DECL_PURE_VIRTUAL_P (newdecl) |= DECL_PURE_VIRTUAL_P (olddecl);
2033       DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl);
2034       DECL_INVALID_OVERRIDER_P (newdecl) |= DECL_INVALID_OVERRIDER_P (olddecl);
2035       DECL_FINAL_P (newdecl) |= DECL_FINAL_P (olddecl);
2036       DECL_OVERRIDE_P (newdecl) |= DECL_OVERRIDE_P (olddecl);
2037       DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl);
2038       DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (newdecl)
2039 	|= DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (olddecl);
2040       if (DECL_OVERLOADED_OPERATOR_P (olddecl))
2041 	DECL_OVERLOADED_OPERATOR_CODE_RAW (newdecl)
2042 	  = DECL_OVERLOADED_OPERATOR_CODE_RAW (olddecl);
2043       new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE;
2044 
2045       /* Optionally warn about more than one declaration for the same
2046 	 name, but don't warn about a function declaration followed by a
2047 	 definition.  */
2048       if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl)
2049 	  && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE)
2050 	  /* Don't warn about extern decl followed by definition.  */
2051 	  && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl))
2052 	  /* Don't warn about friends, let add_friend take care of it.  */
2053 	  && ! (newdecl_is_friend || DECL_FRIEND_P (olddecl))
2054 	  /* Don't warn about declaration followed by specialization.  */
2055 	  && (! DECL_TEMPLATE_SPECIALIZATION (newdecl)
2056 	      || DECL_TEMPLATE_SPECIALIZATION (olddecl)))
2057 	{
2058 	  auto_diagnostic_group d;
2059 	  if (warning_at (newdecl_loc,
2060 			  OPT_Wredundant_decls,
2061 			  "redundant redeclaration of %qD in same scope",
2062 			  newdecl))
2063 	    inform (olddecl_loc,
2064 		    "previous declaration of %qD", olddecl);
2065 	}
2066 
2067       if (!(DECL_TEMPLATE_INSTANTIATION (olddecl)
2068 	    && DECL_TEMPLATE_SPECIALIZATION (newdecl)))
2069 	{
2070 	  if (DECL_DELETED_FN (newdecl))
2071 	    {
2072 	      auto_diagnostic_group d;
2073 	      error_at (newdecl_loc, "deleted definition of %qD", newdecl);
2074 	      inform (olddecl_loc,
2075 		      "previous declaration of %qD", olddecl);
2076 	    }
2077 	  DECL_DELETED_FN (newdecl) |= DECL_DELETED_FN (olddecl);
2078 	}
2079     }
2080 
2081   /* Deal with C++: must preserve virtual function table size.  */
2082   if (TREE_CODE (olddecl) == TYPE_DECL)
2083     {
2084       tree newtype = TREE_TYPE (newdecl);
2085       tree oldtype = TREE_TYPE (olddecl);
2086 
2087       if (newtype != error_mark_node && oldtype != error_mark_node
2088 	  && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype))
2089 	CLASSTYPE_FRIEND_CLASSES (newtype)
2090 	  = CLASSTYPE_FRIEND_CLASSES (oldtype);
2091 
2092       DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl);
2093     }
2094 
2095   /* Copy all the DECL_... slots specified in the new decl except for
2096      any that we copy here from the old type.  */
2097   if (merge_attr)
2098     DECL_ATTRIBUTES (newdecl)
2099       = (*targetm.merge_decl_attributes) (olddecl, newdecl);
2100   else
2101     DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
2102 
2103   if (DECL_DECLARES_FUNCTION_P (olddecl))
2104     {
2105       olddecl_friend = DECL_FRIEND_P (olddecl);
2106       olddecl_hidden_friend = DECL_HIDDEN_FRIEND_P (olddecl);
2107       hidden_friend = (DECL_ANTICIPATED (olddecl)
2108 		       && DECL_HIDDEN_FRIEND_P (olddecl)
2109 		       && newdecl_is_friend);
2110       if (!hidden_friend)
2111 	{
2112 	  DECL_ANTICIPATED (olddecl) = 0;
2113 	  DECL_HIDDEN_FRIEND_P (olddecl) = 0;
2114 	}
2115     }
2116 
2117   if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2118     {
2119       tree old_result = DECL_TEMPLATE_RESULT (olddecl);
2120       tree new_result = DECL_TEMPLATE_RESULT (newdecl);
2121       TREE_TYPE (olddecl) = TREE_TYPE (old_result);
2122 
2123       /* The new decl should not already have gathered any
2124 	 specializations.  */
2125       gcc_assert (!DECL_TEMPLATE_SPECIALIZATIONS (newdecl));
2126 
2127       DECL_ATTRIBUTES (old_result)
2128 	= (*targetm.merge_decl_attributes) (old_result, new_result);
2129 
2130       if (DECL_FUNCTION_TEMPLATE_P (newdecl))
2131 	{
2132 	  if (DECL_SOURCE_LOCATION (newdecl)
2133 	      != DECL_SOURCE_LOCATION (olddecl))
2134 	    {
2135 	      /* Per C++11 8.3.6/4, default arguments cannot be added in
2136 		 later declarations of a function template.  */
2137 	      check_redeclaration_no_default_args (newdecl);
2138 	      /* C++17 11.3.6/4: "If a friend declaration specifies a default
2139 		 argument expression, that declaration... shall be the only
2140 		 declaration of the function or function template in the
2141 		 translation unit."  */
2142 	      check_no_redeclaration_friend_default_args
2143 		(old_result, new_result, olddecl_hidden_friend);
2144 	    }
2145 
2146 	  check_default_args (newdecl);
2147 
2148 	  if (GNU_INLINE_P (old_result) != GNU_INLINE_P (new_result)
2149 	      && DECL_INITIAL (new_result))
2150 	    {
2151 	      if (DECL_INITIAL (old_result))
2152 		DECL_UNINLINABLE (old_result) = 1;
2153 	      else
2154 		DECL_UNINLINABLE (old_result) = DECL_UNINLINABLE (new_result);
2155 	      DECL_EXTERNAL (old_result) = DECL_EXTERNAL (new_result);
2156 	      DECL_NOT_REALLY_EXTERN (old_result)
2157 		= DECL_NOT_REALLY_EXTERN (new_result);
2158 	      DECL_INTERFACE_KNOWN (old_result)
2159 		= DECL_INTERFACE_KNOWN (new_result);
2160 	      DECL_DECLARED_INLINE_P (old_result)
2161 		= DECL_DECLARED_INLINE_P (new_result);
2162 	      DECL_DISREGARD_INLINE_LIMITS (old_result)
2163 	        |= DECL_DISREGARD_INLINE_LIMITS (new_result);
2164 
2165 	    }
2166 	  else
2167 	    {
2168 	      DECL_DECLARED_INLINE_P (old_result)
2169 		|= DECL_DECLARED_INLINE_P (new_result);
2170 	      DECL_DISREGARD_INLINE_LIMITS (old_result)
2171 	        |= DECL_DISREGARD_INLINE_LIMITS (new_result);
2172 	      check_redeclaration_exception_specification (newdecl, olddecl);
2173 
2174 	      merge_attribute_bits (new_result, old_result);
2175 	    }
2176 	}
2177 
2178       /* If the new declaration is a definition, update the file and
2179 	 line information on the declaration, and also make
2180 	 the old declaration the same definition.  */
2181       if (DECL_INITIAL (new_result) != NULL_TREE)
2182 	{
2183 	  DECL_SOURCE_LOCATION (olddecl)
2184 	    = DECL_SOURCE_LOCATION (old_result)
2185 	    = DECL_SOURCE_LOCATION (newdecl);
2186 	  DECL_INITIAL (old_result) = DECL_INITIAL (new_result);
2187 	  if (DECL_FUNCTION_TEMPLATE_P (newdecl))
2188 	    {
2189 	      tree parm;
2190 	      DECL_ARGUMENTS (old_result)
2191 		= DECL_ARGUMENTS (new_result);
2192 	      for (parm = DECL_ARGUMENTS (old_result); parm;
2193 		   parm = DECL_CHAIN (parm))
2194 		DECL_CONTEXT (parm) = old_result;
2195 	    }
2196 	}
2197 
2198       return olddecl;
2199     }
2200 
2201   if (types_match)
2202     {
2203       if (TREE_CODE (newdecl) == FUNCTION_DECL)
2204 	check_redeclaration_exception_specification (newdecl, olddecl);
2205 
2206       /* Automatically handles default parameters.  */
2207       tree oldtype = TREE_TYPE (olddecl);
2208       tree newtype;
2209 
2210       /* For typedefs use the old type, as the new type's DECL_NAME points
2211 	 at newdecl, which will be ggc_freed.  */
2212       if (TREE_CODE (newdecl) == TYPE_DECL)
2213 	{
2214 	  /* But NEWTYPE might have an attribute, honor that.  */
2215 	  tree tem = TREE_TYPE (newdecl);
2216 	  newtype = oldtype;
2217 
2218 	  if (TYPE_USER_ALIGN (tem))
2219 	    {
2220 	      if (TYPE_ALIGN (tem) > TYPE_ALIGN (newtype))
2221 		SET_TYPE_ALIGN (newtype, TYPE_ALIGN (tem));
2222 	      TYPE_USER_ALIGN (newtype) = true;
2223 	    }
2224 
2225 	  /* And remove the new type from the variants list.  */
2226 	  if (TYPE_NAME (TREE_TYPE (newdecl)) == newdecl)
2227 	    {
2228 	      tree remove = TREE_TYPE (newdecl);
2229 	      if (TYPE_MAIN_VARIANT (remove) == remove)
2230 		{
2231 		  gcc_assert (TYPE_NEXT_VARIANT (remove) == NULL_TREE);
2232 		  /* If remove is the main variant, no need to remove that
2233 		     from the list.  One of the DECL_ORIGINAL_TYPE
2234 		     variants, e.g. created for aligned attribute, might still
2235 		     refer to the newdecl TYPE_DECL though, so remove that one
2236 		     in that case.  */
2237 		  if (tree orig = DECL_ORIGINAL_TYPE (newdecl))
2238 		    if (orig != remove)
2239 		      for (tree t = TYPE_MAIN_VARIANT (orig); t;
2240 			   t = TYPE_MAIN_VARIANT (t))
2241 			if (TYPE_NAME (TYPE_NEXT_VARIANT (t)) == newdecl)
2242 			  {
2243 			    TYPE_NEXT_VARIANT (t)
2244 			      = TYPE_NEXT_VARIANT (TYPE_NEXT_VARIANT (t));
2245 			    break;
2246 			  }
2247 		}
2248 	      else
2249 		for (tree t = TYPE_MAIN_VARIANT (remove); ;
2250 		     t = TYPE_NEXT_VARIANT (t))
2251 		  if (TYPE_NEXT_VARIANT (t) == remove)
2252 		    {
2253 		      TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (remove);
2254 		      break;
2255 		    }
2256 	    }
2257 	}
2258       else if (merge_attr)
2259 	newtype = merge_types (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
2260       else
2261 	newtype = TREE_TYPE (newdecl);
2262 
2263       if (VAR_P (newdecl))
2264 	{
2265 	  DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl);
2266 	  /* For already initialized vars, TREE_READONLY could have been
2267 	     cleared in cp_finish_decl, because the var needs runtime
2268 	     initialization or destruction.  Make sure not to set
2269 	     TREE_READONLY on it again.  */
2270 	  if (DECL_INITIALIZED_P (olddecl)
2271 	      && !DECL_EXTERNAL (olddecl)
2272 	      && !TREE_READONLY (olddecl))
2273 	    TREE_READONLY (newdecl) = 0;
2274 	  DECL_INITIALIZED_P (newdecl) |= DECL_INITIALIZED_P (olddecl);
2275 	  DECL_NONTRIVIALLY_INITIALIZED_P (newdecl)
2276 	    |= DECL_NONTRIVIALLY_INITIALIZED_P (olddecl);
2277 	  if (DECL_DEPENDENT_INIT_P (olddecl))
2278 	    SET_DECL_DEPENDENT_INIT_P (newdecl, true);
2279 	  DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (newdecl)
2280 	    |= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (olddecl);
2281 	  DECL_DECLARED_CONSTEXPR_P (newdecl)
2282 	    |= DECL_DECLARED_CONSTEXPR_P (olddecl);
2283 
2284 	  /* Merge the threadprivate attribute from OLDDECL into NEWDECL.  */
2285 	  if (DECL_LANG_SPECIFIC (olddecl)
2286 	      && CP_DECL_THREADPRIVATE_P (olddecl))
2287 	    {
2288 	      /* Allocate a LANG_SPECIFIC structure for NEWDECL, if needed.  */
2289 	      retrofit_lang_decl (newdecl);
2290 	      CP_DECL_THREADPRIVATE_P (newdecl) = 1;
2291 	    }
2292 	}
2293 
2294       /* An explicit specialization of a function template or of a member
2295 	 function of a class template can be declared transaction_safe
2296 	 independently of whether the corresponding template entity is declared
2297 	 transaction_safe. */
2298       if (flag_tm && TREE_CODE (newdecl) == FUNCTION_DECL
2299 	  && DECL_TEMPLATE_INSTANTIATION (olddecl)
2300 	  && DECL_TEMPLATE_SPECIALIZATION (newdecl)
2301 	  && tx_safe_fn_type_p (newtype)
2302 	  && !tx_safe_fn_type_p (TREE_TYPE (newdecl)))
2303 	newtype = tx_unsafe_fn_variant (newtype);
2304 
2305       TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype;
2306 
2307       if (TREE_CODE (newdecl) == FUNCTION_DECL)
2308 	check_default_args (newdecl);
2309 
2310       /* Lay the type out, unless already done.  */
2311       if (! same_type_p (newtype, oldtype)
2312 	  && TREE_TYPE (newdecl) != error_mark_node
2313 	  && !(processing_template_decl && uses_template_parms (newdecl)))
2314 	layout_type (TREE_TYPE (newdecl));
2315 
2316       if ((VAR_P (newdecl)
2317 	   || TREE_CODE (newdecl) == PARM_DECL
2318 	   || TREE_CODE (newdecl) == RESULT_DECL
2319 	   || TREE_CODE (newdecl) == FIELD_DECL
2320 	   || TREE_CODE (newdecl) == TYPE_DECL)
2321 	  && !(processing_template_decl && uses_template_parms (newdecl)))
2322 	layout_decl (newdecl, 0);
2323 
2324       /* Merge deprecatedness.  */
2325       if (TREE_DEPRECATED (newdecl))
2326 	TREE_DEPRECATED (olddecl) = 1;
2327 
2328       /* Preserve function specific target and optimization options */
2329       if (TREE_CODE (newdecl) == FUNCTION_DECL)
2330 	{
2331 	  if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
2332 	      && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
2333 	    DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
2334 	      = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
2335 
2336 	  if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
2337 	      && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
2338 	    DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
2339 	      = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
2340 	}
2341       else
2342 	{
2343 	  /* Merge the const type qualifier.  */
2344 	  if (TREE_READONLY (newdecl))
2345 	    TREE_READONLY (olddecl) = 1;
2346 	  /* Merge the volatile type qualifier.  */
2347 	  if (TREE_THIS_VOLATILE (newdecl))
2348 	    TREE_THIS_VOLATILE (olddecl) = 1;
2349 	}
2350 
2351       /* Merge the initialization information.  */
2352       if (DECL_INITIAL (newdecl) == NULL_TREE
2353 	  && DECL_INITIAL (olddecl) != NULL_TREE)
2354 	{
2355 	  DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2356 	  DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2357 	  if (TREE_CODE (newdecl) == FUNCTION_DECL)
2358 	    {
2359 	      DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
2360 	      DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
2361 	    }
2362 	}
2363 
2364       if (TREE_CODE (newdecl) == FUNCTION_DECL)
2365 	{
2366 	  DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
2367 	    |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
2368 	  DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
2369 	  if (DECL_IS_OPERATOR_NEW_P (olddecl))
2370 	    DECL_SET_IS_OPERATOR_NEW (newdecl, true);
2371 	  DECL_LOOPING_CONST_OR_PURE_P (newdecl)
2372 	    |= DECL_LOOPING_CONST_OR_PURE_P (olddecl);
2373 	  DECL_IS_REPLACEABLE_OPERATOR (newdecl)
2374 	    |= DECL_IS_REPLACEABLE_OPERATOR (olddecl);
2375 
2376 	  if (merge_attr)
2377 	    merge_attribute_bits (newdecl, olddecl);
2378 	  else
2379 	    {
2380 	      /* Merge the noreturn bit.  */
2381 	      TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
2382 	      TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
2383 	      TREE_NOTHROW (olddecl) = TREE_NOTHROW (newdecl);
2384 	      DECL_IS_MALLOC (olddecl) = DECL_IS_MALLOC (newdecl);
2385 	      DECL_PURE_P (olddecl) = DECL_PURE_P (newdecl);
2386 	    }
2387 	  /* Keep the old RTL.  */
2388 	  COPY_DECL_RTL (olddecl, newdecl);
2389 	}
2390       else if (VAR_P (newdecl)
2391 	       && (DECL_SIZE (olddecl) || !DECL_SIZE (newdecl)))
2392 	{
2393 	  /* Keep the old RTL.  We cannot keep the old RTL if the old
2394 	     declaration was for an incomplete object and the new
2395 	     declaration is not since many attributes of the RTL will
2396 	     change.  */
2397 	  COPY_DECL_RTL (olddecl, newdecl);
2398 	}
2399     }
2400   /* If cannot merge, then use the new type and qualifiers,
2401      and don't preserve the old rtl.  */
2402   else
2403     {
2404       /* Clean out any memory we had of the old declaration.  */
2405       tree oldstatic = value_member (olddecl, static_aggregates);
2406       if (oldstatic)
2407 	TREE_VALUE (oldstatic) = error_mark_node;
2408 
2409       TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
2410       TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
2411       TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
2412       TREE_NOTHROW (olddecl) = TREE_NOTHROW (newdecl);
2413       TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
2414     }
2415 
2416   /* Merge the storage class information.  */
2417   merge_weak (newdecl, olddecl);
2418 
2419   DECL_DEFER_OUTPUT (newdecl) |= DECL_DEFER_OUTPUT (olddecl);
2420   TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
2421   TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl);
2422   if (! DECL_EXTERNAL (olddecl))
2423     DECL_EXTERNAL (newdecl) = 0;
2424   if (! DECL_COMDAT (olddecl))
2425     DECL_COMDAT (newdecl) = 0;
2426 
2427   new_template_info = NULL_TREE;
2428   if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl))
2429     {
2430       bool new_redefines_gnu_inline = false;
2431 
2432       if (new_defines_function
2433 	  && ((DECL_INTERFACE_KNOWN (olddecl)
2434 	       && TREE_CODE (olddecl) == FUNCTION_DECL)
2435 	      || (TREE_CODE (olddecl) == TEMPLATE_DECL
2436 		  && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl))
2437 		      == FUNCTION_DECL))))
2438 	{
2439 	  tree fn = olddecl;
2440 
2441 	  if (TREE_CODE (fn) == TEMPLATE_DECL)
2442 	    fn = DECL_TEMPLATE_RESULT (olddecl);
2443 
2444 	  new_redefines_gnu_inline = GNU_INLINE_P (fn) && DECL_INITIAL (fn);
2445 	}
2446 
2447       if (!new_redefines_gnu_inline)
2448 	{
2449 	  DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl);
2450 	  DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl);
2451 	  DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl);
2452 	}
2453       DECL_TEMPLATE_INSTANTIATED (newdecl)
2454 	|= DECL_TEMPLATE_INSTANTIATED (olddecl);
2455       DECL_ODR_USED (newdecl) |= DECL_ODR_USED (olddecl);
2456 
2457       /* If the OLDDECL is an instantiation and/or specialization,
2458 	 then the NEWDECL must be too.  But, it may not yet be marked
2459 	 as such if the caller has created NEWDECL, but has not yet
2460 	 figured out that it is a redeclaration.  */
2461       if (!DECL_USE_TEMPLATE (newdecl))
2462 	DECL_USE_TEMPLATE (newdecl) = DECL_USE_TEMPLATE (olddecl);
2463 
2464       /* Don't really know how much of the language-specific
2465 	 values we should copy from old to new.  */
2466       DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl);
2467       DECL_INITIALIZED_IN_CLASS_P (newdecl)
2468 	|= DECL_INITIALIZED_IN_CLASS_P (olddecl);
2469 
2470       if (LANG_DECL_HAS_MIN (newdecl))
2471 	{
2472 	  DECL_ACCESS (newdecl) = DECL_ACCESS (olddecl);
2473 	  if (DECL_TEMPLATE_INFO (newdecl))
2474 	    {
2475 	      new_template_info = DECL_TEMPLATE_INFO (newdecl);
2476 	      if (DECL_TEMPLATE_INSTANTIATION (olddecl)
2477 		  && DECL_TEMPLATE_SPECIALIZATION (newdecl))
2478 		/* Remember the presence of explicit specialization args.  */
2479 		TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (olddecl))
2480 		  = TINFO_USED_TEMPLATE_ID (new_template_info);
2481 	    }
2482 	  DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl);
2483 	}
2484 
2485       if (DECL_DECLARES_FUNCTION_P (newdecl))
2486 	{
2487 	  /* Only functions have these fields.  */
2488 	  DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl);
2489 	  DECL_BEFRIENDING_CLASSES (newdecl)
2490 	    = chainon (DECL_BEFRIENDING_CLASSES (newdecl),
2491 		       DECL_BEFRIENDING_CLASSES (olddecl));
2492 	  /* DECL_THUNKS is only valid for virtual functions,
2493 	     otherwise it is a DECL_FRIEND_CONTEXT.  */
2494 	  if (DECL_VIRTUAL_P (newdecl))
2495 	    SET_DECL_THUNKS (newdecl, DECL_THUNKS (olddecl));
2496 	}
2497       else if (VAR_P (newdecl))
2498 	{
2499 	  /* Only variables have this field.  */
2500 	  if (VAR_HAD_UNKNOWN_BOUND (olddecl))
2501 	    SET_VAR_HAD_UNKNOWN_BOUND (newdecl);
2502 	}
2503     }
2504 
2505   if (TREE_CODE (newdecl) == FUNCTION_DECL)
2506     {
2507       tree parm;
2508 
2509       /* Merge parameter attributes. */
2510       tree oldarg, newarg;
2511       for (oldarg = DECL_ARGUMENTS(olddecl),  newarg = DECL_ARGUMENTS(newdecl);
2512            oldarg && newarg;
2513            oldarg = DECL_CHAIN(oldarg), newarg = DECL_CHAIN(newarg))
2514 	{
2515           DECL_ATTRIBUTES (newarg)
2516 	    = (*targetm.merge_decl_attributes) (oldarg, newarg);
2517           DECL_ATTRIBUTES (oldarg) = DECL_ATTRIBUTES (newarg);
2518 	}
2519 
2520       if (DECL_TEMPLATE_INSTANTIATION (olddecl)
2521 	  && !DECL_TEMPLATE_INSTANTIATION (newdecl))
2522 	{
2523 	  /* If newdecl is not a specialization, then it is not a
2524 	     template-related function at all.  And that means that we
2525 	     should have exited above, returning 0.  */
2526 	  gcc_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl));
2527 
2528 	  if (DECL_ODR_USED (olddecl))
2529 	    /* From [temp.expl.spec]:
2530 
2531 	       If a template, a member template or the member of a class
2532 	       template is explicitly specialized then that
2533 	       specialization shall be declared before the first use of
2534 	       that specialization that would cause an implicit
2535 	       instantiation to take place, in every translation unit in
2536 	       which such a use occurs.  */
2537 	    error ("explicit specialization of %qD after first use",
2538 		      olddecl);
2539 
2540 	  SET_DECL_TEMPLATE_SPECIALIZATION (olddecl);
2541 	  DECL_COMDAT (newdecl) = (TREE_PUBLIC (newdecl)
2542 				   && DECL_DECLARED_INLINE_P (newdecl));
2543 
2544 	  /* Don't propagate visibility from the template to the
2545 	     specialization here.  We'll do that in determine_visibility if
2546 	     appropriate.  */
2547 	  DECL_VISIBILITY_SPECIFIED (olddecl) = 0;
2548 
2549 	  /* [temp.expl.spec/14] We don't inline explicit specialization
2550 	     just because the primary template says so.  */
2551 	  gcc_assert (!merge_attr);
2552 
2553 	  DECL_DECLARED_INLINE_P (olddecl)
2554 	    = DECL_DECLARED_INLINE_P (newdecl);
2555 
2556 	  DECL_DISREGARD_INLINE_LIMITS (olddecl)
2557 	    = DECL_DISREGARD_INLINE_LIMITS (newdecl);
2558 
2559 	  DECL_UNINLINABLE (olddecl) = DECL_UNINLINABLE (newdecl);
2560 	}
2561       else if (new_defines_function && DECL_INITIAL (olddecl))
2562 	{
2563 	  /* Never inline re-defined extern inline functions.
2564 	     FIXME: this could be better handled by keeping both
2565 	     function as separate declarations.  */
2566 	  DECL_UNINLINABLE (newdecl) = 1;
2567 	}
2568       else
2569 	{
2570 	  if (DECL_PENDING_INLINE_P (olddecl))
2571 	    {
2572 	      DECL_PENDING_INLINE_P (newdecl) = 1;
2573 	      DECL_PENDING_INLINE_INFO (newdecl)
2574 		= DECL_PENDING_INLINE_INFO (olddecl);
2575 	    }
2576 	  else if (DECL_PENDING_INLINE_P (newdecl))
2577 	    ;
2578 	  else if (DECL_SAVED_AUTO_RETURN_TYPE (newdecl) == NULL)
2579 	    DECL_SAVED_AUTO_RETURN_TYPE (newdecl)
2580 	      = DECL_SAVED_AUTO_RETURN_TYPE (olddecl);
2581 
2582 	  DECL_DECLARED_INLINE_P (newdecl) |= DECL_DECLARED_INLINE_P (olddecl);
2583 
2584 	  DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
2585 	    = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
2586 
2587 	  DECL_DISREGARD_INLINE_LIMITS (newdecl)
2588 	    = DECL_DISREGARD_INLINE_LIMITS (olddecl)
2589 	    = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
2590 	       || DECL_DISREGARD_INLINE_LIMITS (olddecl));
2591 	}
2592 
2593       /* Preserve abstractness on cloned [cd]tors.  */
2594       DECL_ABSTRACT_P (newdecl) = DECL_ABSTRACT_P (olddecl);
2595 
2596       /* Update newdecl's parms to point at olddecl.  */
2597       for (parm = DECL_ARGUMENTS (newdecl); parm;
2598 	   parm = DECL_CHAIN (parm))
2599 	DECL_CONTEXT (parm) = olddecl;
2600 
2601       if (! types_match)
2602 	{
2603 	  SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
2604 	  COPY_DECL_ASSEMBLER_NAME (newdecl, olddecl);
2605 	  COPY_DECL_RTL (newdecl, olddecl);
2606 	}
2607       if (! types_match || new_defines_function)
2608 	{
2609 	  /* These need to be copied so that the names are available.
2610 	     Note that if the types do match, we'll preserve inline
2611 	     info and other bits, but if not, we won't.  */
2612 	  DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl);
2613 	  DECL_RESULT (olddecl) = DECL_RESULT (newdecl);
2614 	}
2615       /* If redeclaring a builtin function, it stays built in
2616 	 if newdecl is a gnu_inline definition, or if newdecl is just
2617 	 a declaration.  */
2618       if (fndecl_built_in_p (olddecl)
2619 	  && (new_defines_function ? GNU_INLINE_P (newdecl) : types_match))
2620 	{
2621 	  copy_decl_built_in_function (newdecl, olddecl);
2622 	  /* If we're keeping the built-in definition, keep the rtl,
2623 	     regardless of declaration matches.  */
2624 	  COPY_DECL_RTL (olddecl, newdecl);
2625 	  if (DECL_BUILT_IN_CLASS (newdecl) == BUILT_IN_NORMAL)
2626 	    {
2627 	      enum built_in_function fncode = DECL_FUNCTION_CODE (newdecl);
2628 	      switch (fncode)
2629 		{
2630 		  /* If a compatible prototype of these builtin functions
2631 		     is seen, assume the runtime implements it with the
2632 		     expected semantics.  */
2633 		case BUILT_IN_STPCPY:
2634 		  if (builtin_decl_explicit_p (fncode))
2635 		    set_builtin_decl_implicit_p (fncode, true);
2636 		  break;
2637 		default:
2638 		  if (builtin_decl_explicit_p (fncode))
2639 		    set_builtin_decl_declared_p (fncode, true);
2640 		  break;
2641 		}
2642 
2643 	      copy_attributes_to_builtin (newdecl);
2644 	    }
2645 	}
2646       if (new_defines_function)
2647 	/* If defining a function declared with other language
2648 	   linkage, use the previously declared language linkage.  */
2649 	SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
2650       else if (types_match)
2651 	{
2652 	  DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
2653 	  /* Don't clear out the arguments if we're just redeclaring a
2654 	     function.  */
2655 	  if (DECL_ARGUMENTS (olddecl))
2656 	    DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
2657 	}
2658     }
2659   else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
2660     NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl);
2661 
2662   /* Now preserve various other info from the definition.  */
2663   TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl);
2664   TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl);
2665   DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
2666   COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
2667 
2668   /* Warn about conflicting visibility specifications.  */
2669   if (DECL_VISIBILITY_SPECIFIED (olddecl)
2670       && DECL_VISIBILITY_SPECIFIED (newdecl)
2671       && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
2672     {
2673       auto_diagnostic_group d;
2674       if (warning_at (newdecl_loc, OPT_Wattributes,
2675 		      "%qD: visibility attribute ignored because it "
2676 		      "conflicts with previous declaration", newdecl))
2677 	inform (olddecl_loc,
2678 		"previous declaration of %qD", olddecl);
2679     }
2680   /* Choose the declaration which specified visibility.  */
2681   if (DECL_VISIBILITY_SPECIFIED (olddecl))
2682     {
2683       DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
2684       DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
2685     }
2686   /* Init priority used to be merged from newdecl to olddecl by the memcpy,
2687      so keep this behavior.  */
2688   if (VAR_P (newdecl) && DECL_HAS_INIT_PRIORITY_P (newdecl))
2689     {
2690       SET_DECL_INIT_PRIORITY (olddecl, DECL_INIT_PRIORITY (newdecl));
2691       DECL_HAS_INIT_PRIORITY_P (olddecl) = 1;
2692     }
2693   /* Likewise for DECL_ALIGN, DECL_USER_ALIGN and DECL_PACKED.  */
2694   if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
2695     {
2696       SET_DECL_ALIGN (newdecl, DECL_ALIGN (olddecl));
2697       DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
2698     }
2699   DECL_USER_ALIGN (olddecl) = DECL_USER_ALIGN (newdecl);
2700   if (DECL_WARN_IF_NOT_ALIGN (olddecl)
2701       > DECL_WARN_IF_NOT_ALIGN (newdecl))
2702     SET_DECL_WARN_IF_NOT_ALIGN (newdecl,
2703 				DECL_WARN_IF_NOT_ALIGN (olddecl));
2704   if (TREE_CODE (newdecl) == FIELD_DECL)
2705     DECL_PACKED (olddecl) = DECL_PACKED (newdecl);
2706 
2707   /* The DECL_LANG_SPECIFIC information in OLDDECL will be replaced
2708      with that from NEWDECL below.  */
2709   if (DECL_LANG_SPECIFIC (olddecl))
2710     {
2711       gcc_assert (DECL_LANG_SPECIFIC (olddecl)
2712 		  != DECL_LANG_SPECIFIC (newdecl));
2713       ggc_free (DECL_LANG_SPECIFIC (olddecl));
2714     }
2715 
2716   /* Merge the USED information.  */
2717   if (TREE_USED (olddecl))
2718     TREE_USED (newdecl) = 1;
2719   else if (TREE_USED (newdecl))
2720     TREE_USED (olddecl) = 1;
2721   if (VAR_P (newdecl))
2722     {
2723       if (DECL_READ_P (olddecl))
2724 	DECL_READ_P (newdecl) = 1;
2725       else if (DECL_READ_P (newdecl))
2726 	DECL_READ_P (olddecl) = 1;
2727     }
2728   if (DECL_PRESERVE_P (olddecl))
2729     DECL_PRESERVE_P (newdecl) = 1;
2730   else if (DECL_PRESERVE_P (newdecl))
2731     DECL_PRESERVE_P (olddecl) = 1;
2732 
2733   /* Merge the DECL_FUNCTION_VERSIONED information.  newdecl will be copied
2734      to olddecl and deleted.  */
2735   if (TREE_CODE (newdecl) == FUNCTION_DECL
2736       && DECL_FUNCTION_VERSIONED (olddecl))
2737     {
2738       /* Set the flag for newdecl so that it gets copied to olddecl.  */
2739       DECL_FUNCTION_VERSIONED (newdecl) = 1;
2740       /* newdecl will be purged after copying to olddecl and is no longer
2741          a version.  */
2742       cgraph_node::delete_function_version_by_decl (newdecl);
2743     }
2744 
2745   if (TREE_CODE (newdecl) == FUNCTION_DECL)
2746     {
2747       int function_size;
2748       struct symtab_node *snode = symtab_node::get (olddecl);
2749 
2750       function_size = sizeof (struct tree_decl_common);
2751 
2752       memcpy ((char *) olddecl + sizeof (struct tree_common),
2753 	      (char *) newdecl + sizeof (struct tree_common),
2754 	      function_size - sizeof (struct tree_common));
2755 
2756       memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2757 	      (char *) newdecl + sizeof (struct tree_decl_common),
2758 	      sizeof (struct tree_function_decl) - sizeof (struct tree_decl_common));
2759 
2760       /* Preserve symtab node mapping.  */
2761       olddecl->decl_with_vis.symtab_node = snode;
2762 
2763       if (new_template_info)
2764 	/* If newdecl is a template instantiation, it is possible that
2765 	   the following sequence of events has occurred:
2766 
2767 	   o A friend function was declared in a class template.  The
2768 	   class template was instantiated.
2769 
2770 	   o The instantiation of the friend declaration was
2771 	   recorded on the instantiation list, and is newdecl.
2772 
2773 	   o Later, however, instantiate_class_template called pushdecl
2774 	   on the newdecl to perform name injection.  But, pushdecl in
2775 	   turn called duplicate_decls when it discovered that another
2776 	   declaration of a global function with the same name already
2777 	   existed.
2778 
2779 	   o Here, in duplicate_decls, we decided to clobber newdecl.
2780 
2781 	   If we're going to do that, we'd better make sure that
2782 	   olddecl, and not newdecl, is on the list of
2783 	   instantiations so that if we try to do the instantiation
2784 	   again we won't get the clobbered declaration.  */
2785 	reregister_specialization (newdecl,
2786 				   new_template_info,
2787 				   olddecl);
2788     }
2789   else
2790     {
2791       size_t size = tree_code_size (TREE_CODE (newdecl));
2792 
2793       memcpy ((char *) olddecl + sizeof (struct tree_common),
2794 	      (char *) newdecl + sizeof (struct tree_common),
2795 	      sizeof (struct tree_decl_common) - sizeof (struct tree_common));
2796       switch (TREE_CODE (newdecl))
2797 	{
2798 	case LABEL_DECL:
2799 	case VAR_DECL:
2800 	case RESULT_DECL:
2801 	case PARM_DECL:
2802 	case FIELD_DECL:
2803 	case TYPE_DECL:
2804 	case CONST_DECL:
2805 	  {
2806             struct symtab_node *snode = NULL;
2807 
2808 	    if (VAR_P (newdecl)
2809 		&& (TREE_STATIC (olddecl) || TREE_PUBLIC (olddecl)
2810 		    || DECL_EXTERNAL (olddecl)))
2811 	      snode = symtab_node::get (olddecl);
2812 	    memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2813 		    (char *) newdecl + sizeof (struct tree_decl_common),
2814 		    size - sizeof (struct tree_decl_common)
2815 		    + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
2816 	    if (VAR_P (newdecl))
2817 	      olddecl->decl_with_vis.symtab_node = snode;
2818 	  }
2819 	  break;
2820 	default:
2821 	  memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2822 		  (char *) newdecl + sizeof (struct tree_decl_common),
2823 		  sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common)
2824 		  + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
2825 	  break;
2826 	}
2827     }
2828 
2829   if (VAR_OR_FUNCTION_DECL_P (newdecl))
2830     {
2831       if (DECL_EXTERNAL (olddecl)
2832 	  || TREE_PUBLIC (olddecl)
2833 	  || TREE_STATIC (olddecl))
2834 	{
2835 	  /* Merge the section attribute.
2836 	     We want to issue an error if the sections conflict but that must be
2837 	     done later in decl_attributes since we are called before attributes
2838 	     are assigned.  */
2839 	  if (DECL_SECTION_NAME (newdecl) != NULL)
2840 	    set_decl_section_name (olddecl, DECL_SECTION_NAME (newdecl));
2841 
2842 	  if (DECL_ONE_ONLY (newdecl))
2843 	    {
2844 	      struct symtab_node *oldsym, *newsym;
2845 	      if (TREE_CODE (olddecl) == FUNCTION_DECL)
2846 		oldsym = cgraph_node::get_create (olddecl);
2847 	      else
2848 		oldsym = varpool_node::get_create (olddecl);
2849 	      newsym = symtab_node::get (newdecl);
2850 	      oldsym->set_comdat_group (newsym->get_comdat_group ());
2851 	    }
2852 	}
2853 
2854       if (VAR_P (newdecl)
2855 	  && CP_DECL_THREAD_LOCAL_P (newdecl))
2856 	{
2857 	  CP_DECL_THREAD_LOCAL_P (olddecl) = true;
2858 	  if (!processing_template_decl)
2859 	    set_decl_tls_model (olddecl, DECL_TLS_MODEL (newdecl));
2860 	}
2861     }
2862 
2863   DECL_UID (olddecl) = olddecl_uid;
2864   if (olddecl_friend)
2865     DECL_FRIEND_P (olddecl) = 1;
2866   if (hidden_friend)
2867     {
2868       DECL_ANTICIPATED (olddecl) = 1;
2869       DECL_HIDDEN_FRIEND_P (olddecl) = 1;
2870     }
2871 
2872   /* NEWDECL contains the merged attribute lists.
2873      Update OLDDECL to be the same.  */
2874   DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
2875 
2876   /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
2877     so that encode_section_info has a chance to look at the new decl
2878     flags and attributes.  */
2879   if (DECL_RTL_SET_P (olddecl)
2880       && (TREE_CODE (olddecl) == FUNCTION_DECL
2881 	  || (VAR_P (olddecl)
2882 	      && TREE_STATIC (olddecl))))
2883     make_decl_rtl (olddecl);
2884 
2885   /* The NEWDECL will no longer be needed.  Because every out-of-class
2886      declaration of a member results in a call to duplicate_decls,
2887      freeing these nodes represents in a significant savings.
2888 
2889      Before releasing the node, be sore to remove function from symbol
2890      table that might have been inserted there to record comdat group.
2891      Be sure to however do not free DECL_STRUCT_FUNCTION because this
2892      structure is shared in between newdecl and oldecl.  */
2893   if (TREE_CODE (newdecl) == FUNCTION_DECL)
2894     DECL_STRUCT_FUNCTION (newdecl) = NULL;
2895   if (VAR_OR_FUNCTION_DECL_P (newdecl))
2896     {
2897       struct symtab_node *snode = symtab_node::get (newdecl);
2898       if (snode)
2899 	snode->remove ();
2900     }
2901 
2902   if (TREE_CODE (olddecl) == FUNCTION_DECL)
2903     {
2904       tree clone;
2905       FOR_EACH_CLONE (clone, olddecl)
2906 	{
2907 	  DECL_ATTRIBUTES (clone) = DECL_ATTRIBUTES (olddecl);
2908 	  DECL_PRESERVE_P (clone) |= DECL_PRESERVE_P (olddecl);
2909 	}
2910     }
2911 
2912   /* Remove the associated constraints for newdecl, if any, before
2913      reclaiming memory. */
2914   if (flag_concepts)
2915     remove_constraints (newdecl);
2916 
2917   ggc_free (newdecl);
2918 
2919   return olddecl;
2920 }
2921 
2922 /* Return zero if the declaration NEWDECL is valid
2923    when the declaration OLDDECL (assumed to be for the same name)
2924    has already been seen.
2925    Otherwise return an error message format string with a %s
2926    where the identifier should go.  */
2927 
2928 static const char *
redeclaration_error_message(tree newdecl,tree olddecl)2929 redeclaration_error_message (tree newdecl, tree olddecl)
2930 {
2931   if (TREE_CODE (newdecl) == TYPE_DECL)
2932     {
2933       /* Because C++ can put things into name space for free,
2934 	 constructs like "typedef struct foo { ... } foo"
2935 	 would look like an erroneous redeclaration.  */
2936       if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl)))
2937 	return NULL;
2938       else
2939 	return G_("redefinition of %q#D");
2940     }
2941   else if (TREE_CODE (newdecl) == FUNCTION_DECL)
2942     {
2943       /* If this is a pure function, its olddecl will actually be
2944 	 the original initialization to `0' (which we force to call
2945 	 abort()).  Don't complain about redefinition in this case.  */
2946       if (DECL_LANG_SPECIFIC (olddecl) && DECL_PURE_VIRTUAL_P (olddecl)
2947 	  && DECL_INITIAL (olddecl) == NULL_TREE)
2948 	return NULL;
2949 
2950       /* If both functions come from different namespaces, this is not
2951 	 a redeclaration - this is a conflict with a used function.  */
2952       if (DECL_NAMESPACE_SCOPE_P (olddecl)
2953 	  && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl)
2954 	  && ! decls_match (olddecl, newdecl))
2955 	return G_("%qD conflicts with used function");
2956 
2957       /* We'll complain about linkage mismatches in
2958 	 warn_extern_redeclared_static.  */
2959 
2960       /* Defining the same name twice is no good.  */
2961       if (decl_defined_p (olddecl)
2962 	  && decl_defined_p (newdecl))
2963 	{
2964 	  if (DECL_NAME (olddecl) == NULL_TREE)
2965 	    return G_("%q#D not declared in class");
2966 	  else if (!GNU_INLINE_P (olddecl)
2967 		   || GNU_INLINE_P (newdecl))
2968 	    return G_("redefinition of %q#D");
2969 	}
2970 
2971       if (DECL_DECLARED_INLINE_P (olddecl) && DECL_DECLARED_INLINE_P (newdecl))
2972 	{
2973 	  bool olda = GNU_INLINE_P (olddecl);
2974 	  bool newa = GNU_INLINE_P (newdecl);
2975 
2976 	  if (olda != newa)
2977 	    {
2978 	      if (newa)
2979 		return G_("%q+D redeclared inline with "
2980 			  "%<gnu_inline%> attribute");
2981 	      else
2982 		return G_("%q+D redeclared inline without "
2983 			  "%<gnu_inline%> attribute");
2984 	    }
2985 	}
2986 
2987       /* [class.compare.default]: A definition of a comparison operator as
2988 	 defaulted that appears in a class shall be the first declaration of
2989 	 that function.  */
2990       special_function_kind sfk = special_function_p (olddecl);
2991       if (sfk == sfk_comparison && DECL_DEFAULTED_FN (newdecl))
2992 	return G_("comparison operator %q+D defaulted after "
2993 		  "its first declaration");
2994 
2995       check_abi_tag_redeclaration
2996 	(olddecl, lookup_attribute ("abi_tag", DECL_ATTRIBUTES (olddecl)),
2997 	 lookup_attribute ("abi_tag", DECL_ATTRIBUTES (newdecl)));
2998 
2999       return NULL;
3000     }
3001   else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
3002     {
3003       tree nt, ot;
3004 
3005       if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == CONCEPT_DECL)
3006         return G_("redefinition of %q#D");
3007 
3008       if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) != FUNCTION_DECL)
3009 	return redeclaration_error_message (DECL_TEMPLATE_RESULT (newdecl),
3010 					    DECL_TEMPLATE_RESULT (olddecl));
3011 
3012       if (DECL_TEMPLATE_RESULT (newdecl) == DECL_TEMPLATE_RESULT (olddecl))
3013 	return NULL;
3014 
3015       nt = DECL_TEMPLATE_RESULT (newdecl);
3016       if (DECL_TEMPLATE_INFO (nt))
3017 	nt = DECL_TEMPLATE_RESULT (template_for_substitution (nt));
3018       ot = DECL_TEMPLATE_RESULT (olddecl);
3019       if (DECL_TEMPLATE_INFO (ot))
3020 	ot = DECL_TEMPLATE_RESULT (template_for_substitution (ot));
3021       if (DECL_INITIAL (nt) && DECL_INITIAL (ot)
3022 	  && (!GNU_INLINE_P (ot) || GNU_INLINE_P (nt)))
3023 	return G_("redefinition of %q#D");
3024 
3025       if (DECL_DECLARED_INLINE_P (ot) && DECL_DECLARED_INLINE_P (nt))
3026 	{
3027 	  bool olda = GNU_INLINE_P (ot);
3028 	  bool newa = GNU_INLINE_P (nt);
3029 
3030 	  if (olda != newa)
3031 	    {
3032 	      if (newa)
3033 		return G_("%q+D redeclared inline with "
3034 			  "%<gnu_inline%> attribute");
3035 	      else
3036 		return G_("%q+D redeclared inline without "
3037 		     	  "%<gnu_inline%> attribute");
3038 	    }
3039 	}
3040 
3041       /* Core issue #226 (C++0x):
3042 
3043            If a friend function template declaration specifies a
3044            default template-argument, that declaration shall be a
3045            definition and shall be the only declaration of the
3046            function template in the translation unit.  */
3047       if ((cxx_dialect != cxx98)
3048           && TREE_CODE (ot) == FUNCTION_DECL && DECL_FRIEND_P (ot)
3049           && !check_default_tmpl_args (nt, DECL_TEMPLATE_PARMS (newdecl),
3050                                        /*is_primary=*/true,
3051 				       /*is_partial=*/false,
3052                                        /*is_friend_decl=*/2))
3053         return G_("redeclaration of friend %q#D "
3054 	 	  "may not have default template arguments");
3055 
3056       return NULL;
3057     }
3058   else if (VAR_P (newdecl)
3059 	   && CP_DECL_THREAD_LOCAL_P (newdecl) != CP_DECL_THREAD_LOCAL_P (olddecl)
3060 	   && (! DECL_LANG_SPECIFIC (olddecl)
3061 	       || ! CP_DECL_THREADPRIVATE_P (olddecl)
3062 	       || CP_DECL_THREAD_LOCAL_P (newdecl)))
3063     {
3064       /* Only variables can be thread-local, and all declarations must
3065 	 agree on this property.  */
3066       if (CP_DECL_THREAD_LOCAL_P (newdecl))
3067 	return G_("thread-local declaration of %q#D follows "
3068 	          "non-thread-local declaration");
3069       else
3070 	return G_("non-thread-local declaration of %q#D follows "
3071 	          "thread-local declaration");
3072     }
3073   else if (toplevel_bindings_p () || DECL_NAMESPACE_SCOPE_P (newdecl))
3074     {
3075       /* The objects have been declared at namespace scope.  If either
3076 	 is a member of an anonymous union, then this is an invalid
3077 	 redeclaration.  For example:
3078 
3079 	   int i;
3080 	   union { int i; };
3081 
3082 	   is invalid.  */
3083       if ((VAR_P (newdecl) && DECL_ANON_UNION_VAR_P (newdecl))
3084 	  || (VAR_P (olddecl) && DECL_ANON_UNION_VAR_P (olddecl)))
3085 	return G_("redeclaration of %q#D");
3086       /* If at least one declaration is a reference, there is no
3087 	 conflict.  For example:
3088 
3089 	   int i = 3;
3090 	   extern int i;
3091 
3092 	 is valid.  */
3093       if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
3094 	return NULL;
3095 
3096       /* Static data member declared outside a class definition
3097 	 if the variable is defined within the class with constexpr
3098 	 specifier is declaration rather than definition (and
3099 	 deprecated).  */
3100       if (cxx_dialect >= cxx17
3101 	  && VAR_P (olddecl)
3102 	  && DECL_CLASS_SCOPE_P (olddecl)
3103 	  && DECL_DECLARED_CONSTEXPR_P (olddecl)
3104 	  && !DECL_INITIAL (newdecl))
3105 	{
3106 	  DECL_EXTERNAL (newdecl) = 1;
3107 	  /* For now, only warn with explicit -Wdeprecated.  */
3108 	  if (global_options_set.x_warn_deprecated)
3109 	    {
3110 	      auto_diagnostic_group d;
3111 	      if (warning_at (DECL_SOURCE_LOCATION (newdecl), OPT_Wdeprecated,
3112 				"redundant redeclaration of %<constexpr%> "
3113 				"static data member %qD", newdecl))
3114 		inform (DECL_SOURCE_LOCATION (olddecl),
3115 			  "previous declaration of %qD", olddecl);
3116 	    }
3117 	  return NULL;
3118 	}
3119 
3120       /* Reject two definitions.  */
3121       return G_("redefinition of %q#D");
3122     }
3123   else
3124     {
3125       /* Objects declared with block scope:  */
3126       /* Reject two definitions, and reject a definition
3127 	 together with an external reference.  */
3128       if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl)))
3129 	return G_("redeclaration of %q#D");
3130       return NULL;
3131     }
3132 }
3133 
3134 
3135 /* Hash and equality functions for the named_label table.  */
3136 
3137 hashval_t
hash(const value_type entry)3138 named_label_hash::hash (const value_type entry)
3139 {
3140   return IDENTIFIER_HASH_VALUE (entry->name);
3141 }
3142 
3143 bool
equal(const value_type entry,compare_type name)3144 named_label_hash::equal (const value_type entry, compare_type name)
3145 {
3146   return name == entry->name;
3147 }
3148 
3149 /* Look for a label named ID in the current function.  If one cannot
3150    be found, create one.  Return the named_label_entry, or NULL on
3151    failure.  */
3152 
3153 static named_label_entry *
lookup_label_1(tree id,bool making_local_p)3154 lookup_label_1 (tree id, bool making_local_p)
3155 {
3156   /* You can't use labels at global scope.  */
3157   if (current_function_decl == NULL_TREE)
3158     {
3159       error ("label %qE referenced outside of any function", id);
3160       return NULL;
3161     }
3162 
3163   if (!named_labels)
3164     named_labels = hash_table<named_label_hash>::create_ggc (13);
3165 
3166   hashval_t hash = IDENTIFIER_HASH_VALUE (id);
3167   named_label_entry **slot
3168     = named_labels->find_slot_with_hash (id, hash, INSERT);
3169   named_label_entry *old = *slot;
3170 
3171   if (old && old->label_decl)
3172     {
3173       if (!making_local_p)
3174 	return old;
3175 
3176       if (old->binding_level == current_binding_level)
3177 	{
3178 	  error ("local label %qE conflicts with existing label", id);
3179 	  inform (DECL_SOURCE_LOCATION (old->label_decl), "previous label");
3180 	  return NULL;
3181 	}
3182     }
3183 
3184   /* We are making a new decl, create or reuse the named_label_entry  */
3185   named_label_entry *ent = NULL;
3186   if (old && !old->label_decl)
3187     ent = old;
3188   else
3189     {
3190       ent = ggc_cleared_alloc<named_label_entry> ();
3191       ent->name = id;
3192       ent->outer = old;
3193       *slot = ent;
3194     }
3195 
3196   /* Now create the LABEL_DECL.  */
3197   tree decl = build_decl (input_location, LABEL_DECL, id, void_type_node);
3198 
3199   DECL_CONTEXT (decl) = current_function_decl;
3200   SET_DECL_MODE (decl, VOIDmode);
3201   if (making_local_p)
3202     {
3203       C_DECLARED_LABEL_FLAG (decl) = true;
3204       DECL_CHAIN (decl) = current_binding_level->names;
3205       current_binding_level->names = decl;
3206     }
3207 
3208   ent->label_decl = decl;
3209 
3210   return ent;
3211 }
3212 
3213 /* Wrapper for lookup_label_1.  */
3214 
3215 tree
lookup_label(tree id)3216 lookup_label (tree id)
3217 {
3218   bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3219   named_label_entry *ent = lookup_label_1 (id, false);
3220   timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3221   return ent ? ent->label_decl : NULL_TREE;
3222 }
3223 
3224 tree
declare_local_label(tree id)3225 declare_local_label (tree id)
3226 {
3227   bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3228   named_label_entry *ent = lookup_label_1 (id, true);
3229   timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3230   return ent ? ent->label_decl : NULL_TREE;
3231 }
3232 
3233 /* Returns nonzero if it is ill-formed to jump past the declaration of
3234    DECL.  Returns 2 if it's also a real problem.  */
3235 
3236 static int
decl_jump_unsafe(tree decl)3237 decl_jump_unsafe (tree decl)
3238 {
3239   /* [stmt.dcl]/3: A program that jumps from a point where a local variable
3240      with automatic storage duration is not in scope to a point where it is
3241      in scope is ill-formed unless the variable has scalar type, class type
3242      with a trivial default constructor and a trivial destructor, a
3243      cv-qualified version of one of these types, or an array of one of the
3244      preceding types and is declared without an initializer (8.5).  */
3245   tree type = TREE_TYPE (decl);
3246 
3247   if (!VAR_P (decl) || TREE_STATIC (decl)
3248       || type == error_mark_node)
3249     return 0;
3250 
3251   if (DECL_NONTRIVIALLY_INITIALIZED_P (decl)
3252       || variably_modified_type_p (type, NULL_TREE))
3253     return 2;
3254 
3255   if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3256     return 1;
3257 
3258   return 0;
3259 }
3260 
3261 /* A subroutine of check_previous_goto_1 and check_goto to identify a branch
3262    to the user.  */
3263 
3264 static bool
identify_goto(tree decl,location_t loc,const location_t * locus,diagnostic_t diag_kind)3265 identify_goto (tree decl, location_t loc, const location_t *locus,
3266 	       diagnostic_t diag_kind)
3267 {
3268   bool complained
3269     = emit_diagnostic (diag_kind, loc, 0,
3270 		       decl ? N_("jump to label %qD")
3271 		       : N_("jump to case label"), decl);
3272   if (complained && locus)
3273     inform (*locus, "  from here");
3274   return complained;
3275 }
3276 
3277 /* Check that a single previously seen jump to a newly defined label
3278    is OK.  DECL is the LABEL_DECL or 0; LEVEL is the binding_level for
3279    the jump context; NAMES are the names in scope in LEVEL at the jump
3280    context; LOCUS is the source position of the jump or 0.  Returns
3281    true if all is well.  */
3282 
3283 static bool
check_previous_goto_1(tree decl,cp_binding_level * level,tree names,bool exited_omp,const location_t * locus)3284 check_previous_goto_1 (tree decl, cp_binding_level* level, tree names,
3285 		       bool exited_omp, const location_t *locus)
3286 {
3287   cp_binding_level *b;
3288   bool complained = false;
3289   int identified = 0;
3290   bool saw_eh = false, saw_omp = false, saw_tm = false, saw_cxif = false;
3291 
3292   if (exited_omp)
3293     {
3294       complained = identify_goto (decl, input_location, locus, DK_ERROR);
3295       if (complained)
3296 	inform (input_location, "  exits OpenMP structured block");
3297       saw_omp = true;
3298       identified = 2;
3299     }
3300 
3301   for (b = current_binding_level; b ; b = b->level_chain)
3302     {
3303       tree new_decls, old_decls = (b == level ? names : NULL_TREE);
3304 
3305       for (new_decls = b->names; new_decls != old_decls;
3306 	   new_decls = (DECL_P (new_decls) ? DECL_CHAIN (new_decls)
3307 			: TREE_CHAIN (new_decls)))
3308 	{
3309 	  int problem = decl_jump_unsafe (new_decls);
3310 	  if (! problem)
3311 	    continue;
3312 
3313 	  if (!identified)
3314 	    {
3315 	      complained = identify_goto (decl, input_location, locus,
3316 					  problem > 1
3317 					  ? DK_ERROR : DK_PERMERROR);
3318 	      identified = 1;
3319 	    }
3320 	  if (complained)
3321 	    {
3322 	      if (problem > 1)
3323 		inform (DECL_SOURCE_LOCATION (new_decls),
3324 			"  crosses initialization of %q#D", new_decls);
3325 	      else
3326 		inform (DECL_SOURCE_LOCATION (new_decls),
3327 			"  enters scope of %q#D, which has "
3328 			"non-trivial destructor", new_decls);
3329 	    }
3330 	}
3331 
3332       if (b == level)
3333 	break;
3334 
3335       const char *inf = NULL;
3336       location_t loc = input_location;
3337       switch (b->kind)
3338 	{
3339 	case sk_try:
3340 	  if (!saw_eh)
3341 	    inf = G_("  enters %<try%> block");
3342 	  saw_eh = true;
3343 	  break;
3344 
3345 	case sk_catch:
3346 	  if (!saw_eh)
3347 	    inf = G_("  enters %<catch%> block");
3348 	  saw_eh = true;
3349 	  break;
3350 
3351 	case sk_omp:
3352 	  if (!saw_omp)
3353 	    inf = G_("  enters OpenMP structured block");
3354 	  saw_omp = true;
3355 	  break;
3356 
3357 	case sk_transaction:
3358 	  if (!saw_tm)
3359 	    inf = G_("  enters synchronized or atomic statement");
3360 	  saw_tm = true;
3361 	  break;
3362 
3363 	case sk_block:
3364 	  if (!saw_cxif && level_for_constexpr_if (b->level_chain))
3365 	    {
3366 	      inf = G_("  enters %<constexpr if%> statement");
3367 	      loc = EXPR_LOCATION (b->level_chain->this_entity);
3368 	      saw_cxif = true;
3369 	    }
3370 	  break;
3371 
3372 	default:
3373 	  break;
3374 	}
3375 
3376       if (inf)
3377 	{
3378 	  if (identified < 2)
3379 	    complained = identify_goto (decl, input_location, locus, DK_ERROR);
3380 	  identified = 2;
3381 	  if (complained)
3382 	    inform (loc, inf);
3383 	}
3384     }
3385 
3386   return !identified;
3387 }
3388 
3389 static void
check_previous_goto(tree decl,struct named_label_use_entry * use)3390 check_previous_goto (tree decl, struct named_label_use_entry *use)
3391 {
3392   check_previous_goto_1 (decl, use->binding_level,
3393 			 use->names_in_scope, use->in_omp_scope,
3394 			 &use->o_goto_locus);
3395 }
3396 
3397 static bool
check_switch_goto(cp_binding_level * level)3398 check_switch_goto (cp_binding_level* level)
3399 {
3400   return check_previous_goto_1 (NULL_TREE, level, level->names, false, NULL);
3401 }
3402 
3403 /* Check that a new jump to a label DECL is OK.  Called by
3404    finish_goto_stmt.  */
3405 
3406 void
check_goto(tree decl)3407 check_goto (tree decl)
3408 {
3409   /* We can't know where a computed goto is jumping.
3410      So we assume that it's OK.  */
3411   if (TREE_CODE (decl) != LABEL_DECL)
3412     return;
3413 
3414   /* We didn't record any information about this label when we created it,
3415      and there's not much point since it's trivial to analyze as a return.  */
3416   if (decl == cdtor_label)
3417     return;
3418 
3419   hashval_t hash = IDENTIFIER_HASH_VALUE (DECL_NAME (decl));
3420   named_label_entry **slot
3421     = named_labels->find_slot_with_hash (DECL_NAME (decl), hash, NO_INSERT);
3422   named_label_entry *ent = *slot;
3423 
3424   /* If the label hasn't been defined yet, defer checking.  */
3425   if (! DECL_INITIAL (decl))
3426     {
3427       /* Don't bother creating another use if the last goto had the
3428 	 same data, and will therefore create the same set of errors.  */
3429       if (ent->uses
3430 	  && ent->uses->names_in_scope == current_binding_level->names)
3431 	return;
3432 
3433       named_label_use_entry *new_use
3434 	= ggc_alloc<named_label_use_entry> ();
3435       new_use->binding_level = current_binding_level;
3436       new_use->names_in_scope = current_binding_level->names;
3437       new_use->o_goto_locus = input_location;
3438       new_use->in_omp_scope = false;
3439 
3440       new_use->next = ent->uses;
3441       ent->uses = new_use;
3442       return;
3443     }
3444 
3445   bool saw_catch = false, complained = false;
3446   int identified = 0;
3447   tree bad;
3448   unsigned ix;
3449 
3450   if (ent->in_try_scope || ent->in_catch_scope || ent->in_transaction_scope
3451       || ent->in_constexpr_if
3452       || ent->in_omp_scope || !vec_safe_is_empty (ent->bad_decls))
3453     {
3454       diagnostic_t diag_kind = DK_PERMERROR;
3455       if (ent->in_try_scope || ent->in_catch_scope || ent->in_constexpr_if
3456 	  || ent->in_transaction_scope || ent->in_omp_scope)
3457 	diag_kind = DK_ERROR;
3458       complained = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
3459 				  &input_location, diag_kind);
3460       identified = 1 + (diag_kind == DK_ERROR);
3461     }
3462 
3463   FOR_EACH_VEC_SAFE_ELT (ent->bad_decls, ix, bad)
3464     {
3465       int u = decl_jump_unsafe (bad);
3466 
3467       if (u > 1 && DECL_ARTIFICIAL (bad))
3468 	{
3469 	  /* Can't skip init of __exception_info.  */
3470 	  if (identified == 1)
3471 	    {
3472 	      complained = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
3473 					  &input_location, DK_ERROR);
3474 	      identified = 2;
3475 	    }
3476 	  if (complained)
3477 	    inform (DECL_SOURCE_LOCATION (bad), "  enters %<catch%> block");
3478 	  saw_catch = true;
3479 	}
3480       else if (complained)
3481 	{
3482 	  if (u > 1)
3483 	    inform (DECL_SOURCE_LOCATION (bad),
3484 		    "  skips initialization of %q#D", bad);
3485 	  else
3486 	    inform (DECL_SOURCE_LOCATION (bad),
3487 		    "  enters scope of %q#D which has "
3488 		    "non-trivial destructor", bad);
3489 	}
3490     }
3491 
3492   if (complained)
3493     {
3494       if (ent->in_try_scope)
3495 	inform (input_location, "  enters %<try%> block");
3496       else if (ent->in_catch_scope && !saw_catch)
3497 	inform (input_location, "  enters %<catch%> block");
3498       else if (ent->in_transaction_scope)
3499 	inform (input_location, "  enters synchronized or atomic statement");
3500       else if (ent->in_constexpr_if)
3501 	inform (input_location, "  enters %<constexpr if%> statement");
3502     }
3503 
3504   if (ent->in_omp_scope)
3505     {
3506       if (complained)
3507 	inform (input_location, "  enters OpenMP structured block");
3508     }
3509   else if (flag_openmp)
3510     for (cp_binding_level *b = current_binding_level; b ; b = b->level_chain)
3511       {
3512 	if (b == ent->binding_level)
3513 	  break;
3514 	if (b->kind == sk_omp)
3515 	  {
3516 	    if (identified < 2)
3517 	      {
3518 		complained = identify_goto (decl,
3519 					    DECL_SOURCE_LOCATION (decl),
3520 					    &input_location, DK_ERROR);
3521 		identified = 2;
3522 	      }
3523 	    if (complained)
3524 	      inform (input_location, "  exits OpenMP structured block");
3525 	    break;
3526 	  }
3527       }
3528 }
3529 
3530 /* Check that a return is ok wrt OpenMP structured blocks.
3531    Called by finish_return_stmt.  Returns true if all is well.  */
3532 
3533 bool
check_omp_return(void)3534 check_omp_return (void)
3535 {
3536   for (cp_binding_level *b = current_binding_level; b ; b = b->level_chain)
3537     if (b->kind == sk_omp)
3538       {
3539 	error ("invalid exit from OpenMP structured block");
3540 	return false;
3541       }
3542     else if (b->kind == sk_function_parms)
3543       break;
3544   return true;
3545 }
3546 
3547 /* Define a label, specifying the location in the source file.
3548    Return the LABEL_DECL node for the label.  */
3549 
3550 static tree
define_label_1(location_t location,tree name)3551 define_label_1 (location_t location, tree name)
3552 {
3553   /* After labels, make any new cleanups in the function go into their
3554      own new (temporary) binding contour.  */
3555   for (cp_binding_level *p = current_binding_level;
3556        p->kind != sk_function_parms;
3557        p = p->level_chain)
3558     p->more_cleanups_ok = 0;
3559 
3560   named_label_entry *ent = lookup_label_1 (name, false);
3561   tree decl = ent->label_decl;
3562 
3563   if (DECL_INITIAL (decl) != NULL_TREE)
3564     {
3565       error ("duplicate label %qD", decl);
3566       return error_mark_node;
3567     }
3568   else
3569     {
3570       /* Mark label as having been defined.  */
3571       DECL_INITIAL (decl) = error_mark_node;
3572       /* Say where in the source.  */
3573       DECL_SOURCE_LOCATION (decl) = location;
3574 
3575       ent->binding_level = current_binding_level;
3576       ent->names_in_scope = current_binding_level->names;
3577 
3578       for (named_label_use_entry *use = ent->uses; use; use = use->next)
3579 	check_previous_goto (decl, use);
3580       ent->uses = NULL;
3581     }
3582 
3583   return decl;
3584 }
3585 
3586 /* Wrapper for define_label_1.  */
3587 
3588 tree
define_label(location_t location,tree name)3589 define_label (location_t location, tree name)
3590 {
3591   bool running = timevar_cond_start (TV_NAME_LOOKUP);
3592   tree ret = define_label_1 (location, name);
3593   timevar_cond_stop (TV_NAME_LOOKUP, running);
3594   return ret;
3595 }
3596 
3597 
3598 struct cp_switch
3599 {
3600   cp_binding_level *level;
3601   struct cp_switch *next;
3602   /* The SWITCH_STMT being built.  */
3603   tree switch_stmt;
3604   /* A splay-tree mapping the low element of a case range to the high
3605      element, or NULL_TREE if there is no high element.  Used to
3606      determine whether or not a new case label duplicates an old case
3607      label.  We need a tree, rather than simply a hash table, because
3608      of the GNU case range extension.  */
3609   splay_tree cases;
3610   /* Remember whether a default: case label has been seen.  */
3611   bool has_default_p;
3612   /* Remember whether a BREAK_STMT has been seen in this SWITCH_STMT.  */
3613   bool break_stmt_seen_p;
3614   /* Set if inside of {FOR,DO,WHILE}_BODY nested inside of a switch,
3615      where BREAK_STMT doesn't belong to the SWITCH_STMT.  */
3616   bool in_loop_body_p;
3617 };
3618 
3619 /* A stack of the currently active switch statements.  The innermost
3620    switch statement is on the top of the stack.  There is no need to
3621    mark the stack for garbage collection because it is only active
3622    during the processing of the body of a function, and we never
3623    collect at that point.  */
3624 
3625 static struct cp_switch *switch_stack;
3626 
3627 /* Called right after a switch-statement condition is parsed.
3628    SWITCH_STMT is the switch statement being parsed.  */
3629 
3630 void
push_switch(tree switch_stmt)3631 push_switch (tree switch_stmt)
3632 {
3633   struct cp_switch *p = XNEW (struct cp_switch);
3634   p->level = current_binding_level;
3635   p->next = switch_stack;
3636   p->switch_stmt = switch_stmt;
3637   p->cases = splay_tree_new (case_compare, NULL, NULL);
3638   p->has_default_p = false;
3639   p->break_stmt_seen_p = false;
3640   p->in_loop_body_p = false;
3641   switch_stack = p;
3642 }
3643 
3644 void
pop_switch(void)3645 pop_switch (void)
3646 {
3647   struct cp_switch *cs = switch_stack;
3648 
3649   /* Emit warnings as needed.  */
3650   location_t switch_location = cp_expr_loc_or_input_loc (cs->switch_stmt);
3651   tree cond = SWITCH_STMT_COND (cs->switch_stmt);
3652   const bool bool_cond_p
3653     = (SWITCH_STMT_TYPE (cs->switch_stmt)
3654        && TREE_CODE (SWITCH_STMT_TYPE (cs->switch_stmt)) == BOOLEAN_TYPE);
3655   if (!processing_template_decl)
3656     c_do_switch_warnings (cs->cases, switch_location,
3657 			  SWITCH_STMT_TYPE (cs->switch_stmt), cond,
3658 			  bool_cond_p);
3659 
3660   /* For the benefit of block_may_fallthru remember if the switch body
3661      case labels cover all possible values and if there are break; stmts.  */
3662   if (cs->has_default_p
3663       || (!processing_template_decl
3664 	  && c_switch_covers_all_cases_p (cs->cases,
3665 					  SWITCH_STMT_TYPE (cs->switch_stmt))))
3666     SWITCH_STMT_ALL_CASES_P (cs->switch_stmt) = 1;
3667   if (!cs->break_stmt_seen_p)
3668     SWITCH_STMT_NO_BREAK_P (cs->switch_stmt) = 1;
3669   /* Now that we're done with the switch warnings, set the switch type
3670      to the type of the condition if the index type was of scoped enum type.
3671      (Such types don't participate in the integer promotions.)  We do this
3672      because of bit-fields whose declared type is a scoped enum type:
3673      gimplification will use the lowered index type, but convert the
3674      case values to SWITCH_STMT_TYPE, which would have been the declared type
3675      and verify_gimple_switch doesn't accept that.  */
3676   if (is_bitfield_expr_with_lowered_type (cond))
3677     SWITCH_STMT_TYPE (cs->switch_stmt) = TREE_TYPE (cond);
3678   gcc_assert (!cs->in_loop_body_p);
3679   splay_tree_delete (cs->cases);
3680   switch_stack = switch_stack->next;
3681   free (cs);
3682 }
3683 
3684 /* Note that a BREAK_STMT is about to be added.  If it is inside of
3685    a SWITCH_STMT and not inside of a loop body inside of it, note
3686    in switch_stack we've seen a BREAK_STMT.  */
3687 
3688 void
note_break_stmt(void)3689 note_break_stmt (void)
3690 {
3691   if (switch_stack && !switch_stack->in_loop_body_p)
3692     switch_stack->break_stmt_seen_p = true;
3693 }
3694 
3695 /* Note the start of processing of an iteration statement's body.
3696    The note_break_stmt function will do nothing while processing it.
3697    Return a flag that should be passed to note_iteration_stmt_body_end.  */
3698 
3699 bool
note_iteration_stmt_body_start(void)3700 note_iteration_stmt_body_start (void)
3701 {
3702   if (!switch_stack)
3703     return false;
3704   bool ret = switch_stack->in_loop_body_p;
3705   switch_stack->in_loop_body_p = true;
3706   return ret;
3707 }
3708 
3709 /* Note the end of processing of an iteration statement's body.  */
3710 
3711 void
note_iteration_stmt_body_end(bool prev)3712 note_iteration_stmt_body_end (bool prev)
3713 {
3714   if (switch_stack)
3715     switch_stack->in_loop_body_p = prev;
3716 }
3717 
3718 /* Convert a case constant VALUE in a switch to the type TYPE of the switch
3719    condition.  Note that if TYPE and VALUE are already integral we don't
3720    really do the conversion because the language-independent
3721    warning/optimization code will work better that way.  */
3722 
3723 static tree
case_conversion(tree type,tree value)3724 case_conversion (tree type, tree value)
3725 {
3726   if (value == NULL_TREE)
3727     return value;
3728 
3729   value = mark_rvalue_use (value);
3730 
3731   if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
3732     type = type_promotes_to (type);
3733 
3734   tree ovalue = value;
3735   /* The constant-expression VALUE shall be a converted constant expression
3736      of the adjusted type of the switch condition, which doesn't allow
3737      narrowing conversions.  */
3738   value = build_converted_constant_expr (type, value, tf_warning_or_error);
3739 
3740   if (cxx_dialect >= cxx11
3741       && (SCOPED_ENUM_P (type)
3742 	  || !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (ovalue))))
3743     /* Use the converted value.  */;
3744   else
3745     /* The already integral case.  */
3746     value = ovalue;
3747 
3748   return cxx_constant_value (value);
3749 }
3750 
3751 /* Note that we've seen a definition of a case label, and complain if this
3752    is a bad place for one.  */
3753 
3754 tree
finish_case_label(location_t loc,tree low_value,tree high_value)3755 finish_case_label (location_t loc, tree low_value, tree high_value)
3756 {
3757   tree cond, r;
3758   cp_binding_level *p;
3759   tree type;
3760 
3761   if (low_value == NULL_TREE && high_value == NULL_TREE)
3762     switch_stack->has_default_p = true;
3763 
3764   if (processing_template_decl)
3765     {
3766       tree label;
3767 
3768       /* For templates, just add the case label; we'll do semantic
3769 	 analysis at instantiation-time.  */
3770       label = build_decl (loc, LABEL_DECL, NULL_TREE, void_type_node);
3771       return add_stmt (build_case_label (low_value, high_value, label));
3772     }
3773 
3774   /* Find the condition on which this switch statement depends.  */
3775   cond = SWITCH_STMT_COND (switch_stack->switch_stmt);
3776   if (cond && TREE_CODE (cond) == TREE_LIST)
3777     cond = TREE_VALUE (cond);
3778 
3779   if (!check_switch_goto (switch_stack->level))
3780     return error_mark_node;
3781 
3782   type = SWITCH_STMT_TYPE (switch_stack->switch_stmt);
3783   if (type == error_mark_node)
3784     return error_mark_node;
3785 
3786   low_value = case_conversion (type, low_value);
3787   high_value = case_conversion (type, high_value);
3788 
3789   r = c_add_case_label (loc, switch_stack->cases, cond, low_value, high_value);
3790 
3791   /* After labels, make any new cleanups in the function go into their
3792      own new (temporary) binding contour.  */
3793   for (p = current_binding_level;
3794        p->kind != sk_function_parms;
3795        p = p->level_chain)
3796     p->more_cleanups_ok = 0;
3797 
3798   return r;
3799 }
3800 
3801 struct typename_info {
3802   tree scope;
3803   tree name;
3804   tree template_id;
3805   bool enum_p;
3806   bool class_p;
3807 };
3808 
3809 struct typename_hasher : ggc_ptr_hash<tree_node>
3810 {
3811   typedef typename_info *compare_type;
3812 
3813   /* Hash a TYPENAME_TYPE.  */
3814 
3815   static hashval_t
hashtypename_hasher3816   hash (tree t)
3817   {
3818     hashval_t hash;
3819 
3820     hash = (htab_hash_pointer (TYPE_CONTEXT (t))
3821 	    ^ htab_hash_pointer (TYPE_IDENTIFIER (t)));
3822 
3823     return hash;
3824   }
3825 
3826   /* Compare two TYPENAME_TYPEs.  */
3827 
3828   static bool
equaltypename_hasher3829   equal (tree t1, const typename_info *t2)
3830   {
3831     return (TYPE_IDENTIFIER (t1) == t2->name
3832 	    && TYPE_CONTEXT (t1) == t2->scope
3833 	    && TYPENAME_TYPE_FULLNAME (t1) == t2->template_id
3834 	    && TYPENAME_IS_ENUM_P (t1) == t2->enum_p
3835 	    && TYPENAME_IS_CLASS_P (t1) == t2->class_p);
3836   }
3837 };
3838 
3839 /* Build a TYPENAME_TYPE.  If the type is `typename T::t', CONTEXT is
3840    the type of `T', NAME is the IDENTIFIER_NODE for `t'.
3841 
3842    Returns the new TYPENAME_TYPE.  */
3843 
3844 static GTY (()) hash_table<typename_hasher> *typename_htab;
3845 
3846 tree
build_typename_type(tree context,tree name,tree fullname,enum tag_types tag_type)3847 build_typename_type (tree context, tree name, tree fullname,
3848 		     enum tag_types tag_type)
3849 {
3850   tree t;
3851   tree d;
3852   typename_info ti;
3853   tree *e;
3854   hashval_t hash;
3855 
3856   if (typename_htab == NULL)
3857     typename_htab = hash_table<typename_hasher>::create_ggc (61);
3858 
3859   ti.scope = FROB_CONTEXT (context);
3860   ti.name = name;
3861   ti.template_id = fullname;
3862   ti.enum_p = tag_type == enum_type;
3863   ti.class_p = (tag_type == class_type
3864 		|| tag_type == record_type
3865 		|| tag_type == union_type);
3866   hash =  (htab_hash_pointer (ti.scope)
3867 	   ^ htab_hash_pointer (ti.name));
3868 
3869   /* See if we already have this type.  */
3870   e = typename_htab->find_slot_with_hash (&ti, hash, INSERT);
3871   if (*e)
3872     t = *e;
3873   else
3874     {
3875       /* Build the TYPENAME_TYPE.  */
3876       t = cxx_make_type (TYPENAME_TYPE);
3877       TYPE_CONTEXT (t) = ti.scope;
3878       TYPENAME_TYPE_FULLNAME (t) = ti.template_id;
3879       TYPENAME_IS_ENUM_P (t) = ti.enum_p;
3880       TYPENAME_IS_CLASS_P (t) = ti.class_p;
3881 
3882       /* Build the corresponding TYPE_DECL.  */
3883       d = build_decl (input_location, TYPE_DECL, name, t);
3884       TYPE_NAME (TREE_TYPE (d)) = d;
3885       TYPE_STUB_DECL (TREE_TYPE (d)) = d;
3886       DECL_CONTEXT (d) = FROB_CONTEXT (context);
3887       DECL_ARTIFICIAL (d) = 1;
3888 
3889       /* Store it in the hash table.  */
3890       *e = t;
3891 
3892       /* TYPENAME_TYPEs must always be compared structurally, because
3893 	 they may or may not resolve down to another type depending on
3894 	 the currently open classes. */
3895       SET_TYPE_STRUCTURAL_EQUALITY (t);
3896     }
3897 
3898   return t;
3899 }
3900 
3901 /* Resolve `typename CONTEXT::NAME'.  TAG_TYPE indicates the tag
3902    provided to name the type.  Returns an appropriate type, unless an
3903    error occurs, in which case error_mark_node is returned.  If we
3904    locate a non-artificial TYPE_DECL and TF_KEEP_TYPE_DECL is set, we
3905    return that, rather than the _TYPE it corresponds to, in other
3906    cases we look through the type decl.  If TF_ERROR is set, complain
3907    about errors, otherwise be quiet.  */
3908 
3909 tree
make_typename_type(tree context,tree name,enum tag_types tag_type,tsubst_flags_t complain)3910 make_typename_type (tree context, tree name, enum tag_types tag_type,
3911 		    tsubst_flags_t complain)
3912 {
3913   tree fullname;
3914   tree t;
3915   bool want_template;
3916 
3917   if (name == error_mark_node
3918       || context == NULL_TREE
3919       || context == error_mark_node)
3920     return error_mark_node;
3921 
3922   if (TYPE_P (name))
3923     {
3924       if (!(TYPE_LANG_SPECIFIC (name)
3925 	    && (CLASSTYPE_IS_TEMPLATE (name)
3926 		|| CLASSTYPE_USE_TEMPLATE (name))))
3927 	name = TYPE_IDENTIFIER (name);
3928       else
3929 	/* Create a TEMPLATE_ID_EXPR for the type.  */
3930 	name = build_nt (TEMPLATE_ID_EXPR,
3931 			 CLASSTYPE_TI_TEMPLATE (name),
3932 			 CLASSTYPE_TI_ARGS (name));
3933     }
3934   else if (TREE_CODE (name) == TYPE_DECL)
3935     name = DECL_NAME (name);
3936 
3937   fullname = name;
3938 
3939   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3940     {
3941       name = TREE_OPERAND (name, 0);
3942       if (DECL_TYPE_TEMPLATE_P (name))
3943 	name = TREE_OPERAND (fullname, 0) = DECL_NAME (name);
3944       if (TREE_CODE (name) != IDENTIFIER_NODE)
3945 	{
3946 	  if (complain & tf_error)
3947 	    error ("%qD is not a type", name);
3948 	  return error_mark_node;
3949 	}
3950     }
3951   if (TREE_CODE (name) == TEMPLATE_DECL)
3952     {
3953       if (complain & tf_error)
3954 	error ("%qD used without template arguments", name);
3955       return error_mark_node;
3956     }
3957   gcc_assert (identifier_p (name));
3958   gcc_assert (TYPE_P (context));
3959 
3960   if (TREE_CODE (context) == TYPE_PACK_EXPANSION)
3961     /* This can happen for C++17 variadic using (c++/88986).  */;
3962   else if (!MAYBE_CLASS_TYPE_P (context))
3963     {
3964       if (complain & tf_error)
3965 	error ("%q#T is not a class", context);
3966       return error_mark_node;
3967     }
3968 
3969   /* When the CONTEXT is a dependent type,  NAME could refer to a
3970      dependent base class of CONTEXT.  But look inside it anyway
3971      if CONTEXT is a currently open scope, in case it refers to a
3972      member of the current instantiation or a non-dependent base;
3973      lookup will stop when we hit a dependent base.  */
3974   if (!dependent_scope_p (context))
3975     /* We should only set WANT_TYPE when we're a nested typename type.
3976        Then we can give better diagnostics if we find a non-type.  */
3977     t = lookup_field (context, name, 2, /*want_type=*/true);
3978   else
3979     t = NULL_TREE;
3980 
3981   if ((!t || TREE_CODE (t) == TREE_LIST) && dependent_type_p (context))
3982     return build_typename_type (context, name, fullname, tag_type);
3983 
3984   want_template = TREE_CODE (fullname) == TEMPLATE_ID_EXPR;
3985 
3986   if (!t)
3987     {
3988       if (complain & tf_error)
3989 	{
3990 	  if (!COMPLETE_TYPE_P (context))
3991 	    cxx_incomplete_type_error (NULL_TREE, context);
3992 	  else
3993 	    error (want_template ? G_("no class template named %q#T in %q#T")
3994 		   : G_("no type named %q#T in %q#T"), name, context);
3995 	}
3996       return error_mark_node;
3997     }
3998 
3999   /* Pull out the template from an injected-class-name (or multiple).  */
4000   if (want_template)
4001     t = maybe_get_template_decl_from_type_decl (t);
4002 
4003   if (TREE_CODE (t) == TREE_LIST)
4004     {
4005       if (complain & tf_error)
4006 	{
4007 	  error ("lookup of %qT in %qT is ambiguous", name, context);
4008 	  print_candidates (t);
4009 	}
4010       return error_mark_node;
4011     }
4012 
4013   if (want_template && !DECL_TYPE_TEMPLATE_P (t))
4014     {
4015       if (complain & tf_error)
4016 	error ("%<typename %T::%D%> names %q#T, which is not a class template",
4017 	       context, name, t);
4018       return error_mark_node;
4019     }
4020   if (!want_template && TREE_CODE (t) != TYPE_DECL)
4021     {
4022       if (complain & tf_error)
4023 	error ("%<typename %T::%D%> names %q#T, which is not a type",
4024 	       context, name, t);
4025       return error_mark_node;
4026     }
4027 
4028   if (!perform_or_defer_access_check (TYPE_BINFO (context), t, t, complain))
4029     return error_mark_node;
4030 
4031   /* If we are currently parsing a template and if T is a typedef accessed
4032      through CONTEXT then we need to remember and check access of T at
4033      template instantiation time.  */
4034   add_typedef_to_current_template_for_access_check (t, context, input_location);
4035 
4036   if (want_template)
4037     return lookup_template_class (t, TREE_OPERAND (fullname, 1),
4038 				  NULL_TREE, context,
4039 				  /*entering_scope=*/0,
4040 				  complain | tf_user);
4041 
4042   if (DECL_ARTIFICIAL (t) || !(complain & tf_keep_type_decl))
4043     t = TREE_TYPE (t);
4044 
4045   maybe_record_typedef_use (t);
4046 
4047   return t;
4048 }
4049 
4050 /* Resolve `CONTEXT::template NAME'.  Returns a TEMPLATE_DECL if the name
4051    can be resolved or an UNBOUND_CLASS_TEMPLATE, unless an error occurs,
4052    in which case error_mark_node is returned.
4053 
4054    If PARM_LIST is non-NULL, also make sure that the template parameter
4055    list of TEMPLATE_DECL matches.
4056 
4057    If COMPLAIN zero, don't complain about any errors that occur.  */
4058 
4059 tree
make_unbound_class_template(tree context,tree name,tree parm_list,tsubst_flags_t complain)4060 make_unbound_class_template (tree context, tree name, tree parm_list,
4061 			     tsubst_flags_t complain)
4062 {
4063   tree t;
4064   tree d;
4065 
4066   if (TYPE_P (name))
4067     name = TYPE_IDENTIFIER (name);
4068   else if (DECL_P (name))
4069     name = DECL_NAME (name);
4070   gcc_assert (identifier_p (name));
4071 
4072   if (!dependent_type_p (context)
4073       || currently_open_class (context))
4074     {
4075       tree tmpl = NULL_TREE;
4076 
4077       if (MAYBE_CLASS_TYPE_P (context))
4078 	tmpl = lookup_field (context, name, 0, false);
4079 
4080       if (tmpl && TREE_CODE (tmpl) == TYPE_DECL)
4081 	tmpl = maybe_get_template_decl_from_type_decl (tmpl);
4082 
4083       if (!tmpl || !DECL_TYPE_TEMPLATE_P (tmpl))
4084 	{
4085 	  if (complain & tf_error)
4086 	    error ("no class template named %q#T in %q#T", name, context);
4087 	  return error_mark_node;
4088 	}
4089 
4090       if (parm_list
4091 	  && !comp_template_parms (DECL_TEMPLATE_PARMS (tmpl), parm_list))
4092 	{
4093 	  if (complain & tf_error)
4094 	    {
4095 	      error ("template parameters do not match template %qD", tmpl);
4096 	      inform (DECL_SOURCE_LOCATION (tmpl),
4097 		      "%qD declared here", tmpl);
4098 	    }
4099 	  return error_mark_node;
4100 	}
4101 
4102       if (!perform_or_defer_access_check (TYPE_BINFO (context), tmpl, tmpl,
4103 					  complain))
4104 	return error_mark_node;
4105 
4106       return tmpl;
4107     }
4108 
4109   /* Build the UNBOUND_CLASS_TEMPLATE.  */
4110   t = cxx_make_type (UNBOUND_CLASS_TEMPLATE);
4111   TYPE_CONTEXT (t) = FROB_CONTEXT (context);
4112   TREE_TYPE (t) = NULL_TREE;
4113   SET_TYPE_STRUCTURAL_EQUALITY (t);
4114 
4115   /* Build the corresponding TEMPLATE_DECL.  */
4116   d = build_decl (input_location, TEMPLATE_DECL, name, t);
4117   TYPE_NAME (TREE_TYPE (d)) = d;
4118   TYPE_STUB_DECL (TREE_TYPE (d)) = d;
4119   DECL_CONTEXT (d) = FROB_CONTEXT (context);
4120   DECL_ARTIFICIAL (d) = 1;
4121   DECL_TEMPLATE_PARMS (d) = parm_list;
4122 
4123   return t;
4124 }
4125 
4126 
4127 
4128 /* Push the declarations of builtin types into the global namespace.
4129    RID_INDEX is the index of the builtin type in the array
4130    RID_POINTERS.  NAME is the name used when looking up the builtin
4131    type.  TYPE is the _TYPE node for the builtin type.
4132 
4133    The calls to set_global_binding below should be
4134    eliminated.  Built-in types should not be looked up name; their
4135    names are keywords that the parser can recognize.  However, there
4136    is code in c-common.c that uses identifier_global_value to look up
4137    built-in types by name.  */
4138 
4139 void
record_builtin_type(enum rid rid_index,const char * name,tree type)4140 record_builtin_type (enum rid rid_index,
4141 		     const char* name,
4142 		     tree type)
4143 {
4144   tree decl = NULL_TREE;
4145 
4146   if (name)
4147     {
4148       tree tname = get_identifier (name);
4149       tree tdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, tname, type);
4150       DECL_ARTIFICIAL (tdecl) = 1;
4151       set_global_binding (tdecl);
4152       decl = tdecl;
4153     }
4154 
4155   if ((int) rid_index < (int) RID_MAX)
4156     if (tree rname = ridpointers[(int) rid_index])
4157       if (!decl || DECL_NAME (decl) != rname)
4158 	{
4159 	  tree rdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, rname, type);
4160 	  DECL_ARTIFICIAL (rdecl) = 1;
4161 	  set_global_binding (rdecl);
4162 	  if (!decl)
4163 	    decl = rdecl;
4164 	}
4165 
4166   if (decl)
4167     {
4168       if (!TYPE_NAME (type))
4169 	TYPE_NAME (type) = decl;
4170       debug_hooks->type_decl (decl, 0);
4171     }
4172 }
4173 
4174 /* Push a type into the namespace so that the back ends ignore it.  */
4175 
4176 static void
record_unknown_type(tree type,const char * name)4177 record_unknown_type (tree type, const char* name)
4178 {
4179   tree decl = pushdecl (build_decl (UNKNOWN_LOCATION,
4180 				    TYPE_DECL, get_identifier (name), type));
4181   /* Make sure the "unknown type" typedecl gets ignored for debug info.  */
4182   DECL_IGNORED_P (decl) = 1;
4183   TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
4184   TYPE_SIZE (type) = TYPE_SIZE (void_type_node);
4185   SET_TYPE_ALIGN (type, 1);
4186   TYPE_USER_ALIGN (type) = 0;
4187   SET_TYPE_MODE (type, TYPE_MODE (void_type_node));
4188 }
4189 
4190 /* Create all the predefined identifiers.  */
4191 
4192 static void
initialize_predefined_identifiers(void)4193 initialize_predefined_identifiers (void)
4194 {
4195   struct predefined_identifier
4196   {
4197     const char *name; /* Name.  */
4198     tree *node;  /* Node to store it in.  */
4199     cp_identifier_kind kind;  /* Kind of identifier.  */
4200   };
4201 
4202   /* A table of identifiers to create at startup.  */
4203   static const predefined_identifier predefined_identifiers[] = {
4204     {"C++", &lang_name_cplusplus, cik_normal},
4205     {"C", &lang_name_c, cik_normal},
4206     /* Some of these names have a trailing space so that it is
4207        impossible for them to conflict with names written by users.  */
4208     {"__ct ", &ctor_identifier, cik_ctor},
4209     {"__ct_base ", &base_ctor_identifier, cik_ctor},
4210     {"__ct_comp ", &complete_ctor_identifier, cik_ctor},
4211     {"__dt ", &dtor_identifier, cik_dtor},
4212     {"__dt_base ", &base_dtor_identifier, cik_dtor},
4213     {"__dt_comp ", &complete_dtor_identifier, cik_dtor},
4214     {"__dt_del ", &deleting_dtor_identifier, cik_dtor},
4215     {"__conv_op ", &conv_op_identifier, cik_conv_op},
4216     {"__in_chrg", &in_charge_identifier, cik_normal},
4217     {"this", &this_identifier, cik_normal},
4218     {"__delta", &delta_identifier, cik_normal},
4219     {"__pfn", &pfn_identifier, cik_normal},
4220     {"_vptr", &vptr_identifier, cik_normal},
4221     {"__vtt_parm", &vtt_parm_identifier, cik_normal},
4222     {"::", &global_identifier, cik_normal},
4223       /* The demangler expects anonymous namespaces to be called
4224 	 something starting with '_GLOBAL__N_'.  It no longer needs
4225 	 to be unique to the TU.  */
4226     {"_GLOBAL__N_1", &anon_identifier, cik_normal},
4227     {"auto", &auto_identifier, cik_normal},
4228     {"decltype(auto)", &decltype_auto_identifier, cik_normal},
4229     {"initializer_list", &init_list_identifier, cik_normal},
4230     {"__for_range ", &for_range__identifier, cik_normal},
4231     {"__for_begin ", &for_begin__identifier, cik_normal},
4232     {"__for_end ", &for_end__identifier, cik_normal},
4233     {"__for_range", &for_range_identifier, cik_normal},
4234     {"__for_begin", &for_begin_identifier, cik_normal},
4235     {"__for_end", &for_end_identifier, cik_normal},
4236     {"abi_tag", &abi_tag_identifier, cik_normal},
4237     {"aligned", &aligned_identifier, cik_normal},
4238     {"begin", &begin_identifier, cik_normal},
4239     {"end", &end_identifier, cik_normal},
4240     {"get", &get__identifier, cik_normal},
4241     {"gnu", &gnu_identifier, cik_normal},
4242     {"tuple_element", &tuple_element_identifier, cik_normal},
4243     {"tuple_size", &tuple_size_identifier, cik_normal},
4244     {"type", &type_identifier, cik_normal},
4245     {"value", &value_identifier, cik_normal},
4246     {"_FUN", &fun_identifier, cik_normal},
4247     {"__closure", &closure_identifier, cik_normal},
4248     {"heap uninit", &heap_uninit_identifier, cik_normal},
4249     {"heap ", &heap_identifier, cik_normal},
4250     {"heap deleted", &heap_deleted_identifier, cik_normal},
4251     {NULL, NULL, cik_normal}
4252   };
4253 
4254   for (const predefined_identifier *pid = predefined_identifiers;
4255        pid->name; ++pid)
4256     {
4257       *pid->node = get_identifier (pid->name);
4258       /* Some of these identifiers already have a special kind.  */
4259       if (pid->kind != cik_normal)
4260 	set_identifier_kind (*pid->node, pid->kind);
4261     }
4262 }
4263 
4264 /* Create the predefined scalar types of C,
4265    and some nodes representing standard constants (0, 1, (void *)0).
4266    Initialize the global binding level.
4267    Make definitions for built-in primitive functions.  */
4268 
4269 void
cxx_init_decl_processing(void)4270 cxx_init_decl_processing (void)
4271 {
4272   tree void_ftype;
4273   tree void_ftype_ptr;
4274 
4275   /* Create all the identifiers we need.  */
4276   initialize_predefined_identifiers ();
4277 
4278   /* Create the global variables.  */
4279   push_to_top_level ();
4280 
4281   current_function_decl = NULL_TREE;
4282   current_binding_level = NULL;
4283   /* Enter the global namespace.  */
4284   gcc_assert (global_namespace == NULL_TREE);
4285   global_namespace = build_lang_decl (NAMESPACE_DECL, global_identifier,
4286 				      void_type_node);
4287   TREE_PUBLIC (global_namespace) = 1;
4288   DECL_CONTEXT (global_namespace)
4289     = build_translation_unit_decl (get_identifier (main_input_filename));
4290   /* Remember whether we want the empty class passing ABI change warning
4291      in this TU.  */
4292   TRANSLATION_UNIT_WARN_EMPTY_P (DECL_CONTEXT (global_namespace))
4293     = warn_abi && abi_version_crosses (12);
4294   debug_hooks->register_main_translation_unit
4295     (DECL_CONTEXT (global_namespace));
4296   begin_scope (sk_namespace, global_namespace);
4297   current_namespace = global_namespace;
4298 
4299   if (flag_visibility_ms_compat)
4300     default_visibility = VISIBILITY_HIDDEN;
4301 
4302   /* Initially, C.  */
4303   current_lang_name = lang_name_c;
4304 
4305   /* Create the `std' namespace.  */
4306   push_namespace (get_identifier ("std"));
4307   std_node = current_namespace;
4308   pop_namespace ();
4309 
4310   flag_noexcept_type = (cxx_dialect >= cxx17);
4311 
4312   c_common_nodes_and_builtins ();
4313 
4314   tree bool_ftype = build_function_type_list (boolean_type_node, NULL_TREE);
4315   tree decl
4316     = add_builtin_function ("__builtin_is_constant_evaluated",
4317 			    bool_ftype, CP_BUILT_IN_IS_CONSTANT_EVALUATED,
4318 			    BUILT_IN_FRONTEND, NULL, NULL_TREE);
4319   set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
4320 
4321   tree cptr_ftype = build_function_type_list (const_ptr_type_node, NULL_TREE);
4322   decl = add_builtin_function ("__builtin_source_location",
4323 			       cptr_ftype, CP_BUILT_IN_SOURCE_LOCATION,
4324 			       BUILT_IN_FRONTEND, NULL, NULL_TREE);
4325   set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
4326 
4327   integer_two_node = build_int_cst (NULL_TREE, 2);
4328 
4329   /* Guess at the initial static decls size.  */
4330   vec_alloc (static_decls, 500);
4331 
4332   /* ... and keyed classes.  */
4333   vec_alloc (keyed_classes, 100);
4334 
4335   record_builtin_type (RID_BOOL, "bool", boolean_type_node);
4336   truthvalue_type_node = boolean_type_node;
4337   truthvalue_false_node = boolean_false_node;
4338   truthvalue_true_node = boolean_true_node;
4339 
4340   empty_except_spec = build_tree_list (NULL_TREE, NULL_TREE);
4341   noexcept_true_spec = build_tree_list (boolean_true_node, NULL_TREE);
4342   noexcept_false_spec = build_tree_list (boolean_false_node, NULL_TREE);
4343   noexcept_deferred_spec = build_tree_list (make_node (DEFERRED_NOEXCEPT),
4344 					    NULL_TREE);
4345 
4346 #if 0
4347   record_builtin_type (RID_MAX, NULL, string_type_node);
4348 #endif
4349 
4350   delta_type_node = ptrdiff_type_node;
4351   vtable_index_type = ptrdiff_type_node;
4352 
4353   vtt_parm_type = build_pointer_type (const_ptr_type_node);
4354   void_ftype = build_function_type_list (void_type_node, NULL_TREE);
4355   void_ftype_ptr = build_function_type_list (void_type_node,
4356 					     ptr_type_node, NULL_TREE);
4357   void_ftype_ptr
4358     = build_exception_variant (void_ftype_ptr, empty_except_spec);
4359 
4360   /* Create the conversion operator marker.  This operator's DECL_NAME
4361      is in the identifier table, so we can use identifier equality to
4362      find it.  */
4363   conv_op_marker = build_lang_decl (FUNCTION_DECL, conv_op_identifier,
4364 				    void_ftype);
4365 
4366   /* C++ extensions */
4367 
4368   unknown_type_node = make_node (LANG_TYPE);
4369   record_unknown_type (unknown_type_node, "unknown type");
4370 
4371   /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node.  */
4372   TREE_TYPE (unknown_type_node) = unknown_type_node;
4373 
4374   /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same
4375      result.  */
4376   TYPE_POINTER_TO (unknown_type_node) = unknown_type_node;
4377   TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node;
4378 
4379   init_list_type_node = make_node (LANG_TYPE);
4380   record_unknown_type (init_list_type_node, "init list");
4381 
4382   {
4383     /* Make sure we get a unique function type, so we can give
4384        its pointer type a name.  (This wins for gdb.) */
4385     tree vfunc_type = make_node (FUNCTION_TYPE);
4386     TREE_TYPE (vfunc_type) = integer_type_node;
4387     TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
4388     layout_type (vfunc_type);
4389 
4390     vtable_entry_type = build_pointer_type (vfunc_type);
4391   }
4392   record_builtin_type (RID_MAX, "__vtbl_ptr_type", vtable_entry_type);
4393 
4394   vtbl_type_node
4395     = build_cplus_array_type (vtable_entry_type, NULL_TREE);
4396   layout_type (vtbl_type_node);
4397   vtbl_type_node = cp_build_qualified_type (vtbl_type_node, TYPE_QUAL_CONST);
4398   record_builtin_type (RID_MAX, NULL, vtbl_type_node);
4399   vtbl_ptr_type_node = build_pointer_type (vtable_entry_type);
4400   layout_type (vtbl_ptr_type_node);
4401   record_builtin_type (RID_MAX, NULL, vtbl_ptr_type_node);
4402 
4403   push_namespace (get_identifier ("__cxxabiv1"));
4404   abi_node = current_namespace;
4405   pop_namespace ();
4406 
4407   global_type_node = make_node (LANG_TYPE);
4408   record_unknown_type (global_type_node, "global type");
4409 
4410   any_targ_node = make_node (LANG_TYPE);
4411   record_unknown_type (any_targ_node, "any type");
4412 
4413   /* Now, C++.  */
4414   current_lang_name = lang_name_cplusplus;
4415 
4416   if (aligned_new_threshold > 1
4417       && !pow2p_hwi (aligned_new_threshold))
4418     {
4419       error ("%<-faligned-new=%d%> is not a power of two",
4420 	     aligned_new_threshold);
4421       aligned_new_threshold = 1;
4422     }
4423   if (aligned_new_threshold == -1)
4424     aligned_new_threshold = (cxx_dialect >= cxx17) ? 1 : 0;
4425   if (aligned_new_threshold == 1)
4426     aligned_new_threshold = malloc_alignment () / BITS_PER_UNIT;
4427 
4428   {
4429     tree newattrs, extvisattr;
4430     tree newtype, deltype;
4431     tree ptr_ftype_sizetype;
4432     tree new_eh_spec;
4433 
4434     ptr_ftype_sizetype
4435       = build_function_type_list (ptr_type_node, size_type_node, NULL_TREE);
4436     if (cxx_dialect == cxx98)
4437       {
4438 	tree bad_alloc_id;
4439 	tree bad_alloc_type_node;
4440 	tree bad_alloc_decl;
4441 
4442 	push_nested_namespace (std_node);
4443 	bad_alloc_id = get_identifier ("bad_alloc");
4444 	bad_alloc_type_node = make_class_type (RECORD_TYPE);
4445 	TYPE_CONTEXT (bad_alloc_type_node) = current_namespace;
4446 	bad_alloc_decl
4447 	  = create_implicit_typedef (bad_alloc_id, bad_alloc_type_node);
4448 	DECL_CONTEXT (bad_alloc_decl) = current_namespace;
4449 	pop_nested_namespace (std_node);
4450 
4451 	new_eh_spec
4452 	  = add_exception_specifier (NULL_TREE, bad_alloc_type_node, -1);
4453       }
4454     else
4455       new_eh_spec = noexcept_false_spec;
4456 
4457     /* Ensure attribs.c is initialized.  */
4458     init_attributes ();
4459 
4460     extvisattr = build_tree_list (get_identifier ("externally_visible"),
4461 				  NULL_TREE);
4462     newattrs = tree_cons (get_identifier ("alloc_size"),
4463 			  build_tree_list (NULL_TREE, integer_one_node),
4464 			  extvisattr);
4465     newtype = cp_build_type_attribute_variant (ptr_ftype_sizetype, newattrs);
4466     newtype = build_exception_variant (newtype, new_eh_spec);
4467     deltype = cp_build_type_attribute_variant (void_ftype_ptr, extvisattr);
4468     deltype = build_exception_variant (deltype, empty_except_spec);
4469     tree opnew = push_cp_library_fn (NEW_EXPR, newtype, 0);
4470     DECL_IS_MALLOC (opnew) = 1;
4471     DECL_SET_IS_OPERATOR_NEW (opnew, true);
4472     DECL_IS_REPLACEABLE_OPERATOR (opnew) = 1;
4473     opnew = push_cp_library_fn (VEC_NEW_EXPR, newtype, 0);
4474     DECL_IS_MALLOC (opnew) = 1;
4475     DECL_SET_IS_OPERATOR_NEW (opnew, true);
4476     DECL_IS_REPLACEABLE_OPERATOR (opnew) = 1;
4477     tree opdel = push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
4478     DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4479     DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
4480     opdel = push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
4481     DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4482     DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
4483     if (flag_sized_deallocation)
4484       {
4485 	/* Also push the sized deallocation variants:
4486 	     void operator delete(void*, std::size_t) throw();
4487 	     void operator delete[](void*, std::size_t) throw();  */
4488 	tree void_ftype_ptr_size
4489 	  = build_function_type_list (void_type_node, ptr_type_node,
4490 				      size_type_node, NULL_TREE);
4491 	deltype = cp_build_type_attribute_variant (void_ftype_ptr_size,
4492 						   extvisattr);
4493 	deltype = build_exception_variant (deltype, empty_except_spec);
4494 	opdel = push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
4495 	DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4496 	DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
4497 	opdel = push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
4498 	DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4499 	DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
4500       }
4501 
4502     if (aligned_new_threshold)
4503       {
4504 	push_nested_namespace (std_node);
4505 	tree align_id = get_identifier ("align_val_t");
4506 	align_type_node = start_enum (align_id, NULL_TREE, size_type_node,
4507 				      NULL_TREE, /*scoped*/true, NULL);
4508 	pop_nested_namespace (std_node);
4509 
4510 	/* operator new (size_t, align_val_t); */
4511 	newtype = build_function_type_list (ptr_type_node, size_type_node,
4512 					    align_type_node, NULL_TREE);
4513 	newtype = cp_build_type_attribute_variant (newtype, newattrs);
4514 	newtype = build_exception_variant (newtype, new_eh_spec);
4515 	opnew = push_cp_library_fn (NEW_EXPR, newtype, 0);
4516 	DECL_IS_MALLOC (opnew) = 1;
4517 	DECL_SET_IS_OPERATOR_NEW (opnew, true);
4518 	DECL_IS_REPLACEABLE_OPERATOR (opnew) = 1;
4519 	opnew = push_cp_library_fn (VEC_NEW_EXPR, newtype, 0);
4520 	DECL_IS_MALLOC (opnew) = 1;
4521 	DECL_SET_IS_OPERATOR_NEW (opnew, true);
4522 	DECL_IS_REPLACEABLE_OPERATOR (opnew) = 1;
4523 
4524 	/* operator delete (void *, align_val_t); */
4525 	deltype = build_function_type_list (void_type_node, ptr_type_node,
4526 					    align_type_node, NULL_TREE);
4527 	deltype = cp_build_type_attribute_variant (deltype, extvisattr);
4528 	deltype = build_exception_variant (deltype, empty_except_spec);
4529 	opdel = push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
4530 	DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4531 	DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
4532 	opdel = push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
4533 	DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4534 	DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
4535 
4536 	if (flag_sized_deallocation)
4537 	  {
4538 	    /* operator delete (void *, size_t, align_val_t); */
4539 	    deltype = build_function_type_list (void_type_node, ptr_type_node,
4540 						size_type_node, align_type_node,
4541 						NULL_TREE);
4542 	    deltype = cp_build_type_attribute_variant (deltype, extvisattr);
4543 	    deltype = build_exception_variant (deltype, empty_except_spec);
4544 	    opdel = push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
4545 	    DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4546 	    DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
4547 	    opdel = push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
4548 	    DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4549 	    DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
4550 	  }
4551       }
4552 
4553     nullptr_type_node = make_node (NULLPTR_TYPE);
4554     TYPE_SIZE (nullptr_type_node) = bitsize_int (GET_MODE_BITSIZE (ptr_mode));
4555     TYPE_SIZE_UNIT (nullptr_type_node) = size_int (GET_MODE_SIZE (ptr_mode));
4556     TYPE_UNSIGNED (nullptr_type_node) = 1;
4557     TYPE_PRECISION (nullptr_type_node) = GET_MODE_BITSIZE (ptr_mode);
4558     if (abi_version_at_least (9))
4559       SET_TYPE_ALIGN (nullptr_type_node, GET_MODE_ALIGNMENT (ptr_mode));
4560     SET_TYPE_MODE (nullptr_type_node, ptr_mode);
4561     record_builtin_type (RID_MAX, "decltype(nullptr)", nullptr_type_node);
4562     nullptr_node = build_int_cst (nullptr_type_node, 0);
4563   }
4564 
4565   abort_fndecl
4566     = build_library_fn_ptr ("__cxa_pure_virtual", void_ftype,
4567 			    ECF_NORETURN | ECF_NOTHROW | ECF_COLD);
4568 
4569   /* Perform other language dependent initializations.  */
4570   init_class_processing ();
4571   init_rtti_processing ();
4572   init_template_processing ();
4573 
4574   if (flag_exceptions)
4575     init_exception_processing ();
4576 
4577   if (! supports_one_only ())
4578     flag_weak = 0;
4579 
4580   make_fname_decl = cp_make_fname_decl;
4581   start_fname_decls ();
4582 
4583   /* Show we use EH for cleanups.  */
4584   if (flag_exceptions)
4585     using_eh_for_cleanups ();
4586 }
4587 
4588 /* Generate an initializer for a function naming variable from
4589    NAME. NAME may be NULL, to indicate a dependent name.  TYPE_P is
4590    filled in with the type of the init.  */
4591 
4592 tree
cp_fname_init(const char * name,tree * type_p)4593 cp_fname_init (const char* name, tree *type_p)
4594 {
4595   tree domain = NULL_TREE;
4596   tree type;
4597   tree init = NULL_TREE;
4598   size_t length = 0;
4599 
4600   if (name)
4601     {
4602       length = strlen (name);
4603       domain = build_index_type (size_int (length));
4604       init = build_string (length + 1, name);
4605     }
4606 
4607   type = cp_build_qualified_type (char_type_node, TYPE_QUAL_CONST);
4608   type = build_cplus_array_type (type, domain);
4609 
4610   *type_p = type;
4611 
4612   if (init)
4613     TREE_TYPE (init) = type;
4614   else
4615     init = error_mark_node;
4616 
4617   return init;
4618 }
4619 
4620 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give
4621    the decl, LOC is the location to give the decl, NAME is the
4622    initialization string and TYPE_DEP indicates whether NAME depended
4623    on the type of the function. We make use of that to detect
4624    __PRETTY_FUNCTION__ inside a template fn. This is being done lazily
4625    at the point of first use, so we mustn't push the decl now.  */
4626 
4627 static tree
cp_make_fname_decl(location_t loc,tree id,int type_dep)4628 cp_make_fname_decl (location_t loc, tree id, int type_dep)
4629 {
4630   const char * name = NULL;
4631   bool release_name = false;
4632   if (!(type_dep && in_template_function ()))
4633     {
4634       if (current_function_decl == NULL_TREE)
4635 	name = "top level";
4636       else if (type_dep == 1) /* __PRETTY_FUNCTION__ */
4637 	name = cxx_printable_name (current_function_decl, 2);
4638       else if (type_dep == 0) /* __FUNCTION__ */
4639 	{
4640 	  name = fname_as_string (type_dep);
4641 	  release_name = true;
4642 	}
4643       else
4644 	gcc_unreachable ();
4645     }
4646   tree type;
4647   tree init = cp_fname_init (name, &type);
4648   tree decl = build_decl (loc, VAR_DECL, id, type);
4649 
4650   if (release_name)
4651     free (CONST_CAST (char *, name));
4652 
4653   /* As we're using pushdecl_with_scope, we must set the context.  */
4654   DECL_CONTEXT (decl) = current_function_decl;
4655 
4656   TREE_READONLY (decl) = 1;
4657   DECL_ARTIFICIAL (decl) = 1;
4658   DECL_DECLARED_CONSTEXPR_P (decl) = 1;
4659   TREE_STATIC (decl) = 1;
4660 
4661   TREE_USED (decl) = 1;
4662 
4663   if (init)
4664     {
4665       SET_DECL_VALUE_EXPR (decl, init);
4666       DECL_HAS_VALUE_EXPR_P (decl) = 1;
4667       /* For decl_constant_var_p.  */
4668       DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
4669     }
4670 
4671   if (current_function_decl)
4672     {
4673       DECL_CONTEXT (decl) = current_function_decl;
4674       decl = pushdecl_outermost_localscope (decl);
4675       if (decl != error_mark_node)
4676 	add_decl_expr (decl);
4677     }
4678   else
4679     {
4680       DECL_THIS_STATIC (decl) = true;
4681       pushdecl_top_level_and_finish (decl, NULL_TREE);
4682     }
4683 
4684   return decl;
4685 }
4686 
4687 /* Install DECL as a builtin function at current (global) scope.
4688    Return the new decl (if we found an existing version).  Also
4689    installs it into ::std, if it's not '_*'.  */
4690 
4691 tree
cxx_builtin_function(tree decl)4692 cxx_builtin_function (tree decl)
4693 {
4694   retrofit_lang_decl (decl);
4695 
4696   DECL_ARTIFICIAL (decl) = 1;
4697   SET_DECL_LANGUAGE (decl, lang_c);
4698   /* Runtime library routines are, by definition, available in an
4699      external shared object.  */
4700   DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
4701   DECL_VISIBILITY_SPECIFIED (decl) = 1;
4702 
4703   tree id = DECL_NAME (decl);
4704   const char *name = IDENTIFIER_POINTER (id);
4705   if (name[0] != '_' || name[1] != '_')
4706     /* In the user's namespace, it must be declared before use.  */
4707     DECL_ANTICIPATED (decl) = 1;
4708   else if (IDENTIFIER_LENGTH (id) > strlen ("___chk")
4709 	   && 0 != strncmp (name + 2, "builtin_", strlen ("builtin_"))
4710 	   && 0 == memcmp (name + IDENTIFIER_LENGTH (id) - strlen ("_chk"),
4711 			   "_chk", strlen ("_chk") + 1))
4712     /* Treat __*_chk fortification functions as anticipated as well,
4713        unless they are __builtin_*_chk.  */
4714     DECL_ANTICIPATED (decl) = 1;
4715 
4716   /* All builtins that don't begin with an '_' should additionally
4717      go in the 'std' namespace.  */
4718   if (name[0] != '_')
4719     {
4720       tree std_decl = copy_decl (decl);
4721 
4722       push_nested_namespace (std_node);
4723       DECL_CONTEXT (std_decl) = FROB_CONTEXT (std_node);
4724       pushdecl (std_decl);
4725       pop_nested_namespace (std_node);
4726     }
4727 
4728   DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
4729   decl = pushdecl (decl);
4730 
4731   return decl;
4732 }
4733 
4734 /* Like cxx_builtin_function, but guarantee the function is added to the global
4735    scope.  This is to allow function specific options to add new machine
4736    dependent builtins when the target ISA changes via attribute((target(...)))
4737    which saves space on program startup if the program does not use non-generic
4738    ISAs.  */
4739 
4740 tree
cxx_builtin_function_ext_scope(tree decl)4741 cxx_builtin_function_ext_scope (tree decl)
4742 {
4743   push_nested_namespace (global_namespace);
4744   decl = cxx_builtin_function (decl);
4745   pop_nested_namespace (global_namespace);
4746 
4747   return decl;
4748 }
4749 
4750 /* Implement LANG_HOOKS_SIMULATE_BUILTIN_FUNCTION_DECL.  */
4751 
4752 tree
cxx_simulate_builtin_function_decl(tree decl)4753 cxx_simulate_builtin_function_decl (tree decl)
4754 {
4755   retrofit_lang_decl (decl);
4756 
4757   DECL_ARTIFICIAL (decl) = 1;
4758   SET_DECL_LANGUAGE (decl, lang_cplusplus);
4759   DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
4760   return pushdecl (decl);
4761 }
4762 
4763 /* Generate a FUNCTION_DECL with the typical flags for a runtime library
4764    function.  Not called directly.  */
4765 
4766 static tree
build_library_fn(tree name,enum tree_code operator_code,tree type,int ecf_flags)4767 build_library_fn (tree name, enum tree_code operator_code, tree type,
4768 		  int ecf_flags)
4769 {
4770   tree fn = build_lang_decl (FUNCTION_DECL, name, type);
4771   DECL_EXTERNAL (fn) = 1;
4772   TREE_PUBLIC (fn) = 1;
4773   DECL_ARTIFICIAL (fn) = 1;
4774   DECL_OVERLOADED_OPERATOR_CODE_RAW (fn)
4775     = OVL_OP_INFO (false, operator_code)->ovl_op_code;
4776   SET_DECL_LANGUAGE (fn, lang_c);
4777   /* Runtime library routines are, by definition, available in an
4778      external shared object.  */
4779   DECL_VISIBILITY (fn) = VISIBILITY_DEFAULT;
4780   DECL_VISIBILITY_SPECIFIED (fn) = 1;
4781   set_call_expr_flags (fn, ecf_flags);
4782   return fn;
4783 }
4784 
4785 /* Returns the _DECL for a library function with C++ linkage.  */
4786 
4787 static tree
build_cp_library_fn(tree name,enum tree_code operator_code,tree type,int ecf_flags)4788 build_cp_library_fn (tree name, enum tree_code operator_code, tree type,
4789 		     int ecf_flags)
4790 {
4791   tree fn = build_library_fn (name, operator_code, type, ecf_flags);
4792   DECL_CONTEXT (fn) = FROB_CONTEXT (current_namespace);
4793   SET_DECL_LANGUAGE (fn, lang_cplusplus);
4794   return fn;
4795 }
4796 
4797 /* Like build_library_fn, but takes a C string instead of an
4798    IDENTIFIER_NODE.  */
4799 
4800 tree
build_library_fn_ptr(const char * name,tree type,int ecf_flags)4801 build_library_fn_ptr (const char* name, tree type, int ecf_flags)
4802 {
4803   return build_library_fn (get_identifier (name), ERROR_MARK, type, ecf_flags);
4804 }
4805 
4806 /* Like build_cp_library_fn, but takes a C string instead of an
4807    IDENTIFIER_NODE.  */
4808 
4809 tree
build_cp_library_fn_ptr(const char * name,tree type,int ecf_flags)4810 build_cp_library_fn_ptr (const char* name, tree type, int ecf_flags)
4811 {
4812   return build_cp_library_fn (get_identifier (name), ERROR_MARK, type,
4813 			      ecf_flags);
4814 }
4815 
4816 /* Like build_library_fn, but also pushes the function so that we will
4817    be able to find it via get_global_binding.  Also, the function
4818    may throw exceptions listed in RAISES.  */
4819 
4820 tree
push_library_fn(tree name,tree type,tree raises,int ecf_flags)4821 push_library_fn (tree name, tree type, tree raises, int ecf_flags)
4822 {
4823   tree fn;
4824 
4825   if (raises)
4826     type = build_exception_variant (type, raises);
4827 
4828   fn = build_library_fn (name, ERROR_MARK, type, ecf_flags);
4829   pushdecl_top_level (fn);
4830   return fn;
4831 }
4832 
4833 /* Like build_cp_library_fn, but also pushes the function so that it
4834    will be found by normal lookup.  */
4835 
4836 static tree
push_cp_library_fn(enum tree_code operator_code,tree type,int ecf_flags)4837 push_cp_library_fn (enum tree_code operator_code, tree type,
4838 		    int ecf_flags)
4839 {
4840   tree fn = build_cp_library_fn (ovl_op_identifier (false, operator_code),
4841 				 operator_code, type, ecf_flags);
4842   pushdecl (fn);
4843   if (flag_tm)
4844     apply_tm_attr (fn, get_identifier ("transaction_safe"));
4845   return fn;
4846 }
4847 
4848 /* Like push_library_fn, but takes a TREE_LIST of parm types rather than
4849    a FUNCTION_TYPE.  */
4850 
4851 tree
push_void_library_fn(tree name,tree parmtypes,int ecf_flags)4852 push_void_library_fn (tree name, tree parmtypes, int ecf_flags)
4853 {
4854   tree type = build_function_type (void_type_node, parmtypes);
4855   return push_library_fn (name, type, NULL_TREE, ecf_flags);
4856 }
4857 
4858 /* Like push_library_fn, but also note that this function throws
4859    and does not return.  Used for __throw_foo and the like.  */
4860 
4861 tree
push_throw_library_fn(tree name,tree type)4862 push_throw_library_fn (tree name, tree type)
4863 {
4864   tree fn = push_library_fn (name, type, NULL_TREE, ECF_NORETURN | ECF_COLD);
4865   return fn;
4866 }
4867 
4868 /* When we call finish_struct for an anonymous union, we create
4869    default copy constructors and such.  But, an anonymous union
4870    shouldn't have such things; this function undoes the damage to the
4871    anonymous union type T.
4872 
4873    (The reason that we create the synthesized methods is that we don't
4874    distinguish `union { int i; }' from `typedef union { int i; } U'.
4875    The first is an anonymous union; the second is just an ordinary
4876    union type.)  */
4877 
4878 void
fixup_anonymous_aggr(tree t)4879 fixup_anonymous_aggr (tree t)
4880 {
4881   /* Wipe out memory of synthesized methods.  */
4882   TYPE_HAS_USER_CONSTRUCTOR (t) = 0;
4883   TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0;
4884   TYPE_HAS_COPY_CTOR (t) = 0;
4885   TYPE_HAS_CONST_COPY_CTOR (t) = 0;
4886   TYPE_HAS_COPY_ASSIGN (t) = 0;
4887   TYPE_HAS_CONST_COPY_ASSIGN (t) = 0;
4888 
4889   /* Splice the implicitly generated functions out of TYPE_FIELDS.  */
4890   for (tree probe, *prev_p = &TYPE_FIELDS (t); (probe = *prev_p);)
4891     if (TREE_CODE (probe) == FUNCTION_DECL && DECL_ARTIFICIAL (probe))
4892       *prev_p = DECL_CHAIN (probe);
4893     else
4894       prev_p = &DECL_CHAIN (probe);
4895 
4896   /* Anonymous aggregates cannot have fields with ctors, dtors or complex
4897      assignment operators (because they cannot have these methods themselves).
4898      For anonymous unions this is already checked because they are not allowed
4899      in any union, otherwise we have to check it.  */
4900   if (TREE_CODE (t) != UNION_TYPE)
4901     {
4902       tree field, type;
4903 
4904       for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
4905 	if (TREE_CODE (field) == FIELD_DECL)
4906 	  {
4907 	    type = TREE_TYPE (field);
4908 	    if (CLASS_TYPE_P (type))
4909 	      {
4910 		if (TYPE_NEEDS_CONSTRUCTING (type))
4911 		  error ("member %q+#D with constructor not allowed "
4912 			 "in anonymous aggregate", field);
4913 		if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
4914 		  error ("member %q+#D with destructor not allowed "
4915 			 "in anonymous aggregate", field);
4916 		if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type))
4917 		  error ("member %q+#D with copy assignment operator "
4918 			 "not allowed in anonymous aggregate", field);
4919 	      }
4920 	  }
4921     }
4922 }
4923 
4924 /* Warn for an attribute located at LOCATION that appertains to the
4925    class type CLASS_TYPE that has not been properly placed after its
4926    class-key, in it class-specifier.  */
4927 
4928 void
warn_misplaced_attr_for_class_type(location_t location,tree class_type)4929 warn_misplaced_attr_for_class_type (location_t location,
4930 				    tree class_type)
4931 {
4932   gcc_assert (OVERLOAD_TYPE_P (class_type));
4933 
4934   auto_diagnostic_group d;
4935   if (warning_at (location, OPT_Wattributes,
4936 		  "attribute ignored in declaration "
4937 		  "of %q#T", class_type))
4938     inform (location,
4939 	    "attribute for %q#T must follow the %qs keyword",
4940 	    class_type, class_key_or_enum_as_string (class_type));
4941 }
4942 
4943 /* Returns the cv-qualifiers that apply to the type specified
4944    by the DECLSPECS.  */
4945 
4946 static int
get_type_quals(const cp_decl_specifier_seq * declspecs)4947 get_type_quals (const cp_decl_specifier_seq *declspecs)
4948 {
4949   int type_quals = TYPE_UNQUALIFIED;
4950 
4951   if (decl_spec_seq_has_spec_p (declspecs, ds_const))
4952     type_quals |= TYPE_QUAL_CONST;
4953   if (decl_spec_seq_has_spec_p (declspecs, ds_volatile))
4954     type_quals |= TYPE_QUAL_VOLATILE;
4955   if (decl_spec_seq_has_spec_p (declspecs, ds_restrict))
4956     type_quals |= TYPE_QUAL_RESTRICT;
4957 
4958   return type_quals;
4959 }
4960 
4961 /* Make sure that a declaration with no declarator is well-formed, i.e.
4962    just declares a tagged type or anonymous union.
4963 
4964    Returns the type declared; or NULL_TREE if none.  */
4965 
4966 tree
check_tag_decl(cp_decl_specifier_seq * declspecs,bool explicit_type_instantiation_p)4967 check_tag_decl (cp_decl_specifier_seq *declspecs,
4968 		bool explicit_type_instantiation_p)
4969 {
4970   int saw_friend = decl_spec_seq_has_spec_p (declspecs, ds_friend);
4971   int saw_typedef = decl_spec_seq_has_spec_p (declspecs, ds_typedef);
4972   /* If a class, struct, or enum type is declared by the DECLSPECS
4973      (i.e, if a class-specifier, enum-specifier, or non-typename
4974      elaborated-type-specifier appears in the DECLSPECS),
4975      DECLARED_TYPE is set to the corresponding type.  */
4976   tree declared_type = NULL_TREE;
4977   bool error_p = false;
4978 
4979   if (declspecs->multiple_types_p)
4980     error_at (smallest_type_location (declspecs),
4981 	      "multiple types in one declaration");
4982   else if (declspecs->redefined_builtin_type)
4983     {
4984       location_t loc = declspecs->locations[ds_redefined_builtin_type_spec];
4985       if (!in_system_header_at (loc))
4986 	permerror (loc, "redeclaration of C++ built-in type %qT",
4987 		   declspecs->redefined_builtin_type);
4988       return NULL_TREE;
4989     }
4990 
4991   if (declspecs->type
4992       && TYPE_P (declspecs->type)
4993       && ((TREE_CODE (declspecs->type) != TYPENAME_TYPE
4994 	   && MAYBE_CLASS_TYPE_P (declspecs->type))
4995 	  || TREE_CODE (declspecs->type) == ENUMERAL_TYPE))
4996     declared_type = declspecs->type;
4997   else if (declspecs->type == error_mark_node)
4998     error_p = true;
4999 
5000   if (type_uses_auto (declared_type))
5001     {
5002       error_at (declspecs->locations[ds_type_spec],
5003 		"%<auto%> can only be specified for variables "
5004 		"or function declarations");
5005       return error_mark_node;
5006     }
5007 
5008   if (declared_type && !OVERLOAD_TYPE_P (declared_type))
5009     declared_type = NULL_TREE;
5010 
5011   if (!declared_type && !saw_friend && !error_p)
5012     permerror (input_location, "declaration does not declare anything");
5013   /* Check for an anonymous union.  */
5014   else if (declared_type && RECORD_OR_UNION_CODE_P (TREE_CODE (declared_type))
5015 	   && TYPE_UNNAMED_P (declared_type))
5016     {
5017       /* 7/3 In a simple-declaration, the optional init-declarator-list
5018 	 can be omitted only when declaring a class (clause 9) or
5019 	 enumeration (7.2), that is, when the decl-specifier-seq contains
5020 	 either a class-specifier, an elaborated-type-specifier with
5021 	 a class-key (9.1), or an enum-specifier.  In these cases and
5022 	 whenever a class-specifier or enum-specifier is present in the
5023 	 decl-specifier-seq, the identifiers in these specifiers are among
5024 	 the names being declared by the declaration (as class-name,
5025 	 enum-names, or enumerators, depending on the syntax).  In such
5026 	 cases, and except for the declaration of an unnamed bit-field (9.6),
5027 	 the decl-specifier-seq shall introduce one or more names into the
5028 	 program, or shall redeclare a name introduced by a previous
5029 	 declaration.  [Example:
5030 	     enum { };			// ill-formed
5031 	     typedef class { };		// ill-formed
5032 	 --end example]  */
5033       if (saw_typedef)
5034 	{
5035 	  error_at (declspecs->locations[ds_typedef],
5036 		    "missing type-name in typedef-declaration");
5037 	  return NULL_TREE;
5038 	}
5039       /* Anonymous unions are objects, so they can have specifiers.  */;
5040       SET_ANON_AGGR_TYPE_P (declared_type);
5041 
5042       if (TREE_CODE (declared_type) != UNION_TYPE)
5043 	pedwarn (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (declared_type)),
5044 		 OPT_Wpedantic, "ISO C++ prohibits anonymous structs");
5045     }
5046 
5047   else
5048     {
5049       if (decl_spec_seq_has_spec_p (declspecs, ds_inline))
5050 	error_at (declspecs->locations[ds_inline],
5051 		  "%<inline%> can only be specified for functions");
5052       else if (decl_spec_seq_has_spec_p (declspecs, ds_virtual))
5053 	error_at (declspecs->locations[ds_virtual],
5054 		  "%<virtual%> can only be specified for functions");
5055       else if (saw_friend
5056 	       && (!current_class_type
5057 		   || current_scope () != current_class_type))
5058 	error_at (declspecs->locations[ds_friend],
5059 		  "%<friend%> can only be specified inside a class");
5060       else if (decl_spec_seq_has_spec_p (declspecs, ds_explicit))
5061 	error_at (declspecs->locations[ds_explicit],
5062 		  "%<explicit%> can only be specified for constructors");
5063       else if (declspecs->storage_class)
5064 	error_at (declspecs->locations[ds_storage_class],
5065 		  "a storage class can only be specified for objects "
5066 		  "and functions");
5067       else if (decl_spec_seq_has_spec_p (declspecs, ds_const))
5068 	error_at (declspecs->locations[ds_const],
5069 		  "%<const%> can only be specified for objects and "
5070 		  "functions");
5071       else if (decl_spec_seq_has_spec_p (declspecs, ds_volatile))
5072 	error_at (declspecs->locations[ds_volatile],
5073 		  "%<volatile%> can only be specified for objects and "
5074 		  "functions");
5075       else if (decl_spec_seq_has_spec_p (declspecs, ds_restrict))
5076 	error_at (declspecs->locations[ds_restrict],
5077 		  "%<__restrict%> can only be specified for objects and "
5078 		  "functions");
5079       else if (decl_spec_seq_has_spec_p (declspecs, ds_thread))
5080 	error_at (declspecs->locations[ds_thread],
5081 		  "%<__thread%> can only be specified for objects "
5082 		  "and functions");
5083       else if (saw_typedef)
5084 	warning_at (declspecs->locations[ds_typedef], 0,
5085 		    "%<typedef%> was ignored in this declaration");
5086       else if (decl_spec_seq_has_spec_p (declspecs, ds_constexpr))
5087         error_at (declspecs->locations[ds_constexpr],
5088 		  "%qs cannot be used for type declarations", "constexpr");
5089       else if (decl_spec_seq_has_spec_p (declspecs, ds_constinit))
5090 	error_at (declspecs->locations[ds_constinit],
5091 		  "%qs cannot be used for type declarations", "constinit");
5092       else if (decl_spec_seq_has_spec_p (declspecs, ds_consteval))
5093 	error_at (declspecs->locations[ds_consteval],
5094 		  "%qs cannot be used for type declarations", "consteval");
5095     }
5096 
5097   if (declspecs->attributes && warn_attributes && declared_type)
5098     {
5099       location_t loc;
5100       if (!CLASS_TYPE_P (declared_type)
5101 	  || !CLASSTYPE_TEMPLATE_INSTANTIATION (declared_type))
5102 	/* For a non-template class, use the name location.  */
5103 	loc = location_of (declared_type);
5104       else
5105 	/* For a template class (an explicit instantiation), use the
5106 	   current location.  */
5107 	loc = input_location;
5108 
5109       if (explicit_type_instantiation_p)
5110 	/* [dcl.attr.grammar]/4:
5111 
5112 	       No attribute-specifier-seq shall appertain to an explicit
5113 	       instantiation.  */
5114 	{
5115 	  if (warning_at (loc, OPT_Wattributes,
5116 			  "attribute ignored in explicit instantiation %q#T",
5117 			  declared_type))
5118 	    inform (loc,
5119 		    "no attribute can be applied to "
5120 		    "an explicit instantiation");
5121 	}
5122       else
5123 	warn_misplaced_attr_for_class_type (loc, declared_type);
5124     }
5125 
5126   return declared_type;
5127 }
5128 
5129 /* Called when a declaration is seen that contains no names to declare.
5130    If its type is a reference to a structure, union or enum inherited
5131    from a containing scope, shadow that tag name for the current scope
5132    with a forward reference.
5133    If its type defines a new named structure or union
5134    or defines an enum, it is valid but we need not do anything here.
5135    Otherwise, it is an error.
5136 
5137    C++: may have to grok the declspecs to learn about static,
5138    complain for anonymous unions.
5139 
5140    Returns the TYPE declared -- or NULL_TREE if none.  */
5141 
5142 tree
shadow_tag(cp_decl_specifier_seq * declspecs)5143 shadow_tag (cp_decl_specifier_seq *declspecs)
5144 {
5145   tree t = check_tag_decl (declspecs,
5146 			   /*explicit_type_instantiation_p=*/false);
5147 
5148   if (!t)
5149     return NULL_TREE;
5150 
5151   if (maybe_process_partial_specialization (t) == error_mark_node)
5152     return NULL_TREE;
5153 
5154   /* This is where the variables in an anonymous union are
5155      declared.  An anonymous union declaration looks like:
5156      union { ... } ;
5157      because there is no declarator after the union, the parser
5158      sends that declaration here.  */
5159   if (ANON_AGGR_TYPE_P (t))
5160     {
5161       fixup_anonymous_aggr (t);
5162 
5163       if (TYPE_FIELDS (t))
5164 	{
5165 	  tree decl = grokdeclarator (/*declarator=*/NULL,
5166 				      declspecs, NORMAL, 0, NULL);
5167 	  finish_anon_union (decl);
5168 	}
5169     }
5170 
5171   return t;
5172 }
5173 
5174 /* Decode a "typename", such as "int **", returning a ..._TYPE node.  */
5175 
5176 tree
groktypename(cp_decl_specifier_seq * type_specifiers,const cp_declarator * declarator,bool is_template_arg)5177 groktypename (cp_decl_specifier_seq *type_specifiers,
5178 	      const cp_declarator *declarator,
5179 	      bool is_template_arg)
5180 {
5181   tree attrs;
5182   tree type;
5183   enum decl_context context
5184     = is_template_arg ? TEMPLATE_TYPE_ARG : TYPENAME;
5185   attrs = type_specifiers->attributes;
5186   type_specifiers->attributes = NULL_TREE;
5187   type = grokdeclarator (declarator, type_specifiers, context, 0, &attrs);
5188   if (attrs && type != error_mark_node)
5189     {
5190       if (CLASS_TYPE_P (type))
5191 	warning (OPT_Wattributes, "ignoring attributes applied to class type %qT "
5192 		 "outside of definition", type);
5193       else if (MAYBE_CLASS_TYPE_P (type))
5194 	/* A template type parameter or other dependent type.  */
5195 	warning (OPT_Wattributes, "ignoring attributes applied to dependent "
5196 		 "type %qT without an associated declaration", type);
5197       else
5198 	cplus_decl_attributes (&type, attrs, 0);
5199     }
5200   return type;
5201 }
5202 
5203 /* Process a DECLARATOR for a function-scope variable declaration,
5204    namespace-scope variable declaration, or function declaration.
5205    (Function definitions go through start_function; class member
5206    declarations appearing in the body of the class go through
5207    grokfield.)  The DECL corresponding to the DECLARATOR is returned.
5208    If an error occurs, the error_mark_node is returned instead.
5209 
5210    DECLSPECS are the decl-specifiers for the declaration.  INITIALIZED is
5211    SD_INITIALIZED if an explicit initializer is present, or SD_DEFAULTED
5212    for an explicitly defaulted function, or SD_DELETED for an explicitly
5213    deleted function, but 0 (SD_UNINITIALIZED) if this is a variable
5214    implicitly initialized via a default constructor.  ATTRIBUTES and
5215    PREFIX_ATTRIBUTES are GNU attributes associated with this declaration.
5216 
5217    The scope represented by the context of the returned DECL is pushed
5218    (if it is not the global namespace) and is assigned to
5219    *PUSHED_SCOPE_P.  The caller is then responsible for calling
5220    pop_scope on *PUSHED_SCOPE_P if it is set.  */
5221 
5222 tree
start_decl(const cp_declarator * declarator,cp_decl_specifier_seq * declspecs,int initialized,tree attributes,tree prefix_attributes,tree * pushed_scope_p)5223 start_decl (const cp_declarator *declarator,
5224 	    cp_decl_specifier_seq *declspecs,
5225 	    int initialized,
5226 	    tree attributes,
5227 	    tree prefix_attributes,
5228 	    tree *pushed_scope_p)
5229 {
5230   tree decl;
5231   tree context;
5232   bool was_public;
5233   int flags;
5234   bool alias;
5235 
5236   *pushed_scope_p = NULL_TREE;
5237 
5238   /* An object declared as __attribute__((deprecated)) suppresses
5239      warnings of uses of other deprecated items.  */
5240   if (lookup_attribute ("deprecated", attributes))
5241     deprecated_state = DEPRECATED_SUPPRESS;
5242 
5243   attributes = chainon (attributes, prefix_attributes);
5244 
5245   decl = grokdeclarator (declarator, declspecs, NORMAL, initialized,
5246 			 &attributes);
5247 
5248   deprecated_state = DEPRECATED_NORMAL;
5249 
5250   if (decl == NULL_TREE || VOID_TYPE_P (decl)
5251       || decl == error_mark_node)
5252     return error_mark_node;
5253 
5254   context = CP_DECL_CONTEXT (decl);
5255   if (context != global_namespace)
5256     *pushed_scope_p = push_scope (context);
5257 
5258   /* Is it valid for this decl to have an initializer at all?
5259      If not, set INITIALIZED to zero, which will indirectly
5260      tell `cp_finish_decl' to ignore the initializer once it is parsed.  */
5261   if (initialized
5262       && TREE_CODE (decl) == TYPE_DECL)
5263     {
5264       error_at (DECL_SOURCE_LOCATION (decl),
5265 		"typedef %qD is initialized (use %qs instead)",
5266 		decl, "decltype");
5267       return error_mark_node;
5268     }
5269 
5270   if (initialized)
5271     {
5272       if (! toplevel_bindings_p ()
5273 	  && DECL_EXTERNAL (decl))
5274 	warning (0, "declaration of %q#D has %<extern%> and is initialized",
5275 		 decl);
5276       DECL_EXTERNAL (decl) = 0;
5277       if (toplevel_bindings_p ())
5278 	TREE_STATIC (decl) = 1;
5279     }
5280   alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl)) != 0;
5281 
5282   if (alias && TREE_CODE (decl) == FUNCTION_DECL)
5283     record_key_method_defined (decl);
5284 
5285   /* If this is a typedef that names the class for linkage purposes
5286      (7.1.3p8), apply any attributes directly to the type.  */
5287   if (TREE_CODE (decl) == TYPE_DECL
5288       && OVERLOAD_TYPE_P (TREE_TYPE (decl))
5289       && decl == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (decl))))
5290     flags = ATTR_FLAG_TYPE_IN_PLACE;
5291   else
5292     flags = 0;
5293 
5294   /* Set attributes here so if duplicate decl, will have proper attributes.  */
5295   cplus_decl_attributes (&decl, attributes, flags);
5296 
5297   /* Dllimported symbols cannot be defined.  Static data members (which
5298      can be initialized in-class and dllimported) go through grokfield,
5299      not here, so we don't need to exclude those decls when checking for
5300      a definition.  */
5301   if (initialized && DECL_DLLIMPORT_P (decl))
5302     {
5303       error_at (DECL_SOURCE_LOCATION (decl),
5304 		"definition of %q#D is marked %<dllimport%>", decl);
5305       DECL_DLLIMPORT_P (decl) = 0;
5306     }
5307 
5308   /* If #pragma weak was used, mark the decl weak now.  */
5309   if (!processing_template_decl && !DECL_DECOMPOSITION_P (decl))
5310     maybe_apply_pragma_weak (decl);
5311 
5312   if (TREE_CODE (decl) == FUNCTION_DECL
5313       && DECL_DECLARED_INLINE_P (decl)
5314       && DECL_UNINLINABLE (decl)
5315       && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
5316     warning_at (DECL_SOURCE_LOCATION (decl), 0,
5317 		"inline function %qD given attribute %qs", decl, "noinline");
5318 
5319   if (TYPE_P (context) && COMPLETE_TYPE_P (complete_type (context)))
5320     {
5321       bool this_tmpl = (processing_template_decl
5322 			> template_class_depth (context));
5323       if (VAR_P (decl))
5324 	{
5325 	  tree field = lookup_field (context, DECL_NAME (decl), 0, false);
5326 	  if (field == NULL_TREE
5327 	      || !(VAR_P (field) || variable_template_p (field)))
5328 	    error ("%q+#D is not a static data member of %q#T", decl, context);
5329 	  else if (variable_template_p (field)
5330 		   && (DECL_LANG_SPECIFIC (decl)
5331 		       && DECL_TEMPLATE_SPECIALIZATION (decl)))
5332 	    /* OK, specialization was already checked.  */;
5333 	  else if (variable_template_p (field) && !this_tmpl)
5334 	    {
5335 	      error_at (DECL_SOURCE_LOCATION (decl),
5336 			"non-member-template declaration of %qD", decl);
5337 	      inform (DECL_SOURCE_LOCATION (field), "does not match "
5338 		      "member template declaration here");
5339 	      return error_mark_node;
5340 	    }
5341 	  else
5342 	    {
5343 	      if (variable_template_p (field))
5344 		field = DECL_TEMPLATE_RESULT (field);
5345 
5346 	      if (DECL_CONTEXT (field) != context)
5347 		{
5348 		  if (!same_type_p (DECL_CONTEXT (field), context))
5349 		    permerror (input_location, "ISO C++ does not permit %<%T::%D%> "
5350 			       "to be defined as %<%T::%D%>",
5351 			       DECL_CONTEXT (field), DECL_NAME (decl),
5352 			       context, DECL_NAME (decl));
5353 		  DECL_CONTEXT (decl) = DECL_CONTEXT (field);
5354 		}
5355 	      /* Static data member are tricky; an in-class initialization
5356 		 still doesn't provide a definition, so the in-class
5357 		 declaration will have DECL_EXTERNAL set, but will have an
5358 		 initialization.  Thus, duplicate_decls won't warn
5359 		 about this situation, and so we check here.  */
5360 	      if (initialized && DECL_INITIALIZED_IN_CLASS_P (field))
5361 		error ("duplicate initialization of %qD", decl);
5362 	      field = duplicate_decls (decl, field,
5363 				       /*newdecl_is_friend=*/false);
5364 	      if (field == error_mark_node)
5365 		return error_mark_node;
5366 	      else if (field)
5367 		decl = field;
5368 	    }
5369 	}
5370       else
5371 	{
5372 	  tree field = check_classfn (context, decl,
5373 				      this_tmpl
5374 				      ? current_template_parms
5375 				      : NULL_TREE);
5376 	  if (field && field != error_mark_node
5377 	      && duplicate_decls (decl, field,
5378 				 /*newdecl_is_friend=*/false))
5379 	    decl = field;
5380 	}
5381 
5382       /* cp_finish_decl sets DECL_EXTERNAL if DECL_IN_AGGR_P is set.  */
5383       DECL_IN_AGGR_P (decl) = 0;
5384       /* Do not mark DECL as an explicit specialization if it was not
5385 	 already marked as an instantiation; a declaration should
5386 	 never be marked as a specialization unless we know what
5387 	 template is being specialized.  */
5388       if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
5389 	{
5390 	  SET_DECL_TEMPLATE_SPECIALIZATION (decl);
5391 	  if (TREE_CODE (decl) == FUNCTION_DECL)
5392 	    DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
5393 				  && DECL_DECLARED_INLINE_P (decl));
5394 	  else
5395 	    DECL_COMDAT (decl) = false;
5396 
5397 	  /* [temp.expl.spec] An explicit specialization of a static data
5398 	     member of a template is a definition if the declaration
5399 	     includes an initializer; otherwise, it is a declaration.
5400 
5401 	     We check for processing_specialization so this only applies
5402 	     to the new specialization syntax.  */
5403 	  if (!initialized && processing_specialization)
5404 	    DECL_EXTERNAL (decl) = 1;
5405 	}
5406 
5407       if (DECL_EXTERNAL (decl) && ! DECL_TEMPLATE_SPECIALIZATION (decl)
5408 	  /* Aliases are definitions. */
5409 	  && !alias)
5410 	permerror (declarator->id_loc,
5411 		   "declaration of %q#D outside of class is not definition",
5412 		   decl);
5413     }
5414 
5415   was_public = TREE_PUBLIC (decl);
5416 
5417   /* Enter this declaration into the symbol table.  Don't push the plain
5418      VAR_DECL for a variable template.  */
5419   if (!template_parm_scope_p ()
5420       || !VAR_P (decl))
5421     decl = maybe_push_decl (decl);
5422 
5423   if (processing_template_decl)
5424     {
5425       /* Make sure that for a `constinit' decl push_template_decl creates
5426 	 a DECL_TEMPLATE_INFO info for us, so that cp_finish_decl can then set
5427 	 TINFO_VAR_DECLARED_CONSTINIT.  */
5428       if (decl_spec_seq_has_spec_p (declspecs, ds_constinit))
5429 	retrofit_lang_decl (decl);
5430       decl = push_template_decl (decl);
5431     }
5432   if (decl == error_mark_node)
5433     return error_mark_node;
5434 
5435   if (VAR_P (decl)
5436       && DECL_NAMESPACE_SCOPE_P (decl) && !TREE_PUBLIC (decl) && !was_public
5437       && !DECL_THIS_STATIC (decl) && !DECL_ARTIFICIAL (decl))
5438     {
5439       /* This is a const variable with implicit 'static'.  Set
5440 	 DECL_THIS_STATIC so we can tell it from variables that are
5441 	 !TREE_PUBLIC because of the anonymous namespace.  */
5442       gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (decl)) || errorcount);
5443       DECL_THIS_STATIC (decl) = 1;
5444     }
5445 
5446   if (current_function_decl && VAR_P (decl)
5447       && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
5448     {
5449       bool ok = false;
5450       if (CP_DECL_THREAD_LOCAL_P (decl) && !DECL_REALLY_EXTERN (decl))
5451 	error_at (DECL_SOURCE_LOCATION (decl),
5452 		  "%qD declared %<thread_local%> in %qs function", decl,
5453 		  DECL_IMMEDIATE_FUNCTION_P (current_function_decl)
5454 		  ? "consteval" : "constexpr");
5455       else if (TREE_STATIC (decl))
5456 	error_at (DECL_SOURCE_LOCATION (decl),
5457 		  "%qD declared %<static%> in %qs function", decl,
5458 		  DECL_IMMEDIATE_FUNCTION_P (current_function_decl)
5459 		  ? "consteval" : "constexpr");
5460       else
5461 	ok = true;
5462       if (!ok)
5463 	cp_function_chain->invalid_constexpr = true;
5464     }
5465 
5466   if (!processing_template_decl && VAR_P (decl))
5467     start_decl_1 (decl, initialized);
5468 
5469   return decl;
5470 }
5471 
5472 /* Process the declaration of a variable DECL.  INITIALIZED is true
5473    iff DECL is explicitly initialized.  (INITIALIZED is false if the
5474    variable is initialized via an implicitly-called constructor.)
5475    This function must be called for ordinary variables (including, for
5476    example, implicit instantiations of templates), but must not be
5477    called for template declarations.  */
5478 
5479 void
start_decl_1(tree decl,bool initialized)5480 start_decl_1 (tree decl, bool initialized)
5481 {
5482   tree type;
5483   bool complete_p;
5484   bool aggregate_definition_p;
5485 
5486   gcc_assert (!processing_template_decl);
5487 
5488   if (error_operand_p (decl))
5489     return;
5490 
5491   gcc_assert (VAR_P (decl));
5492 
5493   type = TREE_TYPE (decl);
5494   complete_p = COMPLETE_TYPE_P (type);
5495   aggregate_definition_p = MAYBE_CLASS_TYPE_P (type) && !DECL_EXTERNAL (decl);
5496 
5497   /* If an explicit initializer is present, or if this is a definition
5498      of an aggregate, then we need a complete type at this point.
5499      (Scalars are always complete types, so there is nothing to
5500      check.)  This code just sets COMPLETE_P; errors (if necessary)
5501      are issued below.  */
5502   if ((initialized || aggregate_definition_p)
5503       && !complete_p
5504       && COMPLETE_TYPE_P (complete_type (type)))
5505     {
5506       complete_p = true;
5507       /* We will not yet have set TREE_READONLY on DECL if the type
5508 	 was "const", but incomplete, before this point.  But, now, we
5509 	 have a complete type, so we can try again.  */
5510       cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
5511     }
5512 
5513   if (is_global_var (decl))
5514     {
5515       type_context_kind context = (DECL_THREAD_LOCAL_P (decl)
5516 				   ? TCTX_THREAD_STORAGE
5517 				   : TCTX_STATIC_STORAGE);
5518       verify_type_context (input_location, context, TREE_TYPE (decl));
5519     }
5520   if (initialized)
5521     /* Is it valid for this decl to have an initializer at all?  */
5522     {
5523       /* Don't allow initializations for incomplete types except for
5524 	 arrays which might be completed by the initialization.  */
5525       if (complete_p)
5526 	;			/* A complete type is ok.  */
5527       else if (type_uses_auto (type))
5528 	; 			/* An auto type is ok.  */
5529       else if (TREE_CODE (type) != ARRAY_TYPE)
5530 	{
5531 	  error ("variable %q#D has initializer but incomplete type", decl);
5532 	  type = TREE_TYPE (decl) = error_mark_node;
5533 	}
5534       else if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
5535 	{
5536 	  if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
5537 	    error ("elements of array %q#D have incomplete type", decl);
5538 	  /* else we already gave an error in start_decl.  */
5539 	}
5540     }
5541   else if (aggregate_definition_p && !complete_p)
5542     {
5543       if (type_uses_auto (type))
5544 	gcc_assert (CLASS_PLACEHOLDER_TEMPLATE (type));
5545       else
5546 	{
5547 	  error ("aggregate %q#D has incomplete type and cannot be defined",
5548 		 decl);
5549 	  /* Change the type so that assemble_variable will give
5550 	     DECL an rtl we can live with: (mem (const_int 0)).  */
5551 	  type = TREE_TYPE (decl) = error_mark_node;
5552 	}
5553     }
5554 
5555   /* Create a new scope to hold this declaration if necessary.
5556      Whether or not a new scope is necessary cannot be determined
5557      until after the type has been completed; if the type is a
5558      specialization of a class template it is not until after
5559      instantiation has occurred that TYPE_HAS_NONTRIVIAL_DESTRUCTOR
5560      will be set correctly.  */
5561   maybe_push_cleanup_level (type);
5562 }
5563 
5564 /* Handle initialization of references.  DECL, TYPE, and INIT have the
5565    same meaning as in cp_finish_decl.  *CLEANUP must be NULL on entry,
5566    but will be set to a new CLEANUP_STMT if a temporary is created
5567    that must be destroyed subsequently.
5568 
5569    Returns an initializer expression to use to initialize DECL, or
5570    NULL if the initialization can be performed statically.
5571 
5572    Quotes on semantics can be found in ARM 8.4.3.  */
5573 
5574 static tree
grok_reference_init(tree decl,tree type,tree init,int flags)5575 grok_reference_init (tree decl, tree type, tree init, int flags)
5576 {
5577   if (init == NULL_TREE)
5578     {
5579       if ((DECL_LANG_SPECIFIC (decl) == 0
5580 	   || DECL_IN_AGGR_P (decl) == 0)
5581 	  && ! DECL_THIS_EXTERN (decl))
5582 	error_at (DECL_SOURCE_LOCATION (decl),
5583 		  "%qD declared as reference but not initialized", decl);
5584       return NULL_TREE;
5585     }
5586 
5587   tree ttype = TREE_TYPE (type);
5588   if (TREE_CODE (init) == TREE_LIST)
5589     {
5590       /* This handles (C++20 only) code like
5591 
5592 	   const A& r(1, 2, 3);
5593 
5594 	 where we treat the parenthesized list as a CONSTRUCTOR.  */
5595       if (TREE_TYPE (init) == NULL_TREE
5596 	  && CP_AGGREGATE_TYPE_P (ttype)
5597 	  && !DECL_DECOMPOSITION_P (decl)
5598 	  && (cxx_dialect >= cxx2a))
5599 	{
5600 	  /* We don't know yet if we should treat const A& r(1) as
5601 	     const A& r{1}.  */
5602 	  if (list_length (init) == 1)
5603 	    {
5604 	      flags |= LOOKUP_AGGREGATE_PAREN_INIT;
5605 	      init = build_x_compound_expr_from_list (init, ELK_INIT,
5606 						      tf_warning_or_error);
5607 	    }
5608 	  /* If the list had more than one element, the code is ill-formed
5609 	     pre-C++20, so we can build a constructor right away.  */
5610 	  else
5611 	    {
5612 	      init = build_constructor_from_list (init_list_type_node, init);
5613 	      CONSTRUCTOR_IS_DIRECT_INIT (init) = true;
5614 	      CONSTRUCTOR_IS_PAREN_INIT (init) = true;
5615 	    }
5616 	}
5617       else
5618 	init = build_x_compound_expr_from_list (init, ELK_INIT,
5619 						tf_warning_or_error);
5620     }
5621 
5622   if (TREE_CODE (ttype) != ARRAY_TYPE
5623       && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
5624     /* Note: default conversion is only called in very special cases.  */
5625     init = decay_conversion (init, tf_warning_or_error);
5626 
5627   /* check_initializer handles this for non-reference variables, but for
5628      references we need to do it here or the initializer will get the
5629      incomplete array type and confuse later calls to
5630      cp_complete_array_type.  */
5631   if (TREE_CODE (ttype) == ARRAY_TYPE
5632       && TYPE_DOMAIN (ttype) == NULL_TREE
5633       && (BRACE_ENCLOSED_INITIALIZER_P (init)
5634 	  || TREE_CODE (init) == STRING_CST))
5635     {
5636       cp_complete_array_type (&ttype, init, false);
5637       if (ttype != TREE_TYPE (type))
5638 	type = cp_build_reference_type (ttype, TYPE_REF_IS_RVALUE (type));
5639     }
5640 
5641   /* Convert INIT to the reference type TYPE.  This may involve the
5642      creation of a temporary, whose lifetime must be the same as that
5643      of the reference.  If so, a DECL_EXPR for the temporary will be
5644      added just after the DECL_EXPR for DECL.  That's why we don't set
5645      DECL_INITIAL for local references (instead assigning to them
5646      explicitly); we need to allow the temporary to be initialized
5647      first.  */
5648   return initialize_reference (type, init, flags,
5649 			       tf_warning_or_error);
5650 }
5651 
5652 /* Designated initializers in arrays are not supported in GNU C++.
5653    The parser cannot detect this error since it does not know whether
5654    a given brace-enclosed initializer is for a class type or for an
5655    array.  This function checks that CE does not use a designated
5656    initializer.  If it does, an error is issued.  Returns true if CE
5657    is valid, i.e., does not have a designated initializer.  */
5658 
5659 bool
check_array_designated_initializer(constructor_elt * ce,unsigned HOST_WIDE_INT index)5660 check_array_designated_initializer (constructor_elt *ce,
5661 				    unsigned HOST_WIDE_INT index)
5662 {
5663   /* Designated initializers for array elements are not supported.  */
5664   if (ce->index)
5665     {
5666       /* The parser only allows identifiers as designated
5667 	 initializers.  */
5668       if (ce->index == error_mark_node)
5669 	{
5670 	  error ("name used in a GNU-style designated "
5671 		 "initializer for an array");
5672 	  return false;
5673 	}
5674       else if (identifier_p (ce->index))
5675 	{
5676 	  error ("name %qD used in a GNU-style designated "
5677 		 "initializer for an array", ce->index);
5678 	  return false;
5679 	}
5680 
5681       tree ce_index = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5682 						  ce->index, true);
5683       if (ce_index
5684 	  && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (ce_index))
5685 	  && (TREE_CODE (ce_index = fold_non_dependent_expr (ce_index))
5686 	      == INTEGER_CST))
5687 	{
5688 	  /* A C99 designator is OK if it matches the current index.  */
5689 	  if (wi::to_wide (ce_index) == index)
5690 	    {
5691 	      ce->index = ce_index;
5692 	      return true;
5693 	    }
5694 	  else
5695 	    sorry ("non-trivial designated initializers not supported");
5696 	}
5697       else
5698 	error_at (cp_expr_loc_or_input_loc (ce->index),
5699 		  "C99 designator %qE is not an integral constant-expression",
5700 		  ce->index);
5701 
5702       return false;
5703     }
5704 
5705   return true;
5706 }
5707 
5708 /* When parsing `int a[] = {1, 2};' we don't know the size of the
5709    array until we finish parsing the initializer.  If that's the
5710    situation we're in, update DECL accordingly.  */
5711 
5712 static void
maybe_deduce_size_from_array_init(tree decl,tree init)5713 maybe_deduce_size_from_array_init (tree decl, tree init)
5714 {
5715   tree type = TREE_TYPE (decl);
5716 
5717   if (TREE_CODE (type) == ARRAY_TYPE
5718       && TYPE_DOMAIN (type) == NULL_TREE
5719       && TREE_CODE (decl) != TYPE_DECL)
5720     {
5721       /* do_default is really a C-ism to deal with tentative definitions.
5722 	 But let's leave it here to ease the eventual merge.  */
5723       int do_default = !DECL_EXTERNAL (decl);
5724       tree initializer = init ? init : DECL_INITIAL (decl);
5725       int failure = 0;
5726 
5727       /* Check that there are no designated initializers in INIT, as
5728 	 those are not supported in GNU C++, and as the middle-end
5729 	 will crash if presented with a non-numeric designated
5730 	 initializer.  */
5731       if (initializer && BRACE_ENCLOSED_INITIALIZER_P (initializer))
5732 	{
5733 	  vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initializer);
5734 	  constructor_elt *ce;
5735 	  HOST_WIDE_INT i;
5736 	  FOR_EACH_VEC_SAFE_ELT (v, i, ce)
5737 	    {
5738 	      if (instantiation_dependent_expression_p (ce->index))
5739 		return;
5740 	      if (!check_array_designated_initializer (ce, i))
5741 		failure = 1;
5742 	    }
5743 	}
5744 
5745       if (failure)
5746 	TREE_TYPE (decl) = error_mark_node;
5747       else
5748 	{
5749 	  failure = cp_complete_array_type (&TREE_TYPE (decl), initializer,
5750 					    do_default);
5751 	  if (failure == 1)
5752 	    {
5753 	      error_at (cp_expr_loc_or_loc (initializer,
5754 					 DECL_SOURCE_LOCATION (decl)),
5755 			"initializer fails to determine size of %qD", decl);
5756 	    }
5757 	  else if (failure == 2)
5758 	    {
5759 	      if (do_default)
5760 		{
5761 		  error_at (DECL_SOURCE_LOCATION (decl),
5762 			    "array size missing in %qD", decl);
5763 		}
5764 	      /* If a `static' var's size isn't known, make it extern as
5765 		 well as static, so it does not get allocated.  If it's not
5766 		 `static', then don't mark it extern; finish_incomplete_decl
5767 		 will give it a default size and it will get allocated.  */
5768 	      else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
5769 		DECL_EXTERNAL (decl) = 1;
5770 	    }
5771 	  else if (failure == 3)
5772 	    {
5773 	      error_at (DECL_SOURCE_LOCATION (decl),
5774 			"zero-size array %qD", decl);
5775 	    }
5776 	}
5777 
5778       cp_apply_type_quals_to_decl (cp_type_quals (TREE_TYPE (decl)), decl);
5779 
5780       relayout_decl (decl);
5781     }
5782 }
5783 
5784 /* Set DECL_SIZE, DECL_ALIGN, etc. for DECL (a VAR_DECL), and issue
5785    any appropriate error messages regarding the layout.  */
5786 
5787 static void
layout_var_decl(tree decl)5788 layout_var_decl (tree decl)
5789 {
5790   tree type;
5791 
5792   type = TREE_TYPE (decl);
5793   if (type == error_mark_node)
5794     return;
5795 
5796   /* If we haven't already laid out this declaration, do so now.
5797      Note that we must not call complete type for an external object
5798      because it's type might involve templates that we are not
5799      supposed to instantiate yet.  (And it's perfectly valid to say
5800      `extern X x' for some incomplete type `X'.)  */
5801   if (!DECL_EXTERNAL (decl))
5802     complete_type (type);
5803   if (!DECL_SIZE (decl)
5804       && TREE_TYPE (decl) != error_mark_node
5805       && complete_or_array_type_p (type))
5806     layout_decl (decl, 0);
5807 
5808   if (!DECL_EXTERNAL (decl) && DECL_SIZE (decl) == NULL_TREE)
5809     {
5810       /* An automatic variable with an incomplete type: that is an error.
5811 	 Don't talk about array types here, since we took care of that
5812 	 message in grokdeclarator.  */
5813       error_at (DECL_SOURCE_LOCATION (decl),
5814 		"storage size of %qD isn%'t known", decl);
5815       TREE_TYPE (decl) = error_mark_node;
5816     }
5817 #if 0
5818   /* Keep this code around in case we later want to control debug info
5819      based on whether a type is "used".  (jason 1999-11-11) */
5820 
5821   else if (!DECL_EXTERNAL (decl) && MAYBE_CLASS_TYPE_P (ttype))
5822     /* Let debugger know it should output info for this type.  */
5823     note_debug_info_needed (ttype);
5824 
5825   if (TREE_STATIC (decl) && DECL_CLASS_SCOPE_P (decl))
5826     note_debug_info_needed (DECL_CONTEXT (decl));
5827 #endif
5828 
5829   if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
5830       && DECL_SIZE (decl) != NULL_TREE
5831       && ! TREE_CONSTANT (DECL_SIZE (decl)))
5832     {
5833       if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
5834 	constant_expression_warning (DECL_SIZE (decl));
5835       else
5836 	{
5837 	  error_at (DECL_SOURCE_LOCATION (decl),
5838 		    "storage size of %qD isn%'t constant", decl);
5839 	  TREE_TYPE (decl) = error_mark_node;
5840 	  type = error_mark_node;
5841 	}
5842     }
5843 
5844   /* If the final element initializes a flexible array field, add the size of
5845      that initializer to DECL's size.  */
5846   if (type != error_mark_node
5847       && DECL_INITIAL (decl)
5848       && TREE_CODE (DECL_INITIAL (decl)) == CONSTRUCTOR
5849       && !vec_safe_is_empty (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)))
5850       && DECL_SIZE (decl) != NULL_TREE
5851       && TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST
5852       && TYPE_SIZE (type) != NULL_TREE
5853       && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
5854       && tree_int_cst_equal (DECL_SIZE (decl), TYPE_SIZE (type)))
5855     {
5856       constructor_elt &elt = CONSTRUCTOR_ELTS (DECL_INITIAL (decl))->last ();
5857       if (elt.index)
5858 	{
5859 	  tree itype = TREE_TYPE (elt.index);
5860 	  tree vtype = TREE_TYPE (elt.value);
5861 	  if (TREE_CODE (itype) == ARRAY_TYPE
5862 	      && TYPE_DOMAIN (itype) == NULL
5863 	      && TREE_CODE (vtype) == ARRAY_TYPE
5864 	      && COMPLETE_TYPE_P (vtype))
5865 	    {
5866 	      DECL_SIZE (decl)
5867 		= size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (vtype));
5868 	      DECL_SIZE_UNIT (decl)
5869 		= size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl),
5870 			      TYPE_SIZE_UNIT (vtype));
5871 	    }
5872 	}
5873     }
5874 }
5875 
5876 /* If a local static variable is declared in an inline function, or if
5877    we have a weak definition, we must endeavor to create only one
5878    instance of the variable at link-time.  */
5879 
5880 void
maybe_commonize_var(tree decl)5881 maybe_commonize_var (tree decl)
5882 {
5883   /* Don't mess with __FUNCTION__ and similar.  */
5884   if (DECL_ARTIFICIAL (decl))
5885     return;
5886 
5887   /* Static data in a function with comdat linkage also has comdat
5888      linkage.  */
5889   if ((TREE_STATIC (decl)
5890        && DECL_FUNCTION_SCOPE_P (decl)
5891        && vague_linkage_p (DECL_CONTEXT (decl)))
5892       || (TREE_PUBLIC (decl) && DECL_INLINE_VAR_P (decl)))
5893     {
5894       if (flag_weak)
5895 	{
5896 	  /* With weak symbols, we simply make the variable COMDAT;
5897 	     that will cause copies in multiple translations units to
5898 	     be merged.  */
5899 	  comdat_linkage (decl);
5900 	}
5901       else
5902 	{
5903 	  if (DECL_INITIAL (decl) == NULL_TREE
5904 	      || DECL_INITIAL (decl) == error_mark_node)
5905 	    {
5906 	      /* Without weak symbols, we can use COMMON to merge
5907 		 uninitialized variables.  */
5908 	      TREE_PUBLIC (decl) = 1;
5909 	      DECL_COMMON (decl) = 1;
5910 	    }
5911 	  else
5912 	    {
5913 	      /* While for initialized variables, we must use internal
5914 		 linkage -- which means that multiple copies will not
5915 		 be merged.  */
5916 	      TREE_PUBLIC (decl) = 0;
5917 	      DECL_COMMON (decl) = 0;
5918 	      DECL_INTERFACE_KNOWN (decl) = 1;
5919 	      const char *msg;
5920 	      if (DECL_INLINE_VAR_P (decl))
5921 		msg = G_("sorry: semantics of inline variable "
5922 			 "%q#D are wrong (you%'ll wind up with "
5923 			 "multiple copies)");
5924 	      else
5925 		msg = G_("sorry: semantics of inline function "
5926 			 "static data %q#D are wrong (you%'ll wind "
5927 			 "up with multiple copies)");
5928 	      if (warning_at (DECL_SOURCE_LOCATION (decl), 0,
5929 			      msg, decl))
5930 		inform (DECL_SOURCE_LOCATION (decl),
5931 			"you can work around this by removing the initializer");
5932 	    }
5933 	}
5934     }
5935 }
5936 
5937 /* Issue an error message if DECL is an uninitialized const variable.
5938    CONSTEXPR_CONTEXT_P is true when the function is called in a constexpr
5939    context from potential_constant_expression.  Returns true if all is well,
5940    false otherwise.  */
5941 
5942 bool
check_for_uninitialized_const_var(tree decl,bool constexpr_context_p,tsubst_flags_t complain)5943 check_for_uninitialized_const_var (tree decl, bool constexpr_context_p,
5944 				   tsubst_flags_t complain)
5945 {
5946   tree type = strip_array_types (TREE_TYPE (decl));
5947 
5948   /* ``Unless explicitly declared extern, a const object does not have
5949      external linkage and must be initialized. ($8.4; $12.1)'' ARM
5950      7.1.6 */
5951   if (VAR_P (decl)
5952       && !TYPE_REF_P (type)
5953       && (CP_TYPE_CONST_P (type)
5954 	  /* C++20 permits trivial default initialization in constexpr
5955 	     context (P1331R2).  */
5956 	  || (cxx_dialect < cxx2a
5957 	      && (constexpr_context_p
5958 		  || var_in_constexpr_fn (decl))))
5959       && !DECL_NONTRIVIALLY_INITIALIZED_P (decl))
5960     {
5961       tree field = default_init_uninitialized_part (type);
5962       if (!field)
5963 	return true;
5964 
5965       bool show_notes = true;
5966 
5967       if (!constexpr_context_p || cxx_dialect >= cxx2a)
5968 	{
5969 	  if (CP_TYPE_CONST_P (type))
5970 	    {
5971 	      if (complain & tf_error)
5972 		show_notes = permerror (DECL_SOURCE_LOCATION (decl),
5973 				        "uninitialized %<const %D%>", decl);
5974 	    }
5975 	  else
5976 	    {
5977 	      if (!is_instantiation_of_constexpr (current_function_decl)
5978 		  && (complain & tf_error))
5979 		error_at (DECL_SOURCE_LOCATION (decl),
5980 			  "uninitialized variable %qD in %<constexpr%> "
5981 			  "function", decl);
5982 	      else
5983 		show_notes = false;
5984 	      cp_function_chain->invalid_constexpr = true;
5985 	    }
5986 	}
5987       else if (complain & tf_error)
5988 	error_at (DECL_SOURCE_LOCATION (decl),
5989 		  "uninitialized variable %qD in %<constexpr%> context",
5990 		  decl);
5991 
5992       if (show_notes && CLASS_TYPE_P (type) && (complain & tf_error))
5993 	{
5994 	  tree defaulted_ctor;
5995 
5996 	  inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
5997 		  "%q#T has no user-provided default constructor", type);
5998 	  defaulted_ctor = in_class_defaulted_default_constructor (type);
5999 	  if (defaulted_ctor)
6000 	    inform (DECL_SOURCE_LOCATION (defaulted_ctor),
6001 		    "constructor is not user-provided because it is "
6002 		    "explicitly defaulted in the class body");
6003 	  inform (DECL_SOURCE_LOCATION (field),
6004 		  "and the implicitly-defined constructor does not "
6005 		  "initialize %q#D", field);
6006 	}
6007 
6008       return false;
6009     }
6010 
6011   return true;
6012 }
6013 
6014 /* Structure holding the current initializer being processed by reshape_init.
6015    CUR is a pointer to the current element being processed, END is a pointer
6016    after the last element present in the initializer.  */
6017 struct reshape_iter
6018 {
6019   constructor_elt *cur;
6020   constructor_elt *end;
6021 };
6022 
6023 static tree reshape_init_r (tree, reshape_iter *, tree, tsubst_flags_t);
6024 
6025 /* FIELD is a FIELD_DECL or NULL.  In the former case, the value
6026    returned is the next FIELD_DECL (possibly FIELD itself) that can be
6027    initialized.  If there are no more such fields, the return value
6028    will be NULL.  */
6029 
6030 tree
next_initializable_field(tree field)6031 next_initializable_field (tree field)
6032 {
6033   while (field
6034 	 && (TREE_CODE (field) != FIELD_DECL
6035 	     || DECL_UNNAMED_BIT_FIELD (field)
6036 	     || (DECL_ARTIFICIAL (field)
6037 		 /* In C++17, don't skip base class fields.  */
6038 		 && !(cxx_dialect >= cxx17 && DECL_FIELD_IS_BASE (field))
6039 		 /* Don't skip vptr fields.  We might see them when we're
6040 		    called from reduced_constant_expression_p.  */
6041 		 && !DECL_VIRTUAL_P (field))))
6042     field = DECL_CHAIN (field);
6043 
6044   return field;
6045 }
6046 
6047 /* Return true for [dcl.init.list] direct-list-initialization from
6048    single element of enumeration with a fixed underlying type.  */
6049 
6050 bool
is_direct_enum_init(tree type,tree init)6051 is_direct_enum_init (tree type, tree init)
6052 {
6053   if (cxx_dialect >= cxx17
6054       && TREE_CODE (type) == ENUMERAL_TYPE
6055       && ENUM_FIXED_UNDERLYING_TYPE_P (type)
6056       && TREE_CODE (init) == CONSTRUCTOR
6057       && CONSTRUCTOR_IS_DIRECT_INIT (init)
6058       && CONSTRUCTOR_NELTS (init) == 1)
6059     return true;
6060   return false;
6061 }
6062 
6063 /* Subroutine of reshape_init_array and reshape_init_vector, which does
6064    the actual work. ELT_TYPE is the element type of the array. MAX_INDEX is an
6065    INTEGER_CST representing the size of the array minus one (the maximum index),
6066    or NULL_TREE if the array was declared without specifying the size. D is
6067    the iterator within the constructor.  */
6068 
6069 static tree
reshape_init_array_1(tree elt_type,tree max_index,reshape_iter * d,tree first_initializer_p,tsubst_flags_t complain)6070 reshape_init_array_1 (tree elt_type, tree max_index, reshape_iter *d,
6071 		      tree first_initializer_p, tsubst_flags_t complain)
6072 {
6073   tree new_init;
6074   bool sized_array_p = (max_index && TREE_CONSTANT (max_index));
6075   unsigned HOST_WIDE_INT max_index_cst = 0;
6076   unsigned HOST_WIDE_INT index;
6077 
6078   /* The initializer for an array is always a CONSTRUCTOR.  If this is the
6079      outermost CONSTRUCTOR and the element type is non-aggregate, we don't need
6080      to build a new one.  But don't reuse if not complaining; if this is
6081      tentative, we might also reshape to another type (95319).  */
6082   bool reuse = (first_initializer_p
6083 		&& (complain & tf_error)
6084 		&& !CP_AGGREGATE_TYPE_P (elt_type)
6085 		&& !TREE_SIDE_EFFECTS (first_initializer_p));
6086   if (reuse)
6087     new_init = first_initializer_p;
6088   else
6089     new_init = build_constructor (init_list_type_node, NULL);
6090 
6091   if (sized_array_p)
6092     {
6093       /* Minus 1 is used for zero sized arrays.  */
6094       if (integer_all_onesp (max_index))
6095 	return new_init;
6096 
6097       if (tree_fits_uhwi_p (max_index))
6098 	max_index_cst = tree_to_uhwi (max_index);
6099       /* sizetype is sign extended, not zero extended.  */
6100       else
6101 	max_index_cst = tree_to_uhwi (fold_convert (size_type_node, max_index));
6102     }
6103 
6104   /* Loop until there are no more initializers.  */
6105   for (index = 0;
6106        d->cur != d->end && (!sized_array_p || index <= max_index_cst);
6107        ++index)
6108     {
6109       tree elt_init;
6110       constructor_elt *old_cur = d->cur;
6111 
6112       check_array_designated_initializer (d->cur, index);
6113       elt_init = reshape_init_r (elt_type, d,
6114 				 /*first_initializer_p=*/NULL_TREE,
6115 				 complain);
6116       if (elt_init == error_mark_node)
6117 	return error_mark_node;
6118       tree idx = size_int (index);
6119       if (reuse)
6120 	{
6121 	  old_cur->index = idx;
6122 	  old_cur->value = elt_init;
6123 	}
6124       else
6125 	CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init),
6126 				idx, elt_init);
6127       if (!TREE_CONSTANT (elt_init))
6128 	TREE_CONSTANT (new_init) = false;
6129 
6130       /* This can happen with an invalid initializer (c++/54501).  */
6131       if (d->cur == old_cur && !sized_array_p)
6132 	break;
6133     }
6134 
6135   return new_init;
6136 }
6137 
6138 /* Subroutine of reshape_init_r, processes the initializers for arrays.
6139    Parameters are the same of reshape_init_r.  */
6140 
6141 static tree
reshape_init_array(tree type,reshape_iter * d,tree first_initializer_p,tsubst_flags_t complain)6142 reshape_init_array (tree type, reshape_iter *d, tree first_initializer_p,
6143 		    tsubst_flags_t complain)
6144 {
6145   tree max_index = NULL_TREE;
6146 
6147   gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
6148 
6149   if (TYPE_DOMAIN (type))
6150     max_index = array_type_nelts (type);
6151 
6152   return reshape_init_array_1 (TREE_TYPE (type), max_index, d,
6153 			       first_initializer_p, complain);
6154 }
6155 
6156 /* Subroutine of reshape_init_r, processes the initializers for vectors.
6157    Parameters are the same of reshape_init_r.  */
6158 
6159 static tree
reshape_init_vector(tree type,reshape_iter * d,tsubst_flags_t complain)6160 reshape_init_vector (tree type, reshape_iter *d, tsubst_flags_t complain)
6161 {
6162   tree max_index = NULL_TREE;
6163 
6164   gcc_assert (VECTOR_TYPE_P (type));
6165 
6166   if (COMPOUND_LITERAL_P (d->cur->value))
6167     {
6168       tree value = d->cur->value;
6169       if (!same_type_p (TREE_TYPE (value), type))
6170 	{
6171 	  if (complain & tf_error)
6172 	    error ("invalid type %qT as initializer for a vector of type %qT",
6173 		   TREE_TYPE (d->cur->value), type);
6174 	  value = error_mark_node;
6175 	}
6176       ++d->cur;
6177       return value;
6178     }
6179 
6180   /* For a vector, we initialize it as an array of the appropriate size.  */
6181   if (VECTOR_TYPE_P (type))
6182     max_index = size_int (TYPE_VECTOR_SUBPARTS (type) - 1);
6183 
6184   return reshape_init_array_1 (TREE_TYPE (type), max_index, d,
6185 			       NULL_TREE, complain);
6186 }
6187 
6188 /* Subroutine of reshape_init_r, processes the initializers for classes
6189    or union. Parameters are the same of reshape_init_r.  */
6190 
6191 static tree
reshape_init_class(tree type,reshape_iter * d,bool first_initializer_p,tsubst_flags_t complain)6192 reshape_init_class (tree type, reshape_iter *d, bool first_initializer_p,
6193 		    tsubst_flags_t complain)
6194 {
6195   tree field;
6196   tree new_init;
6197 
6198   gcc_assert (CLASS_TYPE_P (type));
6199 
6200   /* The initializer for a class is always a CONSTRUCTOR.  */
6201   new_init = build_constructor (init_list_type_node, NULL);
6202   field = next_initializable_field (TYPE_FIELDS (type));
6203 
6204   if (!field)
6205     {
6206       /* [dcl.init.aggr]
6207 
6208 	An initializer for an aggregate member that is an
6209 	empty class shall have the form of an empty
6210 	initializer-list {}.  */
6211       if (!first_initializer_p)
6212 	{
6213 	  if (complain & tf_error)
6214 	    error ("initializer for %qT must be brace-enclosed", type);
6215 	  return error_mark_node;
6216 	}
6217       return new_init;
6218     }
6219 
6220   /* Loop through the initializable fields, gathering initializers.  */
6221   while (d->cur != d->end)
6222     {
6223       tree field_init;
6224       constructor_elt *old_cur = d->cur;
6225 
6226       /* Handle designated initializers, as an extension.  */
6227       if (d->cur->index)
6228 	{
6229 	  if (d->cur->index == error_mark_node)
6230 	    return error_mark_node;
6231 
6232 	  if (TREE_CODE (d->cur->index) == FIELD_DECL)
6233 	    {
6234 	      /* We already reshaped this.  */
6235 	      if (field != d->cur->index)
6236 		{
6237 		  tree id = DECL_NAME (d->cur->index);
6238 		  gcc_assert (id);
6239 		  gcc_checking_assert (d->cur->index
6240 				       == get_class_binding (type, id));
6241 		  field = d->cur->index;
6242 		}
6243 	    }
6244 	  else if (TREE_CODE (d->cur->index) == IDENTIFIER_NODE)
6245 	    field = get_class_binding (type, d->cur->index);
6246 	  else
6247 	    {
6248 	      if (complain & tf_error)
6249 		error ("%<[%E] =%> used in a GNU-style designated initializer"
6250 		       " for class %qT", d->cur->index, type);
6251 	      return error_mark_node;
6252 	    }
6253 
6254 	  if (!field || TREE_CODE (field) != FIELD_DECL)
6255 	    {
6256 	      if (complain & tf_error)
6257 		error ("%qT has no non-static data member named %qD", type,
6258 		       d->cur->index);
6259 	      return error_mark_node;
6260 	    }
6261 	}
6262 
6263       /* If we processed all the member of the class, we are done.  */
6264       if (!field)
6265 	break;
6266 
6267       field_init = reshape_init_r (TREE_TYPE (field), d,
6268 				   /*first_initializer_p=*/NULL_TREE,
6269 				   complain);
6270       if (field_init == error_mark_node)
6271 	return error_mark_node;
6272 
6273       if (d->cur == old_cur && d->cur->index)
6274 	{
6275 	  /* This can happen with an invalid initializer for a flexible
6276 	     array member (c++/54441).  */
6277 	  if (complain & tf_error)
6278 	    error ("invalid initializer for %q#D", field);
6279 	  return error_mark_node;
6280 	}
6281 
6282       CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init), field, field_init);
6283 
6284       /* [dcl.init.aggr]
6285 
6286 	When a union  is  initialized with a brace-enclosed
6287 	initializer, the braces shall only contain an
6288 	initializer for the first member of the union.  */
6289       if (TREE_CODE (type) == UNION_TYPE)
6290 	break;
6291 
6292       field = next_initializable_field (DECL_CHAIN (field));
6293     }
6294 
6295   return new_init;
6296 }
6297 
6298 /* Subroutine of reshape_init_r.  We're in a context where C99 initializer
6299    designators are not valid; either complain or return true to indicate
6300    that reshape_init_r should return error_mark_node.  */
6301 
6302 static bool
has_designator_problem(reshape_iter * d,tsubst_flags_t complain)6303 has_designator_problem (reshape_iter *d, tsubst_flags_t complain)
6304 {
6305   if (d->cur->index)
6306     {
6307       if (complain & tf_error)
6308 	error_at (cp_expr_loc_or_input_loc (d->cur->index),
6309 		  "C99 designator %qE outside aggregate initializer",
6310 		  d->cur->index);
6311       else
6312 	return true;
6313     }
6314   return false;
6315 }
6316 
6317 /* Subroutine of reshape_init, which processes a single initializer (part of
6318    a CONSTRUCTOR). TYPE is the type of the variable being initialized, D is the
6319    iterator within the CONSTRUCTOR which points to the initializer to process.
6320    If this is the first initializer of the outermost CONSTRUCTOR node,
6321    FIRST_INITIALIZER_P is that CONSTRUCTOR; otherwise, it is NULL_TREE.  */
6322 
6323 static tree
reshape_init_r(tree type,reshape_iter * d,tree first_initializer_p,tsubst_flags_t complain)6324 reshape_init_r (tree type, reshape_iter *d, tree first_initializer_p,
6325 		tsubst_flags_t complain)
6326 {
6327   tree init = d->cur->value;
6328 
6329   if (error_operand_p (init))
6330     return error_mark_node;
6331 
6332   if (first_initializer_p && !CP_AGGREGATE_TYPE_P (type)
6333       && has_designator_problem (d, complain))
6334     return error_mark_node;
6335 
6336   tree stripped_init = tree_strip_any_location_wrapper (init);
6337 
6338   if (TREE_CODE (type) == COMPLEX_TYPE)
6339     {
6340       /* A complex type can be initialized from one or two initializers,
6341 	 but braces are not elided.  */
6342       d->cur++;
6343       if (BRACE_ENCLOSED_INITIALIZER_P (stripped_init))
6344 	{
6345 	  if (CONSTRUCTOR_NELTS (stripped_init) > 2)
6346 	    {
6347 	      if (complain & tf_error)
6348 		error ("too many initializers for %qT", type);
6349 	      else
6350 		return error_mark_node;
6351 	    }
6352 	}
6353       else if (first_initializer_p && d->cur != d->end)
6354 	{
6355 	  vec<constructor_elt, va_gc> *v = 0;
6356 	  CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, init);
6357 	  CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, d->cur->value);
6358 	  if (has_designator_problem (d, complain))
6359 	    return error_mark_node;
6360 	  d->cur++;
6361 	  init = build_constructor (init_list_type_node, v);
6362 	}
6363       return init;
6364     }
6365 
6366   /* A non-aggregate type is always initialized with a single
6367      initializer.  */
6368   if (!CP_AGGREGATE_TYPE_P (type))
6369     {
6370       /* It is invalid to initialize a non-aggregate type with a
6371 	 brace-enclosed initializer before C++0x.
6372 	 We need to check for BRACE_ENCLOSED_INITIALIZER_P here because
6373 	 of g++.old-deja/g++.mike/p7626.C: a pointer-to-member constant is
6374 	 a CONSTRUCTOR (with a record type).  */
6375       if (TREE_CODE (stripped_init) == CONSTRUCTOR
6376 	  /* Don't complain about a capture-init.  */
6377 	  && !CONSTRUCTOR_IS_DIRECT_INIT (stripped_init)
6378 	  && BRACE_ENCLOSED_INITIALIZER_P (stripped_init))  /* p7626.C */
6379 	{
6380 	  if (SCALAR_TYPE_P (type))
6381 	    {
6382 	      if (cxx_dialect < cxx11)
6383 		{
6384 		  if (complain & tf_error)
6385 		    error ("braces around scalar initializer for type %qT",
6386 			   type);
6387 		  init = error_mark_node;
6388 		}
6389 	      else if (first_initializer_p
6390 		       || (CONSTRUCTOR_NELTS (stripped_init) > 0
6391 			   && (BRACE_ENCLOSED_INITIALIZER_P
6392 			       (CONSTRUCTOR_ELT (stripped_init,0)->value))))
6393 		{
6394 		  if (complain & tf_error)
6395 		    error ("too many braces around scalar initializer "
6396 		           "for type %qT", type);
6397 		  init = error_mark_node;
6398 		}
6399 	    }
6400 	  else
6401 	    maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6402 	}
6403 
6404       d->cur++;
6405       return init;
6406     }
6407 
6408   /* "If T is a class type and the initializer list has a single element of
6409      type cv U, where U is T or a class derived from T, the object is
6410      initialized from that element."  Even if T is an aggregate.  */
6411   if (cxx_dialect >= cxx11 && (CLASS_TYPE_P (type) || VECTOR_TYPE_P (type))
6412       && first_initializer_p
6413       && d->end - d->cur == 1
6414       && reference_related_p (type, TREE_TYPE (init)))
6415     {
6416       d->cur++;
6417       return init;
6418     }
6419 
6420   /* [dcl.init.aggr]
6421 
6422      All implicit type conversions (clause _conv_) are considered when
6423      initializing the aggregate member with an initializer from an
6424      initializer-list.  If the initializer can initialize a member,
6425      the member is initialized.  Otherwise, if the member is itself a
6426      non-empty subaggregate, brace elision is assumed and the
6427      initializer is considered for the initialization of the first
6428      member of the subaggregate.  */
6429   if ((TREE_CODE (init) != CONSTRUCTOR || COMPOUND_LITERAL_P (init))
6430       /* But don't try this for the first initializer, since that would be
6431 	 looking through the outermost braces; A a2 = { a1 }; is not a
6432 	 valid aggregate initialization.  */
6433       && !first_initializer_p
6434       && (same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (init))
6435 	  || can_convert_arg (type, TREE_TYPE (init), init, LOOKUP_NORMAL,
6436 			      complain)))
6437     {
6438       d->cur++;
6439       return init;
6440     }
6441 
6442   /* [dcl.init.string]
6443 
6444       A char array (whether plain char, signed char, or unsigned char)
6445       can be initialized by a string-literal (optionally enclosed in
6446       braces); a wchar_t array can be initialized by a wide
6447       string-literal (optionally enclosed in braces).  */
6448   if (TREE_CODE (type) == ARRAY_TYPE
6449       && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type))))
6450     {
6451       tree str_init = init;
6452       tree stripped_str_init = stripped_init;
6453 
6454       /* Strip one level of braces if and only if they enclose a single
6455 	 element (as allowed by [dcl.init.string]).  */
6456       if (!first_initializer_p
6457 	  && TREE_CODE (stripped_str_init) == CONSTRUCTOR
6458 	  && CONSTRUCTOR_NELTS (stripped_str_init) == 1)
6459 	{
6460 	  str_init = (*CONSTRUCTOR_ELTS (stripped_str_init))[0].value;
6461 	  stripped_str_init = tree_strip_any_location_wrapper (str_init);
6462 	}
6463 
6464       /* If it's a string literal, then it's the initializer for the array
6465 	 as a whole. Otherwise, continue with normal initialization for
6466 	 array types (one value per array element).  */
6467       if (TREE_CODE (stripped_str_init) == STRING_CST)
6468 	{
6469 	  if (has_designator_problem (d, complain))
6470 	    return error_mark_node;
6471 	  d->cur++;
6472 	  return str_init;
6473 	}
6474     }
6475 
6476   /* The following cases are about aggregates. If we are not within a full
6477      initializer already, and there is not a CONSTRUCTOR, it means that there
6478      is a missing set of braces (that is, we are processing the case for
6479      which reshape_init exists).  */
6480   if (!first_initializer_p)
6481     {
6482       if (TREE_CODE (stripped_init) == CONSTRUCTOR)
6483 	{
6484 	  tree init_type = TREE_TYPE (init);
6485 	  if (init_type && TYPE_PTRMEMFUNC_P (init_type))
6486 	    /* There is no need to call reshape_init for pointer-to-member
6487 	       function initializers, as they are always constructed correctly
6488 	       by the front end.  Here we have e.g. {.__pfn=0B, .__delta=0},
6489 	       which is missing outermost braces.  We should warn below, and
6490 	       one of the routines below will wrap it in additional { }.  */;
6491 	  /* For a nested compound literal, proceed to specialized routines,
6492 	     to handle initialization of arrays and similar.  */
6493 	  else if (COMPOUND_LITERAL_P (stripped_init))
6494 	    gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (stripped_init));
6495 	  /* A CONSTRUCTOR of the target's type is a previously
6496 	     digested initializer.  */
6497 	  else if (same_type_ignoring_top_level_qualifiers_p (type, init_type))
6498 	    {
6499 	      ++d->cur;
6500 	      return init;
6501 	    }
6502 	  else
6503 	    {
6504 	      /* Something that hasn't been reshaped yet.  */
6505 	      ++d->cur;
6506 	      gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (stripped_init));
6507 	      return reshape_init (type, init, complain);
6508 	    }
6509 	}
6510 
6511       if (complain & tf_warning)
6512 	warning (OPT_Wmissing_braces,
6513 		 "missing braces around initializer for %qT",
6514 		 type);
6515     }
6516 
6517   /* Dispatch to specialized routines.  */
6518   if (CLASS_TYPE_P (type))
6519     return reshape_init_class (type, d, first_initializer_p, complain);
6520   else if (TREE_CODE (type) == ARRAY_TYPE)
6521     return reshape_init_array (type, d, first_initializer_p, complain);
6522   else if (VECTOR_TYPE_P (type))
6523     return reshape_init_vector (type, d, complain);
6524   else
6525     gcc_unreachable();
6526 }
6527 
6528 /* Undo the brace-elision allowed by [dcl.init.aggr] in a
6529    brace-enclosed aggregate initializer.
6530 
6531    INIT is the CONSTRUCTOR containing the list of initializers describing
6532    a brace-enclosed initializer for an entity of the indicated aggregate TYPE.
6533    It may not presently match the shape of the TYPE; for example:
6534 
6535      struct S { int a; int b; };
6536      struct S a[] = { 1, 2, 3, 4 };
6537 
6538    Here INIT will hold a vector of four elements, rather than a
6539    vector of two elements, each itself a vector of two elements.  This
6540    routine transforms INIT from the former form into the latter.  The
6541    revised CONSTRUCTOR node is returned.  */
6542 
6543 tree
reshape_init(tree type,tree init,tsubst_flags_t complain)6544 reshape_init (tree type, tree init, tsubst_flags_t complain)
6545 {
6546   vec<constructor_elt, va_gc> *v;
6547   reshape_iter d;
6548   tree new_init;
6549 
6550   gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
6551 
6552   v = CONSTRUCTOR_ELTS (init);
6553 
6554   /* An empty constructor does not need reshaping, and it is always a valid
6555      initializer.  */
6556   if (vec_safe_is_empty (v))
6557     return init;
6558 
6559   /* Brace elision is not performed for a CONSTRUCTOR representing
6560      parenthesized aggregate initialization.  */
6561   if (CONSTRUCTOR_IS_PAREN_INIT (init))
6562     return init;
6563 
6564   /* Handle [dcl.init.list] direct-list-initialization from
6565      single element of enumeration with a fixed underlying type.  */
6566   if (is_direct_enum_init (type, init))
6567     {
6568       tree elt = CONSTRUCTOR_ELT (init, 0)->value;
6569       type = cv_unqualified (type);
6570       if (check_narrowing (ENUM_UNDERLYING_TYPE (type), elt, complain))
6571 	{
6572 	  warning_sentinel w (warn_useless_cast);
6573 	  warning_sentinel w2 (warn_ignored_qualifiers);
6574 	  return cp_build_c_cast (input_location, type, elt,
6575 				  tf_warning_or_error);
6576 	}
6577       else
6578 	return error_mark_node;
6579     }
6580 
6581   /* Recurse on this CONSTRUCTOR.  */
6582   d.cur = &(*v)[0];
6583   d.end = d.cur + v->length ();
6584 
6585   new_init = reshape_init_r (type, &d, init, complain);
6586   if (new_init == error_mark_node)
6587     return error_mark_node;
6588 
6589   /* Make sure all the element of the constructor were used. Otherwise,
6590      issue an error about exceeding initializers.  */
6591   if (d.cur != d.end)
6592     {
6593       if (complain & tf_error)
6594 	error ("too many initializers for %qT", type);
6595       return error_mark_node;
6596     }
6597 
6598   if (CONSTRUCTOR_IS_DIRECT_INIT (init)
6599       && BRACE_ENCLOSED_INITIALIZER_P (new_init))
6600     CONSTRUCTOR_IS_DIRECT_INIT (new_init) = true;
6601   if (CONSTRUCTOR_IS_DESIGNATED_INIT (init)
6602       && BRACE_ENCLOSED_INITIALIZER_P (new_init))
6603     CONSTRUCTOR_IS_DESIGNATED_INIT (new_init) = true;
6604 
6605   return new_init;
6606 }
6607 
6608 /* Verify array initializer.  Returns true if errors have been reported.  */
6609 
6610 bool
check_array_initializer(tree decl,tree type,tree init)6611 check_array_initializer (tree decl, tree type, tree init)
6612 {
6613   tree element_type = TREE_TYPE (type);
6614 
6615   /* Structured binding when initialized with an array type needs
6616      to have complete type.  */
6617   if (decl
6618       && DECL_DECOMPOSITION_P (decl)
6619       && !DECL_DECOMP_BASE (decl)
6620       && !COMPLETE_TYPE_P (type))
6621     {
6622       error_at (DECL_SOURCE_LOCATION (decl),
6623 		"structured binding has incomplete type %qT", type);
6624       TREE_TYPE (decl) = error_mark_node;
6625       return true;
6626     }
6627 
6628   /* The array type itself need not be complete, because the
6629      initializer may tell us how many elements are in the array.
6630      But, the elements of the array must be complete.  */
6631   if (!COMPLETE_TYPE_P (complete_type (element_type)))
6632     {
6633       if (decl)
6634 	error_at (DECL_SOURCE_LOCATION (decl),
6635 		  "elements of array %q#D have incomplete type", decl);
6636       else
6637 	error ("elements of array %q#T have incomplete type", type);
6638       return true;
6639     }
6640 
6641   location_t loc = (decl ? location_of (decl) : input_location);
6642   if (!verify_type_context (loc, TCTX_ARRAY_ELEMENT, element_type))
6643     return true;
6644 
6645   /* A compound literal can't have variable size.  */
6646   if (init && !decl
6647       && ((COMPLETE_TYPE_P (type) && !TREE_CONSTANT (TYPE_SIZE (type)))
6648 	  || !TREE_CONSTANT (TYPE_SIZE (element_type))))
6649     {
6650       error ("variable-sized compound literal");
6651       return true;
6652     }
6653   return false;
6654 }
6655 
6656 /* Subroutine of check_initializer; args are passed down from that function.
6657    Set stmts_are_full_exprs_p to 1 across a call to build_aggr_init.  */
6658 
6659 static tree
build_aggr_init_full_exprs(tree decl,tree init,int flags)6660 build_aggr_init_full_exprs (tree decl, tree init, int flags)
6661 
6662 {
6663   gcc_assert (stmts_are_full_exprs_p ());
6664   return build_aggr_init (decl, init, flags, tf_warning_or_error);
6665 }
6666 
6667 /* Verify INIT (the initializer for DECL), and record the
6668    initialization in DECL_INITIAL, if appropriate.  CLEANUP is as for
6669    grok_reference_init.
6670 
6671    If the return value is non-NULL, it is an expression that must be
6672    evaluated dynamically to initialize DECL.  */
6673 
6674 static tree
check_initializer(tree decl,tree init,int flags,vec<tree,va_gc> ** cleanups)6675 check_initializer (tree decl, tree init, int flags, vec<tree, va_gc> **cleanups)
6676 {
6677   tree type;
6678   tree init_code = NULL;
6679   tree core_type;
6680 
6681   /* Things that are going to be initialized need to have complete
6682      type.  */
6683   TREE_TYPE (decl) = type = complete_type (TREE_TYPE (decl));
6684 
6685   if (DECL_HAS_VALUE_EXPR_P (decl))
6686     {
6687       /* A variable with DECL_HAS_VALUE_EXPR_P set is just a placeholder,
6688 	 it doesn't have storage to be initialized.  */
6689       gcc_assert (init == NULL_TREE);
6690       return NULL_TREE;
6691     }
6692 
6693   if (type == error_mark_node)
6694     /* We will have already complained.  */
6695     return NULL_TREE;
6696 
6697   if (TREE_CODE (type) == ARRAY_TYPE)
6698     {
6699       if (check_array_initializer (decl, type, init))
6700 	return NULL_TREE;
6701     }
6702   else if (!COMPLETE_TYPE_P (type))
6703     {
6704       error_at (DECL_SOURCE_LOCATION (decl),
6705 		"%q#D has incomplete type", decl);
6706       TREE_TYPE (decl) = error_mark_node;
6707       return NULL_TREE;
6708     }
6709   else
6710     /* There is no way to make a variable-sized class type in GNU C++.  */
6711     gcc_assert (TREE_CONSTANT (TYPE_SIZE (type)));
6712 
6713   if (init && BRACE_ENCLOSED_INITIALIZER_P (init))
6714     {
6715       int init_len = CONSTRUCTOR_NELTS (init);
6716       if (SCALAR_TYPE_P (type))
6717 	{
6718 	  if (init_len == 0)
6719 	    {
6720 	      maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6721 	      init = build_zero_init (type, NULL_TREE, false);
6722 	    }
6723 	  else if (init_len != 1 && TREE_CODE (type) != COMPLEX_TYPE)
6724 	    {
6725 	      error_at (cp_expr_loc_or_loc (init, DECL_SOURCE_LOCATION (decl)),
6726 			"scalar object %qD requires one element in "
6727 			"initializer", decl);
6728 	      TREE_TYPE (decl) = error_mark_node;
6729 	      return NULL_TREE;
6730 	    }
6731 	}
6732     }
6733 
6734   if (TREE_CODE (decl) == CONST_DECL)
6735     {
6736       gcc_assert (!TYPE_REF_P (type));
6737 
6738       DECL_INITIAL (decl) = init;
6739 
6740       gcc_assert (init != NULL_TREE);
6741       init = NULL_TREE;
6742     }
6743   else if (!init && DECL_REALLY_EXTERN (decl))
6744     ;
6745   else if (init || type_build_ctor_call (type)
6746 	   || TYPE_REF_P (type))
6747     {
6748       if (TYPE_REF_P (type))
6749 	{
6750 	  init = grok_reference_init (decl, type, init, flags);
6751 	  flags |= LOOKUP_ALREADY_DIGESTED;
6752 	}
6753       else if (!init)
6754 	check_for_uninitialized_const_var (decl, /*constexpr_context_p=*/false,
6755 					   tf_warning_or_error);
6756       /* Do not reshape constructors of vectors (they don't need to be
6757 	 reshaped.  */
6758       else if (BRACE_ENCLOSED_INITIALIZER_P (init))
6759 	{
6760 	  if (is_std_init_list (type))
6761 	    {
6762 	      init = perform_implicit_conversion (type, init,
6763 						  tf_warning_or_error);
6764 	      flags |= LOOKUP_ALREADY_DIGESTED;
6765 	    }
6766 	  else if (TYPE_NON_AGGREGATE_CLASS (type))
6767 	    {
6768 	      /* Don't reshape if the class has constructors.  */
6769 	      if (cxx_dialect == cxx98)
6770 		error_at (cp_expr_loc_or_loc (init, DECL_SOURCE_LOCATION (decl)),
6771 			  "in C++98 %qD must be initialized by "
6772 			  "constructor, not by %<{...}%>",
6773 			  decl);
6774 	    }
6775 	  else if (VECTOR_TYPE_P (type) && TYPE_VECTOR_OPAQUE (type))
6776 	    {
6777 	      error ("opaque vector types cannot be initialized");
6778 	      init = error_mark_node;
6779 	    }
6780 	  else
6781 	    {
6782 	      init = reshape_init (type, init, tf_warning_or_error);
6783 	      flags |= LOOKUP_NO_NARROWING;
6784 	    }
6785 	}
6786       /* [dcl.init] "Otherwise, if the destination type is an array, the object
6787 	 is initialized as follows..."  So handle things like
6788 
6789 	  int a[](1, 2, 3);
6790 
6791 	 which is permitted in C++20 by P0960.  */
6792       else if (TREE_CODE (init) == TREE_LIST
6793 	       && TREE_TYPE (init) == NULL_TREE
6794 	       && TREE_CODE (type) == ARRAY_TYPE
6795 	       && !DECL_DECOMPOSITION_P (decl)
6796 	       && (cxx_dialect >= cxx2a))
6797 	{
6798 	  /* [dcl.init.string] "An array of ordinary character type [...]
6799 	     can be initialized by an ordinary string literal [...] by an
6800 	     appropriately-typed string literal enclosed in braces" only
6801 	     talks about braces, but GCC has always accepted
6802 
6803 	       char a[]("foobar");
6804 
6805 	     so we continue to do so.  */
6806 	  tree val = TREE_VALUE (init);
6807 	  if (TREE_CHAIN (init) == NULL_TREE
6808 	      && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type)))
6809 	      && TREE_CODE (tree_strip_any_location_wrapper (val))
6810 		 == STRING_CST)
6811 	    /* If the list has a single element and it's a string literal,
6812 	       then it's the initializer for the array as a whole.  */
6813 	    init = val;
6814 	  else
6815 	    {
6816 	      init = build_constructor_from_list (init_list_type_node, init);
6817 	      CONSTRUCTOR_IS_DIRECT_INIT (init) = true;
6818 	      CONSTRUCTOR_IS_PAREN_INIT (init) = true;
6819 	    }
6820 	}
6821       else if (TREE_CODE (init) == TREE_LIST
6822 	       && TREE_TYPE (init) != unknown_type_node
6823 	       && !MAYBE_CLASS_TYPE_P (type))
6824 	{
6825 	  gcc_assert (TREE_CODE (decl) != RESULT_DECL);
6826 
6827 	  /* We get here with code like `int a (2);' */
6828 	  init = build_x_compound_expr_from_list (init, ELK_INIT,
6829 						  tf_warning_or_error);
6830 	}
6831 
6832       /* If DECL has an array type without a specific bound, deduce the
6833 	 array size from the initializer.  */
6834       maybe_deduce_size_from_array_init (decl, init);
6835       type = TREE_TYPE (decl);
6836       if (type == error_mark_node)
6837 	return NULL_TREE;
6838 
6839       if (((type_build_ctor_call (type) || CLASS_TYPE_P (type))
6840 	   && !(flags & LOOKUP_ALREADY_DIGESTED)
6841 	   && !(init && BRACE_ENCLOSED_INITIALIZER_P (init)
6842 		&& CP_AGGREGATE_TYPE_P (type)
6843 		&& (CLASS_TYPE_P (type)
6844 		    || !TYPE_NEEDS_CONSTRUCTING (type)
6845 		    || type_has_extended_temps (type))))
6846 	  || (DECL_DECOMPOSITION_P (decl) && TREE_CODE (type) == ARRAY_TYPE))
6847 	{
6848 	  init_code = build_aggr_init_full_exprs (decl, init, flags);
6849 
6850 	  /* A constructor call is a non-trivial initializer even if
6851 	     it isn't explicitly written.  */
6852 	  if (TREE_SIDE_EFFECTS (init_code))
6853 	    DECL_NONTRIVIALLY_INITIALIZED_P (decl) = true;
6854 
6855 	  /* If this is a constexpr initializer, expand_default_init will
6856 	     have returned an INIT_EXPR rather than a CALL_EXPR.  In that
6857 	     case, pull the initializer back out and pass it down into
6858 	     store_init_value.  */
6859 	  while (TREE_CODE (init_code) == EXPR_STMT
6860 		 || TREE_CODE (init_code) == CONVERT_EXPR)
6861 	    init_code = TREE_OPERAND (init_code, 0);
6862 	  if (TREE_CODE (init_code) == INIT_EXPR)
6863 	    {
6864 	      /* In C++20, the call to build_aggr_init could have created
6865 		 an INIT_EXPR with a CONSTRUCTOR as the RHS to handle
6866 		 A(1, 2).  */
6867 	      init = TREE_OPERAND (init_code, 1);
6868 	      init_code = NULL_TREE;
6869 	      /* Don't call digest_init; it's unnecessary and will complain
6870 		 about aggregate initialization of non-aggregate classes.  */
6871 	      flags |= LOOKUP_ALREADY_DIGESTED;
6872 	    }
6873 	  else if (DECL_DECLARED_CONSTEXPR_P (decl)
6874 		   || (flags & LOOKUP_CONSTINIT))
6875 	    {
6876 	      /* Declared constexpr or constinit, but no suitable initializer;
6877 		 massage init appropriately so we can pass it into
6878 		 store_init_value for the error.  */
6879 	      tree new_init = NULL_TREE;
6880 	      if (!processing_template_decl
6881 		  && TREE_CODE (init_code) == CALL_EXPR)
6882 		new_init = build_cplus_new (type, init_code, tf_none);
6883 	      else if (CLASS_TYPE_P (type)
6884 		       && (!init || TREE_CODE (init) == TREE_LIST))
6885 		new_init = build_functional_cast (input_location, type,
6886 						  init, tf_none);
6887 	      if (new_init)
6888 		{
6889 		  init = new_init;
6890 		  if (TREE_CODE (init) == TARGET_EXPR
6891 		      && !(flags & LOOKUP_ONLYCONVERTING))
6892 		    TARGET_EXPR_DIRECT_INIT_P (init) = true;
6893 		}
6894 	      init_code = NULL_TREE;
6895 	    }
6896 	  else
6897 	    init = NULL_TREE;
6898 	}
6899 
6900       if (init && TREE_CODE (init) != TREE_VEC)
6901 	{
6902 	  /* In aggregate initialization of a variable, each element
6903 	     initialization is a full-expression because there is no
6904 	     enclosing expression.  */
6905 	  gcc_assert (stmts_are_full_exprs_p ());
6906 
6907 	  init_code = store_init_value (decl, init, cleanups, flags);
6908 
6909 	  if (DECL_INITIAL (decl)
6910 	      && TREE_CODE (DECL_INITIAL (decl)) == CONSTRUCTOR
6911 	      && !vec_safe_is_empty (CONSTRUCTOR_ELTS (DECL_INITIAL (decl))))
6912 	    {
6913 	      tree elt = CONSTRUCTOR_ELTS (DECL_INITIAL (decl))->last ().value;
6914 	      if (TREE_CODE (TREE_TYPE (elt)) == ARRAY_TYPE
6915 		  && TYPE_SIZE (TREE_TYPE (elt)) == NULL_TREE)
6916 		cp_complete_array_type (&TREE_TYPE (elt), elt, false);
6917 	    }
6918 
6919 	  if (pedantic && TREE_CODE (type) == ARRAY_TYPE
6920 	      && DECL_INITIAL (decl)
6921 	      && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
6922 	      && PAREN_STRING_LITERAL_P (DECL_INITIAL (decl)))
6923 	    warning_at (cp_expr_loc_or_loc (DECL_INITIAL (decl),
6924 					 DECL_SOURCE_LOCATION (decl)),
6925 			0, "array %qD initialized by parenthesized "
6926 			"string literal %qE",
6927 			decl, DECL_INITIAL (decl));
6928 	  init = NULL_TREE;
6929 	}
6930     }
6931   else
6932     {
6933       if (CLASS_TYPE_P (core_type = strip_array_types (type))
6934 	  && (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type)
6935 	      || CLASSTYPE_REF_FIELDS_NEED_INIT (core_type)))
6936 	diagnose_uninitialized_cst_or_ref_member (core_type, /*using_new=*/false,
6937 						  /*complain=*/true);
6938 
6939       check_for_uninitialized_const_var (decl, /*constexpr_context_p=*/false,
6940 					 tf_warning_or_error);
6941     }
6942 
6943   if (init && init != error_mark_node)
6944     init_code = build2 (INIT_EXPR, type, decl, init);
6945 
6946   if (init_code && !TREE_SIDE_EFFECTS (init_code)
6947       && init_code != error_mark_node)
6948     init_code = NULL_TREE;
6949 
6950   if (init_code)
6951     {
6952       /* We might have set these in cp_finish_decl.  */
6953       DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = false;
6954       TREE_CONSTANT (decl) = false;
6955     }
6956 
6957   if (init_code
6958       && DECL_IN_AGGR_P (decl)
6959       && DECL_INITIALIZED_IN_CLASS_P (decl))
6960     {
6961       static int explained = 0;
6962 
6963       if (cxx_dialect < cxx11)
6964 	error ("initializer invalid for static member with constructor");
6965       else if (cxx_dialect < cxx17)
6966 	error ("non-constant in-class initialization invalid for static "
6967 	       "member %qD", decl);
6968       else
6969 	error ("non-constant in-class initialization invalid for non-inline "
6970 	       "static member %qD", decl);
6971       if (!explained)
6972 	{
6973 	  inform (input_location,
6974 		  "(an out of class initialization is required)");
6975 	  explained = 1;
6976 	}
6977       return NULL_TREE;
6978     }
6979 
6980   return init_code;
6981 }
6982 
6983 /* If DECL is not a local variable, give it RTL.  */
6984 
6985 static void
make_rtl_for_nonlocal_decl(tree decl,tree init,const char * asmspec)6986 make_rtl_for_nonlocal_decl (tree decl, tree init, const char* asmspec)
6987 {
6988   int toplev = toplevel_bindings_p ();
6989   int defer_p;
6990 
6991   /* Set the DECL_ASSEMBLER_NAME for the object.  */
6992   if (asmspec)
6993     {
6994       /* The `register' keyword, when used together with an
6995 	 asm-specification, indicates that the variable should be
6996 	 placed in a particular register.  */
6997       if (VAR_P (decl) && DECL_REGISTER (decl))
6998 	{
6999 	  set_user_assembler_name (decl, asmspec);
7000 	  DECL_HARD_REGISTER (decl) = 1;
7001 	}
7002       else
7003 	{
7004 	  if (TREE_CODE (decl) == FUNCTION_DECL
7005 	      && fndecl_built_in_p (decl, BUILT_IN_NORMAL))
7006 	    set_builtin_user_assembler_name (decl, asmspec);
7007 	  set_user_assembler_name (decl, asmspec);
7008 	}
7009     }
7010 
7011   /* Handle non-variables up front.  */
7012   if (!VAR_P (decl))
7013     {
7014       rest_of_decl_compilation (decl, toplev, at_eof);
7015       return;
7016     }
7017 
7018   /* If we see a class member here, it should be a static data
7019      member.  */
7020   if (DECL_LANG_SPECIFIC (decl) && DECL_IN_AGGR_P (decl))
7021     {
7022       gcc_assert (TREE_STATIC (decl));
7023       /* An in-class declaration of a static data member should be
7024 	 external; it is only a declaration, and not a definition.  */
7025       if (init == NULL_TREE)
7026 	gcc_assert (DECL_EXTERNAL (decl)
7027 		    || !TREE_PUBLIC (decl));
7028     }
7029 
7030   /* We don't create any RTL for local variables.  */
7031   if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
7032     return;
7033 
7034   /* We defer emission of local statics until the corresponding
7035      DECL_EXPR is expanded.  But with constexpr its function might never
7036      be expanded, so go ahead and tell cgraph about the variable now.  */
7037   defer_p = ((DECL_FUNCTION_SCOPE_P (decl)
7038 	      && !var_in_maybe_constexpr_fn (decl))
7039 	     || DECL_VIRTUAL_P (decl));
7040 
7041   /* Defer template instantiations.  */
7042   if (DECL_LANG_SPECIFIC (decl)
7043       && DECL_IMPLICIT_INSTANTIATION (decl))
7044     defer_p = 1;
7045 
7046   /* If we're not deferring, go ahead and assemble the variable.  */
7047   if (!defer_p)
7048     rest_of_decl_compilation (decl, toplev, at_eof);
7049 }
7050 
7051 /* walk_tree helper for wrap_temporary_cleanups, below.  */
7052 
7053 static tree
wrap_cleanups_r(tree * stmt_p,int * walk_subtrees,void * data)7054 wrap_cleanups_r (tree *stmt_p, int *walk_subtrees, void *data)
7055 {
7056   /* Stop at types or full-expression boundaries.  */
7057   if (TYPE_P (*stmt_p)
7058       || TREE_CODE (*stmt_p) == CLEANUP_POINT_EXPR)
7059     {
7060       *walk_subtrees = 0;
7061       return NULL_TREE;
7062     }
7063 
7064   if (TREE_CODE (*stmt_p) == TARGET_EXPR)
7065     {
7066       tree guard = (tree)data;
7067       tree tcleanup = TARGET_EXPR_CLEANUP (*stmt_p);
7068 
7069       tcleanup = build2 (TRY_CATCH_EXPR, void_type_node, tcleanup, guard);
7070       /* Tell honor_protect_cleanup_actions to handle this as a separate
7071 	 cleanup.  */
7072       TRY_CATCH_IS_CLEANUP (tcleanup) = 1;
7073 
7074       TARGET_EXPR_CLEANUP (*stmt_p) = tcleanup;
7075     }
7076 
7077   return NULL_TREE;
7078 }
7079 
7080 /* We're initializing a local variable which has a cleanup GUARD.  If there
7081    are any temporaries used in the initializer INIT of this variable, we
7082    need to wrap their cleanups with TRY_CATCH_EXPR (, GUARD) so that the
7083    variable will be cleaned up properly if one of them throws.
7084 
7085    Unfortunately, there's no way to express this properly in terms of
7086    nesting, as the regions for the temporaries overlap the region for the
7087    variable itself; if there are two temporaries, the variable needs to be
7088    the first thing destroyed if either of them throws.  However, we only
7089    want to run the variable's cleanup if it actually got constructed.  So
7090    we need to guard the temporary cleanups with the variable's cleanup if
7091    they are run on the normal path, but not if they are run on the
7092    exceptional path.  We implement this by telling
7093    honor_protect_cleanup_actions to strip the variable cleanup from the
7094    exceptional path.  */
7095 
7096 static void
wrap_temporary_cleanups(tree init,tree guard)7097 wrap_temporary_cleanups (tree init, tree guard)
7098 {
7099   cp_walk_tree_without_duplicates (&init, wrap_cleanups_r, (void *)guard);
7100 }
7101 
7102 /* Generate code to initialize DECL (a local variable).  */
7103 
7104 static void
initialize_local_var(tree decl,tree init)7105 initialize_local_var (tree decl, tree init)
7106 {
7107   tree type = TREE_TYPE (decl);
7108   tree cleanup;
7109   int already_used;
7110 
7111   gcc_assert (VAR_P (decl)
7112 	      || TREE_CODE (decl) == RESULT_DECL);
7113   gcc_assert (!TREE_STATIC (decl));
7114 
7115   if (DECL_SIZE (decl) == NULL_TREE)
7116     {
7117       /* If we used it already as memory, it must stay in memory.  */
7118       DECL_INITIAL (decl) = NULL_TREE;
7119       TREE_ADDRESSABLE (decl) = TREE_USED (decl);
7120       return;
7121     }
7122 
7123   if (type == error_mark_node)
7124     return;
7125 
7126   /* Compute and store the initial value.  */
7127   already_used = TREE_USED (decl) || TREE_USED (type);
7128   if (TREE_USED (type))
7129     DECL_READ_P (decl) = 1;
7130 
7131   /* Generate a cleanup, if necessary.  */
7132   cleanup = cxx_maybe_build_cleanup (decl, tf_warning_or_error);
7133 
7134   /* Perform the initialization.  */
7135   if (init)
7136     {
7137       tree rinit = (TREE_CODE (init) == INIT_EXPR
7138 		    ? TREE_OPERAND (init, 1) : NULL_TREE);
7139       if (rinit && !TREE_SIDE_EFFECTS (rinit))
7140 	{
7141 	  /* Stick simple initializers in DECL_INITIAL so that
7142 	     -Wno-init-self works (c++/34772).  */
7143 	  gcc_assert (TREE_OPERAND (init, 0) == decl);
7144 	  DECL_INITIAL (decl) = rinit;
7145 
7146 	  if (warn_init_self && TYPE_REF_P (type))
7147 	    {
7148 	      STRIP_NOPS (rinit);
7149 	      if (rinit == decl)
7150 		warning_at (DECL_SOURCE_LOCATION (decl),
7151 			    OPT_Winit_self,
7152 			    "reference %qD is initialized with itself", decl);
7153 	    }
7154 	}
7155       else
7156 	{
7157 	  int saved_stmts_are_full_exprs_p;
7158 
7159 	  /* If we're only initializing a single object, guard the
7160 	     destructors of any temporaries used in its initializer with
7161 	     its destructor.  This isn't right for arrays because each
7162 	     element initialization is a full-expression.  */
7163 	  if (cleanup && TREE_CODE (type) != ARRAY_TYPE)
7164 	    wrap_temporary_cleanups (init, cleanup);
7165 
7166 	  gcc_assert (building_stmt_list_p ());
7167 	  saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
7168 	  current_stmt_tree ()->stmts_are_full_exprs_p = 1;
7169 	  finish_expr_stmt (init);
7170 	  current_stmt_tree ()->stmts_are_full_exprs_p =
7171 	    saved_stmts_are_full_exprs_p;
7172 	}
7173     }
7174 
7175   /* Set this to 0 so we can tell whether an aggregate which was
7176      initialized was ever used.  Don't do this if it has a
7177      destructor, so we don't complain about the 'resource
7178      allocation is initialization' idiom.  Now set
7179      attribute((unused)) on types so decls of that type will be
7180      marked used. (see TREE_USED, above.)  */
7181   if (TYPE_NEEDS_CONSTRUCTING (type)
7182       && ! already_used
7183       && TYPE_HAS_TRIVIAL_DESTRUCTOR (type)
7184       && DECL_NAME (decl))
7185     TREE_USED (decl) = 0;
7186   else if (already_used)
7187     TREE_USED (decl) = 1;
7188 
7189   if (cleanup)
7190     finish_decl_cleanup (decl, cleanup);
7191 }
7192 
7193 /* DECL is a VAR_DECL for a compiler-generated variable with static
7194    storage duration (like a virtual table) whose initializer is a
7195    compile-time constant.  Initialize the variable and provide it to the
7196    back end.  */
7197 
7198 void
initialize_artificial_var(tree decl,vec<constructor_elt,va_gc> * v)7199 initialize_artificial_var (tree decl, vec<constructor_elt, va_gc> *v)
7200 {
7201   tree init;
7202   gcc_assert (DECL_ARTIFICIAL (decl));
7203   init = build_constructor (TREE_TYPE (decl), v);
7204   gcc_assert (TREE_CODE (init) == CONSTRUCTOR);
7205   DECL_INITIAL (decl) = init;
7206   DECL_INITIALIZED_P (decl) = 1;
7207   /* Mark the decl as constexpr so that we can access its content
7208      at compile time.  */
7209   DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
7210   DECL_DECLARED_CONSTEXPR_P (decl) = true;
7211   determine_visibility (decl);
7212   layout_var_decl (decl);
7213   maybe_commonize_var (decl);
7214   make_rtl_for_nonlocal_decl (decl, init, /*asmspec=*/NULL);
7215 }
7216 
7217 /* INIT is the initializer for a variable, as represented by the
7218    parser.  Returns true iff INIT is value-dependent.  */
7219 
7220 static bool
value_dependent_init_p(tree init)7221 value_dependent_init_p (tree init)
7222 {
7223   if (TREE_CODE (init) == TREE_LIST)
7224     /* A parenthesized initializer, e.g.: int i (3, 2); ? */
7225     return any_value_dependent_elements_p (init);
7226   else if (TREE_CODE (init) == CONSTRUCTOR)
7227   /* A brace-enclosed initializer, e.g.: int i = { 3 }; ? */
7228     {
7229       if (dependent_type_p (TREE_TYPE (init)))
7230 	return true;
7231 
7232       vec<constructor_elt, va_gc> *elts;
7233       size_t nelts;
7234       size_t i;
7235 
7236       elts = CONSTRUCTOR_ELTS (init);
7237       nelts = vec_safe_length (elts);
7238       for (i = 0; i < nelts; ++i)
7239 	if (value_dependent_init_p ((*elts)[i].value))
7240 	  return true;
7241     }
7242   else
7243     /* It must be a simple expression, e.g., int i = 3;  */
7244     return value_dependent_expression_p (init);
7245 
7246   return false;
7247 }
7248 
7249 // Returns true if a DECL is VAR_DECL with the concept specifier.
7250 static inline bool
is_concept_var(tree decl)7251 is_concept_var (tree decl)
7252 {
7253   return (VAR_P (decl)
7254 	  // Not all variables have DECL_LANG_SPECIFIC.
7255           && DECL_LANG_SPECIFIC (decl)
7256           && DECL_DECLARED_CONCEPT_P (decl));
7257 }
7258 
7259 /* A helper function to be called via walk_tree.  If any label exists
7260    under *TP, it is (going to be) forced.  Set has_forced_label_in_static.  */
7261 
7262 static tree
notice_forced_label_r(tree * tp,int * walk_subtrees,void *)7263 notice_forced_label_r (tree *tp, int *walk_subtrees, void *)
7264 {
7265   if (TYPE_P (*tp))
7266     *walk_subtrees = 0;
7267   if (TREE_CODE (*tp) == LABEL_DECL)
7268     cfun->has_forced_label_in_static = 1;
7269   return NULL_TREE;
7270 }
7271 
7272 /* Return true if DECL has either a trivial destructor, or for C++2A
7273    is constexpr and has a constexpr destructor.  */
7274 
7275 static bool
decl_maybe_constant_destruction(tree decl,tree type)7276 decl_maybe_constant_destruction (tree decl, tree type)
7277 {
7278   return (TYPE_HAS_TRIVIAL_DESTRUCTOR (type)
7279 	  || (cxx_dialect >= cxx2a
7280 	      && VAR_P (decl)
7281 	      && DECL_DECLARED_CONSTEXPR_P (decl)
7282 	      && type_has_constexpr_destructor (strip_array_types (type))));
7283 }
7284 
7285 static tree declare_simd_adjust_this (tree *, int *, void *);
7286 
7287 /* Helper function of omp_declare_variant_finalize.  Finalize one
7288    "omp declare variant base" attribute.  Return true if it should be
7289    removed.  */
7290 
7291 static bool
omp_declare_variant_finalize_one(tree decl,tree attr)7292 omp_declare_variant_finalize_one (tree decl, tree attr)
7293 {
7294   if (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
7295     {
7296       walk_tree (&TREE_VALUE (TREE_VALUE (attr)), declare_simd_adjust_this,
7297 		 DECL_ARGUMENTS (decl), NULL);
7298       walk_tree (&TREE_PURPOSE (TREE_VALUE (attr)), declare_simd_adjust_this,
7299 		 DECL_ARGUMENTS (decl), NULL);
7300     }
7301 
7302   tree ctx = TREE_VALUE (TREE_VALUE (attr));
7303   tree simd = omp_get_context_selector (ctx, "construct", "simd");
7304   if (simd)
7305     {
7306       TREE_VALUE (simd)
7307 	= c_omp_declare_simd_clauses_to_numbers (DECL_ARGUMENTS (decl),
7308 						 TREE_VALUE (simd));
7309       /* FIXME, adjusting simd args unimplemented.  */
7310       return true;
7311     }
7312 
7313   tree chain = TREE_CHAIN (TREE_VALUE (attr));
7314   location_t varid_loc
7315     = cp_expr_loc_or_input_loc (TREE_PURPOSE (TREE_CHAIN (chain)));
7316   location_t match_loc = cp_expr_loc_or_input_loc (TREE_PURPOSE (chain));
7317   cp_id_kind idk = (cp_id_kind) tree_to_uhwi (TREE_VALUE (chain));
7318   tree variant = TREE_PURPOSE (TREE_VALUE (attr));
7319 
7320   location_t save_loc = input_location;
7321   input_location = varid_loc;
7322 
7323   releasing_vec args;
7324   tree parm = DECL_ARGUMENTS (decl);
7325   if (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
7326     parm = DECL_CHAIN (parm);
7327   for (; parm; parm = DECL_CHAIN (parm))
7328     if (type_dependent_expression_p (parm))
7329       vec_safe_push (args, build_constructor (TREE_TYPE (parm), NULL));
7330     else if (MAYBE_CLASS_TYPE_P (TREE_TYPE (parm)))
7331       vec_safe_push (args, build_local_temp (TREE_TYPE (parm)));
7332     else
7333       vec_safe_push (args, build_zero_cst (TREE_TYPE (parm)));
7334 
7335   bool koenig_p = false;
7336   if (idk == CP_ID_KIND_UNQUALIFIED || idk == CP_ID_KIND_TEMPLATE_ID)
7337     {
7338       if (identifier_p (variant)
7339 	  /* In C++2A, we may need to perform ADL for a template
7340 	     name.  */
7341 	  || (TREE_CODE (variant) == TEMPLATE_ID_EXPR
7342 	      && identifier_p (TREE_OPERAND (variant, 0))))
7343 	{
7344 	  if (!args->is_empty ())
7345 	    {
7346 	      koenig_p = true;
7347 	      if (!any_type_dependent_arguments_p (args))
7348 		variant = perform_koenig_lookup (variant, args,
7349 						 tf_warning_or_error);
7350 	    }
7351 	  else
7352 	    variant = unqualified_fn_lookup_error (variant);
7353 	}
7354       else if (!args->is_empty () && is_overloaded_fn (variant))
7355 	{
7356 	  tree fn = get_first_fn (variant);
7357 	  fn = STRIP_TEMPLATE (fn);
7358 	  if (!((TREE_CODE (fn) == USING_DECL && DECL_DEPENDENT_P (fn))
7359 		 || DECL_FUNCTION_MEMBER_P (fn)
7360 		 || DECL_LOCAL_FUNCTION_P (fn)))
7361 	    {
7362 	      koenig_p = true;
7363 	      if (!any_type_dependent_arguments_p (args))
7364 		variant = perform_koenig_lookup (variant, args,
7365 						 tf_warning_or_error);
7366 	    }
7367 	}
7368     }
7369 
7370   if (idk == CP_ID_KIND_QUALIFIED)
7371     variant = finish_call_expr (variant, &args, /*disallow_virtual=*/true,
7372 				koenig_p, tf_warning_or_error);
7373   else
7374     variant = finish_call_expr (variant, &args, /*disallow_virtual=*/false,
7375 				koenig_p, tf_warning_or_error);
7376   if (variant == error_mark_node && !processing_template_decl)
7377     return true;
7378 
7379   variant = cp_get_callee_fndecl_nofold (variant);
7380   input_location = save_loc;
7381 
7382   if (variant)
7383     {
7384       const char *varname = IDENTIFIER_POINTER (DECL_NAME (variant));
7385       if (!comptypes (TREE_TYPE (decl), TREE_TYPE (variant), 0))
7386 	{
7387 	  error_at (varid_loc, "variant %qD and base %qD have incompatible "
7388 			       "types", variant, decl);
7389 	  return true;
7390 	}
7391       if (fndecl_built_in_p (variant)
7392 	  && (strncmp (varname, "__builtin_", strlen ("__builtin_")) == 0
7393 	      || strncmp (varname, "__sync_", strlen ("__sync_")) == 0
7394 	      || strncmp (varname, "__atomic_", strlen ("__atomic_")) == 0))
7395 	{
7396 	  error_at (varid_loc, "variant %qD is a built-in", variant);
7397 	  return true;
7398 	}
7399       else
7400 	{
7401 	  tree construct = omp_get_context_selector (ctx, "construct", NULL);
7402 	  c_omp_mark_declare_variant (match_loc, variant, construct);
7403 	  if (!omp_context_selector_matches (ctx))
7404 	    return true;
7405 	  TREE_PURPOSE (TREE_VALUE (attr)) = variant;
7406 	}
7407     }
7408   else if (!processing_template_decl)
7409     {
7410       error_at (varid_loc, "could not find variant declaration");
7411       return true;
7412     }
7413 
7414   return false;
7415 }
7416 
7417 /* Helper function, finish up "omp declare variant base" attribute
7418    now that there is a DECL.  ATTR is the first "omp declare variant base"
7419    attribute.  */
7420 
7421 void
omp_declare_variant_finalize(tree decl,tree attr)7422 omp_declare_variant_finalize (tree decl, tree attr)
7423 {
7424   size_t attr_len = strlen ("omp declare variant base");
7425   tree *list = &DECL_ATTRIBUTES (decl);
7426   bool remove_all = false;
7427   location_t match_loc = DECL_SOURCE_LOCATION (decl);
7428   if (TREE_CHAIN (TREE_VALUE (attr))
7429       && TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr)))
7430       && EXPR_HAS_LOCATION (TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr)))))
7431     match_loc = EXPR_LOCATION (TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr))));
7432   if (DECL_CONSTRUCTOR_P (decl))
7433     {
7434       error_at (match_loc, "%<declare variant%> on constructor %qD", decl);
7435       remove_all = true;
7436     }
7437   else if (DECL_DESTRUCTOR_P (decl))
7438     {
7439       error_at (match_loc, "%<declare variant%> on destructor %qD", decl);
7440       remove_all = true;
7441     }
7442   else if (DECL_DEFAULTED_FN (decl))
7443     {
7444       error_at (match_loc, "%<declare variant%> on defaulted %qD", decl);
7445       remove_all = true;
7446     }
7447   else if (DECL_DELETED_FN (decl))
7448     {
7449       error_at (match_loc, "%<declare variant%> on deleted %qD", decl);
7450       remove_all = true;
7451     }
7452   else if (DECL_VIRTUAL_P (decl))
7453     {
7454       error_at (match_loc, "%<declare variant%> on virtual %qD", decl);
7455       remove_all = true;
7456     }
7457   /* This loop is like private_lookup_attribute, except that it works
7458      with tree * rather than tree, as we might want to remove the
7459      attributes that are diagnosed as errorneous.  */
7460   while (*list)
7461     {
7462       tree attr = get_attribute_name (*list);
7463       size_t ident_len = IDENTIFIER_LENGTH (attr);
7464       if (cmp_attribs ("omp declare variant base", attr_len,
7465 		       IDENTIFIER_POINTER (attr), ident_len))
7466 	{
7467 	  if (remove_all || omp_declare_variant_finalize_one (decl, *list))
7468 	    {
7469 	      *list = TREE_CHAIN (*list);
7470 	      continue;
7471 	    }
7472 	}
7473       list = &TREE_CHAIN (*list);
7474     }
7475 }
7476 
7477 /* Finish processing of a declaration;
7478    install its line number and initial value.
7479    If the length of an array type is not known before,
7480    it must be determined now, from the initial value, or it is an error.
7481 
7482    INIT is the initializer (if any) for DECL.  If INIT_CONST_EXPR_P is
7483    true, then INIT is an integral constant expression.
7484 
7485    FLAGS is LOOKUP_ONLYCONVERTING if the = init syntax was used, else 0
7486    if the (init) syntax was used.  */
7487 
7488 void
cp_finish_decl(tree decl,tree init,bool init_const_expr_p,tree asmspec_tree,int flags)7489 cp_finish_decl (tree decl, tree init, bool init_const_expr_p,
7490 		tree asmspec_tree, int flags)
7491 {
7492   tree type;
7493   vec<tree, va_gc> *cleanups = NULL;
7494   const char *asmspec = NULL;
7495   int was_readonly = 0;
7496   bool var_definition_p = false;
7497   tree auto_node;
7498 
7499   if (decl == error_mark_node)
7500     return;
7501   else if (! decl)
7502     {
7503       if (init)
7504 	error ("assignment (not initialization) in declaration");
7505       return;
7506     }
7507 
7508   gcc_assert (TREE_CODE (decl) != RESULT_DECL);
7509   /* Parameters are handled by store_parm_decls, not cp_finish_decl.  */
7510   gcc_assert (TREE_CODE (decl) != PARM_DECL);
7511 
7512   type = TREE_TYPE (decl);
7513   if (type == error_mark_node)
7514     return;
7515 
7516   /* Warn about register storage specifiers except when in GNU global
7517      or local register variable extension.  */
7518   if (VAR_P (decl) && DECL_REGISTER (decl) && asmspec_tree == NULL_TREE)
7519     {
7520       if (cxx_dialect >= cxx17)
7521 	pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
7522 		 "ISO C++17 does not allow %<register%> storage "
7523 		 "class specifier");
7524       else
7525 	warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
7526 		    "%<register%> storage class specifier used");
7527     }
7528 
7529   /* If a name was specified, get the string.  */
7530   if (at_namespace_scope_p ())
7531     asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
7532   if (asmspec_tree && asmspec_tree != error_mark_node)
7533     asmspec = TREE_STRING_POINTER (asmspec_tree);
7534 
7535   bool in_class_decl
7536     = (current_class_type
7537        && CP_DECL_CONTEXT (decl) == current_class_type
7538        && TYPE_BEING_DEFINED (current_class_type)
7539        && !CLASSTYPE_TEMPLATE_INSTANTIATION (current_class_type));
7540 
7541   if (in_class_decl
7542       && (DECL_INITIAL (decl) || init))
7543     DECL_INITIALIZED_IN_CLASS_P (decl) = 1;
7544 
7545   /* Do auto deduction unless decl is a function or an uninstantiated
7546      template specialization.  */
7547   if (TREE_CODE (decl) != FUNCTION_DECL
7548       && !(init == NULL_TREE
7549 	   && DECL_LANG_SPECIFIC (decl)
7550 	   && DECL_TEMPLATE_INSTANTIATION (decl)
7551 	   && !DECL_TEMPLATE_INSTANTIATED (decl))
7552       && (auto_node = type_uses_auto (type)))
7553     {
7554       tree d_init;
7555       if (init == NULL_TREE)
7556 	gcc_assert (CLASS_PLACEHOLDER_TEMPLATE (auto_node));
7557       d_init = init;
7558       if (d_init)
7559 	{
7560 	  if (TREE_CODE (d_init) == TREE_LIST
7561 	      && !CLASS_PLACEHOLDER_TEMPLATE (auto_node))
7562 	    d_init = build_x_compound_expr_from_list (d_init, ELK_INIT,
7563 						      tf_warning_or_error);
7564 	  d_init = resolve_nondeduced_context (d_init, tf_warning_or_error);
7565 	}
7566       enum auto_deduction_context adc = adc_variable_type;
7567       if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
7568 	adc = adc_decomp_type;
7569       type = TREE_TYPE (decl) = do_auto_deduction (type, d_init, auto_node,
7570 						   tf_warning_or_error, adc,
7571 						   NULL_TREE, flags);
7572       if (type == error_mark_node)
7573 	return;
7574       if (TREE_CODE (type) == FUNCTION_TYPE)
7575 	{
7576 	  error ("initializer for %<decltype(auto) %D%> has function type; "
7577 		 "did you forget the %<()%>?", decl);
7578 	  TREE_TYPE (decl) = error_mark_node;
7579 	  return;
7580 	}
7581       cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
7582     }
7583 
7584   if (ensure_literal_type_for_constexpr_object (decl) == error_mark_node)
7585     {
7586       DECL_DECLARED_CONSTEXPR_P (decl) = 0;
7587       if (VAR_P (decl) && DECL_CLASS_SCOPE_P (decl))
7588 	{
7589 	  init = NULL_TREE;
7590 	  DECL_EXTERNAL (decl) = 1;
7591 	}
7592     }
7593 
7594   if (VAR_P (decl)
7595       && DECL_CLASS_SCOPE_P (decl)
7596       && verify_type_context (DECL_SOURCE_LOCATION (decl),
7597 			      TCTX_STATIC_STORAGE, type)
7598       && DECL_INITIALIZED_IN_CLASS_P (decl))
7599     check_static_variable_definition (decl, type);
7600 
7601   if (init && TREE_CODE (decl) == FUNCTION_DECL)
7602     {
7603       tree clone;
7604       if (init == ridpointers[(int)RID_DELETE])
7605 	{
7606 	  /* FIXME check this is 1st decl.  */
7607 	  DECL_DELETED_FN (decl) = 1;
7608 	  DECL_DECLARED_INLINE_P (decl) = 1;
7609 	  DECL_INITIAL (decl) = error_mark_node;
7610 	  FOR_EACH_CLONE (clone, decl)
7611 	    {
7612 	      DECL_DELETED_FN (clone) = 1;
7613 	      DECL_DECLARED_INLINE_P (clone) = 1;
7614 	      DECL_INITIAL (clone) = error_mark_node;
7615 	    }
7616 	  init = NULL_TREE;
7617 	}
7618       else if (init == ridpointers[(int)RID_DEFAULT])
7619 	{
7620 	  if (defaultable_fn_check (decl))
7621 	    DECL_DEFAULTED_FN (decl) = 1;
7622 	  else
7623 	    DECL_INITIAL (decl) = NULL_TREE;
7624 	}
7625     }
7626 
7627   if (init && VAR_P (decl))
7628     {
7629       DECL_NONTRIVIALLY_INITIALIZED_P (decl) = 1;
7630       /* If DECL is a reference, then we want to know whether init is a
7631 	 reference constant; init_const_expr_p as passed tells us whether
7632 	 it's an rvalue constant.  */
7633       if (TYPE_REF_P (type))
7634 	init_const_expr_p = potential_constant_expression (init);
7635       if (init_const_expr_p)
7636 	{
7637 	  /* Set these flags now for templates.  We'll update the flags in
7638 	     store_init_value for instantiations.  */
7639 	  DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
7640 	  if (decl_maybe_constant_var_p (decl)
7641 	      /* FIXME setting TREE_CONSTANT on refs breaks the back end.  */
7642 	      && !TYPE_REF_P (type))
7643 	    TREE_CONSTANT (decl) = 1;
7644 	}
7645     }
7646 
7647   if (flag_openmp
7648       && TREE_CODE (decl) == FUNCTION_DECL
7649       /* #pragma omp declare variant on methods handled in finish_struct
7650 	 instead.  */
7651       && (!DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
7652 	  || COMPLETE_TYPE_P (DECL_CONTEXT (decl))))
7653     if (tree attr = lookup_attribute ("omp declare variant base",
7654 				      DECL_ATTRIBUTES (decl)))
7655       omp_declare_variant_finalize (decl, attr);
7656 
7657   if (processing_template_decl)
7658     {
7659       bool type_dependent_p;
7660 
7661       /* Add this declaration to the statement-tree.  */
7662       if (at_function_scope_p ())
7663 	add_decl_expr (decl);
7664 
7665       type_dependent_p = dependent_type_p (type);
7666 
7667       if (check_for_bare_parameter_packs (init))
7668 	{
7669 	  init = NULL_TREE;
7670 	  DECL_INITIAL (decl) = NULL_TREE;
7671 	}
7672 
7673       /* Handle `constinit' on variable templates.  */
7674       if (flags & LOOKUP_CONSTINIT)
7675 	TINFO_VAR_DECLARED_CONSTINIT (DECL_TEMPLATE_INFO (decl)) = true;
7676 
7677       /* Generally, initializers in templates are expanded when the
7678 	 template is instantiated.  But, if DECL is a variable constant
7679 	 then it can be used in future constant expressions, so its value
7680 	 must be available. */
7681 
7682       bool dep_init = false;
7683 
7684       if (!VAR_P (decl) || type_dependent_p)
7685 	/* We can't do anything if the decl has dependent type.  */;
7686       else if (!init && is_concept_var (decl))
7687 	{
7688 	  error ("variable concept has no initializer");
7689 	  init = boolean_true_node;
7690 	}
7691       else if (init
7692 	       && init_const_expr_p
7693 	       && !TYPE_REF_P (type)
7694 	       && decl_maybe_constant_var_p (decl)
7695 	       && !(dep_init = value_dependent_init_p (init)))
7696 	{
7697 	  /* This variable seems to be a non-dependent constant, so process
7698 	     its initializer.  If check_initializer returns non-null the
7699 	     initialization wasn't constant after all.  */
7700 	  tree init_code;
7701 	  cleanups = make_tree_vector ();
7702 	  init_code = check_initializer (decl, init, flags, &cleanups);
7703 	  if (init_code == NULL_TREE)
7704 	    init = NULL_TREE;
7705 	  release_tree_vector (cleanups);
7706 	}
7707       else
7708 	{
7709 	  gcc_assert (!DECL_PRETTY_FUNCTION_P (decl));
7710 	  /* Deduce array size even if the initializer is dependent.  */
7711 	  maybe_deduce_size_from_array_init (decl, init);
7712 	  /* And complain about multiple initializers.  */
7713 	  if (init && TREE_CODE (init) == TREE_LIST && TREE_CHAIN (init)
7714 	      && !MAYBE_CLASS_TYPE_P (type))
7715 	    init = build_x_compound_expr_from_list (init, ELK_INIT,
7716 						    tf_warning_or_error);
7717 	}
7718 
7719       if (init)
7720 	DECL_INITIAL (decl) = init;
7721 
7722       if (dep_init)
7723 	{
7724 	  retrofit_lang_decl (decl);
7725 	  SET_DECL_DEPENDENT_INIT_P (decl, true);
7726 	}
7727 
7728       if (VAR_P (decl) && DECL_REGISTER (decl) && asmspec)
7729 	{
7730 	  set_user_assembler_name (decl, asmspec);
7731 	  DECL_HARD_REGISTER (decl) = 1;
7732 	}
7733       return;
7734     }
7735 
7736   /* Just store non-static data member initializers for later.  */
7737   if (init && TREE_CODE (decl) == FIELD_DECL)
7738     DECL_INITIAL (decl) = init;
7739 
7740   /* Take care of TYPE_DECLs up front.  */
7741   if (TREE_CODE (decl) == TYPE_DECL)
7742     {
7743       if (type != error_mark_node
7744 	  && MAYBE_CLASS_TYPE_P (type) && DECL_NAME (decl))
7745 	{
7746 	  if (TREE_TYPE (DECL_NAME (decl)) && TREE_TYPE (decl) != type)
7747 	    warning (0, "shadowing previous type declaration of %q#D", decl);
7748 	  set_identifier_type_value (DECL_NAME (decl), decl);
7749 	}
7750 
7751       /* If we have installed this as the canonical typedef for this
7752 	 type, and that type has not been defined yet, delay emitting
7753 	 the debug information for it, as we will emit it later.  */
7754       if (TYPE_MAIN_DECL (TREE_TYPE (decl)) == decl
7755 	  && !COMPLETE_TYPE_P (TREE_TYPE (decl)))
7756 	TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
7757 
7758       rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl),
7759 				at_eof);
7760       return;
7761     }
7762 
7763   /* A reference will be modified here, as it is initialized.  */
7764   if (! DECL_EXTERNAL (decl)
7765       && TREE_READONLY (decl)
7766       && TYPE_REF_P (type))
7767     {
7768       was_readonly = 1;
7769       TREE_READONLY (decl) = 0;
7770     }
7771 
7772   /* This needs to happen before extend_ref_init_temps.  */
7773   if (VAR_OR_FUNCTION_DECL_P (decl))
7774     {
7775       if (VAR_P (decl))
7776 	maybe_commonize_var (decl);
7777       determine_visibility (decl);
7778     }
7779 
7780   if (VAR_P (decl))
7781     {
7782       duration_kind dk = decl_storage_duration (decl);
7783       /* [dcl.constinit]/1 "The constinit specifier shall be applied
7784 	 only to a declaration of a variable with static or thread storage
7785 	 duration."  */
7786       if ((flags & LOOKUP_CONSTINIT)
7787 	  && !(dk == dk_thread || dk == dk_static))
7788 	{
7789 	  error_at (DECL_SOURCE_LOCATION (decl),
7790 		    "%<constinit%> can only be applied to a variable with "
7791 		    "static or thread storage duration");
7792 	  return;
7793 	}
7794 
7795       /* If this is a local variable that will need a mangled name,
7796 	 register it now.  We must do this before processing the
7797 	 initializer for the variable, since the initialization might
7798 	 require a guard variable, and since the mangled name of the
7799 	 guard variable will depend on the mangled name of this
7800 	 variable.  */
7801       if (DECL_FUNCTION_SCOPE_P (decl)
7802 	  && TREE_STATIC (decl)
7803 	  && !DECL_ARTIFICIAL (decl))
7804 	{
7805 	  /* The variable holding an anonymous union will have had its
7806 	     discriminator set in finish_anon_union, after which it's
7807 	     NAME will have been cleared.  */
7808 	  if (DECL_NAME (decl))
7809 	    determine_local_discriminator (decl);
7810 	  /* Normally has_forced_label_in_static is set during GIMPLE
7811 	     lowering, but [cd]tors are never actually compiled directly.
7812 	     We need to set this early so we can deal with the label
7813 	     address extension.  */
7814 	  if ((DECL_CONSTRUCTOR_P (current_function_decl)
7815 	       || DECL_DESTRUCTOR_P (current_function_decl))
7816 	      && init)
7817 	    {
7818 	      walk_tree (&init, notice_forced_label_r, NULL, NULL);
7819 	      add_local_decl (cfun, decl);
7820 	    }
7821 	  /* And make sure it's in the symbol table for
7822 	     c_parse_final_cleanups to find.  */
7823 	  varpool_node::get_create (decl);
7824 	}
7825 
7826       /* Convert the initializer to the type of DECL, if we have not
7827 	 already initialized DECL.  */
7828       if (!DECL_INITIALIZED_P (decl)
7829 	  /* If !DECL_EXTERNAL then DECL is being defined.  In the
7830 	     case of a static data member initialized inside the
7831 	     class-specifier, there can be an initializer even if DECL
7832 	     is *not* defined.  */
7833 	  && (!DECL_EXTERNAL (decl) || init))
7834 	{
7835 	  cleanups = make_tree_vector ();
7836 	  init = check_initializer (decl, init, flags, &cleanups);
7837 
7838 	  /* Handle:
7839 
7840 	     [dcl.init]
7841 
7842 	     The memory occupied by any object of static storage
7843 	     duration is zero-initialized at program startup before
7844 	     any other initialization takes place.
7845 
7846 	     We cannot create an appropriate initializer until after
7847 	     the type of DECL is finalized.  If DECL_INITIAL is set,
7848 	     then the DECL is statically initialized, and any
7849 	     necessary zero-initialization has already been performed.  */
7850 	  if (TREE_STATIC (decl) && !DECL_INITIAL (decl))
7851 	    DECL_INITIAL (decl) = build_zero_init (TREE_TYPE (decl),
7852 						   /*nelts=*/NULL_TREE,
7853 						   /*static_storage_p=*/true);
7854 	  /* Remember that the initialization for this variable has
7855 	     taken place.  */
7856 	  DECL_INITIALIZED_P (decl) = 1;
7857 	  /* This declaration is the definition of this variable,
7858 	     unless we are initializing a static data member within
7859 	     the class specifier.  */
7860 	  if (!DECL_EXTERNAL (decl))
7861 	    var_definition_p = true;
7862 	}
7863       /* If the variable has an array type, lay out the type, even if
7864 	 there is no initializer.  It is valid to index through the
7865 	 array, and we must get TYPE_ALIGN set correctly on the array
7866 	 type.  */
7867       else if (TREE_CODE (type) == ARRAY_TYPE)
7868 	layout_type (type);
7869 
7870       if (TREE_STATIC (decl)
7871 	  && !at_function_scope_p ()
7872 	  && current_function_decl == NULL)
7873 	/* So decl is a global variable or a static member of a
7874 	   non local class. Record the types it uses
7875 	   so that we can decide later to emit debug info for them.  */
7876 	record_types_used_by_current_var_decl (decl);
7877     }
7878 
7879   /* Add this declaration to the statement-tree.  This needs to happen
7880      after the call to check_initializer so that the DECL_EXPR for a
7881      reference temp is added before the DECL_EXPR for the reference itself.  */
7882   if (DECL_FUNCTION_SCOPE_P (decl))
7883     {
7884       /* If we're building a variable sized type, and we might be
7885 	 reachable other than via the top of the current binding
7886 	 level, then create a new BIND_EXPR so that we deallocate
7887 	 the object at the right time.  */
7888       if (VAR_P (decl)
7889 	  && DECL_SIZE (decl)
7890 	  && !TREE_CONSTANT (DECL_SIZE (decl))
7891 	  && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
7892 	{
7893 	  tree bind;
7894 	  bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
7895 	  TREE_SIDE_EFFECTS (bind) = 1;
7896 	  add_stmt (bind);
7897 	  BIND_EXPR_BODY (bind) = push_stmt_list ();
7898 	}
7899       add_decl_expr (decl);
7900     }
7901 
7902   /* Let the middle end know about variables and functions -- but not
7903      static data members in uninstantiated class templates.  */
7904   if (VAR_OR_FUNCTION_DECL_P (decl))
7905     {
7906       if (VAR_P (decl))
7907 	{
7908 	  layout_var_decl (decl);
7909 	  if (!flag_weak)
7910 	    /* Check again now that we have an initializer.  */
7911 	    maybe_commonize_var (decl);
7912 	  /* A class-scope constexpr variable with an out-of-class declaration.
7913 	     C++17 makes them implicitly inline, but still force it out.  */
7914 	  if (DECL_INLINE_VAR_P (decl)
7915 	      && !DECL_VAR_DECLARED_INLINE_P (decl)
7916 	      && !DECL_TEMPLATE_INSTANTIATION (decl)
7917 	      && !in_class_decl)
7918 	    mark_needed (decl);
7919 	}
7920 
7921       if (var_definition_p
7922 	  /* With -fmerge-all-constants, gimplify_init_constructor
7923 	     might add TREE_STATIC to aggregate variables.  */
7924 	  && (TREE_STATIC (decl)
7925 	      || (flag_merge_constants >= 2
7926 		  && AGGREGATE_TYPE_P (type))))
7927 	{
7928 	  /* If a TREE_READONLY variable needs initialization
7929 	     at runtime, it is no longer readonly and we need to
7930 	     avoid MEM_READONLY_P being set on RTL created for it.  */
7931 	  if (init)
7932 	    {
7933 	      if (TREE_READONLY (decl))
7934 		TREE_READONLY (decl) = 0;
7935 	      was_readonly = 0;
7936 	    }
7937 	  else if (was_readonly)
7938 	    TREE_READONLY (decl) = 1;
7939 
7940 	  /* Likewise if it needs destruction.  */
7941 	  if (!decl_maybe_constant_destruction (decl, type))
7942 	    TREE_READONLY (decl) = 0;
7943 	}
7944       else if (VAR_P (decl)
7945 	       && CP_DECL_THREAD_LOCAL_P (decl)
7946 	       && (!DECL_EXTERNAL (decl) || flag_extern_tls_init)
7947 	       && (was_readonly || TREE_READONLY (decl))
7948 	       && var_needs_tls_wrapper (decl))
7949 	{
7950 	  /* TLS variables need dynamic initialization by the TLS wrapper
7951 	     function, we don't want to hoist accesses to it before the
7952 	     wrapper.  */
7953 	  was_readonly = 0;
7954 	  TREE_READONLY (decl) = 0;
7955 	}
7956 
7957       make_rtl_for_nonlocal_decl (decl, init, asmspec);
7958 
7959       /* Check for abstractness of the type. Notice that there is no
7960 	 need to strip array types here since the check for those types
7961 	 is already done within create_array_type_for_decl.  */
7962       abstract_virtuals_error (decl, type);
7963 
7964       if (TREE_TYPE (decl) == error_mark_node)
7965 	/* No initialization required.  */
7966 	;
7967       else if (TREE_CODE (decl) == FUNCTION_DECL)
7968 	{
7969 	  if (init)
7970 	    {
7971 	      if (init == ridpointers[(int)RID_DEFAULT])
7972 		{
7973 		  /* An out-of-class default definition is defined at
7974 		     the point where it is explicitly defaulted.  */
7975 		  if (DECL_DELETED_FN (decl))
7976 		    maybe_explain_implicit_delete (decl);
7977 		  else if (DECL_INITIAL (decl) == error_mark_node)
7978 		    synthesize_method (decl);
7979 		}
7980 	      else
7981 		error_at (cp_expr_loc_or_loc (init,
7982 					      DECL_SOURCE_LOCATION (decl)),
7983 			  "function %q#D is initialized like a variable",
7984 			  decl);
7985 	    }
7986 	  /* else no initialization required.  */
7987 	}
7988       else if (DECL_EXTERNAL (decl)
7989 	       && ! (DECL_LANG_SPECIFIC (decl)
7990 		     && DECL_NOT_REALLY_EXTERN (decl)))
7991 	{
7992 	  /* check_initializer will have done any constant initialization.  */
7993 	}
7994       /* A variable definition.  */
7995       else if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
7996 	/* Initialize the local variable.  */
7997 	initialize_local_var (decl, init);
7998 
7999       /* If a variable is defined, and then a subsequent
8000 	 definition with external linkage is encountered, we will
8001 	 get here twice for the same variable.  We want to avoid
8002 	 calling expand_static_init more than once.  For variables
8003 	 that are not static data members, we can call
8004 	 expand_static_init only when we actually process the
8005 	 initializer.  It is not legal to redeclare a static data
8006 	 member, so this issue does not arise in that case.  */
8007       else if (var_definition_p && TREE_STATIC (decl))
8008 	expand_static_init (decl, init);
8009     }
8010 
8011   /* If a CLEANUP_STMT was created to destroy a temporary bound to a
8012      reference, insert it in the statement-tree now.  */
8013   if (cleanups)
8014     {
8015       unsigned i; tree t;
8016       FOR_EACH_VEC_ELT (*cleanups, i, t)
8017 	push_cleanup (NULL_TREE, t, false);
8018       release_tree_vector (cleanups);
8019     }
8020 
8021   if (was_readonly)
8022     TREE_READONLY (decl) = 1;
8023 
8024   if (flag_openmp
8025       && VAR_P (decl)
8026       && lookup_attribute ("omp declare target implicit",
8027 			   DECL_ATTRIBUTES (decl)))
8028     {
8029       DECL_ATTRIBUTES (decl)
8030 	= remove_attribute ("omp declare target implicit",
8031 			    DECL_ATTRIBUTES (decl));
8032       complete_type (TREE_TYPE (decl));
8033       if (!cp_omp_mappable_type (TREE_TYPE (decl)))
8034 	{
8035 	  error ("%q+D in declare target directive does not have mappable"
8036 		 " type", decl);
8037 	  cp_omp_emit_unmappable_type_notes (TREE_TYPE (decl));
8038 	}
8039       else if (!lookup_attribute ("omp declare target",
8040 				  DECL_ATTRIBUTES (decl))
8041 	       && !lookup_attribute ("omp declare target link",
8042 				     DECL_ATTRIBUTES (decl)))
8043 	DECL_ATTRIBUTES (decl)
8044 	  = tree_cons (get_identifier ("omp declare target"),
8045 		       NULL_TREE, DECL_ATTRIBUTES (decl));
8046     }
8047 
8048   invoke_plugin_callbacks (PLUGIN_FINISH_DECL, decl);
8049 }
8050 
8051 /* For class TYPE return itself or some its bases that contain
8052    any direct non-static data members.  Return error_mark_node if an
8053    error has been diagnosed.  */
8054 
8055 static tree
find_decomp_class_base(location_t loc,tree type,tree ret)8056 find_decomp_class_base (location_t loc, tree type, tree ret)
8057 {
8058   bool member_seen = false;
8059   for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
8060     if (TREE_CODE (field) != FIELD_DECL
8061 	|| DECL_ARTIFICIAL (field)
8062 	|| DECL_UNNAMED_BIT_FIELD (field))
8063       continue;
8064     else if (ret)
8065       return type;
8066     else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
8067       {
8068 	if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE)
8069 	  error_at (loc, "cannot decompose class type %qT because it has an "
8070 			 "anonymous struct member", type);
8071 	else
8072 	  error_at (loc, "cannot decompose class type %qT because it has an "
8073 			 "anonymous union member", type);
8074 	inform (DECL_SOURCE_LOCATION (field), "declared here");
8075 	return error_mark_node;
8076       }
8077     else if (!accessible_p (type, field, true))
8078       {
8079 	error_at (loc, "cannot decompose inaccessible member %qD of %qT",
8080 		  field, type);
8081 	inform (DECL_SOURCE_LOCATION (field),
8082 		TREE_PRIVATE (field)
8083 		? G_("declared private here")
8084 		: G_("declared protected here"));
8085 	return error_mark_node;
8086       }
8087     else
8088       member_seen = true;
8089 
8090   tree base_binfo, binfo;
8091   tree orig_ret = ret;
8092   int i;
8093   if (member_seen)
8094     ret = type;
8095   for (binfo = TYPE_BINFO (type), i = 0;
8096        BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
8097     {
8098       tree t = find_decomp_class_base (loc, TREE_TYPE (base_binfo), ret);
8099       if (t == error_mark_node)
8100 	return error_mark_node;
8101       if (t != NULL_TREE && t != ret)
8102 	{
8103 	  if (ret == type)
8104 	    {
8105 	      error_at (loc, "cannot decompose class type %qT: both it and "
8106 			     "its base class %qT have non-static data members",
8107 			type, t);
8108 	      return error_mark_node;
8109 	    }
8110 	  else if (orig_ret != NULL_TREE)
8111 	    return t;
8112 	  else if (ret != NULL_TREE)
8113 	    {
8114 	      error_at (loc, "cannot decompose class type %qT: its base "
8115 			     "classes %qT and %qT have non-static data "
8116 			     "members", type, ret, t);
8117 	      return error_mark_node;
8118 	    }
8119 	  else
8120 	    ret = t;
8121 	}
8122     }
8123   return ret;
8124 }
8125 
8126 /* Return std::tuple_size<TYPE>::value.  */
8127 
8128 static tree
get_tuple_size(tree type)8129 get_tuple_size (tree type)
8130 {
8131   tree args = make_tree_vec (1);
8132   TREE_VEC_ELT (args, 0) = type;
8133   tree inst = lookup_template_class (tuple_size_identifier, args,
8134 				     /*in_decl*/NULL_TREE,
8135 				     /*context*/std_node,
8136 				     /*entering_scope*/false, tf_none);
8137   inst = complete_type (inst);
8138   if (inst == error_mark_node || !COMPLETE_TYPE_P (inst))
8139     return NULL_TREE;
8140   tree val = lookup_qualified_name (inst, value_identifier,
8141 				    /*type*/false, /*complain*/false);
8142   if (TREE_CODE (val) == VAR_DECL || TREE_CODE (val) == CONST_DECL)
8143     val = maybe_constant_value (val);
8144   if (TREE_CODE (val) == INTEGER_CST)
8145     return val;
8146   else
8147     return error_mark_node;
8148 }
8149 
8150 /* Return std::tuple_element<I,TYPE>::type.  */
8151 
8152 static tree
get_tuple_element_type(tree type,unsigned i)8153 get_tuple_element_type (tree type, unsigned i)
8154 {
8155   tree args = make_tree_vec (2);
8156   TREE_VEC_ELT (args, 0) = build_int_cst (integer_type_node, i);
8157   TREE_VEC_ELT (args, 1) = type;
8158   tree inst = lookup_template_class (tuple_element_identifier, args,
8159 				     /*in_decl*/NULL_TREE,
8160 				     /*context*/std_node,
8161 				     /*entering_scope*/false,
8162 				     tf_warning_or_error);
8163   return make_typename_type (inst, type_identifier,
8164 			     none_type, tf_warning_or_error);
8165 }
8166 
8167 /* Return e.get<i>() or get<i>(e).  */
8168 
8169 static tree
get_tuple_decomp_init(tree decl,unsigned i)8170 get_tuple_decomp_init (tree decl, unsigned i)
8171 {
8172   tree targs = make_tree_vec (1);
8173   TREE_VEC_ELT (targs, 0) = build_int_cst (integer_type_node, i);
8174 
8175   tree etype = TREE_TYPE (decl);
8176   tree e = convert_from_reference (decl);
8177 
8178   /* [The id-expression] e is an lvalue if the type of the entity e is an
8179      lvalue reference and an xvalue otherwise.  */
8180   if (!TYPE_REF_P (etype)
8181       || TYPE_REF_IS_RVALUE (etype))
8182     e = move (e);
8183 
8184   tree fns = lookup_qualified_name (TREE_TYPE (e), get__identifier,
8185 				    /*type*/false, /*complain*/false);
8186   bool use_member_get = false;
8187 
8188   /* To use a member get, member lookup must find at least one
8189      declaration that is a function template
8190      whose first template parameter is a non-type parameter.  */
8191   for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (fns)); iter; ++iter)
8192     {
8193       tree fn = *iter;
8194       if (TREE_CODE (fn) == TEMPLATE_DECL)
8195 	{
8196 	  tree tparms = DECL_TEMPLATE_PARMS (fn);
8197 	  tree parm = TREE_VEC_ELT (INNERMOST_TEMPLATE_PARMS (tparms), 0);
8198 	  if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL)
8199 	    {
8200 	      use_member_get = true;
8201 	      break;
8202 	    }
8203 	}
8204     }
8205 
8206   if (use_member_get)
8207     {
8208       fns = lookup_template_function (fns, targs);
8209       return build_new_method_call (e, fns, /*args*/NULL,
8210 				    /*path*/NULL_TREE, LOOKUP_NORMAL,
8211 				    /*fn_p*/NULL, tf_warning_or_error);
8212     }
8213   else
8214     {
8215       releasing_vec args (make_tree_vector_single (e));
8216       fns = lookup_template_function (get__identifier, targs);
8217       fns = perform_koenig_lookup (fns, args, tf_warning_or_error);
8218       return finish_call_expr (fns, &args, /*novirt*/false,
8219 			       /*koenig*/true, tf_warning_or_error);
8220     }
8221 }
8222 
8223 /* It's impossible to recover the decltype of a tuple decomposition variable
8224    based on the actual type of the variable, so store it in a hash table.  */
8225 
8226 static GTY((cache)) decl_tree_cache_map *decomp_type_table;
8227 
8228 tree
lookup_decomp_type(tree v)8229 lookup_decomp_type (tree v)
8230 {
8231   return *decomp_type_table->get (v);
8232 }
8233 
8234 /* Mangle a decomposition declaration if needed.  Arguments like
8235    in cp_finish_decomp.  */
8236 
8237 void
cp_maybe_mangle_decomp(tree decl,tree first,unsigned int count)8238 cp_maybe_mangle_decomp (tree decl, tree first, unsigned int count)
8239 {
8240   if (!processing_template_decl
8241       && !error_operand_p (decl)
8242       && TREE_STATIC (decl))
8243     {
8244       auto_vec<tree, 16> v;
8245       v.safe_grow (count);
8246       tree d = first;
8247       for (unsigned int i = 0; i < count; i++, d = DECL_CHAIN (d))
8248 	v[count - i - 1] = d;
8249       SET_DECL_ASSEMBLER_NAME (decl, mangle_decomp (decl, v));
8250       maybe_apply_pragma_weak (decl);
8251     }
8252 }
8253 
8254 /* Finish a decomposition declaration.  DECL is the underlying declaration
8255    "e", FIRST is the head of a chain of decls for the individual identifiers
8256    chained through DECL_CHAIN in reverse order and COUNT is the number of
8257    those decls.  */
8258 
8259 void
cp_finish_decomp(tree decl,tree first,unsigned int count)8260 cp_finish_decomp (tree decl, tree first, unsigned int count)
8261 {
8262   if (error_operand_p (decl))
8263     {
8264      error_out:
8265       while (count--)
8266 	{
8267 	  TREE_TYPE (first) = error_mark_node;
8268 	  if (DECL_HAS_VALUE_EXPR_P (first))
8269 	    {
8270 	      SET_DECL_VALUE_EXPR (first, NULL_TREE);
8271 	      DECL_HAS_VALUE_EXPR_P (first) = 0;
8272 	    }
8273 	  first = DECL_CHAIN (first);
8274 	}
8275       if (DECL_P (decl) && DECL_NAMESPACE_SCOPE_P (decl))
8276 	SET_DECL_ASSEMBLER_NAME (decl, get_identifier ("<decomp>"));
8277       return;
8278     }
8279 
8280   location_t loc = DECL_SOURCE_LOCATION (decl);
8281   if (type_dependent_expression_p (decl)
8282       /* This happens for range for when not in templates.
8283 	 Still add the DECL_VALUE_EXPRs for later processing.  */
8284       || (!processing_template_decl
8285 	  && type_uses_auto (TREE_TYPE (decl))))
8286     {
8287       for (unsigned int i = 0; i < count; i++)
8288 	{
8289 	  if (!DECL_HAS_VALUE_EXPR_P (first))
8290 	    {
8291 	      tree v = build_nt (ARRAY_REF, decl,
8292 				 size_int (count - i - 1),
8293 				 NULL_TREE, NULL_TREE);
8294 	      SET_DECL_VALUE_EXPR (first, v);
8295 	      DECL_HAS_VALUE_EXPR_P (first) = 1;
8296 	    }
8297 	  if (processing_template_decl)
8298 	    fit_decomposition_lang_decl (first, decl);
8299 	  first = DECL_CHAIN (first);
8300 	}
8301       return;
8302     }
8303 
8304   auto_vec<tree, 16> v;
8305   v.safe_grow (count);
8306   tree d = first;
8307   for (unsigned int i = 0; i < count; i++, d = DECL_CHAIN (d))
8308     {
8309       v[count - i - 1] = d;
8310       fit_decomposition_lang_decl (d, decl);
8311     }
8312 
8313   tree type = TREE_TYPE (decl);
8314   tree dexp = decl;
8315 
8316   if (TYPE_REF_P (type))
8317     {
8318       dexp = convert_from_reference (dexp);
8319       type = complete_type (TREE_TYPE (type));
8320       if (type == error_mark_node)
8321 	goto error_out;
8322       if (!COMPLETE_TYPE_P (type))
8323 	{
8324 	  error_at (loc, "structured binding refers to incomplete type %qT",
8325 		    type);
8326 	  goto error_out;
8327 	}
8328     }
8329 
8330   tree eltype = NULL_TREE;
8331   unsigned HOST_WIDE_INT eltscnt = 0;
8332   if (TREE_CODE (type) == ARRAY_TYPE)
8333     {
8334       tree nelts;
8335       nelts = array_type_nelts_top (type);
8336       if (nelts == error_mark_node)
8337 	goto error_out;
8338       if (!tree_fits_uhwi_p (nelts))
8339 	{
8340 	  error_at (loc, "cannot decompose variable length array %qT", type);
8341 	  goto error_out;
8342 	}
8343       eltscnt = tree_to_uhwi (nelts);
8344       if (count != eltscnt)
8345 	{
8346        cnt_mismatch:
8347 	  if (count > eltscnt)
8348 	    error_n (loc, count,
8349 		     "%u name provided for structured binding",
8350 		     "%u names provided for structured binding", count);
8351 	  else
8352 	    error_n (loc, count,
8353 		     "only %u name provided for structured binding",
8354 		     "only %u names provided for structured binding", count);
8355 	  inform_n (loc, eltscnt,
8356 		    "while %qT decomposes into %wu element",
8357 		    "while %qT decomposes into %wu elements",
8358 		    type, eltscnt);
8359 	  goto error_out;
8360 	}
8361       eltype = TREE_TYPE (type);
8362       for (unsigned int i = 0; i < count; i++)
8363 	{
8364 	  TREE_TYPE (v[i]) = eltype;
8365 	  layout_decl (v[i], 0);
8366 	  if (processing_template_decl)
8367 	    continue;
8368 	  tree t = unshare_expr (dexp);
8369 	  t = build4_loc (DECL_SOURCE_LOCATION (v[i]), ARRAY_REF,
8370 			  eltype, t, size_int (i), NULL_TREE,
8371 			  NULL_TREE);
8372 	  SET_DECL_VALUE_EXPR (v[i], t);
8373 	  DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
8374 	}
8375     }
8376   /* 2 GNU extensions.  */
8377   else if (TREE_CODE (type) == COMPLEX_TYPE)
8378     {
8379       eltscnt = 2;
8380       if (count != eltscnt)
8381 	goto cnt_mismatch;
8382       eltype = cp_build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
8383       for (unsigned int i = 0; i < count; i++)
8384 	{
8385 	  TREE_TYPE (v[i]) = eltype;
8386 	  layout_decl (v[i], 0);
8387 	  if (processing_template_decl)
8388 	    continue;
8389 	  tree t = unshare_expr (dexp);
8390 	  t = build1_loc (DECL_SOURCE_LOCATION (v[i]),
8391 			  i ? IMAGPART_EXPR : REALPART_EXPR, eltype,
8392 			  t);
8393 	  SET_DECL_VALUE_EXPR (v[i], t);
8394 	  DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
8395 	}
8396     }
8397   else if (TREE_CODE (type) == VECTOR_TYPE)
8398     {
8399       if (!TYPE_VECTOR_SUBPARTS (type).is_constant (&eltscnt))
8400 	{
8401 	  error_at (loc, "cannot decompose variable length vector %qT", type);
8402 	  goto error_out;
8403 	}
8404       if (count != eltscnt)
8405 	goto cnt_mismatch;
8406       eltype = cp_build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
8407       for (unsigned int i = 0; i < count; i++)
8408 	{
8409 	  TREE_TYPE (v[i]) = eltype;
8410 	  layout_decl (v[i], 0);
8411 	  if (processing_template_decl)
8412 	    continue;
8413 	  tree t = unshare_expr (dexp);
8414 	  convert_vector_to_array_for_subscript (DECL_SOURCE_LOCATION (v[i]),
8415 						 &t, size_int (i));
8416 	  t = build4_loc (DECL_SOURCE_LOCATION (v[i]), ARRAY_REF,
8417 			  eltype, t, size_int (i), NULL_TREE,
8418 			  NULL_TREE);
8419 	  SET_DECL_VALUE_EXPR (v[i], t);
8420 	  DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
8421 	}
8422     }
8423   else if (tree tsize = get_tuple_size (type))
8424     {
8425       if (tsize == error_mark_node)
8426 	{
8427 	  error_at (loc, "%<std::tuple_size<%T>::value%> is not an integral "
8428 			 "constant expression", type);
8429 	  goto error_out;
8430 	}
8431       if (!tree_fits_uhwi_p (tsize))
8432 	{
8433 	  error_n (loc, count,
8434 		   "%u name provided for structured binding",
8435 		   "%u names provided for structured binding", count);
8436 	  inform (loc, "while %qT decomposes into %E elements",
8437 		  type, tsize);
8438 	  goto error_out;
8439 	}
8440       eltscnt = tree_to_uhwi (tsize);
8441       if (count != eltscnt)
8442 	goto cnt_mismatch;
8443       int save_read = DECL_READ_P (decl);
8444       for (unsigned i = 0; i < count; ++i)
8445 	{
8446 	  location_t sloc = input_location;
8447 	  location_t dloc = DECL_SOURCE_LOCATION (v[i]);
8448 
8449 	  input_location = dloc;
8450 	  tree init = get_tuple_decomp_init (decl, i);
8451 	  tree eltype = (init == error_mark_node ? error_mark_node
8452 			 : get_tuple_element_type (type, i));
8453 	  input_location = sloc;
8454 
8455 	  if (VOID_TYPE_P (eltype))
8456 	    {
8457 	      error ("%<std::tuple_element<%u, %T>::type%> is %<void%>",
8458 		     i, type);
8459 	      eltype = error_mark_node;
8460 	    }
8461 	  if (init == error_mark_node || eltype == error_mark_node)
8462 	    {
8463 	      inform (dloc, "in initialization of structured binding "
8464 		      "variable %qD", v[i]);
8465 	      goto error_out;
8466 	    }
8467 	  /* Save the decltype away before reference collapse.  */
8468 	  hash_map_safe_put<hm_ggc> (decomp_type_table, v[i], eltype);
8469 	  eltype = cp_build_reference_type (eltype, !lvalue_p (init));
8470 	  TREE_TYPE (v[i]) = eltype;
8471 	  layout_decl (v[i], 0);
8472 	  if (DECL_HAS_VALUE_EXPR_P (v[i]))
8473 	    {
8474 	      /* In this case the names are variables, not just proxies.  */
8475 	      SET_DECL_VALUE_EXPR (v[i], NULL_TREE);
8476 	      DECL_HAS_VALUE_EXPR_P (v[i]) = 0;
8477 	    }
8478 	  if (!processing_template_decl)
8479 	    {
8480 	      copy_linkage (v[i], decl);
8481 	      cp_finish_decl (v[i], init, /*constexpr*/false,
8482 			      /*asm*/NULL_TREE, LOOKUP_NORMAL);
8483 	    }
8484 	}
8485       /* Ignore reads from the underlying decl performed during initialization
8486 	 of the individual variables.  If those will be read, we'll mark
8487 	 the underlying decl as read at that point.  */
8488       DECL_READ_P (decl) = save_read;
8489     }
8490   else if (TREE_CODE (type) == UNION_TYPE)
8491     {
8492       error_at (loc, "cannot decompose union type %qT", type);
8493       goto error_out;
8494     }
8495   else if (!CLASS_TYPE_P (type))
8496     {
8497       error_at (loc, "cannot decompose non-array non-class type %qT", type);
8498       goto error_out;
8499     }
8500   else if (LAMBDA_TYPE_P (type))
8501     {
8502       error_at (loc, "cannot decompose lambda closure type %qT", type);
8503       goto error_out;
8504     }
8505   else if (processing_template_decl && complete_type (type) == error_mark_node)
8506     goto error_out;
8507   else if (processing_template_decl && !COMPLETE_TYPE_P (type))
8508     pedwarn (loc, 0, "structured binding refers to incomplete class type %qT",
8509 	     type);
8510   else
8511     {
8512       tree btype = find_decomp_class_base (loc, type, NULL_TREE);
8513       if (btype == error_mark_node)
8514 	goto error_out;
8515       else if (btype == NULL_TREE)
8516 	{
8517 	  error_at (loc, "cannot decompose class type %qT without non-static "
8518 			 "data members", type);
8519 	  goto error_out;
8520 	}
8521       for (tree field = TYPE_FIELDS (btype); field; field = TREE_CHAIN (field))
8522 	if (TREE_CODE (field) != FIELD_DECL
8523 	    || DECL_ARTIFICIAL (field)
8524 	    || DECL_UNNAMED_BIT_FIELD (field))
8525 	  continue;
8526 	else
8527 	  eltscnt++;
8528       if (count != eltscnt)
8529 	goto cnt_mismatch;
8530       tree t = dexp;
8531       if (type != btype)
8532 	{
8533 	  t = convert_to_base (t, btype, /*check_access*/true,
8534 			       /*nonnull*/false, tf_warning_or_error);
8535 	  type = btype;
8536 	}
8537       unsigned int i = 0;
8538       for (tree field = TYPE_FIELDS (btype); field; field = TREE_CHAIN (field))
8539 	if (TREE_CODE (field) != FIELD_DECL
8540 	    || DECL_ARTIFICIAL (field)
8541 	    || DECL_UNNAMED_BIT_FIELD (field))
8542 	  continue;
8543 	else
8544 	  {
8545 	    tree tt = finish_non_static_data_member (field, unshare_expr (t),
8546 						     NULL_TREE);
8547 	    if (REFERENCE_REF_P (tt))
8548 	      tt = TREE_OPERAND (tt, 0);
8549 	    TREE_TYPE (v[i]) = TREE_TYPE (tt);
8550 	    layout_decl (v[i], 0);
8551 	    if (!processing_template_decl)
8552 	      {
8553 		SET_DECL_VALUE_EXPR (v[i], tt);
8554 		DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
8555 	      }
8556 	    i++;
8557 	  }
8558     }
8559   if (processing_template_decl)
8560     {
8561       for (unsigned int i = 0; i < count; i++)
8562 	if (!DECL_HAS_VALUE_EXPR_P (v[i]))
8563 	  {
8564 	    tree a = build_nt (ARRAY_REF, decl, size_int (i),
8565 			       NULL_TREE, NULL_TREE);
8566 	    SET_DECL_VALUE_EXPR (v[i], a);
8567 	    DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
8568 	  }
8569     }
8570 }
8571 
8572 /* Returns a declaration for a VAR_DECL as if:
8573 
8574      extern "C" TYPE NAME;
8575 
8576    had been seen.  Used to create compiler-generated global
8577    variables.  */
8578 
8579 static tree
declare_global_var(tree name,tree type)8580 declare_global_var (tree name, tree type)
8581 {
8582   tree decl;
8583 
8584   push_to_top_level ();
8585   decl = build_decl (input_location, VAR_DECL, name, type);
8586   TREE_PUBLIC (decl) = 1;
8587   DECL_EXTERNAL (decl) = 1;
8588   DECL_ARTIFICIAL (decl) = 1;
8589   DECL_CONTEXT (decl) = FROB_CONTEXT (global_namespace);
8590   /* If the user has explicitly declared this variable (perhaps
8591      because the code we are compiling is part of a low-level runtime
8592      library), then it is possible that our declaration will be merged
8593      with theirs by pushdecl.  */
8594   decl = pushdecl (decl);
8595   cp_finish_decl (decl, NULL_TREE, false, NULL_TREE, 0);
8596   pop_from_top_level ();
8597 
8598   return decl;
8599 }
8600 
8601 /* Returns the type for the argument to "__cxa_atexit" (or "atexit",
8602    if "__cxa_atexit" is not being used) corresponding to the function
8603    to be called when the program exits.  */
8604 
8605 static tree
get_atexit_fn_ptr_type(void)8606 get_atexit_fn_ptr_type (void)
8607 {
8608   tree fn_type;
8609 
8610   if (!atexit_fn_ptr_type_node)
8611     {
8612       tree arg_type;
8613       if (flag_use_cxa_atexit
8614 	  && !targetm.cxx.use_atexit_for_cxa_atexit ())
8615 	/* The parameter to "__cxa_atexit" is "void (*)(void *)".  */
8616 	arg_type = ptr_type_node;
8617       else
8618 	/* The parameter to "atexit" is "void (*)(void)".  */
8619 	arg_type = NULL_TREE;
8620 
8621       fn_type = build_function_type_list (void_type_node,
8622 					  arg_type, NULL_TREE);
8623       atexit_fn_ptr_type_node = build_pointer_type (fn_type);
8624     }
8625 
8626   return atexit_fn_ptr_type_node;
8627 }
8628 
8629 /* Returns a pointer to the `atexit' function.  Note that if
8630    FLAG_USE_CXA_ATEXIT is nonzero, then this will actually be the new
8631    `__cxa_atexit' function specified in the IA64 C++ ABI.  */
8632 
8633 static tree
get_atexit_node(void)8634 get_atexit_node (void)
8635 {
8636   tree atexit_fndecl;
8637   tree fn_type;
8638   tree fn_ptr_type;
8639   const char *name;
8640   bool use_aeabi_atexit;
8641 
8642   if (atexit_node)
8643     return atexit_node;
8644 
8645   if (flag_use_cxa_atexit && !targetm.cxx.use_atexit_for_cxa_atexit ())
8646     {
8647       /* The declaration for `__cxa_atexit' is:
8648 
8649 	   int __cxa_atexit (void (*)(void *), void *, void *)
8650 
8651 	 We build up the argument types and then the function type
8652 	 itself.  */
8653       tree argtype0, argtype1, argtype2;
8654 
8655       use_aeabi_atexit = targetm.cxx.use_aeabi_atexit ();
8656       /* First, build the pointer-to-function type for the first
8657 	 argument.  */
8658       fn_ptr_type = get_atexit_fn_ptr_type ();
8659       /* Then, build the rest of the argument types.  */
8660       argtype2 = ptr_type_node;
8661       if (use_aeabi_atexit)
8662 	{
8663 	  argtype1 = fn_ptr_type;
8664 	  argtype0 = ptr_type_node;
8665 	}
8666       else
8667 	{
8668 	  argtype1 = ptr_type_node;
8669 	  argtype0 = fn_ptr_type;
8670 	}
8671       /* And the final __cxa_atexit type.  */
8672       fn_type = build_function_type_list (integer_type_node,
8673 					  argtype0, argtype1, argtype2,
8674 					  NULL_TREE);
8675       if (use_aeabi_atexit)
8676 	name = "__aeabi_atexit";
8677       else
8678 	name = "__cxa_atexit";
8679     }
8680   else
8681     {
8682       /* The declaration for `atexit' is:
8683 
8684 	   int atexit (void (*)());
8685 
8686 	 We build up the argument types and then the function type
8687 	 itself.  */
8688       fn_ptr_type = get_atexit_fn_ptr_type ();
8689       /* Build the final atexit type.  */
8690       fn_type = build_function_type_list (integer_type_node,
8691 					  fn_ptr_type, NULL_TREE);
8692       name = "atexit";
8693     }
8694 
8695   /* Now, build the function declaration.  */
8696   push_lang_context (lang_name_c);
8697   atexit_fndecl = build_library_fn_ptr (name, fn_type, ECF_LEAF | ECF_NOTHROW);
8698   mark_used (atexit_fndecl);
8699   pop_lang_context ();
8700   atexit_node = decay_conversion (atexit_fndecl, tf_warning_or_error);
8701 
8702   return atexit_node;
8703 }
8704 
8705 /* Like get_atexit_node, but for thread-local cleanups.  */
8706 
8707 static tree
get_thread_atexit_node(void)8708 get_thread_atexit_node (void)
8709 {
8710   /* The declaration for `__cxa_thread_atexit' is:
8711 
8712      int __cxa_thread_atexit (void (*)(void *), void *, void *) */
8713   tree fn_type = build_function_type_list (integer_type_node,
8714 					   get_atexit_fn_ptr_type (),
8715 					   ptr_type_node, ptr_type_node,
8716 					   NULL_TREE);
8717 
8718   /* Now, build the function declaration.  */
8719   tree atexit_fndecl = build_library_fn_ptr ("__cxa_thread_atexit", fn_type,
8720 					     ECF_LEAF | ECF_NOTHROW);
8721   return decay_conversion (atexit_fndecl, tf_warning_or_error);
8722 }
8723 
8724 /* Returns the __dso_handle VAR_DECL.  */
8725 
8726 static tree
get_dso_handle_node(void)8727 get_dso_handle_node (void)
8728 {
8729   if (dso_handle_node)
8730     return dso_handle_node;
8731 
8732   /* Declare the variable.  */
8733   dso_handle_node = declare_global_var (get_identifier ("__dso_handle"),
8734 					ptr_type_node);
8735 
8736 #ifdef HAVE_GAS_HIDDEN
8737   if (dso_handle_node != error_mark_node)
8738     {
8739       DECL_VISIBILITY (dso_handle_node) = VISIBILITY_HIDDEN;
8740       DECL_VISIBILITY_SPECIFIED (dso_handle_node) = 1;
8741     }
8742 #endif
8743 
8744   return dso_handle_node;
8745 }
8746 
8747 /* Begin a new function with internal linkage whose job will be simply
8748    to destroy some particular variable.  */
8749 
8750 static GTY(()) int start_cleanup_cnt;
8751 
8752 static tree
start_cleanup_fn(void)8753 start_cleanup_fn (void)
8754 {
8755   char name[32];
8756   tree fntype;
8757   tree fndecl;
8758   bool use_cxa_atexit = flag_use_cxa_atexit
8759 			&& !targetm.cxx.use_atexit_for_cxa_atexit ();
8760 
8761   push_to_top_level ();
8762 
8763   /* No need to mangle this.  */
8764   push_lang_context (lang_name_c);
8765 
8766   /* Build the name of the function.  */
8767   sprintf (name, "__tcf_%d", start_cleanup_cnt++);
8768   /* Build the function declaration.  */
8769   fntype = TREE_TYPE (get_atexit_fn_ptr_type ());
8770   fndecl = build_lang_decl (FUNCTION_DECL, get_identifier (name), fntype);
8771   /* It's a function with internal linkage, generated by the
8772      compiler.  */
8773   TREE_PUBLIC (fndecl) = 0;
8774   DECL_ARTIFICIAL (fndecl) = 1;
8775   /* Make the function `inline' so that it is only emitted if it is
8776      actually needed.  It is unlikely that it will be inlined, since
8777      it is only called via a function pointer, but we avoid unnecessary
8778      emissions this way.  */
8779   DECL_DECLARED_INLINE_P (fndecl) = 1;
8780   DECL_INTERFACE_KNOWN (fndecl) = 1;
8781   /* Build the parameter.  */
8782   if (use_cxa_atexit)
8783     {
8784       tree parmdecl = cp_build_parm_decl (fndecl, NULL_TREE, ptr_type_node);
8785       TREE_USED (parmdecl) = 1;
8786       DECL_READ_P (parmdecl) = 1;
8787       DECL_ARGUMENTS (fndecl) = parmdecl;
8788     }
8789 
8790   pushdecl (fndecl);
8791   start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
8792 
8793   pop_lang_context ();
8794 
8795   return current_function_decl;
8796 }
8797 
8798 /* Finish the cleanup function begun by start_cleanup_fn.  */
8799 
8800 static void
end_cleanup_fn(void)8801 end_cleanup_fn (void)
8802 {
8803   expand_or_defer_fn (finish_function (/*inline_p=*/false));
8804 
8805   pop_from_top_level ();
8806 }
8807 
8808 /* Generate code to handle the destruction of DECL, an object with
8809    static storage duration.  */
8810 
8811 tree
register_dtor_fn(tree decl)8812 register_dtor_fn (tree decl)
8813 {
8814   tree cleanup;
8815   tree addr;
8816   tree compound_stmt;
8817   tree fcall;
8818   tree type;
8819   bool ob_parm, dso_parm, use_dtor;
8820   tree arg0, arg1, arg2;
8821   tree atex_node;
8822 
8823   type = TREE_TYPE (decl);
8824   if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
8825     return void_node;
8826 
8827   if (decl_maybe_constant_destruction (decl, type)
8828       && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
8829     {
8830       cxx_maybe_build_cleanup (decl, tf_warning_or_error);
8831       return void_node;
8832     }
8833 
8834   /* If we're using "__cxa_atexit" (or "__cxa_thread_atexit" or
8835      "__aeabi_atexit"), and DECL is a class object, we can just pass the
8836      destructor to "__cxa_atexit"; we don't have to build a temporary
8837      function to do the cleanup.  */
8838   dso_parm = (flag_use_cxa_atexit
8839 	      && !targetm.cxx.use_atexit_for_cxa_atexit ());
8840   ob_parm = (CP_DECL_THREAD_LOCAL_P (decl) || dso_parm);
8841   use_dtor = ob_parm && CLASS_TYPE_P (type);
8842   if (use_dtor)
8843     {
8844       cleanup = get_class_binding (type, complete_dtor_identifier);
8845 
8846       /* Make sure it is accessible.  */
8847       perform_or_defer_access_check (TYPE_BINFO (type), cleanup, cleanup,
8848 				     tf_warning_or_error);
8849     }
8850   else
8851     {
8852       /* Call build_cleanup before we enter the anonymous function so
8853 	 that any access checks will be done relative to the current
8854 	 scope, rather than the scope of the anonymous function.  */
8855       build_cleanup (decl);
8856 
8857       /* Now start the function.  */
8858       cleanup = start_cleanup_fn ();
8859 
8860       /* Now, recompute the cleanup.  It may contain SAVE_EXPRs that refer
8861 	 to the original function, rather than the anonymous one.  That
8862 	 will make the back end think that nested functions are in use,
8863 	 which causes confusion.  */
8864       push_deferring_access_checks (dk_no_check);
8865       fcall = build_cleanup (decl);
8866       pop_deferring_access_checks ();
8867 
8868       /* Create the body of the anonymous function.  */
8869       compound_stmt = begin_compound_stmt (BCS_FN_BODY);
8870       finish_expr_stmt (fcall);
8871       finish_compound_stmt (compound_stmt);
8872       end_cleanup_fn ();
8873     }
8874 
8875   /* Call atexit with the cleanup function.  */
8876   mark_used (cleanup);
8877   cleanup = build_address (cleanup);
8878 
8879   if (CP_DECL_THREAD_LOCAL_P (decl))
8880     atex_node = get_thread_atexit_node ();
8881   else
8882     atex_node = get_atexit_node ();
8883 
8884   if (use_dtor)
8885     {
8886       /* We must convert CLEANUP to the type that "__cxa_atexit"
8887 	 expects.  */
8888       cleanup = build_nop (get_atexit_fn_ptr_type (), cleanup);
8889       /* "__cxa_atexit" will pass the address of DECL to the
8890 	 cleanup function.  */
8891       mark_used (decl);
8892       addr = build_address (decl);
8893       /* The declared type of the parameter to "__cxa_atexit" is
8894 	 "void *".  For plain "T*", we could just let the
8895 	 machinery in cp_build_function_call convert it -- but if the
8896 	 type is "cv-qualified T *", then we need to convert it
8897 	 before passing it in, to avoid spurious errors.  */
8898       addr = build_nop (ptr_type_node, addr);
8899     }
8900   else
8901     /* Since the cleanup functions we build ignore the address
8902        they're given, there's no reason to pass the actual address
8903        in, and, in general, it's cheaper to pass NULL than any
8904        other value.  */
8905     addr = null_pointer_node;
8906 
8907   if (dso_parm)
8908     arg2 = cp_build_addr_expr (get_dso_handle_node (),
8909 			       tf_warning_or_error);
8910   else if (ob_parm)
8911     /* Just pass NULL to the dso handle parm if we don't actually
8912        have a DSO handle on this target.  */
8913     arg2 = null_pointer_node;
8914   else
8915     arg2 = NULL_TREE;
8916 
8917   if (ob_parm)
8918     {
8919       if (!CP_DECL_THREAD_LOCAL_P (decl)
8920 	  && targetm.cxx.use_aeabi_atexit ())
8921 	{
8922 	  arg1 = cleanup;
8923 	  arg0 = addr;
8924 	}
8925       else
8926 	{
8927 	  arg1 = addr;
8928 	  arg0 = cleanup;
8929 	}
8930     }
8931   else
8932     {
8933       arg0 = cleanup;
8934       arg1 = NULL_TREE;
8935     }
8936   return cp_build_function_call_nary (atex_node, tf_warning_or_error,
8937 				      arg0, arg1, arg2, NULL_TREE);
8938 }
8939 
8940 /* DECL is a VAR_DECL with static storage duration.  INIT, if present,
8941    is its initializer.  Generate code to handle the construction
8942    and destruction of DECL.  */
8943 
8944 static void
expand_static_init(tree decl,tree init)8945 expand_static_init (tree decl, tree init)
8946 {
8947   gcc_assert (VAR_P (decl));
8948   gcc_assert (TREE_STATIC (decl));
8949 
8950   /* Some variables require no dynamic initialization.  */
8951   if (decl_maybe_constant_destruction (decl, TREE_TYPE (decl)))
8952     {
8953       /* Make sure the destructor is callable.  */
8954       cxx_maybe_build_cleanup (decl, tf_warning_or_error);
8955       if (!init)
8956 	return;
8957     }
8958 
8959   if (CP_DECL_THREAD_LOCAL_P (decl) && DECL_GNU_TLS_P (decl)
8960       && !DECL_FUNCTION_SCOPE_P (decl))
8961     {
8962       location_t dloc = DECL_SOURCE_LOCATION (decl);
8963       if (init)
8964 	error_at (dloc, "non-local variable %qD declared %<__thread%> "
8965 		  "needs dynamic initialization", decl);
8966       else
8967 	error_at (dloc, "non-local variable %qD declared %<__thread%> "
8968 		  "has a non-trivial destructor", decl);
8969       static bool informed;
8970       if (!informed)
8971 	{
8972 	  inform (dloc, "C++11 %<thread_local%> allows dynamic "
8973 		  "initialization and destruction");
8974 	  informed = true;
8975 	}
8976       return;
8977     }
8978 
8979   if (DECL_FUNCTION_SCOPE_P (decl))
8980     {
8981       /* Emit code to perform this initialization but once.  */
8982       tree if_stmt = NULL_TREE, inner_if_stmt = NULL_TREE;
8983       tree then_clause = NULL_TREE, inner_then_clause = NULL_TREE;
8984       tree guard, guard_addr;
8985       tree flag, begin;
8986       /* We don't need thread-safety code for thread-local vars.  */
8987       bool thread_guard = (flag_threadsafe_statics
8988 			   && !CP_DECL_THREAD_LOCAL_P (decl));
8989 
8990       /* Emit code to perform this initialization but once.  This code
8991 	 looks like:
8992 
8993 	   static <type> guard;
8994 	   if (!__atomic_load (guard.first_byte)) {
8995 	     if (__cxa_guard_acquire (&guard)) {
8996 	       bool flag = false;
8997 	       try {
8998 		 // Do initialization.
8999 		 flag = true; __cxa_guard_release (&guard);
9000 		 // Register variable for destruction at end of program.
9001 	       } catch {
9002 		 if (!flag) __cxa_guard_abort (&guard);
9003 	       }
9004 	     }
9005 	   }
9006 
9007 	 Note that the `flag' variable is only set to 1 *after* the
9008 	 initialization is complete.  This ensures that an exception,
9009 	 thrown during the construction, will cause the variable to
9010 	 reinitialized when we pass through this code again, as per:
9011 
9012 	   [stmt.dcl]
9013 
9014 	   If the initialization exits by throwing an exception, the
9015 	   initialization is not complete, so it will be tried again
9016 	   the next time control enters the declaration.
9017 
9018 	 This process should be thread-safe, too; multiple threads
9019 	 should not be able to initialize the variable more than
9020 	 once.  */
9021 
9022       /* Create the guard variable.  */
9023       guard = get_guard (decl);
9024 
9025       /* Begin the conditional initialization.  */
9026       if_stmt = begin_if_stmt ();
9027 
9028       finish_if_stmt_cond (get_guard_cond (guard, thread_guard), if_stmt);
9029       then_clause = begin_compound_stmt (BCS_NO_SCOPE);
9030 
9031       if (thread_guard)
9032 	{
9033 	  tree vfntype = NULL_TREE;
9034 	  tree acquire_name, release_name, abort_name;
9035 	  tree acquire_fn, release_fn, abort_fn;
9036 	  guard_addr = build_address (guard);
9037 
9038 	  acquire_name = get_identifier ("__cxa_guard_acquire");
9039 	  release_name = get_identifier ("__cxa_guard_release");
9040 	  abort_name = get_identifier ("__cxa_guard_abort");
9041 	  acquire_fn = get_global_binding (acquire_name);
9042 	  release_fn = get_global_binding (release_name);
9043 	  abort_fn = get_global_binding (abort_name);
9044 	  if (!acquire_fn)
9045 	    acquire_fn = push_library_fn
9046 	      (acquire_name, build_function_type_list (integer_type_node,
9047 						       TREE_TYPE (guard_addr),
9048 						       NULL_TREE),
9049 	       NULL_TREE, ECF_NOTHROW);
9050 	  if (!release_fn || !abort_fn)
9051 	    vfntype = build_function_type_list (void_type_node,
9052 						TREE_TYPE (guard_addr),
9053 						NULL_TREE);
9054 	  if (!release_fn)
9055 	    release_fn = push_library_fn (release_name, vfntype, NULL_TREE,
9056 					  ECF_NOTHROW);
9057 	  if (!abort_fn)
9058 	    abort_fn = push_library_fn (abort_name, vfntype, NULL_TREE,
9059 					ECF_NOTHROW | ECF_LEAF);
9060 
9061 	  inner_if_stmt = begin_if_stmt ();
9062 	  finish_if_stmt_cond (build_call_n (acquire_fn, 1, guard_addr),
9063 			       inner_if_stmt);
9064 
9065 	  inner_then_clause = begin_compound_stmt (BCS_NO_SCOPE);
9066 	  begin = get_target_expr (boolean_false_node);
9067 	  flag = TARGET_EXPR_SLOT (begin);
9068 
9069 	  TARGET_EXPR_CLEANUP (begin)
9070 	    = build3 (COND_EXPR, void_type_node, flag,
9071 		      void_node,
9072 		      build_call_n (abort_fn, 1, guard_addr));
9073 	  CLEANUP_EH_ONLY (begin) = 1;
9074 
9075 	  /* Do the initialization itself.  */
9076 	  init = add_stmt_to_compound (begin, init);
9077 	  init = add_stmt_to_compound (init,
9078 				       build2 (MODIFY_EXPR, void_type_node,
9079 					       flag, boolean_true_node));
9080 
9081 	  /* Use atexit to register a function for destroying this static
9082 	     variable.  Do this before calling __cxa_guard_release.  */
9083 	  init = add_stmt_to_compound (init, register_dtor_fn (decl));
9084 
9085 	  init = add_stmt_to_compound (init, build_call_n (release_fn, 1,
9086 							   guard_addr));
9087 	}
9088       else
9089 	{
9090 	  init = add_stmt_to_compound (init, set_guard (guard));
9091 
9092 	  /* Use atexit to register a function for destroying this static
9093 	     variable.  */
9094 	  init = add_stmt_to_compound (init, register_dtor_fn (decl));
9095 	}
9096 
9097       finish_expr_stmt (init);
9098 
9099       if (thread_guard)
9100 	{
9101 	  finish_compound_stmt (inner_then_clause);
9102 	  finish_then_clause (inner_if_stmt);
9103 	  finish_if_stmt (inner_if_stmt);
9104 	}
9105 
9106       finish_compound_stmt (then_clause);
9107       finish_then_clause (if_stmt);
9108       finish_if_stmt (if_stmt);
9109     }
9110   else if (CP_DECL_THREAD_LOCAL_P (decl))
9111     tls_aggregates = tree_cons (init, decl, tls_aggregates);
9112   else
9113     static_aggregates = tree_cons (init, decl, static_aggregates);
9114 }
9115 
9116 
9117 /* Make TYPE a complete type based on INITIAL_VALUE.
9118    Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
9119    2 if there was no information (in which case assume 0 if DO_DEFAULT),
9120    3 if the initializer list is empty (in pedantic mode). */
9121 
9122 int
cp_complete_array_type(tree * ptype,tree initial_value,bool do_default)9123 cp_complete_array_type (tree *ptype, tree initial_value, bool do_default)
9124 {
9125   int failure;
9126   tree type, elt_type;
9127 
9128   /* Don't get confused by a CONSTRUCTOR for some other type.  */
9129   if (initial_value && TREE_CODE (initial_value) == CONSTRUCTOR
9130       && !BRACE_ENCLOSED_INITIALIZER_P (initial_value)
9131       && TREE_CODE (TREE_TYPE (initial_value)) != ARRAY_TYPE)
9132     return 1;
9133 
9134   if (initial_value)
9135     {
9136       unsigned HOST_WIDE_INT i;
9137       tree value;
9138 
9139       /* An array of character type can be initialized from a
9140 	 brace-enclosed string constant.
9141 
9142 	 FIXME: this code is duplicated from reshape_init. Probably
9143 	 we should just call reshape_init here?  */
9144       if (char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (*ptype)))
9145 	  && TREE_CODE (initial_value) == CONSTRUCTOR
9146 	  && !vec_safe_is_empty (CONSTRUCTOR_ELTS (initial_value)))
9147 	{
9148 	  vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
9149 	  tree value = (*v)[0].value;
9150 	  STRIP_ANY_LOCATION_WRAPPER (value);
9151 
9152 	  if (TREE_CODE (value) == STRING_CST
9153 	      && v->length () == 1)
9154 	    initial_value = value;
9155 	}
9156 
9157       /* If any of the elements are parameter packs, we can't actually
9158 	 complete this type now because the array size is dependent.  */
9159       if (TREE_CODE (initial_value) == CONSTRUCTOR)
9160 	{
9161 	  FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (initial_value),
9162 				      i, value)
9163 	    {
9164 	      if (PACK_EXPANSION_P (value))
9165 		return 0;
9166 	    }
9167 	}
9168     }
9169 
9170   failure = complete_array_type (ptype, initial_value, do_default);
9171 
9172   /* We can create the array before the element type is complete, which
9173      means that we didn't have these two bits set in the original type
9174      either.  In completing the type, we are expected to propagate these
9175      bits.  See also complete_type which does the same thing for arrays
9176      of fixed size.  */
9177   type = *ptype;
9178   if (type != error_mark_node && TYPE_DOMAIN (type))
9179     {
9180       elt_type = TREE_TYPE (type);
9181       TYPE_NEEDS_CONSTRUCTING (type) = TYPE_NEEDS_CONSTRUCTING (elt_type);
9182       TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
9183 	= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (elt_type);
9184     }
9185 
9186   return failure;
9187 }
9188 
9189 /* As above, but either give an error or reject zero-size arrays, depending
9190    on COMPLAIN.  */
9191 
9192 int
cp_complete_array_type_or_error(tree * ptype,tree initial_value,bool do_default,tsubst_flags_t complain)9193 cp_complete_array_type_or_error (tree *ptype, tree initial_value,
9194 				 bool do_default, tsubst_flags_t complain)
9195 {
9196   int failure;
9197   bool sfinae = !(complain & tf_error);
9198   /* In SFINAE context we can't be lenient about zero-size arrays.  */
9199   if (sfinae)
9200     ++pedantic;
9201   failure = cp_complete_array_type (ptype, initial_value, do_default);
9202   if (sfinae)
9203     --pedantic;
9204   if (failure)
9205     {
9206       if (sfinae)
9207 	/* Not an error.  */;
9208       else if (failure == 1)
9209 	error ("initializer fails to determine size of %qT", *ptype);
9210       else if (failure == 2)
9211 	{
9212 	  if (do_default)
9213 	    error ("array size missing in %qT", *ptype);
9214 	}
9215       else if (failure == 3)
9216 	error ("zero-size array %qT", *ptype);
9217       *ptype = error_mark_node;
9218     }
9219   return failure;
9220 }
9221 
9222 /* Return zero if something is declared to be a member of type
9223    CTYPE when in the context of CUR_TYPE.  STRING is the error
9224    message to print in that case.  Otherwise, quietly return 1.  */
9225 
9226 static int
member_function_or_else(tree ctype,tree cur_type,enum overload_flags flags)9227 member_function_or_else (tree ctype, tree cur_type, enum overload_flags flags)
9228 {
9229   if (ctype && ctype != cur_type)
9230     {
9231       if (flags == DTOR_FLAG)
9232 	error ("destructor for alien class %qT cannot be a member", ctype);
9233       else
9234 	error ("constructor for alien class %qT cannot be a member", ctype);
9235       return 0;
9236     }
9237   return 1;
9238 }
9239 
9240 /* Subroutine of `grokdeclarator'.  */
9241 
9242 /* Generate errors possibly applicable for a given set of specifiers.
9243    This is for ARM $7.1.2.  */
9244 
9245 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)9246 bad_specifiers (tree object,
9247 		enum bad_spec_place type,
9248 		int virtualp,
9249 		int quals,
9250 		int inlinep,
9251 		int friendp,
9252 		int raises,
9253 		const location_t* locations)
9254 {
9255   switch (type)
9256     {
9257       case BSP_VAR:
9258 	if (virtualp)
9259 	  error_at (locations[ds_virtual],
9260 		    "%qD declared as a %<virtual%> variable", object);
9261 	if (quals)
9262 	  error ("%<const%> and %<volatile%> function specifiers on "
9263 	         "%qD invalid in variable declaration", object);
9264 	break;
9265       case BSP_PARM:
9266 	if (virtualp)
9267 	  error_at (locations[ds_virtual],
9268 		    "%qD declared as a %<virtual%> parameter", object);
9269 	if (inlinep)
9270 	  error_at (locations[ds_inline],
9271 		    "%qD declared as an %<inline%> parameter", object);
9272 	if (quals)
9273 	  error ("%<const%> and %<volatile%> function specifiers on "
9274 	  	 "%qD invalid in parameter declaration", object);
9275 	break;
9276       case BSP_TYPE:
9277 	if (virtualp)
9278 	  error_at (locations[ds_virtual],
9279 		    "%qD declared as a %<virtual%> type", object);
9280 	if (inlinep)
9281 	  error_at (locations[ds_inline],
9282 		    "%qD declared as an %<inline%> type", object);
9283 	if (quals)
9284 	  error ("%<const%> and %<volatile%> function specifiers on "
9285 	  	 "%qD invalid in type declaration", object);
9286 	break;
9287       case BSP_FIELD:
9288 	if (virtualp)
9289 	  error_at (locations[ds_virtual],
9290 		    "%qD declared as a %<virtual%> field", object);
9291 	if (inlinep)
9292 	  error_at (locations[ds_inline],
9293 		    "%qD declared as an %<inline%> field", object);
9294 	if (quals)
9295 	  error ("%<const%> and %<volatile%> function specifiers on "
9296 	  	 "%qD invalid in field declaration", object);
9297 	break;
9298       default:
9299         gcc_unreachable();
9300     }
9301   if (friendp)
9302     error ("%q+D declared as a friend", object);
9303   if (raises
9304       && !flag_noexcept_type
9305       && (TREE_CODE (object) == TYPE_DECL
9306 	  || (!TYPE_PTRFN_P (TREE_TYPE (object))
9307 	      && !TYPE_REFFN_P (TREE_TYPE (object))
9308 	      && !TYPE_PTRMEMFUNC_P (TREE_TYPE (object)))))
9309     error ("%q+D declared with an exception specification", object);
9310 }
9311 
9312 /* DECL is a member function or static data member and is presently
9313    being defined.  Check that the definition is taking place in a
9314    valid namespace.  */
9315 
9316 static void
check_class_member_definition_namespace(tree decl)9317 check_class_member_definition_namespace (tree decl)
9318 {
9319   /* These checks only apply to member functions and static data
9320      members.  */
9321   gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
9322   /* We check for problems with specializations in pt.c in
9323      check_specialization_namespace, where we can issue better
9324      diagnostics.  */
9325   if (processing_specialization)
9326     return;
9327   /* We check this in check_explicit_instantiation_namespace.  */
9328   if (processing_explicit_instantiation)
9329     return;
9330   /* [class.mfct]
9331 
9332      A member function definition that appears outside of the
9333      class definition shall appear in a namespace scope enclosing
9334      the class definition.
9335 
9336      [class.static.data]
9337 
9338      The definition for a static data member shall appear in a
9339      namespace scope enclosing the member's class definition.  */
9340   if (!is_ancestor (current_namespace, DECL_CONTEXT (decl)))
9341     permerror (input_location, "definition of %qD is not in namespace enclosing %qT",
9342 	       decl, DECL_CONTEXT (decl));
9343 }
9344 
9345 /* Build a PARM_DECL for the "this" parameter of FN.  TYPE is the
9346    METHOD_TYPE for a non-static member function; QUALS are the
9347    cv-qualifiers that apply to the function.  */
9348 
9349 tree
build_this_parm(tree fn,tree type,cp_cv_quals quals)9350 build_this_parm (tree fn, tree type, cp_cv_quals quals)
9351 {
9352   tree this_type;
9353   tree qual_type;
9354   tree parm;
9355   cp_cv_quals this_quals;
9356 
9357   if (CLASS_TYPE_P (type))
9358     {
9359       this_type
9360 	= cp_build_qualified_type (type, quals & ~TYPE_QUAL_RESTRICT);
9361       this_type = build_pointer_type (this_type);
9362     }
9363   else
9364     this_type = type_of_this_parm (type);
9365   /* The `this' parameter is implicitly `const'; it cannot be
9366      assigned to.  */
9367   this_quals = (quals & TYPE_QUAL_RESTRICT) | TYPE_QUAL_CONST;
9368   qual_type = cp_build_qualified_type (this_type, this_quals);
9369   parm = build_artificial_parm (fn, this_identifier, qual_type);
9370   cp_apply_type_quals_to_decl (this_quals, parm);
9371   return parm;
9372 }
9373 
9374 /* DECL is a static member function.  Complain if it was declared
9375    with function-cv-quals.  */
9376 
9377 static void
check_static_quals(tree decl,cp_cv_quals quals)9378 check_static_quals (tree decl, cp_cv_quals quals)
9379 {
9380   if (quals != TYPE_UNQUALIFIED)
9381     error ("static member function %q#D declared with type qualifiers",
9382 	   decl);
9383 }
9384 
9385 // Check that FN takes no arguments and returns bool.
9386 static void
check_concept_fn(tree fn)9387 check_concept_fn (tree fn)
9388 {
9389   // A constraint is nullary.
9390   if (DECL_ARGUMENTS (fn))
9391     error_at (DECL_SOURCE_LOCATION (fn),
9392 	      "concept %q#D declared with function parameters", fn);
9393 
9394   // The declared return type of the concept shall be bool, and
9395   // it shall not be deduced from it definition.
9396   tree type = TREE_TYPE (TREE_TYPE (fn));
9397   if (is_auto (type))
9398     error_at (DECL_SOURCE_LOCATION (fn),
9399 	      "concept %q#D declared with a deduced return type", fn);
9400   else if (type != boolean_type_node)
9401     error_at (DECL_SOURCE_LOCATION (fn),
9402 	      "concept %q#D with non-%<bool%> return type %qT", fn, type);
9403 }
9404 
9405 /* Helper function.  Replace the temporary this parameter injected
9406    during cp_finish_omp_declare_simd with the real this parameter.  */
9407 
9408 static tree
declare_simd_adjust_this(tree * tp,int * walk_subtrees,void * data)9409 declare_simd_adjust_this (tree *tp, int *walk_subtrees, void *data)
9410 {
9411   tree this_parm = (tree) data;
9412   if (TREE_CODE (*tp) == PARM_DECL
9413       && DECL_NAME (*tp) == this_identifier
9414       && *tp != this_parm)
9415     *tp = this_parm;
9416   else if (TYPE_P (*tp))
9417     *walk_subtrees = 0;
9418   return NULL_TREE;
9419 }
9420 
9421 /* CTYPE is class type, or null if non-class.
9422    TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE
9423    or METHOD_TYPE.
9424    DECLARATOR is the function's name.
9425    PARMS is a chain of PARM_DECLs for the function.
9426    VIRTUALP is truthvalue of whether the function is virtual or not.
9427    FLAGS are to be passed through to `grokclassfn'.
9428    QUALS are qualifiers indicating whether the function is `const'
9429    or `volatile'.
9430    RAISES is a list of exceptions that this function can raise.
9431    CHECK is 1 if we must find this method in CTYPE, 0 if we should
9432    not look, and -1 if we should not call `grokclassfn' at all.
9433 
9434    SFK is the kind of special function (if any) for the new function.
9435 
9436    Returns `NULL_TREE' if something goes wrong, after issuing
9437    applicable error messages.  */
9438 
9439 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 initialized,int template_count,tree in_namespace,tree * attrlist,location_t location)9440 grokfndecl (tree ctype,
9441 	    tree type,
9442 	    tree declarator,
9443 	    tree parms,
9444 	    tree orig_declarator,
9445 	    const cp_decl_specifier_seq *declspecs,
9446 	    tree decl_reqs,
9447 	    int virtualp,
9448 	    enum overload_flags flags,
9449 	    cp_cv_quals quals,
9450 	    cp_ref_qualifier rqual,
9451 	    tree raises,
9452 	    int check,
9453 	    int friendp,
9454 	    int publicp,
9455 	    int inlinep,
9456 	    bool deletedp,
9457 	    special_function_kind sfk,
9458 	    bool funcdef_flag,
9459 	    bool late_return_type_p,
9460 	    int initialized,
9461 	    int template_count,
9462 	    tree in_namespace,
9463 	    tree* attrlist,
9464 	    location_t location)
9465 {
9466   tree decl;
9467   int staticp = ctype && TREE_CODE (type) == FUNCTION_TYPE;
9468   tree t;
9469 
9470   if (location == UNKNOWN_LOCATION)
9471     location = input_location;
9472 
9473   /* Was the concept specifier present?  */
9474   bool concept_p = inlinep & 4;
9475 
9476   /* Concept declarations must have a corresponding definition.  */
9477   if (concept_p && !funcdef_flag)
9478     {
9479       error_at (location, "concept %qD has no definition", declarator);
9480       return NULL_TREE;
9481     }
9482 
9483   type = build_cp_fntype_variant (type, rqual, raises, late_return_type_p);
9484 
9485   decl = build_lang_decl_loc (location, FUNCTION_DECL, declarator, type);
9486 
9487   /* Set the constraints on the declaration. */
9488   if (flag_concepts)
9489     {
9490       tree tmpl_reqs = NULL_TREE;
9491       tree ctx = friendp ? current_class_type : ctype;
9492       bool memtmpl = (processing_template_decl > template_class_depth (ctx));
9493       if (memtmpl)
9494         tmpl_reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
9495       tree ci = build_constraints (tmpl_reqs, decl_reqs);
9496       if (concept_p && ci)
9497         {
9498           error_at (location, "a function concept cannot be constrained");
9499           ci = NULL_TREE;
9500         }
9501       /* C++20 CA378: Remove non-templated constrained functions.  */
9502       if (ci && !flag_concepts_ts
9503 	  && (!processing_template_decl
9504 	      || (friendp && !memtmpl && !initialized && !funcdef_flag)))
9505 	{
9506 	  error_at (location, "constraints on a non-templated function");
9507 	  ci = NULL_TREE;
9508 	}
9509       set_constraints (decl, ci);
9510     }
9511 
9512   if (TREE_CODE (type) == METHOD_TYPE)
9513     {
9514       tree parm = build_this_parm (decl, type, quals);
9515       DECL_CHAIN (parm) = parms;
9516       parms = parm;
9517 
9518       /* Allocate space to hold the vptr bit if needed.  */
9519       SET_DECL_ALIGN (decl, MINIMUM_METHOD_BOUNDARY);
9520     }
9521 
9522   DECL_ARGUMENTS (decl) = parms;
9523   for (t = parms; t; t = DECL_CHAIN (t))
9524     DECL_CONTEXT (t) = decl;
9525 
9526   /* Propagate volatile out from type to decl.  */
9527   if (TYPE_VOLATILE (type))
9528     TREE_THIS_VOLATILE (decl) = 1;
9529 
9530   /* Setup decl according to sfk.  */
9531   switch (sfk)
9532     {
9533     case sfk_constructor:
9534     case sfk_copy_constructor:
9535     case sfk_move_constructor:
9536       DECL_CXX_CONSTRUCTOR_P (decl) = 1;
9537       DECL_NAME (decl) = ctor_identifier;
9538       break;
9539     case sfk_destructor:
9540       DECL_CXX_DESTRUCTOR_P (decl) = 1;
9541       DECL_NAME (decl) = dtor_identifier;
9542       break;
9543     default:
9544       break;
9545     }
9546 
9547   if (friendp && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)
9548     {
9549       if (funcdef_flag)
9550 	error_at (location,
9551 		  "defining explicit specialization %qD in friend declaration",
9552 		  orig_declarator);
9553       else
9554 	{
9555 	  tree fns = TREE_OPERAND (orig_declarator, 0);
9556 	  tree args = TREE_OPERAND (orig_declarator, 1);
9557 
9558 	  if (PROCESSING_REAL_TEMPLATE_DECL_P ())
9559 	    {
9560 	      /* Something like `template <class T> friend void f<T>()'.  */
9561 	      error_at (location,
9562 			"invalid use of template-id %qD in declaration "
9563 			"of primary template",
9564 			orig_declarator);
9565 	      return NULL_TREE;
9566 	    }
9567 
9568 
9569 	  /* A friend declaration of the form friend void f<>().  Record
9570 	     the information in the TEMPLATE_ID_EXPR.  */
9571 	  SET_DECL_IMPLICIT_INSTANTIATION (decl);
9572 
9573 	  gcc_assert (identifier_p (fns) || OVL_P (fns));
9574 	  DECL_TEMPLATE_INFO (decl) = build_template_info (fns, args);
9575 
9576 	  for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t))
9577 	    if (TREE_PURPOSE (t)
9578 		&& TREE_CODE (TREE_PURPOSE (t)) == DEFERRED_PARSE)
9579 	    {
9580 	      error_at (defparse_location (TREE_PURPOSE (t)),
9581 			"default arguments are not allowed in declaration "
9582 			"of friend template specialization %qD",
9583 			decl);
9584 	      return NULL_TREE;
9585 	    }
9586 
9587 	  if (inlinep & 1)
9588 	    {
9589 	      error_at (declspecs->locations[ds_inline],
9590 			"%<inline%> is not allowed in declaration of friend "
9591 			"template specialization %qD",
9592 			decl);
9593 	      return NULL_TREE;
9594 	    }
9595 	}
9596     }
9597 
9598   /* C++17 11.3.6/4: "If a friend declaration specifies a default argument
9599      expression, that declaration shall be a definition..."  */
9600   if (friendp && !funcdef_flag)
9601     {
9602       for (tree t = FUNCTION_FIRST_USER_PARMTYPE (decl);
9603 	   t && t != void_list_node; t = TREE_CHAIN (t))
9604 	if (TREE_PURPOSE (t))
9605 	  {
9606 	    permerror (DECL_SOURCE_LOCATION (decl),
9607 		       "friend declaration of %qD specifies default "
9608 		       "arguments and isn%'t a definition", decl);
9609 	    break;
9610 	  }
9611     }
9612 
9613   /* FIXME: For now.  */
9614   if (virtualp && (inlinep & 8) != 0)
9615     {
9616       sorry_at (DECL_SOURCE_LOCATION (decl),
9617 		"%<virtual%> %<consteval%> method %qD not supported yet",
9618 		decl);
9619       inlinep &= ~8;
9620     }
9621 
9622   /* If this decl has namespace scope, set that up.  */
9623   if (in_namespace)
9624     set_decl_namespace (decl, in_namespace, friendp);
9625   else if (!ctype)
9626     DECL_CONTEXT (decl) = FROB_CONTEXT (current_decl_namespace ());
9627 
9628   /* `main' and builtins have implicit 'C' linkage.  */
9629   if (ctype == NULL_TREE
9630       && DECL_FILE_SCOPE_P (decl)
9631       && current_lang_name == lang_name_cplusplus
9632       && (MAIN_NAME_P (declarator)
9633 	  || (IDENTIFIER_LENGTH (declarator) > 10
9634 	      && IDENTIFIER_POINTER (declarator)[0] == '_'
9635 	      && IDENTIFIER_POINTER (declarator)[1] == '_'
9636 	      && strncmp (IDENTIFIER_POINTER (declarator)+2,
9637 			  "builtin_", 8) == 0)
9638 	  || (targetcm.cxx_implicit_extern_c
9639 	      && (targetcm.cxx_implicit_extern_c
9640 		  (IDENTIFIER_POINTER (declarator))))))
9641     SET_DECL_LANGUAGE (decl, lang_c);
9642 
9643   /* Should probably propagate const out from type to decl I bet (mrs).  */
9644   if (staticp)
9645     {
9646       DECL_STATIC_FUNCTION_P (decl) = 1;
9647       DECL_CONTEXT (decl) = ctype;
9648     }
9649 
9650   if (deletedp)
9651     DECL_DELETED_FN (decl) = 1;
9652 
9653   if (ctype)
9654     {
9655       DECL_CONTEXT (decl) = ctype;
9656       if (funcdef_flag)
9657 	check_class_member_definition_namespace (decl);
9658     }
9659 
9660   if (ctype == NULL_TREE && DECL_MAIN_P (decl))
9661     {
9662       if (PROCESSING_REAL_TEMPLATE_DECL_P())
9663 	error_at (location, "cannot declare %<::main%> to be a template");
9664       if (inlinep & 1)
9665 	error_at (declspecs->locations[ds_inline],
9666 		  "cannot declare %<::main%> to be inline");
9667       if (inlinep & 2)
9668 	error_at (declspecs->locations[ds_constexpr],
9669 		  "cannot declare %<::main%> to be %qs", "constexpr");
9670       if (inlinep & 8)
9671 	error_at (declspecs->locations[ds_consteval],
9672 		  "cannot declare %<::main%> to be %qs", "consteval");
9673       if (!publicp)
9674 	error_at (location, "cannot declare %<::main%> to be static");
9675       inlinep = 0;
9676       publicp = 1;
9677     }
9678 
9679   /* Members of anonymous types and local classes have no linkage; make
9680      them internal.  If a typedef is made later, this will be changed.  */
9681   if (ctype && (!TREE_PUBLIC (TYPE_MAIN_DECL (ctype))
9682 		|| decl_function_context (TYPE_MAIN_DECL (ctype))))
9683     publicp = 0;
9684 
9685   if (publicp && cxx_dialect == cxx98)
9686     {
9687       /* [basic.link]: A name with no linkage (notably, the name of a class
9688 	 or enumeration declared in a local scope) shall not be used to
9689 	 declare an entity with linkage.
9690 
9691 	 DR 757 relaxes this restriction for C++0x.  */
9692       no_linkage_error (decl);
9693     }
9694 
9695   TREE_PUBLIC (decl) = publicp;
9696   if (! publicp)
9697     {
9698       DECL_INTERFACE_KNOWN (decl) = 1;
9699       DECL_NOT_REALLY_EXTERN (decl) = 1;
9700     }
9701 
9702   /* If the declaration was declared inline, mark it as such.  */
9703   if (inlinep)
9704     {
9705       DECL_DECLARED_INLINE_P (decl) = 1;
9706       if (publicp)
9707 	DECL_COMDAT (decl) = 1;
9708     }
9709   if (inlinep & 2)
9710     DECL_DECLARED_CONSTEXPR_P (decl) = true;
9711   else if (inlinep & 8)
9712     {
9713       DECL_DECLARED_CONSTEXPR_P (decl) = true;
9714       SET_DECL_IMMEDIATE_FUNCTION_P (decl);
9715     }
9716 
9717   // If the concept declaration specifier was found, check
9718   // that the declaration satisfies the necessary requirements.
9719   if (concept_p)
9720     {
9721       DECL_DECLARED_CONCEPT_P (decl) = true;
9722       check_concept_fn (decl);
9723     }
9724 
9725   DECL_EXTERNAL (decl) = 1;
9726   if (TREE_CODE (type) == FUNCTION_TYPE)
9727     {
9728       if (quals || rqual)
9729 	TREE_TYPE (decl) = apply_memfn_quals (TREE_TYPE (decl),
9730 					      TYPE_UNQUALIFIED,
9731 					      REF_QUAL_NONE);
9732 
9733       if (quals)
9734 	{
9735 	  error (ctype
9736 		 ? G_("static member function %qD cannot have cv-qualifier")
9737 		 : G_("non-member function %qD cannot have cv-qualifier"),
9738 		 decl);
9739 	  quals = TYPE_UNQUALIFIED;
9740 	}
9741 
9742       if (rqual)
9743 	{
9744 	  error (ctype
9745 		 ? G_("static member function %qD cannot have ref-qualifier")
9746 		 : G_("non-member function %qD cannot have ref-qualifier"),
9747 		 decl);
9748 	  rqual = REF_QUAL_NONE;
9749 	}
9750     }
9751 
9752   if (deduction_guide_p (decl))
9753     {
9754       if (!DECL_NAMESPACE_SCOPE_P (decl))
9755 	{
9756 	  error_at (location, "deduction guide %qD must be declared at "
9757 		    "namespace scope", decl);
9758 	  return NULL_TREE;
9759 	}
9760       tree type = TREE_TYPE (DECL_NAME (decl));
9761       if (in_namespace == NULL_TREE
9762 	  && CP_DECL_CONTEXT (decl) != CP_TYPE_CONTEXT (type))
9763 	{
9764 	  error_at (location, "deduction guide %qD must be declared in the "
9765 			      "same scope as %qT", decl, type);
9766 	  inform (location_of (type), "  declared here");
9767 	  return NULL_TREE;
9768 	}
9769       if (funcdef_flag)
9770 	error_at (location,
9771 		  "deduction guide %qD must not have a function body", decl);
9772     }
9773   else if (IDENTIFIER_ANY_OP_P (DECL_NAME (decl))
9774 	   && !grok_op_properties (decl, /*complain=*/true))
9775     return NULL_TREE;
9776   else if (UDLIT_OPER_P (DECL_NAME (decl)))
9777     {
9778       bool long_long_unsigned_p;
9779       bool long_double_p;
9780       const char *suffix = NULL;
9781       /* [over.literal]/6: Literal operators shall not have C linkage. */
9782       if (DECL_LANGUAGE (decl) == lang_c)
9783 	{
9784 	  error_at (location, "literal operator with C linkage");
9785 	  maybe_show_extern_c_location ();
9786 	  return NULL_TREE;
9787 	}
9788 
9789       if (DECL_NAMESPACE_SCOPE_P (decl))
9790 	{
9791 	  if (!check_literal_operator_args (decl, &long_long_unsigned_p,
9792 					    &long_double_p))
9793 	    {
9794 	      error_at (location, "%qD has invalid argument list", decl);
9795 	      return NULL_TREE;
9796 	    }
9797 
9798 	  suffix = UDLIT_OP_SUFFIX (DECL_NAME (decl));
9799 	  if (long_long_unsigned_p)
9800 	    {
9801 	      if (cpp_interpret_int_suffix (parse_in, suffix, strlen (suffix)))
9802 		warning_at (location, 0, "integer suffix %qs"
9803 			    " shadowed by implementation", suffix);
9804 	    }
9805 	  else if (long_double_p)
9806 	    {
9807 	      if (cpp_interpret_float_suffix (parse_in, suffix, strlen (suffix)))
9808 		warning_at (location, 0, "floating-point suffix %qs"
9809 			    " shadowed by implementation", suffix);
9810 	    }
9811 	  /* 17.6.3.3.5  */
9812 	  if (suffix[0] != '_'
9813 	      && !current_function_decl && !(friendp && !funcdef_flag))
9814 	    warning_at (location, OPT_Wliteral_suffix,
9815 			"literal operator suffixes not preceded by %<_%>"
9816 			" are reserved for future standardization");
9817 	}
9818       else
9819 	{
9820 	  error_at (location, "%qD must be a non-member function", decl);
9821 	  return NULL_TREE;
9822 	}
9823     }
9824 
9825   if (funcdef_flag)
9826     /* Make the init_value nonzero so pushdecl knows this is not
9827        tentative.  error_mark_node is replaced later with the BLOCK.  */
9828     DECL_INITIAL (decl) = error_mark_node;
9829 
9830   if (TYPE_NOTHROW_P (type) || nothrow_libfn_p (decl))
9831     TREE_NOTHROW (decl) = 1;
9832 
9833   if (flag_openmp || flag_openmp_simd)
9834     {
9835       /* Adjust "omp declare simd" attributes.  */
9836       tree ods = lookup_attribute ("omp declare simd", *attrlist);
9837       if (ods)
9838 	{
9839 	  tree attr;
9840 	  for (attr = ods; attr;
9841 	       attr = lookup_attribute ("omp declare simd", TREE_CHAIN (attr)))
9842 	    {
9843 	      if (TREE_CODE (type) == METHOD_TYPE)
9844 		walk_tree (&TREE_VALUE (attr), declare_simd_adjust_this,
9845 			   DECL_ARGUMENTS (decl), NULL);
9846 	      if (TREE_VALUE (attr) != NULL_TREE)
9847 		{
9848 		  tree cl = TREE_VALUE (TREE_VALUE (attr));
9849 		  cl = c_omp_declare_simd_clauses_to_numbers
9850 						(DECL_ARGUMENTS (decl), cl);
9851 		  if (cl)
9852 		    TREE_VALUE (TREE_VALUE (attr)) = cl;
9853 		  else
9854 		    TREE_VALUE (attr) = NULL_TREE;
9855 		}
9856 	    }
9857 	}
9858     }
9859 
9860   /* Caller will do the rest of this.  */
9861   if (check < 0)
9862     return decl;
9863 
9864   if (ctype != NULL_TREE)
9865     grokclassfn (ctype, decl, flags);
9866 
9867   /* 12.4/3  */
9868   if (cxx_dialect >= cxx11
9869       && DECL_DESTRUCTOR_P (decl)
9870       && !TYPE_BEING_DEFINED (DECL_CONTEXT (decl))
9871       && !processing_template_decl)
9872     deduce_noexcept_on_destructor (decl);
9873 
9874   decl = check_explicit_specialization (orig_declarator, decl,
9875 					template_count,
9876 					2 * funcdef_flag +
9877 					4 * (friendp != 0) +
9878 	                                8 * concept_p,
9879 					*attrlist);
9880   if (decl == error_mark_node)
9881     return NULL_TREE;
9882 
9883   if (DECL_STATIC_FUNCTION_P (decl))
9884     check_static_quals (decl, quals);
9885 
9886   if (attrlist)
9887     {
9888       cplus_decl_attributes (&decl, *attrlist, 0);
9889       *attrlist = NULL_TREE;
9890     }
9891 
9892   /* Check main's type after attributes have been applied.  */
9893   if (ctype == NULL_TREE && DECL_MAIN_P (decl))
9894     {
9895       if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
9896 			integer_type_node))
9897 	{
9898 	  tree oldtypeargs = TYPE_ARG_TYPES (TREE_TYPE (decl));
9899 	  tree newtype;
9900 	  error_at (declspecs->locations[ds_type_spec],
9901 		    "%<::main%> must return %<int%>");
9902 	  newtype = build_function_type (integer_type_node, oldtypeargs);
9903 	  TREE_TYPE (decl) = newtype;
9904 	}
9905       if (warn_main)
9906 	check_main_parameter_types (decl);
9907     }
9908 
9909   if (ctype != NULL_TREE && check)
9910     {
9911       tree old_decl = check_classfn (ctype, decl,
9912 				     (processing_template_decl
9913 				      > template_class_depth (ctype))
9914 				     ? current_template_parms
9915 				     : NULL_TREE);
9916 
9917       if (old_decl == error_mark_node)
9918 	return NULL_TREE;
9919 
9920       if (old_decl)
9921 	{
9922 	  tree ok;
9923 	  tree pushed_scope;
9924 
9925 	  if (TREE_CODE (old_decl) == TEMPLATE_DECL)
9926 	    /* Because grokfndecl is always supposed to return a
9927 	       FUNCTION_DECL, we pull out the DECL_TEMPLATE_RESULT
9928 	       here.  We depend on our callers to figure out that its
9929 	       really a template that's being returned.  */
9930 	    old_decl = DECL_TEMPLATE_RESULT (old_decl);
9931 
9932 	  if (DECL_STATIC_FUNCTION_P (old_decl)
9933 	      && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
9934 	    {
9935 	      /* Remove the `this' parm added by grokclassfn.  */
9936 	      revert_static_member_fn (decl);
9937 	      check_static_quals (decl, quals);
9938 	    }
9939 	  if (DECL_ARTIFICIAL (old_decl))
9940 	    {
9941 	      error ("definition of implicitly-declared %qD", old_decl);
9942 	      return NULL_TREE;
9943 	    }
9944 	  else if (DECL_DEFAULTED_FN (old_decl))
9945 	    {
9946 	      error ("definition of explicitly-defaulted %q+D", decl);
9947 	      inform (DECL_SOURCE_LOCATION (old_decl),
9948 		      "%q#D explicitly defaulted here", old_decl);
9949 	      return NULL_TREE;
9950 	    }
9951 
9952 	  /* Since we've smashed OLD_DECL to its
9953 	     DECL_TEMPLATE_RESULT, we must do the same to DECL.  */
9954 	  if (TREE_CODE (decl) == TEMPLATE_DECL)
9955 	    decl = DECL_TEMPLATE_RESULT (decl);
9956 
9957 	  /* Attempt to merge the declarations.  This can fail, in
9958 	     the case of some invalid specialization declarations.  */
9959 	  pushed_scope = push_scope (ctype);
9960 	  ok = duplicate_decls (decl, old_decl, friendp);
9961 	  if (pushed_scope)
9962 	    pop_scope (pushed_scope);
9963 	  if (!ok)
9964 	    {
9965 	      error ("no %q#D member function declared in class %qT",
9966 		     decl, ctype);
9967 	      return NULL_TREE;
9968 	    }
9969 	  if (ok == error_mark_node)
9970 	    return NULL_TREE;
9971 	  return old_decl;
9972 	}
9973     }
9974 
9975   if (DECL_CONSTRUCTOR_P (decl) && !grok_ctor_properties (ctype, decl))
9976     return NULL_TREE;
9977 
9978   if (ctype == NULL_TREE || check)
9979     return decl;
9980 
9981   if (virtualp)
9982     DECL_VIRTUAL_P (decl) = 1;
9983 
9984   return decl;
9985 }
9986 
9987 /* decl is a FUNCTION_DECL.
9988    specifiers are the parsed virt-specifiers.
9989 
9990    Set flags to reflect the virt-specifiers.
9991 
9992    Returns decl.  */
9993 
9994 static tree
set_virt_specifiers(tree decl,cp_virt_specifiers specifiers)9995 set_virt_specifiers (tree decl, cp_virt_specifiers specifiers)
9996 {
9997   if (decl == NULL_TREE)
9998     return decl;
9999   if (specifiers & VIRT_SPEC_OVERRIDE)
10000     DECL_OVERRIDE_P (decl) = 1;
10001   if (specifiers & VIRT_SPEC_FINAL)
10002     DECL_FINAL_P (decl) = 1;
10003   return decl;
10004 }
10005 
10006 /* DECL is a VAR_DECL for a static data member.  Set flags to reflect
10007    the linkage that DECL will receive in the object file.  */
10008 
10009 static void
set_linkage_for_static_data_member(tree decl)10010 set_linkage_for_static_data_member (tree decl)
10011 {
10012   /* A static data member always has static storage duration and
10013      external linkage.  Note that static data members are forbidden in
10014      local classes -- the only situation in which a class has
10015      non-external linkage.  */
10016   TREE_PUBLIC (decl) = 1;
10017   TREE_STATIC (decl) = 1;
10018   /* For non-template classes, static data members are always put
10019      out in exactly those files where they are defined, just as
10020      with ordinary namespace-scope variables.  */
10021   if (!processing_template_decl)
10022     DECL_INTERFACE_KNOWN (decl) = 1;
10023 }
10024 
10025 /* Create a VAR_DECL named NAME with the indicated TYPE.
10026 
10027    If SCOPE is non-NULL, it is the class type or namespace containing
10028    the variable.  If SCOPE is NULL, the variable should is created in
10029    the innermost enclosing scope.  */
10030 
10031 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)10032 grokvardecl (tree type,
10033 	     tree name,
10034 	     tree orig_declarator,
10035 	     const cp_decl_specifier_seq *declspecs,
10036 	     int initialized,
10037 	     int type_quals,
10038 	     int inlinep,
10039 	     bool conceptp,
10040 	     int template_count,
10041 	     tree scope,
10042 	     location_t location)
10043 {
10044   tree decl;
10045   tree explicit_scope;
10046 
10047   gcc_assert (!name || identifier_p (name));
10048 
10049   bool constp = (type_quals & TYPE_QUAL_CONST) != 0;
10050   bool volatilep = (type_quals & TYPE_QUAL_VOLATILE) != 0;
10051 
10052   /* Compute the scope in which to place the variable, but remember
10053      whether or not that scope was explicitly specified by the user.   */
10054   explicit_scope = scope;
10055   if (!scope)
10056     {
10057       /* An explicit "extern" specifier indicates a namespace-scope
10058 	 variable.  */
10059       if (declspecs->storage_class == sc_extern)
10060 	scope = current_decl_namespace ();
10061       else if (!at_function_scope_p ())
10062 	scope = current_scope ();
10063     }
10064 
10065   if (scope
10066       && (/* If the variable is a namespace-scope variable declared in a
10067 	     template, we need DECL_LANG_SPECIFIC.  */
10068 	  (TREE_CODE (scope) == NAMESPACE_DECL && processing_template_decl)
10069 	  /* Similarly for namespace-scope variables with language linkage
10070 	     other than C++.  */
10071 	  || (TREE_CODE (scope) == NAMESPACE_DECL
10072 	      && current_lang_name != lang_name_cplusplus)
10073 	  /* Similarly for static data members.  */
10074 	  || TYPE_P (scope)
10075 	  /* Similarly for explicit specializations.  */
10076 	  || (orig_declarator
10077 	      && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)))
10078     decl = build_lang_decl_loc (location, VAR_DECL, name, type);
10079   else
10080     decl = build_decl (location, VAR_DECL, name, type);
10081 
10082   if (explicit_scope && TREE_CODE (explicit_scope) == NAMESPACE_DECL)
10083     set_decl_namespace (decl, explicit_scope, 0);
10084   else
10085     DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
10086 
10087   if (declspecs->storage_class == sc_extern)
10088     {
10089       DECL_THIS_EXTERN (decl) = 1;
10090       DECL_EXTERNAL (decl) = !initialized;
10091     }
10092 
10093   if (DECL_CLASS_SCOPE_P (decl))
10094     {
10095       set_linkage_for_static_data_member (decl);
10096       /* This function is only called with out-of-class definitions.  */
10097       DECL_EXTERNAL (decl) = 0;
10098       check_class_member_definition_namespace (decl);
10099     }
10100   /* At top level, either `static' or no s.c. makes a definition
10101      (perhaps tentative), and absence of `static' makes it public.  */
10102   else if (toplevel_bindings_p ())
10103     {
10104       TREE_PUBLIC (decl) = (declspecs->storage_class != sc_static
10105 			    && (DECL_THIS_EXTERN (decl)
10106 				|| ! constp
10107 				|| volatilep
10108 				|| inlinep));
10109       TREE_STATIC (decl) = ! DECL_EXTERNAL (decl);
10110     }
10111   /* Not at top level, only `static' makes a static definition.  */
10112   else
10113     {
10114       TREE_STATIC (decl) = declspecs->storage_class == sc_static;
10115       TREE_PUBLIC (decl) = DECL_EXTERNAL (decl);
10116     }
10117 
10118   if (decl_spec_seq_has_spec_p (declspecs, ds_thread))
10119     {
10120       if (DECL_EXTERNAL (decl) || TREE_STATIC (decl))
10121 	{
10122 	  CP_DECL_THREAD_LOCAL_P (decl) = true;
10123 	  if (!processing_template_decl)
10124 	    set_decl_tls_model (decl, decl_default_tls_model (decl));
10125 	}
10126       if (declspecs->gnu_thread_keyword_p)
10127 	SET_DECL_GNU_TLS_P (decl);
10128     }
10129 
10130   /* If the type of the decl has no linkage, make sure that we'll
10131      notice that in mark_used.  */
10132   if (cxx_dialect > cxx98
10133       && decl_linkage (decl) != lk_none
10134       && DECL_LANG_SPECIFIC (decl) == NULL
10135       && !DECL_EXTERN_C_P (decl)
10136       && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
10137     retrofit_lang_decl (decl);
10138 
10139   if (TREE_PUBLIC (decl))
10140     {
10141       /* [basic.link]: A name with no linkage (notably, the name of a class
10142 	 or enumeration declared in a local scope) shall not be used to
10143 	 declare an entity with linkage.
10144 
10145 	 DR 757 relaxes this restriction for C++0x.  */
10146       if (cxx_dialect < cxx11)
10147 	no_linkage_error (decl);
10148     }
10149   else
10150     DECL_INTERFACE_KNOWN (decl) = 1;
10151 
10152   if (DECL_NAME (decl)
10153       && MAIN_NAME_P (DECL_NAME (decl))
10154       && scope == global_namespace)
10155     error_at (DECL_SOURCE_LOCATION (decl),
10156 	      "cannot declare %<::main%> to be a global variable");
10157 
10158   /* Check that the variable can be safely declared as a concept.
10159      Note that this also forbids explicit specializations.  */
10160   if (conceptp)
10161     {
10162       if (!processing_template_decl)
10163         {
10164           error_at (declspecs->locations[ds_concept],
10165 		    "a non-template variable cannot be %<concept%>");
10166           return NULL_TREE;
10167         }
10168       else
10169         DECL_DECLARED_CONCEPT_P (decl) = true;
10170       if (!same_type_ignoring_top_level_qualifiers_p (type, boolean_type_node))
10171 	error_at (declspecs->locations[ds_type_spec],
10172 		  "concept must have type %<bool%>");
10173       if (TEMPLATE_PARMS_CONSTRAINTS (current_template_parms))
10174         {
10175           error_at (location, "a variable concept cannot be constrained");
10176           TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = NULL_TREE;
10177         }
10178     }
10179   else if (flag_concepts
10180 	   && processing_template_decl > template_class_depth (scope))
10181     {
10182       tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
10183       tree ci = build_constraints (reqs, NULL_TREE);
10184 
10185       set_constraints (decl, ci);
10186     }
10187 
10188   // Handle explicit specializations and instantiations of variable templates.
10189   if (orig_declarator)
10190     decl = check_explicit_specialization (orig_declarator, decl,
10191 					  template_count, conceptp * 8);
10192 
10193   return decl != error_mark_node ? decl : NULL_TREE;
10194 }
10195 
10196 /* Create and return a canonical pointer to member function type, for
10197    TYPE, which is a POINTER_TYPE to a METHOD_TYPE.  */
10198 
10199 tree
build_ptrmemfunc_type(tree type)10200 build_ptrmemfunc_type (tree type)
10201 {
10202   tree field, fields;
10203   tree t;
10204 
10205   if (type == error_mark_node)
10206     return type;
10207 
10208   /* Make sure that we always have the unqualified pointer-to-member
10209      type first.  */
10210   if (cp_cv_quals quals = cp_type_quals (type))
10211     {
10212       tree unqual = build_ptrmemfunc_type (TYPE_MAIN_VARIANT (type));
10213       return cp_build_qualified_type (unqual, quals);
10214     }
10215 
10216   /* If a canonical type already exists for this type, use it.  We use
10217      this method instead of type_hash_canon, because it only does a
10218      simple equality check on the list of field members.  */
10219 
10220   t = TYPE_PTRMEMFUNC_TYPE (type);
10221   if (t)
10222     return t;
10223 
10224   t = make_node (RECORD_TYPE);
10225 
10226   /* Let the front end know this is a pointer to member function.  */
10227   TYPE_PTRMEMFUNC_FLAG (t) = 1;
10228 
10229   field = build_decl (input_location, FIELD_DECL, pfn_identifier, type);
10230   DECL_NONADDRESSABLE_P (field) = 1;
10231   fields = field;
10232 
10233   field = build_decl (input_location, FIELD_DECL, delta_identifier,
10234 		      delta_type_node);
10235   DECL_NONADDRESSABLE_P (field) = 1;
10236   DECL_CHAIN (field) = fields;
10237   fields = field;
10238 
10239   finish_builtin_struct (t, "__ptrmemfunc_type", fields, ptr_type_node);
10240 
10241   /* Zap out the name so that the back end will give us the debugging
10242      information for this anonymous RECORD_TYPE.  */
10243   TYPE_NAME (t) = NULL_TREE;
10244 
10245   /* Cache this pointer-to-member type so that we can find it again
10246      later.  */
10247   TYPE_PTRMEMFUNC_TYPE (type) = t;
10248 
10249   if (TYPE_STRUCTURAL_EQUALITY_P (type))
10250     SET_TYPE_STRUCTURAL_EQUALITY (t);
10251   else if (TYPE_CANONICAL (type) != type)
10252     TYPE_CANONICAL (t) = build_ptrmemfunc_type (TYPE_CANONICAL (type));
10253 
10254   return t;
10255 }
10256 
10257 /* Create and return a pointer to data member type.  */
10258 
10259 tree
build_ptrmem_type(tree class_type,tree member_type)10260 build_ptrmem_type (tree class_type, tree member_type)
10261 {
10262   if (TREE_CODE (member_type) == METHOD_TYPE)
10263     {
10264       cp_cv_quals quals = type_memfn_quals (member_type);
10265       cp_ref_qualifier rqual = type_memfn_rqual (member_type);
10266       member_type = build_memfn_type (member_type, class_type, quals, rqual);
10267       return build_ptrmemfunc_type (build_pointer_type (member_type));
10268     }
10269   else
10270     {
10271       gcc_assert (TREE_CODE (member_type) != FUNCTION_TYPE);
10272       return build_offset_type (class_type, member_type);
10273     }
10274 }
10275 
10276 /* DECL is a VAR_DECL defined in-class, whose TYPE is also given.
10277    Check to see that the definition is valid.  Issue appropriate error
10278    messages.  */
10279 
10280 static void
check_static_variable_definition(tree decl,tree type)10281 check_static_variable_definition (tree decl, tree type)
10282 {
10283   /* Avoid redundant diagnostics on out-of-class definitions.  */
10284   if (!current_class_type || !TYPE_BEING_DEFINED (current_class_type))
10285     ;
10286   /* Can't check yet if we don't know the type.  */
10287   else if (dependent_type_p (type))
10288     ;
10289   /* If DECL is declared constexpr, we'll do the appropriate checks
10290      in check_initializer.  Similarly for inline static data members.  */
10291   else if (DECL_P (decl)
10292       && (DECL_DECLARED_CONSTEXPR_P (decl)
10293 	  || undeduced_auto_decl (decl)
10294 	  || DECL_VAR_DECLARED_INLINE_P (decl)))
10295     ;
10296   else if (cxx_dialect >= cxx11 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
10297     {
10298       if (!COMPLETE_TYPE_P (type))
10299 	error_at (DECL_SOURCE_LOCATION (decl),
10300 		  "in-class initialization of static data member %q#D of "
10301 		  "incomplete type", decl);
10302       else if (literal_type_p (type))
10303 	permerror (DECL_SOURCE_LOCATION (decl),
10304 		   "%<constexpr%> needed for in-class initialization of "
10305 		   "static data member %q#D of non-integral type", decl);
10306       else
10307 	error_at (DECL_SOURCE_LOCATION (decl),
10308 		  "in-class initialization of static data member %q#D of "
10309 		  "non-literal type", decl);
10310     }
10311   /* Motion 10 at San Diego: If a static const integral data member is
10312      initialized with an integral constant expression, the initializer
10313      may appear either in the declaration (within the class), or in
10314      the definition, but not both.  If it appears in the class, the
10315      member is a member constant.  The file-scope definition is always
10316      required.  */
10317   else if (!ARITHMETIC_TYPE_P (type) && TREE_CODE (type) != ENUMERAL_TYPE)
10318     error_at (DECL_SOURCE_LOCATION (decl),
10319 	      "invalid in-class initialization of static data member "
10320 	      "of non-integral type %qT",
10321 	      type);
10322   else if (!CP_TYPE_CONST_P (type))
10323     error_at (DECL_SOURCE_LOCATION (decl),
10324 	      "ISO C++ forbids in-class initialization of non-const "
10325 	      "static member %qD",
10326 	      decl);
10327   else if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type))
10328     pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
10329 	     "ISO C++ forbids initialization of member constant "
10330 	     "%qD of non-integral type %qT", decl, type);
10331 }
10332 
10333 /* *expr_p is part of the TYPE_SIZE of a variably-sized array.  If any
10334    SAVE_EXPRs in *expr_p wrap expressions with side-effects, break those
10335    expressions out into temporary variables so that walk_tree doesn't
10336    step into them (c++/15764).  */
10337 
10338 static tree
stabilize_save_expr_r(tree * expr_p,int * walk_subtrees,void * data)10339 stabilize_save_expr_r (tree *expr_p, int *walk_subtrees, void *data)
10340 {
10341   hash_set<tree> *pset = (hash_set<tree> *)data;
10342   tree expr = *expr_p;
10343   if (TREE_CODE (expr) == SAVE_EXPR)
10344     {
10345       tree op = TREE_OPERAND (expr, 0);
10346       cp_walk_tree (&op, stabilize_save_expr_r, data, pset);
10347       if (TREE_SIDE_EFFECTS (op))
10348 	TREE_OPERAND (expr, 0) = get_temp_regvar (TREE_TYPE (op), op);
10349       *walk_subtrees = 0;
10350     }
10351   else if (!EXPR_P (expr) || !TREE_SIDE_EFFECTS (expr))
10352     *walk_subtrees = 0;
10353   return NULL;
10354 }
10355 
10356 /* Entry point for the above.  */
10357 
10358 static void
stabilize_vla_size(tree size)10359 stabilize_vla_size (tree size)
10360 {
10361   hash_set<tree> pset;
10362   /* Break out any function calls into temporary variables.  */
10363   cp_walk_tree (&size, stabilize_save_expr_r, &pset, &pset);
10364 }
10365 
10366 /* Reduce a SIZEOF_EXPR to its value.  */
10367 
10368 tree
fold_sizeof_expr(tree t)10369 fold_sizeof_expr (tree t)
10370 {
10371   tree r;
10372   if (SIZEOF_EXPR_TYPE_P (t))
10373     r = cxx_sizeof_or_alignof_type (EXPR_LOCATION (t),
10374 				    TREE_TYPE (TREE_OPERAND (t, 0)),
10375 				    SIZEOF_EXPR, false, false);
10376   else if (TYPE_P (TREE_OPERAND (t, 0)))
10377     r = cxx_sizeof_or_alignof_type (EXPR_LOCATION (t),
10378 				    TREE_OPERAND (t, 0), SIZEOF_EXPR,
10379 				    false, false);
10380   else
10381     r = cxx_sizeof_or_alignof_expr (EXPR_LOCATION (t),
10382 				    TREE_OPERAND (t, 0), SIZEOF_EXPR,
10383 				    false);
10384   if (r == error_mark_node)
10385     r = size_one_node;
10386   return r;
10387 }
10388 
10389 /* Given the SIZE (i.e., number of elements) in an array, compute
10390    an appropriate index type for the array.  If non-NULL, NAME is
10391    the name of the entity being declared.  */
10392 
10393 static tree
compute_array_index_type_loc(location_t name_loc,tree name,tree size,tsubst_flags_t complain)10394 compute_array_index_type_loc (location_t name_loc, tree name, tree size,
10395 			      tsubst_flags_t complain)
10396 {
10397   if (error_operand_p (size))
10398     return error_mark_node;
10399 
10400   /* The type of the index being computed.  */
10401   tree itype;
10402 
10403   /* The original numeric size as seen in the source code before
10404      conversion to size_t.  */
10405   tree origsize = size;
10406 
10407   location_t loc = cp_expr_loc_or_loc (size, name ? name_loc : input_location);
10408 
10409   if (!type_dependent_expression_p (size))
10410     {
10411       origsize = size = mark_rvalue_use (size);
10412 
10413       if (cxx_dialect < cxx11 && TREE_CODE (size) == NOP_EXPR
10414 	  && TREE_SIDE_EFFECTS (size))
10415 	/* In C++98, we mark a non-constant array bound with a magic
10416 	   NOP_EXPR with TREE_SIDE_EFFECTS; don't fold in that case.  */;
10417       else
10418 	{
10419 	  size = build_converted_constant_expr (size_type_node, size, complain);
10420 	  /* Pedantically a constant expression is required here and so
10421 	     __builtin_is_constant_evaluated () should fold to true if it
10422 	     is successfully folded into a constant.  */
10423 	  size = fold_non_dependent_expr (size, complain,
10424 					  /*manifestly_const_eval=*/true);
10425 
10426 	  if (!TREE_CONSTANT (size))
10427 	    size = origsize;
10428 	}
10429 
10430       if (error_operand_p (size))
10431 	return error_mark_node;
10432 
10433       /* The array bound must be an integer type.  */
10434       tree type = TREE_TYPE (size);
10435       if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
10436 	{
10437 	  if (!(complain & tf_error))
10438 	    return error_mark_node;
10439 	  if (name)
10440 	    error_at (loc, "size of array %qD has non-integral type %qT",
10441 		      name, type);
10442 	  else
10443 	    error_at (loc, "size of array has non-integral type %qT", type);
10444 	  size = integer_one_node;
10445 	}
10446     }
10447 
10448   /* A type is dependent if it is...an array type constructed from any
10449      dependent type or whose size is specified by a constant expression
10450      that is value-dependent.  */
10451   /* We can only call value_dependent_expression_p on integral constant
10452      expressions; treat non-constant expressions as dependent, too.  */
10453   if (processing_template_decl
10454       && (!TREE_CONSTANT (size) || value_dependent_expression_p (size)))
10455     {
10456       /* We cannot do any checking for a SIZE that isn't known to be
10457 	 constant. Just build the index type and mark that it requires
10458 	 structural equality checks.  */
10459       itype = build_index_type (build_min (MINUS_EXPR, sizetype,
10460 					   size, size_one_node));
10461       TYPE_DEPENDENT_P (itype) = 1;
10462       TYPE_DEPENDENT_P_VALID (itype) = 1;
10463       SET_TYPE_STRUCTURAL_EQUALITY (itype);
10464       return itype;
10465     }
10466 
10467   if (TREE_CODE (size) != INTEGER_CST)
10468     {
10469       tree folded = cp_fully_fold (size);
10470       if (TREE_CODE (folded) == INTEGER_CST)
10471 	{
10472 	  if (name)
10473 	    pedwarn (loc, OPT_Wpedantic, "size of array %qD is not an "
10474 		     "integral constant-expression", name);
10475 	  else
10476 	    pedwarn (loc, OPT_Wpedantic,
10477 		     "size of array is not an integral constant-expression");
10478 	}
10479       if (TREE_CONSTANT (size) && !TREE_CONSTANT (folded))
10480 	/* We might have lost the TREE_CONSTANT flag e.g. when we are
10481 	   folding a conversion from a pointer to integral type.  In that
10482 	   case issue an error below and don't treat this as a VLA.  */;
10483       else
10484 	/* Use the folded result for VLAs, too; it will have resolved
10485 	   SIZEOF_EXPR.  */
10486 	size = folded;
10487     }
10488 
10489   /* Normally, the array-bound will be a constant.  */
10490   if (TREE_CODE (size) == INTEGER_CST)
10491     {
10492       /* The size to use in diagnostics that reflects the constant
10493 	 size used in the source, rather than SIZE massaged above.  */
10494       tree diagsize = size;
10495 
10496       /* If the original size before conversion to size_t was signed
10497 	 and negative, convert it to ssizetype to restore the sign.  */
10498       if (!TYPE_UNSIGNED (TREE_TYPE (origsize))
10499 	  && TREE_CODE (size) == INTEGER_CST
10500 	  && tree_int_cst_sign_bit (size))
10501 	{
10502 	  diagsize = fold_convert (ssizetype, size);
10503 
10504 	  /* Clear the overflow bit that may have been set as a result
10505 	     of the conversion from the sizetype of the new size to
10506 	     ssizetype.  */
10507 	  TREE_OVERFLOW (diagsize) = false;
10508 	}
10509 
10510       /* Verify that the array has a positive number of elements
10511 	 and issue the appropriate diagnostic if it doesn't.  */
10512       if (!valid_array_size_p (loc, diagsize, name, (complain & tf_error)))
10513 	{
10514 	  if (!(complain & tf_error))
10515 	    return error_mark_node;
10516 	  size = integer_one_node;
10517 	}
10518       /* As an extension we allow zero-sized arrays.  */
10519       else if (integer_zerop (size))
10520 	{
10521 	  if (!(complain & tf_error))
10522 	    /* We must fail if performing argument deduction (as
10523 	       indicated by the state of complain), so that
10524 	       another substitution can be found.  */
10525 	    return error_mark_node;
10526 	  else if (name)
10527 	    pedwarn (loc, OPT_Wpedantic,
10528 		     "ISO C++ forbids zero-size array %qD", name);
10529 	  else
10530 	    pedwarn (loc, OPT_Wpedantic,
10531 		     "ISO C++ forbids zero-size array");
10532 	}
10533     }
10534   else if (TREE_CONSTANT (size)
10535 	   /* We don't allow VLAs at non-function scopes, or during
10536 	      tentative template substitution.  */
10537 	   || !at_function_scope_p ()
10538 	   || !(complain & tf_error))
10539     {
10540       if (!(complain & tf_error))
10541 	return error_mark_node;
10542       /* `(int) &fn' is not a valid array bound.  */
10543       if (name)
10544 	error_at (loc,
10545 		  "size of array %qD is not an integral constant-expression",
10546 		  name);
10547       else
10548 	error_at (loc, "size of array is not an integral constant-expression");
10549       size = integer_one_node;
10550     }
10551   else if (pedantic && warn_vla != 0)
10552     {
10553       if (name)
10554 	pedwarn (name_loc, OPT_Wvla,
10555 		 "ISO C++ forbids variable length array %qD", name);
10556       else
10557 	pedwarn (input_location, OPT_Wvla,
10558 		 "ISO C++ forbids variable length array");
10559     }
10560   else if (warn_vla > 0)
10561     {
10562       if (name)
10563 	warning_at (name_loc, OPT_Wvla,
10564 		    "variable length array %qD is used", name);
10565       else
10566 	warning (OPT_Wvla,
10567                  "variable length array is used");
10568     }
10569 
10570   if (processing_template_decl && !TREE_CONSTANT (size))
10571     /* A variable sized array.  */
10572     itype = build_min (MINUS_EXPR, sizetype, size, integer_one_node);
10573   else
10574     {
10575       /* Compute the index of the largest element in the array.  It is
10576 	 one less than the number of elements in the array.  We save
10577 	 and restore PROCESSING_TEMPLATE_DECL so that computations in
10578 	 cp_build_binary_op will be appropriately folded.  */
10579       {
10580 	processing_template_decl_sentinel s;
10581 	itype = cp_build_binary_op (input_location,
10582 				    MINUS_EXPR,
10583 				    cp_convert (ssizetype, size, complain),
10584 				    cp_convert (ssizetype, integer_one_node,
10585 						complain),
10586 				    complain);
10587 	itype = maybe_constant_value (itype);
10588       }
10589 
10590       if (!TREE_CONSTANT (itype))
10591 	{
10592 	  /* A variable sized array.  */
10593 	  itype = variable_size (itype);
10594 
10595 	  stabilize_vla_size (itype);
10596 
10597 	  if (sanitize_flags_p (SANITIZE_VLA)
10598 	      && current_function_decl != NULL_TREE)
10599 	    {
10600 	      /* We have to add 1 -- in the ubsan routine we generate
10601 		 LE_EXPR rather than LT_EXPR.  */
10602 	      tree t = fold_build2 (PLUS_EXPR, TREE_TYPE (itype), itype,
10603 				    build_one_cst (TREE_TYPE (itype)));
10604 	      t = ubsan_instrument_vla (input_location, t);
10605 	      finish_expr_stmt (t);
10606 	    }
10607 	}
10608       /* Make sure that there was no overflow when creating to a signed
10609 	 index type.  (For example, on a 32-bit machine, an array with
10610 	 size 2^32 - 1 is too big.)  */
10611       else if (TREE_CODE (itype) == INTEGER_CST
10612 	       && TREE_OVERFLOW (itype))
10613 	{
10614 	  if (!(complain & tf_error))
10615 	    return error_mark_node;
10616 	  error ("overflow in array dimension");
10617 	  TREE_OVERFLOW (itype) = 0;
10618 	}
10619     }
10620 
10621   /* Create and return the appropriate index type.  */
10622   itype = build_index_type (itype);
10623 
10624   /* If the index type were dependent, we would have returned early, so
10625      remember that it isn't.  */
10626   TYPE_DEPENDENT_P (itype) = 0;
10627   TYPE_DEPENDENT_P_VALID (itype) = 1;
10628   return itype;
10629 }
10630 
10631 tree
compute_array_index_type(tree name,tree size,tsubst_flags_t complain)10632 compute_array_index_type (tree name, tree size, tsubst_flags_t complain)
10633 {
10634   return compute_array_index_type_loc (input_location, name, size, complain);
10635 }
10636 
10637 /* Returns the scope (if any) in which the entity declared by
10638    DECLARATOR will be located.  If the entity was declared with an
10639    unqualified name, NULL_TREE is returned.  */
10640 
10641 tree
get_scope_of_declarator(const cp_declarator * declarator)10642 get_scope_of_declarator (const cp_declarator *declarator)
10643 {
10644   while (declarator && declarator->kind != cdk_id)
10645     declarator = declarator->declarator;
10646 
10647   /* If the declarator-id is a SCOPE_REF, the scope in which the
10648      declaration occurs is the first operand.  */
10649   if (declarator
10650       && declarator->u.id.qualifying_scope)
10651     return declarator->u.id.qualifying_scope;
10652 
10653   /* Otherwise, the declarator is not a qualified name; the entity will
10654      be declared in the current scope.  */
10655   return NULL_TREE;
10656 }
10657 
10658 /* Returns an ARRAY_TYPE for an array with SIZE elements of the
10659    indicated TYPE.  If non-NULL, NAME is the NAME of the declaration
10660    with this type.  */
10661 
10662 static tree
create_array_type_for_decl(tree name,tree type,tree size,location_t loc)10663 create_array_type_for_decl (tree name, tree type, tree size, location_t loc)
10664 {
10665   tree itype = NULL_TREE;
10666 
10667   /* If things have already gone awry, bail now.  */
10668   if (type == error_mark_node || size == error_mark_node)
10669     return error_mark_node;
10670 
10671   /* 8.3.4/1: If the type of the identifier of D contains the auto
10672      type-specifier, the program is ill-formed.  */
10673   if (type_uses_auto (type))
10674     {
10675       if (name)
10676 	error_at (loc, "%qD declared as array of %qT", name, type);
10677       else
10678 	error ("creating array of %qT", type);
10679       return error_mark_node;
10680     }
10681 
10682   /* If there are some types which cannot be array elements,
10683      issue an error-message and return.  */
10684   switch (TREE_CODE (type))
10685     {
10686     case VOID_TYPE:
10687       if (name)
10688 	error_at (loc, "declaration of %qD as array of void", name);
10689       else
10690         error ("creating array of void");
10691       return error_mark_node;
10692 
10693     case FUNCTION_TYPE:
10694       if (name)
10695 	error_at (loc, "declaration of %qD as array of functions", name);
10696       else
10697         error ("creating array of functions");
10698       return error_mark_node;
10699 
10700     case REFERENCE_TYPE:
10701       if (name)
10702 	error_at (loc, "declaration of %qD as array of references", name);
10703       else
10704         error ("creating array of references");
10705       return error_mark_node;
10706 
10707     case METHOD_TYPE:
10708       if (name)
10709 	error_at (loc, "declaration of %qD as array of function members",
10710 		  name);
10711       else
10712         error ("creating array of function members");
10713       return error_mark_node;
10714 
10715     default:
10716       break;
10717     }
10718 
10719   if (!verify_type_context (name ? loc : input_location,
10720 			    TCTX_ARRAY_ELEMENT, type))
10721     return error_mark_node;
10722 
10723   /* [dcl.array]
10724 
10725      The constant expressions that specify the bounds of the arrays
10726      can be omitted only for the first member of the sequence.  */
10727   if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type))
10728     {
10729       if (name)
10730 	error_at (loc, "declaration of %qD as multidimensional array must "
10731 		  "have bounds for all dimensions except the first",
10732 		  name);
10733       else
10734 	error ("multidimensional array must have bounds for all "
10735 	       "dimensions except the first");
10736 
10737       return error_mark_node;
10738     }
10739 
10740   /* Figure out the index type for the array.  */
10741   if (size)
10742     itype = compute_array_index_type_loc (loc, name, size,
10743 					  tf_warning_or_error);
10744 
10745   /* [dcl.array]
10746      T is called the array element type; this type shall not be [...] an
10747      abstract class type.  */
10748   abstract_virtuals_error (name, type);
10749 
10750   return build_cplus_array_type (type, itype);
10751 }
10752 
10753 /* Returns the smallest location that is not UNKNOWN_LOCATION.  */
10754 
10755 static location_t
min_location(location_t loca,location_t locb)10756 min_location (location_t loca, location_t locb)
10757 {
10758   if (loca == UNKNOWN_LOCATION
10759       || (locb != UNKNOWN_LOCATION
10760 	  && linemap_location_before_p (line_table, locb, loca)))
10761     return locb;
10762   return loca;
10763 }
10764 
10765 /* Returns the smallest location != UNKNOWN_LOCATION among the
10766    three stored in LOCATIONS[ds_const], LOCATIONS[ds_volatile],
10767    and LOCATIONS[ds_restrict].  */
10768 
10769 static location_t
smallest_type_quals_location(int type_quals,const location_t * locations)10770 smallest_type_quals_location (int type_quals, const location_t* locations)
10771 {
10772   location_t loc = UNKNOWN_LOCATION;
10773 
10774   if (type_quals & TYPE_QUAL_CONST)
10775     loc = locations[ds_const];
10776 
10777   if (type_quals & TYPE_QUAL_VOLATILE)
10778     loc = min_location (loc, locations[ds_volatile]);
10779 
10780   if (type_quals & TYPE_QUAL_RESTRICT)
10781     loc = min_location (loc, locations[ds_restrict]);
10782 
10783   return loc;
10784 }
10785 
10786 /* Returns the smallest among the latter and locations[ds_type_spec].  */
10787 
10788 static location_t
smallest_type_location(int type_quals,const location_t * locations)10789 smallest_type_location (int type_quals, const location_t* locations)
10790 {
10791   location_t loc = smallest_type_quals_location (type_quals, locations);
10792   return min_location (loc, locations[ds_type_spec]);
10793 }
10794 
10795 static location_t
smallest_type_location(const cp_decl_specifier_seq * declspecs)10796 smallest_type_location (const cp_decl_specifier_seq *declspecs)
10797 {
10798   int type_quals = get_type_quals (declspecs);
10799   return smallest_type_location (type_quals, declspecs->locations);
10800 }
10801 
10802 /* Check that it's OK to declare a function with the indicated TYPE
10803    and TYPE_QUALS.  SFK indicates the kind of special function (if any)
10804    that this function is.  OPTYPE is the type given in a conversion
10805    operator declaration, or the class type for a constructor/destructor.
10806    Returns the actual return type of the function; that may be different
10807    than TYPE if an error occurs, or for certain special functions.  */
10808 
10809 static tree
check_special_function_return_type(special_function_kind sfk,tree type,tree optype,int type_quals,const location_t * locations)10810 check_special_function_return_type (special_function_kind sfk,
10811 				    tree type,
10812 				    tree optype,
10813 				    int type_quals,
10814 				    const location_t* locations)
10815 {
10816   switch (sfk)
10817     {
10818     case sfk_constructor:
10819       if (type)
10820 	error_at (smallest_type_location (type_quals, locations),
10821 		  "return type specification for constructor invalid");
10822       else if (type_quals != TYPE_UNQUALIFIED)
10823 	error_at (smallest_type_quals_location (type_quals, locations),
10824 		  "qualifiers are not allowed on constructor declaration");
10825 
10826       if (targetm.cxx.cdtor_returns_this ())
10827 	type = build_pointer_type (optype);
10828       else
10829 	type = void_type_node;
10830       break;
10831 
10832     case sfk_destructor:
10833       if (type)
10834 	error_at (smallest_type_location (type_quals, locations),
10835 		  "return type specification for destructor invalid");
10836       else if (type_quals != TYPE_UNQUALIFIED)
10837 	error_at (smallest_type_quals_location (type_quals, locations),
10838 		  "qualifiers are not allowed on destructor declaration");
10839 
10840       /* We can't use the proper return type here because we run into
10841 	 problems with ambiguous bases and covariant returns.  */
10842       if (targetm.cxx.cdtor_returns_this ())
10843 	type = build_pointer_type (void_type_node);
10844       else
10845 	type = void_type_node;
10846       break;
10847 
10848     case sfk_conversion:
10849       if (type)
10850 	error_at (smallest_type_location (type_quals, locations),
10851 		  "return type specified for %<operator %T%>", optype);
10852       else if (type_quals != TYPE_UNQUALIFIED)
10853 	error_at (smallest_type_quals_location (type_quals, locations),
10854 		  "qualifiers are not allowed on declaration of "
10855 		  "%<operator %T%>", optype);
10856 
10857       type = optype;
10858       break;
10859 
10860     case sfk_deduction_guide:
10861       if (type)
10862 	error_at (smallest_type_location (type_quals, locations),
10863 		  "return type specified for deduction guide");
10864       else if (type_quals != TYPE_UNQUALIFIED)
10865 	error_at (smallest_type_quals_location (type_quals, locations),
10866 		  "qualifiers are not allowed on declaration of "
10867 		  "deduction guide");
10868       if (TREE_CODE (optype) == TEMPLATE_TEMPLATE_PARM)
10869 	{
10870 	  error ("template template parameter %qT in declaration of "
10871 		 "deduction guide", optype);
10872 	  type = error_mark_node;
10873 	}
10874       else
10875 	type = make_template_placeholder (CLASSTYPE_TI_TEMPLATE (optype));
10876       for (int i = 0; i < ds_last; ++i)
10877 	if (i != ds_explicit && locations[i])
10878 	  error_at (locations[i],
10879 		    "%<decl-specifier%> in declaration of deduction guide");
10880       break;
10881 
10882     default:
10883       gcc_unreachable ();
10884     }
10885 
10886   return type;
10887 }
10888 
10889 /* A variable or data member (whose unqualified name is IDENTIFIER)
10890    has been declared with the indicated TYPE.  If the TYPE is not
10891    acceptable, issue an error message and return a type to use for
10892    error-recovery purposes.  */
10893 
10894 tree
check_var_type(tree identifier,tree type,location_t loc)10895 check_var_type (tree identifier, tree type, location_t loc)
10896 {
10897   if (VOID_TYPE_P (type))
10898     {
10899       if (!identifier)
10900 	error_at (loc, "unnamed variable or field declared void");
10901       else if (identifier_p (identifier))
10902 	{
10903 	  gcc_assert (!IDENTIFIER_ANY_OP_P (identifier));
10904 	  error_at (loc, "variable or field %qE declared void",
10905 		    identifier);
10906 	}
10907       else
10908 	error_at (loc, "variable or field declared void");
10909       type = error_mark_node;
10910     }
10911 
10912   return type;
10913 }
10914 
10915 /* Handle declaring DECL as an inline variable.  */
10916 
10917 static void
mark_inline_variable(tree decl,location_t loc)10918 mark_inline_variable (tree decl, location_t loc)
10919 {
10920   bool inlinep = true;
10921   if (! toplevel_bindings_p ())
10922     {
10923       error_at (loc, "%<inline%> specifier invalid for variable "
10924 		"%qD declared at block scope", decl);
10925       inlinep = false;
10926     }
10927   else if (cxx_dialect < cxx17)
10928     pedwarn (loc, 0, "inline variables are only available "
10929 	     "with %<-std=c++17%> or %<-std=gnu++17%>");
10930   if (inlinep)
10931     {
10932       retrofit_lang_decl (decl);
10933       SET_DECL_VAR_DECLARED_INLINE_P (decl);
10934     }
10935 }
10936 
10937 
10938 /* Assign a typedef-given name to a class or enumeration type declared
10939    as anonymous at first.  This was split out of grokdeclarator
10940    because it is also used in libcc1.  */
10941 
10942 void
name_unnamed_type(tree type,tree decl)10943 name_unnamed_type (tree type, tree decl)
10944 {
10945   gcc_assert (TYPE_UNNAMED_P (type));
10946 
10947   /* Replace the anonymous name with the real name everywhere.  */
10948   for (tree t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
10949     if (IDENTIFIER_ANON_P (TYPE_IDENTIFIER (t)))
10950       /* We do not rename the debug info representing the unnamed
10951 	 tagged type because the standard says in [dcl.typedef] that
10952 	 the naming applies only for linkage purposes.  */
10953       /*debug_hooks->set_name (t, decl);*/
10954       TYPE_NAME (t) = decl;
10955 
10956   /* If this is a typedef within a template class, the nested
10957      type is a (non-primary) template.  The name for the
10958      template needs updating as well.  */
10959   if (TYPE_LANG_SPECIFIC (type) && CLASSTYPE_TEMPLATE_INFO (type))
10960     DECL_NAME (CLASSTYPE_TI_TEMPLATE (type))
10961       = TYPE_IDENTIFIER (type);
10962 
10963   /* Adjust linkage now that we aren't unnamed anymore.  */
10964   reset_type_linkage (type);
10965 
10966   /* FIXME remangle member functions; member functions of a
10967      type with external linkage have external linkage.  */
10968 
10969   /* Check that our job is done, and that it would fail if we
10970      attempted to do it again.  */
10971   gcc_assert (!TYPE_UNNAMED_P (type));
10972 }
10973 
10974 /* Given declspecs and a declarator (abstract or otherwise), determine
10975    the name and type of the object declared and construct a DECL node
10976    for it.
10977 
10978    DECLSPECS points to the representation of declaration-specifier
10979    sequence that precedes declarator.
10980 
10981    DECL_CONTEXT says which syntactic context this declaration is in:
10982      NORMAL for most contexts.  Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
10983      FUNCDEF for a function definition.  Like NORMAL but a few different
10984       error messages in each case.  Return value may be zero meaning
10985       this definition is too screwy to try to parse.
10986      MEMFUNCDEF for a function definition.  Like FUNCDEF but prepares to
10987       handle member functions (which have FIELD context).
10988       Return value may be zero meaning this definition is too screwy to
10989       try to parse.
10990      PARM for a parameter declaration (either within a function prototype
10991       or before a function body).  Make a PARM_DECL, or return void_type_node.
10992      TPARM for a template parameter declaration.
10993      CATCHPARM for a parameter declaration before a catch clause.
10994      TYPENAME if for a typename (in a cast or sizeof).
10995       Don't make a DECL node; just return the ..._TYPE node.
10996      FIELD for a struct or union field; make a FIELD_DECL.
10997      BITFIELD for a field with specified width.
10998 
10999    INITIALIZED is as for start_decl.
11000 
11001    ATTRLIST is a pointer to the list of attributes, which may be NULL
11002    if there are none; *ATTRLIST may be modified if attributes from inside
11003    the declarator should be applied to the declaration.
11004 
11005    When this function is called, scoping variables (such as
11006    CURRENT_CLASS_TYPE) should reflect the scope in which the
11007    declaration occurs, not the scope in which the new declaration will
11008    be placed.  For example, on:
11009 
11010      void S::f() { ... }
11011 
11012    when grokdeclarator is called for `S::f', the CURRENT_CLASS_TYPE
11013    should not be `S'.
11014 
11015    Returns a DECL (if a declarator is present), a TYPE (if there is no
11016    declarator, in cases like "struct S;"), or the ERROR_MARK_NODE if an
11017    error occurs. */
11018 
11019 tree
grokdeclarator(const cp_declarator * declarator,cp_decl_specifier_seq * declspecs,enum decl_context decl_context,int initialized,tree * attrlist)11020 grokdeclarator (const cp_declarator *declarator,
11021 		cp_decl_specifier_seq *declspecs,
11022 		enum decl_context decl_context,
11023 		int initialized,
11024 		tree* attrlist)
11025 {
11026   tree type = NULL_TREE;
11027   int longlong = 0;
11028   int explicit_intN = 0;
11029   int int_n_alt = 0;
11030   int virtualp, explicitp, friendp, inlinep, staticp;
11031   int explicit_int = 0;
11032   int explicit_char = 0;
11033   int defaulted_int = 0;
11034 
11035   tree typedef_decl = NULL_TREE;
11036   const char *name = NULL;
11037   tree typedef_type = NULL_TREE;
11038   /* True if this declarator is a function definition.  */
11039   bool funcdef_flag = false;
11040   cp_declarator_kind innermost_code = cdk_error;
11041   int bitfield = 0;
11042 #if 0
11043   /* See the code below that used this.  */
11044   tree decl_attr = NULL_TREE;
11045 #endif
11046 
11047   /* Keep track of what sort of function is being processed
11048      so that we can warn about default return values, or explicit
11049      return values which do not match prescribed defaults.  */
11050   special_function_kind sfk = sfk_none;
11051 
11052   tree dname = NULL_TREE;
11053   tree ctor_return_type = NULL_TREE;
11054   enum overload_flags flags = NO_SPECIAL;
11055   /* cv-qualifiers that apply to the declarator, for a declaration of
11056      a member function.  */
11057   cp_cv_quals memfn_quals = TYPE_UNQUALIFIED;
11058   /* virt-specifiers that apply to the declarator, for a declaration of
11059      a member function.  */
11060   cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
11061   /* ref-qualifier that applies to the declarator, for a declaration of
11062      a member function.  */
11063   cp_ref_qualifier rqual = REF_QUAL_NONE;
11064   /* cv-qualifiers that apply to the type specified by the DECLSPECS.  */
11065   int type_quals = get_type_quals (declspecs);
11066   tree raises = NULL_TREE;
11067   int template_count = 0;
11068   tree returned_attrs = NULL_TREE;
11069   tree parms = NULL_TREE;
11070   const cp_declarator *id_declarator;
11071   /* The unqualified name of the declarator; either an
11072      IDENTIFIER_NODE, BIT_NOT_EXPR, or TEMPLATE_ID_EXPR.  */
11073   tree unqualified_id;
11074   /* The class type, if any, in which this entity is located,
11075      or NULL_TREE if none.  Note that this value may be different from
11076      the current class type; for example if an attempt is made to declare
11077      "A::f" inside "B", this value will be "A".  */
11078   tree ctype = current_class_type;
11079   /* The NAMESPACE_DECL for the namespace in which this entity is
11080      located.  If an unqualified name is used to declare the entity,
11081      this value will be NULL_TREE, even if the entity is located at
11082      namespace scope.  */
11083   tree in_namespace = NULL_TREE;
11084   cp_storage_class storage_class;
11085   bool unsigned_p, signed_p, short_p, long_p, thread_p;
11086   bool type_was_error_mark_node = false;
11087   bool parameter_pack_p = declarator ? declarator->parameter_pack_p : false;
11088   bool template_type_arg = false;
11089   bool template_parm_flag = false;
11090   bool typedef_p = decl_spec_seq_has_spec_p (declspecs, ds_typedef);
11091   bool constexpr_p = decl_spec_seq_has_spec_p (declspecs, ds_constexpr);
11092   bool constinit_p = decl_spec_seq_has_spec_p (declspecs, ds_constinit);
11093   bool consteval_p = decl_spec_seq_has_spec_p (declspecs, ds_consteval);
11094   bool late_return_type_p = false;
11095   bool array_parameter_p = false;
11096   tree reqs = NULL_TREE;
11097 
11098   signed_p = decl_spec_seq_has_spec_p (declspecs, ds_signed);
11099   unsigned_p = decl_spec_seq_has_spec_p (declspecs, ds_unsigned);
11100   short_p = decl_spec_seq_has_spec_p (declspecs, ds_short);
11101   long_p = decl_spec_seq_has_spec_p (declspecs, ds_long);
11102   longlong = decl_spec_seq_has_spec_p (declspecs, ds_long_long);
11103   explicit_intN = declspecs->explicit_intN_p;
11104   int_n_alt = declspecs->int_n_alt;
11105   thread_p = decl_spec_seq_has_spec_p (declspecs, ds_thread);
11106 
11107   // Was concept_p specified? Note that ds_concept
11108   // implies ds_constexpr!
11109   bool concept_p = decl_spec_seq_has_spec_p (declspecs, ds_concept);
11110   if (concept_p)
11111     constexpr_p = true;
11112 
11113   if (decl_context == FUNCDEF)
11114     funcdef_flag = true, decl_context = NORMAL;
11115   else if (decl_context == MEMFUNCDEF)
11116     funcdef_flag = true, decl_context = FIELD;
11117   else if (decl_context == BITFIELD)
11118     bitfield = 1, decl_context = FIELD;
11119   else if (decl_context == TEMPLATE_TYPE_ARG)
11120     template_type_arg = true, decl_context = TYPENAME;
11121   else if (decl_context == TPARM)
11122     template_parm_flag = true, decl_context = PARM;
11123 
11124   if (initialized > 1)
11125     funcdef_flag = true;
11126 
11127   location_t typespec_loc = smallest_type_location (type_quals,
11128 						    declspecs->locations);
11129   if (typespec_loc == UNKNOWN_LOCATION)
11130     typespec_loc = input_location;
11131 
11132   location_t id_loc = declarator ? declarator->id_loc : input_location;
11133   if (id_loc == UNKNOWN_LOCATION)
11134     id_loc = input_location;
11135 
11136   /* Look inside a declarator for the name being declared
11137      and get it as a string, for an error message.  */
11138   for (id_declarator = declarator;
11139        id_declarator;
11140        id_declarator = id_declarator->declarator)
11141     {
11142       if (id_declarator->kind != cdk_id)
11143 	innermost_code = id_declarator->kind;
11144 
11145       switch (id_declarator->kind)
11146 	{
11147 	case cdk_function:
11148 	  if (id_declarator->declarator
11149 	      && id_declarator->declarator->kind == cdk_id)
11150 	    {
11151 	      sfk = id_declarator->declarator->u.id.sfk;
11152 	      if (sfk == sfk_destructor)
11153 		flags = DTOR_FLAG;
11154 	    }
11155 	  break;
11156 
11157 	case cdk_id:
11158 	  {
11159 	    tree qualifying_scope = id_declarator->u.id.qualifying_scope;
11160 	    tree decl = id_declarator->u.id.unqualified_name;
11161 	    if (!decl)
11162 	      break;
11163 	    if (qualifying_scope)
11164 	      {
11165 		if (check_for_bare_parameter_packs (qualifying_scope,
11166 						    id_declarator->id_loc))
11167 		  return error_mark_node;
11168 		if (at_function_scope_p ())
11169 		  {
11170 		    /* [dcl.meaning]
11171 
11172 		       A declarator-id shall not be qualified except
11173 		       for ...
11174 
11175 		       None of the cases are permitted in block
11176 		       scope.  */
11177 		    if (qualifying_scope == global_namespace)
11178 		      error ("invalid use of qualified-name %<::%D%>",
11179 			     decl);
11180 		    else if (TYPE_P (qualifying_scope))
11181 		      error ("invalid use of qualified-name %<%T::%D%>",
11182 			     qualifying_scope, decl);
11183 		    else
11184 		      error ("invalid use of qualified-name %<%D::%D%>",
11185 			     qualifying_scope, decl);
11186 		    return error_mark_node;
11187 		  }
11188 		else if (TYPE_P (qualifying_scope))
11189 		  {
11190 		    ctype = qualifying_scope;
11191 		    if (!MAYBE_CLASS_TYPE_P (ctype))
11192 		      {
11193 			error_at (id_declarator->id_loc,
11194 				  "%q#T is not a class or namespace", ctype);
11195 			ctype = NULL_TREE;
11196 		      }
11197 		    else if (innermost_code != cdk_function
11198 			     && current_class_type
11199 			     && !uniquely_derived_from_p (ctype,
11200 							  current_class_type))
11201 		      {
11202 			error_at (id_declarator->id_loc,
11203 				  "invalid use of qualified-name %<%T::%D%>",
11204 				  qualifying_scope, decl);
11205 			return error_mark_node;
11206 		      }
11207 		  }
11208 		else if (TREE_CODE (qualifying_scope) == NAMESPACE_DECL)
11209 		  in_namespace = qualifying_scope;
11210 	      }
11211 	    switch (TREE_CODE (decl))
11212 	      {
11213 	      case BIT_NOT_EXPR:
11214 		{
11215 		  if (innermost_code != cdk_function)
11216 		    {
11217 		      error_at (EXPR_LOCATION (decl),
11218 				"declaration of %qE as non-function", decl);
11219 		      return error_mark_node;
11220 		    }
11221 		  else if (!qualifying_scope
11222 			   && !(current_class_type && at_class_scope_p ()))
11223 		    {
11224 		      error_at (EXPR_LOCATION (decl),
11225 				"declaration of %qE as non-member", decl);
11226 		      return error_mark_node;
11227 		    }
11228 
11229 		  tree type = TREE_OPERAND (decl, 0);
11230 		  if (TYPE_P (type))
11231 		    type = constructor_name (type);
11232 		  name = identifier_to_locale (IDENTIFIER_POINTER (type));
11233 		  dname = decl;
11234 		}
11235 		break;
11236 
11237 	      case TEMPLATE_ID_EXPR:
11238 		{
11239 		  tree fns = TREE_OPERAND (decl, 0);
11240 
11241 		  dname = fns;
11242 		  if (!identifier_p (dname))
11243 		    dname = OVL_NAME (dname);
11244 		}
11245 		/* Fall through.  */
11246 
11247 	      case IDENTIFIER_NODE:
11248 		if (identifier_p (decl))
11249 		  dname = decl;
11250 
11251 		if (IDENTIFIER_KEYWORD_P (dname))
11252 		  {
11253 		    error ("declarator-id missing; using reserved word %qD",
11254 			   dname);
11255 		    name = identifier_to_locale (IDENTIFIER_POINTER (dname));
11256 		  }
11257 		else if (!IDENTIFIER_CONV_OP_P (dname))
11258 		  name = identifier_to_locale (IDENTIFIER_POINTER (dname));
11259 		else
11260 		  {
11261 		    gcc_assert (flags == NO_SPECIAL);
11262 		    flags = TYPENAME_FLAG;
11263 		    sfk = sfk_conversion;
11264 		    tree glob = get_global_binding (dname);
11265 		    if (glob && TREE_CODE (glob) == TYPE_DECL)
11266 		      name = identifier_to_locale (IDENTIFIER_POINTER (dname));
11267 		    else
11268 		      name = "<invalid operator>";
11269 		  }
11270 		break;
11271 
11272 	      default:
11273 		gcc_unreachable ();
11274 	      }
11275 	    break;
11276 	  }
11277 
11278 	case cdk_array:
11279 	case cdk_pointer:
11280 	case cdk_reference:
11281 	case cdk_ptrmem:
11282 	  break;
11283 
11284 	case cdk_decomp:
11285 	  name = "structured binding";
11286 	  break;
11287 
11288 	case cdk_error:
11289 	  return error_mark_node;
11290 
11291 	default:
11292 	  gcc_unreachable ();
11293 	}
11294       if (id_declarator->kind == cdk_id)
11295 	break;
11296     }
11297 
11298   /* [dcl.fct.edf]
11299 
11300      The declarator in a function-definition shall have the form
11301      D1 ( parameter-declaration-clause) ...  */
11302   if (funcdef_flag && innermost_code != cdk_function)
11303     {
11304       error_at (id_loc, "function definition does not declare parameters");
11305       return error_mark_node;
11306     }
11307 
11308   if (flags == TYPENAME_FLAG
11309       && innermost_code != cdk_function
11310       && ! (ctype && !declspecs->any_specifiers_p))
11311     {
11312       error_at (id_loc, "declaration of %qD as non-function", dname);
11313       return error_mark_node;
11314     }
11315 
11316   if (dname && identifier_p (dname))
11317     {
11318       if (UDLIT_OPER_P (dname)
11319 	  && innermost_code != cdk_function)
11320 	{
11321 	  error_at (id_loc, "declaration of %qD as non-function", dname);
11322 	  return error_mark_node;
11323 	}
11324 
11325       if (IDENTIFIER_ANY_OP_P (dname))
11326 	{
11327 	  if (typedef_p)
11328 	    {
11329 	      error_at (id_loc, "declaration of %qD as %<typedef%>", dname);
11330 	      return error_mark_node;
11331 	    }
11332 	  else if (decl_context == PARM || decl_context == CATCHPARM)
11333 	    {
11334 	      error_at (id_loc, "declaration of %qD as parameter", dname);
11335 	      return error_mark_node;
11336 	    }
11337 	}
11338     }
11339 
11340   /* Anything declared one level down from the top level
11341      must be one of the parameters of a function
11342      (because the body is at least two levels down).  */
11343 
11344   /* This heuristic cannot be applied to C++ nodes! Fixed, however,
11345      by not allowing C++ class definitions to specify their parameters
11346      with xdecls (must be spec.d in the parmlist).
11347 
11348      Since we now wait to push a class scope until we are sure that
11349      we are in a legitimate method context, we must set oldcname
11350      explicitly (since current_class_name is not yet alive).
11351 
11352      We also want to avoid calling this a PARM if it is in a namespace.  */
11353 
11354   if (decl_context == NORMAL && !toplevel_bindings_p ())
11355     {
11356       cp_binding_level *b = current_binding_level;
11357       current_binding_level = b->level_chain;
11358       if (current_binding_level != 0 && toplevel_bindings_p ())
11359 	decl_context = PARM;
11360       current_binding_level = b;
11361     }
11362 
11363   if (name == NULL)
11364     name = decl_context == PARM ? "parameter" : "type name";
11365 
11366   if (consteval_p && constexpr_p)
11367     {
11368       error_at (declspecs->locations[ds_consteval],
11369 		"both %qs and %qs specified", "constexpr", "consteval");
11370       return error_mark_node;
11371     }
11372 
11373   if (concept_p && typedef_p)
11374     {
11375       error_at (declspecs->locations[ds_concept],
11376 		"%qs cannot appear in a typedef declaration", "concept");
11377       return error_mark_node;
11378     }
11379 
11380   if (constexpr_p && typedef_p)
11381     {
11382       error_at (declspecs->locations[ds_constexpr],
11383 		"%qs cannot appear in a typedef declaration", "constexpr");
11384       return error_mark_node;
11385     }
11386 
11387   if (consteval_p && typedef_p)
11388     {
11389       error_at (declspecs->locations[ds_consteval],
11390 		"%qs cannot appear in a typedef declaration", "consteval");
11391       return error_mark_node;
11392     }
11393 
11394   if (constinit_p && typedef_p)
11395     {
11396       error_at (declspecs->locations[ds_constinit],
11397 		"%qs cannot appear in a typedef declaration", "constinit");
11398       return error_mark_node;
11399     }
11400 
11401   /* [dcl.spec]/2 "At most one of the constexpr, consteval, and constinit
11402      keywords shall appear in a decl-specifier-seq."  */
11403   if (constinit_p && constexpr_p)
11404     {
11405       gcc_rich_location richloc (declspecs->locations[ds_constinit]);
11406       richloc.add_range (declspecs->locations[ds_constexpr]);
11407       error_at (&richloc,
11408 		"can use at most one of the %<constinit%> and %<constexpr%> "
11409 		"specifiers");
11410       return error_mark_node;
11411     }
11412 
11413   /* If there were multiple types specified in the decl-specifier-seq,
11414      issue an error message.  */
11415   if (declspecs->multiple_types_p)
11416     {
11417       error_at (typespec_loc,
11418 		"two or more data types in declaration of %qs", name);
11419       return error_mark_node;
11420     }
11421 
11422   if (declspecs->conflicting_specifiers_p)
11423     {
11424       error_at (min_location (declspecs->locations[ds_typedef],
11425 			      declspecs->locations[ds_storage_class]),
11426 		"conflicting specifiers in declaration of %qs", name);
11427       return error_mark_node;
11428     }
11429 
11430   /* Extract the basic type from the decl-specifier-seq.  */
11431   type = declspecs->type;
11432   if (type == error_mark_node)
11433     {
11434       type = NULL_TREE;
11435       type_was_error_mark_node = true;
11436     }
11437   cp_warn_deprecated_use (type);
11438   if (type && TREE_CODE (type) == TYPE_DECL)
11439     {
11440       cp_warn_deprecated_use_scopes (CP_DECL_CONTEXT (type));
11441       typedef_decl = type;
11442       type = TREE_TYPE (typedef_decl);
11443       if (DECL_ARTIFICIAL (typedef_decl))
11444 	cp_warn_deprecated_use (type);
11445     }
11446   /* No type at all: default to `int', and set DEFAULTED_INT
11447      because it was not a user-defined typedef.  */
11448   if (type == NULL_TREE)
11449     {
11450       if (signed_p || unsigned_p || long_p || short_p)
11451 	{
11452 	  /* These imply 'int'.  */
11453 	  type = integer_type_node;
11454 	  defaulted_int = 1;
11455 	}
11456       /* If we just have "complex", it is equivalent to "complex double".  */
11457       else if (!longlong && !explicit_intN
11458 	       && decl_spec_seq_has_spec_p (declspecs, ds_complex))
11459 	{
11460 	  type = double_type_node;
11461 	  pedwarn (declspecs->locations[ds_complex], OPT_Wpedantic,
11462 		   "ISO C++ does not support plain %<complex%> meaning "
11463 		   "%<double complex%>");
11464 	}
11465     }
11466   /* Gather flags.  */
11467   explicit_int = declspecs->explicit_int_p;
11468   explicit_char = declspecs->explicit_char_p;
11469 
11470 #if 0
11471   /* See the code below that used this.  */
11472   if (typedef_decl)
11473     decl_attr = DECL_ATTRIBUTES (typedef_decl);
11474 #endif
11475   typedef_type = type;
11476 
11477   if (sfk == sfk_conversion || sfk == sfk_deduction_guide)
11478     ctor_return_type = TREE_TYPE (dname);
11479   else
11480     ctor_return_type = ctype;
11481 
11482   if (sfk != sfk_none)
11483     {
11484       type = check_special_function_return_type (sfk, type,
11485 						 ctor_return_type,
11486 						 type_quals,
11487 						 declspecs->locations);
11488       type_quals = TYPE_UNQUALIFIED;
11489     }
11490   else if (type == NULL_TREE)
11491     {
11492       int is_main;
11493 
11494       explicit_int = -1;
11495 
11496       /* We handle `main' specially here, because 'main () { }' is so
11497 	 common.  With no options, it is allowed.  With -Wreturn-type,
11498 	 it is a warning.  It is only an error with -pedantic-errors.  */
11499       is_main = (funcdef_flag
11500 		 && dname && identifier_p (dname)
11501 		 && MAIN_NAME_P (dname)
11502 		 && ctype == NULL_TREE
11503 		 && in_namespace == NULL_TREE
11504 		 && current_namespace == global_namespace);
11505 
11506       if (type_was_error_mark_node)
11507 	/* We've already issued an error, don't complain more.  */;
11508       else if (in_system_header_at (id_loc) || flag_ms_extensions)
11509 	/* Allow it, sigh.  */;
11510       else if (! is_main)
11511 	permerror (id_loc, "ISO C++ forbids declaration of %qs with no type",
11512 		   name);
11513       else if (pedantic)
11514 	pedwarn (id_loc, OPT_Wpedantic,
11515 		 "ISO C++ forbids declaration of %qs with no type", name);
11516       else
11517 	warning_at (id_loc, OPT_Wreturn_type,
11518 		    "ISO C++ forbids declaration of %qs with no type", name);
11519 
11520       if (type_was_error_mark_node && template_parm_flag)
11521 	/* FIXME we should be able to propagate the error_mark_node as is
11522 	   for other contexts too.  */
11523 	type = error_mark_node;
11524       else
11525 	type = integer_type_node;
11526     }
11527 
11528   ctype = NULL_TREE;
11529 
11530   if (explicit_intN)
11531     {
11532       if (! int_n_enabled_p[declspecs->int_n_idx])
11533 	{
11534 	  error_at (declspecs->locations[ds_type_spec],
11535 		    "%<__int%d%> is not supported by this target",
11536 		    int_n_data[declspecs->int_n_idx].bitsize);
11537 	  explicit_intN = false;
11538 	}
11539       /* Don't pedwarn if the alternate "__intN__" form has been used instead
11540 	 of "__intN".  */
11541       else if (!int_n_alt && pedantic)
11542 	pedwarn (declspecs->locations[ds_type_spec], OPT_Wpedantic,
11543 		 "ISO C++ does not support %<__int%d%> for %qs",
11544 		 int_n_data[declspecs->int_n_idx].bitsize, name);
11545     }
11546 
11547   /* Now process the modifiers that were specified
11548      and check for invalid combinations.  */
11549 
11550   /* Long double is a special combination.  */
11551   if (long_p && !longlong && TYPE_MAIN_VARIANT (type) == double_type_node)
11552     {
11553       long_p = false;
11554       type = cp_build_qualified_type (long_double_type_node,
11555 				      cp_type_quals (type));
11556     }
11557 
11558   /* Check all other uses of type modifiers.  */
11559 
11560   if (unsigned_p || signed_p || long_p || short_p)
11561     {
11562       location_t loc;
11563       const char *key;
11564       if (unsigned_p)
11565 	{
11566 	  key = "unsigned";
11567 	  loc = declspecs->locations[ds_unsigned];
11568 	}
11569       else if (signed_p)
11570 	{
11571 	  key = "signed";
11572 	  loc = declspecs->locations[ds_signed];
11573 	}
11574       else if (longlong)
11575 	{
11576 	  key = "long long";
11577 	  loc = declspecs->locations[ds_long_long];
11578 	}
11579       else if (long_p)
11580 	{
11581 	  key = "long";
11582 	  loc = declspecs->locations[ds_long];
11583 	}
11584       else /* if (short_p) */
11585 	{
11586 	  key = "short";
11587 	  loc = declspecs->locations[ds_short];
11588 	}
11589 
11590       int ok = 0;
11591 
11592       if (signed_p && unsigned_p)
11593 	{
11594 	  gcc_rich_location richloc (declspecs->locations[ds_signed]);
11595 	  richloc.add_range (declspecs->locations[ds_unsigned]);
11596 	  error_at (&richloc,
11597 		    "%<signed%> and %<unsigned%> specified together");
11598 	}
11599       else if (long_p && short_p)
11600 	{
11601 	  gcc_rich_location richloc (declspecs->locations[ds_long]);
11602 	  richloc.add_range (declspecs->locations[ds_short]);
11603 	  error_at (&richloc, "%<long%> and %<short%> specified together");
11604 	}
11605       else if (TREE_CODE (type) != INTEGER_TYPE
11606 	       || type == char8_type_node
11607 	       || type == char16_type_node
11608 	       || type == char32_type_node
11609 	       || ((long_p || short_p)
11610 		   && (explicit_char || explicit_intN)))
11611 	error_at (loc, "%qs specified with %qT", key, type);
11612       else if (!explicit_int && !defaulted_int
11613 	       && !explicit_char && !explicit_intN)
11614 	{
11615 	  if (typedef_decl)
11616 	    {
11617 	      pedwarn (loc, OPT_Wpedantic, "%qs specified with %qT",
11618 		       key, type);
11619 	      ok = !flag_pedantic_errors;
11620 	    }
11621 	  else if (declspecs->decltype_p)
11622 	    error_at (loc, "%qs specified with %<decltype%>", key);
11623 	  else
11624 	    error_at (loc, "%qs specified with %<typeof%>", key);
11625 	}
11626       else
11627 	ok = 1;
11628 
11629       /* Discard the type modifiers if they are invalid.  */
11630       if (! ok)
11631 	{
11632 	  unsigned_p = false;
11633 	  signed_p = false;
11634 	  long_p = false;
11635 	  short_p = false;
11636 	  longlong = 0;
11637 	}
11638     }
11639 
11640   /* Decide whether an integer type is signed or not.
11641      Optionally treat bitfields as signed by default.  */
11642   if (unsigned_p
11643       /* [class.bit]
11644 
11645 	 It is implementation-defined whether a plain (neither
11646 	 explicitly signed or unsigned) char, short, int, or long
11647 	 bit-field is signed or unsigned.
11648 
11649 	 Naturally, we extend this to long long as well.  Note that
11650 	 this does not include wchar_t.  */
11651       || (bitfield && !flag_signed_bitfields
11652 	  && !signed_p
11653 	  /* A typedef for plain `int' without `signed' can be
11654 	     controlled just like plain `int', but a typedef for
11655 	     `signed int' cannot be so controlled.  */
11656 	  && !(typedef_decl
11657 	       && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl))
11658 	  && TREE_CODE (type) == INTEGER_TYPE
11659 	  && !same_type_p (TYPE_MAIN_VARIANT (type), wchar_type_node)))
11660     {
11661       if (explicit_intN)
11662 	type = int_n_trees[declspecs->int_n_idx].unsigned_type;
11663       else if (longlong)
11664 	type = long_long_unsigned_type_node;
11665       else if (long_p)
11666 	type = long_unsigned_type_node;
11667       else if (short_p)
11668 	type = short_unsigned_type_node;
11669       else if (type == char_type_node)
11670 	type = unsigned_char_type_node;
11671       else if (typedef_decl)
11672 	type = unsigned_type_for (type);
11673       else
11674 	type = unsigned_type_node;
11675     }
11676   else if (signed_p && type == char_type_node)
11677     type = signed_char_type_node;
11678   else if (explicit_intN)
11679     type = int_n_trees[declspecs->int_n_idx].signed_type;
11680   else if (longlong)
11681     type = long_long_integer_type_node;
11682   else if (long_p)
11683     type = long_integer_type_node;
11684   else if (short_p)
11685     type = short_integer_type_node;
11686 
11687   if (decl_spec_seq_has_spec_p (declspecs, ds_complex))
11688     {
11689       if (TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
11690 	error_at (declspecs->locations[ds_complex],
11691 		  "complex invalid for %qs", name);
11692       /* If a modifier is specified, the resulting complex is the complex
11693 	 form of TYPE.  E.g, "complex short" is "complex short int".  */
11694       else if (type == integer_type_node)
11695 	type = complex_integer_type_node;
11696       else if (type == float_type_node)
11697 	type = complex_float_type_node;
11698       else if (type == double_type_node)
11699 	type = complex_double_type_node;
11700       else if (type == long_double_type_node)
11701 	type = complex_long_double_type_node;
11702       else
11703 	type = build_complex_type (type);
11704     }
11705 
11706   /* If we're using the injected-class-name to form a compound type or a
11707      declaration, replace it with the underlying class so we don't get
11708      redundant typedefs in the debug output.  But if we are returning the
11709      type unchanged, leave it alone so that it's available to
11710      maybe_get_template_decl_from_type_decl.  */
11711   if (CLASS_TYPE_P (type)
11712       && DECL_SELF_REFERENCE_P (TYPE_NAME (type))
11713       && type == TREE_TYPE (TYPE_NAME (type))
11714       && (declarator || type_quals))
11715     type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
11716 
11717   type_quals |= cp_type_quals (type);
11718   type = cp_build_qualified_type_real
11719     (type, type_quals, ((((typedef_decl && !DECL_ARTIFICIAL (typedef_decl))
11720 			  || declspecs->decltype_p)
11721 			 ? tf_ignore_bad_quals : 0) | tf_warning_or_error));
11722   /* We might have ignored or rejected some of the qualifiers.  */
11723   type_quals = cp_type_quals (type);
11724 
11725   if (cxx_dialect >= cxx17 && type && is_auto (type)
11726       && innermost_code != cdk_function
11727       && id_declarator && declarator != id_declarator)
11728     if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (type))
11729     {
11730       error_at (typespec_loc, "template placeholder type %qT must be followed "
11731 		"by a simple declarator-id", type);
11732       inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here", tmpl);
11733       type = error_mark_node;
11734     }
11735 
11736   staticp = 0;
11737   inlinep = decl_spec_seq_has_spec_p (declspecs, ds_inline);
11738   virtualp =  decl_spec_seq_has_spec_p (declspecs, ds_virtual);
11739   explicitp = decl_spec_seq_has_spec_p (declspecs, ds_explicit);
11740 
11741   storage_class = declspecs->storage_class;
11742   if (storage_class == sc_static)
11743     staticp = 1 + (decl_context == FIELD);
11744 
11745   if (virtualp)
11746     {
11747       if (staticp == 2)
11748 	{
11749 	  gcc_rich_location richloc (declspecs->locations[ds_virtual]);
11750 	  richloc.add_range (declspecs->locations[ds_storage_class]);
11751 	  error_at (&richloc, "member %qD cannot be declared both %<virtual%> "
11752 		    "and %<static%>", dname);
11753 	  storage_class = sc_none;
11754 	  staticp = 0;
11755 	}
11756       if (constexpr_p && cxx_dialect < cxx2a)
11757 	{
11758 	  gcc_rich_location richloc (declspecs->locations[ds_virtual]);
11759 	  richloc.add_range (declspecs->locations[ds_constexpr]);
11760 	  pedwarn (&richloc, OPT_Wpedantic, "member %qD can be declared both "
11761 		   "%<virtual%> and %<constexpr%> only in %<-std=c++2a%> or "
11762 		   "%<-std=gnu++2a%>", dname);
11763 	}
11764     }
11765   friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
11766 
11767   /* Issue errors about use of storage classes for parameters.  */
11768   if (decl_context == PARM)
11769     {
11770       if (typedef_p)
11771 	{
11772 	  error_at (declspecs->locations[ds_typedef],
11773 		    "typedef declaration invalid in parameter declaration");
11774 	  return error_mark_node;
11775 	}
11776       else if (template_parm_flag && storage_class != sc_none)
11777 	{
11778 	  error_at (min_location (declspecs->locations[ds_thread],
11779 				  declspecs->locations[ds_storage_class]),
11780 		    "storage class specified for template parameter %qs",
11781 		    name);
11782 	  return error_mark_node;
11783 	}
11784       else if (storage_class == sc_static
11785 	       || storage_class == sc_extern
11786 	       || thread_p)
11787 	{
11788 	  error_at (min_location (declspecs->locations[ds_thread],
11789 				  declspecs->locations[ds_storage_class]),
11790 		    "storage class specified for parameter %qs", name);
11791 	  return error_mark_node;
11792 	}
11793 
11794       /* Function parameters cannot be concept. */
11795       if (concept_p)
11796 	{
11797 	  error_at (declspecs->locations[ds_concept],
11798 		    "a parameter cannot be declared %qs", "concept");
11799 	  concept_p = 0;
11800 	  constexpr_p = 0;
11801 	}
11802       /* Function parameters cannot be constexpr.  If we saw one, moan
11803          and pretend it wasn't there.  */
11804       else if (constexpr_p)
11805         {
11806           error_at (declspecs->locations[ds_constexpr],
11807 		    "a parameter cannot be declared %qs", "constexpr");
11808           constexpr_p = 0;
11809         }
11810       if (constinit_p)
11811 	{
11812 	  error_at (declspecs->locations[ds_constinit],
11813 		    "a parameter cannot be declared %qs", "constinit");
11814 	  constinit_p = 0;
11815 	}
11816       if (consteval_p)
11817 	{
11818 	  error_at (declspecs->locations[ds_consteval],
11819 		    "a parameter cannot be declared %qs", "consteval");
11820 	  consteval_p = 0;
11821 	}
11822     }
11823 
11824   /* Give error if `virtual' is used outside of class declaration.  */
11825   if (virtualp
11826       && (current_class_name == NULL_TREE || decl_context != FIELD))
11827     {
11828       error_at (declspecs->locations[ds_virtual],
11829 		"%<virtual%> outside class declaration");
11830       virtualp = 0;
11831     }
11832 
11833   if (innermost_code == cdk_decomp)
11834     {
11835       location_t loc = (declarator->kind == cdk_reference
11836 			? declarator->declarator->id_loc : declarator->id_loc);
11837       if (inlinep)
11838 	error_at (declspecs->locations[ds_inline],
11839 		  "structured binding declaration cannot be %qs", "inline");
11840       if (typedef_p)
11841 	error_at (declspecs->locations[ds_typedef],
11842 		  "structured binding declaration cannot be %qs", "typedef");
11843       if (constexpr_p && !concept_p)
11844 	error_at (declspecs->locations[ds_constexpr], "structured "
11845 		  "binding declaration cannot be %qs", "constexpr");
11846       if (consteval_p)
11847 	error_at (declspecs->locations[ds_consteval], "structured "
11848 		  "binding declaration cannot be %qs", "consteval");
11849       if (thread_p && cxx_dialect < cxx2a)
11850 	pedwarn (declspecs->locations[ds_thread], 0,
11851 		 "structured binding declaration can be %qs only in "
11852 		 "%<-std=c++2a%> or %<-std=gnu++2a%>",
11853 		 declspecs->gnu_thread_keyword_p
11854 		 ? "__thread" : "thread_local");
11855       if (concept_p)
11856 	error_at (declspecs->locations[ds_concept],
11857 		  "structured binding declaration cannot be %qs", "concept");
11858       /* [dcl.struct.bind] "A cv that includes volatile is deprecated."  */
11859       if (type_quals & TYPE_QUAL_VOLATILE)
11860 	warning_at (declspecs->locations[ds_volatile], OPT_Wvolatile,
11861 		    "%<volatile%>-qualified structured binding is deprecated");
11862       switch (storage_class)
11863 	{
11864 	case sc_none:
11865 	  break;
11866 	case sc_register:
11867 	  error_at (loc, "structured binding declaration cannot be %qs",
11868 		    "register");
11869 	  break;
11870 	case sc_static:
11871 	  if (cxx_dialect < cxx2a)
11872 	    pedwarn (loc, 0,
11873 		     "structured binding declaration can be %qs only in "
11874 		     "%<-std=c++2a%> or %<-std=gnu++2a%>", "static");
11875 	  break;
11876 	case sc_extern:
11877 	  error_at (loc, "structured binding declaration cannot be %qs",
11878 		    "extern");
11879 	  break;
11880 	case sc_mutable:
11881 	  error_at (loc, "structured binding declaration cannot be %qs",
11882 		    "mutable");
11883 	  break;
11884 	case sc_auto:
11885 	  error_at (loc, "structured binding declaration cannot be "
11886 		    "C++98 %<auto%>");
11887 	  break;
11888 	default:
11889 	  gcc_unreachable ();
11890 	}
11891       if (TREE_CODE (type) != TEMPLATE_TYPE_PARM
11892 	  || TYPE_IDENTIFIER (type) != auto_identifier)
11893 	{
11894 	  if (type != error_mark_node)
11895 	    {
11896 	      error_at (loc, "structured binding declaration cannot have "
11897 			"type %qT", type);
11898 	      inform (loc,
11899 		      "type must be cv-qualified %<auto%> or reference to "
11900 		      "cv-qualified %<auto%>");
11901 	    }
11902 	  type = build_qualified_type (make_auto (), type_quals);
11903 	  declspecs->type = type;
11904 	}
11905       inlinep = 0;
11906       typedef_p = 0;
11907       constexpr_p = 0;
11908       consteval_p = 0;
11909       concept_p = 0;
11910       if (storage_class != sc_static)
11911 	{
11912 	  storage_class = sc_none;
11913 	  declspecs->storage_class = sc_none;
11914 	}
11915     }
11916 
11917   /* Static anonymous unions are dealt with here.  */
11918   if (staticp && decl_context == TYPENAME
11919       && declspecs->type
11920       && ANON_AGGR_TYPE_P (declspecs->type))
11921     decl_context = FIELD;
11922 
11923   /* Warn about storage classes that are invalid for certain
11924      kinds of declarations (parameters, typenames, etc.).  */
11925   if (thread_p
11926       && ((storage_class
11927 	   && storage_class != sc_extern
11928 	   && storage_class != sc_static)
11929 	  || typedef_p))
11930     {
11931       location_t loc
11932 	= min_location (declspecs->locations[ds_thread],
11933 			declspecs->locations[ds_storage_class]);
11934       error_at (loc, "multiple storage classes in declaration of %qs", name);
11935       thread_p = false;
11936     }
11937   if (decl_context != NORMAL
11938       && ((storage_class != sc_none
11939 	   && storage_class != sc_mutable)
11940 	  || thread_p))
11941     {
11942       if ((decl_context == PARM || decl_context == CATCHPARM)
11943 	  && (storage_class == sc_register
11944 	      || storage_class == sc_auto))
11945 	;
11946       else if (typedef_p)
11947 	;
11948       else if (decl_context == FIELD
11949 	       /* C++ allows static class elements.  */
11950 	       && storage_class == sc_static)
11951 	/* C++ also allows inlines and signed and unsigned elements,
11952 	   but in those cases we don't come in here.  */
11953 	;
11954       else
11955 	{
11956 	  location_t loc
11957 	    = min_location (declspecs->locations[ds_thread],
11958 			    declspecs->locations[ds_storage_class]);
11959 	  if (decl_context == FIELD)
11960 	    error_at (loc, "storage class specified for %qs", name);
11961 	  else if (decl_context == PARM || decl_context == CATCHPARM)
11962 	    error_at (loc, "storage class specified for parameter %qs", name);
11963 	  else
11964 	    error_at (loc, "storage class specified for typename");
11965 	  if (storage_class == sc_register
11966 	      || storage_class == sc_auto
11967 	      || storage_class == sc_extern
11968 	      || thread_p)
11969 	    storage_class = sc_none;
11970 	}
11971     }
11972   else if (storage_class == sc_extern && funcdef_flag
11973 	   && ! toplevel_bindings_p ())
11974     error ("nested function %qs declared %<extern%>", name);
11975   else if (toplevel_bindings_p ())
11976     {
11977       if (storage_class == sc_auto)
11978 	error_at (declspecs->locations[ds_storage_class],
11979 		  "top-level declaration of %qs specifies %<auto%>", name);
11980     }
11981   else if (thread_p
11982 	   && storage_class != sc_extern
11983 	   && storage_class != sc_static)
11984     {
11985       if (declspecs->gnu_thread_keyword_p)
11986 	pedwarn (declspecs->locations[ds_thread],
11987 		 0, "function-scope %qs implicitly auto and "
11988 		 "declared %<__thread%>", name);
11989 
11990       /* When thread_local is applied to a variable of block scope the
11991 	 storage-class-specifier static is implied if it does not appear
11992 	 explicitly.  */
11993       storage_class = declspecs->storage_class = sc_static;
11994       staticp = 1;
11995     }
11996 
11997   if (storage_class && friendp)
11998     {
11999       error_at (min_location (declspecs->locations[ds_thread],
12000 			      declspecs->locations[ds_storage_class]),
12001 		"storage class specifiers invalid in friend function "
12002 		"declarations");
12003       storage_class = sc_none;
12004       staticp = 0;
12005     }
12006 
12007   if (!id_declarator)
12008     unqualified_id = NULL_TREE;
12009   else
12010     {
12011       unqualified_id = id_declarator->u.id.unqualified_name;
12012       switch (TREE_CODE (unqualified_id))
12013 	{
12014 	case BIT_NOT_EXPR:
12015 	  unqualified_id = TREE_OPERAND (unqualified_id, 0);
12016 	  if (TYPE_P (unqualified_id))
12017 	    unqualified_id = constructor_name (unqualified_id);
12018 	  break;
12019 
12020 	case IDENTIFIER_NODE:
12021 	case TEMPLATE_ID_EXPR:
12022 	  break;
12023 
12024 	default:
12025 	  gcc_unreachable ();
12026 	}
12027     }
12028 
12029   if (declspecs->std_attributes)
12030     {
12031       location_t attr_loc = declspecs->locations[ds_std_attribute];
12032       if (warning_at (attr_loc, OPT_Wattributes, "attribute ignored"))
12033 	inform (attr_loc, "an attribute that appertains to a type-specifier "
12034 		"is ignored");
12035     }
12036 
12037   /* Determine the type of the entity declared by recurring on the
12038      declarator.  */
12039   for (; declarator; declarator = declarator->declarator)
12040     {
12041       const cp_declarator *inner_declarator;
12042       tree attrs;
12043 
12044       if (type == error_mark_node)
12045 	return error_mark_node;
12046 
12047       attrs = declarator->attributes;
12048       if (attrs)
12049 	{
12050 	  int attr_flags;
12051 
12052 	  attr_flags = 0;
12053 	  if (declarator == NULL || declarator->kind == cdk_id)
12054 	    attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
12055 	  if (declarator->kind == cdk_function)
12056 	    attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
12057 	  if (declarator->kind == cdk_array)
12058 	    attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
12059 	  tree late_attrs = NULL_TREE;
12060 	  if (decl_context != PARM && decl_context != TYPENAME)
12061 	    /* Assume that any attributes that get applied late to
12062 	       templates will DTRT when applied to the declaration
12063 	       as a whole.  */
12064 	    late_attrs = splice_template_attributes (&attrs, type);
12065 	  returned_attrs = decl_attributes (&type,
12066 					    chainon (returned_attrs, attrs),
12067 					    attr_flags);
12068 	  returned_attrs = chainon (late_attrs, returned_attrs);
12069 	}
12070 
12071       inner_declarator = declarator->declarator;
12072 
12073       /* We don't want to warn in parameter context because we don't
12074 	 yet know if the parse will succeed, and this might turn out
12075 	 to be a constructor call.  */
12076       if (decl_context != PARM
12077 	  && decl_context != TYPENAME
12078 	  && !typedef_p
12079 	  && declarator->parenthesized != UNKNOWN_LOCATION
12080 	  /* If the type is class-like and the inner name used a
12081 	     global namespace qualifier, we need the parens.
12082 	     Unfortunately all we can tell is whether a qualified name
12083 	     was used or not.  */
12084 	  && !(inner_declarator
12085 	       && inner_declarator->kind == cdk_id
12086 	       && inner_declarator->u.id.qualifying_scope
12087 	       && (MAYBE_CLASS_TYPE_P (type)
12088 		   || TREE_CODE (type) == ENUMERAL_TYPE)))
12089 	warning_at (declarator->parenthesized, OPT_Wparentheses,
12090 		    "unnecessary parentheses in declaration of %qs", name);
12091       if (declarator->kind == cdk_id || declarator->kind == cdk_decomp)
12092 	break;
12093 
12094       switch (declarator->kind)
12095 	{
12096 	case cdk_array:
12097 	  type = create_array_type_for_decl (dname, type,
12098 					     declarator->u.array.bounds,
12099 					     declarator->id_loc);
12100 	  if (!valid_array_size_p (dname
12101 				   ? declarator->id_loc : input_location,
12102 				   type, dname))
12103 	    type = error_mark_node;
12104 
12105 	  if (declarator->std_attributes)
12106 	    /* [dcl.array]/1:
12107 
12108 	       The optional attribute-specifier-seq appertains to the
12109 	       array.  */
12110 	    returned_attrs = chainon (returned_attrs,
12111 				      declarator->std_attributes);
12112 	  break;
12113 
12114 	case cdk_function:
12115 	  {
12116 	    tree arg_types;
12117 	    int funcdecl_p;
12118 
12119 	    /* Declaring a function type.  */
12120 
12121 	    {
12122 	      iloc_sentinel ils (declspecs->locations[ds_type_spec]);
12123 	      abstract_virtuals_error (ACU_RETURN, type);
12124 	    }
12125 
12126 	    /* Pick up type qualifiers which should be applied to `this'.  */
12127 	    memfn_quals = declarator->u.function.qualifiers;
12128 	    /* Pick up virt-specifiers.  */
12129             virt_specifiers = declarator->u.function.virt_specifiers;
12130 	    /* And ref-qualifier, too */
12131 	    rqual = declarator->u.function.ref_qualifier;
12132 	    /* And tx-qualifier.  */
12133 	    tree tx_qual = declarator->u.function.tx_qualifier;
12134 	    /* Pick up the exception specifications.  */
12135 	    raises = declarator->u.function.exception_specification;
12136 	    /* If the exception-specification is ill-formed, let's pretend
12137 	       there wasn't one.  */
12138 	    if (raises == error_mark_node)
12139 	      raises = NULL_TREE;
12140 
12141 	    if (reqs)
12142 	      error_at (location_of (reqs), "requires-clause on return type");
12143 	    reqs = declarator->u.function.requires_clause;
12144 
12145 	    /* Say it's a definition only for the CALL_EXPR
12146 	       closest to the identifier.  */
12147 	    funcdecl_p = inner_declarator && inner_declarator->kind == cdk_id;
12148 
12149 	    /* Handle a late-specified return type.  */
12150 	    tree late_return_type = declarator->u.function.late_return_type;
12151 	    if (funcdecl_p
12152 		/* This is the case e.g. for
12153 		   using T = auto () -> int.  */
12154 		|| inner_declarator == NULL)
12155 	      {
12156 		if (tree auto_node = type_uses_auto (type))
12157 		  {
12158 		    if (!late_return_type)
12159 		      {
12160 			if (current_class_type
12161 			    && LAMBDA_TYPE_P (current_class_type))
12162 			  /* OK for C++11 lambdas.  */;
12163 			else if (cxx_dialect < cxx14)
12164 			  {
12165 			    error_at (typespec_loc, "%qs function uses "
12166 				      "%<auto%> type specifier without "
12167 				      "trailing return type", name);
12168 			    inform (typespec_loc,
12169 				    "deduced return type only available "
12170 				    "with %<-std=c++14%> or %<-std=gnu++14%>");
12171 			  }
12172 			else if (virtualp)
12173 			  {
12174 			    error_at (typespec_loc, "virtual function "
12175 				      "cannot have deduced return type");
12176 			    virtualp = false;
12177 			  }
12178 		      }
12179 		    else if (!is_auto (type) && sfk != sfk_conversion)
12180 		      {
12181 			error_at (typespec_loc, "%qs function with trailing "
12182 				  "return type has %qT as its type rather "
12183 				  "than plain %<auto%>", name, type);
12184 			return error_mark_node;
12185 		      }
12186 		    else if (is_auto (type) && AUTO_IS_DECLTYPE (type))
12187 		      {
12188 			if (funcdecl_p)
12189 			  error_at (typespec_loc,
12190 				    "%qs function with trailing return type "
12191 				    "has %<decltype(auto)%> as its type "
12192 				    "rather than plain %<auto%>", name);
12193 			else
12194 			  error_at (typespec_loc,
12195 				    "invalid use of %<decltype(auto)%>");
12196 			return error_mark_node;
12197 		      }
12198 		    tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node);
12199 		    if (!tmpl)
12200 		      if (tree late_auto = type_uses_auto (late_return_type))
12201 			tmpl = CLASS_PLACEHOLDER_TEMPLATE (late_auto);
12202 		    if (tmpl && funcdecl_p)
12203 		      {
12204 			if (!dguide_name_p (unqualified_id))
12205 			  {
12206 			    error_at (declarator->id_loc, "deduced class "
12207 				      "type %qD in function return type",
12208 				      DECL_NAME (tmpl));
12209 			    inform (DECL_SOURCE_LOCATION (tmpl),
12210 				    "%qD declared here", tmpl);
12211 			    return error_mark_node;
12212 			  }
12213 			else if (!late_return_type)
12214 			  {
12215 			    error_at (declarator->id_loc, "deduction guide "
12216 				      "for %qT must have trailing return "
12217 				      "type", TREE_TYPE (tmpl));
12218 			    inform (DECL_SOURCE_LOCATION (tmpl),
12219 				    "%qD declared here", tmpl);
12220 			    return error_mark_node;
12221 			  }
12222 			else if (CLASS_TYPE_P (late_return_type)
12223 				 && CLASSTYPE_TEMPLATE_INFO (late_return_type)
12224 				 && (CLASSTYPE_TI_TEMPLATE (late_return_type)
12225 				     == tmpl))
12226 			  /* OK */;
12227 			else
12228 			  error ("trailing return type %qT of deduction guide "
12229 				 "is not a specialization of %qT",
12230 				 late_return_type, TREE_TYPE (tmpl));
12231 		      }
12232 		  }
12233 		else if (late_return_type
12234 			 && sfk != sfk_conversion)
12235 		  {
12236 		    if (late_return_type == error_mark_node)
12237 		      return error_mark_node;
12238 		    if (cxx_dialect < cxx11)
12239 		      /* Not using maybe_warn_cpp0x because this should
12240 			 always be an error.  */
12241 		      error_at (typespec_loc,
12242 				"trailing return type only available "
12243 				"with %<-std=c++11%> or %<-std=gnu++11%>");
12244 		    else
12245 		      error_at (typespec_loc, "%qs function with trailing "
12246 				"return type not declared with %<auto%> "
12247 				"type specifier", name);
12248 		    return error_mark_node;
12249 		  }
12250 	      }
12251 	    type = splice_late_return_type (type, late_return_type);
12252 	    if (type == error_mark_node)
12253 	      return error_mark_node;
12254 
12255 	    if (late_return_type)
12256 	      {
12257 		late_return_type_p = true;
12258 		type_quals = cp_type_quals (type);
12259 	      }
12260 
12261 	    if (type_quals != TYPE_UNQUALIFIED)
12262 	      {
12263 		if (SCALAR_TYPE_P (type) || VOID_TYPE_P (type))
12264 		  warning_at (typespec_loc, OPT_Wignored_qualifiers, "type "
12265 			      "qualifiers ignored on function return type");
12266 		/* [dcl.fct] "A volatile-qualified return type is
12267 		   deprecated."  */
12268 		if (type_quals & TYPE_QUAL_VOLATILE)
12269 		  warning_at (typespec_loc, OPT_Wvolatile,
12270 			      "%<volatile%>-qualified return type is "
12271 			      "deprecated");
12272 
12273 		/* We now know that the TYPE_QUALS don't apply to the
12274 		   decl, but to its return type.  */
12275 		type_quals = TYPE_UNQUALIFIED;
12276 	      }
12277 
12278 	    /* Error about some types functions can't return.  */
12279 
12280 	    if (TREE_CODE (type) == FUNCTION_TYPE)
12281 	      {
12282 		error_at (typespec_loc, "%qs declared as function returning "
12283 			  "a function", name);
12284 		return error_mark_node;
12285 	      }
12286 	    if (TREE_CODE (type) == ARRAY_TYPE)
12287 	      {
12288 		error_at (typespec_loc, "%qs declared as function returning "
12289 			  "an array", name);
12290 		return error_mark_node;
12291 	      }
12292 	    if (constinit_p && funcdecl_p)
12293 	      {
12294 		error_at (declspecs->locations[ds_constinit],
12295 			  "%<constinit%> on function return type is not "
12296 			  "allowed");
12297 		return error_mark_node;
12298 	      }
12299 	    /* Only plain decltype(auto) is allowed.  */
12300 	    if (tree a = type_uses_auto (type))
12301 	      {
12302 		if (AUTO_IS_DECLTYPE (a) && a != type)
12303 		  {
12304 		    error_at (typespec_loc, "%qT as type rather than "
12305 			      "plain %<decltype(auto)%>", type);
12306 		    return error_mark_node;
12307 		  }
12308 	      }
12309 
12310 	    if (ctype == NULL_TREE
12311 		&& decl_context == FIELD
12312 		&& funcdecl_p
12313 		&& friendp == 0)
12314 	      ctype = current_class_type;
12315 
12316 	    if (ctype && (sfk == sfk_constructor
12317 			  || sfk == sfk_destructor))
12318 	      {
12319 		/* We are within a class's scope. If our declarator name
12320 		   is the same as the class name, and we are defining
12321 		   a function, then it is a constructor/destructor, and
12322 		   therefore returns a void type.  */
12323 
12324 		/* ISO C++ 12.4/2.  A destructor may not be declared
12325 		   const or volatile.  A destructor may not be static.
12326 		   A destructor may not be declared with ref-qualifier.
12327 
12328 		   ISO C++ 12.1.  A constructor may not be declared
12329 		   const or volatile.  A constructor may not be
12330 		   virtual.  A constructor may not be static.
12331 		   A constructor may not be declared with ref-qualifier. */
12332 		if (staticp == 2)
12333 		  error_at (declspecs->locations[ds_storage_class],
12334 			    (flags == DTOR_FLAG)
12335 			    ? G_("destructor cannot be static member "
12336 				 "function")
12337 			    : G_("constructor cannot be static member "
12338 				 "function"));
12339 		if (memfn_quals)
12340 		  {
12341 		    error ((flags == DTOR_FLAG)
12342 			   ? G_("destructors may not be cv-qualified")
12343 			   : G_("constructors may not be cv-qualified"));
12344 		    memfn_quals = TYPE_UNQUALIFIED;
12345 		  }
12346 
12347 		if (rqual)
12348 		  {
12349 		    maybe_warn_cpp0x (CPP0X_REF_QUALIFIER);
12350 		    error ((flags == DTOR_FLAG)
12351 			   ? G_("destructors may not be ref-qualified")
12352 			   : G_("constructors may not be ref-qualified"));
12353 		    rqual = REF_QUAL_NONE;
12354 		  }
12355 
12356 		if (decl_context == FIELD
12357 		    && !member_function_or_else (ctype,
12358 						 current_class_type,
12359 						 flags))
12360 		  return error_mark_node;
12361 
12362 		if (flags != DTOR_FLAG)
12363 		  {
12364 		    /* It's a constructor.  */
12365 		    if (explicitp == 1)
12366 		      explicitp = 2;
12367 		    if (virtualp)
12368 		      {
12369 			permerror (declspecs->locations[ds_virtual],
12370 				   "constructors cannot be declared %<virtual%>");
12371 			virtualp = 0;
12372 		      }
12373 		    if (decl_context == FIELD
12374 			&& sfk != sfk_constructor)
12375 		      return error_mark_node;
12376 		  }
12377 		if (decl_context == FIELD)
12378 		  staticp = 0;
12379 	      }
12380 	    else if (friendp)
12381 	      {
12382 		if (virtualp)
12383 		  {
12384 		    /* Cannot be both friend and virtual.  */
12385 		    gcc_rich_location richloc (declspecs->locations[ds_virtual]);
12386 		    richloc.add_range (declspecs->locations[ds_friend]);
12387 		    error_at (&richloc, "virtual functions cannot be friends");
12388 		    friendp = 0;
12389 		  }
12390 		if (decl_context == NORMAL)
12391 		  error_at (declarator->id_loc,
12392 			    "friend declaration not in class definition");
12393 		if (current_function_decl && funcdef_flag)
12394 		  {
12395 		    error_at (declarator->id_loc,
12396 			      "cannot define friend function %qs in a local "
12397 			      "class definition", name);
12398 		    friendp = 0;
12399 		  }
12400 		/* [class.friend]/6: A function can be defined in a friend
12401 		   declaration if the function name is unqualified.  */
12402 		if (funcdef_flag && in_namespace)
12403 		  {
12404 		    if (in_namespace == global_namespace)
12405 		      error_at (declarator->id_loc,
12406 				"friend function definition %qs cannot have "
12407 				"a name qualified with %<::%>", name);
12408 		    else
12409 		      error_at (declarator->id_loc,
12410 				"friend function definition %qs cannot have "
12411 				"a name qualified with %<%D::%>", name,
12412 				in_namespace);
12413 		  }
12414 	      }
12415 	    else if (ctype && sfk == sfk_conversion)
12416 	      {
12417 		if (explicitp == 1)
12418 		  {
12419 		    maybe_warn_cpp0x (CPP0X_EXPLICIT_CONVERSION);
12420 		    explicitp = 2;
12421 		  }
12422 		if (late_return_type_p)
12423 		  error ("a conversion function cannot have a trailing return type");
12424 	      }
12425 	    else if (sfk == sfk_deduction_guide)
12426 	      {
12427 		if (explicitp == 1)
12428 		  explicitp = 2;
12429 	      }
12430 
12431 	    tree pushed_scope = NULL_TREE;
12432 	    if (funcdecl_p
12433 		&& decl_context != FIELD
12434 		&& inner_declarator->u.id.qualifying_scope
12435 		&& CLASS_TYPE_P (inner_declarator->u.id.qualifying_scope))
12436 	      pushed_scope
12437 		= push_scope (inner_declarator->u.id.qualifying_scope);
12438 
12439 	    arg_types = grokparms (declarator->u.function.parameters, &parms);
12440 
12441 	    if (pushed_scope)
12442 	      pop_scope (pushed_scope);
12443 
12444 	    if (inner_declarator
12445 		&& inner_declarator->kind == cdk_id
12446 		&& inner_declarator->u.id.sfk == sfk_destructor
12447 		&& arg_types != void_list_node)
12448 	      {
12449 		error_at (declarator->id_loc,
12450 			  "destructors may not have parameters");
12451 		arg_types = void_list_node;
12452 		parms = NULL_TREE;
12453 	      }
12454 
12455 	    type = build_function_type (type, arg_types);
12456 
12457 	    tree attrs = declarator->std_attributes;
12458 	    if (tx_qual)
12459 	      {
12460 		tree att = build_tree_list (tx_qual, NULL_TREE);
12461 		/* transaction_safe applies to the type, but
12462 		   transaction_safe_dynamic applies to the function.  */
12463 		if (is_attribute_p ("transaction_safe", tx_qual))
12464 		  attrs = chainon (attrs, att);
12465 		else
12466 		  returned_attrs = chainon (returned_attrs, att);
12467 	      }
12468 	    if (attrs)
12469 	      /* [dcl.fct]/2:
12470 
12471 		 The optional attribute-specifier-seq appertains to
12472 		 the function type.  */
12473 	      cplus_decl_attributes (&type, attrs, 0);
12474 
12475 	    if (raises)
12476 	      type = build_exception_variant (type, raises);
12477 	  }
12478 	  break;
12479 
12480 	case cdk_pointer:
12481 	case cdk_reference:
12482 	case cdk_ptrmem:
12483 	  /* Filter out pointers-to-references and references-to-references.
12484 	     We can get these if a TYPE_DECL is used.  */
12485 
12486 	  if (TYPE_REF_P (type))
12487 	    {
12488 	      if (declarator->kind != cdk_reference)
12489 		{
12490 		  error ("cannot declare pointer to %q#T", type);
12491 		  type = TREE_TYPE (type);
12492 		}
12493 
12494 	      /* In C++0x, we allow reference to reference declarations
12495 		 that occur indirectly through typedefs [7.1.3/8 dcl.typedef]
12496 		 and template type arguments [14.3.1/4 temp.arg.type]. The
12497 		 check for direct reference to reference declarations, which
12498 		 are still forbidden, occurs below. Reasoning behind the change
12499 		 can be found in DR106, DR540, and the rvalue reference
12500 		 proposals. */
12501 	      else if (cxx_dialect == cxx98)
12502 		{
12503 		  error ("cannot declare reference to %q#T", type);
12504 		  type = TREE_TYPE (type);
12505 		}
12506 	    }
12507 	  else if (VOID_TYPE_P (type))
12508 	    {
12509 	      if (declarator->kind == cdk_reference)
12510 		error ("cannot declare reference to %q#T", type);
12511 	      else if (declarator->kind == cdk_ptrmem)
12512 		error ("cannot declare pointer to %q#T member", type);
12513 	    }
12514 
12515 	  /* We now know that the TYPE_QUALS don't apply to the decl,
12516 	     but to the target of the pointer.  */
12517 	  type_quals = TYPE_UNQUALIFIED;
12518 
12519 	  /* This code used to handle METHOD_TYPE, but I don't think it's
12520 	     possible to get it here anymore.  */
12521 	  gcc_assert (TREE_CODE (type) != METHOD_TYPE);
12522 	  if (declarator->kind == cdk_ptrmem
12523 	      && TREE_CODE (type) == FUNCTION_TYPE)
12524 	    {
12525 	      memfn_quals |= type_memfn_quals (type);
12526 	      type = build_memfn_type (type,
12527 				       declarator->u.pointer.class_type,
12528 				       memfn_quals,
12529 				       rqual);
12530 	      if (type == error_mark_node)
12531 		return error_mark_node;
12532 
12533 	      rqual = REF_QUAL_NONE;
12534 	      memfn_quals = TYPE_UNQUALIFIED;
12535 	    }
12536 
12537 	  if (TREE_CODE (type) == FUNCTION_TYPE
12538 	      && (type_memfn_quals (type) != TYPE_UNQUALIFIED
12539 		  || type_memfn_rqual (type) != REF_QUAL_NONE))
12540             error (declarator->kind == cdk_reference
12541                    ? G_("cannot declare reference to qualified function type %qT")
12542                    : G_("cannot declare pointer to qualified function type %qT"),
12543 		   type);
12544 
12545 	  /* When the pointed-to type involves components of variable size,
12546 	     care must be taken to ensure that the size evaluation code is
12547 	     emitted early enough to dominate all the possible later uses
12548 	     and late enough for the variables on which it depends to have
12549 	     been assigned.
12550 
12551 	     This is expected to happen automatically when the pointed-to
12552 	     type has a name/declaration of it's own, but special attention
12553 	     is required if the type is anonymous.
12554 
12555 	     We handle the NORMAL and FIELD contexts here by inserting a
12556 	     dummy statement that just evaluates the size at a safe point
12557 	     and ensures it is not deferred until e.g. within a deeper
12558 	     conditional context (c++/43555).
12559 
12560 	     We expect nothing to be needed here for PARM or TYPENAME.
12561 	     Evaluating the size at this point for TYPENAME would
12562 	     actually be incorrect, as we might be in the middle of an
12563 	     expression with side effects on the pointed-to type size
12564 	     "arguments" prior to the pointer declaration point and the
12565 	     size evaluation could end up prior to the side effects.  */
12566 
12567 	  if (!TYPE_NAME (type)
12568 	      && (decl_context == NORMAL || decl_context == FIELD)
12569 	      && at_function_scope_p ()
12570 	      && variably_modified_type_p (type, NULL_TREE))
12571 	    {
12572 	      TYPE_NAME (type) = build_decl (UNKNOWN_LOCATION, TYPE_DECL,
12573 					     NULL_TREE, type);
12574 	      add_decl_expr (TYPE_NAME (type));
12575 	    }
12576 
12577 	  if (declarator->kind == cdk_reference)
12578 	    {
12579 	      /* In C++0x, the type we are creating a reference to might be
12580 		 a typedef which is itself a reference type. In that case,
12581 		 we follow the reference collapsing rules in
12582 		 [7.1.3/8 dcl.typedef] to create the final reference type:
12583 
12584 		 "If a typedef TD names a type that is a reference to a type
12585 		 T, an attempt to create the type 'lvalue reference to cv TD'
12586 		 creates the type 'lvalue reference to T,' while an attempt
12587 		 to create the type "rvalue reference to cv TD' creates the
12588 		 type TD."
12589               */
12590 	      if (VOID_TYPE_P (type))
12591 		/* We already gave an error.  */;
12592 	      else if (TYPE_REF_P (type))
12593 		{
12594 		  if (declarator->u.reference.rvalue_ref)
12595 		    /* Leave type alone.  */;
12596 		  else
12597 		    type = cp_build_reference_type (TREE_TYPE (type), false);
12598 		}
12599 	      else
12600 		type = cp_build_reference_type
12601 		  (type, declarator->u.reference.rvalue_ref);
12602 
12603 	      /* In C++0x, we need this check for direct reference to
12604 		 reference declarations, which are forbidden by
12605 		 [8.3.2/5 dcl.ref]. Reference to reference declarations
12606 		 are only allowed indirectly through typedefs and template
12607 		 type arguments. Example:
12608 
12609 		   void foo(int & &);      // invalid ref-to-ref decl
12610 
12611 		   typedef int & int_ref;
12612 		   void foo(int_ref &);    // valid ref-to-ref decl
12613 	      */
12614 	      if (inner_declarator && inner_declarator->kind == cdk_reference)
12615 		error ("cannot declare reference to %q#T, which is not "
12616 		       "a typedef or a template type argument", type);
12617 	    }
12618 	  else if (TREE_CODE (type) == METHOD_TYPE)
12619 	    type = build_ptrmemfunc_type (build_pointer_type (type));
12620 	  else if (declarator->kind == cdk_ptrmem)
12621 	    {
12622 	      gcc_assert (TREE_CODE (declarator->u.pointer.class_type)
12623 			  != NAMESPACE_DECL);
12624 	      if (declarator->u.pointer.class_type == error_mark_node)
12625 		/* We will already have complained.  */
12626 		type = error_mark_node;
12627 	      else
12628 		type = build_ptrmem_type (declarator->u.pointer.class_type,
12629 					  type);
12630 	    }
12631 	  else
12632 	    type = build_pointer_type (type);
12633 
12634 	  /* Process a list of type modifier keywords (such as
12635 	     const or volatile) that were given inside the `*' or `&'.  */
12636 
12637 	  if (declarator->u.pointer.qualifiers)
12638 	    {
12639 	      type
12640 		= cp_build_qualified_type (type,
12641 					   declarator->u.pointer.qualifiers);
12642 	      type_quals = cp_type_quals (type);
12643 	    }
12644 
12645 	  /* Apply C++11 attributes to the pointer, and not to the
12646 	     type pointed to.  This is unlike what is done for GNU
12647 	     attributes above.  It is to comply with [dcl.ptr]/1:
12648 
12649 		 [the optional attribute-specifier-seq (7.6.1) appertains
12650 		  to the pointer and not to the object pointed to].  */
12651 	  if (declarator->std_attributes)
12652 	    decl_attributes (&type, declarator->std_attributes,
12653 			     0);
12654 
12655 	  ctype = NULL_TREE;
12656 	  break;
12657 
12658 	case cdk_error:
12659 	  break;
12660 
12661 	default:
12662 	  gcc_unreachable ();
12663 	}
12664     }
12665 
12666   id_loc = declarator ? declarator->id_loc : input_location;
12667 
12668   /* A `constexpr' specifier used in an object declaration declares
12669      the object as `const'.  */
12670   if (constexpr_p && innermost_code != cdk_function)
12671     {
12672       /* DR1688 says that a `constexpr' specifier in combination with
12673 	 `volatile' is valid.  */
12674 
12675       if (!TYPE_REF_P (type))
12676 	{
12677 	  type_quals |= TYPE_QUAL_CONST;
12678 	  type = cp_build_qualified_type (type, type_quals);
12679 	}
12680     }
12681 
12682   if (unqualified_id && TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR
12683       && !FUNC_OR_METHOD_TYPE_P (type)
12684       && !variable_template_p (TREE_OPERAND (unqualified_id, 0)))
12685     {
12686       error ("template-id %qD used as a declarator",
12687 	     unqualified_id);
12688       unqualified_id = dname;
12689     }
12690 
12691   /* If TYPE is a FUNCTION_TYPE, but the function name was explicitly
12692      qualified with a class-name, turn it into a METHOD_TYPE, unless
12693      we know that the function is static.  We take advantage of this
12694      opportunity to do other processing that pertains to entities
12695      explicitly declared to be class members.  Note that if DECLARATOR
12696      is non-NULL, we know it is a cdk_id declarator; otherwise, we
12697      would not have exited the loop above.  */
12698   if (declarator
12699       && declarator->kind == cdk_id
12700       && declarator->u.id.qualifying_scope
12701       && MAYBE_CLASS_TYPE_P (declarator->u.id.qualifying_scope))
12702     {
12703       ctype = declarator->u.id.qualifying_scope;
12704       ctype = TYPE_MAIN_VARIANT (ctype);
12705       template_count = num_template_headers_for_class (ctype);
12706 
12707       if (ctype == current_class_type)
12708 	{
12709 	  if (friendp)
12710 	    {
12711 	      permerror (declspecs->locations[ds_friend],
12712 			 "member functions are implicitly "
12713 			 "friends of their class");
12714 	      friendp = 0;
12715 	    }
12716 	  else
12717 	    permerror (id_loc, "extra qualification %<%T::%> on member %qs",
12718 		       ctype, name);
12719 	}
12720       else if (/* If the qualifying type is already complete, then we
12721 		  can skip the following checks.  */
12722 	       !COMPLETE_TYPE_P (ctype)
12723 	       && (/* If the function is being defined, then
12724 		      qualifying type must certainly be complete.  */
12725 		   funcdef_flag
12726 		   /* A friend declaration of "T::f" is OK, even if
12727 		      "T" is a template parameter.  But, if this
12728 		      function is not a friend, the qualifying type
12729 		      must be a class.  */
12730 		   || (!friendp && !CLASS_TYPE_P (ctype))
12731 		   /* For a declaration, the type need not be
12732 		      complete, if either it is dependent (since there
12733 		      is no meaningful definition of complete in that
12734 		      case) or the qualifying class is currently being
12735 		      defined.  */
12736 		   || !(dependent_type_p (ctype)
12737 			|| currently_open_class (ctype)))
12738 	       /* Check that the qualifying type is complete.  */
12739 	       && !complete_type_or_else (ctype, NULL_TREE))
12740 	return error_mark_node;
12741       else if (TREE_CODE (type) == FUNCTION_TYPE)
12742 	{
12743 	  if (current_class_type
12744 	      && (!friendp || funcdef_flag || initialized))
12745 	    {
12746 	      error_at (id_loc, funcdef_flag || initialized
12747 			? G_("cannot define member function %<%T::%s%> "
12748 			     "within %qT")
12749 			: G_("cannot declare member function %<%T::%s%> "
12750 			     "within %qT"),
12751 			ctype, name, current_class_type);
12752 	      return error_mark_node;
12753 	    }
12754 	}
12755       else if (typedef_p && current_class_type)
12756 	{
12757 	  error_at (id_loc, "cannot declare member %<%T::%s%> within %qT",
12758 		    ctype, name, current_class_type);
12759 	  return error_mark_node;
12760 	}
12761     }
12762 
12763   if (ctype == NULL_TREE && decl_context == FIELD && friendp == 0)
12764     ctype = current_class_type;
12765 
12766   /* Now TYPE has the actual type.  */
12767 
12768   if (returned_attrs)
12769     {
12770       if (attrlist)
12771 	*attrlist = chainon (returned_attrs, *attrlist);
12772       else
12773 	attrlist = &returned_attrs;
12774     }
12775 
12776   if (declarator
12777       && declarator->kind == cdk_id
12778       && declarator->std_attributes
12779       && attrlist != NULL)
12780     {
12781       /* [dcl.meaning]/1: The optional attribute-specifier-seq following
12782 	 a declarator-id appertains to the entity that is declared.  */
12783       if (declarator->std_attributes != error_mark_node)
12784 	*attrlist = chainon (*attrlist, declarator->std_attributes);
12785       else
12786 	/* We should have already diagnosed the issue (c++/78344).  */
12787 	gcc_assert (seen_error ());
12788     }
12789 
12790   /* Handle parameter packs. */
12791   if (parameter_pack_p)
12792     {
12793       if (decl_context == PARM)
12794         /* Turn the type into a pack expansion.*/
12795         type = make_pack_expansion (type);
12796       else
12797         error ("non-parameter %qs cannot be a parameter pack", name);
12798     }
12799 
12800   if ((decl_context == FIELD || decl_context == PARM)
12801       && !processing_template_decl
12802       && variably_modified_type_p (type, NULL_TREE))
12803     {
12804       if (decl_context == FIELD)
12805 	error_at (id_loc,
12806 		  "data member may not have variably modified type %qT", type);
12807       else
12808 	error_at (id_loc,
12809 		  "parameter may not have variably modified type %qT", type);
12810       type = error_mark_node;
12811     }
12812 
12813   if (explicitp == 1 || (explicitp && friendp))
12814     {
12815       /* [dcl.fct.spec] (C++11) The explicit specifier shall be used only
12816 	 in the declaration of a constructor or conversion function within
12817 	 a class definition.  */
12818       if (!current_class_type)
12819 	error_at (declspecs->locations[ds_explicit],
12820 		  "%<explicit%> outside class declaration");
12821       else if (friendp)
12822 	error_at (declspecs->locations[ds_explicit],
12823 		  "%<explicit%> in friend declaration");
12824       else
12825 	error_at (declspecs->locations[ds_explicit],
12826 		  "only declarations of constructors and conversion operators "
12827 		  "can be %<explicit%>");
12828       explicitp = 0;
12829     }
12830 
12831   if (storage_class == sc_mutable)
12832     {
12833       location_t sloc = declspecs->locations[ds_storage_class];
12834       if (decl_context != FIELD || friendp)
12835 	{
12836 	  error_at (sloc, "non-member %qs cannot be declared %<mutable%>",
12837 		    name);
12838 	  storage_class = sc_none;
12839 	}
12840       else if (decl_context == TYPENAME || typedef_p)
12841 	{
12842 	  error_at (sloc,
12843 		    "non-object member %qs cannot be declared %<mutable%>",
12844 		    name);
12845 	  storage_class = sc_none;
12846 	}
12847       else if (FUNC_OR_METHOD_TYPE_P (type))
12848 	{
12849 	  error_at (sloc, "function %qs cannot be declared %<mutable%>",
12850 		    name);
12851 	  storage_class = sc_none;
12852 	}
12853       else if (staticp)
12854 	{
12855 	  error_at (sloc, "%<static%> %qs cannot be declared %<mutable%>",
12856 		    name);
12857 	  storage_class = sc_none;
12858 	}
12859       else if (type_quals & TYPE_QUAL_CONST)
12860 	{
12861 	  error_at (sloc, "%<const%> %qs cannot be declared %<mutable%>",
12862 		    name);
12863 	  storage_class = sc_none;
12864 	}
12865       else if (TYPE_REF_P (type))
12866 	{
12867 	  permerror (sloc, "reference %qs cannot be declared %<mutable%>",
12868 		     name);
12869 	  storage_class = sc_none;
12870 	}
12871     }
12872 
12873   /* If this is declaring a typedef name, return a TYPE_DECL.  */
12874   if (typedef_p && decl_context != TYPENAME)
12875     {
12876       bool alias_p = decl_spec_seq_has_spec_p (declspecs, ds_alias);
12877       tree decl;
12878 
12879       if (funcdef_flag)
12880 	{
12881 	  if (decl_context == NORMAL)
12882 	    error_at (id_loc,
12883 		      "typedef may not be a function definition");
12884 	  else
12885 	    error_at (id_loc,
12886 		      "typedef may not be a member function definition");
12887 	  return error_mark_node;
12888 	}
12889 
12890       /* This declaration:
12891 
12892 	   typedef void f(int) const;
12893 
12894 	 declares a function type which is not a member of any
12895 	 particular class, but which is cv-qualified; for
12896 	 example "f S::*" declares a pointer to a const-qualified
12897 	 member function of S.  We record the cv-qualification in the
12898 	 function type.  */
12899       if ((rqual || memfn_quals) && TREE_CODE (type) == FUNCTION_TYPE)
12900         {
12901           type = apply_memfn_quals (type, memfn_quals, rqual);
12902 
12903           /* We have now dealt with these qualifiers.  */
12904           memfn_quals = TYPE_UNQUALIFIED;
12905 	  rqual = REF_QUAL_NONE;
12906         }
12907 
12908       if (type_uses_auto (type))
12909 	{
12910 	  if (alias_p)
12911 	    error_at (declspecs->locations[ds_type_spec],
12912 		      "%<auto%> not allowed in alias declaration");
12913 	  else
12914 	    error_at (declspecs->locations[ds_type_spec],
12915 		      "typedef declared %<auto%>");
12916 	  type = error_mark_node;
12917 	}
12918 
12919       if (reqs)
12920 	error_at (location_of (reqs), "requires-clause on typedef");
12921 
12922       if (id_declarator && declarator->u.id.qualifying_scope)
12923 	{
12924 	  error_at (id_loc, "typedef name may not be a nested-name-specifier");
12925 	  type = error_mark_node;
12926 	}
12927 
12928       if (decl_context == FIELD)
12929 	decl = build_lang_decl_loc (id_loc, TYPE_DECL, unqualified_id, type);
12930       else
12931 	decl = build_decl (id_loc, TYPE_DECL, unqualified_id, type);
12932 
12933       if (decl_context != FIELD)
12934 	{
12935 	  if (!current_function_decl)
12936 	    DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
12937 	  else if (DECL_MAYBE_IN_CHARGE_CDTOR_P (current_function_decl))
12938 	    /* The TYPE_DECL is "abstract" because there will be
12939 	       clones of this constructor/destructor, and there will
12940 	       be copies of this TYPE_DECL generated in those
12941 	       clones.  The decloning optimization (for space) may
12942                revert this subsequently if it determines that
12943                the clones should share a common implementation.  */
12944 	    DECL_ABSTRACT_P (decl) = true;
12945 	}
12946       else if (current_class_type
12947 	       && constructor_name_p (unqualified_id, current_class_type))
12948 	permerror (id_loc, "ISO C++ forbids nested type %qD with same name "
12949 		   "as enclosing class",
12950 		   unqualified_id);
12951 
12952       /* If the user declares "typedef struct {...} foo" then the
12953 	 struct will have an anonymous name.  Fill that name in now.
12954 	 Nothing can refer to it, so nothing needs know about the name
12955 	 change.  */
12956       if (type != error_mark_node
12957 	  && unqualified_id
12958 	  && TYPE_NAME (type)
12959 	  && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12960 	  && TYPE_UNNAMED_P (type)
12961 	  && declspecs->type_definition_p
12962 	  && attributes_naming_typedef_ok (*attrlist)
12963 	  && cp_type_quals (type) == TYPE_UNQUALIFIED)
12964 	name_unnamed_type (type, decl);
12965 
12966       if (signed_p
12967 	  || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
12968 	C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
12969 
12970       bad_specifiers (decl, BSP_TYPE, virtualp,
12971 		      memfn_quals != TYPE_UNQUALIFIED,
12972 		      inlinep, friendp, raises != NULL_TREE,
12973 		      declspecs->locations);
12974 
12975       if (alias_p)
12976 	/* Acknowledge that this was written:
12977 	     `using analias = atype;'.  */
12978 	TYPE_DECL_ALIAS_P (decl) = 1;
12979 
12980       return decl;
12981     }
12982 
12983   /* Detect the case of an array type of unspecified size
12984      which came, as such, direct from a typedef name.
12985      We must copy the type, so that the array's domain can be
12986      individually set by the object's initializer.  */
12987 
12988   if (type && typedef_type
12989       && TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
12990       && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
12991     type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
12992 
12993   /* Detect where we're using a typedef of function type to declare a
12994      function. PARMS will not be set, so we must create it now.  */
12995 
12996   if (type == typedef_type && TREE_CODE (type) == FUNCTION_TYPE)
12997     {
12998       tree decls = NULL_TREE;
12999       tree args;
13000 
13001       for (args = TYPE_ARG_TYPES (type);
13002 	   args && args != void_list_node;
13003 	   args = TREE_CHAIN (args))
13004 	{
13005 	  tree decl = cp_build_parm_decl (NULL_TREE, NULL_TREE,
13006 					  TREE_VALUE (args));
13007 
13008 	  DECL_CHAIN (decl) = decls;
13009 	  decls = decl;
13010 	}
13011 
13012       parms = nreverse (decls);
13013 
13014       if (decl_context != TYPENAME)
13015 	{
13016 	  /* The qualifiers on the function type become the qualifiers on
13017 	     the non-static member function. */
13018 	  memfn_quals |= type_memfn_quals (type);
13019 	  rqual = type_memfn_rqual (type);
13020 	  type_quals = TYPE_UNQUALIFIED;
13021 	  raises = TYPE_RAISES_EXCEPTIONS (type);
13022 	}
13023     }
13024 
13025   /* If this is a type name (such as, in a cast or sizeof),
13026      compute the type and return it now.  */
13027 
13028   if (decl_context == TYPENAME)
13029     {
13030       /* Note that here we don't care about type_quals.  */
13031 
13032       /* Special case: "friend class foo" looks like a TYPENAME context.  */
13033       if (friendp)
13034 	{
13035 	  if (inlinep)
13036 	    {
13037 	      error ("%<inline%> specified for friend class declaration");
13038 	      inlinep = 0;
13039 	    }
13040 
13041 	  if (!current_aggr)
13042 	    {
13043 	      /* Don't allow friend declaration without a class-key.  */
13044 	      if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
13045 		permerror (input_location, "template parameters cannot be friends");
13046 	      else if (TREE_CODE (type) == TYPENAME_TYPE)
13047 		permerror (input_location, "friend declaration requires class-key, "
13048 			   "i.e. %<friend class %T::%D%>",
13049 			   TYPE_CONTEXT (type), TYPENAME_TYPE_FULLNAME (type));
13050 	      else
13051 		permerror (input_location, "friend declaration requires class-key, "
13052 			   "i.e. %<friend %#T%>",
13053 			   type);
13054 	    }
13055 
13056 	  /* Only try to do this stuff if we didn't already give up.  */
13057 	  if (type != integer_type_node)
13058 	    {
13059 	      /* A friendly class?  */
13060 	      if (current_class_type)
13061 		make_friend_class (current_class_type, TYPE_MAIN_VARIANT (type),
13062 				   /*complain=*/true);
13063 	      else
13064 		error ("trying to make class %qT a friend of global scope",
13065 		       type);
13066 
13067 	      type = void_type_node;
13068 	    }
13069 	}
13070       else if (memfn_quals || rqual)
13071 	{
13072 	  if (ctype == NULL_TREE
13073 	      && TREE_CODE (type) == METHOD_TYPE)
13074 	    ctype = TYPE_METHOD_BASETYPE (type);
13075 
13076 	  if (ctype)
13077 	    type = build_memfn_type (type, ctype, memfn_quals, rqual);
13078 	  /* Core issue #547: need to allow this in template type args.
13079 	     Allow it in general in C++11 for alias-declarations.  */
13080 	  else if ((template_type_arg || cxx_dialect >= cxx11)
13081 		   && TREE_CODE (type) == FUNCTION_TYPE)
13082 	    type = apply_memfn_quals (type, memfn_quals, rqual);
13083 	  else
13084 	    error ("invalid qualifiers on non-member function type");
13085 	}
13086 
13087       if (reqs)
13088 	error_at (location_of (reqs), "requires-clause on type-id");
13089 
13090       return type;
13091     }
13092   else if (unqualified_id == NULL_TREE && decl_context != PARM
13093 	   && decl_context != CATCHPARM
13094 	   && TREE_CODE (type) != UNION_TYPE
13095 	   && ! bitfield
13096 	   && innermost_code != cdk_decomp)
13097     {
13098       error ("abstract declarator %qT used as declaration", type);
13099       return error_mark_node;
13100     }
13101 
13102   if (!FUNC_OR_METHOD_TYPE_P (type))
13103     {
13104       /* Only functions may be declared using an operator-function-id.  */
13105       if (dname && IDENTIFIER_ANY_OP_P (dname))
13106 	{
13107 	  error_at (id_loc, "declaration of %qD as non-function", dname);
13108 	  return error_mark_node;
13109 	}
13110 
13111       if (reqs)
13112 	error_at (location_of (reqs),
13113 		  "requires-clause on declaration of non-function type %qT",
13114 		  type);
13115     }
13116 
13117   /* We don't check parameter types here because we can emit a better
13118      error message later.  */
13119   if (decl_context != PARM)
13120     {
13121       type = check_var_type (unqualified_id, type, id_loc);
13122       if (type == error_mark_node)
13123         return error_mark_node;
13124     }
13125 
13126   /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
13127      or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE.  */
13128 
13129   if (decl_context == PARM || decl_context == CATCHPARM)
13130     {
13131       if (ctype || in_namespace)
13132 	error ("cannot use %<::%> in parameter declaration");
13133 
13134       tree auto_node = type_uses_auto (type);
13135       if (auto_node && !(cxx_dialect >= cxx17 && template_parm_flag))
13136 	{
13137 	  if (cxx_dialect >= cxx14)
13138 	    {
13139 	      if (decl_context == PARM && AUTO_IS_DECLTYPE (auto_node))
13140 		error_at (typespec_loc,
13141 			  "cannot declare a parameter with %<decltype(auto)%>");
13142 	      else
13143 		error_at (typespec_loc,
13144 			  "%<auto%> parameter not permitted in this context");
13145 	    }
13146 	  else
13147 	    error_at (typespec_loc, "parameter declared %<auto%>");
13148 	  type = error_mark_node;
13149 	}
13150 
13151       /* A parameter declared as an array of T is really a pointer to T.
13152 	 One declared as a function is really a pointer to a function.
13153 	 One declared as a member is really a pointer to member.  */
13154 
13155       if (TREE_CODE (type) == ARRAY_TYPE)
13156 	{
13157 	  /* Transfer const-ness of array into that of type pointed to.  */
13158 	  type = build_pointer_type (TREE_TYPE (type));
13159 	  type_quals = TYPE_UNQUALIFIED;
13160 	  array_parameter_p = true;
13161 	}
13162       else if (TREE_CODE (type) == FUNCTION_TYPE)
13163 	type = build_pointer_type (type);
13164     }
13165 
13166   if (ctype && TREE_CODE (type) == FUNCTION_TYPE && staticp < 2
13167       && !(unqualified_id
13168 	   && identifier_p (unqualified_id)
13169 	   && IDENTIFIER_NEWDEL_OP_P (unqualified_id)))
13170     {
13171       cp_cv_quals real_quals = memfn_quals;
13172       if (cxx_dialect < cxx14 && constexpr_p
13173 	  && sfk != sfk_constructor && sfk != sfk_destructor)
13174 	real_quals |= TYPE_QUAL_CONST;
13175       type = build_memfn_type (type, ctype, real_quals, rqual);
13176     }
13177 
13178   {
13179     tree decl = NULL_TREE;
13180 
13181     if (decl_context == PARM)
13182       {
13183 	decl = cp_build_parm_decl (NULL_TREE, unqualified_id, type);
13184 	DECL_ARRAY_PARAMETER_P (decl) = array_parameter_p;
13185 
13186 	bad_specifiers (decl, BSP_PARM, virtualp,
13187 			memfn_quals != TYPE_UNQUALIFIED,
13188 			inlinep, friendp, raises != NULL_TREE,
13189 			declspecs->locations);
13190       }
13191     else if (decl_context == FIELD)
13192       {
13193 	if (!staticp && !friendp && TREE_CODE (type) != METHOD_TYPE)
13194 	  if (tree auto_node = type_uses_auto (type))
13195 	    {
13196 	      location_t tloc = declspecs->locations[ds_type_spec];
13197 	      if (CLASS_PLACEHOLDER_TEMPLATE (auto_node))
13198 		error_at (tloc, "invalid use of template-name %qE without an "
13199 			  "argument list",
13200 			  CLASS_PLACEHOLDER_TEMPLATE (auto_node));
13201 	      else
13202 		error_at (tloc, "non-static data member declared with "
13203 			  "placeholder %qT", auto_node);
13204 	      type = error_mark_node;
13205 	    }
13206 
13207 	/* The C99 flexible array extension.  */
13208 	if (!staticp && TREE_CODE (type) == ARRAY_TYPE
13209 	    && TYPE_DOMAIN (type) == NULL_TREE)
13210 	  {
13211 	    if (ctype
13212 		&& (TREE_CODE (ctype) == UNION_TYPE
13213 		    || TREE_CODE (ctype) == QUAL_UNION_TYPE))
13214 	      {
13215 		error_at (id_loc, "flexible array member in union");
13216 		type = error_mark_node;
13217 	      }
13218 	    else
13219 	      {
13220 		/* Array is a flexible member.  */
13221 		if (name)
13222 		  pedwarn (id_loc, OPT_Wpedantic,
13223 			   "ISO C++ forbids flexible array member %qs", name);
13224 		else
13225 		  pedwarn (input_location, OPT_Wpedantic,
13226 			   "ISO C++ forbids flexible array members");
13227 
13228 		/* Flexible array member has a null domain.  */
13229 		type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
13230 	      }
13231 	  }
13232 
13233 	if (type == error_mark_node)
13234 	  {
13235 	    /* Happens when declaring arrays of sizes which
13236 	       are error_mark_node, for example.  */
13237 	    decl = NULL_TREE;
13238 	  }
13239 	else if (in_namespace && !friendp)
13240 	  {
13241 	    /* Something like struct S { int N::j; };  */
13242 	    error_at (id_loc, "invalid use of %<::%>");
13243 	    return error_mark_node;
13244 	  }
13245 	else if (FUNC_OR_METHOD_TYPE_P (type) && unqualified_id)
13246 	  {
13247 	    int publicp = 0;
13248 	    tree function_context;
13249 
13250 	    if (friendp == 0)
13251 	      {
13252 		/* This should never happen in pure C++ (the check
13253 		   could be an assert).  It could happen in
13254 		   Objective-C++ if someone writes invalid code that
13255 		   uses a function declaration for an instance
13256 		   variable or property (instance variables and
13257 		   properties are parsed as FIELD_DECLs, but they are
13258 		   part of an Objective-C class, not a C++ class).
13259 		   That code is invalid and is caught by this
13260 		   check.  */
13261 		if (!ctype)
13262 		  {
13263 		    error ("declaration of function %qD in invalid context",
13264 			   unqualified_id);
13265 		    return error_mark_node;
13266 		  }
13267 
13268 		/* ``A union may [ ... ] not [ have ] virtual functions.''
13269 		   ARM 9.5 */
13270 		if (virtualp && TREE_CODE (ctype) == UNION_TYPE)
13271 		  {
13272 		    error_at (declspecs->locations[ds_virtual],
13273 			      "function %qD declared %<virtual%> inside a union",
13274 			      unqualified_id);
13275 		    return error_mark_node;
13276 		  }
13277 
13278 		if (virtualp
13279 		    && identifier_p (unqualified_id)
13280 		    && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
13281 		  {
13282 		    error_at (declspecs->locations[ds_virtual],
13283 			      "%qD cannot be declared %<virtual%>, since it "
13284 			      "is always static", unqualified_id);
13285 		    virtualp = 0;
13286 		  }
13287 	      }
13288 
13289 	    /* Check that the name used for a destructor makes sense.  */
13290 	    if (sfk == sfk_destructor)
13291 	      {
13292 		tree uqname = id_declarator->u.id.unqualified_name;
13293 
13294 		if (!ctype)
13295 		  {
13296 		    gcc_assert (friendp);
13297 		    error_at (id_loc, "expected qualified name in friend "
13298 			      "declaration for destructor %qD", uqname);
13299 		    return error_mark_node;
13300 		  }
13301 
13302 		if (!check_dtor_name (ctype, TREE_OPERAND (uqname, 0)))
13303 		  {
13304 		    error_at (id_loc, "declaration of %qD as member of %qT",
13305 			      uqname, ctype);
13306 		    return error_mark_node;
13307 		  }
13308                 if (concept_p)
13309                   {
13310                     error_at (declspecs->locations[ds_concept],
13311 			      "a destructor cannot be %qs", "concept");
13312                     return error_mark_node;
13313                   }
13314 		if (constexpr_p && cxx_dialect < cxx2a)
13315 		  {
13316 		    error_at (declspecs->locations[ds_constexpr],
13317 			      "%<constexpr%> destructors only available"
13318 			      " with %<-std=c++2a%> or %<-std=gnu++2a%>");
13319 		    return error_mark_node;
13320 		  }
13321 		if (consteval_p)
13322 		  {
13323 		    error_at (declspecs->locations[ds_consteval],
13324 			      "a destructor cannot be %qs", "consteval");
13325 		    return error_mark_node;
13326 		  }
13327 	      }
13328 	    else if (sfk == sfk_constructor && friendp && !ctype)
13329 	      {
13330 		error ("expected qualified name in friend declaration "
13331 		       "for constructor %qD",
13332 		       id_declarator->u.id.unqualified_name);
13333 		return error_mark_node;
13334 	      }
13335 	    if (sfk == sfk_constructor)
13336 	      if (concept_p)
13337 		{
13338 		  error_at (declspecs->locations[ds_concept],
13339 			    "a constructor cannot be %<concept%>");
13340 		  return error_mark_node;
13341 		}
13342 	    if (concept_p)
13343 	      {
13344 		error_at (declspecs->locations[ds_concept],
13345 			  "a concept cannot be a member function");
13346 		concept_p = false;
13347 	      }
13348 	    else if (consteval_p
13349 		     && identifier_p (unqualified_id)
13350 		     && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
13351 	      {
13352 		error_at (declspecs->locations[ds_consteval],
13353 			  "%qD cannot be %qs", unqualified_id, "consteval");
13354 		consteval_p = false;
13355 	      }
13356 
13357 	    if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
13358 	      {
13359 		tree tmpl = TREE_OPERAND (unqualified_id, 0);
13360 		if (variable_template_p (tmpl))
13361 		  {
13362 		    error_at (id_loc, "specialization of variable template "
13363 			      "%qD declared as function", tmpl);
13364 		    inform (DECL_SOURCE_LOCATION (tmpl),
13365 			    "variable template declared here");
13366 		    return error_mark_node;
13367 		  }
13368 	      }
13369 
13370 	    /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node.  */
13371 	    function_context
13372 	      = (ctype != NULL_TREE
13373 		 ? decl_function_context (TYPE_MAIN_DECL (ctype)) : NULL_TREE);
13374 	    publicp = ((! friendp || ! staticp)
13375 		       && function_context == NULL_TREE);
13376 
13377 	    decl = grokfndecl (ctype, type,
13378 			       TREE_CODE (unqualified_id) != TEMPLATE_ID_EXPR
13379 			       ? unqualified_id : dname,
13380 			       parms,
13381 			       unqualified_id,
13382 			       declspecs,
13383 			       reqs,
13384 			       virtualp, flags, memfn_quals, rqual, raises,
13385 			       friendp ? -1 : 0, friendp, publicp,
13386 			       inlinep | (2 * constexpr_p) | (4 * concept_p)
13387 				       | (8 * consteval_p),
13388 			       initialized == SD_DELETED, sfk,
13389 			       funcdef_flag, late_return_type_p,
13390 			       initialized,
13391 			       template_count, in_namespace,
13392 			       attrlist, id_loc);
13393             decl = set_virt_specifiers (decl, virt_specifiers);
13394 	    if (decl == NULL_TREE)
13395 	      return error_mark_node;
13396 #if 0
13397 	    /* This clobbers the attrs stored in `decl' from `attrlist'.  */
13398 	    /* The decl and setting of decl_attr is also turned off.  */
13399 	    decl = build_decl_attribute_variant (decl, decl_attr);
13400 #endif
13401 
13402 	    /* [class.conv.ctor]
13403 
13404 	       A constructor declared without the function-specifier
13405 	       explicit that can be called with a single parameter
13406 	       specifies a conversion from the type of its first
13407 	       parameter to the type of its class.  Such a constructor
13408 	       is called a converting constructor.  */
13409 	    if (explicitp == 2)
13410 	      DECL_NONCONVERTING_P (decl) = 1;
13411 
13412 	    if (declspecs->explicit_specifier)
13413 	      store_explicit_specifier (decl, declspecs->explicit_specifier);
13414 	  }
13415 	else if (!staticp
13416 		 && ((current_class_type
13417 		      && same_type_p (type, current_class_type))
13418 		     || (!dependent_type_p (type)
13419 			 && !COMPLETE_TYPE_P (complete_type (type))
13420 			 && (!complete_or_array_type_p (type)
13421 			     || initialized == 0))))
13422 	  {
13423 	    if (TREE_CODE (type) != ARRAY_TYPE
13424 		|| !COMPLETE_TYPE_P (TREE_TYPE (type)))
13425 	      {
13426 		if (unqualified_id)
13427 		  {
13428 		    error_at (id_loc, "field %qD has incomplete type %qT",
13429 			      unqualified_id, type);
13430 		    cxx_incomplete_type_inform (strip_array_types (type));
13431 		  }
13432 		else
13433 		  error ("name %qT has incomplete type", type);
13434 
13435 		type = error_mark_node;
13436 		decl = NULL_TREE;
13437 	      }
13438 	  }
13439 	else if (!verify_type_context (input_location,
13440 				       staticp
13441 				       ? TCTX_STATIC_STORAGE
13442 				       : TCTX_FIELD, type))
13443 	  {
13444 	    type = error_mark_node;
13445 	    decl = NULL_TREE;
13446 	  }
13447 	else
13448 	  {
13449 	    if (friendp)
13450 	      {
13451 		if (unqualified_id)
13452 		  error_at (id_loc,
13453 			    "%qE is neither function nor member function; "
13454 			    "cannot be declared friend", unqualified_id);
13455 		else
13456 		  error ("unnamed field is neither function nor member "
13457 			 "function; cannot be declared friend");
13458 		return error_mark_node;
13459 	      }
13460 	    decl = NULL_TREE;
13461 	  }
13462 
13463 	if (friendp)
13464 	  {
13465 	    /* Friends are treated specially.  */
13466 	    if (ctype == current_class_type)
13467 	      ;  /* We already issued a permerror.  */
13468 	    else if (decl && DECL_NAME (decl))
13469 	      {
13470 		if (initialized)
13471 		  /* Kludge: We need funcdef_flag to be true in do_friend for
13472 		     in-class defaulted functions, but that breaks grokfndecl.
13473 		     So set it here.  */
13474 		  funcdef_flag = true;
13475 		if (template_class_depth (current_class_type) == 0)
13476 		  {
13477 		    decl = check_explicit_specialization
13478 		      (unqualified_id, decl, template_count,
13479 		       2 * funcdef_flag + 4);
13480 		    if (decl == error_mark_node)
13481 		      return error_mark_node;
13482 		  }
13483 
13484 		tree scope = ctype ? ctype : in_namespace;
13485 		decl = do_friend (scope, unqualified_id, decl,
13486 				  *attrlist, flags,
13487 				  funcdef_flag);
13488 		return decl;
13489 	      }
13490 	    else
13491 	      return error_mark_node;
13492 	  }
13493 
13494 	/* Structure field.  It may not be a function, except for C++.  */
13495 
13496 	if (decl == NULL_TREE)
13497 	  {
13498 	    if (staticp)
13499 	      {
13500 		/* C++ allows static class members.  All other work
13501 		   for this is done by grokfield.  */
13502 		decl = build_lang_decl_loc (id_loc, VAR_DECL,
13503 					    unqualified_id, type);
13504 		set_linkage_for_static_data_member (decl);
13505 		if (concept_p)
13506 		  error_at (declspecs->locations[ds_concept],
13507 			    "static data member %qE declared %qs",
13508 			    unqualified_id, "concept");
13509 		else if (constexpr_p && !initialized)
13510 		  {
13511 		    error_at (DECL_SOURCE_LOCATION (decl),
13512 			      "%<constexpr%> static data member %qD must "
13513 			      "have an initializer", decl);
13514 		    constexpr_p = false;
13515 		  }
13516 		if (consteval_p)
13517 		  error_at (declspecs->locations[ds_consteval],
13518 			    "static data member %qE declared %qs",
13519 			    unqualified_id, "consteval");
13520 
13521 		if (inlinep)
13522 		  mark_inline_variable (decl, declspecs->locations[ds_inline]);
13523 
13524 		if (!DECL_VAR_DECLARED_INLINE_P (decl)
13525 		    && !(cxx_dialect >= cxx17 && constexpr_p))
13526 		  /* Even if there is an in-class initialization, DECL
13527 		     is considered undefined until an out-of-class
13528 		     definition is provided, unless this is an inline
13529 		     variable.  */
13530 		  DECL_EXTERNAL (decl) = 1;
13531 
13532 		if (thread_p)
13533 		  {
13534 		    CP_DECL_THREAD_LOCAL_P (decl) = true;
13535 		    if (!processing_template_decl)
13536 		      set_decl_tls_model (decl, decl_default_tls_model (decl));
13537 		    if (declspecs->gnu_thread_keyword_p)
13538 		      SET_DECL_GNU_TLS_P (decl);
13539 		  }
13540 	      }
13541 	    else
13542 	      {
13543 		if (concept_p)
13544 		  {
13545 		    error_at (declspecs->locations[ds_concept],
13546 			      "non-static data member %qE declared %qs",
13547 			      unqualified_id, "concept");
13548 		    concept_p = false;
13549 		    constexpr_p = false;
13550 		  }
13551 		else if (constexpr_p)
13552 		  {
13553 		    error_at (declspecs->locations[ds_constexpr],
13554 			      "non-static data member %qE declared %qs",
13555 			      unqualified_id, "constexpr");
13556 		    constexpr_p = false;
13557 		  }
13558 		if (constinit_p)
13559 		  {
13560 		    error_at (declspecs->locations[ds_constinit],
13561 			      "non-static data member %qE declared %qs",
13562 			      unqualified_id, "constinit");
13563 		    constinit_p = false;
13564 		  }
13565 		if (consteval_p)
13566 		  {
13567 		    error_at (declspecs->locations[ds_consteval],
13568 			      "non-static data member %qE declared %qs",
13569 			      unqualified_id, "consteval");
13570 		    consteval_p = false;
13571 		  }
13572 		decl = build_decl (id_loc, FIELD_DECL, unqualified_id, type);
13573 		DECL_NONADDRESSABLE_P (decl) = bitfield;
13574 		if (bitfield && !unqualified_id)
13575 		  {
13576 		    TREE_NO_WARNING (decl) = 1;
13577 		    DECL_PADDING_P (decl) = 1;
13578 		  }
13579 
13580 		if (storage_class == sc_mutable)
13581 		  {
13582 		    DECL_MUTABLE_P (decl) = 1;
13583 		    storage_class = sc_none;
13584 		  }
13585 
13586 		if (initialized)
13587 		  {
13588 		    /* An attempt is being made to initialize a non-static
13589 		       member.  This is new in C++11.  */
13590 		    maybe_warn_cpp0x (CPP0X_NSDMI);
13591 
13592 		    /* If this has been parsed with static storage class, but
13593 		       errors forced staticp to be cleared, ensure NSDMI is
13594 		       not present.  */
13595 		    if (declspecs->storage_class == sc_static)
13596 		      DECL_INITIAL (decl) = error_mark_node;
13597 		  }
13598 	      }
13599 
13600 	    bad_specifiers (decl, BSP_FIELD, virtualp,
13601 			    memfn_quals != TYPE_UNQUALIFIED,
13602 			    staticp ? false : inlinep, friendp,
13603 			    raises != NULL_TREE,
13604 			    declspecs->locations);
13605 	  }
13606       }
13607     else if (FUNC_OR_METHOD_TYPE_P (type))
13608       {
13609 	tree original_name;
13610 	int publicp = 0;
13611 
13612 	if (!unqualified_id)
13613 	  return error_mark_node;
13614 
13615 	if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
13616 	  original_name = dname;
13617 	else
13618 	  original_name = unqualified_id;
13619 	// FIXME:gcc_assert (original_name == dname);
13620 
13621 	if (storage_class == sc_auto)
13622 	  error_at (declspecs->locations[ds_storage_class],
13623 		    "storage class %<auto%> invalid for function %qs", name);
13624 	else if (storage_class == sc_register)
13625 	  error_at (declspecs->locations[ds_storage_class],
13626 		    "storage class %<register%> invalid for function %qs",
13627 		    name);
13628 	else if (thread_p)
13629 	  {
13630 	    if (declspecs->gnu_thread_keyword_p)
13631 	      error_at (declspecs->locations[ds_thread],
13632 			"storage class %<__thread%> invalid for function %qs",
13633 			name);
13634 	    else
13635 	      error_at (declspecs->locations[ds_thread],
13636 			"storage class %<thread_local%> invalid for "
13637 			"function %qs", name);
13638 	  }
13639 
13640         if (virt_specifiers)
13641           error ("virt-specifiers in %qs not allowed outside a class "
13642 		 "definition", name);
13643 	/* Function declaration not at top level.
13644 	   Storage classes other than `extern' are not allowed
13645 	   and `extern' makes no difference.  */
13646 	if (! toplevel_bindings_p ()
13647 	    && (storage_class == sc_static
13648 		|| decl_spec_seq_has_spec_p (declspecs, ds_inline))
13649 	    && pedantic)
13650 	  {
13651 	    if (storage_class == sc_static)
13652 	      pedwarn (declspecs->locations[ds_storage_class], OPT_Wpedantic,
13653 		       "%<static%> specifier invalid for function %qs "
13654 		       "declared out of global scope", name);
13655 	    else
13656 	      pedwarn (declspecs->locations[ds_inline], OPT_Wpedantic,
13657 		       "%<inline%> specifier invalid for function %qs "
13658 		       "declared out of global scope", name);
13659 	  }
13660 
13661 	if (ctype == NULL_TREE)
13662 	  {
13663 	    if (virtualp)
13664 	      {
13665 		error ("virtual non-class function %qs", name);
13666 		virtualp = 0;
13667 	      }
13668 	    else if (sfk == sfk_constructor
13669 		     || sfk == sfk_destructor)
13670 	      {
13671 		error (funcdef_flag
13672 		       ? G_("%qs defined in a non-class scope")
13673 		       : G_("%qs declared in a non-class scope"), name);
13674 		sfk = sfk_none;
13675 	      }
13676 	  }
13677 	if (consteval_p
13678 	    && identifier_p (unqualified_id)
13679 	    && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
13680 	  {
13681 	    error_at (declspecs->locations[ds_consteval],
13682 		      "%qD cannot be %qs", unqualified_id, "consteval");
13683 	    consteval_p = false;
13684 	  }
13685 
13686 	/* Record whether the function is public.  */
13687 	publicp = (ctype != NULL_TREE
13688 		   || storage_class != sc_static);
13689 
13690 	decl = grokfndecl (ctype, type, original_name, parms, unqualified_id,
13691 			   declspecs,
13692                            reqs, virtualp, flags, memfn_quals, rqual, raises,
13693 			   1, friendp,
13694 			   publicp,
13695 			   inlinep | (2 * constexpr_p) | (4 * concept_p)
13696 				   | (8 * consteval_p),
13697 			   initialized == SD_DELETED,
13698                            sfk,
13699                            funcdef_flag,
13700 			   late_return_type_p,
13701 			   initialized,
13702 			   template_count, in_namespace, attrlist,
13703 			   id_loc);
13704 	if (decl == NULL_TREE)
13705 	  return error_mark_node;
13706 
13707 	if (explicitp == 2)
13708 	  DECL_NONCONVERTING_P (decl) = 1;
13709 	if (staticp == 1)
13710 	  {
13711 	    int invalid_static = 0;
13712 
13713 	    /* Don't allow a static member function in a class, and forbid
13714 	       declaring main to be static.  */
13715 	    if (TREE_CODE (type) == METHOD_TYPE)
13716 	      {
13717 		permerror (input_location, "cannot declare member function %qD to have "
13718 			   "static linkage", decl);
13719 		invalid_static = 1;
13720 	      }
13721 	    else if (current_function_decl)
13722 	      {
13723 		/* 7.1.1: There can be no static function declarations within a
13724 		   block.  */
13725 		error_at (declspecs->locations[ds_storage_class],
13726 			  "cannot declare static function inside another function");
13727 		invalid_static = 1;
13728 	      }
13729 
13730 	    if (invalid_static)
13731 	      {
13732 		staticp = 0;
13733 		storage_class = sc_none;
13734 	      }
13735 	  }
13736       }
13737     else
13738       {
13739 	/* It's a variable.  */
13740 
13741 	/* An uninitialized decl with `extern' is a reference.  */
13742 	decl = grokvardecl (type, dname, unqualified_id,
13743 			    declspecs,
13744 			    initialized,
13745 			    type_quals,
13746 			    inlinep,
13747 			    concept_p,
13748 			    template_count,
13749 			    ctype ? ctype : in_namespace,
13750 			    id_loc);
13751 	if (decl == NULL_TREE)
13752 	  return error_mark_node;
13753 
13754 	bad_specifiers (decl, BSP_VAR, virtualp,
13755 			memfn_quals != TYPE_UNQUALIFIED,
13756 			inlinep, friendp, raises != NULL_TREE,
13757 			declspecs->locations);
13758 
13759 	if (ctype)
13760 	  {
13761 	    DECL_CONTEXT (decl) = ctype;
13762 	    if (staticp == 1)
13763 	      {
13764 		permerror (declspecs->locations[ds_storage_class],
13765 			   "%<static%> may not be used when defining "
13766 			   "(as opposed to declaring) a static data member");
13767 		staticp = 0;
13768 		storage_class = sc_none;
13769 	      }
13770 	    if (storage_class == sc_register && TREE_STATIC (decl))
13771 	      {
13772 		error ("static member %qD declared %<register%>", decl);
13773 		storage_class = sc_none;
13774 	      }
13775 	    if (storage_class == sc_extern && pedantic)
13776 	      {
13777 		pedwarn (input_location, OPT_Wpedantic,
13778 			 "cannot explicitly declare member %q#D to have "
13779 			 "extern linkage", decl);
13780 		storage_class = sc_none;
13781 	      }
13782 	  }
13783 	else if (constexpr_p && DECL_EXTERNAL (decl))
13784 	  {
13785 	    error_at (DECL_SOURCE_LOCATION (decl),
13786 		      "declaration of %<constexpr%> variable %qD "
13787 		      "is not a definition", decl);
13788 	    constexpr_p = false;
13789 	  }
13790 	if (consteval_p)
13791 	  {
13792 	    error_at (DECL_SOURCE_LOCATION (decl),
13793 		      "a variable cannot be declared %<consteval%>");
13794 	    consteval_p = false;
13795 	  }
13796 
13797 	if (inlinep)
13798 	  mark_inline_variable (decl, declspecs->locations[ds_inline]);
13799 	if (innermost_code == cdk_decomp)
13800 	  {
13801 	    gcc_assert (declarator && declarator->kind == cdk_decomp);
13802 	    DECL_SOURCE_LOCATION (decl) = id_loc;
13803 	    DECL_ARTIFICIAL (decl) = 1;
13804 	    fit_decomposition_lang_decl (decl, NULL_TREE);
13805 	  }
13806       }
13807 
13808     if (VAR_P (decl) && !initialized)
13809       if (tree auto_node = type_uses_auto (type))
13810 	if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node))
13811 	  {
13812 	    location_t loc = declspecs->locations[ds_type_spec];
13813 	    error_at (loc, "declaration of %q#D has no initializer", decl);
13814 	    TREE_TYPE (decl) = error_mark_node;
13815 	  }
13816 
13817     if (storage_class == sc_extern && initialized && !funcdef_flag)
13818       {
13819 	if (toplevel_bindings_p ())
13820 	  {
13821 	    /* It's common practice (and completely valid) to have a const
13822 	       be initialized and declared extern.  */
13823 	    if (!(type_quals & TYPE_QUAL_CONST))
13824 	      warning_at (DECL_SOURCE_LOCATION (decl), 0,
13825 			  "%qs initialized and declared %<extern%>", name);
13826 	  }
13827 	else
13828 	  {
13829 	    error_at (DECL_SOURCE_LOCATION (decl),
13830 		      "%qs has both %<extern%> and initializer", name);
13831 	    return error_mark_node;
13832 	  }
13833       }
13834 
13835     /* Record `register' declaration for warnings on &
13836        and in case doing stupid register allocation.  */
13837 
13838     if (storage_class == sc_register)
13839       {
13840 	DECL_REGISTER (decl) = 1;
13841 	/* Warn about register storage specifiers on PARM_DECLs.  */
13842 	if (TREE_CODE (decl) == PARM_DECL)
13843 	  {
13844 	    if (cxx_dialect >= cxx17)
13845 	      pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
13846 		       "ISO C++17 does not allow %<register%> storage "
13847 		       "class specifier");
13848 	    else
13849 	      warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
13850 			  "%<register%> storage class specifier used");
13851 	  }
13852       }
13853     else if (storage_class == sc_extern)
13854       DECL_THIS_EXTERN (decl) = 1;
13855     else if (storage_class == sc_static)
13856       DECL_THIS_STATIC (decl) = 1;
13857 
13858     /* Set constexpr flag on vars (functions got it in grokfndecl).  */
13859     if (constexpr_p && VAR_P (decl))
13860       DECL_DECLARED_CONSTEXPR_P (decl) = true;
13861 
13862     /* Record constancy and volatility on the DECL itself .  There's
13863        no need to do this when processing a template; we'll do this
13864        for the instantiated declaration based on the type of DECL.  */
13865     if (!processing_template_decl)
13866       cp_apply_type_quals_to_decl (type_quals, decl);
13867 
13868     return decl;
13869   }
13870 }
13871 
13872 /* Subroutine of start_function.  Ensure that each of the parameter
13873    types (as listed in PARMS) is complete, as is required for a
13874    function definition.  */
13875 
13876 static void
require_complete_types_for_parms(tree parms)13877 require_complete_types_for_parms (tree parms)
13878 {
13879   for (; parms; parms = DECL_CHAIN (parms))
13880     {
13881       if (dependent_type_p (TREE_TYPE (parms)))
13882 	continue;
13883       if (!VOID_TYPE_P (TREE_TYPE (parms))
13884 	  && complete_type_or_else (TREE_TYPE (parms), parms))
13885 	{
13886 	  relayout_decl (parms);
13887 	  DECL_ARG_TYPE (parms) = type_passed_as (TREE_TYPE (parms));
13888 
13889 	  maybe_warn_parm_abi (TREE_TYPE (parms),
13890 			       DECL_SOURCE_LOCATION (parms));
13891 	}
13892       else
13893 	/* grokparms or complete_type_or_else will have already issued
13894 	   an error.  */
13895 	TREE_TYPE (parms) = error_mark_node;
13896     }
13897 }
13898 
13899 /* Returns nonzero if T is a local variable.  */
13900 
13901 int
local_variable_p(const_tree t)13902 local_variable_p (const_tree t)
13903 {
13904   if ((VAR_P (t)
13905        /* A VAR_DECL with a context that is a _TYPE is a static data
13906 	  member.  */
13907        && !TYPE_P (CP_DECL_CONTEXT (t))
13908        /* Any other non-local variable must be at namespace scope.  */
13909        && !DECL_NAMESPACE_SCOPE_P (t))
13910       || (TREE_CODE (t) == PARM_DECL))
13911     return 1;
13912 
13913   return 0;
13914 }
13915 
13916 /* Like local_variable_p, but suitable for use as a tree-walking
13917    function.  */
13918 
13919 static tree
local_variable_p_walkfn(tree * tp,int * walk_subtrees,void *)13920 local_variable_p_walkfn (tree *tp, int *walk_subtrees,
13921 			 void * /*data*/)
13922 {
13923   if (local_variable_p (*tp)
13924       && (!DECL_ARTIFICIAL (*tp) || DECL_NAME (*tp) == this_identifier))
13925     return *tp;
13926   else if (TYPE_P (*tp))
13927     *walk_subtrees = 0;
13928 
13929   return NULL_TREE;
13930 }
13931 
13932 /* Check that ARG, which is a default-argument expression for a
13933    parameter DECL, is valid.  Returns ARG, or ERROR_MARK_NODE, if
13934    something goes wrong.  DECL may also be a _TYPE node, rather than a
13935    DECL, if there is no DECL available.  */
13936 
13937 tree
check_default_argument(tree decl,tree arg,tsubst_flags_t complain)13938 check_default_argument (tree decl, tree arg, tsubst_flags_t complain)
13939 {
13940   tree var;
13941   tree decl_type;
13942 
13943   if (TREE_CODE (arg) == DEFERRED_PARSE)
13944     /* We get a DEFERRED_PARSE when looking at an in-class declaration
13945        with a default argument.  Ignore the argument for now; we'll
13946        deal with it after the class is complete.  */
13947     return arg;
13948 
13949   if (TYPE_P (decl))
13950     {
13951       decl_type = decl;
13952       decl = NULL_TREE;
13953     }
13954   else
13955     decl_type = TREE_TYPE (decl);
13956 
13957   if (arg == error_mark_node
13958       || decl == error_mark_node
13959       || TREE_TYPE (arg) == error_mark_node
13960       || decl_type == error_mark_node)
13961     /* Something already went wrong.  There's no need to check
13962        further.  */
13963     return error_mark_node;
13964 
13965   /* [dcl.fct.default]
13966 
13967      A default argument expression is implicitly converted to the
13968      parameter type.  */
13969   ++cp_unevaluated_operand;
13970   /* Avoid digest_init clobbering the initializer.  */
13971   tree carg = BRACE_ENCLOSED_INITIALIZER_P (arg) ? unshare_expr (arg): arg;
13972   perform_implicit_conversion_flags (decl_type, carg, complain,
13973 				     LOOKUP_IMPLICIT);
13974   --cp_unevaluated_operand;
13975 
13976   /* Avoid redundant -Wzero-as-null-pointer-constant warnings at
13977      the call sites.  */
13978   if (TYPE_PTR_OR_PTRMEM_P (decl_type)
13979       && null_ptr_cst_p (arg)
13980       /* Don't lose side-effects as in PR90473.  */
13981       && !TREE_SIDE_EFFECTS (arg))
13982     return nullptr_node;
13983 
13984   /* [dcl.fct.default]
13985 
13986      Local variables shall not be used in default argument
13987      expressions.
13988 
13989      The keyword `this' shall not be used in a default argument of a
13990      member function.  */
13991   var = cp_walk_tree_without_duplicates (&arg, local_variable_p_walkfn, NULL);
13992   if (var)
13993     {
13994       if (complain & tf_warning_or_error)
13995 	{
13996 	  if (DECL_NAME (var) == this_identifier)
13997 	    permerror (input_location, "default argument %qE uses %qD",
13998 		       arg, var);
13999 	  else
14000 	    error ("default argument %qE uses local variable %qD", arg, var);
14001 	}
14002       return error_mark_node;
14003     }
14004 
14005   /* All is well.  */
14006   return arg;
14007 }
14008 
14009 /* Returns a deprecated type used within TYPE, or NULL_TREE if none.  */
14010 
14011 static tree
type_is_deprecated(tree type)14012 type_is_deprecated (tree type)
14013 {
14014   enum tree_code code;
14015   if (TREE_DEPRECATED (type))
14016     return type;
14017   if (TYPE_NAME (type))
14018     {
14019       if (TREE_DEPRECATED (TYPE_NAME (type)))
14020 	return type;
14021       else
14022 	{
14023 	  cp_warn_deprecated_use_scopes (CP_DECL_CONTEXT (TYPE_NAME (type)));
14024 	  return NULL_TREE;
14025 	}
14026     }
14027 
14028   /* Do warn about using typedefs to a deprecated class.  */
14029   if (OVERLOAD_TYPE_P (type) && type != TYPE_MAIN_VARIANT (type))
14030     return type_is_deprecated (TYPE_MAIN_VARIANT (type));
14031 
14032   code = TREE_CODE (type);
14033 
14034   if (code == POINTER_TYPE || code == REFERENCE_TYPE
14035       || code == OFFSET_TYPE || code == FUNCTION_TYPE
14036       || code == METHOD_TYPE || code == ARRAY_TYPE)
14037     return type_is_deprecated (TREE_TYPE (type));
14038 
14039   if (TYPE_PTRMEMFUNC_P (type))
14040     return type_is_deprecated
14041       (TREE_TYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type))));
14042 
14043   return NULL_TREE;
14044 }
14045 
14046 /* Decode the list of parameter types for a function type.
14047    Given the list of things declared inside the parens,
14048    return a list of types.
14049 
14050    If this parameter does not end with an ellipsis, we append
14051    void_list_node.
14052 
14053    *PARMS is set to the chain of PARM_DECLs created.  */
14054 
14055 tree
grokparms(tree parmlist,tree * parms)14056 grokparms (tree parmlist, tree *parms)
14057 {
14058   tree result = NULL_TREE;
14059   tree decls = NULL_TREE;
14060   tree parm;
14061   int any_error = 0;
14062 
14063   for (parm = parmlist; parm != NULL_TREE; parm = TREE_CHAIN (parm))
14064     {
14065       tree type = NULL_TREE;
14066       tree init = TREE_PURPOSE (parm);
14067       tree decl = TREE_VALUE (parm);
14068 
14069       if (parm == void_list_node)
14070 	break;
14071 
14072       if (! decl || TREE_TYPE (decl) == error_mark_node)
14073 	continue;
14074 
14075       type = TREE_TYPE (decl);
14076       if (VOID_TYPE_P (type))
14077 	{
14078 	  if (same_type_p (type, void_type_node)
14079 	      && !init
14080 	      && !DECL_NAME (decl) && !result
14081 	      && TREE_CHAIN (parm) == void_list_node)
14082 	    /* DR 577: A parameter list consisting of a single
14083 	       unnamed parameter of non-dependent type 'void'.  */
14084 	    break;
14085 	  else if (cv_qualified_p (type))
14086 	    error_at (DECL_SOURCE_LOCATION (decl),
14087 		      "invalid use of cv-qualified type %qT in "
14088 		      "parameter declaration", type);
14089 	  else
14090 	    error_at (DECL_SOURCE_LOCATION (decl),
14091 		      "invalid use of type %<void%> in parameter "
14092 		      "declaration");
14093 	  /* It's not a good idea to actually create parameters of
14094 	     type `void'; other parts of the compiler assume that a
14095 	     void type terminates the parameter list.  */
14096 	  type = error_mark_node;
14097 	  TREE_TYPE (decl) = error_mark_node;
14098 	}
14099 
14100       if (type != error_mark_node)
14101 	{
14102 	  if (deprecated_state != DEPRECATED_SUPPRESS)
14103 	    {
14104 	      tree deptype = type_is_deprecated (type);
14105 	      if (deptype)
14106 		cp_warn_deprecated_use (deptype);
14107 	    }
14108 
14109 	  /* [dcl.fct] "A parameter with volatile-qualified type is
14110 	     deprecated."  */
14111 	  if (CP_TYPE_VOLATILE_P (type))
14112 	    warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wvolatile,
14113 			"%<volatile%>-qualified parameter is "
14114 			"deprecated");
14115 
14116 	  /* Top-level qualifiers on the parameters are
14117 	     ignored for function types.  */
14118 	  type = cp_build_qualified_type (type, 0);
14119 	  if (TREE_CODE (type) == METHOD_TYPE)
14120 	    {
14121 	      error ("parameter %qD invalidly declared method type", decl);
14122 	      type = build_pointer_type (type);
14123 	      TREE_TYPE (decl) = type;
14124 	    }
14125 	  else if (abstract_virtuals_error (decl, type))
14126 	    any_error = 1;  /* Seems like a good idea.  */
14127 	  else if (cxx_dialect < cxx17 && INDIRECT_TYPE_P (type))
14128 	    {
14129 	      /* Before C++17 DR 393:
14130 		 [dcl.fct]/6, parameter types cannot contain pointers
14131 		 (references) to arrays of unknown bound.  */
14132 	      tree t = TREE_TYPE (type);
14133 	      int ptr = TYPE_PTR_P (type);
14134 
14135 	      while (1)
14136 		{
14137 		  if (TYPE_PTR_P (t))
14138 		    ptr = 1;
14139 		  else if (TREE_CODE (t) != ARRAY_TYPE)
14140 		    break;
14141 		  else if (!TYPE_DOMAIN (t))
14142 		    break;
14143 		  t = TREE_TYPE (t);
14144 		}
14145 	      if (TREE_CODE (t) == ARRAY_TYPE)
14146 		pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
14147 			 ptr
14148 			 ? G_("parameter %qD includes pointer to array of "
14149 			      "unknown bound %qT")
14150 			 : G_("parameter %qD includes reference to array of "
14151 			      "unknown bound %qT"),
14152 			 decl, t);
14153 	    }
14154 
14155 	  if (any_error)
14156 	    init = NULL_TREE;
14157 	  else if (init && !processing_template_decl)
14158 	    init = check_default_argument (decl, init, tf_warning_or_error);
14159 	}
14160 
14161       DECL_CHAIN (decl) = decls;
14162       decls = decl;
14163       result = tree_cons (init, type, result);
14164     }
14165   decls = nreverse (decls);
14166   result = nreverse (result);
14167   if (parm)
14168     result = chainon (result, void_list_node);
14169   *parms = decls;
14170 
14171   return result;
14172 }
14173 
14174 
14175 /* D is a constructor or overloaded `operator='.
14176 
14177    Let T be the class in which D is declared. Then, this function
14178    returns:
14179 
14180    -1 if D's is an ill-formed constructor or copy assignment operator
14181       whose first parameter is of type `T'.
14182    0  if D is not a copy constructor or copy assignment
14183       operator.
14184    1  if D is a copy constructor or copy assignment operator whose
14185       first parameter is a reference to non-const qualified T.
14186    2  if D is a copy constructor or copy assignment operator whose
14187       first parameter is a reference to const qualified T.
14188 
14189    This function can be used as a predicate. Positive values indicate
14190    a copy constructor and nonzero values indicate a copy assignment
14191    operator.  */
14192 
14193 int
copy_fn_p(const_tree d)14194 copy_fn_p (const_tree d)
14195 {
14196   tree args;
14197   tree arg_type;
14198   int result = 1;
14199 
14200   gcc_assert (DECL_FUNCTION_MEMBER_P (d));
14201 
14202   if (TREE_CODE (d) == TEMPLATE_DECL
14203       || (DECL_TEMPLATE_INFO (d)
14204 	  && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
14205     /* Instantiations of template member functions are never copy
14206        functions.  Note that member functions of templated classes are
14207        represented as template functions internally, and we must
14208        accept those as copy functions.  */
14209     return 0;
14210 
14211   args = FUNCTION_FIRST_USER_PARMTYPE (d);
14212   if (!args)
14213     return 0;
14214 
14215   arg_type = TREE_VALUE (args);
14216   if (arg_type == error_mark_node)
14217     return 0;
14218 
14219   if (TYPE_MAIN_VARIANT (arg_type) == DECL_CONTEXT (d))
14220     {
14221       /* Pass by value copy assignment operator.  */
14222       result = -1;
14223     }
14224   else if (TYPE_REF_P (arg_type)
14225 	   && !TYPE_REF_IS_RVALUE (arg_type)
14226 	   && TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)) == DECL_CONTEXT (d))
14227     {
14228       if (CP_TYPE_CONST_P (TREE_TYPE (arg_type)))
14229 	result = 2;
14230     }
14231   else
14232     return 0;
14233 
14234   args = TREE_CHAIN (args);
14235 
14236   if (args && args != void_list_node && !TREE_PURPOSE (args))
14237     /* There are more non-optional args.  */
14238     return 0;
14239 
14240   return result;
14241 }
14242 
14243 /* D is a constructor or overloaded `operator='.
14244 
14245    Let T be the class in which D is declared. Then, this function
14246    returns true when D is a move constructor or move assignment
14247    operator, false otherwise.  */
14248 
14249 bool
move_fn_p(const_tree d)14250 move_fn_p (const_tree d)
14251 {
14252   gcc_assert (DECL_FUNCTION_MEMBER_P (d));
14253 
14254   if (cxx_dialect == cxx98)
14255     /* There are no move constructors if we are in C++98 mode.  */
14256     return false;
14257 
14258   if (TREE_CODE (d) == TEMPLATE_DECL
14259       || (DECL_TEMPLATE_INFO (d)
14260          && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
14261     /* Instantiations of template member functions are never move
14262        functions.  Note that member functions of templated classes are
14263        represented as template functions internally, and we must
14264        accept those as move functions.  */
14265     return 0;
14266 
14267   return move_signature_fn_p (d);
14268 }
14269 
14270 /* D is a constructor or overloaded `operator='.
14271 
14272    Then, this function returns true when D has the same signature as a move
14273    constructor or move assignment operator (because either it is such a
14274    ctor/op= or it is a template specialization with the same signature),
14275    false otherwise.  */
14276 
14277 bool
move_signature_fn_p(const_tree d)14278 move_signature_fn_p (const_tree d)
14279 {
14280   tree args;
14281   tree arg_type;
14282   bool result = false;
14283 
14284   args = FUNCTION_FIRST_USER_PARMTYPE (d);
14285   if (!args)
14286     return 0;
14287 
14288   arg_type = TREE_VALUE (args);
14289   if (arg_type == error_mark_node)
14290     return 0;
14291 
14292   if (TYPE_REF_P (arg_type)
14293       && TYPE_REF_IS_RVALUE (arg_type)
14294       && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)),
14295                       DECL_CONTEXT (d)))
14296     result = true;
14297 
14298   args = TREE_CHAIN (args);
14299 
14300   if (args && args != void_list_node && !TREE_PURPOSE (args))
14301     /* There are more non-optional args.  */
14302     return false;
14303 
14304   return result;
14305 }
14306 
14307 /* Remember any special properties of member function DECL.  */
14308 
14309 void
grok_special_member_properties(tree decl)14310 grok_special_member_properties (tree decl)
14311 {
14312   tree class_type;
14313 
14314   if (TREE_CODE (decl) == USING_DECL
14315       || !DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
14316     return;
14317 
14318   class_type = DECL_CONTEXT (decl);
14319   if (IDENTIFIER_CTOR_P (DECL_NAME (decl)))
14320     {
14321       int ctor = copy_fn_p (decl);
14322 
14323       if (!DECL_ARTIFICIAL (decl))
14324 	TYPE_HAS_USER_CONSTRUCTOR (class_type) = 1;
14325 
14326       if (ctor > 0)
14327 	{
14328 	  /* [class.copy]
14329 
14330 	     A non-template constructor for class X is a copy
14331 	     constructor if its first parameter is of type X&, const
14332 	     X&, volatile X& or const volatile X&, and either there
14333 	     are no other parameters or else all other parameters have
14334 	     default arguments.  */
14335 	  TYPE_HAS_COPY_CTOR (class_type) = 1;
14336 	  if (ctor > 1)
14337 	    TYPE_HAS_CONST_COPY_CTOR (class_type) = 1;
14338 	}
14339       else if (sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (decl)))
14340 	TYPE_HAS_DEFAULT_CONSTRUCTOR (class_type) = 1;
14341       else if (is_list_ctor (decl))
14342 	TYPE_HAS_LIST_CTOR (class_type) = 1;
14343 
14344       if (DECL_DECLARED_CONSTEXPR_P (decl)
14345 	  && !ctor && !move_fn_p (decl))
14346 	TYPE_HAS_CONSTEXPR_CTOR (class_type) = 1;
14347     }
14348   else if (DECL_NAME (decl) == assign_op_identifier)
14349     {
14350       /* [class.copy]
14351 
14352 	 A non-template assignment operator for class X is a copy
14353 	 assignment operator if its parameter is of type X, X&, const
14354 	 X&, volatile X& or const volatile X&.  */
14355 
14356       int assop = copy_fn_p (decl);
14357 
14358       if (assop)
14359 	{
14360 	  TYPE_HAS_COPY_ASSIGN (class_type) = 1;
14361 	  if (assop != 1)
14362 	    TYPE_HAS_CONST_COPY_ASSIGN (class_type) = 1;
14363 	}
14364     }
14365   else if (IDENTIFIER_CONV_OP_P (DECL_NAME (decl)))
14366     TYPE_HAS_CONVERSION (class_type) = true;
14367 
14368   /* Destructors are handled in check_methods.  */
14369 }
14370 
14371 /* Check a constructor DECL has the correct form.  Complains
14372    if the class has a constructor of the form X(X).  */
14373 
14374 bool
grok_ctor_properties(const_tree ctype,const_tree decl)14375 grok_ctor_properties (const_tree ctype, const_tree decl)
14376 {
14377   int ctor_parm = copy_fn_p (decl);
14378 
14379   if (ctor_parm < 0)
14380     {
14381       /* [class.copy]
14382 
14383 	 A declaration of a constructor for a class X is ill-formed if
14384 	 its first parameter is of type (optionally cv-qualified) X
14385 	 and either there are no other parameters or else all other
14386 	 parameters have default arguments.
14387 
14388 	 We *don't* complain about member template instantiations that
14389 	 have this form, though; they can occur as we try to decide
14390 	 what constructor to use during overload resolution.  Since
14391 	 overload resolution will never prefer such a constructor to
14392 	 the non-template copy constructor (which is either explicitly
14393 	 or implicitly defined), there's no need to worry about their
14394 	 existence.  Theoretically, they should never even be
14395 	 instantiated, but that's hard to forestall.  */
14396       error_at (DECL_SOURCE_LOCATION (decl),
14397 		"invalid constructor; you probably meant %<%T (const %T&)%>",
14398 		ctype, ctype);
14399       return false;
14400     }
14401 
14402   return true;
14403 }
14404 
14405 /* DECL is a declaration for an overloaded or conversion operator.  If
14406    COMPLAIN is true, errors are issued for invalid declarations.  */
14407 
14408 bool
grok_op_properties(tree decl,bool complain)14409 grok_op_properties (tree decl, bool complain)
14410 {
14411   tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
14412   bool methodp = TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE;
14413   tree name = DECL_NAME (decl);
14414   location_t loc = DECL_SOURCE_LOCATION (decl);
14415 
14416   tree class_type = DECL_CONTEXT (decl);
14417   if (class_type && !CLASS_TYPE_P (class_type))
14418     class_type = NULL_TREE;
14419 
14420   tree_code operator_code;
14421   unsigned op_flags;
14422   if (IDENTIFIER_CONV_OP_P (name))
14423     {
14424       /* Conversion operators are TYPE_EXPR for the purposes of this
14425 	 function.  */
14426       operator_code = TYPE_EXPR;
14427       op_flags = OVL_OP_FLAG_UNARY;
14428     }
14429   else
14430     {
14431       const ovl_op_info_t *ovl_op = IDENTIFIER_OVL_OP_INFO (name);
14432 
14433       operator_code = ovl_op->tree_code;
14434       op_flags = ovl_op->flags;
14435       gcc_checking_assert (operator_code != ERROR_MARK);
14436       DECL_OVERLOADED_OPERATOR_CODE_RAW (decl) = ovl_op->ovl_op_code;
14437     }
14438 
14439   if (op_flags & OVL_OP_FLAG_ALLOC)
14440     {
14441       /* operator new and operator delete are quite special.  */
14442       if (class_type)
14443 	switch (op_flags)
14444 	  {
14445 	  case OVL_OP_FLAG_ALLOC:
14446 	    TYPE_HAS_NEW_OPERATOR (class_type) = 1;
14447 	    break;
14448 
14449 	  case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_DELETE:
14450 	    TYPE_GETS_DELETE (class_type) |= 1;
14451 	    break;
14452 
14453 	  case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_VEC:
14454 	    TYPE_HAS_ARRAY_NEW_OPERATOR (class_type) = 1;
14455 	    break;
14456 
14457 	  case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_DELETE | OVL_OP_FLAG_VEC:
14458 	    TYPE_GETS_DELETE (class_type) |= 2;
14459 	    break;
14460 
14461 	  default:
14462 	    gcc_unreachable ();
14463 	  }
14464 
14465       /* [basic.std.dynamic.allocation]/1:
14466 
14467 	 A program is ill-formed if an allocation function is declared
14468 	 in a namespace scope other than global scope or declared
14469 	 static in global scope.
14470 
14471 	 The same also holds true for deallocation functions.  */
14472       if (DECL_NAMESPACE_SCOPE_P (decl))
14473 	{
14474 	  if (CP_DECL_CONTEXT (decl) != global_namespace)
14475 	    {
14476 	      error_at (loc, "%qD may not be declared within a namespace",
14477 			decl);
14478 	      return false;
14479 	    }
14480 
14481 	  if (!TREE_PUBLIC (decl))
14482 	    {
14483 	      error_at (loc, "%qD may not be declared as static", decl);
14484 	      return false;
14485 	    }
14486 	}
14487 
14488       if (op_flags & OVL_OP_FLAG_DELETE)
14489 	{
14490 	  DECL_SET_IS_OPERATOR_DELETE (decl, true);
14491 	  coerce_delete_type (decl, loc);
14492 	}
14493       else
14494 	{
14495 	  DECL_SET_IS_OPERATOR_NEW (decl, true);
14496 	  TREE_TYPE (decl) = coerce_new_type (TREE_TYPE (decl), loc);
14497 	}
14498 
14499       return true;
14500     }
14501 
14502   /* An operator function must either be a non-static member function
14503      or have at least one parameter of a class, a reference to a class,
14504      an enumeration, or a reference to an enumeration.  13.4.0.6 */
14505   if (! methodp || DECL_STATIC_FUNCTION_P (decl))
14506     {
14507       if (operator_code == TYPE_EXPR
14508 	  || operator_code == CALL_EXPR
14509 	  || operator_code == COMPONENT_REF
14510 	  || operator_code == ARRAY_REF
14511 	  || operator_code == NOP_EXPR)
14512 	{
14513 	  error_at (loc, "%qD must be a nonstatic member function", decl);
14514 	  return false;
14515 	}
14516 
14517       if (DECL_STATIC_FUNCTION_P (decl))
14518 	{
14519 	  error_at (loc, "%qD must be either a non-static member "
14520 		    "function or a non-member function", decl);
14521 	  return false;
14522 	}
14523 
14524       for (tree arg = argtypes; ; arg = TREE_CHAIN (arg))
14525 	{
14526 	  if (!arg || arg == void_list_node)
14527 	    {
14528 	      if (complain)
14529 		error_at(loc, "%qD must have an argument of class or "
14530 			 "enumerated type", decl);
14531 	      return false;
14532 	    }
14533 
14534 	  tree type = non_reference (TREE_VALUE (arg));
14535 	  if (type == error_mark_node)
14536 	    return false;
14537 
14538 	  /* MAYBE_CLASS_TYPE_P, rather than CLASS_TYPE_P, is used
14539 	     because these checks are performed even on template
14540 	     functions.  */
14541 	  if (MAYBE_CLASS_TYPE_P (type)
14542 	      || TREE_CODE (type) == ENUMERAL_TYPE)
14543 	    break;
14544 	}
14545     }
14546 
14547   if (operator_code == CALL_EXPR)
14548     /* There are no further restrictions on the arguments to an overloaded
14549        "operator ()".  */
14550     return true;
14551 
14552   if (operator_code == COND_EXPR)
14553     {
14554       /* 13.4.0.3 */
14555       error_at (loc, "ISO C++ prohibits overloading %<operator ?:%>");
14556       return false;
14557     }
14558 
14559   /* Count the number of arguments and check for ellipsis.  */
14560   int arity = 0;
14561   for (tree arg = argtypes; arg != void_list_node; arg = TREE_CHAIN (arg))
14562     {
14563       if (!arg)
14564 	{
14565 	  /* Variadic.  */
14566 	  error_at (loc, "%qD must not have variable number of arguments",
14567 		    decl);
14568 	  return false;
14569 	}
14570       ++arity;
14571     }
14572 
14573   /* Verify correct number of arguments.  */
14574   switch (op_flags)
14575     {
14576     case OVL_OP_FLAG_AMBIARY:
14577       if (arity == 1)
14578 	{
14579 	  /* We have a unary instance of an ambi-ary op.  Remap to the
14580 	     unary one.  */
14581 	  unsigned alt = ovl_op_alternate[ovl_op_mapping [operator_code]];
14582 	  const ovl_op_info_t *ovl_op = &ovl_op_info[false][alt];
14583 	  gcc_checking_assert (ovl_op->flags == OVL_OP_FLAG_UNARY);
14584 	  operator_code = ovl_op->tree_code;
14585 	  DECL_OVERLOADED_OPERATOR_CODE_RAW (decl) = ovl_op->ovl_op_code;
14586 	}
14587       else if (arity != 2)
14588 	{
14589 	  /* This was an ambiguous operator but is invalid. */
14590 	  error_at (loc,
14591 		    methodp
14592 		    ? G_("%qD must have either zero or one argument")
14593 		    : G_("%qD must have either one or two arguments"), decl);
14594 	  return false;
14595 	}
14596       else if ((operator_code == POSTINCREMENT_EXPR
14597 		|| operator_code == POSTDECREMENT_EXPR)
14598 	       && ! processing_template_decl
14599 	       /* x++ and x--'s second argument must be an int.  */
14600 	       && ! same_type_p (TREE_VALUE (TREE_CHAIN (argtypes)),
14601 				 integer_type_node))
14602 	{
14603 	  error_at (loc,
14604 		    methodp
14605 		    ? G_("postfix %qD must have %<int%> as its argument")
14606 		    : G_("postfix %qD must have %<int%> as its second argument"),
14607 		    decl);
14608 	  return false;
14609 	}
14610       break;
14611 
14612     case OVL_OP_FLAG_UNARY:
14613       if (arity != 1)
14614 	{
14615 	  error_at (loc,
14616 		    methodp
14617 		    ? G_("%qD must have no arguments")
14618 		    : G_("%qD must have exactly one argument"), decl);
14619 	  return false;
14620 	}
14621       break;
14622 
14623     case OVL_OP_FLAG_BINARY:
14624       if (arity != 2)
14625 	{
14626 	  error_at (loc,
14627 		    methodp
14628 		    ? G_("%qD must have exactly one argument")
14629 		    : G_("%qD must have exactly two arguments"), decl);
14630 	  return false;
14631 	}
14632       break;
14633 
14634     default:
14635       gcc_unreachable ();
14636     }
14637 
14638   /* There can be no default arguments.  */
14639   for (tree arg = argtypes; arg != void_list_node; arg = TREE_CHAIN (arg))
14640     if (TREE_PURPOSE (arg))
14641       {
14642 	TREE_PURPOSE (arg) = NULL_TREE;
14643 	error_at (loc, "%qD cannot have default arguments", decl);
14644 	return false;
14645       }
14646 
14647   /* At this point the declaration is well-formed.  It may not be
14648      sensible though.  */
14649 
14650   /* Check member function warnings only on the in-class declaration.
14651      There's no point warning on an out-of-class definition.  */
14652   if (class_type && class_type != current_class_type)
14653     return true;
14654 
14655   /* Warn about conversion operators that will never be used.  */
14656   if (IDENTIFIER_CONV_OP_P (name)
14657       && ! DECL_TEMPLATE_INFO (decl)
14658       && warn_class_conversion)
14659     {
14660       tree t = TREE_TYPE (name);
14661       int ref = TYPE_REF_P (t);
14662 
14663       if (ref)
14664 	t = TYPE_MAIN_VARIANT (TREE_TYPE (t));
14665 
14666       if (VOID_TYPE_P (t))
14667 	warning_at (loc, OPT_Wclass_conversion, "converting %qT to %<void%> "
14668 		    "will never use a type conversion operator", class_type);
14669       else if (class_type)
14670 	{
14671 	  if (same_type_ignoring_top_level_qualifiers_p (t, class_type))
14672 	    warning_at (loc, OPT_Wclass_conversion,
14673 			ref
14674 			? G_("converting %qT to a reference to the same type "
14675 			     "will never use a type conversion operator")
14676 			: G_("converting %qT to the same type "
14677 			     "will never use a type conversion operator"),
14678 			class_type);
14679 	  /* Don't force t to be complete here.  */
14680 	  else if (MAYBE_CLASS_TYPE_P (t)
14681 		   && COMPLETE_TYPE_P (t)
14682 		   && DERIVED_FROM_P (t, class_type))
14683 	    warning_at (loc, OPT_Wclass_conversion,
14684 			ref
14685 			? G_("converting %qT to a reference to a base class "
14686 			     "%qT will never use a type conversion operator")
14687 			: G_("converting %qT to a base class %qT "
14688 			     "will never use a type conversion operator"),
14689 			class_type, t);
14690 	}
14691     }
14692 
14693   if (!warn_ecpp)
14694     return true;
14695 
14696   /* Effective C++ rules below.  */
14697 
14698   /* More Effective C++ rule 7.  */
14699   if (operator_code == TRUTH_ANDIF_EXPR
14700       || operator_code == TRUTH_ORIF_EXPR
14701       || operator_code == COMPOUND_EXPR)
14702     warning_at (loc, OPT_Weffc__,
14703 		"user-defined %qD always evaluates both arguments", decl);
14704 
14705   /* More Effective C++ rule 6.  */
14706   if (operator_code == POSTINCREMENT_EXPR
14707       || operator_code == POSTDECREMENT_EXPR
14708       || operator_code == PREINCREMENT_EXPR
14709       || operator_code == PREDECREMENT_EXPR)
14710     {
14711       tree arg = TREE_VALUE (argtypes);
14712       tree ret = TREE_TYPE (TREE_TYPE (decl));
14713       if (methodp || TYPE_REF_P (arg))
14714 	arg = TREE_TYPE (arg);
14715       arg = TYPE_MAIN_VARIANT (arg);
14716 
14717       if (operator_code == PREINCREMENT_EXPR
14718 	  || operator_code == PREDECREMENT_EXPR)
14719 	{
14720 	  if (!TYPE_REF_P (ret)
14721 	      || !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (ret)), arg))
14722 	    warning_at (loc, OPT_Weffc__, "prefix %qD should return %qT", decl,
14723 			build_reference_type (arg));
14724 	}
14725       else
14726 	{
14727 	  if (!same_type_p (TYPE_MAIN_VARIANT (ret), arg))
14728 	    warning_at (loc, OPT_Weffc__,
14729 			"postfix %qD should return %qT", decl, arg);
14730 	}
14731     }
14732 
14733   /* Effective C++ rule 23.  */
14734   if (!DECL_ASSIGNMENT_OPERATOR_P (decl)
14735       && (operator_code == PLUS_EXPR
14736 	  || operator_code == MINUS_EXPR
14737 	  || operator_code == TRUNC_DIV_EXPR
14738 	  || operator_code == MULT_EXPR
14739 	  || operator_code == TRUNC_MOD_EXPR)
14740       && TYPE_REF_P (TREE_TYPE (TREE_TYPE (decl))))
14741     warning_at (loc, OPT_Weffc__, "%qD should return by value", decl);
14742 
14743   return true;
14744 }
14745 
14746 /* Return a string giving the keyword associate with CODE.  */
14747 
14748 static const char *
tag_name(enum tag_types code)14749 tag_name (enum tag_types code)
14750 {
14751   switch (code)
14752     {
14753     case record_type:
14754       return "struct";
14755     case class_type:
14756       return "class";
14757     case union_type:
14758       return "union";
14759     case enum_type:
14760       return "enum";
14761     case typename_type:
14762       return "typename";
14763     default:
14764       gcc_unreachable ();
14765     }
14766 }
14767 
14768 /* Name lookup in an elaborated-type-specifier (after the keyword
14769    indicated by TAG_CODE) has found the TYPE_DECL DECL.  If the
14770    elaborated-type-specifier is invalid, issue a diagnostic and return
14771    error_mark_node; otherwise, return the *_TYPE to which it referred.
14772    If ALLOW_TEMPLATE_P is true, TYPE may be a class template.  */
14773 
14774 tree
check_elaborated_type_specifier(enum tag_types tag_code,tree decl,bool allow_template_p)14775 check_elaborated_type_specifier (enum tag_types tag_code,
14776 				 tree decl,
14777 				 bool allow_template_p)
14778 {
14779   tree type;
14780 
14781   /* In the case of:
14782 
14783        struct S { struct S *p; };
14784 
14785      name lookup will find the TYPE_DECL for the implicit "S::S"
14786      typedef.  Adjust for that here.  */
14787   if (DECL_SELF_REFERENCE_P (decl))
14788     decl = TYPE_NAME (TREE_TYPE (decl));
14789 
14790   type = TREE_TYPE (decl);
14791 
14792   /* Check TEMPLATE_TYPE_PARM first because DECL_IMPLICIT_TYPEDEF_P
14793      is false for this case as well.  */
14794   if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
14795     {
14796       error ("using template type parameter %qT after %qs",
14797 	     type, tag_name (tag_code));
14798       return error_mark_node;
14799     }
14800   /* Accept template template parameters.  */
14801   else if (allow_template_p
14802 	   && (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM
14803 	       || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM))
14804     ;
14805   /*   [dcl.type.elab]
14806 
14807        If the identifier resolves to a typedef-name or the
14808        simple-template-id resolves to an alias template
14809        specialization, the elaborated-type-specifier is ill-formed.
14810 
14811      In other words, the only legitimate declaration to use in the
14812      elaborated type specifier is the implicit typedef created when
14813      the type is declared.  */
14814   else if (!DECL_IMPLICIT_TYPEDEF_P (decl)
14815 	   && !DECL_SELF_REFERENCE_P (decl)
14816 	   && tag_code != typename_type)
14817     {
14818       if (alias_template_specialization_p (type, nt_opaque))
14819 	error ("using alias template specialization %qT after %qs",
14820 	       type, tag_name (tag_code));
14821       else
14822 	error ("using typedef-name %qD after %qs", decl, tag_name (tag_code));
14823       inform (DECL_SOURCE_LOCATION (decl),
14824 	      "%qD has a previous declaration here", decl);
14825       return error_mark_node;
14826     }
14827   else if (TREE_CODE (type) != RECORD_TYPE
14828 	   && TREE_CODE (type) != UNION_TYPE
14829 	   && tag_code != enum_type
14830 	   && tag_code != typename_type)
14831     {
14832       error ("%qT referred to as %qs", type, tag_name (tag_code));
14833       inform (location_of (type), "%qT has a previous declaration here", type);
14834       return error_mark_node;
14835     }
14836   else if (TREE_CODE (type) != ENUMERAL_TYPE
14837 	   && tag_code == enum_type)
14838     {
14839       error ("%qT referred to as enum", type);
14840       inform (location_of (type), "%qT has a previous declaration here", type);
14841       return error_mark_node;
14842     }
14843   else if (!allow_template_p
14844 	   && TREE_CODE (type) == RECORD_TYPE
14845 	   && CLASSTYPE_IS_TEMPLATE (type))
14846     {
14847       /* If a class template appears as elaborated type specifier
14848 	 without a template header such as:
14849 
14850 	   template <class T> class C {};
14851 	   void f(class C);		// No template header here
14852 
14853 	 then the required template argument is missing.  */
14854       error ("template argument required for %<%s %T%>",
14855 	     tag_name (tag_code),
14856 	     DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)));
14857       return error_mark_node;
14858     }
14859 
14860   return type;
14861 }
14862 
14863 /* Lookup NAME in elaborate type specifier in scope according to
14864    SCOPE and issue diagnostics if necessary.
14865    Return *_TYPE node upon success, NULL_TREE when the NAME is not
14866    found, and ERROR_MARK_NODE for type error.  */
14867 
14868 static tree
lookup_and_check_tag(enum tag_types tag_code,tree name,tag_scope scope,bool template_header_p)14869 lookup_and_check_tag (enum tag_types tag_code, tree name,
14870 		      tag_scope scope, bool template_header_p)
14871 {
14872   tree t;
14873   tree decl;
14874   if (scope == ts_global)
14875     {
14876       /* First try ordinary name lookup, ignoring hidden class name
14877 	 injected via friend declaration.  */
14878       decl = lookup_name_prefer_type (name, 2);
14879       decl = strip_using_decl (decl);
14880       /* If that fails, the name will be placed in the smallest
14881 	 non-class, non-function-prototype scope according to 3.3.1/5.
14882 	 We may already have a hidden name declared as friend in this
14883 	 scope.  So lookup again but not ignoring hidden names.
14884 	 If we find one, that name will be made visible rather than
14885 	 creating a new tag.  */
14886       if (!decl)
14887 	decl = lookup_type_scope (name, ts_within_enclosing_non_class);
14888     }
14889   else
14890     decl = lookup_type_scope (name, scope);
14891 
14892   if (decl
14893       && (DECL_CLASS_TEMPLATE_P (decl)
14894 	  /* If scope is ts_current we're defining a class, so ignore a
14895 	     template template parameter.  */
14896 	  || (scope != ts_current
14897 	      && DECL_TEMPLATE_TEMPLATE_PARM_P (decl))))
14898     decl = DECL_TEMPLATE_RESULT (decl);
14899 
14900   if (decl && TREE_CODE (decl) == TYPE_DECL)
14901     {
14902       /* Look for invalid nested type:
14903 	   class C {
14904 	     class C {};
14905 	   };  */
14906       if (scope == ts_current && DECL_SELF_REFERENCE_P (decl))
14907 	{
14908 	  error ("%qD has the same name as the class in which it is "
14909 		 "declared",
14910 		 decl);
14911 	  return error_mark_node;
14912 	}
14913 
14914       /* Two cases we need to consider when deciding if a class
14915 	 template is allowed as an elaborated type specifier:
14916 	 1. It is a self reference to its own class.
14917 	 2. It comes with a template header.
14918 
14919 	 For example:
14920 
14921 	   template <class T> class C {
14922 	     class C *c1;		// DECL_SELF_REFERENCE_P is true
14923 	     class D;
14924 	   };
14925 	   template <class U> class C; // template_header_p is true
14926 	   template <class T> class C<T>::D {
14927 	     class C *c2;		// DECL_SELF_REFERENCE_P is true
14928 	   };  */
14929 
14930       t = check_elaborated_type_specifier (tag_code,
14931 					   decl,
14932 					   template_header_p
14933 					   | DECL_SELF_REFERENCE_P (decl));
14934       if (template_header_p && t && CLASS_TYPE_P (t)
14935 	  && (!CLASSTYPE_TEMPLATE_INFO (t)
14936 	      || (!PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)))))
14937 	{
14938 	  error ("%qT is not a template", t);
14939 	  inform (location_of (t), "previous declaration here");
14940 	  if (TYPE_CLASS_SCOPE_P (t)
14941 	      && CLASSTYPE_TEMPLATE_INFO (TYPE_CONTEXT (t)))
14942 	    inform (input_location,
14943 		    "perhaps you want to explicitly add %<%T::%>",
14944 		    TYPE_CONTEXT (t));
14945 	  t = error_mark_node;
14946 	}
14947 
14948       return t;
14949     }
14950   else if (decl && TREE_CODE (decl) == TREE_LIST)
14951     {
14952       error ("reference to %qD is ambiguous", name);
14953       print_candidates (decl);
14954       return error_mark_node;
14955     }
14956   else
14957     return NULL_TREE;
14958 }
14959 
14960 /* Get the struct, enum or union (TAG_CODE says which) with tag NAME.
14961    Define the tag as a forward-reference if it is not defined.
14962 
14963    If a declaration is given, process it here, and report an error if
14964    multiple declarations are not identical.
14965 
14966    SCOPE is TS_CURRENT when this is also a definition.  Only look in
14967    the current frame for the name (since C++ allows new names in any
14968    scope.)  It is TS_WITHIN_ENCLOSING_NON_CLASS if this is a friend
14969    declaration.  Only look beginning from the current scope outward up
14970    till the nearest non-class scope.  Otherwise it is TS_GLOBAL.
14971 
14972    TEMPLATE_HEADER_P is true when this declaration is preceded by
14973    a set of template parameters.  */
14974 
14975 static tree
xref_tag_1(enum tag_types tag_code,tree name,tag_scope scope,bool template_header_p)14976 xref_tag_1 (enum tag_types tag_code, tree name,
14977             tag_scope scope, bool template_header_p)
14978 {
14979   enum tree_code code;
14980   tree context = NULL_TREE;
14981 
14982   gcc_assert (identifier_p (name));
14983 
14984   switch (tag_code)
14985     {
14986     case record_type:
14987     case class_type:
14988       code = RECORD_TYPE;
14989       break;
14990     case union_type:
14991       code = UNION_TYPE;
14992       break;
14993     case enum_type:
14994       code = ENUMERAL_TYPE;
14995       break;
14996     default:
14997       gcc_unreachable ();
14998     }
14999 
15000   /* In case of anonymous name, xref_tag is only called to
15001      make type node and push name.  Name lookup is not required.  */
15002   tree t = NULL_TREE;
15003   if (scope != ts_lambda && !IDENTIFIER_ANON_P (name))
15004     t = lookup_and_check_tag  (tag_code, name, scope, template_header_p);
15005 
15006   if (t == error_mark_node)
15007     return error_mark_node;
15008 
15009   if (scope != ts_current && t && current_class_type
15010       && template_class_depth (current_class_type)
15011       && template_header_p)
15012     {
15013       if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
15014 	return t;
15015 
15016       /* Since SCOPE is not TS_CURRENT, we are not looking at a
15017 	 definition of this tag.  Since, in addition, we are currently
15018 	 processing a (member) template declaration of a template
15019 	 class, we must be very careful; consider:
15020 
15021 	   template <class X> struct S1
15022 
15023 	   template <class U> struct S2
15024 	   {
15025 	     template <class V> friend struct S1;
15026 	   };
15027 
15028 	 Here, the S2::S1 declaration should not be confused with the
15029 	 outer declaration.  In particular, the inner version should
15030 	 have a template parameter of level 2, not level 1.
15031 
15032 	 On the other hand, when presented with:
15033 
15034 	   template <class T> struct S1
15035 	   {
15036 	     template <class U> struct S2 {};
15037 	     template <class U> friend struct S2;
15038 	   };
15039 
15040 	 the friend must find S1::S2 eventually.  We accomplish this
15041 	 by making sure that the new type we create to represent this
15042 	 declaration has the right TYPE_CONTEXT.  */
15043       context = TYPE_CONTEXT (t);
15044       t = NULL_TREE;
15045     }
15046 
15047   if (! t)
15048     {
15049       /* If no such tag is yet defined, create a forward-reference node
15050 	 and record it as the "definition".
15051 	 When a real declaration of this type is found,
15052 	 the forward-reference will be altered into a real type.  */
15053       if (code == ENUMERAL_TYPE)
15054 	{
15055 	  error ("use of enum %q#D without previous declaration", name);
15056 	  return error_mark_node;
15057 	}
15058       else
15059 	{
15060 	  t = make_class_type (code);
15061 	  TYPE_CONTEXT (t) = context;
15062 	  if (scope == ts_lambda)
15063 	    {
15064 	      /* Mark it as a lambda type.  */
15065 	      CLASSTYPE_LAMBDA_EXPR (t) = error_mark_node;
15066 	      /* And push it into current scope.  */
15067 	      scope = ts_current;
15068 	    }
15069 	  t = pushtag (name, t, scope);
15070 	}
15071     }
15072   else
15073     {
15074       if (template_header_p && MAYBE_CLASS_TYPE_P (t))
15075         {
15076           /* Check that we aren't trying to overload a class with different
15077              constraints.  */
15078           tree constr = NULL_TREE;
15079           if (current_template_parms)
15080             {
15081               tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
15082               constr = build_constraints (reqs, NULL_TREE);
15083             }
15084 	  if (!redeclare_class_template (t, current_template_parms, constr))
15085 	    return error_mark_node;
15086         }
15087       else if (!processing_template_decl
15088 	       && CLASS_TYPE_P (t)
15089 	       && CLASSTYPE_IS_TEMPLATE (t))
15090 	{
15091 	  error ("redeclaration of %qT as a non-template", t);
15092 	  inform (location_of (t), "previous declaration %qD", t);
15093 	  return error_mark_node;
15094 	}
15095 
15096       if (scope != ts_within_enclosing_non_class && TYPE_HIDDEN_P (t))
15097 	{
15098 	  /* This is no longer an invisible friend.  Make it
15099 	     visible.  */
15100 	  tree decl = TYPE_NAME (t);
15101 
15102 	  DECL_ANTICIPATED (decl) = false;
15103 	  DECL_FRIEND_P (decl) = false;
15104 
15105 	  if (TYPE_TEMPLATE_INFO (t))
15106 	    {
15107 	      tree tmpl = TYPE_TI_TEMPLATE (t);
15108 	      DECL_ANTICIPATED (tmpl) = false;
15109 	      DECL_FRIEND_P (tmpl) = false;
15110 	    }
15111 	}
15112     }
15113 
15114   return t;
15115 }
15116 
15117 /* Wrapper for xref_tag_1.  */
15118 
15119 tree
xref_tag(enum tag_types tag_code,tree name,tag_scope scope,bool template_header_p)15120 xref_tag (enum tag_types tag_code, tree name,
15121           tag_scope scope, bool template_header_p)
15122 {
15123   tree ret;
15124   bool subtime;
15125   subtime = timevar_cond_start (TV_NAME_LOOKUP);
15126   ret = xref_tag_1 (tag_code, name, scope, template_header_p);
15127   timevar_cond_stop (TV_NAME_LOOKUP, subtime);
15128   return ret;
15129 }
15130 
15131 
15132 tree
xref_tag_from_type(tree old,tree id,tag_scope scope)15133 xref_tag_from_type (tree old, tree id, tag_scope scope)
15134 {
15135   enum tag_types tag_kind;
15136 
15137   if (TREE_CODE (old) == RECORD_TYPE)
15138     tag_kind = (CLASSTYPE_DECLARED_CLASS (old) ? class_type : record_type);
15139   else
15140     tag_kind  = union_type;
15141 
15142   if (id == NULL_TREE)
15143     id = TYPE_IDENTIFIER (old);
15144 
15145   return xref_tag (tag_kind, id, scope, false);
15146 }
15147 
15148 /* Create the binfo hierarchy for REF with (possibly NULL) base list
15149    BASE_LIST.  For each element on BASE_LIST the TREE_PURPOSE is an
15150    access_* node, and the TREE_VALUE is the type of the base-class.
15151    Non-NULL TREE_TYPE indicates virtual inheritance.  */
15152 
15153 void
xref_basetypes(tree ref,tree base_list)15154 xref_basetypes (tree ref, tree base_list)
15155 {
15156   tree *basep;
15157   tree binfo, base_binfo;
15158   unsigned max_vbases = 0; /* Maximum direct & indirect virtual bases.  */
15159   unsigned max_bases = 0;  /* Maximum direct bases.  */
15160   unsigned max_dvbases = 0; /* Maximum direct virtual bases.  */
15161   int i;
15162   tree default_access;
15163   tree igo_prev; /* Track Inheritance Graph Order.  */
15164 
15165   if (ref == error_mark_node)
15166     return;
15167 
15168   /* The base of a derived class is private by default, all others are
15169      public.  */
15170   default_access = (TREE_CODE (ref) == RECORD_TYPE
15171 		    && CLASSTYPE_DECLARED_CLASS (ref)
15172 		    ? access_private_node : access_public_node);
15173 
15174   /* First, make sure that any templates in base-classes are
15175      instantiated.  This ensures that if we call ourselves recursively
15176      we do not get confused about which classes are marked and which
15177      are not.  */
15178   basep = &base_list;
15179   while (*basep)
15180     {
15181       tree basetype = TREE_VALUE (*basep);
15182 
15183       /* The dependent_type_p call below should really be dependent_scope_p
15184 	 so that we give a hard error about using an incomplete type as a
15185 	 base, but we allow it with a pedwarn for backward
15186 	 compatibility.  */
15187       if (processing_template_decl
15188 	  && CLASS_TYPE_P (basetype) && TYPE_BEING_DEFINED (basetype))
15189 	cxx_incomplete_type_diagnostic (NULL_TREE, basetype, DK_PEDWARN);
15190       if (!dependent_type_p (basetype)
15191 	  && !complete_type_or_else (basetype, NULL))
15192 	/* An incomplete type.  Remove it from the list.  */
15193 	*basep = TREE_CHAIN (*basep);
15194       else
15195 	{
15196 	  max_bases++;
15197 	  if (TREE_TYPE (*basep))
15198 	    max_dvbases++;
15199 	  if (CLASS_TYPE_P (basetype))
15200 	    max_vbases += vec_safe_length (CLASSTYPE_VBASECLASSES (basetype));
15201 	  basep = &TREE_CHAIN (*basep);
15202 	}
15203     }
15204   max_vbases += max_dvbases;
15205 
15206   TYPE_MARKED_P (ref) = 1;
15207 
15208   /* The binfo slot should be empty, unless this is an (ill-formed)
15209      redefinition.  */
15210   gcc_assert (!TYPE_BINFO (ref) || TYPE_SIZE (ref));
15211 
15212   gcc_assert (TYPE_MAIN_VARIANT (ref) == ref);
15213 
15214   binfo = make_tree_binfo (max_bases);
15215 
15216   TYPE_BINFO (ref) = binfo;
15217   BINFO_OFFSET (binfo) = size_zero_node;
15218   BINFO_TYPE (binfo) = ref;
15219 
15220   /* Apply base-class info set up to the variants of this type.  */
15221   fixup_type_variants (ref);
15222 
15223   if (max_bases)
15224     {
15225       vec_alloc (BINFO_BASE_ACCESSES (binfo), max_bases);
15226       /* A C++98 POD cannot have base classes.  */
15227       CLASSTYPE_NON_LAYOUT_POD_P (ref) = true;
15228 
15229       if (TREE_CODE (ref) == UNION_TYPE)
15230 	{
15231 	  error ("derived union %qT invalid", ref);
15232 	  return;
15233 	}
15234     }
15235 
15236   if (max_bases > 1)
15237     warning (OPT_Wmultiple_inheritance,
15238 	     "%qT defined with multiple direct bases", ref);
15239 
15240   if (max_vbases)
15241     {
15242       /* An aggregate can't have virtual base classes.  */
15243       CLASSTYPE_NON_AGGREGATE (ref) = true;
15244 
15245       vec_alloc (CLASSTYPE_VBASECLASSES (ref), max_vbases);
15246 
15247       if (max_dvbases)
15248 	warning (OPT_Wvirtual_inheritance,
15249 		 "%qT defined with direct virtual base", ref);
15250     }
15251 
15252   for (igo_prev = binfo; base_list; base_list = TREE_CHAIN (base_list))
15253     {
15254       tree access = TREE_PURPOSE (base_list);
15255       int via_virtual = TREE_TYPE (base_list) != NULL_TREE;
15256       tree basetype = TREE_VALUE (base_list);
15257 
15258       if (access == access_default_node)
15259 	access = default_access;
15260 
15261       /* Before C++17, an aggregate cannot have base classes.  In C++17, an
15262 	 aggregate can't have virtual, private, or protected base classes.  */
15263       if (cxx_dialect < cxx17
15264 	  || access != access_public_node
15265 	  || via_virtual)
15266 	CLASSTYPE_NON_AGGREGATE (ref) = true;
15267 
15268       if (PACK_EXPANSION_P (basetype))
15269         basetype = PACK_EXPANSION_PATTERN (basetype);
15270       if (TREE_CODE (basetype) == TYPE_DECL)
15271 	basetype = TREE_TYPE (basetype);
15272       if (!MAYBE_CLASS_TYPE_P (basetype) || TREE_CODE (basetype) == UNION_TYPE)
15273 	{
15274 	  error ("base type %qT fails to be a struct or class type",
15275 		 basetype);
15276 	  goto dropped_base;
15277 	}
15278 
15279       base_binfo = NULL_TREE;
15280       if (CLASS_TYPE_P (basetype) && !dependent_scope_p (basetype))
15281 	{
15282 	  base_binfo = TYPE_BINFO (basetype);
15283 	  /* The original basetype could have been a typedef'd type.  */
15284 	  basetype = BINFO_TYPE (base_binfo);
15285 
15286 	  /* Inherit flags from the base.  */
15287 	  TYPE_HAS_NEW_OPERATOR (ref)
15288 	    |= TYPE_HAS_NEW_OPERATOR (basetype);
15289 	  TYPE_HAS_ARRAY_NEW_OPERATOR (ref)
15290 	    |= TYPE_HAS_ARRAY_NEW_OPERATOR (basetype);
15291 	  TYPE_GETS_DELETE (ref) |= TYPE_GETS_DELETE (basetype);
15292 	  TYPE_HAS_CONVERSION (ref) |= TYPE_HAS_CONVERSION (basetype);
15293 	  CLASSTYPE_DIAMOND_SHAPED_P (ref)
15294 	    |= CLASSTYPE_DIAMOND_SHAPED_P (basetype);
15295 	  CLASSTYPE_REPEATED_BASE_P (ref)
15296 	    |= CLASSTYPE_REPEATED_BASE_P (basetype);
15297 	}
15298 
15299       /* We must do this test after we've seen through a typedef
15300 	 type.  */
15301       if (TYPE_MARKED_P (basetype))
15302 	{
15303 	  if (basetype == ref)
15304 	    error ("recursive type %qT undefined", basetype);
15305 	  else
15306 	    error ("duplicate base type %qT invalid", basetype);
15307 	  goto dropped_base;
15308 	}
15309 
15310       if (PACK_EXPANSION_P (TREE_VALUE (base_list)))
15311         /* Regenerate the pack expansion for the bases. */
15312         basetype = make_pack_expansion (basetype);
15313 
15314       TYPE_MARKED_P (basetype) = 1;
15315 
15316       base_binfo = copy_binfo (base_binfo, basetype, ref,
15317 			       &igo_prev, via_virtual);
15318       if (!BINFO_INHERITANCE_CHAIN (base_binfo))
15319 	BINFO_INHERITANCE_CHAIN (base_binfo) = binfo;
15320 
15321       BINFO_BASE_APPEND (binfo, base_binfo);
15322       BINFO_BASE_ACCESS_APPEND (binfo, access);
15323       continue;
15324 
15325     dropped_base:
15326       /* Update max_vbases to reflect the reality that we are dropping
15327 	 this base:  if it reaches zero we want to undo the vec_alloc
15328 	 above to avoid inconsistencies during error-recovery: eg, in
15329 	 build_special_member_call, CLASSTYPE_VBASECLASSES non null
15330 	 and vtt null (c++/27952).  */
15331       if (via_virtual)
15332 	max_vbases--;
15333       if (CLASS_TYPE_P (basetype))
15334 	max_vbases
15335 	  -= vec_safe_length (CLASSTYPE_VBASECLASSES (basetype));
15336     }
15337 
15338   if (CLASSTYPE_VBASECLASSES (ref)
15339       && max_vbases == 0)
15340     vec_free (CLASSTYPE_VBASECLASSES (ref));
15341 
15342   if (vec_safe_length (CLASSTYPE_VBASECLASSES (ref)) < max_vbases)
15343     /* If we didn't get max_vbases vbases, we must have shared at
15344        least one of them, and are therefore diamond shaped.  */
15345     CLASSTYPE_DIAMOND_SHAPED_P (ref) = 1;
15346 
15347   /* Unmark all the types.  */
15348   for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
15349     TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
15350   TYPE_MARKED_P (ref) = 0;
15351 
15352   /* Now see if we have a repeated base type.  */
15353   if (!CLASSTYPE_REPEATED_BASE_P (ref))
15354     {
15355       for (base_binfo = binfo; base_binfo;
15356 	   base_binfo = TREE_CHAIN (base_binfo))
15357 	{
15358 	  if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
15359 	    {
15360 	      CLASSTYPE_REPEATED_BASE_P (ref) = 1;
15361 	      break;
15362 	    }
15363 	  TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 1;
15364 	}
15365       for (base_binfo = binfo; base_binfo;
15366 	   base_binfo = TREE_CHAIN (base_binfo))
15367 	if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
15368 	  TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
15369 	else
15370 	  break;
15371     }
15372 }
15373 
15374 
15375 /* Copies the enum-related properties from type SRC to type DST.
15376    Used with the underlying type of an enum and the enum itself.  */
15377 static void
copy_type_enum(tree dst,tree src)15378 copy_type_enum (tree dst, tree src)
15379 {
15380   tree t;
15381   for (t = dst; t; t = TYPE_NEXT_VARIANT (t))
15382     {
15383       TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (src);
15384       TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (src);
15385       TYPE_SIZE (t) = TYPE_SIZE (src);
15386       TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (src);
15387       SET_TYPE_MODE (dst, TYPE_MODE (src));
15388       TYPE_PRECISION (t) = TYPE_PRECISION (src);
15389       unsigned valign = TYPE_ALIGN (src);
15390       if (TYPE_USER_ALIGN (t))
15391 	valign = MAX (valign, TYPE_ALIGN (t));
15392       else
15393 	TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (src);
15394       SET_TYPE_ALIGN (t, valign);
15395       TYPE_UNSIGNED (t) = TYPE_UNSIGNED (src);
15396     }
15397 }
15398 
15399 /* Begin compiling the definition of an enumeration type.
15400    NAME is its name,
15401 
15402    if ENUMTYPE is not NULL_TREE then the type has alredy been found.
15403 
15404    UNDERLYING_TYPE is the type that will be used as the storage for
15405    the enumeration type. This should be NULL_TREE if no storage type
15406    was specified.
15407 
15408    ATTRIBUTES are any attributes specified after the enum-key.
15409 
15410    SCOPED_ENUM_P is true if this is a scoped enumeration type.
15411 
15412    if IS_NEW is not NULL, gets TRUE iff a new type is created.
15413 
15414    Returns the type object, as yet incomplete.
15415    Also records info about it so that build_enumerator
15416    may be used to declare the individual values as they are read.  */
15417 
15418 tree
start_enum(tree name,tree enumtype,tree underlying_type,tree attributes,bool scoped_enum_p,bool * is_new)15419 start_enum (tree name, tree enumtype, tree underlying_type,
15420 	    tree attributes, bool scoped_enum_p, bool *is_new)
15421 {
15422   tree prevtype = NULL_TREE;
15423   gcc_assert (identifier_p (name));
15424 
15425   if (is_new)
15426     *is_new = false;
15427   /* [C++0x dcl.enum]p5:
15428 
15429     If not explicitly specified, the underlying type of a scoped
15430     enumeration type is int.  */
15431   if (!underlying_type && scoped_enum_p)
15432     underlying_type = integer_type_node;
15433 
15434   if (underlying_type)
15435     underlying_type = cv_unqualified (underlying_type);
15436 
15437   /* If this is the real definition for a previous forward reference,
15438      fill in the contents in the same object that used to be the
15439      forward reference.  */
15440   if (!enumtype)
15441     enumtype = lookup_and_check_tag (enum_type, name,
15442 				     /*tag_scope=*/ts_current,
15443 				     /*template_header_p=*/false);
15444 
15445   /* In case of a template_decl, the only check that should be deferred
15446      to instantiation time is the comparison of underlying types.  */
15447   if (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE)
15448     {
15449       if (scoped_enum_p != SCOPED_ENUM_P (enumtype))
15450 	{
15451 	  error_at (input_location, "scoped/unscoped mismatch "
15452 		    "in enum %q#T", enumtype);
15453 	  inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
15454 		  "previous definition here");
15455 	  enumtype = error_mark_node;
15456 	}
15457       else if (ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) != !! underlying_type)
15458 	{
15459 	  error_at (input_location, "underlying type mismatch "
15460 		    "in enum %q#T", enumtype);
15461 	  inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
15462 		  "previous definition here");
15463 	  enumtype = error_mark_node;
15464 	}
15465       else if (underlying_type && ENUM_UNDERLYING_TYPE (enumtype)
15466 	       && !same_type_p (underlying_type,
15467 				ENUM_UNDERLYING_TYPE (enumtype)))
15468 	{
15469 	  error_at (input_location, "different underlying type "
15470 		    "in enum %q#T", enumtype);
15471 	  inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
15472 		  "previous definition here");
15473 	  underlying_type = NULL_TREE;
15474 	}
15475     }
15476 
15477   if (!enumtype || TREE_CODE (enumtype) != ENUMERAL_TYPE
15478       || processing_template_decl)
15479     {
15480       /* In case of error, make a dummy enum to allow parsing to
15481 	 continue.  */
15482       if (enumtype == error_mark_node)
15483 	{
15484 	  name = make_anon_name ();
15485 	  enumtype = NULL_TREE;
15486 	}
15487 
15488       /* enumtype may be an ENUMERAL_TYPE if this is a redefinition
15489          of an opaque enum, or an opaque enum of an already defined
15490 	 enumeration (C++11).
15491 	 In any other case, it'll be NULL_TREE. */
15492       if (!enumtype)
15493 	{
15494 	  if (is_new)
15495 	    *is_new = true;
15496 	}
15497       prevtype = enumtype;
15498 
15499       /* Do not push the decl more than once.  */
15500       if (!enumtype
15501 	  || TREE_CODE (enumtype) != ENUMERAL_TYPE)
15502 	{
15503 	  enumtype = cxx_make_type (ENUMERAL_TYPE);
15504 	  enumtype = pushtag (name, enumtype, /*tag_scope=*/ts_current);
15505 
15506 	  /* std::byte aliases anything.  */
15507 	  if (enumtype != error_mark_node
15508 	      && TYPE_CONTEXT (enumtype) == std_node
15509 	      && !strcmp ("byte", TYPE_NAME_STRING (enumtype)))
15510 	    TYPE_ALIAS_SET (enumtype) = 0;
15511 	}
15512       else
15513 	  enumtype = xref_tag (enum_type, name, /*tag_scope=*/ts_current,
15514 			       false);
15515 
15516       if (enumtype == error_mark_node)
15517 	return error_mark_node;
15518 
15519       /* The enum is considered opaque until the opening '{' of the
15520 	 enumerator list.  */
15521       SET_OPAQUE_ENUM_P (enumtype, true);
15522       ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) = !! underlying_type;
15523     }
15524 
15525   SET_SCOPED_ENUM_P (enumtype, scoped_enum_p);
15526 
15527   cplus_decl_attributes (&enumtype, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
15528 
15529   if (underlying_type)
15530     {
15531       if (ENUM_UNDERLYING_TYPE (enumtype))
15532 	/* We already checked that it matches, don't change it to a different
15533 	   typedef variant.  */;
15534       else if (CP_INTEGRAL_TYPE_P (underlying_type))
15535         {
15536 	  copy_type_enum (enumtype, underlying_type);
15537           ENUM_UNDERLYING_TYPE (enumtype) = underlying_type;
15538         }
15539       else if (dependent_type_p (underlying_type))
15540 	ENUM_UNDERLYING_TYPE (enumtype) = underlying_type;
15541       else
15542         error ("underlying type %qT of %qT must be an integral type",
15543                underlying_type, enumtype);
15544     }
15545 
15546   /* If into a template class, the returned enum is always the first
15547      declaration (opaque or not) seen. This way all the references to
15548      this type will be to the same declaration. The following ones are used
15549      only to check for definition errors.  */
15550   if (prevtype && processing_template_decl)
15551     return prevtype;
15552   else
15553     return enumtype;
15554 }
15555 
15556 /* After processing and defining all the values of an enumeration type,
15557    install their decls in the enumeration type.
15558    ENUMTYPE is the type object.  */
15559 
15560 void
finish_enum_value_list(tree enumtype)15561 finish_enum_value_list (tree enumtype)
15562 {
15563   tree values;
15564   tree underlying_type;
15565   tree decl;
15566   tree value;
15567   tree minnode, maxnode;
15568   tree t;
15569 
15570   bool fixed_underlying_type_p
15571     = ENUM_UNDERLYING_TYPE (enumtype) != NULL_TREE;
15572 
15573   /* We built up the VALUES in reverse order.  */
15574   TYPE_VALUES (enumtype) = nreverse (TYPE_VALUES (enumtype));
15575 
15576   /* For an enum defined in a template, just set the type of the values;
15577      all further processing is postponed until the template is
15578      instantiated.  We need to set the type so that tsubst of a CONST_DECL
15579      works.  */
15580   if (processing_template_decl)
15581     {
15582       for (values = TYPE_VALUES (enumtype);
15583 	   values;
15584 	   values = TREE_CHAIN (values))
15585 	TREE_TYPE (TREE_VALUE (values)) = enumtype;
15586       return;
15587     }
15588 
15589   /* Determine the minimum and maximum values of the enumerators.  */
15590   if (TYPE_VALUES (enumtype))
15591     {
15592       minnode = maxnode = NULL_TREE;
15593 
15594       for (values = TYPE_VALUES (enumtype);
15595 	   values;
15596 	   values = TREE_CHAIN (values))
15597 	{
15598 	  decl = TREE_VALUE (values);
15599 
15600 	  /* [dcl.enum]: Following the closing brace of an enum-specifier,
15601 	     each enumerator has the type of its enumeration.  Prior to the
15602 	     closing brace, the type of each enumerator is the type of its
15603 	     initializing value.  */
15604 	  TREE_TYPE (decl) = enumtype;
15605 
15606 	  /* Update the minimum and maximum values, if appropriate.  */
15607 	  value = DECL_INITIAL (decl);
15608 	  if (value == error_mark_node)
15609 	    value = integer_zero_node;
15610 	  /* Figure out what the minimum and maximum values of the
15611 	     enumerators are.  */
15612 	  if (!minnode)
15613 	    minnode = maxnode = value;
15614 	  else if (tree_int_cst_lt (maxnode, value))
15615 	    maxnode = value;
15616 	  else if (tree_int_cst_lt (value, minnode))
15617 	    minnode = value;
15618 	}
15619     }
15620   else
15621     /* [dcl.enum]
15622 
15623        If the enumerator-list is empty, the underlying type is as if
15624        the enumeration had a single enumerator with value 0.  */
15625     minnode = maxnode = integer_zero_node;
15626 
15627   if (!fixed_underlying_type_p)
15628     {
15629       /* Compute the number of bits require to represent all values of the
15630 	 enumeration.  We must do this before the type of MINNODE and
15631 	 MAXNODE are transformed, since tree_int_cst_min_precision relies
15632 	 on the TREE_TYPE of the value it is passed.  */
15633       signop sgn = tree_int_cst_sgn (minnode) >= 0 ? UNSIGNED : SIGNED;
15634       int lowprec = tree_int_cst_min_precision (minnode, sgn);
15635       int highprec = tree_int_cst_min_precision (maxnode, sgn);
15636       int precision = MAX (lowprec, highprec);
15637       unsigned int itk;
15638       bool use_short_enum;
15639 
15640       /* Determine the underlying type of the enumeration.
15641 
15642          [dcl.enum]
15643 
15644          The underlying type of an enumeration is an integral type that
15645          can represent all the enumerator values defined in the
15646          enumeration.  It is implementation-defined which integral type is
15647          used as the underlying type for an enumeration except that the
15648          underlying type shall not be larger than int unless the value of
15649          an enumerator cannot fit in an int or unsigned int.
15650 
15651          We use "int" or an "unsigned int" as the underlying type, even if
15652          a smaller integral type would work, unless the user has
15653          explicitly requested that we use the smallest possible type.  The
15654          user can request that for all enumerations with a command line
15655          flag, or for just one enumeration with an attribute.  */
15656 
15657       use_short_enum = flag_short_enums
15658         || lookup_attribute ("packed", TYPE_ATTRIBUTES (enumtype));
15659 
15660       /* If the precision of the type was specified with an attribute and it
15661 	 was too small, give an error.  Otherwise, use it.  */
15662       if (TYPE_PRECISION (enumtype))
15663 	{
15664 	  if (precision > TYPE_PRECISION (enumtype))
15665 	    error ("specified mode too small for enumerated values");
15666 	  else
15667 	    {
15668 	      use_short_enum = true;
15669 	      precision = TYPE_PRECISION (enumtype);
15670 	    }
15671 	}
15672 
15673       for (itk = (use_short_enum ? itk_char : itk_int);
15674            itk != itk_none;
15675            itk++)
15676         {
15677           underlying_type = integer_types[itk];
15678           if (underlying_type != NULL_TREE
15679 	      && TYPE_PRECISION (underlying_type) >= precision
15680               && TYPE_SIGN (underlying_type) == sgn)
15681             break;
15682         }
15683       if (itk == itk_none)
15684         {
15685           /* DR 377
15686 
15687              IF no integral type can represent all the enumerator values, the
15688              enumeration is ill-formed.  */
15689           error ("no integral type can represent all of the enumerator values "
15690                  "for %qT", enumtype);
15691           precision = TYPE_PRECISION (long_long_integer_type_node);
15692           underlying_type = integer_types[itk_unsigned_long_long];
15693         }
15694 
15695       /* [dcl.enum]
15696 
15697          The value of sizeof() applied to an enumeration type, an object
15698          of an enumeration type, or an enumerator, is the value of sizeof()
15699          applied to the underlying type.  */
15700       copy_type_enum (enumtype, underlying_type);
15701 
15702       /* Compute the minimum and maximum values for the type.
15703 
15704 	 [dcl.enum]
15705 
15706 	 For an enumeration where emin is the smallest enumerator and emax
15707 	 is the largest, the values of the enumeration are the values of the
15708 	 underlying type in the range bmin to bmax, where bmin and bmax are,
15709 	 respectively, the smallest and largest values of the smallest bit-
15710 	 field that can store emin and emax.  */
15711 
15712       /* The middle-end currently assumes that types with TYPE_PRECISION
15713 	 narrower than their underlying type are suitably zero or sign
15714 	 extended to fill their mode.  Similarly, it assumes that the front
15715 	 end assures that a value of a particular type must be within
15716 	 TYPE_MIN_VALUE and TYPE_MAX_VALUE.
15717 
15718 	 We used to set these fields based on bmin and bmax, but that led
15719 	 to invalid assumptions like optimizing away bounds checking.  So
15720 	 now we just set the TYPE_PRECISION, TYPE_MIN_VALUE, and
15721 	 TYPE_MAX_VALUE to the values for the mode above and only restrict
15722 	 the ENUM_UNDERLYING_TYPE for the benefit of diagnostics.  */
15723       ENUM_UNDERLYING_TYPE (enumtype)
15724 	= build_distinct_type_copy (underlying_type);
15725       TYPE_PRECISION (ENUM_UNDERLYING_TYPE (enumtype)) = precision;
15726       set_min_and_max_values_for_integral_type
15727         (ENUM_UNDERLYING_TYPE (enumtype), precision, sgn);
15728 
15729       /* If -fstrict-enums, still constrain TYPE_MIN/MAX_VALUE.  */
15730       if (flag_strict_enums)
15731 	set_min_and_max_values_for_integral_type (enumtype, precision, sgn);
15732     }
15733   else
15734     underlying_type = ENUM_UNDERLYING_TYPE (enumtype);
15735 
15736   /* Convert each of the enumerators to the type of the underlying
15737      type of the enumeration.  */
15738   for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values))
15739     {
15740       decl = TREE_VALUE (values);
15741       iloc_sentinel ils (DECL_SOURCE_LOCATION (decl));
15742       if (fixed_underlying_type_p)
15743         /* If the enumeration type has a fixed underlying type, we
15744            already checked all of the enumerator values.  */
15745         value = DECL_INITIAL (decl);
15746       else
15747         value = perform_implicit_conversion (underlying_type,
15748                                              DECL_INITIAL (decl),
15749                                              tf_warning_or_error);
15750       /* Do not clobber shared ints.  */
15751       if (value != error_mark_node)
15752 	{
15753 	  value = copy_node (value);
15754 
15755 	  TREE_TYPE (value) = enumtype;
15756 	}
15757       DECL_INITIAL (decl) = value;
15758     }
15759 
15760   /* Fix up all variant types of this enum type.  */
15761   for (t = TYPE_MAIN_VARIANT (enumtype); t; t = TYPE_NEXT_VARIANT (t))
15762     TYPE_VALUES (t) = TYPE_VALUES (enumtype);
15763 
15764   if (at_class_scope_p ()
15765       && COMPLETE_TYPE_P (current_class_type)
15766       && UNSCOPED_ENUM_P (enumtype))
15767     {
15768       insert_late_enum_def_bindings (current_class_type, enumtype);
15769       /* TYPE_FIELDS needs fixup.  */
15770       fixup_type_variants (current_class_type);
15771     }
15772 
15773   /* Finish debugging output for this type.  */
15774   rest_of_type_compilation (enumtype, namespace_bindings_p ());
15775 
15776   /* Each enumerator now has the type of its enumeration.  Clear the cache
15777      so that this change in types doesn't confuse us later on.  */
15778   clear_cv_and_fold_caches ();
15779 }
15780 
15781 /* Finishes the enum type. This is called only the first time an
15782    enumeration is seen, be it opaque or odinary.
15783    ENUMTYPE is the type object.  */
15784 
15785 void
finish_enum(tree enumtype)15786 finish_enum (tree enumtype)
15787 {
15788   if (processing_template_decl)
15789     {
15790       if (at_function_scope_p ())
15791 	add_stmt (build_min (TAG_DEFN, enumtype));
15792       return;
15793     }
15794 
15795   /* If this is a forward declaration, there should not be any variants,
15796      though we can get a variant in the middle of an enum-specifier with
15797      wacky code like 'enum E { e = sizeof(const E*) };'  */
15798   gcc_assert (enumtype == TYPE_MAIN_VARIANT (enumtype)
15799 	      && (TYPE_VALUES (enumtype)
15800 		  || !TYPE_NEXT_VARIANT (enumtype)));
15801 }
15802 
15803 /* Build and install a CONST_DECL for an enumeration constant of the
15804    enumeration type ENUMTYPE whose NAME and VALUE (if any) are provided.
15805    Apply ATTRIBUTES if available.  LOC is the location of NAME.
15806    Assignment of sequential values by default is handled here.  */
15807 
15808 void
build_enumerator(tree name,tree value,tree enumtype,tree attributes,location_t loc)15809 build_enumerator (tree name, tree value, tree enumtype, tree attributes,
15810 		  location_t loc)
15811 {
15812   tree decl;
15813   tree context;
15814   tree type;
15815 
15816   /* scalar_constant_value will pull out this expression, so make sure
15817      it's folded as appropriate.  */
15818   if (processing_template_decl)
15819     value = fold_non_dependent_expr (value);
15820 
15821   /* If the VALUE was erroneous, pretend it wasn't there; that will
15822      result in the enum being assigned the next value in sequence.  */
15823   if (value == error_mark_node)
15824     value = NULL_TREE;
15825 
15826   /* Remove no-op casts from the value.  */
15827   if (value)
15828     STRIP_TYPE_NOPS (value);
15829 
15830   if (! processing_template_decl)
15831     {
15832       /* Validate and default VALUE.  */
15833       if (value != NULL_TREE)
15834 	{
15835 	  if (!ENUM_UNDERLYING_TYPE (enumtype))
15836 	    {
15837 	      tree tmp_value = build_expr_type_conversion (WANT_INT | WANT_ENUM,
15838 							   value, true);
15839 	      if (tmp_value)
15840 		value = tmp_value;
15841 	    }
15842 	  else if (! INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P
15843 		   (TREE_TYPE (value)))
15844 	    value = perform_implicit_conversion_flags
15845 	      (ENUM_UNDERLYING_TYPE (enumtype), value, tf_warning_or_error,
15846 	       LOOKUP_IMPLICIT | LOOKUP_NO_NARROWING);
15847 
15848 	  if (value == error_mark_node)
15849 	    value = NULL_TREE;
15850 
15851 	  if (value != NULL_TREE)
15852 	    {
15853 	      if (! INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P
15854 		  (TREE_TYPE (value)))
15855 		{
15856 		  error_at (cp_expr_loc_or_input_loc (value),
15857 			    "enumerator value for %qD must have integral or "
15858 			    "unscoped enumeration type", name);
15859 		  value = NULL_TREE;
15860 		}
15861 	      else
15862 		{
15863 		  value = cxx_constant_value (value);
15864 
15865 		  if (TREE_CODE (value) != INTEGER_CST)
15866 		    {
15867 		      error ("enumerator value for %qD is not an integer "
15868 			     "constant", name);
15869 		      value = NULL_TREE;
15870 		    }
15871 		}
15872 	    }
15873 	}
15874 
15875       /* Default based on previous value.  */
15876       if (value == NULL_TREE)
15877 	{
15878 	  if (TYPE_VALUES (enumtype))
15879 	    {
15880 	      tree prev_value;
15881 
15882 	      /* C++03 7.2/4: If no initializer is specified for the first
15883 		 enumerator, the type is an unspecified integral
15884 		 type. Otherwise the type is the same as the type of the
15885 		 initializing value of the preceding enumerator unless the
15886 		 incremented value is not representable in that type, in
15887 		 which case the type is an unspecified integral type
15888 		 sufficient to contain the incremented value.  */
15889 	      prev_value = DECL_INITIAL (TREE_VALUE (TYPE_VALUES (enumtype)));
15890 	      if (error_operand_p (prev_value))
15891 		value = error_mark_node;
15892 	      else
15893 		{
15894 		  wi::overflow_type overflowed;
15895 		  tree type = TREE_TYPE (prev_value);
15896 		  signop sgn = TYPE_SIGN (type);
15897 		  widest_int wi = wi::add (wi::to_widest (prev_value), 1, sgn,
15898 					   &overflowed);
15899 		  if (!overflowed)
15900 		    {
15901 		      bool pos = !wi::neg_p (wi, sgn);
15902 		      if (!wi::fits_to_tree_p (wi, type))
15903 			{
15904 			  unsigned int itk;
15905 			  for (itk = itk_int; itk != itk_none; itk++)
15906 			    {
15907 			      type = integer_types[itk];
15908 			      if (type != NULL_TREE
15909 				  && (pos || !TYPE_UNSIGNED (type))
15910 				  && wi::fits_to_tree_p (wi, type))
15911 				break;
15912 			    }
15913 			  if (type && cxx_dialect < cxx11
15914 			      && itk > itk_unsigned_long)
15915 			    pedwarn (input_location, OPT_Wlong_long,
15916 				     pos ? G_("\
15917 incremented enumerator value is too large for %<unsigned long%>") : G_("\
15918 incremented enumerator value is too large for %<long%>"));
15919 			}
15920 		      if (type == NULL_TREE)
15921 		        overflowed = wi::OVF_UNKNOWN;
15922 		      else
15923 			value = wide_int_to_tree (type, wi);
15924 		    }
15925 
15926 		  if (overflowed)
15927 		    {
15928 		      error ("overflow in enumeration values at %qD", name);
15929 		      value = error_mark_node;
15930 		    }
15931 		}
15932 	    }
15933 	  else
15934 	    value = integer_zero_node;
15935 	}
15936 
15937       /* Remove no-op casts from the value.  */
15938       STRIP_TYPE_NOPS (value);
15939 
15940       /* If the underlying type of the enum is fixed, check whether
15941          the enumerator values fits in the underlying type.  If it
15942          does not fit, the program is ill-formed [C++0x dcl.enum].  */
15943       if (ENUM_UNDERLYING_TYPE (enumtype)
15944           && value
15945           && TREE_CODE (value) == INTEGER_CST)
15946         {
15947 	  if (!int_fits_type_p (value, ENUM_UNDERLYING_TYPE (enumtype)))
15948 	    error ("enumerator value %qE is outside the range of underlying "
15949 		   "type %qT", value, ENUM_UNDERLYING_TYPE (enumtype));
15950 
15951           /* Convert the value to the appropriate type.  */
15952           value = fold_convert (ENUM_UNDERLYING_TYPE (enumtype), value);
15953         }
15954     }
15955 
15956   /* C++ associates enums with global, function, or class declarations.  */
15957   context = current_scope ();
15958 
15959   /* Build the actual enumeration constant.  Note that the enumeration
15960      constants have the underlying type of the enum (if it is fixed)
15961      or the type of their initializer (if the underlying type of the
15962      enum is not fixed):
15963 
15964       [ C++0x dcl.enum ]
15965 
15966         If the underlying type is fixed, the type of each enumerator
15967         prior to the closing brace is the underlying type; if the
15968         initializing value of an enumerator cannot be represented by
15969         the underlying type, the program is ill-formed. If the
15970         underlying type is not fixed, the type of each enumerator is
15971         the type of its initializing value.
15972 
15973     If the underlying type is not fixed, it will be computed by
15974     finish_enum and we will reset the type of this enumerator.  Of
15975     course, if we're processing a template, there may be no value.  */
15976   type = value ? TREE_TYPE (value) : NULL_TREE;
15977 
15978   decl = build_decl (loc, CONST_DECL, name, type);
15979 
15980   DECL_CONTEXT (decl) = enumtype;
15981   TREE_CONSTANT (decl) = 1;
15982   TREE_READONLY (decl) = 1;
15983   DECL_INITIAL (decl) = value;
15984 
15985   if (attributes)
15986     cplus_decl_attributes (&decl, attributes, 0);
15987 
15988   if (context && context == current_class_type && !SCOPED_ENUM_P (enumtype))
15989     {
15990       /* In something like `struct S { enum E { i = 7 }; };' we put `i'
15991 	 on the TYPE_FIELDS list for `S'.  (That's so that you can say
15992 	 things like `S::i' later.)  */
15993 
15994       /* The enumerator may be getting declared outside of its enclosing
15995 	 class, like so:
15996 
15997 	   class S { public: enum E : int; }; enum S::E : int { i = 7; };
15998 
15999 	 For which case we need to make sure that the access of `S::i'
16000 	 matches the access of `S::E'.  */
16001       tree saved_cas = current_access_specifier;
16002       if (TREE_PRIVATE (TYPE_NAME (enumtype)))
16003 	current_access_specifier = access_private_node;
16004       else if (TREE_PROTECTED (TYPE_NAME (enumtype)))
16005 	current_access_specifier = access_protected_node;
16006       else
16007 	current_access_specifier = access_public_node;
16008 
16009       finish_member_declaration (decl);
16010 
16011       current_access_specifier = saved_cas;
16012     }
16013   else
16014     pushdecl (decl);
16015 
16016   /* Add this enumeration constant to the list for this type.  */
16017   TYPE_VALUES (enumtype) = tree_cons (name, decl, TYPE_VALUES (enumtype));
16018 }
16019 
16020 /* Look for an enumerator with the given NAME within the enumeration
16021    type ENUMTYPE.  This routine is used primarily for qualified name
16022    lookup into an enumerator in C++0x, e.g.,
16023 
16024      enum class Color { Red, Green, Blue };
16025 
16026      Color color = Color::Red;
16027 
16028    Returns the value corresponding to the enumerator, or
16029    NULL_TREE if no such enumerator was found.  */
16030 tree
lookup_enumerator(tree enumtype,tree name)16031 lookup_enumerator (tree enumtype, tree name)
16032 {
16033   tree e;
16034   gcc_assert (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE);
16035 
16036   e = purpose_member (name, TYPE_VALUES (enumtype));
16037   return e? TREE_VALUE (e) : NULL_TREE;
16038 }
16039 
16040 /* Implement LANG_HOOKS_SIMULATE_ENUM_DECL.  */
16041 
16042 tree
cxx_simulate_enum_decl(location_t loc,const char * name,vec<string_int_pair> values)16043 cxx_simulate_enum_decl (location_t loc, const char *name,
16044 			vec<string_int_pair> values)
16045 {
16046   location_t saved_loc = input_location;
16047   input_location = loc;
16048 
16049   tree enumtype = start_enum (get_identifier (name), NULL_TREE, NULL_TREE,
16050 			      NULL_TREE, false, NULL);
16051   if (!OPAQUE_ENUM_P (enumtype))
16052     {
16053       error_at (loc, "multiple definition of %q#T", enumtype);
16054       inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
16055 	      "previous definition here");
16056       return enumtype;
16057     }
16058   SET_OPAQUE_ENUM_P (enumtype, false);
16059   DECL_SOURCE_LOCATION (TYPE_NAME (enumtype)) = loc;
16060 
16061   string_int_pair *value;
16062   unsigned int i;
16063   FOR_EACH_VEC_ELT (values, i, value)
16064     build_enumerator (get_identifier (value->first),
16065 		      build_int_cst (integer_type_node, value->second),
16066 		      enumtype, NULL_TREE, loc);
16067 
16068   finish_enum_value_list (enumtype);
16069   finish_enum (enumtype);
16070 
16071   input_location = saved_loc;
16072   return enumtype;
16073 }
16074 
16075 /* We're defining DECL.  Make sure that its type is OK.  */
16076 
16077 static void
check_function_type(tree decl,tree current_function_parms)16078 check_function_type (tree decl, tree current_function_parms)
16079 {
16080   tree fntype = TREE_TYPE (decl);
16081   tree return_type = complete_type (TREE_TYPE (fntype));
16082 
16083   /* In a function definition, arg types must be complete.  */
16084   require_complete_types_for_parms (current_function_parms);
16085 
16086   if (dependent_type_p (return_type)
16087       || type_uses_auto (return_type))
16088     return;
16089   if (!COMPLETE_OR_VOID_TYPE_P (return_type))
16090     {
16091       tree args = TYPE_ARG_TYPES (fntype);
16092 
16093       error ("return type %q#T is incomplete", return_type);
16094 
16095       /* Make it return void instead.  */
16096       if (TREE_CODE (fntype) == METHOD_TYPE)
16097 	fntype = build_method_type_directly (TREE_TYPE (TREE_VALUE (args)),
16098 					     void_type_node,
16099 					     TREE_CHAIN (args));
16100       else
16101 	fntype = build_function_type (void_type_node, args);
16102       fntype = (cp_build_type_attribute_variant
16103 		(fntype, TYPE_ATTRIBUTES (TREE_TYPE (decl))));
16104       fntype = cxx_copy_lang_qualifiers (fntype, TREE_TYPE (decl));
16105       TREE_TYPE (decl) = fntype;
16106     }
16107   else
16108     {
16109       abstract_virtuals_error (decl, TREE_TYPE (fntype));
16110       maybe_warn_parm_abi (TREE_TYPE (fntype),
16111 			   DECL_SOURCE_LOCATION (decl));
16112     }
16113 }
16114 
16115 /* True iff FN is an implicitly-defined default constructor.  */
16116 
16117 static bool
implicit_default_ctor_p(tree fn)16118 implicit_default_ctor_p (tree fn)
16119 {
16120   return (DECL_CONSTRUCTOR_P (fn)
16121 	  && !user_provided_p (fn)
16122 	  && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn)));
16123 }
16124 
16125 /* Clobber the contents of *this to let the back end know that the object
16126    storage is dead when we enter the constructor or leave the destructor.  */
16127 
16128 static tree
build_clobber_this()16129 build_clobber_this ()
16130 {
16131   /* Clobbering an empty base is pointless, and harmful if its one byte
16132      TYPE_SIZE overlays real data.  */
16133   if (is_empty_class (current_class_type))
16134     return void_node;
16135 
16136   /* If we have virtual bases, clobber the whole object, but only if we're in
16137      charge.  If we don't have virtual bases, clobber the as-base type so we
16138      don't mess with tail padding.  */
16139   bool vbases = CLASSTYPE_VBASECLASSES (current_class_type);
16140 
16141   tree ctype = current_class_type;
16142   if (!vbases)
16143     ctype = CLASSTYPE_AS_BASE (ctype);
16144 
16145   tree clobber = build_clobber (ctype);
16146 
16147   tree thisref = current_class_ref;
16148   if (ctype != current_class_type)
16149     {
16150       thisref = build_nop (build_reference_type (ctype), current_class_ptr);
16151       thisref = convert_from_reference (thisref);
16152     }
16153 
16154   tree exprstmt = build2 (MODIFY_EXPR, void_type_node, thisref, clobber);
16155   if (vbases)
16156     exprstmt = build_if_in_charge (exprstmt);
16157 
16158   return exprstmt;
16159 }
16160 
16161 /* Create the FUNCTION_DECL for a function definition.
16162    DECLSPECS and DECLARATOR are the parts of the declaration;
16163    they describe the function's name and the type it returns,
16164    but twisted together in a fashion that parallels the syntax of C.
16165 
16166    FLAGS is a bitwise or of SF_PRE_PARSED (indicating that the
16167    DECLARATOR is really the DECL for the function we are about to
16168    process and that DECLSPECS should be ignored), SF_INCLASS_INLINE
16169    indicating that the function is an inline defined in-class.
16170 
16171    This function creates a binding context for the function body
16172    as well as setting up the FUNCTION_DECL in current_function_decl.
16173 
16174    For C++, we must first check whether that datum makes any sense.
16175    For example, "class A local_a(1,2);" means that variable local_a
16176    is an aggregate of type A, which should have a constructor
16177    applied to it with the argument list [1, 2].
16178 
16179    On entry, DECL_INITIAL (decl1) should be NULL_TREE or error_mark_node,
16180    or may be a BLOCK if the function has been defined previously
16181    in this translation unit.  On exit, DECL_INITIAL (decl1) will be
16182    error_mark_node if the function has never been defined, or
16183    a BLOCK if the function has been defined somewhere.  */
16184 
16185 bool
start_preparsed_function(tree decl1,tree attrs,int flags)16186 start_preparsed_function (tree decl1, tree attrs, int flags)
16187 {
16188   tree ctype = NULL_TREE;
16189   tree fntype;
16190   tree restype;
16191   int doing_friend = 0;
16192   cp_binding_level *bl;
16193   tree current_function_parms;
16194   struct c_fileinfo *finfo
16195     = get_fileinfo (LOCATION_FILE (DECL_SOURCE_LOCATION (decl1)));
16196   bool honor_interface;
16197 
16198   /* Sanity check.  */
16199   gcc_assert (VOID_TYPE_P (TREE_VALUE (void_list_node)));
16200   gcc_assert (TREE_CHAIN (void_list_node) == NULL_TREE);
16201 
16202   fntype = TREE_TYPE (decl1);
16203   if (TREE_CODE (fntype) == METHOD_TYPE)
16204     ctype = TYPE_METHOD_BASETYPE (fntype);
16205 
16206   /* ISO C++ 11.4/5.  A friend function defined in a class is in
16207      the (lexical) scope of the class in which it is defined.  */
16208   if (!ctype && DECL_FRIEND_P (decl1))
16209     {
16210       ctype = DECL_FRIEND_CONTEXT (decl1);
16211 
16212       /* CTYPE could be null here if we're dealing with a template;
16213 	 for example, `inline friend float foo()' inside a template
16214 	 will have no CTYPE set.  */
16215       if (ctype && TREE_CODE (ctype) != RECORD_TYPE)
16216 	ctype = NULL_TREE;
16217       else
16218 	doing_friend = 1;
16219     }
16220 
16221   if (DECL_DECLARED_INLINE_P (decl1)
16222       && lookup_attribute ("noinline", attrs))
16223     warning_at (DECL_SOURCE_LOCATION (decl1), 0,
16224 		"inline function %qD given attribute %qs", decl1, "noinline");
16225 
16226   /* Handle gnu_inline attribute.  */
16227   if (GNU_INLINE_P (decl1))
16228     {
16229       DECL_EXTERNAL (decl1) = 1;
16230       DECL_NOT_REALLY_EXTERN (decl1) = 0;
16231       DECL_INTERFACE_KNOWN (decl1) = 1;
16232       DECL_DISREGARD_INLINE_LIMITS (decl1) = 1;
16233     }
16234 
16235   if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl1))
16236     /* This is a constructor, we must ensure that any default args
16237        introduced by this definition are propagated to the clones
16238        now. The clones are used directly in overload resolution.  */
16239     adjust_clone_args (decl1);
16240 
16241   /* Sometimes we don't notice that a function is a static member, and
16242      build a METHOD_TYPE for it.  Fix that up now.  */
16243   gcc_assert (!(ctype != NULL_TREE && DECL_STATIC_FUNCTION_P (decl1)
16244 		&& TREE_CODE (TREE_TYPE (decl1)) == METHOD_TYPE));
16245 
16246   /* Set up current_class_type, and enter the scope of the class, if
16247      appropriate.  */
16248   if (ctype)
16249     push_nested_class (ctype);
16250   else if (DECL_STATIC_FUNCTION_P (decl1))
16251     push_nested_class (DECL_CONTEXT (decl1));
16252 
16253   /* Now that we have entered the scope of the class, we must restore
16254      the bindings for any template parameters surrounding DECL1, if it
16255      is an inline member template.  (Order is important; consider the
16256      case where a template parameter has the same name as a field of
16257      the class.)  It is not until after this point that
16258      PROCESSING_TEMPLATE_DECL is guaranteed to be set up correctly.  */
16259   if (flags & SF_INCLASS_INLINE)
16260     maybe_begin_member_template_processing (decl1);
16261 
16262   /* Effective C++ rule 15.  */
16263   if (warn_ecpp
16264       && DECL_ASSIGNMENT_OPERATOR_P (decl1)
16265       && DECL_OVERLOADED_OPERATOR_IS (decl1, NOP_EXPR)
16266       && VOID_TYPE_P (TREE_TYPE (fntype)))
16267     warning (OPT_Weffc__,
16268 	     "%<operator=%> should return a reference to %<*this%>");
16269 
16270   /* Make the init_value nonzero so pushdecl knows this is not tentative.
16271      error_mark_node is replaced below (in poplevel) with the BLOCK.  */
16272   if (!DECL_INITIAL (decl1))
16273     DECL_INITIAL (decl1) = error_mark_node;
16274 
16275   /* This function exists in static storage.
16276      (This does not mean `static' in the C sense!)  */
16277   TREE_STATIC (decl1) = 1;
16278 
16279   /* We must call push_template_decl after current_class_type is set
16280      up.  (If we are processing inline definitions after exiting a
16281      class scope, current_class_type will be NULL_TREE until set above
16282      by push_nested_class.)  */
16283   if (processing_template_decl)
16284     {
16285       tree newdecl1 = push_template_decl (decl1);
16286       if (newdecl1 == error_mark_node)
16287 	{
16288 	  if (ctype || DECL_STATIC_FUNCTION_P (decl1))
16289 	    pop_nested_class ();
16290 	  return false;
16291 	}
16292       decl1 = newdecl1;
16293     }
16294 
16295   /* Make sure the parameter and return types are reasonable.  When
16296      you declare a function, these types can be incomplete, but they
16297      must be complete when you define the function.  */
16298   check_function_type (decl1, DECL_ARGUMENTS (decl1));
16299 
16300   /* Build the return declaration for the function.  */
16301   restype = TREE_TYPE (fntype);
16302 
16303   if (DECL_RESULT (decl1) == NULL_TREE)
16304     {
16305       tree resdecl;
16306 
16307       resdecl = build_decl (input_location, RESULT_DECL, 0, restype);
16308       DECL_ARTIFICIAL (resdecl) = 1;
16309       DECL_IGNORED_P (resdecl) = 1;
16310       DECL_RESULT (decl1) = resdecl;
16311 
16312       cp_apply_type_quals_to_decl (cp_type_quals (restype), resdecl);
16313     }
16314 
16315   /* Record the decl so that the function name is defined.
16316      If we already have a decl for this name, and it is a FUNCTION_DECL,
16317      use the old decl.  */
16318   if (!processing_template_decl && !(flags & SF_PRE_PARSED))
16319     {
16320       /* A specialization is not used to guide overload resolution.  */
16321       if (!DECL_FUNCTION_MEMBER_P (decl1)
16322 	  && !(DECL_USE_TEMPLATE (decl1) &&
16323 	       PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl1))))
16324 	{
16325 	  tree olddecl = pushdecl (decl1);
16326 
16327 	  if (olddecl == error_mark_node)
16328 	    /* If something went wrong when registering the declaration,
16329 	       use DECL1; we have to have a FUNCTION_DECL to use when
16330 	       parsing the body of the function.  */
16331 	    ;
16332 	  else
16333 	    {
16334 	      /* Otherwise, OLDDECL is either a previous declaration
16335 		 of the same function or DECL1 itself.  */
16336 
16337 	      if (warn_missing_declarations
16338 		  && olddecl == decl1
16339 		  && !DECL_MAIN_P (decl1)
16340 		  && TREE_PUBLIC (decl1)
16341 		  && !DECL_DECLARED_INLINE_P (decl1))
16342 		{
16343 		  tree context;
16344 
16345 		  /* Check whether DECL1 is in an anonymous
16346 		     namespace.  */
16347 		  for (context = DECL_CONTEXT (decl1);
16348 		       context;
16349 		       context = DECL_CONTEXT (context))
16350 		    {
16351 		      if (TREE_CODE (context) == NAMESPACE_DECL
16352 			  && DECL_NAME (context) == NULL_TREE)
16353 			break;
16354 		    }
16355 
16356 		  if (context == NULL)
16357 		    warning_at (DECL_SOURCE_LOCATION (decl1),
16358 				OPT_Wmissing_declarations,
16359 				"no previous declaration for %qD", decl1);
16360 		}
16361 
16362 	      decl1 = olddecl;
16363 	    }
16364 	}
16365       else
16366 	{
16367 	  /* We need to set the DECL_CONTEXT.  */
16368 	  if (!DECL_CONTEXT (decl1) && DECL_TEMPLATE_INFO (decl1))
16369 	    DECL_CONTEXT (decl1) = DECL_CONTEXT (DECL_TI_TEMPLATE (decl1));
16370 	}
16371       fntype = TREE_TYPE (decl1);
16372       restype = TREE_TYPE (fntype);
16373 
16374       /* If #pragma weak applies, mark the decl appropriately now.
16375 	 The pragma only applies to global functions.  Because
16376 	 determining whether or not the #pragma applies involves
16377 	 computing the mangled name for the declaration, we cannot
16378 	 apply the pragma until after we have merged this declaration
16379 	 with any previous declarations; if the original declaration
16380 	 has a linkage specification, that specification applies to
16381 	 the definition as well, and may affect the mangled name.  */
16382       if (DECL_FILE_SCOPE_P (decl1))
16383 	maybe_apply_pragma_weak (decl1);
16384     }
16385 
16386   /* We are now in the scope of the function being defined.  */
16387   current_function_decl = decl1;
16388 
16389   /* Save the parm names or decls from this function's declarator
16390      where store_parm_decls will find them.  */
16391   current_function_parms = DECL_ARGUMENTS (decl1);
16392 
16393   /* Let the user know we're compiling this function.  */
16394   announce_function (decl1);
16395 
16396   gcc_assert (DECL_INITIAL (decl1));
16397 
16398   /* This function may already have been parsed, in which case just
16399      return; our caller will skip over the body without parsing.  */
16400   if (DECL_INITIAL (decl1) != error_mark_node)
16401     return true;
16402 
16403   /* Initialize RTL machinery.  We cannot do this until
16404      CURRENT_FUNCTION_DECL and DECL_RESULT are set up.  We do this
16405      even when processing a template; this is how we get
16406      CFUN set up, and our per-function variables initialized.
16407      FIXME factor out the non-RTL stuff.  */
16408   bl = current_binding_level;
16409   allocate_struct_function (decl1, processing_template_decl);
16410 
16411   /* Initialize the language data structures.  Whenever we start
16412      a new function, we destroy temporaries in the usual way.  */
16413   cfun->language = ggc_cleared_alloc<language_function> ();
16414   current_stmt_tree ()->stmts_are_full_exprs_p = 1;
16415   current_binding_level = bl;
16416 
16417   /* If we are (erroneously) defining a function that we have already
16418      defined before, wipe out what we knew before.  */
16419   gcc_checking_assert (!DECL_PENDING_INLINE_P (decl1));
16420   FNDECL_USED_AUTO (decl1) = false;
16421   DECL_SAVED_AUTO_RETURN_TYPE (decl1) = NULL;
16422 
16423   if (!processing_template_decl && type_uses_auto (restype))
16424     {
16425       FNDECL_USED_AUTO (decl1) = true;
16426       DECL_SAVED_AUTO_RETURN_TYPE (decl1) = restype;
16427     }
16428 
16429   /* Start the statement-tree, start the tree now.  */
16430   DECL_SAVED_TREE (decl1) = push_stmt_list ();
16431 
16432   if (ctype && !doing_friend && !DECL_STATIC_FUNCTION_P (decl1))
16433     {
16434       /* We know that this was set up by `grokclassfn'.  We do not
16435 	 wait until `store_parm_decls', since evil parse errors may
16436 	 never get us to that point.  Here we keep the consistency
16437 	 between `current_class_type' and `current_class_ptr'.  */
16438       tree t = DECL_ARGUMENTS (decl1);
16439 
16440       gcc_assert (t != NULL_TREE && TREE_CODE (t) == PARM_DECL);
16441       gcc_assert (TYPE_PTR_P (TREE_TYPE (t)));
16442 
16443       cp_function_chain->x_current_class_ref
16444 	= cp_build_fold_indirect_ref (t);
16445       /* Set this second to avoid shortcut in cp_build_indirect_ref.  */
16446       cp_function_chain->x_current_class_ptr = t;
16447 
16448       /* Constructors and destructors need to know whether they're "in
16449 	 charge" of initializing virtual base classes.  */
16450       t = DECL_CHAIN (t);
16451       if (DECL_HAS_IN_CHARGE_PARM_P (decl1))
16452 	{
16453 	  current_in_charge_parm = t;
16454 	  t = DECL_CHAIN (t);
16455 	}
16456       if (DECL_HAS_VTT_PARM_P (decl1))
16457 	{
16458 	  gcc_assert (DECL_NAME (t) == vtt_parm_identifier);
16459 	  current_vtt_parm = t;
16460 	}
16461     }
16462 
16463   honor_interface = (!DECL_TEMPLATE_INSTANTIATION (decl1)
16464 		     /* Implicitly-defined methods (like the
16465 			destructor for a class in which no destructor
16466 			is explicitly declared) must not be defined
16467 			until their definition is needed.  So, we
16468 			ignore interface specifications for
16469 			compiler-generated functions.  */
16470 		     && !DECL_ARTIFICIAL (decl1));
16471 
16472   if (processing_template_decl)
16473     /* Don't mess with interface flags.  */;
16474   else if (DECL_INTERFACE_KNOWN (decl1))
16475     {
16476       tree ctx = decl_function_context (decl1);
16477 
16478       if (DECL_NOT_REALLY_EXTERN (decl1))
16479 	DECL_EXTERNAL (decl1) = 0;
16480 
16481       if (ctx != NULL_TREE && vague_linkage_p (ctx))
16482 	/* This is a function in a local class in an extern inline
16483 	   or template function.  */
16484 	comdat_linkage (decl1);
16485     }
16486   /* If this function belongs to an interface, it is public.
16487      If it belongs to someone else's interface, it is also external.
16488      This only affects inlines and template instantiations.  */
16489   else if (!finfo->interface_unknown && honor_interface)
16490     {
16491       if (DECL_DECLARED_INLINE_P (decl1)
16492 	  || DECL_TEMPLATE_INSTANTIATION (decl1))
16493 	{
16494 	  DECL_EXTERNAL (decl1)
16495 	    = (finfo->interface_only
16496 	       || (DECL_DECLARED_INLINE_P (decl1)
16497 		   && ! flag_implement_inlines
16498 		   && !DECL_VINDEX (decl1)));
16499 
16500 	  /* For WIN32 we also want to put these in linkonce sections.  */
16501 	  maybe_make_one_only (decl1);
16502 	}
16503       else
16504 	DECL_EXTERNAL (decl1) = 0;
16505       DECL_INTERFACE_KNOWN (decl1) = 1;
16506       /* If this function is in an interface implemented in this file,
16507 	 make sure that the back end knows to emit this function
16508 	 here.  */
16509       if (!DECL_EXTERNAL (decl1))
16510 	mark_needed (decl1);
16511     }
16512   else if (finfo->interface_unknown && finfo->interface_only
16513 	   && honor_interface)
16514     {
16515       /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma
16516 	 interface, we will have both finfo->interface_unknown and
16517 	 finfo->interface_only set.  In that case, we don't want to
16518 	 use the normal heuristics because someone will supply a
16519 	 #pragma implementation elsewhere, and deducing it here would
16520 	 produce a conflict.  */
16521       comdat_linkage (decl1);
16522       DECL_EXTERNAL (decl1) = 0;
16523       DECL_INTERFACE_KNOWN (decl1) = 1;
16524       DECL_DEFER_OUTPUT (decl1) = 1;
16525     }
16526   else
16527     {
16528       /* This is a definition, not a reference.
16529 	 So clear DECL_EXTERNAL, unless this is a GNU extern inline.  */
16530       if (!GNU_INLINE_P (decl1))
16531 	DECL_EXTERNAL (decl1) = 0;
16532 
16533       if ((DECL_DECLARED_INLINE_P (decl1)
16534 	   || DECL_TEMPLATE_INSTANTIATION (decl1))
16535 	  && ! DECL_INTERFACE_KNOWN (decl1))
16536 	DECL_DEFER_OUTPUT (decl1) = 1;
16537       else
16538 	DECL_INTERFACE_KNOWN (decl1) = 1;
16539     }
16540 
16541   /* Determine the ELF visibility attribute for the function.  We must not
16542      do this before calling "pushdecl", as we must allow "duplicate_decls"
16543      to merge any attributes appropriately.  We also need to wait until
16544      linkage is set.  */
16545   if (!DECL_CLONED_FUNCTION_P (decl1))
16546     determine_visibility (decl1);
16547 
16548   if (!processing_template_decl)
16549     maybe_instantiate_noexcept (decl1);
16550 
16551   begin_scope (sk_function_parms, decl1);
16552 
16553   ++function_depth;
16554 
16555   if (DECL_DESTRUCTOR_P (decl1)
16556       || (DECL_CONSTRUCTOR_P (decl1)
16557 	  && targetm.cxx.cdtor_returns_this ()))
16558     {
16559       cdtor_label = create_artificial_label (input_location);
16560       LABEL_DECL_CDTOR (cdtor_label) = true;
16561     }
16562 
16563   start_fname_decls ();
16564 
16565   store_parm_decls (current_function_parms);
16566 
16567   push_operator_bindings ();
16568 
16569   if (!processing_template_decl
16570       && (flag_lifetime_dse > 1)
16571       && DECL_CONSTRUCTOR_P (decl1)
16572       && !DECL_CLONED_FUNCTION_P (decl1)
16573       /* Clobbering an empty base is harmful if it overlays real data.  */
16574       && !is_empty_class (current_class_type)
16575       /* We can't clobber safely for an implicitly-defined default constructor
16576 	 because part of the initialization might happen before we enter the
16577 	 constructor, via AGGR_INIT_ZERO_FIRST (c++/68006).  */
16578       && !implicit_default_ctor_p (decl1))
16579     finish_expr_stmt (build_clobber_this ());
16580 
16581   if (!processing_template_decl
16582       && DECL_CONSTRUCTOR_P (decl1)
16583       && sanitize_flags_p (SANITIZE_VPTR)
16584       && !DECL_CLONED_FUNCTION_P (decl1)
16585       && !implicit_default_ctor_p (decl1))
16586     cp_ubsan_maybe_initialize_vtbl_ptrs (current_class_ptr);
16587 
16588   if (!DECL_OMP_DECLARE_REDUCTION_P (decl1))
16589     start_lambda_scope (decl1);
16590 
16591   return true;
16592 }
16593 
16594 
16595 /* Like start_preparsed_function, except that instead of a
16596    FUNCTION_DECL, this function takes DECLSPECS and DECLARATOR.
16597 
16598    Returns true on success.  If the DECLARATOR is not suitable
16599    for a function, we return false, which tells the parser to
16600    skip the entire function.  */
16601 
16602 bool
start_function(cp_decl_specifier_seq * declspecs,const cp_declarator * declarator,tree attrs)16603 start_function (cp_decl_specifier_seq *declspecs,
16604 		const cp_declarator *declarator,
16605 		tree attrs)
16606 {
16607   tree decl1;
16608 
16609   decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, &attrs);
16610   invoke_plugin_callbacks (PLUGIN_START_PARSE_FUNCTION, decl1);
16611   if (decl1 == error_mark_node)
16612     return false;
16613 
16614   if (DECL_MAIN_P (decl1))
16615     /* main must return int.  grokfndecl should have corrected it
16616        (and issued a diagnostic) if the user got it wrong.  */
16617     gcc_assert (same_type_p (TREE_TYPE (TREE_TYPE (decl1)),
16618 			     integer_type_node));
16619 
16620   return start_preparsed_function (decl1, attrs, /*flags=*/SF_DEFAULT);
16621 }
16622 
16623 /* Returns true iff an EH_SPEC_BLOCK should be created in the body of
16624    FN.  */
16625 
16626 static bool
use_eh_spec_block(tree fn)16627 use_eh_spec_block (tree fn)
16628 {
16629   return (flag_exceptions && flag_enforce_eh_specs
16630 	  && !processing_template_decl
16631 	  /* We insert the EH_SPEC_BLOCK only in the original
16632 	     function; then, it is copied automatically to the
16633 	     clones.  */
16634 	  && !DECL_CLONED_FUNCTION_P (fn)
16635 	  /* Implicitly-generated constructors and destructors have
16636 	     exception specifications.  However, those specifications
16637 	     are the union of the possible exceptions specified by the
16638 	     constructors/destructors for bases and members, so no
16639 	     unallowed exception will ever reach this function.  By
16640 	     not creating the EH_SPEC_BLOCK we save a little memory,
16641 	     and we avoid spurious warnings about unreachable
16642 	     code.  */
16643 	  && !DECL_DEFAULTED_FN (fn)
16644 	  && !type_throw_all_p (TREE_TYPE (fn)));
16645 }
16646 
16647 /* Helper function to push ARGS into the current lexical scope.  DECL
16648    is the function declaration.  NONPARMS is used to handle enum
16649    constants.  */
16650 
16651 void
do_push_parm_decls(tree decl,tree args,tree * nonparms)16652 do_push_parm_decls (tree decl, tree args, tree *nonparms)
16653 {
16654   /* If we're doing semantic analysis, then we'll call pushdecl
16655      for each of these.  We must do them in reverse order so that
16656      they end in the correct forward order.  */
16657   args = nreverse (args);
16658 
16659   tree next;
16660   for (tree parm = args; parm; parm = next)
16661     {
16662       next = DECL_CHAIN (parm);
16663       if (TREE_CODE (parm) == PARM_DECL)
16664 	pushdecl (parm);
16665       else if (nonparms)
16666 	{
16667 	  /* If we find an enum constant or a type tag, put it aside for
16668 	     the moment.  */
16669 	  TREE_CHAIN (parm) = NULL_TREE;
16670 	  *nonparms = chainon (*nonparms, parm);
16671 	}
16672     }
16673 
16674   /* Get the decls in their original chain order and record in the
16675      function.  This is all and only the PARM_DECLs that were
16676      pushed into scope by the loop above.  */
16677   DECL_ARGUMENTS (decl) = get_local_decls ();
16678 }
16679 
16680 /* Store the parameter declarations into the current function declaration.
16681    This is called after parsing the parameter declarations, before
16682    digesting the body of the function.
16683 
16684    Also install to binding contour return value identifier, if any.  */
16685 
16686 static void
store_parm_decls(tree current_function_parms)16687 store_parm_decls (tree current_function_parms)
16688 {
16689   tree fndecl = current_function_decl;
16690 
16691   /* This is a chain of any other decls that came in among the parm
16692      declarations.  If a parm is declared with  enum {foo, bar} x;
16693      then CONST_DECLs for foo and bar are put here.  */
16694   tree nonparms = NULL_TREE;
16695 
16696   if (current_function_parms)
16697     {
16698       /* This case is when the function was defined with an ANSI prototype.
16699 	 The parms already have decls, so we need not do anything here
16700 	 except record them as in effect
16701 	 and complain if any redundant old-style parm decls were written.  */
16702 
16703       tree specparms = current_function_parms;
16704 
16705       /* Must clear this because it might contain TYPE_DECLs declared
16706 	     at class level.  */
16707       current_binding_level->names = NULL;
16708 
16709       do_push_parm_decls (fndecl, specparms, &nonparms);
16710     }
16711   else
16712     DECL_ARGUMENTS (fndecl) = NULL_TREE;
16713 
16714   /* Now store the final chain of decls for the arguments
16715      as the decl-chain of the current lexical scope.
16716      Put the enumerators in as well, at the front so that
16717      DECL_ARGUMENTS is not modified.  */
16718   current_binding_level->names = chainon (nonparms, DECL_ARGUMENTS (fndecl));
16719 
16720   if (use_eh_spec_block (current_function_decl))
16721     current_eh_spec_block = begin_eh_spec_block ();
16722 }
16723 
16724 
16725 /* Set the return value of the constructor (if present).  */
16726 
16727 static void
finish_constructor_body(void)16728 finish_constructor_body (void)
16729 {
16730   tree val;
16731   tree exprstmt;
16732 
16733   if (targetm.cxx.cdtor_returns_this ())
16734     {
16735       /* Any return from a constructor will end up here.  */
16736       add_stmt (build_stmt (input_location, LABEL_EXPR, cdtor_label));
16737 
16738       val = DECL_ARGUMENTS (current_function_decl);
16739       val = build2 (MODIFY_EXPR, TREE_TYPE (val),
16740 		    DECL_RESULT (current_function_decl), val);
16741       /* Return the address of the object.  */
16742       exprstmt = build_stmt (input_location, RETURN_EXPR, val);
16743       add_stmt (exprstmt);
16744     }
16745 }
16746 
16747 /* Do all the processing for the beginning of a destructor; set up the
16748    vtable pointers and cleanups for bases and members.  */
16749 
16750 static void
begin_destructor_body(void)16751 begin_destructor_body (void)
16752 {
16753   tree compound_stmt;
16754 
16755   /* If the CURRENT_CLASS_TYPE is incomplete, we will have already
16756      issued an error message.  We still want to try to process the
16757      body of the function, but initialize_vtbl_ptrs will crash if
16758      TYPE_BINFO is NULL.  */
16759   if (COMPLETE_TYPE_P (current_class_type))
16760     {
16761       compound_stmt = begin_compound_stmt (0);
16762       /* Make all virtual function table pointers in non-virtual base
16763 	 classes point to CURRENT_CLASS_TYPE's virtual function
16764 	 tables.  */
16765       initialize_vtbl_ptrs (current_class_ptr);
16766       finish_compound_stmt (compound_stmt);
16767 
16768       if (flag_lifetime_dse
16769 	  /* Clobbering an empty base is harmful if it overlays real data.  */
16770 	  && !is_empty_class (current_class_type))
16771       {
16772 	if (sanitize_flags_p (SANITIZE_VPTR)
16773 	    && (flag_sanitize_recover & SANITIZE_VPTR) == 0
16774 	    && TYPE_CONTAINS_VPTR_P (current_class_type))
16775 	  {
16776 	    tree binfo = TYPE_BINFO (current_class_type);
16777 	    tree ref
16778 	      = cp_build_fold_indirect_ref (current_class_ptr);
16779 
16780 	    tree vtbl_ptr = build_vfield_ref (ref, TREE_TYPE (binfo));
16781 	    tree vtbl = build_zero_cst (TREE_TYPE (vtbl_ptr));
16782 	    tree stmt = cp_build_modify_expr (input_location, vtbl_ptr,
16783 					      NOP_EXPR, vtbl,
16784 					      tf_warning_or_error);
16785 	    /* If the vptr is shared with some virtual nearly empty base,
16786 	       don't clear it if not in charge, the dtor of the virtual
16787 	       nearly empty base will do that later.  */
16788 	    if (CLASSTYPE_VBASECLASSES (current_class_type))
16789 	      {
16790 		tree c = current_class_type;
16791 		while (CLASSTYPE_PRIMARY_BINFO (c))
16792 		  {
16793 		    if (BINFO_VIRTUAL_P (CLASSTYPE_PRIMARY_BINFO (c)))
16794 		      {
16795 			stmt = convert_to_void (stmt, ICV_STATEMENT,
16796 						tf_warning_or_error);
16797 			stmt = build_if_in_charge (stmt);
16798 			break;
16799 		      }
16800 		    c = BINFO_TYPE (CLASSTYPE_PRIMARY_BINFO (c));
16801 		  }
16802 	      }
16803 	    finish_decl_cleanup (NULL_TREE, stmt);
16804 	  }
16805 	else
16806 	  finish_decl_cleanup (NULL_TREE, build_clobber_this ());
16807       }
16808 
16809       /* And insert cleanups for our bases and members so that they
16810 	 will be properly destroyed if we throw.  */
16811       push_base_cleanups ();
16812     }
16813 }
16814 
16815 /* At the end of every destructor we generate code to delete the object if
16816    necessary.  Do that now.  */
16817 
16818 static void
finish_destructor_body(void)16819 finish_destructor_body (void)
16820 {
16821   tree exprstmt;
16822 
16823   /* Any return from a destructor will end up here; that way all base
16824      and member cleanups will be run when the function returns.  */
16825   add_stmt (build_stmt (input_location, LABEL_EXPR, cdtor_label));
16826 
16827   if (targetm.cxx.cdtor_returns_this ())
16828     {
16829       tree val;
16830 
16831       val = DECL_ARGUMENTS (current_function_decl);
16832       val = build2 (MODIFY_EXPR, TREE_TYPE (val),
16833 		    DECL_RESULT (current_function_decl), val);
16834       /* Return the address of the object.  */
16835       exprstmt = build_stmt (input_location, RETURN_EXPR, val);
16836       add_stmt (exprstmt);
16837     }
16838 }
16839 
16840 /* Do the necessary processing for the beginning of a function body, which
16841    in this case includes member-initializers, but not the catch clauses of
16842    a function-try-block.  Currently, this means opening a binding level
16843    for the member-initializers (in a ctor), member cleanups (in a dtor),
16844    and capture proxies (in a lambda operator()).  */
16845 
16846 tree
begin_function_body(void)16847 begin_function_body (void)
16848 {
16849   tree stmt;
16850 
16851   if (! FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
16852     return NULL_TREE;
16853 
16854   if (processing_template_decl)
16855     /* Do nothing now.  */;
16856   else
16857     /* Always keep the BLOCK node associated with the outermost pair of
16858        curly braces of a function.  These are needed for correct
16859        operation of dwarfout.c.  */
16860     keep_next_level (true);
16861 
16862   stmt = begin_compound_stmt (BCS_FN_BODY);
16863 
16864   if (processing_template_decl)
16865     /* Do nothing now.  */;
16866   else if (DECL_DESTRUCTOR_P (current_function_decl))
16867     begin_destructor_body ();
16868 
16869   return stmt;
16870 }
16871 
16872 /* Do the processing for the end of a function body.  Currently, this means
16873    closing out the cleanups for fully-constructed bases and members, and in
16874    the case of the destructor, deleting the object if desired.  Again, this
16875    is only meaningful for [cd]tors, since they are the only functions where
16876    there is a significant distinction between the main body and any
16877    function catch clauses.  Handling, say, main() return semantics here
16878    would be wrong, as flowing off the end of a function catch clause for
16879    main() would also need to return 0.  */
16880 
16881 void
finish_function_body(tree compstmt)16882 finish_function_body (tree compstmt)
16883 {
16884   if (compstmt == NULL_TREE)
16885     return;
16886 
16887   /* Close the block.  */
16888   finish_compound_stmt (compstmt);
16889 
16890   if (processing_template_decl)
16891     /* Do nothing now.  */;
16892   else if (DECL_CONSTRUCTOR_P (current_function_decl))
16893     finish_constructor_body ();
16894   else if (DECL_DESTRUCTOR_P (current_function_decl))
16895     finish_destructor_body ();
16896 }
16897 
16898 /* Given a function, returns the BLOCK corresponding to the outermost level
16899    of curly braces, skipping the artificial block created for constructor
16900    initializers.  */
16901 
16902 tree
outer_curly_brace_block(tree fndecl)16903 outer_curly_brace_block (tree fndecl)
16904 {
16905   tree block = DECL_INITIAL (fndecl);
16906   if (BLOCK_OUTER_CURLY_BRACE_P (block))
16907     return block;
16908   block = BLOCK_SUBBLOCKS (block);
16909   if (BLOCK_OUTER_CURLY_BRACE_P (block))
16910     return block;
16911   block = BLOCK_SUBBLOCKS (block);
16912   gcc_assert (BLOCK_OUTER_CURLY_BRACE_P (block));
16913   return block;
16914 }
16915 
16916 /* If FNDECL is a class's key method, add the class to the list of
16917    keyed classes that should be emitted.  */
16918 
16919 static void
record_key_method_defined(tree fndecl)16920 record_key_method_defined (tree fndecl)
16921 {
16922   if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fndecl)
16923       && DECL_VIRTUAL_P (fndecl)
16924       && !processing_template_decl)
16925     {
16926       tree fnclass = DECL_CONTEXT (fndecl);
16927       if (fndecl == CLASSTYPE_KEY_METHOD (fnclass))
16928 	vec_safe_push (keyed_classes, fnclass);
16929     }
16930 }
16931 
16932 /* Subroutine of finish_function.
16933    Save the body of constexpr functions for possible
16934    future compile time evaluation.  */
16935 
16936 static void
maybe_save_function_definition(tree fun)16937 maybe_save_function_definition (tree fun)
16938 {
16939   if (!processing_template_decl
16940       && DECL_DECLARED_CONSTEXPR_P (fun)
16941       && !cp_function_chain->invalid_constexpr
16942       && !DECL_CLONED_FUNCTION_P (fun))
16943     register_constexpr_fundef (fun, DECL_SAVED_TREE (fun));
16944 }
16945 
16946 /* Attempt to add a fix-it hint to RICHLOC suggesting the insertion
16947    of "return *this;" immediately before its location, using FNDECL's
16948    first statement (if any) to give the indentation, if appropriate.  */
16949 
16950 static void
add_return_star_this_fixit(gcc_rich_location * richloc,tree fndecl)16951 add_return_star_this_fixit (gcc_rich_location *richloc, tree fndecl)
16952 {
16953   location_t indent = UNKNOWN_LOCATION;
16954   tree stmts = expr_first (DECL_SAVED_TREE (fndecl));
16955   if (stmts)
16956     indent = EXPR_LOCATION (stmts);
16957   richloc->add_fixit_insert_formatted ("return *this;",
16958 				       richloc->get_loc (),
16959 				       indent);
16960 }
16961 
16962 /* This function carries out the subset of finish_function operations needed
16963    to emit the compiler-generated outlined helper functions used by the
16964    coroutines implementation.  */
16965 
16966 static void
emit_coro_helper(tree helper)16967 emit_coro_helper (tree helper)
16968 {
16969   /* This is a partial set of the operations done by finish_function()
16970      plus emitting the result.  */
16971   set_cfun (NULL);
16972   current_function_decl = helper;
16973   begin_scope (sk_function_parms, NULL);
16974   store_parm_decls (DECL_ARGUMENTS (helper));
16975   announce_function (helper);
16976   allocate_struct_function (helper, false);
16977   cfun->language = ggc_cleared_alloc<language_function> ();
16978   poplevel (1, 0, 1);
16979   maybe_save_function_definition (helper);
16980   /* We must start each function with a clear fold cache.  */
16981   clear_fold_cache ();
16982   cp_fold_function (helper);
16983   DECL_CONTEXT (DECL_RESULT (helper)) = helper;
16984   BLOCK_SUPERCONTEXT (DECL_INITIAL (helper)) = helper;
16985   /* This function has coroutine IFNs that we should handle in middle
16986      end lowering.  */
16987   cfun->coroutine_component = true;
16988   cp_genericize (helper);
16989   expand_or_defer_fn (helper);
16990 }
16991 
16992 /* Finish up a function declaration and compile that function
16993    all the way to assembler language output.  The free the storage
16994    for the function definition. INLINE_P is TRUE if we just
16995    finished processing the body of an in-class inline function
16996    definition.  (This processing will have taken place after the
16997    class definition is complete.)  */
16998 
16999 tree
finish_function(bool inline_p)17000 finish_function (bool inline_p)
17001 {
17002   tree fndecl = current_function_decl;
17003   tree fntype, ctype = NULL_TREE;
17004   tree resumer = NULL_TREE, destroyer = NULL_TREE;
17005   bool coro_p = flag_coroutines
17006 		&& !processing_template_decl
17007 		&& DECL_COROUTINE_P (fndecl);
17008   bool coro_emit_helpers = false;
17009 
17010   /* When we get some parse errors, we can end up without a
17011      current_function_decl, so cope.  */
17012   if (fndecl == NULL_TREE)
17013     return error_mark_node;
17014 
17015   if (!DECL_OMP_DECLARE_REDUCTION_P (fndecl))
17016     finish_lambda_scope ();
17017 
17018   if (c_dialect_objc ())
17019     objc_finish_function ();
17020 
17021   record_key_method_defined (fndecl);
17022 
17023   fntype = TREE_TYPE (fndecl);
17024 
17025   /*  TREE_READONLY (fndecl) = 1;
17026       This caused &foo to be of type ptr-to-const-function
17027       which then got a warning when stored in a ptr-to-function variable.  */
17028 
17029   gcc_assert (building_stmt_list_p ());
17030   /* The current function is being defined, so its DECL_INITIAL should
17031      be set, and unless there's a multiple definition, it should be
17032      error_mark_node.  */
17033   gcc_assert (DECL_INITIAL (fndecl) == error_mark_node);
17034 
17035   if (coro_p)
17036     {
17037       /* Only try to emit the coroutine outlined helper functions if the
17038 	 transforms succeeded.  Otherwise, treat errors in the same way as
17039 	 a regular function.  */
17040       coro_emit_helpers = morph_fn_to_coro (fndecl, &resumer, &destroyer);
17041 
17042       /* We should handle coroutine IFNs in middle end lowering.  */
17043       cfun->coroutine_component = true;
17044 
17045       /* Do not try to process the ramp's EH unless outlining succeeded.  */
17046       if (coro_emit_helpers && use_eh_spec_block (fndecl))
17047 	finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
17048 			      (TREE_TYPE (fndecl)),
17049 			      current_eh_spec_block);
17050     }
17051   else
17052   /* For a cloned function, we've already got all the code we need;
17053      there's no need to add any extra bits.  */
17054   if (!DECL_CLONED_FUNCTION_P (fndecl))
17055     {
17056       /* Make it so that `main' always returns 0 by default.  */
17057       if (DECL_MAIN_P (current_function_decl))
17058 	finish_return_stmt (integer_zero_node);
17059 
17060       if (use_eh_spec_block (current_function_decl))
17061 	finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
17062 			      (TREE_TYPE (current_function_decl)),
17063 			      current_eh_spec_block);
17064     }
17065 
17066   /* If we're saving up tree structure, tie off the function now.  */
17067   DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
17068 
17069   finish_fname_decls ();
17070 
17071   /* If this function can't throw any exceptions, remember that.  */
17072   if (!processing_template_decl
17073       && !cp_function_chain->can_throw
17074       && !flag_non_call_exceptions
17075       && !decl_replaceable_p (fndecl))
17076     TREE_NOTHROW (fndecl) = 1;
17077 
17078   /* This must come after expand_function_end because cleanups might
17079      have declarations (from inline functions) that need to go into
17080      this function's blocks.  */
17081 
17082   /* If the current binding level isn't the outermost binding level
17083      for this function, either there is a bug, or we have experienced
17084      syntax errors and the statement tree is malformed.  */
17085   if (current_binding_level->kind != sk_function_parms)
17086     {
17087       /* Make sure we have already experienced errors.  */
17088       gcc_assert (errorcount);
17089 
17090       /* Throw away the broken statement tree and extra binding
17091 	 levels.  */
17092       DECL_SAVED_TREE (fndecl) = alloc_stmt_list ();
17093 
17094       while (current_binding_level->kind != sk_function_parms)
17095 	{
17096 	  if (current_binding_level->kind == sk_class)
17097 	    pop_nested_class ();
17098 	  else
17099 	    poplevel (0, 0, 0);
17100 	}
17101     }
17102   poplevel (1, 0, 1);
17103 
17104   /* Statements should always be full-expressions at the outermost set
17105      of curly braces for a function.  */
17106   gcc_assert (stmts_are_full_exprs_p ());
17107 
17108   /* If there are no return statements in a function with auto return type,
17109      the return type is void.  But if the declared type is something like
17110      auto*, this is an error.  */
17111   if (!processing_template_decl && FNDECL_USED_AUTO (fndecl)
17112       && TREE_TYPE (fntype) == DECL_SAVED_AUTO_RETURN_TYPE (fndecl))
17113     {
17114       if (is_auto (DECL_SAVED_AUTO_RETURN_TYPE (fndecl))
17115           && !current_function_returns_value
17116           && !current_function_returns_null)
17117 	{
17118 	  /* We haven't applied return type deduction because we haven't
17119              seen any return statements. Do that now.  */
17120 	  tree node = type_uses_auto (DECL_SAVED_AUTO_RETURN_TYPE (fndecl));
17121 	  do_auto_deduction (DECL_SAVED_AUTO_RETURN_TYPE (fndecl),
17122 			     void_node, node, tf_warning_or_error,
17123                              adc_return_type);
17124 
17125 	  apply_deduced_return_type (fndecl, void_type_node);
17126 	  fntype = TREE_TYPE (fndecl);
17127 	}
17128       else if (!current_function_returns_value
17129 	       && !current_function_returns_null)
17130 	{
17131 	  error ("no return statements in function returning %qT",
17132 		 DECL_SAVED_AUTO_RETURN_TYPE (fndecl));
17133 	  inform (input_location, "only plain %<auto%> return type can be "
17134 		  "deduced to %<void%>");
17135 	}
17136     }
17137 
17138   /* Remember that we were in class scope.  */
17139   if (current_class_name)
17140     ctype = current_class_type;
17141 
17142   if (DECL_DELETED_FN (fndecl))
17143     {
17144       DECL_INITIAL (fndecl) = error_mark_node;
17145       DECL_SAVED_TREE (fndecl) = NULL_TREE;
17146       goto cleanup;
17147     }
17148 
17149   // If this is a concept, check that the definition is reasonable.
17150   if (DECL_DECLARED_CONCEPT_P (fndecl))
17151     check_function_concept (fndecl);
17152 
17153   if (flag_openmp)
17154     if (tree attr = lookup_attribute ("omp declare variant base",
17155 				      DECL_ATTRIBUTES (fndecl)))
17156       omp_declare_variant_finalize (fndecl, attr);
17157 
17158   /* Lambda closure members are implicitly constexpr if possible.  */
17159   if (cxx_dialect >= cxx17
17160       && LAMBDA_TYPE_P (CP_DECL_CONTEXT (fndecl)))
17161     DECL_DECLARED_CONSTEXPR_P (fndecl)
17162       = ((processing_template_decl
17163 	  || is_valid_constexpr_fn (fndecl, /*complain*/false))
17164 	 && potential_constant_expression (DECL_SAVED_TREE (fndecl)));
17165 
17166   /* Save constexpr function body before it gets munged by
17167      the NRV transformation.   */
17168   maybe_save_function_definition (fndecl);
17169 
17170   /* Invoke the pre-genericize plugin before we start munging things.  */
17171   if (!processing_template_decl)
17172     invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE, fndecl);
17173 
17174   /* Perform delayed folding before NRV transformation.  */
17175   if (!processing_template_decl
17176       && !DECL_IMMEDIATE_FUNCTION_P (fndecl)
17177       && !DECL_OMP_DECLARE_REDUCTION_P (fndecl))
17178     cp_fold_function (fndecl);
17179 
17180   /* Set up the named return value optimization, if we can.  Candidate
17181      variables are selected in check_return_expr.  */
17182   if (current_function_return_value)
17183     {
17184       tree r = current_function_return_value;
17185       tree outer;
17186 
17187       if (r != error_mark_node
17188 	  /* This is only worth doing for fns that return in memory--and
17189 	     simpler, since we don't have to worry about promoted modes.  */
17190 	  && aggregate_value_p (TREE_TYPE (TREE_TYPE (fndecl)), fndecl)
17191 	  /* Only allow this for variables declared in the outer scope of
17192 	     the function so we know that their lifetime always ends with a
17193 	     return; see g++.dg/opt/nrv6.C.  We could be more flexible if
17194 	     we were to do this optimization in tree-ssa.  */
17195 	  && (outer = outer_curly_brace_block (fndecl))
17196 	  && chain_member (r, BLOCK_VARS (outer)))
17197 	finalize_nrv (&DECL_SAVED_TREE (fndecl), r, DECL_RESULT (fndecl));
17198 
17199       current_function_return_value = NULL_TREE;
17200     }
17201 
17202   /* Must mark the RESULT_DECL as being in this function.  */
17203   DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
17204 
17205   /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point
17206      to the FUNCTION_DECL node itself.  */
17207   BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
17208 
17209   /* Complain if there's just no return statement.  */
17210   if (warn_return_type
17211       && !VOID_TYPE_P (TREE_TYPE (fntype))
17212       && !dependent_type_p (TREE_TYPE (fntype))
17213       && !current_function_returns_value && !current_function_returns_null
17214       /* Don't complain if we abort or throw.  */
17215       && !current_function_returns_abnormally
17216       /* Don't complain if there's an infinite loop.  */
17217       && !current_function_infinite_loop
17218       /* Don't complain if we are declared noreturn.  */
17219       && !TREE_THIS_VOLATILE (fndecl)
17220       && !DECL_NAME (DECL_RESULT (fndecl))
17221       && !TREE_NO_WARNING (fndecl)
17222       /* Structor return values (if any) are set by the compiler.  */
17223       && !DECL_CONSTRUCTOR_P (fndecl)
17224       && !DECL_DESTRUCTOR_P (fndecl)
17225       && targetm.warn_func_return (fndecl))
17226     {
17227       gcc_rich_location richloc (input_location);
17228       /* Potentially add a "return *this;" fix-it hint for
17229 	 assignment operators.  */
17230       if (IDENTIFIER_ASSIGN_OP_P (DECL_NAME (fndecl)))
17231 	{
17232 	  tree valtype = TREE_TYPE (DECL_RESULT (fndecl));
17233 	  if (TREE_CODE (valtype) == REFERENCE_TYPE
17234 	      && current_class_ref
17235 	      && same_type_ignoring_top_level_qualifiers_p
17236 		  (TREE_TYPE (valtype), TREE_TYPE (current_class_ref))
17237 	      && global_dc->option_enabled (OPT_Wreturn_type,
17238 					    global_dc->lang_mask,
17239 					    global_dc->option_state))
17240 	    add_return_star_this_fixit (&richloc, fndecl);
17241 	}
17242       if (warning_at (&richloc, OPT_Wreturn_type,
17243 	  "no return statement in function returning non-void"))
17244 	TREE_NO_WARNING (fndecl) = 1;
17245     }
17246 
17247   /* Store the end of the function, so that we get good line number
17248      info for the epilogue.  */
17249   cfun->function_end_locus = input_location;
17250 
17251   /* Complain about parameters that are only set, but never otherwise used.  */
17252   if (warn_unused_but_set_parameter
17253       && !processing_template_decl
17254       && errorcount == unused_but_set_errorcount
17255       && !DECL_CLONED_FUNCTION_P (fndecl))
17256     {
17257       tree decl;
17258 
17259       for (decl = DECL_ARGUMENTS (fndecl);
17260 	   decl;
17261 	   decl = DECL_CHAIN (decl))
17262 	if (TREE_USED (decl)
17263 	    && TREE_CODE (decl) == PARM_DECL
17264 	    && !DECL_READ_P (decl)
17265 	    && DECL_NAME (decl)
17266 	    && !DECL_ARTIFICIAL (decl)
17267 	    && !TREE_NO_WARNING (decl)
17268 	    && !DECL_IN_SYSTEM_HEADER (decl)
17269 	    && TREE_TYPE (decl) != error_mark_node
17270 	    && !TYPE_REF_P (TREE_TYPE (decl))
17271 	    && (!CLASS_TYPE_P (TREE_TYPE (decl))
17272 	        || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl))))
17273 	  warning_at (DECL_SOURCE_LOCATION (decl),
17274 		      OPT_Wunused_but_set_parameter,
17275 		      "parameter %qD set but not used", decl);
17276       unused_but_set_errorcount = errorcount;
17277     }
17278 
17279   /* Complain about locally defined typedefs that are not used in this
17280      function.  */
17281   maybe_warn_unused_local_typedefs ();
17282 
17283   /* Possibly warn about unused parameters.  */
17284   if (warn_unused_parameter
17285       && !processing_template_decl
17286       && !DECL_CLONED_FUNCTION_P (fndecl))
17287     do_warn_unused_parameter (fndecl);
17288 
17289   /* Genericize before inlining.  */
17290   if (!processing_template_decl
17291       && !DECL_IMMEDIATE_FUNCTION_P (fndecl)
17292       && !DECL_OMP_DECLARE_REDUCTION_P (fndecl))
17293     cp_genericize (fndecl);
17294 
17295   /* Emit the resumer and destroyer functions now, providing that we have
17296      not encountered some fatal error.  */
17297   if (coro_emit_helpers)
17298     {
17299       emit_coro_helper (resumer);
17300       emit_coro_helper (destroyer);
17301     }
17302 
17303  cleanup:
17304   /* We're leaving the context of this function, so zap cfun.  It's still in
17305      DECL_STRUCT_FUNCTION, and we'll restore it in tree_rest_of_compilation.  */
17306   set_cfun (NULL);
17307   current_function_decl = NULL;
17308 
17309   /* If this is an in-class inline definition, we may have to pop the
17310      bindings for the template parameters that we added in
17311      maybe_begin_member_template_processing when start_function was
17312      called.  */
17313   if (inline_p)
17314     maybe_end_member_template_processing ();
17315 
17316   /* Leave the scope of the class.  */
17317   if (ctype)
17318     pop_nested_class ();
17319 
17320   --function_depth;
17321 
17322   /* Clean up.  */
17323   current_function_decl = NULL_TREE;
17324 
17325   invoke_plugin_callbacks (PLUGIN_FINISH_PARSE_FUNCTION, fndecl);
17326   return fndecl;
17327 }
17328 
17329 /* Create the FUNCTION_DECL for a function definition.
17330    DECLSPECS and DECLARATOR are the parts of the declaration;
17331    they describe the return type and the name of the function,
17332    but twisted together in a fashion that parallels the syntax of C.
17333 
17334    This function creates a binding context for the function body
17335    as well as setting up the FUNCTION_DECL in current_function_decl.
17336 
17337    Returns a FUNCTION_DECL on success.
17338 
17339    If the DECLARATOR is not suitable for a function (it defines a datum
17340    instead), we return 0, which tells yyparse to report a parse error.
17341 
17342    May return void_type_node indicating that this method is actually
17343    a friend.  See grokfield for more details.
17344 
17345    Came here with a `.pushlevel' .
17346 
17347    DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
17348    CHANGES TO CODE IN `grokfield'.  */
17349 
17350 tree
grokmethod(cp_decl_specifier_seq * declspecs,const cp_declarator * declarator,tree attrlist)17351 grokmethod (cp_decl_specifier_seq *declspecs,
17352 	    const cp_declarator *declarator, tree attrlist)
17353 {
17354   tree fndecl = grokdeclarator (declarator, declspecs, MEMFUNCDEF, 0,
17355 				&attrlist);
17356 
17357   if (fndecl == error_mark_node)
17358     return error_mark_node;
17359 
17360   if (attrlist)
17361     cplus_decl_attributes (&fndecl, attrlist, 0);
17362 
17363   /* Pass friends other than inline friend functions back.  */
17364   if (fndecl == void_type_node)
17365     return fndecl;
17366 
17367   if (DECL_IN_AGGR_P (fndecl))
17368     {
17369       if (DECL_CLASS_SCOPE_P (fndecl))
17370 	error ("%qD is already defined in class %qT", fndecl,
17371 	       DECL_CONTEXT (fndecl));
17372       return error_mark_node;
17373     }
17374 
17375   check_template_shadow (fndecl);
17376 
17377   if (TREE_PUBLIC (fndecl))
17378     DECL_COMDAT (fndecl) = 1;
17379   DECL_DECLARED_INLINE_P (fndecl) = 1;
17380   DECL_NO_INLINE_WARNING_P (fndecl) = 1;
17381 
17382   /* We process method specializations in finish_struct_1.  */
17383   if (processing_template_decl && !DECL_TEMPLATE_SPECIALIZATION (fndecl))
17384     {
17385       fndecl = push_template_decl (fndecl);
17386       if (fndecl == error_mark_node)
17387 	return fndecl;
17388     }
17389 
17390   if (! DECL_FRIEND_P (fndecl))
17391     {
17392       if (DECL_CHAIN (fndecl))
17393 	{
17394 	  fndecl = copy_node (fndecl);
17395 	  TREE_CHAIN (fndecl) = NULL_TREE;
17396 	}
17397     }
17398 
17399   cp_finish_decl (fndecl, NULL_TREE, false, NULL_TREE, 0);
17400 
17401   DECL_IN_AGGR_P (fndecl) = 1;
17402   return fndecl;
17403 }
17404 
17405 
17406 /* VAR is a VAR_DECL.  If its type is incomplete, remember VAR so that
17407    we can lay it out later, when and if its type becomes complete.
17408 
17409    Also handle constexpr variables where the initializer involves
17410    an unlowered PTRMEM_CST because the class isn't complete yet.  */
17411 
17412 void
maybe_register_incomplete_var(tree var)17413 maybe_register_incomplete_var (tree var)
17414 {
17415   gcc_assert (VAR_P (var));
17416 
17417   /* Keep track of variables with incomplete types.  */
17418   if (!processing_template_decl && TREE_TYPE (var) != error_mark_node
17419       && DECL_EXTERNAL (var))
17420     {
17421       tree inner_type = TREE_TYPE (var);
17422 
17423       while (TREE_CODE (inner_type) == ARRAY_TYPE)
17424 	inner_type = TREE_TYPE (inner_type);
17425       inner_type = TYPE_MAIN_VARIANT (inner_type);
17426 
17427       if ((!COMPLETE_TYPE_P (inner_type) && CLASS_TYPE_P (inner_type))
17428 	  /* RTTI TD entries are created while defining the type_info.  */
17429 	  || (TYPE_LANG_SPECIFIC (inner_type)
17430 	      && TYPE_BEING_DEFINED (inner_type)))
17431 	{
17432 	  incomplete_var iv = {var, inner_type};
17433 	  vec_safe_push (incomplete_vars, iv);
17434 	}
17435       else if (!(DECL_LANG_SPECIFIC (var) && DECL_TEMPLATE_INFO (var))
17436 	       && decl_constant_var_p (var)
17437 	       && (TYPE_PTRMEM_P (inner_type) || CLASS_TYPE_P (inner_type)))
17438 	{
17439 	  /* When the outermost open class is complete we can resolve any
17440 	     pointers-to-members.  */
17441 	  tree context = outermost_open_class ();
17442 	  incomplete_var iv = {var, context};
17443 	  vec_safe_push (incomplete_vars, iv);
17444 	}
17445     }
17446 }
17447 
17448 /* Called when a class type (given by TYPE) is defined.  If there are
17449    any existing VAR_DECLs whose type has been completed by this
17450    declaration, update them now.  */
17451 
17452 void
complete_vars(tree type)17453 complete_vars (tree type)
17454 {
17455   unsigned ix;
17456   incomplete_var *iv;
17457 
17458   for (ix = 0; vec_safe_iterate (incomplete_vars, ix, &iv); )
17459     {
17460       if (same_type_p (type, iv->incomplete_type))
17461 	{
17462 	  tree var = iv->decl;
17463 	  tree type = TREE_TYPE (var);
17464 
17465 	  if (type != error_mark_node
17466 	      && (TYPE_MAIN_VARIANT (strip_array_types (type))
17467 		  == iv->incomplete_type))
17468 	    {
17469 	      /* Complete the type of the variable.  The VAR_DECL itself
17470 		 will be laid out in expand_expr.  */
17471 	      complete_type (type);
17472 	      cp_apply_type_quals_to_decl (cp_type_quals (type), var);
17473 	    }
17474 
17475 	  /* Remove this entry from the list.  */
17476 	  incomplete_vars->unordered_remove (ix);
17477 	}
17478       else
17479 	ix++;
17480     }
17481 
17482   /* Check for pending declarations which may have abstract type.  */
17483   complete_type_check_abstract (type);
17484 }
17485 
17486 /* If DECL is of a type which needs a cleanup, build and return an
17487    expression to perform that cleanup here.  Return NULL_TREE if no
17488    cleanup need be done.  DECL can also be a _REF when called from
17489    split_nonconstant_init_1.  */
17490 
17491 tree
cxx_maybe_build_cleanup(tree decl,tsubst_flags_t complain)17492 cxx_maybe_build_cleanup (tree decl, tsubst_flags_t complain)
17493 {
17494   tree type;
17495   tree attr;
17496   tree cleanup;
17497 
17498   /* Assume no cleanup is required.  */
17499   cleanup = NULL_TREE;
17500 
17501   if (error_operand_p (decl))
17502     return cleanup;
17503 
17504   /* Handle "__attribute__((cleanup))".  We run the cleanup function
17505      before the destructor since the destructor is what actually
17506      terminates the lifetime of the object.  */
17507   if (DECL_P (decl))
17508     attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
17509   else
17510     attr = NULL_TREE;
17511   if (attr)
17512     {
17513       tree id;
17514       tree fn;
17515       tree arg;
17516 
17517       /* Get the name specified by the user for the cleanup function.  */
17518       id = TREE_VALUE (TREE_VALUE (attr));
17519       /* Look up the name to find the cleanup function to call.  It is
17520 	 important to use lookup_name here because that is what is
17521 	 used in c-common.c:handle_cleanup_attribute when performing
17522 	 initial checks on the attribute.  Note that those checks
17523 	 include ensuring that the function found is not an overloaded
17524 	 function, or an object with an overloaded call operator,
17525 	 etc.; we can rely on the fact that the function found is an
17526 	 ordinary FUNCTION_DECL.  */
17527       fn = lookup_name (id);
17528       arg = build_address (decl);
17529       if (!mark_used (decl, complain) && !(complain & tf_error))
17530 	return error_mark_node;
17531       cleanup = cp_build_function_call_nary (fn, complain, arg, NULL_TREE);
17532       if (cleanup == error_mark_node)
17533 	return error_mark_node;
17534     }
17535   /* Handle ordinary C++ destructors.  */
17536   type = TREE_TYPE (decl);
17537   if (type_build_dtor_call (type))
17538     {
17539       int flags = LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR;
17540       tree addr;
17541       tree call;
17542 
17543       if (TREE_CODE (type) == ARRAY_TYPE)
17544 	addr = decl;
17545       else
17546 	addr = build_address (decl);
17547 
17548       call = build_delete (input_location, TREE_TYPE (addr), addr,
17549 			   sfk_complete_destructor, flags, 0, complain);
17550       if (call == error_mark_node)
17551 	cleanup = error_mark_node;
17552       else if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
17553 	/* Discard the call.  */;
17554       else if (decl_maybe_constant_destruction (decl, type)
17555 	       && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
17556 	cxx_constant_dtor (call, decl);
17557       else if (cleanup)
17558 	cleanup = cp_build_compound_expr (cleanup, call, complain);
17559       else
17560 	cleanup = call;
17561     }
17562 
17563   /* build_delete sets the location of the destructor call to the
17564      current location, even though the destructor is going to be
17565      called later, at the end of the current scope.  This can lead to
17566      a "jumpy" behavior for users of debuggers when they step around
17567      the end of the block.  So let's unset the location of the
17568      destructor call instead.  */
17569   protected_set_expr_location (cleanup, UNKNOWN_LOCATION);
17570   if (cleanup && CONVERT_EXPR_P (cleanup))
17571     protected_set_expr_location (TREE_OPERAND (cleanup, 0), UNKNOWN_LOCATION);
17572 
17573   if (cleanup
17574       && DECL_P (decl)
17575       && !lookup_attribute ("warn_unused", TYPE_ATTRIBUTES (TREE_TYPE (decl)))
17576       /* Treat objects with destructors as used; the destructor may do
17577 	 something substantive.  */
17578       && !mark_used (decl, complain) && !(complain & tf_error))
17579     return error_mark_node;
17580 
17581   if (cleanup && cfun && !processing_template_decl
17582       && !expr_noexcept_p (cleanup, tf_none))
17583     cp_function_chain->throwing_cleanup = true;
17584 
17585   return cleanup;
17586 }
17587 
17588 
17589 /* Return the FUNCTION_TYPE that corresponds to MEMFNTYPE, which can be a
17590    FUNCTION_DECL, METHOD_TYPE, FUNCTION_TYPE, pointer or reference to
17591    METHOD_TYPE or FUNCTION_TYPE, or pointer to member function.  */
17592 
17593 tree
static_fn_type(tree memfntype)17594 static_fn_type (tree memfntype)
17595 {
17596   tree fntype;
17597   tree args;
17598 
17599   if (TYPE_PTRMEMFUNC_P (memfntype))
17600     memfntype = TYPE_PTRMEMFUNC_FN_TYPE (memfntype);
17601   if (INDIRECT_TYPE_P (memfntype)
17602       || TREE_CODE (memfntype) == FUNCTION_DECL)
17603     memfntype = TREE_TYPE (memfntype);
17604   if (TREE_CODE (memfntype) == FUNCTION_TYPE)
17605     return memfntype;
17606   gcc_assert (TREE_CODE (memfntype) == METHOD_TYPE);
17607   args = TYPE_ARG_TYPES (memfntype);
17608   fntype = build_function_type (TREE_TYPE (memfntype), TREE_CHAIN (args));
17609   fntype = apply_memfn_quals (fntype, type_memfn_quals (memfntype));
17610   fntype = (cp_build_type_attribute_variant
17611 	    (fntype, TYPE_ATTRIBUTES (memfntype)));
17612   fntype = cxx_copy_lang_qualifiers (fntype, memfntype);
17613   return fntype;
17614 }
17615 
17616 /* DECL was originally constructed as a non-static member function,
17617    but turned out to be static.  Update it accordingly.  */
17618 
17619 void
revert_static_member_fn(tree decl)17620 revert_static_member_fn (tree decl)
17621 {
17622   tree stype = static_fn_type (decl);
17623   cp_cv_quals quals = type_memfn_quals (stype);
17624   cp_ref_qualifier rqual = type_memfn_rqual (stype);
17625 
17626   if (quals != TYPE_UNQUALIFIED || rqual != REF_QUAL_NONE)
17627     stype = apply_memfn_quals (stype, TYPE_UNQUALIFIED, REF_QUAL_NONE);
17628 
17629   TREE_TYPE (decl) = stype;
17630 
17631   if (DECL_ARGUMENTS (decl))
17632     DECL_ARGUMENTS (decl) = DECL_CHAIN (DECL_ARGUMENTS (decl));
17633   DECL_STATIC_FUNCTION_P (decl) = 1;
17634 }
17635 
17636 /* Return which tree structure is used by T, or TS_CP_GENERIC if T is
17637    one of the language-independent trees.  */
17638 
17639 enum cp_tree_node_structure_enum
cp_tree_node_structure(union lang_tree_node * t)17640 cp_tree_node_structure (union lang_tree_node * t)
17641 {
17642   switch (TREE_CODE (&t->generic))
17643     {
17644     case ARGUMENT_PACK_SELECT:  return TS_CP_ARGUMENT_PACK_SELECT;
17645     case BASELINK:		return TS_CP_BASELINK;
17646     case CONSTRAINT_INFO:       return TS_CP_CONSTRAINT_INFO;
17647     case DEFERRED_NOEXCEPT:	return TS_CP_DEFERRED_NOEXCEPT;
17648     case DEFERRED_PARSE:	return TS_CP_DEFERRED_PARSE;
17649     case IDENTIFIER_NODE:	return TS_CP_IDENTIFIER;
17650     case LAMBDA_EXPR:		return TS_CP_LAMBDA_EXPR;
17651     case OVERLOAD:		return TS_CP_OVERLOAD;
17652     case PTRMEM_CST:		return TS_CP_PTRMEM;
17653     case STATIC_ASSERT:		return TS_CP_STATIC_ASSERT;
17654     case TEMPLATE_DECL:		return TS_CP_TEMPLATE_DECL;
17655     case TEMPLATE_INFO:		return TS_CP_TEMPLATE_INFO;
17656     case TEMPLATE_PARM_INDEX:	return TS_CP_TPI;
17657     case TRAIT_EXPR:		return TS_CP_TRAIT_EXPR;
17658     case USERDEF_LITERAL:	return TS_CP_USERDEF_LITERAL;
17659     default:			return TS_CP_GENERIC;
17660     }
17661 }
17662 
17663 /* Build the void_list_node (void_type_node having been created).  */
17664 tree
build_void_list_node(void)17665 build_void_list_node (void)
17666 {
17667   tree t = build_tree_list (NULL_TREE, void_type_node);
17668   return t;
17669 }
17670 
17671 bool
cp_missing_noreturn_ok_p(tree decl)17672 cp_missing_noreturn_ok_p (tree decl)
17673 {
17674   /* A missing noreturn is ok for the `main' function.  */
17675   return DECL_MAIN_P (decl);
17676 }
17677 
17678 /* Return the decl used to identify the COMDAT group into which DECL should
17679    be placed.  */
17680 
17681 tree
cxx_comdat_group(tree decl)17682 cxx_comdat_group (tree decl)
17683 {
17684   /* Virtual tables, construction virtual tables, and virtual table
17685      tables all go in a single COMDAT group, named after the primary
17686      virtual table.  */
17687   if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl))
17688     decl = CLASSTYPE_VTABLES (DECL_CONTEXT (decl));
17689   /* For all other DECLs, the COMDAT group is the mangled name of the
17690      declaration itself.  */
17691   else
17692     {
17693       while (DECL_THUNK_P (decl))
17694 	{
17695 	  /* If TARGET_USE_LOCAL_THUNK_ALIAS_P, use_thunk puts the thunk
17696 	     into the same section as the target function.  In that case
17697 	     we must return target's name.  */
17698 	  tree target = THUNK_TARGET (decl);
17699 	  if (TARGET_USE_LOCAL_THUNK_ALIAS_P (target)
17700 	      && DECL_SECTION_NAME (target) != NULL
17701 	      && DECL_ONE_ONLY (target))
17702 	    decl = target;
17703 	  else
17704 	    break;
17705 	}
17706     }
17707 
17708   return decl;
17709 }
17710 
17711 /* Returns the return type for FN as written by the user, which may include
17712    a placeholder for a deduced return type.  */
17713 
17714 tree
fndecl_declared_return_type(tree fn)17715 fndecl_declared_return_type (tree fn)
17716 {
17717   fn = STRIP_TEMPLATE (fn);
17718   if (FNDECL_USED_AUTO (fn))
17719     return DECL_SAVED_AUTO_RETURN_TYPE (fn);
17720 
17721   return TREE_TYPE (TREE_TYPE (fn));
17722 }
17723 
17724 /* Returns true iff DECL is a variable or function declared with an auto type
17725    that has not yet been deduced to a real type.  */
17726 
17727 bool
undeduced_auto_decl(tree decl)17728 undeduced_auto_decl (tree decl)
17729 {
17730   if (cxx_dialect < cxx11)
17731     return false;
17732   STRIP_ANY_LOCATION_WRAPPER (decl);
17733   return ((VAR_OR_FUNCTION_DECL_P (decl)
17734 	   || TREE_CODE (decl) == TEMPLATE_DECL)
17735 	  && type_uses_auto (TREE_TYPE (decl)));
17736 }
17737 
17738 /* Complain if DECL has an undeduced return type.  */
17739 
17740 bool
require_deduced_type(tree decl,tsubst_flags_t complain)17741 require_deduced_type (tree decl, tsubst_flags_t complain)
17742 {
17743   if (undeduced_auto_decl (decl))
17744     {
17745       if (TREE_NO_WARNING (decl) && seen_error ())
17746 	/* We probably already complained about deduction failure.  */;
17747       else if (complain & tf_error)
17748 	error ("use of %qD before deduction of %<auto%>", decl);
17749       return false;
17750     }
17751   return true;
17752 }
17753 
17754 /* Create a representation of the explicit-specifier with
17755    constant-expression of EXPR.  COMPLAIN is as for tsubst.  */
17756 
17757 tree
build_explicit_specifier(tree expr,tsubst_flags_t complain)17758 build_explicit_specifier (tree expr, tsubst_flags_t complain)
17759 {
17760   if (instantiation_dependent_expression_p (expr))
17761     /* Wait for instantiation, tsubst_function_decl will handle it.  */
17762     return expr;
17763 
17764   expr = build_converted_constant_bool_expr (expr, complain);
17765   expr = instantiate_non_dependent_expr_sfinae (expr, complain);
17766   expr = cxx_constant_value (expr);
17767   return expr;
17768 }
17769 
17770 #include "gt-cp-decl.h"
17771