1 /* Report error messages, build initializers, and perform
2    some front-end optimizations for C++ compiler.
3    Copyright (C) 1987-2020 Free Software Foundation, Inc.
4    Hacked by Michael Tiemann (tiemann@cygnus.com)
5 
6 This file is part of GCC.
7 
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12 
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17 
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
21 
22 
23 /* This file is part of the C++ front end.
24    It contains routines to build C++ expressions given their operands,
25    including computing the types of the result, C and C++ specific error
26    checks, and some optimization.  */
27 
28 #include "config.h"
29 #include "system.h"
30 #include "coretypes.h"
31 #include "cp-tree.h"
32 #include "stor-layout.h"
33 #include "varasm.h"
34 #include "intl.h"
35 #include "gcc-rich-location.h"
36 #include "target.h"
37 
38 static tree
39 process_init_constructor (tree type, tree init, int nested, int flags,
40 			  tsubst_flags_t complain);
41 
42 
43 /* Print an error message stemming from an attempt to use
44    BASETYPE as a base class for TYPE.  */
45 
46 tree
error_not_base_type(tree basetype,tree type)47 error_not_base_type (tree basetype, tree type)
48 {
49   if (TREE_CODE (basetype) == FUNCTION_DECL)
50     basetype = DECL_CONTEXT (basetype);
51   error ("type %qT is not a base type for type %qT", basetype, type);
52   return error_mark_node;
53 }
54 
55 tree
binfo_or_else(tree base,tree type)56 binfo_or_else (tree base, tree type)
57 {
58   tree binfo = lookup_base (type, base, ba_unique,
59 			    NULL, tf_warning_or_error);
60 
61   if (binfo == error_mark_node)
62     return NULL_TREE;
63   else if (!binfo)
64     error_not_base_type (base, type);
65   return binfo;
66 }
67 
68 /* According to ARM $7.1.6, "A `const' object may be initialized, but its
69    value may not be changed thereafter.  */
70 
71 void
cxx_readonly_error(location_t loc,tree arg,enum lvalue_use errstring)72 cxx_readonly_error (location_t loc, tree arg, enum lvalue_use errstring)
73 {
74 
75 /* This macro is used to emit diagnostics to ensure that all format
76    strings are complete sentences, visible to gettext and checked at
77    compile time.  */
78 
79 #define ERROR_FOR_ASSIGNMENT(LOC, AS, ASM, IN, DE, ARG)			\
80   do {                                                                  \
81     switch (errstring)                                                  \
82       {                                                                 \
83       case lv_assign:							\
84 	error_at (LOC, AS, ARG);					\
85         break;                                                          \
86       case lv_asm:							\
87 	error_at (LOC, ASM, ARG);					\
88         break;                                                          \
89       case lv_increment:						\
90 	error_at (LOC, IN, ARG);					\
91         break;                                                          \
92       case lv_decrement:                                                \
93 	error_at (LOC, DE, ARG);					\
94         break;                                                          \
95       default:                                                          \
96         gcc_unreachable ();                                             \
97       }                                                                 \
98   } while (0)
99 
100   /* Handle C++-specific things first.  */
101 
102   if (VAR_P (arg)
103       && DECL_LANG_SPECIFIC (arg)
104       && DECL_IN_AGGR_P (arg)
105       && !TREE_STATIC (arg))
106     ERROR_FOR_ASSIGNMENT (loc,
107 			  G_("assignment of constant field %qD"),
108 			  G_("constant field %qD used as %<asm%> output"),
109 			  G_("increment of constant field %qD"),
110 			  G_("decrement of constant field %qD"),
111 			  arg);
112   else if (INDIRECT_REF_P (arg)
113 	   && TYPE_REF_P (TREE_TYPE (TREE_OPERAND (arg, 0)))
114 	   && (VAR_P (TREE_OPERAND (arg, 0))
115 	       || TREE_CODE (TREE_OPERAND (arg, 0)) == PARM_DECL))
116     ERROR_FOR_ASSIGNMENT (loc,
117 			  G_("assignment of read-only reference %qD"),
118 			  G_("read-only reference %qD used as %<asm%> output"),
119 			  G_("increment of read-only reference %qD"),
120 			  G_("decrement of read-only reference %qD"),
121 			  TREE_OPERAND (arg, 0));
122   else
123     readonly_error (loc, arg, errstring);
124 }
125 
126 /* Structure that holds information about declarations whose type was
127    incomplete and we could not check whether it was abstract or not.  */
128 
129 struct GTY((chain_next ("%h.next"), for_user)) pending_abstract_type {
130   /* Declaration which we are checking for abstractness. It is either
131      a DECL node, or an IDENTIFIER_NODE if we do not have a full
132      declaration available.  */
133   tree decl;
134 
135   /* Type which will be checked for abstractness.  */
136   tree type;
137 
138   /* Kind of use in an unnamed declarator.  */
139   enum abstract_class_use use;
140 
141   /* Position of the declaration. This is only needed for IDENTIFIER_NODEs,
142      because DECLs already carry locus information.  */
143   location_t locus;
144 
145   /* Link to the next element in list.  */
146   struct pending_abstract_type* next;
147 };
148 
149 struct abstract_type_hasher : ggc_ptr_hash<pending_abstract_type>
150 {
151   typedef tree compare_type;
152   static hashval_t hash (pending_abstract_type *);
153   static bool equal (pending_abstract_type *, tree);
154 };
155 
156 /* Compute the hash value of the node VAL. This function is used by the
157    hash table abstract_pending_vars.  */
158 
159 hashval_t
hash(pending_abstract_type * pat)160 abstract_type_hasher::hash (pending_abstract_type *pat)
161 {
162   return (hashval_t) TYPE_UID (pat->type);
163 }
164 
165 
166 /* Compare node VAL1 with the type VAL2. This function is used by the
167    hash table abstract_pending_vars.  */
168 
169 bool
equal(pending_abstract_type * pat1,tree type2)170 abstract_type_hasher::equal (pending_abstract_type *pat1, tree type2)
171 {
172   return (pat1->type == type2);
173 }
174 
175 /* Hash table that maintains pending_abstract_type nodes, for which we still
176    need to check for type abstractness.  The key of the table is the type
177    of the declaration.  */
178 static GTY (()) hash_table<abstract_type_hasher> *abstract_pending_vars = NULL;
179 
180 static int abstract_virtuals_error_sfinae (tree, tree, abstract_class_use, tsubst_flags_t);
181 
182 /* This function is called after TYPE is completed, and will check if there
183    are pending declarations for which we still need to verify the abstractness
184    of TYPE, and emit a diagnostic (through abstract_virtuals_error) if TYPE
185    turned out to be incomplete.  */
186 
187 void
complete_type_check_abstract(tree type)188 complete_type_check_abstract (tree type)
189 {
190   struct pending_abstract_type *pat;
191   location_t cur_loc = input_location;
192 
193   gcc_assert (COMPLETE_TYPE_P (type));
194 
195   if (!abstract_pending_vars)
196     return;
197 
198   /* Retrieve the list of pending declarations for this type.  */
199   pending_abstract_type **slot
200     = abstract_pending_vars->find_slot_with_hash (type, TYPE_UID (type),
201 						  NO_INSERT);
202   if (!slot)
203     return;
204   pat = *slot;
205   gcc_assert (pat);
206 
207   /* If the type is not abstract, do not do anything.  */
208   if (CLASSTYPE_PURE_VIRTUALS (type))
209     {
210       struct pending_abstract_type *prev = 0, *next;
211 
212       /* Reverse the list to emit the errors in top-down order.  */
213       for (; pat; pat = next)
214 	{
215 	  next = pat->next;
216 	  pat->next = prev;
217 	  prev = pat;
218 	}
219       pat = prev;
220 
221       /* Go through the list, and call abstract_virtuals_error for each
222 	element: it will issue a diagnostic if the type is abstract.  */
223       while (pat)
224 	{
225 	  gcc_assert (type == pat->type);
226 
227 	  /* Tweak input_location so that the diagnostic appears at the correct
228 	    location. Notice that this is only needed if the decl is an
229 	    IDENTIFIER_NODE.  */
230 	  input_location = pat->locus;
231 	  abstract_virtuals_error_sfinae (pat->decl, pat->type, pat->use,
232 					  tf_warning_or_error);
233 	  pat = pat->next;
234 	}
235     }
236 
237   abstract_pending_vars->clear_slot (slot);
238 
239   input_location = cur_loc;
240 }
241 
242 
243 /* If TYPE has abstract virtual functions, issue an error about trying
244    to create an object of that type.  DECL is the object declared, or
245    NULL_TREE if the declaration is unavailable, in which case USE specifies
246    the kind of invalid use.  Returns 1 if an error occurred; zero if
247    all was well.  */
248 
249 static int
abstract_virtuals_error_sfinae(tree decl,tree type,abstract_class_use use,tsubst_flags_t complain)250 abstract_virtuals_error_sfinae (tree decl, tree type, abstract_class_use use,
251 				tsubst_flags_t complain)
252 {
253   vec<tree, va_gc> *pure;
254 
255   /* This function applies only to classes. Any other entity can never
256      be abstract.  */
257   if (!CLASS_TYPE_P (type))
258     return 0;
259   type = TYPE_MAIN_VARIANT (type);
260 
261 #if 0
262   /* Instantiation here seems to be required by the standard,
263      but breaks e.g. boost::bind.  FIXME!  */
264   /* In SFINAE, non-N3276 context, force instantiation.  */
265   if (!(complain & (tf_error|tf_decltype)))
266     complete_type (type);
267 #endif
268 
269   /* If the type is incomplete, we register it within a hash table,
270      so that we can check again once it is completed. This makes sense
271      only for objects for which we have a declaration or at least a
272      name.  */
273   if (!COMPLETE_TYPE_P (type) && (complain & tf_error))
274     {
275       struct pending_abstract_type *pat;
276 
277       gcc_assert (!decl || DECL_P (decl) || identifier_p (decl));
278 
279       if (!abstract_pending_vars)
280 	abstract_pending_vars
281 	  = hash_table<abstract_type_hasher>::create_ggc (31);
282 
283       pending_abstract_type **slot
284        	= abstract_pending_vars->find_slot_with_hash (type, TYPE_UID (type),
285 						      INSERT);
286 
287       pat = ggc_alloc<pending_abstract_type> ();
288       pat->type = type;
289       pat->decl = decl;
290       pat->use = use;
291       pat->locus = ((decl && DECL_P (decl))
292 		    ? DECL_SOURCE_LOCATION (decl)
293 		    : input_location);
294 
295       pat->next = *slot;
296       *slot = pat;
297 
298       return 0;
299     }
300 
301   if (!TYPE_SIZE (type))
302     /* TYPE is being defined, and during that time
303        CLASSTYPE_PURE_VIRTUALS holds the inline friends.  */
304     return 0;
305 
306   pure = CLASSTYPE_PURE_VIRTUALS (type);
307   if (!pure)
308     return 0;
309 
310   if (!(complain & tf_error))
311     return 1;
312 
313   auto_diagnostic_group d;
314   if (decl)
315     {
316       if (VAR_P (decl))
317 	error ("cannot declare variable %q+D to be of abstract "
318 	       "type %qT", decl, type);
319       else if (TREE_CODE (decl) == PARM_DECL)
320 	{
321 	  if (DECL_NAME (decl))
322 	    error ("cannot declare parameter %q+D to be of abstract type %qT",
323 		   decl, type);
324 	  else
325 	    error ("cannot declare parameter to be of abstract type %qT",
326 		   type);
327 	}
328       else if (TREE_CODE (decl) == FIELD_DECL)
329 	error ("cannot declare field %q+D to be of abstract type %qT",
330 	       decl, type);
331       else if (TREE_CODE (decl) == FUNCTION_DECL
332 	       && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
333 	error ("invalid abstract return type for member function %q+#D", decl);
334       else if (TREE_CODE (decl) == FUNCTION_DECL)
335 	error ("invalid abstract return type for function %q+#D", decl);
336       else if (identifier_p (decl))
337 	/* Here we do not have location information.  */
338 	error ("invalid abstract type %qT for %qE", type, decl);
339       else
340 	error ("invalid abstract type for %q+D", decl);
341     }
342   else switch (use)
343     {
344     case ACU_ARRAY:
345       error ("creating array of %qT, which is an abstract class type", type);
346       break;
347     case ACU_CAST:
348       error ("invalid cast to abstract class type %qT", type);
349       break;
350     case ACU_NEW:
351       error ("invalid new-expression of abstract class type %qT", type);
352       break;
353     case ACU_RETURN:
354       error ("invalid abstract return type %qT", type);
355       break;
356     case ACU_PARM:
357       error ("invalid abstract parameter type %qT", type);
358       break;
359     case ACU_THROW:
360       error ("expression of abstract class type %qT cannot "
361 	     "be used in throw-expression", type);
362       break;
363     case ACU_CATCH:
364       error ("cannot declare %<catch%> parameter to be of abstract "
365 	     "class type %qT", type);
366       break;
367     default:
368       error ("cannot allocate an object of abstract type %qT", type);
369     }
370 
371   /* Only go through this once.  */
372   if (pure->length ())
373     {
374       unsigned ix;
375       tree fn;
376 
377       inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
378 	      "  because the following virtual functions are pure within %qT:",
379 	      type);
380 
381       FOR_EACH_VEC_ELT (*pure, ix, fn)
382 	if (! DECL_CLONED_FUNCTION_P (fn)
383 	    || DECL_COMPLETE_DESTRUCTOR_P (fn))
384 	  inform (DECL_SOURCE_LOCATION (fn), "    %#qD", fn);
385 
386       /* Now truncate the vector.  This leaves it non-null, so we know
387 	 there are pure virtuals, but empty so we don't list them out
388 	 again.  */
389       pure->truncate (0);
390     }
391 
392   return 1;
393 }
394 
395 int
abstract_virtuals_error_sfinae(tree decl,tree type,tsubst_flags_t complain)396 abstract_virtuals_error_sfinae (tree decl, tree type, tsubst_flags_t complain)
397 {
398   return abstract_virtuals_error_sfinae (decl, type, ACU_UNKNOWN, complain);
399 }
400 
401 int
abstract_virtuals_error_sfinae(abstract_class_use use,tree type,tsubst_flags_t complain)402 abstract_virtuals_error_sfinae (abstract_class_use use, tree type,
403 				tsubst_flags_t complain)
404 {
405   return abstract_virtuals_error_sfinae (NULL_TREE, type, use, complain);
406 }
407 
408 
409 /* Wrapper for the above function in the common case of wanting errors.  */
410 
411 int
abstract_virtuals_error(tree decl,tree type)412 abstract_virtuals_error (tree decl, tree type)
413 {
414   return abstract_virtuals_error_sfinae (decl, type, tf_warning_or_error);
415 }
416 
417 int
abstract_virtuals_error(abstract_class_use use,tree type)418 abstract_virtuals_error (abstract_class_use use, tree type)
419 {
420   return abstract_virtuals_error_sfinae (use, type, tf_warning_or_error);
421 }
422 
423 /* Print an inform about the declaration of the incomplete type TYPE.  */
424 
425 void
cxx_incomplete_type_inform(const_tree type)426 cxx_incomplete_type_inform (const_tree type)
427 {
428   if (!TYPE_MAIN_DECL (type))
429     return;
430 
431   location_t loc = DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type));
432   tree ptype = strip_top_quals (CONST_CAST_TREE (type));
433 
434   if (current_class_type
435       && TYPE_BEING_DEFINED (current_class_type)
436       && same_type_p (ptype, current_class_type))
437     inform (loc, "definition of %q#T is not complete until "
438 	    "the closing brace", ptype);
439   else if (!TYPE_TEMPLATE_INFO (ptype))
440     inform (loc, "forward declaration of %q#T", ptype);
441   else
442     inform (loc, "declaration of %q#T", ptype);
443 }
444 
445 /* Print an error message for invalid use of an incomplete type.
446    VALUE is the expression that was used (or 0 if that isn't known)
447    and TYPE is the type that was invalid.  DIAG_KIND indicates the
448    type of diagnostic (see diagnostic.def).  */
449 
450 void
cxx_incomplete_type_diagnostic(location_t loc,const_tree value,const_tree type,diagnostic_t diag_kind)451 cxx_incomplete_type_diagnostic (location_t loc, const_tree value,
452 				const_tree type, diagnostic_t diag_kind)
453 {
454   bool is_decl = false, complained = false;
455 
456   gcc_assert (diag_kind == DK_WARNING
457 	      || diag_kind == DK_PEDWARN
458 	      || diag_kind == DK_ERROR);
459 
460   /* Avoid duplicate error message.  */
461   if (TREE_CODE (type) == ERROR_MARK)
462     return;
463 
464   if (value)
465     {
466       STRIP_ANY_LOCATION_WRAPPER (value);
467 
468       if (VAR_P (value)
469 	  || TREE_CODE (value) == PARM_DECL
470 	  || TREE_CODE (value) == FIELD_DECL)
471 	{
472 	  complained = emit_diagnostic (diag_kind, DECL_SOURCE_LOCATION (value), 0,
473 					"%qD has incomplete type", value);
474 	  is_decl = true;
475 	}
476     }
477  retry:
478   /* We must print an error message.  Be clever about what it says.  */
479 
480   switch (TREE_CODE (type))
481     {
482     case RECORD_TYPE:
483     case UNION_TYPE:
484     case ENUMERAL_TYPE:
485       if (!is_decl)
486 	complained = emit_diagnostic (diag_kind, loc, 0,
487 				      "invalid use of incomplete type %q#T",
488 				      type);
489       if (complained)
490 	cxx_incomplete_type_inform (type);
491       break;
492 
493     case VOID_TYPE:
494       emit_diagnostic (diag_kind, loc, 0,
495 		       "invalid use of %qT", type);
496       break;
497 
498     case ARRAY_TYPE:
499       if (TYPE_DOMAIN (type))
500 	{
501 	  type = TREE_TYPE (type);
502 	  goto retry;
503 	}
504       emit_diagnostic (diag_kind, loc, 0,
505 		       "invalid use of array with unspecified bounds");
506       break;
507 
508     case OFFSET_TYPE:
509     bad_member:
510       {
511 	tree member = TREE_OPERAND (value, 1);
512 	if (is_overloaded_fn (member))
513 	  member = get_first_fn (member);
514 
515 	if (DECL_FUNCTION_MEMBER_P (member)
516 	    && ! flag_ms_extensions)
517 	  {
518 	    gcc_rich_location richloc (loc);
519 	    /* If "member" has no arguments (other than "this"), then
520 	       add a fix-it hint.  */
521 	    if (type_num_arguments (TREE_TYPE (member)) == 1)
522 	      richloc.add_fixit_insert_after ("()");
523 	    emit_diagnostic (diag_kind, &richloc, 0,
524 			     "invalid use of member function %qD "
525 			     "(did you forget the %<()%> ?)", member);
526 	  }
527 	else
528 	  emit_diagnostic (diag_kind, loc, 0,
529 			   "invalid use of member %qD "
530 			   "(did you forget the %<&%> ?)", member);
531       }
532       break;
533 
534     case TEMPLATE_TYPE_PARM:
535       if (is_auto (type))
536 	{
537 	  if (CLASS_PLACEHOLDER_TEMPLATE (type))
538 	    emit_diagnostic (diag_kind, loc, 0,
539 			     "invalid use of placeholder %qT", type);
540 	  else
541 	    emit_diagnostic (diag_kind, loc, 0,
542 			     "invalid use of %qT", type);
543 	}
544       else
545 	emit_diagnostic (diag_kind, loc, 0,
546 			 "invalid use of template type parameter %qT", type);
547       break;
548 
549     case BOUND_TEMPLATE_TEMPLATE_PARM:
550       emit_diagnostic (diag_kind, loc, 0,
551 		       "invalid use of template template parameter %qT",
552 		       TYPE_NAME (type));
553       break;
554 
555     case TYPENAME_TYPE:
556     case DECLTYPE_TYPE:
557       emit_diagnostic (diag_kind, loc, 0,
558 		       "invalid use of dependent type %qT", type);
559       break;
560 
561     case LANG_TYPE:
562       if (type == init_list_type_node)
563 	{
564 	  emit_diagnostic (diag_kind, loc, 0,
565 			   "invalid use of brace-enclosed initializer list");
566 	  break;
567 	}
568       gcc_assert (type == unknown_type_node);
569       if (value && TREE_CODE (value) == COMPONENT_REF)
570 	goto bad_member;
571       else if (value && TREE_CODE (value) == ADDR_EXPR)
572 	emit_diagnostic (diag_kind, loc, 0,
573 			 "address of overloaded function with no contextual "
574 			 "type information");
575       else if (value && TREE_CODE (value) == OVERLOAD)
576 	emit_diagnostic (diag_kind, loc, 0,
577 			 "overloaded function with no contextual type information");
578       else
579 	emit_diagnostic (diag_kind, loc, 0,
580 			 "insufficient contextual information to determine type");
581       break;
582 
583     default:
584       gcc_unreachable ();
585     }
586 }
587 
588 /* Print an error message for invalid use of an incomplete type.
589    VALUE is the expression that was used (or 0 if that isn't known)
590    and TYPE is the type that was invalid.  */
591 
592 void
cxx_incomplete_type_error(location_t loc,const_tree value,const_tree type)593 cxx_incomplete_type_error (location_t loc, const_tree value, const_tree type)
594 {
595   cxx_incomplete_type_diagnostic (loc, value, type, DK_ERROR);
596 }
597 
598 
599 /* The recursive part of split_nonconstant_init.  DEST is an lvalue
600    expression to which INIT should be assigned.  INIT is a CONSTRUCTOR.
601    Return true if the whole of the value was initialized by the
602    generated statements.  */
603 
604 static bool
split_nonconstant_init_1(tree dest,tree init,bool nested)605 split_nonconstant_init_1 (tree dest, tree init, bool nested)
606 {
607   unsigned HOST_WIDE_INT idx, tidx = HOST_WIDE_INT_M1U;
608   tree field_index, value;
609   tree type = TREE_TYPE (dest);
610   tree inner_type = NULL;
611   bool array_type_p = false;
612   bool complete_p = true;
613   HOST_WIDE_INT num_split_elts = 0;
614 
615   switch (TREE_CODE (type))
616     {
617     case ARRAY_TYPE:
618       inner_type = TREE_TYPE (type);
619       array_type_p = true;
620       if ((TREE_SIDE_EFFECTS (init)
621 	   && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
622 	  || vla_type_p (type))
623 	{
624 	  /* For an array, we only need/want a single cleanup region rather
625 	     than one per element.  */
626 	  tree code = build_vec_init (dest, NULL_TREE, init, false, 1,
627 				      tf_warning_or_error);
628 	  add_stmt (code);
629 	  if (nested)
630 	    /* Also clean up the whole array if something later in an enclosing
631 	       init-list throws.  */
632 	    if (tree cleanup = cxx_maybe_build_cleanup (dest,
633 							tf_warning_or_error))
634 	    finish_eh_cleanup (cleanup);
635 	  return true;
636 	}
637       /* FALLTHRU */
638 
639     case RECORD_TYPE:
640     case UNION_TYPE:
641     case QUAL_UNION_TYPE:
642       FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), idx,
643 				field_index, value)
644 	{
645 	  /* The current implementation of this algorithm assumes that
646 	     the field was set for all the elements. This is usually done
647 	     by process_init_constructor.  */
648 	  gcc_assert (field_index);
649 
650 	  if (!array_type_p)
651 	    inner_type = TREE_TYPE (field_index);
652 
653 	  if (TREE_CODE (value) == CONSTRUCTOR)
654 	    {
655 	      tree sub;
656 
657 	      if (array_type_p)
658 		sub = build4 (ARRAY_REF, inner_type, dest, field_index,
659 			      NULL_TREE, NULL_TREE);
660 	      else
661 		sub = build3 (COMPONENT_REF, inner_type, dest, field_index,
662 			      NULL_TREE);
663 
664 	      if (!split_nonconstant_init_1 (sub, value, true))
665 		complete_p = false;
666 	      else
667 		{
668 		  /* Mark element for removal.  */
669 		  CONSTRUCTOR_ELT (init, idx)->index = NULL_TREE;
670 		  if (idx < tidx)
671 		    tidx = idx;
672 		  num_split_elts++;
673 		}
674 	    }
675 	  else if (!initializer_constant_valid_p (value, inner_type))
676 	    {
677 	      tree code;
678 	      tree sub;
679 
680 	      /* Mark element for removal.  */
681 	      CONSTRUCTOR_ELT (init, idx)->index = NULL_TREE;
682 	      if (idx < tidx)
683 		tidx = idx;
684 
685 	      if (TREE_CODE (field_index) == RANGE_EXPR)
686 		{
687 		  /* Use build_vec_init to initialize a range.  */
688 		  tree low = TREE_OPERAND (field_index, 0);
689 		  tree hi = TREE_OPERAND (field_index, 1);
690 		  sub = build4 (ARRAY_REF, inner_type, dest, low,
691 				NULL_TREE, NULL_TREE);
692 		  sub = cp_build_addr_expr (sub, tf_warning_or_error);
693 		  tree max = size_binop (MINUS_EXPR, hi, low);
694 		  code = build_vec_init (sub, max, value, false, 0,
695 					 tf_warning_or_error);
696 		  add_stmt (code);
697 		  if (tree_fits_shwi_p (max))
698 		    num_split_elts += tree_to_shwi (max);
699 		}
700 	      else
701 		{
702 		  if (array_type_p)
703 		    sub = build4 (ARRAY_REF, inner_type, dest, field_index,
704 				  NULL_TREE, NULL_TREE);
705 		  else
706 		    sub = build3 (COMPONENT_REF, inner_type, dest, field_index,
707 				  NULL_TREE);
708 
709 		  /* We may need to add a copy constructor call if
710 		     the field has [[no_unique_address]].  */
711 		  if (unsafe_return_slot_p (sub))
712 		    {
713 		      /* But not if the initializer is an implicit ctor call
714 			 we just built in digest_init.  */
715 		      if (TREE_CODE (value) == TARGET_EXPR
716 			  && TARGET_EXPR_LIST_INIT_P (value))
717 			{
718 			  tree init = TARGET_EXPR_INITIAL (value);
719 			  if (init && TREE_CODE (init) == AGGR_INIT_EXPR
720 			      && AGGR_INIT_VIA_CTOR_P (init))
721 			    goto build_init;
722 			}
723 
724 		      releasing_vec args = make_tree_vector_single (value);
725 		      code = build_special_member_call
726 			(sub, complete_ctor_identifier, &args, inner_type,
727 			 LOOKUP_NORMAL, tf_warning_or_error);
728 		    }
729 		  else
730 		    {
731 		    build_init:
732 		      code = build2 (INIT_EXPR, inner_type, sub, value);
733 		    }
734 		  code = build_stmt (input_location, EXPR_STMT, code);
735 		  code = maybe_cleanup_point_expr_void (code);
736 		  add_stmt (code);
737 		  if (tree cleanup
738 		      = cxx_maybe_build_cleanup (sub, tf_warning_or_error))
739 		    finish_eh_cleanup (cleanup);
740 		}
741 
742 	      num_split_elts++;
743 	    }
744 	}
745       if (num_split_elts == 1)
746 	CONSTRUCTOR_ELTS (init)->ordered_remove (tidx);
747       else if (num_split_elts > 1)
748 	{
749 	  /* Perform the delayed ordered removal of non-constant elements
750 	     we split out.  */
751 	  for (idx = tidx; idx < CONSTRUCTOR_NELTS (init); ++idx)
752 	    if (CONSTRUCTOR_ELT (init, idx)->index == NULL_TREE)
753 	      ;
754 	    else
755 	      {
756 		*CONSTRUCTOR_ELT (init, tidx) = *CONSTRUCTOR_ELT (init, idx);
757 		++tidx;
758 	      }
759 	  vec_safe_truncate (CONSTRUCTOR_ELTS (init), tidx);
760 	}
761       break;
762 
763     case VECTOR_TYPE:
764       if (!initializer_constant_valid_p (init, type))
765 	{
766 	  tree code;
767 	  tree cons = copy_node (init);
768 	  CONSTRUCTOR_ELTS (init) = NULL;
769 	  code = build2 (MODIFY_EXPR, type, dest, cons);
770 	  code = build_stmt (input_location, EXPR_STMT, code);
771 	  add_stmt (code);
772 	  num_split_elts += CONSTRUCTOR_NELTS (init);
773 	}
774       break;
775 
776     default:
777       gcc_unreachable ();
778     }
779 
780   /* The rest of the initializer is now a constant. */
781   TREE_CONSTANT (init) = 1;
782   TREE_SIDE_EFFECTS (init) = 0;
783 
784   /* We didn't split out anything.  */
785   if (num_split_elts == 0)
786     return false;
787 
788   return complete_p && complete_ctor_at_level_p (TREE_TYPE (init),
789 						 num_split_elts, inner_type);
790 }
791 
792 /* A subroutine of store_init_value.  Splits non-constant static
793    initializer INIT into a constant part and generates code to
794    perform the non-constant part of the initialization to DEST.
795    Returns the code for the runtime init.  */
796 
797 tree
split_nonconstant_init(tree dest,tree init)798 split_nonconstant_init (tree dest, tree init)
799 {
800   tree code;
801 
802   if (TREE_CODE (init) == TARGET_EXPR)
803     init = TARGET_EXPR_INITIAL (init);
804   if (TREE_CODE (init) == CONSTRUCTOR)
805     {
806       init = cp_fully_fold_init (init);
807       code = push_stmt_list ();
808       if (split_nonconstant_init_1 (dest, init, false))
809 	init = NULL_TREE;
810       code = pop_stmt_list (code);
811       if (VAR_P (dest) && !is_local_temp (dest))
812 	{
813 	  DECL_INITIAL (dest) = init;
814 	  TREE_READONLY (dest) = 0;
815 	}
816       else if (init)
817 	{
818 	  tree ie = build2 (INIT_EXPR, void_type_node, dest, init);
819 	  code = add_stmt_to_compound (ie, code);
820 	}
821     }
822   else if (TREE_CODE (init) == STRING_CST
823 	   && array_of_runtime_bound_p (TREE_TYPE (dest)))
824     code = build_vec_init (dest, NULL_TREE, init, /*value-init*/false,
825 			   /*from array*/1, tf_warning_or_error);
826   else
827     code = build2 (INIT_EXPR, TREE_TYPE (dest), dest, init);
828 
829   return code;
830 }
831 
832 /* Perform appropriate conversions on the initial value of a variable,
833    store it in the declaration DECL,
834    and print any error messages that are appropriate.
835    If the init is invalid, store an ERROR_MARK.
836 
837    C++: Note that INIT might be a TREE_LIST, which would mean that it is
838    a base class initializer for some aggregate type, hopefully compatible
839    with DECL.  If INIT is a single element, and DECL is an aggregate
840    type, we silently convert INIT into a TREE_LIST, allowing a constructor
841    to be called.
842 
843    If INIT is a TREE_LIST and there is no constructor, turn INIT
844    into a CONSTRUCTOR and use standard initialization techniques.
845    Perhaps a warning should be generated?
846 
847    Returns code to be executed if initialization could not be performed
848    for static variable.  In that case, caller must emit the code.  */
849 
850 tree
store_init_value(tree decl,tree init,vec<tree,va_gc> ** cleanups,int flags)851 store_init_value (tree decl, tree init, vec<tree, va_gc>** cleanups, int flags)
852 {
853   tree value, type;
854 
855   /* If variable's type was invalidly declared, just ignore it.  */
856 
857   type = TREE_TYPE (decl);
858   if (TREE_CODE (type) == ERROR_MARK)
859     return NULL_TREE;
860 
861   if (MAYBE_CLASS_TYPE_P (type))
862     {
863       if (TREE_CODE (init) == TREE_LIST)
864 	{
865 	  error ("constructor syntax used, but no constructor declared "
866 		 "for type %qT", type);
867 	  init = build_constructor_from_list (init_list_type_node, nreverse (init));
868 	}
869     }
870 
871   /* End of special C++ code.  */
872 
873   if (flags & LOOKUP_ALREADY_DIGESTED)
874     value = init;
875   else
876     {
877       if (TREE_STATIC (decl))
878 	flags |= LOOKUP_ALLOW_FLEXARRAY_INIT;
879       /* Digest the specified initializer into an expression.  */
880       value = digest_init_flags (type, init, flags, tf_warning_or_error);
881     }
882 
883   /* Look for braced array initializers for character arrays and
884      recursively convert them into STRING_CSTs.  */
885   value = braced_lists_to_strings (type, value);
886 
887   current_ref_temp_count = 0;
888   value = extend_ref_init_temps (decl, value, cleanups);
889 
890   /* In C++11 constant expression is a semantic, not syntactic, property.
891      In C++98, make sure that what we thought was a constant expression at
892      template definition time is still constant and otherwise perform this
893      as optimization, e.g. to fold SIZEOF_EXPRs in the initializer.  */
894   if (decl_maybe_constant_var_p (decl) || TREE_STATIC (decl))
895     {
896       bool const_init;
897       tree oldval = value;
898       if (DECL_DECLARED_CONSTEXPR_P (decl)
899 	  || (DECL_IN_AGGR_P (decl)
900 	      && DECL_INITIALIZED_IN_CLASS_P (decl)))
901 	{
902 	  value = fold_non_dependent_expr (value, tf_warning_or_error,
903 					   /*manifestly_const_eval=*/true,
904 					   decl);
905 	  /* Diagnose a non-constant initializer for constexpr variable or
906 	     non-inline in-class-initialized static data member.  */
907 	  if (!require_constant_expression (value))
908 	    value = error_mark_node;
909 	  else if (processing_template_decl)
910 	    /* In a template we might not have done the necessary
911 	       transformations to make value actually constant,
912 	       e.g. extend_ref_init_temps.  */
913 	    value = maybe_constant_init (value, decl, true);
914 	  else
915 	    value = cxx_constant_init (value, decl);
916 	}
917       else
918 	value = fold_non_dependent_init (value, tf_warning_or_error,
919 					 /*manifestly_const_eval=*/true, decl);
920       if (TREE_CODE (value) == CONSTRUCTOR && cp_has_mutable_p (type))
921 	/* Poison this CONSTRUCTOR so it can't be copied to another
922 	   constexpr variable.  */
923 	CONSTRUCTOR_MUTABLE_POISON (value) = true;
924       const_init = (reduced_constant_expression_p (value)
925 		    || error_operand_p (value));
926       DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = const_init;
927       /* FIXME setting TREE_CONSTANT on refs breaks the back end.  */
928       if (!TYPE_REF_P (type))
929 	TREE_CONSTANT (decl) = const_init && decl_maybe_constant_var_p (decl);
930       if (!const_init)
931 	{
932 	  /* [dcl.constinit]/2 "If a variable declared with the constinit
933 	     specifier has dynamic initialization, the program is
934 	     ill-formed."  */
935 	  if (flags & LOOKUP_CONSTINIT)
936 	    {
937 	      error_at (location_of (decl),
938 			"%<constinit%> variable %qD does not have a constant "
939 			"initializer", decl);
940 	      if (require_constant_expression (value))
941 		cxx_constant_init (value, decl);
942 	      value = error_mark_node;
943 	    }
944 	  else
945 	    value = oldval;
946 	}
947     }
948   /* Don't fold initializers of automatic variables in constexpr functions,
949      that might fold away something that needs to be diagnosed at constexpr
950      evaluation time.  */
951   if (!current_function_decl
952       || !DECL_DECLARED_CONSTEXPR_P (current_function_decl)
953       || TREE_STATIC (decl))
954     value = cp_fully_fold_init (value);
955 
956   /* Handle aggregate NSDMI in non-constant initializers, too.  */
957   value = replace_placeholders (value, decl);
958 
959   /* If the initializer is not a constant, fill in DECL_INITIAL with
960      the bits that are constant, and then return an expression that
961      will perform the dynamic initialization.  */
962   if (value != error_mark_node
963       && (TREE_SIDE_EFFECTS (value)
964 	  || vla_type_p (type)
965 	  || ! reduced_constant_expression_p (value)))
966     return split_nonconstant_init (decl, value);
967 
968   /* DECL may change value; purge caches.  */
969   clear_cv_and_fold_caches (TREE_STATIC (decl));
970 
971   /* If the value is a constant, just put it in DECL_INITIAL.  If DECL
972      is an automatic variable, the middle end will turn this into a
973      dynamic initialization later.  */
974   DECL_INITIAL (decl) = value;
975   return NULL_TREE;
976 }
977 
978 
979 /* Give diagnostic about narrowing conversions within { }, or as part of
980    a converted constant expression.  If CONST_ONLY, only check
981    constants.  */
982 
983 bool
check_narrowing(tree type,tree init,tsubst_flags_t complain,bool const_only)984 check_narrowing (tree type, tree init, tsubst_flags_t complain,
985 		 bool const_only/*= false*/)
986 {
987   tree ftype = unlowered_expr_type (init);
988   bool ok = true;
989   REAL_VALUE_TYPE d;
990 
991   if (((!warn_narrowing || !(complain & tf_warning))
992        && cxx_dialect == cxx98)
993       || !ARITHMETIC_TYPE_P (type)
994       /* Don't emit bogus warnings with e.g. value-dependent trees.  */
995       || instantiation_dependent_expression_p (init))
996     return ok;
997 
998   if (BRACE_ENCLOSED_INITIALIZER_P (init)
999       && TREE_CODE (type) == COMPLEX_TYPE)
1000     {
1001       tree elttype = TREE_TYPE (type);
1002       if (CONSTRUCTOR_NELTS (init) > 0)
1003         ok &= check_narrowing (elttype, CONSTRUCTOR_ELT (init, 0)->value,
1004 			       complain);
1005       if (CONSTRUCTOR_NELTS (init) > 1)
1006 	ok &= check_narrowing (elttype, CONSTRUCTOR_ELT (init, 1)->value,
1007 			       complain);
1008       return ok;
1009     }
1010 
1011   /* Even non-dependent expressions can still have template
1012      codes like CAST_EXPR, so use *_non_dependent_expr to cope.  */
1013   init = fold_non_dependent_expr (init, complain);
1014   if (init == error_mark_node)
1015     return ok;
1016 
1017   /* If we were asked to only check constants, return early.  */
1018   if (const_only && !TREE_CONSTANT (init))
1019     return ok;
1020 
1021   if (CP_INTEGRAL_TYPE_P (type)
1022       && TREE_CODE (ftype) == REAL_TYPE)
1023     ok = false;
1024   else if (INTEGRAL_OR_ENUMERATION_TYPE_P (ftype)
1025 	   && CP_INTEGRAL_TYPE_P (type))
1026     {
1027       if (TREE_CODE (ftype) == ENUMERAL_TYPE)
1028 	/* Check for narrowing based on the values of the enumeration. */
1029 	ftype = ENUM_UNDERLYING_TYPE (ftype);
1030       if ((tree_int_cst_lt (TYPE_MAX_VALUE (type),
1031 			    TYPE_MAX_VALUE (ftype))
1032 	   || tree_int_cst_lt (TYPE_MIN_VALUE (ftype),
1033 			       TYPE_MIN_VALUE (type)))
1034 	  && (TREE_CODE (init) != INTEGER_CST
1035 	      || !int_fits_type_p (init, type)))
1036 	ok = false;
1037     }
1038   else if (TREE_CODE (ftype) == REAL_TYPE
1039 	   && TREE_CODE (type) == REAL_TYPE)
1040     {
1041       if (TYPE_PRECISION (type) < TYPE_PRECISION (ftype))
1042 	{
1043 	  if (TREE_CODE (init) == REAL_CST)
1044 	    {
1045 	      /* Issue 703: Loss of precision is OK as long as the value is
1046 		 within the representable range of the new type.  */
1047 	      REAL_VALUE_TYPE r;
1048 	      d = TREE_REAL_CST (init);
1049 	      real_convert (&r, TYPE_MODE (type), &d);
1050 	      if (real_isinf (&r))
1051 		ok = false;
1052 	    }
1053 	  else
1054 	    ok = false;
1055 	}
1056     }
1057   else if (INTEGRAL_OR_ENUMERATION_TYPE_P (ftype)
1058 	   && TREE_CODE (type) == REAL_TYPE)
1059     {
1060       ok = false;
1061       if (TREE_CODE (init) == INTEGER_CST)
1062 	{
1063 	  d = real_value_from_int_cst (0, init);
1064 	  if (exact_real_truncate (TYPE_MODE (type), &d))
1065 	    ok = true;
1066 	}
1067     }
1068   else if (TREE_CODE (type) == BOOLEAN_TYPE
1069 	   && (TYPE_PTR_P (ftype) || TYPE_PTRMEM_P (ftype)))
1070     /* This hasn't actually made it into C++20 yet, but let's add it now to get
1071        an idea of the impact.  */
1072     ok = (cxx_dialect < cxx2a);
1073 
1074   bool almost_ok = ok;
1075   if (!ok && !CONSTANT_CLASS_P (init) && (complain & tf_warning_or_error))
1076     {
1077       tree folded = cp_fully_fold (init);
1078       if (TREE_CONSTANT (folded) && check_narrowing (type, folded, tf_none))
1079 	almost_ok = true;
1080     }
1081 
1082   if (!ok)
1083     {
1084       location_t loc = cp_expr_loc_or_input_loc (init);
1085       if (cxx_dialect == cxx98)
1086 	{
1087 	  if (complain & tf_warning)
1088 	    warning_at (loc, OPT_Wnarrowing, "narrowing conversion of %qE "
1089 			"from %qH to %qI is ill-formed in C++11",
1090 			init, ftype, type);
1091 	  ok = true;
1092 	}
1093       else if (!CONSTANT_CLASS_P (init))
1094 	{
1095 	  if (complain & tf_warning_or_error)
1096 	    {
1097 	      auto_diagnostic_group d;
1098 	      if ((!almost_ok || pedantic)
1099 		  && pedwarn (loc, OPT_Wnarrowing,
1100 			      "narrowing conversion of %qE from %qH to %qI",
1101 			      init, ftype, type)
1102 		  && almost_ok)
1103 		inform (loc, " the expression has a constant value but is not "
1104 			"a C++ constant-expression");
1105 	      ok = true;
1106 	    }
1107 	}
1108       else if (complain & tf_error)
1109 	{
1110 	  int savederrorcount = errorcount;
1111 	  global_dc->pedantic_errors = 1;
1112 	  pedwarn (loc, OPT_Wnarrowing,
1113 		   "narrowing conversion of %qE from %qH to %qI",
1114 		   init, ftype, type);
1115 	  if (errorcount == savederrorcount)
1116 	    ok = true;
1117 	  global_dc->pedantic_errors = flag_pedantic_errors;
1118 	}
1119     }
1120 
1121   return ok;
1122 }
1123 
1124 /* True iff TYPE is a C++2a "ordinary" character type.  */
1125 
1126 bool
ordinary_char_type_p(tree type)1127 ordinary_char_type_p (tree type)
1128 {
1129   type = TYPE_MAIN_VARIANT (type);
1130   return (type == char_type_node
1131 	  || type == signed_char_type_node
1132 	  || type == unsigned_char_type_node);
1133 }
1134 
1135 /* Process the initializer INIT for a variable of type TYPE, emitting
1136    diagnostics for invalid initializers and converting the initializer as
1137    appropriate.
1138 
1139    For aggregate types, it assumes that reshape_init has already run, thus the
1140    initializer will have the right shape (brace elision has been undone).
1141 
1142    NESTED is non-zero iff we are being called for an element of a CONSTRUCTOR,
1143    2 iff the element of a CONSTRUCTOR is inside another CONSTRUCTOR.  */
1144 
1145 static tree
digest_init_r(tree type,tree init,int nested,int flags,tsubst_flags_t complain)1146 digest_init_r (tree type, tree init, int nested, int flags,
1147 	       tsubst_flags_t complain)
1148 {
1149   enum tree_code code = TREE_CODE (type);
1150 
1151   if (error_operand_p (init))
1152     return error_mark_node;
1153 
1154   gcc_assert (init);
1155 
1156   /* We must strip the outermost array type when completing the type,
1157      because the its bounds might be incomplete at the moment.  */
1158   if (!complete_type_or_maybe_complain (code == ARRAY_TYPE
1159 					? TREE_TYPE (type) : type, NULL_TREE,
1160 					complain))
1161     return error_mark_node;
1162 
1163   location_t loc = cp_expr_loc_or_input_loc (init);
1164 
1165   tree stripped_init = init;
1166 
1167   if (BRACE_ENCLOSED_INITIALIZER_P (init)
1168       && CONSTRUCTOR_IS_PAREN_INIT (init))
1169     flags |= LOOKUP_AGGREGATE_PAREN_INIT;
1170 
1171   /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue
1172      (g++.old-deja/g++.law/casts2.C).  */
1173   if (TREE_CODE (init) == NON_LVALUE_EXPR)
1174     stripped_init = TREE_OPERAND (init, 0);
1175 
1176   stripped_init = tree_strip_any_location_wrapper (stripped_init);
1177 
1178   /* Initialization of an array of chars from a string constant. The initializer
1179      can be optionally enclosed in braces, but reshape_init has already removed
1180      them if they were present.  */
1181   if (code == ARRAY_TYPE)
1182     {
1183       if (nested && !TYPE_DOMAIN (type))
1184 	/* C++ flexible array members have a null domain.  */
1185 	{
1186 	  if (flags & LOOKUP_ALLOW_FLEXARRAY_INIT)
1187 	    pedwarn (loc, OPT_Wpedantic,
1188 		     "initialization of a flexible array member");
1189 	  else
1190 	    {
1191 	      if (complain & tf_error)
1192 		error_at (loc, "non-static initialization of"
1193 			       " a flexible array member");
1194 	      return error_mark_node;
1195 	    }
1196 	}
1197 
1198       tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
1199       if (char_type_p (typ1)
1200 	  && TREE_CODE (stripped_init) == STRING_CST)
1201 	{
1202 	  tree char_type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (init)));
1203 	  bool incompat_string_cst = false;
1204 
1205 	  if (typ1 != char_type)
1206 	    {
1207 	      /* The array element type does not match the initializing string
1208 	         literal element type; this is only allowed when both types are
1209 	         ordinary character type.  There are no string literals of
1210 	         signed or unsigned char type in the language, but we can get
1211 	         them internally from converting braced-init-lists to
1212 	         STRING_CST.  */
1213 	      if (ordinary_char_type_p (typ1)
1214 		  && ordinary_char_type_p (char_type))
1215 		/* OK */;
1216 	      else
1217 		incompat_string_cst = true;
1218 	    }
1219 
1220 	  if (incompat_string_cst)
1221 	    {
1222 	      if (complain & tf_error)
1223 		error_at (loc, "cannot initialize array of %qT from "
1224 		          "a string literal with type array of %qT",
1225 		          typ1, char_type);
1226 	      return error_mark_node;
1227 	    }
1228 
1229 	  if (nested == 2 && !TYPE_DOMAIN (type))
1230 	    {
1231 	      if (complain & tf_error)
1232 		error_at (loc, "initialization of flexible array member "
1233 			       "in a nested context");
1234 	      return error_mark_node;
1235 	    }
1236 
1237 	  if (type != TREE_TYPE (init)
1238 	      && !variably_modified_type_p (type, NULL_TREE))
1239 	    {
1240 	      init = copy_node (init);
1241 	      TREE_TYPE (init) = type;
1242 	      /* If we have a location wrapper, then also copy the wrapped
1243 		 node, and update the copy's type.  */
1244 	      if (location_wrapper_p (init))
1245 		{
1246 		  stripped_init = copy_node (stripped_init);
1247 		  TREE_OPERAND (init, 0) = stripped_init;
1248 		  TREE_TYPE (stripped_init) = type;
1249 		}
1250 	    }
1251 	  if (TYPE_DOMAIN (type) && TREE_CONSTANT (TYPE_SIZE (type)))
1252 	    {
1253 	      /* Not a flexible array member.  */
1254 	      int size = TREE_INT_CST_LOW (TYPE_SIZE (type));
1255 	      size = (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
1256 	      /* In C it is ok to subtract 1 from the length of the string
1257 		 because it's ok to ignore the terminating null char that is
1258 		 counted in the length of the constant, but in C++ this would
1259 		 be invalid.  */
1260 	      if (size < TREE_STRING_LENGTH (stripped_init))
1261 		{
1262 		  permerror (loc, "initializer-string for %qT is too long",
1263 			     type);
1264 
1265 		  init = build_string (size,
1266 				       TREE_STRING_POINTER (stripped_init));
1267 		  TREE_TYPE (init) = type;
1268 		}
1269 	    }
1270 	  return init;
1271 	}
1272     }
1273 
1274   /* Handle scalar types (including conversions) and references.  */
1275   if ((code != COMPLEX_TYPE || BRACE_ENCLOSED_INITIALIZER_P (stripped_init))
1276       && (SCALAR_TYPE_P (type) || code == REFERENCE_TYPE))
1277     {
1278       /* Narrowing is OK when initializing an aggregate from
1279 	 a parenthesized list.  */
1280       if (nested && !(flags & LOOKUP_AGGREGATE_PAREN_INIT))
1281 	flags |= LOOKUP_NO_NARROWING;
1282       init = convert_for_initialization (0, type, init, flags,
1283 					 ICR_INIT, NULL_TREE, 0,
1284 					 complain);
1285 
1286       return init;
1287     }
1288 
1289   /* Come here only for aggregates: records, arrays, unions, complex numbers
1290      and vectors.  */
1291   gcc_assert (code == ARRAY_TYPE
1292 	      || VECTOR_TYPE_P (type)
1293 	      || code == RECORD_TYPE
1294 	      || code == UNION_TYPE
1295 	      || code == COMPLEX_TYPE);
1296 
1297   /* "If T is a class type and the initializer list has a single
1298      element of type cv U, where U is T or a class derived from T,
1299      the object is initialized from that element."  */
1300   if (cxx_dialect >= cxx11
1301       && BRACE_ENCLOSED_INITIALIZER_P (stripped_init)
1302       && CONSTRUCTOR_NELTS (stripped_init) == 1
1303       && ((CLASS_TYPE_P (type) && !CLASSTYPE_NON_AGGREGATE (type))
1304 	  || VECTOR_TYPE_P (type)))
1305     {
1306       tree elt = CONSTRUCTOR_ELT (stripped_init, 0)->value;
1307       if (reference_related_p (type, TREE_TYPE (elt)))
1308 	{
1309 	  /* In C++17, aggregates can have bases, thus participate in
1310 	     aggregate initialization.  In the following case:
1311 
1312 	       struct B { int c; };
1313 	       struct D : B { };
1314 	       D d{{D{{42}}}};
1315 
1316 	    there's an extra set of braces, so the D temporary initializes
1317 	    the first element of d, which is the B base subobject.  The base
1318 	    of type B is copy-initialized from the D temporary, causing
1319 	    object slicing.  */
1320 	  tree field = next_initializable_field (TYPE_FIELDS (type));
1321 	  if (field && DECL_FIELD_IS_BASE (field))
1322 	    {
1323 	      if (warning_at (loc, 0, "initializing a base class of type %qT "
1324 			      "results in object slicing", TREE_TYPE (field)))
1325 		inform (loc, "remove %<{ }%> around initializer");
1326 	    }
1327 	  else if (flag_checking)
1328 	    /* We should have fixed this in reshape_init.  */
1329 	    gcc_unreachable ();
1330 	}
1331     }
1332 
1333   if (BRACE_ENCLOSED_INITIALIZER_P (stripped_init)
1334       && !TYPE_NON_AGGREGATE_CLASS (type))
1335     return process_init_constructor (type, stripped_init, nested, flags,
1336 				     complain);
1337   else
1338     {
1339       if (COMPOUND_LITERAL_P (stripped_init) && code == ARRAY_TYPE)
1340 	{
1341 	  if (complain & tf_error)
1342 	    error_at (loc, "cannot initialize aggregate of type %qT with "
1343 		      "a compound literal", type);
1344 
1345 	  return error_mark_node;
1346 	}
1347 
1348       if (code == ARRAY_TYPE
1349 	  && !BRACE_ENCLOSED_INITIALIZER_P (stripped_init))
1350 	{
1351 	  /* Allow the result of build_array_copy and of
1352 	     build_value_init_noctor.  */
1353 	  if ((TREE_CODE (stripped_init) == VEC_INIT_EXPR
1354 	       || TREE_CODE (stripped_init) == CONSTRUCTOR)
1355 	      && (same_type_ignoring_top_level_qualifiers_p
1356 		  (type, TREE_TYPE (init))))
1357 	    return init;
1358 
1359 	  if (complain & tf_error)
1360 	    error_at (loc, "array must be initialized with a brace-enclosed"
1361 		      " initializer");
1362 	  return error_mark_node;
1363 	}
1364 
1365       return convert_for_initialization (NULL_TREE, type, init,
1366 					 flags,
1367 					 ICR_INIT, NULL_TREE, 0,
1368                                          complain);
1369     }
1370 }
1371 
1372 tree
digest_init(tree type,tree init,tsubst_flags_t complain)1373 digest_init (tree type, tree init, tsubst_flags_t complain)
1374 {
1375   return digest_init_r (type, init, 0, LOOKUP_IMPLICIT, complain);
1376 }
1377 
1378 tree
digest_init_flags(tree type,tree init,int flags,tsubst_flags_t complain)1379 digest_init_flags (tree type, tree init, int flags, tsubst_flags_t complain)
1380 {
1381   return digest_init_r (type, init, 0, flags, complain);
1382 }
1383 
1384 /* Process the initializer INIT for an NSDMI DECL (a FIELD_DECL).  */
1385 tree
digest_nsdmi_init(tree decl,tree init,tsubst_flags_t complain)1386 digest_nsdmi_init (tree decl, tree init, tsubst_flags_t complain)
1387 {
1388   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
1389 
1390   tree type = TREE_TYPE (decl);
1391   if (DECL_BIT_FIELD_TYPE (decl))
1392     type = DECL_BIT_FIELD_TYPE (decl);
1393   int flags = LOOKUP_IMPLICIT;
1394   if (DIRECT_LIST_INIT_P (init))
1395     {
1396       flags = LOOKUP_NORMAL;
1397       complain |= tf_no_cleanup;
1398     }
1399   if (BRACE_ENCLOSED_INITIALIZER_P (init)
1400       && CP_AGGREGATE_TYPE_P (type))
1401     init = reshape_init (type, init, complain);
1402   init = digest_init_flags (type, init, flags, complain);
1403   return init;
1404 }
1405 
1406 /* Set of flags used within process_init_constructor to describe the
1407    initializers.  */
1408 #define PICFLAG_ERRONEOUS 1
1409 #define PICFLAG_NOT_ALL_CONSTANT 2
1410 #define PICFLAG_NOT_ALL_SIMPLE 4
1411 #define PICFLAG_SIDE_EFFECTS 8
1412 
1413 /* Given an initializer INIT, return the flag (PICFLAG_*) which better
1414    describe it.  */
1415 
1416 static int
picflag_from_initializer(tree init)1417 picflag_from_initializer (tree init)
1418 {
1419   if (init == error_mark_node)
1420     return PICFLAG_ERRONEOUS;
1421   else if (!TREE_CONSTANT (init))
1422     {
1423       if (TREE_SIDE_EFFECTS (init))
1424 	return PICFLAG_SIDE_EFFECTS;
1425       else
1426 	return PICFLAG_NOT_ALL_CONSTANT;
1427     }
1428   else if (!initializer_constant_valid_p (init, TREE_TYPE (init)))
1429     return PICFLAG_NOT_ALL_SIMPLE;
1430   return 0;
1431 }
1432 
1433 /* Adjust INIT for going into a CONSTRUCTOR.  */
1434 
1435 static tree
massage_init_elt(tree type,tree init,int nested,int flags,tsubst_flags_t complain)1436 massage_init_elt (tree type, tree init, int nested, int flags,
1437 		  tsubst_flags_t complain)
1438 {
1439   int new_flags = LOOKUP_IMPLICIT;
1440   if (flags & LOOKUP_ALLOW_FLEXARRAY_INIT)
1441     new_flags |= LOOKUP_ALLOW_FLEXARRAY_INIT;
1442   if (flags & LOOKUP_AGGREGATE_PAREN_INIT)
1443     new_flags |= LOOKUP_AGGREGATE_PAREN_INIT;
1444   init = digest_init_r (type, init, nested ? 2 : 1, new_flags, complain);
1445   /* When we defer constant folding within a statement, we may want to
1446      defer this folding as well.  */
1447   tree t = fold_non_dependent_init (init, complain);
1448   if (TREE_CONSTANT (t))
1449     init = t;
1450   return init;
1451 }
1452 
1453 /* Subroutine of process_init_constructor, which will process an initializer
1454    INIT for an array or vector of type TYPE. Returns the flags (PICFLAG_*)
1455    which describe the initializers.  */
1456 
1457 static int
process_init_constructor_array(tree type,tree init,int nested,int flags,tsubst_flags_t complain)1458 process_init_constructor_array (tree type, tree init, int nested, int flags,
1459 				tsubst_flags_t complain)
1460 {
1461   unsigned HOST_WIDE_INT i, len = 0;
1462   int picflags = 0;
1463   bool unbounded = false;
1464   constructor_elt *ce;
1465   vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (init);
1466 
1467   gcc_assert (TREE_CODE (type) == ARRAY_TYPE
1468 	      || VECTOR_TYPE_P (type));
1469 
1470   if (TREE_CODE (type) == ARRAY_TYPE)
1471     {
1472       /* C++ flexible array members have a null domain.  */
1473       tree domain = TYPE_DOMAIN (type);
1474       if (domain && TREE_CONSTANT (TYPE_MAX_VALUE (domain)))
1475 	len = wi::ext (wi::to_offset (TYPE_MAX_VALUE (domain))
1476                        - wi::to_offset (TYPE_MIN_VALUE (domain)) + 1,
1477 		       TYPE_PRECISION (TREE_TYPE (domain)),
1478 		       TYPE_SIGN (TREE_TYPE (domain))).to_uhwi ();
1479       else
1480 	unbounded = true;  /* Take as many as there are.  */
1481 
1482       if (nested == 2 && !domain && !vec_safe_is_empty (v))
1483 	{
1484 	  if (complain & tf_error)
1485 	    error_at (cp_expr_loc_or_input_loc (init),
1486 		      "initialization of flexible array member "
1487 		      "in a nested context");
1488 	  return PICFLAG_ERRONEOUS;
1489 	}
1490     }
1491   else
1492     /* Vectors are like simple fixed-size arrays.  */
1493     unbounded = !TYPE_VECTOR_SUBPARTS (type).is_constant (&len);
1494 
1495   /* There must not be more initializers than needed.  */
1496   if (!unbounded && vec_safe_length (v) > len)
1497     {
1498       if (complain & tf_error)
1499 	error ("too many initializers for %qT", type);
1500       else
1501 	return PICFLAG_ERRONEOUS;
1502     }
1503 
1504   FOR_EACH_VEC_SAFE_ELT (v, i, ce)
1505     {
1506       if (!ce->index)
1507 	ce->index = size_int (i);
1508       else if (!check_array_designated_initializer (ce, i))
1509 	ce->index = error_mark_node;
1510       gcc_assert (ce->value);
1511       ce->value
1512 	= massage_init_elt (TREE_TYPE (type), ce->value, nested, flags,
1513 			    complain);
1514 
1515       if (TREE_CODE (ce->value) == CONSTRUCTOR
1516 	  && CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ce->value))
1517 	{
1518 	  /* Shift CONSTRUCTOR_PLACEHOLDER_BOUNDARY from the element initializer
1519 	     up to the array initializer, so that the call to
1520 	     replace_placeholders from store_init_value can resolve any
1521 	     PLACEHOLDER_EXPRs inside this element initializer.  */
1522 	  CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ce->value) = 0;
1523 	  CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init) = 1;
1524 	}
1525 
1526       gcc_checking_assert
1527 	(ce->value == error_mark_node
1528 	 || (same_type_ignoring_top_level_qualifiers_p
1529 	     (strip_array_types (TREE_TYPE (type)),
1530 	      strip_array_types (TREE_TYPE (ce->value)))));
1531 
1532       picflags |= picflag_from_initializer (ce->value);
1533     }
1534 
1535   /* No more initializers. If the array is unbounded, we are done. Otherwise,
1536      we must add initializers ourselves.  */
1537   if (!unbounded)
1538     for (; i < len; ++i)
1539       {
1540 	tree next;
1541 
1542 	if (type_build_ctor_call (TREE_TYPE (type)))
1543 	  {
1544 	    /* If this type needs constructors run for default-initialization,
1545 	       we can't rely on the back end to do it for us, so make the
1546 	       initialization explicit by list-initializing from T{}.  */
1547 	    next = build_constructor (init_list_type_node, NULL);
1548 	    next = massage_init_elt (TREE_TYPE (type), next, nested, flags,
1549 				     complain);
1550 	    if (initializer_zerop (next))
1551 	      /* The default zero-initialization is fine for us; don't
1552 		 add anything to the CONSTRUCTOR.  */
1553 	      next = NULL_TREE;
1554 	    else if (TREE_CODE (next) == CONSTRUCTOR
1555 		     && CONSTRUCTOR_PLACEHOLDER_BOUNDARY (next))
1556 	      {
1557 		/* As above.  */
1558 		CONSTRUCTOR_PLACEHOLDER_BOUNDARY (next) = 0;
1559 		CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init) = 1;
1560 	      }
1561 	  }
1562 	else if (!zero_init_p (TREE_TYPE (type)))
1563 	  next = build_zero_init (TREE_TYPE (type),
1564 				  /*nelts=*/NULL_TREE,
1565 				  /*static_storage_p=*/false);
1566 	else
1567 	  /* The default zero-initialization is fine for us; don't
1568 	     add anything to the CONSTRUCTOR.  */
1569 	  next = NULL_TREE;
1570 
1571 	if (next)
1572 	  {
1573 	    picflags |= picflag_from_initializer (next);
1574 	    if (len > i+1
1575 		&& (initializer_constant_valid_p (next, TREE_TYPE (next))
1576 		    == null_pointer_node))
1577 	      {
1578 		tree range = build2 (RANGE_EXPR, size_type_node,
1579 				     build_int_cst (size_type_node, i),
1580 				     build_int_cst (size_type_node, len - 1));
1581 		CONSTRUCTOR_APPEND_ELT (v, range, next);
1582 		break;
1583 	      }
1584 	    else
1585 	      CONSTRUCTOR_APPEND_ELT (v, size_int (i), next);
1586 	  }
1587 	else
1588 	  /* Don't bother checking all the other elements.  */
1589 	  break;
1590       }
1591 
1592   CONSTRUCTOR_ELTS (init) = v;
1593   return picflags;
1594 }
1595 
1596 /* Subroutine of process_init_constructor, which will process an initializer
1597    INIT for a class of type TYPE. Returns the flags (PICFLAG_*) which describe
1598    the initializers.  */
1599 
1600 static int
process_init_constructor_record(tree type,tree init,int nested,int flags,tsubst_flags_t complain)1601 process_init_constructor_record (tree type, tree init, int nested, int flags,
1602 				 tsubst_flags_t complain)
1603 {
1604   vec<constructor_elt, va_gc> *v = NULL;
1605   tree field;
1606   int skipped = 0;
1607 
1608   gcc_assert (TREE_CODE (type) == RECORD_TYPE);
1609   gcc_assert (!CLASSTYPE_VBASECLASSES (type));
1610   gcc_assert (!TYPE_BINFO (type)
1611 	      || cxx_dialect >= cxx17
1612 	      || !BINFO_N_BASE_BINFOS (TYPE_BINFO (type)));
1613   gcc_assert (!TYPE_POLYMORPHIC_P (type));
1614 
1615  restart:
1616   int picflags = 0;
1617   unsigned HOST_WIDE_INT idx = 0;
1618   int designator_skip = -1;
1619   /* Generally, we will always have an index for each initializer (which is
1620      a FIELD_DECL, put by reshape_init), but compound literals don't go trough
1621      reshape_init. So we need to handle both cases.  */
1622   for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
1623     {
1624       tree next;
1625       tree type;
1626 
1627       if (TREE_CODE (field) != FIELD_DECL
1628 	  || (DECL_ARTIFICIAL (field)
1629 	      && !(cxx_dialect >= cxx17 && DECL_FIELD_IS_BASE (field))))
1630 	continue;
1631 
1632       if (DECL_UNNAMED_BIT_FIELD (field))
1633 	continue;
1634 
1635       /* If this is a bitfield, first convert to the declared type.  */
1636       type = TREE_TYPE (field);
1637       if (DECL_BIT_FIELD_TYPE (field))
1638 	type = DECL_BIT_FIELD_TYPE (field);
1639       if (type == error_mark_node)
1640 	return PICFLAG_ERRONEOUS;
1641 
1642       next = NULL_TREE;
1643       if (idx < CONSTRUCTOR_NELTS (init))
1644 	{
1645 	  constructor_elt *ce = &(*CONSTRUCTOR_ELTS (init))[idx];
1646 	  if (ce->index)
1647 	    {
1648 	      /* We can have either a FIELD_DECL or an IDENTIFIER_NODE. The
1649 		 latter case can happen in templates where lookup has to be
1650 		 deferred.  */
1651 	      gcc_assert (TREE_CODE (ce->index) == FIELD_DECL
1652 			  || identifier_p (ce->index));
1653 	      if (ce->index == field || ce->index == DECL_NAME (field))
1654 		next = ce->value;
1655 	      else if (ANON_AGGR_TYPE_P (type)
1656 		       && search_anon_aggr (type,
1657 					    TREE_CODE (ce->index) == FIELD_DECL
1658 					    ? DECL_NAME (ce->index)
1659 					    : ce->index))
1660 		/* If the element is an anonymous union object and the
1661 		   initializer list is a designated-initializer-list, the
1662 		   anonymous union object is initialized by the
1663 		   designated-initializer-list { D }, where D is the
1664 		   designated-initializer-clause naming a member of the
1665 		   anonymous union object.  */
1666 		next = build_constructor_single (init_list_type_node,
1667 						 ce->index, ce->value);
1668 	      else
1669 		{
1670 		  ce = NULL;
1671 		  if (designator_skip == -1)
1672 		    designator_skip = 1;
1673 		}
1674 	    }
1675 	  else
1676 	    {
1677 	      designator_skip = 0;
1678 	      next = ce->value;
1679 	    }
1680 
1681 	  if (ce)
1682 	    {
1683 	      gcc_assert (ce->value);
1684 	      next = massage_init_elt (type, next, nested, flags, complain);
1685 	      ++idx;
1686 	    }
1687 	}
1688       if (next == error_mark_node)
1689 	/* We skip initializers for empty bases/fields, so skipping an invalid
1690 	   one could make us accept invalid code.  */
1691 	return PICFLAG_ERRONEOUS;
1692       else if (next)
1693 	/* Already handled above.  */;
1694       else if (DECL_INITIAL (field))
1695 	{
1696 	  if (skipped > 0)
1697 	    {
1698 	      /* We're using an NSDMI past a field with implicit
1699 	         zero-init.  Go back and make it explicit.  */
1700 	      skipped = -1;
1701 	      vec_safe_truncate (v, 0);
1702 	      goto restart;
1703 	    }
1704 	  /* C++14 aggregate NSDMI.  */
1705 	  next = get_nsdmi (field, /*ctor*/false, complain);
1706 	  if (!CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init)
1707 	      && find_placeholders (next))
1708 	    CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init) = 1;
1709 	}
1710       else if (type_build_ctor_call (TREE_TYPE (field)))
1711 	{
1712 	  /* If this type needs constructors run for
1713 	     default-initialization, we can't rely on the back end to do it
1714 	     for us, so build up TARGET_EXPRs.  If the type in question is
1715 	     a class, just build one up; if it's an array, recurse.  */
1716 	  next = build_constructor (init_list_type_node, NULL);
1717 	  next = massage_init_elt (TREE_TYPE (field), next, nested, flags,
1718 				   complain);
1719 
1720 	  /* Warn when some struct elements are implicitly initialized.  */
1721 	  if ((complain & tf_warning)
1722 	      && !EMPTY_CONSTRUCTOR_P (init))
1723 	    warning (OPT_Wmissing_field_initializers,
1724 		     "missing initializer for member %qD", field);
1725 	}
1726       else
1727 	{
1728 	  const_tree fldtype = TREE_TYPE (field);
1729 	  if (TYPE_REF_P (fldtype))
1730 	    {
1731 	      if (complain & tf_error)
1732 		error ("member %qD is uninitialized reference", field);
1733 	      else
1734 		return PICFLAG_ERRONEOUS;
1735 	    }
1736 	  else if (CLASSTYPE_REF_FIELDS_NEED_INIT (fldtype))
1737 	    {
1738 	      if (complain & tf_error)
1739 		error ("member %qD with uninitialized reference fields", field);
1740 	      else
1741 		return PICFLAG_ERRONEOUS;
1742 	    }
1743 	  /* Do nothing for flexible array members since they need not have any
1744 	     elements.  Don't worry about 'skipped' because a flexarray has to
1745 	     be the last field.  */
1746 	  else if (TREE_CODE (fldtype) == ARRAY_TYPE && !TYPE_DOMAIN (fldtype))
1747 	    continue;
1748 
1749 	  /* Warn when some struct elements are implicitly initialized
1750 	     to zero.  */
1751 	  if ((complain & tf_warning)
1752 	      && !EMPTY_CONSTRUCTOR_P (init))
1753 	    warning (OPT_Wmissing_field_initializers,
1754 		     "missing initializer for member %qD", field);
1755 
1756 	  if (!zero_init_p (fldtype) || skipped < 0)
1757 	    {
1758 	      if (TYPE_REF_P (fldtype))
1759 		next = build_zero_cst (TREE_TYPE (field));
1760 	      else
1761 		next = build_zero_init (TREE_TYPE (field), /*nelts=*/NULL_TREE,
1762 					/*static_storage_p=*/false);
1763 	    }
1764 	  else
1765 	    {
1766 	      /* The default zero-initialization is fine for us; don't
1767 		 add anything to the CONSTRUCTOR.  */
1768 	      skipped = 1;
1769 	      continue;
1770 	    }
1771 	}
1772 
1773       if (DECL_SIZE (field) && integer_zerop (DECL_SIZE (field))
1774 	  && !TREE_SIDE_EFFECTS (next))
1775 	/* Don't add trivial initialization of an empty base/field to the
1776 	   constructor, as they might not be ordered the way the back-end
1777 	   expects.  */
1778 	continue;
1779 
1780       /* If this is a bitfield, now convert to the lowered type.  */
1781       if (type != TREE_TYPE (field))
1782 	next = cp_convert_and_check (TREE_TYPE (field), next, complain);
1783       picflags |= picflag_from_initializer (next);
1784       CONSTRUCTOR_APPEND_ELT (v, field, next);
1785     }
1786 
1787   if (idx < CONSTRUCTOR_NELTS (init))
1788     {
1789       if (complain & tf_error)
1790 	{
1791 	  constructor_elt *ce = &(*CONSTRUCTOR_ELTS (init))[idx];
1792 	  /* For better diagnostics, try to find out if it is really
1793 	     the case of too many initializers or if designators are
1794 	     in incorrect order.  */
1795 	  if (designator_skip == 1 && ce->index)
1796 	    {
1797 	      gcc_assert (TREE_CODE (ce->index) == FIELD_DECL
1798 			  || identifier_p (ce->index));
1799 	      for (field = TYPE_FIELDS (type);
1800 		   field; field = DECL_CHAIN (field))
1801 		{
1802 		  if (TREE_CODE (field) != FIELD_DECL
1803 		      || (DECL_ARTIFICIAL (field)
1804 			  && !(cxx_dialect >= cxx17
1805 			       && DECL_FIELD_IS_BASE (field))))
1806 		    continue;
1807 
1808 		  if (DECL_UNNAMED_BIT_FIELD (field))
1809 		    continue;
1810 
1811 		  if (ce->index == field || ce->index == DECL_NAME (field))
1812 		    break;
1813 		  if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1814 		    {
1815 		      tree t
1816 			= search_anon_aggr (TREE_TYPE (field),
1817 					    TREE_CODE (ce->index) == FIELD_DECL
1818 					    ? DECL_NAME (ce->index)
1819 					    : ce->index);
1820 		      if (t)
1821 			{
1822 			  field = t;
1823 			  break;
1824 			}
1825 		    }
1826 		}
1827 	    }
1828 	  if (field)
1829 	    error ("designator order for field %qD does not match declaration "
1830 		   "order in %qT", field, type);
1831 	  else
1832 	    error ("too many initializers for %qT", type);
1833 	}
1834       else
1835 	return PICFLAG_ERRONEOUS;
1836     }
1837 
1838   CONSTRUCTOR_ELTS (init) = v;
1839   return picflags;
1840 }
1841 
1842 /* Subroutine of process_init_constructor, which will process a single
1843    initializer INIT for a union of type TYPE. Returns the flags (PICFLAG_*)
1844    which describe the initializer.  */
1845 
1846 static int
process_init_constructor_union(tree type,tree init,int nested,int flags,tsubst_flags_t complain)1847 process_init_constructor_union (tree type, tree init, int nested, int flags,
1848 				tsubst_flags_t complain)
1849 {
1850   constructor_elt *ce;
1851   int len;
1852 
1853   /* If the initializer was empty, use the union's NSDMI if it has one.
1854      Otherwise use default zero initialization.  */
1855   if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)))
1856     {
1857       for (tree field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1858 	{
1859 	  if (TREE_CODE (field) == FIELD_DECL
1860 	      && DECL_INITIAL (field) != NULL_TREE)
1861 	    {
1862 	      tree val = get_nsdmi (field, /*in_ctor=*/false, complain);
1863 	      if (!CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init)
1864 		  && find_placeholders (val))
1865 		CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init) = 1;
1866 	      CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (init), field, val);
1867 	      break;
1868 	    }
1869 	}
1870 
1871       if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)))
1872 	return 0;
1873     }
1874 
1875   len = CONSTRUCTOR_ELTS (init)->length ();
1876   if (len > 1)
1877     {
1878       if (!(complain & tf_error))
1879 	return PICFLAG_ERRONEOUS;
1880       error ("too many initializers for %qT", type);
1881       CONSTRUCTOR_ELTS (init)->block_remove (1, len-1);
1882     }
1883 
1884   ce = &(*CONSTRUCTOR_ELTS (init))[0];
1885 
1886   /* If this element specifies a field, initialize via that field.  */
1887   if (ce->index)
1888     {
1889       if (TREE_CODE (ce->index) == FIELD_DECL)
1890 	;
1891       else if (identifier_p (ce->index))
1892 	{
1893 	  /* This can happen within a cast, see g++.dg/opt/cse2.C.  */
1894 	  tree name = ce->index;
1895 	  tree field;
1896 	  for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1897 	    if (DECL_NAME (field) == name)
1898 	      break;
1899 	  if (!field)
1900 	    {
1901 	      if (complain & tf_error)
1902 		error ("no field %qD found in union being initialized",
1903 		       field);
1904 	      ce->value = error_mark_node;
1905 	    }
1906 	  ce->index = field;
1907 	}
1908       else
1909 	{
1910 	  gcc_assert (TREE_CODE (ce->index) == INTEGER_CST
1911 		      || TREE_CODE (ce->index) == RANGE_EXPR);
1912 	  if (complain & tf_error)
1913 	    error ("index value instead of field name in union initializer");
1914 	  ce->value = error_mark_node;
1915 	}
1916     }
1917   else
1918     {
1919       /* Find the first named field.  ANSI decided in September 1990
1920 	 that only named fields count here.  */
1921       tree field = TYPE_FIELDS (type);
1922       while (field && (!DECL_NAME (field) || TREE_CODE (field) != FIELD_DECL))
1923 	field = TREE_CHAIN (field);
1924       if (field == NULL_TREE)
1925 	{
1926 	  if (complain & tf_error)
1927 	    error ("too many initializers for %qT", type);
1928 	  ce->value = error_mark_node;
1929 	}
1930       ce->index = field;
1931     }
1932 
1933   if (ce->value && ce->value != error_mark_node)
1934     ce->value = massage_init_elt (TREE_TYPE (ce->index), ce->value, nested,
1935 				  flags, complain);
1936 
1937   return picflag_from_initializer (ce->value);
1938 }
1939 
1940 /* Process INIT, a constructor for a variable of aggregate type TYPE. The
1941    constructor is a brace-enclosed initializer, and will be modified in-place.
1942 
1943    Each element is converted to the right type through digest_init, and
1944    missing initializers are added following the language rules (zero-padding,
1945    etc.).
1946 
1947    After the execution, the initializer will have TREE_CONSTANT if all elts are
1948    constant, and TREE_STATIC set if, in addition, all elts are simple enough
1949    constants that the assembler and linker can compute them.
1950 
1951    The function returns the initializer itself, or error_mark_node in case
1952    of error.  */
1953 
1954 static tree
process_init_constructor(tree type,tree init,int nested,int flags,tsubst_flags_t complain)1955 process_init_constructor (tree type, tree init, int nested, int flags,
1956 			  tsubst_flags_t complain)
1957 {
1958   int picflags;
1959 
1960   gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
1961 
1962   if (TREE_CODE (type) == ARRAY_TYPE || VECTOR_TYPE_P (type))
1963     picflags = process_init_constructor_array (type, init, nested, flags,
1964 					       complain);
1965   else if (TREE_CODE (type) == RECORD_TYPE)
1966     picflags = process_init_constructor_record (type, init, nested, flags,
1967 						complain);
1968   else if (TREE_CODE (type) == UNION_TYPE)
1969     picflags = process_init_constructor_union (type, init, nested, flags,
1970 					       complain);
1971   else
1972     gcc_unreachable ();
1973 
1974   if (picflags & PICFLAG_ERRONEOUS)
1975     return error_mark_node;
1976 
1977   TREE_TYPE (init) = type;
1978   if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == NULL_TREE)
1979     cp_complete_array_type (&TREE_TYPE (init), init, /*do_default=*/0);
1980   if (picflags & PICFLAG_SIDE_EFFECTS)
1981     {
1982       TREE_CONSTANT (init) = false;
1983       TREE_SIDE_EFFECTS (init) = true;
1984     }
1985   else if (picflags & PICFLAG_NOT_ALL_CONSTANT)
1986     {
1987       /* Make sure TREE_CONSTANT isn't set from build_constructor.  */
1988       TREE_CONSTANT (init) = false;
1989       TREE_SIDE_EFFECTS (init) = false;
1990     }
1991   else
1992     {
1993       TREE_CONSTANT (init) = 1;
1994       TREE_SIDE_EFFECTS (init) = false;
1995       if (!(picflags & PICFLAG_NOT_ALL_SIMPLE))
1996 	TREE_STATIC (init) = 1;
1997     }
1998   return init;
1999 }
2000 
2001 /* Given a structure or union value DATUM, construct and return
2002    the structure or union component which results from narrowing
2003    that value to the base specified in BASETYPE.  For example, given the
2004    hierarchy
2005 
2006    class L { int ii; };
2007    class A : L { ... };
2008    class B : L { ... };
2009    class C : A, B { ... };
2010 
2011    and the declaration
2012 
2013    C x;
2014 
2015    then the expression
2016 
2017    x.A::ii refers to the ii member of the L part of
2018    the A part of the C object named by X.  In this case,
2019    DATUM would be x, and BASETYPE would be A.
2020 
2021    I used to think that this was nonconformant, that the standard specified
2022    that first we look up ii in A, then convert x to an L& and pull out the
2023    ii part.  But in fact, it does say that we convert x to an A&; A here
2024    is known as the "naming class".  (jason 2000-12-19)
2025 
2026    BINFO_P points to a variable initialized either to NULL_TREE or to the
2027    binfo for the specific base subobject we want to convert to.  */
2028 
2029 tree
build_scoped_ref(tree datum,tree basetype,tree * binfo_p)2030 build_scoped_ref (tree datum, tree basetype, tree* binfo_p)
2031 {
2032   tree binfo;
2033 
2034   if (datum == error_mark_node)
2035     return error_mark_node;
2036   if (*binfo_p)
2037     binfo = *binfo_p;
2038   else
2039     binfo = lookup_base (TREE_TYPE (datum), basetype, ba_check,
2040 			 NULL, tf_warning_or_error);
2041 
2042   if (!binfo || binfo == error_mark_node)
2043     {
2044       *binfo_p = NULL_TREE;
2045       if (!binfo)
2046 	error_not_base_type (basetype, TREE_TYPE (datum));
2047       return error_mark_node;
2048     }
2049 
2050   *binfo_p = binfo;
2051   return build_base_path (PLUS_EXPR, datum, binfo, 1,
2052 			  tf_warning_or_error);
2053 }
2054 
2055 /* Build a reference to an object specified by the C++ `->' operator.
2056    Usually this just involves dereferencing the object, but if the
2057    `->' operator is overloaded, then such overloads must be
2058    performed until an object which does not have the `->' operator
2059    overloaded is found.  An error is reported when circular pointer
2060    delegation is detected.  */
2061 
2062 tree
build_x_arrow(location_t loc,tree expr,tsubst_flags_t complain)2063 build_x_arrow (location_t loc, tree expr, tsubst_flags_t complain)
2064 {
2065   tree orig_expr = expr;
2066   tree type = TREE_TYPE (expr);
2067   tree last_rval = NULL_TREE;
2068   vec<tree, va_gc> *types_memoized = NULL;
2069 
2070   if (type == error_mark_node)
2071     return error_mark_node;
2072 
2073   if (processing_template_decl)
2074     {
2075       if (type && TYPE_PTR_P (type)
2076 	  && !dependent_scope_p (TREE_TYPE (type)))
2077 	/* Pointer to current instantiation, don't treat as dependent.  */;
2078       else if (type_dependent_expression_p (expr))
2079 	return build_min_nt_loc (loc, ARROW_EXPR, expr);
2080       expr = build_non_dependent_expr (expr);
2081     }
2082 
2083   if (MAYBE_CLASS_TYPE_P (type))
2084     {
2085       struct tinst_level *actual_inst = current_instantiation ();
2086       tree fn = NULL;
2087 
2088       while ((expr = build_new_op (loc, COMPONENT_REF,
2089 				   LOOKUP_NORMAL, expr, NULL_TREE, NULL_TREE,
2090 				   &fn, complain)))
2091 	{
2092 	  if (expr == error_mark_node)
2093 	    return error_mark_node;
2094 
2095 	  /* This provides a better instantiation backtrace in case of
2096 	     error.  */
2097 	  if (fn && DECL_USE_TEMPLATE (fn))
2098 	    push_tinst_level_loc (fn,
2099 				  (current_instantiation () != actual_inst)
2100 				  ? DECL_SOURCE_LOCATION (fn)
2101 				  : input_location);
2102 	  fn = NULL;
2103 
2104 	  if (vec_member (TREE_TYPE (expr), types_memoized))
2105 	    {
2106 	      if (complain & tf_error)
2107 		error ("circular pointer delegation detected");
2108 	      return error_mark_node;
2109 	    }
2110 
2111 	  vec_safe_push (types_memoized, TREE_TYPE (expr));
2112 	  last_rval = expr;
2113 	}
2114 
2115       while (current_instantiation () != actual_inst)
2116 	pop_tinst_level ();
2117 
2118       if (last_rval == NULL_TREE)
2119 	{
2120 	  if (complain & tf_error)
2121 	    error ("base operand of %<->%> has non-pointer type %qT", type);
2122 	  return error_mark_node;
2123 	}
2124 
2125       if (TYPE_REF_P (TREE_TYPE (last_rval)))
2126 	last_rval = convert_from_reference (last_rval);
2127     }
2128   else
2129     {
2130       last_rval = decay_conversion (expr, complain);
2131       if (last_rval == error_mark_node)
2132 	return error_mark_node;
2133     }
2134 
2135   if (TYPE_PTR_P (TREE_TYPE (last_rval)))
2136     {
2137       if (processing_template_decl)
2138 	{
2139 	  expr = build_min (ARROW_EXPR, TREE_TYPE (TREE_TYPE (last_rval)),
2140 			    orig_expr);
2141 	  TREE_SIDE_EFFECTS (expr) = TREE_SIDE_EFFECTS (last_rval);
2142 	  return expr;
2143 	}
2144 
2145       return cp_build_indirect_ref (loc, last_rval, RO_ARROW, complain);
2146     }
2147 
2148   if (complain & tf_error)
2149     {
2150       if (types_memoized)
2151 	error ("result of %<operator->()%> yields non-pointer result");
2152       else
2153 	error ("base operand of %<->%> is not a pointer");
2154     }
2155   return error_mark_node;
2156 }
2157 
2158 /* Return an expression for "DATUM .* COMPONENT".  DATUM has not
2159    already been checked out to be of aggregate type.  */
2160 
2161 tree
build_m_component_ref(tree datum,tree component,tsubst_flags_t complain)2162 build_m_component_ref (tree datum, tree component, tsubst_flags_t complain)
2163 {
2164   tree ptrmem_type;
2165   tree objtype;
2166   tree type;
2167   tree binfo;
2168   tree ctype;
2169 
2170   datum = mark_lvalue_use (datum);
2171   component = mark_rvalue_use (component);
2172 
2173   if (error_operand_p (datum) || error_operand_p (component))
2174     return error_mark_node;
2175 
2176   ptrmem_type = TREE_TYPE (component);
2177   if (!TYPE_PTRMEM_P (ptrmem_type))
2178     {
2179       if (complain & tf_error)
2180 	error ("%qE cannot be used as a member pointer, since it is of "
2181 	       "type %qT", component, ptrmem_type);
2182       return error_mark_node;
2183     }
2184 
2185   objtype = TYPE_MAIN_VARIANT (TREE_TYPE (datum));
2186   if (! MAYBE_CLASS_TYPE_P (objtype))
2187     {
2188       if (complain & tf_error)
2189 	error ("cannot apply member pointer %qE to %qE, which is of "
2190 	       "non-class type %qT", component, datum, objtype);
2191       return error_mark_node;
2192     }
2193 
2194   type = TYPE_PTRMEM_POINTED_TO_TYPE (ptrmem_type);
2195   ctype = complete_type (TYPE_PTRMEM_CLASS_TYPE (ptrmem_type));
2196 
2197   if (!COMPLETE_TYPE_P (ctype))
2198     {
2199       if (!same_type_p (ctype, objtype))
2200 	goto mismatch;
2201       binfo = NULL;
2202     }
2203   else
2204     {
2205       binfo = lookup_base (objtype, ctype, ba_check, NULL, complain);
2206 
2207       if (!binfo)
2208 	{
2209 	mismatch:
2210 	  if (complain & tf_error)
2211 	    error ("pointer to member type %qT incompatible with object "
2212 		   "type %qT", type, objtype);
2213 	  return error_mark_node;
2214 	}
2215       else if (binfo == error_mark_node)
2216 	return error_mark_node;
2217     }
2218 
2219   if (TYPE_PTRDATAMEM_P (ptrmem_type))
2220     {
2221       bool is_lval = real_lvalue_p (datum);
2222       tree ptype;
2223 
2224       /* Compute the type of the field, as described in [expr.ref].
2225 	 There's no such thing as a mutable pointer-to-member, so
2226 	 things are not as complex as they are for references to
2227 	 non-static data members.  */
2228       type = cp_build_qualified_type (type,
2229 				      (cp_type_quals (type)
2230 				       | cp_type_quals (TREE_TYPE (datum))));
2231 
2232       datum = build_address (datum);
2233 
2234       /* Convert object to the correct base.  */
2235       if (binfo)
2236 	{
2237 	  datum = build_base_path (PLUS_EXPR, datum, binfo, 1, complain);
2238 	  if (datum == error_mark_node)
2239 	    return error_mark_node;
2240 	}
2241 
2242       /* Build an expression for "object + offset" where offset is the
2243 	 value stored in the pointer-to-data-member.  */
2244       ptype = build_pointer_type (type);
2245       datum = fold_build_pointer_plus (fold_convert (ptype, datum), component);
2246       datum = cp_build_fold_indirect_ref (datum);
2247       if (datum == error_mark_node)
2248 	return error_mark_node;
2249 
2250       /* If the object expression was an rvalue, return an rvalue.  */
2251       if (!is_lval)
2252 	datum = move (datum);
2253       return datum;
2254     }
2255   else
2256     {
2257       /* 5.5/6: In a .* expression whose object expression is an rvalue, the
2258 	 program is ill-formed if the second operand is a pointer to member
2259 	 function with ref-qualifier & (for C++2A: unless its cv-qualifier-seq
2260 	 is const). In a .* expression whose object expression is an lvalue,
2261 	 the program is ill-formed if the second operand is a pointer to member
2262 	 function with ref-qualifier &&.  */
2263       if (FUNCTION_REF_QUALIFIED (type))
2264 	{
2265 	  bool lval = lvalue_p (datum);
2266 	  if (lval && FUNCTION_RVALUE_QUALIFIED (type))
2267 	    {
2268 	      if (complain & tf_error)
2269 		error ("pointer-to-member-function type %qT requires an rvalue",
2270 		       ptrmem_type);
2271 	      return error_mark_node;
2272 	    }
2273 	  else if (!lval && !FUNCTION_RVALUE_QUALIFIED (type))
2274 	    {
2275 	      if ((type_memfn_quals (type)
2276 		   & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE))
2277 		  != TYPE_QUAL_CONST)
2278 		{
2279 		  if (complain & tf_error)
2280 		    error ("pointer-to-member-function type %qT requires "
2281 			   "an lvalue", ptrmem_type);
2282 		  return error_mark_node;
2283 		}
2284 	      else if (cxx_dialect < cxx2a)
2285 		{
2286 		  if (complain & tf_warning_or_error)
2287 		    pedwarn (input_location, OPT_Wpedantic,
2288 			     "pointer-to-member-function type %qT requires "
2289 			     "an lvalue before C++2a", ptrmem_type);
2290 		  else
2291 		    return error_mark_node;
2292 		}
2293 	    }
2294 	}
2295       return build2 (OFFSET_REF, type, datum, component);
2296     }
2297 }
2298 
2299 /* Return a tree node for the expression TYPENAME '(' PARMS ')'.  */
2300 
2301 static tree
build_functional_cast_1(location_t loc,tree exp,tree parms,tsubst_flags_t complain)2302 build_functional_cast_1 (location_t loc, tree exp, tree parms,
2303 			 tsubst_flags_t complain)
2304 {
2305   /* This is either a call to a constructor,
2306      or a C cast in C++'s `functional' notation.  */
2307 
2308   /* The type to which we are casting.  */
2309   tree type;
2310 
2311   if (error_operand_p (exp) || parms == error_mark_node)
2312     return error_mark_node;
2313 
2314   if (TREE_CODE (exp) == TYPE_DECL)
2315     {
2316       type = TREE_TYPE (exp);
2317 
2318       if (DECL_ARTIFICIAL (exp))
2319 	cp_warn_deprecated_use (type);
2320     }
2321   else
2322     type = exp;
2323 
2324   /* We need to check this explicitly, since value-initialization of
2325      arrays is allowed in other situations.  */
2326   if (TREE_CODE (type) == ARRAY_TYPE)
2327     {
2328       if (complain & tf_error)
2329 	error_at (loc, "functional cast to array type %qT", type);
2330       return error_mark_node;
2331     }
2332 
2333   if (tree anode = type_uses_auto (type))
2334     {
2335       if (!CLASS_PLACEHOLDER_TEMPLATE (anode))
2336 	{
2337 	  if (complain & tf_error)
2338 	    error_at (loc, "invalid use of %qT", anode);
2339 	  return error_mark_node;
2340 	}
2341       else if (!parms)
2342 	{
2343 	  /* Even if there are no parameters, we might be able to deduce from
2344 	     default template arguments.  Pass TF_NONE so that we don't
2345 	     generate redundant diagnostics.  */
2346 	  type = do_auto_deduction (type, parms, anode, tf_none,
2347 				    adc_variable_type);
2348 	  if (type == error_mark_node)
2349 	    {
2350 	      if (complain & tf_error)
2351 		error_at (loc, "cannot deduce template arguments "
2352 			  "for %qT from %<()%>", anode);
2353 	      return error_mark_node;
2354 	    }
2355 	}
2356       else
2357 	type = do_auto_deduction (type, parms, anode, complain,
2358 				  adc_variable_type);
2359     }
2360 
2361   if (processing_template_decl)
2362     {
2363       tree t;
2364 
2365       /* Diagnose this even in a template.  We could also try harder
2366 	 to give all the usual errors when the type and args are
2367 	 non-dependent...  */
2368       if (TYPE_REF_P (type) && !parms)
2369 	{
2370 	  if (complain & tf_error)
2371 	    error_at (loc, "invalid value-initialization of reference type");
2372 	  return error_mark_node;
2373 	}
2374 
2375       t = build_min (CAST_EXPR, type, parms);
2376       /* We don't know if it will or will not have side effects.  */
2377       TREE_SIDE_EFFECTS (t) = 1;
2378       return t;
2379     }
2380 
2381   if (! MAYBE_CLASS_TYPE_P (type))
2382     {
2383       if (parms == NULL_TREE)
2384 	{
2385 	  if (VOID_TYPE_P (type))
2386 	    return void_node;
2387 	  return build_value_init (cv_unqualified (type), complain);
2388 	}
2389 
2390       /* This must build a C cast.  */
2391       parms = build_x_compound_expr_from_list (parms, ELK_FUNC_CAST, complain);
2392       return cp_build_c_cast (loc, type, parms, complain);
2393     }
2394 
2395   /* Prepare to evaluate as a call to a constructor.  If this expression
2396      is actually used, for example,
2397 
2398      return X (arg1, arg2, ...);
2399 
2400      then the slot being initialized will be filled in.  */
2401 
2402   if (!complete_type_or_maybe_complain (type, NULL_TREE, complain))
2403     return error_mark_node;
2404   if (abstract_virtuals_error_sfinae (ACU_CAST, type, complain))
2405     return error_mark_node;
2406 
2407   /* [expr.type.conv]
2408 
2409      If the expression list is a single-expression, the type
2410      conversion is equivalent (in definedness, and if defined in
2411      meaning) to the corresponding cast expression.  */
2412   if (parms && TREE_CHAIN (parms) == NULL_TREE)
2413     return cp_build_c_cast (loc, type, TREE_VALUE (parms), complain);
2414 
2415   /* [expr.type.conv]
2416 
2417      The expression T(), where T is a simple-type-specifier for a
2418      non-array complete object type or the (possibly cv-qualified)
2419      void type, creates an rvalue of the specified type, which is
2420      value-initialized.  */
2421 
2422   if (parms == NULL_TREE)
2423     {
2424       exp = build_value_init (type, complain);
2425       exp = get_target_expr_sfinae (exp, complain);
2426       return exp;
2427     }
2428 
2429   /* Call the constructor.  */
2430   releasing_vec parmvec;
2431   for (; parms != NULL_TREE; parms = TREE_CHAIN (parms))
2432     vec_safe_push (parmvec, TREE_VALUE (parms));
2433   exp = build_special_member_call (NULL_TREE, complete_ctor_identifier,
2434 				   &parmvec, type, LOOKUP_NORMAL, complain);
2435 
2436   if (exp == error_mark_node)
2437     return error_mark_node;
2438 
2439   return build_cplus_new (type, exp, complain);
2440 }
2441 
2442 tree
build_functional_cast(location_t loc,tree exp,tree parms,tsubst_flags_t complain)2443 build_functional_cast (location_t loc, tree exp, tree parms,
2444 		       tsubst_flags_t complain)
2445 {
2446   tree result = build_functional_cast_1 (loc, exp, parms, complain);
2447   protected_set_expr_location (result, loc);
2448   return result;
2449 }
2450 
2451 
2452 /* Add new exception specifier SPEC, to the LIST we currently have.
2453    If it's already in LIST then do nothing.
2454    Moan if it's bad and we're allowed to. COMPLAIN < 0 means we
2455    know what we're doing.  */
2456 
2457 tree
add_exception_specifier(tree list,tree spec,tsubst_flags_t complain)2458 add_exception_specifier (tree list, tree spec, tsubst_flags_t complain)
2459 {
2460   bool ok;
2461   tree core = spec;
2462   bool is_ptr;
2463   diagnostic_t diag_type = DK_UNSPECIFIED; /* none */
2464 
2465   if (spec == error_mark_node)
2466     return list;
2467 
2468   gcc_assert (spec && (!list || TREE_VALUE (list)));
2469 
2470   /* [except.spec] 1, type in an exception specifier shall not be
2471      incomplete, or pointer or ref to incomplete other than pointer
2472      to cv void.  */
2473   is_ptr = TYPE_PTR_P (core);
2474   if (is_ptr || TYPE_REF_P (core))
2475     core = TREE_TYPE (core);
2476   if (complain < 0)
2477     ok = true;
2478   else if (VOID_TYPE_P (core))
2479     ok = is_ptr;
2480   else if (TREE_CODE (core) == TEMPLATE_TYPE_PARM)
2481     ok = true;
2482   else if (processing_template_decl)
2483     ok = true;
2484   else if (!verify_type_context (input_location, TCTX_EXCEPTIONS, core,
2485 				 !(complain & tf_error)))
2486     return error_mark_node;
2487   else
2488     {
2489       ok = true;
2490       /* 15.4/1 says that types in an exception specifier must be complete,
2491 	 but it seems more reasonable to only require this on definitions
2492 	 and calls.  So just give a pedwarn at this point; we will give an
2493 	 error later if we hit one of those two cases.  */
2494       if (!COMPLETE_TYPE_P (complete_type (core)))
2495 	diag_type = DK_PEDWARN; /* pedwarn */
2496     }
2497 
2498   if (ok)
2499     {
2500       tree probe;
2501 
2502       for (probe = list; probe; probe = TREE_CHAIN (probe))
2503 	if (same_type_p (TREE_VALUE (probe), spec))
2504 	  break;
2505       if (!probe)
2506 	list = tree_cons (NULL_TREE, spec, list);
2507     }
2508   else
2509     diag_type = DK_ERROR; /* error */
2510 
2511   if (diag_type != DK_UNSPECIFIED
2512       && (complain & tf_warning_or_error))
2513     cxx_incomplete_type_diagnostic (NULL_TREE, core, diag_type);
2514 
2515   return list;
2516 }
2517 
2518 /* Like nothrow_spec_p, but don't abort on deferred noexcept.  */
2519 
2520 static bool
nothrow_spec_p_uninst(const_tree spec)2521 nothrow_spec_p_uninst (const_tree spec)
2522 {
2523   if (DEFERRED_NOEXCEPT_SPEC_P (spec))
2524     return false;
2525   return nothrow_spec_p (spec);
2526 }
2527 
2528 /* Combine the two exceptions specifier lists LIST and ADD, and return
2529    their union.  */
2530 
2531 tree
merge_exception_specifiers(tree list,tree add)2532 merge_exception_specifiers (tree list, tree add)
2533 {
2534   tree noex, orig_list;
2535 
2536   if (list == error_mark_node || add == error_mark_node)
2537     return error_mark_node;
2538 
2539   /* No exception-specifier or noexcept(false) are less strict than
2540      anything else.  Prefer the newer variant (LIST).  */
2541   if (!list || list == noexcept_false_spec)
2542     return list;
2543   else if (!add || add == noexcept_false_spec)
2544     return add;
2545 
2546   /* noexcept(true) and throw() are stricter than anything else.
2547      As above, prefer the more recent one (LIST).  */
2548   if (nothrow_spec_p_uninst (add))
2549     return list;
2550 
2551   /* Two implicit noexcept specs (e.g. on a destructor) are equivalent.  */
2552   if (UNEVALUATED_NOEXCEPT_SPEC_P (add)
2553       && UNEVALUATED_NOEXCEPT_SPEC_P (list))
2554     return list;
2555   /* We should have instantiated other deferred noexcept specs by now.  */
2556   gcc_assert (!DEFERRED_NOEXCEPT_SPEC_P (add));
2557 
2558   if (nothrow_spec_p_uninst (list))
2559     return add;
2560   noex = TREE_PURPOSE (list);
2561   gcc_checking_assert (!TREE_PURPOSE (add)
2562 		       || errorcount || !flag_exceptions
2563 		       || cp_tree_equal (noex, TREE_PURPOSE (add)));
2564 
2565   /* Combine the dynamic-exception-specifiers, if any.  */
2566   orig_list = list;
2567   for (; add && TREE_VALUE (add); add = TREE_CHAIN (add))
2568     {
2569       tree spec = TREE_VALUE (add);
2570       tree probe;
2571 
2572       for (probe = orig_list; probe && TREE_VALUE (probe);
2573 	   probe = TREE_CHAIN (probe))
2574 	if (same_type_p (TREE_VALUE (probe), spec))
2575 	  break;
2576       if (!probe)
2577 	{
2578 	  spec = build_tree_list (NULL_TREE, spec);
2579 	  TREE_CHAIN (spec) = list;
2580 	  list = spec;
2581 	}
2582     }
2583 
2584   /* Keep the noexcept-specifier at the beginning of the list.  */
2585   if (noex != TREE_PURPOSE (list))
2586     list = tree_cons (noex, TREE_VALUE (list), TREE_CHAIN (list));
2587 
2588   return list;
2589 }
2590 
2591 /* Subroutine of build_call.  Ensure that each of the types in the
2592    exception specification is complete.  Technically, 15.4/1 says that
2593    they need to be complete when we see a declaration of the function,
2594    but we should be able to get away with only requiring this when the
2595    function is defined or called.  See also add_exception_specifier.  */
2596 
2597 void
require_complete_eh_spec_types(tree fntype,tree decl)2598 require_complete_eh_spec_types (tree fntype, tree decl)
2599 {
2600   tree raises;
2601   /* Don't complain about calls to op new.  */
2602   if (decl && DECL_ARTIFICIAL (decl))
2603     return;
2604   for (raises = TYPE_RAISES_EXCEPTIONS (fntype); raises;
2605        raises = TREE_CHAIN (raises))
2606     {
2607       tree type = TREE_VALUE (raises);
2608       if (type && !COMPLETE_TYPE_P (type))
2609 	{
2610 	  if (decl)
2611 	    error
2612 	      ("call to function %qD which throws incomplete type %q#T",
2613 	       decl, type);
2614 	  else
2615 	    error ("call to function which throws incomplete type %q#T",
2616 		   decl);
2617 	}
2618     }
2619 }
2620 
2621 
2622 #include "gt-cp-typeck2.h"
2623