1 /* Process declarations and variables for C++ compiler.
2    Copyright (C) 1988-2018 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 
55 /* Possible cases of bad specifiers type used by bad_specifiers. */
56 enum bad_spec_place {
57   BSP_VAR,    /* variable */
58   BSP_PARM,   /* parameter */
59   BSP_TYPE,   /* type */
60   BSP_FIELD   /* field */
61 };
62 
63 static const char *redeclaration_error_message (tree, tree);
64 
65 static int decl_jump_unsafe (tree);
66 static void require_complete_types_for_parms (tree);
67 static void push_local_name (tree);
68 static tree grok_reference_init (tree, tree, tree, int);
69 static tree grokvardecl (tree, tree, tree, const cp_decl_specifier_seq *,
70 			 int, int, int, bool, int, tree);
71 static int check_static_variable_definition (tree, tree);
72 static void record_unknown_type (tree, const char *);
73 static tree builtin_function_1 (tree, tree, bool);
74 static int member_function_or_else (tree, tree, enum overload_flags);
75 static tree local_variable_p_walkfn (tree *, int *, void *);
76 static const char *tag_name (enum tag_types);
77 static tree lookup_and_check_tag (enum tag_types, tree, tag_scope, bool);
78 static void maybe_deduce_size_from_array_init (tree, tree);
79 static void layout_var_decl (tree);
80 static tree check_initializer (tree, tree, int, vec<tree, va_gc> **);
81 static void make_rtl_for_nonlocal_decl (tree, tree, const char *);
82 static void save_function_data (tree);
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);
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 
104 /* The following symbols are subsumed in the cp_global_trees array, and
105    listed here individually for documentation purposes.
106 
107    C++ extensions
108 	tree wchar_decl_node;
109 
110 	tree vtable_entry_type;
111 	tree delta_type_node;
112 	tree __t_desc_type_node;
113 
114 	tree class_type_node;
115 	tree unknown_type_node;
116 
117    Array type `vtable_entry_type[]'
118 
119 	tree vtbl_type_node;
120 	tree vtbl_ptr_type_node;
121 
122    Namespaces,
123 
124 	tree std_node;
125 	tree abi_node;
126 
127    A FUNCTION_DECL which can call `abort'.  Not necessarily the
128    one that the user will declare, but sufficient to be called
129    by routines that want to abort the program.
130 
131 	tree abort_fndecl;
132 
133    Used by RTTI
134 	tree type_info_type_node, tinfo_decl_id, tinfo_decl_type;
135 	tree tinfo_var_id;  */
136 
137 tree cp_global_trees[CPTI_MAX];
138 
139 #define local_names cp_function_chain->x_local_names
140 
141 /* A list of objects which have constructors or destructors
142    which reside in the global scope.  The decl is stored in
143    the TREE_VALUE slot and the initializer is stored
144    in the TREE_PURPOSE slot.  */
145 tree static_aggregates;
146 
147 /* Like static_aggregates, but for thread_local variables.  */
148 tree tls_aggregates;
149 
150 /* -- end of C++ */
151 
152 /* A node for the integer constant 2.  */
153 
154 tree integer_two_node;
155 
156 /* vector of static decls.  */
157 vec<tree, va_gc> *static_decls;
158 
159 /* vector of keyed classes.  */
160 vec<tree, va_gc> *keyed_classes;
161 
162 /* Used only for jumps to as-yet undefined labels, since jumps to
163    defined labels can have their validity checked immediately.  */
164 
165 struct GTY((chain_next ("%h.next"))) named_label_use_entry {
166   struct named_label_use_entry *next;
167   /* The binding level to which this entry is *currently* attached.
168      This is initially the binding level in which the goto appeared,
169      but is modified as scopes are closed.  */
170   cp_binding_level *binding_level;
171   /* The head of the names list that was current when the goto appeared,
172      or the inner scope popped.  These are the decls that will *not* be
173      skipped when jumping to the label.  */
174   tree names_in_scope;
175   /* The location of the goto, for error reporting.  */
176   location_t o_goto_locus;
177   /* True if an OpenMP structured block scope has been closed since
178      the goto appeared.  This means that the branch from the label will
179      illegally exit an OpenMP scope.  */
180   bool in_omp_scope;
181 };
182 
183 /* A list of all LABEL_DECLs in the function that have names.  Here so
184    we can clear out their names' definitions at the end of the
185    function, and so we can check the validity of jumps to these labels.  */
186 
187 struct GTY((for_user)) named_label_entry {
188 
189   tree name;  /* Name of decl. */
190 
191   tree label_decl; /* LABEL_DECL, unless deleted local label. */
192 
193   named_label_entry *outer; /* Outer shadowed chain.  */
194 
195   /* The binding level to which the label is *currently* attached.
196      This is initially set to the binding level in which the label
197      is defined, but is modified as scopes are closed.  */
198   cp_binding_level *binding_level;
199 
200   /* The head of the names list that was current when the label was
201      defined, or the inner scope popped.  These are the decls that will
202      be skipped when jumping to the label.  */
203   tree names_in_scope;
204 
205   /* A vector of all decls from all binding levels that would be
206      crossed by a backward branch to the label.  */
207   vec<tree, va_gc> *bad_decls;
208 
209   /* A list of uses of the label, before the label is defined.  */
210   named_label_use_entry *uses;
211 
212   /* The following bits are set after the label is defined, and are
213      updated as scopes are popped.  They indicate that a jump to the
214      label will illegally enter a scope of the given flavor.  */
215   bool in_try_scope;
216   bool in_catch_scope;
217   bool in_omp_scope;
218   bool in_transaction_scope;
219   bool in_constexpr_if;
220 };
221 
222 #define named_labels cp_function_chain->x_named_labels
223 
224 /* The number of function bodies which we are currently processing.
225    (Zero if we are at namespace scope, one inside the body of a
226    function, two inside the body of a function in a local class, etc.)  */
227 int function_depth;
228 
229 /* Whether the exception-specifier is part of a function type (i.e. C++17).  */
230 bool flag_noexcept_type;
231 
232 /* States indicating how grokdeclarator() should handle declspecs marked
233    with __attribute__((deprecated)).  An object declared as
234    __attribute__((deprecated)) suppresses warnings of uses of other
235    deprecated items.  */
236 enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
237 
238 
239 /* A list of VAR_DECLs whose type was incomplete at the time the
240    variable was declared.  */
241 
242 struct GTY(()) incomplete_var {
243   tree decl;
244   tree incomplete_type;
245 };
246 
247 
248 static GTY(()) vec<incomplete_var, va_gc> *incomplete_vars;
249 
250 /* Returns the kind of template specialization we are currently
251    processing, given that it's declaration contained N_CLASS_SCOPES
252    explicit scope qualifications.  */
253 
254 tmpl_spec_kind
current_tmpl_spec_kind(int n_class_scopes)255 current_tmpl_spec_kind (int n_class_scopes)
256 {
257   int n_template_parm_scopes = 0;
258   int seen_specialization_p = 0;
259   int innermost_specialization_p = 0;
260   cp_binding_level *b;
261 
262   /* Scan through the template parameter scopes.  */
263   for (b = current_binding_level;
264        b->kind == sk_template_parms;
265        b = b->level_chain)
266     {
267       /* If we see a specialization scope inside a parameter scope,
268 	 then something is wrong.  That corresponds to a declaration
269 	 like:
270 
271 	    template <class T> template <> ...
272 
273 	 which is always invalid since [temp.expl.spec] forbids the
274 	 specialization of a class member template if the enclosing
275 	 class templates are not explicitly specialized as well.  */
276       if (b->explicit_spec_p)
277 	{
278 	  if (n_template_parm_scopes == 0)
279 	    innermost_specialization_p = 1;
280 	  else
281 	    seen_specialization_p = 1;
282 	}
283       else if (seen_specialization_p == 1)
284 	return tsk_invalid_member_spec;
285 
286       ++n_template_parm_scopes;
287     }
288 
289   /* Handle explicit instantiations.  */
290   if (processing_explicit_instantiation)
291     {
292       if (n_template_parm_scopes != 0)
293 	/* We've seen a template parameter list during an explicit
294 	   instantiation.  For example:
295 
296 	     template <class T> template void f(int);
297 
298 	   This is erroneous.  */
299 	return tsk_invalid_expl_inst;
300       else
301 	return tsk_expl_inst;
302     }
303 
304   if (n_template_parm_scopes < n_class_scopes)
305     /* We've not seen enough template headers to match all the
306        specialized classes present.  For example:
307 
308 	 template <class T> void R<T>::S<T>::f(int);
309 
310        This is invalid; there needs to be one set of template
311        parameters for each class.  */
312     return tsk_insufficient_parms;
313   else if (n_template_parm_scopes == n_class_scopes)
314     /* We're processing a non-template declaration (even though it may
315        be a member of a template class.)  For example:
316 
317 	 template <class T> void S<T>::f(int);
318 
319        The `class T' matches the `S<T>', leaving no template headers
320        corresponding to the `f'.  */
321     return tsk_none;
322   else if (n_template_parm_scopes > n_class_scopes + 1)
323     /* We've got too many template headers.  For example:
324 
325 	 template <> template <class T> void f (T);
326 
327        There need to be more enclosing classes.  */
328     return tsk_excessive_parms;
329   else
330     /* This must be a template.  It's of the form:
331 
332 	 template <class T> template <class U> void S<T>::f(U);
333 
334        This is a specialization if the innermost level was a
335        specialization; otherwise it's just a definition of the
336        template.  */
337     return innermost_specialization_p ? tsk_expl_spec : tsk_template;
338 }
339 
340 /* Exit the current scope.  */
341 
342 void
finish_scope(void)343 finish_scope (void)
344 {
345   poplevel (0, 0, 0);
346 }
347 
348 /* When a label goes out of scope, check to see if that label was used
349    in a valid manner, and issue any appropriate warnings or errors.  */
350 
351 static void
check_label_used(tree label)352 check_label_used (tree label)
353 {
354   if (!processing_template_decl)
355     {
356       if (DECL_INITIAL (label) == NULL_TREE)
357 	{
358 	  location_t location;
359 
360 	  error ("label %q+D used but not defined", label);
361 	  location = input_location;
362 	    /* FIXME want (LOCATION_FILE (input_location), (line)0) */
363 	  /* Avoid crashing later.  */
364 	  define_label (location, DECL_NAME (label));
365 	}
366       else
367 	warn_for_unused_label (label);
368     }
369 }
370 
371 /* Helper function to sort named label entries in a vector by DECL_UID.  */
372 
373 static int
sort_labels(const void * a,const void * b)374 sort_labels (const void *a, const void *b)
375 {
376   tree label1 = *(tree const *) a;
377   tree label2 = *(tree const *) b;
378 
379   /* DECL_UIDs can never be equal.  */
380   return DECL_UID (label1) > DECL_UID (label2) ? -1 : +1;
381 }
382 
383 /* At the end of a function, all labels declared within the function
384    go out of scope.  BLOCK is the top-level block for the
385    function.  */
386 
387 static void
pop_labels(tree block)388 pop_labels (tree block)
389 {
390   if (!named_labels)
391     return;
392 
393   /* We need to add the labels to the block chain, so debug
394      information is emitted.  But, we want the order to be stable so
395      need to sort them first.  Otherwise the debug output could be
396      randomly ordered.  I guess it's mostly stable, unless the hash
397      table implementation changes.  */
398   auto_vec<tree, 32> labels (named_labels->elements ());
399   hash_table<named_label_hash>::iterator end (named_labels->end ());
400   for (hash_table<named_label_hash>::iterator iter
401 	 (named_labels->begin ()); iter != end; ++iter)
402     {
403       named_label_entry *ent = *iter;
404 
405       gcc_checking_assert (!ent->outer);
406       if (ent->label_decl)
407 	labels.quick_push (ent->label_decl);
408       ggc_free (ent);
409     }
410   named_labels = NULL;
411   labels.qsort (sort_labels);
412 
413   while (labels.length ())
414     {
415       tree label = labels.pop ();
416 
417       DECL_CHAIN (label) = BLOCK_VARS (block);
418       BLOCK_VARS (block) = label;
419 
420       check_label_used (label);
421     }
422 }
423 
424 /* At the end of a block with local labels, restore the outer definition.  */
425 
426 static void
pop_local_label(tree id,tree label)427 pop_local_label (tree id, tree label)
428 {
429   check_label_used (label);
430   named_label_entry **slot = named_labels->find_slot_with_hash
431     (id, IDENTIFIER_HASH_VALUE (id), NO_INSERT);
432   named_label_entry *ent = *slot;
433 
434   if (ent->outer)
435     ent = ent->outer;
436   else
437     {
438       ent = ggc_cleared_alloc<named_label_entry> ();
439       ent->name = id;
440     }
441   *slot = ent;
442 }
443 
444 /* The following two routines are used to interface to Objective-C++.
445    The binding level is purposely treated as an opaque type.  */
446 
447 void *
objc_get_current_scope(void)448 objc_get_current_scope (void)
449 {
450   return current_binding_level;
451 }
452 
453 /* The following routine is used by the NeXT-style SJLJ exceptions;
454    variables get marked 'volatile' so as to not be clobbered by
455    _setjmp()/_longjmp() calls.  All variables in the current scope,
456    as well as parent scopes up to (but not including) ENCLOSING_BLK
457    shall be thusly marked.  */
458 
459 void
objc_mark_locals_volatile(void * enclosing_blk)460 objc_mark_locals_volatile (void *enclosing_blk)
461 {
462   cp_binding_level *scope;
463 
464   for (scope = current_binding_level;
465        scope && scope != enclosing_blk;
466        scope = scope->level_chain)
467     {
468       tree decl;
469 
470       for (decl = scope->names; decl; decl = TREE_CHAIN (decl))
471 	objc_volatilize_decl (decl);
472 
473       /* Do not climb up past the current function.  */
474       if (scope->kind == sk_function_parms)
475 	break;
476     }
477 }
478 
479 /* True if B is the level for the condition of a constexpr if.  */
480 
481 static bool
level_for_constexpr_if(cp_binding_level * b)482 level_for_constexpr_if (cp_binding_level *b)
483 {
484   return (b->kind == sk_cond && b->this_entity
485 	  && TREE_CODE (b->this_entity) == IF_STMT
486 	  && IF_STMT_CONSTEXPR_P (b->this_entity));
487 }
488 
489 /* Update data for defined and undefined labels when leaving a scope.  */
490 
491 int
poplevel_named_label_1(named_label_entry ** slot,cp_binding_level * bl)492 poplevel_named_label_1 (named_label_entry **slot, cp_binding_level *bl)
493 {
494   named_label_entry *ent = *slot;
495   cp_binding_level *obl = bl->level_chain;
496 
497   if (ent->binding_level == bl)
498     {
499       tree decl;
500 
501       /* ENT->NAMES_IN_SCOPE may contain a mixture of DECLs and
502 	 TREE_LISTs representing OVERLOADs, so be careful.  */
503       for (decl = ent->names_in_scope; decl; decl = (DECL_P (decl)
504 						     ? DECL_CHAIN (decl)
505 						     : TREE_CHAIN (decl)))
506 	if (decl_jump_unsafe (decl))
507 	  vec_safe_push (ent->bad_decls, decl);
508 
509       ent->binding_level = obl;
510       ent->names_in_scope = obl->names;
511       switch (bl->kind)
512 	{
513 	case sk_try:
514 	  ent->in_try_scope = true;
515 	  break;
516 	case sk_catch:
517 	  ent->in_catch_scope = true;
518 	  break;
519 	case sk_omp:
520 	  ent->in_omp_scope = true;
521 	  break;
522 	case sk_transaction:
523 	  ent->in_transaction_scope = true;
524 	  break;
525 	case sk_block:
526 	  if (level_for_constexpr_if (bl->level_chain))
527 	    ent->in_constexpr_if = true;
528 	  break;
529 	default:
530 	  break;
531 	}
532     }
533   else if (ent->uses)
534     {
535       struct named_label_use_entry *use;
536 
537       for (use = ent->uses; use ; use = use->next)
538 	if (use->binding_level == bl)
539 	  {
540 	    use->binding_level = obl;
541 	    use->names_in_scope = obl->names;
542 	    if (bl->kind == sk_omp)
543 	      use->in_omp_scope = true;
544 	  }
545     }
546 
547   return 1;
548 }
549 
550 /* Saved errorcount to avoid -Wunused-but-set-{parameter,variable} warnings
551    when errors were reported, except for -Werror-unused-but-set-*.  */
552 static int unused_but_set_errorcount;
553 
554 /* Exit a binding level.
555    Pop the level off, and restore the state of the identifier-decl mappings
556    that were in effect when this level was entered.
557 
558    If KEEP == 1, this level had explicit declarations, so
559    and create a "block" (a BLOCK node) for the level
560    to record its declarations and subblocks for symbol table output.
561 
562    If FUNCTIONBODY is nonzero, this level is the body of a function,
563    so create a block as if KEEP were set and also clear out all
564    label names.
565 
566    If REVERSE is nonzero, reverse the order of decls before putting
567    them into the BLOCK.  */
568 
569 tree
poplevel(int keep,int reverse,int functionbody)570 poplevel (int keep, int reverse, int functionbody)
571 {
572   tree link;
573   /* The chain of decls was accumulated in reverse order.
574      Put it into forward order, just for cleanliness.  */
575   tree decls;
576   tree subblocks;
577   tree block;
578   tree decl;
579   int leaving_for_scope;
580   scope_kind kind;
581   unsigned ix;
582 
583   bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
584  restart:
585 
586   block = NULL_TREE;
587 
588   gcc_assert (current_binding_level->kind != sk_class
589 	      && current_binding_level->kind != sk_namespace);
590 
591   if (current_binding_level->kind == sk_cleanup)
592     functionbody = 0;
593   subblocks = functionbody >= 0 ? current_binding_level->blocks : 0;
594 
595   gcc_assert (!vec_safe_length (current_binding_level->class_shadowed));
596 
597   /* We used to use KEEP == 2 to indicate that the new block should go
598      at the beginning of the list of blocks at this binding level,
599      rather than the end.  This hack is no longer used.  */
600   gcc_assert (keep == 0 || keep == 1);
601 
602   if (current_binding_level->keep)
603     keep = 1;
604 
605   /* Any uses of undefined labels, and any defined labels, now operate
606      under constraints of next binding contour.  */
607   if (cfun && !functionbody && named_labels)
608     named_labels->traverse<cp_binding_level *, poplevel_named_label_1>
609 		   (current_binding_level);
610 
611   /* Get the decls in the order they were written.
612      Usually current_binding_level->names is in reverse order.
613      But parameter decls were previously put in forward order.  */
614 
615   decls = current_binding_level->names;
616   if (reverse)
617     {
618       decls = nreverse (decls);
619       current_binding_level->names = decls;
620     }
621 
622   /* If there were any declarations or structure tags in that level,
623      or if this level is a function body,
624      create a BLOCK to record them for the life of this function.  */
625   block = NULL_TREE;
626   /* Avoid function body block if possible.  */
627   if (functionbody && subblocks && BLOCK_CHAIN (subblocks) == NULL_TREE)
628     keep = 0;
629   else if (keep == 1 || functionbody)
630     block = make_node (BLOCK);
631   if (block != NULL_TREE)
632     {
633       BLOCK_VARS (block) = decls;
634       BLOCK_SUBBLOCKS (block) = subblocks;
635     }
636 
637   /* In each subblock, record that this is its superior.  */
638   if (keep >= 0)
639     for (link = subblocks; link; link = BLOCK_CHAIN (link))
640       BLOCK_SUPERCONTEXT (link) = block;
641 
642   /* We still support the old for-scope rules, whereby the variables
643      in a init statement were in scope after the for-statement ended.
644      We only use the new rules if flag_new_for_scope is nonzero.  */
645   leaving_for_scope
646     = current_binding_level->kind == sk_for && flag_new_for_scope;
647 
648   /* Before we remove the declarations first check for unused variables.  */
649   if ((warn_unused_variable || warn_unused_but_set_variable)
650       && current_binding_level->kind != sk_template_parms
651       && !processing_template_decl)
652     for (tree d = get_local_decls (); d; d = TREE_CHAIN (d))
653       {
654 	/* There are cases where D itself is a TREE_LIST.  See in
655 	   push_local_binding where the list of decls returned by
656 	   getdecls is built.  */
657 	decl = TREE_CODE (d) == TREE_LIST ? TREE_VALUE (d) : d;
658 
659 	tree type = TREE_TYPE (decl);
660 	if (VAR_P (decl)
661 	    && (! TREE_USED (decl) || !DECL_READ_P (decl))
662 	    && ! DECL_IN_SYSTEM_HEADER (decl)
663 	    /* For structured bindings, consider only real variables, not
664 	       subobjects.  */
665 	    && (DECL_DECOMPOSITION_P (decl) ? !DECL_DECOMP_BASE (decl)
666 		: (DECL_NAME (decl) && !DECL_ARTIFICIAL (decl)))
667 	    && type != error_mark_node
668 	    && (!CLASS_TYPE_P (type)
669 		|| !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
670 		|| lookup_attribute ("warn_unused",
671 				     TYPE_ATTRIBUTES (TREE_TYPE (decl)))))
672 	  {
673 	    if (! TREE_USED (decl))
674 	      {
675 		if (!DECL_NAME (decl) && DECL_DECOMPOSITION_P (decl))
676 		  warning_at (DECL_SOURCE_LOCATION (decl),
677 			      OPT_Wunused_variable,
678 			      "unused structured binding declaration");
679 		else
680 		  warning_at (DECL_SOURCE_LOCATION (decl),
681 			      OPT_Wunused_variable, "unused variable %qD", decl);
682 	      }
683 	    else if (DECL_CONTEXT (decl) == current_function_decl
684 		     // For -Wunused-but-set-variable leave references alone.
685 		     && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
686 		     && errorcount == unused_but_set_errorcount)
687 	      {
688 		if (!DECL_NAME (decl) && DECL_DECOMPOSITION_P (decl))
689 		  warning_at (DECL_SOURCE_LOCATION (decl),
690 			      OPT_Wunused_but_set_variable, "structured "
691 			      "binding declaration set but not used");
692 		else
693 		  warning_at (DECL_SOURCE_LOCATION (decl),
694 			      OPT_Wunused_but_set_variable,
695 			      "variable %qD set but not used", decl);
696 		unused_but_set_errorcount = errorcount;
697 	      }
698 	  }
699       }
700 
701   /* Remove declarations for all the DECLs in this level.  */
702   for (link = decls; link; link = TREE_CHAIN (link))
703     {
704       decl = TREE_CODE (link) == TREE_LIST ? TREE_VALUE (link) : link;
705       tree name = OVL_NAME (decl);
706 
707       if (leaving_for_scope && VAR_P (decl)
708 	  /* It's hard to make this ARM compatibility hack play nicely with
709 	     lambdas, and it really isn't necessary in C++11 mode.  */
710 	  && cxx_dialect < cxx11
711 	  && name)
712 	{
713 	  cxx_binding *ob = outer_binding (name,
714 					   IDENTIFIER_BINDING (name),
715 					   /*class_p=*/true);
716 	  tree ns_binding = NULL_TREE;
717 	  if (!ob)
718 	    ns_binding = get_namespace_binding (current_namespace, name);
719 
720 	  if (ob && ob->scope == current_binding_level->level_chain)
721 	    /* We have something like:
722 
723 		 int i;
724 		 for (int i; ;);
725 
726 	       and we are leaving the `for' scope.  There's no reason to
727 	       keep the binding of the inner `i' in this case.  */
728 	    ;
729 	  else if ((ob && (TREE_CODE (ob->value) == TYPE_DECL))
730 		   || (ns_binding && TREE_CODE (ns_binding) == TYPE_DECL))
731 	    /* Here, we have something like:
732 
733 		 typedef int I;
734 
735 		 void f () {
736 		   for (int I; ;);
737 		 }
738 
739 	       We must pop the for-scope binding so we know what's a
740 	       type and what isn't.  */
741 	    ;
742 	  else
743 	    {
744 	      /* Mark this VAR_DECL as dead so that we can tell we left it
745 		 there only for backward compatibility.  */
746 	      DECL_DEAD_FOR_LOCAL (link) = 1;
747 
748 	      /* Keep track of what should have happened when we
749 		 popped the binding.  */
750 	      if (ob && ob->value)
751 		{
752 		  SET_DECL_SHADOWED_FOR_VAR (link, ob->value);
753 		  DECL_HAS_SHADOWED_FOR_VAR_P (link) = 1;
754 		}
755 
756 	      /* Add it to the list of dead variables in the next
757 		 outermost binding to that we can remove these when we
758 		 leave that binding.  */
759 	      vec_safe_push (
760 		  current_binding_level->level_chain->dead_vars_from_for,
761 		  link);
762 
763 	      /* Although we don't pop the cxx_binding, we do clear
764 		 its SCOPE since the scope is going away now.  */
765 	      IDENTIFIER_BINDING (name)->scope
766 		= current_binding_level->level_chain;
767 
768 	      /* Don't remove the binding. */
769 	      name = NULL_TREE;
770 	    }
771 	}
772       /* Remove the binding.  */
773       if (TREE_CODE (decl) == LABEL_DECL)
774 	pop_local_label (name, decl);
775       else
776 	pop_local_binding (name, decl);
777     }
778 
779   /* Remove declarations for any `for' variables from inner scopes
780      that we kept around.  */
781   FOR_EACH_VEC_SAFE_ELT_REVERSE (current_binding_level->dead_vars_from_for,
782 			         ix, decl)
783     pop_local_binding (DECL_NAME (decl), decl);
784 
785   /* Restore the IDENTIFIER_TYPE_VALUEs.  */
786   for (link = current_binding_level->type_shadowed;
787        link; link = TREE_CHAIN (link))
788     SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link), TREE_VALUE (link));
789 
790   /* There may be OVERLOADs (wrapped in TREE_LISTs) on the BLOCK_VARs
791      list if a `using' declaration put them there.  The debugging
792      back ends won't understand OVERLOAD, so we remove them here.
793      Because the BLOCK_VARS are (temporarily) shared with
794      CURRENT_BINDING_LEVEL->NAMES we must do this fixup after we have
795      popped all the bindings.  Also remove undeduced 'auto' decls,
796      which LTO doesn't understand, and can't have been used by anything.  */
797   if (block)
798     {
799       tree* d;
800 
801       for (d = &BLOCK_VARS (block); *d; )
802 	{
803 	  if (TREE_CODE (*d) == TREE_LIST
804 	      || (!processing_template_decl
805 		  && undeduced_auto_decl (*d)))
806 	    *d = TREE_CHAIN (*d);
807 	  else
808 	    d = &DECL_CHAIN (*d);
809 	}
810     }
811 
812   /* If the level being exited is the top level of a function,
813      check over all the labels.  */
814   if (functionbody)
815     {
816       if (block)
817 	{
818 	  /* Since this is the top level block of a function, the vars are
819 	     the function's parameters.  Don't leave them in the BLOCK
820 	     because they are found in the FUNCTION_DECL instead.  */
821 	  BLOCK_VARS (block) = 0;
822 	  pop_labels (block);
823 	}
824       else
825 	pop_labels (subblocks);
826     }
827 
828   kind = current_binding_level->kind;
829   if (kind == sk_cleanup)
830     {
831       tree stmt;
832 
833       /* If this is a temporary binding created for a cleanup, then we'll
834 	 have pushed a statement list level.  Pop that, create a new
835 	 BIND_EXPR for the block, and insert it into the stream.  */
836       stmt = pop_stmt_list (current_binding_level->statement_list);
837       stmt = c_build_bind_expr (input_location, block, stmt);
838       add_stmt (stmt);
839     }
840 
841   leave_scope ();
842   if (functionbody)
843     {
844       /* The current function is being defined, so its DECL_INITIAL
845 	 should be error_mark_node.  */
846       gcc_assert (DECL_INITIAL (current_function_decl) == error_mark_node);
847       DECL_INITIAL (current_function_decl) = block ? block : subblocks;
848       if (subblocks)
849 	{
850 	  if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
851 	    {
852 	      if (BLOCK_SUBBLOCKS (subblocks))
853 		BLOCK_OUTER_CURLY_BRACE_P (BLOCK_SUBBLOCKS (subblocks)) = 1;
854 	    }
855 	  else
856 	    BLOCK_OUTER_CURLY_BRACE_P (subblocks) = 1;
857 	}
858     }
859   else if (block)
860     current_binding_level->blocks
861       = block_chainon (current_binding_level->blocks, block);
862 
863   /* If we did not make a block for the level just exited,
864      any blocks made for inner levels
865      (since they cannot be recorded as subblocks in that level)
866      must be carried forward so they will later become subblocks
867      of something else.  */
868   else if (subblocks)
869     current_binding_level->blocks
870       = block_chainon (current_binding_level->blocks, subblocks);
871 
872   /* Each and every BLOCK node created here in `poplevel' is important
873      (e.g. for proper debugging information) so if we created one
874      earlier, mark it as "used".  */
875   if (block)
876     TREE_USED (block) = 1;
877 
878   /* All temporary bindings created for cleanups are popped silently.  */
879   if (kind == sk_cleanup)
880     goto restart;
881 
882   timevar_cond_stop (TV_NAME_LOOKUP, subtime);
883   return block;
884 }
885 
886 /* Call wrapup_globals_declarations for the globals in NAMESPACE.  */
887 /* Diagnose odr-used extern inline variables without definitions
888    in the current TU.  */
889 
890 int
wrapup_namespace_globals()891 wrapup_namespace_globals ()
892 {
893   if (vec<tree, va_gc> *statics = static_decls)
894     {
895       tree decl;
896       unsigned int i;
897       FOR_EACH_VEC_ELT (*statics, i, decl)
898 	{
899 	  if (warn_unused_function
900 	      && TREE_CODE (decl) == FUNCTION_DECL
901 	      && DECL_INITIAL (decl) == 0
902 	      && DECL_EXTERNAL (decl)
903 	      && !TREE_PUBLIC (decl)
904 	      && !DECL_ARTIFICIAL (decl)
905 	      && !DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
906 	      && !TREE_NO_WARNING (decl))
907 	    warning_at (DECL_SOURCE_LOCATION (decl),
908 			OPT_Wunused_function,
909 			"%qF declared %<static%> but never defined", decl);
910 
911 	  if (VAR_P (decl)
912 	      && DECL_EXTERNAL (decl)
913 	      && DECL_INLINE_VAR_P (decl)
914 	      && DECL_ODR_USED (decl))
915 	    error_at (DECL_SOURCE_LOCATION (decl),
916 		      "odr-used inline variable %qD is not defined", decl);
917 	}
918 
919       /* Clear out the list, so we don't rescan next time.  */
920       static_decls = NULL;
921 
922       /* Write out any globals that need to be output.  */
923       return wrapup_global_declarations (statics->address (),
924 					 statics->length ());
925     }
926   return 0;
927 }
928 
929 /* In C++, you don't have to write `struct S' to refer to `S'; you
930    can just use `S'.  We accomplish this by creating a TYPE_DECL as
931    if the user had written `typedef struct S S'.  Create and return
932    the TYPE_DECL for TYPE.  */
933 
934 tree
create_implicit_typedef(tree name,tree type)935 create_implicit_typedef (tree name, tree type)
936 {
937   tree decl;
938 
939   decl = build_decl (input_location, TYPE_DECL, name, type);
940   DECL_ARTIFICIAL (decl) = 1;
941   /* There are other implicit type declarations, like the one *within*
942      a class that allows you to write `S::S'.  We must distinguish
943      amongst these.  */
944   SET_DECL_IMPLICIT_TYPEDEF_P (decl);
945   TYPE_NAME (type) = decl;
946   TYPE_STUB_DECL (type) = decl;
947 
948   return decl;
949 }
950 
951 /* Remember a local name for name-mangling purposes.  */
952 
953 static void
push_local_name(tree decl)954 push_local_name (tree decl)
955 {
956   size_t i, nelts;
957   tree t, name;
958 
959   timevar_start (TV_NAME_LOOKUP);
960 
961   name = DECL_NAME (decl);
962 
963   nelts = vec_safe_length (local_names);
964   for (i = 0; i < nelts; i++)
965     {
966       t = (*local_names)[i];
967       if (DECL_NAME (t) == name)
968 	{
969 	  retrofit_lang_decl (decl);
970 	  DECL_LANG_SPECIFIC (decl)->u.base.u2sel = 1;
971 	  if (DECL_DISCRIMINATOR_SET_P (t))
972 	    DECL_DISCRIMINATOR (decl) = DECL_DISCRIMINATOR (t) + 1;
973 	  else
974 	    DECL_DISCRIMINATOR (decl) = 1;
975 
976 	  (*local_names)[i] = decl;
977 	  timevar_stop (TV_NAME_LOOKUP);
978 	  return;
979 	}
980     }
981 
982   vec_safe_push (local_names, decl);
983   timevar_stop (TV_NAME_LOOKUP);
984 }
985 
986 /* Subroutine of duplicate_decls: return truthvalue of whether
987    or not types of these decls match.
988 
989    For C++, we must compare the parameter list so that `int' can match
990    `int&' in a parameter position, but `int&' is not confused with
991    `const int&'.  */
992 
993 int
decls_match(tree newdecl,tree olddecl,bool record_versions)994 decls_match (tree newdecl, tree olddecl, bool record_versions /* = true */)
995 {
996   int types_match;
997 
998   if (newdecl == olddecl)
999     return 1;
1000 
1001   if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
1002     /* If the two DECLs are not even the same kind of thing, we're not
1003        interested in their types.  */
1004     return 0;
1005 
1006   gcc_assert (DECL_P (newdecl));
1007 
1008   if (TREE_CODE (newdecl) == FUNCTION_DECL)
1009     {
1010       tree f1 = TREE_TYPE (newdecl);
1011       tree f2 = TREE_TYPE (olddecl);
1012       tree p1 = TYPE_ARG_TYPES (f1);
1013       tree p2 = TYPE_ARG_TYPES (f2);
1014       tree r2;
1015 
1016       /* Specializations of different templates are different functions
1017 	 even if they have the same type.  */
1018       tree t1 = (DECL_USE_TEMPLATE (newdecl)
1019 		 ? DECL_TI_TEMPLATE (newdecl)
1020 		 : NULL_TREE);
1021       tree t2 = (DECL_USE_TEMPLATE (olddecl)
1022 		 ? DECL_TI_TEMPLATE (olddecl)
1023 		 : NULL_TREE);
1024       if (t1 != t2)
1025 	return 0;
1026 
1027       if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
1028 	  && ! (DECL_EXTERN_C_P (newdecl)
1029 		&& DECL_EXTERN_C_P (olddecl)))
1030 	return 0;
1031 
1032       /* A new declaration doesn't match a built-in one unless it
1033 	 is also extern "C".  */
1034       if (DECL_IS_BUILTIN (olddecl)
1035 	  && DECL_EXTERN_C_P (olddecl) && !DECL_EXTERN_C_P (newdecl))
1036 	return 0;
1037 
1038       if (TREE_CODE (f1) != TREE_CODE (f2))
1039 	return 0;
1040 
1041       /* A declaration with deduced return type should use its pre-deduction
1042 	 type for declaration matching.  */
1043       r2 = fndecl_declared_return_type (olddecl);
1044 
1045       if (same_type_p (TREE_TYPE (f1), r2))
1046 	{
1047 	  if (!prototype_p (f2) && DECL_EXTERN_C_P (olddecl)
1048 	      && (DECL_BUILT_IN (olddecl)
1049 #ifndef NO_IMPLICIT_EXTERN_C
1050 		  || (DECL_IN_SYSTEM_HEADER (newdecl) && !DECL_CLASS_SCOPE_P (newdecl))
1051 		  || (DECL_IN_SYSTEM_HEADER (olddecl) && !DECL_CLASS_SCOPE_P (olddecl))
1052 #endif
1053 	      ))
1054 	    {
1055 	      types_match = self_promoting_args_p (p1);
1056 	      if (p1 == void_list_node)
1057 		TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
1058 	    }
1059 #ifndef NO_IMPLICIT_EXTERN_C
1060 	  else if (!prototype_p (f1)
1061 		   && (DECL_EXTERN_C_P (olddecl)
1062 		       && DECL_IN_SYSTEM_HEADER (olddecl)
1063 		       && !DECL_CLASS_SCOPE_P (olddecl))
1064 		   && (DECL_EXTERN_C_P (newdecl)
1065 		       && DECL_IN_SYSTEM_HEADER (newdecl)
1066 		       && !DECL_CLASS_SCOPE_P (newdecl)))
1067 	    {
1068 	      types_match = self_promoting_args_p (p2);
1069 	      TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
1070 	    }
1071 #endif
1072 	  else
1073 	    types_match =
1074 	      compparms (p1, p2)
1075 	      && type_memfn_rqual (f1) == type_memfn_rqual (f2)
1076 	      && (TYPE_ATTRIBUTES (TREE_TYPE (newdecl)) == NULL_TREE
1077 	          || comp_type_attributes (TREE_TYPE (newdecl),
1078 					   TREE_TYPE (olddecl)) != 0);
1079 	}
1080       else
1081 	types_match = 0;
1082 
1083       /* The decls dont match if they correspond to two different versions
1084 	 of the same function.   Disallow extern "C" functions to be
1085 	 versions for now.  */
1086       if (types_match
1087 	  && !DECL_EXTERN_C_P (newdecl)
1088 	  && !DECL_EXTERN_C_P (olddecl)
1089 	  && record_versions
1090 	  && maybe_version_functions (newdecl, olddecl,
1091 				      (!DECL_FUNCTION_VERSIONED (newdecl)
1092 				       || !DECL_FUNCTION_VERSIONED (olddecl))))
1093 	return 0;
1094     }
1095   else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1096     {
1097       tree oldres = DECL_TEMPLATE_RESULT (olddecl);
1098       tree newres = DECL_TEMPLATE_RESULT (newdecl);
1099 
1100       if (TREE_CODE (newres) != TREE_CODE (oldres))
1101 	return 0;
1102 
1103       if (!comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
1104 				DECL_TEMPLATE_PARMS (olddecl)))
1105 	return 0;
1106 
1107       if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
1108 	types_match = (same_type_p (TREE_TYPE (oldres), TREE_TYPE (newres))
1109 		       && equivalently_constrained (olddecl, newdecl));
1110       else
1111 	// We don't need to check equivalently_constrained for variable and
1112 	// function templates because we check it on the results.
1113 	types_match = decls_match (oldres, newres);
1114     }
1115   else
1116     {
1117       /* Need to check scope for variable declaration (VAR_DECL).
1118 	 For typedef (TYPE_DECL), scope is ignored.  */
1119       if (VAR_P (newdecl)
1120 	  && CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
1121 	  /* [dcl.link]
1122 	     Two declarations for an object with C language linkage
1123 	     with the same name (ignoring the namespace that qualify
1124 	     it) that appear in different namespace scopes refer to
1125 	     the same object.  */
1126 	  && !(DECL_EXTERN_C_P (olddecl) && DECL_EXTERN_C_P (newdecl)))
1127 	return 0;
1128 
1129       if (TREE_TYPE (newdecl) == error_mark_node)
1130 	types_match = TREE_TYPE (olddecl) == error_mark_node;
1131       else if (TREE_TYPE (olddecl) == NULL_TREE)
1132 	types_match = TREE_TYPE (newdecl) == NULL_TREE;
1133       else if (TREE_TYPE (newdecl) == NULL_TREE)
1134 	types_match = 0;
1135       else
1136 	types_match = comptypes (TREE_TYPE (newdecl),
1137 				 TREE_TYPE (olddecl),
1138 				 COMPARE_REDECLARATION);
1139     }
1140 
1141   // Normal functions can be constrained, as can variable partial
1142   // specializations.
1143   if (types_match && VAR_OR_FUNCTION_DECL_P (newdecl))
1144     types_match = equivalently_constrained (newdecl, olddecl);
1145 
1146   return types_match;
1147 }
1148 
1149 /* NEWDECL and OLDDECL have identical signatures.  If they are
1150    different versions adjust them and return true.
1151    If RECORD is set to true, record function versions.  */
1152 
1153 bool
maybe_version_functions(tree newdecl,tree olddecl,bool record)1154 maybe_version_functions (tree newdecl, tree olddecl, bool record)
1155 {
1156   if (!targetm.target_option.function_versions (newdecl, olddecl))
1157     return false;
1158 
1159   if (!DECL_FUNCTION_VERSIONED (olddecl))
1160     {
1161       DECL_FUNCTION_VERSIONED (olddecl) = 1;
1162       if (DECL_ASSEMBLER_NAME_SET_P (olddecl))
1163 	mangle_decl (olddecl);
1164     }
1165 
1166   if (!DECL_FUNCTION_VERSIONED (newdecl))
1167     {
1168       DECL_FUNCTION_VERSIONED (newdecl) = 1;
1169       if (DECL_ASSEMBLER_NAME_SET_P (newdecl))
1170 	mangle_decl (newdecl);
1171     }
1172 
1173   if (record)
1174     cgraph_node::record_function_versions (olddecl, newdecl);
1175 
1176   return true;
1177 }
1178 
1179 /* If NEWDECL is `static' and an `extern' was seen previously,
1180    warn about it.  OLDDECL is the previous declaration.
1181 
1182    Note that this does not apply to the C++ case of declaring
1183    a variable `extern const' and then later `const'.
1184 
1185    Don't complain about built-in functions, since they are beyond
1186    the user's control.  */
1187 
1188 void
warn_extern_redeclared_static(tree newdecl,tree olddecl)1189 warn_extern_redeclared_static (tree newdecl, tree olddecl)
1190 {
1191   if (TREE_CODE (newdecl) == TYPE_DECL
1192       || TREE_CODE (newdecl) == TEMPLATE_DECL
1193       || TREE_CODE (newdecl) == CONST_DECL
1194       || TREE_CODE (newdecl) == NAMESPACE_DECL)
1195     return;
1196 
1197   /* Don't get confused by static member functions; that's a different
1198      use of `static'.  */
1199   if (TREE_CODE (newdecl) == FUNCTION_DECL
1200       && DECL_STATIC_FUNCTION_P (newdecl))
1201     return;
1202 
1203   /* If the old declaration was `static', or the new one isn't, then
1204      everything is OK.  */
1205   if (DECL_THIS_STATIC (olddecl) || !DECL_THIS_STATIC (newdecl))
1206     return;
1207 
1208   /* It's OK to declare a builtin function as `static'.  */
1209   if (TREE_CODE (olddecl) == FUNCTION_DECL
1210       && DECL_ARTIFICIAL (olddecl))
1211     return;
1212 
1213   if (permerror (DECL_SOURCE_LOCATION (newdecl),
1214 		 "%qD was declared %<extern%> and later %<static%>", newdecl))
1215     inform (DECL_SOURCE_LOCATION (olddecl),
1216 	    "previous declaration of %qD", olddecl);
1217 }
1218 
1219 /* NEW_DECL is a redeclaration of OLD_DECL; both are functions or
1220    function templates.  If their exception specifications do not
1221    match, issue a diagnostic.  */
1222 
1223 static void
check_redeclaration_exception_specification(tree new_decl,tree old_decl)1224 check_redeclaration_exception_specification (tree new_decl,
1225 					     tree old_decl)
1226 {
1227   tree new_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (new_decl));
1228   tree old_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old_decl));
1229 
1230   /* Two default specs are equivalent, don't force evaluation.  */
1231   if (UNEVALUATED_NOEXCEPT_SPEC_P (new_exceptions)
1232       && UNEVALUATED_NOEXCEPT_SPEC_P (old_exceptions))
1233     return;
1234 
1235   if (!type_dependent_expression_p (old_decl))
1236     {
1237       maybe_instantiate_noexcept (new_decl);
1238       maybe_instantiate_noexcept (old_decl);
1239     }
1240   new_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (new_decl));
1241   old_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old_decl));
1242 
1243   /* [except.spec]
1244 
1245      If any declaration of a function has an exception-specification,
1246      all declarations, including the definition and an explicit
1247      specialization, of that function shall have an
1248      exception-specification with the same set of type-ids.  */
1249   if (! DECL_IS_BUILTIN (old_decl)
1250       && !comp_except_specs (new_exceptions, old_exceptions, ce_normal))
1251     {
1252       const char *const msg
1253 	= G_("declaration of %qF has a different exception specifier");
1254       bool complained = true;
1255       location_t new_loc = DECL_SOURCE_LOCATION (new_decl);
1256       if (DECL_IN_SYSTEM_HEADER (old_decl))
1257 	complained = pedwarn (new_loc, OPT_Wsystem_headers, msg, new_decl);
1258       else if (!flag_exceptions)
1259 	/* We used to silently permit mismatched eh specs with
1260 	   -fno-exceptions, so make them a pedwarn now.  */
1261 	complained = pedwarn (new_loc, OPT_Wpedantic, msg, new_decl);
1262       else
1263 	error_at (new_loc, msg, new_decl);
1264       if (complained)
1265 	inform (DECL_SOURCE_LOCATION (old_decl),
1266 		"from previous declaration %qF", old_decl);
1267     }
1268 }
1269 
1270 /* Return true if OLD_DECL and NEW_DECL agree on constexprness.
1271    Otherwise issue diagnostics.  */
1272 
1273 static bool
validate_constexpr_redeclaration(tree old_decl,tree new_decl)1274 validate_constexpr_redeclaration (tree old_decl, tree new_decl)
1275 {
1276   old_decl = STRIP_TEMPLATE (old_decl);
1277   new_decl = STRIP_TEMPLATE (new_decl);
1278   if (!VAR_OR_FUNCTION_DECL_P (old_decl)
1279       || !VAR_OR_FUNCTION_DECL_P (new_decl))
1280     return true;
1281   if (DECL_DECLARED_CONSTEXPR_P (old_decl)
1282       == DECL_DECLARED_CONSTEXPR_P (new_decl))
1283     return true;
1284   if (TREE_CODE (old_decl) == FUNCTION_DECL)
1285     {
1286       if (DECL_BUILT_IN (old_decl))
1287 	{
1288 	  /* Hide a built-in declaration.  */
1289 	  DECL_DECLARED_CONSTEXPR_P (old_decl)
1290 	    = DECL_DECLARED_CONSTEXPR_P (new_decl);
1291 	  return true;
1292 	}
1293       /* 7.1.5 [dcl.constexpr]
1294 	 Note: An explicit specialization can differ from the template
1295 	 declaration with respect to the constexpr specifier.  */
1296       if (! DECL_TEMPLATE_SPECIALIZATION (old_decl)
1297 	  && DECL_TEMPLATE_SPECIALIZATION (new_decl))
1298 	return true;
1299 
1300       error_at (DECL_SOURCE_LOCATION (new_decl),
1301 		"redeclaration %qD differs in %<constexpr%> "
1302 		"from previous declaration", new_decl);
1303       inform (DECL_SOURCE_LOCATION (old_decl),
1304 	      "previous declaration %qD", old_decl);
1305       return false;
1306     }
1307   return true;
1308 }
1309 
1310 // If OLDDECL and NEWDECL are concept declarations with the same type
1311 // (i.e., and template parameters), but different requirements,
1312 // emit diagnostics and return true. Otherwise, return false.
1313 static inline bool
check_concept_refinement(tree olddecl,tree newdecl)1314 check_concept_refinement (tree olddecl, tree newdecl)
1315 {
1316   if (!DECL_DECLARED_CONCEPT_P (olddecl) || !DECL_DECLARED_CONCEPT_P (newdecl))
1317     return false;
1318 
1319   tree d1 = DECL_TEMPLATE_RESULT (olddecl);
1320   tree d2 = DECL_TEMPLATE_RESULT (newdecl);
1321   if (TREE_CODE (d1) != TREE_CODE (d2))
1322     return false;
1323 
1324   tree t1 = TREE_TYPE (d1);
1325   tree t2 = TREE_TYPE (d2);
1326   if (TREE_CODE (d1) == FUNCTION_DECL)
1327     {
1328       if (compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2))
1329           && comp_template_parms (DECL_TEMPLATE_PARMS (olddecl),
1330                                   DECL_TEMPLATE_PARMS (newdecl))
1331           && !equivalently_constrained (olddecl, newdecl))
1332         {
1333           error ("cannot specialize concept %q#D", olddecl);
1334           return true;
1335         }
1336     }
1337   return false;
1338 }
1339 
1340 /* DECL is a redeclaration of a function or function template.  If
1341    it does have default arguments issue a diagnostic.  Note: this
1342    function is used to enforce the requirements in C++11 8.3.6 about
1343    no default arguments in redeclarations.  */
1344 
1345 static void
check_redeclaration_no_default_args(tree decl)1346 check_redeclaration_no_default_args (tree decl)
1347 {
1348   gcc_assert (DECL_DECLARES_FUNCTION_P (decl));
1349 
1350   for (tree t = FUNCTION_FIRST_USER_PARMTYPE (decl);
1351        t && t != void_list_node; t = TREE_CHAIN (t))
1352     if (TREE_PURPOSE (t))
1353       {
1354 	permerror (DECL_SOURCE_LOCATION (decl),
1355 		   "redeclaration of %q#D may not have default "
1356 		   "arguments", decl);
1357 	return;
1358       }
1359 }
1360 
1361 /* Merge tree bits that correspond to attributes noreturn, nothrow,
1362    const,  malloc, and pure from NEWDECL with those of OLDDECL.  */
1363 
1364 static void
merge_attribute_bits(tree newdecl,tree olddecl)1365 merge_attribute_bits (tree newdecl, tree olddecl)
1366 {
1367   TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1368   TREE_THIS_VOLATILE (olddecl) |= TREE_THIS_VOLATILE (newdecl);
1369   TREE_NOTHROW (newdecl) |= TREE_NOTHROW (olddecl);
1370   TREE_NOTHROW (olddecl) |= TREE_NOTHROW (newdecl);
1371   TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1372   TREE_READONLY (olddecl) |= TREE_READONLY (newdecl);
1373   DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1374   DECL_IS_MALLOC (olddecl) |= DECL_IS_MALLOC (newdecl);
1375   DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
1376   DECL_PURE_P (olddecl) |= DECL_PURE_P (newdecl);
1377   DECL_UNINLINABLE (newdecl) |= DECL_UNINLINABLE (olddecl);
1378   DECL_UNINLINABLE (olddecl) |= DECL_UNINLINABLE (newdecl);
1379 }
1380 
1381 #define GNU_INLINE_P(fn) (DECL_DECLARED_INLINE_P (fn)			\
1382 			  && lookup_attribute ("gnu_inline",		\
1383 					       DECL_ATTRIBUTES (fn)))
1384 
1385 /* If NEWDECL is a redeclaration of OLDDECL, merge the declarations.
1386    If the redeclaration is invalid, a diagnostic is issued, and the
1387    error_mark_node is returned.  Otherwise, OLDDECL is returned.
1388 
1389    If NEWDECL is not a redeclaration of OLDDECL, NULL_TREE is
1390    returned.
1391 
1392    NEWDECL_IS_FRIEND is true if NEWDECL was declared as a friend.  */
1393 
1394 tree
duplicate_decls(tree newdecl,tree olddecl,bool newdecl_is_friend)1395 duplicate_decls (tree newdecl, tree olddecl, bool newdecl_is_friend)
1396 {
1397   unsigned olddecl_uid = DECL_UID (olddecl);
1398   int olddecl_friend = 0, types_match = 0, hidden_friend = 0;
1399   int new_defines_function = 0;
1400   tree new_template_info;
1401 
1402   if (newdecl == olddecl)
1403     return olddecl;
1404 
1405   types_match = decls_match (newdecl, olddecl);
1406 
1407   /* If either the type of the new decl or the type of the old decl is an
1408      error_mark_node, then that implies that we have already issued an
1409      error (earlier) for some bogus type specification, and in that case,
1410      it is rather pointless to harass the user with yet more error message
1411      about the same declaration, so just pretend the types match here.  */
1412   if (TREE_TYPE (newdecl) == error_mark_node
1413       || TREE_TYPE (olddecl) == error_mark_node)
1414     return error_mark_node;
1415 
1416   if (DECL_NAME (newdecl)
1417       && DECL_NAME (olddecl)
1418       && UDLIT_OPER_P (DECL_NAME (newdecl))
1419       && UDLIT_OPER_P (DECL_NAME (olddecl)))
1420     {
1421       if (TREE_CODE (newdecl) == TEMPLATE_DECL
1422 	  && TREE_CODE (olddecl) != TEMPLATE_DECL
1423 	  && check_raw_literal_operator (olddecl))
1424 	error ("literal operator template %q+D conflicts with"
1425 	       " raw literal operator %qD", newdecl, olddecl);
1426       else if (TREE_CODE (newdecl) != TEMPLATE_DECL
1427 	       && TREE_CODE (olddecl) == TEMPLATE_DECL
1428 	       && check_raw_literal_operator (newdecl))
1429 	error ("raw literal operator %q+D conflicts with"
1430 	       " literal operator template %qD", newdecl, olddecl);
1431     }
1432 
1433   /* True to merge attributes between the declarations, false to
1434      set OLDDECL's attributes to those of NEWDECL (for template
1435      explicit specializations that specify their own attributes
1436      independent of those specified for the primary template).  */
1437   const bool merge_attr = (TREE_CODE (newdecl) != FUNCTION_DECL
1438 			   || !DECL_TEMPLATE_SPECIALIZATION (newdecl)
1439 			   || DECL_TEMPLATE_SPECIALIZATION (olddecl));
1440 
1441   if (DECL_P (olddecl)
1442       && TREE_CODE (newdecl) == FUNCTION_DECL
1443       && TREE_CODE (olddecl) == FUNCTION_DECL
1444       && merge_attr
1445       && diagnose_mismatched_attributes (olddecl, newdecl))
1446     {
1447       if (DECL_INITIAL (olddecl))
1448 	inform (DECL_SOURCE_LOCATION (olddecl),
1449 		"previous definition of %qD was here", olddecl);
1450       else
1451 	inform (DECL_SOURCE_LOCATION (olddecl),
1452 		"previous declaration of %qD was here", olddecl);
1453     }
1454 
1455   /* Check for redeclaration and other discrepancies.  */
1456   if (TREE_CODE (olddecl) == FUNCTION_DECL
1457       && DECL_ARTIFICIAL (olddecl))
1458     {
1459       gcc_assert (!DECL_HIDDEN_FRIEND_P (olddecl));
1460       if (TREE_CODE (newdecl) != FUNCTION_DECL)
1461 	{
1462 	  /* Avoid warnings redeclaring built-ins which have not been
1463 	     explicitly declared.  */
1464 	  if (DECL_ANTICIPATED (olddecl))
1465 	    {
1466 	      if (TREE_PUBLIC (newdecl)
1467 		  && CP_DECL_CONTEXT (newdecl) == global_namespace)
1468 		warning_at (DECL_SOURCE_LOCATION (newdecl),
1469 			    OPT_Wbuiltin_declaration_mismatch,
1470 			    "built-in function %qD declared as non-function",
1471 			    newdecl);
1472 	      return NULL_TREE;
1473 	    }
1474 
1475 	  /* If you declare a built-in or predefined function name as static,
1476 	     the old definition is overridden, but optionally warn this was a
1477 	     bad choice of name.  */
1478 	  if (! TREE_PUBLIC (newdecl))
1479 	    {
1480 	      warning (OPT_Wshadow,
1481                        DECL_BUILT_IN (olddecl)
1482                        ? G_("shadowing built-in function %q#D")
1483                        : G_("shadowing library function %q#D"), olddecl);
1484 	      /* Discard the old built-in function.  */
1485 	      return NULL_TREE;
1486 	    }
1487 	  /* If the built-in is not ansi, then programs can override
1488 	     it even globally without an error.  */
1489 	  else if (! DECL_BUILT_IN (olddecl))
1490 	    warning_at (DECL_SOURCE_LOCATION (newdecl), 0,
1491 			"library function %q#D redeclared as non-function %q#D",
1492 			olddecl, newdecl);
1493 	  else
1494 	    error ("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 (DECL_SOURCE_LOCATION (newdecl),
1502 		    "redeclaration of %<pragma omp declare reduction%>");
1503 	  inform (DECL_SOURCE_LOCATION (olddecl),
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 (DECL_SOURCE_LOCATION (newdecl),
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 (DECL_BUILT_IN (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 (DECL_SOURCE_LOCATION (newdecl),
1592 				    "definition of %q#D ambiguates built-in "
1593 				    "declaration %q#D", newdecl, olddecl);
1594 			  return error_mark_node;
1595 			}
1596 		      if (permerror (DECL_SOURCE_LOCATION (newdecl),
1597 				     "new declaration %q#D ambiguates built-in"
1598 				     " declaration %q#D", newdecl, olddecl)
1599 			  && flag_permissive)
1600 			inform (DECL_SOURCE_LOCATION (newdecl),
1601 				"ignoring the %q#D declaration", newdecl);
1602 		      return flag_permissive ? olddecl : error_mark_node;
1603 		    }
1604 		}
1605 
1606 	      /* A near match; override the builtin.  */
1607 
1608 	      if (TREE_PUBLIC (newdecl))
1609 		warning_at (DECL_SOURCE_LOCATION (newdecl),
1610 			    OPT_Wbuiltin_declaration_mismatch,
1611 			    "new declaration %q#D ambiguates built-in "
1612 			    "declaration %q#D", newdecl, olddecl);
1613 	      else
1614 		warning (OPT_Wshadow,
1615                          DECL_BUILT_IN (olddecl)
1616                          ? G_("shadowing built-in function %q#D")
1617                          : G_("shadowing library function %q#D"), olddecl);
1618 	    }
1619 	  else
1620 	    /* Discard the old built-in function.  */
1621 	    return NULL_TREE;
1622 
1623 	  /* Replace the old RTL to avoid problems with inlining.  */
1624 	  COPY_DECL_RTL (newdecl, olddecl);
1625 	}
1626       /* Even if the types match, prefer the new declarations type for
1627 	 built-ins which have not been explicitly declared, for
1628 	 exception lists, etc...  */
1629       else if (DECL_IS_BUILTIN (olddecl))
1630 	{
1631 	  tree type = TREE_TYPE (newdecl);
1632 	  tree attribs = (*targetm.merge_type_attributes)
1633 	    (TREE_TYPE (olddecl), type);
1634 
1635 	  type = cp_build_type_attribute_variant (type, attribs);
1636 	  TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = type;
1637 	}
1638 
1639       /* If a function is explicitly declared "throw ()", propagate that to
1640 	 the corresponding builtin.  */
1641       if (DECL_BUILT_IN_CLASS (olddecl) == BUILT_IN_NORMAL
1642 	  && DECL_ANTICIPATED (olddecl)
1643 	  && TREE_NOTHROW (newdecl)
1644 	  && !TREE_NOTHROW (olddecl))
1645 	{
1646 	  enum built_in_function fncode = DECL_FUNCTION_CODE (olddecl);
1647 	  tree tmpdecl = builtin_decl_explicit (fncode);
1648 	  if (tmpdecl && tmpdecl != olddecl && types_match)
1649 	    TREE_NOTHROW (tmpdecl)  = 1;
1650 	}
1651 
1652       /* Whether or not the builtin can throw exceptions has no
1653 	 bearing on this declarator.  */
1654       TREE_NOTHROW (olddecl) = 0;
1655 
1656       if (DECL_THIS_STATIC (newdecl) && !DECL_THIS_STATIC (olddecl))
1657 	{
1658 	  /* If a builtin function is redeclared as `static', merge
1659 	     the declarations, but make the original one static.  */
1660 	  DECL_THIS_STATIC (olddecl) = 1;
1661 	  TREE_PUBLIC (olddecl) = 0;
1662 
1663 	  /* Make the old declaration consistent with the new one so
1664 	     that all remnants of the builtin-ness of this function
1665 	     will be banished.  */
1666 	  SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
1667 	  COPY_DECL_RTL (newdecl, olddecl);
1668 	}
1669     }
1670   else if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1671     {
1672       /* C++ Standard, 3.3, clause 4:
1673 	 "[Note: a namespace name or a class template name must be unique
1674 	 in its declarative region (7.3.2, clause 14). ]"  */
1675       if (TREE_CODE (olddecl) != NAMESPACE_DECL
1676 	  && TREE_CODE (newdecl) != NAMESPACE_DECL
1677 	  && (TREE_CODE (olddecl) != TEMPLATE_DECL
1678 	      || TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) != TYPE_DECL)
1679 	  && (TREE_CODE (newdecl) != TEMPLATE_DECL
1680 	      || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) != TYPE_DECL))
1681 	{
1682 	  if ((TREE_CODE (olddecl) == TYPE_DECL && DECL_ARTIFICIAL (olddecl)
1683 	       && TREE_CODE (newdecl) != TYPE_DECL)
1684 	      || (TREE_CODE (newdecl) == TYPE_DECL && DECL_ARTIFICIAL (newdecl)
1685 		  && TREE_CODE (olddecl) != TYPE_DECL))
1686 	    {
1687 	      /* We do nothing special here, because C++ does such nasty
1688 		 things with TYPE_DECLs.  Instead, just let the TYPE_DECL
1689 		 get shadowed, and know that if we need to find a TYPE_DECL
1690 		 for a given name, we can look in the IDENTIFIER_TYPE_VALUE
1691 		 slot of the identifier.  */
1692 	      return NULL_TREE;
1693 	    }
1694 
1695 	    if ((TREE_CODE (newdecl) == FUNCTION_DECL
1696 		 && DECL_FUNCTION_TEMPLATE_P (olddecl))
1697 		|| (TREE_CODE (olddecl) == FUNCTION_DECL
1698 		    && DECL_FUNCTION_TEMPLATE_P (newdecl)))
1699 	      return NULL_TREE;
1700 	}
1701 
1702       error ("%q#D redeclared as different kind of symbol", newdecl);
1703       if (TREE_CODE (olddecl) == TREE_LIST)
1704 	olddecl = TREE_VALUE (olddecl);
1705       inform (DECL_SOURCE_LOCATION (olddecl),
1706 	      "previous declaration %q#D", olddecl);
1707 
1708       return error_mark_node;
1709     }
1710   else if (!types_match)
1711     {
1712       if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl))
1713 	/* These are certainly not duplicate declarations; they're
1714 	   from different scopes.  */
1715 	return NULL_TREE;
1716 
1717       if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1718 	{
1719 	  /* The name of a class template may not be declared to refer to
1720 	     any other template, class, function, object, namespace, value,
1721 	     or type in the same scope.  */
1722 	  if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == TYPE_DECL
1723 	      || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
1724 	    {
1725 	      error ("conflicting declaration of template %q+#D", newdecl);
1726 	      inform (DECL_SOURCE_LOCATION (olddecl),
1727 		      "previous declaration %q#D", olddecl);
1728 	      return error_mark_node;
1729 	    }
1730 	  else if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == FUNCTION_DECL
1731 		   && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL
1732 		   && compparms (TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl))),
1733 				 TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl))))
1734 		   && comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
1735 					   DECL_TEMPLATE_PARMS (olddecl))
1736 		   /* Template functions can be disambiguated by
1737 		      return type.  */
1738 		   && same_type_p (TREE_TYPE (TREE_TYPE (newdecl)),
1739 				   TREE_TYPE (TREE_TYPE (olddecl)))
1740                    // Template functions can also be disambiguated by
1741                    // constraints.
1742                    && equivalently_constrained (olddecl, newdecl))
1743 	    {
1744 	      error ("ambiguating new declaration %q+#D", newdecl);
1745 	      inform (DECL_SOURCE_LOCATION (olddecl),
1746 		      "old declaration %q#D", olddecl);
1747 	    }
1748           else if (check_concept_refinement (olddecl, newdecl))
1749 	    return error_mark_node;
1750 	  return NULL_TREE;
1751 	}
1752       if (TREE_CODE (newdecl) == FUNCTION_DECL)
1753 	{
1754 	  if (DECL_EXTERN_C_P (newdecl) && DECL_EXTERN_C_P (olddecl))
1755 	    {
1756 	      error ("conflicting declaration of C function %q+#D",
1757 		     newdecl);
1758 	      inform (DECL_SOURCE_LOCATION (olddecl),
1759 		      "previous declaration %q#D", olddecl);
1760 	      return NULL_TREE;
1761 	    }
1762 	  /* For function versions, params and types match, but they
1763 	     are not ambiguous.  */
1764 	  else if ((!DECL_FUNCTION_VERSIONED (newdecl)
1765 		    && !DECL_FUNCTION_VERSIONED (olddecl))
1766                    // The functions have the same parameter types.
1767 		   && compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1768 				 TYPE_ARG_TYPES (TREE_TYPE (olddecl)))
1769                    // And the same constraints.
1770                    && equivalently_constrained (newdecl, olddecl))
1771 	    {
1772 	      error ("ambiguating new declaration of %q+#D", newdecl);
1773 	      inform (DECL_SOURCE_LOCATION (olddecl),
1774 		      "old declaration %q#D", olddecl);
1775               return error_mark_node;
1776 	    }
1777 	  else
1778 	    return NULL_TREE;
1779 	}
1780       else
1781 	{
1782 	  error ("conflicting declaration %q+#D", newdecl);
1783 	  inform (DECL_SOURCE_LOCATION (olddecl),
1784 		  "previous declaration as %q#D", olddecl);
1785 	  return error_mark_node;
1786 	}
1787     }
1788   else if (TREE_CODE (newdecl) == FUNCTION_DECL
1789 	    && ((DECL_TEMPLATE_SPECIALIZATION (olddecl)
1790 		 && (!DECL_TEMPLATE_INFO (newdecl)
1791 		     || (DECL_TI_TEMPLATE (newdecl)
1792 			 != DECL_TI_TEMPLATE (olddecl))))
1793 		|| (DECL_TEMPLATE_SPECIALIZATION (newdecl)
1794 		    && (!DECL_TEMPLATE_INFO (olddecl)
1795 			|| (DECL_TI_TEMPLATE (olddecl)
1796 			    != DECL_TI_TEMPLATE (newdecl))))))
1797     /* It's OK to have a template specialization and a non-template
1798        with the same type, or to have specializations of two
1799        different templates with the same type.  Note that if one is a
1800        specialization, and the other is an instantiation of the same
1801        template, that we do not exit at this point.  That situation
1802        can occur if we instantiate a template class, and then
1803        specialize one of its methods.  This situation is valid, but
1804        the declarations must be merged in the usual way.  */
1805     return NULL_TREE;
1806   else if (TREE_CODE (newdecl) == FUNCTION_DECL
1807 	   && ((DECL_TEMPLATE_INSTANTIATION (olddecl)
1808 		&& !DECL_USE_TEMPLATE (newdecl))
1809 	       || (DECL_TEMPLATE_INSTANTIATION (newdecl)
1810 		   && !DECL_USE_TEMPLATE (olddecl))))
1811     /* One of the declarations is a template instantiation, and the
1812        other is not a template at all.  That's OK.  */
1813     return NULL_TREE;
1814   else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
1815     {
1816       /* In [namespace.alias] we have:
1817 
1818 	   In a declarative region, a namespace-alias-definition can be
1819 	   used to redefine a namespace-alias declared in that declarative
1820 	   region to refer only to the namespace to which it already
1821 	   refers.
1822 
1823 	 Therefore, if we encounter a second alias directive for the same
1824 	 alias, we can just ignore the second directive.  */
1825       if (DECL_NAMESPACE_ALIAS (newdecl)
1826 	  && (DECL_NAMESPACE_ALIAS (newdecl)
1827 	      == DECL_NAMESPACE_ALIAS (olddecl)))
1828 	return olddecl;
1829 
1830       /* Leave it to update_binding to merge or report error.  */
1831       return NULL_TREE;
1832     }
1833   else
1834     {
1835       const char *errmsg = redeclaration_error_message (newdecl, olddecl);
1836       if (errmsg)
1837 	{
1838 	  error_at (DECL_SOURCE_LOCATION (newdecl), errmsg, newdecl);
1839 	  if (DECL_NAME (olddecl) != NULL_TREE)
1840 	    inform (DECL_SOURCE_LOCATION (olddecl),
1841 		    (DECL_INITIAL (olddecl) && namespace_bindings_p ())
1842 		    ? G_("%q#D previously defined here")
1843 		    : G_("%q#D previously declared here"), olddecl);
1844 	  return error_mark_node;
1845 	}
1846       else if (TREE_CODE (olddecl) == FUNCTION_DECL
1847 	       && DECL_INITIAL (olddecl) != NULL_TREE
1848 	       && !prototype_p (TREE_TYPE (olddecl))
1849 	       && prototype_p (TREE_TYPE (newdecl)))
1850 	{
1851 	  /* Prototype decl follows defn w/o prototype.  */
1852 	  if (warning_at (DECL_SOURCE_LOCATION (newdecl), 0,
1853 			  "prototype specified for %q#D", newdecl))
1854 	    inform (DECL_SOURCE_LOCATION (olddecl),
1855 		    "previous non-prototype definition here");
1856 	}
1857       else if (VAR_OR_FUNCTION_DECL_P (olddecl)
1858 	       && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl))
1859 	{
1860 	  /* [dcl.link]
1861 	     If two declarations of the same function or object
1862 	     specify different linkage-specifications ..., the program
1863 	     is ill-formed.... Except for functions with C++ linkage,
1864 	     a function declaration without a linkage specification
1865 	     shall not precede the first linkage specification for
1866 	     that function.  A function can be declared without a
1867 	     linkage specification after an explicit linkage
1868 	     specification has been seen; the linkage explicitly
1869 	     specified in the earlier declaration is not affected by
1870 	     such a function declaration.
1871 
1872 	     DR 563 raises the question why the restrictions on
1873 	     functions should not also apply to objects.  Older
1874 	     versions of G++ silently ignore the linkage-specification
1875 	     for this example:
1876 
1877 	       namespace N {
1878                  extern int i;
1879    	         extern "C" int i;
1880                }
1881 
1882              which is clearly wrong.  Therefore, we now treat objects
1883 	     like functions.  */
1884 	  if (current_lang_depth () == 0)
1885 	    {
1886 	      /* There is no explicit linkage-specification, so we use
1887 		 the linkage from the previous declaration.  */
1888 	      retrofit_lang_decl (newdecl);
1889 	      SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
1890 	    }
1891 	  else
1892 	    {
1893 	      error ("conflicting declaration of %q+#D with %qL linkage",
1894 		     newdecl, DECL_LANGUAGE (newdecl));
1895 	      inform (DECL_SOURCE_LOCATION (olddecl),
1896 		      "previous declaration with %qL linkage",
1897 		      DECL_LANGUAGE (olddecl));
1898 	    }
1899 	}
1900 
1901       if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl))
1902 	;
1903       else if (TREE_CODE (olddecl) == FUNCTION_DECL)
1904 	{
1905 	  /* Note: free functions, as TEMPLATE_DECLs, are handled below.  */
1906 	  if (DECL_FUNCTION_MEMBER_P (olddecl)
1907 	      && (/* grokfndecl passes member function templates too
1908 		     as FUNCTION_DECLs.  */
1909 		  DECL_TEMPLATE_INFO (olddecl)
1910 		  /* C++11 8.3.6/6.
1911 		     Default arguments for a member function of a class
1912 		     template shall be specified on the initial declaration
1913 		     of the member function within the class template.  */
1914 		  || CLASSTYPE_TEMPLATE_INFO (CP_DECL_CONTEXT (olddecl))))
1915 	    check_redeclaration_no_default_args (newdecl);
1916 	  else
1917 	    {
1918 	      tree t1 = FUNCTION_FIRST_USER_PARMTYPE (olddecl);
1919 	      tree t2 = FUNCTION_FIRST_USER_PARMTYPE (newdecl);
1920 	      int i = 1;
1921 
1922 	      for (; t1 && t1 != void_list_node;
1923 		   t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++)
1924 		if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
1925 		  {
1926 		    if (simple_cst_equal (TREE_PURPOSE (t1),
1927 					  TREE_PURPOSE (t2)) == 1)
1928 		      {
1929 			if (permerror (input_location,
1930 				       "default argument given for parameter "
1931 				       "%d of %q#D", i, newdecl))
1932 			  inform (DECL_SOURCE_LOCATION (olddecl),
1933 				  "previous specification in %q#D here",
1934 				  olddecl);
1935 		      }
1936 		    else
1937 		      {
1938 			error ("default argument given for parameter %d "
1939 			       "of %q#D", i, newdecl);
1940 			inform (DECL_SOURCE_LOCATION (olddecl),
1941 				"previous specification in %q#D here",
1942 				olddecl);
1943 		      }
1944 		  }
1945 	    }
1946 	}
1947     }
1948 
1949   /* Do not merge an implicit typedef with an explicit one.  In:
1950 
1951        class A;
1952        ...
1953        typedef class A A __attribute__ ((foo));
1954 
1955      the attribute should apply only to the typedef.  */
1956   if (TREE_CODE (olddecl) == TYPE_DECL
1957       && (DECL_IMPLICIT_TYPEDEF_P (olddecl)
1958 	  || DECL_IMPLICIT_TYPEDEF_P (newdecl)))
1959     return NULL_TREE;
1960 
1961   /* If new decl is `static' and an `extern' was seen previously,
1962      warn about it.  */
1963   warn_extern_redeclared_static (newdecl, olddecl);
1964 
1965   if (!validate_constexpr_redeclaration (olddecl, newdecl))
1966     return error_mark_node;
1967 
1968   /* We have committed to returning 1 at this point.  */
1969   if (TREE_CODE (newdecl) == FUNCTION_DECL)
1970     {
1971       /* Now that functions must hold information normally held
1972 	 by field decls, there is extra work to do so that
1973 	 declaration information does not get destroyed during
1974 	 definition.  */
1975       if (DECL_VINDEX (olddecl))
1976 	DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl);
1977       if (DECL_CONTEXT (olddecl))
1978 	DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1979       DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl);
1980       DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1981       DECL_PURE_VIRTUAL_P (newdecl) |= DECL_PURE_VIRTUAL_P (olddecl);
1982       DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl);
1983       DECL_INVALID_OVERRIDER_P (newdecl) |= DECL_INVALID_OVERRIDER_P (olddecl);
1984       DECL_FINAL_P (newdecl) |= DECL_FINAL_P (olddecl);
1985       DECL_OVERRIDE_P (newdecl) |= DECL_OVERRIDE_P (olddecl);
1986       DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl);
1987       if (DECL_OVERLOADED_OPERATOR_P (olddecl))
1988 	DECL_OVERLOADED_OPERATOR_CODE_RAW (newdecl)
1989 	  = DECL_OVERLOADED_OPERATOR_CODE_RAW (olddecl);
1990       new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE;
1991 
1992       /* Optionally warn about more than one declaration for the same
1993 	 name, but don't warn about a function declaration followed by a
1994 	 definition.  */
1995       if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl)
1996 	  && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE)
1997 	  /* Don't warn about extern decl followed by definition.  */
1998 	  && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl))
1999 	  /* Don't warn about friends, let add_friend take care of it.  */
2000 	  && ! (newdecl_is_friend || DECL_FRIEND_P (olddecl))
2001 	  /* Don't warn about declaration followed by specialization.  */
2002 	  && (! DECL_TEMPLATE_SPECIALIZATION (newdecl)
2003 	      || DECL_TEMPLATE_SPECIALIZATION (olddecl)))
2004 	{
2005 	  if (warning_at (DECL_SOURCE_LOCATION (newdecl),
2006 			  OPT_Wredundant_decls,
2007 			  "redundant redeclaration of %qD in same scope",
2008 			  newdecl))
2009 	    inform (DECL_SOURCE_LOCATION (olddecl),
2010 		    "previous declaration of %qD", olddecl);
2011 	}
2012 
2013       if (!(DECL_TEMPLATE_INSTANTIATION (olddecl)
2014 	    && DECL_TEMPLATE_SPECIALIZATION (newdecl)))
2015 	{
2016 	  if (DECL_DELETED_FN (newdecl))
2017 	    {
2018 	      error ("deleted definition of %q+D", newdecl);
2019 	      inform (DECL_SOURCE_LOCATION (olddecl),
2020 		      "previous declaration of %qD", olddecl);
2021 	    }
2022 	  DECL_DELETED_FN (newdecl) |= DECL_DELETED_FN (olddecl);
2023 	}
2024     }
2025 
2026   /* Deal with C++: must preserve virtual function table size.  */
2027   if (TREE_CODE (olddecl) == TYPE_DECL)
2028     {
2029       tree newtype = TREE_TYPE (newdecl);
2030       tree oldtype = TREE_TYPE (olddecl);
2031 
2032       if (newtype != error_mark_node && oldtype != error_mark_node
2033 	  && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype))
2034 	CLASSTYPE_FRIEND_CLASSES (newtype)
2035 	  = CLASSTYPE_FRIEND_CLASSES (oldtype);
2036 
2037       DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl);
2038     }
2039 
2040   /* Copy all the DECL_... slots specified in the new decl except for
2041      any that we copy here from the old type.  */
2042   if (merge_attr)
2043     DECL_ATTRIBUTES (newdecl)
2044       = (*targetm.merge_decl_attributes) (olddecl, newdecl);
2045   else
2046     DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
2047 
2048   if (DECL_DECLARES_FUNCTION_P (olddecl) && DECL_DECLARES_FUNCTION_P (newdecl))
2049     {
2050       olddecl_friend = DECL_FRIEND_P (olddecl);
2051       hidden_friend = (DECL_ANTICIPATED (olddecl)
2052 		       && DECL_HIDDEN_FRIEND_P (olddecl)
2053 		       && newdecl_is_friend);
2054       if (!hidden_friend)
2055 	{
2056 	  DECL_ANTICIPATED (olddecl) = 0;
2057 	  DECL_HIDDEN_FRIEND_P (olddecl) = 0;
2058 	}
2059     }
2060 
2061   if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2062     {
2063       tree old_result;
2064       tree new_result;
2065       old_result = DECL_TEMPLATE_RESULT (olddecl);
2066       new_result = DECL_TEMPLATE_RESULT (newdecl);
2067       TREE_TYPE (olddecl) = TREE_TYPE (old_result);
2068       DECL_TEMPLATE_SPECIALIZATIONS (olddecl)
2069 	= chainon (DECL_TEMPLATE_SPECIALIZATIONS (olddecl),
2070 		   DECL_TEMPLATE_SPECIALIZATIONS (newdecl));
2071 
2072       DECL_ATTRIBUTES (old_result)
2073 	= (*targetm.merge_decl_attributes) (old_result, new_result);
2074 
2075       if (DECL_FUNCTION_TEMPLATE_P (newdecl))
2076 	{
2077 	  /* Per C++11 8.3.6/4, default arguments cannot be added in later
2078 	     declarations of a function template.  */
2079 	  if (DECL_SOURCE_LOCATION (newdecl)
2080 	      != DECL_SOURCE_LOCATION (olddecl))
2081 	    check_redeclaration_no_default_args (newdecl);
2082 
2083 	  check_default_args (newdecl);
2084 
2085 	  if (GNU_INLINE_P (old_result) != GNU_INLINE_P (new_result)
2086 	      && DECL_INITIAL (new_result))
2087 	    {
2088 	      if (DECL_INITIAL (old_result))
2089 		DECL_UNINLINABLE (old_result) = 1;
2090 	      else
2091 		DECL_UNINLINABLE (old_result) = DECL_UNINLINABLE (new_result);
2092 	      DECL_EXTERNAL (old_result) = DECL_EXTERNAL (new_result);
2093 	      DECL_NOT_REALLY_EXTERN (old_result)
2094 		= DECL_NOT_REALLY_EXTERN (new_result);
2095 	      DECL_INTERFACE_KNOWN (old_result)
2096 		= DECL_INTERFACE_KNOWN (new_result);
2097 	      DECL_DECLARED_INLINE_P (old_result)
2098 		= DECL_DECLARED_INLINE_P (new_result);
2099 	      DECL_DISREGARD_INLINE_LIMITS (old_result)
2100 	        |= DECL_DISREGARD_INLINE_LIMITS (new_result);
2101 
2102 	    }
2103 	  else
2104 	    {
2105 	      DECL_DECLARED_INLINE_P (old_result)
2106 		|= DECL_DECLARED_INLINE_P (new_result);
2107 	      DECL_DISREGARD_INLINE_LIMITS (old_result)
2108 	        |= DECL_DISREGARD_INLINE_LIMITS (new_result);
2109 	      check_redeclaration_exception_specification (newdecl, olddecl);
2110 
2111 	      merge_attribute_bits (new_result, old_result);
2112 	    }
2113 	}
2114 
2115       /* If the new declaration is a definition, update the file and
2116 	 line information on the declaration, and also make
2117 	 the old declaration the same definition.  */
2118       if (DECL_INITIAL (new_result) != NULL_TREE)
2119 	{
2120 	  DECL_SOURCE_LOCATION (olddecl)
2121 	    = DECL_SOURCE_LOCATION (old_result)
2122 	    = DECL_SOURCE_LOCATION (newdecl);
2123 	  DECL_INITIAL (old_result) = DECL_INITIAL (new_result);
2124 	  if (DECL_FUNCTION_TEMPLATE_P (newdecl))
2125 	    {
2126 	      tree parm;
2127 	      DECL_ARGUMENTS (old_result)
2128 		= DECL_ARGUMENTS (new_result);
2129 	      for (parm = DECL_ARGUMENTS (old_result); parm;
2130 		   parm = DECL_CHAIN (parm))
2131 		DECL_CONTEXT (parm) = old_result;
2132 	    }
2133 	}
2134 
2135       return olddecl;
2136     }
2137 
2138   if (types_match)
2139     {
2140       if (TREE_CODE (newdecl) == FUNCTION_DECL)
2141 	check_redeclaration_exception_specification (newdecl, olddecl);
2142 
2143       /* Automatically handles default parameters.  */
2144       tree oldtype = TREE_TYPE (olddecl);
2145       tree newtype;
2146 
2147       /* For typedefs use the old type, as the new type's DECL_NAME points
2148 	 at newdecl, which will be ggc_freed.  */
2149       if (TREE_CODE (newdecl) == TYPE_DECL)
2150 	{
2151 	  /* But NEWTYPE might have an attribute, honor that.  */
2152 	  tree tem = TREE_TYPE (newdecl);
2153 	  newtype = oldtype;
2154 
2155 	  if (TYPE_USER_ALIGN (tem))
2156 	    {
2157 	      if (TYPE_ALIGN (tem) > TYPE_ALIGN (newtype))
2158 		SET_TYPE_ALIGN (newtype, TYPE_ALIGN (tem));
2159 	      TYPE_USER_ALIGN (newtype) = true;
2160 	    }
2161 
2162 	  /* And remove the new type from the variants list.  */
2163 	  if (TYPE_NAME (TREE_TYPE (newdecl)) == newdecl)
2164 	    {
2165 	      tree remove = TREE_TYPE (newdecl);
2166 	      if (TYPE_MAIN_VARIANT (remove) == remove)
2167 		{
2168 		  gcc_assert (TYPE_NEXT_VARIANT (remove) == NULL_TREE);
2169 		  /* If remove is the main variant, no need to remove that
2170 		     from the list.  One of the DECL_ORIGINAL_TYPE
2171 		     variants, e.g. created for aligned attribute, might still
2172 		     refer to the newdecl TYPE_DECL though, so remove that one
2173 		     in that case.  */
2174 		  if (tree orig = DECL_ORIGINAL_TYPE (newdecl))
2175 		    if (orig != remove)
2176 		      for (tree t = TYPE_MAIN_VARIANT (orig); t;
2177 			   t = TYPE_MAIN_VARIANT (t))
2178 			if (TYPE_NAME (TYPE_NEXT_VARIANT (t)) == newdecl)
2179 			  {
2180 			    TYPE_NEXT_VARIANT (t)
2181 			      = TYPE_NEXT_VARIANT (TYPE_NEXT_VARIANT (t));
2182 			    break;
2183 			  }
2184 		}
2185 	      else
2186 		for (tree t = TYPE_MAIN_VARIANT (remove); ;
2187 		     t = TYPE_NEXT_VARIANT (t))
2188 		  if (TYPE_NEXT_VARIANT (t) == remove)
2189 		    {
2190 		      TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (remove);
2191 		      break;
2192 		    }
2193 	    }
2194 	}
2195       else if (merge_attr)
2196 	newtype = merge_types (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
2197       else
2198 	newtype = TREE_TYPE (newdecl);
2199 
2200       if (VAR_P (newdecl))
2201 	{
2202 	  DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl);
2203 	  /* For already initialized vars, TREE_READONLY could have been
2204 	     cleared in cp_finish_decl, because the var needs runtime
2205 	     initialization or destruction.  Make sure not to set
2206 	     TREE_READONLY on it again.  */
2207 	  if (DECL_INITIALIZED_P (olddecl)
2208 	      && !DECL_EXTERNAL (olddecl)
2209 	      && !TREE_READONLY (olddecl))
2210 	    TREE_READONLY (newdecl) = 0;
2211 	  DECL_INITIALIZED_P (newdecl) |= DECL_INITIALIZED_P (olddecl);
2212 	  DECL_NONTRIVIALLY_INITIALIZED_P (newdecl)
2213 	    |= DECL_NONTRIVIALLY_INITIALIZED_P (olddecl);
2214 	  if (DECL_DEPENDENT_INIT_P (olddecl))
2215 	    SET_DECL_DEPENDENT_INIT_P (newdecl, true);
2216 	  DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (newdecl)
2217 	    |= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (olddecl);
2218           if (DECL_CLASS_SCOPE_P (olddecl))
2219             DECL_DECLARED_CONSTEXPR_P (newdecl)
2220 	      |= DECL_DECLARED_CONSTEXPR_P (olddecl);
2221 
2222 	  /* Merge the threadprivate attribute from OLDDECL into NEWDECL.  */
2223 	  if (DECL_LANG_SPECIFIC (olddecl)
2224 	      && CP_DECL_THREADPRIVATE_P (olddecl))
2225 	    {
2226 	      /* Allocate a LANG_SPECIFIC structure for NEWDECL, if needed.  */
2227 	      retrofit_lang_decl (newdecl);
2228 	      CP_DECL_THREADPRIVATE_P (newdecl) = 1;
2229 	    }
2230 	}
2231 
2232       /* An explicit specialization of a function template or of a member
2233 	 function of a class template can be declared transaction_safe
2234 	 independently of whether the corresponding template entity is declared
2235 	 transaction_safe. */
2236       if (flag_tm && TREE_CODE (newdecl) == FUNCTION_DECL
2237 	  && DECL_TEMPLATE_INSTANTIATION (olddecl)
2238 	  && DECL_TEMPLATE_SPECIALIZATION (newdecl)
2239 	  && tx_safe_fn_type_p (newtype)
2240 	  && !tx_safe_fn_type_p (TREE_TYPE (newdecl)))
2241 	newtype = tx_unsafe_fn_variant (newtype);
2242 
2243       TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype;
2244 
2245       if (TREE_CODE (newdecl) == FUNCTION_DECL)
2246 	check_default_args (newdecl);
2247 
2248       /* Lay the type out, unless already done.  */
2249       if (! same_type_p (newtype, oldtype)
2250 	  && TREE_TYPE (newdecl) != error_mark_node
2251 	  && !(processing_template_decl && uses_template_parms (newdecl)))
2252 	layout_type (TREE_TYPE (newdecl));
2253 
2254       if ((VAR_P (newdecl)
2255 	   || TREE_CODE (newdecl) == PARM_DECL
2256 	   || TREE_CODE (newdecl) == RESULT_DECL
2257 	   || TREE_CODE (newdecl) == FIELD_DECL
2258 	   || TREE_CODE (newdecl) == TYPE_DECL)
2259 	  && !(processing_template_decl && uses_template_parms (newdecl)))
2260 	layout_decl (newdecl, 0);
2261 
2262       /* Merge deprecatedness.  */
2263       if (TREE_DEPRECATED (newdecl))
2264 	TREE_DEPRECATED (olddecl) = 1;
2265 
2266       /* Preserve function specific target and optimization options */
2267       if (TREE_CODE (newdecl) == FUNCTION_DECL)
2268 	{
2269 	  if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
2270 	      && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
2271 	    DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
2272 	      = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
2273 
2274 	  if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
2275 	      && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
2276 	    DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
2277 	      = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
2278 	}
2279       else
2280 	{
2281 	  /* Merge the const type qualifier.  */
2282 	  if (TREE_READONLY (newdecl))
2283 	    TREE_READONLY (olddecl) = 1;
2284 	  /* Merge the volatile type qualifier.  */
2285 	  if (TREE_THIS_VOLATILE (newdecl))
2286 	    TREE_THIS_VOLATILE (olddecl) = 1;
2287 	}
2288 
2289       /* Merge the initialization information.  */
2290       if (DECL_INITIAL (newdecl) == NULL_TREE
2291 	  && DECL_INITIAL (olddecl) != NULL_TREE)
2292 	{
2293 	  DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2294 	  DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2295 	  if (TREE_CODE (newdecl) == FUNCTION_DECL)
2296 	    {
2297 	      DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
2298 	      DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
2299 	    }
2300 	}
2301 
2302       if (TREE_CODE (newdecl) == FUNCTION_DECL)
2303 	{
2304 	  DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
2305 	    |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
2306 	  DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
2307 	  DECL_IS_OPERATOR_NEW (newdecl) |= DECL_IS_OPERATOR_NEW (olddecl);
2308 	  DECL_LOOPING_CONST_OR_PURE_P (newdecl)
2309 	    |= DECL_LOOPING_CONST_OR_PURE_P (olddecl);
2310 
2311 	  if (merge_attr)
2312 	    merge_attribute_bits (newdecl, olddecl);
2313 	  else
2314 	    {
2315 	      /* Merge the noreturn bit.  */
2316 	      TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
2317 	      TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
2318 	      TREE_NOTHROW (olddecl) = TREE_NOTHROW (newdecl);
2319 	      DECL_IS_MALLOC (olddecl) = DECL_IS_MALLOC (newdecl);
2320 	      DECL_PURE_P (olddecl) = DECL_PURE_P (newdecl);
2321 	    }
2322 	  /* Keep the old RTL.  */
2323 	  COPY_DECL_RTL (olddecl, newdecl);
2324 	}
2325       else if (VAR_P (newdecl)
2326 	       && (DECL_SIZE (olddecl) || !DECL_SIZE (newdecl)))
2327 	{
2328 	  /* Keep the old RTL.  We cannot keep the old RTL if the old
2329 	     declaration was for an incomplete object and the new
2330 	     declaration is not since many attributes of the RTL will
2331 	     change.  */
2332 	  COPY_DECL_RTL (olddecl, newdecl);
2333 	}
2334     }
2335   /* If cannot merge, then use the new type and qualifiers,
2336      and don't preserve the old rtl.  */
2337   else
2338     {
2339       /* Clean out any memory we had of the old declaration.  */
2340       tree oldstatic = value_member (olddecl, static_aggregates);
2341       if (oldstatic)
2342 	TREE_VALUE (oldstatic) = error_mark_node;
2343 
2344       TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
2345       TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
2346       TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
2347       TREE_NOTHROW (olddecl) = TREE_NOTHROW (newdecl);
2348       TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
2349     }
2350 
2351   /* Merge the storage class information.  */
2352   merge_weak (newdecl, olddecl);
2353 
2354   DECL_DEFER_OUTPUT (newdecl) |= DECL_DEFER_OUTPUT (olddecl);
2355   TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
2356   TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl);
2357   if (! DECL_EXTERNAL (olddecl))
2358     DECL_EXTERNAL (newdecl) = 0;
2359   if (! DECL_COMDAT (olddecl))
2360     DECL_COMDAT (newdecl) = 0;
2361 
2362   new_template_info = NULL_TREE;
2363   if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl))
2364     {
2365       bool new_redefines_gnu_inline = false;
2366 
2367       if (new_defines_function
2368 	  && ((DECL_INTERFACE_KNOWN (olddecl)
2369 	       && TREE_CODE (olddecl) == FUNCTION_DECL)
2370 	      || (TREE_CODE (olddecl) == TEMPLATE_DECL
2371 		  && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl))
2372 		      == FUNCTION_DECL))))
2373 	{
2374 	  tree fn = olddecl;
2375 
2376 	  if (TREE_CODE (fn) == TEMPLATE_DECL)
2377 	    fn = DECL_TEMPLATE_RESULT (olddecl);
2378 
2379 	  new_redefines_gnu_inline = GNU_INLINE_P (fn) && DECL_INITIAL (fn);
2380 	}
2381 
2382       if (!new_redefines_gnu_inline)
2383 	{
2384 	  DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl);
2385 	  DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl);
2386 	  DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl);
2387 	}
2388       DECL_TEMPLATE_INSTANTIATED (newdecl)
2389 	|= DECL_TEMPLATE_INSTANTIATED (olddecl);
2390       DECL_ODR_USED (newdecl) |= DECL_ODR_USED (olddecl);
2391 
2392       /* If the OLDDECL is an instantiation and/or specialization,
2393 	 then the NEWDECL must be too.  But, it may not yet be marked
2394 	 as such if the caller has created NEWDECL, but has not yet
2395 	 figured out that it is a redeclaration.  */
2396       if (!DECL_USE_TEMPLATE (newdecl))
2397 	DECL_USE_TEMPLATE (newdecl) = DECL_USE_TEMPLATE (olddecl);
2398 
2399       /* Don't really know how much of the language-specific
2400 	 values we should copy from old to new.  */
2401       DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl);
2402       DECL_REPO_AVAILABLE_P (newdecl) = DECL_REPO_AVAILABLE_P (olddecl);
2403       DECL_INITIALIZED_IN_CLASS_P (newdecl)
2404 	|= DECL_INITIALIZED_IN_CLASS_P (olddecl);
2405 
2406       if (LANG_DECL_HAS_MIN (newdecl))
2407 	{
2408 	  DECL_LANG_SPECIFIC (newdecl)->u.min.u2 =
2409 	    DECL_LANG_SPECIFIC (olddecl)->u.min.u2;
2410 	  if (DECL_TEMPLATE_INFO (newdecl))
2411 	    {
2412 	      new_template_info = DECL_TEMPLATE_INFO (newdecl);
2413 	      if (DECL_TEMPLATE_INSTANTIATION (olddecl)
2414 		  && DECL_TEMPLATE_SPECIALIZATION (newdecl))
2415 		/* Remember the presence of explicit specialization args.  */
2416 		TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (olddecl))
2417 		  = TINFO_USED_TEMPLATE_ID (new_template_info);
2418 	    }
2419 	  DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl);
2420 	}
2421       /* Only functions have these fields.  */
2422       if (DECL_DECLARES_FUNCTION_P (newdecl))
2423 	{
2424 	  DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl);
2425 	  DECL_BEFRIENDING_CLASSES (newdecl)
2426 	    = chainon (DECL_BEFRIENDING_CLASSES (newdecl),
2427 		       DECL_BEFRIENDING_CLASSES (olddecl));
2428 	  /* DECL_THUNKS is only valid for virtual functions,
2429 	     otherwise it is a DECL_FRIEND_CONTEXT.  */
2430 	  if (DECL_VIRTUAL_P (newdecl))
2431 	    SET_DECL_THUNKS (newdecl, DECL_THUNKS (olddecl));
2432 	}
2433       /* Only variables have this field.  */
2434       else if (VAR_P (newdecl)
2435 	       && VAR_HAD_UNKNOWN_BOUND (olddecl))
2436 	SET_VAR_HAD_UNKNOWN_BOUND (newdecl);
2437     }
2438 
2439   if (TREE_CODE (newdecl) == FUNCTION_DECL)
2440     {
2441       tree parm;
2442 
2443       /* Merge parameter attributes. */
2444       tree oldarg, newarg;
2445       for (oldarg = DECL_ARGUMENTS(olddecl),
2446                newarg = DECL_ARGUMENTS(newdecl);
2447            oldarg && newarg;
2448            oldarg = DECL_CHAIN(oldarg), newarg = DECL_CHAIN(newarg)) {
2449           DECL_ATTRIBUTES (newarg)
2450               = (*targetm.merge_decl_attributes) (oldarg, newarg);
2451           DECL_ATTRIBUTES (oldarg) = DECL_ATTRIBUTES (newarg);
2452       }
2453 
2454       if (DECL_TEMPLATE_INSTANTIATION (olddecl)
2455 	  && !DECL_TEMPLATE_INSTANTIATION (newdecl))
2456 	{
2457 	  /* If newdecl is not a specialization, then it is not a
2458 	     template-related function at all.  And that means that we
2459 	     should have exited above, returning 0.  */
2460 	  gcc_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl));
2461 
2462 	  if (DECL_ODR_USED (olddecl))
2463 	    /* From [temp.expl.spec]:
2464 
2465 	       If a template, a member template or the member of a class
2466 	       template is explicitly specialized then that
2467 	       specialization shall be declared before the first use of
2468 	       that specialization that would cause an implicit
2469 	       instantiation to take place, in every translation unit in
2470 	       which such a use occurs.  */
2471 	    error ("explicit specialization of %qD after first use",
2472 		      olddecl);
2473 
2474 	  SET_DECL_TEMPLATE_SPECIALIZATION (olddecl);
2475 	  DECL_COMDAT (newdecl) = (TREE_PUBLIC (newdecl)
2476 				   && DECL_DECLARED_INLINE_P (newdecl));
2477 
2478 	  /* Don't propagate visibility from the template to the
2479 	     specialization here.  We'll do that in determine_visibility if
2480 	     appropriate.  */
2481 	  DECL_VISIBILITY_SPECIFIED (olddecl) = 0;
2482 
2483 	  /* [temp.expl.spec/14] We don't inline explicit specialization
2484 	     just because the primary template says so.  */
2485 	  gcc_assert (!merge_attr);
2486 
2487 	  DECL_DECLARED_INLINE_P (olddecl)
2488 	    = DECL_DECLARED_INLINE_P (newdecl);
2489 
2490 	  DECL_DISREGARD_INLINE_LIMITS (olddecl)
2491 	    = DECL_DISREGARD_INLINE_LIMITS (newdecl);
2492 
2493 	  DECL_UNINLINABLE (olddecl) = DECL_UNINLINABLE (newdecl);
2494 	}
2495       else if (new_defines_function && DECL_INITIAL (olddecl))
2496 	{
2497 	  /* Never inline re-defined extern inline functions.
2498 	     FIXME: this could be better handled by keeping both
2499 	     function as separate declarations.  */
2500 	  DECL_UNINLINABLE (newdecl) = 1;
2501 	}
2502       else
2503 	{
2504 	  if (DECL_PENDING_INLINE_P (olddecl))
2505 	    {
2506 	      DECL_PENDING_INLINE_P (newdecl) = 1;
2507 	      DECL_PENDING_INLINE_INFO (newdecl)
2508 		= DECL_PENDING_INLINE_INFO (olddecl);
2509 	    }
2510 	  else if (DECL_PENDING_INLINE_P (newdecl))
2511 	    ;
2512 	  else if (DECL_SAVED_FUNCTION_DATA (newdecl) == NULL)
2513 	    DECL_SAVED_FUNCTION_DATA (newdecl)
2514 	      = DECL_SAVED_FUNCTION_DATA (olddecl);
2515 
2516 	  DECL_DECLARED_INLINE_P (newdecl) |= DECL_DECLARED_INLINE_P (olddecl);
2517 
2518 	  DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
2519 	    = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
2520 
2521 	  DECL_DISREGARD_INLINE_LIMITS (newdecl)
2522 	    = DECL_DISREGARD_INLINE_LIMITS (olddecl)
2523 	    = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
2524 	       || DECL_DISREGARD_INLINE_LIMITS (olddecl));
2525 	}
2526 
2527       /* Preserve abstractness on cloned [cd]tors.  */
2528       DECL_ABSTRACT_P (newdecl) = DECL_ABSTRACT_P (olddecl);
2529 
2530       /* Update newdecl's parms to point at olddecl.  */
2531       for (parm = DECL_ARGUMENTS (newdecl); parm;
2532 	   parm = DECL_CHAIN (parm))
2533 	DECL_CONTEXT (parm) = olddecl;
2534 
2535       if (! types_match)
2536 	{
2537 	  SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
2538 	  COPY_DECL_ASSEMBLER_NAME (newdecl, olddecl);
2539 	  COPY_DECL_RTL (newdecl, olddecl);
2540 	}
2541       if (! types_match || new_defines_function)
2542 	{
2543 	  /* These need to be copied so that the names are available.
2544 	     Note that if the types do match, we'll preserve inline
2545 	     info and other bits, but if not, we won't.  */
2546 	  DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl);
2547 	  DECL_RESULT (olddecl) = DECL_RESULT (newdecl);
2548 	}
2549       /* If redeclaring a builtin function, it stays built in
2550 	 if newdecl is a gnu_inline definition, or if newdecl is just
2551 	 a declaration.  */
2552       if (DECL_BUILT_IN (olddecl)
2553 	  && (new_defines_function ? GNU_INLINE_P (newdecl) : types_match))
2554 	{
2555 	  DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
2556 	  DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
2557 	  /* If we're keeping the built-in definition, keep the rtl,
2558 	     regardless of declaration matches.  */
2559 	  COPY_DECL_RTL (olddecl, newdecl);
2560 	  if (DECL_BUILT_IN_CLASS (newdecl) == BUILT_IN_NORMAL)
2561 	    {
2562 	      enum built_in_function fncode = DECL_FUNCTION_CODE (newdecl);
2563 	      switch (fncode)
2564 		{
2565 		  /* If a compatible prototype of these builtin functions
2566 		     is seen, assume the runtime implements it with the
2567 		     expected semantics.  */
2568 		case BUILT_IN_STPCPY:
2569 		  if (builtin_decl_explicit_p (fncode))
2570 		    set_builtin_decl_implicit_p (fncode, true);
2571 		  break;
2572 		default:
2573 		  if (builtin_decl_explicit_p (fncode))
2574 		    set_builtin_decl_declared_p (fncode, true);
2575 		  break;
2576 		}
2577 	    }
2578 
2579 	  copy_attributes_to_builtin (newdecl);
2580 	}
2581       if (new_defines_function)
2582 	/* If defining a function declared with other language
2583 	   linkage, use the previously declared language linkage.  */
2584 	SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
2585       else if (types_match)
2586 	{
2587 	  DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
2588 	  /* Don't clear out the arguments if we're just redeclaring a
2589 	     function.  */
2590 	  if (DECL_ARGUMENTS (olddecl))
2591 	    DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
2592 	}
2593     }
2594   else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
2595     NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl);
2596 
2597   /* Now preserve various other info from the definition.  */
2598   TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl);
2599   TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl);
2600   DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
2601   COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
2602 
2603   /* Warn about conflicting visibility specifications.  */
2604   if (DECL_VISIBILITY_SPECIFIED (olddecl)
2605       && DECL_VISIBILITY_SPECIFIED (newdecl)
2606       && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
2607     {
2608       if (warning_at (DECL_SOURCE_LOCATION (newdecl), OPT_Wattributes,
2609 		      "%qD: visibility attribute ignored because it "
2610 		      "conflicts with previous declaration", newdecl))
2611 	inform (DECL_SOURCE_LOCATION (olddecl),
2612 		"previous declaration of %qD", olddecl);
2613     }
2614   /* Choose the declaration which specified visibility.  */
2615   if (DECL_VISIBILITY_SPECIFIED (olddecl))
2616     {
2617       DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
2618       DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
2619     }
2620   /* Init priority used to be merged from newdecl to olddecl by the memcpy,
2621      so keep this behavior.  */
2622   if (VAR_P (newdecl) && DECL_HAS_INIT_PRIORITY_P (newdecl))
2623     {
2624       SET_DECL_INIT_PRIORITY (olddecl, DECL_INIT_PRIORITY (newdecl));
2625       DECL_HAS_INIT_PRIORITY_P (olddecl) = 1;
2626     }
2627   /* Likewise for DECL_ALIGN, DECL_USER_ALIGN and DECL_PACKED.  */
2628   if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
2629     {
2630       SET_DECL_ALIGN (newdecl, DECL_ALIGN (olddecl));
2631       DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
2632     }
2633   DECL_USER_ALIGN (olddecl) = DECL_USER_ALIGN (newdecl);
2634   if (DECL_WARN_IF_NOT_ALIGN (olddecl)
2635       > DECL_WARN_IF_NOT_ALIGN (newdecl))
2636     SET_DECL_WARN_IF_NOT_ALIGN (newdecl,
2637 				DECL_WARN_IF_NOT_ALIGN (olddecl));
2638   if (TREE_CODE (newdecl) == FIELD_DECL)
2639     DECL_PACKED (olddecl) = DECL_PACKED (newdecl);
2640 
2641   /* The DECL_LANG_SPECIFIC information in OLDDECL will be replaced
2642      with that from NEWDECL below.  */
2643   if (DECL_LANG_SPECIFIC (olddecl))
2644     {
2645       gcc_assert (DECL_LANG_SPECIFIC (olddecl)
2646 		  != DECL_LANG_SPECIFIC (newdecl));
2647       ggc_free (DECL_LANG_SPECIFIC (olddecl));
2648     }
2649 
2650   /* Merge the USED information.  */
2651   if (TREE_USED (olddecl))
2652     TREE_USED (newdecl) = 1;
2653   else if (TREE_USED (newdecl))
2654     TREE_USED (olddecl) = 1;
2655   if (VAR_P (newdecl))
2656     {
2657       if (DECL_READ_P (olddecl))
2658 	DECL_READ_P (newdecl) = 1;
2659       else if (DECL_READ_P (newdecl))
2660 	DECL_READ_P (olddecl) = 1;
2661     }
2662   if (DECL_PRESERVE_P (olddecl))
2663     DECL_PRESERVE_P (newdecl) = 1;
2664   else if (DECL_PRESERVE_P (newdecl))
2665     DECL_PRESERVE_P (olddecl) = 1;
2666 
2667   /* Merge the DECL_FUNCTION_VERSIONED information.  newdecl will be copied
2668      to olddecl and deleted.  */
2669   if (TREE_CODE (newdecl) == FUNCTION_DECL
2670       && DECL_FUNCTION_VERSIONED (olddecl))
2671     {
2672       /* Set the flag for newdecl so that it gets copied to olddecl.  */
2673       DECL_FUNCTION_VERSIONED (newdecl) = 1;
2674       /* newdecl will be purged after copying to olddecl and is no longer
2675          a version.  */
2676       cgraph_node::delete_function_version_by_decl (newdecl);
2677     }
2678 
2679   if (TREE_CODE (newdecl) == FUNCTION_DECL)
2680     {
2681       int function_size;
2682       struct symtab_node *snode = symtab_node::get (olddecl);
2683 
2684       function_size = sizeof (struct tree_decl_common);
2685 
2686       memcpy ((char *) olddecl + sizeof (struct tree_common),
2687 	      (char *) newdecl + sizeof (struct tree_common),
2688 	      function_size - sizeof (struct tree_common));
2689 
2690       memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2691 	      (char *) newdecl + sizeof (struct tree_decl_common),
2692 	      sizeof (struct tree_function_decl) - sizeof (struct tree_decl_common));
2693 
2694       /* Preserve symtab node mapping.  */
2695       olddecl->decl_with_vis.symtab_node = snode;
2696 
2697       if (new_template_info)
2698 	/* If newdecl is a template instantiation, it is possible that
2699 	   the following sequence of events has occurred:
2700 
2701 	   o A friend function was declared in a class template.  The
2702 	   class template was instantiated.
2703 
2704 	   o The instantiation of the friend declaration was
2705 	   recorded on the instantiation list, and is newdecl.
2706 
2707 	   o Later, however, instantiate_class_template called pushdecl
2708 	   on the newdecl to perform name injection.  But, pushdecl in
2709 	   turn called duplicate_decls when it discovered that another
2710 	   declaration of a global function with the same name already
2711 	   existed.
2712 
2713 	   o Here, in duplicate_decls, we decided to clobber newdecl.
2714 
2715 	   If we're going to do that, we'd better make sure that
2716 	   olddecl, and not newdecl, is on the list of
2717 	   instantiations so that if we try to do the instantiation
2718 	   again we won't get the clobbered declaration.  */
2719 	reregister_specialization (newdecl,
2720 				   new_template_info,
2721 				   olddecl);
2722     }
2723   else
2724     {
2725       size_t size = tree_code_size (TREE_CODE (newdecl));
2726 
2727       memcpy ((char *) olddecl + sizeof (struct tree_common),
2728 	      (char *) newdecl + sizeof (struct tree_common),
2729 	      sizeof (struct tree_decl_common) - sizeof (struct tree_common));
2730       switch (TREE_CODE (newdecl))
2731 	{
2732 	case LABEL_DECL:
2733 	case VAR_DECL:
2734 	case RESULT_DECL:
2735 	case PARM_DECL:
2736 	case FIELD_DECL:
2737 	case TYPE_DECL:
2738 	case CONST_DECL:
2739 	  {
2740             struct symtab_node *snode = NULL;
2741 
2742 	    if (VAR_P (newdecl)
2743 		&& (TREE_STATIC (olddecl) || TREE_PUBLIC (olddecl)
2744 		    || DECL_EXTERNAL (olddecl)))
2745 	      snode = symtab_node::get (olddecl);
2746 	    memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2747 		    (char *) newdecl + sizeof (struct tree_decl_common),
2748 		    size - sizeof (struct tree_decl_common)
2749 		    + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
2750 	    if (VAR_P (newdecl))
2751 	      olddecl->decl_with_vis.symtab_node = snode;
2752 	  }
2753 	  break;
2754 	default:
2755 	  memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2756 		  (char *) newdecl + sizeof (struct tree_decl_common),
2757 		  sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common)
2758 		  + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
2759 	  break;
2760 	}
2761     }
2762 
2763   if (VAR_OR_FUNCTION_DECL_P (newdecl))
2764     {
2765       if (DECL_EXTERNAL (olddecl)
2766 	  || TREE_PUBLIC (olddecl)
2767 	  || TREE_STATIC (olddecl))
2768 	{
2769 	  /* Merge the section attribute.
2770 	     We want to issue an error if the sections conflict but that must be
2771 	     done later in decl_attributes since we are called before attributes
2772 	     are assigned.  */
2773 	  if (DECL_SECTION_NAME (newdecl) != NULL)
2774 	    set_decl_section_name (olddecl, DECL_SECTION_NAME (newdecl));
2775 
2776 	  if (DECL_ONE_ONLY (newdecl))
2777 	    {
2778 	      struct symtab_node *oldsym, *newsym;
2779 	      if (TREE_CODE (olddecl) == FUNCTION_DECL)
2780 		oldsym = cgraph_node::get_create (olddecl);
2781 	      else
2782 		oldsym = varpool_node::get_create (olddecl);
2783 	      newsym = symtab_node::get (newdecl);
2784 	      oldsym->set_comdat_group (newsym->get_comdat_group ());
2785 	    }
2786 	}
2787 
2788       if (VAR_P (newdecl)
2789 	  && CP_DECL_THREAD_LOCAL_P (newdecl))
2790 	{
2791 	  CP_DECL_THREAD_LOCAL_P (olddecl) = true;
2792 	  if (!processing_template_decl)
2793 	    set_decl_tls_model (olddecl, DECL_TLS_MODEL (newdecl));
2794 	}
2795     }
2796 
2797   DECL_UID (olddecl) = olddecl_uid;
2798   if (olddecl_friend)
2799     DECL_FRIEND_P (olddecl) = 1;
2800   if (hidden_friend)
2801     {
2802       DECL_ANTICIPATED (olddecl) = 1;
2803       DECL_HIDDEN_FRIEND_P (olddecl) = 1;
2804     }
2805 
2806   /* NEWDECL contains the merged attribute lists.
2807      Update OLDDECL to be the same.  */
2808   DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
2809 
2810   /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
2811     so that encode_section_info has a chance to look at the new decl
2812     flags and attributes.  */
2813   if (DECL_RTL_SET_P (olddecl)
2814       && (TREE_CODE (olddecl) == FUNCTION_DECL
2815 	  || (VAR_P (olddecl)
2816 	      && TREE_STATIC (olddecl))))
2817     make_decl_rtl (olddecl);
2818 
2819   /* The NEWDECL will no longer be needed.  Because every out-of-class
2820      declaration of a member results in a call to duplicate_decls,
2821      freeing these nodes represents in a significant savings.
2822 
2823      Before releasing the node, be sore to remove function from symbol
2824      table that might have been inserted there to record comdat group.
2825      Be sure to however do not free DECL_STRUCT_FUNCTION because this
2826      structure is shared in between newdecl and oldecl.  */
2827   if (TREE_CODE (newdecl) == FUNCTION_DECL)
2828     DECL_STRUCT_FUNCTION (newdecl) = NULL;
2829   if (VAR_OR_FUNCTION_DECL_P (newdecl))
2830     {
2831       struct symtab_node *snode = symtab_node::get (newdecl);
2832       if (snode)
2833 	snode->remove ();
2834     }
2835 
2836   /* Remove the associated constraints for newdecl, if any, before
2837      reclaiming memory. */
2838   if (flag_concepts)
2839     remove_constraints (newdecl);
2840 
2841   ggc_free (newdecl);
2842 
2843   return olddecl;
2844 }
2845 
2846 /* Return zero if the declaration NEWDECL is valid
2847    when the declaration OLDDECL (assumed to be for the same name)
2848    has already been seen.
2849    Otherwise return an error message format string with a %s
2850    where the identifier should go.  */
2851 
2852 static const char *
redeclaration_error_message(tree newdecl,tree olddecl)2853 redeclaration_error_message (tree newdecl, tree olddecl)
2854 {
2855   if (TREE_CODE (newdecl) == TYPE_DECL)
2856     {
2857       /* Because C++ can put things into name space for free,
2858 	 constructs like "typedef struct foo { ... } foo"
2859 	 would look like an erroneous redeclaration.  */
2860       if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl)))
2861 	return NULL;
2862       else
2863 	return G_("redefinition of %q#D");
2864     }
2865   else if (TREE_CODE (newdecl) == FUNCTION_DECL)
2866     {
2867       /* If this is a pure function, its olddecl will actually be
2868 	 the original initialization to `0' (which we force to call
2869 	 abort()).  Don't complain about redefinition in this case.  */
2870       if (DECL_LANG_SPECIFIC (olddecl) && DECL_PURE_VIRTUAL_P (olddecl)
2871 	  && DECL_INITIAL (olddecl) == NULL_TREE)
2872 	return NULL;
2873 
2874       /* If both functions come from different namespaces, this is not
2875 	 a redeclaration - this is a conflict with a used function.  */
2876       if (DECL_NAMESPACE_SCOPE_P (olddecl)
2877 	  && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl)
2878 	  && ! decls_match (olddecl, newdecl))
2879 	return G_("%qD conflicts with used function");
2880 
2881       /* We'll complain about linkage mismatches in
2882 	 warn_extern_redeclared_static.  */
2883 
2884       /* Defining the same name twice is no good.  */
2885       if (decl_defined_p (olddecl)
2886 	  && decl_defined_p (newdecl))
2887 	{
2888 	  if (DECL_NAME (olddecl) == NULL_TREE)
2889 	    return G_("%q#D not declared in class");
2890 	  else if (!GNU_INLINE_P (olddecl)
2891 		   || GNU_INLINE_P (newdecl))
2892 	    return G_("redefinition of %q#D");
2893 	}
2894 
2895       if (DECL_DECLARED_INLINE_P (olddecl) && DECL_DECLARED_INLINE_P (newdecl))
2896 	{
2897 	  bool olda = GNU_INLINE_P (olddecl);
2898 	  bool newa = GNU_INLINE_P (newdecl);
2899 
2900 	  if (olda != newa)
2901 	    {
2902 	      if (newa)
2903 		return G_("%q+D redeclared inline with "
2904 			  "%<gnu_inline%> attribute");
2905 	      else
2906 		return G_("%q+D redeclared inline without "
2907 			  "%<gnu_inline%> attribute");
2908 	    }
2909 	}
2910 
2911       check_abi_tag_redeclaration
2912 	(olddecl, lookup_attribute ("abi_tag", DECL_ATTRIBUTES (olddecl)),
2913 	 lookup_attribute ("abi_tag", DECL_ATTRIBUTES (newdecl)));
2914 
2915       return NULL;
2916     }
2917   else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2918     {
2919       tree nt, ot;
2920 
2921       if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
2922 	{
2923 	  if (COMPLETE_TYPE_P (TREE_TYPE (newdecl))
2924 	      && COMPLETE_TYPE_P (TREE_TYPE (olddecl)))
2925 	    return G_("redefinition of %q#D");
2926 	  return NULL;
2927 	}
2928 
2929       if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) != FUNCTION_DECL
2930 	  || (DECL_TEMPLATE_RESULT (newdecl)
2931 	      == DECL_TEMPLATE_RESULT (olddecl)))
2932 	return NULL;
2933 
2934       nt = DECL_TEMPLATE_RESULT (newdecl);
2935       if (DECL_TEMPLATE_INFO (nt))
2936 	nt = DECL_TEMPLATE_RESULT (template_for_substitution (nt));
2937       ot = DECL_TEMPLATE_RESULT (olddecl);
2938       if (DECL_TEMPLATE_INFO (ot))
2939 	ot = DECL_TEMPLATE_RESULT (template_for_substitution (ot));
2940       if (DECL_INITIAL (nt) && DECL_INITIAL (ot)
2941 	  && (!GNU_INLINE_P (ot) || GNU_INLINE_P (nt)))
2942 	return G_("redefinition of %q#D");
2943 
2944       if (DECL_DECLARED_INLINE_P (ot) && DECL_DECLARED_INLINE_P (nt))
2945 	{
2946 	  bool olda = GNU_INLINE_P (ot);
2947 	  bool newa = GNU_INLINE_P (nt);
2948 
2949 	  if (olda != newa)
2950 	    {
2951 	      if (newa)
2952 		return G_("%q+D redeclared inline with "
2953 			  "%<gnu_inline%> attribute");
2954 	      else
2955 		return G_("%q+D redeclared inline without "
2956 		     	  "%<gnu_inline%> attribute");
2957 	    }
2958 	}
2959 
2960       /* Core issue #226 (C++0x):
2961 
2962            If a friend function template declaration specifies a
2963            default template-argument, that declaration shall be a
2964            definition and shall be the only declaration of the
2965            function template in the translation unit.  */
2966       if ((cxx_dialect != cxx98)
2967           && TREE_CODE (ot) == FUNCTION_DECL && DECL_FRIEND_P (ot)
2968           && !check_default_tmpl_args (nt, DECL_TEMPLATE_PARMS (newdecl),
2969                                        /*is_primary=*/true,
2970 				       /*is_partial=*/false,
2971                                        /*is_friend_decl=*/2))
2972         return G_("redeclaration of friend %q#D "
2973 	 	  "may not have default template arguments");
2974 
2975       return NULL;
2976     }
2977   else if (VAR_P (newdecl)
2978 	   && CP_DECL_THREAD_LOCAL_P (newdecl) != CP_DECL_THREAD_LOCAL_P (olddecl)
2979 	   && (! DECL_LANG_SPECIFIC (olddecl)
2980 	       || ! CP_DECL_THREADPRIVATE_P (olddecl)
2981 	       || CP_DECL_THREAD_LOCAL_P (newdecl)))
2982     {
2983       /* Only variables can be thread-local, and all declarations must
2984 	 agree on this property.  */
2985       if (CP_DECL_THREAD_LOCAL_P (newdecl))
2986 	return G_("thread-local declaration of %q#D follows "
2987 	          "non-thread-local declaration");
2988       else
2989 	return G_("non-thread-local declaration of %q#D follows "
2990 	          "thread-local declaration");
2991     }
2992   else if (toplevel_bindings_p () || DECL_NAMESPACE_SCOPE_P (newdecl))
2993     {
2994       /* The objects have been declared at namespace scope.  If either
2995 	 is a member of an anonymous union, then this is an invalid
2996 	 redeclaration.  For example:
2997 
2998 	   int i;
2999 	   union { int i; };
3000 
3001 	   is invalid.  */
3002       if ((VAR_P (newdecl) && DECL_ANON_UNION_VAR_P (newdecl))
3003 	  || (VAR_P (olddecl) && DECL_ANON_UNION_VAR_P (olddecl)))
3004 	return G_("redeclaration of %q#D");
3005       /* If at least one declaration is a reference, there is no
3006 	 conflict.  For example:
3007 
3008 	   int i = 3;
3009 	   extern int i;
3010 
3011 	 is valid.  */
3012       if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
3013 	return NULL;
3014 
3015       /* Static data member declared outside a class definition
3016 	 if the variable is defined within the class with constexpr
3017 	 specifier is declaration rather than definition (and
3018 	 deprecated).  */
3019       if (cxx_dialect >= cxx17
3020 	  && VAR_P (olddecl)
3021 	  && DECL_CLASS_SCOPE_P (olddecl)
3022 	  && DECL_DECLARED_CONSTEXPR_P (olddecl)
3023 	  && !DECL_INITIAL (newdecl))
3024 	{
3025 	  DECL_EXTERNAL (newdecl) = 1;
3026 	  /* For now, only warn with explicit -Wdeprecated.  */
3027 	  if (global_options_set.x_warn_deprecated
3028 	      && warning_at (DECL_SOURCE_LOCATION (newdecl), OPT_Wdeprecated,
3029 			     "redundant redeclaration of %<constexpr%> static "
3030 			     "data member %qD", newdecl))
3031 	    inform (DECL_SOURCE_LOCATION (olddecl),
3032 		    "previous declaration of %qD", olddecl);
3033 	  return NULL;
3034 	}
3035 
3036       /* Reject two definitions.  */
3037       return G_("redefinition of %q#D");
3038     }
3039   else
3040     {
3041       /* Objects declared with block scope:  */
3042       /* Reject two definitions, and reject a definition
3043 	 together with an external reference.  */
3044       if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl)))
3045 	return G_("redeclaration of %q#D");
3046       return NULL;
3047     }
3048 }
3049 
3050 
3051 /* Hash and equality functions for the named_label table.  */
3052 
3053 hashval_t
hash(const value_type entry)3054 named_label_hash::hash (const value_type entry)
3055 {
3056   return IDENTIFIER_HASH_VALUE (entry->name);
3057 }
3058 
3059 bool
equal(const value_type entry,compare_type name)3060 named_label_hash::equal (const value_type entry, compare_type name)
3061 {
3062   return name == entry->name;
3063 }
3064 
3065 /* Look for a label named ID in the current function.  If one cannot
3066    be found, create one.  Return the named_label_entry, or NULL on
3067    failure.  */
3068 
3069 static named_label_entry *
lookup_label_1(tree id,bool making_local_p)3070 lookup_label_1 (tree id, bool making_local_p)
3071 {
3072   /* You can't use labels at global scope.  */
3073   if (current_function_decl == NULL_TREE)
3074     {
3075       error ("label %qE referenced outside of any function", id);
3076       return NULL;
3077     }
3078 
3079   if (!named_labels)
3080     named_labels = hash_table<named_label_hash>::create_ggc (13);
3081 
3082   hashval_t hash = IDENTIFIER_HASH_VALUE (id);
3083   named_label_entry **slot
3084     = named_labels->find_slot_with_hash (id, hash, INSERT);
3085   named_label_entry *old = *slot;
3086 
3087   if (old && old->label_decl)
3088     {
3089       if (!making_local_p)
3090 	return old;
3091 
3092       if (old->binding_level == current_binding_level)
3093 	{
3094 	  error ("local label %qE conflicts with existing label", id);
3095 	  inform (DECL_SOURCE_LOCATION (old->label_decl), "previous label");
3096 	  return NULL;
3097 	}
3098     }
3099 
3100   /* We are making a new decl, create or reuse the named_label_entry  */
3101   named_label_entry *ent = NULL;
3102   if (old && !old->label_decl)
3103     ent = old;
3104   else
3105     {
3106       ent = ggc_cleared_alloc<named_label_entry> ();
3107       ent->name = id;
3108       ent->outer = old;
3109       *slot = ent;
3110     }
3111 
3112   /* Now create the LABEL_DECL.  */
3113   tree decl = build_decl (input_location, LABEL_DECL, id, void_type_node);
3114 
3115   DECL_CONTEXT (decl) = current_function_decl;
3116   SET_DECL_MODE (decl, VOIDmode);
3117   if (making_local_p)
3118     {
3119       C_DECLARED_LABEL_FLAG (decl) = true;
3120       DECL_CHAIN (decl) = current_binding_level->names;
3121       current_binding_level->names = decl;
3122     }
3123 
3124   ent->label_decl = decl;
3125 
3126   return ent;
3127 }
3128 
3129 /* Wrapper for lookup_label_1.  */
3130 
3131 tree
lookup_label(tree id)3132 lookup_label (tree id)
3133 {
3134   bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3135   named_label_entry *ent = lookup_label_1 (id, false);
3136   timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3137   return ent ? ent->label_decl : NULL_TREE;
3138 }
3139 
3140 tree
declare_local_label(tree id)3141 declare_local_label (tree id)
3142 {
3143   bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3144   named_label_entry *ent = lookup_label_1 (id, true);
3145   timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3146   return ent ? ent->label_decl : NULL_TREE;
3147 }
3148 
3149 /* Returns nonzero if it is ill-formed to jump past the declaration of
3150    DECL.  Returns 2 if it's also a real problem.  */
3151 
3152 static int
decl_jump_unsafe(tree decl)3153 decl_jump_unsafe (tree decl)
3154 {
3155   /* [stmt.dcl]/3: A program that jumps from a point where a local variable
3156      with automatic storage duration is not in scope to a point where it is
3157      in scope is ill-formed unless the variable has scalar type, class type
3158      with a trivial default constructor and a trivial destructor, a
3159      cv-qualified version of one of these types, or an array of one of the
3160      preceding types and is declared without an initializer (8.5).  */
3161   tree type = TREE_TYPE (decl);
3162 
3163   if (!VAR_P (decl) || TREE_STATIC (decl)
3164       || type == error_mark_node)
3165     return 0;
3166 
3167   if (DECL_NONTRIVIALLY_INITIALIZED_P (decl)
3168       || variably_modified_type_p (type, NULL_TREE))
3169     return 2;
3170 
3171   if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3172     return 1;
3173 
3174   return 0;
3175 }
3176 
3177 /* A subroutine of check_previous_goto_1 and check_goto to identify a branch
3178    to the user.  */
3179 
3180 static bool
identify_goto(tree decl,location_t loc,const location_t * locus,diagnostic_t diag_kind)3181 identify_goto (tree decl, location_t loc, const location_t *locus,
3182 	       diagnostic_t diag_kind)
3183 {
3184   bool complained
3185     = emit_diagnostic (diag_kind, loc, 0,
3186 		       decl ? N_("jump to label %qD")
3187 		       : N_("jump to case label"), decl);
3188   if (complained && locus)
3189     inform (*locus, "  from here");
3190   return complained;
3191 }
3192 
3193 /* Check that a single previously seen jump to a newly defined label
3194    is OK.  DECL is the LABEL_DECL or 0; LEVEL is the binding_level for
3195    the jump context; NAMES are the names in scope in LEVEL at the jump
3196    context; LOCUS is the source position of the jump or 0.  Returns
3197    true if all is well.  */
3198 
3199 static bool
check_previous_goto_1(tree decl,cp_binding_level * level,tree names,bool exited_omp,const location_t * locus)3200 check_previous_goto_1 (tree decl, cp_binding_level* level, tree names,
3201 		       bool exited_omp, const location_t *locus)
3202 {
3203   cp_binding_level *b;
3204   bool complained = false;
3205   int identified = 0;
3206   bool saw_eh = false, saw_omp = false, saw_tm = false, saw_cxif = false;
3207 
3208   if (exited_omp)
3209     {
3210       complained = identify_goto (decl, input_location, locus, DK_ERROR);
3211       if (complained)
3212 	inform (input_location, "  exits OpenMP structured block");
3213       saw_omp = true;
3214       identified = 2;
3215     }
3216 
3217   for (b = current_binding_level; b ; b = b->level_chain)
3218     {
3219       tree new_decls, old_decls = (b == level ? names : NULL_TREE);
3220 
3221       for (new_decls = b->names; new_decls != old_decls;
3222 	   new_decls = (DECL_P (new_decls) ? DECL_CHAIN (new_decls)
3223 			: TREE_CHAIN (new_decls)))
3224 	{
3225 	  int problem = decl_jump_unsafe (new_decls);
3226 	  if (! problem)
3227 	    continue;
3228 
3229 	  if (!identified)
3230 	    {
3231 	      complained = identify_goto (decl, input_location, locus,
3232 					  DK_PERMERROR);
3233 	      identified = 1;
3234 	    }
3235 	  if (complained)
3236 	    {
3237 	      if (problem > 1)
3238 		inform (DECL_SOURCE_LOCATION (new_decls),
3239 			"  crosses initialization of %q#D", new_decls);
3240 	      else
3241 		inform (DECL_SOURCE_LOCATION (new_decls),
3242 			"  enters scope of %q#D, which has "
3243 			"non-trivial destructor", new_decls);
3244 	    }
3245 	}
3246 
3247       if (b == level)
3248 	break;
3249 
3250       const char *inf = NULL;
3251       location_t loc = input_location;
3252       switch (b->kind)
3253 	{
3254 	case sk_try:
3255 	  if (!saw_eh)
3256 	    inf = N_("enters try block");
3257 	  saw_eh = true;
3258 	  break;
3259 
3260 	case sk_catch:
3261 	  if (!saw_eh)
3262 	    inf = N_("enters catch block");
3263 	  saw_eh = true;
3264 	  break;
3265 
3266 	case sk_omp:
3267 	  if (!saw_omp)
3268 	    inf = N_("enters OpenMP structured block");
3269 	  saw_omp = true;
3270 	  break;
3271 
3272 	case sk_transaction:
3273 	  if (!saw_tm)
3274 	    inf = N_("enters synchronized or atomic statement");
3275 	  saw_tm = true;
3276 	  break;
3277 
3278 	case sk_block:
3279 	  if (!saw_cxif && level_for_constexpr_if (b->level_chain))
3280 	    {
3281 	      inf = N_("enters constexpr if statement");
3282 	      loc = EXPR_LOCATION (b->level_chain->this_entity);
3283 	      saw_cxif = true;
3284 	    }
3285 	  break;
3286 
3287 	default:
3288 	  break;
3289 	}
3290 
3291       if (inf)
3292 	{
3293 	  if (identified < 2)
3294 	    complained = identify_goto (decl, input_location, locus, DK_ERROR);
3295 	  identified = 2;
3296 	  if (complained)
3297 	    inform (loc, "  %s", inf);
3298 	}
3299     }
3300 
3301   return !identified;
3302 }
3303 
3304 static void
check_previous_goto(tree decl,struct named_label_use_entry * use)3305 check_previous_goto (tree decl, struct named_label_use_entry *use)
3306 {
3307   check_previous_goto_1 (decl, use->binding_level,
3308 			 use->names_in_scope, use->in_omp_scope,
3309 			 &use->o_goto_locus);
3310 }
3311 
3312 static bool
check_switch_goto(cp_binding_level * level)3313 check_switch_goto (cp_binding_level* level)
3314 {
3315   return check_previous_goto_1 (NULL_TREE, level, level->names, false, NULL);
3316 }
3317 
3318 /* Check that a new jump to a label DECL is OK.  Called by
3319    finish_goto_stmt.  */
3320 
3321 void
check_goto(tree decl)3322 check_goto (tree decl)
3323 {
3324   /* We can't know where a computed goto is jumping.
3325      So we assume that it's OK.  */
3326   if (TREE_CODE (decl) != LABEL_DECL)
3327     return;
3328 
3329   /* We didn't record any information about this label when we created it,
3330      and there's not much point since it's trivial to analyze as a return.  */
3331   if (decl == cdtor_label)
3332     return;
3333 
3334   hashval_t hash = IDENTIFIER_HASH_VALUE (DECL_NAME (decl));
3335   named_label_entry **slot
3336     = named_labels->find_slot_with_hash (DECL_NAME (decl), hash, NO_INSERT);
3337   named_label_entry *ent = *slot;
3338 
3339   /* If the label hasn't been defined yet, defer checking.  */
3340   if (! DECL_INITIAL (decl))
3341     {
3342       /* Don't bother creating another use if the last goto had the
3343 	 same data, and will therefore create the same set of errors.  */
3344       if (ent->uses
3345 	  && ent->uses->names_in_scope == current_binding_level->names)
3346 	return;
3347 
3348       named_label_use_entry *new_use
3349 	= ggc_alloc<named_label_use_entry> ();
3350       new_use->binding_level = current_binding_level;
3351       new_use->names_in_scope = current_binding_level->names;
3352       new_use->o_goto_locus = input_location;
3353       new_use->in_omp_scope = false;
3354 
3355       new_use->next = ent->uses;
3356       ent->uses = new_use;
3357       return;
3358     }
3359 
3360   bool saw_catch = false, complained = false;
3361   int identified = 0;
3362   tree bad;
3363   unsigned ix;
3364 
3365   if (ent->in_try_scope || ent->in_catch_scope || ent->in_transaction_scope
3366       || ent->in_constexpr_if
3367       || ent->in_omp_scope || !vec_safe_is_empty (ent->bad_decls))
3368     {
3369       diagnostic_t diag_kind = DK_PERMERROR;
3370       if (ent->in_try_scope || ent->in_catch_scope || ent->in_constexpr_if
3371 	  || ent->in_transaction_scope || ent->in_omp_scope)
3372 	diag_kind = DK_ERROR;
3373       complained = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
3374 				  &input_location, diag_kind);
3375       identified = 1 + (diag_kind == DK_ERROR);
3376     }
3377 
3378   FOR_EACH_VEC_SAFE_ELT (ent->bad_decls, ix, bad)
3379     {
3380       int u = decl_jump_unsafe (bad);
3381 
3382       if (u > 1 && DECL_ARTIFICIAL (bad))
3383 	{
3384 	  /* Can't skip init of __exception_info.  */
3385 	  if (identified == 1)
3386 	    {
3387 	      complained = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
3388 					  &input_location, DK_ERROR);
3389 	      identified = 2;
3390 	    }
3391 	  if (complained)
3392 	    inform (DECL_SOURCE_LOCATION (bad), "  enters catch block");
3393 	  saw_catch = true;
3394 	}
3395       else if (complained)
3396 	{
3397 	  if (u > 1)
3398 	    inform (DECL_SOURCE_LOCATION (bad),
3399 		    "  skips initialization of %q#D", bad);
3400 	  else
3401 	    inform (DECL_SOURCE_LOCATION (bad),
3402 		    "  enters scope of %q#D which has "
3403 		    "non-trivial destructor", bad);
3404 	}
3405     }
3406 
3407   if (complained)
3408     {
3409       if (ent->in_try_scope)
3410 	inform (input_location, "  enters try block");
3411       else if (ent->in_catch_scope && !saw_catch)
3412 	inform (input_location, "  enters catch block");
3413       else if (ent->in_transaction_scope)
3414 	inform (input_location, "  enters synchronized or atomic statement");
3415       else if (ent->in_constexpr_if)
3416 	inform (input_location, "  enters %<constexpr%> if statement");
3417     }
3418 
3419   if (ent->in_omp_scope)
3420     {
3421       if (complained)
3422 	inform (input_location, "  enters OpenMP structured block");
3423     }
3424   else if (flag_openmp)
3425     for (cp_binding_level *b = current_binding_level; b ; b = b->level_chain)
3426       {
3427 	if (b == ent->binding_level)
3428 	  break;
3429 	if (b->kind == sk_omp)
3430 	  {
3431 	    if (identified < 2)
3432 	      {
3433 		complained = identify_goto (decl,
3434 					    DECL_SOURCE_LOCATION (decl),
3435 					    &input_location, DK_ERROR);
3436 		identified = 2;
3437 	      }
3438 	    if (complained)
3439 	      inform (input_location, "  exits OpenMP structured block");
3440 	    break;
3441 	  }
3442       }
3443 }
3444 
3445 /* Check that a return is ok wrt OpenMP structured blocks.
3446    Called by finish_return_stmt.  Returns true if all is well.  */
3447 
3448 bool
check_omp_return(void)3449 check_omp_return (void)
3450 {
3451   for (cp_binding_level *b = current_binding_level; b ; b = b->level_chain)
3452     if (b->kind == sk_omp)
3453       {
3454 	error ("invalid exit from OpenMP structured block");
3455 	return false;
3456       }
3457     else if (b->kind == sk_function_parms)
3458       break;
3459   return true;
3460 }
3461 
3462 /* Define a label, specifying the location in the source file.
3463    Return the LABEL_DECL node for the label.  */
3464 
3465 static tree
define_label_1(location_t location,tree name)3466 define_label_1 (location_t location, tree name)
3467 {
3468   /* After labels, make any new cleanups in the function go into their
3469      own new (temporary) binding contour.  */
3470   for (cp_binding_level *p = current_binding_level;
3471        p->kind != sk_function_parms;
3472        p = p->level_chain)
3473     p->more_cleanups_ok = 0;
3474 
3475   named_label_entry *ent = lookup_label_1 (name, false);
3476   tree decl = ent->label_decl;
3477 
3478   if (DECL_INITIAL (decl) != NULL_TREE)
3479     {
3480       error ("duplicate label %qD", decl);
3481       return error_mark_node;
3482     }
3483   else
3484     {
3485       /* Mark label as having been defined.  */
3486       DECL_INITIAL (decl) = error_mark_node;
3487       /* Say where in the source.  */
3488       DECL_SOURCE_LOCATION (decl) = location;
3489 
3490       ent->binding_level = current_binding_level;
3491       ent->names_in_scope = current_binding_level->names;
3492 
3493       for (named_label_use_entry *use = ent->uses; use; use = use->next)
3494 	check_previous_goto (decl, use);
3495       ent->uses = NULL;
3496     }
3497 
3498   return decl;
3499 }
3500 
3501 /* Wrapper for define_label_1.  */
3502 
3503 tree
define_label(location_t location,tree name)3504 define_label (location_t location, tree name)
3505 {
3506   bool running = timevar_cond_start (TV_NAME_LOOKUP);
3507   tree ret = define_label_1 (location, name);
3508   timevar_cond_stop (TV_NAME_LOOKUP, running);
3509   return ret;
3510 }
3511 
3512 
3513 struct cp_switch
3514 {
3515   cp_binding_level *level;
3516   struct cp_switch *next;
3517   /* The SWITCH_STMT being built.  */
3518   tree switch_stmt;
3519   /* A splay-tree mapping the low element of a case range to the high
3520      element, or NULL_TREE if there is no high element.  Used to
3521      determine whether or not a new case label duplicates an old case
3522      label.  We need a tree, rather than simply a hash table, because
3523      of the GNU case range extension.  */
3524   splay_tree cases;
3525   /* Remember whether there was a case value that is outside the
3526      range of the original type of the controlling expression.  */
3527   bool outside_range_p;
3528   /* Remember whether a default: case label has been seen.  */
3529   bool has_default_p;
3530   /* Remember whether a BREAK_STMT has been seen in this SWITCH_STMT.  */
3531   bool break_stmt_seen_p;
3532   /* Set if inside of {FOR,DO,WHILE}_BODY nested inside of a switch,
3533      where BREAK_STMT doesn't belong to the SWITCH_STMT.  */
3534   bool in_loop_body_p;
3535 };
3536 
3537 /* A stack of the currently active switch statements.  The innermost
3538    switch statement is on the top of the stack.  There is no need to
3539    mark the stack for garbage collection because it is only active
3540    during the processing of the body of a function, and we never
3541    collect at that point.  */
3542 
3543 static struct cp_switch *switch_stack;
3544 
3545 /* Called right after a switch-statement condition is parsed.
3546    SWITCH_STMT is the switch statement being parsed.  */
3547 
3548 void
push_switch(tree switch_stmt)3549 push_switch (tree switch_stmt)
3550 {
3551   struct cp_switch *p = XNEW (struct cp_switch);
3552   p->level = current_binding_level;
3553   p->next = switch_stack;
3554   p->switch_stmt = switch_stmt;
3555   p->cases = splay_tree_new (case_compare, NULL, NULL);
3556   p->outside_range_p = false;
3557   p->has_default_p = false;
3558   p->break_stmt_seen_p = false;
3559   p->in_loop_body_p = false;
3560   switch_stack = p;
3561 }
3562 
3563 void
pop_switch(void)3564 pop_switch (void)
3565 {
3566   struct cp_switch *cs = switch_stack;
3567   location_t switch_location;
3568 
3569   /* Emit warnings as needed.  */
3570   switch_location = EXPR_LOC_OR_LOC (cs->switch_stmt, input_location);
3571   const bool bool_cond_p
3572     = (SWITCH_STMT_TYPE (cs->switch_stmt)
3573        && TREE_CODE (SWITCH_STMT_TYPE (cs->switch_stmt)) == BOOLEAN_TYPE);
3574   if (!processing_template_decl)
3575     c_do_switch_warnings (cs->cases, switch_location,
3576 			  SWITCH_STMT_TYPE (cs->switch_stmt),
3577 			  SWITCH_STMT_COND (cs->switch_stmt),
3578 			  bool_cond_p, cs->outside_range_p);
3579 
3580   /* For the benefit of block_may_fallthru remember if the switch body
3581      case labels cover all possible values and if there are break; stmts.  */
3582   if (cs->has_default_p
3583       || (!processing_template_decl
3584 	  && c_switch_covers_all_cases_p (cs->cases,
3585 					  SWITCH_STMT_TYPE (cs->switch_stmt))))
3586     SWITCH_STMT_ALL_CASES_P (cs->switch_stmt) = 1;
3587   if (!cs->break_stmt_seen_p)
3588     SWITCH_STMT_NO_BREAK_P (cs->switch_stmt) = 1;
3589   gcc_assert (!cs->in_loop_body_p);
3590   splay_tree_delete (cs->cases);
3591   switch_stack = switch_stack->next;
3592   free (cs);
3593 }
3594 
3595 /* Note that a BREAK_STMT is about to be added.  If it is inside of
3596    a SWITCH_STMT and not inside of a loop body inside of it, note
3597    in switch_stack we've seen a BREAK_STMT.  */
3598 
3599 void
note_break_stmt(void)3600 note_break_stmt (void)
3601 {
3602   if (switch_stack && !switch_stack->in_loop_body_p)
3603     switch_stack->break_stmt_seen_p = true;
3604 }
3605 
3606 /* Note the start of processing of an iteration statement's body.
3607    The note_break_stmt function will do nothing while processing it.
3608    Return a flag that should be passed to note_iteration_stmt_body_end.  */
3609 
3610 bool
note_iteration_stmt_body_start(void)3611 note_iteration_stmt_body_start (void)
3612 {
3613   if (!switch_stack)
3614     return false;
3615   bool ret = switch_stack->in_loop_body_p;
3616   switch_stack->in_loop_body_p = true;
3617   return ret;
3618 }
3619 
3620 /* Note the end of processing of an iteration statement's body.  */
3621 
3622 void
note_iteration_stmt_body_end(bool prev)3623 note_iteration_stmt_body_end (bool prev)
3624 {
3625   if (switch_stack)
3626     switch_stack->in_loop_body_p = prev;
3627 }
3628 
3629 /* Convert a case constant VALUE in a switch to the type TYPE of the switch
3630    condition.  Note that if TYPE and VALUE are already integral we don't
3631    really do the conversion because the language-independent
3632    warning/optimization code will work better that way.  */
3633 
3634 static tree
case_conversion(tree type,tree value)3635 case_conversion (tree type, tree value)
3636 {
3637   if (value == NULL_TREE)
3638     return value;
3639 
3640   value = mark_rvalue_use (value);
3641 
3642   if (cxx_dialect >= cxx11
3643       && (SCOPED_ENUM_P (type)
3644 	  || !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (value))))
3645     {
3646       if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
3647 	type = type_promotes_to (type);
3648       value = (perform_implicit_conversion_flags
3649 	       (type, value, tf_warning_or_error,
3650 		LOOKUP_IMPLICIT | LOOKUP_NO_NON_INTEGRAL));
3651     }
3652   return cxx_constant_value (value);
3653 }
3654 
3655 /* Note that we've seen a definition of a case label, and complain if this
3656    is a bad place for one.  */
3657 
3658 tree
finish_case_label(location_t loc,tree low_value,tree high_value)3659 finish_case_label (location_t loc, tree low_value, tree high_value)
3660 {
3661   tree cond, r;
3662   cp_binding_level *p;
3663   tree type;
3664 
3665   if (low_value == NULL_TREE && high_value == NULL_TREE)
3666     switch_stack->has_default_p = true;
3667 
3668   if (processing_template_decl)
3669     {
3670       tree label;
3671 
3672       /* For templates, just add the case label; we'll do semantic
3673 	 analysis at instantiation-time.  */
3674       label = build_decl (loc, LABEL_DECL, NULL_TREE, NULL_TREE);
3675       return add_stmt (build_case_label (low_value, high_value, label));
3676     }
3677 
3678   /* Find the condition on which this switch statement depends.  */
3679   cond = SWITCH_STMT_COND (switch_stack->switch_stmt);
3680   if (cond && TREE_CODE (cond) == TREE_LIST)
3681     cond = TREE_VALUE (cond);
3682 
3683   if (!check_switch_goto (switch_stack->level))
3684     return error_mark_node;
3685 
3686   type = SWITCH_STMT_TYPE (switch_stack->switch_stmt);
3687 
3688   low_value = case_conversion (type, low_value);
3689   high_value = case_conversion (type, high_value);
3690 
3691   r = c_add_case_label (loc, switch_stack->cases, cond, type,
3692 			low_value, high_value,
3693 			&switch_stack->outside_range_p);
3694 
3695   /* After labels, make any new cleanups in the function go into their
3696      own new (temporary) binding contour.  */
3697   for (p = current_binding_level;
3698        p->kind != sk_function_parms;
3699        p = p->level_chain)
3700     p->more_cleanups_ok = 0;
3701 
3702   return r;
3703 }
3704 
3705 struct typename_info {
3706   tree scope;
3707   tree name;
3708   tree template_id;
3709   bool enum_p;
3710   bool class_p;
3711 };
3712 
3713 struct typename_hasher : ggc_ptr_hash<tree_node>
3714 {
3715   typedef typename_info *compare_type;
3716 
3717   /* Hash a TYPENAME_TYPE.  */
3718 
3719   static hashval_t
hashtypename_hasher3720   hash (tree t)
3721   {
3722     hashval_t hash;
3723 
3724     hash = (htab_hash_pointer (TYPE_CONTEXT (t))
3725 	    ^ htab_hash_pointer (TYPE_IDENTIFIER (t)));
3726 
3727     return hash;
3728   }
3729 
3730   /* Compare two TYPENAME_TYPEs.  */
3731 
3732   static bool
equaltypename_hasher3733   equal (tree t1, const typename_info *t2)
3734   {
3735     return (TYPE_IDENTIFIER (t1) == t2->name
3736 	    && TYPE_CONTEXT (t1) == t2->scope
3737 	    && TYPENAME_TYPE_FULLNAME (t1) == t2->template_id
3738 	    && TYPENAME_IS_ENUM_P (t1) == t2->enum_p
3739 	    && TYPENAME_IS_CLASS_P (t1) == t2->class_p);
3740   }
3741 };
3742 
3743 /* Build a TYPENAME_TYPE.  If the type is `typename T::t', CONTEXT is
3744    the type of `T', NAME is the IDENTIFIER_NODE for `t'.
3745 
3746    Returns the new TYPENAME_TYPE.  */
3747 
3748 static GTY (()) hash_table<typename_hasher> *typename_htab;
3749 
3750 tree
build_typename_type(tree context,tree name,tree fullname,enum tag_types tag_type)3751 build_typename_type (tree context, tree name, tree fullname,
3752 		     enum tag_types tag_type)
3753 {
3754   tree t;
3755   tree d;
3756   typename_info ti;
3757   tree *e;
3758   hashval_t hash;
3759 
3760   if (typename_htab == NULL)
3761     typename_htab = hash_table<typename_hasher>::create_ggc (61);
3762 
3763   ti.scope = FROB_CONTEXT (context);
3764   ti.name = name;
3765   ti.template_id = fullname;
3766   ti.enum_p = tag_type == enum_type;
3767   ti.class_p = (tag_type == class_type
3768 		|| tag_type == record_type
3769 		|| tag_type == union_type);
3770   hash =  (htab_hash_pointer (ti.scope)
3771 	   ^ htab_hash_pointer (ti.name));
3772 
3773   /* See if we already have this type.  */
3774   e = typename_htab->find_slot_with_hash (&ti, hash, INSERT);
3775   if (*e)
3776     t = *e;
3777   else
3778     {
3779       /* Build the TYPENAME_TYPE.  */
3780       t = cxx_make_type (TYPENAME_TYPE);
3781       TYPE_CONTEXT (t) = ti.scope;
3782       TYPENAME_TYPE_FULLNAME (t) = ti.template_id;
3783       TYPENAME_IS_ENUM_P (t) = ti.enum_p;
3784       TYPENAME_IS_CLASS_P (t) = ti.class_p;
3785 
3786       /* Build the corresponding TYPE_DECL.  */
3787       d = build_decl (input_location, TYPE_DECL, name, t);
3788       TYPE_NAME (TREE_TYPE (d)) = d;
3789       TYPE_STUB_DECL (TREE_TYPE (d)) = d;
3790       DECL_CONTEXT (d) = FROB_CONTEXT (context);
3791       DECL_ARTIFICIAL (d) = 1;
3792 
3793       /* Store it in the hash table.  */
3794       *e = t;
3795 
3796       /* TYPENAME_TYPEs must always be compared structurally, because
3797 	 they may or may not resolve down to another type depending on
3798 	 the currently open classes. */
3799       SET_TYPE_STRUCTURAL_EQUALITY (t);
3800     }
3801 
3802   return t;
3803 }
3804 
3805 /* Resolve `typename CONTEXT::NAME'.  TAG_TYPE indicates the tag
3806    provided to name the type.  Returns an appropriate type, unless an
3807    error occurs, in which case error_mark_node is returned.  If we
3808    locate a non-artificial TYPE_DECL and TF_KEEP_TYPE_DECL is set, we
3809    return that, rather than the _TYPE it corresponds to, in other
3810    cases we look through the type decl.  If TF_ERROR is set, complain
3811    about errors, otherwise be quiet.  */
3812 
3813 tree
make_typename_type(tree context,tree name,enum tag_types tag_type,tsubst_flags_t complain)3814 make_typename_type (tree context, tree name, enum tag_types tag_type,
3815 		    tsubst_flags_t complain)
3816 {
3817   tree fullname;
3818   tree t;
3819   bool want_template;
3820 
3821   if (name == error_mark_node
3822       || context == NULL_TREE
3823       || context == error_mark_node)
3824     return error_mark_node;
3825 
3826   if (TYPE_P (name))
3827     {
3828       if (!(TYPE_LANG_SPECIFIC (name)
3829 	    && (CLASSTYPE_IS_TEMPLATE (name)
3830 		|| CLASSTYPE_USE_TEMPLATE (name))))
3831 	name = TYPE_IDENTIFIER (name);
3832       else
3833 	/* Create a TEMPLATE_ID_EXPR for the type.  */
3834 	name = build_nt (TEMPLATE_ID_EXPR,
3835 			 CLASSTYPE_TI_TEMPLATE (name),
3836 			 CLASSTYPE_TI_ARGS (name));
3837     }
3838   else if (TREE_CODE (name) == TYPE_DECL)
3839     name = DECL_NAME (name);
3840 
3841   fullname = name;
3842 
3843   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3844     {
3845       name = TREE_OPERAND (name, 0);
3846       if (DECL_TYPE_TEMPLATE_P (name))
3847 	name = TREE_OPERAND (fullname, 0) = DECL_NAME (name);
3848       if (TREE_CODE (name) != IDENTIFIER_NODE)
3849 	{
3850 	  if (complain & tf_error)
3851 	    error ("%qD is not a type", name);
3852 	  return error_mark_node;
3853 	}
3854     }
3855   if (TREE_CODE (name) == TEMPLATE_DECL)
3856     {
3857       if (complain & tf_error)
3858 	error ("%qD used without template parameters", name);
3859       return error_mark_node;
3860     }
3861   gcc_assert (identifier_p (name));
3862   gcc_assert (TYPE_P (context));
3863 
3864   if (!MAYBE_CLASS_TYPE_P (context))
3865     {
3866       if (complain & tf_error)
3867 	error ("%q#T is not a class", context);
3868       return error_mark_node;
3869     }
3870 
3871   /* When the CONTEXT is a dependent type,  NAME could refer to a
3872      dependent base class of CONTEXT.  But look inside it anyway
3873      if CONTEXT is a currently open scope, in case it refers to a
3874      member of the current instantiation or a non-dependent base;
3875      lookup will stop when we hit a dependent base.  */
3876   if (!dependent_scope_p (context))
3877     /* We should only set WANT_TYPE when we're a nested typename type.
3878        Then we can give better diagnostics if we find a non-type.  */
3879     t = lookup_field (context, name, 2, /*want_type=*/true);
3880   else
3881     t = NULL_TREE;
3882 
3883   if ((!t || TREE_CODE (t) == TREE_LIST) && dependent_type_p (context))
3884     return build_typename_type (context, name, fullname, tag_type);
3885 
3886   want_template = TREE_CODE (fullname) == TEMPLATE_ID_EXPR;
3887 
3888   if (!t)
3889     {
3890       if (complain & tf_error)
3891 	{
3892 	  if (!COMPLETE_TYPE_P (context))
3893 	    cxx_incomplete_type_error (NULL_TREE, context);
3894 	  else
3895 	    error (want_template ? G_("no class template named %q#T in %q#T")
3896 		   : G_("no type named %q#T in %q#T"), name, context);
3897 	}
3898       return error_mark_node;
3899     }
3900 
3901   /* Pull out the template from an injected-class-name (or multiple).  */
3902   if (want_template)
3903     t = maybe_get_template_decl_from_type_decl (t);
3904 
3905   if (TREE_CODE (t) == TREE_LIST)
3906     {
3907       if (complain & tf_error)
3908 	{
3909 	  error ("lookup of %qT in %qT is ambiguous", name, context);
3910 	  print_candidates (t);
3911 	}
3912       return error_mark_node;
3913     }
3914 
3915   if (want_template && !DECL_TYPE_TEMPLATE_P (t))
3916     {
3917       if (complain & tf_error)
3918 	error ("%<typename %T::%D%> names %q#T, which is not a class template",
3919 	       context, name, t);
3920       return error_mark_node;
3921     }
3922   if (!want_template && TREE_CODE (t) != TYPE_DECL)
3923     {
3924       if (complain & tf_error)
3925 	error ("%<typename %T::%D%> names %q#T, which is not a type",
3926 	       context, name, t);
3927       return error_mark_node;
3928     }
3929 
3930   if (!perform_or_defer_access_check (TYPE_BINFO (context), t, t, complain))
3931     return error_mark_node;
3932 
3933   /* If we are currently parsing a template and if T is a typedef accessed
3934      through CONTEXT then we need to remember and check access of T at
3935      template instantiation time.  */
3936   add_typedef_to_current_template_for_access_check (t, context, input_location);
3937 
3938   if (want_template)
3939     return lookup_template_class (t, TREE_OPERAND (fullname, 1),
3940 				  NULL_TREE, context,
3941 				  /*entering_scope=*/0,
3942 				  complain | tf_user);
3943 
3944   if (DECL_ARTIFICIAL (t) || !(complain & tf_keep_type_decl))
3945     t = TREE_TYPE (t);
3946 
3947   maybe_record_typedef_use (t);
3948 
3949   return t;
3950 }
3951 
3952 /* Resolve `CONTEXT::template NAME'.  Returns a TEMPLATE_DECL if the name
3953    can be resolved or an UNBOUND_CLASS_TEMPLATE, unless an error occurs,
3954    in which case error_mark_node is returned.
3955 
3956    If PARM_LIST is non-NULL, also make sure that the template parameter
3957    list of TEMPLATE_DECL matches.
3958 
3959    If COMPLAIN zero, don't complain about any errors that occur.  */
3960 
3961 tree
make_unbound_class_template(tree context,tree name,tree parm_list,tsubst_flags_t complain)3962 make_unbound_class_template (tree context, tree name, tree parm_list,
3963 			     tsubst_flags_t complain)
3964 {
3965   tree t;
3966   tree d;
3967 
3968   if (TYPE_P (name))
3969     name = TYPE_IDENTIFIER (name);
3970   else if (DECL_P (name))
3971     name = DECL_NAME (name);
3972   gcc_assert (identifier_p (name));
3973 
3974   if (!dependent_type_p (context)
3975       || currently_open_class (context))
3976     {
3977       tree tmpl = NULL_TREE;
3978 
3979       if (MAYBE_CLASS_TYPE_P (context))
3980 	tmpl = lookup_field (context, name, 0, false);
3981 
3982       if (tmpl && TREE_CODE (tmpl) == TYPE_DECL)
3983 	tmpl = maybe_get_template_decl_from_type_decl (tmpl);
3984 
3985       if (!tmpl || !DECL_TYPE_TEMPLATE_P (tmpl))
3986 	{
3987 	  if (complain & tf_error)
3988 	    error ("no class template named %q#T in %q#T", name, context);
3989 	  return error_mark_node;
3990 	}
3991 
3992       if (parm_list
3993 	  && !comp_template_parms (DECL_TEMPLATE_PARMS (tmpl), parm_list))
3994 	{
3995 	  if (complain & tf_error)
3996 	    {
3997 	      error ("template parameters do not match template %qD", tmpl);
3998 	      inform (DECL_SOURCE_LOCATION (tmpl),
3999 		      "%qD declared here", tmpl);
4000 	    }
4001 	  return error_mark_node;
4002 	}
4003 
4004       if (!perform_or_defer_access_check (TYPE_BINFO (context), tmpl, tmpl,
4005 					  complain))
4006 	return error_mark_node;
4007 
4008       return tmpl;
4009     }
4010 
4011   /* Build the UNBOUND_CLASS_TEMPLATE.  */
4012   t = cxx_make_type (UNBOUND_CLASS_TEMPLATE);
4013   TYPE_CONTEXT (t) = FROB_CONTEXT (context);
4014   TREE_TYPE (t) = NULL_TREE;
4015   SET_TYPE_STRUCTURAL_EQUALITY (t);
4016 
4017   /* Build the corresponding TEMPLATE_DECL.  */
4018   d = build_decl (input_location, TEMPLATE_DECL, name, t);
4019   TYPE_NAME (TREE_TYPE (d)) = d;
4020   TYPE_STUB_DECL (TREE_TYPE (d)) = d;
4021   DECL_CONTEXT (d) = FROB_CONTEXT (context);
4022   DECL_ARTIFICIAL (d) = 1;
4023   DECL_TEMPLATE_PARMS (d) = parm_list;
4024 
4025   return t;
4026 }
4027 
4028 
4029 
4030 /* Push the declarations of builtin types into the global namespace.
4031    RID_INDEX is the index of the builtin type in the array
4032    RID_POINTERS.  NAME is the name used when looking up the builtin
4033    type.  TYPE is the _TYPE node for the builtin type.
4034 
4035    The calls to set_global_binding below should be
4036    eliminated.  Built-in types should not be looked up name; their
4037    names are keywords that the parser can recognize.  However, there
4038    is code in c-common.c that uses identifier_global_value to look up
4039    built-in types by name.  */
4040 
4041 void
record_builtin_type(enum rid rid_index,const char * name,tree type)4042 record_builtin_type (enum rid rid_index,
4043 		     const char* name,
4044 		     tree type)
4045 {
4046   tree decl = NULL_TREE;
4047 
4048   if (name)
4049     {
4050       tree tname = get_identifier (name);
4051       tree tdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, tname, type);
4052       DECL_ARTIFICIAL (tdecl) = 1;
4053       set_global_binding (tdecl);
4054       decl = tdecl;
4055     }
4056 
4057   if ((int) rid_index < (int) RID_MAX)
4058     if (tree rname = ridpointers[(int) rid_index])
4059       if (!decl || DECL_NAME (decl) != rname)
4060 	{
4061 	  tree rdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, rname, type);
4062 	  DECL_ARTIFICIAL (rdecl) = 1;
4063 	  set_global_binding (rdecl);
4064 	  if (!decl)
4065 	    decl = rdecl;
4066 	}
4067 
4068   if (decl)
4069     {
4070       if (!TYPE_NAME (type))
4071 	TYPE_NAME (type) = decl;
4072       debug_hooks->type_decl (decl, 0);
4073     }
4074 }
4075 
4076 /* Push a type into the namespace so that the back ends ignore it.  */
4077 
4078 static void
record_unknown_type(tree type,const char * name)4079 record_unknown_type (tree type, const char* name)
4080 {
4081   tree decl = pushdecl (build_decl (UNKNOWN_LOCATION,
4082 				    TYPE_DECL, get_identifier (name), type));
4083   /* Make sure the "unknown type" typedecl gets ignored for debug info.  */
4084   DECL_IGNORED_P (decl) = 1;
4085   TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
4086   TYPE_SIZE (type) = TYPE_SIZE (void_type_node);
4087   SET_TYPE_ALIGN (type, 1);
4088   TYPE_USER_ALIGN (type) = 0;
4089   SET_TYPE_MODE (type, TYPE_MODE (void_type_node));
4090 }
4091 
4092 /* Create all the predefined identifiers.  */
4093 
4094 static void
initialize_predefined_identifiers(void)4095 initialize_predefined_identifiers (void)
4096 {
4097   struct predefined_identifier
4098   {
4099     const char *name; /* Name.  */
4100     tree *node;  /* Node to store it in.  */
4101     cp_identifier_kind kind;  /* Kind of identifier.  */
4102   };
4103 
4104   /* A table of identifiers to create at startup.  */
4105   static const predefined_identifier predefined_identifiers[] = {
4106     {"C++", &lang_name_cplusplus, cik_normal},
4107     {"C", &lang_name_c, cik_normal},
4108     /* Some of these names have a trailing space so that it is
4109        impossible for them to conflict with names written by users.  */
4110     {"__ct ", &ctor_identifier, cik_ctor},
4111     {"__ct_base ", &base_ctor_identifier, cik_ctor},
4112     {"__ct_comp ", &complete_ctor_identifier, cik_ctor},
4113     {"__dt ", &dtor_identifier, cik_dtor},
4114     {"__dt_base ", &base_dtor_identifier, cik_dtor},
4115     {"__dt_comp ", &complete_dtor_identifier, cik_dtor},
4116     {"__dt_del ", &deleting_dtor_identifier, cik_dtor},
4117     {"__conv_op ", &conv_op_identifier, cik_conv_op},
4118     {"__in_chrg", &in_charge_identifier, cik_normal},
4119     {"this", &this_identifier, cik_normal},
4120     {"__delta", &delta_identifier, cik_normal},
4121     {"__pfn", &pfn_identifier, cik_normal},
4122     {"_vptr", &vptr_identifier, cik_normal},
4123     {"__vtt_parm", &vtt_parm_identifier, cik_normal},
4124     {"::", &global_identifier, cik_normal},
4125     {"std", &std_identifier, cik_normal},
4126       /* The demangler expects anonymous namespaces to be called
4127 	 something starting with '_GLOBAL__N_'.  It no longer needs
4128 	 to be unique to the TU.  */
4129     {"_GLOBAL__N_1", &anon_identifier, cik_normal},
4130     {"auto", &auto_identifier, cik_normal},
4131     {"decltype(auto)", &decltype_auto_identifier, cik_normal},
4132     {"initializer_list", &init_list_identifier, cik_normal},
4133     {NULL, NULL, cik_normal}
4134   };
4135 
4136   for (const predefined_identifier *pid = predefined_identifiers;
4137        pid->name; ++pid)
4138     {
4139       *pid->node = get_identifier (pid->name);
4140       /* Some of these identifiers already have a special kind.  */
4141       if (pid->kind != cik_normal)
4142 	set_identifier_kind (*pid->node, pid->kind);
4143     }
4144 }
4145 
4146 /* Create the predefined scalar types of C,
4147    and some nodes representing standard constants (0, 1, (void *)0).
4148    Initialize the global binding level.
4149    Make definitions for built-in primitive functions.  */
4150 
4151 void
cxx_init_decl_processing(void)4152 cxx_init_decl_processing (void)
4153 {
4154   tree void_ftype;
4155   tree void_ftype_ptr;
4156 
4157   /* Create all the identifiers we need.  */
4158   initialize_predefined_identifiers ();
4159 
4160   /* Create the global variables.  */
4161   push_to_top_level ();
4162 
4163   current_function_decl = NULL_TREE;
4164   current_binding_level = NULL;
4165   /* Enter the global namespace.  */
4166   gcc_assert (global_namespace == NULL_TREE);
4167   global_namespace = build_lang_decl (NAMESPACE_DECL, global_identifier,
4168 				      void_type_node);
4169   TREE_PUBLIC (global_namespace) = 1;
4170   DECL_CONTEXT (global_namespace)
4171     = build_translation_unit_decl (get_identifier (main_input_filename));
4172   /* Remember whether we want the empty class passing ABI change warning
4173      in this TU.  */
4174   TRANSLATION_UNIT_WARN_EMPTY_P (DECL_CONTEXT (global_namespace))
4175     = warn_abi && abi_version_crosses (12);
4176   debug_hooks->register_main_translation_unit
4177     (DECL_CONTEXT (global_namespace));
4178   begin_scope (sk_namespace, global_namespace);
4179   current_namespace = global_namespace;
4180 
4181   if (flag_visibility_ms_compat)
4182     default_visibility = VISIBILITY_HIDDEN;
4183 
4184   /* Initially, C.  */
4185   current_lang_name = lang_name_c;
4186 
4187   /* Create the `std' namespace.  */
4188   push_namespace (std_identifier);
4189   std_node = current_namespace;
4190   pop_namespace ();
4191 
4192   flag_noexcept_type = (cxx_dialect >= cxx17);
4193   /* There's no fixed location for <command-line>, the current
4194      location is <builtins>, which is somewhat confusing.  */
4195   if (!flag_new_for_scope)
4196     warning_at (UNKNOWN_LOCATION, OPT_Wdeprecated,
4197 		"%<-fno-for-scope%> is deprecated");
4198   if (flag_friend_injection)
4199     warning_at (UNKNOWN_LOCATION, OPT_Wdeprecated,
4200 		"%<-ffriend-injection%> is deprecated");
4201 
4202   c_common_nodes_and_builtins ();
4203 
4204   integer_two_node = build_int_cst (NULL_TREE, 2);
4205 
4206   /* Guess at the initial static decls size.  */
4207   vec_alloc (static_decls, 500);
4208 
4209   /* ... and keyed classes.  */
4210   vec_alloc (keyed_classes, 100);
4211 
4212   record_builtin_type (RID_BOOL, "bool", boolean_type_node);
4213   truthvalue_type_node = boolean_type_node;
4214   truthvalue_false_node = boolean_false_node;
4215   truthvalue_true_node = boolean_true_node;
4216 
4217   empty_except_spec = build_tree_list (NULL_TREE, NULL_TREE);
4218   noexcept_true_spec = build_tree_list (boolean_true_node, NULL_TREE);
4219   noexcept_false_spec = build_tree_list (boolean_false_node, NULL_TREE);
4220   noexcept_deferred_spec = build_tree_list (make_node (DEFERRED_NOEXCEPT),
4221 					    NULL_TREE);
4222 
4223 #if 0
4224   record_builtin_type (RID_MAX, NULL, string_type_node);
4225 #endif
4226 
4227   delta_type_node = ptrdiff_type_node;
4228   vtable_index_type = ptrdiff_type_node;
4229 
4230   vtt_parm_type = build_pointer_type (const_ptr_type_node);
4231   void_ftype = build_function_type_list (void_type_node, NULL_TREE);
4232   void_ftype_ptr = build_function_type_list (void_type_node,
4233 					     ptr_type_node, NULL_TREE);
4234   void_ftype_ptr
4235     = build_exception_variant (void_ftype_ptr, empty_except_spec);
4236 
4237   /* Create the conversion operator marker.  This operator's DECL_NAME
4238      is in the identifier table, so we can use identifier equality to
4239      find it.  */
4240   conv_op_marker = build_lang_decl (FUNCTION_DECL, conv_op_identifier,
4241 				    void_ftype);
4242 
4243   /* C++ extensions */
4244 
4245   unknown_type_node = make_node (LANG_TYPE);
4246   record_unknown_type (unknown_type_node, "unknown type");
4247 
4248   /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node.  */
4249   TREE_TYPE (unknown_type_node) = unknown_type_node;
4250 
4251   /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same
4252      result.  */
4253   TYPE_POINTER_TO (unknown_type_node) = unknown_type_node;
4254   TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node;
4255 
4256   init_list_type_node = make_node (LANG_TYPE);
4257   record_unknown_type (init_list_type_node, "init list");
4258 
4259   {
4260     /* Make sure we get a unique function type, so we can give
4261        its pointer type a name.  (This wins for gdb.) */
4262     tree vfunc_type = make_node (FUNCTION_TYPE);
4263     TREE_TYPE (vfunc_type) = integer_type_node;
4264     TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
4265     layout_type (vfunc_type);
4266 
4267     vtable_entry_type = build_pointer_type (vfunc_type);
4268   }
4269   record_builtin_type (RID_MAX, "__vtbl_ptr_type", vtable_entry_type);
4270 
4271   vtbl_type_node
4272     = build_cplus_array_type (vtable_entry_type, NULL_TREE);
4273   layout_type (vtbl_type_node);
4274   vtbl_type_node = cp_build_qualified_type (vtbl_type_node, TYPE_QUAL_CONST);
4275   record_builtin_type (RID_MAX, NULL, vtbl_type_node);
4276   vtbl_ptr_type_node = build_pointer_type (vtable_entry_type);
4277   layout_type (vtbl_ptr_type_node);
4278   record_builtin_type (RID_MAX, NULL, vtbl_ptr_type_node);
4279 
4280   push_namespace (get_identifier ("__cxxabiv1"));
4281   abi_node = current_namespace;
4282   pop_namespace ();
4283 
4284   global_type_node = make_node (LANG_TYPE);
4285   record_unknown_type (global_type_node, "global type");
4286 
4287   any_targ_node = make_node (LANG_TYPE);
4288   record_unknown_type (any_targ_node, "any type");
4289 
4290   /* Now, C++.  */
4291   current_lang_name = lang_name_cplusplus;
4292 
4293   if (aligned_new_threshold > 1
4294       && !pow2p_hwi (aligned_new_threshold))
4295     {
4296       error ("-faligned-new=%d is not a power of two", aligned_new_threshold);
4297       aligned_new_threshold = 1;
4298     }
4299   if (aligned_new_threshold == -1)
4300     aligned_new_threshold = (cxx_dialect >= cxx17) ? 1 : 0;
4301   if (aligned_new_threshold == 1)
4302     aligned_new_threshold = malloc_alignment () / BITS_PER_UNIT;
4303 
4304   {
4305     tree newattrs, extvisattr;
4306     tree newtype, deltype;
4307     tree ptr_ftype_sizetype;
4308     tree new_eh_spec;
4309 
4310     ptr_ftype_sizetype
4311       = build_function_type_list (ptr_type_node, size_type_node, NULL_TREE);
4312     if (cxx_dialect == cxx98)
4313       {
4314 	tree bad_alloc_id;
4315 	tree bad_alloc_type_node;
4316 	tree bad_alloc_decl;
4317 
4318 	push_namespace (std_identifier);
4319 	bad_alloc_id = get_identifier ("bad_alloc");
4320 	bad_alloc_type_node = make_class_type (RECORD_TYPE);
4321 	TYPE_CONTEXT (bad_alloc_type_node) = current_namespace;
4322 	bad_alloc_decl
4323 	  = create_implicit_typedef (bad_alloc_id, bad_alloc_type_node);
4324 	DECL_CONTEXT (bad_alloc_decl) = current_namespace;
4325 	pop_namespace ();
4326 
4327 	new_eh_spec
4328 	  = add_exception_specifier (NULL_TREE, bad_alloc_type_node, -1);
4329       }
4330     else
4331       new_eh_spec = noexcept_false_spec;
4332 
4333     /* Ensure attribs.c is initialized.  */
4334     init_attributes ();
4335 
4336     /* Ensure constraint.cc is initialized. */
4337     init_constraint_processing ();
4338 
4339     extvisattr = build_tree_list (get_identifier ("externally_visible"),
4340 				  NULL_TREE);
4341     newattrs = tree_cons (get_identifier ("alloc_size"),
4342 			  build_tree_list (NULL_TREE, integer_one_node),
4343 			  extvisattr);
4344     newtype = cp_build_type_attribute_variant (ptr_ftype_sizetype, newattrs);
4345     newtype = build_exception_variant (newtype, new_eh_spec);
4346     deltype = cp_build_type_attribute_variant (void_ftype_ptr, extvisattr);
4347     deltype = build_exception_variant (deltype, empty_except_spec);
4348     tree opnew = push_cp_library_fn (NEW_EXPR, newtype, 0);
4349     DECL_IS_MALLOC (opnew) = 1;
4350     DECL_IS_OPERATOR_NEW (opnew) = 1;
4351     opnew = push_cp_library_fn (VEC_NEW_EXPR, newtype, 0);
4352     DECL_IS_MALLOC (opnew) = 1;
4353     DECL_IS_OPERATOR_NEW (opnew) = 1;
4354     push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
4355     push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
4356     if (flag_sized_deallocation)
4357       {
4358 	/* Also push the sized deallocation variants:
4359 	     void operator delete(void*, std::size_t) throw();
4360 	     void operator delete[](void*, std::size_t) throw();  */
4361 	tree void_ftype_ptr_size
4362 	  = build_function_type_list (void_type_node, ptr_type_node,
4363 				      size_type_node, NULL_TREE);
4364 	deltype = cp_build_type_attribute_variant (void_ftype_ptr_size,
4365 						   extvisattr);
4366 	deltype = build_exception_variant (deltype, empty_except_spec);
4367 	push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
4368 	push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
4369       }
4370 
4371     if (aligned_new_threshold)
4372       {
4373 	push_namespace (std_identifier);
4374 	tree align_id = get_identifier ("align_val_t");
4375 	align_type_node = start_enum (align_id, NULL_TREE, size_type_node,
4376 				      NULL_TREE, /*scoped*/true, NULL);
4377 	pop_namespace ();
4378 
4379 	/* operator new (size_t, align_val_t); */
4380 	newtype = build_function_type_list (ptr_type_node, size_type_node,
4381 					    align_type_node, NULL_TREE);
4382 	newtype = cp_build_type_attribute_variant (newtype, newattrs);
4383 	newtype = build_exception_variant (newtype, new_eh_spec);
4384 	opnew = push_cp_library_fn (NEW_EXPR, newtype, 0);
4385 	DECL_IS_MALLOC (opnew) = 1;
4386 	DECL_IS_OPERATOR_NEW (opnew) = 1;
4387 	opnew = push_cp_library_fn (VEC_NEW_EXPR, newtype, 0);
4388 	DECL_IS_MALLOC (opnew) = 1;
4389 	DECL_IS_OPERATOR_NEW (opnew) = 1;
4390 
4391 	/* operator delete (void *, align_val_t); */
4392 	deltype = build_function_type_list (void_type_node, ptr_type_node,
4393 					    align_type_node, NULL_TREE);
4394 	deltype = cp_build_type_attribute_variant (deltype, extvisattr);
4395 	deltype = build_exception_variant (deltype, empty_except_spec);
4396 	push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
4397 	push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
4398 
4399 	if (flag_sized_deallocation)
4400 	  {
4401 	    /* operator delete (void *, size_t, align_val_t); */
4402 	    deltype = build_function_type_list (void_type_node, ptr_type_node,
4403 						size_type_node, align_type_node,
4404 						NULL_TREE);
4405 	    deltype = cp_build_type_attribute_variant (deltype, extvisattr);
4406 	    deltype = build_exception_variant (deltype, empty_except_spec);
4407 	    push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
4408 	    push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
4409 	  }
4410       }
4411 
4412     nullptr_type_node = make_node (NULLPTR_TYPE);
4413     TYPE_SIZE (nullptr_type_node) = bitsize_int (GET_MODE_BITSIZE (ptr_mode));
4414     TYPE_SIZE_UNIT (nullptr_type_node) = size_int (GET_MODE_SIZE (ptr_mode));
4415     TYPE_UNSIGNED (nullptr_type_node) = 1;
4416     TYPE_PRECISION (nullptr_type_node) = GET_MODE_BITSIZE (ptr_mode);
4417     if (abi_version_at_least (9))
4418       SET_TYPE_ALIGN (nullptr_type_node, GET_MODE_ALIGNMENT (ptr_mode));
4419     SET_TYPE_MODE (nullptr_type_node, ptr_mode);
4420     record_builtin_type (RID_MAX, "decltype(nullptr)", nullptr_type_node);
4421     nullptr_node = build_int_cst (nullptr_type_node, 0);
4422   }
4423 
4424   abort_fndecl
4425     = build_library_fn_ptr ("__cxa_pure_virtual", void_ftype,
4426 			    ECF_NORETURN | ECF_NOTHROW | ECF_COLD);
4427 
4428   /* Perform other language dependent initializations.  */
4429   init_class_processing ();
4430   init_rtti_processing ();
4431   init_template_processing ();
4432 
4433   if (flag_exceptions)
4434     init_exception_processing ();
4435 
4436   if (! supports_one_only ())
4437     flag_weak = 0;
4438 
4439   make_fname_decl = cp_make_fname_decl;
4440   start_fname_decls ();
4441 
4442   /* Show we use EH for cleanups.  */
4443   if (flag_exceptions)
4444     using_eh_for_cleanups ();
4445 }
4446 
4447 /* Generate an initializer for a function naming variable from
4448    NAME. NAME may be NULL, to indicate a dependent name.  TYPE_P is
4449    filled in with the type of the init.  */
4450 
4451 tree
cp_fname_init(const char * name,tree * type_p)4452 cp_fname_init (const char* name, tree *type_p)
4453 {
4454   tree domain = NULL_TREE;
4455   tree type;
4456   tree init = NULL_TREE;
4457   size_t length = 0;
4458 
4459   if (name)
4460     {
4461       length = strlen (name);
4462       domain = build_index_type (size_int (length));
4463       init = build_string (length + 1, name);
4464     }
4465 
4466   type = cp_build_qualified_type (char_type_node, TYPE_QUAL_CONST);
4467   type = build_cplus_array_type (type, domain);
4468 
4469   *type_p = type;
4470 
4471   if (init)
4472     TREE_TYPE (init) = type;
4473   else
4474     init = error_mark_node;
4475 
4476   return init;
4477 }
4478 
4479 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give
4480    the decl, LOC is the location to give the decl, NAME is the
4481    initialization string and TYPE_DEP indicates whether NAME depended
4482    on the type of the function. We make use of that to detect
4483    __PRETTY_FUNCTION__ inside a template fn. This is being done lazily
4484    at the point of first use, so we mustn't push the decl now.  */
4485 
4486 static tree
cp_make_fname_decl(location_t loc,tree id,int type_dep)4487 cp_make_fname_decl (location_t loc, tree id, int type_dep)
4488 {
4489   const char *const name = (type_dep && processing_template_decl
4490 			    ? NULL : fname_as_string (type_dep));
4491   tree type;
4492   tree init = cp_fname_init (name, &type);
4493   tree decl = build_decl (loc, VAR_DECL, id, type);
4494 
4495   if (name)
4496     free (CONST_CAST (char *, name));
4497 
4498   TREE_STATIC (decl) = 1;
4499   TREE_READONLY (decl) = 1;
4500   DECL_ARTIFICIAL (decl) = 1;
4501 
4502   TREE_USED (decl) = 1;
4503 
4504   if (current_function_decl)
4505     {
4506       DECL_CONTEXT (decl) = current_function_decl;
4507       decl = pushdecl_outermost_localscope (decl);
4508       cp_finish_decl (decl, init, /*init_const_expr_p=*/false, NULL_TREE,
4509 		      LOOKUP_ONLYCONVERTING);
4510     }
4511   else
4512     {
4513       DECL_THIS_STATIC (decl) = true;
4514       pushdecl_top_level_and_finish (decl, init);
4515     }
4516 
4517   return decl;
4518 }
4519 
4520 static tree
builtin_function_1(tree decl,tree context,bool is_global)4521 builtin_function_1 (tree decl, tree context, bool is_global)
4522 {
4523   tree          id = DECL_NAME (decl);
4524   const char *name = IDENTIFIER_POINTER (id);
4525 
4526   retrofit_lang_decl (decl);
4527 
4528   DECL_ARTIFICIAL (decl) = 1;
4529   SET_DECL_LANGUAGE (decl, lang_c);
4530   /* Runtime library routines are, by definition, available in an
4531      external shared object.  */
4532   DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
4533   DECL_VISIBILITY_SPECIFIED (decl) = 1;
4534 
4535   DECL_CONTEXT (decl) = context;
4536 
4537   /* A function in the user's namespace should have an explicit
4538      declaration before it is used.  Mark the built-in function as
4539      anticipated but not actually declared.  */
4540   if (name[0] != '_' || name[1] != '_')
4541     DECL_ANTICIPATED (decl) = 1;
4542   else if (strncmp (name + 2, "builtin_", strlen ("builtin_")) != 0)
4543     {
4544       size_t len = strlen (name);
4545 
4546       /* Treat __*_chk fortification functions as anticipated as well,
4547 	 unless they are __builtin_*.  */
4548       if (len > strlen ("___chk")
4549 	  && memcmp (name + len - strlen ("_chk"),
4550 		     "_chk", strlen ("_chk") + 1) == 0)
4551 	DECL_ANTICIPATED (decl) = 1;
4552     }
4553 
4554   if (is_global)
4555     pushdecl_top_level (decl);
4556   else
4557     pushdecl (decl);
4558 
4559   return decl;
4560 }
4561 
4562 tree
cxx_builtin_function(tree decl)4563 cxx_builtin_function (tree decl)
4564 {
4565   tree          id = DECL_NAME (decl);
4566   const char *name = IDENTIFIER_POINTER (id);
4567   /* All builtins that don't begin with an '_' should additionally
4568      go in the 'std' namespace.  */
4569   if (name[0] != '_')
4570     {
4571       tree decl2 = copy_node(decl);
4572       push_namespace (std_identifier);
4573       builtin_function_1 (decl2, std_node, false);
4574       pop_namespace ();
4575     }
4576 
4577   return builtin_function_1 (decl, NULL_TREE, false);
4578 }
4579 
4580 /* Like cxx_builtin_function, but guarantee the function is added to the global
4581    scope.  This is to allow function specific options to add new machine
4582    dependent builtins when the target ISA changes via attribute((target(...)))
4583    which saves space on program startup if the program does not use non-generic
4584    ISAs.  */
4585 
4586 tree
cxx_builtin_function_ext_scope(tree decl)4587 cxx_builtin_function_ext_scope (tree decl)
4588 {
4589 
4590   tree          id = DECL_NAME (decl);
4591   const char *name = IDENTIFIER_POINTER (id);
4592   /* All builtins that don't begin with an '_' should additionally
4593      go in the 'std' namespace.  */
4594   if (name[0] != '_')
4595     {
4596       tree decl2 = copy_node(decl);
4597       push_namespace (std_identifier);
4598       builtin_function_1 (decl2, std_node, true);
4599       pop_namespace ();
4600     }
4601 
4602   return builtin_function_1 (decl, NULL_TREE, true);
4603 }
4604 
4605 /* Generate a FUNCTION_DECL with the typical flags for a runtime library
4606    function.  Not called directly.  */
4607 
4608 static tree
build_library_fn(tree name,enum tree_code operator_code,tree type,int ecf_flags)4609 build_library_fn (tree name, enum tree_code operator_code, tree type,
4610 		  int ecf_flags)
4611 {
4612   tree fn = build_lang_decl (FUNCTION_DECL, name, type);
4613   DECL_EXTERNAL (fn) = 1;
4614   TREE_PUBLIC (fn) = 1;
4615   DECL_ARTIFICIAL (fn) = 1;
4616   DECL_OVERLOADED_OPERATOR_CODE_RAW (fn)
4617     = OVL_OP_INFO (false, operator_code)->ovl_op_code;
4618   SET_DECL_LANGUAGE (fn, lang_c);
4619   /* Runtime library routines are, by definition, available in an
4620      external shared object.  */
4621   DECL_VISIBILITY (fn) = VISIBILITY_DEFAULT;
4622   DECL_VISIBILITY_SPECIFIED (fn) = 1;
4623   set_call_expr_flags (fn, ecf_flags);
4624   return fn;
4625 }
4626 
4627 /* Returns the _DECL for a library function with C++ linkage.  */
4628 
4629 static tree
build_cp_library_fn(tree name,enum tree_code operator_code,tree type,int ecf_flags)4630 build_cp_library_fn (tree name, enum tree_code operator_code, tree type,
4631 		     int ecf_flags)
4632 {
4633   tree fn = build_library_fn (name, operator_code, type, ecf_flags);
4634   DECL_CONTEXT (fn) = FROB_CONTEXT (current_namespace);
4635   SET_DECL_LANGUAGE (fn, lang_cplusplus);
4636   return fn;
4637 }
4638 
4639 /* Like build_library_fn, but takes a C string instead of an
4640    IDENTIFIER_NODE.  */
4641 
4642 tree
build_library_fn_ptr(const char * name,tree type,int ecf_flags)4643 build_library_fn_ptr (const char* name, tree type, int ecf_flags)
4644 {
4645   return build_library_fn (get_identifier (name), ERROR_MARK, type, ecf_flags);
4646 }
4647 
4648 /* Like build_cp_library_fn, but takes a C string instead of an
4649    IDENTIFIER_NODE.  */
4650 
4651 tree
build_cp_library_fn_ptr(const char * name,tree type,int ecf_flags)4652 build_cp_library_fn_ptr (const char* name, tree type, int ecf_flags)
4653 {
4654   return build_cp_library_fn (get_identifier (name), ERROR_MARK, type,
4655 			      ecf_flags);
4656 }
4657 
4658 /* Like build_library_fn, but also pushes the function so that we will
4659    be able to find it via get_global_binding.  Also, the function
4660    may throw exceptions listed in RAISES.  */
4661 
4662 tree
push_library_fn(tree name,tree type,tree raises,int ecf_flags)4663 push_library_fn (tree name, tree type, tree raises, int ecf_flags)
4664 {
4665   tree fn;
4666 
4667   if (raises)
4668     type = build_exception_variant (type, raises);
4669 
4670   fn = build_library_fn (name, ERROR_MARK, type, ecf_flags);
4671   pushdecl_top_level (fn);
4672   return fn;
4673 }
4674 
4675 /* Like build_cp_library_fn, but also pushes the function so that it
4676    will be found by normal lookup.  */
4677 
4678 static tree
push_cp_library_fn(enum tree_code operator_code,tree type,int ecf_flags)4679 push_cp_library_fn (enum tree_code operator_code, tree type,
4680 		    int ecf_flags)
4681 {
4682   tree fn = build_cp_library_fn (ovl_op_identifier (false, operator_code),
4683 				 operator_code, type, ecf_flags);
4684   pushdecl (fn);
4685   if (flag_tm)
4686     apply_tm_attr (fn, get_identifier ("transaction_safe"));
4687   return fn;
4688 }
4689 
4690 /* Like push_library_fn, but takes a TREE_LIST of parm types rather than
4691    a FUNCTION_TYPE.  */
4692 
4693 tree
push_void_library_fn(tree name,tree parmtypes,int ecf_flags)4694 push_void_library_fn (tree name, tree parmtypes, int ecf_flags)
4695 {
4696   tree type = build_function_type (void_type_node, parmtypes);
4697   return push_library_fn (name, type, NULL_TREE, ecf_flags);
4698 }
4699 
4700 /* Like push_library_fn, but also note that this function throws
4701    and does not return.  Used for __throw_foo and the like.  */
4702 
4703 tree
push_throw_library_fn(tree name,tree type)4704 push_throw_library_fn (tree name, tree type)
4705 {
4706   tree fn = push_library_fn (name, type, NULL_TREE, ECF_NORETURN | ECF_COLD);
4707   return fn;
4708 }
4709 
4710 /* When we call finish_struct for an anonymous union, we create
4711    default copy constructors and such.  But, an anonymous union
4712    shouldn't have such things; this function undoes the damage to the
4713    anonymous union type T.
4714 
4715    (The reason that we create the synthesized methods is that we don't
4716    distinguish `union { int i; }' from `typedef union { int i; } U'.
4717    The first is an anonymous union; the second is just an ordinary
4718    union type.)  */
4719 
4720 void
fixup_anonymous_aggr(tree t)4721 fixup_anonymous_aggr (tree t)
4722 {
4723   /* Wipe out memory of synthesized methods.  */
4724   TYPE_HAS_USER_CONSTRUCTOR (t) = 0;
4725   TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0;
4726   TYPE_HAS_COPY_CTOR (t) = 0;
4727   TYPE_HAS_CONST_COPY_CTOR (t) = 0;
4728   TYPE_HAS_COPY_ASSIGN (t) = 0;
4729   TYPE_HAS_CONST_COPY_ASSIGN (t) = 0;
4730 
4731   /* Splice the implicitly generated functions out of TYPE_FIELDS.  */
4732   for (tree probe, *prev_p = &TYPE_FIELDS (t); (probe = *prev_p);)
4733     if (TREE_CODE (probe) == FUNCTION_DECL && DECL_ARTIFICIAL (probe))
4734       *prev_p = DECL_CHAIN (probe);
4735     else
4736       prev_p = &DECL_CHAIN (probe);
4737 
4738   /* Anonymous aggregates cannot have fields with ctors, dtors or complex
4739      assignment operators (because they cannot have these methods themselves).
4740      For anonymous unions this is already checked because they are not allowed
4741      in any union, otherwise we have to check it.  */
4742   if (TREE_CODE (t) != UNION_TYPE)
4743     {
4744       tree field, type;
4745 
4746       for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
4747 	if (TREE_CODE (field) == FIELD_DECL)
4748 	  {
4749 	    type = TREE_TYPE (field);
4750 	    if (CLASS_TYPE_P (type))
4751 	      {
4752 		if (TYPE_NEEDS_CONSTRUCTING (type))
4753 		  error ("member %q+#D with constructor not allowed "
4754 			 "in anonymous aggregate", field);
4755 		if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
4756 		  error ("member %q+#D with destructor not allowed "
4757 			 "in anonymous aggregate", field);
4758 		if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type))
4759 		  error ("member %q+#D with copy assignment operator "
4760 			 "not allowed in anonymous aggregate", field);
4761 	      }
4762 	  }
4763     }
4764 }
4765 
4766 /* Warn for an attribute located at LOCATION that appertains to the
4767    class type CLASS_TYPE that has not been properly placed after its
4768    class-key, in it class-specifier.  */
4769 
4770 void
warn_misplaced_attr_for_class_type(source_location location,tree class_type)4771 warn_misplaced_attr_for_class_type (source_location location,
4772 				    tree class_type)
4773 {
4774   gcc_assert (OVERLOAD_TYPE_P (class_type));
4775 
4776   if (warning_at (location, OPT_Wattributes,
4777 		  "attribute ignored in declaration "
4778 		  "of %q#T", class_type))
4779     inform (location,
4780 	    "attribute for %q#T must follow the %qs keyword",
4781 	    class_type, class_key_or_enum_as_string (class_type));
4782 }
4783 
4784 /* Make sure that a declaration with no declarator is well-formed, i.e.
4785    just declares a tagged type or anonymous union.
4786 
4787    Returns the type declared; or NULL_TREE if none.  */
4788 
4789 tree
check_tag_decl(cp_decl_specifier_seq * declspecs,bool explicit_type_instantiation_p)4790 check_tag_decl (cp_decl_specifier_seq *declspecs,
4791 		bool explicit_type_instantiation_p)
4792 {
4793   int saw_friend = decl_spec_seq_has_spec_p (declspecs, ds_friend);
4794   int saw_typedef = decl_spec_seq_has_spec_p (declspecs, ds_typedef);
4795   /* If a class, struct, or enum type is declared by the DECLSPECS
4796      (i.e, if a class-specifier, enum-specifier, or non-typename
4797      elaborated-type-specifier appears in the DECLSPECS),
4798      DECLARED_TYPE is set to the corresponding type.  */
4799   tree declared_type = NULL_TREE;
4800   bool error_p = false;
4801 
4802   if (declspecs->multiple_types_p)
4803     error ("multiple types in one declaration");
4804   else if (declspecs->redefined_builtin_type)
4805     {
4806       if (!in_system_header_at (input_location))
4807 	permerror (declspecs->locations[ds_redefined_builtin_type_spec],
4808 		   "redeclaration of C++ built-in type %qT",
4809 		   declspecs->redefined_builtin_type);
4810       return NULL_TREE;
4811     }
4812 
4813   if (declspecs->type
4814       && TYPE_P (declspecs->type)
4815       && ((TREE_CODE (declspecs->type) != TYPENAME_TYPE
4816 	   && MAYBE_CLASS_TYPE_P (declspecs->type))
4817 	  || TREE_CODE (declspecs->type) == ENUMERAL_TYPE))
4818     declared_type = declspecs->type;
4819   else if (declspecs->type == error_mark_node)
4820     error_p = true;
4821   if (declared_type == NULL_TREE && ! saw_friend && !error_p)
4822     permerror (input_location, "declaration does not declare anything");
4823   else if (declared_type != NULL_TREE && type_uses_auto (declared_type))
4824     {
4825       error_at (declspecs->locations[ds_type_spec],
4826 		"%<auto%> can only be specified for variables "
4827 		"or function declarations");
4828       return error_mark_node;
4829     }
4830   /* Check for an anonymous union.  */
4831   else if (declared_type && RECORD_OR_UNION_CODE_P (TREE_CODE (declared_type))
4832 	   && TYPE_UNNAMED_P (declared_type))
4833     {
4834       /* 7/3 In a simple-declaration, the optional init-declarator-list
4835 	 can be omitted only when declaring a class (clause 9) or
4836 	 enumeration (7.2), that is, when the decl-specifier-seq contains
4837 	 either a class-specifier, an elaborated-type-specifier with
4838 	 a class-key (9.1), or an enum-specifier.  In these cases and
4839 	 whenever a class-specifier or enum-specifier is present in the
4840 	 decl-specifier-seq, the identifiers in these specifiers are among
4841 	 the names being declared by the declaration (as class-name,
4842 	 enum-names, or enumerators, depending on the syntax).  In such
4843 	 cases, and except for the declaration of an unnamed bit-field (9.6),
4844 	 the decl-specifier-seq shall introduce one or more names into the
4845 	 program, or shall redeclare a name introduced by a previous
4846 	 declaration.  [Example:
4847 	     enum { };			// ill-formed
4848 	     typedef class { };		// ill-formed
4849 	 --end example]  */
4850       if (saw_typedef)
4851 	{
4852 	  error ("missing type-name in typedef-declaration");
4853 	  return NULL_TREE;
4854 	}
4855       /* Anonymous unions are objects, so they can have specifiers.  */;
4856       SET_ANON_AGGR_TYPE_P (declared_type);
4857 
4858       if (TREE_CODE (declared_type) != UNION_TYPE
4859 	  && !in_system_header_at (input_location))
4860 	pedwarn (input_location, OPT_Wpedantic, "ISO C++ prohibits anonymous structs");
4861     }
4862 
4863   else
4864     {
4865       if (decl_spec_seq_has_spec_p (declspecs, ds_inline))
4866 	error_at (declspecs->locations[ds_inline],
4867 		  "%<inline%> can only be specified for functions");
4868       else if (decl_spec_seq_has_spec_p (declspecs, ds_virtual))
4869 	error_at (declspecs->locations[ds_virtual],
4870 		  "%<virtual%> can only be specified for functions");
4871       else if (saw_friend
4872 	       && (!current_class_type
4873 		   || current_scope () != current_class_type))
4874 	error_at (declspecs->locations[ds_friend],
4875 		  "%<friend%> can only be specified inside a class");
4876       else if (decl_spec_seq_has_spec_p (declspecs, ds_explicit))
4877 	error_at (declspecs->locations[ds_explicit],
4878 		  "%<explicit%> can only be specified for constructors");
4879       else if (declspecs->storage_class)
4880 	error_at (declspecs->locations[ds_storage_class],
4881 		  "a storage class can only be specified for objects "
4882 		  "and functions");
4883       else if (decl_spec_seq_has_spec_p (declspecs, ds_const))
4884 	error_at (declspecs->locations[ds_const],
4885 		  "%<const%> can only be specified for objects and "
4886 		  "functions");
4887       else if (decl_spec_seq_has_spec_p (declspecs, ds_volatile))
4888 	error_at (declspecs->locations[ds_volatile],
4889 		  "%<volatile%> can only be specified for objects and "
4890 		  "functions");
4891       else if (decl_spec_seq_has_spec_p (declspecs, ds_restrict))
4892 	error_at (declspecs->locations[ds_restrict],
4893 		  "%<__restrict%> can only be specified for objects and "
4894 		  "functions");
4895       else if (decl_spec_seq_has_spec_p (declspecs, ds_thread))
4896 	error_at (declspecs->locations[ds_thread],
4897 		  "%<__thread%> can only be specified for objects "
4898 		  "and functions");
4899       else if (saw_typedef)
4900 	warning_at (declspecs->locations[ds_typedef], 0,
4901 		    "%<typedef%> was ignored in this declaration");
4902       else if (decl_spec_seq_has_spec_p (declspecs,  ds_constexpr))
4903         error_at (declspecs->locations[ds_constexpr],
4904 		  "%<constexpr%> cannot be used for type declarations");
4905     }
4906 
4907   if (declspecs->attributes && warn_attributes && declared_type)
4908     {
4909       location_t loc;
4910       if (!CLASS_TYPE_P (declared_type)
4911 	  || !CLASSTYPE_TEMPLATE_INSTANTIATION (declared_type))
4912 	/* For a non-template class, use the name location.  */
4913 	loc = location_of (declared_type);
4914       else
4915 	/* For a template class (an explicit instantiation), use the
4916 	   current location.  */
4917 	loc = input_location;
4918 
4919       if (explicit_type_instantiation_p)
4920 	/* [dcl.attr.grammar]/4:
4921 
4922 	       No attribute-specifier-seq shall appertain to an explicit
4923 	       instantiation.  */
4924 	{
4925 	  if (warning_at (loc, OPT_Wattributes,
4926 			  "attribute ignored in explicit instantiation %q#T",
4927 			  declared_type))
4928 	    inform (loc,
4929 		    "no attribute can be applied to "
4930 		    "an explicit instantiation");
4931 	}
4932       else
4933 	warn_misplaced_attr_for_class_type (loc, declared_type);
4934     }
4935 
4936   return declared_type;
4937 }
4938 
4939 /* Called when a declaration is seen that contains no names to declare.
4940    If its type is a reference to a structure, union or enum inherited
4941    from a containing scope, shadow that tag name for the current scope
4942    with a forward reference.
4943    If its type defines a new named structure or union
4944    or defines an enum, it is valid but we need not do anything here.
4945    Otherwise, it is an error.
4946 
4947    C++: may have to grok the declspecs to learn about static,
4948    complain for anonymous unions.
4949 
4950    Returns the TYPE declared -- or NULL_TREE if none.  */
4951 
4952 tree
shadow_tag(cp_decl_specifier_seq * declspecs)4953 shadow_tag (cp_decl_specifier_seq *declspecs)
4954 {
4955   tree t = check_tag_decl (declspecs,
4956 			   /*explicit_type_instantiation_p=*/false);
4957 
4958   if (!t)
4959     return NULL_TREE;
4960 
4961   if (maybe_process_partial_specialization (t) == error_mark_node)
4962     return NULL_TREE;
4963 
4964   /* This is where the variables in an anonymous union are
4965      declared.  An anonymous union declaration looks like:
4966      union { ... } ;
4967      because there is no declarator after the union, the parser
4968      sends that declaration here.  */
4969   if (ANON_AGGR_TYPE_P (t))
4970     {
4971       fixup_anonymous_aggr (t);
4972 
4973       if (TYPE_FIELDS (t))
4974 	{
4975 	  tree decl = grokdeclarator (/*declarator=*/NULL,
4976 				      declspecs, NORMAL, 0, NULL);
4977 	  finish_anon_union (decl);
4978 	}
4979     }
4980 
4981   return t;
4982 }
4983 
4984 /* Decode a "typename", such as "int **", returning a ..._TYPE node.  */
4985 
4986 tree
groktypename(cp_decl_specifier_seq * type_specifiers,const cp_declarator * declarator,bool is_template_arg)4987 groktypename (cp_decl_specifier_seq *type_specifiers,
4988 	      const cp_declarator *declarator,
4989 	      bool is_template_arg)
4990 {
4991   tree attrs;
4992   tree type;
4993   enum decl_context context
4994     = is_template_arg ? TEMPLATE_TYPE_ARG : TYPENAME;
4995   attrs = type_specifiers->attributes;
4996   type_specifiers->attributes = NULL_TREE;
4997   type = grokdeclarator (declarator, type_specifiers, context, 0, &attrs);
4998   if (attrs && type != error_mark_node)
4999     {
5000       if (CLASS_TYPE_P (type))
5001 	warning (OPT_Wattributes, "ignoring attributes applied to class type %qT "
5002 		 "outside of definition", type);
5003       else if (MAYBE_CLASS_TYPE_P (type))
5004 	/* A template type parameter or other dependent type.  */
5005 	warning (OPT_Wattributes, "ignoring attributes applied to dependent "
5006 		 "type %qT without an associated declaration", type);
5007       else
5008 	cplus_decl_attributes (&type, attrs, 0);
5009     }
5010   return type;
5011 }
5012 
5013 /* Process a DECLARATOR for a function-scope variable declaration,
5014    namespace-scope variable declaration, or function declaration.
5015    (Function definitions go through start_function; class member
5016    declarations appearing in the body of the class go through
5017    grokfield.)  The DECL corresponding to the DECLARATOR is returned.
5018    If an error occurs, the error_mark_node is returned instead.
5019 
5020    DECLSPECS are the decl-specifiers for the declaration.  INITIALIZED is
5021    SD_INITIALIZED if an explicit initializer is present, or SD_DEFAULTED
5022    for an explicitly defaulted function, or SD_DELETED for an explicitly
5023    deleted function, but 0 (SD_UNINITIALIZED) if this is a variable
5024    implicitly initialized via a default constructor.  ATTRIBUTES and
5025    PREFIX_ATTRIBUTES are GNU attributes associated with this declaration.
5026 
5027    The scope represented by the context of the returned DECL is pushed
5028    (if it is not the global namespace) and is assigned to
5029    *PUSHED_SCOPE_P.  The caller is then responsible for calling
5030    pop_scope on *PUSHED_SCOPE_P if it is set.  */
5031 
5032 tree
start_decl(const cp_declarator * declarator,cp_decl_specifier_seq * declspecs,int initialized,tree attributes,tree prefix_attributes,tree * pushed_scope_p)5033 start_decl (const cp_declarator *declarator,
5034 	    cp_decl_specifier_seq *declspecs,
5035 	    int initialized,
5036 	    tree attributes,
5037 	    tree prefix_attributes,
5038 	    tree *pushed_scope_p)
5039 {
5040   tree decl;
5041   tree context;
5042   bool was_public;
5043   int flags;
5044   bool alias;
5045 
5046   *pushed_scope_p = NULL_TREE;
5047 
5048   /* An object declared as __attribute__((deprecated)) suppresses
5049      warnings of uses of other deprecated items.  */
5050   if (lookup_attribute ("deprecated", attributes))
5051     deprecated_state = DEPRECATED_SUPPRESS;
5052 
5053   attributes = chainon (attributes, prefix_attributes);
5054 
5055   decl = grokdeclarator (declarator, declspecs, NORMAL, initialized,
5056 			 &attributes);
5057 
5058   deprecated_state = DEPRECATED_NORMAL;
5059 
5060   if (decl == NULL_TREE || VOID_TYPE_P (decl)
5061       || decl == error_mark_node)
5062     return error_mark_node;
5063 
5064   context = CP_DECL_CONTEXT (decl);
5065   if (context != global_namespace)
5066     *pushed_scope_p = push_scope (context);
5067 
5068   /* Is it valid for this decl to have an initializer at all?
5069      If not, set INITIALIZED to zero, which will indirectly
5070      tell `cp_finish_decl' to ignore the initializer once it is parsed.  */
5071   if (initialized
5072       && TREE_CODE (decl) == TYPE_DECL)
5073     {
5074       error ("typedef %qD is initialized (use decltype instead)", decl);
5075       return error_mark_node;
5076     }
5077 
5078   if (initialized)
5079     {
5080       if (! toplevel_bindings_p ()
5081 	  && DECL_EXTERNAL (decl))
5082 	warning (0, "declaration of %q#D has %<extern%> and is initialized",
5083 		 decl);
5084       DECL_EXTERNAL (decl) = 0;
5085       if (toplevel_bindings_p ())
5086 	TREE_STATIC (decl) = 1;
5087     }
5088   alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl)) != 0;
5089 
5090   if (alias && TREE_CODE (decl) == FUNCTION_DECL)
5091     record_key_method_defined (decl);
5092 
5093   /* If this is a typedef that names the class for linkage purposes
5094      (7.1.3p8), apply any attributes directly to the type.  */
5095   if (TREE_CODE (decl) == TYPE_DECL
5096       && OVERLOAD_TYPE_P (TREE_TYPE (decl))
5097       && decl == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (decl))))
5098     flags = ATTR_FLAG_TYPE_IN_PLACE;
5099   else
5100     flags = 0;
5101 
5102   /* Set attributes here so if duplicate decl, will have proper attributes.  */
5103   cplus_decl_attributes (&decl, attributes, flags);
5104 
5105   /* Dllimported symbols cannot be defined.  Static data members (which
5106      can be initialized in-class and dllimported) go through grokfield,
5107      not here, so we don't need to exclude those decls when checking for
5108      a definition.  */
5109   if (initialized && DECL_DLLIMPORT_P (decl))
5110     {
5111       error ("definition of %q#D is marked %<dllimport%>", decl);
5112       DECL_DLLIMPORT_P (decl) = 0;
5113     }
5114 
5115   /* If #pragma weak was used, mark the decl weak now.  */
5116   if (!processing_template_decl && !DECL_DECOMPOSITION_P (decl))
5117     maybe_apply_pragma_weak (decl);
5118 
5119   if (TREE_CODE (decl) == FUNCTION_DECL
5120       && DECL_DECLARED_INLINE_P (decl)
5121       && DECL_UNINLINABLE (decl)
5122       && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
5123     warning_at (DECL_SOURCE_LOCATION (decl), 0,
5124 		"inline function %qD given attribute noinline", decl);
5125 
5126   if (TYPE_P (context) && COMPLETE_TYPE_P (complete_type (context)))
5127     {
5128       bool this_tmpl = (processing_template_decl
5129 			> template_class_depth (context));
5130       if (VAR_P (decl))
5131 	{
5132 	  tree field = lookup_field (context, DECL_NAME (decl), 0, false);
5133 	  if (field == NULL_TREE
5134 	      || !(VAR_P (field) || variable_template_p (field)))
5135 	    error ("%q+#D is not a static data member of %q#T", decl, context);
5136 	  else if (variable_template_p (field)
5137 		   && (DECL_LANG_SPECIFIC (decl)
5138 		       && DECL_TEMPLATE_SPECIALIZATION (decl)))
5139 	    /* OK, specialization was already checked.  */;
5140 	  else if (variable_template_p (field) && !this_tmpl)
5141 	    {
5142 	      error_at (DECL_SOURCE_LOCATION (decl),
5143 			"non-member-template declaration of %qD", decl);
5144 	      inform (DECL_SOURCE_LOCATION (field), "does not match "
5145 		      "member template declaration here");
5146 	      return error_mark_node;
5147 	    }
5148 	  else
5149 	    {
5150 	      if (variable_template_p (field))
5151 		field = DECL_TEMPLATE_RESULT (field);
5152 
5153 	      if (DECL_CONTEXT (field) != context)
5154 		{
5155 		  if (!same_type_p (DECL_CONTEXT (field), context))
5156 		    permerror (input_location, "ISO C++ does not permit %<%T::%D%> "
5157 			       "to be defined as %<%T::%D%>",
5158 			       DECL_CONTEXT (field), DECL_NAME (decl),
5159 			       context, DECL_NAME (decl));
5160 		  DECL_CONTEXT (decl) = DECL_CONTEXT (field);
5161 		}
5162 	      /* Static data member are tricky; an in-class initialization
5163 		 still doesn't provide a definition, so the in-class
5164 		 declaration will have DECL_EXTERNAL set, but will have an
5165 		 initialization.  Thus, duplicate_decls won't warn
5166 		 about this situation, and so we check here.  */
5167 	      if (initialized && DECL_INITIALIZED_IN_CLASS_P (field))
5168 		error ("duplicate initialization of %qD", decl);
5169 	      field = duplicate_decls (decl, field,
5170 				       /*newdecl_is_friend=*/false);
5171 	      if (field == error_mark_node)
5172 		return error_mark_node;
5173 	      else if (field)
5174 		decl = field;
5175 	    }
5176 	}
5177       else
5178 	{
5179 	  tree field = check_classfn (context, decl,
5180 				      this_tmpl
5181 				      ? current_template_parms
5182 				      : NULL_TREE);
5183 	  if (field && field != error_mark_node
5184 	      && duplicate_decls (decl, field,
5185 				 /*newdecl_is_friend=*/false))
5186 	    decl = field;
5187 	}
5188 
5189       /* cp_finish_decl sets DECL_EXTERNAL if DECL_IN_AGGR_P is set.  */
5190       DECL_IN_AGGR_P (decl) = 0;
5191       /* Do not mark DECL as an explicit specialization if it was not
5192 	 already marked as an instantiation; a declaration should
5193 	 never be marked as a specialization unless we know what
5194 	 template is being specialized.  */
5195       if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
5196 	{
5197 	  SET_DECL_TEMPLATE_SPECIALIZATION (decl);
5198 	  if (TREE_CODE (decl) == FUNCTION_DECL)
5199 	    DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
5200 				  && DECL_DECLARED_INLINE_P (decl));
5201 	  else
5202 	    DECL_COMDAT (decl) = false;
5203 
5204 	  /* [temp.expl.spec] An explicit specialization of a static data
5205 	     member of a template is a definition if the declaration
5206 	     includes an initializer; otherwise, it is a declaration.
5207 
5208 	     We check for processing_specialization so this only applies
5209 	     to the new specialization syntax.  */
5210 	  if (!initialized && processing_specialization)
5211 	    DECL_EXTERNAL (decl) = 1;
5212 	}
5213 
5214       if (DECL_EXTERNAL (decl) && ! DECL_TEMPLATE_SPECIALIZATION (decl)
5215 	  /* Aliases are definitions. */
5216 	  && !alias)
5217 	permerror (input_location, "declaration of %q#D outside of class is not definition",
5218 		   decl);
5219     }
5220 
5221   was_public = TREE_PUBLIC (decl);
5222 
5223   /* Enter this declaration into the symbol table.  Don't push the plain
5224      VAR_DECL for a variable template.  */
5225   if (!template_parm_scope_p ()
5226       || !VAR_P (decl))
5227     decl = maybe_push_decl (decl);
5228 
5229   if (processing_template_decl)
5230     decl = push_template_decl (decl);
5231   if (decl == error_mark_node)
5232     return error_mark_node;
5233 
5234   if (VAR_P (decl)
5235       && DECL_NAMESPACE_SCOPE_P (decl) && !TREE_PUBLIC (decl) && !was_public
5236       && !DECL_THIS_STATIC (decl) && !DECL_ARTIFICIAL (decl))
5237     {
5238       /* This is a const variable with implicit 'static'.  Set
5239 	 DECL_THIS_STATIC so we can tell it from variables that are
5240 	 !TREE_PUBLIC because of the anonymous namespace.  */
5241       gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (decl)) || errorcount);
5242       DECL_THIS_STATIC (decl) = 1;
5243     }
5244 
5245   if (current_function_decl && VAR_P (decl)
5246       && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
5247     {
5248       bool ok = false;
5249       if (CP_DECL_THREAD_LOCAL_P (decl))
5250 	error ("%qD declared %<thread_local%> in %<constexpr%> function",
5251 	       decl);
5252       else if (TREE_STATIC (decl))
5253 	error ("%qD declared %<static%> in %<constexpr%> function", decl);
5254       else
5255 	ok = true;
5256       if (!ok)
5257 	cp_function_chain->invalid_constexpr = true;
5258     }
5259 
5260   if (!processing_template_decl && VAR_P (decl))
5261     start_decl_1 (decl, initialized);
5262 
5263   return decl;
5264 }
5265 
5266 /* Process the declaration of a variable DECL.  INITIALIZED is true
5267    iff DECL is explicitly initialized.  (INITIALIZED is false if the
5268    variable is initialized via an implicitly-called constructor.)
5269    This function must be called for ordinary variables (including, for
5270    example, implicit instantiations of templates), but must not be
5271    called for template declarations.  */
5272 
5273 void
start_decl_1(tree decl,bool initialized)5274 start_decl_1 (tree decl, bool initialized)
5275 {
5276   tree type;
5277   bool complete_p;
5278   bool aggregate_definition_p;
5279 
5280   gcc_assert (!processing_template_decl);
5281 
5282   if (error_operand_p (decl))
5283     return;
5284 
5285   gcc_assert (VAR_P (decl));
5286 
5287   type = TREE_TYPE (decl);
5288   complete_p = COMPLETE_TYPE_P (type);
5289   aggregate_definition_p = MAYBE_CLASS_TYPE_P (type) && !DECL_EXTERNAL (decl);
5290 
5291   /* If an explicit initializer is present, or if this is a definition
5292      of an aggregate, then we need a complete type at this point.
5293      (Scalars are always complete types, so there is nothing to
5294      check.)  This code just sets COMPLETE_P; errors (if necessary)
5295      are issued below.  */
5296   if ((initialized || aggregate_definition_p)
5297       && !complete_p
5298       && COMPLETE_TYPE_P (complete_type (type)))
5299     {
5300       complete_p = true;
5301       /* We will not yet have set TREE_READONLY on DECL if the type
5302 	 was "const", but incomplete, before this point.  But, now, we
5303 	 have a complete type, so we can try again.  */
5304       cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
5305     }
5306 
5307   if (initialized)
5308     /* Is it valid for this decl to have an initializer at all?  */
5309     {
5310       /* Don't allow initializations for incomplete types except for
5311 	 arrays which might be completed by the initialization.  */
5312       if (complete_p)
5313 	;			/* A complete type is ok.  */
5314       else if (type_uses_auto (type))
5315 	; 			/* An auto type is ok.  */
5316       else if (TREE_CODE (type) != ARRAY_TYPE)
5317 	{
5318 	  error ("variable %q#D has initializer but incomplete type", decl);
5319 	  type = TREE_TYPE (decl) = error_mark_node;
5320 	}
5321       else if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
5322 	{
5323 	  if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
5324 	    error ("elements of array %q#D have incomplete type", decl);
5325 	  /* else we already gave an error in start_decl.  */
5326 	}
5327     }
5328   else if (aggregate_definition_p && !complete_p)
5329     {
5330       if (type_uses_auto (type))
5331 	gcc_assert (CLASS_PLACEHOLDER_TEMPLATE (type));
5332       else
5333 	{
5334 	  error ("aggregate %q#D has incomplete type and cannot be defined",
5335 		 decl);
5336 	  /* Change the type so that assemble_variable will give
5337 	     DECL an rtl we can live with: (mem (const_int 0)).  */
5338 	  type = TREE_TYPE (decl) = error_mark_node;
5339 	}
5340     }
5341 
5342   /* Create a new scope to hold this declaration if necessary.
5343      Whether or not a new scope is necessary cannot be determined
5344      until after the type has been completed; if the type is a
5345      specialization of a class template it is not until after
5346      instantiation has occurred that TYPE_HAS_NONTRIVIAL_DESTRUCTOR
5347      will be set correctly.  */
5348   maybe_push_cleanup_level (type);
5349 }
5350 
5351 /* Handle initialization of references.  DECL, TYPE, and INIT have the
5352    same meaning as in cp_finish_decl.  *CLEANUP must be NULL on entry,
5353    but will be set to a new CLEANUP_STMT if a temporary is created
5354    that must be destroyed subsequently.
5355 
5356    Returns an initializer expression to use to initialize DECL, or
5357    NULL if the initialization can be performed statically.
5358 
5359    Quotes on semantics can be found in ARM 8.4.3.  */
5360 
5361 static tree
grok_reference_init(tree decl,tree type,tree init,int flags)5362 grok_reference_init (tree decl, tree type, tree init, int flags)
5363 {
5364   if (init == NULL_TREE)
5365     {
5366       if ((DECL_LANG_SPECIFIC (decl) == 0
5367 	   || DECL_IN_AGGR_P (decl) == 0)
5368 	  && ! DECL_THIS_EXTERN (decl))
5369 	error ("%qD declared as reference but not initialized", decl);
5370       return NULL_TREE;
5371     }
5372 
5373   if (TREE_CODE (init) == TREE_LIST)
5374     init = build_x_compound_expr_from_list (init, ELK_INIT,
5375 					    tf_warning_or_error);
5376 
5377   tree ttype = TREE_TYPE (type);
5378   if (TREE_CODE (ttype) != ARRAY_TYPE
5379       && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
5380     /* Note: default conversion is only called in very special cases.  */
5381     init = decay_conversion (init, tf_warning_or_error);
5382 
5383   /* check_initializer handles this for non-reference variables, but for
5384      references we need to do it here or the initializer will get the
5385      incomplete array type and confuse later calls to
5386      cp_complete_array_type.  */
5387   if (TREE_CODE (ttype) == ARRAY_TYPE
5388       && TYPE_DOMAIN (ttype) == NULL_TREE
5389       && (BRACE_ENCLOSED_INITIALIZER_P (init)
5390 	  || TREE_CODE (init) == STRING_CST))
5391     {
5392       cp_complete_array_type (&ttype, init, false);
5393       if (ttype != TREE_TYPE (type))
5394 	type = cp_build_reference_type (ttype, TYPE_REF_IS_RVALUE (type));
5395     }
5396 
5397   /* Convert INIT to the reference type TYPE.  This may involve the
5398      creation of a temporary, whose lifetime must be the same as that
5399      of the reference.  If so, a DECL_EXPR for the temporary will be
5400      added just after the DECL_EXPR for DECL.  That's why we don't set
5401      DECL_INITIAL for local references (instead assigning to them
5402      explicitly); we need to allow the temporary to be initialized
5403      first.  */
5404   return initialize_reference (type, init, flags,
5405 			       tf_warning_or_error);
5406 }
5407 
5408 /* Designated initializers in arrays are not supported in GNU C++.
5409    The parser cannot detect this error since it does not know whether
5410    a given brace-enclosed initializer is for a class type or for an
5411    array.  This function checks that CE does not use a designated
5412    initializer.  If it does, an error is issued.  Returns true if CE
5413    is valid, i.e., does not have a designated initializer.  */
5414 
5415 bool
check_array_designated_initializer(constructor_elt * ce,unsigned HOST_WIDE_INT index)5416 check_array_designated_initializer (constructor_elt *ce,
5417 				    unsigned HOST_WIDE_INT index)
5418 {
5419   /* Designated initializers for array elements are not supported.  */
5420   if (ce->index)
5421     {
5422       /* The parser only allows identifiers as designated
5423 	 initializers.  */
5424       if (ce->index == error_mark_node)
5425 	{
5426 	  error ("name used in a GNU-style designated "
5427 		 "initializer for an array");
5428 	  return false;
5429 	}
5430       else if (identifier_p (ce->index))
5431 	{
5432 	  error ("name %qD used in a GNU-style designated "
5433 		 "initializer for an array", ce->index);
5434 	  return false;
5435 	}
5436 
5437       tree ce_index = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5438 						  ce->index, true);
5439       if (ce_index
5440 	  && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (ce_index))
5441 	  && (TREE_CODE (ce_index = fold_non_dependent_expr (ce_index))
5442 	      == INTEGER_CST))
5443 	{
5444 	  /* A C99 designator is OK if it matches the current index.  */
5445 	  if (wi::to_wide (ce_index) == index)
5446 	    {
5447 	      ce->index = ce_index;
5448 	      return true;
5449 	    }
5450 	  else
5451 	    sorry ("non-trivial designated initializers not supported");
5452 	}
5453       else
5454 	error ("C99 designator %qE is not an integral constant-expression",
5455 	       ce->index);
5456 
5457       return false;
5458     }
5459 
5460   return true;
5461 }
5462 
5463 /* When parsing `int a[] = {1, 2};' we don't know the size of the
5464    array until we finish parsing the initializer.  If that's the
5465    situation we're in, update DECL accordingly.  */
5466 
5467 static void
maybe_deduce_size_from_array_init(tree decl,tree init)5468 maybe_deduce_size_from_array_init (tree decl, tree init)
5469 {
5470   tree type = TREE_TYPE (decl);
5471 
5472   if (TREE_CODE (type) == ARRAY_TYPE
5473       && TYPE_DOMAIN (type) == NULL_TREE
5474       && TREE_CODE (decl) != TYPE_DECL)
5475     {
5476       /* do_default is really a C-ism to deal with tentative definitions.
5477 	 But let's leave it here to ease the eventual merge.  */
5478       int do_default = !DECL_EXTERNAL (decl);
5479       tree initializer = init ? init : DECL_INITIAL (decl);
5480       int failure = 0;
5481 
5482       /* Check that there are no designated initializers in INIT, as
5483 	 those are not supported in GNU C++, and as the middle-end
5484 	 will crash if presented with a non-numeric designated
5485 	 initializer.  */
5486       if (initializer && BRACE_ENCLOSED_INITIALIZER_P (initializer))
5487 	{
5488 	  vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initializer);
5489 	  constructor_elt *ce;
5490 	  HOST_WIDE_INT i;
5491 	  FOR_EACH_VEC_SAFE_ELT (v, i, ce)
5492 	    {
5493 	      if (instantiation_dependent_expression_p (ce->index))
5494 		return;
5495 	      if (!check_array_designated_initializer (ce, i))
5496 		failure = 1;
5497 	    }
5498 	}
5499 
5500       if (failure)
5501 	TREE_TYPE (decl) = error_mark_node;
5502       else
5503 	{
5504 	  failure = cp_complete_array_type (&TREE_TYPE (decl), initializer,
5505 					    do_default);
5506 	  if (failure == 1)
5507 	    {
5508 	      error_at (EXPR_LOC_OR_LOC (initializer,
5509 					 DECL_SOURCE_LOCATION (decl)),
5510 			"initializer fails to determine size of %qD", decl);
5511 	    }
5512 	  else if (failure == 2)
5513 	    {
5514 	      if (do_default)
5515 		{
5516 		  error_at (DECL_SOURCE_LOCATION (decl),
5517 			    "array size missing in %qD", decl);
5518 		}
5519 	      /* If a `static' var's size isn't known, make it extern as
5520 		 well as static, so it does not get allocated.  If it's not
5521 		 `static', then don't mark it extern; finish_incomplete_decl
5522 		 will give it a default size and it will get allocated.  */
5523 	      else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
5524 		DECL_EXTERNAL (decl) = 1;
5525 	    }
5526 	  else if (failure == 3)
5527 	    {
5528 	      error_at (DECL_SOURCE_LOCATION (decl),
5529 			"zero-size array %qD", decl);
5530 	    }
5531 	}
5532 
5533       cp_apply_type_quals_to_decl (cp_type_quals (TREE_TYPE (decl)), decl);
5534 
5535       relayout_decl (decl);
5536     }
5537 }
5538 
5539 /* Set DECL_SIZE, DECL_ALIGN, etc. for DECL (a VAR_DECL), and issue
5540    any appropriate error messages regarding the layout.  */
5541 
5542 static void
layout_var_decl(tree decl)5543 layout_var_decl (tree decl)
5544 {
5545   tree type;
5546 
5547   type = TREE_TYPE (decl);
5548   if (type == error_mark_node)
5549     return;
5550 
5551   /* If we haven't already laid out this declaration, do so now.
5552      Note that we must not call complete type for an external object
5553      because it's type might involve templates that we are not
5554      supposed to instantiate yet.  (And it's perfectly valid to say
5555      `extern X x' for some incomplete type `X'.)  */
5556   if (!DECL_EXTERNAL (decl))
5557     complete_type (type);
5558   if (!DECL_SIZE (decl)
5559       && TREE_TYPE (decl) != error_mark_node
5560       && complete_or_array_type_p (type))
5561     layout_decl (decl, 0);
5562 
5563   if (!DECL_EXTERNAL (decl) && DECL_SIZE (decl) == NULL_TREE)
5564     {
5565       /* An automatic variable with an incomplete type: that is an error.
5566 	 Don't talk about array types here, since we took care of that
5567 	 message in grokdeclarator.  */
5568       error_at (DECL_SOURCE_LOCATION (decl),
5569 		"storage size of %qD isn%'t known", decl);
5570       TREE_TYPE (decl) = error_mark_node;
5571     }
5572 #if 0
5573   /* Keep this code around in case we later want to control debug info
5574      based on whether a type is "used".  (jason 1999-11-11) */
5575 
5576   else if (!DECL_EXTERNAL (decl) && MAYBE_CLASS_TYPE_P (ttype))
5577     /* Let debugger know it should output info for this type.  */
5578     note_debug_info_needed (ttype);
5579 
5580   if (TREE_STATIC (decl) && DECL_CLASS_SCOPE_P (decl))
5581     note_debug_info_needed (DECL_CONTEXT (decl));
5582 #endif
5583 
5584   if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
5585       && DECL_SIZE (decl) != NULL_TREE
5586       && ! TREE_CONSTANT (DECL_SIZE (decl)))
5587     {
5588       if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
5589 	constant_expression_warning (DECL_SIZE (decl));
5590       else
5591 	{
5592 	  error_at (DECL_SOURCE_LOCATION (decl),
5593 		    "storage size of %qD isn%'t constant", decl);
5594 	  TREE_TYPE (decl) = error_mark_node;
5595 	}
5596     }
5597 }
5598 
5599 /* If a local static variable is declared in an inline function, or if
5600    we have a weak definition, we must endeavor to create only one
5601    instance of the variable at link-time.  */
5602 
5603 void
maybe_commonize_var(tree decl)5604 maybe_commonize_var (tree decl)
5605 {
5606   /* Static data in a function with comdat linkage also has comdat
5607      linkage.  */
5608   if ((TREE_STATIC (decl)
5609        /* Don't mess with __FUNCTION__.  */
5610        && ! DECL_ARTIFICIAL (decl)
5611        && DECL_FUNCTION_SCOPE_P (decl)
5612        && vague_linkage_p (DECL_CONTEXT (decl)))
5613       || (TREE_PUBLIC (decl) && DECL_INLINE_VAR_P (decl)))
5614     {
5615       if (flag_weak)
5616 	{
5617 	  /* With weak symbols, we simply make the variable COMDAT;
5618 	     that will cause copies in multiple translations units to
5619 	     be merged.  */
5620 	  comdat_linkage (decl);
5621 	}
5622       else
5623 	{
5624 	  if (DECL_INITIAL (decl) == NULL_TREE
5625 	      || DECL_INITIAL (decl) == error_mark_node)
5626 	    {
5627 	      /* Without weak symbols, we can use COMMON to merge
5628 		 uninitialized variables.  */
5629 	      TREE_PUBLIC (decl) = 1;
5630 	      DECL_COMMON (decl) = 1;
5631 	    }
5632 	  else
5633 	    {
5634 	      /* While for initialized variables, we must use internal
5635 		 linkage -- which means that multiple copies will not
5636 		 be merged.  */
5637 	      TREE_PUBLIC (decl) = 0;
5638 	      DECL_COMMON (decl) = 0;
5639 	      DECL_INTERFACE_KNOWN (decl) = 1;
5640 	      const char *msg;
5641 	      if (DECL_INLINE_VAR_P (decl))
5642 		msg = G_("sorry: semantics of inline variable "
5643 			 "%q#D are wrong (you%'ll wind up with "
5644 			 "multiple copies)");
5645 	      else
5646 		msg = G_("sorry: semantics of inline function "
5647 			 "static data %q#D are wrong (you%'ll wind "
5648 			 "up with multiple copies)");
5649 	      if (warning_at (DECL_SOURCE_LOCATION (decl), 0,
5650 			      msg, decl))
5651 		inform (DECL_SOURCE_LOCATION (decl),
5652 			"you can work around this by removing the initializer");
5653 	    }
5654 	}
5655     }
5656 }
5657 
5658 /* Issue an error message if DECL is an uninitialized const variable.
5659    CONSTEXPR_CONTEXT_P is true when the function is called in a constexpr
5660    context from potential_constant_expression.  Returns true if all is well,
5661    false otherwise.  */
5662 
5663 bool
check_for_uninitialized_const_var(tree decl,bool constexpr_context_p,tsubst_flags_t complain)5664 check_for_uninitialized_const_var (tree decl, bool constexpr_context_p,
5665 				   tsubst_flags_t complain)
5666 {
5667   tree type = strip_array_types (TREE_TYPE (decl));
5668 
5669   /* ``Unless explicitly declared extern, a const object does not have
5670      external linkage and must be initialized. ($8.4; $12.1)'' ARM
5671      7.1.6 */
5672   if (VAR_P (decl)
5673       && TREE_CODE (type) != REFERENCE_TYPE
5674       && (constexpr_context_p
5675 	  || CP_TYPE_CONST_P (type) || var_in_constexpr_fn (decl))
5676       && !DECL_NONTRIVIALLY_INITIALIZED_P (decl))
5677     {
5678       tree field = default_init_uninitialized_part (type);
5679       if (!field)
5680 	return true;
5681 
5682       if (!constexpr_context_p)
5683 	{
5684 	  if (CP_TYPE_CONST_P (type))
5685 	    {
5686 	      if (complain & tf_error)
5687 		permerror (DECL_SOURCE_LOCATION (decl),
5688 			   "uninitialized const %qD", decl);
5689 	    }
5690 	  else
5691 	    {
5692 	      if (!is_instantiation_of_constexpr (current_function_decl)
5693 		  && (complain & tf_error))
5694 		error_at (DECL_SOURCE_LOCATION (decl),
5695 			  "uninitialized variable %qD in %<constexpr%> "
5696 			  "function", decl);
5697 	      cp_function_chain->invalid_constexpr = true;
5698 	    }
5699 	}
5700       else if (complain & tf_error)
5701 	error_at (DECL_SOURCE_LOCATION (decl),
5702 		  "uninitialized variable %qD in %<constexpr%> context",
5703 		  decl);
5704 
5705       if (CLASS_TYPE_P (type) && (complain & tf_error))
5706 	{
5707 	  tree defaulted_ctor;
5708 
5709 	  inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
5710 		  "%q#T has no user-provided default constructor", type);
5711 	  defaulted_ctor = in_class_defaulted_default_constructor (type);
5712 	  if (defaulted_ctor)
5713 	    inform (DECL_SOURCE_LOCATION (defaulted_ctor),
5714 		    "constructor is not user-provided because it is "
5715 		    "explicitly defaulted in the class body");
5716 	  inform (DECL_SOURCE_LOCATION (field),
5717 		  "and the implicitly-defined constructor does not "
5718 		  "initialize %q#D", field);
5719 	}
5720 
5721       return false;
5722     }
5723 
5724   return true;
5725 }
5726 
5727 /* Structure holding the current initializer being processed by reshape_init.
5728    CUR is a pointer to the current element being processed, END is a pointer
5729    after the last element present in the initializer.  */
5730 struct reshape_iter
5731 {
5732   constructor_elt *cur;
5733   constructor_elt *end;
5734 };
5735 
5736 static tree reshape_init_r (tree, reshape_iter *, bool, tsubst_flags_t);
5737 
5738 /* FIELD is a FIELD_DECL or NULL.  In the former case, the value
5739    returned is the next FIELD_DECL (possibly FIELD itself) that can be
5740    initialized.  If there are no more such fields, the return value
5741    will be NULL.  */
5742 
5743 tree
next_initializable_field(tree field)5744 next_initializable_field (tree field)
5745 {
5746   while (field
5747 	 && (TREE_CODE (field) != FIELD_DECL
5748 	     || DECL_UNNAMED_BIT_FIELD (field)
5749 	     || (DECL_ARTIFICIAL (field)
5750 		 && !(cxx_dialect >= cxx17 && DECL_FIELD_IS_BASE (field)))))
5751     field = DECL_CHAIN (field);
5752 
5753   return field;
5754 }
5755 
5756 /* Return true for [dcl.init.list] direct-list-initialization from
5757    single element of enumeration with a fixed underlying type.  */
5758 
5759 bool
is_direct_enum_init(tree type,tree init)5760 is_direct_enum_init (tree type, tree init)
5761 {
5762   if (cxx_dialect >= cxx17
5763       && TREE_CODE (type) == ENUMERAL_TYPE
5764       && ENUM_FIXED_UNDERLYING_TYPE_P (type)
5765       && TREE_CODE (init) == CONSTRUCTOR
5766       && CONSTRUCTOR_IS_DIRECT_INIT (init)
5767       && CONSTRUCTOR_NELTS (init) == 1)
5768     return true;
5769   return false;
5770 }
5771 
5772 /* Subroutine of reshape_init_array and reshape_init_vector, which does
5773    the actual work. ELT_TYPE is the element type of the array. MAX_INDEX is an
5774    INTEGER_CST representing the size of the array minus one (the maximum index),
5775    or NULL_TREE if the array was declared without specifying the size. D is
5776    the iterator within the constructor.  */
5777 
5778 static tree
reshape_init_array_1(tree elt_type,tree max_index,reshape_iter * d,tsubst_flags_t complain)5779 reshape_init_array_1 (tree elt_type, tree max_index, reshape_iter *d,
5780 		      tsubst_flags_t complain)
5781 {
5782   tree new_init;
5783   bool sized_array_p = (max_index && TREE_CONSTANT (max_index));
5784   unsigned HOST_WIDE_INT max_index_cst = 0;
5785   unsigned HOST_WIDE_INT index;
5786 
5787   /* The initializer for an array is always a CONSTRUCTOR.  */
5788   new_init = build_constructor (init_list_type_node, NULL);
5789 
5790   if (sized_array_p)
5791     {
5792       /* Minus 1 is used for zero sized arrays.  */
5793       if (integer_all_onesp (max_index))
5794 	return new_init;
5795 
5796       if (tree_fits_uhwi_p (max_index))
5797 	max_index_cst = tree_to_uhwi (max_index);
5798       /* sizetype is sign extended, not zero extended.  */
5799       else
5800 	max_index_cst = tree_to_uhwi (fold_convert (size_type_node, max_index));
5801     }
5802 
5803   /* Loop until there are no more initializers.  */
5804   for (index = 0;
5805        d->cur != d->end && (!sized_array_p || index <= max_index_cst);
5806        ++index)
5807     {
5808       tree elt_init;
5809       constructor_elt *old_cur = d->cur;
5810 
5811       check_array_designated_initializer (d->cur, index);
5812       elt_init = reshape_init_r (elt_type, d, /*first_initializer_p=*/false,
5813 				 complain);
5814       if (elt_init == error_mark_node)
5815 	return error_mark_node;
5816       CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init),
5817 			      size_int (index), elt_init);
5818       if (!TREE_CONSTANT (elt_init))
5819 	TREE_CONSTANT (new_init) = false;
5820 
5821       /* This can happen with an invalid initializer (c++/54501).  */
5822       if (d->cur == old_cur && !sized_array_p)
5823 	break;
5824     }
5825 
5826   return new_init;
5827 }
5828 
5829 /* Subroutine of reshape_init_r, processes the initializers for arrays.
5830    Parameters are the same of reshape_init_r.  */
5831 
5832 static tree
reshape_init_array(tree type,reshape_iter * d,tsubst_flags_t complain)5833 reshape_init_array (tree type, reshape_iter *d, tsubst_flags_t complain)
5834 {
5835   tree max_index = NULL_TREE;
5836 
5837   gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
5838 
5839   if (TYPE_DOMAIN (type))
5840     max_index = array_type_nelts (type);
5841 
5842   return reshape_init_array_1 (TREE_TYPE (type), max_index, d, complain);
5843 }
5844 
5845 /* Subroutine of reshape_init_r, processes the initializers for vectors.
5846    Parameters are the same of reshape_init_r.  */
5847 
5848 static tree
reshape_init_vector(tree type,reshape_iter * d,tsubst_flags_t complain)5849 reshape_init_vector (tree type, reshape_iter *d, tsubst_flags_t complain)
5850 {
5851   tree max_index = NULL_TREE;
5852 
5853   gcc_assert (VECTOR_TYPE_P (type));
5854 
5855   if (COMPOUND_LITERAL_P (d->cur->value))
5856     {
5857       tree value = d->cur->value;
5858       if (!same_type_p (TREE_TYPE (value), type))
5859 	{
5860 	  if (complain & tf_error)
5861 	    error ("invalid type %qT as initializer for a vector of type %qT",
5862 		   TREE_TYPE (d->cur->value), type);
5863 	  value = error_mark_node;
5864 	}
5865       ++d->cur;
5866       return value;
5867     }
5868 
5869   /* For a vector, we initialize it as an array of the appropriate size.  */
5870   if (VECTOR_TYPE_P (type))
5871     max_index = size_int (TYPE_VECTOR_SUBPARTS (type) - 1);
5872 
5873   return reshape_init_array_1 (TREE_TYPE (type), max_index, d, complain);
5874 }
5875 
5876 /* Subroutine of reshape_init_r, processes the initializers for classes
5877    or union. Parameters are the same of reshape_init_r.  */
5878 
5879 static tree
reshape_init_class(tree type,reshape_iter * d,bool first_initializer_p,tsubst_flags_t complain)5880 reshape_init_class (tree type, reshape_iter *d, bool first_initializer_p,
5881 		    tsubst_flags_t complain)
5882 {
5883   tree field;
5884   tree new_init;
5885 
5886   gcc_assert (CLASS_TYPE_P (type));
5887 
5888   /* The initializer for a class is always a CONSTRUCTOR.  */
5889   new_init = build_constructor (init_list_type_node, NULL);
5890   field = next_initializable_field (TYPE_FIELDS (type));
5891 
5892   if (!field)
5893     {
5894       /* [dcl.init.aggr]
5895 
5896 	An initializer for an aggregate member that is an
5897 	empty class shall have the form of an empty
5898 	initializer-list {}.  */
5899       if (!first_initializer_p)
5900 	{
5901 	  if (complain & tf_error)
5902 	    error ("initializer for %qT must be brace-enclosed", type);
5903 	  return error_mark_node;
5904 	}
5905       return new_init;
5906     }
5907 
5908   /* Loop through the initializable fields, gathering initializers.  */
5909   while (d->cur != d->end)
5910     {
5911       tree field_init;
5912       constructor_elt *old_cur = d->cur;
5913 
5914       /* Handle designated initializers, as an extension.  */
5915       if (d->cur->index)
5916 	{
5917 	  if (d->cur->index == error_mark_node)
5918 	    return error_mark_node;
5919 
5920 	  if (TREE_CODE (d->cur->index) == FIELD_DECL)
5921 	    {
5922 	      /* We already reshaped this.  */
5923 	      if (field != d->cur->index)
5924 		{
5925 		  tree id = DECL_NAME (d->cur->index);
5926 		  gcc_assert (id);
5927 		  gcc_checking_assert (d->cur->index
5928 				       == get_class_binding (type, id, false));
5929 		  field = d->cur->index;
5930 		}
5931 	    }
5932 	  else if (TREE_CODE (d->cur->index) == IDENTIFIER_NODE)
5933 	    field = get_class_binding (type, d->cur->index, false);
5934 	  else
5935 	    {
5936 	      if (complain & tf_error)
5937 		error ("%<[%E] =%> used in a GNU-style designated initializer"
5938 		       " for class %qT", d->cur->index, type);
5939 	      return error_mark_node;
5940 	    }
5941 
5942 	  if (!field || TREE_CODE (field) != FIELD_DECL)
5943 	    {
5944 	      if (complain & tf_error)
5945 		error ("%qT has no non-static data member named %qD", type,
5946 		       d->cur->index);
5947 	      return error_mark_node;
5948 	    }
5949 	}
5950 
5951       /* If we processed all the member of the class, we are done.  */
5952       if (!field)
5953 	break;
5954 
5955       field_init = reshape_init_r (TREE_TYPE (field), d,
5956 				   /*first_initializer_p=*/false, complain);
5957       if (field_init == error_mark_node)
5958 	return error_mark_node;
5959 
5960       if (d->cur == old_cur && d->cur->index)
5961 	{
5962 	  /* This can happen with an invalid initializer for a flexible
5963 	     array member (c++/54441).  */
5964 	  if (complain & tf_error)
5965 	    error ("invalid initializer for %q#D", field);
5966 	  return error_mark_node;
5967 	}
5968 
5969       CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init), field, field_init);
5970 
5971       /* [dcl.init.aggr]
5972 
5973 	When a union  is  initialized with a brace-enclosed
5974 	initializer, the braces shall only contain an
5975 	initializer for the first member of the union.  */
5976       if (TREE_CODE (type) == UNION_TYPE)
5977 	break;
5978 
5979       field = next_initializable_field (DECL_CHAIN (field));
5980     }
5981 
5982   return new_init;
5983 }
5984 
5985 /* Subroutine of reshape_init_r.  We're in a context where C99 initializer
5986    designators are not valid; either complain or return true to indicate
5987    that reshape_init_r should return error_mark_node.  */
5988 
5989 static bool
has_designator_problem(reshape_iter * d,tsubst_flags_t complain)5990 has_designator_problem (reshape_iter *d, tsubst_flags_t complain)
5991 {
5992   if (d->cur->index)
5993     {
5994       if (complain & tf_error)
5995 	error ("C99 designator %qE outside aggregate initializer",
5996 	       d->cur->index);
5997       else
5998 	return true;
5999     }
6000   return false;
6001 }
6002 
6003 /* Subroutine of reshape_init, which processes a single initializer (part of
6004    a CONSTRUCTOR). TYPE is the type of the variable being initialized, D is the
6005    iterator within the CONSTRUCTOR which points to the initializer to process.
6006    FIRST_INITIALIZER_P is true if this is the first initializer of the
6007    outermost CONSTRUCTOR node.  */
6008 
6009 static tree
reshape_init_r(tree type,reshape_iter * d,bool first_initializer_p,tsubst_flags_t complain)6010 reshape_init_r (tree type, reshape_iter *d, bool first_initializer_p,
6011 		tsubst_flags_t complain)
6012 {
6013   tree init = d->cur->value;
6014 
6015   if (error_operand_p (init))
6016     return error_mark_node;
6017 
6018   if (first_initializer_p && !CP_AGGREGATE_TYPE_P (type)
6019       && has_designator_problem (d, complain))
6020     return error_mark_node;
6021 
6022   if (TREE_CODE (type) == COMPLEX_TYPE)
6023     {
6024       /* A complex type can be initialized from one or two initializers,
6025 	 but braces are not elided.  */
6026       d->cur++;
6027       if (BRACE_ENCLOSED_INITIALIZER_P (init))
6028 	{
6029 	  if (CONSTRUCTOR_NELTS (init) > 2)
6030 	    {
6031 	      if (complain & tf_error)
6032 		error ("too many initializers for %qT", type);
6033 	      else
6034 		return error_mark_node;
6035 	    }
6036 	}
6037       else if (first_initializer_p && d->cur != d->end)
6038 	{
6039 	  vec<constructor_elt, va_gc> *v = 0;
6040 	  CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, init);
6041 	  CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, d->cur->value);
6042 	  if (has_designator_problem (d, complain))
6043 	    return error_mark_node;
6044 	  d->cur++;
6045 	  init = build_constructor (init_list_type_node, v);
6046 	}
6047       return init;
6048     }
6049 
6050   /* A non-aggregate type is always initialized with a single
6051      initializer.  */
6052   if (!CP_AGGREGATE_TYPE_P (type))
6053     {
6054       /* It is invalid to initialize a non-aggregate type with a
6055 	 brace-enclosed initializer before C++0x.
6056 	 We need to check for BRACE_ENCLOSED_INITIALIZER_P here because
6057 	 of g++.old-deja/g++.mike/p7626.C: a pointer-to-member constant is
6058 	 a CONSTRUCTOR (with a record type).  */
6059       if (TREE_CODE (init) == CONSTRUCTOR
6060 	  /* Don't complain about a capture-init.  */
6061 	  && !CONSTRUCTOR_IS_DIRECT_INIT (init)
6062 	  && BRACE_ENCLOSED_INITIALIZER_P (init))  /* p7626.C */
6063 	{
6064 	  if (SCALAR_TYPE_P (type))
6065 	    {
6066 	      if (cxx_dialect < cxx11
6067 		  /* Isn't value-initialization.  */
6068 		  || CONSTRUCTOR_NELTS (init) > 0)
6069 		{
6070 		  if (complain & tf_error)
6071 		    error ("braces around scalar initializer for type %qT",
6072 			   type);
6073 		  init = error_mark_node;
6074 		}
6075 	    }
6076 	  else
6077 	    maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6078 	}
6079 
6080       d->cur++;
6081       return init;
6082     }
6083 
6084   /* "If T is a class type and the initializer list has a single element of
6085      type cv U, where U is T or a class derived from T, the object is
6086      initialized from that element."  Even if T is an aggregate.  */
6087   if (cxx_dialect >= cxx11 && (CLASS_TYPE_P (type) || VECTOR_TYPE_P (type))
6088       && first_initializer_p
6089       && d->end - d->cur == 1
6090       && reference_related_p (type, TREE_TYPE (init)))
6091     {
6092       d->cur++;
6093       return init;
6094     }
6095 
6096   /* [dcl.init.aggr]
6097 
6098      All implicit type conversions (clause _conv_) are considered when
6099      initializing the aggregate member with an initializer from an
6100      initializer-list.  If the initializer can initialize a member,
6101      the member is initialized.  Otherwise, if the member is itself a
6102      non-empty subaggregate, brace elision is assumed and the
6103      initializer is considered for the initialization of the first
6104      member of the subaggregate.  */
6105   if (TREE_CODE (init) != CONSTRUCTOR
6106       /* But don't try this for the first initializer, since that would be
6107 	 looking through the outermost braces; A a2 = { a1 }; is not a
6108 	 valid aggregate initialization.  */
6109       && !first_initializer_p
6110       && (same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (init))
6111 	  || can_convert_arg (type, TREE_TYPE (init), init, LOOKUP_NORMAL,
6112 			      complain)))
6113     {
6114       d->cur++;
6115       return init;
6116     }
6117 
6118   /* [dcl.init.string]
6119 
6120       A char array (whether plain char, signed char, or unsigned char)
6121       can be initialized by a string-literal (optionally enclosed in
6122       braces); a wchar_t array can be initialized by a wide
6123       string-literal (optionally enclosed in braces).  */
6124   if (TREE_CODE (type) == ARRAY_TYPE
6125       && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type))))
6126     {
6127       tree str_init = init;
6128 
6129       /* Strip one level of braces if and only if they enclose a single
6130 	 element (as allowed by [dcl.init.string]).  */
6131       if (!first_initializer_p
6132 	  && TREE_CODE (str_init) == CONSTRUCTOR
6133 	  && CONSTRUCTOR_NELTS (str_init) == 1)
6134 	{
6135 	  str_init = (*CONSTRUCTOR_ELTS (str_init))[0].value;
6136 	}
6137 
6138       /* If it's a string literal, then it's the initializer for the array
6139 	 as a whole. Otherwise, continue with normal initialization for
6140 	 array types (one value per array element).  */
6141       if (TREE_CODE (str_init) == STRING_CST)
6142 	{
6143 	  if (has_designator_problem (d, complain))
6144 	    return error_mark_node;
6145 	  d->cur++;
6146 	  return str_init;
6147 	}
6148     }
6149 
6150   /* The following cases are about aggregates. If we are not within a full
6151      initializer already, and there is not a CONSTRUCTOR, it means that there
6152      is a missing set of braces (that is, we are processing the case for
6153      which reshape_init exists).  */
6154   if (!first_initializer_p)
6155     {
6156       if (TREE_CODE (init) == CONSTRUCTOR)
6157 	{
6158 	  tree init_type = TREE_TYPE (init);
6159 	  if (init_type && TYPE_PTRMEMFUNC_P (init_type))
6160 	    /* There is no need to call reshape_init for pointer-to-member
6161 	       function initializers, as they are always constructed correctly
6162 	       by the front end.  Here we have e.g. {.__pfn=0B, .__delta=0},
6163 	       which is missing outermost braces.  We should warn below, and
6164 	       one of the routines below will wrap it in additional { }.  */;
6165 	  /* For a nested compound literal, proceed to specialized routines,
6166 	     to handle initialization of arrays and similar.  */
6167 	  else if (COMPOUND_LITERAL_P (init))
6168 	    gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (init));
6169 	  /* A CONSTRUCTOR of the target's type is a previously
6170 	     digested initializer.  */
6171 	  else if (same_type_ignoring_top_level_qualifiers_p (type, init_type))
6172 	    {
6173 	      ++d->cur;
6174 	      gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (init));
6175 	      return init;
6176 	    }
6177 	  else
6178 	    {
6179 	      /* Something that hasn't been reshaped yet.  */
6180 	      ++d->cur;
6181 	      gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
6182 	      return reshape_init (type, init, complain);
6183 	    }
6184 	}
6185 
6186       if (complain & tf_warning)
6187 	warning (OPT_Wmissing_braces,
6188 		 "missing braces around initializer for %qT",
6189 		 type);
6190     }
6191 
6192   /* Dispatch to specialized routines.  */
6193   if (CLASS_TYPE_P (type))
6194     return reshape_init_class (type, d, first_initializer_p, complain);
6195   else if (TREE_CODE (type) == ARRAY_TYPE)
6196     return reshape_init_array (type, d, complain);
6197   else if (VECTOR_TYPE_P (type))
6198     return reshape_init_vector (type, d, complain);
6199   else
6200     gcc_unreachable();
6201 }
6202 
6203 /* Undo the brace-elision allowed by [dcl.init.aggr] in a
6204    brace-enclosed aggregate initializer.
6205 
6206    INIT is the CONSTRUCTOR containing the list of initializers describing
6207    a brace-enclosed initializer for an entity of the indicated aggregate TYPE.
6208    It may not presently match the shape of the TYPE; for example:
6209 
6210      struct S { int a; int b; };
6211      struct S a[] = { 1, 2, 3, 4 };
6212 
6213    Here INIT will hold a vector of four elements, rather than a
6214    vector of two elements, each itself a vector of two elements.  This
6215    routine transforms INIT from the former form into the latter.  The
6216    revised CONSTRUCTOR node is returned.  */
6217 
6218 tree
reshape_init(tree type,tree init,tsubst_flags_t complain)6219 reshape_init (tree type, tree init, tsubst_flags_t complain)
6220 {
6221   vec<constructor_elt, va_gc> *v;
6222   reshape_iter d;
6223   tree new_init;
6224 
6225   gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
6226 
6227   v = CONSTRUCTOR_ELTS (init);
6228 
6229   /* An empty constructor does not need reshaping, and it is always a valid
6230      initializer.  */
6231   if (vec_safe_is_empty (v))
6232     return init;
6233 
6234   /* Handle [dcl.init.list] direct-list-initialization from
6235      single element of enumeration with a fixed underlying type.  */
6236   if (is_direct_enum_init (type, init))
6237     {
6238       tree elt = CONSTRUCTOR_ELT (init, 0)->value;
6239       type = cv_unqualified (type);
6240       if (check_narrowing (ENUM_UNDERLYING_TYPE (type), elt, complain))
6241 	{
6242 	  warning_sentinel w (warn_useless_cast);
6243 	  warning_sentinel w2 (warn_ignored_qualifiers);
6244 	  return cp_build_c_cast (type, elt, tf_warning_or_error);
6245 	}
6246       else
6247 	return error_mark_node;
6248     }
6249 
6250   /* Recurse on this CONSTRUCTOR.  */
6251   d.cur = &(*v)[0];
6252   d.end = d.cur + v->length ();
6253 
6254   new_init = reshape_init_r (type, &d, true, complain);
6255   if (new_init == error_mark_node)
6256     return error_mark_node;
6257 
6258   /* Make sure all the element of the constructor were used. Otherwise,
6259      issue an error about exceeding initializers.  */
6260   if (d.cur != d.end)
6261     {
6262       if (complain & tf_error)
6263 	error ("too many initializers for %qT", type);
6264       return error_mark_node;
6265     }
6266 
6267   if (CONSTRUCTOR_IS_DIRECT_INIT (init)
6268       && BRACE_ENCLOSED_INITIALIZER_P (new_init))
6269     CONSTRUCTOR_IS_DIRECT_INIT (new_init) = true;
6270 
6271   return new_init;
6272 }
6273 
6274 /* Verify array initializer.  Returns true if errors have been reported.  */
6275 
6276 bool
check_array_initializer(tree decl,tree type,tree init)6277 check_array_initializer (tree decl, tree type, tree init)
6278 {
6279   tree element_type = TREE_TYPE (type);
6280 
6281   /* The array type itself need not be complete, because the
6282      initializer may tell us how many elements are in the array.
6283      But, the elements of the array must be complete.  */
6284   if (!COMPLETE_TYPE_P (complete_type (element_type)))
6285     {
6286       if (decl)
6287 	error_at (DECL_SOURCE_LOCATION (decl),
6288 		  "elements of array %q#D have incomplete type", decl);
6289       else
6290 	error ("elements of array %q#T have incomplete type", type);
6291       return true;
6292     }
6293   /* A compound literal can't have variable size.  */
6294   if (init && !decl
6295       && ((COMPLETE_TYPE_P (type) && !TREE_CONSTANT (TYPE_SIZE (type)))
6296 	  || !TREE_CONSTANT (TYPE_SIZE (element_type))))
6297     {
6298       error ("variable-sized compound literal");
6299       return true;
6300     }
6301   return false;
6302 }
6303 
6304 /* Subroutine of check_initializer; args are passed down from that function.
6305    Set stmts_are_full_exprs_p to 1 across a call to build_aggr_init.  */
6306 
6307 static tree
build_aggr_init_full_exprs(tree decl,tree init,int flags)6308 build_aggr_init_full_exprs (tree decl, tree init, int flags)
6309 
6310 {
6311   gcc_assert (stmts_are_full_exprs_p ());
6312   return build_aggr_init (decl, init, flags, tf_warning_or_error);
6313 }
6314 
6315 /* Verify INIT (the initializer for DECL), and record the
6316    initialization in DECL_INITIAL, if appropriate.  CLEANUP is as for
6317    grok_reference_init.
6318 
6319    If the return value is non-NULL, it is an expression that must be
6320    evaluated dynamically to initialize DECL.  */
6321 
6322 static tree
check_initializer(tree decl,tree init,int flags,vec<tree,va_gc> ** cleanups)6323 check_initializer (tree decl, tree init, int flags, vec<tree, va_gc> **cleanups)
6324 {
6325   tree type = TREE_TYPE (decl);
6326   tree init_code = NULL;
6327   tree core_type;
6328 
6329   /* Things that are going to be initialized need to have complete
6330      type.  */
6331   TREE_TYPE (decl) = type = complete_type (TREE_TYPE (decl));
6332 
6333   if (DECL_HAS_VALUE_EXPR_P (decl))
6334     {
6335       /* A variable with DECL_HAS_VALUE_EXPR_P set is just a placeholder,
6336 	 it doesn't have storage to be initialized.  */
6337       gcc_assert (init == NULL_TREE);
6338       return NULL_TREE;
6339     }
6340 
6341   if (type == error_mark_node)
6342     /* We will have already complained.  */
6343     return NULL_TREE;
6344 
6345   if (TREE_CODE (type) == ARRAY_TYPE)
6346     {
6347       if (check_array_initializer (decl, type, init))
6348 	return NULL_TREE;
6349     }
6350   else if (!COMPLETE_TYPE_P (type))
6351     {
6352       error_at (DECL_SOURCE_LOCATION (decl),
6353 		"%q#D has incomplete type", decl);
6354       TREE_TYPE (decl) = error_mark_node;
6355       return NULL_TREE;
6356     }
6357   else
6358     /* There is no way to make a variable-sized class type in GNU C++.  */
6359     gcc_assert (TREE_CONSTANT (TYPE_SIZE (type)));
6360 
6361   if (init && BRACE_ENCLOSED_INITIALIZER_P (init))
6362     {
6363       int init_len = CONSTRUCTOR_NELTS (init);
6364       if (SCALAR_TYPE_P (type))
6365 	{
6366 	  if (init_len == 0)
6367 	    {
6368 	      maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6369 	      init = build_zero_init (type, NULL_TREE, false);
6370 	    }
6371 	  else if (init_len != 1 && TREE_CODE (type) != COMPLEX_TYPE)
6372 	    {
6373 	      error_at (EXPR_LOC_OR_LOC (init, DECL_SOURCE_LOCATION (decl)),
6374 			"scalar object %qD requires one element in "
6375 			"initializer", decl);
6376 	      TREE_TYPE (decl) = error_mark_node;
6377 	      return NULL_TREE;
6378 	    }
6379 	}
6380     }
6381 
6382   if (TREE_CODE (decl) == CONST_DECL)
6383     {
6384       gcc_assert (TREE_CODE (type) != REFERENCE_TYPE);
6385 
6386       DECL_INITIAL (decl) = init;
6387 
6388       gcc_assert (init != NULL_TREE);
6389       init = NULL_TREE;
6390     }
6391   else if (!init && DECL_REALLY_EXTERN (decl))
6392     ;
6393   else if (init || type_build_ctor_call (type)
6394 	   || TREE_CODE (type) == REFERENCE_TYPE)
6395     {
6396       if (TREE_CODE (type) == REFERENCE_TYPE)
6397 	{
6398 	  init = grok_reference_init (decl, type, init, flags);
6399 	  flags |= LOOKUP_ALREADY_DIGESTED;
6400 	}
6401       else if (!init)
6402 	check_for_uninitialized_const_var (decl, /*constexpr_context_p=*/false,
6403 					   tf_warning_or_error);
6404       /* Do not reshape constructors of vectors (they don't need to be
6405 	 reshaped.  */
6406       else if (BRACE_ENCLOSED_INITIALIZER_P (init))
6407 	{
6408 	  if (is_std_init_list (type))
6409 	    {
6410 	      init = perform_implicit_conversion (type, init,
6411 						  tf_warning_or_error);
6412 	      flags |= LOOKUP_ALREADY_DIGESTED;
6413 	    }
6414 	  else if (TYPE_NON_AGGREGATE_CLASS (type))
6415 	    {
6416 	      /* Don't reshape if the class has constructors.  */
6417 	      if (cxx_dialect == cxx98)
6418 		error_at (EXPR_LOC_OR_LOC (init, DECL_SOURCE_LOCATION (decl)),
6419 			  "in C++98 %qD must be initialized by "
6420 			  "constructor, not by %<{...}%>",
6421 			  decl);
6422 	    }
6423 	  else if (VECTOR_TYPE_P (type) && TYPE_VECTOR_OPAQUE (type))
6424 	    {
6425 	      error ("opaque vector types cannot be initialized");
6426 	      init = error_mark_node;
6427 	    }
6428 	  else
6429 	    {
6430 	      init = reshape_init (type, init, tf_warning_or_error);
6431 	      flags |= LOOKUP_NO_NARROWING;
6432 	    }
6433 	}
6434       else if (TREE_CODE (init) == TREE_LIST
6435 	       && TREE_TYPE (init) != unknown_type_node
6436 	       && !MAYBE_CLASS_TYPE_P (type))
6437 	{
6438 	  gcc_assert (TREE_CODE (decl) != RESULT_DECL);
6439 
6440 	  /* We get here with code like `int a (2);' */
6441 	  init = build_x_compound_expr_from_list (init, ELK_INIT,
6442 						  tf_warning_or_error);
6443 	}
6444 
6445       /* If DECL has an array type without a specific bound, deduce the
6446 	 array size from the initializer.  */
6447       maybe_deduce_size_from_array_init (decl, init);
6448       type = TREE_TYPE (decl);
6449       if (type == error_mark_node)
6450 	return NULL_TREE;
6451 
6452       if (((type_build_ctor_call (type) || CLASS_TYPE_P (type))
6453 	   && !(flags & LOOKUP_ALREADY_DIGESTED)
6454 	   && !(init && BRACE_ENCLOSED_INITIALIZER_P (init)
6455 		&& CP_AGGREGATE_TYPE_P (type)
6456 		&& (CLASS_TYPE_P (type)
6457 		    || !TYPE_NEEDS_CONSTRUCTING (type)
6458 		    || type_has_extended_temps (type))))
6459 	  || (DECL_DECOMPOSITION_P (decl) && TREE_CODE (type) == ARRAY_TYPE))
6460 	{
6461 	  init_code = build_aggr_init_full_exprs (decl, init, flags);
6462 
6463 	  /* A constructor call is a non-trivial initializer even if
6464 	     it isn't explicitly written.  */
6465 	  if (TREE_SIDE_EFFECTS (init_code))
6466 	    DECL_NONTRIVIALLY_INITIALIZED_P (decl) = true;
6467 
6468 	  /* If this is a constexpr initializer, expand_default_init will
6469 	     have returned an INIT_EXPR rather than a CALL_EXPR.  In that
6470 	     case, pull the initializer back out and pass it down into
6471 	     store_init_value.  */
6472 	  while (TREE_CODE (init_code) == EXPR_STMT
6473 		 || TREE_CODE (init_code) == CONVERT_EXPR)
6474 	    init_code = TREE_OPERAND (init_code, 0);
6475 	  if (TREE_CODE (init_code) == INIT_EXPR)
6476 	    {
6477 	      init = TREE_OPERAND (init_code, 1);
6478 	      init_code = NULL_TREE;
6479 	      /* Don't call digest_init; it's unnecessary and will complain
6480 		 about aggregate initialization of non-aggregate classes.  */
6481 	      flags |= LOOKUP_ALREADY_DIGESTED;
6482 	    }
6483 	  else if (DECL_DECLARED_CONSTEXPR_P (decl))
6484 	    {
6485 	      /* Declared constexpr, but no suitable initializer; massage
6486 		 init appropriately so we can pass it into store_init_value
6487 		 for the error.  */
6488 	      if (CLASS_TYPE_P (type)
6489 		  && (!init || TREE_CODE (init) == TREE_LIST))
6490 		{
6491 		  init = build_functional_cast (type, init, tf_none);
6492 		  if (TREE_CODE (init) == TARGET_EXPR)
6493 		    TARGET_EXPR_DIRECT_INIT_P (init) = true;
6494 		}
6495 	      init_code = NULL_TREE;
6496 	    }
6497 	  else
6498 	    init = NULL_TREE;
6499 	}
6500 
6501       if (init && TREE_CODE (init) != TREE_VEC)
6502 	{
6503 	  /* In aggregate initialization of a variable, each element
6504 	     initialization is a full-expression because there is no
6505 	     enclosing expression.  */
6506 	  gcc_assert (stmts_are_full_exprs_p ());
6507 
6508 	  init_code = store_init_value (decl, init, cleanups, flags);
6509 
6510 	  if (pedantic && TREE_CODE (type) == ARRAY_TYPE
6511 	      && DECL_INITIAL (decl)
6512 	      && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
6513 	      && PAREN_STRING_LITERAL_P (DECL_INITIAL (decl)))
6514 	    warning_at (EXPR_LOC_OR_LOC (DECL_INITIAL (decl),
6515 					 DECL_SOURCE_LOCATION (decl)),
6516 			0, "array %qD initialized by parenthesized "
6517 			"string literal %qE",
6518 			decl, DECL_INITIAL (decl));
6519 	  init = NULL;
6520 	}
6521     }
6522   else
6523     {
6524       if (CLASS_TYPE_P (core_type = strip_array_types (type))
6525 	  && (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type)
6526 	      || CLASSTYPE_REF_FIELDS_NEED_INIT (core_type)))
6527 	diagnose_uninitialized_cst_or_ref_member (core_type, /*using_new=*/false,
6528 						  /*complain=*/true);
6529 
6530       check_for_uninitialized_const_var (decl, /*constexpr_context_p=*/false,
6531 					 tf_warning_or_error);
6532     }
6533 
6534   if (init && init != error_mark_node)
6535     init_code = build2 (INIT_EXPR, type, decl, init);
6536 
6537   if (init_code)
6538     {
6539       /* We might have set these in cp_finish_decl.  */
6540       DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = false;
6541       TREE_CONSTANT (decl) = false;
6542     }
6543 
6544   if (init_code
6545       && (DECL_IN_AGGR_P (decl)
6546 	  && DECL_INITIALIZED_IN_CLASS_P (decl)
6547 	  && !DECL_VAR_DECLARED_INLINE_P (decl)))
6548     {
6549       static int explained = 0;
6550 
6551       if (cxx_dialect < cxx11)
6552 	error ("initializer invalid for static member with constructor");
6553       else if (cxx_dialect < cxx17)
6554 	error ("non-constant in-class initialization invalid for static "
6555 	       "member %qD", decl);
6556       else
6557 	error ("non-constant in-class initialization invalid for non-inline "
6558 	       "static member %qD", decl);
6559       if (!explained)
6560 	{
6561 	  inform (input_location,
6562 		  "(an out of class initialization is required)");
6563 	  explained = 1;
6564 	}
6565       return NULL_TREE;
6566     }
6567 
6568   return init_code;
6569 }
6570 
6571 /* If DECL is not a local variable, give it RTL.  */
6572 
6573 static void
make_rtl_for_nonlocal_decl(tree decl,tree init,const char * asmspec)6574 make_rtl_for_nonlocal_decl (tree decl, tree init, const char* asmspec)
6575 {
6576   int toplev = toplevel_bindings_p ();
6577   int defer_p;
6578 
6579   /* Set the DECL_ASSEMBLER_NAME for the object.  */
6580   if (asmspec)
6581     {
6582       /* The `register' keyword, when used together with an
6583 	 asm-specification, indicates that the variable should be
6584 	 placed in a particular register.  */
6585       if (VAR_P (decl) && DECL_REGISTER (decl))
6586 	{
6587 	  set_user_assembler_name (decl, asmspec);
6588 	  DECL_HARD_REGISTER (decl) = 1;
6589 	}
6590       else
6591 	{
6592 	  if (TREE_CODE (decl) == FUNCTION_DECL
6593 	      && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
6594 	    set_builtin_user_assembler_name (decl, asmspec);
6595 	  set_user_assembler_name (decl, asmspec);
6596 	}
6597     }
6598 
6599   /* Handle non-variables up front.  */
6600   if (!VAR_P (decl))
6601     {
6602       rest_of_decl_compilation (decl, toplev, at_eof);
6603       return;
6604     }
6605 
6606   /* If we see a class member here, it should be a static data
6607      member.  */
6608   if (DECL_LANG_SPECIFIC (decl) && DECL_IN_AGGR_P (decl))
6609     {
6610       gcc_assert (TREE_STATIC (decl));
6611       /* An in-class declaration of a static data member should be
6612 	 external; it is only a declaration, and not a definition.  */
6613       if (init == NULL_TREE)
6614 	gcc_assert (DECL_EXTERNAL (decl)
6615 		    || !TREE_PUBLIC (decl)
6616 		    || DECL_INLINE_VAR_P (decl));
6617     }
6618 
6619   /* We don't create any RTL for local variables.  */
6620   if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
6621     return;
6622 
6623   /* We defer emission of local statics until the corresponding
6624      DECL_EXPR is expanded.  But with constexpr its function might never
6625      be expanded, so go ahead and tell cgraph about the variable now.  */
6626   defer_p = ((DECL_FUNCTION_SCOPE_P (decl)
6627 	      && !var_in_maybe_constexpr_fn (decl))
6628 	     || DECL_VIRTUAL_P (decl));
6629 
6630   /* Defer template instantiations.  */
6631   if (DECL_LANG_SPECIFIC (decl)
6632       && DECL_IMPLICIT_INSTANTIATION (decl))
6633     defer_p = 1;
6634 
6635   /* If we're not deferring, go ahead and assemble the variable.  */
6636   if (!defer_p)
6637     rest_of_decl_compilation (decl, toplev, at_eof);
6638 }
6639 
6640 /* walk_tree helper for wrap_temporary_cleanups, below.  */
6641 
6642 static tree
wrap_cleanups_r(tree * stmt_p,int * walk_subtrees,void * data)6643 wrap_cleanups_r (tree *stmt_p, int *walk_subtrees, void *data)
6644 {
6645   /* Stop at types or full-expression boundaries.  */
6646   if (TYPE_P (*stmt_p)
6647       || TREE_CODE (*stmt_p) == CLEANUP_POINT_EXPR)
6648     {
6649       *walk_subtrees = 0;
6650       return NULL_TREE;
6651     }
6652 
6653   if (TREE_CODE (*stmt_p) == TARGET_EXPR)
6654     {
6655       tree guard = (tree)data;
6656       tree tcleanup = TARGET_EXPR_CLEANUP (*stmt_p);
6657 
6658       tcleanup = build2 (TRY_CATCH_EXPR, void_type_node, tcleanup, guard);
6659       /* Tell honor_protect_cleanup_actions to handle this as a separate
6660 	 cleanup.  */
6661       TRY_CATCH_IS_CLEANUP (tcleanup) = 1;
6662 
6663       TARGET_EXPR_CLEANUP (*stmt_p) = tcleanup;
6664     }
6665 
6666   return NULL_TREE;
6667 }
6668 
6669 /* We're initializing a local variable which has a cleanup GUARD.  If there
6670    are any temporaries used in the initializer INIT of this variable, we
6671    need to wrap their cleanups with TRY_CATCH_EXPR (, GUARD) so that the
6672    variable will be cleaned up properly if one of them throws.
6673 
6674    Unfortunately, there's no way to express this properly in terms of
6675    nesting, as the regions for the temporaries overlap the region for the
6676    variable itself; if there are two temporaries, the variable needs to be
6677    the first thing destroyed if either of them throws.  However, we only
6678    want to run the variable's cleanup if it actually got constructed.  So
6679    we need to guard the temporary cleanups with the variable's cleanup if
6680    they are run on the normal path, but not if they are run on the
6681    exceptional path.  We implement this by telling
6682    honor_protect_cleanup_actions to strip the variable cleanup from the
6683    exceptional path.  */
6684 
6685 static void
wrap_temporary_cleanups(tree init,tree guard)6686 wrap_temporary_cleanups (tree init, tree guard)
6687 {
6688   cp_walk_tree_without_duplicates (&init, wrap_cleanups_r, (void *)guard);
6689 }
6690 
6691 /* Generate code to initialize DECL (a local variable).  */
6692 
6693 static void
initialize_local_var(tree decl,tree init)6694 initialize_local_var (tree decl, tree init)
6695 {
6696   tree type = TREE_TYPE (decl);
6697   tree cleanup;
6698   int already_used;
6699 
6700   gcc_assert (VAR_P (decl)
6701 	      || TREE_CODE (decl) == RESULT_DECL);
6702   gcc_assert (!TREE_STATIC (decl));
6703 
6704   if (DECL_SIZE (decl) == NULL_TREE)
6705     {
6706       /* If we used it already as memory, it must stay in memory.  */
6707       DECL_INITIAL (decl) = NULL_TREE;
6708       TREE_ADDRESSABLE (decl) = TREE_USED (decl);
6709       return;
6710     }
6711 
6712   if (type == error_mark_node)
6713     return;
6714 
6715   /* Compute and store the initial value.  */
6716   already_used = TREE_USED (decl) || TREE_USED (type);
6717   if (TREE_USED (type))
6718     DECL_READ_P (decl) = 1;
6719 
6720   /* Generate a cleanup, if necessary.  */
6721   cleanup = cxx_maybe_build_cleanup (decl, tf_warning_or_error);
6722 
6723   /* Perform the initialization.  */
6724   if (init)
6725     {
6726       tree rinit = (TREE_CODE (init) == INIT_EXPR
6727 		    ? TREE_OPERAND (init, 1) : NULL_TREE);
6728       if (rinit && !TREE_SIDE_EFFECTS (rinit))
6729 	{
6730 	  /* Stick simple initializers in DECL_INITIAL so that
6731 	     -Wno-init-self works (c++/34772).  */
6732 	  gcc_assert (TREE_OPERAND (init, 0) == decl);
6733 	  DECL_INITIAL (decl) = rinit;
6734 
6735 	  if (warn_init_self && TREE_CODE (type) == REFERENCE_TYPE)
6736 	    {
6737 	      STRIP_NOPS (rinit);
6738 	      if (rinit == decl)
6739 		warning_at (DECL_SOURCE_LOCATION (decl),
6740 			    OPT_Winit_self,
6741 			    "reference %qD is initialized with itself", decl);
6742 	    }
6743 	}
6744       else
6745 	{
6746 	  int saved_stmts_are_full_exprs_p;
6747 
6748 	  /* If we're only initializing a single object, guard the
6749 	     destructors of any temporaries used in its initializer with
6750 	     its destructor.  This isn't right for arrays because each
6751 	     element initialization is a full-expression.  */
6752 	  if (cleanup && TREE_CODE (type) != ARRAY_TYPE)
6753 	    wrap_temporary_cleanups (init, cleanup);
6754 
6755 	  gcc_assert (building_stmt_list_p ());
6756 	  saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
6757 	  current_stmt_tree ()->stmts_are_full_exprs_p = 1;
6758 	  finish_expr_stmt (init);
6759 	  current_stmt_tree ()->stmts_are_full_exprs_p =
6760 	    saved_stmts_are_full_exprs_p;
6761 	}
6762     }
6763 
6764   /* Set this to 0 so we can tell whether an aggregate which was
6765      initialized was ever used.  Don't do this if it has a
6766      destructor, so we don't complain about the 'resource
6767      allocation is initialization' idiom.  Now set
6768      attribute((unused)) on types so decls of that type will be
6769      marked used. (see TREE_USED, above.)  */
6770   if (TYPE_NEEDS_CONSTRUCTING (type)
6771       && ! already_used
6772       && TYPE_HAS_TRIVIAL_DESTRUCTOR (type)
6773       && DECL_NAME (decl))
6774     TREE_USED (decl) = 0;
6775   else if (already_used)
6776     TREE_USED (decl) = 1;
6777 
6778   if (cleanup)
6779     finish_decl_cleanup (decl, cleanup);
6780 }
6781 
6782 /* DECL is a VAR_DECL for a compiler-generated variable with static
6783    storage duration (like a virtual table) whose initializer is a
6784    compile-time constant.  Initialize the variable and provide it to the
6785    back end.  */
6786 
6787 void
initialize_artificial_var(tree decl,vec<constructor_elt,va_gc> * v)6788 initialize_artificial_var (tree decl, vec<constructor_elt, va_gc> *v)
6789 {
6790   tree init;
6791   gcc_assert (DECL_ARTIFICIAL (decl));
6792   init = build_constructor (TREE_TYPE (decl), v);
6793   gcc_assert (TREE_CODE (init) == CONSTRUCTOR);
6794   DECL_INITIAL (decl) = init;
6795   DECL_INITIALIZED_P (decl) = 1;
6796   determine_visibility (decl);
6797   layout_var_decl (decl);
6798   maybe_commonize_var (decl);
6799   make_rtl_for_nonlocal_decl (decl, init, /*asmspec=*/NULL);
6800 }
6801 
6802 /* INIT is the initializer for a variable, as represented by the
6803    parser.  Returns true iff INIT is value-dependent.  */
6804 
6805 static bool
value_dependent_init_p(tree init)6806 value_dependent_init_p (tree init)
6807 {
6808   if (TREE_CODE (init) == TREE_LIST)
6809     /* A parenthesized initializer, e.g.: int i (3, 2); ? */
6810     return any_value_dependent_elements_p (init);
6811   else if (TREE_CODE (init) == CONSTRUCTOR)
6812   /* A brace-enclosed initializer, e.g.: int i = { 3 }; ? */
6813     {
6814       if (dependent_type_p (TREE_TYPE (init)))
6815 	return true;
6816 
6817       vec<constructor_elt, va_gc> *elts;
6818       size_t nelts;
6819       size_t i;
6820 
6821       elts = CONSTRUCTOR_ELTS (init);
6822       nelts = vec_safe_length (elts);
6823       for (i = 0; i < nelts; ++i)
6824 	if (value_dependent_init_p ((*elts)[i].value))
6825 	  return true;
6826     }
6827   else
6828     /* It must be a simple expression, e.g., int i = 3;  */
6829     return value_dependent_expression_p (init);
6830 
6831   return false;
6832 }
6833 
6834 // Returns true if a DECL is VAR_DECL with the concept specifier.
6835 static inline bool
is_concept_var(tree decl)6836 is_concept_var (tree decl)
6837 {
6838   return (VAR_P (decl)
6839 	  // Not all variables have DECL_LANG_SPECIFIC.
6840           && DECL_LANG_SPECIFIC (decl)
6841           && DECL_DECLARED_CONCEPT_P (decl));
6842 }
6843 
6844 /* A helper function to be called via walk_tree.  If any label exists
6845    under *TP, it is (going to be) forced.  Set has_forced_label_in_static.  */
6846 
6847 static tree
notice_forced_label_r(tree * tp,int * walk_subtrees,void *)6848 notice_forced_label_r (tree *tp, int *walk_subtrees, void *)
6849 {
6850   if (TYPE_P (*tp))
6851     *walk_subtrees = 0;
6852   if (TREE_CODE (*tp) == LABEL_DECL)
6853     cfun->has_forced_label_in_static = 1;
6854   return NULL_TREE;
6855 }
6856 
6857 /* Finish processing of a declaration;
6858    install its line number and initial value.
6859    If the length of an array type is not known before,
6860    it must be determined now, from the initial value, or it is an error.
6861 
6862    INIT is the initializer (if any) for DECL.  If INIT_CONST_EXPR_P is
6863    true, then INIT is an integral constant expression.
6864 
6865    FLAGS is LOOKUP_ONLYCONVERTING if the = init syntax was used, else 0
6866    if the (init) syntax was used.  */
6867 
6868 void
cp_finish_decl(tree decl,tree init,bool init_const_expr_p,tree asmspec_tree,int flags)6869 cp_finish_decl (tree decl, tree init, bool init_const_expr_p,
6870 		tree asmspec_tree, int flags)
6871 {
6872   tree type;
6873   vec<tree, va_gc> *cleanups = NULL;
6874   const char *asmspec = NULL;
6875   int was_readonly = 0;
6876   bool var_definition_p = false;
6877   tree auto_node;
6878 
6879   if (decl == error_mark_node)
6880     return;
6881   else if (! decl)
6882     {
6883       if (init)
6884 	error ("assignment (not initialization) in declaration");
6885       return;
6886     }
6887 
6888   gcc_assert (TREE_CODE (decl) != RESULT_DECL);
6889   /* Parameters are handled by store_parm_decls, not cp_finish_decl.  */
6890   gcc_assert (TREE_CODE (decl) != PARM_DECL);
6891 
6892   type = TREE_TYPE (decl);
6893   if (type == error_mark_node)
6894     return;
6895 
6896   /* Warn about register storage specifiers except when in GNU global
6897      or local register variable extension.  */
6898   if (VAR_P (decl) && DECL_REGISTER (decl) && asmspec_tree == NULL_TREE)
6899     {
6900       if (cxx_dialect >= cxx17)
6901 	pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
6902 		 "ISO C++17 does not allow %<register%> storage "
6903 		 "class specifier");
6904       else
6905 	warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
6906 		    "%<register%> storage class specifier used");
6907     }
6908 
6909   /* If a name was specified, get the string.  */
6910   if (at_namespace_scope_p ())
6911     asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
6912   if (asmspec_tree && asmspec_tree != error_mark_node)
6913     asmspec = TREE_STRING_POINTER (asmspec_tree);
6914 
6915   if (current_class_type
6916       && CP_DECL_CONTEXT (decl) == current_class_type
6917       && TYPE_BEING_DEFINED (current_class_type)
6918       && !CLASSTYPE_TEMPLATE_INSTANTIATION (current_class_type)
6919       && (DECL_INITIAL (decl) || init))
6920     DECL_INITIALIZED_IN_CLASS_P (decl) = 1;
6921 
6922   if (TREE_CODE (decl) != FUNCTION_DECL
6923       && (auto_node = type_uses_auto (type)))
6924     {
6925       tree d_init;
6926       if (init == NULL_TREE)
6927 	{
6928 	  if (DECL_LANG_SPECIFIC (decl)
6929 	      && DECL_TEMPLATE_INSTANTIATION (decl)
6930 	      && !DECL_TEMPLATE_INSTANTIATED (decl))
6931 	    {
6932 	      /* init is null because we're deferring instantiating the
6933 		 initializer until we need it.  Well, we need it now.  */
6934 	      instantiate_decl (decl, /*defer_ok*/true, /*expl*/false);
6935 	      return;
6936 	    }
6937 
6938 	  gcc_assert (CLASS_PLACEHOLDER_TEMPLATE (auto_node));
6939 	}
6940       d_init = init;
6941       if (d_init)
6942 	{
6943 	  if (TREE_CODE (d_init) == TREE_LIST
6944 	      && !CLASS_PLACEHOLDER_TEMPLATE (auto_node))
6945 	    d_init = build_x_compound_expr_from_list (d_init, ELK_INIT,
6946 						      tf_warning_or_error);
6947 	  d_init = resolve_nondeduced_context (d_init, tf_warning_or_error);
6948 	}
6949       enum auto_deduction_context adc = adc_variable_type;
6950       if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
6951 	adc = adc_decomp_type;
6952       type = TREE_TYPE (decl) = do_auto_deduction (type, d_init, auto_node,
6953 						   tf_warning_or_error, adc,
6954 						   NULL_TREE, flags);
6955       if (type == error_mark_node)
6956 	return;
6957       if (TREE_CODE (type) == FUNCTION_TYPE)
6958 	{
6959 	  error ("initializer for %<decltype(auto) %D%> has function type "
6960 		 "(did you forget the %<()%> ?)", decl);
6961 	  TREE_TYPE (decl) = error_mark_node;
6962 	  return;
6963 	}
6964       cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
6965     }
6966 
6967   if (ensure_literal_type_for_constexpr_object (decl) == error_mark_node)
6968     {
6969       DECL_DECLARED_CONSTEXPR_P (decl) = 0;
6970       if (VAR_P (decl) && DECL_CLASS_SCOPE_P (decl))
6971 	{
6972 	  init = NULL_TREE;
6973 	  DECL_EXTERNAL (decl) = 1;
6974 	}
6975     }
6976 
6977   if (VAR_P (decl)
6978       && DECL_CLASS_SCOPE_P (decl)
6979       && DECL_INITIALIZED_IN_CLASS_P (decl))
6980     check_static_variable_definition (decl, type);
6981 
6982   if (init && TREE_CODE (decl) == FUNCTION_DECL)
6983     {
6984       tree clone;
6985       if (init == ridpointers[(int)RID_DELETE])
6986 	{
6987 	  /* FIXME check this is 1st decl.  */
6988 	  DECL_DELETED_FN (decl) = 1;
6989 	  DECL_DECLARED_INLINE_P (decl) = 1;
6990 	  DECL_INITIAL (decl) = error_mark_node;
6991 	  FOR_EACH_CLONE (clone, decl)
6992 	    {
6993 	      DECL_DELETED_FN (clone) = 1;
6994 	      DECL_DECLARED_INLINE_P (clone) = 1;
6995 	      DECL_INITIAL (clone) = error_mark_node;
6996 	    }
6997 	  init = NULL_TREE;
6998 	}
6999       else if (init == ridpointers[(int)RID_DEFAULT])
7000 	{
7001 	  if (defaultable_fn_check (decl))
7002 	    DECL_DEFAULTED_FN (decl) = 1;
7003 	  else
7004 	    DECL_INITIAL (decl) = NULL_TREE;
7005 	}
7006     }
7007 
7008   if (init && VAR_P (decl))
7009     {
7010       DECL_NONTRIVIALLY_INITIALIZED_P (decl) = 1;
7011       /* If DECL is a reference, then we want to know whether init is a
7012 	 reference constant; init_const_expr_p as passed tells us whether
7013 	 it's an rvalue constant.  */
7014       if (TREE_CODE (type) == REFERENCE_TYPE)
7015 	init_const_expr_p = potential_constant_expression (init);
7016       if (init_const_expr_p)
7017 	{
7018 	  /* Set these flags now for templates.  We'll update the flags in
7019 	     store_init_value for instantiations.  */
7020 	  DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
7021 	  if (decl_maybe_constant_var_p (decl)
7022 	      /* FIXME setting TREE_CONSTANT on refs breaks the back end.  */
7023 	      && TREE_CODE (type) != REFERENCE_TYPE)
7024 	    TREE_CONSTANT (decl) = 1;
7025 	}
7026     }
7027 
7028   if (processing_template_decl)
7029     {
7030       bool type_dependent_p;
7031 
7032       /* Add this declaration to the statement-tree.  */
7033       if (at_function_scope_p ())
7034 	add_decl_expr (decl);
7035 
7036       type_dependent_p = dependent_type_p (type);
7037 
7038       if (check_for_bare_parameter_packs (init))
7039 	{
7040 	  init = NULL_TREE;
7041 	  DECL_INITIAL (decl) = NULL_TREE;
7042 	}
7043 
7044       /* Generally, initializers in templates are expanded when the
7045 	 template is instantiated.  But, if DECL is a variable constant
7046 	 then it can be used in future constant expressions, so its value
7047 	 must be available. */
7048 
7049       bool dep_init = false;
7050 
7051       if (!VAR_P (decl) || type_dependent_p)
7052 	/* We can't do anything if the decl has dependent type.  */;
7053       else if (!init && is_concept_var (decl))
7054 	{
7055 	  error ("variable concept has no initializer");
7056 	  init = boolean_true_node;
7057 	}
7058       else if (init
7059 	       && init_const_expr_p
7060 	       && TREE_CODE (type) != REFERENCE_TYPE
7061 	       && decl_maybe_constant_var_p (decl)
7062 	       && !(dep_init = value_dependent_init_p (init)))
7063 	{
7064 	  /* This variable seems to be a non-dependent constant, so process
7065 	     its initializer.  If check_initializer returns non-null the
7066 	     initialization wasn't constant after all.  */
7067 	  tree init_code;
7068 	  cleanups = make_tree_vector ();
7069 	  init_code = check_initializer (decl, init, flags, &cleanups);
7070 	  if (init_code == NULL_TREE)
7071 	    init = NULL_TREE;
7072 	  release_tree_vector (cleanups);
7073 	}
7074       else if (!DECL_PRETTY_FUNCTION_P (decl))
7075 	{
7076 	  /* Deduce array size even if the initializer is dependent.  */
7077 	  maybe_deduce_size_from_array_init (decl, init);
7078 	  /* And complain about multiple initializers.  */
7079 	  if (init && TREE_CODE (init) == TREE_LIST && TREE_CHAIN (init)
7080 	      && !MAYBE_CLASS_TYPE_P (type))
7081 	    init = build_x_compound_expr_from_list (init, ELK_INIT,
7082 						    tf_warning_or_error);
7083 	}
7084 
7085       if (init)
7086 	{
7087 	  if (TREE_CODE (init) == TREE_LIST)
7088 	    lookup_list_keep (init, true);
7089 	  DECL_INITIAL (decl) = init;
7090 	}
7091       if (dep_init)
7092 	{
7093 	  retrofit_lang_decl (decl);
7094 	  SET_DECL_DEPENDENT_INIT_P (decl, true);
7095 	}
7096       return;
7097     }
7098 
7099   /* Just store non-static data member initializers for later.  */
7100   if (init && TREE_CODE (decl) == FIELD_DECL)
7101     DECL_INITIAL (decl) = init;
7102 
7103   /* Take care of TYPE_DECLs up front.  */
7104   if (TREE_CODE (decl) == TYPE_DECL)
7105     {
7106       if (type != error_mark_node
7107 	  && MAYBE_CLASS_TYPE_P (type) && DECL_NAME (decl))
7108 	{
7109 	  if (TREE_TYPE (DECL_NAME (decl)) && TREE_TYPE (decl) != type)
7110 	    warning (0, "shadowing previous type declaration of %q#D", decl);
7111 	  set_identifier_type_value (DECL_NAME (decl), decl);
7112 	}
7113 
7114       /* If we have installed this as the canonical typedef for this
7115 	 type, and that type has not been defined yet, delay emitting
7116 	 the debug information for it, as we will emit it later.  */
7117       if (TYPE_MAIN_DECL (TREE_TYPE (decl)) == decl
7118 	  && !COMPLETE_TYPE_P (TREE_TYPE (decl)))
7119 	TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
7120 
7121       rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl),
7122 				at_eof);
7123       return;
7124     }
7125 
7126   /* A reference will be modified here, as it is initialized.  */
7127   if (! DECL_EXTERNAL (decl)
7128       && TREE_READONLY (decl)
7129       && TREE_CODE (type) == REFERENCE_TYPE)
7130     {
7131       was_readonly = 1;
7132       TREE_READONLY (decl) = 0;
7133     }
7134 
7135   if (VAR_P (decl))
7136     {
7137       /* If this is a local variable that will need a mangled name,
7138 	 register it now.  We must do this before processing the
7139 	 initializer for the variable, since the initialization might
7140 	 require a guard variable, and since the mangled name of the
7141 	 guard variable will depend on the mangled name of this
7142 	 variable.  */
7143       if (DECL_FUNCTION_SCOPE_P (decl)
7144 	  && TREE_STATIC (decl)
7145 	  && !DECL_ARTIFICIAL (decl))
7146 	{
7147 	  push_local_name (decl);
7148 	  /* Normally has_forced_label_in_static is set during GIMPLE
7149 	     lowering, but [cd]tors are never actually compiled directly.
7150 	     We need to set this early so we can deal with the label
7151 	     address extension.  */
7152 	  if ((DECL_CONSTRUCTOR_P (current_function_decl)
7153 	       || DECL_DESTRUCTOR_P (current_function_decl))
7154 	      && init)
7155 	    {
7156 	      walk_tree (&init, notice_forced_label_r, NULL, NULL);
7157 	      add_local_decl (cfun, decl);
7158 	    }
7159 	  /* And make sure it's in the symbol table for
7160 	     c_parse_final_cleanups to find.  */
7161 	  varpool_node::get_create (decl);
7162 	}
7163 
7164       /* Convert the initializer to the type of DECL, if we have not
7165 	 already initialized DECL.  */
7166       if (!DECL_INITIALIZED_P (decl)
7167 	  /* If !DECL_EXTERNAL then DECL is being defined.  In the
7168 	     case of a static data member initialized inside the
7169 	     class-specifier, there can be an initializer even if DECL
7170 	     is *not* defined.  */
7171 	  && (!DECL_EXTERNAL (decl) || init))
7172 	{
7173 	  cleanups = make_tree_vector ();
7174 	  init = check_initializer (decl, init, flags, &cleanups);
7175 
7176 	  /* Handle:
7177 
7178 	     [dcl.init]
7179 
7180 	     The memory occupied by any object of static storage
7181 	     duration is zero-initialized at program startup before
7182 	     any other initialization takes place.
7183 
7184 	     We cannot create an appropriate initializer until after
7185 	     the type of DECL is finalized.  If DECL_INITIAL is set,
7186 	     then the DECL is statically initialized, and any
7187 	     necessary zero-initialization has already been performed.  */
7188 	  if (TREE_STATIC (decl) && !DECL_INITIAL (decl))
7189 	    DECL_INITIAL (decl) = build_zero_init (TREE_TYPE (decl),
7190 						   /*nelts=*/NULL_TREE,
7191 						   /*static_storage_p=*/true);
7192 	  /* Remember that the initialization for this variable has
7193 	     taken place.  */
7194 	  DECL_INITIALIZED_P (decl) = 1;
7195 	  /* This declaration is the definition of this variable,
7196 	     unless we are initializing a static data member within
7197 	     the class specifier.  */
7198 	  if (!DECL_EXTERNAL (decl))
7199 	    var_definition_p = true;
7200 	}
7201       /* If the variable has an array type, lay out the type, even if
7202 	 there is no initializer.  It is valid to index through the
7203 	 array, and we must get TYPE_ALIGN set correctly on the array
7204 	 type.  */
7205       else if (TREE_CODE (type) == ARRAY_TYPE)
7206 	layout_type (type);
7207 
7208       if (TREE_STATIC (decl)
7209 	  && !at_function_scope_p ()
7210 	  && current_function_decl == NULL)
7211 	/* So decl is a global variable or a static member of a
7212 	   non local class. Record the types it uses
7213 	   so that we can decide later to emit debug info for them.  */
7214 	record_types_used_by_current_var_decl (decl);
7215     }
7216 
7217   /* Add this declaration to the statement-tree.  This needs to happen
7218      after the call to check_initializer so that the DECL_EXPR for a
7219      reference temp is added before the DECL_EXPR for the reference itself.  */
7220   if (DECL_FUNCTION_SCOPE_P (decl))
7221     {
7222       /* If we're building a variable sized type, and we might be
7223 	 reachable other than via the top of the current binding
7224 	 level, then create a new BIND_EXPR so that we deallocate
7225 	 the object at the right time.  */
7226       if (VAR_P (decl)
7227 	  && DECL_SIZE (decl)
7228 	  && !TREE_CONSTANT (DECL_SIZE (decl))
7229 	  && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
7230 	{
7231 	  tree bind;
7232 	  bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
7233 	  TREE_SIDE_EFFECTS (bind) = 1;
7234 	  add_stmt (bind);
7235 	  BIND_EXPR_BODY (bind) = push_stmt_list ();
7236 	}
7237       add_decl_expr (decl);
7238     }
7239 
7240   /* Let the middle end know about variables and functions -- but not
7241      static data members in uninstantiated class templates.  */
7242   if (VAR_OR_FUNCTION_DECL_P (decl))
7243     {
7244       if (VAR_P (decl))
7245 	{
7246 	  layout_var_decl (decl);
7247 	  maybe_commonize_var (decl);
7248 	}
7249 
7250       /* This needs to happen after the linkage is set. */
7251       determine_visibility (decl);
7252 
7253       if (var_definition_p && TREE_STATIC (decl))
7254 	{
7255 	  /* If a TREE_READONLY variable needs initialization
7256 	     at runtime, it is no longer readonly and we need to
7257 	     avoid MEM_READONLY_P being set on RTL created for it.  */
7258 	  if (init)
7259 	    {
7260 	      if (TREE_READONLY (decl))
7261 		TREE_READONLY (decl) = 0;
7262 	      was_readonly = 0;
7263 	    }
7264 	  else if (was_readonly)
7265 	    TREE_READONLY (decl) = 1;
7266 
7267 	  /* Likewise if it needs destruction.  */
7268 	  if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
7269 	    TREE_READONLY (decl) = 0;
7270 	}
7271 
7272       make_rtl_for_nonlocal_decl (decl, init, asmspec);
7273 
7274       /* Check for abstractness of the type. Notice that there is no
7275 	 need to strip array types here since the check for those types
7276 	 is already done within create_array_type_for_decl.  */
7277       abstract_virtuals_error (decl, type);
7278 
7279       if (TREE_TYPE (decl) == error_mark_node)
7280 	/* No initialization required.  */
7281 	;
7282       else if (TREE_CODE (decl) == FUNCTION_DECL)
7283 	{
7284 	  if (init)
7285 	    {
7286 	      if (init == ridpointers[(int)RID_DEFAULT])
7287 		{
7288 		  /* An out-of-class default definition is defined at
7289 		     the point where it is explicitly defaulted.  */
7290 		  if (DECL_DELETED_FN (decl))
7291 		    maybe_explain_implicit_delete (decl);
7292 		  else if (DECL_INITIAL (decl) == error_mark_node)
7293 		    synthesize_method (decl);
7294 		}
7295 	      else
7296 		error ("function %q#D is initialized like a variable", decl);
7297 	    }
7298 	  /* else no initialization required.  */
7299 	}
7300       else if (DECL_EXTERNAL (decl)
7301 	       && ! (DECL_LANG_SPECIFIC (decl)
7302 		     && DECL_NOT_REALLY_EXTERN (decl)))
7303 	{
7304 	  /* check_initializer will have done any constant initialization.  */
7305 	}
7306       /* A variable definition.  */
7307       else if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
7308 	/* Initialize the local variable.  */
7309 	initialize_local_var (decl, init);
7310 
7311       /* If a variable is defined, and then a subsequent
7312 	 definition with external linkage is encountered, we will
7313 	 get here twice for the same variable.  We want to avoid
7314 	 calling expand_static_init more than once.  For variables
7315 	 that are not static data members, we can call
7316 	 expand_static_init only when we actually process the
7317 	 initializer.  It is not legal to redeclare a static data
7318 	 member, so this issue does not arise in that case.  */
7319       else if (var_definition_p && TREE_STATIC (decl))
7320 	expand_static_init (decl, init);
7321     }
7322 
7323   /* If a CLEANUP_STMT was created to destroy a temporary bound to a
7324      reference, insert it in the statement-tree now.  */
7325   if (cleanups)
7326     {
7327       unsigned i; tree t;
7328       FOR_EACH_VEC_ELT (*cleanups, i, t)
7329 	push_cleanup (decl, t, false);
7330       release_tree_vector (cleanups);
7331     }
7332 
7333   if (was_readonly)
7334     TREE_READONLY (decl) = 1;
7335 
7336   if (flag_openmp
7337       && VAR_P (decl)
7338       && lookup_attribute ("omp declare target implicit",
7339 			   DECL_ATTRIBUTES (decl)))
7340     {
7341       DECL_ATTRIBUTES (decl)
7342 	= remove_attribute ("omp declare target implicit",
7343 			    DECL_ATTRIBUTES (decl));
7344       complete_type (TREE_TYPE (decl));
7345       if (!cp_omp_mappable_type (TREE_TYPE (decl)))
7346 	error ("%q+D in declare target directive does not have mappable type",
7347 	       decl);
7348       else if (!lookup_attribute ("omp declare target",
7349 				  DECL_ATTRIBUTES (decl))
7350 	       && !lookup_attribute ("omp declare target link",
7351 				     DECL_ATTRIBUTES (decl)))
7352 	DECL_ATTRIBUTES (decl)
7353 	  = tree_cons (get_identifier ("omp declare target"),
7354 		       NULL_TREE, DECL_ATTRIBUTES (decl));
7355     }
7356 
7357   invoke_plugin_callbacks (PLUGIN_FINISH_DECL, decl);
7358 }
7359 
7360 /* For class TYPE return itself or some its bases that contain
7361    any direct non-static data members.  Return error_mark_node if an
7362    error has been diagnosed.  */
7363 
7364 static tree
find_decomp_class_base(location_t loc,tree type,tree ret)7365 find_decomp_class_base (location_t loc, tree type, tree ret)
7366 {
7367   bool member_seen = false;
7368   for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
7369     if (TREE_CODE (field) != FIELD_DECL
7370 	|| DECL_ARTIFICIAL (field)
7371 	|| DECL_UNNAMED_BIT_FIELD (field))
7372       continue;
7373     else if (ret)
7374       return type;
7375     else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
7376       {
7377 	if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE)
7378 	  error_at (loc, "cannot decompose class type %qT because it has an "
7379 			 "anonymous struct member", type);
7380 	else
7381 	  error_at (loc, "cannot decompose class type %qT because it has an "
7382 			 "anonymous union member", type);
7383 	inform (DECL_SOURCE_LOCATION (field), "declared here");
7384 	return error_mark_node;
7385       }
7386     else if (!accessible_p (type, field, true))
7387       {
7388 	error_at (loc, "cannot decompose inaccessible member %qD of %qT",
7389 		  field, type);
7390 	inform (DECL_SOURCE_LOCATION (field),
7391 		TREE_PRIVATE (field)
7392 		? G_("declared private here")
7393 		: G_("declared protected here"));
7394 	return error_mark_node;
7395       }
7396     else
7397       member_seen = true;
7398 
7399   tree base_binfo, binfo;
7400   tree orig_ret = ret;
7401   int i;
7402   if (member_seen)
7403     ret = type;
7404   for (binfo = TYPE_BINFO (type), i = 0;
7405        BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
7406     {
7407       tree t = find_decomp_class_base (loc, TREE_TYPE (base_binfo), ret);
7408       if (t == error_mark_node)
7409 	return error_mark_node;
7410       if (t != NULL_TREE && t != ret)
7411 	{
7412 	  if (ret == type)
7413 	    {
7414 	      error_at (loc, "cannot decompose class type %qT: both it and "
7415 			     "its base class %qT have non-static data members",
7416 			type, t);
7417 	      return error_mark_node;
7418 	    }
7419 	  else if (orig_ret != NULL_TREE)
7420 	    return t;
7421 	  else if (ret != NULL_TREE)
7422 	    {
7423 	      error_at (loc, "cannot decompose class type %qT: its base "
7424 			     "classes %qT and %qT have non-static data "
7425 			     "members", type, ret, t);
7426 	      return error_mark_node;
7427 	    }
7428 	  else
7429 	    ret = t;
7430 	}
7431     }
7432   return ret;
7433 }
7434 
7435 /* Return std::tuple_size<TYPE>::value.  */
7436 
7437 static tree
get_tuple_size(tree type)7438 get_tuple_size (tree type)
7439 {
7440   tree args = make_tree_vec (1);
7441   TREE_VEC_ELT (args, 0) = type;
7442   tree inst = lookup_template_class (get_identifier ("tuple_size"), args,
7443 				     /*in_decl*/NULL_TREE,
7444 				     /*context*/std_node,
7445 				     /*entering_scope*/false, tf_none);
7446   inst = complete_type (inst);
7447   if (inst == error_mark_node || !COMPLETE_TYPE_P (inst))
7448     return NULL_TREE;
7449   tree val = lookup_qualified_name (inst, get_identifier ("value"),
7450 				    /*type*/false, /*complain*/false);
7451   if (TREE_CODE (val) == VAR_DECL || TREE_CODE (val) == CONST_DECL)
7452     val = maybe_constant_value (val);
7453   if (TREE_CODE (val) == INTEGER_CST)
7454     return val;
7455   else
7456     return error_mark_node;
7457 }
7458 
7459 /* Return std::tuple_element<I,TYPE>::type.  */
7460 
7461 static tree
get_tuple_element_type(tree type,unsigned i)7462 get_tuple_element_type (tree type, unsigned i)
7463 {
7464   tree args = make_tree_vec (2);
7465   TREE_VEC_ELT (args, 0) = build_int_cst (integer_type_node, i);
7466   TREE_VEC_ELT (args, 1) = type;
7467   tree inst = lookup_template_class (get_identifier ("tuple_element"), args,
7468 				     /*in_decl*/NULL_TREE,
7469 				     /*context*/std_node,
7470 				     /*entering_scope*/false,
7471 				     tf_warning_or_error);
7472   return make_typename_type (inst, get_identifier ("type"),
7473 			     none_type, tf_warning_or_error);
7474 }
7475 
7476 /* Return e.get<i>() or get<i>(e).  */
7477 
7478 static tree
get_tuple_decomp_init(tree decl,unsigned i)7479 get_tuple_decomp_init (tree decl, unsigned i)
7480 {
7481   tree get_id = get_identifier ("get");
7482   tree targs = make_tree_vec (1);
7483   TREE_VEC_ELT (targs, 0) = build_int_cst (integer_type_node, i);
7484 
7485   tree etype = TREE_TYPE (decl);
7486   tree e = convert_from_reference (decl);
7487 
7488   /* [The id-expression] e is an lvalue if the type of the entity e is an
7489      lvalue reference and an xvalue otherwise.  */
7490   if (TREE_CODE (etype) != REFERENCE_TYPE
7491       || TYPE_REF_IS_RVALUE (etype))
7492     e = move (e);
7493 
7494   tree fns = lookup_qualified_name (TREE_TYPE (e), get_id,
7495 				    /*type*/false, /*complain*/false);
7496   bool use_member_get = false;
7497 
7498   /* To use a member get, member lookup must find at least one
7499      declaration that is a function template
7500      whose first template parameter is a non-type parameter.  */
7501   for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (fns)); iter; ++iter)
7502     {
7503       tree fn = *iter;
7504       if (TREE_CODE (fn) == TEMPLATE_DECL)
7505 	{
7506 	  tree tparms = DECL_TEMPLATE_PARMS (fn);
7507 	  tree parm = TREE_VEC_ELT (INNERMOST_TEMPLATE_PARMS (tparms), 0);
7508 	  if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL)
7509 	    {
7510 	      use_member_get = true;
7511 	      break;
7512 	    }
7513 	}
7514     }
7515 
7516   if (use_member_get)
7517     {
7518       fns = lookup_template_function (fns, targs);
7519       return build_new_method_call (e, fns, /*args*/NULL,
7520 				    /*path*/NULL_TREE, LOOKUP_NORMAL,
7521 				    /*fn_p*/NULL, tf_warning_or_error);
7522     }
7523   else
7524     {
7525       vec<tree,va_gc> *args = make_tree_vector_single (e);
7526       fns = lookup_template_function (get_id, targs);
7527       fns = perform_koenig_lookup (fns, args, tf_warning_or_error);
7528       return finish_call_expr (fns, &args, /*novirt*/false,
7529 			       /*koenig*/true, tf_warning_or_error);
7530     }
7531 }
7532 
7533 /* It's impossible to recover the decltype of a tuple decomposition variable
7534    based on the actual type of the variable, so store it in a hash table.  */
7535 
7536 static GTY((cache)) tree_cache_map *decomp_type_table;
7537 static void
store_decomp_type(tree v,tree t)7538 store_decomp_type (tree v, tree t)
7539 {
7540   if (!decomp_type_table)
7541     decomp_type_table = tree_cache_map::create_ggc (13);
7542   decomp_type_table->put (v, t);
7543 }
7544 
7545 tree
lookup_decomp_type(tree v)7546 lookup_decomp_type (tree v)
7547 {
7548   return *decomp_type_table->get (v);
7549 }
7550 
7551 /* Mangle a decomposition declaration if needed.  Arguments like
7552    in cp_finish_decomp.  */
7553 
7554 void
cp_maybe_mangle_decomp(tree decl,tree first,unsigned int count)7555 cp_maybe_mangle_decomp (tree decl, tree first, unsigned int count)
7556 {
7557   if (!processing_template_decl
7558       && !error_operand_p (decl)
7559       && DECL_NAMESPACE_SCOPE_P (decl))
7560     {
7561       auto_vec<tree, 16> v;
7562       v.safe_grow (count);
7563       tree d = first;
7564       for (unsigned int i = 0; i < count; i++, d = DECL_CHAIN (d))
7565 	v[count - i - 1] = d;
7566       SET_DECL_ASSEMBLER_NAME (decl, mangle_decomp (decl, v));
7567       maybe_apply_pragma_weak (decl);
7568     }
7569 }
7570 
7571 /* Finish a decomposition declaration.  DECL is the underlying declaration
7572    "e", FIRST is the head of a chain of decls for the individual identifiers
7573    chained through DECL_CHAIN in reverse order and COUNT is the number of
7574    those decls.  */
7575 
7576 void
cp_finish_decomp(tree decl,tree first,unsigned int count)7577 cp_finish_decomp (tree decl, tree first, unsigned int count)
7578 {
7579   if (error_operand_p (decl))
7580     {
7581      error_out:
7582       while (count--)
7583 	{
7584 	  TREE_TYPE (first) = error_mark_node;
7585 	  if (DECL_HAS_VALUE_EXPR_P (first))
7586 	    {
7587 	      SET_DECL_VALUE_EXPR (first, NULL_TREE);
7588 	      DECL_HAS_VALUE_EXPR_P (first) = 0;
7589 	    }
7590 	  first = DECL_CHAIN (first);
7591 	}
7592       if (DECL_P (decl) && DECL_NAMESPACE_SCOPE_P (decl))
7593 	SET_DECL_ASSEMBLER_NAME (decl, get_identifier ("<decomp>"));
7594       return;
7595     }
7596 
7597   location_t loc = DECL_SOURCE_LOCATION (decl);
7598   if (type_dependent_expression_p (decl)
7599       /* This happens for range for when not in templates.
7600 	 Still add the DECL_VALUE_EXPRs for later processing.  */
7601       || (!processing_template_decl
7602 	  && type_uses_auto (TREE_TYPE (decl))))
7603     {
7604       for (unsigned int i = 0; i < count; i++)
7605 	{
7606 	  if (!DECL_HAS_VALUE_EXPR_P (first))
7607 	    {
7608 	      tree v = build_nt (ARRAY_REF, decl,
7609 				 size_int (count - i - 1),
7610 				 NULL_TREE, NULL_TREE);
7611 	      SET_DECL_VALUE_EXPR (first, v);
7612 	      DECL_HAS_VALUE_EXPR_P (first) = 1;
7613 	    }
7614 	  if (processing_template_decl)
7615 	    fit_decomposition_lang_decl (first, decl);
7616 	  first = DECL_CHAIN (first);
7617 	}
7618       return;
7619     }
7620 
7621   auto_vec<tree, 16> v;
7622   v.safe_grow (count);
7623   tree d = first;
7624   for (unsigned int i = 0; i < count; i++, d = DECL_CHAIN (d))
7625     {
7626       v[count - i - 1] = d;
7627       fit_decomposition_lang_decl (d, decl);
7628     }
7629 
7630   tree type = TREE_TYPE (decl);
7631   tree dexp = decl;
7632 
7633   if (TREE_CODE (type) == REFERENCE_TYPE)
7634     {
7635       dexp = convert_from_reference (dexp);
7636       type = complete_type (TREE_TYPE (type));
7637       if (type == error_mark_node)
7638 	goto error_out;
7639       if (!COMPLETE_TYPE_P (type))
7640 	{
7641 	  error_at (loc, "structured binding refers to incomplete type %qT",
7642 		    type);
7643 	  goto error_out;
7644 	}
7645     }
7646 
7647   tree eltype = NULL_TREE;
7648   unsigned HOST_WIDE_INT eltscnt = 0;
7649   if (TREE_CODE (type) == ARRAY_TYPE)
7650     {
7651       tree nelts;
7652       nelts = array_type_nelts_top (type);
7653       if (nelts == error_mark_node)
7654 	goto error_out;
7655       if (!tree_fits_uhwi_p (nelts))
7656 	{
7657 	  error_at (loc, "cannot decompose variable length array %qT", type);
7658 	  goto error_out;
7659 	}
7660       eltscnt = tree_to_uhwi (nelts);
7661       if (count != eltscnt)
7662 	{
7663        cnt_mismatch:
7664 	  if (count > eltscnt)
7665 	    error_n (loc, count,
7666 		     "%u name provided for structured binding",
7667 		     "%u names provided for structured binding", count);
7668 	  else
7669 	    error_n (loc, count,
7670 		     "only %u name provided for structured binding",
7671 		     "only %u names provided for structured binding", count);
7672 	  inform_n (loc, eltscnt,
7673 		    "while %qT decomposes into %wu element",
7674 		    "while %qT decomposes into %wu elements",
7675 		    type, eltscnt);
7676 	  goto error_out;
7677 	}
7678       eltype = TREE_TYPE (type);
7679       for (unsigned int i = 0; i < count; i++)
7680 	{
7681 	  TREE_TYPE (v[i]) = eltype;
7682 	  layout_decl (v[i], 0);
7683 	  if (processing_template_decl)
7684 	    continue;
7685 	  tree t = unshare_expr (dexp);
7686 	  t = build4_loc (DECL_SOURCE_LOCATION (v[i]), ARRAY_REF,
7687 			  eltype, t, size_int (i), NULL_TREE,
7688 			  NULL_TREE);
7689 	  SET_DECL_VALUE_EXPR (v[i], t);
7690 	  DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
7691 	}
7692     }
7693   /* 2 GNU extensions.  */
7694   else if (TREE_CODE (type) == COMPLEX_TYPE)
7695     {
7696       eltscnt = 2;
7697       if (count != eltscnt)
7698 	goto cnt_mismatch;
7699       eltype = cp_build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
7700       for (unsigned int i = 0; i < count; i++)
7701 	{
7702 	  TREE_TYPE (v[i]) = eltype;
7703 	  layout_decl (v[i], 0);
7704 	  if (processing_template_decl)
7705 	    continue;
7706 	  tree t = unshare_expr (dexp);
7707 	  t = build1_loc (DECL_SOURCE_LOCATION (v[i]),
7708 			  i ? IMAGPART_EXPR : REALPART_EXPR, eltype,
7709 			  t);
7710 	  SET_DECL_VALUE_EXPR (v[i], t);
7711 	  DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
7712 	}
7713     }
7714   else if (TREE_CODE (type) == VECTOR_TYPE)
7715     {
7716       if (!TYPE_VECTOR_SUBPARTS (type).is_constant (&eltscnt))
7717 	{
7718 	  error_at (loc, "cannot decompose variable length vector %qT", type);
7719 	  goto error_out;
7720 	}
7721       if (count != eltscnt)
7722 	goto cnt_mismatch;
7723       eltype = cp_build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
7724       for (unsigned int i = 0; i < count; i++)
7725 	{
7726 	  TREE_TYPE (v[i]) = eltype;
7727 	  layout_decl (v[i], 0);
7728 	  if (processing_template_decl)
7729 	    continue;
7730 	  tree t = unshare_expr (dexp);
7731 	  convert_vector_to_array_for_subscript (DECL_SOURCE_LOCATION (v[i]),
7732 						 &t, size_int (i));
7733 	  t = build4_loc (DECL_SOURCE_LOCATION (v[i]), ARRAY_REF,
7734 			  eltype, t, size_int (i), NULL_TREE,
7735 			  NULL_TREE);
7736 	  SET_DECL_VALUE_EXPR (v[i], t);
7737 	  DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
7738 	}
7739     }
7740   else if (tree tsize = get_tuple_size (type))
7741     {
7742       if (tsize == error_mark_node)
7743 	{
7744 	  error_at (loc, "%<std::tuple_size<%T>::value%> is not an integral "
7745 			 "constant expression", type);
7746 	  goto error_out;
7747 	}
7748       if (!tree_fits_uhwi_p (tsize))
7749 	{
7750 	  error_n (loc, count,
7751 		   "%u name provided for structured binding",
7752 		   "%u names provided for structured binding", count);
7753 	  inform (loc, "while %qT decomposes into %E elements",
7754 		  type, tsize);
7755 	  goto error_out;
7756 	}
7757       eltscnt = tree_to_uhwi (tsize);
7758       if (count != eltscnt)
7759 	goto cnt_mismatch;
7760       int save_read = DECL_READ_P (decl);
7761       for (unsigned i = 0; i < count; ++i)
7762 	{
7763 	  location_t sloc = input_location;
7764 	  location_t dloc = DECL_SOURCE_LOCATION (v[i]);
7765 
7766 	  input_location = dloc;
7767 	  tree init = get_tuple_decomp_init (decl, i);
7768 	  tree eltype = (init == error_mark_node ? error_mark_node
7769 			 : get_tuple_element_type (type, i));
7770 	  input_location = sloc;
7771 
7772 	  if (init == error_mark_node || eltype == error_mark_node)
7773 	    {
7774 	      inform (dloc, "in initialization of structured binding "
7775 		      "variable %qD", v[i]);
7776 	      goto error_out;
7777 	    }
7778 	  /* Save the decltype away before reference collapse.  */
7779 	  store_decomp_type (v[i], eltype);
7780 	  eltype = cp_build_reference_type (eltype, !lvalue_p (init));
7781 	  TREE_TYPE (v[i]) = eltype;
7782 	  layout_decl (v[i], 0);
7783 	  if (DECL_HAS_VALUE_EXPR_P (v[i]))
7784 	    {
7785 	      /* In this case the names are variables, not just proxies.  */
7786 	      SET_DECL_VALUE_EXPR (v[i], NULL_TREE);
7787 	      DECL_HAS_VALUE_EXPR_P (v[i]) = 0;
7788 	    }
7789 	  if (!processing_template_decl)
7790 	    cp_finish_decl (v[i], init, /*constexpr*/false,
7791 			    /*asm*/NULL_TREE, LOOKUP_NORMAL);
7792 	}
7793       /* Ignore reads from the underlying decl performed during initialization
7794 	 of the individual variables.  If those will be read, we'll mark
7795 	 the underlying decl as read at that point.  */
7796       DECL_READ_P (decl) = save_read;
7797     }
7798   else if (TREE_CODE (type) == UNION_TYPE)
7799     {
7800       error_at (loc, "cannot decompose union type %qT", type);
7801       goto error_out;
7802     }
7803   else if (!CLASS_TYPE_P (type))
7804     {
7805       error_at (loc, "cannot decompose non-array non-class type %qT", type);
7806       goto error_out;
7807     }
7808   else if (LAMBDA_TYPE_P (type))
7809     {
7810       error_at (loc, "cannot decompose lambda closure type %qT", type);
7811       goto error_out;
7812     }
7813   else if (processing_template_decl && !COMPLETE_TYPE_P (type))
7814     pedwarn (loc, 0, "structured binding refers to incomplete class type %qT",
7815 	     type);
7816   else
7817     {
7818       tree btype = find_decomp_class_base (loc, type, NULL_TREE);
7819       if (btype == error_mark_node)
7820 	goto error_out;
7821       else if (btype == NULL_TREE)
7822 	{
7823 	  error_at (loc, "cannot decompose class type %qT without non-static "
7824 			 "data members", type);
7825 	  goto error_out;
7826 	}
7827       for (tree field = TYPE_FIELDS (btype); field; field = TREE_CHAIN (field))
7828 	if (TREE_CODE (field) != FIELD_DECL
7829 	    || DECL_ARTIFICIAL (field)
7830 	    || DECL_UNNAMED_BIT_FIELD (field))
7831 	  continue;
7832 	else
7833 	  eltscnt++;
7834       if (count != eltscnt)
7835 	goto cnt_mismatch;
7836       tree t = dexp;
7837       if (type != btype)
7838 	{
7839 	  t = convert_to_base (t, btype, /*check_access*/true,
7840 			       /*nonnull*/false, tf_warning_or_error);
7841 	  type = btype;
7842 	}
7843       unsigned int i = 0;
7844       for (tree field = TYPE_FIELDS (btype); field; field = TREE_CHAIN (field))
7845 	if (TREE_CODE (field) != FIELD_DECL
7846 	    || DECL_ARTIFICIAL (field)
7847 	    || DECL_UNNAMED_BIT_FIELD (field))
7848 	  continue;
7849 	else
7850 	  {
7851 	    tree tt = finish_non_static_data_member (field, unshare_expr (t),
7852 						     NULL_TREE);
7853 	    if (REFERENCE_REF_P (tt))
7854 	      tt = TREE_OPERAND (tt, 0);
7855 	    TREE_TYPE (v[i]) = TREE_TYPE (tt);
7856 	    layout_decl (v[i], 0);
7857 	    if (!processing_template_decl)
7858 	      {
7859 		SET_DECL_VALUE_EXPR (v[i], tt);
7860 		DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
7861 	      }
7862 	    i++;
7863 	  }
7864     }
7865   if (processing_template_decl)
7866     {
7867       for (unsigned int i = 0; i < count; i++)
7868 	if (!DECL_HAS_VALUE_EXPR_P (v[i]))
7869 	  {
7870 	    tree a = build_nt (ARRAY_REF, decl, size_int (i),
7871 			       NULL_TREE, NULL_TREE);
7872 	    SET_DECL_VALUE_EXPR (v[i], a);
7873 	    DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
7874 	  }
7875     }
7876 }
7877 
7878 /* Returns a declaration for a VAR_DECL as if:
7879 
7880      extern "C" TYPE NAME;
7881 
7882    had been seen.  Used to create compiler-generated global
7883    variables.  */
7884 
7885 static tree
declare_global_var(tree name,tree type)7886 declare_global_var (tree name, tree type)
7887 {
7888   tree decl;
7889 
7890   push_to_top_level ();
7891   decl = build_decl (input_location, VAR_DECL, name, type);
7892   TREE_PUBLIC (decl) = 1;
7893   DECL_EXTERNAL (decl) = 1;
7894   DECL_ARTIFICIAL (decl) = 1;
7895   DECL_CONTEXT (decl) = FROB_CONTEXT (global_namespace);
7896   /* If the user has explicitly declared this variable (perhaps
7897      because the code we are compiling is part of a low-level runtime
7898      library), then it is possible that our declaration will be merged
7899      with theirs by pushdecl.  */
7900   decl = pushdecl (decl);
7901   cp_finish_decl (decl, NULL_TREE, false, NULL_TREE, 0);
7902   pop_from_top_level ();
7903 
7904   return decl;
7905 }
7906 
7907 /* Returns the type for the argument to "__cxa_atexit" (or "atexit",
7908    if "__cxa_atexit" is not being used) corresponding to the function
7909    to be called when the program exits.  */
7910 
7911 static tree
get_atexit_fn_ptr_type(void)7912 get_atexit_fn_ptr_type (void)
7913 {
7914   tree fn_type;
7915 
7916   if (!atexit_fn_ptr_type_node)
7917     {
7918       tree arg_type;
7919       if (flag_use_cxa_atexit
7920 	  && !targetm.cxx.use_atexit_for_cxa_atexit ())
7921 	/* The parameter to "__cxa_atexit" is "void (*)(void *)".  */
7922 	arg_type = ptr_type_node;
7923       else
7924 	/* The parameter to "atexit" is "void (*)(void)".  */
7925 	arg_type = NULL_TREE;
7926 
7927       fn_type = build_function_type_list (void_type_node,
7928 					  arg_type, NULL_TREE);
7929       atexit_fn_ptr_type_node = build_pointer_type (fn_type);
7930     }
7931 
7932   return atexit_fn_ptr_type_node;
7933 }
7934 
7935 /* Returns a pointer to the `atexit' function.  Note that if
7936    FLAG_USE_CXA_ATEXIT is nonzero, then this will actually be the new
7937    `__cxa_atexit' function specified in the IA64 C++ ABI.  */
7938 
7939 static tree
get_atexit_node(void)7940 get_atexit_node (void)
7941 {
7942   tree atexit_fndecl;
7943   tree fn_type;
7944   tree fn_ptr_type;
7945   const char *name;
7946   bool use_aeabi_atexit;
7947 
7948   if (atexit_node)
7949     return atexit_node;
7950 
7951   if (flag_use_cxa_atexit && !targetm.cxx.use_atexit_for_cxa_atexit ())
7952     {
7953       /* The declaration for `__cxa_atexit' is:
7954 
7955 	   int __cxa_atexit (void (*)(void *), void *, void *)
7956 
7957 	 We build up the argument types and then the function type
7958 	 itself.  */
7959       tree argtype0, argtype1, argtype2;
7960 
7961       use_aeabi_atexit = targetm.cxx.use_aeabi_atexit ();
7962       /* First, build the pointer-to-function type for the first
7963 	 argument.  */
7964       fn_ptr_type = get_atexit_fn_ptr_type ();
7965       /* Then, build the rest of the argument types.  */
7966       argtype2 = ptr_type_node;
7967       if (use_aeabi_atexit)
7968 	{
7969 	  argtype1 = fn_ptr_type;
7970 	  argtype0 = ptr_type_node;
7971 	}
7972       else
7973 	{
7974 	  argtype1 = ptr_type_node;
7975 	  argtype0 = fn_ptr_type;
7976 	}
7977       /* And the final __cxa_atexit type.  */
7978       fn_type = build_function_type_list (integer_type_node,
7979 					  argtype0, argtype1, argtype2,
7980 					  NULL_TREE);
7981       if (use_aeabi_atexit)
7982 	name = "__aeabi_atexit";
7983       else
7984 	name = "__cxa_atexit";
7985     }
7986   else
7987     {
7988       /* The declaration for `atexit' is:
7989 
7990 	   int atexit (void (*)());
7991 
7992 	 We build up the argument types and then the function type
7993 	 itself.  */
7994       fn_ptr_type = get_atexit_fn_ptr_type ();
7995       /* Build the final atexit type.  */
7996       fn_type = build_function_type_list (integer_type_node,
7997 					  fn_ptr_type, NULL_TREE);
7998       name = "atexit";
7999     }
8000 
8001   /* Now, build the function declaration.  */
8002   push_lang_context (lang_name_c);
8003   atexit_fndecl = build_library_fn_ptr (name, fn_type, ECF_LEAF | ECF_NOTHROW);
8004   mark_used (atexit_fndecl);
8005   pop_lang_context ();
8006   atexit_node = decay_conversion (atexit_fndecl, tf_warning_or_error);
8007 
8008   return atexit_node;
8009 }
8010 
8011 /* Like get_atexit_node, but for thread-local cleanups.  */
8012 
8013 static tree
get_thread_atexit_node(void)8014 get_thread_atexit_node (void)
8015 {
8016   /* The declaration for `__cxa_thread_atexit' is:
8017 
8018      int __cxa_thread_atexit (void (*)(void *), void *, void *) */
8019   tree fn_type = build_function_type_list (integer_type_node,
8020 					   get_atexit_fn_ptr_type (),
8021 					   ptr_type_node, ptr_type_node,
8022 					   NULL_TREE);
8023 
8024   /* Now, build the function declaration.  */
8025   tree atexit_fndecl = build_library_fn_ptr ("__cxa_thread_atexit", fn_type,
8026 					     ECF_LEAF | ECF_NOTHROW);
8027   return decay_conversion (atexit_fndecl, tf_warning_or_error);
8028 }
8029 
8030 /* Returns the __dso_handle VAR_DECL.  */
8031 
8032 static tree
get_dso_handle_node(void)8033 get_dso_handle_node (void)
8034 {
8035   if (dso_handle_node)
8036     return dso_handle_node;
8037 
8038   /* Declare the variable.  */
8039   dso_handle_node = declare_global_var (get_identifier ("__dso_handle"),
8040 					ptr_type_node);
8041 
8042 #ifdef HAVE_GAS_HIDDEN
8043   if (dso_handle_node != error_mark_node)
8044     {
8045       DECL_VISIBILITY (dso_handle_node) = VISIBILITY_HIDDEN;
8046       DECL_VISIBILITY_SPECIFIED (dso_handle_node) = 1;
8047     }
8048 #endif
8049 
8050   return dso_handle_node;
8051 }
8052 
8053 /* Begin a new function with internal linkage whose job will be simply
8054    to destroy some particular variable.  */
8055 
8056 static GTY(()) int start_cleanup_cnt;
8057 
8058 static tree
start_cleanup_fn(void)8059 start_cleanup_fn (void)
8060 {
8061   char name[32];
8062   tree fntype;
8063   tree fndecl;
8064   bool use_cxa_atexit = flag_use_cxa_atexit
8065 			&& !targetm.cxx.use_atexit_for_cxa_atexit ();
8066 
8067   push_to_top_level ();
8068 
8069   /* No need to mangle this.  */
8070   push_lang_context (lang_name_c);
8071 
8072   /* Build the name of the function.  */
8073   sprintf (name, "__tcf_%d", start_cleanup_cnt++);
8074   /* Build the function declaration.  */
8075   fntype = TREE_TYPE (get_atexit_fn_ptr_type ());
8076   fndecl = build_lang_decl (FUNCTION_DECL, get_identifier (name), fntype);
8077   /* It's a function with internal linkage, generated by the
8078      compiler.  */
8079   TREE_PUBLIC (fndecl) = 0;
8080   DECL_ARTIFICIAL (fndecl) = 1;
8081   /* Make the function `inline' so that it is only emitted if it is
8082      actually needed.  It is unlikely that it will be inlined, since
8083      it is only called via a function pointer, but we avoid unnecessary
8084      emissions this way.  */
8085   DECL_DECLARED_INLINE_P (fndecl) = 1;
8086   DECL_INTERFACE_KNOWN (fndecl) = 1;
8087   /* Build the parameter.  */
8088   if (use_cxa_atexit)
8089     {
8090       tree parmdecl = cp_build_parm_decl (fndecl, NULL_TREE, ptr_type_node);
8091       TREE_USED (parmdecl) = 1;
8092       DECL_READ_P (parmdecl) = 1;
8093       DECL_ARGUMENTS (fndecl) = parmdecl;
8094     }
8095 
8096   pushdecl (fndecl);
8097   start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
8098 
8099   pop_lang_context ();
8100 
8101   return current_function_decl;
8102 }
8103 
8104 /* Finish the cleanup function begun by start_cleanup_fn.  */
8105 
8106 static void
end_cleanup_fn(void)8107 end_cleanup_fn (void)
8108 {
8109   expand_or_defer_fn (finish_function (/*inline_p=*/false));
8110 
8111   pop_from_top_level ();
8112 }
8113 
8114 /* Generate code to handle the destruction of DECL, an object with
8115    static storage duration.  */
8116 
8117 tree
register_dtor_fn(tree decl)8118 register_dtor_fn (tree decl)
8119 {
8120   tree cleanup;
8121   tree addr;
8122   tree compound_stmt;
8123   tree fcall;
8124   tree type;
8125   bool ob_parm, dso_parm, use_dtor;
8126   tree arg0, arg1, arg2;
8127   tree atex_node;
8128 
8129   type = TREE_TYPE (decl);
8130   if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
8131     return void_node;
8132 
8133   /* If we're using "__cxa_atexit" (or "__cxa_thread_atexit" or
8134      "__aeabi_atexit"), and DECL is a class object, we can just pass the
8135      destructor to "__cxa_atexit"; we don't have to build a temporary
8136      function to do the cleanup.  */
8137   dso_parm = (flag_use_cxa_atexit
8138 	      && !targetm.cxx.use_atexit_for_cxa_atexit ());
8139   ob_parm = (CP_DECL_THREAD_LOCAL_P (decl) || dso_parm);
8140   use_dtor = ob_parm && CLASS_TYPE_P (type);
8141   if (use_dtor)
8142     {
8143       cleanup = get_class_binding (type, complete_dtor_identifier);
8144 
8145       /* Make sure it is accessible.  */
8146       perform_or_defer_access_check (TYPE_BINFO (type), cleanup, cleanup,
8147 				     tf_warning_or_error);
8148     }
8149   else
8150     {
8151       /* Call build_cleanup before we enter the anonymous function so
8152 	 that any access checks will be done relative to the current
8153 	 scope, rather than the scope of the anonymous function.  */
8154       build_cleanup (decl);
8155 
8156       /* Now start the function.  */
8157       cleanup = start_cleanup_fn ();
8158 
8159       /* Now, recompute the cleanup.  It may contain SAVE_EXPRs that refer
8160 	 to the original function, rather than the anonymous one.  That
8161 	 will make the back end think that nested functions are in use,
8162 	 which causes confusion.  */
8163       push_deferring_access_checks (dk_no_check);
8164       fcall = build_cleanup (decl);
8165       pop_deferring_access_checks ();
8166 
8167       /* Create the body of the anonymous function.  */
8168       compound_stmt = begin_compound_stmt (BCS_FN_BODY);
8169       finish_expr_stmt (fcall);
8170       finish_compound_stmt (compound_stmt);
8171       end_cleanup_fn ();
8172     }
8173 
8174   /* Call atexit with the cleanup function.  */
8175   mark_used (cleanup);
8176   cleanup = build_address (cleanup);
8177 
8178   if (CP_DECL_THREAD_LOCAL_P (decl))
8179     atex_node = get_thread_atexit_node ();
8180   else
8181     atex_node = get_atexit_node ();
8182 
8183   if (use_dtor)
8184     {
8185       /* We must convert CLEANUP to the type that "__cxa_atexit"
8186 	 expects.  */
8187       cleanup = build_nop (get_atexit_fn_ptr_type (), cleanup);
8188       /* "__cxa_atexit" will pass the address of DECL to the
8189 	 cleanup function.  */
8190       mark_used (decl);
8191       addr = build_address (decl);
8192       /* The declared type of the parameter to "__cxa_atexit" is
8193 	 "void *".  For plain "T*", we could just let the
8194 	 machinery in cp_build_function_call convert it -- but if the
8195 	 type is "cv-qualified T *", then we need to convert it
8196 	 before passing it in, to avoid spurious errors.  */
8197       addr = build_nop (ptr_type_node, addr);
8198     }
8199   else
8200     /* Since the cleanup functions we build ignore the address
8201        they're given, there's no reason to pass the actual address
8202        in, and, in general, it's cheaper to pass NULL than any
8203        other value.  */
8204     addr = null_pointer_node;
8205 
8206   if (dso_parm)
8207     arg2 = cp_build_addr_expr (get_dso_handle_node (),
8208 			       tf_warning_or_error);
8209   else if (ob_parm)
8210     /* Just pass NULL to the dso handle parm if we don't actually
8211        have a DSO handle on this target.  */
8212     arg2 = null_pointer_node;
8213   else
8214     arg2 = NULL_TREE;
8215 
8216   if (ob_parm)
8217     {
8218       if (!CP_DECL_THREAD_LOCAL_P (decl)
8219 	  && targetm.cxx.use_aeabi_atexit ())
8220 	{
8221 	  arg1 = cleanup;
8222 	  arg0 = addr;
8223 	}
8224       else
8225 	{
8226 	  arg1 = addr;
8227 	  arg0 = cleanup;
8228 	}
8229     }
8230   else
8231     {
8232       arg0 = cleanup;
8233       arg1 = NULL_TREE;
8234     }
8235   return cp_build_function_call_nary (atex_node, tf_warning_or_error,
8236 				      arg0, arg1, arg2, NULL_TREE);
8237 }
8238 
8239 /* DECL is a VAR_DECL with static storage duration.  INIT, if present,
8240    is its initializer.  Generate code to handle the construction
8241    and destruction of DECL.  */
8242 
8243 static void
expand_static_init(tree decl,tree init)8244 expand_static_init (tree decl, tree init)
8245 {
8246   gcc_assert (VAR_P (decl));
8247   gcc_assert (TREE_STATIC (decl));
8248 
8249   /* Some variables require no dynamic initialization.  */
8250   if (TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
8251     {
8252       /* Make sure the destructor is callable.  */
8253       cxx_maybe_build_cleanup (decl, tf_warning_or_error);
8254       if (!init)
8255 	return;
8256     }
8257 
8258   if (CP_DECL_THREAD_LOCAL_P (decl) && DECL_GNU_TLS_P (decl)
8259       && !DECL_FUNCTION_SCOPE_P (decl))
8260     {
8261       if (init)
8262 	error ("non-local variable %qD declared %<__thread%> "
8263 	       "needs dynamic initialization", decl);
8264       else
8265 	error ("non-local variable %qD declared %<__thread%> "
8266 	       "has a non-trivial destructor", decl);
8267       static bool informed;
8268       if (!informed)
8269 	{
8270 	  inform (DECL_SOURCE_LOCATION (decl),
8271 		  "C++11 %<thread_local%> allows dynamic initialization "
8272 		  "and destruction");
8273 	  informed = true;
8274 	}
8275       return;
8276     }
8277 
8278   if (DECL_FUNCTION_SCOPE_P (decl))
8279     {
8280       /* Emit code to perform this initialization but once.  */
8281       tree if_stmt = NULL_TREE, inner_if_stmt = NULL_TREE;
8282       tree then_clause = NULL_TREE, inner_then_clause = NULL_TREE;
8283       tree guard, guard_addr;
8284       tree flag, begin;
8285       /* We don't need thread-safety code for thread-local vars.  */
8286       bool thread_guard = (flag_threadsafe_statics
8287 			   && !CP_DECL_THREAD_LOCAL_P (decl));
8288 
8289       /* Emit code to perform this initialization but once.  This code
8290 	 looks like:
8291 
8292 	   static <type> guard;
8293 	   if (!__atomic_load (guard.first_byte)) {
8294 	     if (__cxa_guard_acquire (&guard)) {
8295 	       bool flag = false;
8296 	       try {
8297 		 // Do initialization.
8298 		 flag = true; __cxa_guard_release (&guard);
8299 		 // Register variable for destruction at end of program.
8300 	       } catch {
8301 		 if (!flag) __cxa_guard_abort (&guard);
8302 	       }
8303 	     }
8304 	   }
8305 
8306 	 Note that the `flag' variable is only set to 1 *after* the
8307 	 initialization is complete.  This ensures that an exception,
8308 	 thrown during the construction, will cause the variable to
8309 	 reinitialized when we pass through this code again, as per:
8310 
8311 	   [stmt.dcl]
8312 
8313 	   If the initialization exits by throwing an exception, the
8314 	   initialization is not complete, so it will be tried again
8315 	   the next time control enters the declaration.
8316 
8317 	 This process should be thread-safe, too; multiple threads
8318 	 should not be able to initialize the variable more than
8319 	 once.  */
8320 
8321       /* Create the guard variable.  */
8322       guard = get_guard (decl);
8323 
8324       /* Begin the conditional initialization.  */
8325       if_stmt = begin_if_stmt ();
8326 
8327       finish_if_stmt_cond (get_guard_cond (guard, thread_guard), if_stmt);
8328       then_clause = begin_compound_stmt (BCS_NO_SCOPE);
8329 
8330       if (thread_guard)
8331 	{
8332 	  tree vfntype = NULL_TREE;
8333 	  tree acquire_name, release_name, abort_name;
8334 	  tree acquire_fn, release_fn, abort_fn;
8335 	  guard_addr = build_address (guard);
8336 
8337 	  acquire_name = get_identifier ("__cxa_guard_acquire");
8338 	  release_name = get_identifier ("__cxa_guard_release");
8339 	  abort_name = get_identifier ("__cxa_guard_abort");
8340 	  acquire_fn = get_global_binding (acquire_name);
8341 	  release_fn = get_global_binding (release_name);
8342 	  abort_fn = get_global_binding (abort_name);
8343 	  if (!acquire_fn)
8344 	    acquire_fn = push_library_fn
8345 	      (acquire_name, build_function_type_list (integer_type_node,
8346 						       TREE_TYPE (guard_addr),
8347 						       NULL_TREE),
8348 	       NULL_TREE, ECF_NOTHROW);
8349 	  if (!release_fn || !abort_fn)
8350 	    vfntype = build_function_type_list (void_type_node,
8351 						TREE_TYPE (guard_addr),
8352 						NULL_TREE);
8353 	  if (!release_fn)
8354 	    release_fn = push_library_fn (release_name, vfntype, NULL_TREE,
8355 					  ECF_NOTHROW);
8356 	  if (!abort_fn)
8357 	    abort_fn = push_library_fn (abort_name, vfntype, NULL_TREE,
8358 					ECF_NOTHROW | ECF_LEAF);
8359 
8360 	  inner_if_stmt = begin_if_stmt ();
8361 	  finish_if_stmt_cond (build_call_n (acquire_fn, 1, guard_addr),
8362 			       inner_if_stmt);
8363 
8364 	  inner_then_clause = begin_compound_stmt (BCS_NO_SCOPE);
8365 	  begin = get_target_expr (boolean_false_node);
8366 	  flag = TARGET_EXPR_SLOT (begin);
8367 
8368 	  TARGET_EXPR_CLEANUP (begin)
8369 	    = build3 (COND_EXPR, void_type_node, flag,
8370 		      void_node,
8371 		      build_call_n (abort_fn, 1, guard_addr));
8372 	  CLEANUP_EH_ONLY (begin) = 1;
8373 
8374 	  /* Do the initialization itself.  */
8375 	  init = add_stmt_to_compound (begin, init);
8376 	  init = add_stmt_to_compound
8377 	    (init, build2 (MODIFY_EXPR, void_type_node, flag, boolean_true_node));
8378 	  init = add_stmt_to_compound
8379 	    (init, build_call_n (release_fn, 1, guard_addr));
8380 	}
8381       else
8382 	init = add_stmt_to_compound (init, set_guard (guard));
8383 
8384       /* Use atexit to register a function for destroying this static
8385 	 variable.  */
8386       init = add_stmt_to_compound (init, register_dtor_fn (decl));
8387 
8388       finish_expr_stmt (init);
8389 
8390       if (thread_guard)
8391 	{
8392 	  finish_compound_stmt (inner_then_clause);
8393 	  finish_then_clause (inner_if_stmt);
8394 	  finish_if_stmt (inner_if_stmt);
8395 	}
8396 
8397       finish_compound_stmt (then_clause);
8398       finish_then_clause (if_stmt);
8399       finish_if_stmt (if_stmt);
8400     }
8401   else if (CP_DECL_THREAD_LOCAL_P (decl))
8402     tls_aggregates = tree_cons (init, decl, tls_aggregates);
8403   else
8404     static_aggregates = tree_cons (init, decl, static_aggregates);
8405 }
8406 
8407 
8408 /* Make TYPE a complete type based on INITIAL_VALUE.
8409    Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
8410    2 if there was no information (in which case assume 0 if DO_DEFAULT),
8411    3 if the initializer list is empty (in pedantic mode). */
8412 
8413 int
cp_complete_array_type(tree * ptype,tree initial_value,bool do_default)8414 cp_complete_array_type (tree *ptype, tree initial_value, bool do_default)
8415 {
8416   int failure;
8417   tree type, elt_type;
8418 
8419   /* Don't get confused by a CONSTRUCTOR for some other type.  */
8420   if (initial_value && TREE_CODE (initial_value) == CONSTRUCTOR
8421       && !BRACE_ENCLOSED_INITIALIZER_P (initial_value)
8422       && TREE_CODE (TREE_TYPE (initial_value)) != ARRAY_TYPE)
8423     return 1;
8424 
8425   if (initial_value)
8426     {
8427       unsigned HOST_WIDE_INT i;
8428       tree value;
8429 
8430       /* An array of character type can be initialized from a
8431 	 brace-enclosed string constant.
8432 
8433 	 FIXME: this code is duplicated from reshape_init. Probably
8434 	 we should just call reshape_init here?  */
8435       if (char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (*ptype)))
8436 	  && TREE_CODE (initial_value) == CONSTRUCTOR
8437 	  && !vec_safe_is_empty (CONSTRUCTOR_ELTS (initial_value)))
8438 	{
8439 	  vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
8440 	  tree value = (*v)[0].value;
8441 
8442 	  if (TREE_CODE (value) == STRING_CST
8443 	      && v->length () == 1)
8444 	    initial_value = value;
8445 	}
8446 
8447       /* If any of the elements are parameter packs, we can't actually
8448 	 complete this type now because the array size is dependent.  */
8449       if (TREE_CODE (initial_value) == CONSTRUCTOR)
8450 	{
8451 	  FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (initial_value),
8452 				      i, value)
8453 	    {
8454 	      if (PACK_EXPANSION_P (value))
8455 		return 0;
8456 	    }
8457 	}
8458     }
8459 
8460   failure = complete_array_type (ptype, initial_value, do_default);
8461 
8462   /* We can create the array before the element type is complete, which
8463      means that we didn't have these two bits set in the original type
8464      either.  In completing the type, we are expected to propagate these
8465      bits.  See also complete_type which does the same thing for arrays
8466      of fixed size.  */
8467   type = *ptype;
8468   if (type != error_mark_node && TYPE_DOMAIN (type))
8469     {
8470       elt_type = TREE_TYPE (type);
8471       TYPE_NEEDS_CONSTRUCTING (type) = TYPE_NEEDS_CONSTRUCTING (elt_type);
8472       TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
8473 	= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (elt_type);
8474     }
8475 
8476   return failure;
8477 }
8478 
8479 /* As above, but either give an error or reject zero-size arrays, depending
8480    on COMPLAIN.  */
8481 
8482 int
cp_complete_array_type_or_error(tree * ptype,tree initial_value,bool do_default,tsubst_flags_t complain)8483 cp_complete_array_type_or_error (tree *ptype, tree initial_value,
8484 				 bool do_default, tsubst_flags_t complain)
8485 {
8486   int failure;
8487   bool sfinae = !(complain & tf_error);
8488   /* In SFINAE context we can't be lenient about zero-size arrays.  */
8489   if (sfinae)
8490     ++pedantic;
8491   failure = cp_complete_array_type (ptype, initial_value, do_default);
8492   if (sfinae)
8493     --pedantic;
8494   if (failure)
8495     {
8496       if (sfinae)
8497 	/* Not an error.  */;
8498       else if (failure == 1)
8499 	error ("initializer fails to determine size of %qT", *ptype);
8500       else if (failure == 2)
8501 	{
8502 	  if (do_default)
8503 	    error ("array size missing in %qT", *ptype);
8504 	}
8505       else if (failure == 3)
8506 	error ("zero-size array %qT", *ptype);
8507       *ptype = error_mark_node;
8508     }
8509   return failure;
8510 }
8511 
8512 /* Return zero if something is declared to be a member of type
8513    CTYPE when in the context of CUR_TYPE.  STRING is the error
8514    message to print in that case.  Otherwise, quietly return 1.  */
8515 
8516 static int
member_function_or_else(tree ctype,tree cur_type,enum overload_flags flags)8517 member_function_or_else (tree ctype, tree cur_type, enum overload_flags flags)
8518 {
8519   if (ctype && ctype != cur_type)
8520     {
8521       if (flags == DTOR_FLAG)
8522 	error ("destructor for alien class %qT cannot be a member", ctype);
8523       else
8524 	error ("constructor for alien class %qT cannot be a member", ctype);
8525       return 0;
8526     }
8527   return 1;
8528 }
8529 
8530 /* Subroutine of `grokdeclarator'.  */
8531 
8532 /* Generate errors possibly applicable for a given set of specifiers.
8533    This is for ARM $7.1.2.  */
8534 
8535 static void
bad_specifiers(tree object,enum bad_spec_place type,int virtualp,int quals,int inlinep,int friendp,int raises)8536 bad_specifiers (tree object,
8537 		enum bad_spec_place type,
8538 		int virtualp,
8539 		int quals,
8540 		int inlinep,
8541 		int friendp,
8542 		int raises)
8543 {
8544   switch (type)
8545     {
8546       case BSP_VAR:
8547 	if (virtualp)
8548 	  error ("%qD declared as a %<virtual%> variable", object);
8549 	if (quals)
8550 	  error ("%<const%> and %<volatile%> function specifiers on "
8551 	         "%qD invalid in variable declaration", object);
8552 	break;
8553       case BSP_PARM:
8554 	if (virtualp)
8555 	  error ("%qD declared as a %<virtual%> parameter", object);
8556 	if (inlinep)
8557 	  error ("%qD declared as an %<inline%> parameter", object);
8558 	if (quals)
8559 	  error ("%<const%> and %<volatile%> function specifiers on "
8560 	  	 "%qD invalid in parameter declaration", object);
8561 	break;
8562       case BSP_TYPE:
8563 	if (virtualp)
8564 	  error ("%qD declared as a %<virtual%> type", object);
8565 	if (inlinep)
8566 	  error ("%qD declared as an %<inline%> type", object);
8567 	if (quals)
8568 	  error ("%<const%> and %<volatile%> function specifiers on "
8569 	  	 "%qD invalid in type declaration", object);
8570 	break;
8571       case BSP_FIELD:
8572 	if (virtualp)
8573 	  error ("%qD declared as a %<virtual%> field", object);
8574 	if (inlinep)
8575 	  error ("%qD declared as an %<inline%> field", object);
8576 	if (quals)
8577 	  error ("%<const%> and %<volatile%> function specifiers on "
8578 	  	 "%qD invalid in field declaration", object);
8579 	break;
8580       default:
8581         gcc_unreachable();
8582     }
8583   if (friendp)
8584     error ("%q+D declared as a friend", object);
8585   if (raises
8586       && !flag_noexcept_type
8587       && (TREE_CODE (object) == TYPE_DECL
8588 	  || (!TYPE_PTRFN_P (TREE_TYPE (object))
8589 	      && !TYPE_REFFN_P (TREE_TYPE (object))
8590 	      && !TYPE_PTRMEMFUNC_P (TREE_TYPE (object)))))
8591     error ("%q+D declared with an exception specification", object);
8592 }
8593 
8594 /* DECL is a member function or static data member and is presently
8595    being defined.  Check that the definition is taking place in a
8596    valid namespace.  */
8597 
8598 static void
check_class_member_definition_namespace(tree decl)8599 check_class_member_definition_namespace (tree decl)
8600 {
8601   /* These checks only apply to member functions and static data
8602      members.  */
8603   gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
8604   /* We check for problems with specializations in pt.c in
8605      check_specialization_namespace, where we can issue better
8606      diagnostics.  */
8607   if (processing_specialization)
8608     return;
8609   /* We check this in check_explicit_instantiation_namespace.  */
8610   if (processing_explicit_instantiation)
8611     return;
8612   /* [class.mfct]
8613 
8614      A member function definition that appears outside of the
8615      class definition shall appear in a namespace scope enclosing
8616      the class definition.
8617 
8618      [class.static.data]
8619 
8620      The definition for a static data member shall appear in a
8621      namespace scope enclosing the member's class definition.  */
8622   if (!is_ancestor (current_namespace, DECL_CONTEXT (decl)))
8623     permerror (input_location, "definition of %qD is not in namespace enclosing %qT",
8624 	       decl, DECL_CONTEXT (decl));
8625 }
8626 
8627 /* Build a PARM_DECL for the "this" parameter of FN.  TYPE is the
8628    METHOD_TYPE for a non-static member function; QUALS are the
8629    cv-qualifiers that apply to the function.  */
8630 
8631 tree
build_this_parm(tree fn,tree type,cp_cv_quals quals)8632 build_this_parm (tree fn, tree type, cp_cv_quals quals)
8633 {
8634   tree this_type;
8635   tree qual_type;
8636   tree parm;
8637   cp_cv_quals this_quals;
8638 
8639   if (CLASS_TYPE_P (type))
8640     {
8641       this_type
8642 	= cp_build_qualified_type (type, quals & ~TYPE_QUAL_RESTRICT);
8643       this_type = build_pointer_type (this_type);
8644     }
8645   else
8646     this_type = type_of_this_parm (type);
8647   /* The `this' parameter is implicitly `const'; it cannot be
8648      assigned to.  */
8649   this_quals = (quals & TYPE_QUAL_RESTRICT) | TYPE_QUAL_CONST;
8650   qual_type = cp_build_qualified_type (this_type, this_quals);
8651   parm = build_artificial_parm (fn, this_identifier, qual_type);
8652   cp_apply_type_quals_to_decl (this_quals, parm);
8653   return parm;
8654 }
8655 
8656 /* DECL is a static member function.  Complain if it was declared
8657    with function-cv-quals.  */
8658 
8659 static void
check_static_quals(tree decl,cp_cv_quals quals)8660 check_static_quals (tree decl, cp_cv_quals quals)
8661 {
8662   if (quals != TYPE_UNQUALIFIED)
8663     error ("static member function %q#D declared with type qualifiers",
8664 	   decl);
8665 }
8666 
8667 // Check that FN takes no arguments and returns bool.
8668 static void
check_concept_fn(tree fn)8669 check_concept_fn (tree fn)
8670 {
8671   // A constraint is nullary.
8672   if (DECL_ARGUMENTS (fn))
8673     error ("concept %q#D declared with function parameters", fn);
8674 
8675   // The declared return type of the concept shall be bool, and
8676   // it shall not be deduced from it definition.
8677   tree type = TREE_TYPE (TREE_TYPE (fn));
8678   if (is_auto (type))
8679     error ("concept %q#D declared with a deduced return type", fn);
8680   else if (type != boolean_type_node)
8681     error ("concept %q#D with non-%<bool%> return type %qT", fn, type);
8682 }
8683 
8684 /* Helper function.  Replace the temporary this parameter injected
8685    during cp_finish_omp_declare_simd with the real this parameter.  */
8686 
8687 static tree
declare_simd_adjust_this(tree * tp,int * walk_subtrees,void * data)8688 declare_simd_adjust_this (tree *tp, int *walk_subtrees, void *data)
8689 {
8690   tree this_parm = (tree) data;
8691   if (TREE_CODE (*tp) == PARM_DECL
8692       && DECL_NAME (*tp) == this_identifier
8693       && *tp != this_parm)
8694     *tp = this_parm;
8695   else if (TYPE_P (*tp))
8696     *walk_subtrees = 0;
8697   return NULL_TREE;
8698 }
8699 
8700 /* CTYPE is class type, or null if non-class.
8701    TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE
8702    or METHOD_TYPE.
8703    DECLARATOR is the function's name.
8704    PARMS is a chain of PARM_DECLs for the function.
8705    VIRTUALP is truthvalue of whether the function is virtual or not.
8706    FLAGS are to be passed through to `grokclassfn'.
8707    QUALS are qualifiers indicating whether the function is `const'
8708    or `volatile'.
8709    RAISES is a list of exceptions that this function can raise.
8710    CHECK is 1 if we must find this method in CTYPE, 0 if we should
8711    not look, and -1 if we should not call `grokclassfn' at all.
8712 
8713    SFK is the kind of special function (if any) for the new function.
8714 
8715    Returns `NULL_TREE' if something goes wrong, after issuing
8716    applicable error messages.  */
8717 
8718 static tree
grokfndecl(tree ctype,tree type,tree declarator,tree parms,tree orig_declarator,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,int template_count,tree in_namespace,tree * attrlist,location_t location)8719 grokfndecl (tree ctype,
8720 	    tree type,
8721 	    tree declarator,
8722 	    tree parms,
8723 	    tree orig_declarator,
8724 	    tree decl_reqs,
8725 	    int virtualp,
8726 	    enum overload_flags flags,
8727 	    cp_cv_quals quals,
8728 	    cp_ref_qualifier rqual,
8729 	    tree raises,
8730 	    int check,
8731 	    int friendp,
8732 	    int publicp,
8733 	    int inlinep,
8734 	    bool deletedp,
8735 	    special_function_kind sfk,
8736 	    bool funcdef_flag,
8737 	    int template_count,
8738 	    tree in_namespace,
8739 	    tree* attrlist,
8740 	    location_t location)
8741 {
8742   tree decl;
8743   int staticp = ctype && TREE_CODE (type) == FUNCTION_TYPE;
8744   tree t;
8745 
8746   // Was the concept specifier present?
8747   bool concept_p = inlinep & 4;
8748 
8749   // Concept declarations must have a corresponding definition.
8750   if (concept_p && !funcdef_flag)
8751     {
8752       error ("concept %qD has no definition", declarator);
8753       return NULL_TREE;
8754     }
8755 
8756   if (rqual)
8757     type = build_ref_qualified_type (type, rqual);
8758   if (raises)
8759     type = build_exception_variant (type, raises);
8760 
8761   decl = build_lang_decl (FUNCTION_DECL, declarator, type);
8762 
8763   /* Set the constraints on the declaration. */
8764   if (flag_concepts)
8765     {
8766       tree tmpl_reqs = NULL_TREE;
8767       if (processing_template_decl > template_class_depth (ctype))
8768         tmpl_reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
8769 
8770       /* Adjust the required expression into a constraint. */
8771       if (decl_reqs)
8772         decl_reqs = normalize_expression (decl_reqs);
8773 
8774       tree ci = build_constraints (tmpl_reqs, decl_reqs);
8775       set_constraints (decl, ci);
8776     }
8777 
8778   /* If we have an explicit location, use it, otherwise use whatever
8779      build_lang_decl used (probably input_location).  */
8780   if (location != UNKNOWN_LOCATION)
8781     DECL_SOURCE_LOCATION (decl) = location;
8782 
8783   if (TREE_CODE (type) == METHOD_TYPE)
8784     {
8785       tree parm = build_this_parm (decl, type, quals);
8786       DECL_CHAIN (parm) = parms;
8787       parms = parm;
8788 
8789       /* Allocate space to hold the vptr bit if needed.  */
8790       SET_DECL_ALIGN (decl, MINIMUM_METHOD_BOUNDARY);
8791     }
8792 
8793   DECL_ARGUMENTS (decl) = parms;
8794   for (t = parms; t; t = DECL_CHAIN (t))
8795     DECL_CONTEXT (t) = decl;
8796 
8797   /* Propagate volatile out from type to decl.  */
8798   if (TYPE_VOLATILE (type))
8799     TREE_THIS_VOLATILE (decl) = 1;
8800 
8801   /* Setup decl according to sfk.  */
8802   switch (sfk)
8803     {
8804     case sfk_constructor:
8805     case sfk_copy_constructor:
8806     case sfk_move_constructor:
8807       DECL_CXX_CONSTRUCTOR_P (decl) = 1;
8808       DECL_NAME (decl) = ctor_identifier;
8809       break;
8810     case sfk_destructor:
8811       DECL_CXX_DESTRUCTOR_P (decl) = 1;
8812       DECL_NAME (decl) = dtor_identifier;
8813       break;
8814     default:
8815       break;
8816     }
8817 
8818   if (friendp && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)
8819     {
8820       if (funcdef_flag)
8821 	error ("defining explicit specialization %qD in friend declaration",
8822 	       orig_declarator);
8823       else
8824 	{
8825 	  tree fns = TREE_OPERAND (orig_declarator, 0);
8826 	  tree args = TREE_OPERAND (orig_declarator, 1);
8827 
8828 	  if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8829 	    {
8830 	      /* Something like `template <class T> friend void f<T>()'.  */
8831 	      error ("invalid use of template-id %qD in declaration "
8832 		     "of primary template",
8833 		     orig_declarator);
8834 	      return NULL_TREE;
8835 	    }
8836 
8837 
8838 	  /* A friend declaration of the form friend void f<>().  Record
8839 	     the information in the TEMPLATE_ID_EXPR.  */
8840 	  SET_DECL_IMPLICIT_INSTANTIATION (decl);
8841 
8842 	  gcc_assert (identifier_p (fns) || TREE_CODE (fns) == OVERLOAD);
8843 	  DECL_TEMPLATE_INFO (decl) = build_template_info (fns, args);
8844 
8845 	  for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t))
8846 	    if (TREE_PURPOSE (t)
8847 		&& TREE_CODE (TREE_PURPOSE (t)) == DEFAULT_ARG)
8848 	    {
8849 	      error ("default arguments are not allowed in declaration "
8850 		     "of friend template specialization %qD",
8851 		     decl);
8852 	      return NULL_TREE;
8853 	    }
8854 
8855 	  if (inlinep & 1)
8856 	    {
8857 	      error ("%<inline%> is not allowed in declaration of friend "
8858 		     "template specialization %qD",
8859 		     decl);
8860 	      return NULL_TREE;
8861 	    }
8862 	}
8863     }
8864 
8865   /* If this decl has namespace scope, set that up.  */
8866   if (in_namespace)
8867     set_decl_namespace (decl, in_namespace, friendp);
8868   else if (!ctype)
8869     DECL_CONTEXT (decl) = FROB_CONTEXT (current_decl_namespace ());
8870 
8871   /* `main' and builtins have implicit 'C' linkage.  */
8872   if (ctype == NULL_TREE
8873       && DECL_FILE_SCOPE_P (decl)
8874       && current_lang_name == lang_name_cplusplus
8875       && (MAIN_NAME_P (declarator)
8876 	  || (IDENTIFIER_LENGTH (declarator) > 10
8877 	      && IDENTIFIER_POINTER (declarator)[0] == '_'
8878 	      && IDENTIFIER_POINTER (declarator)[1] == '_'
8879 	      && strncmp (IDENTIFIER_POINTER (declarator)+2,
8880 			  "builtin_", 8) == 0)
8881 	  || (targetcm.cxx_implicit_extern_c
8882 	      && (targetcm.cxx_implicit_extern_c
8883 		  (IDENTIFIER_POINTER (declarator))))))
8884     SET_DECL_LANGUAGE (decl, lang_c);
8885 
8886   /* Should probably propagate const out from type to decl I bet (mrs).  */
8887   if (staticp)
8888     {
8889       DECL_STATIC_FUNCTION_P (decl) = 1;
8890       DECL_CONTEXT (decl) = ctype;
8891     }
8892 
8893   if (deletedp)
8894     DECL_DELETED_FN (decl) = 1;
8895 
8896   if (ctype)
8897     {
8898       DECL_CONTEXT (decl) = ctype;
8899       if (funcdef_flag)
8900 	check_class_member_definition_namespace (decl);
8901     }
8902 
8903   if (ctype == NULL_TREE && DECL_MAIN_P (decl))
8904     {
8905       if (PROCESSING_REAL_TEMPLATE_DECL_P())
8906 	error ("cannot declare %<::main%> to be a template");
8907       if (inlinep & 1)
8908 	error ("cannot declare %<::main%> to be inline");
8909       if (inlinep & 2)
8910 	error ("cannot declare %<::main%> to be %<constexpr%>");
8911       if (!publicp)
8912 	error ("cannot declare %<::main%> to be static");
8913       inlinep = 0;
8914       publicp = 1;
8915     }
8916 
8917   /* Members of anonymous types and local classes have no linkage; make
8918      them internal.  If a typedef is made later, this will be changed.  */
8919   if (ctype && (!TREE_PUBLIC (TYPE_MAIN_DECL (ctype))
8920 		|| decl_function_context (TYPE_MAIN_DECL (ctype))))
8921     publicp = 0;
8922 
8923   if (publicp && cxx_dialect == cxx98)
8924     {
8925       /* [basic.link]: A name with no linkage (notably, the name of a class
8926 	 or enumeration declared in a local scope) shall not be used to
8927 	 declare an entity with linkage.
8928 
8929 	 DR 757 relaxes this restriction for C++0x.  */
8930       no_linkage_error (decl);
8931     }
8932 
8933   TREE_PUBLIC (decl) = publicp;
8934   if (! publicp)
8935     {
8936       DECL_INTERFACE_KNOWN (decl) = 1;
8937       DECL_NOT_REALLY_EXTERN (decl) = 1;
8938     }
8939 
8940   /* If the declaration was declared inline, mark it as such.  */
8941   if (inlinep)
8942     {
8943       DECL_DECLARED_INLINE_P (decl) = 1;
8944       if (publicp)
8945 	DECL_COMDAT (decl) = 1;
8946     }
8947   if (inlinep & 2)
8948     DECL_DECLARED_CONSTEXPR_P (decl) = true;
8949 
8950   // If the concept declaration specifier was found, check
8951   // that the declaration satisfies the necessary requirements.
8952   if (concept_p)
8953     {
8954       DECL_DECLARED_CONCEPT_P (decl) = true;
8955       check_concept_fn (decl);
8956     }
8957 
8958   DECL_EXTERNAL (decl) = 1;
8959   if (TREE_CODE (type) == FUNCTION_TYPE)
8960     {
8961       if (quals || rqual)
8962 	TREE_TYPE (decl) = apply_memfn_quals (TREE_TYPE (decl),
8963 					      TYPE_UNQUALIFIED,
8964 					      REF_QUAL_NONE);
8965 
8966       if (quals)
8967 	{
8968 	  error (ctype
8969 		 ? G_("static member function %qD cannot have cv-qualifier")
8970 		 : G_("non-member function %qD cannot have cv-qualifier"),
8971 		 decl);
8972 	  quals = TYPE_UNQUALIFIED;
8973 	}
8974 
8975       if (rqual)
8976 	{
8977 	  error (ctype
8978 		 ? G_("static member function %qD cannot have ref-qualifier")
8979 		 : G_("non-member function %qD cannot have ref-qualifier"),
8980 		 decl);
8981 	  rqual = REF_QUAL_NONE;
8982 	}
8983     }
8984 
8985   if (deduction_guide_p (decl))
8986     {
8987       if (!DECL_NAMESPACE_SCOPE_P (decl))
8988 	{
8989 	  error_at (location, "deduction guide %qD must be declared at "
8990 		    "namespace scope", decl);
8991 	  return NULL_TREE;
8992 	}
8993       if (funcdef_flag)
8994 	error_at (location,
8995 		  "deduction guide %qD must not have a function body", decl);
8996     }
8997   else if (IDENTIFIER_ANY_OP_P (DECL_NAME (decl))
8998 	   && !grok_op_properties (decl, /*complain=*/true))
8999     return NULL_TREE;
9000   else if (UDLIT_OPER_P (DECL_NAME (decl)))
9001     {
9002       bool long_long_unsigned_p;
9003       bool long_double_p;
9004       const char *suffix = NULL;
9005       /* [over.literal]/6: Literal operators shall not have C linkage. */
9006       if (DECL_LANGUAGE (decl) == lang_c)
9007 	{
9008 	  error ("literal operator with C linkage");
9009 	  maybe_show_extern_c_location ();
9010 	  return NULL_TREE;
9011 	}
9012 
9013       if (DECL_NAMESPACE_SCOPE_P (decl))
9014 	{
9015 	  if (!check_literal_operator_args (decl, &long_long_unsigned_p,
9016 					    &long_double_p))
9017 	    {
9018 	      error ("%qD has invalid argument list", decl);
9019 	      return NULL_TREE;
9020 	    }
9021 
9022 	  suffix = UDLIT_OP_SUFFIX (DECL_NAME (decl));
9023 	  if (long_long_unsigned_p)
9024 	    {
9025 	      if (cpp_interpret_int_suffix (parse_in, suffix, strlen (suffix)))
9026 		warning (0, "integer suffix %qs"
9027 			    " shadowed by implementation", suffix);
9028 	    }
9029 	  else if (long_double_p)
9030 	    {
9031 	      if (cpp_interpret_float_suffix (parse_in, suffix, strlen (suffix)))
9032 		warning (0, "floating point suffix %qs"
9033 			    " shadowed by implementation", suffix);
9034 	    }
9035 	  /* 17.6.3.3.5  */
9036 	  if (suffix[0] != '_'
9037 	      && !in_system_header_at (DECL_SOURCE_LOCATION (decl))
9038 	      && !current_function_decl && !(friendp && !funcdef_flag))
9039 	    warning (OPT_Wliteral_suffix,
9040 		     "literal operator suffixes not preceded by %<_%>"
9041 		     " are reserved for future standardization");
9042 	}
9043       else
9044 	{
9045 	  error ("%qD must be a non-member function", decl);
9046 	  return NULL_TREE;
9047 	}
9048     }
9049 
9050   if (funcdef_flag)
9051     /* Make the init_value nonzero so pushdecl knows this is not
9052        tentative.  error_mark_node is replaced later with the BLOCK.  */
9053     DECL_INITIAL (decl) = error_mark_node;
9054 
9055   if (TYPE_NOTHROW_P (type) || nothrow_libfn_p (decl))
9056     TREE_NOTHROW (decl) = 1;
9057 
9058   if (flag_openmp || flag_openmp_simd)
9059     {
9060       /* Adjust "omp declare simd" attributes.  */
9061       tree ods = lookup_attribute ("omp declare simd", *attrlist);
9062       if (ods)
9063 	{
9064 	  tree attr;
9065 	  for (attr = ods; attr;
9066 	       attr = lookup_attribute ("omp declare simd", TREE_CHAIN (attr)))
9067 	    {
9068 	      if (TREE_CODE (type) == METHOD_TYPE)
9069 		walk_tree (&TREE_VALUE (attr), declare_simd_adjust_this,
9070 			   DECL_ARGUMENTS (decl), NULL);
9071 	      if (TREE_VALUE (attr) != NULL_TREE)
9072 		{
9073 		  tree cl = TREE_VALUE (TREE_VALUE (attr));
9074 		  cl = c_omp_declare_simd_clauses_to_numbers
9075 						(DECL_ARGUMENTS (decl), cl);
9076 		  if (cl)
9077 		    TREE_VALUE (TREE_VALUE (attr)) = cl;
9078 		  else
9079 		    TREE_VALUE (attr) = NULL_TREE;
9080 		}
9081 	    }
9082 	}
9083     }
9084 
9085   /* Caller will do the rest of this.  */
9086   if (check < 0)
9087     return decl;
9088 
9089   if (ctype != NULL_TREE)
9090     grokclassfn (ctype, decl, flags);
9091 
9092   /* 12.4/3  */
9093   if (cxx_dialect >= cxx11
9094       && DECL_DESTRUCTOR_P (decl)
9095       && !TYPE_BEING_DEFINED (DECL_CONTEXT (decl))
9096       && !processing_template_decl)
9097     deduce_noexcept_on_destructor (decl);
9098 
9099   decl = check_explicit_specialization (orig_declarator, decl,
9100 					template_count,
9101 					2 * funcdef_flag +
9102 					4 * (friendp != 0) +
9103 	                                8 * concept_p,
9104 					*attrlist);
9105   if (decl == error_mark_node)
9106     return NULL_TREE;
9107 
9108   if (DECL_STATIC_FUNCTION_P (decl))
9109     check_static_quals (decl, quals);
9110 
9111   if (attrlist)
9112     {
9113       cplus_decl_attributes (&decl, *attrlist, 0);
9114       *attrlist = NULL_TREE;
9115     }
9116 
9117   /* Check main's type after attributes have been applied.  */
9118   if (ctype == NULL_TREE && DECL_MAIN_P (decl))
9119     {
9120       if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
9121 			integer_type_node))
9122 	{
9123 	  tree oldtypeargs = TYPE_ARG_TYPES (TREE_TYPE (decl));
9124 	  tree newtype;
9125 	  error ("%<::main%> must return %<int%>");
9126 	  newtype = build_function_type (integer_type_node, oldtypeargs);
9127 	  TREE_TYPE (decl) = newtype;
9128 	}
9129       if (warn_main)
9130 	check_main_parameter_types (decl);
9131     }
9132 
9133   if (ctype != NULL_TREE && check)
9134     {
9135       tree old_decl = check_classfn (ctype, decl,
9136 				     (processing_template_decl
9137 				      > template_class_depth (ctype))
9138 				     ? current_template_parms
9139 				     : NULL_TREE);
9140 
9141       if (old_decl == error_mark_node)
9142 	return NULL_TREE;
9143 
9144       if (old_decl)
9145 	{
9146 	  tree ok;
9147 	  tree pushed_scope;
9148 
9149 	  if (TREE_CODE (old_decl) == TEMPLATE_DECL)
9150 	    /* Because grokfndecl is always supposed to return a
9151 	       FUNCTION_DECL, we pull out the DECL_TEMPLATE_RESULT
9152 	       here.  We depend on our callers to figure out that its
9153 	       really a template that's being returned.  */
9154 	    old_decl = DECL_TEMPLATE_RESULT (old_decl);
9155 
9156 	  if (DECL_STATIC_FUNCTION_P (old_decl)
9157 	      && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
9158 	    {
9159 	      /* Remove the `this' parm added by grokclassfn.  */
9160 	      revert_static_member_fn (decl);
9161 	      check_static_quals (decl, quals);
9162 	    }
9163 	  if (DECL_ARTIFICIAL (old_decl))
9164 	    {
9165 	      error ("definition of implicitly-declared %qD", old_decl);
9166 	      return NULL_TREE;
9167 	    }
9168 	  else if (DECL_DEFAULTED_FN (old_decl))
9169 	    {
9170 	      error ("definition of explicitly-defaulted %q+D", decl);
9171 	      inform (DECL_SOURCE_LOCATION (old_decl),
9172 		      "%q#D explicitly defaulted here", old_decl);
9173 	      return NULL_TREE;
9174 	    }
9175 
9176 	  /* Since we've smashed OLD_DECL to its
9177 	     DECL_TEMPLATE_RESULT, we must do the same to DECL.  */
9178 	  if (TREE_CODE (decl) == TEMPLATE_DECL)
9179 	    decl = DECL_TEMPLATE_RESULT (decl);
9180 
9181 	  /* Attempt to merge the declarations.  This can fail, in
9182 	     the case of some invalid specialization declarations.  */
9183 	  pushed_scope = push_scope (ctype);
9184 	  ok = duplicate_decls (decl, old_decl, friendp);
9185 	  if (pushed_scope)
9186 	    pop_scope (pushed_scope);
9187 	  if (!ok)
9188 	    {
9189 	      error ("no %q#D member function declared in class %qT",
9190 		     decl, ctype);
9191 	      return NULL_TREE;
9192 	    }
9193 	  if (ok == error_mark_node)
9194 	    return NULL_TREE;
9195 	  return old_decl;
9196 	}
9197     }
9198 
9199   if (DECL_CONSTRUCTOR_P (decl) && !grok_ctor_properties (ctype, decl))
9200     return NULL_TREE;
9201 
9202   if (ctype == NULL_TREE || check)
9203     return decl;
9204 
9205   if (virtualp)
9206     DECL_VIRTUAL_P (decl) = 1;
9207 
9208   return decl;
9209 }
9210 
9211 /* decl is a FUNCTION_DECL.
9212    specifiers are the parsed virt-specifiers.
9213 
9214    Set flags to reflect the virt-specifiers.
9215 
9216    Returns decl.  */
9217 
9218 static tree
set_virt_specifiers(tree decl,cp_virt_specifiers specifiers)9219 set_virt_specifiers (tree decl, cp_virt_specifiers specifiers)
9220 {
9221   if (decl == NULL_TREE)
9222     return decl;
9223   if (specifiers & VIRT_SPEC_OVERRIDE)
9224     DECL_OVERRIDE_P (decl) = 1;
9225   if (specifiers & VIRT_SPEC_FINAL)
9226     DECL_FINAL_P (decl) = 1;
9227   return decl;
9228 }
9229 
9230 /* DECL is a VAR_DECL for a static data member.  Set flags to reflect
9231    the linkage that DECL will receive in the object file.  */
9232 
9233 static void
set_linkage_for_static_data_member(tree decl)9234 set_linkage_for_static_data_member (tree decl)
9235 {
9236   /* A static data member always has static storage duration and
9237      external linkage.  Note that static data members are forbidden in
9238      local classes -- the only situation in which a class has
9239      non-external linkage.  */
9240   TREE_PUBLIC (decl) = 1;
9241   TREE_STATIC (decl) = 1;
9242   /* For non-template classes, static data members are always put
9243      out in exactly those files where they are defined, just as
9244      with ordinary namespace-scope variables.  */
9245   if (!processing_template_decl)
9246     DECL_INTERFACE_KNOWN (decl) = 1;
9247 }
9248 
9249 /* Create a VAR_DECL named NAME with the indicated TYPE.
9250 
9251    If SCOPE is non-NULL, it is the class type or namespace containing
9252    the variable.  If SCOPE is NULL, the variable should is created in
9253    the innermost enclosing scope.  */
9254 
9255 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)9256 grokvardecl (tree type,
9257 	     tree name,
9258 	     tree orig_declarator,
9259 	     const cp_decl_specifier_seq *declspecs,
9260 	     int initialized,
9261 	     int type_quals,
9262 	     int inlinep,
9263 	     bool conceptp,
9264 	     int template_count,
9265 	     tree scope)
9266 {
9267   tree decl;
9268   tree explicit_scope;
9269 
9270   gcc_assert (!name || identifier_p (name));
9271 
9272   bool constp = (type_quals & TYPE_QUAL_CONST) != 0;
9273   bool volatilep = (type_quals & TYPE_QUAL_VOLATILE) != 0;
9274 
9275   /* Compute the scope in which to place the variable, but remember
9276      whether or not that scope was explicitly specified by the user.   */
9277   explicit_scope = scope;
9278   if (!scope)
9279     {
9280       /* An explicit "extern" specifier indicates a namespace-scope
9281 	 variable.  */
9282       if (declspecs->storage_class == sc_extern)
9283 	scope = current_decl_namespace ();
9284       else if (!at_function_scope_p ())
9285 	scope = current_scope ();
9286     }
9287 
9288   if (scope
9289       && (/* If the variable is a namespace-scope variable declared in a
9290 	     template, we need DECL_LANG_SPECIFIC.  */
9291 	  (TREE_CODE (scope) == NAMESPACE_DECL && processing_template_decl)
9292 	  /* Similarly for namespace-scope variables with language linkage
9293 	     other than C++.  */
9294 	  || (TREE_CODE (scope) == NAMESPACE_DECL
9295 	      && current_lang_name != lang_name_cplusplus)
9296 	  /* Similarly for static data members.  */
9297 	  || TYPE_P (scope)
9298 	  /* Similarly for explicit specializations.  */
9299 	  || (orig_declarator
9300 	      && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)))
9301     decl = build_lang_decl (VAR_DECL, name, type);
9302   else
9303     decl = build_decl (input_location, VAR_DECL, name, type);
9304 
9305   if (explicit_scope && TREE_CODE (explicit_scope) == NAMESPACE_DECL)
9306     set_decl_namespace (decl, explicit_scope, 0);
9307   else
9308     DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
9309 
9310   if (declspecs->storage_class == sc_extern)
9311     {
9312       DECL_THIS_EXTERN (decl) = 1;
9313       DECL_EXTERNAL (decl) = !initialized;
9314     }
9315 
9316   if (DECL_CLASS_SCOPE_P (decl))
9317     {
9318       set_linkage_for_static_data_member (decl);
9319       /* This function is only called with out-of-class definitions.  */
9320       DECL_EXTERNAL (decl) = 0;
9321       check_class_member_definition_namespace (decl);
9322     }
9323   /* At top level, either `static' or no s.c. makes a definition
9324      (perhaps tentative), and absence of `static' makes it public.  */
9325   else if (toplevel_bindings_p ())
9326     {
9327       TREE_PUBLIC (decl) = (declspecs->storage_class != sc_static
9328 			    && (DECL_THIS_EXTERN (decl)
9329 				|| ! constp
9330 				|| volatilep
9331 				|| inlinep));
9332       TREE_STATIC (decl) = ! DECL_EXTERNAL (decl);
9333     }
9334   /* Not at top level, only `static' makes a static definition.  */
9335   else
9336     {
9337       TREE_STATIC (decl) = declspecs->storage_class == sc_static;
9338       TREE_PUBLIC (decl) = DECL_EXTERNAL (decl);
9339     }
9340 
9341   if (decl_spec_seq_has_spec_p (declspecs, ds_thread))
9342     {
9343       if (DECL_EXTERNAL (decl) || TREE_STATIC (decl))
9344 	{
9345 	  CP_DECL_THREAD_LOCAL_P (decl) = true;
9346 	  if (!processing_template_decl)
9347 	    set_decl_tls_model (decl, decl_default_tls_model (decl));
9348 	}
9349       if (declspecs->gnu_thread_keyword_p)
9350 	SET_DECL_GNU_TLS_P (decl);
9351     }
9352 
9353   /* If the type of the decl has no linkage, make sure that we'll
9354      notice that in mark_used.  */
9355   if (cxx_dialect > cxx98
9356       && decl_linkage (decl) != lk_none
9357       && DECL_LANG_SPECIFIC (decl) == NULL
9358       && !DECL_EXTERN_C_P (decl)
9359       && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
9360     retrofit_lang_decl (decl);
9361 
9362   if (TREE_PUBLIC (decl))
9363     {
9364       /* [basic.link]: A name with no linkage (notably, the name of a class
9365 	 or enumeration declared in a local scope) shall not be used to
9366 	 declare an entity with linkage.
9367 
9368 	 DR 757 relaxes this restriction for C++0x.  */
9369       if (cxx_dialect < cxx11)
9370 	no_linkage_error (decl);
9371     }
9372   else
9373     DECL_INTERFACE_KNOWN (decl) = 1;
9374 
9375   if (DECL_NAME (decl)
9376       && MAIN_NAME_P (DECL_NAME (decl))
9377       && scope == global_namespace)
9378     error ("cannot declare %<::main%> to be a global variable");
9379 
9380   /* Check that the variable can be safely declared as a concept.
9381      Note that this also forbids explicit specializations.  */
9382   if (conceptp)
9383     {
9384       if (!processing_template_decl)
9385         {
9386           error ("a non-template variable cannot be %<concept%>");
9387           return NULL_TREE;
9388         }
9389       else
9390         DECL_DECLARED_CONCEPT_P (decl) = true;
9391       if (!same_type_ignoring_top_level_qualifiers_p (type, boolean_type_node))
9392 	error_at (declspecs->locations[ds_type_spec],
9393 		  "concept must have type %<bool%>");
9394     }
9395   else if (flag_concepts
9396 	   && processing_template_decl > template_class_depth (scope))
9397     {
9398       tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
9399       tree ci = build_constraints (reqs, NULL_TREE);
9400       set_constraints (decl, ci);
9401     }
9402 
9403   // Handle explicit specializations and instantiations of variable templates.
9404   if (orig_declarator)
9405     decl = check_explicit_specialization (orig_declarator, decl,
9406 					  template_count, conceptp * 8);
9407 
9408   return decl != error_mark_node ? decl : NULL_TREE;
9409 }
9410 
9411 /* Create and return a canonical pointer to member function type, for
9412    TYPE, which is a POINTER_TYPE to a METHOD_TYPE.  */
9413 
9414 tree
build_ptrmemfunc_type(tree type)9415 build_ptrmemfunc_type (tree type)
9416 {
9417   tree field, fields;
9418   tree t;
9419 
9420   if (type == error_mark_node)
9421     return type;
9422 
9423   /* Make sure that we always have the unqualified pointer-to-member
9424      type first.  */
9425   if (cp_cv_quals quals = cp_type_quals (type))
9426     {
9427       tree unqual = build_ptrmemfunc_type (TYPE_MAIN_VARIANT (type));
9428       return cp_build_qualified_type (unqual, quals);
9429     }
9430 
9431   /* If a canonical type already exists for this type, use it.  We use
9432      this method instead of type_hash_canon, because it only does a
9433      simple equality check on the list of field members.  */
9434 
9435   t = TYPE_PTRMEMFUNC_TYPE (type);
9436   if (t)
9437     return t;
9438 
9439   t = make_node (RECORD_TYPE);
9440 
9441   /* Let the front end know this is a pointer to member function.  */
9442   TYPE_PTRMEMFUNC_FLAG (t) = 1;
9443 
9444   field = build_decl (input_location, FIELD_DECL, pfn_identifier, type);
9445   fields = field;
9446 
9447   field = build_decl (input_location, FIELD_DECL, delta_identifier,
9448 		      delta_type_node);
9449   DECL_CHAIN (field) = fields;
9450   fields = field;
9451 
9452   finish_builtin_struct (t, "__ptrmemfunc_type", fields, ptr_type_node);
9453 
9454   /* Zap out the name so that the back end will give us the debugging
9455      information for this anonymous RECORD_TYPE.  */
9456   TYPE_NAME (t) = NULL_TREE;
9457 
9458   /* Cache this pointer-to-member type so that we can find it again
9459      later.  */
9460   TYPE_PTRMEMFUNC_TYPE (type) = t;
9461 
9462   if (TYPE_STRUCTURAL_EQUALITY_P (type))
9463     SET_TYPE_STRUCTURAL_EQUALITY (t);
9464   else if (TYPE_CANONICAL (type) != type)
9465     TYPE_CANONICAL (t) = build_ptrmemfunc_type (TYPE_CANONICAL (type));
9466 
9467   return t;
9468 }
9469 
9470 /* Create and return a pointer to data member type.  */
9471 
9472 tree
build_ptrmem_type(tree class_type,tree member_type)9473 build_ptrmem_type (tree class_type, tree member_type)
9474 {
9475   if (TREE_CODE (member_type) == METHOD_TYPE)
9476     {
9477       cp_cv_quals quals = type_memfn_quals (member_type);
9478       cp_ref_qualifier rqual = type_memfn_rqual (member_type);
9479       member_type = build_memfn_type (member_type, class_type, quals, rqual);
9480       return build_ptrmemfunc_type (build_pointer_type (member_type));
9481     }
9482   else
9483     {
9484       gcc_assert (TREE_CODE (member_type) != FUNCTION_TYPE);
9485       return build_offset_type (class_type, member_type);
9486     }
9487 }
9488 
9489 /* DECL is a VAR_DECL defined in-class, whose TYPE is also given.
9490    Check to see that the definition is valid.  Issue appropriate error
9491    messages.  Return 1 if the definition is particularly bad, or 0
9492    otherwise.  */
9493 
9494 static int
check_static_variable_definition(tree decl,tree type)9495 check_static_variable_definition (tree decl, tree type)
9496 {
9497   /* Avoid redundant diagnostics on out-of-class definitions.  */
9498   if (!current_class_type || !TYPE_BEING_DEFINED (current_class_type))
9499     return 0;
9500   /* Can't check yet if we don't know the type.  */
9501   if (dependent_type_p (type))
9502     return 0;
9503   /* If DECL is declared constexpr, we'll do the appropriate checks
9504      in check_initializer.  Similarly for inline static data members.  */
9505   if (DECL_P (decl)
9506       && (DECL_DECLARED_CONSTEXPR_P (decl)
9507 	  || DECL_VAR_DECLARED_INLINE_P (decl)))
9508     return 0;
9509   else if (cxx_dialect >= cxx11 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
9510     {
9511       if (!COMPLETE_TYPE_P (type))
9512 	error_at (DECL_SOURCE_LOCATION (decl),
9513 		  "in-class initialization of static data member %q#D of "
9514 		  "incomplete type", decl);
9515       else if (literal_type_p (type))
9516 	permerror (DECL_SOURCE_LOCATION (decl),
9517 		   "%<constexpr%> needed for in-class initialization of "
9518 		   "static data member %q#D of non-integral type", decl);
9519       else
9520 	error_at (DECL_SOURCE_LOCATION (decl),
9521 		  "in-class initialization of static data member %q#D of "
9522 		  "non-literal type", decl);
9523       return 1;
9524     }
9525 
9526   /* Motion 10 at San Diego: If a static const integral data member is
9527      initialized with an integral constant expression, the initializer
9528      may appear either in the declaration (within the class), or in
9529      the definition, but not both.  If it appears in the class, the
9530      member is a member constant.  The file-scope definition is always
9531      required.  */
9532   if (!ARITHMETIC_TYPE_P (type) && TREE_CODE (type) != ENUMERAL_TYPE)
9533     {
9534       error_at (DECL_SOURCE_LOCATION (decl),
9535 		"invalid in-class initialization of static data member "
9536 		"of non-integral type %qT",
9537 		type);
9538       return 1;
9539     }
9540   else if (!CP_TYPE_CONST_P (type))
9541     error_at (DECL_SOURCE_LOCATION (decl),
9542 	      "ISO C++ forbids in-class initialization of non-const "
9543 	      "static member %qD",
9544 	      decl);
9545   else if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type))
9546     pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
9547 	     "ISO C++ forbids initialization of member constant "
9548 	     "%qD of non-integral type %qT", decl, type);
9549 
9550   return 0;
9551 }
9552 
9553 /* *expr_p is part of the TYPE_SIZE of a variably-sized array.  If any
9554    SAVE_EXPRs in *expr_p wrap expressions with side-effects, break those
9555    expressions out into temporary variables so that walk_tree doesn't
9556    step into them (c++/15764).  */
9557 
9558 static tree
stabilize_save_expr_r(tree * expr_p,int * walk_subtrees,void * data)9559 stabilize_save_expr_r (tree *expr_p, int *walk_subtrees, void *data)
9560 {
9561   hash_set<tree> *pset = (hash_set<tree> *)data;
9562   tree expr = *expr_p;
9563   if (TREE_CODE (expr) == SAVE_EXPR)
9564     {
9565       tree op = TREE_OPERAND (expr, 0);
9566       cp_walk_tree (&op, stabilize_save_expr_r, data, pset);
9567       if (TREE_SIDE_EFFECTS (op))
9568 	TREE_OPERAND (expr, 0) = get_temp_regvar (TREE_TYPE (op), op);
9569       *walk_subtrees = 0;
9570     }
9571   else if (!EXPR_P (expr) || !TREE_SIDE_EFFECTS (expr))
9572     *walk_subtrees = 0;
9573   return NULL;
9574 }
9575 
9576 /* Entry point for the above.  */
9577 
9578 static void
stabilize_vla_size(tree size)9579 stabilize_vla_size (tree size)
9580 {
9581   hash_set<tree> pset;
9582   /* Break out any function calls into temporary variables.  */
9583   cp_walk_tree (&size, stabilize_save_expr_r, &pset, &pset);
9584 }
9585 
9586 /* Reduce a SIZEOF_EXPR to its value.  */
9587 
9588 tree
fold_sizeof_expr(tree t)9589 fold_sizeof_expr (tree t)
9590 {
9591   tree r;
9592   if (SIZEOF_EXPR_TYPE_P (t))
9593     r = cxx_sizeof_or_alignof_type (TREE_TYPE (TREE_OPERAND (t, 0)),
9594 				    SIZEOF_EXPR, false, false);
9595   else if (TYPE_P (TREE_OPERAND (t, 0)))
9596     r = cxx_sizeof_or_alignof_type (TREE_OPERAND (t, 0), SIZEOF_EXPR,
9597 				    false, false);
9598   else
9599     r = cxx_sizeof_or_alignof_expr (TREE_OPERAND (t, 0), SIZEOF_EXPR,
9600 				    false);
9601   if (r == error_mark_node)
9602     r = size_one_node;
9603   return r;
9604 }
9605 
9606 /* Given the SIZE (i.e., number of elements) in an array, compute
9607    an appropriate index type for the array.  If non-NULL, NAME is
9608    the name of the entity being declared.  */
9609 
9610 tree
compute_array_index_type(tree name,tree size,tsubst_flags_t complain)9611 compute_array_index_type (tree name, tree size, tsubst_flags_t complain)
9612 {
9613   tree itype;
9614   tree osize = size;
9615 
9616   if (error_operand_p (size))
9617     return error_mark_node;
9618 
9619   if (!type_dependent_expression_p (size))
9620     {
9621       osize = size = mark_rvalue_use (size);
9622 
9623       if (cxx_dialect < cxx11 && TREE_CODE (size) == NOP_EXPR
9624 	  && TREE_SIDE_EFFECTS (size))
9625 	/* In C++98, we mark a non-constant array bound with a magic
9626 	   NOP_EXPR with TREE_SIDE_EFFECTS; don't fold in that case.  */;
9627       else
9628 	{
9629 	  size = instantiate_non_dependent_expr_sfinae (size, complain);
9630 	  size = build_converted_constant_expr (size_type_node, size, complain);
9631 	  size = maybe_constant_value (size);
9632 
9633 	  if (!TREE_CONSTANT (size))
9634 	    size = osize;
9635 	}
9636 
9637       if (error_operand_p (size))
9638 	return error_mark_node;
9639 
9640       /* The array bound must be an integer type.  */
9641       tree type = TREE_TYPE (size);
9642       if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
9643 	{
9644 	  if (!(complain & tf_error))
9645 	    return error_mark_node;
9646 	  if (name)
9647 	    error ("size of array %qD has non-integral type %qT", name, type);
9648 	  else
9649 	    error ("size of array has non-integral type %qT", type);
9650 	  size = integer_one_node;
9651 	}
9652     }
9653 
9654   /* A type is dependent if it is...an array type constructed from any
9655      dependent type or whose size is specified by a constant expression
9656      that is value-dependent.  */
9657   /* We can only call value_dependent_expression_p on integral constant
9658      expressions; treat non-constant expressions as dependent, too.  */
9659   if (processing_template_decl
9660       && (type_dependent_expression_p (size)
9661 	  || !TREE_CONSTANT (size) || value_dependent_expression_p (size)))
9662     {
9663       /* We cannot do any checking for a SIZE that isn't known to be
9664 	 constant. Just build the index type and mark that it requires
9665 	 structural equality checks.  */
9666       itype = build_index_type (build_min (MINUS_EXPR, sizetype,
9667 					   size, size_one_node));
9668       TYPE_DEPENDENT_P (itype) = 1;
9669       TYPE_DEPENDENT_P_VALID (itype) = 1;
9670       SET_TYPE_STRUCTURAL_EQUALITY (itype);
9671       return itype;
9672     }
9673 
9674   if (TREE_CODE (size) != INTEGER_CST)
9675     {
9676       tree folded = cp_fully_fold (size);
9677       if (TREE_CODE (folded) == INTEGER_CST)
9678 	pedwarn (location_of (size), OPT_Wpedantic,
9679 		 "size of array is not an integral constant-expression");
9680       /* Use the folded result for VLAs, too; it will have resolved
9681 	 SIZEOF_EXPR.  */
9682       size = folded;
9683     }
9684 
9685   /* Normally, the array-bound will be a constant.  */
9686   if (TREE_CODE (size) == INTEGER_CST)
9687     {
9688       /* An array must have a positive number of elements.  */
9689       if (!valid_constant_size_p (size))
9690 	{
9691 	  if (!(complain & tf_error))
9692 	    return error_mark_node;
9693 
9694 	  if (name)
9695 	    error ("size of array %qD is negative", name);
9696 	  else
9697 	    error ("size of array is negative");
9698 	  size = integer_one_node;
9699 	}
9700       /* As an extension we allow zero-sized arrays.  */
9701       else if (integer_zerop (size))
9702 	{
9703 	  if (!(complain & tf_error))
9704 	    /* We must fail if performing argument deduction (as
9705 	       indicated by the state of complain), so that
9706 	       another substitution can be found.  */
9707 	    return error_mark_node;
9708 	  else if (in_system_header_at (input_location))
9709 	    /* Allow them in system headers because glibc uses them.  */;
9710 	  else if (name)
9711 	    pedwarn (input_location, OPT_Wpedantic, "ISO C++ forbids zero-size array %qD", name);
9712 	  else
9713 	    pedwarn (input_location, OPT_Wpedantic, "ISO C++ forbids zero-size array");
9714 	}
9715     }
9716   else if (TREE_CONSTANT (size)
9717 	   /* We don't allow VLAs at non-function scopes, or during
9718 	      tentative template substitution.  */
9719 	   || !at_function_scope_p ()
9720 	   || !(complain & tf_error))
9721     {
9722       if (!(complain & tf_error))
9723 	return error_mark_node;
9724       /* `(int) &fn' is not a valid array bound.  */
9725       if (name)
9726 	error ("size of array %qD is not an integral constant-expression",
9727 	       name);
9728       else
9729 	error ("size of array is not an integral constant-expression");
9730       size = integer_one_node;
9731     }
9732   else if (pedantic && warn_vla != 0)
9733     {
9734       if (name)
9735 	pedwarn (input_location, OPT_Wvla, "ISO C++ forbids variable length array %qD", name);
9736       else
9737 	pedwarn (input_location, OPT_Wvla, "ISO C++ forbids variable length array");
9738     }
9739   else if (warn_vla > 0)
9740     {
9741       if (name)
9742 	warning (OPT_Wvla,
9743                  "variable length array %qD is used", name);
9744       else
9745 	warning (OPT_Wvla,
9746                  "variable length array is used");
9747     }
9748 
9749   if (processing_template_decl && !TREE_CONSTANT (size))
9750     /* A variable sized array.  */
9751     itype = build_min (MINUS_EXPR, sizetype, size, integer_one_node);
9752   else
9753     {
9754       /* Compute the index of the largest element in the array.  It is
9755 	 one less than the number of elements in the array.  We save
9756 	 and restore PROCESSING_TEMPLATE_DECL so that computations in
9757 	 cp_build_binary_op will be appropriately folded.  */
9758       {
9759 	processing_template_decl_sentinel s;
9760 	itype = cp_build_binary_op (input_location,
9761 				    MINUS_EXPR,
9762 				    cp_convert (ssizetype, size, complain),
9763 				    cp_convert (ssizetype, integer_one_node,
9764 						complain),
9765 				    complain);
9766 	itype = maybe_constant_value (itype);
9767       }
9768 
9769       if (!TREE_CONSTANT (itype))
9770 	{
9771 	  /* A variable sized array.  */
9772 	  itype = variable_size (itype);
9773 
9774 	  stabilize_vla_size (itype);
9775 
9776 	  if (sanitize_flags_p (SANITIZE_VLA)
9777 	      && current_function_decl != NULL_TREE)
9778 	    {
9779 	      /* We have to add 1 -- in the ubsan routine we generate
9780 		 LE_EXPR rather than LT_EXPR.  */
9781 	      tree t = fold_build2 (PLUS_EXPR, TREE_TYPE (itype), itype,
9782 				    build_one_cst (TREE_TYPE (itype)));
9783 	      t = ubsan_instrument_vla (input_location, t);
9784 	      finish_expr_stmt (t);
9785 	    }
9786 	}
9787       /* Make sure that there was no overflow when creating to a signed
9788 	 index type.  (For example, on a 32-bit machine, an array with
9789 	 size 2^32 - 1 is too big.)  */
9790       else if (TREE_CODE (itype) == INTEGER_CST
9791 	       && TREE_OVERFLOW (itype))
9792 	{
9793 	  if (!(complain & tf_error))
9794 	    return error_mark_node;
9795 	  error ("overflow in array dimension");
9796 	  TREE_OVERFLOW (itype) = 0;
9797 	}
9798     }
9799 
9800   /* Create and return the appropriate index type.  */
9801   itype = build_index_type (itype);
9802 
9803   /* If the index type were dependent, we would have returned early, so
9804      remember that it isn't.  */
9805   TYPE_DEPENDENT_P (itype) = 0;
9806   TYPE_DEPENDENT_P_VALID (itype) = 1;
9807   return itype;
9808 }
9809 
9810 /* Returns the scope (if any) in which the entity declared by
9811    DECLARATOR will be located.  If the entity was declared with an
9812    unqualified name, NULL_TREE is returned.  */
9813 
9814 tree
get_scope_of_declarator(const cp_declarator * declarator)9815 get_scope_of_declarator (const cp_declarator *declarator)
9816 {
9817   while (declarator && declarator->kind != cdk_id)
9818     declarator = declarator->declarator;
9819 
9820   /* If the declarator-id is a SCOPE_REF, the scope in which the
9821      declaration occurs is the first operand.  */
9822   if (declarator
9823       && declarator->u.id.qualifying_scope)
9824     return declarator->u.id.qualifying_scope;
9825 
9826   /* Otherwise, the declarator is not a qualified name; the entity will
9827      be declared in the current scope.  */
9828   return NULL_TREE;
9829 }
9830 
9831 /* Returns an ARRAY_TYPE for an array with SIZE elements of the
9832    indicated TYPE.  If non-NULL, NAME is the NAME of the declaration
9833    with this type.  */
9834 
9835 static tree
create_array_type_for_decl(tree name,tree type,tree size)9836 create_array_type_for_decl (tree name, tree type, tree size)
9837 {
9838   tree itype = NULL_TREE;
9839 
9840   /* If things have already gone awry, bail now.  */
9841   if (type == error_mark_node || size == error_mark_node)
9842     return error_mark_node;
9843 
9844   /* 8.3.4/1: If the type of the identifier of D contains the auto
9845      type-specifier, the program is ill-formed.  */
9846   if (type_uses_auto (type))
9847     {
9848       error ("%qD declared as array of %qT", name, type);
9849       return error_mark_node;
9850     }
9851 
9852   /* If there are some types which cannot be array elements,
9853      issue an error-message and return.  */
9854   switch (TREE_CODE (type))
9855     {
9856     case VOID_TYPE:
9857       if (name)
9858         error ("declaration of %qD as array of void", name);
9859       else
9860         error ("creating array of void");
9861       return error_mark_node;
9862 
9863     case FUNCTION_TYPE:
9864       if (name)
9865         error ("declaration of %qD as array of functions", name);
9866       else
9867         error ("creating array of functions");
9868       return error_mark_node;
9869 
9870     case REFERENCE_TYPE:
9871       if (name)
9872         error ("declaration of %qD as array of references", name);
9873       else
9874         error ("creating array of references");
9875       return error_mark_node;
9876 
9877     case METHOD_TYPE:
9878       if (name)
9879         error ("declaration of %qD as array of function members", name);
9880       else
9881         error ("creating array of function members");
9882       return error_mark_node;
9883 
9884     default:
9885       break;
9886     }
9887 
9888   /* [dcl.array]
9889 
9890      The constant expressions that specify the bounds of the arrays
9891      can be omitted only for the first member of the sequence.  */
9892   if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type))
9893     {
9894       if (name)
9895 	error ("declaration of %qD as multidimensional array must "
9896 	       "have bounds for all dimensions except the first",
9897 	       name);
9898       else
9899 	error ("multidimensional array must have bounds for all "
9900 	       "dimensions except the first");
9901 
9902       return error_mark_node;
9903     }
9904 
9905   /* Figure out the index type for the array.  */
9906   if (size)
9907     itype = compute_array_index_type (name, size, tf_warning_or_error);
9908 
9909   /* [dcl.array]
9910      T is called the array element type; this type shall not be [...] an
9911      abstract class type.  */
9912   abstract_virtuals_error (name, type);
9913 
9914   return build_cplus_array_type (type, itype);
9915 }
9916 
9917 /* Returns the smallest location != UNKNOWN_LOCATION among the
9918    three stored in LOCATIONS[ds_const], LOCATIONS[ds_volatile],
9919    and LOCATIONS[ds_restrict].  */
9920 
9921 static location_t
smallest_type_quals_location(int type_quals,const location_t * locations)9922 smallest_type_quals_location (int type_quals, const location_t* locations)
9923 {
9924   location_t loc = UNKNOWN_LOCATION;
9925 
9926   if (type_quals & TYPE_QUAL_CONST)
9927     loc = locations[ds_const];
9928 
9929   if ((type_quals & TYPE_QUAL_VOLATILE)
9930       && (loc == UNKNOWN_LOCATION || locations[ds_volatile] < loc))
9931     loc = locations[ds_volatile];
9932 
9933   if ((type_quals & TYPE_QUAL_RESTRICT)
9934       && (loc == UNKNOWN_LOCATION || locations[ds_restrict] < loc))
9935     loc = locations[ds_restrict];
9936 
9937   return loc;
9938 }
9939 
9940 /* Check that it's OK to declare a function with the indicated TYPE
9941    and TYPE_QUALS.  SFK indicates the kind of special function (if any)
9942    that this function is.  OPTYPE is the type given in a conversion
9943    operator declaration, or the class type for a constructor/destructor.
9944    Returns the actual return type of the function; that may be different
9945    than TYPE if an error occurs, or for certain special functions.  */
9946 
9947 static tree
check_special_function_return_type(special_function_kind sfk,tree type,tree optype,int type_quals,const location_t * locations)9948 check_special_function_return_type (special_function_kind sfk,
9949 				    tree type,
9950 				    tree optype,
9951 				    int type_quals,
9952 				    const location_t* locations)
9953 {
9954   switch (sfk)
9955     {
9956     case sfk_constructor:
9957       if (type)
9958 	error ("return type specification for constructor invalid");
9959       else if (type_quals != TYPE_UNQUALIFIED)
9960 	error_at (smallest_type_quals_location (type_quals, locations),
9961 		  "qualifiers are not allowed on constructor declaration");
9962 
9963       if (targetm.cxx.cdtor_returns_this ())
9964 	type = build_pointer_type (optype);
9965       else
9966 	type = void_type_node;
9967       break;
9968 
9969     case sfk_destructor:
9970       if (type)
9971 	error ("return type specification for destructor invalid");
9972       else if (type_quals != TYPE_UNQUALIFIED)
9973 	error_at (smallest_type_quals_location (type_quals, locations),
9974 		  "qualifiers are not allowed on destructor declaration");
9975 
9976       /* We can't use the proper return type here because we run into
9977 	 problems with ambiguous bases and covariant returns.  */
9978       if (targetm.cxx.cdtor_returns_this ())
9979 	type = build_pointer_type (void_type_node);
9980       else
9981 	type = void_type_node;
9982       break;
9983 
9984     case sfk_conversion:
9985       if (type)
9986 	error ("return type specified for %<operator %T%>", optype);
9987       else if (type_quals != TYPE_UNQUALIFIED)
9988 	error_at (smallest_type_quals_location (type_quals, locations),
9989 		  "qualifiers are not allowed on declaration of "
9990 		  "%<operator %T%>", optype);
9991 
9992       type = optype;
9993       break;
9994 
9995     case sfk_deduction_guide:
9996       if (type)
9997 	error ("return type specified for deduction guide");
9998       else if (type_quals != TYPE_UNQUALIFIED)
9999 	error_at (smallest_type_quals_location (type_quals, locations),
10000 		  "qualifiers are not allowed on declaration of "
10001 		  "deduction guide");
10002       if (TREE_CODE (optype) == TEMPLATE_TEMPLATE_PARM)
10003 	{
10004 	  error ("template template parameter %qT in declaration of "
10005 		 "deduction guide", optype);
10006 	  type = error_mark_node;
10007 	}
10008       else
10009 	type = make_template_placeholder (CLASSTYPE_TI_TEMPLATE (optype));
10010       for (int i = 0; i < ds_last; ++i)
10011 	if (i != ds_explicit && locations[i])
10012 	  error_at (locations[i],
10013 		    "decl-specifier in declaration of deduction guide");
10014       break;
10015 
10016     default:
10017       gcc_unreachable ();
10018     }
10019 
10020   return type;
10021 }
10022 
10023 /* A variable or data member (whose unqualified name is IDENTIFIER)
10024    has been declared with the indicated TYPE.  If the TYPE is not
10025    acceptable, issue an error message and return a type to use for
10026    error-recovery purposes.  */
10027 
10028 tree
check_var_type(tree identifier,tree type)10029 check_var_type (tree identifier, tree type)
10030 {
10031   if (VOID_TYPE_P (type))
10032     {
10033       if (!identifier)
10034 	error ("unnamed variable or field declared void");
10035       else if (identifier_p (identifier))
10036 	{
10037 	  gcc_assert (!IDENTIFIER_ANY_OP_P (identifier));
10038 	  error ("variable or field %qE declared void", identifier);
10039 	}
10040       else
10041 	error ("variable or field declared void");
10042       type = error_mark_node;
10043     }
10044 
10045   return type;
10046 }
10047 
10048 /* Handle declaring DECL as an inline variable.  */
10049 
10050 static void
mark_inline_variable(tree decl)10051 mark_inline_variable (tree decl)
10052 {
10053   bool inlinep = true;
10054   if (! toplevel_bindings_p ())
10055     {
10056       error ("%<inline%> specifier invalid for variable "
10057 	     "%qD declared at block scope", decl);
10058       inlinep = false;
10059     }
10060   else if (cxx_dialect < cxx17)
10061     pedwarn (DECL_SOURCE_LOCATION (decl), 0,
10062 	     "inline variables are only available "
10063 	     "with -std=c++17 or -std=gnu++17");
10064   if (inlinep)
10065     {
10066       retrofit_lang_decl (decl);
10067       SET_DECL_VAR_DECLARED_INLINE_P (decl);
10068     }
10069 }
10070 
10071 
10072 /* Assign a typedef-given name to a class or enumeration type declared
10073    as anonymous at first.  This was split out of grokdeclarator
10074    because it is also used in libcc1.  */
10075 
10076 void
name_unnamed_type(tree type,tree decl)10077 name_unnamed_type (tree type, tree decl)
10078 {
10079   gcc_assert (TYPE_UNNAMED_P (type));
10080 
10081   /* Replace the anonymous name with the real name everywhere.  */
10082   for (tree t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
10083     {
10084       if (anon_aggrname_p (TYPE_IDENTIFIER (t)))
10085 	/* We do not rename the debug info representing the
10086 	   unnamed tagged type because the standard says in
10087 	   [dcl.typedef] that the naming applies only for
10088 	   linkage purposes.  */
10089 	/*debug_hooks->set_name (t, decl);*/
10090 	TYPE_NAME (t) = decl;
10091     }
10092 
10093   if (TYPE_LANG_SPECIFIC (type))
10094     TYPE_WAS_UNNAMED (type) = 1;
10095 
10096   /* If this is a typedef within a template class, the nested
10097      type is a (non-primary) template.  The name for the
10098      template needs updating as well.  */
10099   if (TYPE_LANG_SPECIFIC (type) && CLASSTYPE_TEMPLATE_INFO (type))
10100     DECL_NAME (CLASSTYPE_TI_TEMPLATE (type))
10101       = TYPE_IDENTIFIER (type);
10102 
10103   /* Adjust linkage now that we aren't unnamed anymore.  */
10104   reset_type_linkage (type);
10105 
10106   /* FIXME remangle member functions; member functions of a
10107      type with external linkage have external linkage.  */
10108 
10109   /* Check that our job is done, and that it would fail if we
10110      attempted to do it again.  */
10111   gcc_assert (!TYPE_UNNAMED_P (type));
10112 }
10113 
10114 /* Given declspecs and a declarator (abstract or otherwise), determine
10115    the name and type of the object declared and construct a DECL node
10116    for it.
10117 
10118    DECLSPECS points to the representation of declaration-specifier
10119    sequence that precedes declarator.
10120 
10121    DECL_CONTEXT says which syntactic context this declaration is in:
10122      NORMAL for most contexts.  Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
10123      FUNCDEF for a function definition.  Like NORMAL but a few different
10124       error messages in each case.  Return value may be zero meaning
10125       this definition is too screwy to try to parse.
10126      MEMFUNCDEF for a function definition.  Like FUNCDEF but prepares to
10127       handle member functions (which have FIELD context).
10128       Return value may be zero meaning this definition is too screwy to
10129       try to parse.
10130      PARM for a parameter declaration (either within a function prototype
10131       or before a function body).  Make a PARM_DECL, or return void_type_node.
10132      TPARM for a template parameter declaration.
10133      CATCHPARM for a parameter declaration before a catch clause.
10134      TYPENAME if for a typename (in a cast or sizeof).
10135       Don't make a DECL node; just return the ..._TYPE node.
10136      FIELD for a struct or union field; make a FIELD_DECL.
10137      BITFIELD for a field with specified width.
10138 
10139    INITIALIZED is as for start_decl.
10140 
10141    ATTRLIST is a pointer to the list of attributes, which may be NULL
10142    if there are none; *ATTRLIST may be modified if attributes from inside
10143    the declarator should be applied to the declaration.
10144 
10145    When this function is called, scoping variables (such as
10146    CURRENT_CLASS_TYPE) should reflect the scope in which the
10147    declaration occurs, not the scope in which the new declaration will
10148    be placed.  For example, on:
10149 
10150      void S::f() { ... }
10151 
10152    when grokdeclarator is called for `S::f', the CURRENT_CLASS_TYPE
10153    should not be `S'.
10154 
10155    Returns a DECL (if a declarator is present), a TYPE (if there is no
10156    declarator, in cases like "struct S;"), or the ERROR_MARK_NODE if an
10157    error occurs. */
10158 
10159 tree
grokdeclarator(const cp_declarator * declarator,cp_decl_specifier_seq * declspecs,enum decl_context decl_context,int initialized,tree * attrlist)10160 grokdeclarator (const cp_declarator *declarator,
10161 		cp_decl_specifier_seq *declspecs,
10162 		enum decl_context decl_context,
10163 		int initialized,
10164 		tree* attrlist)
10165 {
10166   tree type = NULL_TREE;
10167   int longlong = 0;
10168   int explicit_intN = 0;
10169   int virtualp, explicitp, friendp, inlinep, staticp;
10170   int explicit_int = 0;
10171   int explicit_char = 0;
10172   int defaulted_int = 0;
10173 
10174   tree typedef_decl = NULL_TREE;
10175   const char *name = NULL;
10176   tree typedef_type = NULL_TREE;
10177   /* True if this declarator is a function definition.  */
10178   bool funcdef_flag = false;
10179   cp_declarator_kind innermost_code = cdk_error;
10180   int bitfield = 0;
10181 #if 0
10182   /* See the code below that used this.  */
10183   tree decl_attr = NULL_TREE;
10184 #endif
10185 
10186   /* Keep track of what sort of function is being processed
10187      so that we can warn about default return values, or explicit
10188      return values which do not match prescribed defaults.  */
10189   special_function_kind sfk = sfk_none;
10190 
10191   tree dname = NULL_TREE;
10192   tree ctor_return_type = NULL_TREE;
10193   enum overload_flags flags = NO_SPECIAL;
10194   /* cv-qualifiers that apply to the declarator, for a declaration of
10195      a member function.  */
10196   cp_cv_quals memfn_quals = TYPE_UNQUALIFIED;
10197   /* virt-specifiers that apply to the declarator, for a declaration of
10198      a member function.  */
10199   cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
10200   /* ref-qualifier that applies to the declarator, for a declaration of
10201      a member function.  */
10202   cp_ref_qualifier rqual = REF_QUAL_NONE;
10203   /* cv-qualifiers that apply to the type specified by the DECLSPECS.  */
10204   int type_quals = TYPE_UNQUALIFIED;
10205   tree raises = NULL_TREE;
10206   int template_count = 0;
10207   tree returned_attrs = NULL_TREE;
10208   tree parms = NULL_TREE;
10209   const cp_declarator *id_declarator;
10210   /* The unqualified name of the declarator; either an
10211      IDENTIFIER_NODE, BIT_NOT_EXPR, or TEMPLATE_ID_EXPR.  */
10212   tree unqualified_id;
10213   /* The class type, if any, in which this entity is located,
10214      or NULL_TREE if none.  Note that this value may be different from
10215      the current class type; for example if an attempt is made to declare
10216      "A::f" inside "B", this value will be "A".  */
10217   tree ctype = current_class_type;
10218   /* The NAMESPACE_DECL for the namespace in which this entity is
10219      located.  If an unqualified name is used to declare the entity,
10220      this value will be NULL_TREE, even if the entity is located at
10221      namespace scope.  */
10222   tree in_namespace = NULL_TREE;
10223   cp_storage_class storage_class;
10224   bool unsigned_p, signed_p, short_p, long_p, thread_p;
10225   bool type_was_error_mark_node = false;
10226   bool parameter_pack_p = declarator ? declarator->parameter_pack_p : false;
10227   bool template_type_arg = false;
10228   bool template_parm_flag = false;
10229   bool typedef_p = decl_spec_seq_has_spec_p (declspecs, ds_typedef);
10230   bool constexpr_p = decl_spec_seq_has_spec_p (declspecs, ds_constexpr);
10231   bool late_return_type_p = false;
10232   bool array_parameter_p = false;
10233   source_location saved_loc = input_location;
10234   tree reqs = NULL_TREE;
10235 
10236   signed_p = decl_spec_seq_has_spec_p (declspecs, ds_signed);
10237   unsigned_p = decl_spec_seq_has_spec_p (declspecs, ds_unsigned);
10238   short_p = decl_spec_seq_has_spec_p (declspecs, ds_short);
10239   long_p = decl_spec_seq_has_spec_p (declspecs, ds_long);
10240   longlong = decl_spec_seq_has_spec_p (declspecs, ds_long_long);
10241   explicit_intN = declspecs->explicit_intN_p;
10242   thread_p = decl_spec_seq_has_spec_p (declspecs, ds_thread);
10243 
10244   // Was concept_p specified? Note that ds_concept
10245   // implies ds_constexpr!
10246   bool concept_p = decl_spec_seq_has_spec_p (declspecs, ds_concept);
10247   if (concept_p)
10248     constexpr_p = true;
10249 
10250   if (decl_spec_seq_has_spec_p (declspecs, ds_const))
10251     type_quals |= TYPE_QUAL_CONST;
10252   if (decl_spec_seq_has_spec_p (declspecs, ds_volatile))
10253     type_quals |= TYPE_QUAL_VOLATILE;
10254   if (decl_spec_seq_has_spec_p (declspecs, ds_restrict))
10255     type_quals |= TYPE_QUAL_RESTRICT;
10256 
10257   if (decl_context == FUNCDEF)
10258     funcdef_flag = true, decl_context = NORMAL;
10259   else if (decl_context == MEMFUNCDEF)
10260     funcdef_flag = true, decl_context = FIELD;
10261   else if (decl_context == BITFIELD)
10262     bitfield = 1, decl_context = FIELD;
10263   else if (decl_context == TEMPLATE_TYPE_ARG)
10264     template_type_arg = true, decl_context = TYPENAME;
10265   else if (decl_context == TPARM)
10266     template_parm_flag = true, decl_context = PARM;
10267 
10268   if (initialized > 1)
10269     funcdef_flag = true;
10270 
10271   location_t typespec_loc = smallest_type_quals_location (type_quals,
10272 						      declspecs->locations);
10273   if (typespec_loc == UNKNOWN_LOCATION)
10274     typespec_loc = declspecs->locations[ds_type_spec];
10275   if (typespec_loc == UNKNOWN_LOCATION)
10276     typespec_loc = input_location;
10277 
10278   /* Look inside a declarator for the name being declared
10279      and get it as a string, for an error message.  */
10280   for (id_declarator = declarator;
10281        id_declarator;
10282        id_declarator = id_declarator->declarator)
10283     {
10284       if (id_declarator->kind != cdk_id)
10285 	innermost_code = id_declarator->kind;
10286 
10287       switch (id_declarator->kind)
10288 	{
10289 	case cdk_function:
10290 	  if (id_declarator->declarator
10291 	      && id_declarator->declarator->kind == cdk_id)
10292 	    {
10293 	      sfk = id_declarator->declarator->u.id.sfk;
10294 	      if (sfk == sfk_destructor)
10295 		flags = DTOR_FLAG;
10296 	    }
10297 	  break;
10298 
10299 	case cdk_id:
10300 	  {
10301 	    tree qualifying_scope = id_declarator->u.id.qualifying_scope;
10302 	    tree decl = id_declarator->u.id.unqualified_name;
10303 	    if (!decl)
10304 	      break;
10305 	    if (qualifying_scope)
10306 	      {
10307 		if (check_for_bare_parameter_packs (qualifying_scope,
10308 						    id_declarator->id_loc))
10309 		  return error_mark_node;
10310 		if (at_function_scope_p ())
10311 		  {
10312 		    /* [dcl.meaning]
10313 
10314 		       A declarator-id shall not be qualified except
10315 		       for ...
10316 
10317 		       None of the cases are permitted in block
10318 		       scope.  */
10319 		    if (qualifying_scope == global_namespace)
10320 		      error ("invalid use of qualified-name %<::%D%>",
10321 			     decl);
10322 		    else if (TYPE_P (qualifying_scope))
10323 		      error ("invalid use of qualified-name %<%T::%D%>",
10324 			     qualifying_scope, decl);
10325 		    else
10326 		      error ("invalid use of qualified-name %<%D::%D%>",
10327 			     qualifying_scope, decl);
10328 		    return error_mark_node;
10329 		  }
10330 		else if (TYPE_P (qualifying_scope))
10331 		  {
10332 		    ctype = qualifying_scope;
10333 		    if (!MAYBE_CLASS_TYPE_P (ctype))
10334 		      {
10335 			error ("%q#T is not a class or a namespace", ctype);
10336 			ctype = NULL_TREE;
10337 		      }
10338 		    else if (innermost_code != cdk_function
10339 			     && current_class_type
10340 			     && !uniquely_derived_from_p (ctype,
10341 							  current_class_type))
10342 		      {
10343 			error ("invalid use of qualified-name %<%T::%D%>",
10344 			       qualifying_scope, decl);
10345 			return error_mark_node;
10346 		      }
10347 		  }
10348 		else if (TREE_CODE (qualifying_scope) == NAMESPACE_DECL)
10349 		  in_namespace = qualifying_scope;
10350 	      }
10351 	    switch (TREE_CODE (decl))
10352 	      {
10353 	      case BIT_NOT_EXPR:
10354 		{
10355 		  if (innermost_code != cdk_function)
10356 		    {
10357 		      error ("declaration of %qD as non-function", decl);
10358 		      return error_mark_node;
10359 		    }
10360 		  else if (!qualifying_scope
10361 			   && !(current_class_type && at_class_scope_p ()))
10362 		    {
10363 		      error ("declaration of %qD as non-member", decl);
10364 		      return error_mark_node;
10365 		    }
10366 
10367 		  tree type = TREE_OPERAND (decl, 0);
10368 		  if (TYPE_P (type))
10369 		    type = constructor_name (type);
10370 		  name = identifier_to_locale (IDENTIFIER_POINTER (type));
10371 		  dname = decl;
10372 		}
10373 		break;
10374 
10375 	      case TEMPLATE_ID_EXPR:
10376 		{
10377 		  tree fns = TREE_OPERAND (decl, 0);
10378 
10379 		  dname = fns;
10380 		  if (!identifier_p (dname))
10381 		    dname = OVL_NAME (dname);
10382 		}
10383 		/* Fall through.  */
10384 
10385 	      case IDENTIFIER_NODE:
10386 		if (identifier_p (decl))
10387 		  dname = decl;
10388 
10389 		if (IDENTIFIER_KEYWORD_P (dname))
10390 		  {
10391 		    error ("declarator-id missing; using reserved word %qD",
10392 			   dname);
10393 		    name = identifier_to_locale (IDENTIFIER_POINTER (dname));
10394 		  }
10395 		else if (!IDENTIFIER_CONV_OP_P (dname))
10396 		  name = identifier_to_locale (IDENTIFIER_POINTER (dname));
10397 		else
10398 		  {
10399 		    gcc_assert (flags == NO_SPECIAL);
10400 		    flags = TYPENAME_FLAG;
10401 		    sfk = sfk_conversion;
10402 		    tree glob = get_global_binding (dname);
10403 		    if (glob && TREE_CODE (glob) == TYPE_DECL)
10404 		      name = identifier_to_locale (IDENTIFIER_POINTER (dname));
10405 		    else
10406 		      name = "<invalid operator>";
10407 		  }
10408 		break;
10409 
10410 	      default:
10411 		gcc_unreachable ();
10412 	      }
10413 	    break;
10414 	  }
10415 
10416 	case cdk_array:
10417 	case cdk_pointer:
10418 	case cdk_reference:
10419 	case cdk_ptrmem:
10420 	  break;
10421 
10422 	case cdk_decomp:
10423 	  name = "structured binding";
10424 	  break;
10425 
10426 	case cdk_error:
10427 	  return error_mark_node;
10428 
10429 	default:
10430 	  gcc_unreachable ();
10431 	}
10432       if (id_declarator->kind == cdk_id)
10433 	break;
10434     }
10435 
10436   /* [dcl.fct.edf]
10437 
10438      The declarator in a function-definition shall have the form
10439      D1 ( parameter-declaration-clause) ...  */
10440   if (funcdef_flag && innermost_code != cdk_function)
10441     {
10442       error ("function definition does not declare parameters");
10443       return error_mark_node;
10444     }
10445 
10446   if (flags == TYPENAME_FLAG
10447       && innermost_code != cdk_function
10448       && ! (ctype && !declspecs->any_specifiers_p))
10449     {
10450       error ("declaration of %qD as non-function", dname);
10451       return error_mark_node;
10452     }
10453 
10454   if (dname && identifier_p (dname))
10455     {
10456       if (UDLIT_OPER_P (dname)
10457 	  && innermost_code != cdk_function)
10458 	{
10459 	  error ("declaration of %qD as non-function", dname);
10460 	  return error_mark_node;
10461 	}
10462 
10463       if (IDENTIFIER_ANY_OP_P (dname))
10464 	{
10465 	  if (typedef_p)
10466 	    {
10467 	      error ("declaration of %qD as %<typedef%>", dname);
10468 	      return error_mark_node;
10469 	    }
10470 	  else if (decl_context == PARM || decl_context == CATCHPARM)
10471 	    {
10472 	      error ("declaration of %qD as parameter", dname);
10473 	      return error_mark_node;
10474 	    }
10475 	}
10476     }
10477 
10478   /* Anything declared one level down from the top level
10479      must be one of the parameters of a function
10480      (because the body is at least two levels down).  */
10481 
10482   /* This heuristic cannot be applied to C++ nodes! Fixed, however,
10483      by not allowing C++ class definitions to specify their parameters
10484      with xdecls (must be spec.d in the parmlist).
10485 
10486      Since we now wait to push a class scope until we are sure that
10487      we are in a legitimate method context, we must set oldcname
10488      explicitly (since current_class_name is not yet alive).
10489 
10490      We also want to avoid calling this a PARM if it is in a namespace.  */
10491 
10492   if (decl_context == NORMAL && !toplevel_bindings_p ())
10493     {
10494       cp_binding_level *b = current_binding_level;
10495       current_binding_level = b->level_chain;
10496       if (current_binding_level != 0 && toplevel_bindings_p ())
10497 	decl_context = PARM;
10498       current_binding_level = b;
10499     }
10500 
10501   if (name == NULL)
10502     name = decl_context == PARM ? "parameter" : "type name";
10503 
10504   if (concept_p && typedef_p)
10505     {
10506       error ("%<concept%> cannot appear in a typedef declaration");
10507       return error_mark_node;
10508     }
10509 
10510   if (constexpr_p && typedef_p)
10511     {
10512       error ("%<constexpr%> cannot appear in a typedef declaration");
10513       return error_mark_node;
10514     }
10515 
10516   /* If there were multiple types specified in the decl-specifier-seq,
10517      issue an error message.  */
10518   if (declspecs->multiple_types_p)
10519     {
10520       error ("two or more data types in declaration of %qs", name);
10521       return error_mark_node;
10522     }
10523 
10524   if (declspecs->conflicting_specifiers_p)
10525     {
10526       error ("conflicting specifiers in declaration of %qs", name);
10527       return error_mark_node;
10528     }
10529 
10530   /* Extract the basic type from the decl-specifier-seq.  */
10531   type = declspecs->type;
10532   if (type == error_mark_node)
10533     {
10534       type = NULL_TREE;
10535       type_was_error_mark_node = true;
10536     }
10537   /* If the entire declaration is itself tagged as deprecated then
10538      suppress reports of deprecated items.  */
10539   if (type && TREE_DEPRECATED (type)
10540       && deprecated_state != DEPRECATED_SUPPRESS)
10541     cp_warn_deprecated_use (type);
10542   if (type && TREE_CODE (type) == TYPE_DECL)
10543     {
10544       typedef_decl = type;
10545       type = TREE_TYPE (typedef_decl);
10546       if (TREE_DEPRECATED (type)
10547 	  && DECL_ARTIFICIAL (typedef_decl)
10548 	  && deprecated_state != DEPRECATED_SUPPRESS)
10549 	cp_warn_deprecated_use (type);
10550     }
10551   /* No type at all: default to `int', and set DEFAULTED_INT
10552      because it was not a user-defined typedef.  */
10553   if (type == NULL_TREE)
10554     {
10555       if (signed_p || unsigned_p || long_p || short_p)
10556 	{
10557 	  /* These imply 'int'.  */
10558 	  type = integer_type_node;
10559 	  defaulted_int = 1;
10560 	}
10561       /* If we just have "complex", it is equivalent to "complex double".  */
10562       else if (!longlong && !explicit_intN
10563 	       && decl_spec_seq_has_spec_p (declspecs, ds_complex))
10564 	{
10565 	  type = double_type_node;
10566 	  pedwarn (declspecs->locations[ds_complex], OPT_Wpedantic,
10567 		   "ISO C++ does not support plain %<complex%> meaning "
10568 		   "%<double complex%>");
10569 	}
10570     }
10571   /* Gather flags.  */
10572   explicit_int = declspecs->explicit_int_p;
10573   explicit_char = declspecs->explicit_char_p;
10574 
10575 #if 0
10576   /* See the code below that used this.  */
10577   if (typedef_decl)
10578     decl_attr = DECL_ATTRIBUTES (typedef_decl);
10579 #endif
10580   typedef_type = type;
10581 
10582   if (sfk == sfk_conversion || sfk == sfk_deduction_guide)
10583     ctor_return_type = TREE_TYPE (dname);
10584   else
10585     ctor_return_type = ctype;
10586 
10587   if (sfk != sfk_none)
10588     {
10589       type = check_special_function_return_type (sfk, type,
10590 						 ctor_return_type,
10591 						 type_quals,
10592 						 declspecs->locations);
10593       type_quals = TYPE_UNQUALIFIED;
10594     }
10595   else if (type == NULL_TREE)
10596     {
10597       int is_main;
10598 
10599       explicit_int = -1;
10600 
10601       /* We handle `main' specially here, because 'main () { }' is so
10602 	 common.  With no options, it is allowed.  With -Wreturn-type,
10603 	 it is a warning.  It is only an error with -pedantic-errors.  */
10604       is_main = (funcdef_flag
10605 		 && dname && identifier_p (dname)
10606 		 && MAIN_NAME_P (dname)
10607 		 && ctype == NULL_TREE
10608 		 && in_namespace == NULL_TREE
10609 		 && current_namespace == global_namespace);
10610 
10611       if (type_was_error_mark_node)
10612 	/* We've already issued an error, don't complain more.  */;
10613       else if (in_system_header_at (input_location) || flag_ms_extensions)
10614 	/* Allow it, sigh.  */;
10615       else if (! is_main)
10616 	permerror (input_location, "ISO C++ forbids declaration of %qs with no type", name);
10617       else if (pedantic)
10618 	pedwarn (input_location, OPT_Wpedantic,
10619 		 "ISO C++ forbids declaration of %qs with no type", name);
10620       else
10621 	warning (OPT_Wreturn_type,
10622                  "ISO C++ forbids declaration of %qs with no type", name);
10623 
10624       if (type_was_error_mark_node && template_parm_flag)
10625 	/* FIXME we should be able to propagate the error_mark_node as is
10626 	   for other contexts too.  */
10627 	type = error_mark_node;
10628       else
10629 	type = integer_type_node;
10630     }
10631 
10632   ctype = NULL_TREE;
10633 
10634   if (explicit_intN)
10635     {
10636       if (! int_n_enabled_p[declspecs->int_n_idx])
10637 	{
10638 	  error ("%<__int%d%> is not supported by this target",
10639 		 int_n_data[declspecs->int_n_idx].bitsize);
10640 	  explicit_intN = false;
10641 	}
10642       else if (pedantic && ! in_system_header_at (input_location))
10643 	pedwarn (input_location, OPT_Wpedantic,
10644 		 "ISO C++ does not support %<__int%d%> for %qs",
10645 		 int_n_data[declspecs->int_n_idx].bitsize, name);
10646     }
10647 
10648   /* Now process the modifiers that were specified
10649      and check for invalid combinations.  */
10650 
10651   /* Long double is a special combination.  */
10652   if (long_p && !longlong && TYPE_MAIN_VARIANT (type) == double_type_node)
10653     {
10654       long_p = false;
10655       type = cp_build_qualified_type (long_double_type_node,
10656 				      cp_type_quals (type));
10657     }
10658 
10659   /* Check all other uses of type modifiers.  */
10660 
10661   if (unsigned_p || signed_p || long_p || short_p)
10662     {
10663       int ok = 0;
10664 
10665       if ((signed_p || unsigned_p) && TREE_CODE (type) != INTEGER_TYPE)
10666 	error ("%<signed%> or %<unsigned%> invalid for %qs", name);
10667       else if (signed_p && unsigned_p)
10668 	error ("%<signed%> and %<unsigned%> specified together for %qs", name);
10669       else if (longlong && TREE_CODE (type) != INTEGER_TYPE)
10670 	error ("%<long long%> invalid for %qs", name);
10671       else if (long_p && TREE_CODE (type) == REAL_TYPE)
10672 	error ("%<long%> invalid for %qs", name);
10673       else if (short_p && TREE_CODE (type) == REAL_TYPE)
10674 	error ("%<short%> invalid for %qs", name);
10675       else if ((long_p || short_p) && TREE_CODE (type) != INTEGER_TYPE)
10676 	error ("%<long%> or %<short%> invalid for %qs", name);
10677       else if ((long_p || short_p || explicit_char || explicit_int) && explicit_intN)
10678 	error ("%<long%>, %<int%>, %<short%>, or %<char%> invalid for %qs", name);
10679       else if ((long_p || short_p) && explicit_char)
10680 	error ("%<long%> or %<short%> specified with char for %qs", name);
10681       else if (long_p && short_p)
10682 	error ("%<long%> and %<short%> specified together for %qs", name);
10683       else if (type == char16_type_node || type == char32_type_node)
10684 	{
10685 	  if (signed_p || unsigned_p)
10686 	    error ("%<signed%> or %<unsigned%> invalid for %qs", name);
10687 	  else if (short_p || long_p)
10688 	    error ("%<short%> or %<long%> invalid for %qs", name);
10689 	}
10690       else
10691 	{
10692 	  ok = 1;
10693 	  if (!explicit_int && !defaulted_int && !explicit_char && !explicit_intN && pedantic)
10694 	    {
10695 	      pedwarn (input_location, OPT_Wpedantic,
10696 		       "long, short, signed or unsigned used invalidly for %qs",
10697 		       name);
10698 	      if (flag_pedantic_errors)
10699 		ok = 0;
10700 	    }
10701 	}
10702 
10703       /* Discard the type modifiers if they are invalid.  */
10704       if (! ok)
10705 	{
10706 	  unsigned_p = false;
10707 	  signed_p = false;
10708 	  long_p = false;
10709 	  short_p = false;
10710 	  longlong = 0;
10711 	}
10712     }
10713 
10714   /* Decide whether an integer type is signed or not.
10715      Optionally treat bitfields as signed by default.  */
10716   if (unsigned_p
10717       /* [class.bit]
10718 
10719 	 It is implementation-defined whether a plain (neither
10720 	 explicitly signed or unsigned) char, short, int, or long
10721 	 bit-field is signed or unsigned.
10722 
10723 	 Naturally, we extend this to long long as well.  Note that
10724 	 this does not include wchar_t.  */
10725       || (bitfield && !flag_signed_bitfields
10726 	  && !signed_p
10727 	  /* A typedef for plain `int' without `signed' can be
10728 	     controlled just like plain `int', but a typedef for
10729 	     `signed int' cannot be so controlled.  */
10730 	  && !(typedef_decl
10731 	       && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl))
10732 	  && TREE_CODE (type) == INTEGER_TYPE
10733 	  && !same_type_p (TYPE_MAIN_VARIANT (type), wchar_type_node)))
10734     {
10735       if (explicit_intN)
10736 	type = int_n_trees[declspecs->int_n_idx].unsigned_type;
10737       else if (longlong)
10738 	type = long_long_unsigned_type_node;
10739       else if (long_p)
10740 	type = long_unsigned_type_node;
10741       else if (short_p)
10742 	type = short_unsigned_type_node;
10743       else if (type == char_type_node)
10744 	type = unsigned_char_type_node;
10745       else if (typedef_decl)
10746 	type = unsigned_type_for (type);
10747       else
10748 	type = unsigned_type_node;
10749     }
10750   else if (signed_p && type == char_type_node)
10751     type = signed_char_type_node;
10752   else if (explicit_intN)
10753     type = int_n_trees[declspecs->int_n_idx].signed_type;
10754   else if (longlong)
10755     type = long_long_integer_type_node;
10756   else if (long_p)
10757     type = long_integer_type_node;
10758   else if (short_p)
10759     type = short_integer_type_node;
10760 
10761   if (decl_spec_seq_has_spec_p (declspecs, ds_complex))
10762     {
10763       if (TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
10764 	error ("complex invalid for %qs", name);
10765       /* If a modifier is specified, the resulting complex is the complex
10766 	 form of TYPE.  E.g, "complex short" is "complex short int".  */
10767       else if (type == integer_type_node)
10768 	type = complex_integer_type_node;
10769       else if (type == float_type_node)
10770 	type = complex_float_type_node;
10771       else if (type == double_type_node)
10772 	type = complex_double_type_node;
10773       else if (type == long_double_type_node)
10774 	type = complex_long_double_type_node;
10775       else
10776 	type = build_complex_type (type);
10777     }
10778 
10779   /* If we're using the injected-class-name to form a compound type or a
10780      declaration, replace it with the underlying class so we don't get
10781      redundant typedefs in the debug output.  But if we are returning the
10782      type unchanged, leave it alone so that it's available to
10783      maybe_get_template_decl_from_type_decl.  */
10784   if (CLASS_TYPE_P (type)
10785       && DECL_SELF_REFERENCE_P (TYPE_NAME (type))
10786       && type == TREE_TYPE (TYPE_NAME (type))
10787       && (declarator || type_quals))
10788     type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
10789 
10790   type_quals |= cp_type_quals (type);
10791   type = cp_build_qualified_type_real
10792     (type, type_quals, ((((typedef_decl && !DECL_ARTIFICIAL (typedef_decl))
10793 			  || declspecs->decltype_p)
10794 			 ? tf_ignore_bad_quals : 0) | tf_warning_or_error));
10795   /* We might have ignored or rejected some of the qualifiers.  */
10796   type_quals = cp_type_quals (type);
10797 
10798   if (cxx_dialect >= cxx17 && type && is_auto (type)
10799       && innermost_code != cdk_function
10800       && id_declarator && declarator != id_declarator)
10801     if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (type))
10802     {
10803       error_at (typespec_loc, "template placeholder type %qT must be followed "
10804 		"by a simple declarator-id", type);
10805       inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here", tmpl);
10806     }
10807 
10808   staticp = 0;
10809   inlinep = decl_spec_seq_has_spec_p (declspecs, ds_inline);
10810   virtualp =  decl_spec_seq_has_spec_p (declspecs, ds_virtual);
10811   explicitp = decl_spec_seq_has_spec_p (declspecs, ds_explicit);
10812 
10813   storage_class = declspecs->storage_class;
10814   if (storage_class == sc_static)
10815     staticp = 1 + (decl_context == FIELD);
10816 
10817   if (virtualp)
10818     {
10819       if (staticp == 2)
10820 	{
10821 	  error ("member %qD cannot be declared both %<virtual%> "
10822 		 "and %<static%>", dname);
10823 	  storage_class = sc_none;
10824 	  staticp = 0;
10825 	}
10826       if (constexpr_p)
10827 	error ("member %qD cannot be declared both %<virtual%> "
10828 	       "and %<constexpr%>", dname);
10829     }
10830   friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
10831 
10832   /* Issue errors about use of storage classes for parameters.  */
10833   if (decl_context == PARM)
10834     {
10835       if (typedef_p)
10836 	{
10837 	  error ("typedef declaration invalid in parameter declaration");
10838 	  return error_mark_node;
10839 	}
10840       else if (template_parm_flag && storage_class != sc_none)
10841 	{
10842 	  error ("storage class specified for template parameter %qs", name);
10843 	  return error_mark_node;
10844 	}
10845       else if (storage_class == sc_static
10846 	       || storage_class == sc_extern
10847 	       || thread_p)
10848 	error ("storage class specifiers invalid in parameter declarations");
10849 
10850       /* Function parameters cannot be concept. */
10851       if (concept_p)
10852           error ("a parameter cannot be declared %<concept%>");
10853       /* Function parameters cannot be constexpr.  If we saw one, moan
10854          and pretend it wasn't there.  */
10855       else if (constexpr_p)
10856         {
10857           error ("a parameter cannot be declared %<constexpr%>");
10858           constexpr_p = 0;
10859         }
10860     }
10861 
10862   /* Give error if `virtual' is used outside of class declaration.  */
10863   if (virtualp
10864       && (current_class_name == NULL_TREE || decl_context != FIELD))
10865     {
10866       error_at (declspecs->locations[ds_virtual],
10867 		"%<virtual%> outside class declaration");
10868       virtualp = 0;
10869     }
10870 
10871   if (innermost_code == cdk_decomp)
10872     {
10873       location_t loc = (declarator->kind == cdk_reference
10874 			? declarator->declarator->id_loc : declarator->id_loc);
10875       if (inlinep)
10876 	error_at (declspecs->locations[ds_inline],
10877 		  "structured binding declaration cannot be %<inline%>");
10878       if (typedef_p)
10879 	error_at (declspecs->locations[ds_typedef],
10880 		  "structured binding declaration cannot be %<typedef%>");
10881       if (constexpr_p)
10882 	error_at (declspecs->locations[ds_constexpr], "structured "
10883 		  "binding declaration cannot be %<constexpr%>");
10884       if (thread_p)
10885 	error_at (declspecs->locations[ds_thread],
10886 		  "structured binding declaration cannot be %qs",
10887 		  declspecs->gnu_thread_keyword_p
10888 		  ? "__thread" : "thread_local");
10889       if (concept_p)
10890 	error_at (declspecs->locations[ds_concept],
10891 		  "structured binding declaration cannot be %<concept%>");
10892       switch (storage_class)
10893 	{
10894 	case sc_none:
10895 	  break;
10896 	case sc_register:
10897 	  error_at (loc, "structured binding declaration cannot be "
10898 		    "%<register%>");
10899 	  break;
10900 	case sc_static:
10901 	  error_at (loc, "structured binding declaration cannot be "
10902 		    "%<static%>");
10903 	  break;
10904 	case sc_extern:
10905 	  error_at (loc, "structured binding declaration cannot be "
10906 		    "%<extern%>");
10907 	  break;
10908 	case sc_mutable:
10909 	  error_at (loc, "structured binding declaration cannot be "
10910 		    "%<mutable%>");
10911 	  break;
10912 	case sc_auto:
10913 	  error_at (loc, "structured binding declaration cannot be "
10914 		    "C++98 %<auto%>");
10915 	  break;
10916 	default:
10917 	  gcc_unreachable ();
10918 	}
10919       if (TREE_CODE (type) != TEMPLATE_TYPE_PARM
10920 	  || TYPE_IDENTIFIER (type) != auto_identifier)
10921 	{
10922 	  if (type != error_mark_node)
10923 	    {
10924 	      error_at (loc, "structured binding declaration cannot have "
10925 			"type %qT", type);
10926 	      inform (loc,
10927 		      "type must be cv-qualified %<auto%> or reference to "
10928 		      "cv-qualified %<auto%>");
10929 	    }
10930 	  type = build_qualified_type (make_auto (), type_quals);
10931 	  declspecs->type = type;
10932 	}
10933       inlinep = 0;
10934       typedef_p = 0;
10935       constexpr_p = 0;
10936       thread_p = 0;
10937       concept_p = 0;
10938       storage_class = sc_none;
10939       staticp = 0;
10940       declspecs->storage_class = sc_none;
10941       declspecs->locations[ds_thread] = UNKNOWN_LOCATION;
10942     }
10943 
10944   /* Static anonymous unions are dealt with here.  */
10945   if (staticp && decl_context == TYPENAME
10946       && declspecs->type
10947       && ANON_AGGR_TYPE_P (declspecs->type))
10948     decl_context = FIELD;
10949 
10950   /* Warn about storage classes that are invalid for certain
10951      kinds of declarations (parameters, typenames, etc.).  */
10952   if (thread_p
10953       && ((storage_class
10954 	   && storage_class != sc_extern
10955 	   && storage_class != sc_static)
10956 	  || typedef_p))
10957     {
10958       error ("multiple storage classes in declaration of %qs", name);
10959       thread_p = false;
10960     }
10961   if (decl_context != NORMAL
10962       && ((storage_class != sc_none
10963 	   && storage_class != sc_mutable)
10964 	  || thread_p))
10965     {
10966       if ((decl_context == PARM || decl_context == CATCHPARM)
10967 	  && (storage_class == sc_register
10968 	      || storage_class == sc_auto))
10969 	;
10970       else if (typedef_p)
10971 	;
10972       else if (decl_context == FIELD
10973 	       /* C++ allows static class elements.  */
10974 	       && storage_class == sc_static)
10975 	/* C++ also allows inlines and signed and unsigned elements,
10976 	   but in those cases we don't come in here.  */
10977 	;
10978       else
10979 	{
10980 	  if (decl_context == FIELD)
10981 	    error ("storage class specified for %qs", name);
10982 	  else
10983 	    {
10984 	      if (decl_context == PARM || decl_context == CATCHPARM)
10985 		error ("storage class specified for parameter %qs", name);
10986 	      else
10987 		error ("storage class specified for typename");
10988 	    }
10989 	  if (storage_class == sc_register
10990 	      || storage_class == sc_auto
10991 	      || storage_class == sc_extern
10992 	      || thread_p)
10993 	    storage_class = sc_none;
10994 	}
10995     }
10996   else if (storage_class == sc_extern && funcdef_flag
10997 	   && ! toplevel_bindings_p ())
10998     error ("nested function %qs declared %<extern%>", name);
10999   else if (toplevel_bindings_p ())
11000     {
11001       if (storage_class == sc_auto)
11002 	error ("top-level declaration of %qs specifies %<auto%>", name);
11003     }
11004   else if (thread_p
11005 	   && storage_class != sc_extern
11006 	   && storage_class != sc_static)
11007     {
11008       if (declspecs->gnu_thread_keyword_p)
11009 	pedwarn (input_location, 0, "function-scope %qs implicitly auto and "
11010 		 "declared %<__thread%>", name);
11011 
11012       /* When thread_local is applied to a variable of block scope the
11013 	 storage-class-specifier static is implied if it does not appear
11014 	 explicitly.  */
11015       storage_class = declspecs->storage_class = sc_static;
11016       staticp = 1;
11017     }
11018 
11019   if (storage_class && friendp)
11020     {
11021       error ("storage class specifiers invalid in friend function declarations");
11022       storage_class = sc_none;
11023       staticp = 0;
11024     }
11025 
11026   if (!id_declarator)
11027     unqualified_id = NULL_TREE;
11028   else
11029     {
11030       unqualified_id = id_declarator->u.id.unqualified_name;
11031       switch (TREE_CODE (unqualified_id))
11032 	{
11033 	case BIT_NOT_EXPR:
11034 	  unqualified_id = TREE_OPERAND (unqualified_id, 0);
11035 	  if (TYPE_P (unqualified_id))
11036 	    unqualified_id = constructor_name (unqualified_id);
11037 	  break;
11038 
11039 	case IDENTIFIER_NODE:
11040 	case TEMPLATE_ID_EXPR:
11041 	  break;
11042 
11043 	default:
11044 	  gcc_unreachable ();
11045 	}
11046     }
11047 
11048   if (declspecs->std_attributes)
11049     {
11050       location_t attr_loc = declspecs->locations[ds_std_attribute];
11051       if (warning_at (attr_loc, OPT_Wattributes, "attribute ignored"))
11052 	inform (attr_loc, "an attribute that appertains to a type-specifier "
11053 		"is ignored");
11054     }
11055 
11056   /* Determine the type of the entity declared by recurring on the
11057      declarator.  */
11058   for (; declarator; declarator = declarator->declarator)
11059     {
11060       const cp_declarator *inner_declarator;
11061       tree attrs;
11062 
11063       if (type == error_mark_node)
11064 	return error_mark_node;
11065 
11066       attrs = declarator->attributes;
11067       if (attrs)
11068 	{
11069 	  int attr_flags;
11070 
11071 	  attr_flags = 0;
11072 	  if (declarator == NULL || declarator->kind == cdk_id)
11073 	    attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
11074 	  if (declarator->kind == cdk_function)
11075 	    attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
11076 	  if (declarator->kind == cdk_array)
11077 	    attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
11078 	  returned_attrs = decl_attributes (&type,
11079 					    chainon (returned_attrs, attrs),
11080 					    attr_flags);
11081 	}
11082 
11083       inner_declarator = declarator->declarator;
11084 
11085       /* We don't want to warn in parameter context because we don't
11086 	 yet know if the parse will succeed, and this might turn out
11087 	 to be a constructor call.  */
11088       if (decl_context != PARM
11089 	  && decl_context != TYPENAME
11090 	  && !typedef_p
11091 	  && declarator->parenthesized != UNKNOWN_LOCATION
11092 	  /* If the type is class-like and the inner name used a
11093 	     global namespace qualifier, we need the parens.
11094 	     Unfortunately all we can tell is whether a qualified name
11095 	     was used or not.  */
11096 	  && !(inner_declarator
11097 	       && inner_declarator->kind == cdk_id
11098 	       && inner_declarator->u.id.qualifying_scope
11099 	       && (MAYBE_CLASS_TYPE_P (type)
11100 		   || TREE_CODE (type) == ENUMERAL_TYPE)))
11101 	warning_at (declarator->parenthesized, OPT_Wparentheses,
11102 		    "unnecessary parentheses in declaration of %qs", name);
11103       if (declarator->kind == cdk_id || declarator->kind == cdk_decomp)
11104 	break;
11105 
11106       switch (declarator->kind)
11107 	{
11108 	case cdk_array:
11109 	  type = create_array_type_for_decl (dname, type,
11110 					     declarator->u.array.bounds);
11111 	  if (!valid_array_size_p (input_location, type, dname))
11112 	    type = error_mark_node;
11113 
11114 	  if (declarator->std_attributes)
11115 	    /* [dcl.array]/1:
11116 
11117 	       The optional attribute-specifier-seq appertains to the
11118 	       array.  */
11119 	    returned_attrs = chainon (returned_attrs,
11120 				      declarator->std_attributes);
11121 	  break;
11122 
11123 	case cdk_function:
11124 	  {
11125 	    tree arg_types;
11126 	    int funcdecl_p;
11127 
11128 	    /* Declaring a function type.  */
11129 
11130 	    input_location = declspecs->locations[ds_type_spec];
11131 	    abstract_virtuals_error (ACU_RETURN, type);
11132 	    input_location = saved_loc;
11133 
11134 	    /* Pick up type qualifiers which should be applied to `this'.  */
11135 	    memfn_quals = declarator->u.function.qualifiers;
11136 	    /* Pick up virt-specifiers.  */
11137             virt_specifiers = declarator->u.function.virt_specifiers;
11138 	    /* And ref-qualifier, too */
11139 	    rqual = declarator->u.function.ref_qualifier;
11140 	    /* And tx-qualifier.  */
11141 	    tree tx_qual = declarator->u.function.tx_qualifier;
11142 	    /* Pick up the exception specifications.  */
11143 	    raises = declarator->u.function.exception_specification;
11144 	    /* If the exception-specification is ill-formed, let's pretend
11145 	       there wasn't one.  */
11146 	    if (raises == error_mark_node)
11147 	      raises = NULL_TREE;
11148 
11149 	    if (reqs)
11150 	      error_at (location_of (reqs), "requires-clause on return type");
11151 	    reqs = declarator->u.function.requires_clause;
11152 
11153 	    /* Say it's a definition only for the CALL_EXPR
11154 	       closest to the identifier.  */
11155 	    funcdecl_p = inner_declarator && inner_declarator->kind == cdk_id;
11156 
11157 	    /* Handle a late-specified return type.  */
11158 	    tree late_return_type = declarator->u.function.late_return_type;
11159 	    if (funcdecl_p)
11160 	      {
11161 		if (tree auto_node = type_uses_auto (type))
11162 		  {
11163 		    if (!late_return_type)
11164 		      {
11165 			if (current_class_type
11166 			    && LAMBDA_TYPE_P (current_class_type))
11167 			  /* OK for C++11 lambdas.  */;
11168 			else if (cxx_dialect < cxx14)
11169 			  {
11170 			    error ("%qs function uses "
11171 				   "%<auto%> type specifier without trailing "
11172 				   "return type", name);
11173 			    inform (input_location, "deduced return type "
11174 				    "only available with -std=c++14 or "
11175 				    "-std=gnu++14");
11176 			  }
11177 			else if (virtualp)
11178 			  {
11179 			    error ("virtual function cannot "
11180 				   "have deduced return type");
11181 			    virtualp = false;
11182 			  }
11183 		      }
11184 		    else if (!is_auto (type) && sfk != sfk_conversion)
11185 		      {
11186 			error ("%qs function with trailing return type has"
11187 			       " %qT as its type rather than plain %<auto%>",
11188 			       name, type);
11189 			return error_mark_node;
11190 		      }
11191 		    tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node);
11192 		    if (!tmpl)
11193 		      if (tree late_auto = type_uses_auto (late_return_type))
11194 			tmpl = CLASS_PLACEHOLDER_TEMPLATE (late_auto);
11195 		    if (tmpl)
11196 		      {
11197 			if (!dguide_name_p (unqualified_id))
11198 			  {
11199 			    error_at (declarator->id_loc, "deduced class "
11200 				      "type %qD in function return type",
11201 				      DECL_NAME (tmpl));
11202 			    inform (DECL_SOURCE_LOCATION (tmpl),
11203 				    "%qD declared here", tmpl);
11204 			    return error_mark_node;
11205 			  }
11206 			else if (!late_return_type)
11207 			  {
11208 			    error_at (declarator->id_loc, "deduction guide "
11209 				      "for %qT must have trailing return "
11210 				      "type", TREE_TYPE (tmpl));
11211 			    inform (DECL_SOURCE_LOCATION (tmpl),
11212 				    "%qD declared here", tmpl);
11213 			    return error_mark_node;
11214 			  }
11215 			else if (CLASS_TYPE_P (late_return_type)
11216 				 && CLASSTYPE_TEMPLATE_INFO (late_return_type)
11217 				 && (CLASSTYPE_TI_TEMPLATE (late_return_type)
11218 				     == tmpl))
11219 			  /* OK */;
11220 			else
11221 			  error ("trailing return type %qT of deduction guide "
11222 				 "is not a specialization of %qT",
11223 				 late_return_type, TREE_TYPE (tmpl));
11224 		      }
11225 		  }
11226 		else if (late_return_type
11227 			 && sfk != sfk_conversion)
11228 		  {
11229 		    if (cxx_dialect < cxx11)
11230 		      /* Not using maybe_warn_cpp0x because this should
11231 			 always be an error.  */
11232 		      error ("trailing return type only available with "
11233 			     "-std=c++11 or -std=gnu++11");
11234 		    else
11235 		      error ("%qs function with trailing return type not "
11236 			     "declared with %<auto%> type specifier", name);
11237 		    return error_mark_node;
11238 		  }
11239 	      }
11240 	    type = splice_late_return_type (type, late_return_type);
11241 	    if (type == error_mark_node)
11242 	      return error_mark_node;
11243 
11244 	    if (late_return_type)
11245 	      {
11246 		late_return_type_p = true;
11247 		type_quals = cp_type_quals (type);
11248 	      }
11249 
11250 	    if (type_quals != TYPE_UNQUALIFIED)
11251 	      {
11252 		if (SCALAR_TYPE_P (type) || VOID_TYPE_P (type))
11253 		  warning_at (typespec_loc, OPT_Wignored_qualifiers, "type "
11254 			      "qualifiers ignored on function return type");
11255 		/* We now know that the TYPE_QUALS don't apply to the
11256 		   decl, but to its return type.  */
11257 		type_quals = TYPE_UNQUALIFIED;
11258 	      }
11259 
11260 	    /* Error about some types functions can't return.  */
11261 
11262 	    if (TREE_CODE (type) == FUNCTION_TYPE)
11263 	      {
11264 		error_at (typespec_loc, "%qs declared as function returning "
11265 			  "a function", name);
11266 		return error_mark_node;
11267 	      }
11268 	    if (TREE_CODE (type) == ARRAY_TYPE)
11269 	      {
11270 		error_at (typespec_loc, "%qs declared as function returning "
11271 			  "an array", name);
11272 		return error_mark_node;
11273 	      }
11274 
11275 	    if (ctype == NULL_TREE
11276 		&& decl_context == FIELD
11277 		&& funcdecl_p
11278 		&& friendp == 0)
11279 	      ctype = current_class_type;
11280 
11281 	    if (ctype && (sfk == sfk_constructor
11282 			  || sfk == sfk_destructor))
11283 	      {
11284 		/* We are within a class's scope. If our declarator name
11285 		   is the same as the class name, and we are defining
11286 		   a function, then it is a constructor/destructor, and
11287 		   therefore returns a void type.  */
11288 
11289 		/* ISO C++ 12.4/2.  A destructor may not be declared
11290 		   const or volatile.  A destructor may not be static.
11291 		   A destructor may not be declared with ref-qualifier.
11292 
11293 		   ISO C++ 12.1.  A constructor may not be declared
11294 		   const or volatile.  A constructor may not be
11295 		   virtual.  A constructor may not be static.
11296 		   A constructor may not be declared with ref-qualifier. */
11297 		if (staticp == 2)
11298 		  error ((flags == DTOR_FLAG)
11299 			 ? G_("destructor cannot be static member function")
11300 			 : G_("constructor cannot be static member function"));
11301 		if (memfn_quals)
11302 		  {
11303 		    error ((flags == DTOR_FLAG)
11304 			   ? G_("destructors may not be cv-qualified")
11305 			   : G_("constructors may not be cv-qualified"));
11306 		    memfn_quals = TYPE_UNQUALIFIED;
11307 		  }
11308 
11309 		if (rqual)
11310 		  {
11311 		    maybe_warn_cpp0x (CPP0X_REF_QUALIFIER);
11312 		    error ((flags == DTOR_FLAG)
11313 			   ? G_("destructors may not be ref-qualified")
11314 			   : G_("constructors may not be ref-qualified"));
11315 		    rqual = REF_QUAL_NONE;
11316 		  }
11317 
11318 		if (decl_context == FIELD
11319 		    && !member_function_or_else (ctype,
11320 						 current_class_type,
11321 						 flags))
11322 		  return error_mark_node;
11323 
11324 		if (flags != DTOR_FLAG)
11325 		  {
11326 		    /* It's a constructor.  */
11327 		    if (explicitp == 1)
11328 		      explicitp = 2;
11329 		    if (virtualp)
11330 		      {
11331 			permerror (input_location,
11332 				   "constructors cannot be declared %<virtual%>");
11333 			virtualp = 0;
11334 		      }
11335 		    if (decl_context == FIELD
11336 			&& sfk != sfk_constructor)
11337 		      return error_mark_node;
11338 		  }
11339 		if (decl_context == FIELD)
11340 		  staticp = 0;
11341 	      }
11342 	    else if (friendp)
11343 	      {
11344 		if (virtualp)
11345 		  {
11346 		    /* Cannot be both friend and virtual.  */
11347 		    error ("virtual functions cannot be friends");
11348 		    friendp = 0;
11349 		  }
11350 		if (decl_context == NORMAL)
11351 		  error ("friend declaration not in class definition");
11352 		if (current_function_decl && funcdef_flag)
11353 		  error ("can%'t define friend function %qs in a local "
11354 			 "class definition",
11355 			 name);
11356 	      }
11357 	    else if (ctype && sfk == sfk_conversion)
11358 	      {
11359 		if (explicitp == 1)
11360 		  {
11361 		    maybe_warn_cpp0x (CPP0X_EXPLICIT_CONVERSION);
11362 		    explicitp = 2;
11363 		  }
11364 		if (late_return_type_p)
11365 		  error ("a conversion function cannot have a trailing return type");
11366 	      }
11367 	    else if (sfk == sfk_deduction_guide)
11368 	      {
11369 		if (explicitp == 1)
11370 		  explicitp = 2;
11371 	      }
11372 
11373 	    tree pushed_scope = NULL_TREE;
11374 	    if (funcdecl_p
11375 		&& decl_context != FIELD
11376 		&& inner_declarator->u.id.qualifying_scope
11377 		&& CLASS_TYPE_P (inner_declarator->u.id.qualifying_scope))
11378 	      pushed_scope
11379 		= push_scope (inner_declarator->u.id.qualifying_scope);
11380 
11381 	    arg_types = grokparms (declarator->u.function.parameters, &parms);
11382 
11383 	    if (pushed_scope)
11384 	      pop_scope (pushed_scope);
11385 
11386 	    if (inner_declarator
11387 		&& inner_declarator->kind == cdk_id
11388 		&& inner_declarator->u.id.sfk == sfk_destructor
11389 		&& arg_types != void_list_node)
11390 	      {
11391 		error ("destructors may not have parameters");
11392 		arg_types = void_list_node;
11393 		parms = NULL_TREE;
11394 	      }
11395 
11396 	    type = build_function_type (type, arg_types);
11397 
11398 	    tree attrs = declarator->std_attributes;
11399 	    if (tx_qual)
11400 	      {
11401 		tree att = build_tree_list (tx_qual, NULL_TREE);
11402 		/* transaction_safe applies to the type, but
11403 		   transaction_safe_dynamic applies to the function.  */
11404 		if (is_attribute_p ("transaction_safe", tx_qual))
11405 		  attrs = chainon (attrs, att);
11406 		else
11407 		  returned_attrs = chainon (returned_attrs, att);
11408 	      }
11409 	    if (attrs)
11410 	      /* [dcl.fct]/2:
11411 
11412 		 The optional attribute-specifier-seq appertains to
11413 		 the function type.  */
11414 	      decl_attributes (&type, attrs, 0);
11415 
11416 	    if (raises)
11417 	      type = build_exception_variant (type, raises);
11418 	  }
11419 	  break;
11420 
11421 	case cdk_pointer:
11422 	case cdk_reference:
11423 	case cdk_ptrmem:
11424 	  /* Filter out pointers-to-references and references-to-references.
11425 	     We can get these if a TYPE_DECL is used.  */
11426 
11427 	  if (TREE_CODE (type) == REFERENCE_TYPE)
11428 	    {
11429 	      if (declarator->kind != cdk_reference)
11430 		{
11431 		  error ("cannot declare pointer to %q#T", type);
11432 		  type = TREE_TYPE (type);
11433 		}
11434 
11435 	      /* In C++0x, we allow reference to reference declarations
11436 		 that occur indirectly through typedefs [7.1.3/8 dcl.typedef]
11437 		 and template type arguments [14.3.1/4 temp.arg.type]. The
11438 		 check for direct reference to reference declarations, which
11439 		 are still forbidden, occurs below. Reasoning behind the change
11440 		 can be found in DR106, DR540, and the rvalue reference
11441 		 proposals. */
11442 	      else if (cxx_dialect == cxx98)
11443 		{
11444 		  error ("cannot declare reference to %q#T", type);
11445 		  type = TREE_TYPE (type);
11446 		}
11447 	    }
11448 	  else if (VOID_TYPE_P (type))
11449 	    {
11450 	      if (declarator->kind == cdk_reference)
11451 		error ("cannot declare reference to %q#T", type);
11452 	      else if (declarator->kind == cdk_ptrmem)
11453 		error ("cannot declare pointer to %q#T member", type);
11454 	    }
11455 
11456 	  /* We now know that the TYPE_QUALS don't apply to the decl,
11457 	     but to the target of the pointer.  */
11458 	  type_quals = TYPE_UNQUALIFIED;
11459 
11460 	  /* This code used to handle METHOD_TYPE, but I don't think it's
11461 	     possible to get it here anymore.  */
11462 	  gcc_assert (TREE_CODE (type) != METHOD_TYPE);
11463 	  if (declarator->kind == cdk_ptrmem
11464 	      && TREE_CODE (type) == FUNCTION_TYPE)
11465 	    {
11466 	      memfn_quals |= type_memfn_quals (type);
11467 	      type = build_memfn_type (type,
11468 				       declarator->u.pointer.class_type,
11469 				       memfn_quals,
11470 				       rqual);
11471 	      if (type == error_mark_node)
11472 		return error_mark_node;
11473 
11474 	      rqual = REF_QUAL_NONE;
11475 	      memfn_quals = TYPE_UNQUALIFIED;
11476 	    }
11477 
11478 	  if (TREE_CODE (type) == FUNCTION_TYPE
11479 	      && (type_memfn_quals (type) != TYPE_UNQUALIFIED
11480 		  || type_memfn_rqual (type) != REF_QUAL_NONE))
11481             error (declarator->kind == cdk_reference
11482                    ? G_("cannot declare reference to qualified function type %qT")
11483                    : G_("cannot declare pointer to qualified function type %qT"),
11484 		   type);
11485 
11486 	  /* When the pointed-to type involves components of variable size,
11487 	     care must be taken to ensure that the size evaluation code is
11488 	     emitted early enough to dominate all the possible later uses
11489 	     and late enough for the variables on which it depends to have
11490 	     been assigned.
11491 
11492 	     This is expected to happen automatically when the pointed-to
11493 	     type has a name/declaration of it's own, but special attention
11494 	     is required if the type is anonymous.
11495 
11496 	     We handle the NORMAL and FIELD contexts here by inserting a
11497 	     dummy statement that just evaluates the size at a safe point
11498 	     and ensures it is not deferred until e.g. within a deeper
11499 	     conditional context (c++/43555).
11500 
11501 	     We expect nothing to be needed here for PARM or TYPENAME.
11502 	     Evaluating the size at this point for TYPENAME would
11503 	     actually be incorrect, as we might be in the middle of an
11504 	     expression with side effects on the pointed-to type size
11505 	     "arguments" prior to the pointer declaration point and the
11506 	     size evaluation could end up prior to the side effects.  */
11507 
11508 	  if (!TYPE_NAME (type)
11509 	      && (decl_context == NORMAL || decl_context == FIELD)
11510 	      && at_function_scope_p ()
11511 	      && variably_modified_type_p (type, NULL_TREE))
11512 	    {
11513 	      TYPE_NAME (type) = build_decl (UNKNOWN_LOCATION, TYPE_DECL,
11514 					     NULL_TREE, type);
11515 	      add_decl_expr (TYPE_NAME (type));
11516 	    }
11517 
11518 	  if (declarator->kind == cdk_reference)
11519 	    {
11520 	      /* In C++0x, the type we are creating a reference to might be
11521 		 a typedef which is itself a reference type. In that case,
11522 		 we follow the reference collapsing rules in
11523 		 [7.1.3/8 dcl.typedef] to create the final reference type:
11524 
11525 		 "If a typedef TD names a type that is a reference to a type
11526 		 T, an attempt to create the type 'lvalue reference to cv TD'
11527 		 creates the type 'lvalue reference to T,' while an attempt
11528 		 to create the type "rvalue reference to cv TD' creates the
11529 		 type TD."
11530               */
11531 	      if (VOID_TYPE_P (type))
11532 		/* We already gave an error.  */;
11533 	      else if (TREE_CODE (type) == REFERENCE_TYPE)
11534 		{
11535 		  if (declarator->u.reference.rvalue_ref)
11536 		    /* Leave type alone.  */;
11537 		  else
11538 		    type = cp_build_reference_type (TREE_TYPE (type), false);
11539 		}
11540 	      else
11541 		type = cp_build_reference_type
11542 		  (type, declarator->u.reference.rvalue_ref);
11543 
11544 	      /* In C++0x, we need this check for direct reference to
11545 		 reference declarations, which are forbidden by
11546 		 [8.3.2/5 dcl.ref]. Reference to reference declarations
11547 		 are only allowed indirectly through typedefs and template
11548 		 type arguments. Example:
11549 
11550 		   void foo(int & &);      // invalid ref-to-ref decl
11551 
11552 		   typedef int & int_ref;
11553 		   void foo(int_ref &);    // valid ref-to-ref decl
11554 	      */
11555 	      if (inner_declarator && inner_declarator->kind == cdk_reference)
11556 		error ("cannot declare reference to %q#T, which is not "
11557 		       "a typedef or a template type argument", type);
11558 	    }
11559 	  else if (TREE_CODE (type) == METHOD_TYPE)
11560 	    type = build_ptrmemfunc_type (build_pointer_type (type));
11561 	  else if (declarator->kind == cdk_ptrmem)
11562 	    {
11563 	      gcc_assert (TREE_CODE (declarator->u.pointer.class_type)
11564 			  != NAMESPACE_DECL);
11565 	      if (declarator->u.pointer.class_type == error_mark_node)
11566 		/* We will already have complained.  */
11567 		type = error_mark_node;
11568 	      else
11569 		type = build_ptrmem_type (declarator->u.pointer.class_type,
11570 					  type);
11571 	    }
11572 	  else
11573 	    type = build_pointer_type (type);
11574 
11575 	  /* Process a list of type modifier keywords (such as
11576 	     const or volatile) that were given inside the `*' or `&'.  */
11577 
11578 	  if (declarator->u.pointer.qualifiers)
11579 	    {
11580 	      type
11581 		= cp_build_qualified_type (type,
11582 					   declarator->u.pointer.qualifiers);
11583 	      type_quals = cp_type_quals (type);
11584 	    }
11585 
11586 	  /* Apply C++11 attributes to the pointer, and not to the
11587 	     type pointed to.  This is unlike what is done for GNU
11588 	     attributes above.  It is to comply with [dcl.ptr]/1:
11589 
11590 		 [the optional attribute-specifier-seq (7.6.1) appertains
11591 		  to the pointer and not to the object pointed to].  */
11592 	  if (declarator->std_attributes)
11593 	    decl_attributes (&type, declarator->std_attributes,
11594 			     0);
11595 
11596 	  ctype = NULL_TREE;
11597 	  break;
11598 
11599 	case cdk_error:
11600 	  break;
11601 
11602 	default:
11603 	  gcc_unreachable ();
11604 	}
11605     }
11606 
11607   /* A `constexpr' specifier used in an object declaration declares
11608      the object as `const'.  */
11609   if (constexpr_p && innermost_code != cdk_function)
11610     {
11611       /* DR1688 says that a `constexpr' specifier in combination with
11612 	 `volatile' is valid.  */
11613 
11614       if (TREE_CODE (type) != REFERENCE_TYPE)
11615 	{
11616 	  type_quals |= TYPE_QUAL_CONST;
11617 	  type = cp_build_qualified_type (type, type_quals);
11618 	}
11619     }
11620 
11621   if (unqualified_id && TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR
11622       && TREE_CODE (type) != FUNCTION_TYPE
11623       && TREE_CODE (type) != METHOD_TYPE
11624       && !variable_template_p (TREE_OPERAND (unqualified_id, 0)))
11625     {
11626       error ("template-id %qD used as a declarator",
11627 	     unqualified_id);
11628       unqualified_id = dname;
11629     }
11630 
11631   /* If TYPE is a FUNCTION_TYPE, but the function name was explicitly
11632      qualified with a class-name, turn it into a METHOD_TYPE, unless
11633      we know that the function is static.  We take advantage of this
11634      opportunity to do other processing that pertains to entities
11635      explicitly declared to be class members.  Note that if DECLARATOR
11636      is non-NULL, we know it is a cdk_id declarator; otherwise, we
11637      would not have exited the loop above.  */
11638   if (declarator
11639       && declarator->kind == cdk_id
11640       && declarator->u.id.qualifying_scope
11641       && MAYBE_CLASS_TYPE_P (declarator->u.id.qualifying_scope))
11642     {
11643       ctype = declarator->u.id.qualifying_scope;
11644       ctype = TYPE_MAIN_VARIANT (ctype);
11645       template_count = num_template_headers_for_class (ctype);
11646 
11647       if (ctype == current_class_type)
11648 	{
11649 	  if (friendp)
11650 	    {
11651 	      permerror (input_location, "member functions are implicitly "
11652 					 "friends of their class");
11653 	      friendp = 0;
11654 	    }
11655 	  else
11656 	    permerror (declarator->id_loc,
11657 		       "extra qualification %<%T::%> on member %qs",
11658 		       ctype, name);
11659 	}
11660       else if (/* If the qualifying type is already complete, then we
11661 		  can skip the following checks.  */
11662 	       !COMPLETE_TYPE_P (ctype)
11663 	       && (/* If the function is being defined, then
11664 		      qualifying type must certainly be complete.  */
11665 		   funcdef_flag
11666 		   /* A friend declaration of "T::f" is OK, even if
11667 		      "T" is a template parameter.  But, if this
11668 		      function is not a friend, the qualifying type
11669 		      must be a class.  */
11670 		   || (!friendp && !CLASS_TYPE_P (ctype))
11671 		   /* For a declaration, the type need not be
11672 		      complete, if either it is dependent (since there
11673 		      is no meaningful definition of complete in that
11674 		      case) or the qualifying class is currently being
11675 		      defined.  */
11676 		   || !(dependent_type_p (ctype)
11677 			|| currently_open_class (ctype)))
11678 	       /* Check that the qualifying type is complete.  */
11679 	       && !complete_type_or_else (ctype, NULL_TREE))
11680 	return error_mark_node;
11681       else if (TREE_CODE (type) == FUNCTION_TYPE)
11682 	{
11683 	  if (current_class_type
11684 	      && (!friendp || funcdef_flag || initialized))
11685 	    {
11686 	      error (funcdef_flag || initialized
11687 		     ? G_("cannot define member function %<%T::%s%> "
11688 			  "within %qT")
11689 		     : G_("cannot declare member function %<%T::%s%> "
11690 			  "within %qT"),
11691 		     ctype, name, current_class_type);
11692 	      return error_mark_node;
11693 	    }
11694 	}
11695       else if (typedef_p && current_class_type)
11696 	{
11697 	  error ("cannot declare member %<%T::%s%> within %qT",
11698 		 ctype, name, current_class_type);
11699 	  return error_mark_node;
11700 	}
11701     }
11702 
11703   if (ctype == NULL_TREE && decl_context == FIELD && friendp == 0)
11704     ctype = current_class_type;
11705 
11706   /* Now TYPE has the actual type.  */
11707 
11708   if (returned_attrs)
11709     {
11710       if (attrlist)
11711 	*attrlist = chainon (returned_attrs, *attrlist);
11712       else
11713 	attrlist = &returned_attrs;
11714     }
11715 
11716   if (declarator
11717       && declarator->kind == cdk_id
11718       && declarator->std_attributes
11719       && attrlist != NULL)
11720     {
11721       /* [dcl.meaning]/1: The optional attribute-specifier-seq following
11722 	 a declarator-id appertains to the entity that is declared.  */
11723       if (declarator->std_attributes != error_mark_node)
11724 	*attrlist = chainon (*attrlist, declarator->std_attributes);
11725       else
11726 	/* We should have already diagnosed the issue (c++/78344).  */
11727 	gcc_assert (seen_error ());
11728     }
11729 
11730   /* Handle parameter packs. */
11731   if (parameter_pack_p)
11732     {
11733       if (decl_context == PARM)
11734         /* Turn the type into a pack expansion.*/
11735         type = make_pack_expansion (type);
11736       else
11737         error ("non-parameter %qs cannot be a parameter pack", name);
11738     }
11739 
11740   if ((decl_context == FIELD || decl_context == PARM)
11741       && !processing_template_decl
11742       && variably_modified_type_p (type, NULL_TREE))
11743     {
11744       if (decl_context == FIELD)
11745 	error ("data member may not have variably modified type %qT", type);
11746       else
11747 	error ("parameter may not have variably modified type %qT", type);
11748       type = error_mark_node;
11749     }
11750 
11751   if (explicitp == 1 || (explicitp && friendp))
11752     {
11753       /* [dcl.fct.spec] (C++11) The explicit specifier shall be used only
11754 	 in the declaration of a constructor or conversion function within
11755 	 a class definition.  */
11756       if (!current_class_type)
11757 	error_at (declspecs->locations[ds_explicit],
11758 		  "%<explicit%> outside class declaration");
11759       else if (friendp)
11760 	error_at (declspecs->locations[ds_explicit],
11761 		  "%<explicit%> in friend declaration");
11762       else
11763 	error_at (declspecs->locations[ds_explicit],
11764 		  "only declarations of constructors and conversion operators "
11765 		  "can be %<explicit%>");
11766       explicitp = 0;
11767     }
11768 
11769   if (storage_class == sc_mutable)
11770     {
11771       if (decl_context != FIELD || friendp)
11772 	{
11773 	  error ("non-member %qs cannot be declared %<mutable%>", name);
11774 	  storage_class = sc_none;
11775 	}
11776       else if (decl_context == TYPENAME || typedef_p)
11777 	{
11778 	  error ("non-object member %qs cannot be declared %<mutable%>", name);
11779 	  storage_class = sc_none;
11780 	}
11781       else if (TREE_CODE (type) == FUNCTION_TYPE
11782 	       || TREE_CODE (type) == METHOD_TYPE)
11783 	{
11784 	  error ("function %qs cannot be declared %<mutable%>", name);
11785 	  storage_class = sc_none;
11786 	}
11787       else if (staticp)
11788 	{
11789 	  error ("static %qs cannot be declared %<mutable%>", name);
11790 	  storage_class = sc_none;
11791 	}
11792       else if (type_quals & TYPE_QUAL_CONST)
11793 	{
11794 	  error ("const %qs cannot be declared %<mutable%>", name);
11795 	  storage_class = sc_none;
11796 	}
11797       else if (TREE_CODE (type) == REFERENCE_TYPE)
11798 	{
11799 	  permerror (input_location, "reference %qs cannot be declared "
11800 	             "%<mutable%>", name);
11801 	  storage_class = sc_none;
11802 	}
11803     }
11804 
11805   /* If this is declaring a typedef name, return a TYPE_DECL.  */
11806   if (typedef_p && decl_context != TYPENAME)
11807     {
11808       tree decl;
11809 
11810       /* This declaration:
11811 
11812 	   typedef void f(int) const;
11813 
11814 	 declares a function type which is not a member of any
11815 	 particular class, but which is cv-qualified; for
11816 	 example "f S::*" declares a pointer to a const-qualified
11817 	 member function of S.  We record the cv-qualification in the
11818 	 function type.  */
11819       if ((rqual || memfn_quals) && TREE_CODE (type) == FUNCTION_TYPE)
11820         {
11821           type = apply_memfn_quals (type, memfn_quals, rqual);
11822 
11823           /* We have now dealt with these qualifiers.  */
11824           memfn_quals = TYPE_UNQUALIFIED;
11825 	  rqual = REF_QUAL_NONE;
11826         }
11827 
11828       if (type_uses_auto (type))
11829 	{
11830 	  error ("typedef declared %<auto%>");
11831 	  type = error_mark_node;
11832 	}
11833 
11834       if (reqs)
11835 	error_at (location_of (reqs), "requires-clause on typedef");
11836 
11837       if (id_declarator && declarator->u.id.qualifying_scope)
11838 	{
11839 	  error ("typedef name may not be a nested-name-specifier");
11840 	  type = error_mark_node;
11841 	}
11842 
11843       if (decl_context == FIELD)
11844 	decl = build_lang_decl (TYPE_DECL, unqualified_id, type);
11845       else
11846 	decl = build_decl (input_location, TYPE_DECL, unqualified_id, type);
11847 
11848       if (decl_context != FIELD)
11849 	{
11850 	  if (!current_function_decl)
11851 	    DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
11852 	  else if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (current_function_decl)
11853 		   || (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P
11854 		       (current_function_decl)))
11855 	    /* The TYPE_DECL is "abstract" because there will be
11856 	       clones of this constructor/destructor, and there will
11857 	       be copies of this TYPE_DECL generated in those
11858 	       clones.  The decloning optimization (for space) may
11859                revert this subsequently if it determines that
11860                the clones should share a common implementation.  */
11861 	    DECL_ABSTRACT_P (decl) = true;
11862 	}
11863       else if (current_class_type
11864 	       && constructor_name_p (unqualified_id, current_class_type))
11865 	permerror (input_location, "ISO C++ forbids nested type %qD with same name "
11866 		   "as enclosing class",
11867 		   unqualified_id);
11868 
11869       /* If the user declares "typedef struct {...} foo" then the
11870 	 struct will have an anonymous name.  Fill that name in now.
11871 	 Nothing can refer to it, so nothing needs know about the name
11872 	 change.  */
11873       if (type != error_mark_node
11874 	  && unqualified_id
11875 	  && TYPE_NAME (type)
11876 	  && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
11877 	  && TYPE_UNNAMED_P (type)
11878 	  && declspecs->type_definition_p
11879 	  && attributes_naming_typedef_ok (*attrlist)
11880 	  && cp_type_quals (type) == TYPE_UNQUALIFIED)
11881 	name_unnamed_type (type, decl);
11882 
11883       if (signed_p
11884 	  || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
11885 	C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
11886 
11887       bad_specifiers (decl, BSP_TYPE, virtualp,
11888 		      memfn_quals != TYPE_UNQUALIFIED,
11889 		      inlinep, friendp, raises != NULL_TREE);
11890 
11891       if (decl_spec_seq_has_spec_p (declspecs, ds_alias))
11892 	/* Acknowledge that this was written:
11893 	     `using analias = atype;'.  */
11894 	TYPE_DECL_ALIAS_P (decl) = 1;
11895 
11896       return decl;
11897     }
11898 
11899   /* Detect the case of an array type of unspecified size
11900      which came, as such, direct from a typedef name.
11901      We must copy the type, so that the array's domain can be
11902      individually set by the object's initializer.  */
11903 
11904   if (type && typedef_type
11905       && TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
11906       && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
11907     type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
11908 
11909   /* Detect where we're using a typedef of function type to declare a
11910      function. PARMS will not be set, so we must create it now.  */
11911 
11912   if (type == typedef_type && TREE_CODE (type) == FUNCTION_TYPE)
11913     {
11914       tree decls = NULL_TREE;
11915       tree args;
11916 
11917       for (args = TYPE_ARG_TYPES (type);
11918 	   args && args != void_list_node;
11919 	   args = TREE_CHAIN (args))
11920 	{
11921 	  tree decl = cp_build_parm_decl (NULL_TREE, NULL_TREE,
11922 					  TREE_VALUE (args));
11923 
11924 	  DECL_CHAIN (decl) = decls;
11925 	  decls = decl;
11926 	}
11927 
11928       parms = nreverse (decls);
11929 
11930       if (decl_context != TYPENAME)
11931 	{
11932 	  /* The qualifiers on the function type become the qualifiers on
11933 	     the non-static member function. */
11934 	  memfn_quals |= type_memfn_quals (type);
11935 	  rqual = type_memfn_rqual (type);
11936 	  type_quals = TYPE_UNQUALIFIED;
11937 	}
11938     }
11939 
11940   /* If this is a type name (such as, in a cast or sizeof),
11941      compute the type and return it now.  */
11942 
11943   if (decl_context == TYPENAME)
11944     {
11945       /* Note that here we don't care about type_quals.  */
11946 
11947       /* Special case: "friend class foo" looks like a TYPENAME context.  */
11948       if (friendp)
11949 	{
11950 	  if (inlinep)
11951 	    {
11952 	      error ("%<inline%> specified for friend class declaration");
11953 	      inlinep = 0;
11954 	    }
11955 
11956 	  if (!current_aggr)
11957 	    {
11958 	      /* Don't allow friend declaration without a class-key.  */
11959 	      if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
11960 		permerror (input_location, "template parameters cannot be friends");
11961 	      else if (TREE_CODE (type) == TYPENAME_TYPE)
11962 		permerror (input_location, "friend declaration requires class-key, "
11963 			   "i.e. %<friend class %T::%D%>",
11964 			   TYPE_CONTEXT (type), TYPENAME_TYPE_FULLNAME (type));
11965 	      else
11966 		permerror (input_location, "friend declaration requires class-key, "
11967 			   "i.e. %<friend %#T%>",
11968 			   type);
11969 	    }
11970 
11971 	  /* Only try to do this stuff if we didn't already give up.  */
11972 	  if (type != integer_type_node)
11973 	    {
11974 	      /* A friendly class?  */
11975 	      if (current_class_type)
11976 		make_friend_class (current_class_type, TYPE_MAIN_VARIANT (type),
11977 				   /*complain=*/true);
11978 	      else
11979 		error ("trying to make class %qT a friend of global scope",
11980 		       type);
11981 
11982 	      type = void_type_node;
11983 	    }
11984 	}
11985       else if (memfn_quals || rqual)
11986 	{
11987 	  if (ctype == NULL_TREE
11988 	      && TREE_CODE (type) == METHOD_TYPE)
11989 	    ctype = TYPE_METHOD_BASETYPE (type);
11990 
11991 	  if (ctype)
11992 	    type = build_memfn_type (type, ctype, memfn_quals, rqual);
11993 	  /* Core issue #547: need to allow this in template type args.
11994 	     Allow it in general in C++11 for alias-declarations.  */
11995 	  else if ((template_type_arg || cxx_dialect >= cxx11)
11996 		   && TREE_CODE (type) == FUNCTION_TYPE)
11997 	    type = apply_memfn_quals (type, memfn_quals, rqual);
11998 	  else
11999 	    error ("invalid qualifiers on non-member function type");
12000 	}
12001 
12002       if (reqs)
12003 	error_at (location_of (reqs), "requires-clause on type-id");
12004 
12005       return type;
12006     }
12007   else if (unqualified_id == NULL_TREE && decl_context != PARM
12008 	   && decl_context != CATCHPARM
12009 	   && TREE_CODE (type) != UNION_TYPE
12010 	   && ! bitfield
12011 	   && innermost_code != cdk_decomp)
12012     {
12013       error ("abstract declarator %qT used as declaration", type);
12014       return error_mark_node;
12015     }
12016 
12017   if (!FUNC_OR_METHOD_TYPE_P (type))
12018     {
12019       /* Only functions may be declared using an operator-function-id.  */
12020       if (dname && IDENTIFIER_ANY_OP_P (dname))
12021 	{
12022 	  error ("declaration of %qD as non-function", dname);
12023 	  return error_mark_node;
12024 	}
12025 
12026       if (reqs)
12027 	error_at (location_of (reqs),
12028 		  "requires-clause on declaration of non-function type %qT",
12029 		  type);
12030     }
12031 
12032   /* We don't check parameter types here because we can emit a better
12033      error message later.  */
12034   if (decl_context != PARM)
12035     {
12036       type = check_var_type (unqualified_id, type);
12037       if (type == error_mark_node)
12038         return error_mark_node;
12039     }
12040 
12041   /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
12042      or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE.  */
12043 
12044   if (decl_context == PARM || decl_context == CATCHPARM)
12045     {
12046       if (ctype || in_namespace)
12047 	error ("cannot use %<::%> in parameter declaration");
12048 
12049       if (type_uses_auto (type)
12050 	  && !(cxx_dialect >= cxx17 && template_parm_flag))
12051 	{
12052 	  if (cxx_dialect >= cxx14)
12053 	    error ("%<auto%> parameter not permitted in this context");
12054 	  else
12055 	    error ("parameter declared %<auto%>");
12056 	  type = error_mark_node;
12057 	}
12058 
12059       /* A parameter declared as an array of T is really a pointer to T.
12060 	 One declared as a function is really a pointer to a function.
12061 	 One declared as a member is really a pointer to member.  */
12062 
12063       if (TREE_CODE (type) == ARRAY_TYPE)
12064 	{
12065 	  /* Transfer const-ness of array into that of type pointed to.  */
12066 	  type = build_pointer_type (TREE_TYPE (type));
12067 	  type_quals = TYPE_UNQUALIFIED;
12068 	  array_parameter_p = true;
12069 	}
12070       else if (TREE_CODE (type) == FUNCTION_TYPE)
12071 	type = build_pointer_type (type);
12072     }
12073 
12074   if (ctype && TREE_CODE (type) == FUNCTION_TYPE && staticp < 2
12075       && !(identifier_p (unqualified_id)
12076 	   && IDENTIFIER_NEWDEL_OP_P (unqualified_id)))
12077     {
12078       cp_cv_quals real_quals = memfn_quals;
12079       if (cxx_dialect < cxx14 && constexpr_p
12080 	  && sfk != sfk_constructor && sfk != sfk_destructor)
12081 	real_quals |= TYPE_QUAL_CONST;
12082       type = build_memfn_type (type, ctype, real_quals, rqual);
12083     }
12084 
12085   {
12086     tree decl = NULL_TREE;
12087 
12088     if (decl_context == PARM)
12089       {
12090 	decl = cp_build_parm_decl (NULL_TREE, unqualified_id, type);
12091 	DECL_ARRAY_PARAMETER_P (decl) = array_parameter_p;
12092 
12093 	bad_specifiers (decl, BSP_PARM, virtualp,
12094 			memfn_quals != TYPE_UNQUALIFIED,
12095 			inlinep, friendp, raises != NULL_TREE);
12096       }
12097     else if (decl_context == FIELD)
12098       {
12099 	if (!staticp && !friendp && TREE_CODE (type) != METHOD_TYPE)
12100 	  if (tree auto_node = type_uses_auto (type))
12101 	    {
12102 	      location_t loc = declspecs->locations[ds_type_spec];
12103 	      if (CLASS_PLACEHOLDER_TEMPLATE (auto_node))
12104 		error_at (loc, "invalid use of template-name %qE without an "
12105 			  "argument list",
12106 			  CLASS_PLACEHOLDER_TEMPLATE (auto_node));
12107 	      else
12108 		error_at (loc, "non-static data member declared with "
12109 			  "placeholder %qT", auto_node);
12110 	      type = error_mark_node;
12111 	    }
12112 
12113 	/* The C99 flexible array extension.  */
12114 	if (!staticp && TREE_CODE (type) == ARRAY_TYPE
12115 	    && TYPE_DOMAIN (type) == NULL_TREE)
12116 	  {
12117 	    if (ctype
12118 		&& (TREE_CODE (ctype) == UNION_TYPE
12119 		    || TREE_CODE (ctype) == QUAL_UNION_TYPE))
12120 	      {
12121 		error ("flexible array member in union");
12122 		type = error_mark_node;
12123 	      }
12124 	    else
12125 	      {
12126 		/* Array is a flexible member.  */
12127 		if (in_system_header_at (input_location))
12128 		  /* Do not warn on flexible array members in system
12129 		     headers because glibc uses them.  */;
12130 		else if (name)
12131 		  pedwarn (input_location, OPT_Wpedantic,
12132 			   "ISO C++ forbids flexible array member %qs", name);
12133 		else
12134 		  pedwarn (input_location, OPT_Wpedantic,
12135 			   "ISO C++ forbids flexible array members");
12136 
12137 		/* Flexible array member has a null domain.  */
12138 		type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
12139 	      }
12140 	  }
12141 
12142 	if (type == error_mark_node)
12143 	  {
12144 	    /* Happens when declaring arrays of sizes which
12145 	       are error_mark_node, for example.  */
12146 	    decl = NULL_TREE;
12147 	  }
12148 	else if (in_namespace && !friendp)
12149 	  {
12150 	    /* Something like struct S { int N::j; };  */
12151 	    error ("invalid use of %<::%>");
12152 	    return error_mark_node;
12153 	  }
12154 	else if (TREE_CODE (type) == FUNCTION_TYPE
12155 		 || TREE_CODE (type) == METHOD_TYPE)
12156 	  {
12157 	    int publicp = 0;
12158 	    tree function_context;
12159 
12160 	    if (friendp == 0)
12161 	      {
12162 		/* This should never happen in pure C++ (the check
12163 		   could be an assert).  It could happen in
12164 		   Objective-C++ if someone writes invalid code that
12165 		   uses a function declaration for an instance
12166 		   variable or property (instance variables and
12167 		   properties are parsed as FIELD_DECLs, but they are
12168 		   part of an Objective-C class, not a C++ class).
12169 		   That code is invalid and is caught by this
12170 		   check.  */
12171 		if (!ctype)
12172 		  {
12173 		    error ("declaration of function %qD in invalid context",
12174 			   unqualified_id);
12175 		    return error_mark_node;
12176 		  }
12177 
12178 		/* ``A union may [ ... ] not [ have ] virtual functions.''
12179 		   ARM 9.5 */
12180 		if (virtualp && TREE_CODE (ctype) == UNION_TYPE)
12181 		  {
12182 		    error ("function %qD declared %<virtual%> inside a union",
12183 			   unqualified_id);
12184 		    return error_mark_node;
12185 		  }
12186 
12187 		if (virtualp
12188 		    && identifier_p (unqualified_id)
12189 		    && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
12190 		  {
12191 		    error ("%qD cannot be declared %<virtual%>, since it "
12192 			   "is always static", unqualified_id);
12193 		    virtualp = 0;
12194 		  }
12195 	      }
12196 
12197 	    /* Check that the name used for a destructor makes sense.  */
12198 	    if (sfk == sfk_destructor)
12199 	      {
12200 		tree uqname = id_declarator->u.id.unqualified_name;
12201 
12202 		if (!ctype)
12203 		  {
12204 		    gcc_assert (friendp);
12205 		    error ("expected qualified name in friend declaration "
12206 			   "for destructor %qD", uqname);
12207 		    return error_mark_node;
12208 		  }
12209 
12210 		if (!check_dtor_name (ctype, TREE_OPERAND (uqname, 0)))
12211 		  {
12212 		    error ("declaration of %qD as member of %qT",
12213 			   uqname, ctype);
12214 		    return error_mark_node;
12215 		  }
12216                 if (concept_p)
12217                   {
12218                     error ("a destructor cannot be %<concept%>");
12219                     return error_mark_node;
12220                   }
12221                 if (constexpr_p)
12222                   {
12223                     error ("a destructor cannot be %<constexpr%>");
12224                     return error_mark_node;
12225                   }
12226 	      }
12227 	    else if (sfk == sfk_constructor && friendp && !ctype)
12228 	      {
12229 		error ("expected qualified name in friend declaration "
12230 		       "for constructor %qD",
12231 		       id_declarator->u.id.unqualified_name);
12232 		return error_mark_node;
12233 	      }
12234 	    if (sfk == sfk_constructor)
12235 	      if (concept_p)
12236 		{
12237 		  error ("a constructor cannot be %<concept%>");
12238 		  return error_mark_node;
12239 		}
12240 	    if (concept_p)
12241 	      {
12242 		error ("a concept cannot be a member function");
12243 		concept_p = false;
12244 	      }
12245 
12246 	    if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
12247 	      {
12248 		tree tmpl = TREE_OPERAND (unqualified_id, 0);
12249 		if (variable_template_p (tmpl))
12250 		  {
12251 		    error ("specialization of variable template %qD "
12252 			   "declared as function", tmpl);
12253 		    inform (DECL_SOURCE_LOCATION (tmpl),
12254 			    "variable template declared here");
12255 		    return error_mark_node;
12256 		  }
12257 	      }
12258 
12259 	    /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node.  */
12260 	    function_context = (ctype != NULL_TREE) ?
12261 	      decl_function_context (TYPE_MAIN_DECL (ctype)) : NULL_TREE;
12262 	    publicp = (! friendp || ! staticp)
12263 	      && function_context == NULL_TREE;
12264 
12265 	    if (late_return_type_p)
12266 	      TYPE_HAS_LATE_RETURN_TYPE (type) = 1;
12267 
12268 	    decl = grokfndecl (ctype, type,
12269 			       TREE_CODE (unqualified_id) != TEMPLATE_ID_EXPR
12270 			       ? unqualified_id : dname,
12271 			       parms,
12272 			       unqualified_id,
12273 			       reqs,
12274 			       virtualp, flags, memfn_quals, rqual, raises,
12275 			       friendp ? -1 : 0, friendp, publicp,
12276                                inlinep | (2 * constexpr_p) | (4 * concept_p),
12277 			       initialized == SD_DELETED, sfk,
12278 			       funcdef_flag, template_count, in_namespace,
12279 			       attrlist, declarator->id_loc);
12280             decl = set_virt_specifiers (decl, virt_specifiers);
12281 	    if (decl == NULL_TREE)
12282 	      return error_mark_node;
12283 #if 0
12284 	    /* This clobbers the attrs stored in `decl' from `attrlist'.  */
12285 	    /* The decl and setting of decl_attr is also turned off.  */
12286 	    decl = build_decl_attribute_variant (decl, decl_attr);
12287 #endif
12288 
12289 	    /* [class.conv.ctor]
12290 
12291 	       A constructor declared without the function-specifier
12292 	       explicit that can be called with a single parameter
12293 	       specifies a conversion from the type of its first
12294 	       parameter to the type of its class.  Such a constructor
12295 	       is called a converting constructor.  */
12296 	    if (explicitp == 2)
12297 	      DECL_NONCONVERTING_P (decl) = 1;
12298 	  }
12299 	else if (!staticp && !dependent_type_p (type)
12300 		 && !COMPLETE_TYPE_P (complete_type (type))
12301 		 && (!complete_or_array_type_p (type)
12302 		     || initialized == 0))
12303 	  {
12304 	    if (TREE_CODE (type) != ARRAY_TYPE
12305 		|| !COMPLETE_TYPE_P (TREE_TYPE (type)))
12306 	      {
12307 		if (unqualified_id)
12308 		  {
12309 		    error ("field %qD has incomplete type %qT",
12310 			   unqualified_id, type);
12311 		    cxx_incomplete_type_inform (strip_array_types (type));
12312 		  }
12313 		else
12314 		  error ("name %qT has incomplete type", type);
12315 
12316 		type = error_mark_node;
12317 		decl = NULL_TREE;
12318 	      }
12319 	  }
12320 	else
12321 	  {
12322 	    if (friendp)
12323 	      {
12324 		error ("%qE is neither function nor member function; "
12325 		       "cannot be declared friend", unqualified_id);
12326 		return error_mark_node;
12327 	      }
12328 	    decl = NULL_TREE;
12329 	  }
12330 
12331 	if (friendp)
12332 	  {
12333 	    /* Friends are treated specially.  */
12334 	    if (ctype == current_class_type)
12335 	      ;  /* We already issued a permerror.  */
12336 	    else if (decl && DECL_NAME (decl))
12337 	      {
12338 		if (template_class_depth (current_class_type) == 0)
12339 		  {
12340 		    decl = check_explicit_specialization
12341 		      (unqualified_id, decl, template_count,
12342 		       2 * funcdef_flag + 4);
12343 		    if (decl == error_mark_node)
12344 		      return error_mark_node;
12345 		  }
12346 
12347 		decl = do_friend (ctype, unqualified_id, decl,
12348 				  *attrlist, flags,
12349 				  funcdef_flag);
12350 		return decl;
12351 	      }
12352 	    else
12353 	      return error_mark_node;
12354 	  }
12355 
12356 	/* Structure field.  It may not be a function, except for C++.  */
12357 
12358 	if (decl == NULL_TREE)
12359 	  {
12360 	    if (staticp)
12361 	      {
12362 		/* C++ allows static class members.  All other work
12363 		   for this is done by grokfield.  */
12364 		decl = build_lang_decl_loc (declarator
12365 					    ? declarator->id_loc
12366 					    : input_location,
12367 					    VAR_DECL, unqualified_id, type);
12368 		set_linkage_for_static_data_member (decl);
12369 		if (concept_p)
12370 		    error ("static data member %qE declared %<concept%>",
12371 			   unqualified_id);
12372 		else if (constexpr_p && !initialized)
12373 		  {
12374 		    error ("%<constexpr%> static data member %qD must have an "
12375 			   "initializer", decl);
12376 		    constexpr_p = false;
12377 		  }
12378 
12379 		if (inlinep)
12380 		  mark_inline_variable (decl);
12381 
12382 		if (!DECL_VAR_DECLARED_INLINE_P (decl)
12383 		    && !(cxx_dialect >= cxx17 && constexpr_p))
12384 		  /* Even if there is an in-class initialization, DECL
12385 		     is considered undefined until an out-of-class
12386 		     definition is provided, unless this is an inline
12387 		     variable.  */
12388 		  DECL_EXTERNAL (decl) = 1;
12389 
12390 		if (thread_p)
12391 		  {
12392 		    CP_DECL_THREAD_LOCAL_P (decl) = true;
12393 		    if (!processing_template_decl)
12394 		      set_decl_tls_model (decl, decl_default_tls_model (decl));
12395 		    if (declspecs->gnu_thread_keyword_p)
12396 		      SET_DECL_GNU_TLS_P (decl);
12397 		  }
12398 	      }
12399 	    else
12400 	      {
12401 		if (concept_p)
12402 		  error ("non-static data member %qE declared %<concept%>",
12403 			 unqualified_id);
12404                 else if (constexpr_p)
12405 		  {
12406 		    error ("non-static data member %qE declared %<constexpr%>",
12407 			   unqualified_id);
12408 		    constexpr_p = false;
12409 		  }
12410 		decl = build_decl (input_location,
12411 				   FIELD_DECL, unqualified_id, type);
12412 		DECL_NONADDRESSABLE_P (decl) = bitfield;
12413 		if (bitfield && !unqualified_id)
12414 		  {
12415 		    TREE_NO_WARNING (decl) = 1;
12416 		    DECL_PADDING_P (decl) = 1;
12417 		  }
12418 
12419 		if (storage_class == sc_mutable)
12420 		  {
12421 		    DECL_MUTABLE_P (decl) = 1;
12422 		    storage_class = sc_none;
12423 		  }
12424 
12425 		if (initialized)
12426 		  {
12427 		    /* An attempt is being made to initialize a non-static
12428 		       member.  This is new in C++11.  */
12429 		    maybe_warn_cpp0x (CPP0X_NSDMI);
12430 
12431 		    /* If this has been parsed with static storage class, but
12432 		       errors forced staticp to be cleared, ensure NSDMI is
12433 		       not present.  */
12434 		    if (declspecs->storage_class == sc_static)
12435 		      DECL_INITIAL (decl) = error_mark_node;
12436 		  }
12437 	      }
12438 
12439 	    bad_specifiers (decl, BSP_FIELD, virtualp,
12440 			    memfn_quals != TYPE_UNQUALIFIED,
12441 			    staticp ? false : inlinep, friendp,
12442 			    raises != NULL_TREE);
12443 	  }
12444       }
12445     else if (TREE_CODE (type) == FUNCTION_TYPE
12446 	     || TREE_CODE (type) == METHOD_TYPE)
12447       {
12448 	tree original_name;
12449 	int publicp = 0;
12450 
12451 	if (!unqualified_id)
12452 	  return error_mark_node;
12453 
12454 	if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
12455 	  original_name = dname;
12456 	else
12457 	  original_name = unqualified_id;
12458 	// FIXME:gcc_assert (original_name == dname);
12459 
12460 	if (storage_class == sc_auto)
12461 	  error ("storage class %<auto%> invalid for function %qs", name);
12462 	else if (storage_class == sc_register)
12463 	  error ("storage class %<register%> invalid for function %qs", name);
12464 	else if (thread_p)
12465 	  {
12466 	    if (declspecs->gnu_thread_keyword_p)
12467 	      error ("storage class %<__thread%> invalid for function %qs",
12468 		     name);
12469 	    else
12470 	      error ("storage class %<thread_local%> invalid for function %qs",
12471 		     name);
12472 	  }
12473 
12474         if (virt_specifiers)
12475           error ("virt-specifiers in %qs not allowed outside a class definition", name);
12476 	/* Function declaration not at top level.
12477 	   Storage classes other than `extern' are not allowed
12478 	   and `extern' makes no difference.  */
12479 	if (! toplevel_bindings_p ()
12480 	    && (storage_class == sc_static
12481 		|| decl_spec_seq_has_spec_p (declspecs, ds_inline))
12482 	    && pedantic)
12483 	  {
12484 	    if (storage_class == sc_static)
12485 	      pedwarn (input_location, OPT_Wpedantic,
12486 		       "%<static%> specifier invalid for function %qs "
12487 		       "declared out of global scope", name);
12488 	    else
12489 	      pedwarn (input_location, OPT_Wpedantic,
12490 		       "%<inline%> specifier invalid for function %qs "
12491 		       "declared out of global scope", name);
12492 	  }
12493 
12494 	if (ctype == NULL_TREE)
12495 	  {
12496 	    if (virtualp)
12497 	      {
12498 		error ("virtual non-class function %qs", name);
12499 		virtualp = 0;
12500 	      }
12501 	    else if (sfk == sfk_constructor
12502 		     || sfk == sfk_destructor)
12503 	      {
12504 		error (funcdef_flag
12505 		       ? G_("%qs defined in a non-class scope")
12506 		       : G_("%qs declared in a non-class scope"), name);
12507 		sfk = sfk_none;
12508 	      }
12509 	  }
12510 
12511 	/* Record whether the function is public.  */
12512 	publicp = (ctype != NULL_TREE
12513 		   || storage_class != sc_static);
12514 
12515 	if (late_return_type_p)
12516 	  TYPE_HAS_LATE_RETURN_TYPE (type) = 1;
12517 
12518 	decl = grokfndecl (ctype, type, original_name, parms, unqualified_id,
12519                            reqs, virtualp, flags, memfn_quals, rqual, raises,
12520 			   1, friendp,
12521 			   publicp,
12522                            inlinep | (2 * constexpr_p) | (4 * concept_p),
12523 			   initialized == SD_DELETED,
12524                            sfk,
12525                            funcdef_flag,
12526 			   template_count, in_namespace, attrlist,
12527 			   declarator->id_loc);
12528 	if (decl == NULL_TREE)
12529 	  return error_mark_node;
12530 
12531 	if (explicitp == 2)
12532 	  DECL_NONCONVERTING_P (decl) = 1;
12533 	if (staticp == 1)
12534 	  {
12535 	    int invalid_static = 0;
12536 
12537 	    /* Don't allow a static member function in a class, and forbid
12538 	       declaring main to be static.  */
12539 	    if (TREE_CODE (type) == METHOD_TYPE)
12540 	      {
12541 		permerror (input_location, "cannot declare member function %qD to have "
12542 			   "static linkage", decl);
12543 		invalid_static = 1;
12544 	      }
12545 	    else if (current_function_decl)
12546 	      {
12547 		/* 7.1.1: There can be no static function declarations within a
12548 		   block.  */
12549 		error ("cannot declare static function inside another function");
12550 		invalid_static = 1;
12551 	      }
12552 
12553 	    if (invalid_static)
12554 	      {
12555 		staticp = 0;
12556 		storage_class = sc_none;
12557 	      }
12558 	  }
12559       }
12560     else
12561       {
12562 	/* It's a variable.  */
12563 
12564 	/* An uninitialized decl with `extern' is a reference.  */
12565 	decl = grokvardecl (type, dname, unqualified_id,
12566 			    declspecs,
12567 			    initialized,
12568 			    type_quals,
12569 			    inlinep,
12570 			    concept_p,
12571 			    template_count,
12572 			    ctype ? ctype : in_namespace);
12573 	if (decl == NULL_TREE)
12574 	  return error_mark_node;
12575 
12576 	bad_specifiers (decl, BSP_VAR, virtualp,
12577 			memfn_quals != TYPE_UNQUALIFIED,
12578 			inlinep, friendp, raises != NULL_TREE);
12579 
12580 	if (ctype)
12581 	  {
12582 	    DECL_CONTEXT (decl) = ctype;
12583 	    if (staticp == 1)
12584 	      {
12585 		permerror (input_location, "%<static%> may not be used when defining "
12586 			   "(as opposed to declaring) a static data member");
12587 		staticp = 0;
12588 		storage_class = sc_none;
12589 	      }
12590 	    if (storage_class == sc_register && TREE_STATIC (decl))
12591 	      {
12592 		error ("static member %qD declared %<register%>", decl);
12593 		storage_class = sc_none;
12594 	      }
12595 	    if (storage_class == sc_extern && pedantic)
12596 	      {
12597 		pedwarn (input_location, OPT_Wpedantic,
12598 			 "cannot explicitly declare member %q#D to have "
12599 			 "extern linkage", decl);
12600 		storage_class = sc_none;
12601 	      }
12602 	  }
12603 	else if (constexpr_p && DECL_EXTERNAL (decl))
12604 	  {
12605 	    error ("declaration of %<constexpr%> variable %qD "
12606 		   "is not a definition", decl);
12607 	    constexpr_p = false;
12608 	  }
12609 
12610 	if (inlinep)
12611 	  mark_inline_variable (decl);
12612 	if (innermost_code == cdk_decomp)
12613 	  {
12614 	    gcc_assert (declarator && declarator->kind == cdk_decomp);
12615 	    DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
12616 	    DECL_ARTIFICIAL (decl) = 1;
12617 	    fit_decomposition_lang_decl (decl, NULL_TREE);
12618 	  }
12619       }
12620 
12621     if (VAR_P (decl) && !initialized)
12622       if (tree auto_node = type_uses_auto (type))
12623 	if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node))
12624 	  {
12625 	    location_t loc = declspecs->locations[ds_type_spec];
12626 	    error_at (loc, "declaration of %q#D has no initializer", decl);
12627 	    TREE_TYPE (decl) = error_mark_node;
12628 	  }
12629 
12630     if (storage_class == sc_extern && initialized && !funcdef_flag)
12631       {
12632 	if (toplevel_bindings_p ())
12633 	  {
12634 	    /* It's common practice (and completely valid) to have a const
12635 	       be initialized and declared extern.  */
12636 	    if (!(type_quals & TYPE_QUAL_CONST))
12637 	      warning (0, "%qs initialized and declared %<extern%>", name);
12638 	  }
12639 	else
12640 	  {
12641 	    error ("%qs has both %<extern%> and initializer", name);
12642 	    return error_mark_node;
12643 	  }
12644       }
12645 
12646     /* Record `register' declaration for warnings on &
12647        and in case doing stupid register allocation.  */
12648 
12649     if (storage_class == sc_register)
12650       {
12651 	DECL_REGISTER (decl) = 1;
12652 	/* Warn about register storage specifiers on PARM_DECLs.  */
12653 	if (TREE_CODE (decl) == PARM_DECL)
12654 	  {
12655 	    if (cxx_dialect >= cxx17)
12656 	      pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
12657 		       "ISO C++17 does not allow %<register%> storage "
12658 		       "class specifier");
12659 	    else
12660 	      warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
12661 			  "%<register%> storage class specifier used");
12662 	  }
12663       }
12664     else if (storage_class == sc_extern)
12665       DECL_THIS_EXTERN (decl) = 1;
12666     else if (storage_class == sc_static)
12667       DECL_THIS_STATIC (decl) = 1;
12668 
12669     /* Set constexpr flag on vars (functions got it in grokfndecl).  */
12670     if (constexpr_p && VAR_P (decl))
12671       DECL_DECLARED_CONSTEXPR_P (decl) = true;
12672 
12673     /* Record constancy and volatility on the DECL itself .  There's
12674        no need to do this when processing a template; we'll do this
12675        for the instantiated declaration based on the type of DECL.  */
12676     if (!processing_template_decl)
12677       cp_apply_type_quals_to_decl (type_quals, decl);
12678 
12679     return decl;
12680   }
12681 }
12682 
12683 /* Subroutine of start_function.  Ensure that each of the parameter
12684    types (as listed in PARMS) is complete, as is required for a
12685    function definition.  */
12686 
12687 static void
require_complete_types_for_parms(tree parms)12688 require_complete_types_for_parms (tree parms)
12689 {
12690   for (; parms; parms = DECL_CHAIN (parms))
12691     {
12692       if (dependent_type_p (TREE_TYPE (parms)))
12693 	continue;
12694       if (!VOID_TYPE_P (TREE_TYPE (parms))
12695 	  && complete_type_or_else (TREE_TYPE (parms), parms))
12696 	{
12697 	  relayout_decl (parms);
12698 	  DECL_ARG_TYPE (parms) = type_passed_as (TREE_TYPE (parms));
12699 
12700 	  maybe_warn_parm_abi (TREE_TYPE (parms),
12701 			       DECL_SOURCE_LOCATION (parms));
12702 	}
12703       else
12704 	/* grokparms or complete_type_or_else will have already issued
12705 	   an error.  */
12706 	TREE_TYPE (parms) = error_mark_node;
12707     }
12708 }
12709 
12710 /* Returns nonzero if T is a local variable.  */
12711 
12712 int
local_variable_p(const_tree t)12713 local_variable_p (const_tree t)
12714 {
12715   if ((VAR_P (t)
12716        /* A VAR_DECL with a context that is a _TYPE is a static data
12717 	  member.  */
12718        && !TYPE_P (CP_DECL_CONTEXT (t))
12719        /* Any other non-local variable must be at namespace scope.  */
12720        && !DECL_NAMESPACE_SCOPE_P (t))
12721       || (TREE_CODE (t) == PARM_DECL))
12722     return 1;
12723 
12724   return 0;
12725 }
12726 
12727 /* Like local_variable_p, but suitable for use as a tree-walking
12728    function.  */
12729 
12730 static tree
local_variable_p_walkfn(tree * tp,int * walk_subtrees,void *)12731 local_variable_p_walkfn (tree *tp, int *walk_subtrees,
12732 			 void * /*data*/)
12733 {
12734   if (local_variable_p (*tp)
12735       && (!DECL_ARTIFICIAL (*tp) || DECL_NAME (*tp) == this_identifier))
12736     return *tp;
12737   else if (TYPE_P (*tp))
12738     *walk_subtrees = 0;
12739 
12740   return NULL_TREE;
12741 }
12742 
12743 /* Check that ARG, which is a default-argument expression for a
12744    parameter DECL, is valid.  Returns ARG, or ERROR_MARK_NODE, if
12745    something goes wrong.  DECL may also be a _TYPE node, rather than a
12746    DECL, if there is no DECL available.  */
12747 
12748 tree
check_default_argument(tree decl,tree arg,tsubst_flags_t complain)12749 check_default_argument (tree decl, tree arg, tsubst_flags_t complain)
12750 {
12751   tree var;
12752   tree decl_type;
12753 
12754   if (TREE_CODE (arg) == DEFAULT_ARG)
12755     /* We get a DEFAULT_ARG when looking at an in-class declaration
12756        with a default argument.  Ignore the argument for now; we'll
12757        deal with it after the class is complete.  */
12758     return arg;
12759 
12760   if (TYPE_P (decl))
12761     {
12762       decl_type = decl;
12763       decl = NULL_TREE;
12764     }
12765   else
12766     decl_type = TREE_TYPE (decl);
12767 
12768   if (arg == error_mark_node
12769       || decl == error_mark_node
12770       || TREE_TYPE (arg) == error_mark_node
12771       || decl_type == error_mark_node)
12772     /* Something already went wrong.  There's no need to check
12773        further.  */
12774     return error_mark_node;
12775 
12776   /* [dcl.fct.default]
12777 
12778      A default argument expression is implicitly converted to the
12779      parameter type.  */
12780   ++cp_unevaluated_operand;
12781   /* Avoid digest_init clobbering the initializer.  */
12782   tree carg = BRACE_ENCLOSED_INITIALIZER_P (arg) ? unshare_expr (arg): arg;
12783   perform_implicit_conversion_flags (decl_type, carg, complain,
12784 				     LOOKUP_IMPLICIT);
12785   --cp_unevaluated_operand;
12786 
12787   /* Avoid redundant -Wzero-as-null-pointer-constant warnings at
12788      the call sites.  */
12789   if (TYPE_PTR_OR_PTRMEM_P (decl_type)
12790       && null_ptr_cst_p (arg))
12791     return nullptr_node;
12792 
12793   /* [dcl.fct.default]
12794 
12795      Local variables shall not be used in default argument
12796      expressions.
12797 
12798      The keyword `this' shall not be used in a default argument of a
12799      member function.  */
12800   var = cp_walk_tree_without_duplicates (&arg, local_variable_p_walkfn, NULL);
12801   if (var)
12802     {
12803       if (complain & tf_warning_or_error)
12804 	{
12805 	  if (DECL_NAME (var) == this_identifier)
12806 	    permerror (input_location, "default argument %qE uses %qD",
12807 		       arg, var);
12808 	  else
12809 	    error ("default argument %qE uses local variable %qD", arg, var);
12810 	}
12811       return error_mark_node;
12812     }
12813 
12814   /* All is well.  */
12815   return arg;
12816 }
12817 
12818 /* Returns a deprecated type used within TYPE, or NULL_TREE if none.  */
12819 
12820 static tree
type_is_deprecated(tree type)12821 type_is_deprecated (tree type)
12822 {
12823   enum tree_code code;
12824   if (TREE_DEPRECATED (type))
12825     return type;
12826   if (TYPE_NAME (type))
12827     {
12828       if (TREE_DEPRECATED (TYPE_NAME (type)))
12829 	return type;
12830       else
12831 	return NULL_TREE;
12832     }
12833 
12834   /* Do warn about using typedefs to a deprecated class.  */
12835   if (OVERLOAD_TYPE_P (type) && type != TYPE_MAIN_VARIANT (type))
12836     return type_is_deprecated (TYPE_MAIN_VARIANT (type));
12837 
12838   code = TREE_CODE (type);
12839 
12840   if (code == POINTER_TYPE || code == REFERENCE_TYPE
12841       || code == OFFSET_TYPE || code == FUNCTION_TYPE
12842       || code == METHOD_TYPE || code == ARRAY_TYPE)
12843     return type_is_deprecated (TREE_TYPE (type));
12844 
12845   if (TYPE_PTRMEMFUNC_P (type))
12846     return type_is_deprecated
12847       (TREE_TYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type))));
12848 
12849   return NULL_TREE;
12850 }
12851 
12852 /* Decode the list of parameter types for a function type.
12853    Given the list of things declared inside the parens,
12854    return a list of types.
12855 
12856    If this parameter does not end with an ellipsis, we append
12857    void_list_node.
12858 
12859    *PARMS is set to the chain of PARM_DECLs created.  */
12860 
12861 tree
grokparms(tree parmlist,tree * parms)12862 grokparms (tree parmlist, tree *parms)
12863 {
12864   tree result = NULL_TREE;
12865   tree decls = NULL_TREE;
12866   tree parm;
12867   int any_error = 0;
12868 
12869   for (parm = parmlist; parm != NULL_TREE; parm = TREE_CHAIN (parm))
12870     {
12871       tree type = NULL_TREE;
12872       tree init = TREE_PURPOSE (parm);
12873       tree decl = TREE_VALUE (parm);
12874 
12875       if (parm == void_list_node)
12876 	break;
12877 
12878       if (! decl || TREE_TYPE (decl) == error_mark_node)
12879 	continue;
12880 
12881       type = TREE_TYPE (decl);
12882       if (VOID_TYPE_P (type))
12883 	{
12884 	  if (same_type_p (type, void_type_node)
12885 	      && !init
12886 	      && !DECL_NAME (decl) && !result
12887 	      && TREE_CHAIN (parm) == void_list_node)
12888 	    /* DR 577: A parameter list consisting of a single
12889 	       unnamed parameter of non-dependent type 'void'.  */
12890 	    break;
12891 	  else if (cv_qualified_p (type))
12892 	    error_at (DECL_SOURCE_LOCATION (decl),
12893 		      "invalid use of cv-qualified type %qT in "
12894 		      "parameter declaration", type);
12895 	  else
12896 	    error_at (DECL_SOURCE_LOCATION (decl),
12897 		      "invalid use of type %<void%> in parameter "
12898 		      "declaration");
12899 	  /* It's not a good idea to actually create parameters of
12900 	     type `void'; other parts of the compiler assume that a
12901 	     void type terminates the parameter list.  */
12902 	  type = error_mark_node;
12903 	  TREE_TYPE (decl) = error_mark_node;
12904 	}
12905 
12906       if (type != error_mark_node)
12907 	{
12908 	  if (deprecated_state != DEPRECATED_SUPPRESS)
12909 	    {
12910 	      tree deptype = type_is_deprecated (type);
12911 	      if (deptype)
12912 		cp_warn_deprecated_use (deptype);
12913 	    }
12914 
12915 	  /* Top-level qualifiers on the parameters are
12916 	     ignored for function types.  */
12917 	  type = cp_build_qualified_type (type, 0);
12918 	  if (TREE_CODE (type) == METHOD_TYPE)
12919 	    {
12920 	      error ("parameter %qD invalidly declared method type", decl);
12921 	      type = build_pointer_type (type);
12922 	      TREE_TYPE (decl) = type;
12923 	    }
12924 	  else if (abstract_virtuals_error (decl, type))
12925 	    any_error = 1;  /* Seems like a good idea.  */
12926 	  else if (cxx_dialect < cxx17 && POINTER_TYPE_P (type))
12927 	    {
12928 	      /* Before C++17 DR 393:
12929 		 [dcl.fct]/6, parameter types cannot contain pointers
12930 		 (references) to arrays of unknown bound.  */
12931 	      tree t = TREE_TYPE (type);
12932 	      int ptr = TYPE_PTR_P (type);
12933 
12934 	      while (1)
12935 		{
12936 		  if (TYPE_PTR_P (t))
12937 		    ptr = 1;
12938 		  else if (TREE_CODE (t) != ARRAY_TYPE)
12939 		    break;
12940 		  else if (!TYPE_DOMAIN (t))
12941 		    break;
12942 		  t = TREE_TYPE (t);
12943 		}
12944 	      if (TREE_CODE (t) == ARRAY_TYPE)
12945 		pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
12946 			 ptr
12947 			 ? G_("parameter %qD includes pointer to array of "
12948 			      "unknown bound %qT")
12949 			 : G_("parameter %qD includes reference to array of "
12950 			      "unknown bound %qT"),
12951 			 decl, t);
12952 	    }
12953 
12954 	  if (any_error)
12955 	    init = NULL_TREE;
12956 	  else if (init && !processing_template_decl)
12957 	    init = check_default_argument (decl, init, tf_warning_or_error);
12958 	}
12959 
12960       DECL_CHAIN (decl) = decls;
12961       decls = decl;
12962       result = tree_cons (init, type, result);
12963     }
12964   decls = nreverse (decls);
12965   result = nreverse (result);
12966   if (parm)
12967     result = chainon (result, void_list_node);
12968   *parms = decls;
12969 
12970   return result;
12971 }
12972 
12973 
12974 /* D is a constructor or overloaded `operator='.
12975 
12976    Let T be the class in which D is declared. Then, this function
12977    returns:
12978 
12979    -1 if D's is an ill-formed constructor or copy assignment operator
12980       whose first parameter is of type `T'.
12981    0  if D is not a copy constructor or copy assignment
12982       operator.
12983    1  if D is a copy constructor or copy assignment operator whose
12984       first parameter is a reference to non-const qualified T.
12985    2  if D is a copy constructor or copy assignment operator whose
12986       first parameter is a reference to const qualified T.
12987 
12988    This function can be used as a predicate. Positive values indicate
12989    a copy constructor and nonzero values indicate a copy assignment
12990    operator.  */
12991 
12992 int
copy_fn_p(const_tree d)12993 copy_fn_p (const_tree d)
12994 {
12995   tree args;
12996   tree arg_type;
12997   int result = 1;
12998 
12999   gcc_assert (DECL_FUNCTION_MEMBER_P (d));
13000 
13001   if (TREE_CODE (d) == TEMPLATE_DECL
13002       || (DECL_TEMPLATE_INFO (d)
13003 	  && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
13004     /* Instantiations of template member functions are never copy
13005        functions.  Note that member functions of templated classes are
13006        represented as template functions internally, and we must
13007        accept those as copy functions.  */
13008     return 0;
13009 
13010   args = FUNCTION_FIRST_USER_PARMTYPE (d);
13011   if (!args)
13012     return 0;
13013 
13014   arg_type = TREE_VALUE (args);
13015   if (arg_type == error_mark_node)
13016     return 0;
13017 
13018   if (TYPE_MAIN_VARIANT (arg_type) == DECL_CONTEXT (d))
13019     {
13020       /* Pass by value copy assignment operator.  */
13021       result = -1;
13022     }
13023   else if (TREE_CODE (arg_type) == REFERENCE_TYPE
13024 	   && !TYPE_REF_IS_RVALUE (arg_type)
13025 	   && TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)) == DECL_CONTEXT (d))
13026     {
13027       if (CP_TYPE_CONST_P (TREE_TYPE (arg_type)))
13028 	result = 2;
13029     }
13030   else
13031     return 0;
13032 
13033   args = TREE_CHAIN (args);
13034 
13035   if (args && args != void_list_node && !TREE_PURPOSE (args))
13036     /* There are more non-optional args.  */
13037     return 0;
13038 
13039   return result;
13040 }
13041 
13042 /* D is a constructor or overloaded `operator='.
13043 
13044    Let T be the class in which D is declared. Then, this function
13045    returns true when D is a move constructor or move assignment
13046    operator, false otherwise.  */
13047 
13048 bool
move_fn_p(const_tree d)13049 move_fn_p (const_tree d)
13050 {
13051   gcc_assert (DECL_FUNCTION_MEMBER_P (d));
13052 
13053   if (cxx_dialect == cxx98)
13054     /* There are no move constructors if we are in C++98 mode.  */
13055     return false;
13056 
13057   if (TREE_CODE (d) == TEMPLATE_DECL
13058       || (DECL_TEMPLATE_INFO (d)
13059          && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
13060     /* Instantiations of template member functions are never move
13061        functions.  Note that member functions of templated classes are
13062        represented as template functions internally, and we must
13063        accept those as move functions.  */
13064     return 0;
13065 
13066   return move_signature_fn_p (d);
13067 }
13068 
13069 /* D is a constructor or overloaded `operator='.
13070 
13071    Then, this function returns true when D has the same signature as a move
13072    constructor or move assignment operator (because either it is such a
13073    ctor/op= or it is a template specialization with the same signature),
13074    false otherwise.  */
13075 
13076 bool
move_signature_fn_p(const_tree d)13077 move_signature_fn_p (const_tree d)
13078 {
13079   tree args;
13080   tree arg_type;
13081   bool result = false;
13082 
13083   args = FUNCTION_FIRST_USER_PARMTYPE (d);
13084   if (!args)
13085     return 0;
13086 
13087   arg_type = TREE_VALUE (args);
13088   if (arg_type == error_mark_node)
13089     return 0;
13090 
13091   if (TREE_CODE (arg_type) == REFERENCE_TYPE
13092       && TYPE_REF_IS_RVALUE (arg_type)
13093       && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)),
13094                       DECL_CONTEXT (d)))
13095     result = true;
13096 
13097   args = TREE_CHAIN (args);
13098 
13099   if (args && args != void_list_node && !TREE_PURPOSE (args))
13100     /* There are more non-optional args.  */
13101     return false;
13102 
13103   return result;
13104 }
13105 
13106 /* Remember any special properties of member function DECL.  */
13107 
13108 void
grok_special_member_properties(tree decl)13109 grok_special_member_properties (tree decl)
13110 {
13111   tree class_type;
13112 
13113   if (!DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
13114     return;
13115 
13116   class_type = DECL_CONTEXT (decl);
13117   if (IDENTIFIER_CTOR_P (DECL_NAME (decl)))
13118     {
13119       int ctor = copy_fn_p (decl);
13120 
13121       if (!DECL_ARTIFICIAL (decl))
13122 	TYPE_HAS_USER_CONSTRUCTOR (class_type) = 1;
13123 
13124       if (ctor > 0)
13125 	{
13126 	  /* [class.copy]
13127 
13128 	     A non-template constructor for class X is a copy
13129 	     constructor if its first parameter is of type X&, const
13130 	     X&, volatile X& or const volatile X&, and either there
13131 	     are no other parameters or else all other parameters have
13132 	     default arguments.  */
13133 	  TYPE_HAS_COPY_CTOR (class_type) = 1;
13134 	  if (user_provided_p (decl))
13135 	    TYPE_HAS_COMPLEX_COPY_CTOR (class_type) = 1;
13136 	  if (ctor > 1)
13137 	    TYPE_HAS_CONST_COPY_CTOR (class_type) = 1;
13138 	}
13139       else if (sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (decl)))
13140 	TYPE_HAS_DEFAULT_CONSTRUCTOR (class_type) = 1;
13141       else if (move_fn_p (decl) && user_provided_p (decl))
13142 	TYPE_HAS_COMPLEX_MOVE_CTOR (class_type) = 1;
13143       else if (is_list_ctor (decl))
13144 	TYPE_HAS_LIST_CTOR (class_type) = 1;
13145 
13146       if (DECL_DECLARED_CONSTEXPR_P (decl)
13147 	  && !ctor && !move_fn_p (decl))
13148 	TYPE_HAS_CONSTEXPR_CTOR (class_type) = 1;
13149     }
13150   else if (DECL_NAME (decl) == assign_op_identifier)
13151     {
13152       /* [class.copy]
13153 
13154 	 A non-template assignment operator for class X is a copy
13155 	 assignment operator if its parameter is of type X, X&, const
13156 	 X&, volatile X& or const volatile X&.  */
13157 
13158       int assop = copy_fn_p (decl);
13159 
13160       if (assop)
13161 	{
13162 	  TYPE_HAS_COPY_ASSIGN (class_type) = 1;
13163 	  if (user_provided_p (decl))
13164 	    TYPE_HAS_COMPLEX_COPY_ASSIGN (class_type) = 1;
13165 	  if (assop != 1)
13166 	    TYPE_HAS_CONST_COPY_ASSIGN (class_type) = 1;
13167 	}
13168       else if (move_fn_p (decl) && user_provided_p (decl))
13169 	TYPE_HAS_COMPLEX_MOVE_ASSIGN (class_type) = 1;
13170     }
13171   else if (IDENTIFIER_CONV_OP_P (DECL_NAME (decl)))
13172     TYPE_HAS_CONVERSION (class_type) = true;
13173 
13174   /* Destructors are handled in check_methods.  */
13175 }
13176 
13177 /* Check a constructor DECL has the correct form.  Complains
13178    if the class has a constructor of the form X(X).  */
13179 
13180 bool
grok_ctor_properties(const_tree ctype,const_tree decl)13181 grok_ctor_properties (const_tree ctype, const_tree decl)
13182 {
13183   int ctor_parm = copy_fn_p (decl);
13184 
13185   if (ctor_parm < 0)
13186     {
13187       /* [class.copy]
13188 
13189 	 A declaration of a constructor for a class X is ill-formed if
13190 	 its first parameter is of type (optionally cv-qualified) X
13191 	 and either there are no other parameters or else all other
13192 	 parameters have default arguments.
13193 
13194 	 We *don't* complain about member template instantiations that
13195 	 have this form, though; they can occur as we try to decide
13196 	 what constructor to use during overload resolution.  Since
13197 	 overload resolution will never prefer such a constructor to
13198 	 the non-template copy constructor (which is either explicitly
13199 	 or implicitly defined), there's no need to worry about their
13200 	 existence.  Theoretically, they should never even be
13201 	 instantiated, but that's hard to forestall.  */
13202       error ("invalid constructor; you probably meant %<%T (const %T&)%>",
13203 		ctype, ctype);
13204       return false;
13205     }
13206 
13207   return true;
13208 }
13209 
13210 /* DECL is a declaration for an overloaded or conversion operator.  If
13211    COMPLAIN is true, errors are issued for invalid declarations.  */
13212 
13213 bool
grok_op_properties(tree decl,bool complain)13214 grok_op_properties (tree decl, bool complain)
13215 {
13216   tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
13217   bool methodp = TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE;
13218   tree name = DECL_NAME (decl);
13219 
13220   tree class_type = DECL_CONTEXT (decl);
13221   if (class_type && !CLASS_TYPE_P (class_type))
13222     class_type = NULL_TREE;
13223 
13224   tree_code operator_code;
13225   unsigned op_flags;
13226   if (IDENTIFIER_CONV_OP_P (name))
13227     {
13228       /* Conversion operators are TYPE_EXPR for the purposes of this
13229 	 function.  */
13230       operator_code = TYPE_EXPR;
13231       op_flags = OVL_OP_FLAG_UNARY;
13232     }
13233   else
13234     {
13235       const ovl_op_info_t *ovl_op = IDENTIFIER_OVL_OP_INFO (name);
13236 
13237       operator_code = ovl_op->tree_code;
13238       op_flags = ovl_op->flags;
13239       gcc_checking_assert (operator_code != ERROR_MARK);
13240       DECL_OVERLOADED_OPERATOR_CODE_RAW (decl) = ovl_op->ovl_op_code;
13241     }
13242 
13243   if (op_flags & OVL_OP_FLAG_ALLOC)
13244     {
13245       /* operator new and operator delete are quite special.  */
13246       if (class_type)
13247 	switch (op_flags)
13248 	  {
13249 	  case OVL_OP_FLAG_ALLOC:
13250 	    TYPE_HAS_NEW_OPERATOR (class_type) = 1;
13251 	    break;
13252 
13253 	  case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_DELETE:
13254 	    TYPE_GETS_DELETE (class_type) |= 1;
13255 	    break;
13256 
13257 	  case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_VEC:
13258 	    TYPE_HAS_ARRAY_NEW_OPERATOR (class_type) = 1;
13259 	    break;
13260 
13261 	  case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_DELETE | OVL_OP_FLAG_VEC:
13262 	    TYPE_GETS_DELETE (class_type) |= 2;
13263 	    break;
13264 
13265 	  default:
13266 	    gcc_unreachable ();
13267 	  }
13268 
13269       /* [basic.std.dynamic.allocation]/1:
13270 
13271 	 A program is ill-formed if an allocation function is declared
13272 	 in a namespace scope other than global scope or declared
13273 	 static in global scope.
13274 
13275 	 The same also holds true for deallocation functions.  */
13276       if (DECL_NAMESPACE_SCOPE_P (decl))
13277 	{
13278 	  if (CP_DECL_CONTEXT (decl) != global_namespace)
13279 	    {
13280 	      error ("%qD may not be declared within a namespace", decl);
13281 	      return false;
13282 	    }
13283 
13284 	  if (!TREE_PUBLIC (decl))
13285 	    {
13286 	      error ("%qD may not be declared as static", decl);
13287 	      return false;
13288 	    }
13289 	}
13290 
13291       if (op_flags & OVL_OP_FLAG_DELETE)
13292 	TREE_TYPE (decl) = coerce_delete_type (TREE_TYPE (decl));
13293       else
13294 	{
13295 	  DECL_IS_OPERATOR_NEW (decl) = 1;
13296 	  TREE_TYPE (decl) = coerce_new_type (TREE_TYPE (decl));
13297 	}
13298 
13299       return true;
13300     }
13301 
13302   /* An operator function must either be a non-static member function
13303      or have at least one parameter of a class, a reference to a class,
13304      an enumeration, or a reference to an enumeration.  13.4.0.6 */
13305   if (! methodp || DECL_STATIC_FUNCTION_P (decl))
13306     {
13307       if (operator_code == TYPE_EXPR
13308 	  || operator_code == CALL_EXPR
13309 	  || operator_code == COMPONENT_REF
13310 	  || operator_code == ARRAY_REF
13311 	  || operator_code == NOP_EXPR)
13312 	{
13313 	  error ("%qD must be a nonstatic member function", decl);
13314 	  return false;
13315 	}
13316 
13317       if (DECL_STATIC_FUNCTION_P (decl))
13318 	{
13319 	  error ("%qD must be either a non-static member "
13320 		 "function or a non-member function", decl);
13321 	  return false;
13322 	}
13323 
13324       for (tree arg = argtypes; ; arg = TREE_CHAIN (arg))
13325 	{
13326 	  if (!arg || arg == void_list_node)
13327 	    {
13328 	      if (complain)
13329 		error ("%qD must have an argument of class or "
13330 		       "enumerated type", decl);
13331 	      return false;
13332 	    }
13333 
13334 	  tree type = non_reference (TREE_VALUE (arg));
13335 	  if (type == error_mark_node)
13336 	    return false;
13337 
13338 	  /* MAYBE_CLASS_TYPE_P, rather than CLASS_TYPE_P, is used
13339 	     because these checks are performed even on template
13340 	     functions.  */
13341 	  if (MAYBE_CLASS_TYPE_P (type)
13342 	      || TREE_CODE (type) == ENUMERAL_TYPE)
13343 	    break;
13344 	}
13345     }
13346 
13347   if (operator_code == CALL_EXPR)
13348     /* There are no further restrictions on the arguments to an overloaded
13349        "operator ()".  */
13350     return true;
13351 
13352   if (operator_code == COND_EXPR)
13353     {
13354       /* 13.4.0.3 */
13355       error ("ISO C++ prohibits overloading operator ?:");
13356       return false;
13357     }
13358 
13359   /* Count the number of arguments and check for ellipsis.  */
13360   int arity = 0;
13361   for (tree arg = argtypes; arg != void_list_node; arg = TREE_CHAIN (arg))
13362     {
13363       if (!arg)
13364 	{
13365 	  /* Variadic.  */
13366 	  error ("%qD must not have variable number of arguments", decl);
13367 	  return false;
13368 	}
13369       ++arity;
13370     }
13371 
13372   /* Verify correct number of arguments.  */
13373   switch (op_flags)
13374     {
13375     case OVL_OP_FLAG_AMBIARY:
13376       if (arity == 1)
13377 	{
13378 	  /* We have a unary instance of an ambi-ary op.  Remap to the
13379 	     unary one.  */
13380 	  unsigned alt = ovl_op_alternate[ovl_op_mapping [operator_code]];
13381 	  const ovl_op_info_t *ovl_op = &ovl_op_info[false][alt];
13382 	  gcc_checking_assert (ovl_op->flags == OVL_OP_FLAG_UNARY);
13383 	  operator_code = ovl_op->tree_code;
13384 	  DECL_OVERLOADED_OPERATOR_CODE_RAW (decl) = ovl_op->ovl_op_code;
13385 	}
13386       else if (arity != 2)
13387 	{
13388 	  /* This was an ambiguous operator but is invalid. */
13389 	  error (methodp
13390 		 ? G_("%qD must have either zero or one argument")
13391 		 : G_("%qD must have either one or two arguments"), decl);
13392 	  return false;
13393 	}
13394       else if ((operator_code == POSTINCREMENT_EXPR
13395 		|| operator_code == POSTDECREMENT_EXPR)
13396 	       && ! processing_template_decl
13397 	       /* x++ and x--'s second argument must be an int.  */
13398 	       && ! same_type_p (TREE_VALUE (TREE_CHAIN (argtypes)),
13399 				 integer_type_node))
13400 	{
13401 	  error (methodp
13402 		 ? G_("postfix %qD must have %<int%> as its argument")
13403 		 : G_("postfix %qD must have %<int%> as its second argument"),
13404 		 decl);
13405 	  return false;
13406 	}
13407       break;
13408 
13409     case OVL_OP_FLAG_UNARY:
13410       if (arity != 1)
13411 	{
13412 	  error (methodp
13413 		 ? G_("%qD must have no arguments")
13414 		 : G_("%qD must have exactly one argument"), decl);
13415 	  return false;
13416 	}
13417       break;
13418 
13419     case OVL_OP_FLAG_BINARY:
13420       if (arity != 2)
13421 	{
13422 	  error (methodp
13423 		 ? G_("%qD must have exactly one argument")
13424 		 : G_("%qD must have exactly two arguments"), decl);
13425 	  return false;
13426 	}
13427       break;
13428 
13429     default:
13430       gcc_unreachable ();
13431     }
13432 
13433   /* There can be no default arguments.  */
13434   for (tree arg = argtypes; arg != void_list_node; arg = TREE_CHAIN (arg))
13435     if (TREE_PURPOSE (arg))
13436       {
13437 	TREE_PURPOSE (arg) = NULL_TREE;
13438 	if (operator_code == POSTINCREMENT_EXPR
13439 	    || operator_code == POSTDECREMENT_EXPR)
13440 	  pedwarn (input_location, OPT_Wpedantic,
13441 		   "%qD cannot have default arguments", decl);
13442 	else
13443 	  {
13444 	    error ("%qD cannot have default arguments", decl);
13445 	    return false;
13446 	  }
13447       }
13448 
13449   /* At this point the declaration is well-formed.  It may not be
13450      sensible though.  */
13451 
13452   /* Check member function warnings only on the in-class declaration.
13453      There's no point warning on an out-of-class definition.  */
13454   if (class_type && class_type != current_class_type)
13455     return true;
13456 
13457   /* Warn about conversion operators that will never be used.  */
13458   if (IDENTIFIER_CONV_OP_P (name)
13459       && ! DECL_TEMPLATE_INFO (decl)
13460       && warn_conversion)
13461     {
13462       tree t = TREE_TYPE (name);
13463       int ref = (TREE_CODE (t) == REFERENCE_TYPE);
13464 
13465       if (ref)
13466 	t = TYPE_MAIN_VARIANT (TREE_TYPE (t));
13467 
13468       if (VOID_TYPE_P (t))
13469 	warning (OPT_Wconversion,
13470 		 ref
13471 		 ? G_("conversion to a reference to void "
13472 		      "will never use a type conversion operator")
13473 		 : G_("conversion to void "
13474 		      "will never use a type conversion operator"));
13475       else if (class_type)
13476 	{
13477 	  if (t == class_type)
13478 	    warning (OPT_Wconversion,
13479                      ref
13480                      ? G_("conversion to a reference to the same type "
13481                           "will never use a type conversion operator")
13482                      : G_("conversion to the same type "
13483                           "will never use a type conversion operator"));
13484 	  /* Don't force t to be complete here.  */
13485 	  else if (MAYBE_CLASS_TYPE_P (t)
13486 		   && COMPLETE_TYPE_P (t)
13487 		   && DERIVED_FROM_P (t, class_type))
13488 	    warning (OPT_Wconversion,
13489 		     ref
13490 		     ? G_("conversion to a reference to a base class "
13491 			  "will never use a type conversion operator")
13492 		     : G_("conversion to a base class "
13493 			  "will never use a type conversion operator"));
13494 	}
13495     }
13496 
13497   if (!warn_ecpp)
13498     return true;
13499 
13500   /* Effective C++ rules below.  */
13501 
13502   /* More Effective C++ rule 7.  */
13503   if (operator_code == TRUTH_ANDIF_EXPR
13504       || operator_code == TRUTH_ORIF_EXPR
13505       || operator_code == COMPOUND_EXPR)
13506     warning (OPT_Weffc__,
13507 	     "user-defined %qD always evaluates both arguments", decl);
13508 
13509   /* More Effective C++ rule 6.  */
13510   if (operator_code == POSTINCREMENT_EXPR
13511       || operator_code == POSTDECREMENT_EXPR
13512       || operator_code == PREINCREMENT_EXPR
13513       || operator_code == PREDECREMENT_EXPR)
13514     {
13515       tree arg = TREE_VALUE (argtypes);
13516       tree ret = TREE_TYPE (TREE_TYPE (decl));
13517       if (methodp || TREE_CODE (arg) == REFERENCE_TYPE)
13518 	arg = TREE_TYPE (arg);
13519       arg = TYPE_MAIN_VARIANT (arg);
13520 
13521       if (operator_code == PREINCREMENT_EXPR
13522 	  || operator_code == PREDECREMENT_EXPR)
13523 	{
13524 	  if (TREE_CODE (ret) != REFERENCE_TYPE
13525 	      || !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (ret)), arg))
13526 	    warning (OPT_Weffc__, "prefix %qD should return %qT", decl,
13527 		     build_reference_type (arg));
13528 	}
13529       else
13530 	{
13531 	  if (!same_type_p (TYPE_MAIN_VARIANT (ret), arg))
13532 	    warning (OPT_Weffc__, "postfix %qD should return %qT", decl, arg);
13533 	}
13534     }
13535 
13536   /* Effective C++ rule 23.  */
13537   if (!DECL_ASSIGNMENT_OPERATOR_P (decl)
13538       && (operator_code == PLUS_EXPR
13539 	  || operator_code == MINUS_EXPR
13540 	  || operator_code == TRUNC_DIV_EXPR
13541 	  || operator_code == MULT_EXPR
13542 	  || operator_code == TRUNC_MOD_EXPR)
13543       && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == REFERENCE_TYPE)
13544     warning (OPT_Weffc__, "%qD should return by value", decl);
13545 
13546   return true;
13547 }
13548 
13549 /* Return a string giving the keyword associate with CODE.  */
13550 
13551 static const char *
tag_name(enum tag_types code)13552 tag_name (enum tag_types code)
13553 {
13554   switch (code)
13555     {
13556     case record_type:
13557       return "struct";
13558     case class_type:
13559       return "class";
13560     case union_type:
13561       return "union";
13562     case enum_type:
13563       return "enum";
13564     case typename_type:
13565       return "typename";
13566     default:
13567       gcc_unreachable ();
13568     }
13569 }
13570 
13571 /* Name lookup in an elaborated-type-specifier (after the keyword
13572    indicated by TAG_CODE) has found the TYPE_DECL DECL.  If the
13573    elaborated-type-specifier is invalid, issue a diagnostic and return
13574    error_mark_node; otherwise, return the *_TYPE to which it referred.
13575    If ALLOW_TEMPLATE_P is true, TYPE may be a class template.  */
13576 
13577 tree
check_elaborated_type_specifier(enum tag_types tag_code,tree decl,bool allow_template_p)13578 check_elaborated_type_specifier (enum tag_types tag_code,
13579 				 tree decl,
13580 				 bool allow_template_p)
13581 {
13582   tree type;
13583 
13584   /* In the case of:
13585 
13586        struct S { struct S *p; };
13587 
13588      name lookup will find the TYPE_DECL for the implicit "S::S"
13589      typedef.  Adjust for that here.  */
13590   if (DECL_SELF_REFERENCE_P (decl))
13591     decl = TYPE_NAME (TREE_TYPE (decl));
13592 
13593   type = TREE_TYPE (decl);
13594 
13595   /* Check TEMPLATE_TYPE_PARM first because DECL_IMPLICIT_TYPEDEF_P
13596      is false for this case as well.  */
13597   if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
13598     {
13599       error ("using template type parameter %qT after %qs",
13600 	     type, tag_name (tag_code));
13601       return error_mark_node;
13602     }
13603   /* Accept template template parameters.  */
13604   else if (allow_template_p
13605 	   && (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM
13606 	       || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM))
13607     ;
13608   /*   [dcl.type.elab]
13609 
13610        If the identifier resolves to a typedef-name or the
13611        simple-template-id resolves to an alias template
13612        specialization, the elaborated-type-specifier is ill-formed.
13613 
13614      In other words, the only legitimate declaration to use in the
13615      elaborated type specifier is the implicit typedef created when
13616      the type is declared.  */
13617   else if (!DECL_IMPLICIT_TYPEDEF_P (decl)
13618 	   && !DECL_SELF_REFERENCE_P (decl)
13619 	   && tag_code != typename_type)
13620     {
13621       if (alias_template_specialization_p (type))
13622 	error ("using alias template specialization %qT after %qs",
13623 	       type, tag_name (tag_code));
13624       else
13625 	error ("using typedef-name %qD after %qs", decl, tag_name (tag_code));
13626       inform (DECL_SOURCE_LOCATION (decl),
13627 	      "%qD has a previous declaration here", decl);
13628       return error_mark_node;
13629     }
13630   else if (TREE_CODE (type) != RECORD_TYPE
13631 	   && TREE_CODE (type) != UNION_TYPE
13632 	   && tag_code != enum_type
13633 	   && tag_code != typename_type)
13634     {
13635       error ("%qT referred to as %qs", type, tag_name (tag_code));
13636       inform (location_of (type), "%qT has a previous declaration here", type);
13637       return error_mark_node;
13638     }
13639   else if (TREE_CODE (type) != ENUMERAL_TYPE
13640 	   && tag_code == enum_type)
13641     {
13642       error ("%qT referred to as enum", type);
13643       inform (location_of (type), "%qT has a previous declaration here", type);
13644       return error_mark_node;
13645     }
13646   else if (!allow_template_p
13647 	   && TREE_CODE (type) == RECORD_TYPE
13648 	   && CLASSTYPE_IS_TEMPLATE (type))
13649     {
13650       /* If a class template appears as elaborated type specifier
13651 	 without a template header such as:
13652 
13653 	   template <class T> class C {};
13654 	   void f(class C);		// No template header here
13655 
13656 	 then the required template argument is missing.  */
13657       error ("template argument required for %<%s %T%>",
13658 	     tag_name (tag_code),
13659 	     DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)));
13660       return error_mark_node;
13661     }
13662 
13663   return type;
13664 }
13665 
13666 /* Lookup NAME in elaborate type specifier in scope according to
13667    SCOPE and issue diagnostics if necessary.
13668    Return *_TYPE node upon success, NULL_TREE when the NAME is not
13669    found, and ERROR_MARK_NODE for type error.  */
13670 
13671 static tree
lookup_and_check_tag(enum tag_types tag_code,tree name,tag_scope scope,bool template_header_p)13672 lookup_and_check_tag (enum tag_types tag_code, tree name,
13673 		      tag_scope scope, bool template_header_p)
13674 {
13675   tree t;
13676   tree decl;
13677   if (scope == ts_global)
13678     {
13679       /* First try ordinary name lookup, ignoring hidden class name
13680 	 injected via friend declaration.  */
13681       decl = lookup_name_prefer_type (name, 2);
13682       decl = strip_using_decl (decl);
13683       /* If that fails, the name will be placed in the smallest
13684 	 non-class, non-function-prototype scope according to 3.3.1/5.
13685 	 We may already have a hidden name declared as friend in this
13686 	 scope.  So lookup again but not ignoring hidden names.
13687 	 If we find one, that name will be made visible rather than
13688 	 creating a new tag.  */
13689       if (!decl)
13690 	decl = lookup_type_scope (name, ts_within_enclosing_non_class);
13691     }
13692   else
13693     decl = lookup_type_scope (name, scope);
13694 
13695   if (decl
13696       && (DECL_CLASS_TEMPLATE_P (decl)
13697 	  /* If scope is ts_current we're defining a class, so ignore a
13698 	     template template parameter.  */
13699 	  || (scope != ts_current
13700 	      && DECL_TEMPLATE_TEMPLATE_PARM_P (decl))))
13701     decl = DECL_TEMPLATE_RESULT (decl);
13702 
13703   if (decl && TREE_CODE (decl) == TYPE_DECL)
13704     {
13705       /* Look for invalid nested type:
13706 	   class C {
13707 	     class C {};
13708 	   };  */
13709       if (scope == ts_current && DECL_SELF_REFERENCE_P (decl))
13710 	{
13711 	  error ("%qD has the same name as the class in which it is "
13712 		 "declared",
13713 		 decl);
13714 	  return error_mark_node;
13715 	}
13716 
13717       /* Two cases we need to consider when deciding if a class
13718 	 template is allowed as an elaborated type specifier:
13719 	 1. It is a self reference to its own class.
13720 	 2. It comes with a template header.
13721 
13722 	 For example:
13723 
13724 	   template <class T> class C {
13725 	     class C *c1;		// DECL_SELF_REFERENCE_P is true
13726 	     class D;
13727 	   };
13728 	   template <class U> class C; // template_header_p is true
13729 	   template <class T> class C<T>::D {
13730 	     class C *c2;		// DECL_SELF_REFERENCE_P is true
13731 	   };  */
13732 
13733       t = check_elaborated_type_specifier (tag_code,
13734 					   decl,
13735 					   template_header_p
13736 					   | DECL_SELF_REFERENCE_P (decl));
13737       if (template_header_p && t && CLASS_TYPE_P (t)
13738 	  && (!CLASSTYPE_TEMPLATE_INFO (t)
13739 	      || (!PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)))))
13740 	{
13741 	  error ("%qT is not a template", t);
13742 	  inform (location_of (t), "previous declaration here");
13743 	  if (TYPE_CLASS_SCOPE_P (t)
13744 	      && CLASSTYPE_TEMPLATE_INFO (TYPE_CONTEXT (t)))
13745 	    inform (input_location,
13746 		    "perhaps you want to explicitly add %<%T::%>",
13747 		    TYPE_CONTEXT (t));
13748 	  t = error_mark_node;
13749 	}
13750 
13751       return t;
13752     }
13753   else if (decl && TREE_CODE (decl) == TREE_LIST)
13754     {
13755       error ("reference to %qD is ambiguous", name);
13756       print_candidates (decl);
13757       return error_mark_node;
13758     }
13759   else
13760     return NULL_TREE;
13761 }
13762 
13763 /* Get the struct, enum or union (TAG_CODE says which) with tag NAME.
13764    Define the tag as a forward-reference if it is not defined.
13765 
13766    If a declaration is given, process it here, and report an error if
13767    multiple declarations are not identical.
13768 
13769    SCOPE is TS_CURRENT when this is also a definition.  Only look in
13770    the current frame for the name (since C++ allows new names in any
13771    scope.)  It is TS_WITHIN_ENCLOSING_NON_CLASS if this is a friend
13772    declaration.  Only look beginning from the current scope outward up
13773    till the nearest non-class scope.  Otherwise it is TS_GLOBAL.
13774 
13775    TEMPLATE_HEADER_P is true when this declaration is preceded by
13776    a set of template parameters.  */
13777 
13778 static tree
xref_tag_1(enum tag_types tag_code,tree name,tag_scope scope,bool template_header_p)13779 xref_tag_1 (enum tag_types tag_code, tree name,
13780             tag_scope scope, bool template_header_p)
13781 {
13782   enum tree_code code;
13783   tree context = NULL_TREE;
13784 
13785   gcc_assert (identifier_p (name));
13786 
13787   switch (tag_code)
13788     {
13789     case record_type:
13790     case class_type:
13791       code = RECORD_TYPE;
13792       break;
13793     case union_type:
13794       code = UNION_TYPE;
13795       break;
13796     case enum_type:
13797       code = ENUMERAL_TYPE;
13798       break;
13799     default:
13800       gcc_unreachable ();
13801     }
13802 
13803   /* In case of anonymous name, xref_tag is only called to
13804      make type node and push name.  Name lookup is not required.  */
13805   tree t = NULL_TREE;
13806   if (scope != ts_lambda && !anon_aggrname_p (name))
13807     t = lookup_and_check_tag  (tag_code, name, scope, template_header_p);
13808 
13809   if (t == error_mark_node)
13810     return error_mark_node;
13811 
13812   if (scope != ts_current && t && current_class_type
13813       && template_class_depth (current_class_type)
13814       && template_header_p)
13815     {
13816       if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
13817 	return t;
13818 
13819       /* Since SCOPE is not TS_CURRENT, we are not looking at a
13820 	 definition of this tag.  Since, in addition, we are currently
13821 	 processing a (member) template declaration of a template
13822 	 class, we must be very careful; consider:
13823 
13824 	   template <class X> struct S1
13825 
13826 	   template <class U> struct S2
13827 	   {
13828 	     template <class V> friend struct S1;
13829 	   };
13830 
13831 	 Here, the S2::S1 declaration should not be confused with the
13832 	 outer declaration.  In particular, the inner version should
13833 	 have a template parameter of level 2, not level 1.
13834 
13835 	 On the other hand, when presented with:
13836 
13837 	   template <class T> struct S1
13838 	   {
13839 	     template <class U> struct S2 {};
13840 	     template <class U> friend struct S2;
13841 	   };
13842 
13843 	 the friend must find S1::S2 eventually.  We accomplish this
13844 	 by making sure that the new type we create to represent this
13845 	 declaration has the right TYPE_CONTEXT.  */
13846       context = TYPE_CONTEXT (t);
13847       t = NULL_TREE;
13848     }
13849 
13850   if (! t)
13851     {
13852       /* If no such tag is yet defined, create a forward-reference node
13853 	 and record it as the "definition".
13854 	 When a real declaration of this type is found,
13855 	 the forward-reference will be altered into a real type.  */
13856       if (code == ENUMERAL_TYPE)
13857 	{
13858 	  error ("use of enum %q#D without previous declaration", name);
13859 	  return error_mark_node;
13860 	}
13861       else
13862 	{
13863 	  t = make_class_type (code);
13864 	  TYPE_CONTEXT (t) = context;
13865 	  if (scope == ts_lambda)
13866 	    {
13867 	      /* Mark it as a lambda type.  */
13868 	      CLASSTYPE_LAMBDA_EXPR (t) = error_mark_node;
13869 	      /* And push it into current scope.  */
13870 	      scope = ts_current;
13871 	    }
13872 	  t = pushtag (name, t, scope);
13873 	}
13874     }
13875   else
13876     {
13877       if (template_header_p && MAYBE_CLASS_TYPE_P (t))
13878         {
13879           /* Check that we aren't trying to overload a class with different
13880              constraints.  */
13881           tree constr = NULL_TREE;
13882           if (current_template_parms)
13883             {
13884               tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
13885               constr = build_constraints (reqs, NULL_TREE);
13886             }
13887 	  if (!redeclare_class_template (t, current_template_parms, constr))
13888 	    return error_mark_node;
13889         }
13890       else if (!processing_template_decl
13891 	       && CLASS_TYPE_P (t)
13892 	       && CLASSTYPE_IS_TEMPLATE (t))
13893 	{
13894 	  error ("redeclaration of %qT as a non-template", t);
13895 	  inform (location_of (t), "previous declaration %qD", t);
13896 	  return error_mark_node;
13897 	}
13898 
13899       if (scope != ts_within_enclosing_non_class && TYPE_HIDDEN_P (t))
13900 	{
13901 	  /* This is no longer an invisible friend.  Make it
13902 	     visible.  */
13903 	  tree decl = TYPE_NAME (t);
13904 
13905 	  DECL_ANTICIPATED (decl) = false;
13906 	  DECL_FRIEND_P (decl) = false;
13907 
13908 	  if (TYPE_TEMPLATE_INFO (t))
13909 	    {
13910 	      tree tmpl = TYPE_TI_TEMPLATE (t);
13911 	      DECL_ANTICIPATED (tmpl) = false;
13912 	      DECL_FRIEND_P (tmpl) = false;
13913 	    }
13914 	}
13915     }
13916 
13917   return t;
13918 }
13919 
13920 /* Wrapper for xref_tag_1.  */
13921 
13922 tree
xref_tag(enum tag_types tag_code,tree name,tag_scope scope,bool template_header_p)13923 xref_tag (enum tag_types tag_code, tree name,
13924           tag_scope scope, bool template_header_p)
13925 {
13926   tree ret;
13927   bool subtime;
13928   subtime = timevar_cond_start (TV_NAME_LOOKUP);
13929   ret = xref_tag_1 (tag_code, name, scope, template_header_p);
13930   timevar_cond_stop (TV_NAME_LOOKUP, subtime);
13931   return ret;
13932 }
13933 
13934 
13935 tree
xref_tag_from_type(tree old,tree id,tag_scope scope)13936 xref_tag_from_type (tree old, tree id, tag_scope scope)
13937 {
13938   enum tag_types tag_kind;
13939 
13940   if (TREE_CODE (old) == RECORD_TYPE)
13941     tag_kind = (CLASSTYPE_DECLARED_CLASS (old) ? class_type : record_type);
13942   else
13943     tag_kind  = union_type;
13944 
13945   if (id == NULL_TREE)
13946     id = TYPE_IDENTIFIER (old);
13947 
13948   return xref_tag (tag_kind, id, scope, false);
13949 }
13950 
13951 /* Create the binfo hierarchy for REF with (possibly NULL) base list
13952    BASE_LIST.  For each element on BASE_LIST the TREE_PURPOSE is an
13953    access_* node, and the TREE_VALUE is the type of the base-class.
13954    Non-NULL TREE_TYPE indicates virtual inheritance.  */
13955 
13956 void
xref_basetypes(tree ref,tree base_list)13957 xref_basetypes (tree ref, tree base_list)
13958 {
13959   tree *basep;
13960   tree binfo, base_binfo;
13961   unsigned max_vbases = 0; /* Maximum direct & indirect virtual bases.  */
13962   unsigned max_bases = 0;  /* Maximum direct bases.  */
13963   unsigned max_dvbases = 0; /* Maximum direct virtual bases.  */
13964   int i;
13965   tree default_access;
13966   tree igo_prev; /* Track Inheritance Graph Order.  */
13967 
13968   if (ref == error_mark_node)
13969     return;
13970 
13971   /* The base of a derived class is private by default, all others are
13972      public.  */
13973   default_access = (TREE_CODE (ref) == RECORD_TYPE
13974 		    && CLASSTYPE_DECLARED_CLASS (ref)
13975 		    ? access_private_node : access_public_node);
13976 
13977   /* First, make sure that any templates in base-classes are
13978      instantiated.  This ensures that if we call ourselves recursively
13979      we do not get confused about which classes are marked and which
13980      are not.  */
13981   basep = &base_list;
13982   while (*basep)
13983     {
13984       tree basetype = TREE_VALUE (*basep);
13985 
13986       /* The dependent_type_p call below should really be dependent_scope_p
13987 	 so that we give a hard error about using an incomplete type as a
13988 	 base, but we allow it with a pedwarn for backward
13989 	 compatibility.  */
13990       if (processing_template_decl
13991 	  && CLASS_TYPE_P (basetype) && TYPE_BEING_DEFINED (basetype))
13992 	cxx_incomplete_type_diagnostic (NULL_TREE, basetype, DK_PEDWARN);
13993       if (!dependent_type_p (basetype)
13994 	  && !complete_type_or_else (basetype, NULL))
13995 	/* An incomplete type.  Remove it from the list.  */
13996 	*basep = TREE_CHAIN (*basep);
13997       else
13998 	{
13999 	  max_bases++;
14000 	  if (TREE_TYPE (*basep))
14001 	    max_dvbases++;
14002 	  if (CLASS_TYPE_P (basetype))
14003 	    max_vbases += vec_safe_length (CLASSTYPE_VBASECLASSES (basetype));
14004 	  basep = &TREE_CHAIN (*basep);
14005 	}
14006     }
14007   max_vbases += max_dvbases;
14008 
14009   TYPE_MARKED_P (ref) = 1;
14010 
14011   /* The binfo slot should be empty, unless this is an (ill-formed)
14012      redefinition.  */
14013   gcc_assert (!TYPE_BINFO (ref) || TYPE_SIZE (ref));
14014 
14015   gcc_assert (TYPE_MAIN_VARIANT (ref) == ref);
14016 
14017   binfo = make_tree_binfo (max_bases);
14018 
14019   TYPE_BINFO (ref) = binfo;
14020   BINFO_OFFSET (binfo) = size_zero_node;
14021   BINFO_TYPE (binfo) = ref;
14022 
14023   /* Apply base-class info set up to the variants of this type.  */
14024   fixup_type_variants (ref);
14025 
14026   if (max_bases)
14027     {
14028       vec_alloc (BINFO_BASE_ACCESSES (binfo), max_bases);
14029       /* A C++98 POD cannot have base classes.  */
14030       CLASSTYPE_NON_LAYOUT_POD_P (ref) = true;
14031 
14032       if (TREE_CODE (ref) == UNION_TYPE)
14033 	{
14034 	  error ("derived union %qT invalid", ref);
14035 	  return;
14036 	}
14037     }
14038 
14039   if (max_bases > 1)
14040     warning (OPT_Wmultiple_inheritance,
14041 	     "%qT defined with multiple direct bases", ref);
14042 
14043   if (max_vbases)
14044     {
14045       /* An aggregate can't have virtual base classes.  */
14046       CLASSTYPE_NON_AGGREGATE (ref) = true;
14047 
14048       vec_alloc (CLASSTYPE_VBASECLASSES (ref), max_vbases);
14049 
14050       if (max_dvbases)
14051 	warning (OPT_Wvirtual_inheritance,
14052 		 "%qT defined with direct virtual base", ref);
14053     }
14054 
14055   for (igo_prev = binfo; base_list; base_list = TREE_CHAIN (base_list))
14056     {
14057       tree access = TREE_PURPOSE (base_list);
14058       int via_virtual = TREE_TYPE (base_list) != NULL_TREE;
14059       tree basetype = TREE_VALUE (base_list);
14060 
14061       if (access == access_default_node)
14062 	access = default_access;
14063 
14064       /* Before C++17, an aggregate cannot have base classes.  In C++17, an
14065 	 aggregate can't have virtual, private, or protected base classes.  */
14066       if (cxx_dialect < cxx17
14067 	  || access != access_public_node
14068 	  || via_virtual)
14069 	CLASSTYPE_NON_AGGREGATE (ref) = true;
14070 
14071       if (PACK_EXPANSION_P (basetype))
14072         basetype = PACK_EXPANSION_PATTERN (basetype);
14073       if (TREE_CODE (basetype) == TYPE_DECL)
14074 	basetype = TREE_TYPE (basetype);
14075       if (!MAYBE_CLASS_TYPE_P (basetype) || TREE_CODE (basetype) == UNION_TYPE)
14076 	{
14077 	  error ("base type %qT fails to be a struct or class type",
14078 		 basetype);
14079 	  goto dropped_base;
14080 	}
14081 
14082       base_binfo = NULL_TREE;
14083       if (CLASS_TYPE_P (basetype) && !dependent_scope_p (basetype))
14084 	{
14085 	  base_binfo = TYPE_BINFO (basetype);
14086 	  /* The original basetype could have been a typedef'd type.  */
14087 	  basetype = BINFO_TYPE (base_binfo);
14088 
14089 	  /* Inherit flags from the base.  */
14090 	  TYPE_HAS_NEW_OPERATOR (ref)
14091 	    |= TYPE_HAS_NEW_OPERATOR (basetype);
14092 	  TYPE_HAS_ARRAY_NEW_OPERATOR (ref)
14093 	    |= TYPE_HAS_ARRAY_NEW_OPERATOR (basetype);
14094 	  TYPE_GETS_DELETE (ref) |= TYPE_GETS_DELETE (basetype);
14095 	  TYPE_HAS_CONVERSION (ref) |= TYPE_HAS_CONVERSION (basetype);
14096 	  CLASSTYPE_DIAMOND_SHAPED_P (ref)
14097 	    |= CLASSTYPE_DIAMOND_SHAPED_P (basetype);
14098 	  CLASSTYPE_REPEATED_BASE_P (ref)
14099 	    |= CLASSTYPE_REPEATED_BASE_P (basetype);
14100 	}
14101 
14102       /* We must do this test after we've seen through a typedef
14103 	 type.  */
14104       if (TYPE_MARKED_P (basetype))
14105 	{
14106 	  if (basetype == ref)
14107 	    error ("recursive type %qT undefined", basetype);
14108 	  else
14109 	    error ("duplicate base type %qT invalid", basetype);
14110 	  goto dropped_base;
14111 	}
14112 
14113       if (PACK_EXPANSION_P (TREE_VALUE (base_list)))
14114         /* Regenerate the pack expansion for the bases. */
14115         basetype = make_pack_expansion (basetype);
14116 
14117       TYPE_MARKED_P (basetype) = 1;
14118 
14119       base_binfo = copy_binfo (base_binfo, basetype, ref,
14120 			       &igo_prev, via_virtual);
14121       if (!BINFO_INHERITANCE_CHAIN (base_binfo))
14122 	BINFO_INHERITANCE_CHAIN (base_binfo) = binfo;
14123 
14124       BINFO_BASE_APPEND (binfo, base_binfo);
14125       BINFO_BASE_ACCESS_APPEND (binfo, access);
14126       continue;
14127 
14128     dropped_base:
14129       /* Update max_vbases to reflect the reality that we are dropping
14130 	 this base:  if it reaches zero we want to undo the vec_alloc
14131 	 above to avoid inconsistencies during error-recovery: eg, in
14132 	 build_special_member_call, CLASSTYPE_VBASECLASSES non null
14133 	 and vtt null (c++/27952).  */
14134       if (via_virtual)
14135 	max_vbases--;
14136       if (CLASS_TYPE_P (basetype))
14137 	max_vbases
14138 	  -= vec_safe_length (CLASSTYPE_VBASECLASSES (basetype));
14139     }
14140 
14141   if (CLASSTYPE_VBASECLASSES (ref)
14142       && max_vbases == 0)
14143     vec_free (CLASSTYPE_VBASECLASSES (ref));
14144 
14145   if (vec_safe_length (CLASSTYPE_VBASECLASSES (ref)) < max_vbases)
14146     /* If we didn't get max_vbases vbases, we must have shared at
14147        least one of them, and are therefore diamond shaped.  */
14148     CLASSTYPE_DIAMOND_SHAPED_P (ref) = 1;
14149 
14150   /* Unmark all the types.  */
14151   for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
14152     TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
14153   TYPE_MARKED_P (ref) = 0;
14154 
14155   /* Now see if we have a repeated base type.  */
14156   if (!CLASSTYPE_REPEATED_BASE_P (ref))
14157     {
14158       for (base_binfo = binfo; base_binfo;
14159 	   base_binfo = TREE_CHAIN (base_binfo))
14160 	{
14161 	  if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
14162 	    {
14163 	      CLASSTYPE_REPEATED_BASE_P (ref) = 1;
14164 	      break;
14165 	    }
14166 	  TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 1;
14167 	}
14168       for (base_binfo = binfo; base_binfo;
14169 	   base_binfo = TREE_CHAIN (base_binfo))
14170 	if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
14171 	  TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
14172 	else
14173 	  break;
14174     }
14175 }
14176 
14177 
14178 /* Copies the enum-related properties from type SRC to type DST.
14179    Used with the underlying type of an enum and the enum itself.  */
14180 static void
copy_type_enum(tree dst,tree src)14181 copy_type_enum (tree dst, tree src)
14182 {
14183   tree t;
14184   for (t = dst; t; t = TYPE_NEXT_VARIANT (t))
14185     {
14186       TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (src);
14187       TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (src);
14188       TYPE_SIZE (t) = TYPE_SIZE (src);
14189       TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (src);
14190       SET_TYPE_MODE (dst, TYPE_MODE (src));
14191       TYPE_PRECISION (t) = TYPE_PRECISION (src);
14192       unsigned valign = TYPE_ALIGN (src);
14193       if (TYPE_USER_ALIGN (t))
14194 	valign = MAX (valign, TYPE_ALIGN (t));
14195       else
14196 	TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (src);
14197       SET_TYPE_ALIGN (t, valign);
14198       TYPE_UNSIGNED (t) = TYPE_UNSIGNED (src);
14199     }
14200 }
14201 
14202 /* Begin compiling the definition of an enumeration type.
14203    NAME is its name,
14204 
14205    if ENUMTYPE is not NULL_TREE then the type has alredy been found.
14206 
14207    UNDERLYING_TYPE is the type that will be used as the storage for
14208    the enumeration type. This should be NULL_TREE if no storage type
14209    was specified.
14210 
14211    ATTRIBUTES are any attributes specified after the enum-key.
14212 
14213    SCOPED_ENUM_P is true if this is a scoped enumeration type.
14214 
14215    if IS_NEW is not NULL, gets TRUE iff a new type is created.
14216 
14217    Returns the type object, as yet incomplete.
14218    Also records info about it so that build_enumerator
14219    may be used to declare the individual values as they are read.  */
14220 
14221 tree
start_enum(tree name,tree enumtype,tree underlying_type,tree attributes,bool scoped_enum_p,bool * is_new)14222 start_enum (tree name, tree enumtype, tree underlying_type,
14223 	    tree attributes, bool scoped_enum_p, bool *is_new)
14224 {
14225   tree prevtype = NULL_TREE;
14226   gcc_assert (identifier_p (name));
14227 
14228   if (is_new)
14229     *is_new = false;
14230   /* [C++0x dcl.enum]p5:
14231 
14232     If not explicitly specified, the underlying type of a scoped
14233     enumeration type is int.  */
14234   if (!underlying_type && scoped_enum_p)
14235     underlying_type = integer_type_node;
14236 
14237   if (underlying_type)
14238     underlying_type = cv_unqualified (underlying_type);
14239 
14240   /* If this is the real definition for a previous forward reference,
14241      fill in the contents in the same object that used to be the
14242      forward reference.  */
14243   if (!enumtype)
14244     enumtype = lookup_and_check_tag (enum_type, name,
14245 				     /*tag_scope=*/ts_current,
14246 				     /*template_header_p=*/false);
14247 
14248   /* In case of a template_decl, the only check that should be deferred
14249      to instantiation time is the comparison of underlying types.  */
14250   if (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE)
14251     {
14252       if (scoped_enum_p != SCOPED_ENUM_P (enumtype))
14253 	{
14254 	  error_at (input_location, "scoped/unscoped mismatch "
14255 		    "in enum %q#T", enumtype);
14256 	  inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
14257 		  "previous definition here");
14258 	  enumtype = error_mark_node;
14259 	}
14260       else if (ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) != !! underlying_type)
14261 	{
14262 	  error_at (input_location, "underlying type mismatch "
14263 		    "in enum %q#T", enumtype);
14264 	  inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
14265 		  "previous definition here");
14266 	  enumtype = error_mark_node;
14267 	}
14268       else if (underlying_type && ENUM_UNDERLYING_TYPE (enumtype)
14269 	       && !dependent_type_p (underlying_type)
14270 	       && !dependent_type_p (ENUM_UNDERLYING_TYPE (enumtype))
14271 	       && !same_type_p (underlying_type,
14272 				ENUM_UNDERLYING_TYPE (enumtype)))
14273 	{
14274 	  error_at (input_location, "different underlying type "
14275 		    "in enum %q#T", enumtype);
14276 	  inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
14277 		  "previous definition here");
14278 	  underlying_type = NULL_TREE;
14279 	}
14280     }
14281 
14282   if (!enumtype || TREE_CODE (enumtype) != ENUMERAL_TYPE
14283       || processing_template_decl)
14284     {
14285       /* In case of error, make a dummy enum to allow parsing to
14286 	 continue.  */
14287       if (enumtype == error_mark_node)
14288 	{
14289 	  name = make_anon_name ();
14290 	  enumtype = NULL_TREE;
14291 	}
14292 
14293       /* enumtype may be an ENUMERAL_TYPE if this is a redefinition
14294          of an opaque enum, or an opaque enum of an already defined
14295 	 enumeration (C++0x only).
14296 	 In any other case, it'll be NULL_TREE. */
14297       if (!enumtype)
14298 	{
14299 	  if (is_new)
14300 	    *is_new = true;
14301 	}
14302       prevtype = enumtype;
14303 
14304       /* Do not push the decl more than once, unless we need to
14305 	 compare underlying types at instantiation time */
14306       if (!enumtype
14307 	  || TREE_CODE (enumtype) != ENUMERAL_TYPE
14308 	  || (underlying_type
14309 	      && dependent_type_p (underlying_type))
14310 	  || (ENUM_UNDERLYING_TYPE (enumtype)
14311 	      && dependent_type_p (ENUM_UNDERLYING_TYPE (enumtype))))
14312 	{
14313 	  enumtype = cxx_make_type (ENUMERAL_TYPE);
14314 	  enumtype = pushtag (name, enumtype, /*tag_scope=*/ts_current);
14315 
14316 	  /* std::byte aliases anything.  */
14317 	  if (enumtype != error_mark_node
14318 	      && TYPE_CONTEXT (enumtype) == std_node
14319 	      && !strcmp ("byte", TYPE_NAME_STRING (enumtype)))
14320 	    TYPE_ALIAS_SET (enumtype) = 0;
14321 	}
14322       else
14323 	  enumtype = xref_tag (enum_type, name, /*tag_scope=*/ts_current,
14324 			       false);
14325 
14326       if (enumtype == error_mark_node)
14327 	return error_mark_node;
14328 
14329       /* The enum is considered opaque until the opening '{' of the
14330 	 enumerator list.  */
14331       SET_OPAQUE_ENUM_P (enumtype, true);
14332       ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) = !! underlying_type;
14333     }
14334 
14335   SET_SCOPED_ENUM_P (enumtype, scoped_enum_p);
14336 
14337   cplus_decl_attributes (&enumtype, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
14338 
14339   if (underlying_type)
14340     {
14341       if (ENUM_UNDERLYING_TYPE (enumtype))
14342 	/* We already checked that it matches, don't change it to a different
14343 	   typedef variant.  */;
14344       else if (CP_INTEGRAL_TYPE_P (underlying_type))
14345         {
14346 	  copy_type_enum (enumtype, underlying_type);
14347           ENUM_UNDERLYING_TYPE (enumtype) = underlying_type;
14348         }
14349       else if (dependent_type_p (underlying_type))
14350 	ENUM_UNDERLYING_TYPE (enumtype) = underlying_type;
14351       else
14352         error ("underlying type %qT of %qT must be an integral type",
14353                underlying_type, enumtype);
14354     }
14355 
14356   /* If into a template class, the returned enum is always the first
14357      declaration (opaque or not) seen. This way all the references to
14358      this type will be to the same declaration. The following ones are used
14359      only to check for definition errors.  */
14360   if (prevtype && processing_template_decl)
14361     return prevtype;
14362   else
14363     return enumtype;
14364 }
14365 
14366 /* After processing and defining all the values of an enumeration type,
14367    install their decls in the enumeration type.
14368    ENUMTYPE is the type object.  */
14369 
14370 void
finish_enum_value_list(tree enumtype)14371 finish_enum_value_list (tree enumtype)
14372 {
14373   tree values;
14374   tree underlying_type;
14375   tree decl;
14376   tree value;
14377   tree minnode, maxnode;
14378   tree t;
14379 
14380   bool fixed_underlying_type_p
14381     = ENUM_UNDERLYING_TYPE (enumtype) != NULL_TREE;
14382 
14383   /* We built up the VALUES in reverse order.  */
14384   TYPE_VALUES (enumtype) = nreverse (TYPE_VALUES (enumtype));
14385 
14386   /* For an enum defined in a template, just set the type of the values;
14387      all further processing is postponed until the template is
14388      instantiated.  We need to set the type so that tsubst of a CONST_DECL
14389      works.  */
14390   if (processing_template_decl)
14391     {
14392       for (values = TYPE_VALUES (enumtype);
14393 	   values;
14394 	   values = TREE_CHAIN (values))
14395 	TREE_TYPE (TREE_VALUE (values)) = enumtype;
14396       return;
14397     }
14398 
14399   /* Determine the minimum and maximum values of the enumerators.  */
14400   if (TYPE_VALUES (enumtype))
14401     {
14402       minnode = maxnode = NULL_TREE;
14403 
14404       for (values = TYPE_VALUES (enumtype);
14405 	   values;
14406 	   values = TREE_CHAIN (values))
14407 	{
14408 	  decl = TREE_VALUE (values);
14409 
14410 	  /* [dcl.enum]: Following the closing brace of an enum-specifier,
14411 	     each enumerator has the type of its enumeration.  Prior to the
14412 	     closing brace, the type of each enumerator is the type of its
14413 	     initializing value.  */
14414 	  TREE_TYPE (decl) = enumtype;
14415 
14416 	  /* Update the minimum and maximum values, if appropriate.  */
14417 	  value = DECL_INITIAL (decl);
14418 	  if (value == error_mark_node)
14419 	    value = integer_zero_node;
14420 	  /* Figure out what the minimum and maximum values of the
14421 	     enumerators are.  */
14422 	  if (!minnode)
14423 	    minnode = maxnode = value;
14424 	  else if (tree_int_cst_lt (maxnode, value))
14425 	    maxnode = value;
14426 	  else if (tree_int_cst_lt (value, minnode))
14427 	    minnode = value;
14428 	}
14429     }
14430   else
14431     /* [dcl.enum]
14432 
14433        If the enumerator-list is empty, the underlying type is as if
14434        the enumeration had a single enumerator with value 0.  */
14435     minnode = maxnode = integer_zero_node;
14436 
14437   if (!fixed_underlying_type_p)
14438     {
14439       /* Compute the number of bits require to represent all values of the
14440 	 enumeration.  We must do this before the type of MINNODE and
14441 	 MAXNODE are transformed, since tree_int_cst_min_precision relies
14442 	 on the TREE_TYPE of the value it is passed.  */
14443       signop sgn = tree_int_cst_sgn (minnode) >= 0 ? UNSIGNED : SIGNED;
14444       int lowprec = tree_int_cst_min_precision (minnode, sgn);
14445       int highprec = tree_int_cst_min_precision (maxnode, sgn);
14446       int precision = MAX (lowprec, highprec);
14447       unsigned int itk;
14448       bool use_short_enum;
14449 
14450       /* Determine the underlying type of the enumeration.
14451 
14452          [dcl.enum]
14453 
14454          The underlying type of an enumeration is an integral type that
14455          can represent all the enumerator values defined in the
14456          enumeration.  It is implementation-defined which integral type is
14457          used as the underlying type for an enumeration except that the
14458          underlying type shall not be larger than int unless the value of
14459          an enumerator cannot fit in an int or unsigned int.
14460 
14461          We use "int" or an "unsigned int" as the underlying type, even if
14462          a smaller integral type would work, unless the user has
14463          explicitly requested that we use the smallest possible type.  The
14464          user can request that for all enumerations with a command line
14465          flag, or for just one enumeration with an attribute.  */
14466 
14467       use_short_enum = flag_short_enums
14468         || lookup_attribute ("packed", TYPE_ATTRIBUTES (enumtype));
14469 
14470       /* If the precision of the type was specified with an attribute and it
14471 	 was too small, give an error.  Otherwise, use it.  */
14472       if (TYPE_PRECISION (enumtype))
14473 	{
14474 	  if (precision > TYPE_PRECISION (enumtype))
14475 	    error ("specified mode too small for enumeral values");
14476 	  else
14477 	    {
14478 	      use_short_enum = true;
14479 	      precision = TYPE_PRECISION (enumtype);
14480 	    }
14481 	}
14482 
14483       for (itk = (use_short_enum ? itk_char : itk_int);
14484            itk != itk_none;
14485            itk++)
14486         {
14487           underlying_type = integer_types[itk];
14488           if (underlying_type != NULL_TREE
14489 	      && TYPE_PRECISION (underlying_type) >= precision
14490               && TYPE_SIGN (underlying_type) == sgn)
14491             break;
14492         }
14493       if (itk == itk_none)
14494         {
14495           /* DR 377
14496 
14497              IF no integral type can represent all the enumerator values, the
14498              enumeration is ill-formed.  */
14499           error ("no integral type can represent all of the enumerator values "
14500                  "for %qT", enumtype);
14501           precision = TYPE_PRECISION (long_long_integer_type_node);
14502           underlying_type = integer_types[itk_unsigned_long_long];
14503         }
14504 
14505       /* [dcl.enum]
14506 
14507          The value of sizeof() applied to an enumeration type, an object
14508          of an enumeration type, or an enumerator, is the value of sizeof()
14509          applied to the underlying type.  */
14510       copy_type_enum (enumtype, underlying_type);
14511 
14512       /* Compute the minimum and maximum values for the type.
14513 
14514 	 [dcl.enum]
14515 
14516 	 For an enumeration where emin is the smallest enumerator and emax
14517 	 is the largest, the values of the enumeration are the values of the
14518 	 underlying type in the range bmin to bmax, where bmin and bmax are,
14519 	 respectively, the smallest and largest values of the smallest bit-
14520 	 field that can store emin and emax.  */
14521 
14522       /* The middle-end currently assumes that types with TYPE_PRECISION
14523 	 narrower than their underlying type are suitably zero or sign
14524 	 extended to fill their mode.  Similarly, it assumes that the front
14525 	 end assures that a value of a particular type must be within
14526 	 TYPE_MIN_VALUE and TYPE_MAX_VALUE.
14527 
14528 	 We used to set these fields based on bmin and bmax, but that led
14529 	 to invalid assumptions like optimizing away bounds checking.  So
14530 	 now we just set the TYPE_PRECISION, TYPE_MIN_VALUE, and
14531 	 TYPE_MAX_VALUE to the values for the mode above and only restrict
14532 	 the ENUM_UNDERLYING_TYPE for the benefit of diagnostics.  */
14533       ENUM_UNDERLYING_TYPE (enumtype)
14534 	= build_distinct_type_copy (underlying_type);
14535       TYPE_PRECISION (ENUM_UNDERLYING_TYPE (enumtype)) = precision;
14536       set_min_and_max_values_for_integral_type
14537         (ENUM_UNDERLYING_TYPE (enumtype), precision, sgn);
14538 
14539       /* If -fstrict-enums, still constrain TYPE_MIN/MAX_VALUE.  */
14540       if (flag_strict_enums)
14541 	set_min_and_max_values_for_integral_type (enumtype, precision, sgn);
14542     }
14543   else
14544     underlying_type = ENUM_UNDERLYING_TYPE (enumtype);
14545 
14546   /* Convert each of the enumerators to the type of the underlying
14547      type of the enumeration.  */
14548   for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values))
14549     {
14550       location_t saved_location;
14551 
14552       decl = TREE_VALUE (values);
14553       saved_location = input_location;
14554       input_location = DECL_SOURCE_LOCATION (decl);
14555       if (fixed_underlying_type_p)
14556         /* If the enumeration type has a fixed underlying type, we
14557            already checked all of the enumerator values.  */
14558         value = DECL_INITIAL (decl);
14559       else
14560         value = perform_implicit_conversion (underlying_type,
14561                                              DECL_INITIAL (decl),
14562                                              tf_warning_or_error);
14563       input_location = saved_location;
14564 
14565       /* Do not clobber shared ints.  */
14566       if (value != error_mark_node)
14567 	{
14568 	  value = copy_node (value);
14569 
14570 	  TREE_TYPE (value) = enumtype;
14571 	}
14572       DECL_INITIAL (decl) = value;
14573     }
14574 
14575   /* Fix up all variant types of this enum type.  */
14576   for (t = TYPE_MAIN_VARIANT (enumtype); t; t = TYPE_NEXT_VARIANT (t))
14577     TYPE_VALUES (t) = TYPE_VALUES (enumtype);
14578 
14579   if (at_class_scope_p ()
14580       && COMPLETE_TYPE_P (current_class_type)
14581       && UNSCOPED_ENUM_P (enumtype))
14582     {
14583       insert_late_enum_def_bindings (current_class_type, enumtype);
14584       /* TYPE_FIELDS needs fixup.  */
14585       fixup_type_variants (current_class_type);
14586     }
14587 
14588   /* Finish debugging output for this type.  */
14589   rest_of_type_compilation (enumtype, namespace_bindings_p ());
14590 
14591   /* Each enumerator now has the type of its enumeration.  Clear the cache
14592      so that this change in types doesn't confuse us later on.  */
14593   clear_cv_and_fold_caches ();
14594 }
14595 
14596 /* Finishes the enum type. This is called only the first time an
14597    enumeration is seen, be it opaque or odinary.
14598    ENUMTYPE is the type object.  */
14599 
14600 void
finish_enum(tree enumtype)14601 finish_enum (tree enumtype)
14602 {
14603   if (processing_template_decl)
14604     {
14605       if (at_function_scope_p ())
14606 	add_stmt (build_min (TAG_DEFN, enumtype));
14607       return;
14608     }
14609 
14610   /* If this is a forward declaration, there should not be any variants,
14611      though we can get a variant in the middle of an enum-specifier with
14612      wacky code like 'enum E { e = sizeof(const E*) };'  */
14613   gcc_assert (enumtype == TYPE_MAIN_VARIANT (enumtype)
14614 	      && (TYPE_VALUES (enumtype)
14615 		  || !TYPE_NEXT_VARIANT (enumtype)));
14616 }
14617 
14618 /* Build and install a CONST_DECL for an enumeration constant of the
14619    enumeration type ENUMTYPE whose NAME and VALUE (if any) are provided.
14620    Apply ATTRIBUTES if available.  LOC is the location of NAME.
14621    Assignment of sequential values by default is handled here.  */
14622 
14623 void
build_enumerator(tree name,tree value,tree enumtype,tree attributes,location_t loc)14624 build_enumerator (tree name, tree value, tree enumtype, tree attributes,
14625 		  location_t loc)
14626 {
14627   tree decl;
14628   tree context;
14629   tree type;
14630 
14631   /* scalar_constant_value will pull out this expression, so make sure
14632      it's folded as appropriate.  */
14633   if (processing_template_decl)
14634     value = fold_non_dependent_expr (value);
14635 
14636   /* If the VALUE was erroneous, pretend it wasn't there; that will
14637      result in the enum being assigned the next value in sequence.  */
14638   if (value == error_mark_node)
14639     value = NULL_TREE;
14640 
14641   /* Remove no-op casts from the value.  */
14642   if (value)
14643     STRIP_TYPE_NOPS (value);
14644 
14645   if (! processing_template_decl)
14646     {
14647       /* Validate and default VALUE.  */
14648       if (value != NULL_TREE)
14649 	{
14650 	  if (!ENUM_UNDERLYING_TYPE (enumtype))
14651 	    {
14652 	      tree tmp_value = build_expr_type_conversion (WANT_INT | WANT_ENUM,
14653 							   value, true);
14654 	      if (tmp_value)
14655 		value = tmp_value;
14656 	    }
14657 	  else if (! INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P
14658 		   (TREE_TYPE (value)))
14659 	    value = perform_implicit_conversion_flags
14660 	      (ENUM_UNDERLYING_TYPE (enumtype), value, tf_warning_or_error,
14661 	       LOOKUP_IMPLICIT | LOOKUP_NO_NARROWING);
14662 
14663 	  if (value == error_mark_node)
14664 	    value = NULL_TREE;
14665 
14666 	  if (value != NULL_TREE)
14667 	    {
14668 	      if (! INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P
14669 		  (TREE_TYPE (value)))
14670 		{
14671 		  error ("enumerator value for %qD must have integral or "
14672 			 "unscoped enumeration type", name);
14673 		  value = NULL_TREE;
14674 		}
14675 	      else
14676 		{
14677 		  value = cxx_constant_value (value);
14678 
14679 		  if (TREE_CODE (value) != INTEGER_CST)
14680 		    {
14681 		      error ("enumerator value for %qD is not an integer "
14682 			     "constant", name);
14683 		      value = NULL_TREE;
14684 		    }
14685 		}
14686 	    }
14687 	}
14688 
14689       /* Default based on previous value.  */
14690       if (value == NULL_TREE)
14691 	{
14692 	  if (TYPE_VALUES (enumtype))
14693 	    {
14694 	      tree prev_value;
14695 	      bool overflowed;
14696 
14697 	      /* C++03 7.2/4: If no initializer is specified for the first
14698 		 enumerator, the type is an unspecified integral
14699 		 type. Otherwise the type is the same as the type of the
14700 		 initializing value of the preceding enumerator unless the
14701 		 incremented value is not representable in that type, in
14702 		 which case the type is an unspecified integral type
14703 		 sufficient to contain the incremented value.  */
14704 	      prev_value = DECL_INITIAL (TREE_VALUE (TYPE_VALUES (enumtype)));
14705 	      if (error_operand_p (prev_value))
14706 		value = error_mark_node;
14707 	      else
14708 		{
14709 		  tree type = TREE_TYPE (prev_value);
14710 		  signop sgn = TYPE_SIGN (type);
14711 		  widest_int wi = wi::add (wi::to_widest (prev_value), 1, sgn,
14712 					   &overflowed);
14713 		  if (!overflowed)
14714 		    {
14715 		      bool pos = !wi::neg_p (wi, sgn);
14716 		      if (!wi::fits_to_tree_p (wi, type))
14717 			{
14718 			  unsigned int itk;
14719 			  for (itk = itk_int; itk != itk_none; itk++)
14720 			    {
14721 			      type = integer_types[itk];
14722 			      if (type != NULL_TREE
14723 				  && (pos || !TYPE_UNSIGNED (type))
14724 				  && wi::fits_to_tree_p (wi, type))
14725 				break;
14726 			    }
14727 			  if (type && cxx_dialect < cxx11
14728 			      && itk > itk_unsigned_long)
14729 			    pedwarn (input_location, OPT_Wlong_long,
14730 				     pos ? G_("\
14731 incremented enumerator value is too large for %<unsigned long%>") : G_("\
14732 incremented enumerator value is too large for %<long%>"));
14733 			}
14734 		      if (type == NULL_TREE)
14735 			overflowed = true;
14736 		      else
14737 			value = wide_int_to_tree (type, wi);
14738 		    }
14739 
14740 		  if (overflowed)
14741 		    {
14742 		      error ("overflow in enumeration values at %qD", name);
14743 		      value = error_mark_node;
14744 		    }
14745 		}
14746 	    }
14747 	  else
14748 	    value = integer_zero_node;
14749 	}
14750 
14751       /* Remove no-op casts from the value.  */
14752       STRIP_TYPE_NOPS (value);
14753 
14754       /* If the underlying type of the enum is fixed, check whether
14755          the enumerator values fits in the underlying type.  If it
14756          does not fit, the program is ill-formed [C++0x dcl.enum].  */
14757       if (ENUM_UNDERLYING_TYPE (enumtype)
14758           && value
14759           && TREE_CODE (value) == INTEGER_CST)
14760         {
14761 	  if (!int_fits_type_p (value, ENUM_UNDERLYING_TYPE (enumtype)))
14762 	    error ("enumerator value %qE is outside the range of underlying "
14763 		   "type %qT", value, ENUM_UNDERLYING_TYPE (enumtype));
14764 
14765           /* Convert the value to the appropriate type.  */
14766           value = fold_convert (ENUM_UNDERLYING_TYPE (enumtype), value);
14767         }
14768     }
14769 
14770   /* C++ associates enums with global, function, or class declarations.  */
14771   context = current_scope ();
14772 
14773   /* Build the actual enumeration constant.  Note that the enumeration
14774      constants have the underlying type of the enum (if it is fixed)
14775      or the type of their initializer (if the underlying type of the
14776      enum is not fixed):
14777 
14778       [ C++0x dcl.enum ]
14779 
14780         If the underlying type is fixed, the type of each enumerator
14781         prior to the closing brace is the underlying type; if the
14782         initializing value of an enumerator cannot be represented by
14783         the underlying type, the program is ill-formed. If the
14784         underlying type is not fixed, the type of each enumerator is
14785         the type of its initializing value.
14786 
14787     If the underlying type is not fixed, it will be computed by
14788     finish_enum and we will reset the type of this enumerator.  Of
14789     course, if we're processing a template, there may be no value.  */
14790   type = value ? TREE_TYPE (value) : NULL_TREE;
14791 
14792   decl = build_decl (loc, CONST_DECL, name, type);
14793 
14794   DECL_CONTEXT (decl) = enumtype;
14795   TREE_CONSTANT (decl) = 1;
14796   TREE_READONLY (decl) = 1;
14797   DECL_INITIAL (decl) = value;
14798 
14799   if (attributes)
14800     cplus_decl_attributes (&decl, attributes, 0);
14801 
14802   if (context && context == current_class_type && !SCOPED_ENUM_P (enumtype))
14803     {
14804       /* In something like `struct S { enum E { i = 7 }; };' we put `i'
14805 	 on the TYPE_FIELDS list for `S'.  (That's so that you can say
14806 	 things like `S::i' later.)  */
14807 
14808       /* The enumerator may be getting declared outside of its enclosing
14809 	 class, like so:
14810 
14811 	   class S { public: enum E : int; }; enum S::E : int { i = 7; };
14812 
14813 	 For which case we need to make sure that the access of `S::i'
14814 	 matches the access of `S::E'.  */
14815       tree saved_cas = current_access_specifier;
14816       if (TREE_PRIVATE (TYPE_NAME (enumtype)))
14817 	current_access_specifier = access_private_node;
14818       else if (TREE_PROTECTED (TYPE_NAME (enumtype)))
14819 	current_access_specifier = access_protected_node;
14820       else
14821 	current_access_specifier = access_public_node;
14822 
14823       finish_member_declaration (decl);
14824 
14825       current_access_specifier = saved_cas;
14826     }
14827   else
14828     pushdecl (decl);
14829 
14830   /* Add this enumeration constant to the list for this type.  */
14831   TYPE_VALUES (enumtype) = tree_cons (name, decl, TYPE_VALUES (enumtype));
14832 }
14833 
14834 /* Look for an enumerator with the given NAME within the enumeration
14835    type ENUMTYPE.  This routine is used primarily for qualified name
14836    lookup into an enumerator in C++0x, e.g.,
14837 
14838      enum class Color { Red, Green, Blue };
14839 
14840      Color color = Color::Red;
14841 
14842    Returns the value corresponding to the enumerator, or
14843    NULL_TREE if no such enumerator was found.  */
14844 tree
lookup_enumerator(tree enumtype,tree name)14845 lookup_enumerator (tree enumtype, tree name)
14846 {
14847   tree e;
14848   gcc_assert (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE);
14849 
14850   e = purpose_member (name, TYPE_VALUES (enumtype));
14851   return e? TREE_VALUE (e) : NULL_TREE;
14852 }
14853 
14854 
14855 /* We're defining DECL.  Make sure that its type is OK.  */
14856 
14857 static void
check_function_type(tree decl,tree current_function_parms)14858 check_function_type (tree decl, tree current_function_parms)
14859 {
14860   tree fntype = TREE_TYPE (decl);
14861   tree return_type = complete_type (TREE_TYPE (fntype));
14862 
14863   /* In a function definition, arg types must be complete.  */
14864   require_complete_types_for_parms (current_function_parms);
14865 
14866   if (dependent_type_p (return_type)
14867       || type_uses_auto (return_type))
14868     return;
14869   if (!COMPLETE_OR_VOID_TYPE_P (return_type))
14870     {
14871       tree args = TYPE_ARG_TYPES (fntype);
14872 
14873       error ("return type %q#T is incomplete", return_type);
14874 
14875       /* Make it return void instead.  */
14876       if (TREE_CODE (fntype) == METHOD_TYPE)
14877 	fntype = build_method_type_directly (TREE_TYPE (TREE_VALUE (args)),
14878 					     void_type_node,
14879 					     TREE_CHAIN (args));
14880       else
14881 	fntype = build_function_type (void_type_node, args);
14882       fntype
14883 	= build_exception_variant (fntype,
14884 				   TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl)));
14885       fntype = (cp_build_type_attribute_variant
14886 		(fntype, TYPE_ATTRIBUTES (TREE_TYPE (decl))));
14887       TREE_TYPE (decl) = fntype;
14888     }
14889   else
14890     {
14891       abstract_virtuals_error (decl, TREE_TYPE (fntype));
14892       maybe_warn_parm_abi (TREE_TYPE (fntype),
14893 			   DECL_SOURCE_LOCATION (decl));
14894     }
14895 }
14896 
14897 /* True iff FN is an implicitly-defined default constructor.  */
14898 
14899 static bool
implicit_default_ctor_p(tree fn)14900 implicit_default_ctor_p (tree fn)
14901 {
14902   return (DECL_CONSTRUCTOR_P (fn)
14903 	  && !user_provided_p (fn)
14904 	  && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn)));
14905 }
14906 
14907 /* Clobber the contents of *this to let the back end know that the object
14908    storage is dead when we enter the constructor or leave the destructor.  */
14909 
14910 static tree
build_clobber_this()14911 build_clobber_this ()
14912 {
14913   /* Clobbering an empty base is pointless, and harmful if its one byte
14914      TYPE_SIZE overlays real data.  */
14915   if (is_empty_class (current_class_type))
14916     return void_node;
14917 
14918   /* If we have virtual bases, clobber the whole object, but only if we're in
14919      charge.  If we don't have virtual bases, clobber the as-base type so we
14920      don't mess with tail padding.  */
14921   bool vbases = CLASSTYPE_VBASECLASSES (current_class_type);
14922 
14923   tree ctype = current_class_type;
14924   if (!vbases)
14925     ctype = CLASSTYPE_AS_BASE (ctype);
14926 
14927   tree clobber = build_constructor (ctype, NULL);
14928   TREE_THIS_VOLATILE (clobber) = true;
14929 
14930   tree thisref = current_class_ref;
14931   if (ctype != current_class_type)
14932     {
14933       thisref = build_nop (build_reference_type (ctype), current_class_ptr);
14934       thisref = convert_from_reference (thisref);
14935     }
14936 
14937   tree exprstmt = build2 (MODIFY_EXPR, void_type_node, thisref, clobber);
14938   if (vbases)
14939     exprstmt = build_if_in_charge (exprstmt);
14940 
14941   return exprstmt;
14942 }
14943 
14944 /* Create the FUNCTION_DECL for a function definition.
14945    DECLSPECS and DECLARATOR are the parts of the declaration;
14946    they describe the function's name and the type it returns,
14947    but twisted together in a fashion that parallels the syntax of C.
14948 
14949    FLAGS is a bitwise or of SF_PRE_PARSED (indicating that the
14950    DECLARATOR is really the DECL for the function we are about to
14951    process and that DECLSPECS should be ignored), SF_INCLASS_INLINE
14952    indicating that the function is an inline defined in-class.
14953 
14954    This function creates a binding context for the function body
14955    as well as setting up the FUNCTION_DECL in current_function_decl.
14956 
14957    For C++, we must first check whether that datum makes any sense.
14958    For example, "class A local_a(1,2);" means that variable local_a
14959    is an aggregate of type A, which should have a constructor
14960    applied to it with the argument list [1, 2].
14961 
14962    On entry, DECL_INITIAL (decl1) should be NULL_TREE or error_mark_node,
14963    or may be a BLOCK if the function has been defined previously
14964    in this translation unit.  On exit, DECL_INITIAL (decl1) will be
14965    error_mark_node if the function has never been defined, or
14966    a BLOCK if the function has been defined somewhere.  */
14967 
14968 bool
start_preparsed_function(tree decl1,tree attrs,int flags)14969 start_preparsed_function (tree decl1, tree attrs, int flags)
14970 {
14971   tree ctype = NULL_TREE;
14972   tree fntype;
14973   tree restype;
14974   int doing_friend = 0;
14975   cp_binding_level *bl;
14976   tree current_function_parms;
14977   struct c_fileinfo *finfo
14978     = get_fileinfo (LOCATION_FILE (DECL_SOURCE_LOCATION (decl1)));
14979   bool honor_interface;
14980 
14981   /* Sanity check.  */
14982   gcc_assert (VOID_TYPE_P (TREE_VALUE (void_list_node)));
14983   gcc_assert (TREE_CHAIN (void_list_node) == NULL_TREE);
14984 
14985   fntype = TREE_TYPE (decl1);
14986   if (TREE_CODE (fntype) == METHOD_TYPE)
14987     ctype = TYPE_METHOD_BASETYPE (fntype);
14988 
14989   /* ISO C++ 11.4/5.  A friend function defined in a class is in
14990      the (lexical) scope of the class in which it is defined.  */
14991   if (!ctype && DECL_FRIEND_P (decl1))
14992     {
14993       ctype = DECL_FRIEND_CONTEXT (decl1);
14994 
14995       /* CTYPE could be null here if we're dealing with a template;
14996 	 for example, `inline friend float foo()' inside a template
14997 	 will have no CTYPE set.  */
14998       if (ctype && TREE_CODE (ctype) != RECORD_TYPE)
14999 	ctype = NULL_TREE;
15000       else
15001 	doing_friend = 1;
15002     }
15003 
15004   if (DECL_DECLARED_INLINE_P (decl1)
15005       && lookup_attribute ("noinline", attrs))
15006     warning_at (DECL_SOURCE_LOCATION (decl1), 0,
15007 		"inline function %qD given attribute noinline", decl1);
15008 
15009   /* Handle gnu_inline attribute.  */
15010   if (GNU_INLINE_P (decl1))
15011     {
15012       DECL_EXTERNAL (decl1) = 1;
15013       DECL_NOT_REALLY_EXTERN (decl1) = 0;
15014       DECL_INTERFACE_KNOWN (decl1) = 1;
15015       DECL_DISREGARD_INLINE_LIMITS (decl1) = 1;
15016     }
15017 
15018   if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl1))
15019     /* This is a constructor, we must ensure that any default args
15020        introduced by this definition are propagated to the clones
15021        now. The clones are used directly in overload resolution.  */
15022     adjust_clone_args (decl1);
15023 
15024   /* Sometimes we don't notice that a function is a static member, and
15025      build a METHOD_TYPE for it.  Fix that up now.  */
15026   gcc_assert (!(ctype != NULL_TREE && DECL_STATIC_FUNCTION_P (decl1)
15027 		&& TREE_CODE (TREE_TYPE (decl1)) == METHOD_TYPE));
15028 
15029   /* Set up current_class_type, and enter the scope of the class, if
15030      appropriate.  */
15031   if (ctype)
15032     push_nested_class (ctype);
15033   else if (DECL_STATIC_FUNCTION_P (decl1))
15034     push_nested_class (DECL_CONTEXT (decl1));
15035 
15036   /* Now that we have entered the scope of the class, we must restore
15037      the bindings for any template parameters surrounding DECL1, if it
15038      is an inline member template.  (Order is important; consider the
15039      case where a template parameter has the same name as a field of
15040      the class.)  It is not until after this point that
15041      PROCESSING_TEMPLATE_DECL is guaranteed to be set up correctly.  */
15042   if (flags & SF_INCLASS_INLINE)
15043     maybe_begin_member_template_processing (decl1);
15044 
15045   /* Effective C++ rule 15.  */
15046   if (warn_ecpp
15047       && DECL_ASSIGNMENT_OPERATOR_P (decl1)
15048       && DECL_OVERLOADED_OPERATOR_IS (decl1, NOP_EXPR)
15049       && VOID_TYPE_P (TREE_TYPE (fntype)))
15050     warning (OPT_Weffc__,
15051 	     "%<operator=%> should return a reference to %<*this%>");
15052 
15053   /* Make the init_value nonzero so pushdecl knows this is not tentative.
15054      error_mark_node is replaced below (in poplevel) with the BLOCK.  */
15055   if (!DECL_INITIAL (decl1))
15056     DECL_INITIAL (decl1) = error_mark_node;
15057 
15058   /* This function exists in static storage.
15059      (This does not mean `static' in the C sense!)  */
15060   TREE_STATIC (decl1) = 1;
15061 
15062   /* We must call push_template_decl after current_class_type is set
15063      up.  (If we are processing inline definitions after exiting a
15064      class scope, current_class_type will be NULL_TREE until set above
15065      by push_nested_class.)  */
15066   if (processing_template_decl)
15067     {
15068       tree newdecl1 = push_template_decl (decl1);
15069       if (newdecl1 == error_mark_node)
15070 	{
15071 	  if (ctype || DECL_STATIC_FUNCTION_P (decl1))
15072 	    pop_nested_class ();
15073 	  return false;
15074 	}
15075       decl1 = newdecl1;
15076     }
15077 
15078   /* Make sure the parameter and return types are reasonable.  When
15079      you declare a function, these types can be incomplete, but they
15080      must be complete when you define the function.  */
15081   check_function_type (decl1, DECL_ARGUMENTS (decl1));
15082 
15083   /* Build the return declaration for the function.  */
15084   restype = TREE_TYPE (fntype);
15085 
15086   if (DECL_RESULT (decl1) == NULL_TREE)
15087     {
15088       tree resdecl;
15089 
15090       resdecl = build_decl (input_location, RESULT_DECL, 0, restype);
15091       DECL_ARTIFICIAL (resdecl) = 1;
15092       DECL_IGNORED_P (resdecl) = 1;
15093       DECL_RESULT (decl1) = resdecl;
15094 
15095       cp_apply_type_quals_to_decl (cp_type_quals (restype), resdecl);
15096     }
15097 
15098   /* Record the decl so that the function name is defined.
15099      If we already have a decl for this name, and it is a FUNCTION_DECL,
15100      use the old decl.  */
15101   if (!processing_template_decl && !(flags & SF_PRE_PARSED))
15102     {
15103       /* A specialization is not used to guide overload resolution.  */
15104       if (!DECL_FUNCTION_MEMBER_P (decl1)
15105 	  && !(DECL_USE_TEMPLATE (decl1) &&
15106 	       PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl1))))
15107 	{
15108 	  tree olddecl = pushdecl (decl1);
15109 
15110 	  if (olddecl == error_mark_node)
15111 	    /* If something went wrong when registering the declaration,
15112 	       use DECL1; we have to have a FUNCTION_DECL to use when
15113 	       parsing the body of the function.  */
15114 	    ;
15115 	  else
15116 	    {
15117 	      /* Otherwise, OLDDECL is either a previous declaration
15118 		 of the same function or DECL1 itself.  */
15119 
15120 	      if (warn_missing_declarations
15121 		  && olddecl == decl1
15122 		  && !DECL_MAIN_P (decl1)
15123 		  && TREE_PUBLIC (decl1)
15124 		  && !DECL_DECLARED_INLINE_P (decl1))
15125 		{
15126 		  tree context;
15127 
15128 		  /* Check whether DECL1 is in an anonymous
15129 		     namespace.  */
15130 		  for (context = DECL_CONTEXT (decl1);
15131 		       context;
15132 		       context = DECL_CONTEXT (context))
15133 		    {
15134 		      if (TREE_CODE (context) == NAMESPACE_DECL
15135 			  && DECL_NAME (context) == NULL_TREE)
15136 			break;
15137 		    }
15138 
15139 		  if (context == NULL)
15140 		    warning_at (DECL_SOURCE_LOCATION (decl1),
15141 				OPT_Wmissing_declarations,
15142 				"no previous declaration for %qD", decl1);
15143 		}
15144 
15145 	      decl1 = olddecl;
15146 	    }
15147 	}
15148       else
15149 	{
15150 	  /* We need to set the DECL_CONTEXT.  */
15151 	  if (!DECL_CONTEXT (decl1) && DECL_TEMPLATE_INFO (decl1))
15152 	    DECL_CONTEXT (decl1) = DECL_CONTEXT (DECL_TI_TEMPLATE (decl1));
15153 	}
15154       fntype = TREE_TYPE (decl1);
15155       restype = TREE_TYPE (fntype);
15156 
15157       /* If #pragma weak applies, mark the decl appropriately now.
15158 	 The pragma only applies to global functions.  Because
15159 	 determining whether or not the #pragma applies involves
15160 	 computing the mangled name for the declaration, we cannot
15161 	 apply the pragma until after we have merged this declaration
15162 	 with any previous declarations; if the original declaration
15163 	 has a linkage specification, that specification applies to
15164 	 the definition as well, and may affect the mangled name.  */
15165       if (DECL_FILE_SCOPE_P (decl1))
15166 	maybe_apply_pragma_weak (decl1);
15167     }
15168 
15169   /* We are now in the scope of the function being defined.  */
15170   current_function_decl = decl1;
15171 
15172   /* Save the parm names or decls from this function's declarator
15173      where store_parm_decls will find them.  */
15174   current_function_parms = DECL_ARGUMENTS (decl1);
15175 
15176   /* Let the user know we're compiling this function.  */
15177   announce_function (decl1);
15178 
15179   gcc_assert (DECL_INITIAL (decl1));
15180 
15181   /* This function may already have been parsed, in which case just
15182      return; our caller will skip over the body without parsing.  */
15183   if (DECL_INITIAL (decl1) != error_mark_node)
15184     return true;
15185 
15186   /* Initialize RTL machinery.  We cannot do this until
15187      CURRENT_FUNCTION_DECL and DECL_RESULT are set up.  We do this
15188      even when processing a template; this is how we get
15189      CFUN set up, and our per-function variables initialized.
15190      FIXME factor out the non-RTL stuff.  */
15191   bl = current_binding_level;
15192   allocate_struct_function (decl1, processing_template_decl);
15193 
15194   /* Initialize the language data structures.  Whenever we start
15195      a new function, we destroy temporaries in the usual way.  */
15196   cfun->language = ggc_cleared_alloc<language_function> ();
15197   current_stmt_tree ()->stmts_are_full_exprs_p = 1;
15198   current_binding_level = bl;
15199 
15200   if (!processing_template_decl && type_uses_auto (restype))
15201     {
15202       FNDECL_USED_AUTO (decl1) = true;
15203       current_function_auto_return_pattern = restype;
15204     }
15205 
15206   /* Start the statement-tree, start the tree now.  */
15207   DECL_SAVED_TREE (decl1) = push_stmt_list ();
15208 
15209   /* If we are (erroneously) defining a function that we have already
15210      defined before, wipe out what we knew before.  */
15211   if (!DECL_PENDING_INLINE_P (decl1))
15212     DECL_SAVED_FUNCTION_DATA (decl1) = NULL;
15213 
15214   if (ctype && !doing_friend && !DECL_STATIC_FUNCTION_P (decl1))
15215     {
15216       /* We know that this was set up by `grokclassfn'.  We do not
15217 	 wait until `store_parm_decls', since evil parse errors may
15218 	 never get us to that point.  Here we keep the consistency
15219 	 between `current_class_type' and `current_class_ptr'.  */
15220       tree t = DECL_ARGUMENTS (decl1);
15221 
15222       gcc_assert (t != NULL_TREE && TREE_CODE (t) == PARM_DECL);
15223       gcc_assert (TYPE_PTR_P (TREE_TYPE (t)));
15224 
15225       cp_function_chain->x_current_class_ref
15226 	= cp_build_fold_indirect_ref (t);
15227       /* Set this second to avoid shortcut in cp_build_indirect_ref.  */
15228       cp_function_chain->x_current_class_ptr = t;
15229 
15230       /* Constructors and destructors need to know whether they're "in
15231 	 charge" of initializing virtual base classes.  */
15232       t = DECL_CHAIN (t);
15233       if (DECL_HAS_IN_CHARGE_PARM_P (decl1))
15234 	{
15235 	  current_in_charge_parm = t;
15236 	  t = DECL_CHAIN (t);
15237 	}
15238       if (DECL_HAS_VTT_PARM_P (decl1))
15239 	{
15240 	  gcc_assert (DECL_NAME (t) == vtt_parm_identifier);
15241 	  current_vtt_parm = t;
15242 	}
15243     }
15244 
15245   honor_interface = (!DECL_TEMPLATE_INSTANTIATION (decl1)
15246 		     /* Implicitly-defined methods (like the
15247 			destructor for a class in which no destructor
15248 			is explicitly declared) must not be defined
15249 			until their definition is needed.  So, we
15250 			ignore interface specifications for
15251 			compiler-generated functions.  */
15252 		     && !DECL_ARTIFICIAL (decl1));
15253 
15254   if (processing_template_decl)
15255     /* Don't mess with interface flags.  */;
15256   else if (DECL_INTERFACE_KNOWN (decl1))
15257     {
15258       tree ctx = decl_function_context (decl1);
15259 
15260       if (DECL_NOT_REALLY_EXTERN (decl1))
15261 	DECL_EXTERNAL (decl1) = 0;
15262 
15263       if (ctx != NULL_TREE && vague_linkage_p (ctx))
15264 	/* This is a function in a local class in an extern inline
15265 	   or template function.  */
15266 	comdat_linkage (decl1);
15267     }
15268   /* If this function belongs to an interface, it is public.
15269      If it belongs to someone else's interface, it is also external.
15270      This only affects inlines and template instantiations.  */
15271   else if (!finfo->interface_unknown && honor_interface)
15272     {
15273       if (DECL_DECLARED_INLINE_P (decl1)
15274 	  || DECL_TEMPLATE_INSTANTIATION (decl1))
15275 	{
15276 	  DECL_EXTERNAL (decl1)
15277 	    = (finfo->interface_only
15278 	       || (DECL_DECLARED_INLINE_P (decl1)
15279 		   && ! flag_implement_inlines
15280 		   && !DECL_VINDEX (decl1)));
15281 
15282 	  /* For WIN32 we also want to put these in linkonce sections.  */
15283 	  maybe_make_one_only (decl1);
15284 	}
15285       else
15286 	DECL_EXTERNAL (decl1) = 0;
15287       DECL_INTERFACE_KNOWN (decl1) = 1;
15288       /* If this function is in an interface implemented in this file,
15289 	 make sure that the back end knows to emit this function
15290 	 here.  */
15291       if (!DECL_EXTERNAL (decl1))
15292 	mark_needed (decl1);
15293     }
15294   else if (finfo->interface_unknown && finfo->interface_only
15295 	   && honor_interface)
15296     {
15297       /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma
15298 	 interface, we will have both finfo->interface_unknown and
15299 	 finfo->interface_only set.  In that case, we don't want to
15300 	 use the normal heuristics because someone will supply a
15301 	 #pragma implementation elsewhere, and deducing it here would
15302 	 produce a conflict.  */
15303       comdat_linkage (decl1);
15304       DECL_EXTERNAL (decl1) = 0;
15305       DECL_INTERFACE_KNOWN (decl1) = 1;
15306       DECL_DEFER_OUTPUT (decl1) = 1;
15307     }
15308   else
15309     {
15310       /* This is a definition, not a reference.
15311 	 So clear DECL_EXTERNAL, unless this is a GNU extern inline.  */
15312       if (!GNU_INLINE_P (decl1))
15313 	DECL_EXTERNAL (decl1) = 0;
15314 
15315       if ((DECL_DECLARED_INLINE_P (decl1)
15316 	   || DECL_TEMPLATE_INSTANTIATION (decl1))
15317 	  && ! DECL_INTERFACE_KNOWN (decl1))
15318 	DECL_DEFER_OUTPUT (decl1) = 1;
15319       else
15320 	DECL_INTERFACE_KNOWN (decl1) = 1;
15321     }
15322 
15323   /* Determine the ELF visibility attribute for the function.  We must not
15324      do this before calling "pushdecl", as we must allow "duplicate_decls"
15325      to merge any attributes appropriately.  We also need to wait until
15326      linkage is set.  */
15327   if (!DECL_CLONED_FUNCTION_P (decl1))
15328     determine_visibility (decl1);
15329 
15330   if (!processing_template_decl)
15331     maybe_instantiate_noexcept (decl1);
15332 
15333   begin_scope (sk_function_parms, decl1);
15334 
15335   ++function_depth;
15336 
15337   if (DECL_DESTRUCTOR_P (decl1)
15338       || (DECL_CONSTRUCTOR_P (decl1)
15339 	  && targetm.cxx.cdtor_returns_this ()))
15340     {
15341       cdtor_label = create_artificial_label (input_location);
15342       LABEL_DECL_CDTOR (cdtor_label) = true;
15343     }
15344 
15345   start_fname_decls ();
15346 
15347   store_parm_decls (current_function_parms);
15348 
15349   if (!processing_template_decl
15350       && (flag_lifetime_dse > 1)
15351       && DECL_CONSTRUCTOR_P (decl1)
15352       && !DECL_CLONED_FUNCTION_P (decl1)
15353       /* Clobbering an empty base is harmful if it overlays real data.  */
15354       && !is_empty_class (current_class_type)
15355       /* We can't clobber safely for an implicitly-defined default constructor
15356 	 because part of the initialization might happen before we enter the
15357 	 constructor, via AGGR_INIT_ZERO_FIRST (c++/68006).  */
15358       && !implicit_default_ctor_p (decl1))
15359     finish_expr_stmt (build_clobber_this ());
15360 
15361   if (!processing_template_decl
15362       && DECL_CONSTRUCTOR_P (decl1)
15363       && sanitize_flags_p (SANITIZE_VPTR)
15364       && !DECL_CLONED_FUNCTION_P (decl1)
15365       && !implicit_default_ctor_p (decl1))
15366     cp_ubsan_maybe_initialize_vtbl_ptrs (current_class_ptr);
15367 
15368   if (!DECL_OMP_DECLARE_REDUCTION_P (decl1))
15369     start_lambda_scope (decl1);
15370 
15371   return true;
15372 }
15373 
15374 
15375 /* Like start_preparsed_function, except that instead of a
15376    FUNCTION_DECL, this function takes DECLSPECS and DECLARATOR.
15377 
15378    Returns true on success.  If the DECLARATOR is not suitable
15379    for a function, we return false, which tells the parser to
15380    skip the entire function.  */
15381 
15382 bool
start_function(cp_decl_specifier_seq * declspecs,const cp_declarator * declarator,tree attrs)15383 start_function (cp_decl_specifier_seq *declspecs,
15384 		const cp_declarator *declarator,
15385 		tree attrs)
15386 {
15387   tree decl1;
15388 
15389   decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, &attrs);
15390   invoke_plugin_callbacks (PLUGIN_START_PARSE_FUNCTION, decl1);
15391   if (decl1 == error_mark_node)
15392     return false;
15393   /* If the declarator is not suitable for a function definition,
15394      cause a syntax error.  */
15395   if (decl1 == NULL_TREE || TREE_CODE (decl1) != FUNCTION_DECL)
15396     {
15397       error ("invalid function declaration");
15398       return false;
15399     }
15400 
15401   if (DECL_MAIN_P (decl1))
15402     /* main must return int.  grokfndecl should have corrected it
15403        (and issued a diagnostic) if the user got it wrong.  */
15404     gcc_assert (same_type_p (TREE_TYPE (TREE_TYPE (decl1)),
15405 			     integer_type_node));
15406 
15407   return start_preparsed_function (decl1, attrs, /*flags=*/SF_DEFAULT);
15408 }
15409 
15410 /* Returns true iff an EH_SPEC_BLOCK should be created in the body of
15411    FN.  */
15412 
15413 static bool
use_eh_spec_block(tree fn)15414 use_eh_spec_block (tree fn)
15415 {
15416   return (flag_exceptions && flag_enforce_eh_specs
15417 	  && !processing_template_decl
15418 	  && !type_throw_all_p (TREE_TYPE (fn))
15419 	  /* We insert the EH_SPEC_BLOCK only in the original
15420 	     function; then, it is copied automatically to the
15421 	     clones.  */
15422 	  && !DECL_CLONED_FUNCTION_P (fn)
15423 	  /* Implicitly-generated constructors and destructors have
15424 	     exception specifications.  However, those specifications
15425 	     are the union of the possible exceptions specified by the
15426 	     constructors/destructors for bases and members, so no
15427 	     unallowed exception will ever reach this function.  By
15428 	     not creating the EH_SPEC_BLOCK we save a little memory,
15429 	     and we avoid spurious warnings about unreachable
15430 	     code.  */
15431 	  && !DECL_DEFAULTED_FN (fn));
15432 }
15433 
15434 /* Store the parameter declarations into the current function declaration.
15435    This is called after parsing the parameter declarations, before
15436    digesting the body of the function.
15437 
15438    Also install to binding contour return value identifier, if any.  */
15439 
15440 static void
store_parm_decls(tree current_function_parms)15441 store_parm_decls (tree current_function_parms)
15442 {
15443   tree fndecl = current_function_decl;
15444   tree parm;
15445 
15446   /* This is a chain of any other decls that came in among the parm
15447      declarations.  If a parm is declared with  enum {foo, bar} x;
15448      then CONST_DECLs for foo and bar are put here.  */
15449   tree nonparms = NULL_TREE;
15450 
15451   if (current_function_parms)
15452     {
15453       /* This case is when the function was defined with an ANSI prototype.
15454 	 The parms already have decls, so we need not do anything here
15455 	 except record them as in effect
15456 	 and complain if any redundant old-style parm decls were written.  */
15457 
15458       tree specparms = current_function_parms;
15459       tree next;
15460 
15461       /* Must clear this because it might contain TYPE_DECLs declared
15462 	     at class level.  */
15463       current_binding_level->names = NULL;
15464 
15465       /* If we're doing semantic analysis, then we'll call pushdecl
15466 	     for each of these.  We must do them in reverse order so that
15467 	     they end in the correct forward order.  */
15468       specparms = nreverse (specparms);
15469 
15470       for (parm = specparms; parm; parm = next)
15471 	{
15472 	  next = DECL_CHAIN (parm);
15473 	  if (TREE_CODE (parm) == PARM_DECL)
15474 	    pushdecl (parm);
15475 	  else
15476 	    {
15477 	      /* If we find an enum constant or a type tag,
15478 		 put it aside for the moment.  */
15479 	      TREE_CHAIN (parm) = NULL_TREE;
15480 	      nonparms = chainon (nonparms, parm);
15481 	    }
15482 	}
15483 
15484       /* Get the decls in their original chain order and record in the
15485 	 function.  This is all and only the PARM_DECLs that were
15486 	 pushed into scope by the loop above.  */
15487       DECL_ARGUMENTS (fndecl) = get_local_decls ();
15488     }
15489   else
15490     DECL_ARGUMENTS (fndecl) = NULL_TREE;
15491 
15492   /* Now store the final chain of decls for the arguments
15493      as the decl-chain of the current lexical scope.
15494      Put the enumerators in as well, at the front so that
15495      DECL_ARGUMENTS is not modified.  */
15496   current_binding_level->names = chainon (nonparms, DECL_ARGUMENTS (fndecl));
15497 
15498   if (use_eh_spec_block (current_function_decl))
15499     current_eh_spec_block = begin_eh_spec_block ();
15500 }
15501 
15502 
15503 /* We have finished doing semantic analysis on DECL, but have not yet
15504    generated RTL for its body.  Save away our current state, so that
15505    when we want to generate RTL later we know what to do.  */
15506 
15507 static void
save_function_data(tree decl)15508 save_function_data (tree decl)
15509 {
15510   struct language_function *f;
15511 
15512   /* Save the language-specific per-function data so that we can
15513      get it back when we really expand this function.  */
15514   gcc_assert (!DECL_PENDING_INLINE_P (decl));
15515 
15516   /* Make a copy.  */
15517   f = ggc_alloc<language_function> ();
15518   memcpy (f, cp_function_chain, sizeof (struct language_function));
15519   DECL_SAVED_FUNCTION_DATA (decl) = f;
15520 
15521   /* Clear out the bits we don't need.  */
15522   f->base.x_stmt_tree.x_cur_stmt_list = NULL;
15523   f->bindings = NULL;
15524   f->x_local_names = NULL;
15525   f->base.local_typedefs = NULL;
15526 }
15527 
15528 
15529 /* Set the return value of the constructor (if present).  */
15530 
15531 static void
finish_constructor_body(void)15532 finish_constructor_body (void)
15533 {
15534   tree val;
15535   tree exprstmt;
15536 
15537   if (targetm.cxx.cdtor_returns_this ())
15538     {
15539       /* Any return from a constructor will end up here.  */
15540       add_stmt (build_stmt (input_location, LABEL_EXPR, cdtor_label));
15541 
15542       val = DECL_ARGUMENTS (current_function_decl);
15543       val = build2 (MODIFY_EXPR, TREE_TYPE (val),
15544 		    DECL_RESULT (current_function_decl), val);
15545       /* Return the address of the object.  */
15546       exprstmt = build_stmt (input_location, RETURN_EXPR, val);
15547       add_stmt (exprstmt);
15548     }
15549 }
15550 
15551 /* Do all the processing for the beginning of a destructor; set up the
15552    vtable pointers and cleanups for bases and members.  */
15553 
15554 static void
begin_destructor_body(void)15555 begin_destructor_body (void)
15556 {
15557   tree compound_stmt;
15558 
15559   /* If the CURRENT_CLASS_TYPE is incomplete, we will have already
15560      issued an error message.  We still want to try to process the
15561      body of the function, but initialize_vtbl_ptrs will crash if
15562      TYPE_BINFO is NULL.  */
15563   if (COMPLETE_TYPE_P (current_class_type))
15564     {
15565       compound_stmt = begin_compound_stmt (0);
15566       /* Make all virtual function table pointers in non-virtual base
15567 	 classes point to CURRENT_CLASS_TYPE's virtual function
15568 	 tables.  */
15569       initialize_vtbl_ptrs (current_class_ptr);
15570       finish_compound_stmt (compound_stmt);
15571 
15572       if (flag_lifetime_dse
15573 	  /* Clobbering an empty base is harmful if it overlays real data.  */
15574 	  && !is_empty_class (current_class_type))
15575       {
15576 	if (sanitize_flags_p (SANITIZE_VPTR)
15577 	    && (flag_sanitize_recover & SANITIZE_VPTR) == 0
15578 	    && TYPE_CONTAINS_VPTR_P (current_class_type))
15579 	  {
15580 	    tree binfo = TYPE_BINFO (current_class_type);
15581 	    tree ref
15582 	      = cp_build_fold_indirect_ref (current_class_ptr);
15583 
15584 	    tree vtbl_ptr = build_vfield_ref (ref, TREE_TYPE (binfo));
15585 	    tree vtbl = build_zero_cst (TREE_TYPE (vtbl_ptr));
15586 	    tree stmt = cp_build_modify_expr (input_location, vtbl_ptr,
15587 					      NOP_EXPR, vtbl,
15588 					      tf_warning_or_error);
15589 	    /* If the vptr is shared with some virtual nearly empty base,
15590 	       don't clear it if not in charge, the dtor of the virtual
15591 	       nearly empty base will do that later.  */
15592 	    if (CLASSTYPE_VBASECLASSES (current_class_type)
15593 		&& CLASSTYPE_PRIMARY_BINFO (current_class_type)
15594 		&& BINFO_VIRTUAL_P
15595 			  (CLASSTYPE_PRIMARY_BINFO (current_class_type)))
15596 	      {
15597 		stmt = convert_to_void (stmt, ICV_STATEMENT,
15598 					tf_warning_or_error);
15599 		stmt = build_if_in_charge (stmt);
15600 	      }
15601 	    finish_decl_cleanup (NULL_TREE, stmt);
15602 	  }
15603 	else
15604 	  finish_decl_cleanup (NULL_TREE, build_clobber_this ());
15605       }
15606 
15607       /* And insert cleanups for our bases and members so that they
15608 	 will be properly destroyed if we throw.  */
15609       push_base_cleanups ();
15610     }
15611 }
15612 
15613 /* At the end of every destructor we generate code to delete the object if
15614    necessary.  Do that now.  */
15615 
15616 static void
finish_destructor_body(void)15617 finish_destructor_body (void)
15618 {
15619   tree exprstmt;
15620 
15621   /* Any return from a destructor will end up here; that way all base
15622      and member cleanups will be run when the function returns.  */
15623   add_stmt (build_stmt (input_location, LABEL_EXPR, cdtor_label));
15624 
15625   if (targetm.cxx.cdtor_returns_this ())
15626     {
15627       tree val;
15628 
15629       val = DECL_ARGUMENTS (current_function_decl);
15630       val = build2 (MODIFY_EXPR, TREE_TYPE (val),
15631 		    DECL_RESULT (current_function_decl), val);
15632       /* Return the address of the object.  */
15633       exprstmt = build_stmt (input_location, RETURN_EXPR, val);
15634       add_stmt (exprstmt);
15635     }
15636 }
15637 
15638 /* Do the necessary processing for the beginning of a function body, which
15639    in this case includes member-initializers, but not the catch clauses of
15640    a function-try-block.  Currently, this means opening a binding level
15641    for the member-initializers (in a ctor), member cleanups (in a dtor),
15642    and capture proxies (in a lambda operator()).  */
15643 
15644 tree
begin_function_body(void)15645 begin_function_body (void)
15646 {
15647   tree stmt;
15648 
15649   if (! FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
15650     return NULL_TREE;
15651 
15652   if (processing_template_decl)
15653     /* Do nothing now.  */;
15654   else
15655     /* Always keep the BLOCK node associated with the outermost pair of
15656        curly braces of a function.  These are needed for correct
15657        operation of dwarfout.c.  */
15658     keep_next_level (true);
15659 
15660   stmt = begin_compound_stmt (BCS_FN_BODY);
15661 
15662   if (processing_template_decl)
15663     /* Do nothing now.  */;
15664   else if (DECL_DESTRUCTOR_P (current_function_decl))
15665     begin_destructor_body ();
15666 
15667   return stmt;
15668 }
15669 
15670 /* Do the processing for the end of a function body.  Currently, this means
15671    closing out the cleanups for fully-constructed bases and members, and in
15672    the case of the destructor, deleting the object if desired.  Again, this
15673    is only meaningful for [cd]tors, since they are the only functions where
15674    there is a significant distinction between the main body and any
15675    function catch clauses.  Handling, say, main() return semantics here
15676    would be wrong, as flowing off the end of a function catch clause for
15677    main() would also need to return 0.  */
15678 
15679 void
finish_function_body(tree compstmt)15680 finish_function_body (tree compstmt)
15681 {
15682   if (compstmt == NULL_TREE)
15683     return;
15684 
15685   /* Close the block.  */
15686   finish_compound_stmt (compstmt);
15687 
15688   if (processing_template_decl)
15689     /* Do nothing now.  */;
15690   else if (DECL_CONSTRUCTOR_P (current_function_decl))
15691     finish_constructor_body ();
15692   else if (DECL_DESTRUCTOR_P (current_function_decl))
15693     finish_destructor_body ();
15694 }
15695 
15696 /* Given a function, returns the BLOCK corresponding to the outermost level
15697    of curly braces, skipping the artificial block created for constructor
15698    initializers.  */
15699 
15700 tree
outer_curly_brace_block(tree fndecl)15701 outer_curly_brace_block (tree fndecl)
15702 {
15703   tree block = DECL_INITIAL (fndecl);
15704   if (BLOCK_OUTER_CURLY_BRACE_P (block))
15705     return block;
15706   block = BLOCK_SUBBLOCKS (block);
15707   if (BLOCK_OUTER_CURLY_BRACE_P (block))
15708     return block;
15709   block = BLOCK_SUBBLOCKS (block);
15710   gcc_assert (BLOCK_OUTER_CURLY_BRACE_P (block));
15711   return block;
15712 }
15713 
15714 /* If FNDECL is a class's key method, add the class to the list of
15715    keyed classes that should be emitted.  */
15716 
15717 static void
record_key_method_defined(tree fndecl)15718 record_key_method_defined (tree fndecl)
15719 {
15720   if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fndecl)
15721       && DECL_VIRTUAL_P (fndecl)
15722       && !processing_template_decl)
15723     {
15724       tree fnclass = DECL_CONTEXT (fndecl);
15725       if (fndecl == CLASSTYPE_KEY_METHOD (fnclass))
15726 	vec_safe_push (keyed_classes, fnclass);
15727     }
15728 }
15729 
15730 /* Subroutine of finish_function.
15731    Save the body of constexpr functions for possible
15732    future compile time evaluation.  */
15733 
15734 static void
maybe_save_function_definition(tree fun)15735 maybe_save_function_definition (tree fun)
15736 {
15737   if (!processing_template_decl
15738       && DECL_DECLARED_CONSTEXPR_P (fun)
15739       && !cp_function_chain->invalid_constexpr
15740       && !DECL_CLONED_FUNCTION_P (fun))
15741     register_constexpr_fundef (fun, DECL_SAVED_TREE (fun));
15742 }
15743 
15744 /* Finish up a function declaration and compile that function
15745    all the way to assembler language output.  The free the storage
15746    for the function definition. INLINE_P is TRUE if we just
15747    finished processing the body of an in-class inline function
15748    definition.  (This processing will have taken place after the
15749    class definition is complete.)  */
15750 
15751 tree
finish_function(bool inline_p)15752 finish_function (bool inline_p)
15753 {
15754   tree fndecl = current_function_decl;
15755   tree fntype, ctype = NULL_TREE;
15756 
15757   /* When we get some parse errors, we can end up without a
15758      current_function_decl, so cope.  */
15759   if (fndecl == NULL_TREE)
15760     return error_mark_node;
15761 
15762   if (!DECL_OMP_DECLARE_REDUCTION_P (fndecl))
15763     finish_lambda_scope ();
15764 
15765   if (c_dialect_objc ())
15766     objc_finish_function ();
15767 
15768   record_key_method_defined (fndecl);
15769 
15770   fntype = TREE_TYPE (fndecl);
15771 
15772   /*  TREE_READONLY (fndecl) = 1;
15773       This caused &foo to be of type ptr-to-const-function
15774       which then got a warning when stored in a ptr-to-function variable.  */
15775 
15776   gcc_assert (building_stmt_list_p ());
15777   /* The current function is being defined, so its DECL_INITIAL should
15778      be set, and unless there's a multiple definition, it should be
15779      error_mark_node.  */
15780   gcc_assert (DECL_INITIAL (fndecl) == error_mark_node);
15781 
15782   /* For a cloned function, we've already got all the code we need;
15783      there's no need to add any extra bits.  */
15784   if (!DECL_CLONED_FUNCTION_P (fndecl))
15785     {
15786       /* Make it so that `main' always returns 0 by default.  */
15787       if (DECL_MAIN_P (current_function_decl))
15788 	finish_return_stmt (integer_zero_node);
15789 
15790       if (use_eh_spec_block (current_function_decl))
15791 	finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
15792 			      (TREE_TYPE (current_function_decl)),
15793 			      current_eh_spec_block);
15794     }
15795 
15796   /* If we're saving up tree structure, tie off the function now.  */
15797   DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
15798 
15799   finish_fname_decls ();
15800 
15801   /* If this function can't throw any exceptions, remember that.  */
15802   if (!processing_template_decl
15803       && !cp_function_chain->can_throw
15804       && !flag_non_call_exceptions
15805       && !decl_replaceable_p (fndecl))
15806     TREE_NOTHROW (fndecl) = 1;
15807 
15808   /* This must come after expand_function_end because cleanups might
15809      have declarations (from inline functions) that need to go into
15810      this function's blocks.  */
15811 
15812   /* If the current binding level isn't the outermost binding level
15813      for this function, either there is a bug, or we have experienced
15814      syntax errors and the statement tree is malformed.  */
15815   if (current_binding_level->kind != sk_function_parms)
15816     {
15817       /* Make sure we have already experienced errors.  */
15818       gcc_assert (errorcount);
15819 
15820       /* Throw away the broken statement tree and extra binding
15821 	 levels.  */
15822       DECL_SAVED_TREE (fndecl) = alloc_stmt_list ();
15823 
15824       while (current_binding_level->kind != sk_function_parms)
15825 	{
15826 	  if (current_binding_level->kind == sk_class)
15827 	    pop_nested_class ();
15828 	  else
15829 	    poplevel (0, 0, 0);
15830 	}
15831     }
15832   poplevel (1, 0, 1);
15833 
15834   /* Statements should always be full-expressions at the outermost set
15835      of curly braces for a function.  */
15836   gcc_assert (stmts_are_full_exprs_p ());
15837 
15838   /* If there are no return statements in a function with auto return type,
15839      the return type is void.  But if the declared type is something like
15840      auto*, this is an error.  */
15841   if (!processing_template_decl && FNDECL_USED_AUTO (fndecl)
15842       && TREE_TYPE (fntype) == current_function_auto_return_pattern)
15843     {
15844       if (is_auto (current_function_auto_return_pattern))
15845 	{
15846 	  apply_deduced_return_type (fndecl, void_type_node);
15847 	  fntype = TREE_TYPE (fndecl);
15848 	}
15849       else if (!current_function_returns_value
15850 	       && !current_function_returns_null)
15851 	{
15852 	  error ("no return statements in function returning %qT",
15853 		 current_function_auto_return_pattern);
15854 	  inform (input_location, "only plain %<auto%> return type can be "
15855 		  "deduced to %<void%>");
15856 	}
15857     }
15858 
15859   // If this is a concept, check that the definition is reasonable.
15860   if (DECL_DECLARED_CONCEPT_P (fndecl))
15861     check_function_concept (fndecl);
15862 
15863   /* Lambda closure members are implicitly constexpr if possible.  */
15864   if (cxx_dialect >= cxx17
15865       && LAMBDA_TYPE_P (CP_DECL_CONTEXT (fndecl)))
15866     DECL_DECLARED_CONSTEXPR_P (fndecl)
15867       = ((processing_template_decl
15868 	  || is_valid_constexpr_fn (fndecl, /*complain*/false))
15869 	 && potential_constant_expression (DECL_SAVED_TREE (fndecl)));
15870 
15871   /* Save constexpr function body before it gets munged by
15872      the NRV transformation.   */
15873   maybe_save_function_definition (fndecl);
15874 
15875   /* Invoke the pre-genericize plugin before we start munging things.  */
15876   if (!processing_template_decl)
15877     invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE, fndecl);
15878 
15879   /* Perform delayed folding before NRV transformation.  */
15880   if (!processing_template_decl && !DECL_OMP_DECLARE_REDUCTION_P (fndecl))
15881     cp_fold_function (fndecl);
15882 
15883   /* Set up the named return value optimization, if we can.  Candidate
15884      variables are selected in check_return_expr.  */
15885   if (current_function_return_value)
15886     {
15887       tree r = current_function_return_value;
15888       tree outer;
15889 
15890       if (r != error_mark_node
15891 	  /* This is only worth doing for fns that return in memory--and
15892 	     simpler, since we don't have to worry about promoted modes.  */
15893 	  && aggregate_value_p (TREE_TYPE (TREE_TYPE (fndecl)), fndecl)
15894 	  /* Only allow this for variables declared in the outer scope of
15895 	     the function so we know that their lifetime always ends with a
15896 	     return; see g++.dg/opt/nrv6.C.  We could be more flexible if
15897 	     we were to do this optimization in tree-ssa.  */
15898 	  && (outer = outer_curly_brace_block (fndecl))
15899 	  && chain_member (r, BLOCK_VARS (outer)))
15900 	finalize_nrv (&DECL_SAVED_TREE (fndecl), r, DECL_RESULT (fndecl));
15901 
15902       current_function_return_value = NULL_TREE;
15903     }
15904 
15905   /* Remember that we were in class scope.  */
15906   if (current_class_name)
15907     ctype = current_class_type;
15908 
15909   /* Must mark the RESULT_DECL as being in this function.  */
15910   DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
15911 
15912   /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point
15913      to the FUNCTION_DECL node itself.  */
15914   BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
15915 
15916   /* Save away current state, if appropriate.  */
15917   if (!processing_template_decl)
15918     save_function_data (fndecl);
15919 
15920   /* Complain if there's just no return statement.  */
15921   if (warn_return_type
15922       && !VOID_TYPE_P (TREE_TYPE (fntype))
15923       && !dependent_type_p (TREE_TYPE (fntype))
15924       && !current_function_returns_value && !current_function_returns_null
15925       /* Don't complain if we abort or throw.  */
15926       && !current_function_returns_abnormally
15927       /* Don't complain if there's an infinite loop.  */
15928       && !current_function_infinite_loop
15929       /* Don't complain if we are declared noreturn.  */
15930       && !TREE_THIS_VOLATILE (fndecl)
15931       && !DECL_NAME (DECL_RESULT (fndecl))
15932       && !TREE_NO_WARNING (fndecl)
15933       /* Structor return values (if any) are set by the compiler.  */
15934       && !DECL_CONSTRUCTOR_P (fndecl)
15935       && !DECL_DESTRUCTOR_P (fndecl)
15936       && targetm.warn_func_return (fndecl))
15937     {
15938       warning (OPT_Wreturn_type,
15939  	       "no return statement in function returning non-void");
15940       TREE_NO_WARNING (fndecl) = 1;
15941     }
15942 
15943   /* Store the end of the function, so that we get good line number
15944      info for the epilogue.  */
15945   cfun->function_end_locus = input_location;
15946 
15947   /* Complain about parameters that are only set, but never otherwise used.  */
15948   if (warn_unused_but_set_parameter
15949       && !processing_template_decl
15950       && errorcount == unused_but_set_errorcount
15951       && !DECL_CLONED_FUNCTION_P (fndecl))
15952     {
15953       tree decl;
15954 
15955       for (decl = DECL_ARGUMENTS (fndecl);
15956 	   decl;
15957 	   decl = DECL_CHAIN (decl))
15958 	if (TREE_USED (decl)
15959 	    && TREE_CODE (decl) == PARM_DECL
15960 	    && !DECL_READ_P (decl)
15961 	    && DECL_NAME (decl)
15962 	    && !DECL_ARTIFICIAL (decl)
15963 	    && !TREE_NO_WARNING (decl)
15964 	    && !DECL_IN_SYSTEM_HEADER (decl)
15965 	    && TREE_TYPE (decl) != error_mark_node
15966 	    && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
15967 	    && (!CLASS_TYPE_P (TREE_TYPE (decl))
15968 	        || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl))))
15969 	  warning_at (DECL_SOURCE_LOCATION (decl),
15970 		      OPT_Wunused_but_set_parameter,
15971 		      "parameter %qD set but not used", decl);
15972       unused_but_set_errorcount = errorcount;
15973     }
15974 
15975   /* Complain about locally defined typedefs that are not used in this
15976      function.  */
15977   maybe_warn_unused_local_typedefs ();
15978 
15979   /* Possibly warn about unused parameters.  */
15980   if (warn_unused_parameter
15981       && !processing_template_decl
15982       && !DECL_CLONED_FUNCTION_P (fndecl))
15983     do_warn_unused_parameter (fndecl);
15984 
15985   /* Genericize before inlining.  */
15986   if (!processing_template_decl)
15987     {
15988       struct language_function *f = DECL_SAVED_FUNCTION_DATA (fndecl);
15989       if (!DECL_OMP_DECLARE_REDUCTION_P (fndecl))
15990 	cp_genericize (fndecl);
15991       /* Clear out the bits we don't need.  */
15992       f->x_current_class_ptr = NULL;
15993       f->x_current_class_ref = NULL;
15994       f->x_eh_spec_block = NULL;
15995       f->x_in_charge_parm = NULL;
15996       f->x_vtt_parm = NULL;
15997       f->x_return_value = NULL;
15998       f->bindings = NULL;
15999       f->extern_decl_map = NULL;
16000       f->infinite_loops = NULL;
16001     }
16002   /* Clear out the bits we don't need.  */
16003   local_names = NULL;
16004 
16005   /* We're leaving the context of this function, so zap cfun.  It's still in
16006      DECL_STRUCT_FUNCTION, and we'll restore it in tree_rest_of_compilation.  */
16007   set_cfun (NULL);
16008   current_function_decl = NULL;
16009 
16010   /* If this is an in-class inline definition, we may have to pop the
16011      bindings for the template parameters that we added in
16012      maybe_begin_member_template_processing when start_function was
16013      called.  */
16014   if (inline_p)
16015     maybe_end_member_template_processing ();
16016 
16017   /* Leave the scope of the class.  */
16018   if (ctype)
16019     pop_nested_class ();
16020 
16021   --function_depth;
16022 
16023   /* Clean up.  */
16024   current_function_decl = NULL_TREE;
16025 
16026   invoke_plugin_callbacks (PLUGIN_FINISH_PARSE_FUNCTION, fndecl);
16027   return fndecl;
16028 }
16029 
16030 /* Create the FUNCTION_DECL for a function definition.
16031    DECLSPECS and DECLARATOR are the parts of the declaration;
16032    they describe the return type and the name of the function,
16033    but twisted together in a fashion that parallels the syntax of C.
16034 
16035    This function creates a binding context for the function body
16036    as well as setting up the FUNCTION_DECL in current_function_decl.
16037 
16038    Returns a FUNCTION_DECL on success.
16039 
16040    If the DECLARATOR is not suitable for a function (it defines a datum
16041    instead), we return 0, which tells yyparse to report a parse error.
16042 
16043    May return void_type_node indicating that this method is actually
16044    a friend.  See grokfield for more details.
16045 
16046    Came here with a `.pushlevel' .
16047 
16048    DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
16049    CHANGES TO CODE IN `grokfield'.  */
16050 
16051 tree
grokmethod(cp_decl_specifier_seq * declspecs,const cp_declarator * declarator,tree attrlist)16052 grokmethod (cp_decl_specifier_seq *declspecs,
16053 	    const cp_declarator *declarator, tree attrlist)
16054 {
16055   tree fndecl = grokdeclarator (declarator, declspecs, MEMFUNCDEF, 0,
16056 				&attrlist);
16057 
16058   if (fndecl == error_mark_node)
16059     return error_mark_node;
16060 
16061   if (fndecl == NULL || TREE_CODE (fndecl) != FUNCTION_DECL)
16062     {
16063       error ("invalid member function declaration");
16064       return error_mark_node;
16065     }
16066 
16067   if (attrlist)
16068     cplus_decl_attributes (&fndecl, attrlist, 0);
16069 
16070   /* Pass friends other than inline friend functions back.  */
16071   if (fndecl == void_type_node)
16072     return fndecl;
16073 
16074   if (DECL_IN_AGGR_P (fndecl))
16075     {
16076       if (DECL_CLASS_SCOPE_P (fndecl))
16077 	error ("%qD is already defined in class %qT", fndecl,
16078 	       DECL_CONTEXT (fndecl));
16079       return error_mark_node;
16080     }
16081 
16082   check_template_shadow (fndecl);
16083 
16084   if (TREE_PUBLIC (fndecl))
16085     DECL_COMDAT (fndecl) = 1;
16086   DECL_DECLARED_INLINE_P (fndecl) = 1;
16087   DECL_NO_INLINE_WARNING_P (fndecl) = 1;
16088 
16089   /* We process method specializations in finish_struct_1.  */
16090   if (processing_template_decl && !DECL_TEMPLATE_SPECIALIZATION (fndecl))
16091     {
16092       fndecl = push_template_decl (fndecl);
16093       if (fndecl == error_mark_node)
16094 	return fndecl;
16095     }
16096 
16097   if (! DECL_FRIEND_P (fndecl))
16098     {
16099       if (DECL_CHAIN (fndecl))
16100 	{
16101 	  fndecl = copy_node (fndecl);
16102 	  TREE_CHAIN (fndecl) = NULL_TREE;
16103 	}
16104     }
16105 
16106   cp_finish_decl (fndecl, NULL_TREE, false, NULL_TREE, 0);
16107 
16108   DECL_IN_AGGR_P (fndecl) = 1;
16109   return fndecl;
16110 }
16111 
16112 
16113 /* VAR is a VAR_DECL.  If its type is incomplete, remember VAR so that
16114    we can lay it out later, when and if its type becomes complete.
16115 
16116    Also handle constexpr variables where the initializer involves
16117    an unlowered PTRMEM_CST because the class isn't complete yet.  */
16118 
16119 void
maybe_register_incomplete_var(tree var)16120 maybe_register_incomplete_var (tree var)
16121 {
16122   gcc_assert (VAR_P (var));
16123 
16124   /* Keep track of variables with incomplete types.  */
16125   if (!processing_template_decl && TREE_TYPE (var) != error_mark_node
16126       && DECL_EXTERNAL (var))
16127     {
16128       tree inner_type = TREE_TYPE (var);
16129 
16130       while (TREE_CODE (inner_type) == ARRAY_TYPE)
16131 	inner_type = TREE_TYPE (inner_type);
16132       inner_type = TYPE_MAIN_VARIANT (inner_type);
16133 
16134       if ((!COMPLETE_TYPE_P (inner_type) && CLASS_TYPE_P (inner_type))
16135 	  /* RTTI TD entries are created while defining the type_info.  */
16136 	  || (TYPE_LANG_SPECIFIC (inner_type)
16137 	      && TYPE_BEING_DEFINED (inner_type)))
16138 	{
16139 	  incomplete_var iv = {var, inner_type};
16140 	  vec_safe_push (incomplete_vars, iv);
16141 	}
16142       else if (!(DECL_LANG_SPECIFIC (var) && DECL_TEMPLATE_INFO (var))
16143 	       && decl_constant_var_p (var)
16144 	       && (TYPE_PTRMEM_P (inner_type) || CLASS_TYPE_P (inner_type)))
16145 	{
16146 	  /* When the outermost open class is complete we can resolve any
16147 	     pointers-to-members.  */
16148 	  tree context = outermost_open_class ();
16149 	  incomplete_var iv = {var, context};
16150 	  vec_safe_push (incomplete_vars, iv);
16151 	}
16152     }
16153 }
16154 
16155 /* Called when a class type (given by TYPE) is defined.  If there are
16156    any existing VAR_DECLs whose type has been completed by this
16157    declaration, update them now.  */
16158 
16159 void
complete_vars(tree type)16160 complete_vars (tree type)
16161 {
16162   unsigned ix;
16163   incomplete_var *iv;
16164 
16165   for (ix = 0; vec_safe_iterate (incomplete_vars, ix, &iv); )
16166     {
16167       if (same_type_p (type, iv->incomplete_type))
16168 	{
16169 	  tree var = iv->decl;
16170 	  tree type = TREE_TYPE (var);
16171 
16172 	  if (type != error_mark_node
16173 	      && (TYPE_MAIN_VARIANT (strip_array_types (type))
16174 		  == iv->incomplete_type))
16175 	    {
16176 	      /* Complete the type of the variable.  The VAR_DECL itself
16177 		 will be laid out in expand_expr.  */
16178 	      complete_type (type);
16179 	      cp_apply_type_quals_to_decl (cp_type_quals (type), var);
16180 	    }
16181 
16182 	  /* Remove this entry from the list.  */
16183 	  incomplete_vars->unordered_remove (ix);
16184 	}
16185       else
16186 	ix++;
16187     }
16188 
16189   /* Check for pending declarations which may have abstract type.  */
16190   complete_type_check_abstract (type);
16191 }
16192 
16193 /* If DECL is of a type which needs a cleanup, build and return an
16194    expression to perform that cleanup here.  Return NULL_TREE if no
16195    cleanup need be done.  DECL can also be a _REF when called from
16196    split_nonconstant_init_1.  */
16197 
16198 tree
cxx_maybe_build_cleanup(tree decl,tsubst_flags_t complain)16199 cxx_maybe_build_cleanup (tree decl, tsubst_flags_t complain)
16200 {
16201   tree type;
16202   tree attr;
16203   tree cleanup;
16204 
16205   /* Assume no cleanup is required.  */
16206   cleanup = NULL_TREE;
16207 
16208   if (error_operand_p (decl))
16209     return cleanup;
16210 
16211   /* Handle "__attribute__((cleanup))".  We run the cleanup function
16212      before the destructor since the destructor is what actually
16213      terminates the lifetime of the object.  */
16214   if (DECL_P (decl))
16215     attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
16216   else
16217     attr = NULL_TREE;
16218   if (attr)
16219     {
16220       tree id;
16221       tree fn;
16222       tree arg;
16223 
16224       /* Get the name specified by the user for the cleanup function.  */
16225       id = TREE_VALUE (TREE_VALUE (attr));
16226       /* Look up the name to find the cleanup function to call.  It is
16227 	 important to use lookup_name here because that is what is
16228 	 used in c-common.c:handle_cleanup_attribute when performing
16229 	 initial checks on the attribute.  Note that those checks
16230 	 include ensuring that the function found is not an overloaded
16231 	 function, or an object with an overloaded call operator,
16232 	 etc.; we can rely on the fact that the function found is an
16233 	 ordinary FUNCTION_DECL.  */
16234       fn = lookup_name (id);
16235       arg = build_address (decl);
16236       if (!mark_used (decl, complain) && !(complain & tf_error))
16237 	return error_mark_node;
16238       cleanup = cp_build_function_call_nary (fn, complain, arg, NULL_TREE);
16239       if (cleanup == error_mark_node)
16240 	return error_mark_node;
16241     }
16242   /* Handle ordinary C++ destructors.  */
16243   type = TREE_TYPE (decl);
16244   if (type_build_dtor_call (type))
16245     {
16246       int flags = LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR;
16247       tree addr;
16248       tree call;
16249 
16250       if (TREE_CODE (type) == ARRAY_TYPE)
16251 	addr = decl;
16252       else
16253 	addr = build_address (decl);
16254 
16255       call = build_delete (TREE_TYPE (addr), addr,
16256 			   sfk_complete_destructor, flags, 0, complain);
16257       if (call == error_mark_node)
16258 	cleanup = error_mark_node;
16259       else if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
16260 	/* Discard the call.  */;
16261       else if (cleanup)
16262 	cleanup = cp_build_compound_expr (cleanup, call, complain);
16263       else
16264 	cleanup = call;
16265     }
16266 
16267   /* build_delete sets the location of the destructor call to the
16268      current location, even though the destructor is going to be
16269      called later, at the end of the current scope.  This can lead to
16270      a "jumpy" behavior for users of debuggers when they step around
16271      the end of the block.  So let's unset the location of the
16272      destructor call instead.  */
16273   protected_set_expr_location (cleanup, UNKNOWN_LOCATION);
16274 
16275   if (cleanup
16276       && DECL_P (decl)
16277       && !lookup_attribute ("warn_unused", TYPE_ATTRIBUTES (TREE_TYPE (decl)))
16278       /* Treat objects with destructors as used; the destructor may do
16279 	 something substantive.  */
16280       && !mark_used (decl, complain) && !(complain & tf_error))
16281     return error_mark_node;
16282 
16283   return cleanup;
16284 }
16285 
16286 
16287 /* Return the FUNCTION_TYPE that corresponds to MEMFNTYPE, which can be a
16288    FUNCTION_DECL, METHOD_TYPE, FUNCTION_TYPE, pointer or reference to
16289    METHOD_TYPE or FUNCTION_TYPE, or pointer to member function.  */
16290 
16291 tree
static_fn_type(tree memfntype)16292 static_fn_type (tree memfntype)
16293 {
16294   tree fntype;
16295   tree args;
16296 
16297   if (TYPE_PTRMEMFUNC_P (memfntype))
16298     memfntype = TYPE_PTRMEMFUNC_FN_TYPE (memfntype);
16299   if (POINTER_TYPE_P (memfntype)
16300       || TREE_CODE (memfntype) == FUNCTION_DECL)
16301     memfntype = TREE_TYPE (memfntype);
16302   if (TREE_CODE (memfntype) == FUNCTION_TYPE)
16303     return memfntype;
16304   gcc_assert (TREE_CODE (memfntype) == METHOD_TYPE);
16305   args = TYPE_ARG_TYPES (memfntype);
16306   cp_ref_qualifier rqual = type_memfn_rqual (memfntype);
16307   fntype = build_function_type (TREE_TYPE (memfntype), TREE_CHAIN (args));
16308   fntype = apply_memfn_quals (fntype, type_memfn_quals (memfntype), rqual);
16309   fntype = (cp_build_type_attribute_variant
16310 	    (fntype, TYPE_ATTRIBUTES (memfntype)));
16311   fntype = (build_exception_variant
16312 	    (fntype, TYPE_RAISES_EXCEPTIONS (memfntype)));
16313   if (TYPE_HAS_LATE_RETURN_TYPE (memfntype))
16314     TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1;
16315   return fntype;
16316 }
16317 
16318 /* DECL was originally constructed as a non-static member function,
16319    but turned out to be static.  Update it accordingly.  */
16320 
16321 void
revert_static_member_fn(tree decl)16322 revert_static_member_fn (tree decl)
16323 {
16324   tree stype = static_fn_type (decl);
16325   cp_cv_quals quals = type_memfn_quals (stype);
16326   cp_ref_qualifier rqual = type_memfn_rqual (stype);
16327 
16328   if (quals != TYPE_UNQUALIFIED || rqual != REF_QUAL_NONE)
16329     stype = apply_memfn_quals (stype, TYPE_UNQUALIFIED, REF_QUAL_NONE);
16330 
16331   TREE_TYPE (decl) = stype;
16332 
16333   if (DECL_ARGUMENTS (decl))
16334     DECL_ARGUMENTS (decl) = DECL_CHAIN (DECL_ARGUMENTS (decl));
16335   DECL_STATIC_FUNCTION_P (decl) = 1;
16336 }
16337 
16338 /* Return which tree structure is used by T, or TS_CP_GENERIC if T is
16339    one of the language-independent trees.  */
16340 
16341 enum cp_tree_node_structure_enum
cp_tree_node_structure(union lang_tree_node * t)16342 cp_tree_node_structure (union lang_tree_node * t)
16343 {
16344   switch (TREE_CODE (&t->generic))
16345     {
16346     case DEFAULT_ARG:		return TS_CP_DEFAULT_ARG;
16347     case DEFERRED_NOEXCEPT:	return TS_CP_DEFERRED_NOEXCEPT;
16348     case IDENTIFIER_NODE:	return TS_CP_IDENTIFIER;
16349     case OVERLOAD:		return TS_CP_OVERLOAD;
16350     case TEMPLATE_PARM_INDEX:	return TS_CP_TPI;
16351     case PTRMEM_CST:		return TS_CP_PTRMEM;
16352     case BASELINK:		return TS_CP_BASELINK;
16353     case TEMPLATE_DECL:		return TS_CP_TEMPLATE_DECL;
16354     case STATIC_ASSERT:		return TS_CP_STATIC_ASSERT;
16355     case ARGUMENT_PACK_SELECT:  return TS_CP_ARGUMENT_PACK_SELECT;
16356     case TRAIT_EXPR:		return TS_CP_TRAIT_EXPR;
16357     case LAMBDA_EXPR:		return TS_CP_LAMBDA_EXPR;
16358     case TEMPLATE_INFO:		return TS_CP_TEMPLATE_INFO;
16359     case CONSTRAINT_INFO:       return TS_CP_CONSTRAINT_INFO;
16360     case USERDEF_LITERAL:	return TS_CP_USERDEF_LITERAL;
16361     default:			return TS_CP_GENERIC;
16362     }
16363 }
16364 
16365 /* Build the void_list_node (void_type_node having been created).  */
16366 tree
build_void_list_node(void)16367 build_void_list_node (void)
16368 {
16369   tree t = build_tree_list (NULL_TREE, void_type_node);
16370   return t;
16371 }
16372 
16373 bool
cp_missing_noreturn_ok_p(tree decl)16374 cp_missing_noreturn_ok_p (tree decl)
16375 {
16376   /* A missing noreturn is ok for the `main' function.  */
16377   return DECL_MAIN_P (decl);
16378 }
16379 
16380 /* Return the decl used to identify the COMDAT group into which DECL should
16381    be placed.  */
16382 
16383 tree
cxx_comdat_group(tree decl)16384 cxx_comdat_group (tree decl)
16385 {
16386   /* Virtual tables, construction virtual tables, and virtual table
16387      tables all go in a single COMDAT group, named after the primary
16388      virtual table.  */
16389   if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl))
16390     decl = CLASSTYPE_VTABLES (DECL_CONTEXT (decl));
16391   /* For all other DECLs, the COMDAT group is the mangled name of the
16392      declaration itself.  */
16393   else
16394     {
16395       while (DECL_THUNK_P (decl))
16396 	{
16397 	  /* If TARGET_USE_LOCAL_THUNK_ALIAS_P, use_thunk puts the thunk
16398 	     into the same section as the target function.  In that case
16399 	     we must return target's name.  */
16400 	  tree target = THUNK_TARGET (decl);
16401 	  if (TARGET_USE_LOCAL_THUNK_ALIAS_P (target)
16402 	      && DECL_SECTION_NAME (target) != NULL
16403 	      && DECL_ONE_ONLY (target))
16404 	    decl = target;
16405 	  else
16406 	    break;
16407 	}
16408     }
16409 
16410   return decl;
16411 }
16412 
16413 /* Returns the return type for FN as written by the user, which may include
16414    a placeholder for a deduced return type.  */
16415 
16416 tree
fndecl_declared_return_type(tree fn)16417 fndecl_declared_return_type (tree fn)
16418 {
16419   fn = STRIP_TEMPLATE (fn);
16420   if (FNDECL_USED_AUTO (fn))
16421     {
16422       struct language_function *f = NULL;
16423       if (DECL_STRUCT_FUNCTION (fn))
16424 	f = DECL_STRUCT_FUNCTION (fn)->language;
16425       if (f == NULL)
16426 	f = DECL_SAVED_FUNCTION_DATA (fn);
16427       return f->x_auto_return_pattern;
16428     }
16429   return TREE_TYPE (TREE_TYPE (fn));
16430 }
16431 
16432 /* Returns true iff DECL is a variable or function declared with an auto type
16433    that has not yet been deduced to a real type.  */
16434 
16435 bool
undeduced_auto_decl(tree decl)16436 undeduced_auto_decl (tree decl)
16437 {
16438   if (cxx_dialect < cxx11)
16439     return false;
16440   return ((VAR_OR_FUNCTION_DECL_P (decl)
16441 	   || TREE_CODE (decl) == TEMPLATE_DECL)
16442 	  && type_uses_auto (TREE_TYPE (decl)));
16443 }
16444 
16445 /* Complain if DECL has an undeduced return type.  */
16446 
16447 bool
require_deduced_type(tree decl,tsubst_flags_t complain)16448 require_deduced_type (tree decl, tsubst_flags_t complain)
16449 {
16450   if (undeduced_auto_decl (decl))
16451     {
16452       if (complain & tf_error)
16453 	error ("use of %qD before deduction of %<auto%>", decl);
16454       return false;
16455     }
16456   return true;
16457 }
16458 
16459 #include "gt-cp-decl.h"
16460