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