xref: /dragonfly/contrib/gcc-8.0/gcc/cp/typeck.c (revision 50b09fda)
1 /* Build expressions with type checking for C++ compiler.
2    Copyright (C) 1987-2018 Free Software Foundation, Inc.
3    Hacked by Michael Tiemann (tiemann@cygnus.com)
4 
5 This file is part of GCC.
6 
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
11 
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3.  If not see
19 <http://www.gnu.org/licenses/>.  */
20 
21 
22 /* This file is part of the C++ front end.
23    It contains routines to build C++ expressions given their operands,
24    including computing the types of the result, C and C++ specific error
25    checks, and some optimization.  */
26 
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "target.h"
31 #include "cp-tree.h"
32 #include "stor-layout.h"
33 #include "varasm.h"
34 #include "intl.h"
35 #include "convert.h"
36 #include "c-family/c-objc.h"
37 #include "c-family/c-ubsan.h"
38 #include "params.h"
39 #include "gcc-rich-location.h"
40 #include "stringpool.h"
41 #include "attribs.h"
42 #include "asan.h"
43 
44 static tree cp_build_addr_expr_strict (tree, tsubst_flags_t);
45 static tree cp_build_function_call (tree, tree, tsubst_flags_t);
46 static tree pfn_from_ptrmemfunc (tree);
47 static tree delta_from_ptrmemfunc (tree);
48 static tree convert_for_assignment (tree, tree, impl_conv_rhs, tree, int,
49 				    tsubst_flags_t, int);
50 static tree cp_pointer_int_sum (location_t, enum tree_code, tree, tree,
51 				tsubst_flags_t);
52 static tree rationalize_conditional_expr (enum tree_code, tree,
53 					  tsubst_flags_t);
54 static int comp_ptr_ttypes_real (tree, tree, int);
55 static bool comp_except_types (tree, tree, bool);
56 static bool comp_array_types (const_tree, const_tree, bool);
57 static tree pointer_diff (location_t, tree, tree, tree, tsubst_flags_t, tree *);
58 static tree get_delta_difference (tree, tree, bool, bool, tsubst_flags_t);
59 static void casts_away_constness_r (tree *, tree *, tsubst_flags_t);
60 static bool casts_away_constness (tree, tree, tsubst_flags_t);
61 static bool maybe_warn_about_returning_address_of_local (tree);
62 static tree lookup_destructor (tree, tree, tree, tsubst_flags_t);
63 static void error_args_num (location_t, tree, bool);
64 static int convert_arguments (tree, vec<tree, va_gc> **, tree, int,
65                               tsubst_flags_t);
66 
67 /* Do `exp = require_complete_type (exp);' to make sure exp
68    does not have an incomplete type.  (That includes void types.)
69    Returns error_mark_node if the VALUE does not have
70    complete type when this function returns.  */
71 
72 tree
73 require_complete_type_sfinae (tree value, tsubst_flags_t complain)
74 {
75   tree type;
76 
77   if (processing_template_decl || value == error_mark_node)
78     return value;
79 
80   if (TREE_CODE (value) == OVERLOAD)
81     type = unknown_type_node;
82   else
83     type = TREE_TYPE (value);
84 
85   if (type == error_mark_node)
86     return error_mark_node;
87 
88   /* First, detect a valid value with a complete type.  */
89   if (COMPLETE_TYPE_P (type))
90     return value;
91 
92   if (complete_type_or_maybe_complain (type, value, complain))
93     return value;
94   else
95     return error_mark_node;
96 }
97 
98 tree
99 require_complete_type (tree value)
100 {
101   return require_complete_type_sfinae (value, tf_warning_or_error);
102 }
103 
104 /* Try to complete TYPE, if it is incomplete.  For example, if TYPE is
105    a template instantiation, do the instantiation.  Returns TYPE,
106    whether or not it could be completed, unless something goes
107    horribly wrong, in which case the error_mark_node is returned.  */
108 
109 tree
110 complete_type (tree type)
111 {
112   if (type == NULL_TREE)
113     /* Rather than crash, we return something sure to cause an error
114        at some point.  */
115     return error_mark_node;
116 
117   if (type == error_mark_node || COMPLETE_TYPE_P (type))
118     ;
119   else if (TREE_CODE (type) == ARRAY_TYPE)
120     {
121       tree t = complete_type (TREE_TYPE (type));
122       unsigned int needs_constructing, has_nontrivial_dtor;
123       if (COMPLETE_TYPE_P (t) && !dependent_type_p (type))
124 	layout_type (type);
125       needs_constructing
126 	= TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t));
127       has_nontrivial_dtor
128 	= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t));
129       for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
130 	{
131 	  TYPE_NEEDS_CONSTRUCTING (t) = needs_constructing;
132 	  TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = has_nontrivial_dtor;
133 	}
134     }
135   else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INSTANTIATION (type))
136     instantiate_class_template (TYPE_MAIN_VARIANT (type));
137 
138   return type;
139 }
140 
141 /* Like complete_type, but issue an error if the TYPE cannot be completed.
142    VALUE is used for informative diagnostics.
143    Returns NULL_TREE if the type cannot be made complete.  */
144 
145 tree
146 complete_type_or_maybe_complain (tree type, tree value, tsubst_flags_t complain)
147 {
148   type = complete_type (type);
149   if (type == error_mark_node)
150     /* We already issued an error.  */
151     return NULL_TREE;
152   else if (!COMPLETE_TYPE_P (type))
153     {
154       if (complain & tf_error)
155 	cxx_incomplete_type_diagnostic (value, type, DK_ERROR);
156       return NULL_TREE;
157     }
158   else
159     return type;
160 }
161 
162 tree
163 complete_type_or_else (tree type, tree value)
164 {
165   return complete_type_or_maybe_complain (type, value, tf_warning_or_error);
166 }
167 
168 
169 /* Return the common type of two parameter lists.
170    We assume that comptypes has already been done and returned 1;
171    if that isn't so, this may crash.
172 
173    As an optimization, free the space we allocate if the parameter
174    lists are already common.  */
175 
176 static tree
177 commonparms (tree p1, tree p2)
178 {
179   tree oldargs = p1, newargs, n;
180   int i, len;
181   int any_change = 0;
182 
183   len = list_length (p1);
184   newargs = tree_last (p1);
185 
186   if (newargs == void_list_node)
187     i = 1;
188   else
189     {
190       i = 0;
191       newargs = 0;
192     }
193 
194   for (; i < len; i++)
195     newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
196 
197   n = newargs;
198 
199   for (i = 0; p1;
200        p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++)
201     {
202       if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2))
203 	{
204 	  TREE_PURPOSE (n) = TREE_PURPOSE (p1);
205 	  any_change = 1;
206 	}
207       else if (! TREE_PURPOSE (p1))
208 	{
209 	  if (TREE_PURPOSE (p2))
210 	    {
211 	      TREE_PURPOSE (n) = TREE_PURPOSE (p2);
212 	      any_change = 1;
213 	    }
214 	}
215       else
216 	{
217 	  if (simple_cst_equal (TREE_PURPOSE (p1), TREE_PURPOSE (p2)) != 1)
218 	    any_change = 1;
219 	  TREE_PURPOSE (n) = TREE_PURPOSE (p2);
220 	}
221       if (TREE_VALUE (p1) != TREE_VALUE (p2))
222 	{
223 	  any_change = 1;
224 	  TREE_VALUE (n) = merge_types (TREE_VALUE (p1), TREE_VALUE (p2));
225 	}
226       else
227 	TREE_VALUE (n) = TREE_VALUE (p1);
228     }
229   if (! any_change)
230     return oldargs;
231 
232   return newargs;
233 }
234 
235 /* Given a type, perhaps copied for a typedef,
236    find the "original" version of it.  */
237 static tree
238 original_type (tree t)
239 {
240   int quals = cp_type_quals (t);
241   while (t != error_mark_node
242 	 && TYPE_NAME (t) != NULL_TREE)
243     {
244       tree x = TYPE_NAME (t);
245       if (TREE_CODE (x) != TYPE_DECL)
246 	break;
247       x = DECL_ORIGINAL_TYPE (x);
248       if (x == NULL_TREE)
249 	break;
250       t = x;
251     }
252   return cp_build_qualified_type (t, quals);
253 }
254 
255 /* Return the common type for two arithmetic types T1 and T2 under the
256    usual arithmetic conversions.  The default conversions have already
257    been applied, and enumerated types converted to their compatible
258    integer types.  */
259 
260 static tree
261 cp_common_type (tree t1, tree t2)
262 {
263   enum tree_code code1 = TREE_CODE (t1);
264   enum tree_code code2 = TREE_CODE (t2);
265   tree attributes;
266   int i;
267 
268 
269   /* In what follows, we slightly generalize the rules given in [expr] so
270      as to deal with `long long' and `complex'.  First, merge the
271      attributes.  */
272   attributes = (*targetm.merge_type_attributes) (t1, t2);
273 
274   if (SCOPED_ENUM_P (t1) || SCOPED_ENUM_P (t2))
275     {
276       if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
277 	return build_type_attribute_variant (t1, attributes);
278       else
279 	return NULL_TREE;
280     }
281 
282   /* FIXME: Attributes.  */
283   gcc_assert (ARITHMETIC_TYPE_P (t1)
284 	      || VECTOR_TYPE_P (t1)
285 	      || UNSCOPED_ENUM_P (t1));
286   gcc_assert (ARITHMETIC_TYPE_P (t2)
287 	      || VECTOR_TYPE_P (t2)
288 	      || UNSCOPED_ENUM_P (t2));
289 
290   /* If one type is complex, form the common type of the non-complex
291      components, then make that complex.  Use T1 or T2 if it is the
292      required type.  */
293   if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
294     {
295       tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
296       tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
297       tree subtype
298 	= type_after_usual_arithmetic_conversions (subtype1, subtype2);
299 
300       if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
301 	return build_type_attribute_variant (t1, attributes);
302       else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
303 	return build_type_attribute_variant (t2, attributes);
304       else
305 	return build_type_attribute_variant (build_complex_type (subtype),
306 					     attributes);
307     }
308 
309   if (code1 == VECTOR_TYPE)
310     {
311       /* When we get here we should have two vectors of the same size.
312 	 Just prefer the unsigned one if present.  */
313       if (TYPE_UNSIGNED (t1))
314 	return build_type_attribute_variant (t1, attributes);
315       else
316 	return build_type_attribute_variant (t2, attributes);
317     }
318 
319   /* If only one is real, use it as the result.  */
320   if (code1 == REAL_TYPE && code2 != REAL_TYPE)
321     return build_type_attribute_variant (t1, attributes);
322   if (code2 == REAL_TYPE && code1 != REAL_TYPE)
323     return build_type_attribute_variant (t2, attributes);
324 
325   /* Both real or both integers; use the one with greater precision.  */
326   if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
327     return build_type_attribute_variant (t1, attributes);
328   else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
329     return build_type_attribute_variant (t2, attributes);
330 
331   /* The types are the same; no need to do anything fancy.  */
332   if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
333     return build_type_attribute_variant (t1, attributes);
334 
335   if (code1 != REAL_TYPE)
336     {
337       /* If one is unsigned long long, then convert the other to unsigned
338 	 long long.  */
339       if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_unsigned_type_node)
340 	  || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_unsigned_type_node))
341 	return build_type_attribute_variant (long_long_unsigned_type_node,
342 					     attributes);
343       /* If one is a long long, and the other is an unsigned long, and
344 	 long long can represent all the values of an unsigned long, then
345 	 convert to a long long.  Otherwise, convert to an unsigned long
346 	 long.  Otherwise, if either operand is long long, convert the
347 	 other to long long.
348 
349 	 Since we're here, we know the TYPE_PRECISION is the same;
350 	 therefore converting to long long cannot represent all the values
351 	 of an unsigned long, so we choose unsigned long long in that
352 	 case.  */
353       if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_integer_type_node)
354 	  || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_integer_type_node))
355 	{
356 	  tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
357 		    ? long_long_unsigned_type_node
358 		    : long_long_integer_type_node);
359 	  return build_type_attribute_variant (t, attributes);
360 	}
361 
362       /* Go through the same procedure, but for longs.  */
363       if (same_type_p (TYPE_MAIN_VARIANT (t1), long_unsigned_type_node)
364 	  || same_type_p (TYPE_MAIN_VARIANT (t2), long_unsigned_type_node))
365 	return build_type_attribute_variant (long_unsigned_type_node,
366 					     attributes);
367       if (same_type_p (TYPE_MAIN_VARIANT (t1), long_integer_type_node)
368 	  || same_type_p (TYPE_MAIN_VARIANT (t2), long_integer_type_node))
369 	{
370 	  tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
371 		    ? long_unsigned_type_node : long_integer_type_node);
372 	  return build_type_attribute_variant (t, attributes);
373 	}
374 
375       /* For __intN types, either the type is __int128 (and is lower
376 	 priority than the types checked above, but higher than other
377 	 128-bit types) or it's known to not be the same size as other
378 	 types (enforced in toplev.c).  Prefer the unsigned type. */
379       for (i = 0; i < NUM_INT_N_ENTS; i ++)
380 	{
381 	  if (int_n_enabled_p [i]
382 	      && (same_type_p (TYPE_MAIN_VARIANT (t1), int_n_trees[i].signed_type)
383 		  || same_type_p (TYPE_MAIN_VARIANT (t2), int_n_trees[i].signed_type)
384 		  || same_type_p (TYPE_MAIN_VARIANT (t1), int_n_trees[i].unsigned_type)
385 		  || same_type_p (TYPE_MAIN_VARIANT (t2), int_n_trees[i].unsigned_type)))
386 	    {
387 	      tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
388 			? int_n_trees[i].unsigned_type
389 			: int_n_trees[i].signed_type);
390 	      return build_type_attribute_variant (t, attributes);
391 	    }
392 	}
393 
394       /* Otherwise prefer the unsigned one.  */
395       if (TYPE_UNSIGNED (t1))
396 	return build_type_attribute_variant (t1, attributes);
397       else
398 	return build_type_attribute_variant (t2, attributes);
399     }
400   else
401     {
402       if (same_type_p (TYPE_MAIN_VARIANT (t1), long_double_type_node)
403 	  || same_type_p (TYPE_MAIN_VARIANT (t2), long_double_type_node))
404 	return build_type_attribute_variant (long_double_type_node,
405 					     attributes);
406       if (same_type_p (TYPE_MAIN_VARIANT (t1), double_type_node)
407 	  || same_type_p (TYPE_MAIN_VARIANT (t2), double_type_node))
408 	return build_type_attribute_variant (double_type_node,
409 					     attributes);
410       if (same_type_p (TYPE_MAIN_VARIANT (t1), float_type_node)
411 	  || same_type_p (TYPE_MAIN_VARIANT (t2), float_type_node))
412 	return build_type_attribute_variant (float_type_node,
413 					     attributes);
414 
415       /* Two floating-point types whose TYPE_MAIN_VARIANTs are none of
416 	 the standard C++ floating-point types.  Logic earlier in this
417 	 function has already eliminated the possibility that
418 	 TYPE_PRECISION (t2) != TYPE_PRECISION (t1), so there's no
419 	 compelling reason to choose one or the other.  */
420       return build_type_attribute_variant (t1, attributes);
421     }
422 }
423 
424 /* T1 and T2 are arithmetic or enumeration types.  Return the type
425    that will result from the "usual arithmetic conversions" on T1 and
426    T2 as described in [expr].  */
427 
428 tree
429 type_after_usual_arithmetic_conversions (tree t1, tree t2)
430 {
431   gcc_assert (ARITHMETIC_TYPE_P (t1)
432 	      || VECTOR_TYPE_P (t1)
433 	      || UNSCOPED_ENUM_P (t1));
434   gcc_assert (ARITHMETIC_TYPE_P (t2)
435 	      || VECTOR_TYPE_P (t2)
436 	      || UNSCOPED_ENUM_P (t2));
437 
438   /* Perform the integral promotions.  We do not promote real types here.  */
439   if (INTEGRAL_OR_ENUMERATION_TYPE_P (t1)
440       && INTEGRAL_OR_ENUMERATION_TYPE_P (t2))
441     {
442       t1 = type_promotes_to (t1);
443       t2 = type_promotes_to (t2);
444     }
445 
446   return cp_common_type (t1, t2);
447 }
448 
449 static void
450 composite_pointer_error (diagnostic_t kind, tree t1, tree t2,
451 			 composite_pointer_operation operation)
452 {
453   switch (operation)
454     {
455     case CPO_COMPARISON:
456       emit_diagnostic (kind, input_location, 0,
457 		       "comparison between "
458 		       "distinct pointer types %qT and %qT lacks a cast",
459 		       t1, t2);
460       break;
461     case CPO_CONVERSION:
462       emit_diagnostic (kind, input_location, 0,
463 		       "conversion between "
464 		       "distinct pointer types %qT and %qT lacks a cast",
465 		       t1, t2);
466       break;
467     case CPO_CONDITIONAL_EXPR:
468       emit_diagnostic (kind, input_location, 0,
469 		       "conditional expression between "
470 		       "distinct pointer types %qT and %qT lacks a cast",
471 		       t1, t2);
472       break;
473     default:
474       gcc_unreachable ();
475     }
476 }
477 
478 /* Subroutine of composite_pointer_type to implement the recursive
479    case.  See that function for documentation of the parameters.  */
480 
481 static tree
482 composite_pointer_type_r (tree t1, tree t2,
483 			  composite_pointer_operation operation,
484 			  tsubst_flags_t complain)
485 {
486   tree pointee1;
487   tree pointee2;
488   tree result_type;
489   tree attributes;
490 
491   /* Determine the types pointed to by T1 and T2.  */
492   if (TYPE_PTR_P (t1))
493     {
494       pointee1 = TREE_TYPE (t1);
495       pointee2 = TREE_TYPE (t2);
496     }
497   else
498     {
499       pointee1 = TYPE_PTRMEM_POINTED_TO_TYPE (t1);
500       pointee2 = TYPE_PTRMEM_POINTED_TO_TYPE (t2);
501     }
502 
503   /* [expr.rel]
504 
505      Otherwise, the composite pointer type is a pointer type
506      similar (_conv.qual_) to the type of one of the operands,
507      with a cv-qualification signature (_conv.qual_) that is the
508      union of the cv-qualification signatures of the operand
509      types.  */
510   if (same_type_ignoring_top_level_qualifiers_p (pointee1, pointee2))
511     result_type = pointee1;
512   else if ((TYPE_PTR_P (pointee1) && TYPE_PTR_P (pointee2))
513 	   || (TYPE_PTRMEM_P (pointee1) && TYPE_PTRMEM_P (pointee2)))
514     {
515       result_type = composite_pointer_type_r (pointee1, pointee2, operation,
516 					      complain);
517       if (result_type == error_mark_node)
518 	return error_mark_node;
519     }
520   else
521     {
522       if (complain & tf_error)
523 	composite_pointer_error (DK_PERMERROR, t1, t2, operation);
524       else
525 	return error_mark_node;
526       result_type = void_type_node;
527     }
528   result_type = cp_build_qualified_type (result_type,
529 					 (cp_type_quals (pointee1)
530 					  | cp_type_quals (pointee2)));
531   /* If the original types were pointers to members, so is the
532      result.  */
533   if (TYPE_PTRMEM_P (t1))
534     {
535       if (!same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
536 			TYPE_PTRMEM_CLASS_TYPE (t2)))
537 	{
538 	  if (complain & tf_error)
539 	    composite_pointer_error (DK_PERMERROR, t1, t2, operation);
540 	  else
541 	    return error_mark_node;
542 	}
543       result_type = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
544 				       result_type);
545     }
546   else
547     result_type = build_pointer_type (result_type);
548 
549   /* Merge the attributes.  */
550   attributes = (*targetm.merge_type_attributes) (t1, t2);
551   return build_type_attribute_variant (result_type, attributes);
552 }
553 
554 /* Return the composite pointer type (see [expr.rel]) for T1 and T2.
555    ARG1 and ARG2 are the values with those types.  The OPERATION is to
556    describe the operation between the pointer types,
557    in case an error occurs.
558 
559    This routine also implements the computation of a common type for
560    pointers-to-members as per [expr.eq].  */
561 
562 tree
563 composite_pointer_type (tree t1, tree t2, tree arg1, tree arg2,
564 			composite_pointer_operation operation,
565 			tsubst_flags_t complain)
566 {
567   tree class1;
568   tree class2;
569 
570   /* [expr.rel]
571 
572      If one operand is a null pointer constant, the composite pointer
573      type is the type of the other operand.  */
574   if (null_ptr_cst_p (arg1))
575     return t2;
576   if (null_ptr_cst_p (arg2))
577     return t1;
578 
579   /* We have:
580 
581        [expr.rel]
582 
583        If one of the operands has type "pointer to cv1 void*", then
584        the other has type "pointer to cv2T", and the composite pointer
585        type is "pointer to cv12 void", where cv12 is the union of cv1
586        and cv2.
587 
588     If either type is a pointer to void, make sure it is T1.  */
589   if (TYPE_PTR_P (t2) && VOID_TYPE_P (TREE_TYPE (t2)))
590     std::swap (t1, t2);
591 
592   /* Now, if T1 is a pointer to void, merge the qualifiers.  */
593   if (TYPE_PTR_P (t1) && VOID_TYPE_P (TREE_TYPE (t1)))
594     {
595       tree attributes;
596       tree result_type;
597 
598       if (TYPE_PTRFN_P (t2))
599 	{
600 	  if (complain & tf_error)
601 	    {
602 	      switch (operation)
603 		{
604 		case CPO_COMPARISON:
605 		  pedwarn (input_location, OPT_Wpedantic,
606 			   "ISO C++ forbids comparison between pointer "
607 			   "of type %<void *%> and pointer-to-function");
608 		  break;
609 		case CPO_CONVERSION:
610 		  pedwarn (input_location, OPT_Wpedantic,
611 			   "ISO C++ forbids conversion between pointer "
612 			   "of type %<void *%> and pointer-to-function");
613 		  break;
614 		case CPO_CONDITIONAL_EXPR:
615 		  pedwarn (input_location, OPT_Wpedantic,
616 			   "ISO C++ forbids conditional expression between "
617 			   "pointer of type %<void *%> and "
618 			   "pointer-to-function");
619 		  break;
620 		default:
621 		  gcc_unreachable ();
622 		}
623 	    }
624 	  else
625 	    return error_mark_node;
626         }
627       result_type
628 	= cp_build_qualified_type (void_type_node,
629 				   (cp_type_quals (TREE_TYPE (t1))
630 				    | cp_type_quals (TREE_TYPE (t2))));
631       result_type = build_pointer_type (result_type);
632       /* Merge the attributes.  */
633       attributes = (*targetm.merge_type_attributes) (t1, t2);
634       return build_type_attribute_variant (result_type, attributes);
635     }
636 
637   if (c_dialect_objc () && TYPE_PTR_P (t1)
638       && TYPE_PTR_P (t2))
639     {
640       if (objc_have_common_type (t1, t2, -3, NULL_TREE))
641 	return objc_common_type (t1, t2);
642     }
643 
644   /* if T1 or T2 is "pointer to noexcept function" and the other type is
645      "pointer to function", where the function types are otherwise the same,
646      "pointer to function" */
647   if (fnptr_conv_p (t1, t2))
648     return t1;
649   if (fnptr_conv_p (t2, t1))
650     return t2;
651 
652   /* [expr.eq] permits the application of a pointer conversion to
653      bring the pointers to a common type.  */
654   if (TYPE_PTR_P (t1) && TYPE_PTR_P (t2)
655       && CLASS_TYPE_P (TREE_TYPE (t1))
656       && CLASS_TYPE_P (TREE_TYPE (t2))
657       && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (t1),
658 						     TREE_TYPE (t2)))
659     {
660       class1 = TREE_TYPE (t1);
661       class2 = TREE_TYPE (t2);
662 
663       if (DERIVED_FROM_P (class1, class2))
664 	t2 = (build_pointer_type
665 	      (cp_build_qualified_type (class1, cp_type_quals (class2))));
666       else if (DERIVED_FROM_P (class2, class1))
667 	t1 = (build_pointer_type
668 	      (cp_build_qualified_type (class2, cp_type_quals (class1))));
669       else
670         {
671           if (complain & tf_error)
672 	    composite_pointer_error (DK_ERROR, t1, t2, operation);
673           return error_mark_node;
674         }
675     }
676   /* [expr.eq] permits the application of a pointer-to-member
677      conversion to change the class type of one of the types.  */
678   else if (TYPE_PTRMEM_P (t1)
679            && !same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
680 			    TYPE_PTRMEM_CLASS_TYPE (t2)))
681     {
682       class1 = TYPE_PTRMEM_CLASS_TYPE (t1);
683       class2 = TYPE_PTRMEM_CLASS_TYPE (t2);
684 
685       if (DERIVED_FROM_P (class1, class2))
686 	t1 = build_ptrmem_type (class2, TYPE_PTRMEM_POINTED_TO_TYPE (t1));
687       else if (DERIVED_FROM_P (class2, class1))
688 	t2 = build_ptrmem_type (class1, TYPE_PTRMEM_POINTED_TO_TYPE (t2));
689       else
690         {
691           if (complain & tf_error)
692             switch (operation)
693               {
694               case CPO_COMPARISON:
695                 error ("comparison between distinct "
696                        "pointer-to-member types %qT and %qT lacks a cast",
697                        t1, t2);
698                 break;
699               case CPO_CONVERSION:
700                 error ("conversion between distinct "
701                        "pointer-to-member types %qT and %qT lacks a cast",
702                        t1, t2);
703                 break;
704               case CPO_CONDITIONAL_EXPR:
705                 error ("conditional expression between distinct "
706                        "pointer-to-member types %qT and %qT lacks a cast",
707                        t1, t2);
708                 break;
709               default:
710                 gcc_unreachable ();
711               }
712           return error_mark_node;
713         }
714     }
715 
716   return composite_pointer_type_r (t1, t2, operation, complain);
717 }
718 
719 /* Return the merged type of two types.
720    We assume that comptypes has already been done and returned 1;
721    if that isn't so, this may crash.
722 
723    This just combines attributes and default arguments; any other
724    differences would cause the two types to compare unalike.  */
725 
726 tree
727 merge_types (tree t1, tree t2)
728 {
729   enum tree_code code1;
730   enum tree_code code2;
731   tree attributes;
732 
733   /* Save time if the two types are the same.  */
734   if (t1 == t2)
735     return t1;
736   if (original_type (t1) == original_type (t2))
737     return t1;
738 
739   /* If one type is nonsense, use the other.  */
740   if (t1 == error_mark_node)
741     return t2;
742   if (t2 == error_mark_node)
743     return t1;
744 
745   /* Handle merging an auto redeclaration with a previous deduced
746      return type.  */
747   if (is_auto (t1))
748     return t2;
749 
750   /* Merge the attributes.  */
751   attributes = (*targetm.merge_type_attributes) (t1, t2);
752 
753   if (TYPE_PTRMEMFUNC_P (t1))
754     t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
755   if (TYPE_PTRMEMFUNC_P (t2))
756     t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
757 
758   code1 = TREE_CODE (t1);
759   code2 = TREE_CODE (t2);
760   if (code1 != code2)
761     {
762       gcc_assert (code1 == TYPENAME_TYPE || code2 == TYPENAME_TYPE);
763       if (code1 == TYPENAME_TYPE)
764 	{
765           t1 = resolve_typename_type (t1, /*only_current_p=*/true);
766 	  code1 = TREE_CODE (t1);
767 	}
768       else
769 	{
770           t2 = resolve_typename_type (t2, /*only_current_p=*/true);
771 	  code2 = TREE_CODE (t2);
772 	}
773     }
774 
775   switch (code1)
776     {
777     case POINTER_TYPE:
778     case REFERENCE_TYPE:
779       /* For two pointers, do this recursively on the target type.  */
780       {
781 	tree target = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
782 	int quals = cp_type_quals (t1);
783 
784 	if (code1 == POINTER_TYPE)
785 	  {
786 	    t1 = build_pointer_type (target);
787 	    if (TREE_CODE (target) == METHOD_TYPE)
788 	      t1 = build_ptrmemfunc_type (t1);
789 	  }
790 	else
791 	  t1 = cp_build_reference_type (target, TYPE_REF_IS_RVALUE (t1));
792 	t1 = build_type_attribute_variant (t1, attributes);
793 	t1 = cp_build_qualified_type (t1, quals);
794 
795 	return t1;
796       }
797 
798     case OFFSET_TYPE:
799       {
800 	int quals;
801 	tree pointee;
802 	quals = cp_type_quals (t1);
803 	pointee = merge_types (TYPE_PTRMEM_POINTED_TO_TYPE (t1),
804 			       TYPE_PTRMEM_POINTED_TO_TYPE (t2));
805 	t1 = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
806 				pointee);
807 	t1 = cp_build_qualified_type (t1, quals);
808 	break;
809       }
810 
811     case ARRAY_TYPE:
812       {
813 	tree elt = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
814 	/* Save space: see if the result is identical to one of the args.  */
815 	if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
816 	  return build_type_attribute_variant (t1, attributes);
817 	if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
818 	  return build_type_attribute_variant (t2, attributes);
819 	/* Merge the element types, and have a size if either arg has one.  */
820 	t1 = build_cplus_array_type
821 	  (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
822 	break;
823       }
824 
825     case FUNCTION_TYPE:
826       /* Function types: prefer the one that specified arg types.
827 	 If both do, merge the arg types.  Also merge the return types.  */
828       {
829 	tree valtype = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
830 	tree p1 = TYPE_ARG_TYPES (t1);
831 	tree p2 = TYPE_ARG_TYPES (t2);
832 	tree parms;
833 	tree rval, raises;
834 	bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t1);
835 
836 	/* Save space: see if the result is identical to one of the args.  */
837 	if (valtype == TREE_TYPE (t1) && ! p2)
838 	  return cp_build_type_attribute_variant (t1, attributes);
839 	if (valtype == TREE_TYPE (t2) && ! p1)
840 	  return cp_build_type_attribute_variant (t2, attributes);
841 
842 	/* Simple way if one arg fails to specify argument types.  */
843 	if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node)
844 	  parms = p2;
845 	else if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node)
846 	  parms = p1;
847 	else
848 	  parms = commonparms (p1, p2);
849 
850 	rval = build_function_type (valtype, parms);
851 	gcc_assert (type_memfn_quals (t1) == type_memfn_quals (t2));
852 	gcc_assert (type_memfn_rqual (t1) == type_memfn_rqual (t2));
853 	rval = apply_memfn_quals (rval,
854 				  type_memfn_quals (t1),
855 				  type_memfn_rqual (t1));
856 	raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
857 					     TYPE_RAISES_EXCEPTIONS (t2));
858 	t1 = build_exception_variant (rval, raises);
859 	if (late_return_type_p)
860 	  TYPE_HAS_LATE_RETURN_TYPE (t1) = 1;
861 	break;
862       }
863 
864     case METHOD_TYPE:
865       {
866 	/* Get this value the long way, since TYPE_METHOD_BASETYPE
867 	   is just the main variant of this.  */
868 	tree basetype = class_of_this_parm (t2);
869 	tree raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
870 						  TYPE_RAISES_EXCEPTIONS (t2));
871 	cp_ref_qualifier rqual = type_memfn_rqual (t1);
872 	tree t3;
873 	bool late_return_type_1_p = TYPE_HAS_LATE_RETURN_TYPE (t1);
874 	bool late_return_type_2_p = TYPE_HAS_LATE_RETURN_TYPE (t2);
875 
876 	/* If this was a member function type, get back to the
877 	   original type of type member function (i.e., without
878 	   the class instance variable up front.  */
879 	t1 = build_function_type (TREE_TYPE (t1),
880 				  TREE_CHAIN (TYPE_ARG_TYPES (t1)));
881 	t2 = build_function_type (TREE_TYPE (t2),
882 				  TREE_CHAIN (TYPE_ARG_TYPES (t2)));
883 	t3 = merge_types (t1, t2);
884 	t3 = build_method_type_directly (basetype, TREE_TYPE (t3),
885 					 TYPE_ARG_TYPES (t3));
886 	t1 = build_exception_variant (t3, raises);
887 	t1 = build_ref_qualified_type (t1, rqual);
888 	if (late_return_type_1_p)
889 	  TYPE_HAS_LATE_RETURN_TYPE (t1) = 1;
890 	if (late_return_type_2_p)
891 	  TYPE_HAS_LATE_RETURN_TYPE (t2) = 1;
892 	break;
893       }
894 
895     case TYPENAME_TYPE:
896       /* There is no need to merge attributes into a TYPENAME_TYPE.
897 	 When the type is instantiated it will have whatever
898 	 attributes result from the instantiation.  */
899       return t1;
900 
901     default:;
902       if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
903 	return t1;
904       else if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
905 	return t2;
906       break;
907     }
908 
909   return cp_build_type_attribute_variant (t1, attributes);
910 }
911 
912 /* Return the ARRAY_TYPE type without its domain.  */
913 
914 tree
915 strip_array_domain (tree type)
916 {
917   tree t2;
918   gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
919   if (TYPE_DOMAIN (type) == NULL_TREE)
920     return type;
921   t2 = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
922   return cp_build_type_attribute_variant (t2, TYPE_ATTRIBUTES (type));
923 }
924 
925 /* Wrapper around cp_common_type that is used by c-common.c and other
926    front end optimizations that remove promotions.
927 
928    Return the common type for two arithmetic types T1 and T2 under the
929    usual arithmetic conversions.  The default conversions have already
930    been applied, and enumerated types converted to their compatible
931    integer types.  */
932 
933 tree
934 common_type (tree t1, tree t2)
935 {
936   /* If one type is nonsense, use the other  */
937   if (t1 == error_mark_node)
938     return t2;
939   if (t2 == error_mark_node)
940     return t1;
941 
942   return cp_common_type (t1, t2);
943 }
944 
945 /* Return the common type of two pointer types T1 and T2.  This is the
946    type for the result of most arithmetic operations if the operands
947    have the given two types.
948 
949    We assume that comp_target_types has already been done and returned
950    nonzero; if that isn't so, this may crash.  */
951 
952 tree
953 common_pointer_type (tree t1, tree t2)
954 {
955   gcc_assert ((TYPE_PTR_P (t1) && TYPE_PTR_P (t2))
956               || (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
957               || (TYPE_PTRMEMFUNC_P (t1) && TYPE_PTRMEMFUNC_P (t2)));
958 
959   return composite_pointer_type (t1, t2, error_mark_node, error_mark_node,
960                                  CPO_CONVERSION, tf_warning_or_error);
961 }
962 
963 /* Compare two exception specifier types for exactness or subsetness, if
964    allowed. Returns false for mismatch, true for match (same, or
965    derived and !exact).
966 
967    [except.spec] "If a class X ... objects of class X or any class publicly
968    and unambiguously derived from X. Similarly, if a pointer type Y * ...
969    exceptions of type Y * or that are pointers to any type publicly and
970    unambiguously derived from Y. Otherwise a function only allows exceptions
971    that have the same type ..."
972    This does not mention cv qualifiers and is different to what throw
973    [except.throw] and catch [except.catch] will do. They will ignore the
974    top level cv qualifiers, and allow qualifiers in the pointer to class
975    example.
976 
977    We implement the letter of the standard.  */
978 
979 static bool
980 comp_except_types (tree a, tree b, bool exact)
981 {
982   if (same_type_p (a, b))
983     return true;
984   else if (!exact)
985     {
986       if (cp_type_quals (a) || cp_type_quals (b))
987 	return false;
988 
989       if (TYPE_PTR_P (a) && TYPE_PTR_P (b))
990 	{
991 	  a = TREE_TYPE (a);
992 	  b = TREE_TYPE (b);
993 	  if (cp_type_quals (a) || cp_type_quals (b))
994 	    return false;
995 	}
996 
997       if (TREE_CODE (a) != RECORD_TYPE
998 	  || TREE_CODE (b) != RECORD_TYPE)
999 	return false;
1000 
1001       if (publicly_uniquely_derived_p (a, b))
1002 	return true;
1003     }
1004   return false;
1005 }
1006 
1007 /* Return true if TYPE1 and TYPE2 are equivalent exception specifiers.
1008    If EXACT is ce_derived, T2 can be stricter than T1 (according to 15.4/5).
1009    If EXACT is ce_type, the C++17 type compatibility rules apply.
1010    If EXACT is ce_normal, the compatibility rules in 15.4/3 apply.
1011    If EXACT is ce_exact, the specs must be exactly the same. Exception lists
1012    are unordered, but we've already filtered out duplicates. Most lists will
1013    be in order, we should try to make use of that.  */
1014 
1015 bool
1016 comp_except_specs (const_tree t1, const_tree t2, int exact)
1017 {
1018   const_tree probe;
1019   const_tree base;
1020   int  length = 0;
1021 
1022   if (t1 == t2)
1023     return true;
1024 
1025   /* First handle noexcept.  */
1026   if (exact < ce_exact)
1027     {
1028       if (exact == ce_type
1029 	  && (canonical_eh_spec (CONST_CAST_TREE (t1))
1030 	      == canonical_eh_spec (CONST_CAST_TREE (t2))))
1031 	return true;
1032 
1033       /* noexcept(false) is compatible with no exception-specification,
1034 	 and less strict than any spec.  */
1035       if (t1 == noexcept_false_spec)
1036 	return t2 == NULL_TREE || exact == ce_derived;
1037       /* Even a derived noexcept(false) is compatible with no
1038 	 exception-specification.  */
1039       if (t2 == noexcept_false_spec)
1040 	return t1 == NULL_TREE;
1041 
1042       /* Otherwise, if we aren't looking for an exact match, noexcept is
1043 	 equivalent to throw().  */
1044       if (t1 == noexcept_true_spec)
1045 	t1 = empty_except_spec;
1046       if (t2 == noexcept_true_spec)
1047 	t2 = empty_except_spec;
1048     }
1049 
1050   /* If any noexcept is left, it is only comparable to itself;
1051      either we're looking for an exact match or we're redeclaring a
1052      template with dependent noexcept.  */
1053   if ((t1 && TREE_PURPOSE (t1))
1054       || (t2 && TREE_PURPOSE (t2)))
1055     return (t1 && t2
1056 	    && cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)));
1057 
1058   if (t1 == NULL_TREE)			   /* T1 is ...  */
1059     return t2 == NULL_TREE || exact == ce_derived;
1060   if (!TREE_VALUE (t1))			   /* t1 is EMPTY */
1061     return t2 != NULL_TREE && !TREE_VALUE (t2);
1062   if (t2 == NULL_TREE)			   /* T2 is ...  */
1063     return false;
1064   if (TREE_VALUE (t1) && !TREE_VALUE (t2)) /* T2 is EMPTY, T1 is not */
1065     return exact == ce_derived;
1066 
1067   /* Neither set is ... or EMPTY, make sure each part of T2 is in T1.
1068      Count how many we find, to determine exactness. For exact matching and
1069      ordered T1, T2, this is an O(n) operation, otherwise its worst case is
1070      O(nm).  */
1071   for (base = t1; t2 != NULL_TREE; t2 = TREE_CHAIN (t2))
1072     {
1073       for (probe = base; probe != NULL_TREE; probe = TREE_CHAIN (probe))
1074 	{
1075 	  tree a = TREE_VALUE (probe);
1076 	  tree b = TREE_VALUE (t2);
1077 
1078 	  if (comp_except_types (a, b, exact))
1079 	    {
1080 	      if (probe == base && exact > ce_derived)
1081 		base = TREE_CHAIN (probe);
1082 	      length++;
1083 	      break;
1084 	    }
1085 	}
1086       if (probe == NULL_TREE)
1087 	return false;
1088     }
1089   return exact == ce_derived || base == NULL_TREE || length == list_length (t1);
1090 }
1091 
1092 /* Compare the array types T1 and T2.  ALLOW_REDECLARATION is true if
1093    [] can match [size].  */
1094 
1095 static bool
1096 comp_array_types (const_tree t1, const_tree t2, bool allow_redeclaration)
1097 {
1098   tree d1;
1099   tree d2;
1100   tree max1, max2;
1101 
1102   if (t1 == t2)
1103     return true;
1104 
1105   /* The type of the array elements must be the same.  */
1106   if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1107     return false;
1108 
1109   d1 = TYPE_DOMAIN (t1);
1110   d2 = TYPE_DOMAIN (t2);
1111 
1112   if (d1 == d2)
1113     return true;
1114 
1115   /* If one of the arrays is dimensionless, and the other has a
1116      dimension, they are of different types.  However, it is valid to
1117      write:
1118 
1119        extern int a[];
1120        int a[3];
1121 
1122      by [basic.link]:
1123 
1124        declarations for an array object can specify
1125        array types that differ by the presence or absence of a major
1126        array bound (_dcl.array_).  */
1127   if (!d1 || !d2)
1128     return allow_redeclaration;
1129 
1130   /* Check that the dimensions are the same.  */
1131 
1132   if (!cp_tree_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2)))
1133     return false;
1134   max1 = TYPE_MAX_VALUE (d1);
1135   max2 = TYPE_MAX_VALUE (d2);
1136 
1137   if (!cp_tree_equal (max1, max2))
1138     return false;
1139 
1140   return true;
1141 }
1142 
1143 /* Compare the relative position of T1 and T2 into their respective
1144    template parameter list.
1145    T1 and T2 must be template parameter types.
1146    Return TRUE if T1 and T2 have the same position, FALSE otherwise.  */
1147 
1148 static bool
1149 comp_template_parms_position (tree t1, tree t2)
1150 {
1151   tree index1, index2;
1152   gcc_assert (t1 && t2
1153 	      && TREE_CODE (t1) == TREE_CODE (t2)
1154 	      && (TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM
1155 		  || TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM
1156 		  || TREE_CODE (t1) == TEMPLATE_TYPE_PARM));
1157 
1158   index1 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t1));
1159   index2 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t2));
1160 
1161   /* Then compare their relative position.  */
1162   if (TEMPLATE_PARM_IDX (index1) != TEMPLATE_PARM_IDX (index2)
1163       || TEMPLATE_PARM_LEVEL (index1) != TEMPLATE_PARM_LEVEL (index2)
1164       || (TEMPLATE_PARM_PARAMETER_PACK (index1)
1165 	  != TEMPLATE_PARM_PARAMETER_PACK (index2)))
1166     return false;
1167 
1168   /* In C++14 we can end up comparing 'auto' to a normal template
1169      parameter.  Don't confuse them.  */
1170   if (cxx_dialect >= cxx14 && (is_auto (t1) || is_auto (t2)))
1171     return TYPE_IDENTIFIER (t1) == TYPE_IDENTIFIER (t2);
1172 
1173   return true;
1174 }
1175 
1176 /* Heuristic check if two parameter types can be considered ABI-equivalent.  */
1177 
1178 static bool
1179 cxx_safe_arg_type_equiv_p (tree t1, tree t2)
1180 {
1181   t1 = TYPE_MAIN_VARIANT (t1);
1182   t2 = TYPE_MAIN_VARIANT (t2);
1183 
1184   if (TREE_CODE (t1) == POINTER_TYPE
1185       && TREE_CODE (t2) == POINTER_TYPE)
1186     return true;
1187 
1188   /* The signedness of the parameter matters only when an integral
1189      type smaller than int is promoted to int, otherwise only the
1190      precision of the parameter matters.
1191      This check should make sure that the callee does not see
1192      undefined values in argument registers.  */
1193   if (INTEGRAL_TYPE_P (t1)
1194       && INTEGRAL_TYPE_P (t2)
1195       && TYPE_PRECISION (t1) == TYPE_PRECISION (t2)
1196       && (TYPE_UNSIGNED (t1) == TYPE_UNSIGNED (t2)
1197 	  || !targetm.calls.promote_prototypes (NULL_TREE)
1198 	  || TYPE_PRECISION (t1) >= TYPE_PRECISION (integer_type_node)))
1199     return true;
1200 
1201   return same_type_p (t1, t2);
1202 }
1203 
1204 /* Check if a type cast between two function types can be considered safe.  */
1205 
1206 static bool
1207 cxx_safe_function_type_cast_p (tree t1, tree t2)
1208 {
1209   if (TREE_TYPE (t1) == void_type_node &&
1210       TYPE_ARG_TYPES (t1) == void_list_node)
1211     return true;
1212 
1213   if (TREE_TYPE (t2) == void_type_node &&
1214       TYPE_ARG_TYPES (t2) == void_list_node)
1215     return true;
1216 
1217   if (!cxx_safe_arg_type_equiv_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1218     return false;
1219 
1220   for (t1 = TYPE_ARG_TYPES (t1), t2 = TYPE_ARG_TYPES (t2);
1221        t1 && t2;
1222        t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1223     if (!cxx_safe_arg_type_equiv_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1224       return false;
1225 
1226   return true;
1227 }
1228 
1229 /* Subroutine in comptypes.  */
1230 
1231 static bool
1232 structural_comptypes (tree t1, tree t2, int strict)
1233 {
1234   if (t1 == t2)
1235     return true;
1236 
1237   /* Suppress errors caused by previously reported errors.  */
1238   if (t1 == error_mark_node || t2 == error_mark_node)
1239     return false;
1240 
1241   gcc_assert (TYPE_P (t1) && TYPE_P (t2));
1242 
1243   /* TYPENAME_TYPEs should be resolved if the qualifying scope is the
1244      current instantiation.  */
1245   if (TREE_CODE (t1) == TYPENAME_TYPE)
1246     t1 = resolve_typename_type (t1, /*only_current_p=*/true);
1247 
1248   if (TREE_CODE (t2) == TYPENAME_TYPE)
1249     t2 = resolve_typename_type (t2, /*only_current_p=*/true);
1250 
1251   if (TYPE_PTRMEMFUNC_P (t1))
1252     t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
1253   if (TYPE_PTRMEMFUNC_P (t2))
1254     t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
1255 
1256   /* Different classes of types can't be compatible.  */
1257   if (TREE_CODE (t1) != TREE_CODE (t2))
1258     return false;
1259 
1260   /* Qualifiers must match.  For array types, we will check when we
1261      recur on the array element types.  */
1262   if (TREE_CODE (t1) != ARRAY_TYPE
1263       && cp_type_quals (t1) != cp_type_quals (t2))
1264     return false;
1265   if (TREE_CODE (t1) == FUNCTION_TYPE
1266       && type_memfn_quals (t1) != type_memfn_quals (t2))
1267     return false;
1268   /* Need to check this before TYPE_MAIN_VARIANT.
1269      FIXME function qualifiers should really change the main variant.  */
1270   if (TREE_CODE (t1) == FUNCTION_TYPE
1271       || TREE_CODE (t1) == METHOD_TYPE)
1272     {
1273       if (type_memfn_rqual (t1) != type_memfn_rqual (t2))
1274 	return false;
1275       if (flag_noexcept_type
1276 	  && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (t1),
1277 				 TYPE_RAISES_EXCEPTIONS (t2),
1278 				 ce_type))
1279 	return false;
1280     }
1281 
1282   /* Allow for two different type nodes which have essentially the same
1283      definition.  Note that we already checked for equality of the type
1284      qualifiers (just above).  */
1285 
1286   if (TREE_CODE (t1) != ARRAY_TYPE
1287       && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1288     return true;
1289 
1290 
1291   /* Compare the types.  Break out if they could be the same.  */
1292   switch (TREE_CODE (t1))
1293     {
1294     case VOID_TYPE:
1295     case BOOLEAN_TYPE:
1296       /* All void and bool types are the same.  */
1297       break;
1298 
1299     case INTEGER_TYPE:
1300     case FIXED_POINT_TYPE:
1301     case REAL_TYPE:
1302       /* With these nodes, we can't determine type equivalence by
1303 	 looking at what is stored in the nodes themselves, because
1304 	 two nodes might have different TYPE_MAIN_VARIANTs but still
1305 	 represent the same type.  For example, wchar_t and int could
1306 	 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1307 	 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1308 	 and are distinct types. On the other hand, int and the
1309 	 following typedef
1310 
1311            typedef int INT __attribute((may_alias));
1312 
1313 	 have identical properties, different TYPE_MAIN_VARIANTs, but
1314 	 represent the same type.  The canonical type system keeps
1315 	 track of equivalence in this case, so we fall back on it.  */
1316       return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1317 
1318     case TEMPLATE_TEMPLATE_PARM:
1319     case BOUND_TEMPLATE_TEMPLATE_PARM:
1320       if (!comp_template_parms_position (t1, t2))
1321 	return false;
1322       if (!comp_template_parms
1323 	  (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1)),
1324 	   DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2))))
1325 	return false;
1326       if (TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM)
1327 	break;
1328       /* Don't check inheritance.  */
1329       strict = COMPARE_STRICT;
1330       /* Fall through.  */
1331 
1332     case RECORD_TYPE:
1333     case UNION_TYPE:
1334       if (TYPE_TEMPLATE_INFO (t1) && TYPE_TEMPLATE_INFO (t2)
1335 	  && (TYPE_TI_TEMPLATE (t1) == TYPE_TI_TEMPLATE (t2)
1336 	      || TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM)
1337 	  && comp_template_args (TYPE_TI_ARGS (t1), TYPE_TI_ARGS (t2)))
1338 	break;
1339 
1340       if ((strict & COMPARE_BASE) && DERIVED_FROM_P (t1, t2))
1341 	break;
1342       else if ((strict & COMPARE_DERIVED) && DERIVED_FROM_P (t2, t1))
1343 	break;
1344 
1345       return false;
1346 
1347     case OFFSET_TYPE:
1348       if (!comptypes (TYPE_OFFSET_BASETYPE (t1), TYPE_OFFSET_BASETYPE (t2),
1349 		      strict & ~COMPARE_REDECLARATION))
1350 	return false;
1351       if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1352 	return false;
1353       break;
1354 
1355     case REFERENCE_TYPE:
1356       if (TYPE_REF_IS_RVALUE (t1) != TYPE_REF_IS_RVALUE (t2))
1357 	return false;
1358       /* fall through to checks for pointer types */
1359       gcc_fallthrough ();
1360 
1361     case POINTER_TYPE:
1362       if (TYPE_MODE (t1) != TYPE_MODE (t2)
1363 	  || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1364 	return false;
1365       break;
1366 
1367     case METHOD_TYPE:
1368     case FUNCTION_TYPE:
1369       if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1370 	return false;
1371       if (!compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)))
1372 	return false;
1373       break;
1374 
1375     case ARRAY_TYPE:
1376       /* Target types must match incl. qualifiers.  */
1377       if (!comp_array_types (t1, t2, !!(strict & COMPARE_REDECLARATION)))
1378 	return false;
1379       break;
1380 
1381     case TEMPLATE_TYPE_PARM:
1382       /* If T1 and T2 don't have the same relative position in their
1383 	 template parameters set, they can't be equal.  */
1384       if (!comp_template_parms_position (t1, t2))
1385 	return false;
1386       /* Constrained 'auto's are distinct from parms that don't have the same
1387 	 constraints.  */
1388       if (!equivalent_placeholder_constraints (t1, t2))
1389 	return false;
1390       break;
1391 
1392     case TYPENAME_TYPE:
1393       if (!cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1),
1394 			  TYPENAME_TYPE_FULLNAME (t2)))
1395 	return false;
1396       /* Qualifiers don't matter on scopes.  */
1397       if (!same_type_ignoring_top_level_qualifiers_p (TYPE_CONTEXT (t1),
1398 						      TYPE_CONTEXT (t2)))
1399 	return false;
1400       break;
1401 
1402     case UNBOUND_CLASS_TEMPLATE:
1403       if (!cp_tree_equal (TYPE_IDENTIFIER (t1), TYPE_IDENTIFIER (t2)))
1404 	return false;
1405       if (!same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2)))
1406 	return false;
1407       break;
1408 
1409     case COMPLEX_TYPE:
1410       if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1411 	return false;
1412       break;
1413 
1414     case VECTOR_TYPE:
1415       if (maybe_ne (TYPE_VECTOR_SUBPARTS (t1), TYPE_VECTOR_SUBPARTS (t2))
1416 	  || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1417 	return false;
1418       break;
1419 
1420     case TYPE_PACK_EXPANSION:
1421       return (same_type_p (PACK_EXPANSION_PATTERN (t1),
1422 			   PACK_EXPANSION_PATTERN (t2))
1423 	      && comp_template_args (PACK_EXPANSION_EXTRA_ARGS (t1),
1424 				     PACK_EXPANSION_EXTRA_ARGS (t2)));
1425 
1426     case DECLTYPE_TYPE:
1427       if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t1)
1428           != DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t2)
1429 	  || (DECLTYPE_FOR_LAMBDA_CAPTURE (t1)
1430 	      != DECLTYPE_FOR_LAMBDA_CAPTURE (t2))
1431 	  || (DECLTYPE_FOR_LAMBDA_PROXY (t1)
1432 	      != DECLTYPE_FOR_LAMBDA_PROXY (t2))
1433           || !cp_tree_equal (DECLTYPE_TYPE_EXPR (t1),
1434                              DECLTYPE_TYPE_EXPR (t2)))
1435         return false;
1436       break;
1437 
1438     case UNDERLYING_TYPE:
1439       return same_type_p (UNDERLYING_TYPE_TYPE (t1),
1440 			  UNDERLYING_TYPE_TYPE (t2));
1441 
1442     default:
1443       return false;
1444     }
1445 
1446   /* If we get here, we know that from a target independent POV the
1447      types are the same.  Make sure the target attributes are also
1448      the same.  */
1449   return comp_type_attributes (t1, t2);
1450 }
1451 
1452 /* Return true if T1 and T2 are related as allowed by STRICT.  STRICT
1453    is a bitwise-or of the COMPARE_* flags.  */
1454 
1455 bool
1456 comptypes (tree t1, tree t2, int strict)
1457 {
1458   if (strict == COMPARE_STRICT)
1459     {
1460       if (t1 == t2)
1461 	return true;
1462 
1463       if (t1 == error_mark_node || t2 == error_mark_node)
1464 	return false;
1465 
1466       if (TYPE_STRUCTURAL_EQUALITY_P (t1) || TYPE_STRUCTURAL_EQUALITY_P (t2))
1467 	/* At least one of the types requires structural equality, so
1468 	   perform a deep check. */
1469 	return structural_comptypes (t1, t2, strict);
1470 
1471       if (flag_checking && USE_CANONICAL_TYPES)
1472 	{
1473 	  bool result = structural_comptypes (t1, t2, strict);
1474 
1475 	  if (result && TYPE_CANONICAL (t1) != TYPE_CANONICAL (t2))
1476 	    /* The two types are structurally equivalent, but their
1477 	       canonical types were different. This is a failure of the
1478 	       canonical type propagation code.*/
1479 	    internal_error
1480 	      ("canonical types differ for identical types %qT and %qT",
1481 	       t1, t2);
1482 	  else if (!result && TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2))
1483 	    /* Two types are structurally different, but the canonical
1484 	       types are the same. This means we were over-eager in
1485 	       assigning canonical types. */
1486 	    internal_error
1487 	      ("same canonical type node for different types %qT and %qT",
1488 	       t1, t2);
1489 
1490 	  return result;
1491 	}
1492       if (!flag_checking && USE_CANONICAL_TYPES)
1493 	return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1494       else
1495 	return structural_comptypes (t1, t2, strict);
1496     }
1497   else if (strict == COMPARE_STRUCTURAL)
1498     return structural_comptypes (t1, t2, COMPARE_STRICT);
1499   else
1500     return structural_comptypes (t1, t2, strict);
1501 }
1502 
1503 /* Returns nonzero iff TYPE1 and TYPE2 are the same type, ignoring
1504    top-level qualifiers.  */
1505 
1506 bool
1507 same_type_ignoring_top_level_qualifiers_p (tree type1, tree type2)
1508 {
1509   if (type1 == error_mark_node || type2 == error_mark_node)
1510     return false;
1511 
1512   type1 = cp_build_qualified_type (type1, TYPE_UNQUALIFIED);
1513   type2 = cp_build_qualified_type (type2, TYPE_UNQUALIFIED);
1514   return same_type_p (type1, type2);
1515 }
1516 
1517 /* Returns 1 if TYPE1 is at least as qualified as TYPE2.  */
1518 
1519 bool
1520 at_least_as_qualified_p (const_tree type1, const_tree type2)
1521 {
1522   int q1 = cp_type_quals (type1);
1523   int q2 = cp_type_quals (type2);
1524 
1525   /* All qualifiers for TYPE2 must also appear in TYPE1.  */
1526   return (q1 & q2) == q2;
1527 }
1528 
1529 /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1530    more cv-qualified that TYPE1, and 0 otherwise.  */
1531 
1532 int
1533 comp_cv_qualification (int q1, int q2)
1534 {
1535   if (q1 == q2)
1536     return 0;
1537 
1538   if ((q1 & q2) == q2)
1539     return 1;
1540   else if ((q1 & q2) == q1)
1541     return -1;
1542 
1543   return 0;
1544 }
1545 
1546 int
1547 comp_cv_qualification (const_tree type1, const_tree type2)
1548 {
1549   int q1 = cp_type_quals (type1);
1550   int q2 = cp_type_quals (type2);
1551   return comp_cv_qualification (q1, q2);
1552 }
1553 
1554 /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1555    subset of the cv-qualification signature of TYPE2, and the types
1556    are similar.  Returns -1 if the other way 'round, and 0 otherwise.  */
1557 
1558 int
1559 comp_cv_qual_signature (tree type1, tree type2)
1560 {
1561   if (comp_ptr_ttypes_real (type2, type1, -1))
1562     return 1;
1563   else if (comp_ptr_ttypes_real (type1, type2, -1))
1564     return -1;
1565   else
1566     return 0;
1567 }
1568 
1569 /* Subroutines of `comptypes'.  */
1570 
1571 /* Return true if two parameter type lists PARMS1 and PARMS2 are
1572    equivalent in the sense that functions with those parameter types
1573    can have equivalent types.  The two lists must be equivalent,
1574    element by element.  */
1575 
1576 bool
1577 compparms (const_tree parms1, const_tree parms2)
1578 {
1579   const_tree t1, t2;
1580 
1581   /* An unspecified parmlist matches any specified parmlist
1582      whose argument types don't need default promotions.  */
1583 
1584   for (t1 = parms1, t2 = parms2;
1585        t1 || t2;
1586        t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1587     {
1588       /* If one parmlist is shorter than the other,
1589 	 they fail to match.  */
1590       if (!t1 || !t2)
1591 	return false;
1592       if (!same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1593 	return false;
1594     }
1595   return true;
1596 }
1597 
1598 
1599 /* Process a sizeof or alignof expression where the operand is a
1600    type. STD_ALIGNOF indicates whether an alignof has C++11 (minimum alignment)
1601    or GNU (preferred alignment) semantics; it is ignored if op is
1602    SIZEOF_EXPR.  */
1603 
1604 tree
1605 cxx_sizeof_or_alignof_type (tree type, enum tree_code op, bool std_alignof,
1606 			    bool complain)
1607 {
1608   tree value;
1609   bool dependent_p;
1610 
1611   gcc_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR);
1612   if (type == error_mark_node)
1613     return error_mark_node;
1614 
1615   type = non_reference (type);
1616   if (TREE_CODE (type) == METHOD_TYPE)
1617     {
1618       if (complain)
1619 	pedwarn (input_location, OPT_Wpointer_arith,
1620 		 "invalid application of %qs to a member function",
1621 		 OVL_OP_INFO (false, op)->name);
1622       else
1623 	return error_mark_node;
1624       value = size_one_node;
1625     }
1626 
1627   dependent_p = dependent_type_p (type);
1628   if (!dependent_p)
1629     complete_type (type);
1630   if (dependent_p
1631       /* VLA types will have a non-constant size.  In the body of an
1632 	 uninstantiated template, we don't need to try to compute the
1633 	 value, because the sizeof expression is not an integral
1634 	 constant expression in that case.  And, if we do try to
1635 	 compute the value, we'll likely end up with SAVE_EXPRs, which
1636 	 the template substitution machinery does not expect to see.  */
1637       || (processing_template_decl
1638 	  && COMPLETE_TYPE_P (type)
1639 	  && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST))
1640     {
1641       value = build_min (op, size_type_node, type);
1642       TREE_READONLY (value) = 1;
1643       if (op == ALIGNOF_EXPR && std_alignof)
1644 	ALIGNOF_EXPR_STD_P (value) = true;
1645       return value;
1646     }
1647 
1648   return c_sizeof_or_alignof_type (input_location, complete_type (type),
1649 				   op == SIZEOF_EXPR, std_alignof,
1650 				   complain);
1651 }
1652 
1653 /* Return the size of the type, without producing any warnings for
1654    types whose size cannot be taken.  This routine should be used only
1655    in some other routine that has already produced a diagnostic about
1656    using the size of such a type.  */
1657 tree
1658 cxx_sizeof_nowarn (tree type)
1659 {
1660   if (TREE_CODE (type) == FUNCTION_TYPE
1661       || VOID_TYPE_P (type)
1662       || TREE_CODE (type) == ERROR_MARK)
1663     return size_one_node;
1664   else if (!COMPLETE_TYPE_P (type))
1665     return size_zero_node;
1666   else
1667     return cxx_sizeof_or_alignof_type (type, SIZEOF_EXPR, false, false);
1668 }
1669 
1670 /* Process a sizeof expression where the operand is an expression.  */
1671 
1672 static tree
1673 cxx_sizeof_expr (tree e, tsubst_flags_t complain)
1674 {
1675   if (e == error_mark_node)
1676     return error_mark_node;
1677 
1678   if (processing_template_decl)
1679     {
1680       e = build_min (SIZEOF_EXPR, size_type_node, e);
1681       TREE_SIDE_EFFECTS (e) = 0;
1682       TREE_READONLY (e) = 1;
1683 
1684       return e;
1685     }
1686 
1687   /* To get the size of a static data member declared as an array of
1688      unknown bound, we need to instantiate it.  */
1689   if (VAR_P (e)
1690       && VAR_HAD_UNKNOWN_BOUND (e)
1691       && DECL_TEMPLATE_INSTANTIATION (e))
1692     instantiate_decl (e, /*defer_ok*/true, /*expl_inst_mem*/false);
1693 
1694   if (TREE_CODE (e) == PARM_DECL
1695       && DECL_ARRAY_PARAMETER_P (e)
1696       && (complain & tf_warning))
1697     {
1698       if (warning (OPT_Wsizeof_array_argument, "%<sizeof%> on array function "
1699 		   "parameter %qE will return size of %qT", e, TREE_TYPE (e)))
1700 	inform (DECL_SOURCE_LOCATION (e), "declared here");
1701     }
1702 
1703   e = mark_type_use (e);
1704 
1705   if (bitfield_p (e))
1706     {
1707       if (complain & tf_error)
1708         error ("invalid application of %<sizeof%> to a bit-field");
1709       else
1710         return error_mark_node;
1711       e = char_type_node;
1712     }
1713   else if (is_overloaded_fn (e))
1714     {
1715       if (complain & tf_error)
1716         permerror (input_location, "ISO C++ forbids applying %<sizeof%> to an expression of "
1717                    "function type");
1718       else
1719         return error_mark_node;
1720       e = char_type_node;
1721     }
1722   else if (type_unknown_p (e))
1723     {
1724       if (complain & tf_error)
1725         cxx_incomplete_type_error (e, TREE_TYPE (e));
1726       else
1727         return error_mark_node;
1728       e = char_type_node;
1729     }
1730   else
1731     e = TREE_TYPE (e);
1732 
1733   return cxx_sizeof_or_alignof_type (e, SIZEOF_EXPR, false, complain & tf_error);
1734 }
1735 
1736 /* Implement the __alignof keyword: Return the minimum required
1737    alignment of E, measured in bytes.  For VAR_DECL's and
1738    FIELD_DECL's return DECL_ALIGN (which can be set from an
1739    "aligned" __attribute__ specification).  */
1740 
1741 static tree
1742 cxx_alignof_expr (tree e, tsubst_flags_t complain)
1743 {
1744   tree t;
1745 
1746   if (e == error_mark_node)
1747     return error_mark_node;
1748 
1749   if (processing_template_decl)
1750     {
1751       e = build_min (ALIGNOF_EXPR, size_type_node, e);
1752       TREE_SIDE_EFFECTS (e) = 0;
1753       TREE_READONLY (e) = 1;
1754 
1755       return e;
1756     }
1757 
1758   e = mark_type_use (e);
1759 
1760   if (VAR_P (e))
1761     t = size_int (DECL_ALIGN_UNIT (e));
1762   else if (bitfield_p (e))
1763     {
1764       if (complain & tf_error)
1765         error ("invalid application of %<__alignof%> to a bit-field");
1766       else
1767         return error_mark_node;
1768       t = size_one_node;
1769     }
1770   else if (TREE_CODE (e) == COMPONENT_REF
1771 	   && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL)
1772     t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (e, 1)));
1773   else if (is_overloaded_fn (e))
1774     {
1775       if (complain & tf_error)
1776         permerror (input_location, "ISO C++ forbids applying %<__alignof%> to an expression of "
1777                    "function type");
1778       else
1779         return error_mark_node;
1780       if (TREE_CODE (e) == FUNCTION_DECL)
1781 	t = size_int (DECL_ALIGN_UNIT (e));
1782       else
1783 	t = size_one_node;
1784     }
1785   else if (type_unknown_p (e))
1786     {
1787       if (complain & tf_error)
1788         cxx_incomplete_type_error (e, TREE_TYPE (e));
1789       else
1790         return error_mark_node;
1791       t = size_one_node;
1792     }
1793   else
1794     return cxx_sizeof_or_alignof_type (TREE_TYPE (e), ALIGNOF_EXPR, false,
1795                                        complain & tf_error);
1796 
1797   return fold_convert (size_type_node, t);
1798 }
1799 
1800 /* Process a sizeof or alignof expression E with code OP where the operand
1801    is an expression.  */
1802 
1803 tree
1804 cxx_sizeof_or_alignof_expr (tree e, enum tree_code op, bool complain)
1805 {
1806   if (op == SIZEOF_EXPR)
1807     return cxx_sizeof_expr (e, complain? tf_warning_or_error : tf_none);
1808   else
1809     return cxx_alignof_expr (e, complain? tf_warning_or_error : tf_none);
1810 }
1811 
1812 /*  Build a representation of an expression 'alignas(E).'  Return the
1813     folded integer value of E if it is an integral constant expression
1814     that resolves to a valid alignment.  If E depends on a template
1815     parameter, return a syntactic representation tree of kind
1816     ALIGNOF_EXPR.  Otherwise, return an error_mark_node if the
1817     expression is ill formed, or NULL_TREE if E is NULL_TREE.  */
1818 
1819 tree
1820 cxx_alignas_expr (tree e)
1821 {
1822   if (e == NULL_TREE || e == error_mark_node
1823       || (!TYPE_P (e) && !require_potential_rvalue_constant_expression (e)))
1824     return e;
1825 
1826   if (TYPE_P (e))
1827     /* [dcl.align]/3:
1828 
1829 	   When the alignment-specifier is of the form
1830 	   alignas(type-id ), it shall have the same effect as
1831 	   alignas(alignof(type-id )).  */
1832 
1833     return cxx_sizeof_or_alignof_type (e, ALIGNOF_EXPR, true, false);
1834 
1835   /* If we reach this point, it means the alignas expression if of
1836      the form "alignas(assignment-expression)", so we should follow
1837      what is stated by [dcl.align]/2.  */
1838 
1839   if (value_dependent_expression_p (e))
1840     /* Leave value-dependent expression alone for now. */
1841     return e;
1842 
1843   e = instantiate_non_dependent_expr (e);
1844   e = mark_rvalue_use (e);
1845 
1846   /* [dcl.align]/2 says:
1847 
1848          the assignment-expression shall be an integral constant
1849 	 expression.  */
1850 
1851   if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (e)))
1852     {
1853       error ("%<alignas%> argument has non-integral type %qT", TREE_TYPE (e));
1854       return error_mark_node;
1855     }
1856 
1857   return cxx_constant_value (e);
1858 }
1859 
1860 
1861 /* EXPR is being used in a context that is not a function call.
1862    Enforce:
1863 
1864      [expr.ref]
1865 
1866      The expression can be used only as the left-hand operand of a
1867      member function call.
1868 
1869      [expr.mptr.operator]
1870 
1871      If the result of .* or ->* is a function, then that result can be
1872      used only as the operand for the function call operator ().
1873 
1874    by issuing an error message if appropriate.  Returns true iff EXPR
1875    violates these rules.  */
1876 
1877 bool
1878 invalid_nonstatic_memfn_p (location_t loc, tree expr, tsubst_flags_t complain)
1879 {
1880   if (expr == NULL_TREE)
1881     return false;
1882   /* Don't enforce this in MS mode.  */
1883   if (flag_ms_extensions)
1884     return false;
1885   if (is_overloaded_fn (expr) && !really_overloaded_fn (expr))
1886     expr = get_first_fn (expr);
1887   if (DECL_NONSTATIC_MEMBER_FUNCTION_P (expr))
1888     {
1889       if (complain & tf_error)
1890 	{
1891 	  if (DECL_P (expr))
1892 	    {
1893 	      error_at (loc, "invalid use of non-static member function %qD",
1894 			expr);
1895 	      inform (DECL_SOURCE_LOCATION (expr), "declared here");
1896 	    }
1897 	  else
1898 	    error_at (loc, "invalid use of non-static member function of "
1899 		      "type %qT", TREE_TYPE (expr));
1900 	}
1901       return true;
1902     }
1903   return false;
1904 }
1905 
1906 /* If EXP is a reference to a bitfield, and the type of EXP does not
1907    match the declared type of the bitfield, return the declared type
1908    of the bitfield.  Otherwise, return NULL_TREE.  */
1909 
1910 tree
1911 is_bitfield_expr_with_lowered_type (const_tree exp)
1912 {
1913   switch (TREE_CODE (exp))
1914     {
1915     case COND_EXPR:
1916       if (!is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1)
1917 					       ? TREE_OPERAND (exp, 1)
1918 					       : TREE_OPERAND (exp, 0)))
1919 	return NULL_TREE;
1920       return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 2));
1921 
1922     case COMPOUND_EXPR:
1923       return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1));
1924 
1925     case MODIFY_EXPR:
1926     case SAVE_EXPR:
1927       return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
1928 
1929     case COMPONENT_REF:
1930       {
1931 	tree field;
1932 
1933 	field = TREE_OPERAND (exp, 1);
1934 	if (TREE_CODE (field) != FIELD_DECL || !DECL_BIT_FIELD_TYPE (field))
1935 	  return NULL_TREE;
1936 	if (same_type_ignoring_top_level_qualifiers_p
1937 	    (TREE_TYPE (exp), DECL_BIT_FIELD_TYPE (field)))
1938 	  return NULL_TREE;
1939 	return DECL_BIT_FIELD_TYPE (field);
1940       }
1941 
1942     case VAR_DECL:
1943       if (DECL_HAS_VALUE_EXPR_P (exp))
1944 	return is_bitfield_expr_with_lowered_type (DECL_VALUE_EXPR
1945 						   (CONST_CAST_TREE (exp)));
1946       return NULL_TREE;
1947 
1948     CASE_CONVERT:
1949       if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (exp, 0)))
1950 	  == TYPE_MAIN_VARIANT (TREE_TYPE (exp)))
1951 	return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
1952       /* Fallthrough.  */
1953 
1954     default:
1955       return NULL_TREE;
1956     }
1957 }
1958 
1959 /* Like is_bitfield_with_lowered_type, except that if EXP is not a
1960    bitfield with a lowered type, the type of EXP is returned, rather
1961    than NULL_TREE.  */
1962 
1963 tree
1964 unlowered_expr_type (const_tree exp)
1965 {
1966   tree type;
1967   tree etype = TREE_TYPE (exp);
1968 
1969   type = is_bitfield_expr_with_lowered_type (exp);
1970   if (type)
1971     type = cp_build_qualified_type (type, cp_type_quals (etype));
1972   else
1973     type = etype;
1974 
1975   return type;
1976 }
1977 
1978 /* Perform the conversions in [expr] that apply when an lvalue appears
1979    in an rvalue context: the lvalue-to-rvalue, array-to-pointer, and
1980    function-to-pointer conversions.  In addition, bitfield references are
1981    converted to their declared types. Note that this function does not perform
1982    the lvalue-to-rvalue conversion for class types. If you need that conversion
1983    for class types, then you probably need to use force_rvalue.
1984 
1985    Although the returned value is being used as an rvalue, this
1986    function does not wrap the returned expression in a
1987    NON_LVALUE_EXPR; the caller is expected to be mindful of the fact
1988    that the return value is no longer an lvalue.  */
1989 
1990 tree
1991 decay_conversion (tree exp,
1992 		  tsubst_flags_t complain,
1993 		  bool reject_builtin /* = true */)
1994 {
1995   tree type;
1996   enum tree_code code;
1997   location_t loc = EXPR_LOC_OR_LOC (exp, input_location);
1998 
1999   type = TREE_TYPE (exp);
2000   if (type == error_mark_node)
2001     return error_mark_node;
2002 
2003   exp = resolve_nondeduced_context (exp, complain);
2004   if (type_unknown_p (exp))
2005     {
2006       if (complain & tf_error)
2007 	cxx_incomplete_type_error (exp, TREE_TYPE (exp));
2008       return error_mark_node;
2009     }
2010 
2011   code = TREE_CODE (type);
2012 
2013   if (error_operand_p (exp))
2014     return error_mark_node;
2015 
2016   if (NULLPTR_TYPE_P (type) && !TREE_SIDE_EFFECTS (exp))
2017     {
2018       mark_rvalue_use (exp, loc, reject_builtin);
2019       return nullptr_node;
2020     }
2021 
2022   /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2023      Leave such NOP_EXPRs, since RHS is being used in non-lvalue context.  */
2024   if (code == VOID_TYPE)
2025     {
2026       if (complain & tf_error)
2027 	error_at (loc, "void value not ignored as it ought to be");
2028       return error_mark_node;
2029     }
2030   if (invalid_nonstatic_memfn_p (loc, exp, complain))
2031     return error_mark_node;
2032   if (code == FUNCTION_TYPE || is_overloaded_fn (exp))
2033     {
2034       exp = mark_lvalue_use (exp);
2035       if (reject_builtin && reject_gcc_builtin (exp, loc))
2036 	return error_mark_node;
2037       return cp_build_addr_expr (exp, complain);
2038     }
2039   if (code == ARRAY_TYPE)
2040     {
2041       tree adr;
2042       tree ptrtype;
2043 
2044       exp = mark_lvalue_use (exp);
2045 
2046       if (INDIRECT_REF_P (exp))
2047 	return build_nop (build_pointer_type (TREE_TYPE (type)),
2048 			  TREE_OPERAND (exp, 0));
2049 
2050       if (TREE_CODE (exp) == COMPOUND_EXPR)
2051 	{
2052 	  tree op1 = decay_conversion (TREE_OPERAND (exp, 1), complain);
2053 	  if (op1 == error_mark_node)
2054             return error_mark_node;
2055 	  return build2 (COMPOUND_EXPR, TREE_TYPE (op1),
2056 			 TREE_OPERAND (exp, 0), op1);
2057 	}
2058 
2059       if (!obvalue_p (exp)
2060 	  && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
2061 	{
2062 	  if (complain & tf_error)
2063 	    error_at (loc, "invalid use of non-lvalue array");
2064 	  return error_mark_node;
2065 	}
2066 
2067       /* Don't let an array compound literal decay to a pointer.  It can
2068 	 still be used to initialize an array or bind to a reference.  */
2069       if (TREE_CODE (exp) == TARGET_EXPR)
2070 	{
2071 	  if (complain & tf_error)
2072 	    error_at (loc, "taking address of temporary array");
2073 	  return error_mark_node;
2074 	}
2075 
2076       ptrtype = build_pointer_type (TREE_TYPE (type));
2077 
2078       if (VAR_P (exp))
2079 	{
2080 	  if (!cxx_mark_addressable (exp))
2081 	    return error_mark_node;
2082 	  adr = build_nop (ptrtype, build_address (exp));
2083 	  return adr;
2084 	}
2085       /* This way is better for a COMPONENT_REF since it can
2086 	 simplify the offset for a component.  */
2087       adr = cp_build_addr_expr (exp, complain);
2088       return cp_convert (ptrtype, adr, complain);
2089     }
2090 
2091   /* Otherwise, it's the lvalue-to-rvalue conversion.  */
2092   exp = mark_rvalue_use (exp, loc, reject_builtin);
2093 
2094   /* If a bitfield is used in a context where integral promotion
2095      applies, then the caller is expected to have used
2096      default_conversion.  That function promotes bitfields correctly
2097      before calling this function.  At this point, if we have a
2098      bitfield referenced, we may assume that is not subject to
2099      promotion, and that, therefore, the type of the resulting rvalue
2100      is the declared type of the bitfield.  */
2101   exp = convert_bitfield_to_declared_type (exp);
2102 
2103   /* We do not call rvalue() here because we do not want to wrap EXP
2104      in a NON_LVALUE_EXPR.  */
2105 
2106   /* [basic.lval]
2107 
2108      Non-class rvalues always have cv-unqualified types.  */
2109   type = TREE_TYPE (exp);
2110   if (!CLASS_TYPE_P (type) && cv_qualified_p (type))
2111     exp = build_nop (cv_unqualified (type), exp);
2112 
2113   if (!complete_type_or_maybe_complain (type, exp, complain))
2114     return error_mark_node;
2115 
2116   return exp;
2117 }
2118 
2119 /* Perform preparatory conversions, as part of the "usual arithmetic
2120    conversions".  In particular, as per [expr]:
2121 
2122      Whenever an lvalue expression appears as an operand of an
2123      operator that expects the rvalue for that operand, the
2124      lvalue-to-rvalue, array-to-pointer, or function-to-pointer
2125      standard conversions are applied to convert the expression to an
2126      rvalue.
2127 
2128    In addition, we perform integral promotions here, as those are
2129    applied to both operands to a binary operator before determining
2130    what additional conversions should apply.  */
2131 
2132 static tree
2133 cp_default_conversion (tree exp, tsubst_flags_t complain)
2134 {
2135   /* Check for target-specific promotions.  */
2136   tree promoted_type = targetm.promoted_type (TREE_TYPE (exp));
2137   if (promoted_type)
2138     exp = cp_convert (promoted_type, exp, complain);
2139   /* Perform the integral promotions first so that bitfield
2140      expressions (which may promote to "int", even if the bitfield is
2141      declared "unsigned") are promoted correctly.  */
2142   else if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (exp)))
2143     exp = cp_perform_integral_promotions (exp, complain);
2144   /* Perform the other conversions.  */
2145   exp = decay_conversion (exp, complain);
2146 
2147   return exp;
2148 }
2149 
2150 /* C version.  */
2151 
2152 tree
2153 default_conversion (tree exp)
2154 {
2155   return cp_default_conversion (exp, tf_warning_or_error);
2156 }
2157 
2158 /* EXPR is an expression with an integral or enumeration type.
2159    Perform the integral promotions in [conv.prom], and return the
2160    converted value.  */
2161 
2162 tree
2163 cp_perform_integral_promotions (tree expr, tsubst_flags_t complain)
2164 {
2165   tree type;
2166   tree promoted_type;
2167 
2168   expr = mark_rvalue_use (expr);
2169   if (error_operand_p (expr))
2170     return error_mark_node;
2171 
2172   /* [conv.prom]
2173 
2174      If the bitfield has an enumerated type, it is treated as any
2175      other value of that type for promotion purposes.  */
2176   type = is_bitfield_expr_with_lowered_type (expr);
2177   if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
2178     type = TREE_TYPE (expr);
2179   gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
2180   /* Scoped enums don't promote.  */
2181   if (SCOPED_ENUM_P (type))
2182     return expr;
2183   promoted_type = type_promotes_to (type);
2184   if (type != promoted_type)
2185     expr = cp_convert (promoted_type, expr, complain);
2186   return expr;
2187 }
2188 
2189 /* C version.  */
2190 
2191 tree
2192 perform_integral_promotions (tree expr)
2193 {
2194   return cp_perform_integral_promotions (expr, tf_warning_or_error);
2195 }
2196 
2197 /* Returns nonzero iff exp is a STRING_CST or the result of applying
2198    decay_conversion to one.  */
2199 
2200 int
2201 string_conv_p (const_tree totype, const_tree exp, int warn)
2202 {
2203   tree t;
2204 
2205   if (!TYPE_PTR_P (totype))
2206     return 0;
2207 
2208   t = TREE_TYPE (totype);
2209   if (!same_type_p (t, char_type_node)
2210       && !same_type_p (t, char16_type_node)
2211       && !same_type_p (t, char32_type_node)
2212       && !same_type_p (t, wchar_type_node))
2213     return 0;
2214 
2215   STRIP_ANY_LOCATION_WRAPPER (exp);
2216 
2217   if (TREE_CODE (exp) == STRING_CST)
2218     {
2219       /* Make sure that we don't try to convert between char and wide chars.  */
2220       if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t))
2221 	return 0;
2222     }
2223   else
2224     {
2225       /* Is this a string constant which has decayed to 'const char *'?  */
2226       t = build_pointer_type (cp_build_qualified_type (t, TYPE_QUAL_CONST));
2227       if (!same_type_p (TREE_TYPE (exp), t))
2228 	return 0;
2229       STRIP_NOPS (exp);
2230       if (TREE_CODE (exp) != ADDR_EXPR
2231 	  || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST)
2232 	return 0;
2233     }
2234   if (warn)
2235     {
2236       if (cxx_dialect >= cxx11)
2237 	pedwarn (input_location, OPT_Wwrite_strings,
2238 		 "ISO C++ forbids converting a string constant to %qT",
2239 		 totype);
2240       else
2241 	warning (OPT_Wwrite_strings,
2242 		 "deprecated conversion from string constant to %qT",
2243 		 totype);
2244     }
2245 
2246   return 1;
2247 }
2248 
2249 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
2250    can, for example, use as an lvalue.  This code used to be in
2251    unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
2252    expressions, where we're dealing with aggregates.  But now it's again only
2253    called from unary_complex_lvalue.  The case (in particular) that led to
2254    this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
2255    get it there.  */
2256 
2257 static tree
2258 rationalize_conditional_expr (enum tree_code code, tree t,
2259                               tsubst_flags_t complain)
2260 {
2261   location_t loc = EXPR_LOC_OR_LOC (t, input_location);
2262 
2263   /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
2264      the first operand is always the one to be used if both operands
2265      are equal, so we know what conditional expression this used to be.  */
2266   if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR)
2267     {
2268       tree op0 = TREE_OPERAND (t, 0);
2269       tree op1 = TREE_OPERAND (t, 1);
2270 
2271       /* The following code is incorrect if either operand side-effects.  */
2272       gcc_assert (!TREE_SIDE_EFFECTS (op0)
2273 		  && !TREE_SIDE_EFFECTS (op1));
2274       return
2275 	build_conditional_expr (loc,
2276 				build_x_binary_op (loc,
2277 						   (TREE_CODE (t) == MIN_EXPR
2278 						    ? LE_EXPR : GE_EXPR),
2279 						   op0, TREE_CODE (op0),
2280 						   op1, TREE_CODE (op1),
2281 						   /*overload=*/NULL,
2282 						   complain),
2283                                 cp_build_unary_op (code, op0, false, complain),
2284                                 cp_build_unary_op (code, op1, false, complain),
2285                                 complain);
2286     }
2287 
2288   return
2289     build_conditional_expr (loc, TREE_OPERAND (t, 0),
2290 			    cp_build_unary_op (code, TREE_OPERAND (t, 1), false,
2291                                                complain),
2292 			    cp_build_unary_op (code, TREE_OPERAND (t, 2), false,
2293                                                complain),
2294                             complain);
2295 }
2296 
2297 /* Given the TYPE of an anonymous union field inside T, return the
2298    FIELD_DECL for the field.  If not found return NULL_TREE.  Because
2299    anonymous unions can nest, we must also search all anonymous unions
2300    that are directly reachable.  */
2301 
2302 tree
2303 lookup_anon_field (tree t, tree type)
2304 {
2305   tree field;
2306 
2307   t = TYPE_MAIN_VARIANT (t);
2308 
2309   for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
2310     {
2311       if (TREE_STATIC (field))
2312 	continue;
2313       if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
2314 	continue;
2315 
2316       /* If we find it directly, return the field.  */
2317       if (DECL_NAME (field) == NULL_TREE
2318 	  && type == TYPE_MAIN_VARIANT (TREE_TYPE (field)))
2319 	{
2320 	  return field;
2321 	}
2322 
2323       /* Otherwise, it could be nested, search harder.  */
2324       if (DECL_NAME (field) == NULL_TREE
2325 	  && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2326 	{
2327 	  tree subfield = lookup_anon_field (TREE_TYPE (field), type);
2328 	  if (subfield)
2329 	    return subfield;
2330 	}
2331     }
2332   return NULL_TREE;
2333 }
2334 
2335 /* Build an expression representing OBJECT.MEMBER.  OBJECT is an
2336    expression; MEMBER is a DECL or baselink.  If ACCESS_PATH is
2337    non-NULL, it indicates the path to the base used to name MEMBER.
2338    If PRESERVE_REFERENCE is true, the expression returned will have
2339    REFERENCE_TYPE if the MEMBER does.  Otherwise, the expression
2340    returned will have the type referred to by the reference.
2341 
2342    This function does not perform access control; that is either done
2343    earlier by the parser when the name of MEMBER is resolved to MEMBER
2344    itself, or later when overload resolution selects one of the
2345    functions indicated by MEMBER.  */
2346 
2347 tree
2348 build_class_member_access_expr (cp_expr object, tree member,
2349 				tree access_path, bool preserve_reference,
2350 				tsubst_flags_t complain)
2351 {
2352   tree object_type;
2353   tree member_scope;
2354   tree result = NULL_TREE;
2355   tree using_decl = NULL_TREE;
2356 
2357   if (error_operand_p (object) || error_operand_p (member))
2358     return error_mark_node;
2359 
2360   gcc_assert (DECL_P (member) || BASELINK_P (member));
2361 
2362   /* [expr.ref]
2363 
2364      The type of the first expression shall be "class object" (of a
2365      complete type).  */
2366   object_type = TREE_TYPE (object);
2367   if (!currently_open_class (object_type)
2368       && !complete_type_or_maybe_complain (object_type, object, complain))
2369     return error_mark_node;
2370   if (!CLASS_TYPE_P (object_type))
2371     {
2372       if (complain & tf_error)
2373 	{
2374 	  if (POINTER_TYPE_P (object_type)
2375 	      && CLASS_TYPE_P (TREE_TYPE (object_type)))
2376 	    error ("request for member %qD in %qE, which is of pointer "
2377 		   "type %qT (maybe you meant to use %<->%> ?)",
2378 		   member, object.get_value (), object_type);
2379 	  else
2380 	    error ("request for member %qD in %qE, which is of non-class "
2381 		   "type %qT", member, object.get_value (), object_type);
2382 	}
2383       return error_mark_node;
2384     }
2385 
2386   /* The standard does not seem to actually say that MEMBER must be a
2387      member of OBJECT_TYPE.  However, that is clearly what is
2388      intended.  */
2389   if (DECL_P (member))
2390     {
2391       member_scope = DECL_CLASS_CONTEXT (member);
2392       if (!mark_used (member, complain) && !(complain & tf_error))
2393 	return error_mark_node;
2394       if (TREE_DEPRECATED (member))
2395 	warn_deprecated_use (member, NULL_TREE);
2396     }
2397   else
2398     member_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (member));
2399   /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will
2400      presently be the anonymous union.  Go outwards until we find a
2401      type related to OBJECT_TYPE.  */
2402   while ((ANON_AGGR_TYPE_P (member_scope) || UNSCOPED_ENUM_P (member_scope))
2403 	 && !same_type_ignoring_top_level_qualifiers_p (member_scope,
2404 							object_type))
2405     member_scope = TYPE_CONTEXT (member_scope);
2406   if (!member_scope || !DERIVED_FROM_P (member_scope, object_type))
2407     {
2408       if (complain & tf_error)
2409 	{
2410 	  if (TREE_CODE (member) == FIELD_DECL)
2411 	    error ("invalid use of nonstatic data member %qE", member);
2412 	  else
2413 	    error ("%qD is not a member of %qT", member, object_type);
2414 	}
2415       return error_mark_node;
2416     }
2417 
2418   /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into
2419      `(*(a ?  &b : &c)).x', and so on.  A COND_EXPR is only an lvalue
2420      in the front end; only _DECLs and _REFs are lvalues in the back end.  */
2421   {
2422     tree temp = unary_complex_lvalue (ADDR_EXPR, object);
2423     if (temp)
2424       object = cp_build_fold_indirect_ref (temp);
2425   }
2426 
2427   /* In [expr.ref], there is an explicit list of the valid choices for
2428      MEMBER.  We check for each of those cases here.  */
2429   if (VAR_P (member))
2430     {
2431       /* A static data member.  */
2432       result = member;
2433       mark_exp_read (object);
2434       /* If OBJECT has side-effects, they are supposed to occur.  */
2435       if (TREE_SIDE_EFFECTS (object))
2436 	result = build2 (COMPOUND_EXPR, TREE_TYPE (result), object, result);
2437     }
2438   else if (TREE_CODE (member) == FIELD_DECL)
2439     {
2440       /* A non-static data member.  */
2441       bool null_object_p;
2442       int type_quals;
2443       tree member_type;
2444 
2445       if (INDIRECT_REF_P (object))
2446 	null_object_p =
2447 	  integer_zerop (tree_strip_nop_conversions (TREE_OPERAND (object, 0)));
2448       else
2449 	null_object_p = false;
2450 
2451       /* Convert OBJECT to the type of MEMBER.  */
2452       if (!same_type_p (TYPE_MAIN_VARIANT (object_type),
2453 			TYPE_MAIN_VARIANT (member_scope)))
2454 	{
2455 	  tree binfo;
2456 	  base_kind kind;
2457 
2458 	  binfo = lookup_base (access_path ? access_path : object_type,
2459 			       member_scope, ba_unique, &kind, complain);
2460 	  if (binfo == error_mark_node)
2461 	    return error_mark_node;
2462 
2463 	  /* It is invalid to try to get to a virtual base of a
2464 	     NULL object.  The most common cause is invalid use of
2465 	     offsetof macro.  */
2466 	  if (null_object_p && kind == bk_via_virtual)
2467 	    {
2468 	      if (complain & tf_error)
2469 		{
2470 		  error ("invalid access to non-static data member %qD in "
2471 			 "virtual base of NULL object", member);
2472 		}
2473 	      return error_mark_node;
2474 	    }
2475 
2476 	  /* Convert to the base.  */
2477 	  object = build_base_path (PLUS_EXPR, object, binfo,
2478 				    /*nonnull=*/1, complain);
2479 	  /* If we found the base successfully then we should be able
2480 	     to convert to it successfully.  */
2481 	  gcc_assert (object != error_mark_node);
2482 	}
2483 
2484       /* If MEMBER is from an anonymous aggregate, we have converted
2485 	 OBJECT so that it refers to the class containing the
2486 	 anonymous union.  Generate a reference to the anonymous union
2487 	 itself, and recur to find MEMBER.  */
2488       if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member))
2489 	  /* When this code is called from build_field_call, the
2490 	     object already has the type of the anonymous union.
2491 	     That is because the COMPONENT_REF was already
2492 	     constructed, and was then disassembled before calling
2493 	     build_field_call.  After the function-call code is
2494 	     cleaned up, this waste can be eliminated.  */
2495 	  && (!same_type_ignoring_top_level_qualifiers_p
2496 	      (TREE_TYPE (object), DECL_CONTEXT (member))))
2497 	{
2498 	  tree anonymous_union;
2499 
2500 	  anonymous_union = lookup_anon_field (TREE_TYPE (object),
2501 					       DECL_CONTEXT (member));
2502 	  object = build_class_member_access_expr (object,
2503 						   anonymous_union,
2504 						   /*access_path=*/NULL_TREE,
2505 						   preserve_reference,
2506 						   complain);
2507 	}
2508 
2509       /* Compute the type of the field, as described in [expr.ref].  */
2510       type_quals = TYPE_UNQUALIFIED;
2511       member_type = TREE_TYPE (member);
2512       if (TREE_CODE (member_type) != REFERENCE_TYPE)
2513 	{
2514 	  type_quals = (cp_type_quals (member_type)
2515 			| cp_type_quals (object_type));
2516 
2517 	  /* A field is const (volatile) if the enclosing object, or the
2518 	     field itself, is const (volatile).  But, a mutable field is
2519 	     not const, even within a const object.  */
2520 	  if (DECL_MUTABLE_P (member))
2521 	    type_quals &= ~TYPE_QUAL_CONST;
2522 	  member_type = cp_build_qualified_type (member_type, type_quals);
2523 	}
2524 
2525       result = build3_loc (input_location, COMPONENT_REF, member_type,
2526 			   object, member, NULL_TREE);
2527 
2528       /* Mark the expression const or volatile, as appropriate.  Even
2529 	 though we've dealt with the type above, we still have to mark the
2530 	 expression itself.  */
2531       if (type_quals & TYPE_QUAL_CONST)
2532 	TREE_READONLY (result) = 1;
2533       if (type_quals & TYPE_QUAL_VOLATILE)
2534 	TREE_THIS_VOLATILE (result) = 1;
2535     }
2536   else if (BASELINK_P (member))
2537     {
2538       /* The member is a (possibly overloaded) member function.  */
2539       tree functions;
2540       tree type;
2541 
2542       /* If the MEMBER is exactly one static member function, then we
2543 	 know the type of the expression.  Otherwise, we must wait
2544 	 until overload resolution has been performed.  */
2545       functions = BASELINK_FUNCTIONS (member);
2546       if (TREE_CODE (functions) == FUNCTION_DECL
2547 	  && DECL_STATIC_FUNCTION_P (functions))
2548 	type = TREE_TYPE (functions);
2549       else
2550 	type = unknown_type_node;
2551       /* Note that we do not convert OBJECT to the BASELINK_BINFO
2552 	 base.  That will happen when the function is called.  */
2553       result = build3 (COMPONENT_REF, type, object, member, NULL_TREE);
2554     }
2555   else if (TREE_CODE (member) == CONST_DECL)
2556     {
2557       /* The member is an enumerator.  */
2558       result = member;
2559       /* If OBJECT has side-effects, they are supposed to occur.  */
2560       if (TREE_SIDE_EFFECTS (object))
2561 	result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
2562 			 object, result);
2563     }
2564   else if ((using_decl = strip_using_decl (member)) != member)
2565     result = build_class_member_access_expr (object,
2566 					     using_decl,
2567 					     access_path, preserve_reference,
2568 					     complain);
2569   else
2570     {
2571       if (complain & tf_error)
2572 	error ("invalid use of %qD", member);
2573       return error_mark_node;
2574     }
2575 
2576   if (!preserve_reference)
2577     /* [expr.ref]
2578 
2579        If E2 is declared to have type "reference to T", then ... the
2580        type of E1.E2 is T.  */
2581     result = convert_from_reference (result);
2582 
2583   return result;
2584 }
2585 
2586 /* Return the destructor denoted by OBJECT.SCOPE::DTOR_NAME, or, if
2587    SCOPE is NULL, by OBJECT.DTOR_NAME, where DTOR_NAME is ~type.  */
2588 
2589 static tree
2590 lookup_destructor (tree object, tree scope, tree dtor_name,
2591 		   tsubst_flags_t complain)
2592 {
2593   tree object_type = TREE_TYPE (object);
2594   tree dtor_type = TREE_OPERAND (dtor_name, 0);
2595   tree expr;
2596 
2597   /* We've already complained about this destructor.  */
2598   if (dtor_type == error_mark_node)
2599     return error_mark_node;
2600 
2601   if (scope && !check_dtor_name (scope, dtor_type))
2602     {
2603       if (complain & tf_error)
2604 	error ("qualified type %qT does not match destructor name ~%qT",
2605 	       scope, dtor_type);
2606       return error_mark_node;
2607     }
2608   if (is_auto (dtor_type))
2609     dtor_type = object_type;
2610   else if (identifier_p (dtor_type))
2611     {
2612       /* In a template, names we can't find a match for are still accepted
2613 	 destructor names, and we check them here.  */
2614       if (check_dtor_name (object_type, dtor_type))
2615 	dtor_type = object_type;
2616       else
2617 	{
2618 	  if (complain & tf_error)
2619 	    error ("object type %qT does not match destructor name ~%qT",
2620 		   object_type, dtor_type);
2621 	  return error_mark_node;
2622 	}
2623 
2624     }
2625   else if (!DERIVED_FROM_P (dtor_type, TYPE_MAIN_VARIANT (object_type)))
2626     {
2627       if (complain & tf_error)
2628 	error ("the type being destroyed is %qT, but the destructor "
2629 	       "refers to %qT", TYPE_MAIN_VARIANT (object_type), dtor_type);
2630       return error_mark_node;
2631     }
2632   expr = lookup_member (dtor_type, complete_dtor_identifier,
2633 			/*protect=*/1, /*want_type=*/false,
2634 			tf_warning_or_error);
2635   if (!expr)
2636     {
2637       if (complain & tf_error)
2638 	cxx_incomplete_type_error (dtor_name, dtor_type);
2639       return error_mark_node;
2640     }
2641   expr = (adjust_result_of_qualified_name_lookup
2642 	  (expr, dtor_type, object_type));
2643   if (scope == NULL_TREE)
2644     /* We need to call adjust_result_of_qualified_name_lookup in case the
2645        destructor names a base class, but we unset BASELINK_QUALIFIED_P so
2646        that we still get virtual function binding.  */
2647     BASELINK_QUALIFIED_P (expr) = false;
2648   return expr;
2649 }
2650 
2651 /* An expression of the form "A::template B" has been resolved to
2652    DECL.  Issue a diagnostic if B is not a template or template
2653    specialization.  */
2654 
2655 void
2656 check_template_keyword (tree decl)
2657 {
2658   /* The standard says:
2659 
2660       [temp.names]
2661 
2662       If a name prefixed by the keyword template is not a member
2663       template, the program is ill-formed.
2664 
2665      DR 228 removed the restriction that the template be a member
2666      template.
2667 
2668      DR 96, if accepted would add the further restriction that explicit
2669      template arguments must be provided if the template keyword is
2670      used, but, as of 2005-10-16, that DR is still in "drafting".  If
2671      this DR is accepted, then the semantic checks here can be
2672      simplified, as the entity named must in fact be a template
2673      specialization, rather than, as at present, a set of overloaded
2674      functions containing at least one template function.  */
2675   if (TREE_CODE (decl) != TEMPLATE_DECL
2676       && TREE_CODE (decl) != TEMPLATE_ID_EXPR)
2677     {
2678       if (VAR_P (decl))
2679 	{
2680 	  if (DECL_USE_TEMPLATE (decl)
2681 	      && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
2682 	    ;
2683 	  else
2684 	    permerror (input_location, "%qD is not a template", decl);
2685 	}
2686       else if (!is_overloaded_fn (decl))
2687 	permerror (input_location, "%qD is not a template", decl);
2688       else
2689 	{
2690 	  bool found = false;
2691 
2692 	  for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
2693 	       !found && iter; ++iter)
2694 	    {
2695 	      tree fn = *iter;
2696 	      if (TREE_CODE (fn) == TEMPLATE_DECL
2697 		  || TREE_CODE (fn) == TEMPLATE_ID_EXPR
2698 		  || (TREE_CODE (fn) == FUNCTION_DECL
2699 		      && DECL_USE_TEMPLATE (fn)
2700 		      && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (fn))))
2701 		found = true;
2702 	    }
2703 	  if (!found)
2704 	    permerror (input_location, "%qD is not a template", decl);
2705 	}
2706     }
2707 }
2708 
2709 /* Record that an access failure occurred on BASETYPE_PATH attempting
2710    to access FIELD_DECL.  */
2711 
2712 void
2713 access_failure_info::record_access_failure (tree basetype_path,
2714 					    tree field_decl)
2715 {
2716   m_was_inaccessible = true;
2717   m_basetype_path = basetype_path;
2718   m_field_decl = field_decl;
2719 }
2720 
2721 /* If an access failure was recorded, then attempt to locate an
2722    accessor function for the pertinent field, and if one is
2723    available, add a note and fix-it hint suggesting using it.  */
2724 
2725 void
2726 access_failure_info::maybe_suggest_accessor (bool const_p) const
2727 {
2728   if (!m_was_inaccessible)
2729     return;
2730 
2731   tree accessor
2732     = locate_field_accessor (m_basetype_path, m_field_decl, const_p);
2733   if (!accessor)
2734     return;
2735 
2736   /* The accessor must itself be accessible for it to be a reasonable
2737      suggestion.  */
2738   if (!accessible_p (m_basetype_path, accessor, true))
2739     return;
2740 
2741   rich_location richloc (line_table, input_location);
2742   pretty_printer pp;
2743   pp_printf (&pp, "%s()", IDENTIFIER_POINTER (DECL_NAME (accessor)));
2744   richloc.add_fixit_replace (pp_formatted_text (&pp));
2745   inform (&richloc, "field %q#D can be accessed via %q#D",
2746 	  m_field_decl, accessor);
2747 }
2748 
2749 /* This function is called by the parser to process a class member
2750    access expression of the form OBJECT.NAME.  NAME is a node used by
2751    the parser to represent a name; it is not yet a DECL.  It may,
2752    however, be a BASELINK where the BASELINK_FUNCTIONS is a
2753    TEMPLATE_ID_EXPR.  Templates must be looked up by the parser, and
2754    there is no reason to do the lookup twice, so the parser keeps the
2755    BASELINK.  TEMPLATE_P is true iff NAME was explicitly declared to
2756    be a template via the use of the "A::template B" syntax.  */
2757 
2758 tree
2759 finish_class_member_access_expr (cp_expr object, tree name, bool template_p,
2760 				 tsubst_flags_t complain)
2761 {
2762   tree expr;
2763   tree object_type;
2764   tree member;
2765   tree access_path = NULL_TREE;
2766   tree orig_object = object;
2767   tree orig_name = name;
2768 
2769   if (object == error_mark_node || name == error_mark_node)
2770     return error_mark_node;
2771 
2772   /* If OBJECT is an ObjC class instance, we must obey ObjC access rules.  */
2773   if (!objc_is_public (object, name))
2774     return error_mark_node;
2775 
2776   object_type = TREE_TYPE (object);
2777 
2778   if (processing_template_decl)
2779     {
2780       if (/* If OBJECT is dependent, so is OBJECT.NAME.  */
2781 	  type_dependent_object_expression_p (object)
2782 	  /* If NAME is "f<args>", where either 'f' or 'args' is
2783 	     dependent, then the expression is dependent.  */
2784 	  || (TREE_CODE (name) == TEMPLATE_ID_EXPR
2785 	      && dependent_template_id_p (TREE_OPERAND (name, 0),
2786 					  TREE_OPERAND (name, 1)))
2787 	  /* If NAME is "T::X" where "T" is dependent, then the
2788 	     expression is dependent.  */
2789 	  || (TREE_CODE (name) == SCOPE_REF
2790 	      && TYPE_P (TREE_OPERAND (name, 0))
2791 	      && dependent_scope_p (TREE_OPERAND (name, 0))))
2792 	{
2793 	dependent:
2794 	  return build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF,
2795 				   orig_object, orig_name, NULL_TREE);
2796 	}
2797       object = build_non_dependent_expr (object);
2798     }
2799   else if (c_dialect_objc ()
2800 	   && identifier_p (name)
2801 	   && (expr = objc_maybe_build_component_ref (object, name)))
2802     return expr;
2803 
2804   /* [expr.ref]
2805 
2806      The type of the first expression shall be "class object" (of a
2807      complete type).  */
2808   if (!currently_open_class (object_type)
2809       && !complete_type_or_maybe_complain (object_type, object, complain))
2810     return error_mark_node;
2811   if (!CLASS_TYPE_P (object_type))
2812     {
2813       if (complain & tf_error)
2814 	{
2815 	  if (POINTER_TYPE_P (object_type)
2816 	      && CLASS_TYPE_P (TREE_TYPE (object_type)))
2817 	    error ("request for member %qD in %qE, which is of pointer "
2818 		   "type %qT (maybe you meant to use %<->%> ?)",
2819 		   name, object.get_value (), object_type);
2820 	  else
2821 	    error ("request for member %qD in %qE, which is of non-class "
2822 		   "type %qT", name, object.get_value (), object_type);
2823 	}
2824       return error_mark_node;
2825     }
2826 
2827   if (BASELINK_P (name))
2828     /* A member function that has already been looked up.  */
2829     member = name;
2830   else
2831     {
2832       bool is_template_id = false;
2833       tree template_args = NULL_TREE;
2834       tree scope = NULL_TREE;
2835 
2836       access_path = object_type;
2837 
2838       if (TREE_CODE (name) == SCOPE_REF)
2839 	{
2840 	  /* A qualified name.  The qualifying class or namespace `S'
2841 	     has already been looked up; it is either a TYPE or a
2842 	     NAMESPACE_DECL.  */
2843 	  scope = TREE_OPERAND (name, 0);
2844 	  name = TREE_OPERAND (name, 1);
2845 
2846 	  /* If SCOPE is a namespace, then the qualified name does not
2847 	     name a member of OBJECT_TYPE.  */
2848 	  if (TREE_CODE (scope) == NAMESPACE_DECL)
2849 	    {
2850 	      if (complain & tf_error)
2851 		error ("%<%D::%D%> is not a member of %qT",
2852 		       scope, name, object_type);
2853 	      return error_mark_node;
2854 	    }
2855 	}
2856 
2857       if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
2858 	{
2859 	  is_template_id = true;
2860 	  template_args = TREE_OPERAND (name, 1);
2861 	  name = TREE_OPERAND (name, 0);
2862 
2863 	  if (!identifier_p (name))
2864 	    name = OVL_NAME (name);
2865 	}
2866 
2867       if (scope)
2868 	{
2869 	  if (TREE_CODE (scope) == ENUMERAL_TYPE)
2870 	    {
2871 	      gcc_assert (!is_template_id);
2872 	      /* Looking up a member enumerator (c++/56793).  */
2873 	      if (!TYPE_CLASS_SCOPE_P (scope)
2874 		  || !DERIVED_FROM_P (TYPE_CONTEXT (scope), object_type))
2875 		{
2876 		  if (complain & tf_error)
2877 		    error ("%<%D::%D%> is not a member of %qT",
2878 			   scope, name, object_type);
2879 		  return error_mark_node;
2880 		}
2881 	      tree val = lookup_enumerator (scope, name);
2882 	      if (!val)
2883 		{
2884 		  if (complain & tf_error)
2885 		    error ("%qD is not a member of %qD",
2886 			   name, scope);
2887 		  return error_mark_node;
2888 		}
2889 
2890 	      if (TREE_SIDE_EFFECTS (object))
2891 		val = build2 (COMPOUND_EXPR, TREE_TYPE (val), object, val);
2892 	      return val;
2893 	    }
2894 
2895 	  gcc_assert (CLASS_TYPE_P (scope));
2896 	  gcc_assert (identifier_p (name) || TREE_CODE (name) == BIT_NOT_EXPR);
2897 
2898 	  if (constructor_name_p (name, scope))
2899 	    {
2900 	      if (complain & tf_error)
2901 		error ("cannot call constructor %<%T::%D%> directly",
2902 		       scope, name);
2903 	      return error_mark_node;
2904 	    }
2905 
2906 	  /* Find the base of OBJECT_TYPE corresponding to SCOPE.  */
2907 	  access_path = lookup_base (object_type, scope, ba_check,
2908 				     NULL, complain);
2909 	  if (access_path == error_mark_node)
2910 	    return error_mark_node;
2911 	  if (!access_path)
2912 	    {
2913 	      if (any_dependent_bases_p (object_type))
2914 		goto dependent;
2915 	      if (complain & tf_error)
2916 		error ("%qT is not a base of %qT", scope, object_type);
2917 	      return error_mark_node;
2918 	    }
2919 	}
2920 
2921       if (TREE_CODE (name) == BIT_NOT_EXPR)
2922 	{
2923 	  if (dependent_type_p (object_type))
2924 	    /* The destructor isn't declared yet.  */
2925 	    goto dependent;
2926 	  member = lookup_destructor (object, scope, name, complain);
2927 	}
2928       else
2929 	{
2930 	  /* Look up the member.  */
2931 	  access_failure_info afi;
2932 	  member = lookup_member (access_path, name, /*protect=*/1,
2933 				  /*want_type=*/false, complain,
2934 				  &afi);
2935 	  afi.maybe_suggest_accessor (TYPE_READONLY (object_type));
2936 	  if (member == NULL_TREE)
2937 	    {
2938 	      if (dependent_type_p (object_type))
2939 		/* Try again at instantiation time.  */
2940 		goto dependent;
2941 	      if (complain & tf_error)
2942 		{
2943 		  tree guessed_id = lookup_member_fuzzy (access_path, name,
2944 							 /*want_type=*/false);
2945 		  if (guessed_id)
2946 		    {
2947 		      location_t bogus_component_loc = input_location;
2948 		      gcc_rich_location rich_loc (bogus_component_loc);
2949 		      rich_loc.add_fixit_misspelled_id (bogus_component_loc,
2950 							guessed_id);
2951 		      error_at (&rich_loc,
2952 				"%q#T has no member named %qE;"
2953 				" did you mean %qE?",
2954 				TREE_CODE (access_path) == TREE_BINFO
2955 				? TREE_TYPE (access_path) : object_type,
2956 				name, guessed_id);
2957 		    }
2958 		  else
2959 		    error ("%q#T has no member named %qE",
2960 			   TREE_CODE (access_path) == TREE_BINFO
2961 			   ? TREE_TYPE (access_path) : object_type, name);
2962 		}
2963 	      return error_mark_node;
2964 	    }
2965 	  if (member == error_mark_node)
2966 	    return error_mark_node;
2967 	  if (DECL_P (member)
2968 	      && any_dependent_type_attributes_p (DECL_ATTRIBUTES (member)))
2969 	    /* Dependent type attributes on the decl mean that the TREE_TYPE is
2970 	       wrong, so don't use it.  */
2971 	    goto dependent;
2972 	  if (TREE_CODE (member) == USING_DECL && DECL_DEPENDENT_P (member))
2973 	    goto dependent;
2974 	}
2975 
2976       if (is_template_id)
2977 	{
2978 	  tree templ = member;
2979 
2980 	  if (BASELINK_P (templ))
2981 	    member = lookup_template_function (templ, template_args);
2982 	  else if (variable_template_p (templ))
2983 	    member = (lookup_and_finish_template_variable
2984 		      (templ, template_args, complain));
2985 	  else
2986 	    {
2987 	      if (complain & tf_error)
2988 		error ("%qD is not a member template function", name);
2989 	      return error_mark_node;
2990 	    }
2991 	}
2992     }
2993 
2994   if (TREE_DEPRECATED (member))
2995     warn_deprecated_use (member, NULL_TREE);
2996 
2997   if (template_p)
2998     check_template_keyword (member);
2999 
3000   expr = build_class_member_access_expr (object, member, access_path,
3001 					 /*preserve_reference=*/false,
3002 					 complain);
3003   if (processing_template_decl && expr != error_mark_node)
3004     {
3005       if (BASELINK_P (member))
3006 	{
3007 	  if (TREE_CODE (orig_name) == SCOPE_REF)
3008 	    BASELINK_QUALIFIED_P (member) = 1;
3009 	  orig_name = member;
3010 	}
3011       return build_min_non_dep (COMPONENT_REF, expr,
3012 				orig_object, orig_name,
3013 				NULL_TREE);
3014     }
3015 
3016   return expr;
3017 }
3018 
3019 /* Build a COMPONENT_REF of OBJECT and MEMBER with the appropriate
3020    type.  */
3021 
3022 tree
3023 build_simple_component_ref (tree object, tree member)
3024 {
3025   tree type = cp_build_qualified_type (TREE_TYPE (member),
3026 				       cp_type_quals (TREE_TYPE (object)));
3027   return build3_loc (input_location,
3028 		     COMPONENT_REF, type,
3029 		     object, member, NULL_TREE);
3030 }
3031 
3032 /* Return an expression for the MEMBER_NAME field in the internal
3033    representation of PTRMEM, a pointer-to-member function.  (Each
3034    pointer-to-member function type gets its own RECORD_TYPE so it is
3035    more convenient to access the fields by name than by FIELD_DECL.)
3036    This routine converts the NAME to a FIELD_DECL and then creates the
3037    node for the complete expression.  */
3038 
3039 tree
3040 build_ptrmemfunc_access_expr (tree ptrmem, tree member_name)
3041 {
3042   tree ptrmem_type;
3043   tree member;
3044 
3045   if (TREE_CODE (ptrmem) == CONSTRUCTOR)
3046     {
3047       unsigned int ix;
3048       tree index, value;
3049       FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ptrmem),
3050 				ix, index, value)
3051 	if (index && DECL_P (index) && DECL_NAME (index) == member_name)
3052 	  return value;
3053       gcc_unreachable ();
3054     }
3055 
3056   /* This code is a stripped down version of
3057      build_class_member_access_expr.  It does not work to use that
3058      routine directly because it expects the object to be of class
3059      type.  */
3060   ptrmem_type = TREE_TYPE (ptrmem);
3061   gcc_assert (TYPE_PTRMEMFUNC_P (ptrmem_type));
3062   for (member = TYPE_FIELDS (ptrmem_type); member;
3063        member = DECL_CHAIN (member))
3064     if (DECL_NAME (member) == member_name)
3065       break;
3066   tree res = build_simple_component_ref (ptrmem, member);
3067 
3068   TREE_NO_WARNING (res) = 1;
3069   return res;
3070 }
3071 
3072 /* Given an expression PTR for a pointer, return an expression
3073    for the value pointed to.
3074    ERRORSTRING is the name of the operator to appear in error messages.
3075 
3076    This function may need to overload OPERATOR_FNNAME.
3077    Must also handle REFERENCE_TYPEs for C++.  */
3078 
3079 tree
3080 build_x_indirect_ref (location_t loc, tree expr, ref_operator errorstring,
3081                       tsubst_flags_t complain)
3082 {
3083   tree orig_expr = expr;
3084   tree rval;
3085   tree overload = NULL_TREE;
3086 
3087   if (processing_template_decl)
3088     {
3089       /* Retain the type if we know the operand is a pointer.  */
3090       if (TREE_TYPE (expr) && POINTER_TYPE_P (TREE_TYPE (expr)))
3091 	return build_min (INDIRECT_REF, TREE_TYPE (TREE_TYPE (expr)), expr);
3092       if (type_dependent_expression_p (expr))
3093 	return build_min_nt_loc (loc, INDIRECT_REF, expr);
3094       expr = build_non_dependent_expr (expr);
3095     }
3096 
3097   rval = build_new_op (loc, INDIRECT_REF, LOOKUP_NORMAL, expr,
3098 		       NULL_TREE, NULL_TREE, &overload, complain);
3099   if (!rval)
3100     rval = cp_build_indirect_ref (expr, errorstring, complain);
3101 
3102   if (processing_template_decl && rval != error_mark_node)
3103     {
3104       if (overload != NULL_TREE)
3105 	return (build_min_non_dep_op_overload
3106 		(INDIRECT_REF, rval, overload, orig_expr));
3107 
3108       return build_min_non_dep (INDIRECT_REF, rval, orig_expr);
3109     }
3110   else
3111     return rval;
3112 }
3113 
3114 /* The implementation of the above, and of indirection implied by other
3115    constructs.  If DO_FOLD is true, fold away INDIRECT_REF of ADDR_EXPR.  */
3116 
3117 static tree
3118 cp_build_indirect_ref_1 (tree ptr, ref_operator errorstring,
3119 			 tsubst_flags_t complain, bool do_fold)
3120 {
3121   tree pointer, type;
3122 
3123   /* RO_NULL should only be used with the folding entry points below, not
3124      cp_build_indirect_ref.  */
3125   gcc_checking_assert (errorstring != RO_NULL || do_fold);
3126 
3127   if (ptr == current_class_ptr
3128       || (TREE_CODE (ptr) == NOP_EXPR
3129 	  && TREE_OPERAND (ptr, 0) == current_class_ptr
3130 	  && (same_type_ignoring_top_level_qualifiers_p
3131 	      (TREE_TYPE (ptr), TREE_TYPE (current_class_ptr)))))
3132     return current_class_ref;
3133 
3134   pointer = (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
3135 	     ? ptr : decay_conversion (ptr, complain));
3136   if (pointer == error_mark_node)
3137     return error_mark_node;
3138 
3139   type = TREE_TYPE (pointer);
3140 
3141   if (POINTER_TYPE_P (type))
3142     {
3143       /* [expr.unary.op]
3144 
3145 	 If the type of the expression is "pointer to T," the type
3146 	 of  the  result  is  "T."  */
3147       tree t = TREE_TYPE (type);
3148 
3149       if ((CONVERT_EXPR_P (ptr)
3150 	   || TREE_CODE (ptr) == VIEW_CONVERT_EXPR)
3151 	  && (!CLASS_TYPE_P (t) || !CLASSTYPE_EMPTY_P (t)))
3152 	{
3153 	  /* If a warning is issued, mark it to avoid duplicates from
3154 	     the backend.  This only needs to be done at
3155 	     warn_strict_aliasing > 2.  */
3156 	  if (warn_strict_aliasing > 2)
3157 	    if (strict_aliasing_warning (EXPR_LOCATION (ptr),
3158 					 type, TREE_OPERAND (ptr, 0)))
3159 	      TREE_NO_WARNING (ptr) = 1;
3160 	}
3161 
3162       if (VOID_TYPE_P (t))
3163 	{
3164 	  /* A pointer to incomplete type (other than cv void) can be
3165 	     dereferenced [expr.unary.op]/1  */
3166           if (complain & tf_error)
3167             error ("%qT is not a pointer-to-object type", type);
3168 	  return error_mark_node;
3169 	}
3170       else if (do_fold && TREE_CODE (pointer) == ADDR_EXPR
3171 	       && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
3172 	/* The POINTER was something like `&x'.  We simplify `*&x' to
3173 	   `x'.  */
3174 	return TREE_OPERAND (pointer, 0);
3175       else
3176 	{
3177 	  tree ref = build1 (INDIRECT_REF, t, pointer);
3178 
3179 	  /* We *must* set TREE_READONLY when dereferencing a pointer to const,
3180 	     so that we get the proper error message if the result is used
3181 	     to assign to.  Also, &* is supposed to be a no-op.  */
3182 	  TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
3183 	  TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
3184 	  TREE_SIDE_EFFECTS (ref)
3185 	    = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer));
3186 	  return ref;
3187 	}
3188     }
3189   else if (!(complain & tf_error))
3190     /* Don't emit any errors; we'll just return ERROR_MARK_NODE later.  */
3191     ;
3192   /* `pointer' won't be an error_mark_node if we were given a
3193      pointer to member, so it's cool to check for this here.  */
3194   else if (TYPE_PTRMEM_P (type))
3195     switch (errorstring)
3196       {
3197          case RO_ARRAY_INDEXING:
3198            error ("invalid use of array indexing on pointer to member");
3199            break;
3200          case RO_UNARY_STAR:
3201            error ("invalid use of unary %<*%> on pointer to member");
3202            break;
3203          case RO_IMPLICIT_CONVERSION:
3204            error ("invalid use of implicit conversion on pointer to member");
3205            break;
3206          case RO_ARROW_STAR:
3207            error ("left hand operand of %<->*%> must be a pointer to class, "
3208 		  "but is a pointer to member of type %qT", type);
3209            break;
3210          default:
3211            gcc_unreachable ();
3212       }
3213   else if (pointer != error_mark_node)
3214     invalid_indirection_error (input_location, type, errorstring);
3215 
3216   return error_mark_node;
3217 }
3218 
3219 /* Entry point used by c-common, which expects folding.  */
3220 
3221 tree
3222 build_indirect_ref (location_t /*loc*/,
3223 		    tree ptr, ref_operator errorstring)
3224 {
3225   return cp_build_indirect_ref_1 (ptr, errorstring, tf_warning_or_error, true);
3226 }
3227 
3228 /* Entry point used by internal indirection needs that don't correspond to any
3229    syntactic construct.  */
3230 
3231 tree
3232 cp_build_fold_indirect_ref (tree pointer)
3233 {
3234   return cp_build_indirect_ref_1 (pointer, RO_NULL, tf_warning_or_error, true);
3235 }
3236 
3237 /* Entry point used by indirection needs that correspond to some syntactic
3238    construct.  */
3239 
3240 tree
3241 cp_build_indirect_ref (tree ptr, ref_operator errorstring,
3242 		       tsubst_flags_t complain)
3243 {
3244   return cp_build_indirect_ref_1 (ptr, errorstring, complain, false);
3245 }
3246 
3247 /* This handles expressions of the form "a[i]", which denotes
3248    an array reference.
3249 
3250    This is logically equivalent in C to *(a+i), but we may do it differently.
3251    If A is a variable or a member, we generate a primitive ARRAY_REF.
3252    This avoids forcing the array out of registers, and can work on
3253    arrays that are not lvalues (for example, members of structures returned
3254    by functions).
3255 
3256    If INDEX is of some user-defined type, it must be converted to
3257    integer type.  Otherwise, to make a compatible PLUS_EXPR, it
3258    will inherit the type of the array, which will be some pointer type.
3259 
3260    LOC is the location to use in building the array reference.  */
3261 
3262 tree
3263 cp_build_array_ref (location_t loc, tree array, tree idx,
3264 		    tsubst_flags_t complain)
3265 {
3266   tree ret;
3267 
3268   if (idx == 0)
3269     {
3270       if (complain & tf_error)
3271 	error_at (loc, "subscript missing in array reference");
3272       return error_mark_node;
3273     }
3274 
3275   if (TREE_TYPE (array) == error_mark_node
3276       || TREE_TYPE (idx) == error_mark_node)
3277     return error_mark_node;
3278 
3279   /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
3280      inside it.  */
3281   switch (TREE_CODE (array))
3282     {
3283     case COMPOUND_EXPR:
3284       {
3285 	tree value = cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
3286 					 complain);
3287 	ret = build2 (COMPOUND_EXPR, TREE_TYPE (value),
3288 		      TREE_OPERAND (array, 0), value);
3289 	SET_EXPR_LOCATION (ret, loc);
3290 	return ret;
3291       }
3292 
3293     case COND_EXPR:
3294       ret = build_conditional_expr
3295 	       (loc, TREE_OPERAND (array, 0),
3296 	       cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
3297 				   complain),
3298 	       cp_build_array_ref (loc, TREE_OPERAND (array, 2), idx,
3299 				   complain),
3300 	       complain);
3301       protected_set_expr_location (ret, loc);
3302       return ret;
3303 
3304     default:
3305       break;
3306     }
3307 
3308   bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, idx);
3309 
3310   if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
3311     {
3312       tree rval, type;
3313 
3314       warn_array_subscript_with_type_char (loc, idx);
3315 
3316       if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (idx)))
3317 	{
3318 	  if (complain & tf_error)
3319 	    error_at (loc, "array subscript is not an integer");
3320 	  return error_mark_node;
3321 	}
3322 
3323       /* Apply integral promotions *after* noticing character types.
3324 	 (It is unclear why we do these promotions -- the standard
3325 	 does not say that we should.  In fact, the natural thing would
3326 	 seem to be to convert IDX to ptrdiff_t; we're performing
3327 	 pointer arithmetic.)  */
3328       idx = cp_perform_integral_promotions (idx, complain);
3329 
3330       /* An array that is indexed by a non-constant
3331 	 cannot be stored in a register; we must be able to do
3332 	 address arithmetic on its address.
3333 	 Likewise an array of elements of variable size.  */
3334       if (TREE_CODE (idx) != INTEGER_CST
3335 	  || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
3336 	      && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
3337 		  != INTEGER_CST)))
3338 	{
3339 	  if (!cxx_mark_addressable (array, true))
3340 	    return error_mark_node;
3341 	}
3342 
3343       /* An array that is indexed by a constant value which is not within
3344 	 the array bounds cannot be stored in a register either; because we
3345 	 would get a crash in store_bit_field/extract_bit_field when trying
3346 	 to access a non-existent part of the register.  */
3347       if (TREE_CODE (idx) == INTEGER_CST
3348 	  && TYPE_DOMAIN (TREE_TYPE (array))
3349 	  && ! int_fits_type_p (idx, TYPE_DOMAIN (TREE_TYPE (array))))
3350 	{
3351 	  if (!cxx_mark_addressable (array))
3352 	    return error_mark_node;
3353 	}
3354 
3355       /* Note in C++ it is valid to subscript a `register' array, since
3356 	 it is valid to take the address of something with that
3357 	 storage specification.  */
3358       if (extra_warnings)
3359 	{
3360 	  tree foo = array;
3361 	  while (TREE_CODE (foo) == COMPONENT_REF)
3362 	    foo = TREE_OPERAND (foo, 0);
3363 	  if (VAR_P (foo) && DECL_REGISTER (foo)
3364 	      && (complain & tf_warning))
3365 	    warning_at (loc, OPT_Wextra,
3366 			"subscripting array declared %<register%>");
3367 	}
3368 
3369       type = TREE_TYPE (TREE_TYPE (array));
3370       rval = build4 (ARRAY_REF, type, array, idx, NULL_TREE, NULL_TREE);
3371       /* Array ref is const/volatile if the array elements are
3372 	 or if the array is..  */
3373       TREE_READONLY (rval)
3374 	|= (CP_TYPE_CONST_P (type) | TREE_READONLY (array));
3375       TREE_SIDE_EFFECTS (rval)
3376 	|= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array));
3377       TREE_THIS_VOLATILE (rval)
3378 	|= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array));
3379       ret = require_complete_type_sfinae (rval, complain);
3380       protected_set_expr_location (ret, loc);
3381       if (non_lvalue)
3382 	ret = non_lvalue_loc (loc, ret);
3383       return ret;
3384     }
3385 
3386   {
3387     tree ar = cp_default_conversion (array, complain);
3388     tree ind = cp_default_conversion (idx, complain);
3389 
3390     /* Put the integer in IND to simplify error checking.  */
3391     if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
3392       std::swap (ar, ind);
3393 
3394     if (ar == error_mark_node || ind == error_mark_node)
3395       return error_mark_node;
3396 
3397     if (!TYPE_PTR_P (TREE_TYPE (ar)))
3398       {
3399 	if (complain & tf_error)
3400 	  error_at (loc, "subscripted value is neither array nor pointer");
3401 	return error_mark_node;
3402       }
3403     if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
3404       {
3405 	if (complain & tf_error)
3406 	  error_at (loc, "array subscript is not an integer");
3407 	return error_mark_node;
3408       }
3409 
3410     warn_array_subscript_with_type_char (loc, idx);
3411 
3412     ret = cp_build_indirect_ref (cp_build_binary_op (input_location,
3413 						     PLUS_EXPR, ar, ind,
3414 						     complain),
3415                                  RO_ARRAY_INDEXING,
3416                                  complain);
3417     protected_set_expr_location (ret, loc);
3418     if (non_lvalue)
3419       ret = non_lvalue_loc (loc, ret);
3420     return ret;
3421   }
3422 }
3423 
3424 /* Entry point for Obj-C++.  */
3425 
3426 tree
3427 build_array_ref (location_t loc, tree array, tree idx)
3428 {
3429   return cp_build_array_ref (loc, array, idx, tf_warning_or_error);
3430 }
3431 
3432 /* Resolve a pointer to member function.  INSTANCE is the object
3433    instance to use, if the member points to a virtual member.
3434 
3435    This used to avoid checking for virtual functions if basetype
3436    has no virtual functions, according to an earlier ANSI draft.
3437    With the final ISO C++ rules, such an optimization is
3438    incorrect: A pointer to a derived member can be static_cast
3439    to pointer-to-base-member, as long as the dynamic object
3440    later has the right member.  So now we only do this optimization
3441    when we know the dynamic type of the object.  */
3442 
3443 tree
3444 get_member_function_from_ptrfunc (tree *instance_ptrptr, tree function,
3445 				  tsubst_flags_t complain)
3446 {
3447   if (TREE_CODE (function) == OFFSET_REF)
3448     function = TREE_OPERAND (function, 1);
3449 
3450   if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
3451     {
3452       tree idx, delta, e1, e2, e3, vtbl;
3453       bool nonvirtual;
3454       tree fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
3455       tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
3456 
3457       tree instance_ptr = *instance_ptrptr;
3458       tree instance_save_expr = 0;
3459       if (instance_ptr == error_mark_node)
3460 	{
3461 	  if (TREE_CODE (function) == PTRMEM_CST)
3462 	    {
3463 	      /* Extracting the function address from a pmf is only
3464 		 allowed with -Wno-pmf-conversions. It only works for
3465 		 pmf constants.  */
3466 	      e1 = build_addr_func (PTRMEM_CST_MEMBER (function), complain);
3467 	      e1 = convert (fntype, e1);
3468 	      return e1;
3469 	    }
3470 	  else
3471 	    {
3472 	      if (complain & tf_error)
3473 		error ("object missing in use of %qE", function);
3474 	      return error_mark_node;
3475 	    }
3476 	}
3477 
3478       /* True if we know that the dynamic type of the object doesn't have
3479 	 virtual functions, so we can assume the PFN field is a pointer.  */
3480       nonvirtual = (COMPLETE_TYPE_P (basetype)
3481 		    && !TYPE_POLYMORPHIC_P (basetype)
3482 		    && resolves_to_fixed_type_p (instance_ptr, 0));
3483 
3484       /* If we don't really have an object (i.e. in an ill-formed
3485 	 conversion from PMF to pointer), we can't resolve virtual
3486 	 functions anyway.  */
3487       if (!nonvirtual && is_dummy_object (instance_ptr))
3488 	nonvirtual = true;
3489 
3490       if (TREE_SIDE_EFFECTS (instance_ptr))
3491 	instance_ptr = instance_save_expr = save_expr (instance_ptr);
3492 
3493       if (TREE_SIDE_EFFECTS (function))
3494 	function = save_expr (function);
3495 
3496       /* Start by extracting all the information from the PMF itself.  */
3497       e3 = pfn_from_ptrmemfunc (function);
3498       delta = delta_from_ptrmemfunc (function);
3499       idx = build1 (NOP_EXPR, vtable_index_type, e3);
3500       switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
3501 	{
3502 	  int flag_sanitize_save;
3503 	case ptrmemfunc_vbit_in_pfn:
3504 	  e1 = cp_build_binary_op (input_location,
3505 				   BIT_AND_EXPR, idx, integer_one_node,
3506 				   complain);
3507 	  idx = cp_build_binary_op (input_location,
3508 				    MINUS_EXPR, idx, integer_one_node,
3509 				    complain);
3510 	  if (idx == error_mark_node)
3511 	    return error_mark_node;
3512 	  break;
3513 
3514 	case ptrmemfunc_vbit_in_delta:
3515 	  e1 = cp_build_binary_op (input_location,
3516 				   BIT_AND_EXPR, delta, integer_one_node,
3517 				   complain);
3518 	  /* Don't instrument the RSHIFT_EXPR we're about to create because
3519 	     we're going to use DELTA number of times, and that wouldn't play
3520 	     well with SAVE_EXPRs therein.  */
3521 	  flag_sanitize_save = flag_sanitize;
3522 	  flag_sanitize = 0;
3523 	  delta = cp_build_binary_op (input_location,
3524 				      RSHIFT_EXPR, delta, integer_one_node,
3525 				      complain);
3526 	  flag_sanitize = flag_sanitize_save;
3527 	  if (delta == error_mark_node)
3528 	    return error_mark_node;
3529 	  break;
3530 
3531 	default:
3532 	  gcc_unreachable ();
3533 	}
3534 
3535       if (e1 == error_mark_node)
3536 	return error_mark_node;
3537 
3538       /* Convert down to the right base before using the instance.  A
3539 	 special case is that in a pointer to member of class C, C may
3540 	 be incomplete.  In that case, the function will of course be
3541 	 a member of C, and no conversion is required.  In fact,
3542 	 lookup_base will fail in that case, because incomplete
3543 	 classes do not have BINFOs.  */
3544       if (!same_type_ignoring_top_level_qualifiers_p
3545 	  (basetype, TREE_TYPE (TREE_TYPE (instance_ptr))))
3546 	{
3547 	  basetype = lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr)),
3548 				  basetype, ba_check, NULL, complain);
3549 	  instance_ptr = build_base_path (PLUS_EXPR, instance_ptr, basetype,
3550 					  1, complain);
3551 	  if (instance_ptr == error_mark_node)
3552 	    return error_mark_node;
3553 	}
3554       /* ...and then the delta in the PMF.  */
3555       instance_ptr = fold_build_pointer_plus (instance_ptr, delta);
3556 
3557       /* Hand back the adjusted 'this' argument to our caller.  */
3558       *instance_ptrptr = instance_ptr;
3559 
3560       if (nonvirtual)
3561 	/* Now just return the pointer.  */
3562 	return e3;
3563 
3564       /* Next extract the vtable pointer from the object.  */
3565       vtbl = build1 (NOP_EXPR, build_pointer_type (vtbl_ptr_type_node),
3566 		     instance_ptr);
3567       vtbl = cp_build_fold_indirect_ref (vtbl);
3568       if (vtbl == error_mark_node)
3569 	return error_mark_node;
3570 
3571       /* Finally, extract the function pointer from the vtable.  */
3572       e2 = fold_build_pointer_plus_loc (input_location, vtbl, idx);
3573       e2 = cp_build_fold_indirect_ref (e2);
3574       if (e2 == error_mark_node)
3575 	return error_mark_node;
3576       TREE_CONSTANT (e2) = 1;
3577 
3578       /* When using function descriptors, the address of the
3579 	 vtable entry is treated as a function pointer.  */
3580       if (TARGET_VTABLE_USES_DESCRIPTORS)
3581 	e2 = build1 (NOP_EXPR, TREE_TYPE (e2),
3582 		     cp_build_addr_expr (e2, complain));
3583 
3584       e2 = fold_convert (TREE_TYPE (e3), e2);
3585       e1 = build_conditional_expr (input_location, e1, e2, e3, complain);
3586       if (e1 == error_mark_node)
3587 	return error_mark_node;
3588 
3589       /* Make sure this doesn't get evaluated first inside one of the
3590 	 branches of the COND_EXPR.  */
3591       if (instance_save_expr)
3592 	e1 = build2 (COMPOUND_EXPR, TREE_TYPE (e1),
3593 		     instance_save_expr, e1);
3594 
3595       function = e1;
3596     }
3597   return function;
3598 }
3599 
3600 /* Used by the C-common bits.  */
3601 tree
3602 build_function_call (location_t /*loc*/,
3603 		     tree function, tree params)
3604 {
3605   return cp_build_function_call (function, params, tf_warning_or_error);
3606 }
3607 
3608 /* Used by the C-common bits.  */
3609 tree
3610 build_function_call_vec (location_t /*loc*/, vec<location_t> /*arg_loc*/,
3611 			 tree function, vec<tree, va_gc> *params,
3612 			 vec<tree, va_gc> * /*origtypes*/)
3613 {
3614   vec<tree, va_gc> *orig_params = params;
3615   tree ret = cp_build_function_call_vec (function, &params,
3616 					 tf_warning_or_error);
3617 
3618   /* cp_build_function_call_vec can reallocate PARAMS by adding
3619      default arguments.  That should never happen here.  Verify
3620      that.  */
3621   gcc_assert (params == orig_params);
3622 
3623   return ret;
3624 }
3625 
3626 /* Build a function call using a tree list of arguments.  */
3627 
3628 static tree
3629 cp_build_function_call (tree function, tree params, tsubst_flags_t complain)
3630 {
3631   vec<tree, va_gc> *vec;
3632   tree ret;
3633 
3634   vec = make_tree_vector ();
3635   for (; params != NULL_TREE; params = TREE_CHAIN (params))
3636     vec_safe_push (vec, TREE_VALUE (params));
3637   ret = cp_build_function_call_vec (function, &vec, complain);
3638   release_tree_vector (vec);
3639   return ret;
3640 }
3641 
3642 /* Build a function call using varargs.  */
3643 
3644 tree
3645 cp_build_function_call_nary (tree function, tsubst_flags_t complain, ...)
3646 {
3647   vec<tree, va_gc> *vec;
3648   va_list args;
3649   tree ret, t;
3650 
3651   vec = make_tree_vector ();
3652   va_start (args, complain);
3653   for (t = va_arg (args, tree); t != NULL_TREE; t = va_arg (args, tree))
3654     vec_safe_push (vec, t);
3655   va_end (args);
3656   ret = cp_build_function_call_vec (function, &vec, complain);
3657   release_tree_vector (vec);
3658   return ret;
3659 }
3660 
3661 /* Build a function call using a vector of arguments.  PARAMS may be
3662    NULL if there are no parameters.  This changes the contents of
3663    PARAMS.  */
3664 
3665 tree
3666 cp_build_function_call_vec (tree function, vec<tree, va_gc> **params,
3667 			    tsubst_flags_t complain)
3668 {
3669   tree fntype, fndecl;
3670   int is_method;
3671   tree original = function;
3672   int nargs;
3673   tree *argarray;
3674   tree parm_types;
3675   vec<tree, va_gc> *allocated = NULL;
3676   tree ret;
3677 
3678   /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3679      expressions, like those used for ObjC messenger dispatches.  */
3680   if (params != NULL && !vec_safe_is_empty (*params))
3681     function = objc_rewrite_function_call (function, (**params)[0]);
3682 
3683   /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3684      Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context.  */
3685   if (TREE_CODE (function) == NOP_EXPR
3686       && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
3687     function = TREE_OPERAND (function, 0);
3688 
3689   if (TREE_CODE (function) == FUNCTION_DECL)
3690     {
3691       /* If the function is a non-template member function
3692          or a non-template friend, then we need to check the
3693          constraints.
3694 
3695         Note that if overload resolution failed with a single
3696         candidate this function will be used to explicitly diagnose
3697         the failure for the single call expression. The check is
3698         technically redundant since we also would have failed in
3699         add_function_candidate. */
3700       if (flag_concepts
3701           && (complain & tf_error)
3702           && !constraints_satisfied_p (function))
3703         {
3704           error ("cannot call function %qD", function);
3705           location_t loc = DECL_SOURCE_LOCATION (function);
3706           diagnose_constraints (loc, function, NULL_TREE);
3707           return error_mark_node;
3708         }
3709 
3710       if (!mark_used (function, complain) && !(complain & tf_error))
3711 	return error_mark_node;
3712       fndecl = function;
3713 
3714       /* Convert anything with function type to a pointer-to-function.  */
3715       if (DECL_MAIN_P (function))
3716 	{
3717 	  if (complain & tf_error)
3718 	    pedwarn (input_location, OPT_Wpedantic,
3719 		     "ISO C++ forbids calling %<::main%> from within program");
3720 	  else
3721 	    return error_mark_node;
3722 	}
3723       function = build_addr_func (function, complain);
3724     }
3725   else
3726     {
3727       fndecl = NULL_TREE;
3728 
3729       function = build_addr_func (function, complain);
3730     }
3731 
3732   if (function == error_mark_node)
3733     return error_mark_node;
3734 
3735   fntype = TREE_TYPE (function);
3736 
3737   if (TYPE_PTRMEMFUNC_P (fntype))
3738     {
3739       if (complain & tf_error)
3740 	error ("must use %<.*%> or %<->*%> to call pointer-to-member "
3741 	       "function in %<%E (...)%>, e.g. %<(... ->* %E) (...)%>",
3742 	       original, original);
3743       return error_mark_node;
3744     }
3745 
3746   is_method = (TYPE_PTR_P (fntype)
3747 	       && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
3748 
3749   if (!(TYPE_PTRFN_P (fntype)
3750 	|| is_method
3751 	|| TREE_CODE (function) == TEMPLATE_ID_EXPR))
3752     {
3753       if (complain & tf_error)
3754 	{
3755 	  if (!flag_diagnostics_show_caret)
3756 	    error_at (input_location,
3757 		      "%qE cannot be used as a function", original);
3758 	  else if (DECL_P (original))
3759 	    error_at (input_location,
3760 		      "%qD cannot be used as a function", original);
3761 	  else
3762 	    error_at (input_location,
3763 		      "expression cannot be used as a function");
3764 	}
3765 
3766       return error_mark_node;
3767     }
3768 
3769   /* fntype now gets the type of function pointed to.  */
3770   fntype = TREE_TYPE (fntype);
3771   parm_types = TYPE_ARG_TYPES (fntype);
3772 
3773   if (params == NULL)
3774     {
3775       allocated = make_tree_vector ();
3776       params = &allocated;
3777     }
3778 
3779     nargs = convert_arguments (parm_types, params, fndecl, LOOKUP_NORMAL,
3780 			       complain);
3781   if (nargs < 0)
3782     return error_mark_node;
3783 
3784   argarray = (*params)->address ();
3785 
3786   /* Check for errors in format strings and inappropriately
3787      null parameters.  */
3788   bool warned_p = check_function_arguments (input_location, fndecl, fntype,
3789 					    nargs, argarray, NULL);
3790 
3791   ret = build_cxx_call (function, nargs, argarray, complain);
3792 
3793   if (warned_p)
3794     {
3795       tree c = extract_call_expr (ret);
3796       if (TREE_CODE (c) == CALL_EXPR)
3797 	TREE_NO_WARNING (c) = 1;
3798     }
3799 
3800   if (allocated != NULL)
3801     release_tree_vector (allocated);
3802 
3803   return ret;
3804 }
3805 
3806 /* Subroutine of convert_arguments.
3807    Print an error message about a wrong number of arguments.  */
3808 
3809 static void
3810 error_args_num (location_t loc, tree fndecl, bool too_many_p)
3811 {
3812   if (fndecl)
3813     {
3814       if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
3815 	{
3816 	  if (DECL_NAME (fndecl) == NULL_TREE
3817 	      || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl)))
3818 	    error_at (loc,
3819 		      too_many_p
3820 		      ? G_("too many arguments to constructor %q#D")
3821 		      : G_("too few arguments to constructor %q#D"),
3822 		      fndecl);
3823 	  else
3824 	    error_at (loc,
3825 		      too_many_p
3826 		      ? G_("too many arguments to member function %q#D")
3827 		      : G_("too few arguments to member function %q#D"),
3828 		      fndecl);
3829 	}
3830       else
3831 	error_at (loc,
3832 		  too_many_p
3833 		  ? G_("too many arguments to function %q#D")
3834 		  : G_("too few arguments to function %q#D"),
3835 		  fndecl);
3836       if (!DECL_IS_BUILTIN (fndecl))
3837 	inform (DECL_SOURCE_LOCATION (fndecl), "declared here");
3838     }
3839   else
3840     {
3841       if (c_dialect_objc ()  &&  objc_message_selector ())
3842 	error_at (loc,
3843 		  too_many_p
3844 		  ? G_("too many arguments to method %q#D")
3845 		  : G_("too few arguments to method %q#D"),
3846 		  objc_message_selector ());
3847       else
3848 	error_at (loc, too_many_p ? G_("too many arguments to function")
3849 		                  : G_("too few arguments to function"));
3850     }
3851 }
3852 
3853 /* Convert the actual parameter expressions in the list VALUES to the
3854    types in the list TYPELIST.  The converted expressions are stored
3855    back in the VALUES vector.
3856    If parmdecls is exhausted, or when an element has NULL as its type,
3857    perform the default conversions.
3858 
3859    NAME is an IDENTIFIER_NODE or 0.  It is used only for error messages.
3860 
3861    This is also where warnings about wrong number of args are generated.
3862 
3863    Returns the actual number of arguments processed (which might be less
3864    than the length of the vector), or -1 on error.
3865 
3866    In C++, unspecified trailing parameters can be filled in with their
3867    default arguments, if such were specified.  Do so here.  */
3868 
3869 static int
3870 convert_arguments (tree typelist, vec<tree, va_gc> **values, tree fndecl,
3871 		   int flags, tsubst_flags_t complain)
3872 {
3873   tree typetail;
3874   unsigned int i;
3875 
3876   /* Argument passing is always copy-initialization.  */
3877   flags |= LOOKUP_ONLYCONVERTING;
3878 
3879   for (i = 0, typetail = typelist;
3880        i < vec_safe_length (*values);
3881        i++)
3882     {
3883       tree type = typetail ? TREE_VALUE (typetail) : 0;
3884       tree val = (**values)[i];
3885 
3886       if (val == error_mark_node || type == error_mark_node)
3887 	return -1;
3888 
3889       if (type == void_type_node)
3890 	{
3891           if (complain & tf_error)
3892             {
3893 	      error_args_num (input_location, fndecl, /*too_many_p=*/true);
3894               return i;
3895             }
3896           else
3897             return -1;
3898 	}
3899 
3900       /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3901 	 Strip such NOP_EXPRs, since VAL is used in non-lvalue context.  */
3902       if (TREE_CODE (val) == NOP_EXPR
3903 	  && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
3904 	  && (type == 0 || TREE_CODE (type) != REFERENCE_TYPE))
3905 	val = TREE_OPERAND (val, 0);
3906 
3907       if (type == 0 || TREE_CODE (type) != REFERENCE_TYPE)
3908 	{
3909 	  if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
3910 	      || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE
3911 	      || TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
3912 	    val = decay_conversion (val, complain);
3913 	}
3914 
3915       if (val == error_mark_node)
3916 	return -1;
3917 
3918       if (type != 0)
3919 	{
3920 	  /* Formal parm type is specified by a function prototype.  */
3921 	  tree parmval;
3922 
3923 	  if (!COMPLETE_TYPE_P (complete_type (type)))
3924 	    {
3925               if (complain & tf_error)
3926                 {
3927                   if (fndecl)
3928                     error ("parameter %P of %qD has incomplete type %qT",
3929                            i, fndecl, type);
3930                   else
3931                     error ("parameter %P has incomplete type %qT", i, type);
3932                 }
3933 	      parmval = error_mark_node;
3934 	    }
3935 	  else
3936 	    {
3937 	      parmval = convert_for_initialization
3938 		(NULL_TREE, type, val, flags,
3939 		 ICR_ARGPASS, fndecl, i, complain);
3940 	      parmval = convert_for_arg_passing (type, parmval, complain);
3941 	    }
3942 
3943 	  if (parmval == error_mark_node)
3944 	    return -1;
3945 
3946 	  (**values)[i] = parmval;
3947 	}
3948       else
3949 	{
3950 	  if (fndecl && magic_varargs_p (fndecl))
3951 	    /* Don't do ellipsis conversion for __built_in_constant_p
3952 	       as this will result in spurious errors for non-trivial
3953 	       types.  */
3954 	    val = require_complete_type_sfinae (val, complain);
3955 	  else
3956 	    val = convert_arg_to_ellipsis (val, complain);
3957 
3958 	  (**values)[i] = val;
3959 	}
3960 
3961       if (typetail)
3962 	typetail = TREE_CHAIN (typetail);
3963     }
3964 
3965   if (typetail != 0 && typetail != void_list_node)
3966     {
3967       /* See if there are default arguments that can be used.  Because
3968 	 we hold default arguments in the FUNCTION_TYPE (which is so
3969 	 wrong), we can see default parameters here from deduced
3970 	 contexts (and via typeof) for indirect function calls.
3971 	 Fortunately we know whether we have a function decl to
3972 	 provide default arguments in a language conformant
3973 	 manner.  */
3974       if (fndecl && TREE_PURPOSE (typetail)
3975 	  && TREE_CODE (TREE_PURPOSE (typetail)) != DEFAULT_ARG)
3976 	{
3977 	  for (; typetail != void_list_node; ++i)
3978 	    {
3979 	      /* After DR777, with explicit template args we can end up with a
3980 		 default argument followed by no default argument.  */
3981 	      if (!TREE_PURPOSE (typetail))
3982 		break;
3983 	      tree parmval
3984 		= convert_default_arg (TREE_VALUE (typetail),
3985 				       TREE_PURPOSE (typetail),
3986 				       fndecl, i, complain);
3987 
3988 	      if (parmval == error_mark_node)
3989 		return -1;
3990 
3991 	      vec_safe_push (*values, parmval);
3992 	      typetail = TREE_CHAIN (typetail);
3993 	      /* ends with `...'.  */
3994 	      if (typetail == NULL_TREE)
3995 		break;
3996 	    }
3997 	}
3998 
3999       if (typetail && typetail != void_list_node)
4000 	{
4001 	  if (complain & tf_error)
4002 	    error_args_num (input_location, fndecl, /*too_many_p=*/false);
4003 	  return -1;
4004 	}
4005     }
4006 
4007   return (int) i;
4008 }
4009 
4010 /* Build a binary-operation expression, after performing default
4011    conversions on the operands.  CODE is the kind of expression to
4012    build.  ARG1 and ARG2 are the arguments.  ARG1_CODE and ARG2_CODE
4013    are the tree codes which correspond to ARG1 and ARG2 when issuing
4014    warnings about possibly misplaced parentheses.  They may differ
4015    from the TREE_CODE of ARG1 and ARG2 if the parser has done constant
4016    folding (e.g., if the parser sees "a | 1 + 1", it may call this
4017    routine with ARG2 being an INTEGER_CST and ARG2_CODE == PLUS_EXPR).
4018    To avoid issuing any parentheses warnings, pass ARG1_CODE and/or
4019    ARG2_CODE as ERROR_MARK.  */
4020 
4021 tree
4022 build_x_binary_op (location_t loc, enum tree_code code, tree arg1,
4023 		   enum tree_code arg1_code, tree arg2,
4024 		   enum tree_code arg2_code, tree *overload_p,
4025 		   tsubst_flags_t complain)
4026 {
4027   tree orig_arg1;
4028   tree orig_arg2;
4029   tree expr;
4030   tree overload = NULL_TREE;
4031 
4032   orig_arg1 = arg1;
4033   orig_arg2 = arg2;
4034 
4035   if (processing_template_decl)
4036     {
4037       if (type_dependent_expression_p (arg1)
4038 	  || type_dependent_expression_p (arg2))
4039 	return build_min_nt_loc (loc, code, arg1, arg2);
4040       arg1 = build_non_dependent_expr (arg1);
4041       arg2 = build_non_dependent_expr (arg2);
4042     }
4043 
4044   if (code == DOTSTAR_EXPR)
4045     expr = build_m_component_ref (arg1, arg2, complain);
4046   else
4047     expr = build_new_op (loc, code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE,
4048 			 &overload, complain);
4049 
4050   if (overload_p != NULL)
4051     *overload_p = overload;
4052 
4053   /* Check for cases such as x+y<<z which users are likely to
4054      misinterpret.  But don't warn about obj << x + y, since that is a
4055      common idiom for I/O.  */
4056   if (warn_parentheses
4057       && (complain & tf_warning)
4058       && !processing_template_decl
4059       && !error_operand_p (arg1)
4060       && !error_operand_p (arg2)
4061       && (code != LSHIFT_EXPR
4062 	  || !CLASS_TYPE_P (TREE_TYPE (arg1))))
4063     warn_about_parentheses (loc, code, arg1_code, orig_arg1,
4064 			    arg2_code, orig_arg2);
4065 
4066   if (processing_template_decl && expr != error_mark_node)
4067     {
4068       if (overload != NULL_TREE)
4069 	return (build_min_non_dep_op_overload
4070 		(code, expr, overload, orig_arg1, orig_arg2));
4071 
4072       return build_min_non_dep (code, expr, orig_arg1, orig_arg2);
4073     }
4074 
4075   return expr;
4076 }
4077 
4078 /* Build and return an ARRAY_REF expression.  */
4079 
4080 tree
4081 build_x_array_ref (location_t loc, tree arg1, tree arg2,
4082 		   tsubst_flags_t complain)
4083 {
4084   tree orig_arg1 = arg1;
4085   tree orig_arg2 = arg2;
4086   tree expr;
4087   tree overload = NULL_TREE;
4088 
4089   if (processing_template_decl)
4090     {
4091       if (type_dependent_expression_p (arg1)
4092 	  || type_dependent_expression_p (arg2))
4093 	return build_min_nt_loc (loc, ARRAY_REF, arg1, arg2,
4094 				 NULL_TREE, NULL_TREE);
4095       arg1 = build_non_dependent_expr (arg1);
4096       arg2 = build_non_dependent_expr (arg2);
4097     }
4098 
4099   expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, arg1, arg2,
4100 		       NULL_TREE, &overload, complain);
4101 
4102   if (processing_template_decl && expr != error_mark_node)
4103     {
4104       if (overload != NULL_TREE)
4105 	return (build_min_non_dep_op_overload
4106 		(ARRAY_REF, expr, overload, orig_arg1, orig_arg2));
4107 
4108       return build_min_non_dep (ARRAY_REF, expr, orig_arg1, orig_arg2,
4109 				NULL_TREE, NULL_TREE);
4110     }
4111   return expr;
4112 }
4113 
4114 /* Return whether OP is an expression of enum type cast to integer
4115    type.  In C++ even unsigned enum types are cast to signed integer
4116    types.  We do not want to issue warnings about comparisons between
4117    signed and unsigned types when one of the types is an enum type.
4118    Those warnings are always false positives in practice.  */
4119 
4120 static bool
4121 enum_cast_to_int (tree op)
4122 {
4123   if (CONVERT_EXPR_P (op)
4124       && TREE_TYPE (op) == integer_type_node
4125       && TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == ENUMERAL_TYPE
4126       && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 0))))
4127     return true;
4128 
4129   /* The cast may have been pushed into a COND_EXPR.  */
4130   if (TREE_CODE (op) == COND_EXPR)
4131     return (enum_cast_to_int (TREE_OPERAND (op, 1))
4132 	    || enum_cast_to_int (TREE_OPERAND (op, 2)));
4133 
4134   return false;
4135 }
4136 
4137 /* For the c-common bits.  */
4138 tree
4139 build_binary_op (location_t location, enum tree_code code, tree op0, tree op1,
4140 		 bool /*convert_p*/)
4141 {
4142   return cp_build_binary_op (location, code, op0, op1, tf_warning_or_error);
4143 }
4144 
4145 /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
4146    into a value of TYPE type.  Comparison is done via VEC_COND_EXPR.  */
4147 
4148 static tree
4149 build_vec_cmp (tree_code code, tree type,
4150 	       tree arg0, tree arg1)
4151 {
4152   tree zero_vec = build_zero_cst (type);
4153   tree minus_one_vec = build_minus_one_cst (type);
4154   tree cmp_type = build_same_sized_truth_vector_type(type);
4155   tree cmp = build2 (code, cmp_type, arg0, arg1);
4156   return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
4157 }
4158 
4159 /* Possibly warn about an address never being NULL.  */
4160 
4161 static void
4162 warn_for_null_address (location_t location, tree op, tsubst_flags_t complain)
4163 {
4164   if (!warn_address
4165       || (complain & tf_warning) == 0
4166       || c_inhibit_evaluation_warnings != 0
4167       || TREE_NO_WARNING (op))
4168     return;
4169 
4170   tree cop = fold_non_dependent_expr (op);
4171 
4172   if (TREE_CODE (cop) == ADDR_EXPR
4173       && decl_with_nonnull_addr_p (TREE_OPERAND (cop, 0))
4174       && !TREE_NO_WARNING (cop))
4175     warning_at (location, OPT_Waddress, "the address of %qD will never "
4176 		"be NULL", TREE_OPERAND (cop, 0));
4177 
4178   if (CONVERT_EXPR_P (op)
4179       && TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == REFERENCE_TYPE)
4180     {
4181       tree inner_op = op;
4182       STRIP_NOPS (inner_op);
4183 
4184       if (DECL_P (inner_op))
4185 	warning_at (location, OPT_Waddress,
4186 		    "the compiler can assume that the address of "
4187 		    "%qD will never be NULL", inner_op);
4188     }
4189 }
4190 
4191 /* Build a binary-operation expression without default conversions.
4192    CODE is the kind of expression to build.
4193    LOCATION is the location_t of the operator in the source code.
4194    This function differs from `build' in several ways:
4195    the data type of the result is computed and recorded in it,
4196    warnings are generated if arg data types are invalid,
4197    special handling for addition and subtraction of pointers is known,
4198    and some optimization is done (operations on narrow ints
4199    are done in the narrower type when that gives the same result).
4200    Constant folding is also done before the result is returned.
4201 
4202    Note that the operands will never have enumeral types
4203    because either they have just had the default conversions performed
4204    or they have both just been converted to some other type in which
4205    the arithmetic is to be done.
4206 
4207    C++: must do special pointer arithmetic when implementing
4208    multiple inheritance, and deal with pointer to member functions.  */
4209 
4210 tree
4211 cp_build_binary_op (location_t location,
4212 		    enum tree_code code, tree orig_op0, tree orig_op1,
4213 		    tsubst_flags_t complain)
4214 {
4215   tree op0, op1;
4216   enum tree_code code0, code1;
4217   tree type0, type1;
4218   const char *invalid_op_diag;
4219 
4220   /* Expression code to give to the expression when it is built.
4221      Normally this is CODE, which is what the caller asked for,
4222      but in some special cases we change it.  */
4223   enum tree_code resultcode = code;
4224 
4225   /* Data type in which the computation is to be performed.
4226      In the simplest cases this is the common type of the arguments.  */
4227   tree result_type = NULL_TREE;
4228 
4229   /* Nonzero means operands have already been type-converted
4230      in whatever way is necessary.
4231      Zero means they need to be converted to RESULT_TYPE.  */
4232   int converted = 0;
4233 
4234   /* Nonzero means create the expression with this type, rather than
4235      RESULT_TYPE.  */
4236   tree build_type = 0;
4237 
4238   /* Nonzero means after finally constructing the expression
4239      convert it to this type.  */
4240   tree final_type = 0;
4241 
4242   tree result, result_ovl;
4243 
4244   /* Nonzero if this is an operation like MIN or MAX which can
4245      safely be computed in short if both args are promoted shorts.
4246      Also implies COMMON.
4247      -1 indicates a bitwise operation; this makes a difference
4248      in the exact conditions for when it is safe to do the operation
4249      in a narrower mode.  */
4250   int shorten = 0;
4251 
4252   /* Nonzero if this is a comparison operation;
4253      if both args are promoted shorts, compare the original shorts.
4254      Also implies COMMON.  */
4255   int short_compare = 0;
4256 
4257   /* Nonzero means set RESULT_TYPE to the common type of the args.  */
4258   int common = 0;
4259 
4260   /* True if both operands have arithmetic type.  */
4261   bool arithmetic_types_p;
4262 
4263   /* Apply default conversions.  */
4264   op0 = orig_op0;
4265   op1 = orig_op1;
4266 
4267   /* Remember whether we're doing / or %.  */
4268   bool doing_div_or_mod = false;
4269 
4270   /* Remember whether we're doing << or >>.  */
4271   bool doing_shift = false;
4272 
4273   /* Tree holding instrumentation expression.  */
4274   tree instrument_expr = NULL_TREE;
4275 
4276   if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
4277       || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
4278       || code == TRUTH_XOR_EXPR)
4279     {
4280       if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
4281 	op0 = decay_conversion (op0, complain);
4282       if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
4283 	op1 = decay_conversion (op1, complain);
4284     }
4285   else
4286     {
4287       if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
4288 	op0 = cp_default_conversion (op0, complain);
4289       if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
4290 	op1 = cp_default_conversion (op1, complain);
4291     }
4292 
4293   /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue.  */
4294   STRIP_TYPE_NOPS (op0);
4295   STRIP_TYPE_NOPS (op1);
4296 
4297   /* DTRT if one side is an overloaded function, but complain about it.  */
4298   if (type_unknown_p (op0))
4299     {
4300       tree t = instantiate_type (TREE_TYPE (op1), op0, tf_none);
4301       if (t != error_mark_node)
4302 	{
4303 	  if (complain & tf_error)
4304 	    permerror (input_location, "assuming cast to type %qT from overloaded function",
4305 		       TREE_TYPE (t));
4306 	  op0 = t;
4307 	}
4308     }
4309   if (type_unknown_p (op1))
4310     {
4311       tree t = instantiate_type (TREE_TYPE (op0), op1, tf_none);
4312       if (t != error_mark_node)
4313 	{
4314 	  if (complain & tf_error)
4315 	    permerror (input_location, "assuming cast to type %qT from overloaded function",
4316 		       TREE_TYPE (t));
4317 	  op1 = t;
4318 	}
4319     }
4320 
4321   type0 = TREE_TYPE (op0);
4322   type1 = TREE_TYPE (op1);
4323 
4324   /* The expression codes of the data types of the arguments tell us
4325      whether the arguments are integers, floating, pointers, etc.  */
4326   code0 = TREE_CODE (type0);
4327   code1 = TREE_CODE (type1);
4328 
4329   /* If an error was already reported for one of the arguments,
4330      avoid reporting another error.  */
4331   if (code0 == ERROR_MARK || code1 == ERROR_MARK)
4332     return error_mark_node;
4333 
4334   if ((invalid_op_diag
4335        = targetm.invalid_binary_op (code, type0, type1)))
4336     {
4337       if (complain & tf_error)
4338 	error (invalid_op_diag);
4339       return error_mark_node;
4340     }
4341 
4342   /* Issue warnings about peculiar, but valid, uses of NULL.  */
4343   if ((null_node_p (orig_op0) || null_node_p (orig_op1))
4344       /* It's reasonable to use pointer values as operands of &&
4345 	 and ||, so NULL is no exception.  */
4346       && code != TRUTH_ANDIF_EXPR && code != TRUTH_ORIF_EXPR
4347       && ( /* Both are NULL (or 0) and the operation was not a
4348 	      comparison or a pointer subtraction.  */
4349 	  (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1)
4350 	   && code != EQ_EXPR && code != NE_EXPR && code != MINUS_EXPR)
4351 	  /* Or if one of OP0 or OP1 is neither a pointer nor NULL.  */
4352 	  || (!null_ptr_cst_p (orig_op0)
4353 	      && !TYPE_PTR_OR_PTRMEM_P (type0))
4354 	  || (!null_ptr_cst_p (orig_op1)
4355 	      && !TYPE_PTR_OR_PTRMEM_P (type1)))
4356       && (complain & tf_warning))
4357     {
4358       source_location loc =
4359 	expansion_point_location_if_in_system_header (input_location);
4360 
4361       warning_at (loc, OPT_Wpointer_arith, "NULL used in arithmetic");
4362     }
4363 
4364   /* In case when one of the operands of the binary operation is
4365      a vector and another is a scalar -- convert scalar to vector.  */
4366   if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
4367     {
4368       enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
4369 						     complain & tf_error);
4370 
4371       switch (convert_flag)
4372         {
4373           case stv_error:
4374             return error_mark_node;
4375           case stv_firstarg:
4376             {
4377               op0 = convert (TREE_TYPE (type1), op0);
4378 	      op0 = save_expr (op0);
4379               op0 = build_vector_from_val (type1, op0);
4380               type0 = TREE_TYPE (op0);
4381               code0 = TREE_CODE (type0);
4382               converted = 1;
4383               break;
4384             }
4385           case stv_secondarg:
4386             {
4387               op1 = convert (TREE_TYPE (type0), op1);
4388 	      op1 = save_expr (op1);
4389               op1 = build_vector_from_val (type0, op1);
4390               type1 = TREE_TYPE (op1);
4391               code1 = TREE_CODE (type1);
4392               converted = 1;
4393               break;
4394             }
4395           default:
4396             break;
4397         }
4398     }
4399 
4400   switch (code)
4401     {
4402     case MINUS_EXPR:
4403       /* Subtraction of two similar pointers.
4404 	 We must subtract them as integers, then divide by object size.  */
4405       if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
4406 	  && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
4407 							TREE_TYPE (type1)))
4408 	{
4409 	  result = pointer_diff (location, op0, op1,
4410 				 common_pointer_type (type0, type1), complain,
4411 				 &instrument_expr);
4412 	  if (instrument_expr != NULL)
4413 	    result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
4414 			     instrument_expr, result);
4415 
4416 	  return result;
4417 	}
4418       /* In all other cases except pointer - int, the usual arithmetic
4419 	 rules apply.  */
4420       else if (!(code0 == POINTER_TYPE && code1 == INTEGER_TYPE))
4421 	{
4422 	  common = 1;
4423 	  break;
4424 	}
4425       /* The pointer - int case is just like pointer + int; fall
4426 	 through.  */
4427       gcc_fallthrough ();
4428     case PLUS_EXPR:
4429       if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
4430 	  && (code0 == INTEGER_TYPE || code1 == INTEGER_TYPE))
4431 	{
4432 	  tree ptr_operand;
4433 	  tree int_operand;
4434 	  ptr_operand = ((code0 == POINTER_TYPE) ? op0 : op1);
4435 	  int_operand = ((code0 == INTEGER_TYPE) ? op0 : op1);
4436 	  if (processing_template_decl)
4437 	    {
4438 	      result_type = TREE_TYPE (ptr_operand);
4439 	      break;
4440 	    }
4441 	  return cp_pointer_int_sum (location, code,
4442 				     ptr_operand,
4443 				     int_operand,
4444 				     complain);
4445 	}
4446       common = 1;
4447       break;
4448 
4449     case MULT_EXPR:
4450       common = 1;
4451       break;
4452 
4453     case TRUNC_DIV_EXPR:
4454     case CEIL_DIV_EXPR:
4455     case FLOOR_DIV_EXPR:
4456     case ROUND_DIV_EXPR:
4457     case EXACT_DIV_EXPR:
4458       if (TREE_CODE (op0) == SIZEOF_EXPR && TREE_CODE (op1) == SIZEOF_EXPR)
4459 	{
4460 	  tree type0 = TREE_OPERAND (op0, 0);
4461 	  tree type1 = TREE_OPERAND (op1, 0);
4462 	  tree first_arg = type0;
4463 	  if (!TYPE_P (type0))
4464 	    type0 = TREE_TYPE (type0);
4465 	  if (!TYPE_P (type1))
4466 	    type1 = TREE_TYPE (type1);
4467 	  if (POINTER_TYPE_P (type0) && same_type_p (TREE_TYPE (type0), type1)
4468 	      && !(TREE_CODE (first_arg) == PARM_DECL
4469 		   && DECL_ARRAY_PARAMETER_P (first_arg)
4470 		   && warn_sizeof_array_argument)
4471 	      && (complain & tf_warning))
4472 	    if (warning_at (location, OPT_Wsizeof_pointer_div,
4473 			    "division %<sizeof (%T) / sizeof (%T)%> does "
4474 			    "not compute the number of array elements",
4475 			    type0, type1))
4476 	      if (DECL_P (first_arg))
4477 		inform (DECL_SOURCE_LOCATION (first_arg),
4478 			"first %<sizeof%> operand was declared here");
4479 	}
4480 
4481       if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4482 	   || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
4483 	  && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4484 	      || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
4485 	{
4486 	  enum tree_code tcode0 = code0, tcode1 = code1;
4487 	  tree cop1 = fold_non_dependent_expr (op1);
4488 	  doing_div_or_mod = true;
4489 	  warn_for_div_by_zero (location, cop1);
4490 
4491 	  if (tcode0 == COMPLEX_TYPE || tcode0 == VECTOR_TYPE)
4492 	    tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
4493 	  if (tcode1 == COMPLEX_TYPE || tcode1 == VECTOR_TYPE)
4494 	    tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
4495 
4496 	  if (!(tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE))
4497 	    resultcode = RDIV_EXPR;
4498 	  else
4499 	    /* When dividing two signed integers, we have to promote to int.
4500 	       unless we divide by a constant != -1.  Note that default
4501 	       conversion will have been performed on the operands at this
4502 	       point, so we have to dig out the original type to find out if
4503 	       it was unsigned.  */
4504 	    shorten = ((TREE_CODE (op0) == NOP_EXPR
4505 			&& TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
4506 		       || (TREE_CODE (op1) == INTEGER_CST
4507 			   && ! integer_all_onesp (op1)));
4508 
4509 	  common = 1;
4510 	}
4511       break;
4512 
4513     case BIT_AND_EXPR:
4514     case BIT_IOR_EXPR:
4515     case BIT_XOR_EXPR:
4516       if ((code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4517 	  || (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4518 	      && !VECTOR_FLOAT_TYPE_P (type0)
4519 	      && !VECTOR_FLOAT_TYPE_P (type1)))
4520 	shorten = -1;
4521       break;
4522 
4523     case TRUNC_MOD_EXPR:
4524     case FLOOR_MOD_EXPR:
4525       {
4526 	tree cop1 = fold_non_dependent_expr (op1);
4527 	doing_div_or_mod = true;
4528 	warn_for_div_by_zero (location, cop1);
4529       }
4530 
4531       if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4532 	  && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4533 	  && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
4534 	common = 1;
4535       else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4536 	{
4537 	  /* Although it would be tempting to shorten always here, that loses
4538 	     on some targets, since the modulo instruction is undefined if the
4539 	     quotient can't be represented in the computation mode.  We shorten
4540 	     only if unsigned or if dividing by something we know != -1.  */
4541 	  shorten = ((TREE_CODE (op0) == NOP_EXPR
4542 		      && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
4543 		     || (TREE_CODE (op1) == INTEGER_CST
4544 			 && ! integer_all_onesp (op1)));
4545 	  common = 1;
4546 	}
4547       break;
4548 
4549     case TRUTH_ANDIF_EXPR:
4550     case TRUTH_ORIF_EXPR:
4551     case TRUTH_AND_EXPR:
4552     case TRUTH_OR_EXPR:
4553       if (!VECTOR_TYPE_P (type0) && VECTOR_TYPE_P (type1))
4554 	{
4555 	  if (!COMPARISON_CLASS_P (op1))
4556 	    op1 = cp_build_binary_op (EXPR_LOCATION (op1), NE_EXPR, op1,
4557 				      build_zero_cst (type1), complain);
4558 	  if (code == TRUTH_ANDIF_EXPR)
4559 	    {
4560 	      tree z = build_zero_cst (TREE_TYPE (op1));
4561 	      return build_conditional_expr (location, op0, op1, z, complain);
4562 	    }
4563 	  else if (code == TRUTH_ORIF_EXPR)
4564 	    {
4565 	      tree m1 = build_all_ones_cst (TREE_TYPE (op1));
4566 	      return build_conditional_expr (location, op0, m1, op1, complain);
4567 	    }
4568 	  else
4569 	    gcc_unreachable ();
4570 	}
4571       if (VECTOR_TYPE_P (type0))
4572 	{
4573 	  if (!COMPARISON_CLASS_P (op0))
4574 	    op0 = cp_build_binary_op (EXPR_LOCATION (op0), NE_EXPR, op0,
4575 				      build_zero_cst (type0), complain);
4576 	  if (!VECTOR_TYPE_P (type1))
4577 	    {
4578 	      tree m1 = build_all_ones_cst (TREE_TYPE (op0));
4579 	      tree z = build_zero_cst (TREE_TYPE (op0));
4580 	      op1 = build_conditional_expr (location, op1, m1, z, complain);
4581 	    }
4582 	  else if (!COMPARISON_CLASS_P (op1))
4583 	    op1 = cp_build_binary_op (EXPR_LOCATION (op1), NE_EXPR, op1,
4584 				      build_zero_cst (type1), complain);
4585 
4586 	  if (code == TRUTH_ANDIF_EXPR)
4587 	    code = BIT_AND_EXPR;
4588 	  else if (code == TRUTH_ORIF_EXPR)
4589 	    code = BIT_IOR_EXPR;
4590 	  else
4591 	    gcc_unreachable ();
4592 
4593 	  return cp_build_binary_op (location, code, op0, op1, complain);
4594 	}
4595 
4596       result_type = boolean_type_node;
4597       break;
4598 
4599       /* Shift operations: result has same type as first operand;
4600 	 always convert second operand to int.
4601 	 Also set SHORT_SHIFT if shifting rightward.  */
4602 
4603     case RSHIFT_EXPR:
4604       if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
4605           && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
4606         {
4607           result_type = type0;
4608           converted = 1;
4609         }
4610       else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4611 	       && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4612 	       && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
4613 	       && known_eq (TYPE_VECTOR_SUBPARTS (type0),
4614 			    TYPE_VECTOR_SUBPARTS (type1)))
4615 	{
4616 	  result_type = type0;
4617 	  converted = 1;
4618 	}
4619       else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4620 	{
4621 	  tree const_op1 = fold_non_dependent_expr (op1);
4622 	  if (TREE_CODE (const_op1) != INTEGER_CST)
4623 	    const_op1 = op1;
4624 	  result_type = type0;
4625 	  doing_shift = true;
4626 	  if (TREE_CODE (const_op1) == INTEGER_CST)
4627 	    {
4628 	      if (tree_int_cst_lt (const_op1, integer_zero_node))
4629 		{
4630 		  if ((complain & tf_warning)
4631 		      && c_inhibit_evaluation_warnings == 0)
4632 		    warning (OPT_Wshift_count_negative,
4633 			     "right shift count is negative");
4634 		}
4635 	      else
4636 		{
4637 		  if (compare_tree_int (const_op1, TYPE_PRECISION (type0)) >= 0
4638 		      && (complain & tf_warning)
4639 		      && c_inhibit_evaluation_warnings == 0)
4640 		    warning (OPT_Wshift_count_overflow,
4641 			     "right shift count >= width of type");
4642 		}
4643 	    }
4644 	  /* Avoid converting op1 to result_type later.  */
4645 	  converted = 1;
4646 	}
4647       break;
4648 
4649     case LSHIFT_EXPR:
4650       if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
4651           && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
4652         {
4653           result_type = type0;
4654           converted = 1;
4655         }
4656       else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4657 	       && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4658 	       && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
4659 	       && known_eq (TYPE_VECTOR_SUBPARTS (type0),
4660 			    TYPE_VECTOR_SUBPARTS (type1)))
4661 	{
4662 	  result_type = type0;
4663 	  converted = 1;
4664 	}
4665       else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4666 	{
4667 	  tree const_op0 = fold_non_dependent_expr (op0);
4668 	  if (TREE_CODE (const_op0) != INTEGER_CST)
4669 	    const_op0 = op0;
4670 	  tree const_op1 = fold_non_dependent_expr (op1);
4671 	  if (TREE_CODE (const_op1) != INTEGER_CST)
4672 	    const_op1 = op1;
4673 	  result_type = type0;
4674 	  doing_shift = true;
4675 	  if (TREE_CODE (const_op0) == INTEGER_CST
4676 	      && tree_int_cst_sgn (const_op0) < 0
4677 	      && (complain & tf_warning)
4678 	      && c_inhibit_evaluation_warnings == 0)
4679 	    warning (OPT_Wshift_negative_value,
4680 		     "left shift of negative value");
4681 	  if (TREE_CODE (const_op1) == INTEGER_CST)
4682 	    {
4683 	      if (tree_int_cst_lt (const_op1, integer_zero_node))
4684 		{
4685 		  if ((complain & tf_warning)
4686 		      && c_inhibit_evaluation_warnings == 0)
4687 		    warning (OPT_Wshift_count_negative,
4688 			     "left shift count is negative");
4689 		}
4690 	      else if (compare_tree_int (const_op1,
4691 					 TYPE_PRECISION (type0)) >= 0)
4692 		{
4693 		  if ((complain & tf_warning)
4694 		      && c_inhibit_evaluation_warnings == 0)
4695 		    warning (OPT_Wshift_count_overflow,
4696 			     "left shift count >= width of type");
4697 		}
4698 	      else if (TREE_CODE (const_op0) == INTEGER_CST
4699 		       && (complain & tf_warning))
4700 		maybe_warn_shift_overflow (location, const_op0, const_op1);
4701 	    }
4702 	  /* Avoid converting op1 to result_type later.  */
4703 	  converted = 1;
4704 	}
4705       break;
4706 
4707     case RROTATE_EXPR:
4708     case LROTATE_EXPR:
4709       if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4710 	{
4711 	  result_type = type0;
4712 	  if (TREE_CODE (op1) == INTEGER_CST)
4713 	    {
4714 	      if (tree_int_cst_lt (op1, integer_zero_node))
4715 		{
4716 		  if (complain & tf_warning)
4717 		    warning (0, (code == LROTATE_EXPR)
4718 			          ? G_("left rotate count is negative")
4719    			          : G_("right rotate count is negative"));
4720 		}
4721 	      else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
4722 		{
4723 		  if (complain & tf_warning)
4724 		    warning (0, (code == LROTATE_EXPR)
4725                                   ? G_("left rotate count >= width of type")
4726                                   : G_("right rotate count >= width of type"));
4727 		}
4728 	    }
4729 	  /* Convert the shift-count to an integer, regardless of
4730 	     size of value being shifted.  */
4731 	  if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
4732 	    op1 = cp_convert (integer_type_node, op1, complain);
4733 	}
4734       break;
4735 
4736     case EQ_EXPR:
4737     case NE_EXPR:
4738       if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4739 	goto vector_compare;
4740       if ((complain & tf_warning)
4741 	  && (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1)))
4742 	warning (OPT_Wfloat_equal,
4743 		 "comparing floating point with == or != is unsafe");
4744       if ((complain & tf_warning)
4745 	  && ((TREE_CODE (orig_op0) == STRING_CST
4746 	       && !integer_zerop (cp_fully_fold (op1)))
4747 	      || (TREE_CODE (orig_op1) == STRING_CST
4748 		  && !integer_zerop (cp_fully_fold (op0)))))
4749 	warning (OPT_Waddress, "comparison with string literal results "
4750 			       "in unspecified behavior");
4751 
4752       build_type = boolean_type_node;
4753       if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4754 	   || code0 == COMPLEX_TYPE || code0 == ENUMERAL_TYPE)
4755 	  && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4756 	      || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE))
4757 	short_compare = 1;
4758       else if (((code0 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type0))
4759 		&& null_ptr_cst_p (orig_op1))
4760 	       /* Handle, eg, (void*)0 (c++/43906), and more.  */
4761 	       || (code0 == POINTER_TYPE
4762 		   && TYPE_PTR_P (type1) && integer_zerop (op1)))
4763 	{
4764 	  if (TYPE_PTR_P (type1))
4765 	    result_type = composite_pointer_type (type0, type1, op0, op1,
4766 						  CPO_COMPARISON, complain);
4767 	  else
4768 	    result_type = type0;
4769 
4770 	  if (char_type_p (TREE_TYPE (orig_op1))
4771 	      && warning (OPT_Wpointer_compare,
4772 			  "comparison between pointer and zero character "
4773 			  "constant"))
4774 	    inform (input_location,
4775 		    "did you mean to dereference the pointer?");
4776 	  warn_for_null_address (location, op0, complain);
4777 	}
4778       else if (((code1 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type1))
4779 		&& null_ptr_cst_p (orig_op0))
4780 	       /* Handle, eg, (void*)0 (c++/43906), and more.  */
4781 	       || (code1 == POINTER_TYPE
4782 		   && TYPE_PTR_P (type0) && integer_zerop (op0)))
4783 	{
4784 	  if (TYPE_PTR_P (type0))
4785 	    result_type = composite_pointer_type (type0, type1, op0, op1,
4786 						  CPO_COMPARISON, complain);
4787 	  else
4788 	    result_type = type1;
4789 
4790 	  if (char_type_p (TREE_TYPE (orig_op0))
4791 	      && warning (OPT_Wpointer_compare,
4792 			  "comparison between pointer and zero character "
4793 			  "constant"))
4794 	    inform (input_location,
4795 		    "did you mean to dereference the pointer?");
4796 	  warn_for_null_address (location, op1, complain);
4797 	}
4798       else if ((code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4799 	       || (TYPE_PTRDATAMEM_P (type0) && TYPE_PTRDATAMEM_P (type1)))
4800 	result_type = composite_pointer_type (type0, type1, op0, op1,
4801 					      CPO_COMPARISON, complain);
4802       else if (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1))
4803 	/* One of the operands must be of nullptr_t type.  */
4804         result_type = TREE_TYPE (nullptr_node);
4805       else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
4806 	{
4807 	  result_type = type0;
4808 	  if (complain & tf_error)
4809             permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4810           else
4811             return error_mark_node;
4812 	}
4813       else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
4814 	{
4815 	  result_type = type1;
4816 	  if (complain & tf_error)
4817 	    permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4818           else
4819             return error_mark_node;
4820 	}
4821       else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (orig_op1))
4822 	{
4823 	  if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4824 	      == ptrmemfunc_vbit_in_delta)
4825 	    {
4826 	      tree pfn0, delta0, e1, e2;
4827 
4828 	      if (TREE_SIDE_EFFECTS (op0))
4829 		op0 = save_expr (op0);
4830 
4831 	      pfn0 = pfn_from_ptrmemfunc (op0);
4832 	      delta0 = delta_from_ptrmemfunc (op0);
4833 	      e1 = cp_build_binary_op (location,
4834 				       EQ_EXPR,
4835 	  			       pfn0,
4836 				       build_zero_cst (TREE_TYPE (pfn0)),
4837 				       complain);
4838 	      e2 = cp_build_binary_op (location,
4839 				       BIT_AND_EXPR,
4840 				       delta0,
4841 				       integer_one_node,
4842 				       complain);
4843 
4844 	      if (complain & tf_warning)
4845 		maybe_warn_zero_as_null_pointer_constant (op1, input_location);
4846 
4847 	      e2 = cp_build_binary_op (location,
4848 				       EQ_EXPR, e2, integer_zero_node,
4849 				       complain);
4850 	      op0 = cp_build_binary_op (location,
4851 					TRUTH_ANDIF_EXPR, e1, e2,
4852 					complain);
4853 	      op1 = cp_convert (TREE_TYPE (op0), integer_one_node, complain);
4854 	    }
4855      	  else
4856 	    {
4857 	      op0 = build_ptrmemfunc_access_expr (op0, pfn_identifier);
4858 	      op1 = cp_convert (TREE_TYPE (op0), op1, complain);
4859 	    }
4860 	  result_type = TREE_TYPE (op0);
4861 	}
4862       else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (orig_op0))
4863 	return cp_build_binary_op (location, code, op1, op0, complain);
4864       else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1))
4865 	{
4866 	  tree type;
4867 	  /* E will be the final comparison.  */
4868 	  tree e;
4869 	  /* E1 and E2 are for scratch.  */
4870 	  tree e1;
4871 	  tree e2;
4872 	  tree pfn0;
4873 	  tree pfn1;
4874 	  tree delta0;
4875 	  tree delta1;
4876 
4877 	  type = composite_pointer_type (type0, type1, op0, op1,
4878 					 CPO_COMPARISON, complain);
4879 
4880 	  if (!same_type_p (TREE_TYPE (op0), type))
4881 	    op0 = cp_convert_and_check (type, op0, complain);
4882 	  if (!same_type_p (TREE_TYPE (op1), type))
4883 	    op1 = cp_convert_and_check (type, op1, complain);
4884 
4885 	  if (op0 == error_mark_node || op1 == error_mark_node)
4886 	    return error_mark_node;
4887 
4888 	  if (TREE_SIDE_EFFECTS (op0))
4889 	    op0 = save_expr (op0);
4890 	  if (TREE_SIDE_EFFECTS (op1))
4891 	    op1 = save_expr (op1);
4892 
4893 	  pfn0 = pfn_from_ptrmemfunc (op0);
4894 	  pfn0 = cp_fully_fold (pfn0);
4895 	  /* Avoid -Waddress warnings (c++/64877).  */
4896 	  if (TREE_CODE (pfn0) == ADDR_EXPR)
4897 	    TREE_NO_WARNING (pfn0) = 1;
4898 	  pfn1 = pfn_from_ptrmemfunc (op1);
4899 	  pfn1 = cp_fully_fold (pfn1);
4900 	  delta0 = delta_from_ptrmemfunc (op0);
4901 	  delta1 = delta_from_ptrmemfunc (op1);
4902 	  if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4903 	      == ptrmemfunc_vbit_in_delta)
4904 	    {
4905 	      /* We generate:
4906 
4907 		 (op0.pfn == op1.pfn
4908 		  && ((op0.delta == op1.delta)
4909      		       || (!op0.pfn && op0.delta & 1 == 0
4910 			   && op1.delta & 1 == 0))
4911 
4912 	         The reason for the `!op0.pfn' bit is that a NULL
4913 	         pointer-to-member is any member with a zero PFN and
4914 	         LSB of the DELTA field is 0.  */
4915 
4916 	      e1 = cp_build_binary_op (location, BIT_AND_EXPR,
4917 				       delta0,
4918 				       integer_one_node,
4919 				       complain);
4920 	      e1 = cp_build_binary_op (location,
4921 				       EQ_EXPR, e1, integer_zero_node,
4922 				       complain);
4923 	      e2 = cp_build_binary_op (location, BIT_AND_EXPR,
4924 				       delta1,
4925 				       integer_one_node,
4926 				       complain);
4927 	      e2 = cp_build_binary_op (location,
4928 				       EQ_EXPR, e2, integer_zero_node,
4929 				       complain);
4930 	      e1 = cp_build_binary_op (location,
4931 				       TRUTH_ANDIF_EXPR, e2, e1,
4932 				       complain);
4933 	      e2 = cp_build_binary_op (location, EQ_EXPR,
4934 				       pfn0,
4935 				       build_zero_cst (TREE_TYPE (pfn0)),
4936 				       complain);
4937 	      e2 = cp_build_binary_op (location,
4938 				       TRUTH_ANDIF_EXPR, e2, e1, complain);
4939 	      e1 = cp_build_binary_op (location,
4940 				       EQ_EXPR, delta0, delta1, complain);
4941 	      e1 = cp_build_binary_op (location,
4942 				       TRUTH_ORIF_EXPR, e1, e2, complain);
4943 	    }
4944 	  else
4945 	    {
4946 	      /* We generate:
4947 
4948 	         (op0.pfn == op1.pfn
4949 	         && (!op0.pfn || op0.delta == op1.delta))
4950 
4951 	         The reason for the `!op0.pfn' bit is that a NULL
4952 	         pointer-to-member is any member with a zero PFN; the
4953 	         DELTA field is unspecified.  */
4954 
4955     	      e1 = cp_build_binary_op (location,
4956 				       EQ_EXPR, delta0, delta1, complain);
4957 	      e2 = cp_build_binary_op (location,
4958 				       EQ_EXPR,
4959 		      		       pfn0,
4960 			   	       build_zero_cst (TREE_TYPE (pfn0)),
4961 				       complain);
4962 	      e1 = cp_build_binary_op (location,
4963 				       TRUTH_ORIF_EXPR, e1, e2, complain);
4964 	    }
4965 	  e2 = build2 (EQ_EXPR, boolean_type_node, pfn0, pfn1);
4966 	  e = cp_build_binary_op (location,
4967 				  TRUTH_ANDIF_EXPR, e2, e1, complain);
4968 	  if (code == EQ_EXPR)
4969 	    return e;
4970 	  return cp_build_binary_op (location,
4971 				     EQ_EXPR, e, integer_zero_node, complain);
4972 	}
4973       else
4974 	{
4975 	  gcc_assert (!TYPE_PTRMEMFUNC_P (type0)
4976 		      || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0),
4977 				       type1));
4978 	  gcc_assert (!TYPE_PTRMEMFUNC_P (type1)
4979 		      || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1),
4980 				       type0));
4981 	}
4982 
4983       break;
4984 
4985     case MAX_EXPR:
4986     case MIN_EXPR:
4987       if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
4988 	   && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
4989 	shorten = 1;
4990       else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4991 	result_type = composite_pointer_type (type0, type1, op0, op1,
4992 					      CPO_COMPARISON, complain);
4993       break;
4994 
4995     case LE_EXPR:
4996     case GE_EXPR:
4997     case LT_EXPR:
4998     case GT_EXPR:
4999       if (TREE_CODE (orig_op0) == STRING_CST
5000 	  || TREE_CODE (orig_op1) == STRING_CST)
5001 	{
5002 	  if (complain & tf_warning)
5003 	    warning (OPT_Waddress, "comparison with string literal results "
5004 				   "in unspecified behavior");
5005 	}
5006 
5007       if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
5008 	{
5009 	vector_compare:
5010 	  tree intt;
5011 	  if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
5012 							  TREE_TYPE (type1))
5013 	      && !vector_types_compatible_elements_p (type0, type1))
5014 	    {
5015 	      if (complain & tf_error)
5016 		{
5017 		  error_at (location, "comparing vectors with different "
5018 				      "element types");
5019 		  inform (location, "operand types are %qT and %qT",
5020 			  type0, type1);
5021 		}
5022 	      return error_mark_node;
5023 	    }
5024 
5025 	  if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
5026 			TYPE_VECTOR_SUBPARTS (type1)))
5027 	    {
5028 	      if (complain & tf_error)
5029 		{
5030 		  error_at (location, "comparing vectors with different "
5031 				      "number of elements");
5032 		  inform (location, "operand types are %qT and %qT",
5033 			  type0, type1);
5034 		}
5035 	      return error_mark_node;
5036 	    }
5037 
5038 	  /* It's not precisely specified how the usual arithmetic
5039 	     conversions apply to the vector types.  Here, we use
5040 	     the unsigned type if one of the operands is signed and
5041 	     the other one is unsigned.  */
5042 	  if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
5043 	    {
5044 	      if (!TYPE_UNSIGNED (type0))
5045 		op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
5046 	      else
5047 		op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
5048 	      warning_at (location, OPT_Wsign_compare, "comparison between "
5049 			  "types %qT and %qT", type0, type1);
5050 	    }
5051 
5052 	  /* Always construct signed integer vector type.  */
5053 	  intt = c_common_type_for_size
5054 	    (GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (type0))), 0);
5055 	  if (!intt)
5056 	    {
5057 	      if (complain & tf_error)
5058 		error_at (location, "could not find an integer type "
5059 			  "of the same size as %qT", TREE_TYPE (type0));
5060 	      return error_mark_node;
5061 	    }
5062 	  result_type = build_opaque_vector_type (intt,
5063 						  TYPE_VECTOR_SUBPARTS (type0));
5064 	  converted = 1;
5065 	  return build_vec_cmp (resultcode, result_type, op0, op1);
5066 	}
5067       build_type = boolean_type_node;
5068       if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
5069 	   || code0 == ENUMERAL_TYPE)
5070 	   && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
5071 	       || code1 == ENUMERAL_TYPE))
5072 	short_compare = 1;
5073       else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
5074 	result_type = composite_pointer_type (type0, type1, op0, op1,
5075 					      CPO_COMPARISON, complain);
5076       else if (code0 == POINTER_TYPE && null_ptr_cst_p (orig_op1))
5077 	{
5078 	  result_type = type0;
5079 	  if (extra_warnings && (complain & tf_warning))
5080 	    warning (OPT_Wextra,
5081 		     "ordered comparison of pointer with integer zero");
5082 	}
5083       else if (code1 == POINTER_TYPE && null_ptr_cst_p (orig_op0))
5084 	{
5085 	  result_type = type1;
5086 	  if (extra_warnings && (complain & tf_warning))
5087 	    warning (OPT_Wextra,
5088 		     "ordered comparison of pointer with integer zero");
5089 	}
5090       else if (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1))
5091 	/* One of the operands must be of nullptr_t type.  */
5092         result_type = TREE_TYPE (nullptr_node);
5093       else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
5094 	{
5095 	  result_type = type0;
5096 	  if (complain & tf_error)
5097 	    permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
5098           else
5099             return error_mark_node;
5100 	}
5101       else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
5102 	{
5103 	  result_type = type1;
5104 	  if (complain & tf_error)
5105 	    permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
5106           else
5107             return error_mark_node;
5108 	}
5109 
5110       if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
5111 	  && sanitize_flags_p (SANITIZE_POINTER_COMPARE))
5112 	{
5113 	  op0 = save_expr (op0);
5114 	  op1 = save_expr (op1);
5115 
5116 	  tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_COMPARE);
5117 	  instrument_expr = build_call_expr_loc (location, tt, 2, op0, op1);
5118 	}
5119 
5120       break;
5121 
5122     case UNORDERED_EXPR:
5123     case ORDERED_EXPR:
5124     case UNLT_EXPR:
5125     case UNLE_EXPR:
5126     case UNGT_EXPR:
5127     case UNGE_EXPR:
5128     case UNEQ_EXPR:
5129       build_type = integer_type_node;
5130       if (code0 != REAL_TYPE || code1 != REAL_TYPE)
5131 	{
5132 	  if (complain & tf_error)
5133 	    error ("unordered comparison on non-floating point argument");
5134 	  return error_mark_node;
5135 	}
5136       common = 1;
5137       break;
5138 
5139     default:
5140       break;
5141     }
5142 
5143   if (((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
5144 	|| code0 == ENUMERAL_TYPE)
5145        && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
5146 	   || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE)))
5147     arithmetic_types_p = 1;
5148   else
5149     {
5150       arithmetic_types_p = 0;
5151       /* Vector arithmetic is only allowed when both sides are vectors.  */
5152       if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
5153 	{
5154 	  if (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
5155 	      || !vector_types_compatible_elements_p (type0, type1))
5156 	    {
5157 	      if (complain & tf_error)
5158 		{
5159 		  /* "location" already embeds the locations of the
5160 		     operands, so we don't need to add them separately
5161 		     to richloc.  */
5162 		  rich_location richloc (line_table, location);
5163 		  binary_op_error (&richloc, code, type0, type1);
5164 		}
5165 	      return error_mark_node;
5166 	    }
5167 	  arithmetic_types_p = 1;
5168 	}
5169     }
5170   /* Determine the RESULT_TYPE, if it is not already known.  */
5171   if (!result_type
5172       && arithmetic_types_p
5173       && (shorten || common || short_compare))
5174     {
5175       result_type = cp_common_type (type0, type1);
5176       if (complain & tf_warning)
5177 	do_warn_double_promotion (result_type, type0, type1,
5178 				  "implicit conversion from %qH to %qI "
5179 				  "to match other operand of binary "
5180 				  "expression",
5181 				  location);
5182     }
5183 
5184   if (!result_type)
5185     {
5186       if (complain & tf_error)
5187 	error_at (location,
5188 		  "invalid operands of types %qT and %qT to binary %qO",
5189 		  TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), code);
5190       return error_mark_node;
5191     }
5192 
5193   /* If we're in a template, the only thing we need to know is the
5194      RESULT_TYPE.  */
5195   if (processing_template_decl)
5196     {
5197       /* Since the middle-end checks the type when doing a build2, we
5198 	 need to build the tree in pieces.  This built tree will never
5199 	 get out of the front-end as we replace it when instantiating
5200 	 the template.  */
5201       tree tmp = build2 (resultcode,
5202 			 build_type ? build_type : result_type,
5203 			 NULL_TREE, op1);
5204       TREE_OPERAND (tmp, 0) = op0;
5205       return tmp;
5206     }
5207 
5208   /* Remember the original type; RESULT_TYPE might be changed later on
5209      by shorten_binary_op.  */
5210   tree orig_type = result_type;
5211 
5212   if (arithmetic_types_p)
5213     {
5214       bool first_complex = (code0 == COMPLEX_TYPE);
5215       bool second_complex = (code1 == COMPLEX_TYPE);
5216       int none_complex = (!first_complex && !second_complex);
5217 
5218       /* Adapted from patch for c/24581.  */
5219       if (first_complex != second_complex
5220 	  && (code == PLUS_EXPR
5221 	      || code == MINUS_EXPR
5222 	      || code == MULT_EXPR
5223 	      || (code == TRUNC_DIV_EXPR && first_complex))
5224 	  && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
5225 	  && flag_signed_zeros)
5226 	{
5227 	  /* An operation on mixed real/complex operands must be
5228 	     handled specially, but the language-independent code can
5229 	     more easily optimize the plain complex arithmetic if
5230 	     -fno-signed-zeros.  */
5231 	  tree real_type = TREE_TYPE (result_type);
5232 	  tree real, imag;
5233 	  if (first_complex)
5234 	    {
5235 	      if (TREE_TYPE (op0) != result_type)
5236 		op0 = cp_convert_and_check (result_type, op0, complain);
5237 	      if (TREE_TYPE (op1) != real_type)
5238 		op1 = cp_convert_and_check (real_type, op1, complain);
5239 	    }
5240 	  else
5241 	    {
5242 	      if (TREE_TYPE (op0) != real_type)
5243 		op0 = cp_convert_and_check (real_type, op0, complain);
5244 	      if (TREE_TYPE (op1) != result_type)
5245 		op1 = cp_convert_and_check (result_type, op1, complain);
5246 	    }
5247 	  if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
5248 	    return error_mark_node;
5249 	  if (first_complex)
5250 	    {
5251 	      op0 = save_expr (op0);
5252 	      real = cp_build_unary_op (REALPART_EXPR, op0, true, complain);
5253 	      imag = cp_build_unary_op (IMAGPART_EXPR, op0, true, complain);
5254 	      switch (code)
5255 		{
5256 		case MULT_EXPR:
5257 		case TRUNC_DIV_EXPR:
5258 		  op1 = save_expr (op1);
5259 		  imag = build2 (resultcode, real_type, imag, op1);
5260 		  /* Fall through.  */
5261 		case PLUS_EXPR:
5262 		case MINUS_EXPR:
5263 		  real = build2 (resultcode, real_type, real, op1);
5264 		  break;
5265 		default:
5266 		  gcc_unreachable();
5267 		}
5268 	    }
5269 	  else
5270 	    {
5271 	      op1 = save_expr (op1);
5272 	      real = cp_build_unary_op (REALPART_EXPR, op1, true, complain);
5273 	      imag = cp_build_unary_op (IMAGPART_EXPR, op1, true, complain);
5274 	      switch (code)
5275 		{
5276 		case MULT_EXPR:
5277 		  op0 = save_expr (op0);
5278 		  imag = build2 (resultcode, real_type, op0, imag);
5279 		  /* Fall through.  */
5280 		case PLUS_EXPR:
5281 		  real = build2 (resultcode, real_type, op0, real);
5282 		  break;
5283 		case MINUS_EXPR:
5284 		  real = build2 (resultcode, real_type, op0, real);
5285 		  imag = build1 (NEGATE_EXPR, real_type, imag);
5286 		  break;
5287 		default:
5288 		  gcc_unreachable();
5289 		}
5290 	    }
5291 	  result = build2 (COMPLEX_EXPR, result_type, real, imag);
5292 	  return result;
5293 	}
5294 
5295       /* For certain operations (which identify themselves by shorten != 0)
5296 	 if both args were extended from the same smaller type,
5297 	 do the arithmetic in that type and then extend.
5298 
5299 	 shorten !=0 and !=1 indicates a bitwise operation.
5300 	 For them, this optimization is safe only if
5301 	 both args are zero-extended or both are sign-extended.
5302 	 Otherwise, we might change the result.
5303 	 E.g., (short)-1 | (unsigned short)-1 is (int)-1
5304 	 but calculated in (unsigned short) it would be (unsigned short)-1.  */
5305 
5306       if (shorten && none_complex)
5307 	{
5308 	  final_type = result_type;
5309 	  result_type = shorten_binary_op (result_type, op0, op1,
5310 					   shorten == -1);
5311 	}
5312 
5313       /* Comparison operations are shortened too but differently.
5314 	 They identify themselves by setting short_compare = 1.  */
5315 
5316       if (short_compare)
5317 	{
5318 	  /* We call shorten_compare only for diagnostic-reason.  */
5319 	  tree xop0 = fold_simple (op0), xop1 = fold_simple (op1),
5320 	       xresult_type = result_type;
5321 	  enum tree_code xresultcode = resultcode;
5322 	  shorten_compare (location, &xop0, &xop1, &xresult_type,
5323 			       &xresultcode);
5324 	}
5325 
5326       if ((short_compare || code == MIN_EXPR || code == MAX_EXPR)
5327 	  && warn_sign_compare
5328 	  /* Do not warn until the template is instantiated; we cannot
5329 	     bound the ranges of the arguments until that point.  */
5330 	  && !processing_template_decl
5331           && (complain & tf_warning)
5332 	  && c_inhibit_evaluation_warnings == 0
5333 	  /* Even unsigned enum types promote to signed int.  We don't
5334 	     want to issue -Wsign-compare warnings for this case.  */
5335 	  && !enum_cast_to_int (orig_op0)
5336 	  && !enum_cast_to_int (orig_op1))
5337 	{
5338 	  tree oop0 = maybe_constant_value (orig_op0);
5339 	  tree oop1 = maybe_constant_value (orig_op1);
5340 
5341 	  if (TREE_CODE (oop0) != INTEGER_CST)
5342 	    oop0 = cp_fully_fold (orig_op0);
5343 	  if (TREE_CODE (oop1) != INTEGER_CST)
5344 	    oop1 = cp_fully_fold (orig_op1);
5345 	  warn_for_sign_compare (location, oop0, oop1, op0, op1,
5346 				 result_type, resultcode);
5347 	}
5348     }
5349 
5350   /* If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
5351      Then the expression will be built.
5352      It will be given type FINAL_TYPE if that is nonzero;
5353      otherwise, it will be given type RESULT_TYPE.  */
5354   if (! converted)
5355     {
5356       if (TREE_TYPE (op0) != result_type)
5357 	op0 = cp_convert_and_check (result_type, op0, complain);
5358       if (TREE_TYPE (op1) != result_type)
5359 	op1 = cp_convert_and_check (result_type, op1, complain);
5360 
5361       if (op0 == error_mark_node || op1 == error_mark_node)
5362 	return error_mark_node;
5363     }
5364 
5365   if (build_type == NULL_TREE)
5366     build_type = result_type;
5367 
5368   if (sanitize_flags_p ((SANITIZE_SHIFT
5369 			 | SANITIZE_DIVIDE | SANITIZE_FLOAT_DIVIDE))
5370       && current_function_decl != NULL_TREE
5371       && !processing_template_decl
5372       && (doing_div_or_mod || doing_shift))
5373     {
5374       /* OP0 and/or OP1 might have side-effects.  */
5375       op0 = cp_save_expr (op0);
5376       op1 = cp_save_expr (op1);
5377       op0 = fold_non_dependent_expr (op0);
5378       op1 = fold_non_dependent_expr (op1);
5379       if (doing_div_or_mod
5380 	  && sanitize_flags_p (SANITIZE_DIVIDE | SANITIZE_FLOAT_DIVIDE))
5381 	{
5382 	  /* For diagnostics we want to use the promoted types without
5383 	     shorten_binary_op.  So convert the arguments to the
5384 	     original result_type.  */
5385 	  tree cop0 = op0;
5386 	  tree cop1 = op1;
5387 	  if (TREE_TYPE (cop0) != orig_type)
5388 	    cop0 = cp_convert (orig_type, op0, complain);
5389 	  if (TREE_TYPE (cop1) != orig_type)
5390 	    cop1 = cp_convert (orig_type, op1, complain);
5391 	  instrument_expr = ubsan_instrument_division (location, cop0, cop1);
5392 	}
5393       else if (doing_shift && sanitize_flags_p (SANITIZE_SHIFT))
5394 	instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
5395     }
5396 
5397   result = build2_loc (location, resultcode, build_type, op0, op1);
5398   if (final_type != 0)
5399     result = cp_convert (final_type, result, complain);
5400 
5401   if (instrument_expr != NULL)
5402     result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
5403 		     instrument_expr, result);
5404 
5405   if (!processing_template_decl)
5406     {
5407       op0 = cp_fully_fold (op0);
5408       /* Only consider the second argument if the first isn't overflowed.  */
5409       if (!CONSTANT_CLASS_P (op0) || TREE_OVERFLOW_P (op0))
5410 	return result;
5411       op1 = cp_fully_fold (op1);
5412       if (!CONSTANT_CLASS_P (op1) || TREE_OVERFLOW_P (op1))
5413 	return result;
5414     }
5415   else if (!CONSTANT_CLASS_P (op0) || !CONSTANT_CLASS_P (op1)
5416 	   || TREE_OVERFLOW_P (op0) || TREE_OVERFLOW_P (op1))
5417     return result;
5418 
5419   result_ovl = fold_build2 (resultcode, build_type, op0, op1);
5420   if (TREE_OVERFLOW_P (result_ovl))
5421     overflow_warning (location, result_ovl);
5422 
5423   return result;
5424 }
5425 
5426 /* Build a VEC_PERM_EXPR.
5427    This is a simple wrapper for c_build_vec_perm_expr.  */
5428 tree
5429 build_x_vec_perm_expr (location_t loc,
5430 			tree arg0, tree arg1, tree arg2,
5431 			tsubst_flags_t complain)
5432 {
5433   tree orig_arg0 = arg0;
5434   tree orig_arg1 = arg1;
5435   tree orig_arg2 = arg2;
5436   if (processing_template_decl)
5437     {
5438       if (type_dependent_expression_p (arg0)
5439 	  || type_dependent_expression_p (arg1)
5440 	  || type_dependent_expression_p (arg2))
5441 	return build_min_nt_loc (loc, VEC_PERM_EXPR, arg0, arg1, arg2);
5442       arg0 = build_non_dependent_expr (arg0);
5443       if (arg1)
5444 	arg1 = build_non_dependent_expr (arg1);
5445       arg2 = build_non_dependent_expr (arg2);
5446     }
5447   tree exp = c_build_vec_perm_expr (loc, arg0, arg1, arg2, complain & tf_error);
5448   if (processing_template_decl && exp != error_mark_node)
5449     return build_min_non_dep (VEC_PERM_EXPR, exp, orig_arg0,
5450 			      orig_arg1, orig_arg2);
5451   return exp;
5452 }
5453 
5454 /* Return a tree for the sum or difference (RESULTCODE says which)
5455    of pointer PTROP and integer INTOP.  */
5456 
5457 static tree
5458 cp_pointer_int_sum (location_t loc, enum tree_code resultcode, tree ptrop,
5459 		    tree intop, tsubst_flags_t complain)
5460 {
5461   tree res_type = TREE_TYPE (ptrop);
5462 
5463   /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
5464      in certain circumstance (when it's valid to do so).  So we need
5465      to make sure it's complete.  We don't need to check here, if we
5466      can actually complete it at all, as those checks will be done in
5467      pointer_int_sum() anyway.  */
5468   complete_type (TREE_TYPE (res_type));
5469 
5470   return pointer_int_sum (loc, resultcode, ptrop,
5471 			  intop, complain & tf_warning_or_error);
5472 }
5473 
5474 /* Return a tree for the difference of pointers OP0 and OP1.
5475    The resulting tree has type int.  If POINTER_SUBTRACT sanitization is
5476    enabled, assign to INSTRUMENT_EXPR call to libsanitizer.  */
5477 
5478 static tree
5479 pointer_diff (location_t loc, tree op0, tree op1, tree ptrtype,
5480 	      tsubst_flags_t complain, tree *instrument_expr)
5481 {
5482   tree result, inttype;
5483   tree restype = ptrdiff_type_node;
5484   tree target_type = TREE_TYPE (ptrtype);
5485 
5486   if (!complete_type_or_else (target_type, NULL_TREE))
5487     return error_mark_node;
5488 
5489   if (VOID_TYPE_P (target_type))
5490     {
5491       if (complain & tf_error)
5492 	permerror (loc, "ISO C++ forbids using pointer of "
5493 		   "type %<void *%> in subtraction");
5494       else
5495 	return error_mark_node;
5496     }
5497   if (TREE_CODE (target_type) == FUNCTION_TYPE)
5498     {
5499       if (complain & tf_error)
5500 	permerror (loc, "ISO C++ forbids using pointer to "
5501 		   "a function in subtraction");
5502       else
5503 	return error_mark_node;
5504     }
5505   if (TREE_CODE (target_type) == METHOD_TYPE)
5506     {
5507       if (complain & tf_error)
5508 	permerror (loc, "ISO C++ forbids using pointer to "
5509 		   "a method in subtraction");
5510       else
5511 	return error_mark_node;
5512     }
5513 
5514   /* Determine integer type result of the subtraction.  This will usually
5515      be the same as the result type (ptrdiff_t), but may need to be a wider
5516      type if pointers for the address space are wider than ptrdiff_t.  */
5517   if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
5518     inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
5519   else
5520     inttype = restype;
5521 
5522   if (sanitize_flags_p (SANITIZE_POINTER_SUBTRACT))
5523     {
5524       op0 = save_expr (op0);
5525       op1 = save_expr (op1);
5526 
5527       tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_SUBTRACT);
5528       *instrument_expr = build_call_expr_loc (loc, tt, 2, op0, op1);
5529     }
5530 
5531   /* First do the subtraction, then build the divide operator
5532      and only convert at the very end.
5533      Do not do default conversions in case restype is a short type.  */
5534 
5535   /* POINTER_DIFF_EXPR requires a signed integer type of the same size as
5536      pointers.  If some platform cannot provide that, or has a larger
5537      ptrdiff_type to support differences larger than half the address
5538      space, cast the pointers to some larger integer type and do the
5539      computations in that type.  */
5540   if (TYPE_PRECISION (inttype) > TYPE_PRECISION (TREE_TYPE (op0)))
5541     op0 = cp_build_binary_op (loc,
5542 			      MINUS_EXPR,
5543 			      cp_convert (inttype, op0, complain),
5544 			      cp_convert (inttype, op1, complain),
5545 			      complain);
5546   else
5547     op0 = build2_loc (loc, POINTER_DIFF_EXPR, inttype, op0, op1);
5548 
5549   /* This generates an error if op1 is a pointer to an incomplete type.  */
5550   if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1))))
5551     {
5552       if (complain & tf_error)
5553 	error_at (loc, "invalid use of a pointer to an incomplete type in "
5554 		  "pointer arithmetic");
5555       else
5556 	return error_mark_node;
5557     }
5558 
5559   if (pointer_to_zero_sized_aggr_p (TREE_TYPE (op1)))
5560     {
5561       if (complain & tf_error)
5562 	error_at (loc, "arithmetic on pointer to an empty aggregate");
5563       else
5564 	return error_mark_node;
5565     }
5566 
5567   op1 = (TYPE_PTROB_P (ptrtype)
5568 	 ? size_in_bytes_loc (loc, target_type)
5569 	 : integer_one_node);
5570 
5571   /* Do the division.  */
5572 
5573   result = build2_loc (loc, EXACT_DIV_EXPR, inttype, op0,
5574 		       cp_convert (inttype, op1, complain));
5575   return cp_convert (restype, result, complain);
5576 }
5577 
5578 /* Construct and perhaps optimize a tree representation
5579    for a unary operation.  CODE, a tree_code, specifies the operation
5580    and XARG is the operand.  */
5581 
5582 tree
5583 build_x_unary_op (location_t loc, enum tree_code code, cp_expr xarg,
5584 		  tsubst_flags_t complain)
5585 {
5586   tree orig_expr = xarg;
5587   tree exp;
5588   int ptrmem = 0;
5589   tree overload = NULL_TREE;
5590 
5591   if (processing_template_decl)
5592     {
5593       if (type_dependent_expression_p (xarg))
5594 	return build_min_nt_loc (loc, code, xarg.get_value (), NULL_TREE);
5595 
5596       xarg = build_non_dependent_expr (xarg);
5597     }
5598 
5599   exp = NULL_TREE;
5600 
5601   /* [expr.unary.op] says:
5602 
5603        The address of an object of incomplete type can be taken.
5604 
5605      (And is just the ordinary address operator, not an overloaded
5606      "operator &".)  However, if the type is a template
5607      specialization, we must complete the type at this point so that
5608      an overloaded "operator &" will be available if required.  */
5609   if (code == ADDR_EXPR
5610       && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
5611       && ((CLASS_TYPE_P (TREE_TYPE (xarg))
5612 	   && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (xarg))))
5613 	  || (TREE_CODE (xarg) == OFFSET_REF)))
5614     /* Don't look for a function.  */;
5615   else
5616     exp = build_new_op (loc, code, LOOKUP_NORMAL, xarg, NULL_TREE,
5617 			NULL_TREE, &overload, complain);
5618 
5619   if (!exp && code == ADDR_EXPR)
5620     {
5621       if (is_overloaded_fn (xarg))
5622 	{
5623 	  tree fn = get_first_fn (xarg);
5624 	  if (DECL_CONSTRUCTOR_P (fn) || DECL_DESTRUCTOR_P (fn))
5625 	    {
5626 	      if (complain & tf_error)
5627 		error (DECL_CONSTRUCTOR_P (fn)
5628 		       ? G_("taking address of constructor %qD")
5629 		       : G_("taking address of destructor %qD"),
5630 		       fn);
5631 	      return error_mark_node;
5632 	    }
5633 	}
5634 
5635       /* A pointer to member-function can be formed only by saying
5636 	 &X::mf.  */
5637       if (!flag_ms_extensions && TREE_CODE (TREE_TYPE (xarg)) == METHOD_TYPE
5638 	  && (TREE_CODE (xarg) != OFFSET_REF || !PTRMEM_OK_P (xarg)))
5639 	{
5640 	  if (TREE_CODE (xarg) != OFFSET_REF
5641 	      || !TYPE_P (TREE_OPERAND (xarg, 0)))
5642 	    {
5643 	      if (complain & tf_error)
5644 		{
5645 		  error ("invalid use of %qE to form a "
5646 			 "pointer-to-member-function", xarg.get_value ());
5647 		  if (TREE_CODE (xarg) != OFFSET_REF)
5648 		    inform (input_location, "  a qualified-id is required");
5649 		}
5650 	      return error_mark_node;
5651 	    }
5652 	  else
5653 	    {
5654 	      if (complain & tf_error)
5655 		error ("parentheses around %qE cannot be used to form a"
5656 		       " pointer-to-member-function",
5657 		       xarg.get_value ());
5658 	      else
5659 		return error_mark_node;
5660 	      PTRMEM_OK_P (xarg) = 1;
5661 	    }
5662 	}
5663 
5664       if (TREE_CODE (xarg) == OFFSET_REF)
5665 	{
5666 	  ptrmem = PTRMEM_OK_P (xarg);
5667 
5668 	  if (!ptrmem && !flag_ms_extensions
5669 	      && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg, 1))) == METHOD_TYPE)
5670 	    {
5671 	      /* A single non-static member, make sure we don't allow a
5672 		 pointer-to-member.  */
5673 	      xarg = build2 (OFFSET_REF, TREE_TYPE (xarg),
5674 			     TREE_OPERAND (xarg, 0),
5675 			     ovl_make (TREE_OPERAND (xarg, 1)));
5676 	      PTRMEM_OK_P (xarg) = ptrmem;
5677 	    }
5678 	}
5679 
5680       exp = cp_build_addr_expr_strict (xarg, complain);
5681     }
5682 
5683   if (processing_template_decl && exp != error_mark_node)
5684     {
5685       if (overload != NULL_TREE)
5686 	return (build_min_non_dep_op_overload
5687 		(code, exp, overload, orig_expr, integer_zero_node));
5688 
5689       exp = build_min_non_dep (code, exp, orig_expr,
5690 			       /*For {PRE,POST}{INC,DEC}REMENT_EXPR*/NULL_TREE);
5691     }
5692   if (TREE_CODE (exp) == ADDR_EXPR)
5693     PTRMEM_OK_P (exp) = ptrmem;
5694   return exp;
5695 }
5696 
5697 /* Construct and perhaps optimize a tree representation
5698    for __builtin_addressof operation.  ARG specifies the operand.  */
5699 
5700 tree
5701 cp_build_addressof (location_t loc, tree arg, tsubst_flags_t complain)
5702 {
5703   tree orig_expr = arg;
5704 
5705   if (processing_template_decl)
5706     {
5707       if (type_dependent_expression_p (arg))
5708 	return build_min_nt_loc (loc, ADDRESSOF_EXPR, arg, NULL_TREE);
5709 
5710       arg = build_non_dependent_expr (arg);
5711     }
5712 
5713   tree exp = cp_build_addr_expr_strict (arg, complain);
5714 
5715   if (processing_template_decl && exp != error_mark_node)
5716     exp = build_min_non_dep (ADDRESSOF_EXPR, exp, orig_expr, NULL_TREE);
5717   return exp;
5718 }
5719 
5720 /* Like c_common_truthvalue_conversion, but handle pointer-to-member
5721    constants, where a null value is represented by an INTEGER_CST of
5722    -1.  */
5723 
5724 tree
5725 cp_truthvalue_conversion (tree expr)
5726 {
5727   tree type = TREE_TYPE (expr);
5728   if (TYPE_PTR_OR_PTRMEM_P (type)
5729       /* Avoid ICE on invalid use of non-static member function.  */
5730       || TREE_CODE (expr) == FUNCTION_DECL)
5731     return build_binary_op (input_location, NE_EXPR, expr, nullptr_node, true);
5732   else
5733     return c_common_truthvalue_conversion (input_location, expr);
5734 }
5735 
5736 /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR.  */
5737 
5738 tree
5739 condition_conversion (tree expr)
5740 {
5741   tree t;
5742   /* Anything that might happen in a template should go through
5743      maybe_convert_cond.  */
5744   gcc_assert (!processing_template_decl);
5745   t = perform_implicit_conversion_flags (boolean_type_node, expr,
5746 					 tf_warning_or_error, LOOKUP_NORMAL);
5747   t = fold_build_cleanup_point_expr (boolean_type_node, t);
5748   return t;
5749 }
5750 
5751 /* Returns the address of T.  This function will fold away
5752    ADDR_EXPR of INDIRECT_REF.  */
5753 
5754 tree
5755 build_address (tree t)
5756 {
5757   if (error_operand_p (t) || !cxx_mark_addressable (t))
5758     return error_mark_node;
5759   gcc_checking_assert (TREE_CODE (t) != CONSTRUCTOR
5760 		       || processing_template_decl);
5761   t = build_fold_addr_expr_loc (EXPR_LOCATION (t), t);
5762   if (TREE_CODE (t) != ADDR_EXPR)
5763     t = rvalue (t);
5764   return t;
5765 }
5766 
5767 /* Return a NOP_EXPR converting EXPR to TYPE.  */
5768 
5769 tree
5770 build_nop (tree type, tree expr)
5771 {
5772   if (type == error_mark_node || error_operand_p (expr))
5773     return expr;
5774   return build1_loc (EXPR_LOCATION (expr), NOP_EXPR, type, expr);
5775 }
5776 
5777 /* Take the address of ARG, whatever that means under C++ semantics.
5778    If STRICT_LVALUE is true, require an lvalue; otherwise, allow xvalues
5779    and class rvalues as well.
5780 
5781    Nothing should call this function directly; instead, callers should use
5782    cp_build_addr_expr or cp_build_addr_expr_strict.  */
5783 
5784 static tree
5785 cp_build_addr_expr_1 (tree arg, bool strict_lvalue, tsubst_flags_t complain)
5786 {
5787   tree argtype;
5788   tree val;
5789 
5790   if (!arg || error_operand_p (arg))
5791     return error_mark_node;
5792 
5793   arg = mark_lvalue_use (arg);
5794   if (error_operand_p (arg))
5795     return error_mark_node;
5796 
5797   argtype = lvalue_type (arg);
5798 
5799   gcc_assert (!(identifier_p (arg) && IDENTIFIER_ANY_OP_P (arg)));
5800 
5801   if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg)
5802       && !really_overloaded_fn (arg))
5803     {
5804       /* They're trying to take the address of a unique non-static
5805 	 member function.  This is ill-formed (except in MS-land),
5806 	 but let's try to DTRT.
5807 	 Note: We only handle unique functions here because we don't
5808 	 want to complain if there's a static overload; non-unique
5809 	 cases will be handled by instantiate_type.  But we need to
5810 	 handle this case here to allow casts on the resulting PMF.
5811 	 We could defer this in non-MS mode, but it's easier to give
5812 	 a useful error here.  */
5813 
5814       /* Inside constant member functions, the `this' pointer
5815 	 contains an extra const qualifier.  TYPE_MAIN_VARIANT
5816 	 is used here to remove this const from the diagnostics
5817 	 and the created OFFSET_REF.  */
5818       tree base = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg, 0)));
5819       tree fn = get_first_fn (TREE_OPERAND (arg, 1));
5820       if (!mark_used (fn, complain) && !(complain & tf_error))
5821 	return error_mark_node;
5822 
5823       if (! flag_ms_extensions)
5824 	{
5825 	  tree name = DECL_NAME (fn);
5826 	  if (!(complain & tf_error))
5827 	    return error_mark_node;
5828 	  else if (current_class_type
5829 		   && TREE_OPERAND (arg, 0) == current_class_ref)
5830 	    /* An expression like &memfn.  */
5831 	    permerror (input_location, "ISO C++ forbids taking the address of an unqualified"
5832 		       " or parenthesized non-static member function to form"
5833 		       " a pointer to member function.  Say %<&%T::%D%>",
5834 		       base, name);
5835 	  else
5836 	    permerror (input_location, "ISO C++ forbids taking the address of a bound member"
5837 		       " function to form a pointer to member function."
5838 		       "  Say %<&%T::%D%>",
5839 		       base, name);
5840 	}
5841       arg = build_offset_ref (base, fn, /*address_p=*/true, complain);
5842     }
5843 
5844   /* Uninstantiated types are all functions.  Taking the
5845      address of a function is a no-op, so just return the
5846      argument.  */
5847   if (type_unknown_p (arg))
5848     return build1 (ADDR_EXPR, unknown_type_node, arg);
5849 
5850   if (TREE_CODE (arg) == OFFSET_REF)
5851     /* We want a pointer to member; bypass all the code for actually taking
5852        the address of something.  */
5853     goto offset_ref;
5854 
5855   /* Anything not already handled and not a true memory reference
5856      is an error.  */
5857   if (TREE_CODE (argtype) != FUNCTION_TYPE
5858       && TREE_CODE (argtype) != METHOD_TYPE)
5859     {
5860       cp_lvalue_kind kind = lvalue_kind (arg);
5861       if (kind == clk_none)
5862 	{
5863 	  if (complain & tf_error)
5864 	    lvalue_error (input_location, lv_addressof);
5865 	  return error_mark_node;
5866 	}
5867       if (strict_lvalue && (kind & (clk_rvalueref|clk_class)))
5868 	{
5869 	  if (!(complain & tf_error))
5870 	    return error_mark_node;
5871 	  if (kind & clk_class)
5872 	    /* Make this a permerror because we used to accept it.  */
5873 	    permerror (input_location, "taking address of temporary");
5874 	  else
5875 	    error ("taking address of xvalue (rvalue reference)");
5876 	}
5877     }
5878 
5879   if (TREE_CODE (argtype) == REFERENCE_TYPE)
5880     {
5881       tree type = build_pointer_type (TREE_TYPE (argtype));
5882       arg = build1 (CONVERT_EXPR, type, arg);
5883       return arg;
5884     }
5885   else if (pedantic && DECL_MAIN_P (arg))
5886     {
5887       /* ARM $3.4 */
5888       /* Apparently a lot of autoconf scripts for C++ packages do this,
5889 	 so only complain if -Wpedantic.  */
5890       if (complain & (flag_pedantic_errors ? tf_error : tf_warning))
5891 	pedwarn (input_location, OPT_Wpedantic,
5892 		 "ISO C++ forbids taking address of function %<::main%>");
5893       else if (flag_pedantic_errors)
5894 	return error_mark_node;
5895     }
5896 
5897   /* Let &* cancel out to simplify resulting code.  */
5898   if (INDIRECT_REF_P (arg))
5899     {
5900       arg = TREE_OPERAND (arg, 0);
5901       if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE)
5902 	{
5903 	  tree type = build_pointer_type (TREE_TYPE (TREE_TYPE (arg)));
5904 	  arg = build1 (CONVERT_EXPR, type, arg);
5905 	}
5906       else
5907 	/* Don't let this be an lvalue.  */
5908 	arg = rvalue (arg);
5909       return arg;
5910     }
5911 
5912   /* Handle complex lvalues (when permitted)
5913      by reduction to simpler cases.  */
5914   val = unary_complex_lvalue (ADDR_EXPR, arg);
5915   if (val != 0)
5916     return val;
5917 
5918   switch (TREE_CODE (arg))
5919     {
5920     CASE_CONVERT:
5921     case FLOAT_EXPR:
5922     case FIX_TRUNC_EXPR:
5923       /* We should have handled this above in the lvalue_kind check.  */
5924       gcc_unreachable ();
5925       break;
5926 
5927     case BASELINK:
5928       arg = BASELINK_FUNCTIONS (arg);
5929       /* Fall through.  */
5930 
5931     case OVERLOAD:
5932       arg = OVL_FIRST (arg);
5933       break;
5934 
5935     case OFFSET_REF:
5936     offset_ref:
5937       /* Turn a reference to a non-static data member into a
5938 	 pointer-to-member.  */
5939       {
5940 	tree type;
5941 	tree t;
5942 
5943 	gcc_assert (PTRMEM_OK_P (arg));
5944 
5945 	t = TREE_OPERAND (arg, 1);
5946 	if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
5947 	  {
5948 	    if (complain & tf_error)
5949 	      error ("cannot create pointer to reference member %qD", t);
5950 	    return error_mark_node;
5951 	  }
5952 
5953 	type = build_ptrmem_type (context_for_name_lookup (t),
5954 				  TREE_TYPE (t));
5955 	t = make_ptrmem_cst (type, TREE_OPERAND (arg, 1));
5956 	return t;
5957       }
5958 
5959     default:
5960       break;
5961     }
5962 
5963   if (argtype != error_mark_node)
5964     argtype = build_pointer_type (argtype);
5965 
5966   if (bitfield_p (arg))
5967     {
5968       if (complain & tf_error)
5969 	error ("attempt to take address of bit-field");
5970       return error_mark_node;
5971     }
5972 
5973   /* In a template, we are processing a non-dependent expression
5974      so we can just form an ADDR_EXPR with the correct type.  */
5975   if (processing_template_decl || TREE_CODE (arg) != COMPONENT_REF)
5976     {
5977       if (TREE_CODE (arg) == FUNCTION_DECL
5978 	  && !mark_used (arg, complain) && !(complain & tf_error))
5979 	return error_mark_node;
5980       val = build_address (arg);
5981       if (TREE_CODE (arg) == OFFSET_REF)
5982 	PTRMEM_OK_P (val) = PTRMEM_OK_P (arg);
5983     }
5984   else if (BASELINK_P (TREE_OPERAND (arg, 1)))
5985     {
5986       tree fn = BASELINK_FUNCTIONS (TREE_OPERAND (arg, 1));
5987 
5988       /* We can only get here with a single static member
5989 	 function.  */
5990       gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
5991 		  && DECL_STATIC_FUNCTION_P (fn));
5992       if (!mark_used (fn, complain) && !(complain & tf_error))
5993 	return error_mark_node;
5994       val = build_address (fn);
5995       if (TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
5996 	/* Do not lose object's side effects.  */
5997 	val = build2 (COMPOUND_EXPR, TREE_TYPE (val),
5998 		      TREE_OPERAND (arg, 0), val);
5999     }
6000   else
6001     {
6002       tree object = TREE_OPERAND (arg, 0);
6003       tree field = TREE_OPERAND (arg, 1);
6004       gcc_assert (same_type_ignoring_top_level_qualifiers_p
6005 		  (TREE_TYPE (object), decl_type_context (field)));
6006       val = build_address (arg);
6007     }
6008 
6009   if (TYPE_PTR_P (argtype)
6010       && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
6011     {
6012       build_ptrmemfunc_type (argtype);
6013       val = build_ptrmemfunc (argtype, val, 0,
6014 			      /*c_cast_p=*/false,
6015 			      complain);
6016     }
6017 
6018   return val;
6019 }
6020 
6021 /* Take the address of ARG if it has one, even if it's an rvalue.  */
6022 
6023 tree
6024 cp_build_addr_expr (tree arg, tsubst_flags_t complain)
6025 {
6026   return cp_build_addr_expr_1 (arg, 0, complain);
6027 }
6028 
6029 /* Take the address of ARG, but only if it's an lvalue.  */
6030 
6031 static tree
6032 cp_build_addr_expr_strict (tree arg, tsubst_flags_t complain)
6033 {
6034   return cp_build_addr_expr_1 (arg, 1, complain);
6035 }
6036 
6037 /* C++: Must handle pointers to members.
6038 
6039    Perhaps type instantiation should be extended to handle conversion
6040    from aggregates to types we don't yet know we want?  (Or are those
6041    cases typically errors which should be reported?)
6042 
6043    NOCONVERT suppresses the default promotions (such as from short to int).  */
6044 
6045 tree
6046 cp_build_unary_op (enum tree_code code, tree xarg, bool noconvert,
6047                    tsubst_flags_t complain)
6048 {
6049   /* No default_conversion here.  It causes trouble for ADDR_EXPR.  */
6050   tree arg = xarg;
6051   location_t location = EXPR_LOC_OR_LOC (arg, input_location);
6052   tree argtype = 0;
6053   const char *errstring = NULL;
6054   tree val;
6055   const char *invalid_op_diag;
6056 
6057   if (!arg || error_operand_p (arg))
6058     return error_mark_node;
6059 
6060   if ((invalid_op_diag
6061        = targetm.invalid_unary_op ((code == UNARY_PLUS_EXPR
6062 				    ? CONVERT_EXPR
6063 				    : code),
6064 				   TREE_TYPE (xarg))))
6065     {
6066       if (complain & tf_error)
6067 	error (invalid_op_diag);
6068       return error_mark_node;
6069     }
6070 
6071   switch (code)
6072     {
6073     case UNARY_PLUS_EXPR:
6074     case NEGATE_EXPR:
6075       {
6076 	int flags = WANT_ARITH | WANT_ENUM;
6077 	/* Unary plus (but not unary minus) is allowed on pointers.  */
6078 	if (code == UNARY_PLUS_EXPR)
6079 	  flags |= WANT_POINTER;
6080 	arg = build_expr_type_conversion (flags, arg, true);
6081 	if (!arg)
6082 	  errstring = (code == NEGATE_EXPR
6083 		       ? _("wrong type argument to unary minus")
6084 		       : _("wrong type argument to unary plus"));
6085 	else
6086 	  {
6087 	    if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
6088 	      arg = cp_perform_integral_promotions (arg, complain);
6089 
6090 	    /* Make sure the result is not an lvalue: a unary plus or minus
6091 	       expression is always a rvalue.  */
6092 	    arg = rvalue (arg);
6093 	  }
6094       }
6095       break;
6096 
6097     case BIT_NOT_EXPR:
6098       if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
6099 	{
6100 	  code = CONJ_EXPR;
6101 	  if (!noconvert)
6102 	    {
6103 	      arg = cp_default_conversion (arg, complain);
6104 	      if (arg == error_mark_node)
6105 		return error_mark_node;
6106 	    }
6107 	}
6108       else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM
6109 						   | WANT_VECTOR_OR_COMPLEX,
6110 						   arg, true)))
6111 	errstring = _("wrong type argument to bit-complement");
6112       else if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
6113 	{
6114 	  /* Warn if the expression has boolean value.  */
6115 	  if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE
6116 	      && (complain & tf_warning)
6117 	      && warning_at (location, OPT_Wbool_operation,
6118 			     "%<~%> on an expression of type bool"))
6119 	    inform (location, "did you mean to use logical not (%<!%>)?");
6120 	  arg = cp_perform_integral_promotions (arg, complain);
6121 	}
6122       else if (!noconvert && VECTOR_TYPE_P (TREE_TYPE (arg)))
6123 	arg = mark_rvalue_use (arg);
6124       break;
6125 
6126     case ABS_EXPR:
6127       if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
6128 	errstring = _("wrong type argument to abs");
6129       else if (!noconvert)
6130 	{
6131 	  arg = cp_default_conversion (arg, complain);
6132 	  if (arg == error_mark_node)
6133 	    return error_mark_node;
6134 	}
6135       break;
6136 
6137     case CONJ_EXPR:
6138       /* Conjugating a real value is a no-op, but allow it anyway.  */
6139       if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
6140 	errstring = _("wrong type argument to conjugation");
6141       else if (!noconvert)
6142 	{
6143 	  arg = cp_default_conversion (arg, complain);
6144 	  if (arg == error_mark_node)
6145 	    return error_mark_node;
6146 	}
6147       break;
6148 
6149     case TRUTH_NOT_EXPR:
6150       if (VECTOR_TYPE_P (TREE_TYPE (arg)))
6151 	return cp_build_binary_op (input_location, EQ_EXPR, arg,
6152 				   build_zero_cst (TREE_TYPE (arg)), complain);
6153       arg = perform_implicit_conversion (boolean_type_node, arg,
6154 					 complain);
6155       val = invert_truthvalue_loc (input_location, arg);
6156       if (arg != error_mark_node)
6157 	return val;
6158       errstring = _("in argument to unary !");
6159       break;
6160 
6161     case NOP_EXPR:
6162       break;
6163 
6164     case REALPART_EXPR:
6165     case IMAGPART_EXPR:
6166       arg = build_real_imag_expr (input_location, code, arg);
6167       return arg;
6168 
6169     case PREINCREMENT_EXPR:
6170     case POSTINCREMENT_EXPR:
6171     case PREDECREMENT_EXPR:
6172     case POSTDECREMENT_EXPR:
6173       /* Handle complex lvalues (when permitted)
6174 	 by reduction to simpler cases.  */
6175 
6176       val = unary_complex_lvalue (code, arg);
6177       if (val != 0)
6178 	return val;
6179 
6180       arg = mark_lvalue_use (arg);
6181 
6182       /* Increment or decrement the real part of the value,
6183 	 and don't change the imaginary part.  */
6184       if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
6185 	{
6186 	  tree real, imag;
6187 
6188 	  arg = cp_stabilize_reference (arg);
6189 	  real = cp_build_unary_op (REALPART_EXPR, arg, true, complain);
6190 	  imag = cp_build_unary_op (IMAGPART_EXPR, arg, true, complain);
6191 	  real = cp_build_unary_op (code, real, true, complain);
6192 	  if (real == error_mark_node || imag == error_mark_node)
6193 	    return error_mark_node;
6194 	  return build2 (COMPLEX_EXPR, TREE_TYPE (arg),
6195 			 real, imag);
6196 	}
6197 
6198       /* Report invalid types.  */
6199 
6200       if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
6201 					      arg, true)))
6202 	{
6203 	  if (code == PREINCREMENT_EXPR)
6204 	    errstring = _("no pre-increment operator for type");
6205 	  else if (code == POSTINCREMENT_EXPR)
6206 	    errstring = _("no post-increment operator for type");
6207 	  else if (code == PREDECREMENT_EXPR)
6208 	    errstring = _("no pre-decrement operator for type");
6209 	  else
6210 	    errstring = _("no post-decrement operator for type");
6211 	  break;
6212 	}
6213       else if (arg == error_mark_node)
6214 	return error_mark_node;
6215 
6216       /* Report something read-only.  */
6217 
6218       if (CP_TYPE_CONST_P (TREE_TYPE (arg))
6219 	  || TREE_READONLY (arg))
6220         {
6221           if (complain & tf_error)
6222             cxx_readonly_error (arg, ((code == PREINCREMENT_EXPR
6223 				      || code == POSTINCREMENT_EXPR)
6224 				     ? lv_increment : lv_decrement));
6225           else
6226             return error_mark_node;
6227         }
6228 
6229       {
6230 	tree inc;
6231 	tree declared_type = unlowered_expr_type (arg);
6232 
6233 	argtype = TREE_TYPE (arg);
6234 
6235 	/* ARM $5.2.5 last annotation says this should be forbidden.  */
6236 	if (TREE_CODE (argtype) == ENUMERAL_TYPE)
6237           {
6238             if (complain & tf_error)
6239               permerror (input_location, (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
6240                          ? G_("ISO C++ forbids incrementing an enum")
6241                          : G_("ISO C++ forbids decrementing an enum"));
6242             else
6243               return error_mark_node;
6244           }
6245 
6246 	/* Compute the increment.  */
6247 
6248 	if (TYPE_PTR_P (argtype))
6249 	  {
6250 	    tree type = complete_type (TREE_TYPE (argtype));
6251 
6252 	    if (!COMPLETE_OR_VOID_TYPE_P (type))
6253               {
6254                 if (complain & tf_error)
6255                   error (((code == PREINCREMENT_EXPR
6256                            || code == POSTINCREMENT_EXPR))
6257                          ? G_("cannot increment a pointer to incomplete type %qT")
6258                          : G_("cannot decrement a pointer to incomplete type %qT"),
6259                          TREE_TYPE (argtype));
6260                 else
6261                   return error_mark_node;
6262               }
6263 	    else if (!TYPE_PTROB_P (argtype))
6264               {
6265                 if (complain & tf_error)
6266                   pedwarn (input_location, OPT_Wpointer_arith,
6267 			   (code == PREINCREMENT_EXPR
6268                               || code == POSTINCREMENT_EXPR)
6269 			   ? G_("ISO C++ forbids incrementing a pointer of type %qT")
6270 			   : G_("ISO C++ forbids decrementing a pointer of type %qT"),
6271 			   argtype);
6272                 else
6273                   return error_mark_node;
6274               }
6275 
6276 	    inc = cxx_sizeof_nowarn (TREE_TYPE (argtype));
6277 	  }
6278 	else
6279 	  inc = VECTOR_TYPE_P (argtype)
6280 	    ? build_one_cst (argtype)
6281 	    : integer_one_node;
6282 
6283 	inc = cp_convert (argtype, inc, complain);
6284 
6285 	/* If 'arg' is an Objective-C PROPERTY_REF expression, then we
6286 	   need to ask Objective-C to build the increment or decrement
6287 	   expression for it.  */
6288 	if (objc_is_property_ref (arg))
6289 	  return objc_build_incr_expr_for_property_ref (input_location, code,
6290 							arg, inc);
6291 
6292 	/* Complain about anything else that is not a true lvalue.  */
6293 	if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
6294 				    || code == POSTINCREMENT_EXPR)
6295 				   ? lv_increment : lv_decrement),
6296                              complain))
6297 	  return error_mark_node;
6298 
6299 	/* Forbid using -- or ++ in C++17 on `bool'.  */
6300 	if (TREE_CODE (declared_type) == BOOLEAN_TYPE)
6301 	  {
6302 	    if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
6303 	      {
6304                 if (complain & tf_error)
6305 		  error ("use of an operand of type %qT in %<operator--%> "
6306 			 "is forbidden", boolean_type_node);
6307 		return error_mark_node;
6308 	      }
6309 	    else
6310 	      {
6311 		if (cxx_dialect >= cxx17)
6312 		  {
6313 		    if (complain & tf_error)
6314 		      error ("use of an operand of type %qT in "
6315 			     "%<operator++%> is forbidden in C++17",
6316 			     boolean_type_node);
6317 		    return error_mark_node;
6318 		  }
6319 		/* Otherwise, [depr.incr.bool] says this is deprecated.  */
6320 		else if (!in_system_header_at (input_location))
6321 		  warning (OPT_Wdeprecated, "use of an operand of type %qT "
6322 			   "in %<operator++%> is deprecated",
6323 			   boolean_type_node);
6324 	      }
6325 	    val = boolean_increment (code, arg);
6326 	  }
6327 	else if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
6328 	  /* An rvalue has no cv-qualifiers.  */
6329 	  val = build2 (code, cv_unqualified (TREE_TYPE (arg)), arg, inc);
6330 	else
6331 	  val = build2 (code, TREE_TYPE (arg), arg, inc);
6332 
6333 	TREE_SIDE_EFFECTS (val) = 1;
6334 	return val;
6335       }
6336 
6337     case ADDR_EXPR:
6338       /* Note that this operation never does default_conversion
6339 	 regardless of NOCONVERT.  */
6340       return cp_build_addr_expr (arg, complain);
6341 
6342     default:
6343       break;
6344     }
6345 
6346   if (!errstring)
6347     {
6348       if (argtype == 0)
6349 	argtype = TREE_TYPE (arg);
6350       return build1 (code, argtype, arg);
6351     }
6352 
6353   if (complain & tf_error)
6354     error ("%s", errstring);
6355   return error_mark_node;
6356 }
6357 
6358 /* Hook for the c-common bits that build a unary op.  */
6359 tree
6360 build_unary_op (location_t /*location*/,
6361 		enum tree_code code, tree xarg, bool noconvert)
6362 {
6363   return cp_build_unary_op (code, xarg, noconvert, tf_warning_or_error);
6364 }
6365 
6366 /* Adjust LVALUE, an MODIFY_EXPR, PREINCREMENT_EXPR or PREDECREMENT_EXPR,
6367    so that it is a valid lvalue even for GENERIC by replacing
6368    (lhs = rhs) with ((lhs = rhs), lhs)
6369    (--lhs) with ((--lhs), lhs)
6370    (++lhs) with ((++lhs), lhs)
6371    and if lhs has side-effects, calling cp_stabilize_reference on it, so
6372    that it can be evaluated multiple times.  */
6373 
6374 tree
6375 genericize_compound_lvalue (tree lvalue)
6376 {
6377   if (TREE_SIDE_EFFECTS (TREE_OPERAND (lvalue, 0)))
6378     lvalue = build2 (TREE_CODE (lvalue), TREE_TYPE (lvalue),
6379 		     cp_stabilize_reference (TREE_OPERAND (lvalue, 0)),
6380 		     TREE_OPERAND (lvalue, 1));
6381   return build2 (COMPOUND_EXPR, TREE_TYPE (TREE_OPERAND (lvalue, 0)),
6382 		 lvalue, TREE_OPERAND (lvalue, 0));
6383 }
6384 
6385 /* Apply unary lvalue-demanding operator CODE to the expression ARG
6386    for certain kinds of expressions which are not really lvalues
6387    but which we can accept as lvalues.
6388 
6389    If ARG is not a kind of expression we can handle, return
6390    NULL_TREE.  */
6391 
6392 tree
6393 unary_complex_lvalue (enum tree_code code, tree arg)
6394 {
6395   /* Inside a template, making these kinds of adjustments is
6396      pointless; we are only concerned with the type of the
6397      expression.  */
6398   if (processing_template_decl)
6399     return NULL_TREE;
6400 
6401   /* Handle (a, b) used as an "lvalue".  */
6402   if (TREE_CODE (arg) == COMPOUND_EXPR)
6403     {
6404       tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 1), false,
6405                                             tf_warning_or_error);
6406       return build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
6407 		     TREE_OPERAND (arg, 0), real_result);
6408     }
6409 
6410   /* Handle (a ? b : c) used as an "lvalue".  */
6411   if (TREE_CODE (arg) == COND_EXPR
6412       || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
6413     return rationalize_conditional_expr (code, arg, tf_warning_or_error);
6414 
6415   /* Handle (a = b), (++a), and (--a) used as an "lvalue".  */
6416   if (TREE_CODE (arg) == MODIFY_EXPR
6417       || TREE_CODE (arg) == PREINCREMENT_EXPR
6418       || TREE_CODE (arg) == PREDECREMENT_EXPR)
6419     return unary_complex_lvalue (code, genericize_compound_lvalue (arg));
6420 
6421   if (code != ADDR_EXPR)
6422     return NULL_TREE;
6423 
6424   /* Handle (a = b) used as an "lvalue" for `&'.  */
6425   if (TREE_CODE (arg) == MODIFY_EXPR
6426       || TREE_CODE (arg) == INIT_EXPR)
6427     {
6428       tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 0), false,
6429                                             tf_warning_or_error);
6430       arg = build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
6431 		    arg, real_result);
6432       TREE_NO_WARNING (arg) = 1;
6433       return arg;
6434     }
6435 
6436   if (TREE_CODE (TREE_TYPE (arg)) == FUNCTION_TYPE
6437       || TREE_CODE (TREE_TYPE (arg)) == METHOD_TYPE
6438       || TREE_CODE (arg) == OFFSET_REF)
6439     return NULL_TREE;
6440 
6441   /* We permit compiler to make function calls returning
6442      objects of aggregate type look like lvalues.  */
6443   {
6444     tree targ = arg;
6445 
6446     if (TREE_CODE (targ) == SAVE_EXPR)
6447       targ = TREE_OPERAND (targ, 0);
6448 
6449     if (TREE_CODE (targ) == CALL_EXPR && MAYBE_CLASS_TYPE_P (TREE_TYPE (targ)))
6450       {
6451 	if (TREE_CODE (arg) == SAVE_EXPR)
6452 	  targ = arg;
6453 	else
6454 	  targ = build_cplus_new (TREE_TYPE (arg), arg, tf_warning_or_error);
6455 	return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
6456       }
6457 
6458     if (TREE_CODE (arg) == SAVE_EXPR && INDIRECT_REF_P (targ))
6459       return build3 (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
6460 		     TREE_OPERAND (targ, 0), current_function_decl, NULL);
6461   }
6462 
6463   /* Don't let anything else be handled specially.  */
6464   return NULL_TREE;
6465 }
6466 
6467 /* Mark EXP saying that we need to be able to take the
6468    address of it; it should not be allocated in a register.
6469    Value is true if successful.  ARRAY_REF_P is true if this
6470    is for ARRAY_REF construction - in that case we don't want
6471    to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
6472    it is fine to use ARRAY_REFs for vector subscripts on vector
6473    register variables.
6474 
6475    C++: we do not allow `current_class_ptr' to be addressable.  */
6476 
6477 bool
6478 cxx_mark_addressable (tree exp, bool array_ref_p)
6479 {
6480   tree x = exp;
6481 
6482   while (1)
6483     switch (TREE_CODE (x))
6484       {
6485       case VIEW_CONVERT_EXPR:
6486 	if (array_ref_p
6487 	    && TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
6488 	    && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x, 0))))
6489 	  return true;
6490 	/* FALLTHRU */
6491       case ADDR_EXPR:
6492       case COMPONENT_REF:
6493       case ARRAY_REF:
6494       case REALPART_EXPR:
6495       case IMAGPART_EXPR:
6496 	x = TREE_OPERAND (x, 0);
6497 	break;
6498 
6499       case PARM_DECL:
6500 	if (x == current_class_ptr)
6501 	  {
6502 	    error ("cannot take the address of %<this%>, which is an rvalue expression");
6503 	    TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later.  */
6504 	    return true;
6505 	  }
6506 	/* Fall through.  */
6507 
6508       case VAR_DECL:
6509 	/* Caller should not be trying to mark initialized
6510 	   constant fields addressable.  */
6511 	gcc_assert (DECL_LANG_SPECIFIC (x) == 0
6512 		    || DECL_IN_AGGR_P (x) == 0
6513 		    || TREE_STATIC (x)
6514 		    || DECL_EXTERNAL (x));
6515 	/* Fall through.  */
6516 
6517       case RESULT_DECL:
6518 	if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
6519 	    && !DECL_ARTIFICIAL (x))
6520 	  {
6521 	    if (VAR_P (x) && DECL_HARD_REGISTER (x))
6522 	      {
6523 		error
6524 		  ("address of explicit register variable %qD requested", x);
6525 		return false;
6526 	      }
6527 	    else if (extra_warnings)
6528 	      warning
6529 		(OPT_Wextra, "address requested for %qD, which is declared %<register%>", x);
6530 	  }
6531 	TREE_ADDRESSABLE (x) = 1;
6532 	return true;
6533 
6534       case CONST_DECL:
6535       case FUNCTION_DECL:
6536 	TREE_ADDRESSABLE (x) = 1;
6537 	return true;
6538 
6539       case CONSTRUCTOR:
6540 	TREE_ADDRESSABLE (x) = 1;
6541 	return true;
6542 
6543       case TARGET_EXPR:
6544 	TREE_ADDRESSABLE (x) = 1;
6545 	cxx_mark_addressable (TREE_OPERAND (x, 0));
6546 	return true;
6547 
6548       default:
6549 	return true;
6550     }
6551 }
6552 
6553 /* Build and return a conditional expression IFEXP ? OP1 : OP2.  */
6554 
6555 tree
6556 build_x_conditional_expr (location_t loc, tree ifexp, tree op1, tree op2,
6557                           tsubst_flags_t complain)
6558 {
6559   tree orig_ifexp = ifexp;
6560   tree orig_op1 = op1;
6561   tree orig_op2 = op2;
6562   tree expr;
6563 
6564   if (processing_template_decl)
6565     {
6566       /* The standard says that the expression is type-dependent if
6567 	 IFEXP is type-dependent, even though the eventual type of the
6568 	 expression doesn't dependent on IFEXP.  */
6569       if (type_dependent_expression_p (ifexp)
6570 	  /* As a GNU extension, the middle operand may be omitted.  */
6571 	  || (op1 && type_dependent_expression_p (op1))
6572 	  || type_dependent_expression_p (op2))
6573 	return build_min_nt_loc (loc, COND_EXPR, ifexp, op1, op2);
6574       ifexp = build_non_dependent_expr (ifexp);
6575       if (op1)
6576 	op1 = build_non_dependent_expr (op1);
6577       op2 = build_non_dependent_expr (op2);
6578     }
6579 
6580   expr = build_conditional_expr (loc, ifexp, op1, op2, complain);
6581   if (processing_template_decl && expr != error_mark_node)
6582     {
6583       tree min = build_min_non_dep (COND_EXPR, expr,
6584 				    orig_ifexp, orig_op1, orig_op2);
6585       expr = convert_from_reference (min);
6586     }
6587   return expr;
6588 }
6589 
6590 /* Given a list of expressions, return a compound expression
6591    that performs them all and returns the value of the last of them.  */
6592 
6593 tree
6594 build_x_compound_expr_from_list (tree list, expr_list_kind exp,
6595 				 tsubst_flags_t complain)
6596 {
6597   tree expr = TREE_VALUE (list);
6598 
6599   if (BRACE_ENCLOSED_INITIALIZER_P (expr)
6600       && !CONSTRUCTOR_IS_DIRECT_INIT (expr))
6601     {
6602       if (complain & tf_error)
6603 	pedwarn (EXPR_LOC_OR_LOC (expr, input_location), 0,
6604 		 "list-initializer for non-class type must not "
6605 		 "be parenthesized");
6606       else
6607 	return error_mark_node;
6608     }
6609 
6610   if (TREE_CHAIN (list))
6611     {
6612       if (complain & tf_error)
6613 	switch (exp)
6614 	  {
6615 	  case ELK_INIT:
6616 	    permerror (input_location, "expression list treated as compound "
6617 				       "expression in initializer");
6618 	    break;
6619 	  case ELK_MEM_INIT:
6620 	    permerror (input_location, "expression list treated as compound "
6621 				       "expression in mem-initializer");
6622 	    break;
6623 	  case ELK_FUNC_CAST:
6624 	    permerror (input_location, "expression list treated as compound "
6625 				       "expression in functional cast");
6626 	    break;
6627 	  default:
6628 	    gcc_unreachable ();
6629 	  }
6630       else
6631 	return error_mark_node;
6632 
6633       for (list = TREE_CHAIN (list); list; list = TREE_CHAIN (list))
6634 	expr = build_x_compound_expr (EXPR_LOCATION (TREE_VALUE (list)),
6635 				      expr, TREE_VALUE (list), complain);
6636     }
6637 
6638   return expr;
6639 }
6640 
6641 /* Like build_x_compound_expr_from_list, but using a VEC.  */
6642 
6643 tree
6644 build_x_compound_expr_from_vec (vec<tree, va_gc> *vec, const char *msg,
6645 				tsubst_flags_t complain)
6646 {
6647   if (vec_safe_is_empty (vec))
6648     return NULL_TREE;
6649   else if (vec->length () == 1)
6650     return (*vec)[0];
6651   else
6652     {
6653       tree expr;
6654       unsigned int ix;
6655       tree t;
6656 
6657       if (msg != NULL)
6658 	{
6659 	  if (complain & tf_error)
6660 	    permerror (input_location,
6661 		       "%s expression list treated as compound expression",
6662 		       msg);
6663 	  else
6664 	    return error_mark_node;
6665 	}
6666 
6667       expr = (*vec)[0];
6668       for (ix = 1; vec->iterate (ix, &t); ++ix)
6669 	expr = build_x_compound_expr (EXPR_LOCATION (t), expr,
6670 				      t, complain);
6671 
6672       return expr;
6673     }
6674 }
6675 
6676 /* Handle overloading of the ',' operator when needed.  */
6677 
6678 tree
6679 build_x_compound_expr (location_t loc, tree op1, tree op2,
6680 		       tsubst_flags_t complain)
6681 {
6682   tree result;
6683   tree orig_op1 = op1;
6684   tree orig_op2 = op2;
6685   tree overload = NULL_TREE;
6686 
6687   if (processing_template_decl)
6688     {
6689       if (type_dependent_expression_p (op1)
6690 	  || type_dependent_expression_p (op2))
6691 	return build_min_nt_loc (loc, COMPOUND_EXPR, op1, op2);
6692       op1 = build_non_dependent_expr (op1);
6693       op2 = build_non_dependent_expr (op2);
6694     }
6695 
6696   result = build_new_op (loc, COMPOUND_EXPR, LOOKUP_NORMAL, op1, op2,
6697 			 NULL_TREE, &overload, complain);
6698   if (!result)
6699     result = cp_build_compound_expr (op1, op2, complain);
6700 
6701   if (processing_template_decl && result != error_mark_node)
6702     {
6703       if (overload != NULL_TREE)
6704 	return (build_min_non_dep_op_overload
6705 		(COMPOUND_EXPR, result, overload, orig_op1, orig_op2));
6706 
6707       return build_min_non_dep (COMPOUND_EXPR, result, orig_op1, orig_op2);
6708     }
6709 
6710   return result;
6711 }
6712 
6713 /* Like cp_build_compound_expr, but for the c-common bits.  */
6714 
6715 tree
6716 build_compound_expr (location_t /*loc*/, tree lhs, tree rhs)
6717 {
6718   return cp_build_compound_expr (lhs, rhs, tf_warning_or_error);
6719 }
6720 
6721 /* Build a compound expression.  */
6722 
6723 tree
6724 cp_build_compound_expr (tree lhs, tree rhs, tsubst_flags_t complain)
6725 {
6726   lhs = convert_to_void (lhs, ICV_LEFT_OF_COMMA, complain);
6727 
6728   if (lhs == error_mark_node || rhs == error_mark_node)
6729     return error_mark_node;
6730 
6731   if (TREE_CODE (rhs) == TARGET_EXPR)
6732     {
6733       /* If the rhs is a TARGET_EXPR, then build the compound
6734 	 expression inside the target_expr's initializer. This
6735 	 helps the compiler to eliminate unnecessary temporaries.  */
6736       tree init = TREE_OPERAND (rhs, 1);
6737 
6738       init = build2 (COMPOUND_EXPR, TREE_TYPE (init), lhs, init);
6739       TREE_OPERAND (rhs, 1) = init;
6740 
6741       return rhs;
6742     }
6743 
6744   if (type_unknown_p (rhs))
6745     {
6746       if (complain & tf_error)
6747 	error ("no context to resolve type of %qE", rhs);
6748       return error_mark_node;
6749     }
6750 
6751   return build2 (COMPOUND_EXPR, TREE_TYPE (rhs), lhs, rhs);
6752 }
6753 
6754 /* Issue a diagnostic message if casting from SRC_TYPE to DEST_TYPE
6755    casts away constness.  CAST gives the type of cast.  Returns true
6756    if the cast is ill-formed, false if it is well-formed.
6757 
6758    ??? This function warns for casting away any qualifier not just
6759    const.  We would like to specify exactly what qualifiers are casted
6760    away.
6761 */
6762 
6763 static bool
6764 check_for_casting_away_constness (tree src_type, tree dest_type,
6765 				  enum tree_code cast, tsubst_flags_t complain)
6766 {
6767   /* C-style casts are allowed to cast away constness.  With
6768      WARN_CAST_QUAL, we still want to issue a warning.  */
6769   if (cast == CAST_EXPR && !warn_cast_qual)
6770     return false;
6771 
6772   if (!casts_away_constness (src_type, dest_type, complain))
6773     return false;
6774 
6775   switch (cast)
6776     {
6777     case CAST_EXPR:
6778       if (complain & tf_warning)
6779 	warning (OPT_Wcast_qual,
6780 		 "cast from type %qT to type %qT casts away qualifiers",
6781 		 src_type, dest_type);
6782       return false;
6783 
6784     case STATIC_CAST_EXPR:
6785       if (complain & tf_error)
6786 	error ("static_cast from type %qT to type %qT casts away qualifiers",
6787 	       src_type, dest_type);
6788       return true;
6789 
6790     case REINTERPRET_CAST_EXPR:
6791       if (complain & tf_error)
6792 	error ("reinterpret_cast from type %qT to type %qT casts away qualifiers",
6793 	       src_type, dest_type);
6794       return true;
6795 
6796     default:
6797       gcc_unreachable();
6798     }
6799 }
6800 
6801 /* Warns if the cast from expression EXPR to type TYPE is useless.  */
6802 void
6803 maybe_warn_about_useless_cast (tree type, tree expr, tsubst_flags_t complain)
6804 {
6805   if (warn_useless_cast
6806       && complain & tf_warning)
6807     {
6808       if ((TREE_CODE (type) == REFERENCE_TYPE
6809 	   && (TYPE_REF_IS_RVALUE (type)
6810 	       ? xvalue_p (expr) : lvalue_p (expr))
6811 	   && same_type_p (TREE_TYPE (expr), TREE_TYPE (type)))
6812 	  || same_type_p (TREE_TYPE (expr), type))
6813 	warning (OPT_Wuseless_cast, "useless cast to type %q#T", type);
6814     }
6815 }
6816 
6817 /* Warns if the cast ignores cv-qualifiers on TYPE.  */
6818 void
6819 maybe_warn_about_cast_ignoring_quals (tree type, tsubst_flags_t complain)
6820 {
6821   if (warn_ignored_qualifiers
6822       && complain & tf_warning
6823       && !CLASS_TYPE_P (type)
6824       && (cp_type_quals (type) & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)))
6825     {
6826       warning (OPT_Wignored_qualifiers, "type qualifiers ignored on cast "
6827 	       "result type");
6828     }
6829 }
6830 
6831 /* Convert EXPR (an expression with pointer-to-member type) to TYPE
6832    (another pointer-to-member type in the same hierarchy) and return
6833    the converted expression.  If ALLOW_INVERSE_P is permitted, a
6834    pointer-to-derived may be converted to pointer-to-base; otherwise,
6835    only the other direction is permitted.  If C_CAST_P is true, this
6836    conversion is taking place as part of a C-style cast.  */
6837 
6838 tree
6839 convert_ptrmem (tree type, tree expr, bool allow_inverse_p,
6840 		bool c_cast_p, tsubst_flags_t complain)
6841 {
6842   if (same_type_p (type, TREE_TYPE (expr)))
6843     return expr;
6844 
6845   if (TYPE_PTRDATAMEM_P (type))
6846     {
6847       tree delta;
6848 
6849       delta = get_delta_difference (TYPE_PTRMEM_CLASS_TYPE (TREE_TYPE (expr)),
6850 				    TYPE_PTRMEM_CLASS_TYPE (type),
6851 				    allow_inverse_p,
6852 				    c_cast_p, complain);
6853       if (delta == error_mark_node)
6854 	return error_mark_node;
6855 
6856       if (!integer_zerop (delta))
6857 	{
6858 	  tree cond, op1, op2;
6859 
6860 	  if (TREE_CODE (expr) == PTRMEM_CST)
6861 	    expr = cplus_expand_constant (expr);
6862 	  cond = cp_build_binary_op (input_location,
6863 				     EQ_EXPR,
6864 				     expr,
6865 				     build_int_cst (TREE_TYPE (expr), -1),
6866 				     complain);
6867 	  op1 = build_nop (ptrdiff_type_node, expr);
6868 	  op2 = cp_build_binary_op (input_location,
6869 				    PLUS_EXPR, op1, delta,
6870 				    complain);
6871 
6872 	  expr = fold_build3_loc (input_location,
6873 			      COND_EXPR, ptrdiff_type_node, cond, op1, op2);
6874 
6875 	}
6876 
6877       return build_nop (type, expr);
6878     }
6879   else
6880     return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr,
6881 			     allow_inverse_p, c_cast_p, complain);
6882 }
6883 
6884 /* Perform a static_cast from EXPR to TYPE.  When C_CAST_P is true,
6885    this static_cast is being attempted as one of the possible casts
6886    allowed by a C-style cast.  (In that case, accessibility of base
6887    classes is not considered, and it is OK to cast away
6888    constness.)  Return the result of the cast.  *VALID_P is set to
6889    indicate whether or not the cast was valid.  */
6890 
6891 static tree
6892 build_static_cast_1 (tree type, tree expr, bool c_cast_p,
6893 		     bool *valid_p, tsubst_flags_t complain)
6894 {
6895   tree intype;
6896   tree result;
6897   cp_lvalue_kind clk;
6898 
6899   /* Assume the cast is valid.  */
6900   *valid_p = true;
6901 
6902   intype = unlowered_expr_type (expr);
6903 
6904   /* Save casted types in the function's used types hash table.  */
6905   used_types_insert (type);
6906 
6907   /* A prvalue of non-class type is cv-unqualified.  */
6908   if (!CLASS_TYPE_P (type))
6909     type = cv_unqualified (type);
6910 
6911   /* [expr.static.cast]
6912 
6913      An lvalue of type "cv1 B", where B is a class type, can be cast
6914      to type "reference to cv2 D", where D is a class derived (clause
6915      _class.derived_) from B, if a valid standard conversion from
6916      "pointer to D" to "pointer to B" exists (_conv.ptr_), cv2 is the
6917      same cv-qualification as, or greater cv-qualification than, cv1,
6918      and B is not a virtual base class of D.  */
6919   /* We check this case before checking the validity of "TYPE t =
6920      EXPR;" below because for this case:
6921 
6922        struct B {};
6923        struct D : public B { D(const B&); };
6924        extern B& b;
6925        void f() { static_cast<const D&>(b); }
6926 
6927      we want to avoid constructing a new D.  The standard is not
6928      completely clear about this issue, but our interpretation is
6929      consistent with other compilers.  */
6930   if (TREE_CODE (type) == REFERENCE_TYPE
6931       && CLASS_TYPE_P (TREE_TYPE (type))
6932       && CLASS_TYPE_P (intype)
6933       && (TYPE_REF_IS_RVALUE (type) || lvalue_p (expr))
6934       && DERIVED_FROM_P (intype, TREE_TYPE (type))
6935       && can_convert (build_pointer_type (TYPE_MAIN_VARIANT (intype)),
6936 		      build_pointer_type (TYPE_MAIN_VARIANT
6937 					  (TREE_TYPE (type))),
6938 		      complain)
6939       && (c_cast_p
6940 	  || at_least_as_qualified_p (TREE_TYPE (type), intype)))
6941     {
6942       tree base;
6943 
6944       if (processing_template_decl)
6945 	return expr;
6946 
6947       /* There is a standard conversion from "D*" to "B*" even if "B"
6948 	 is ambiguous or inaccessible.  If this is really a
6949 	 static_cast, then we check both for inaccessibility and
6950 	 ambiguity.  However, if this is a static_cast being performed
6951 	 because the user wrote a C-style cast, then accessibility is
6952 	 not considered.  */
6953       base = lookup_base (TREE_TYPE (type), intype,
6954 			  c_cast_p ? ba_unique : ba_check,
6955 			  NULL, complain);
6956       expr = build_address (expr);
6957 
6958       if (sanitize_flags_p (SANITIZE_VPTR))
6959 	{
6960 	  tree ubsan_check
6961 	    = cp_ubsan_maybe_instrument_downcast (input_location, type,
6962 						  intype, expr);
6963 	  if (ubsan_check)
6964 	    expr = ubsan_check;
6965 	}
6966 
6967       /* Convert from "B*" to "D*".  This function will check that "B"
6968 	 is not a virtual base of "D".  Even if we don't have a guarantee
6969 	 that expr is NULL, if the static_cast is to a reference type,
6970 	 it is UB if it would be NULL, so omit the non-NULL check.  */
6971       expr = build_base_path (MINUS_EXPR, expr, base,
6972 			      /*nonnull=*/flag_delete_null_pointer_checks,
6973 			      complain);
6974 
6975       /* Convert the pointer to a reference -- but then remember that
6976 	 there are no expressions with reference type in C++.
6977 
6978          We call rvalue so that there's an actual tree code
6979          (NON_LVALUE_EXPR) for the static_cast; otherwise, if the operand
6980          is a variable with the same type, the conversion would get folded
6981          away, leaving just the variable and causing lvalue_kind to give
6982          the wrong answer.  */
6983       expr = cp_fold_convert (type, expr);
6984 
6985       /* When -fsanitize=null, make sure to diagnose reference binding to
6986 	 NULL even when the reference is converted to pointer later on.  */
6987       if (sanitize_flags_p (SANITIZE_NULL)
6988 	  && TREE_CODE (expr) == COND_EXPR
6989 	  && TREE_OPERAND (expr, 2)
6990 	  && TREE_CODE (TREE_OPERAND (expr, 2)) == INTEGER_CST
6991 	  && TREE_TYPE (TREE_OPERAND (expr, 2)) == type)
6992 	ubsan_maybe_instrument_reference (&TREE_OPERAND (expr, 2));
6993 
6994       return convert_from_reference (rvalue (expr));
6995     }
6996 
6997   /* "A glvalue of type cv1 T1 can be cast to type rvalue reference to
6998      cv2 T2 if cv2 T2 is reference-compatible with cv1 T1 (8.5.3)."  */
6999   if (TREE_CODE (type) == REFERENCE_TYPE
7000       && TYPE_REF_IS_RVALUE (type)
7001       && (clk = real_lvalue_p (expr))
7002       && reference_related_p (TREE_TYPE (type), intype)
7003       && (c_cast_p || at_least_as_qualified_p (TREE_TYPE (type), intype)))
7004     {
7005       if (processing_template_decl)
7006 	return expr;
7007       if (clk == clk_ordinary)
7008 	{
7009 	  /* Handle the (non-bit-field) lvalue case here by casting to
7010 	     lvalue reference and then changing it to an rvalue reference.
7011 	     Casting an xvalue to rvalue reference will be handled by the
7012 	     main code path.  */
7013 	  tree lref = cp_build_reference_type (TREE_TYPE (type), false);
7014 	  result = (perform_direct_initialization_if_possible
7015 		    (lref, expr, c_cast_p, complain));
7016 	  result = build1 (NON_LVALUE_EXPR, type, result);
7017 	  return convert_from_reference (result);
7018 	}
7019       else
7020 	/* For a bit-field or packed field, bind to a temporary.  */
7021 	expr = rvalue (expr);
7022     }
7023 
7024   /* Resolve overloaded address here rather than once in
7025      implicit_conversion and again in the inverse code below.  */
7026   if (TYPE_PTRMEMFUNC_P (type) && type_unknown_p (expr))
7027     {
7028       expr = instantiate_type (type, expr, complain);
7029       intype = TREE_TYPE (expr);
7030     }
7031 
7032   /* [expr.static.cast]
7033 
7034      Any expression can be explicitly converted to type cv void.  */
7035   if (VOID_TYPE_P (type))
7036     return convert_to_void (expr, ICV_CAST, complain);
7037 
7038   /* [class.abstract]
7039      An abstract class shall not be used ... as the type of an explicit
7040      conversion.  */
7041   if (abstract_virtuals_error_sfinae (ACU_CAST, type, complain))
7042     return error_mark_node;
7043 
7044   /* [expr.static.cast]
7045 
7046      An expression e can be explicitly converted to a type T using a
7047      static_cast of the form static_cast<T>(e) if the declaration T
7048      t(e);" is well-formed, for some invented temporary variable
7049      t.  */
7050   result = perform_direct_initialization_if_possible (type, expr,
7051 						      c_cast_p, complain);
7052   if (result)
7053     {
7054       if (processing_template_decl)
7055 	return expr;
7056 
7057       result = convert_from_reference (result);
7058 
7059       /* [expr.static.cast]
7060 
7061 	 If T is a reference type, the result is an lvalue; otherwise,
7062 	 the result is an rvalue.  */
7063       if (TREE_CODE (type) != REFERENCE_TYPE)
7064 	{
7065 	  result = rvalue (result);
7066 
7067 	  if (result == expr && SCALAR_TYPE_P (type))
7068 	    /* Leave some record of the cast.  */
7069 	    result = build_nop (type, expr);
7070 	}
7071       return result;
7072     }
7073 
7074   /* [expr.static.cast]
7075 
7076      The inverse of any standard conversion sequence (clause _conv_),
7077      other than the lvalue-to-rvalue (_conv.lval_), array-to-pointer
7078      (_conv.array_), function-to-pointer (_conv.func_), and boolean
7079      (_conv.bool_) conversions, can be performed explicitly using
7080      static_cast subject to the restriction that the explicit
7081      conversion does not cast away constness (_expr.const.cast_), and
7082      the following additional rules for specific cases:  */
7083   /* For reference, the conversions not excluded are: integral
7084      promotions, floating point promotion, integral conversions,
7085      floating point conversions, floating-integral conversions,
7086      pointer conversions, and pointer to member conversions.  */
7087   /* DR 128
7088 
7089      A value of integral _or enumeration_ type can be explicitly
7090      converted to an enumeration type.  */
7091   /* The effect of all that is that any conversion between any two
7092      types which are integral, floating, or enumeration types can be
7093      performed.  */
7094   if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
7095        || SCALAR_FLOAT_TYPE_P (type))
7096       && (INTEGRAL_OR_ENUMERATION_TYPE_P (intype)
7097 	  || SCALAR_FLOAT_TYPE_P (intype)))
7098     {
7099       if (processing_template_decl)
7100 	return expr;
7101       return ocp_convert (type, expr, CONV_C_CAST, LOOKUP_NORMAL, complain);
7102     }
7103 
7104   if (TYPE_PTR_P (type) && TYPE_PTR_P (intype)
7105       && CLASS_TYPE_P (TREE_TYPE (type))
7106       && CLASS_TYPE_P (TREE_TYPE (intype))
7107       && can_convert (build_pointer_type (TYPE_MAIN_VARIANT
7108 					  (TREE_TYPE (intype))),
7109 		      build_pointer_type (TYPE_MAIN_VARIANT
7110 					  (TREE_TYPE (type))),
7111 		      complain))
7112     {
7113       tree base;
7114 
7115       if (processing_template_decl)
7116 	return expr;
7117 
7118       if (!c_cast_p
7119 	  && check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR,
7120 					       complain))
7121 	return error_mark_node;
7122       base = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
7123 			  c_cast_p ? ba_unique : ba_check,
7124 			  NULL, complain);
7125       expr = build_base_path (MINUS_EXPR, expr, base, /*nonnull=*/false,
7126 			      complain);
7127 
7128       if (sanitize_flags_p (SANITIZE_VPTR))
7129 	{
7130 	  tree ubsan_check
7131 	    = cp_ubsan_maybe_instrument_downcast (input_location, type,
7132 						  intype, expr);
7133 	  if (ubsan_check)
7134 	    expr = ubsan_check;
7135 	}
7136 
7137       return cp_fold_convert (type, expr);
7138     }
7139 
7140   if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
7141       || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
7142     {
7143       tree c1;
7144       tree c2;
7145       tree t1;
7146       tree t2;
7147 
7148       c1 = TYPE_PTRMEM_CLASS_TYPE (intype);
7149       c2 = TYPE_PTRMEM_CLASS_TYPE (type);
7150 
7151       if (TYPE_PTRDATAMEM_P (type))
7152 	{
7153 	  t1 = (build_ptrmem_type
7154 		(c1,
7155 		 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (intype))));
7156 	  t2 = (build_ptrmem_type
7157 		(c2,
7158 		 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
7159 	}
7160       else
7161 	{
7162 	  t1 = intype;
7163 	  t2 = type;
7164 	}
7165       if (can_convert (t1, t2, complain) || can_convert (t2, t1, complain))
7166 	{
7167 	  if (!c_cast_p
7168 	      && check_for_casting_away_constness (intype, type,
7169 						   STATIC_CAST_EXPR,
7170 						   complain))
7171 	    return error_mark_node;
7172 	  if (processing_template_decl)
7173 	    return expr;
7174 	  return convert_ptrmem (type, expr, /*allow_inverse_p=*/1,
7175 				 c_cast_p, complain);
7176 	}
7177     }
7178 
7179   /* [expr.static.cast]
7180 
7181      An rvalue of type "pointer to cv void" can be explicitly
7182      converted to a pointer to object type.  A value of type pointer
7183      to object converted to "pointer to cv void" and back to the
7184      original pointer type will have its original value.  */
7185   if (TYPE_PTR_P (intype)
7186       && VOID_TYPE_P (TREE_TYPE (intype))
7187       && TYPE_PTROB_P (type))
7188     {
7189       if (!c_cast_p
7190 	  && check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR,
7191 					       complain))
7192 	return error_mark_node;
7193       if (processing_template_decl)
7194 	return expr;
7195       return build_nop (type, expr);
7196     }
7197 
7198   *valid_p = false;
7199   return error_mark_node;
7200 }
7201 
7202 /* Return an expression representing static_cast<TYPE>(EXPR).  */
7203 
7204 tree
7205 build_static_cast (tree type, tree oexpr, tsubst_flags_t complain)
7206 {
7207   tree expr = oexpr;
7208   tree result;
7209   bool valid_p;
7210 
7211   if (type == error_mark_node || expr == error_mark_node)
7212     return error_mark_node;
7213 
7214   bool dependent = (dependent_type_p (type)
7215 		    || type_dependent_expression_p (expr));
7216   if (dependent)
7217     {
7218     tmpl:
7219       expr = build_min (STATIC_CAST_EXPR, type, oexpr);
7220       /* We don't know if it will or will not have side effects.  */
7221       TREE_SIDE_EFFECTS (expr) = 1;
7222       return convert_from_reference (expr);
7223     }
7224   else if (processing_template_decl)
7225     expr = build_non_dependent_expr (expr);
7226 
7227   /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7228      Strip such NOP_EXPRs if VALUE is being used in non-lvalue context.  */
7229   if (TREE_CODE (type) != REFERENCE_TYPE
7230       && TREE_CODE (expr) == NOP_EXPR
7231       && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
7232     expr = TREE_OPERAND (expr, 0);
7233 
7234   result = build_static_cast_1 (type, expr, /*c_cast_p=*/false, &valid_p,
7235                                 complain);
7236   if (valid_p)
7237     {
7238       if (result != error_mark_node)
7239 	{
7240 	  maybe_warn_about_useless_cast (type, expr, complain);
7241 	  maybe_warn_about_cast_ignoring_quals (type, complain);
7242 	}
7243       if (processing_template_decl)
7244 	goto tmpl;
7245       return result;
7246     }
7247 
7248   if (complain & tf_error)
7249     error ("invalid static_cast from type %qT to type %qT",
7250            TREE_TYPE (expr), type);
7251   return error_mark_node;
7252 }
7253 
7254 /* EXPR is an expression with member function or pointer-to-member
7255    function type.  TYPE is a pointer type.  Converting EXPR to TYPE is
7256    not permitted by ISO C++, but we accept it in some modes.  If we
7257    are not in one of those modes, issue a diagnostic.  Return the
7258    converted expression.  */
7259 
7260 tree
7261 convert_member_func_to_ptr (tree type, tree expr, tsubst_flags_t complain)
7262 {
7263   tree intype;
7264   tree decl;
7265 
7266   intype = TREE_TYPE (expr);
7267   gcc_assert (TYPE_PTRMEMFUNC_P (intype)
7268 	      || TREE_CODE (intype) == METHOD_TYPE);
7269 
7270   if (!(complain & tf_warning_or_error))
7271     return error_mark_node;
7272 
7273   if (pedantic || warn_pmf2ptr)
7274     pedwarn (input_location, pedantic ? OPT_Wpedantic : OPT_Wpmf_conversions,
7275 	     "converting from %qH to %qI", intype, type);
7276 
7277   if (TREE_CODE (intype) == METHOD_TYPE)
7278     expr = build_addr_func (expr, complain);
7279   else if (TREE_CODE (expr) == PTRMEM_CST)
7280     expr = build_address (PTRMEM_CST_MEMBER (expr));
7281   else
7282     {
7283       decl = maybe_dummy_object (TYPE_PTRMEM_CLASS_TYPE (intype), 0);
7284       decl = build_address (decl);
7285       expr = get_member_function_from_ptrfunc (&decl, expr, complain);
7286     }
7287 
7288   if (expr == error_mark_node)
7289     return error_mark_node;
7290 
7291   return build_nop (type, expr);
7292 }
7293 
7294 /* Build a NOP_EXPR to TYPE, but mark it as a reinterpret_cast so that
7295    constexpr evaluation knows to reject it.  */
7296 
7297 static tree
7298 build_nop_reinterpret (tree type, tree expr)
7299 {
7300   tree ret = build_nop (type, expr);
7301   if (ret != expr)
7302     REINTERPRET_CAST_P (ret) = true;
7303   return ret;
7304 }
7305 
7306 /* Return a representation for a reinterpret_cast from EXPR to TYPE.
7307    If C_CAST_P is true, this reinterpret cast is being done as part of
7308    a C-style cast.  If VALID_P is non-NULL, *VALID_P is set to
7309    indicate whether or not reinterpret_cast was valid.  */
7310 
7311 static tree
7312 build_reinterpret_cast_1 (tree type, tree expr, bool c_cast_p,
7313 			  bool *valid_p, tsubst_flags_t complain)
7314 {
7315   tree intype;
7316 
7317   /* Assume the cast is invalid.  */
7318   if (valid_p)
7319     *valid_p = true;
7320 
7321   if (type == error_mark_node || error_operand_p (expr))
7322     return error_mark_node;
7323 
7324   intype = TREE_TYPE (expr);
7325 
7326   /* Save casted types in the function's used types hash table.  */
7327   used_types_insert (type);
7328 
7329   /* A prvalue of non-class type is cv-unqualified.  */
7330   if (!CLASS_TYPE_P (type))
7331     type = cv_unqualified (type);
7332 
7333   /* [expr.reinterpret.cast]
7334      An lvalue expression of type T1 can be cast to the type
7335      "reference to T2" if an expression of type "pointer to T1" can be
7336      explicitly converted to the type "pointer to T2" using a
7337      reinterpret_cast.  */
7338   if (TREE_CODE (type) == REFERENCE_TYPE)
7339     {
7340       if (! lvalue_p (expr))
7341 	{
7342           if (complain & tf_error)
7343             error ("invalid cast of an rvalue expression of type "
7344                    "%qT to type %qT",
7345                    intype, type);
7346 	  return error_mark_node;
7347 	}
7348 
7349       /* Warn about a reinterpret_cast from "A*" to "B&" if "A" and
7350 	 "B" are related class types; the reinterpret_cast does not
7351 	 adjust the pointer.  */
7352       if (TYPE_PTR_P (intype)
7353           && (complain & tf_warning)
7354 	  && (comptypes (TREE_TYPE (intype), TREE_TYPE (type),
7355 			 COMPARE_BASE | COMPARE_DERIVED)))
7356 	warning (0, "casting %qT to %qT does not dereference pointer",
7357 		 intype, type);
7358 
7359       expr = cp_build_addr_expr (expr, complain);
7360 
7361       if (warn_strict_aliasing > 2)
7362 	strict_aliasing_warning (EXPR_LOCATION (expr), type, expr);
7363 
7364       if (expr != error_mark_node)
7365 	expr = build_reinterpret_cast_1
7366 	  (build_pointer_type (TREE_TYPE (type)), expr, c_cast_p,
7367 	   valid_p, complain);
7368       if (expr != error_mark_node)
7369 	/* cp_build_indirect_ref isn't right for rvalue refs.  */
7370 	expr = convert_from_reference (fold_convert (type, expr));
7371       return expr;
7372     }
7373 
7374   /* As a G++ extension, we consider conversions from member
7375      functions, and pointers to member functions to
7376      pointer-to-function and pointer-to-void types.  If
7377      -Wno-pmf-conversions has not been specified,
7378      convert_member_func_to_ptr will issue an error message.  */
7379   if ((TYPE_PTRMEMFUNC_P (intype)
7380        || TREE_CODE (intype) == METHOD_TYPE)
7381       && TYPE_PTR_P (type)
7382       && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
7383 	  || VOID_TYPE_P (TREE_TYPE (type))))
7384     return convert_member_func_to_ptr (type, expr, complain);
7385 
7386   /* If the cast is not to a reference type, the lvalue-to-rvalue,
7387      array-to-pointer, and function-to-pointer conversions are
7388      performed.  */
7389   expr = decay_conversion (expr, complain);
7390 
7391   /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7392      Strip such NOP_EXPRs if VALUE is being used in non-lvalue context.  */
7393   if (TREE_CODE (expr) == NOP_EXPR
7394       && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
7395     expr = TREE_OPERAND (expr, 0);
7396 
7397   if (error_operand_p (expr))
7398     return error_mark_node;
7399 
7400   intype = TREE_TYPE (expr);
7401 
7402   /* [expr.reinterpret.cast]
7403      A pointer can be converted to any integral type large enough to
7404      hold it. ... A value of type std::nullptr_t can be converted to
7405      an integral type; the conversion has the same meaning and
7406      validity as a conversion of (void*)0 to the integral type.  */
7407   if (CP_INTEGRAL_TYPE_P (type)
7408       && (TYPE_PTR_P (intype) || NULLPTR_TYPE_P (intype)))
7409     {
7410       if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
7411         {
7412           if (complain & tf_error)
7413             permerror (input_location, "cast from %qH to %qI loses precision",
7414                        intype, type);
7415           else
7416             return error_mark_node;
7417         }
7418       if (NULLPTR_TYPE_P (intype))
7419         return build_int_cst (type, 0);
7420     }
7421   /* [expr.reinterpret.cast]
7422      A value of integral or enumeration type can be explicitly
7423      converted to a pointer.  */
7424   else if (TYPE_PTR_P (type) && INTEGRAL_OR_ENUMERATION_TYPE_P (intype))
7425     /* OK */
7426     ;
7427   else if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
7428 	    || TYPE_PTR_OR_PTRMEM_P (type))
7429 	   && same_type_p (type, intype))
7430     /* DR 799 */
7431     return rvalue (expr);
7432   else if (TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
7433     {
7434       if ((complain & tf_warning)
7435 	  && !cxx_safe_function_type_cast_p (TREE_TYPE (type),
7436 					     TREE_TYPE (intype)))
7437 	warning (OPT_Wcast_function_type,
7438 		 "cast between incompatible function types"
7439 		 " from %qH to %qI", intype, type);
7440       return build_nop_reinterpret (type, expr);
7441     }
7442   else if (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype))
7443     {
7444       if ((complain & tf_warning)
7445 	  && !cxx_safe_function_type_cast_p
7446 		(TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (type)),
7447 		 TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (intype))))
7448 	warning (OPT_Wcast_function_type,
7449 		 "cast between incompatible pointer to member types"
7450 		 " from %qH to %qI", intype, type);
7451       return build_nop_reinterpret (type, expr);
7452     }
7453   else if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
7454 	   || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
7455     {
7456       if (!c_cast_p
7457 	  && check_for_casting_away_constness (intype, type,
7458 					       REINTERPRET_CAST_EXPR,
7459 					       complain))
7460 	return error_mark_node;
7461       /* Warn about possible alignment problems.  */
7462       if ((STRICT_ALIGNMENT || warn_cast_align == 2)
7463 	  && (complain & tf_warning)
7464 	  && !VOID_TYPE_P (type)
7465 	  && TREE_CODE (TREE_TYPE (intype)) != FUNCTION_TYPE
7466 	  && COMPLETE_TYPE_P (TREE_TYPE (type))
7467 	  && COMPLETE_TYPE_P (TREE_TYPE (intype))
7468 	  && min_align_of_type (TREE_TYPE (type))
7469 	     > min_align_of_type (TREE_TYPE (intype)))
7470 	warning (OPT_Wcast_align, "cast from %qH to %qI "
7471 		 "increases required alignment of target type", intype, type);
7472 
7473       if (warn_strict_aliasing <= 2)
7474 	/* strict_aliasing_warning STRIP_NOPs its expr.  */
7475 	strict_aliasing_warning (EXPR_LOCATION (expr), type, expr);
7476 
7477       return build_nop_reinterpret (type, expr);
7478     }
7479   else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
7480 	   || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
7481     {
7482       if (complain & tf_warning)
7483 	/* C++11 5.2.10 p8 says that "Converting a function pointer to an
7484 	   object pointer type or vice versa is conditionally-supported."  */
7485 	warning (OPT_Wconditionally_supported,
7486 		 "casting between pointer-to-function and pointer-to-object "
7487 		 "is conditionally-supported");
7488       return build_nop_reinterpret (type, expr);
7489     }
7490   else if (VECTOR_TYPE_P (type))
7491     return convert_to_vector (type, expr);
7492   else if (VECTOR_TYPE_P (intype)
7493 	   && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
7494     return convert_to_integer_nofold (type, expr);
7495   else
7496     {
7497       if (valid_p)
7498 	*valid_p = false;
7499       if (complain & tf_error)
7500         error ("invalid cast from type %qT to type %qT", intype, type);
7501       return error_mark_node;
7502     }
7503 
7504   expr = cp_convert (type, expr, complain);
7505   if (TREE_CODE (expr) == NOP_EXPR)
7506     /* Mark any nop_expr that created as a reintepret_cast.  */
7507     REINTERPRET_CAST_P (expr) = true;
7508   return expr;
7509 }
7510 
7511 tree
7512 build_reinterpret_cast (tree type, tree expr, tsubst_flags_t complain)
7513 {
7514   tree r;
7515 
7516   if (type == error_mark_node || expr == error_mark_node)
7517     return error_mark_node;
7518 
7519   if (processing_template_decl)
7520     {
7521       tree t = build_min (REINTERPRET_CAST_EXPR, type, expr);
7522 
7523       if (!TREE_SIDE_EFFECTS (t)
7524 	  && type_dependent_expression_p (expr))
7525 	/* There might turn out to be side effects inside expr.  */
7526 	TREE_SIDE_EFFECTS (t) = 1;
7527       return convert_from_reference (t);
7528     }
7529 
7530   r = build_reinterpret_cast_1 (type, expr, /*c_cast_p=*/false,
7531 				/*valid_p=*/NULL, complain);
7532   if (r != error_mark_node)
7533     {
7534       maybe_warn_about_useless_cast (type, expr, complain);
7535       maybe_warn_about_cast_ignoring_quals (type, complain);
7536     }
7537   return r;
7538 }
7539 
7540 /* Perform a const_cast from EXPR to TYPE.  If the cast is valid,
7541    return an appropriate expression.  Otherwise, return
7542    error_mark_node.  If the cast is not valid, and COMPLAIN is true,
7543    then a diagnostic will be issued.  If VALID_P is non-NULL, we are
7544    performing a C-style cast, its value upon return will indicate
7545    whether or not the conversion succeeded.  */
7546 
7547 static tree
7548 build_const_cast_1 (tree dst_type, tree expr, tsubst_flags_t complain,
7549 		    bool *valid_p)
7550 {
7551   tree src_type;
7552   tree reference_type;
7553 
7554   /* Callers are responsible for handling error_mark_node as a
7555      destination type.  */
7556   gcc_assert (dst_type != error_mark_node);
7557   /* In a template, callers should be building syntactic
7558      representations of casts, not using this machinery.  */
7559   gcc_assert (!processing_template_decl);
7560 
7561   /* Assume the conversion is invalid.  */
7562   if (valid_p)
7563     *valid_p = false;
7564 
7565   if (!POINTER_TYPE_P (dst_type) && !TYPE_PTRDATAMEM_P (dst_type))
7566     {
7567       if (complain & tf_error)
7568 	error ("invalid use of const_cast with type %qT, "
7569 	       "which is not a pointer, "
7570 	       "reference, nor a pointer-to-data-member type", dst_type);
7571       return error_mark_node;
7572     }
7573 
7574   if (TREE_CODE (TREE_TYPE (dst_type)) == FUNCTION_TYPE)
7575     {
7576       if (complain & tf_error)
7577 	error ("invalid use of const_cast with type %qT, which is a pointer "
7578 	       "or reference to a function type", dst_type);
7579       return error_mark_node;
7580     }
7581 
7582   /* A prvalue of non-class type is cv-unqualified.  */
7583   dst_type = cv_unqualified (dst_type);
7584 
7585   /* Save casted types in the function's used types hash table.  */
7586   used_types_insert (dst_type);
7587 
7588   src_type = TREE_TYPE (expr);
7589   /* Expressions do not really have reference types.  */
7590   if (TREE_CODE (src_type) == REFERENCE_TYPE)
7591     src_type = TREE_TYPE (src_type);
7592 
7593   /* [expr.const.cast]
7594 
7595      For two object types T1 and T2, if a pointer to T1 can be explicitly
7596      converted to the type "pointer to T2" using a const_cast, then the
7597      following conversions can also be made:
7598 
7599      -- an lvalue of type T1 can be explicitly converted to an lvalue of
7600      type T2 using the cast const_cast<T2&>;
7601 
7602      -- a glvalue of type T1 can be explicitly converted to an xvalue of
7603      type T2 using the cast const_cast<T2&&>; and
7604 
7605      -- if T1 is a class type, a prvalue of type T1 can be explicitly
7606      converted to an xvalue of type T2 using the cast const_cast<T2&&>.  */
7607 
7608   if (TREE_CODE (dst_type) == REFERENCE_TYPE)
7609     {
7610       reference_type = dst_type;
7611       if (!TYPE_REF_IS_RVALUE (dst_type)
7612 	  ? lvalue_p (expr)
7613 	  : obvalue_p (expr))
7614 	/* OK.  */;
7615       else
7616 	{
7617 	  if (complain & tf_error)
7618 	    error ("invalid const_cast of an rvalue of type %qT to type %qT",
7619 		   src_type, dst_type);
7620 	  return error_mark_node;
7621 	}
7622       dst_type = build_pointer_type (TREE_TYPE (dst_type));
7623       src_type = build_pointer_type (src_type);
7624     }
7625   else
7626     {
7627       reference_type = NULL_TREE;
7628       /* If the destination type is not a reference type, the
7629 	 lvalue-to-rvalue, array-to-pointer, and function-to-pointer
7630 	 conversions are performed.  */
7631       src_type = type_decays_to (src_type);
7632       if (src_type == error_mark_node)
7633 	return error_mark_node;
7634     }
7635 
7636   if (TYPE_PTR_P (src_type) || TYPE_PTRDATAMEM_P (src_type))
7637     {
7638       if (comp_ptr_ttypes_const (dst_type, src_type))
7639 	{
7640 	  if (valid_p)
7641 	    {
7642 	      *valid_p = true;
7643 	      /* This cast is actually a C-style cast.  Issue a warning if
7644 		 the user is making a potentially unsafe cast.  */
7645 	      check_for_casting_away_constness (src_type, dst_type,
7646 						CAST_EXPR, complain);
7647 	      /* ??? comp_ptr_ttypes_const ignores TYPE_ALIGN.  */
7648 	      if ((STRICT_ALIGNMENT || warn_cast_align == 2)
7649 		  && (complain & tf_warning)
7650 		  && min_align_of_type (TREE_TYPE (dst_type))
7651 		     > min_align_of_type (TREE_TYPE (src_type)))
7652 		warning (OPT_Wcast_align, "cast from %qH to %qI "
7653 			 "increases required alignment of target type",
7654 			 src_type, dst_type);
7655 	    }
7656 	  if (reference_type)
7657 	    {
7658 	      expr = cp_build_addr_expr (expr, complain);
7659 	      if (expr == error_mark_node)
7660 		return error_mark_node;
7661 	      expr = build_nop (reference_type, expr);
7662 	      return convert_from_reference (expr);
7663 	    }
7664 	  else
7665 	    {
7666 	      expr = decay_conversion (expr, complain);
7667 	      if (expr == error_mark_node)
7668 		return error_mark_node;
7669 
7670 	      /* build_c_cast puts on a NOP_EXPR to make the result not an
7671 		 lvalue.  Strip such NOP_EXPRs if VALUE is being used in
7672 		 non-lvalue context.  */
7673 	      if (TREE_CODE (expr) == NOP_EXPR
7674 		  && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
7675 		expr = TREE_OPERAND (expr, 0);
7676 	      return build_nop (dst_type, expr);
7677 	    }
7678 	}
7679       else if (valid_p
7680 	       && !at_least_as_qualified_p (TREE_TYPE (dst_type),
7681 					    TREE_TYPE (src_type)))
7682 	check_for_casting_away_constness (src_type, dst_type, CAST_EXPR,
7683 					  complain);
7684     }
7685 
7686   if (complain & tf_error)
7687     error ("invalid const_cast from type %qT to type %qT",
7688 	   src_type, dst_type);
7689   return error_mark_node;
7690 }
7691 
7692 tree
7693 build_const_cast (tree type, tree expr, tsubst_flags_t complain)
7694 {
7695   tree r;
7696 
7697   if (type == error_mark_node || error_operand_p (expr))
7698     return error_mark_node;
7699 
7700   if (processing_template_decl)
7701     {
7702       tree t = build_min (CONST_CAST_EXPR, type, expr);
7703 
7704       if (!TREE_SIDE_EFFECTS (t)
7705 	  && type_dependent_expression_p (expr))
7706 	/* There might turn out to be side effects inside expr.  */
7707 	TREE_SIDE_EFFECTS (t) = 1;
7708       return convert_from_reference (t);
7709     }
7710 
7711   r = build_const_cast_1 (type, expr, complain, /*valid_p=*/NULL);
7712   if (r != error_mark_node)
7713     {
7714       maybe_warn_about_useless_cast (type, expr, complain);
7715       maybe_warn_about_cast_ignoring_quals (type, complain);
7716     }
7717   return r;
7718 }
7719 
7720 /* Like cp_build_c_cast, but for the c-common bits.  */
7721 
7722 tree
7723 build_c_cast (location_t /*loc*/, tree type, tree expr)
7724 {
7725   return cp_build_c_cast (type, expr, tf_warning_or_error);
7726 }
7727 
7728 /* Like the "build_c_cast" used for c-common, but using cp_expr to
7729    preserve location information even for tree nodes that don't
7730    support it.  */
7731 
7732 cp_expr
7733 build_c_cast (location_t loc, tree type, cp_expr expr)
7734 {
7735   cp_expr result = cp_build_c_cast (type, expr, tf_warning_or_error);
7736   result.set_location (loc);
7737   return result;
7738 }
7739 
7740 /* Build an expression representing an explicit C-style cast to type
7741    TYPE of expression EXPR.  */
7742 
7743 tree
7744 cp_build_c_cast (tree type, tree expr, tsubst_flags_t complain)
7745 {
7746   tree value = expr;
7747   tree result;
7748   bool valid_p;
7749 
7750   if (type == error_mark_node || error_operand_p (expr))
7751     return error_mark_node;
7752 
7753   if (processing_template_decl)
7754     {
7755       tree t = build_min (CAST_EXPR, type,
7756 			  tree_cons (NULL_TREE, value, NULL_TREE));
7757       /* We don't know if it will or will not have side effects.  */
7758       TREE_SIDE_EFFECTS (t) = 1;
7759       return convert_from_reference (t);
7760     }
7761 
7762   /* Casts to a (pointer to a) specific ObjC class (or 'id' or
7763      'Class') should always be retained, because this information aids
7764      in method lookup.  */
7765   if (objc_is_object_ptr (type)
7766       && objc_is_object_ptr (TREE_TYPE (expr)))
7767     return build_nop (type, expr);
7768 
7769   /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7770      Strip such NOP_EXPRs if VALUE is being used in non-lvalue context.  */
7771   if (TREE_CODE (type) != REFERENCE_TYPE
7772       && TREE_CODE (value) == NOP_EXPR
7773       && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
7774     value = TREE_OPERAND (value, 0);
7775 
7776   if (TREE_CODE (type) == ARRAY_TYPE)
7777     {
7778       /* Allow casting from T1* to T2[] because Cfront allows it.
7779 	 NIHCL uses it. It is not valid ISO C++ however.  */
7780       if (TYPE_PTR_P (TREE_TYPE (expr)))
7781 	{
7782           if (complain & tf_error)
7783             permerror (input_location, "ISO C++ forbids casting to an array type %qT", type);
7784           else
7785             return error_mark_node;
7786 	  type = build_pointer_type (TREE_TYPE (type));
7787 	}
7788       else
7789 	{
7790           if (complain & tf_error)
7791             error ("ISO C++ forbids casting to an array type %qT", type);
7792 	  return error_mark_node;
7793 	}
7794     }
7795 
7796   if (TREE_CODE (type) == FUNCTION_TYPE
7797       || TREE_CODE (type) == METHOD_TYPE)
7798     {
7799       if (complain & tf_error)
7800         error ("invalid cast to function type %qT", type);
7801       return error_mark_node;
7802     }
7803 
7804   if (TYPE_PTR_P (type)
7805       && TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
7806       /* Casting to an integer of smaller size is an error detected elsewhere.  */
7807       && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (value))
7808       /* Don't warn about converting any constant.  */
7809       && !TREE_CONSTANT (value))
7810     warning_at (input_location, OPT_Wint_to_pointer_cast,
7811 		"cast to pointer from integer of different size");
7812 
7813   /* A C-style cast can be a const_cast.  */
7814   result = build_const_cast_1 (type, value, complain & tf_warning,
7815 			       &valid_p);
7816   if (valid_p)
7817     {
7818       if (result != error_mark_node)
7819 	{
7820 	  maybe_warn_about_useless_cast (type, value, complain);
7821 	  maybe_warn_about_cast_ignoring_quals (type, complain);
7822 	}
7823       return result;
7824     }
7825 
7826   /* Or a static cast.  */
7827   result = build_static_cast_1 (type, value, /*c_cast_p=*/true,
7828 				&valid_p, complain);
7829   /* Or a reinterpret_cast.  */
7830   if (!valid_p)
7831     result = build_reinterpret_cast_1 (type, value, /*c_cast_p=*/true,
7832 				       &valid_p, complain);
7833   /* The static_cast or reinterpret_cast may be followed by a
7834      const_cast.  */
7835   if (valid_p
7836       /* A valid cast may result in errors if, for example, a
7837 	 conversion to an ambiguous base class is required.  */
7838       && !error_operand_p (result))
7839     {
7840       tree result_type;
7841 
7842       maybe_warn_about_useless_cast (type, value, complain);
7843       maybe_warn_about_cast_ignoring_quals (type, complain);
7844 
7845       /* Non-class rvalues always have cv-unqualified type.  */
7846       if (!CLASS_TYPE_P (type))
7847 	type = TYPE_MAIN_VARIANT (type);
7848       result_type = TREE_TYPE (result);
7849       if (!CLASS_TYPE_P (result_type) && TREE_CODE (type) != REFERENCE_TYPE)
7850 	result_type = TYPE_MAIN_VARIANT (result_type);
7851       /* If the type of RESULT does not match TYPE, perform a
7852 	 const_cast to make it match.  If the static_cast or
7853 	 reinterpret_cast succeeded, we will differ by at most
7854 	 cv-qualification, so the follow-on const_cast is guaranteed
7855 	 to succeed.  */
7856       if (!same_type_p (non_reference (type), non_reference (result_type)))
7857 	{
7858 	  result = build_const_cast_1 (type, result, false, &valid_p);
7859 	  gcc_assert (valid_p);
7860 	}
7861       return result;
7862     }
7863 
7864   return error_mark_node;
7865 }
7866 
7867 /* For use from the C common bits.  */
7868 tree
7869 build_modify_expr (location_t location,
7870 		   tree lhs, tree /*lhs_origtype*/,
7871 		   enum tree_code modifycode,
7872 		   location_t /*rhs_location*/, tree rhs,
7873 		   tree /*rhs_origtype*/)
7874 {
7875   return cp_build_modify_expr (location, lhs, modifycode, rhs,
7876 			       tf_warning_or_error);
7877 }
7878 
7879 /* Build an assignment expression of lvalue LHS from value RHS.
7880    MODIFYCODE is the code for a binary operator that we use
7881    to combine the old value of LHS with RHS to get the new value.
7882    Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
7883 
7884    C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed.  */
7885 
7886 tree
7887 cp_build_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
7888 		      tree rhs, tsubst_flags_t complain)
7889 {
7890   lhs = mark_lvalue_use_nonread (lhs);
7891 
7892   tree result = NULL_TREE;
7893   tree newrhs = rhs;
7894   tree lhstype = TREE_TYPE (lhs);
7895   tree olhs = lhs;
7896   tree olhstype = lhstype;
7897   bool plain_assign = (modifycode == NOP_EXPR);
7898   bool compound_side_effects_p = false;
7899   tree preeval = NULL_TREE;
7900 
7901   /* Avoid duplicate error messages from operands that had errors.  */
7902   if (error_operand_p (lhs) || error_operand_p (rhs))
7903     return error_mark_node;
7904 
7905   while (TREE_CODE (lhs) == COMPOUND_EXPR)
7906     {
7907       if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
7908 	compound_side_effects_p = true;
7909       lhs = TREE_OPERAND (lhs, 1);
7910     }
7911 
7912   /* Handle control structure constructs used as "lvalues".  Note that we
7913      leave COMPOUND_EXPR on the LHS because it is sequenced after the RHS.  */
7914   switch (TREE_CODE (lhs))
7915     {
7916       /* Handle --foo = 5; as these are valid constructs in C++.  */
7917     case PREDECREMENT_EXPR:
7918     case PREINCREMENT_EXPR:
7919       if (compound_side_effects_p)
7920 	newrhs = rhs = stabilize_expr (rhs, &preeval);
7921       lhs = genericize_compound_lvalue (lhs);
7922     maybe_add_compound:
7923       /* If we had (bar, --foo) = 5; or (bar, (baz, --foo)) = 5;
7924 	 and looked through the COMPOUND_EXPRs, readd them now around
7925 	 the resulting lhs.  */
7926       if (TREE_CODE (olhs) == COMPOUND_EXPR)
7927 	{
7928 	  lhs = build2 (COMPOUND_EXPR, lhstype, TREE_OPERAND (olhs, 0), lhs);
7929 	  tree *ptr = &TREE_OPERAND (lhs, 1);
7930 	  for (olhs = TREE_OPERAND (olhs, 1);
7931 	       TREE_CODE (olhs) == COMPOUND_EXPR;
7932 	       olhs = TREE_OPERAND (olhs, 1))
7933 	    {
7934 	      *ptr = build2 (COMPOUND_EXPR, lhstype,
7935 			     TREE_OPERAND (olhs, 0), *ptr);
7936 	      ptr = &TREE_OPERAND (*ptr, 1);
7937 	    }
7938 	}
7939       break;
7940 
7941     case MODIFY_EXPR:
7942       if (compound_side_effects_p)
7943 	newrhs = rhs = stabilize_expr (rhs, &preeval);
7944       lhs = genericize_compound_lvalue (lhs);
7945       goto maybe_add_compound;
7946 
7947     case MIN_EXPR:
7948     case MAX_EXPR:
7949       /* MIN_EXPR and MAX_EXPR are currently only permitted as lvalues,
7950 	 when neither operand has side-effects.  */
7951       if (!lvalue_or_else (lhs, lv_assign, complain))
7952 	return error_mark_node;
7953 
7954       gcc_assert (!TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0))
7955 		  && !TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 1)));
7956 
7957       lhs = build3 (COND_EXPR, TREE_TYPE (lhs),
7958 		    build2 (TREE_CODE (lhs) == MIN_EXPR ? LE_EXPR : GE_EXPR,
7959 			    boolean_type_node,
7960 			    TREE_OPERAND (lhs, 0),
7961 			    TREE_OPERAND (lhs, 1)),
7962 		    TREE_OPERAND (lhs, 0),
7963 		    TREE_OPERAND (lhs, 1));
7964       gcc_fallthrough ();
7965 
7966       /* Handle (a ? b : c) used as an "lvalue".  */
7967     case COND_EXPR:
7968       {
7969 	/* Produce (a ? (b = rhs) : (c = rhs))
7970 	   except that the RHS goes through a save-expr
7971 	   so the code to compute it is only emitted once.  */
7972 	tree cond;
7973 
7974 	if (VOID_TYPE_P (TREE_TYPE (rhs)))
7975 	  {
7976 	    if (complain & tf_error)
7977 	      error ("void value not ignored as it ought to be");
7978 	    return error_mark_node;
7979 	  }
7980 
7981 	rhs = stabilize_expr (rhs, &preeval);
7982 
7983 	/* Check this here to avoid odd errors when trying to convert
7984 	   a throw to the type of the COND_EXPR.  */
7985 	if (!lvalue_or_else (lhs, lv_assign, complain))
7986 	  return error_mark_node;
7987 
7988 	cond = build_conditional_expr
7989 	  (input_location, TREE_OPERAND (lhs, 0),
7990 	   cp_build_modify_expr (loc, TREE_OPERAND (lhs, 1),
7991 				 modifycode, rhs, complain),
7992 	   cp_build_modify_expr (loc, TREE_OPERAND (lhs, 2),
7993 				 modifycode, rhs, complain),
7994            complain);
7995 
7996 	if (cond == error_mark_node)
7997 	  return cond;
7998 	/* If we had (e, (a ? b : c)) = d; or (e, (f, (a ? b : c))) = d;
7999 	   and looked through the COMPOUND_EXPRs, readd them now around
8000 	   the resulting cond before adding the preevaluated rhs.  */
8001 	if (TREE_CODE (olhs) == COMPOUND_EXPR)
8002 	  {
8003 	    cond = build2 (COMPOUND_EXPR, TREE_TYPE (cond),
8004 			   TREE_OPERAND (olhs, 0), cond);
8005 	    tree *ptr = &TREE_OPERAND (cond, 1);
8006 	    for (olhs = TREE_OPERAND (olhs, 1);
8007 		 TREE_CODE (olhs) == COMPOUND_EXPR;
8008 		 olhs = TREE_OPERAND (olhs, 1))
8009 	      {
8010 		*ptr = build2 (COMPOUND_EXPR, TREE_TYPE (cond),
8011 			       TREE_OPERAND (olhs, 0), *ptr);
8012 		ptr = &TREE_OPERAND (*ptr, 1);
8013 	      }
8014 	  }
8015 	/* Make sure the code to compute the rhs comes out
8016 	   before the split.  */
8017 	result = cond;
8018 	goto ret;
8019       }
8020 
8021     default:
8022       lhs = olhs;
8023       break;
8024     }
8025 
8026   if (modifycode == INIT_EXPR)
8027     {
8028       if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
8029 	/* Do the default thing.  */;
8030       else if (TREE_CODE (rhs) == CONSTRUCTOR)
8031 	{
8032 	  /* Compound literal.  */
8033 	  if (! same_type_p (TREE_TYPE (rhs), lhstype))
8034 	    /* Call convert to generate an error; see PR 11063.  */
8035 	    rhs = convert (lhstype, rhs);
8036 	  result = build2 (INIT_EXPR, lhstype, lhs, rhs);
8037 	  TREE_SIDE_EFFECTS (result) = 1;
8038 	  goto ret;
8039 	}
8040       else if (! MAYBE_CLASS_TYPE_P (lhstype))
8041 	/* Do the default thing.  */;
8042       else
8043 	{
8044 	  vec<tree, va_gc> *rhs_vec = make_tree_vector_single (rhs);
8045 	  result = build_special_member_call (lhs, complete_ctor_identifier,
8046 					      &rhs_vec, lhstype, LOOKUP_NORMAL,
8047                                               complain);
8048 	  release_tree_vector (rhs_vec);
8049 	  if (result == NULL_TREE)
8050 	    return error_mark_node;
8051 	  goto ret;
8052 	}
8053     }
8054   else
8055     {
8056       lhs = require_complete_type_sfinae (lhs, complain);
8057       if (lhs == error_mark_node)
8058 	return error_mark_node;
8059 
8060       if (modifycode == NOP_EXPR)
8061 	{
8062 	  if (c_dialect_objc ())
8063 	    {
8064 	      result = objc_maybe_build_modify_expr (lhs, rhs);
8065 	      if (result)
8066 		goto ret;
8067 	    }
8068 
8069 	  /* `operator=' is not an inheritable operator.  */
8070 	  if (! MAYBE_CLASS_TYPE_P (lhstype))
8071 	    /* Do the default thing.  */;
8072 	  else
8073 	    {
8074 	      result = build_new_op (input_location, MODIFY_EXPR,
8075 				     LOOKUP_NORMAL, lhs, rhs,
8076 				     make_node (NOP_EXPR), /*overload=*/NULL,
8077 				     complain);
8078 	      if (result == NULL_TREE)
8079 		return error_mark_node;
8080 	      goto ret;
8081 	    }
8082 	  lhstype = olhstype;
8083 	}
8084       else
8085 	{
8086 	  tree init = NULL_TREE;
8087 
8088 	  /* A binary op has been requested.  Combine the old LHS
8089 	     value with the RHS producing the value we should actually
8090 	     store into the LHS.  */
8091 	  gcc_assert (!((TREE_CODE (lhstype) == REFERENCE_TYPE
8092 			 && MAYBE_CLASS_TYPE_P (TREE_TYPE (lhstype)))
8093 			|| MAYBE_CLASS_TYPE_P (lhstype)));
8094 
8095 	  /* Preevaluate the RHS to make sure its evaluation is complete
8096 	     before the lvalue-to-rvalue conversion of the LHS:
8097 
8098 	     [expr.ass] With respect to an indeterminately-sequenced
8099 	     function call, the operation of a compound assignment is a
8100 	     single evaluation. [ Note: Therefore, a function call shall
8101 	     not intervene between the lvalue-to-rvalue conversion and the
8102 	     side effect associated with any single compound assignment
8103 	     operator. -- end note ]  */
8104 	  lhs = cp_stabilize_reference (lhs);
8105 	  rhs = decay_conversion (rhs, complain);
8106 	  if (rhs == error_mark_node)
8107 	    return error_mark_node;
8108 	  rhs = stabilize_expr (rhs, &init);
8109 	  newrhs = cp_build_binary_op (loc, modifycode, lhs, rhs, complain);
8110 	  if (newrhs == error_mark_node)
8111 	    {
8112 	      if (complain & tf_error)
8113 		error ("  in evaluation of %<%Q(%#T, %#T)%>", modifycode,
8114 		       TREE_TYPE (lhs), TREE_TYPE (rhs));
8115 	      return error_mark_node;
8116 	    }
8117 
8118 	  if (init)
8119 	    newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), init, newrhs);
8120 
8121 	  /* Now it looks like a plain assignment.  */
8122 	  modifycode = NOP_EXPR;
8123 	  if (c_dialect_objc ())
8124 	    {
8125 	      result = objc_maybe_build_modify_expr (lhs, newrhs);
8126 	      if (result)
8127 		goto ret;
8128 	    }
8129 	}
8130       gcc_assert (TREE_CODE (lhstype) != REFERENCE_TYPE);
8131       gcc_assert (TREE_CODE (TREE_TYPE (newrhs)) != REFERENCE_TYPE);
8132     }
8133 
8134   /* The left-hand side must be an lvalue.  */
8135   if (!lvalue_or_else (lhs, lv_assign, complain))
8136     return error_mark_node;
8137 
8138   /* Warn about modifying something that is `const'.  Don't warn if
8139      this is initialization.  */
8140   if (modifycode != INIT_EXPR
8141       && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype)
8142 	  /* Functions are not modifiable, even though they are
8143 	     lvalues.  */
8144 	  || TREE_CODE (TREE_TYPE (lhs)) == FUNCTION_TYPE
8145 	  || TREE_CODE (TREE_TYPE (lhs)) == METHOD_TYPE
8146 	  /* If it's an aggregate and any field is const, then it is
8147 	     effectively const.  */
8148 	  || (CLASS_TYPE_P (lhstype)
8149 	      && C_TYPE_FIELDS_READONLY (lhstype))))
8150     {
8151       if (complain & tf_error)
8152 	cxx_readonly_error (lhs, lv_assign);
8153       return error_mark_node;
8154     }
8155 
8156   /* If storing into a structure or union member, it may have been given a
8157      lowered bitfield type.  We need to convert to the declared type first,
8158      so retrieve it now.  */
8159 
8160   olhstype = unlowered_expr_type (lhs);
8161 
8162   /* Convert new value to destination type.  */
8163 
8164   if (TREE_CODE (lhstype) == ARRAY_TYPE)
8165     {
8166       int from_array;
8167 
8168       if (BRACE_ENCLOSED_INITIALIZER_P (newrhs))
8169 	{
8170 	  if (modifycode != INIT_EXPR)
8171 	    {
8172 	      if (complain & tf_error)
8173 		error ("assigning to an array from an initializer list");
8174 	      return error_mark_node;
8175 	    }
8176 	  if (check_array_initializer (lhs, lhstype, newrhs))
8177 	    return error_mark_node;
8178 	  newrhs = digest_init (lhstype, newrhs, complain);
8179 	  if (newrhs == error_mark_node)
8180 	    return error_mark_node;
8181 	}
8182 
8183       /* C++11 8.5/17: "If the destination type is an array of characters,
8184 	 an array of char16_t, an array of char32_t, or an array of wchar_t,
8185 	 and the initializer is a string literal...".  */
8186       else if (TREE_CODE (newrhs) == STRING_CST
8187 	       && char_type_p (TREE_TYPE (TYPE_MAIN_VARIANT (lhstype)))
8188 	       && modifycode == INIT_EXPR)
8189 	{
8190 	  newrhs = digest_init (lhstype, newrhs, complain);
8191 	  if (newrhs == error_mark_node)
8192 	    return error_mark_node;
8193 	}
8194 
8195       else if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype),
8196 				     TYPE_MAIN_VARIANT (TREE_TYPE (newrhs))))
8197 	{
8198 	  if (complain & tf_error)
8199 	    error ("incompatible types in assignment of %qT to %qT",
8200 		   TREE_TYPE (rhs), lhstype);
8201 	  return error_mark_node;
8202 	}
8203 
8204       /* Allow array assignment in compiler-generated code.  */
8205       else if (!current_function_decl
8206 	       || !DECL_DEFAULTED_FN (current_function_decl))
8207 	{
8208           /* This routine is used for both initialization and assignment.
8209              Make sure the diagnostic message differentiates the context.  */
8210 	  if (complain & tf_error)
8211 	    {
8212 	      if (modifycode == INIT_EXPR)
8213 		error ("array used as initializer");
8214 	      else
8215 		error ("invalid array assignment");
8216 	    }
8217 	  return error_mark_node;
8218 	}
8219 
8220       from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
8221 		   ? 1 + (modifycode != INIT_EXPR): 0;
8222       result = build_vec_init (lhs, NULL_TREE, newrhs,
8223 			       /*explicit_value_init_p=*/false,
8224 			       from_array, complain);
8225       goto ret;
8226     }
8227 
8228   if (modifycode == INIT_EXPR)
8229     /* Calls with INIT_EXPR are all direct-initialization, so don't set
8230        LOOKUP_ONLYCONVERTING.  */
8231     newrhs = convert_for_initialization (lhs, olhstype, newrhs, LOOKUP_NORMAL,
8232 					 ICR_INIT, NULL_TREE, 0,
8233                                          complain);
8234   else
8235     newrhs = convert_for_assignment (olhstype, newrhs, ICR_ASSIGN,
8236 				     NULL_TREE, 0, complain, LOOKUP_IMPLICIT);
8237 
8238   if (!same_type_p (lhstype, olhstype))
8239     newrhs = cp_convert_and_check (lhstype, newrhs, complain);
8240 
8241   if (modifycode != INIT_EXPR)
8242     {
8243       if (TREE_CODE (newrhs) == CALL_EXPR
8244 	  && TYPE_NEEDS_CONSTRUCTING (lhstype))
8245 	newrhs = build_cplus_new (lhstype, newrhs, complain);
8246 
8247       /* Can't initialize directly from a TARGET_EXPR, since that would
8248 	 cause the lhs to be constructed twice, and possibly result in
8249 	 accidental self-initialization.  So we force the TARGET_EXPR to be
8250 	 expanded without a target.  */
8251       if (TREE_CODE (newrhs) == TARGET_EXPR)
8252 	newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
8253 			 TREE_OPERAND (newrhs, 0));
8254     }
8255 
8256   if (newrhs == error_mark_node)
8257     return error_mark_node;
8258 
8259   if (c_dialect_objc () && flag_objc_gc)
8260     {
8261       result = objc_generate_write_barrier (lhs, modifycode, newrhs);
8262 
8263       if (result)
8264 	goto ret;
8265     }
8266 
8267   result = build2 (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
8268 		   lhstype, lhs, newrhs);
8269 
8270   TREE_SIDE_EFFECTS (result) = 1;
8271   if (!plain_assign)
8272     TREE_NO_WARNING (result) = 1;
8273 
8274  ret:
8275   if (preeval)
8276     result = build2 (COMPOUND_EXPR, TREE_TYPE (result), preeval, result);
8277   return result;
8278 }
8279 
8280 cp_expr
8281 build_x_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
8282 		     tree rhs, tsubst_flags_t complain)
8283 {
8284   tree orig_lhs = lhs;
8285   tree orig_rhs = rhs;
8286   tree overload = NULL_TREE;
8287   tree op = build_nt (modifycode, NULL_TREE, NULL_TREE);
8288 
8289   if (processing_template_decl)
8290     {
8291       if (modifycode == NOP_EXPR
8292 	  || type_dependent_expression_p (lhs)
8293 	  || type_dependent_expression_p (rhs))
8294         return build_min_nt_loc (loc, MODOP_EXPR, lhs,
8295 				 build_min_nt_loc (loc, modifycode, NULL_TREE,
8296 						   NULL_TREE), rhs);
8297 
8298       lhs = build_non_dependent_expr (lhs);
8299       rhs = build_non_dependent_expr (rhs);
8300     }
8301 
8302   if (modifycode != NOP_EXPR)
8303     {
8304       tree rval = build_new_op (loc, MODIFY_EXPR, LOOKUP_NORMAL,
8305 				lhs, rhs, op, &overload, complain);
8306       if (rval)
8307 	{
8308 	  if (rval == error_mark_node)
8309 	    return rval;
8310 	  TREE_NO_WARNING (rval) = 1;
8311 	  if (processing_template_decl)
8312 	    {
8313 	      if (overload != NULL_TREE)
8314 		return (build_min_non_dep_op_overload
8315 			(MODIFY_EXPR, rval, overload, orig_lhs, orig_rhs));
8316 
8317 	      return (build_min_non_dep
8318 		      (MODOP_EXPR, rval, orig_lhs, op, orig_rhs));
8319 	    }
8320 	  return rval;
8321 	}
8322     }
8323   return cp_build_modify_expr (loc, lhs, modifycode, rhs, complain);
8324 }
8325 
8326 /* Helper function for get_delta_difference which assumes FROM is a base
8327    class of TO.  Returns a delta for the conversion of pointer-to-member
8328    of FROM to pointer-to-member of TO.  If the conversion is invalid and
8329    tf_error is not set in COMPLAIN returns error_mark_node, otherwise
8330    returns zero.  If FROM is not a base class of TO, returns NULL_TREE.
8331    If C_CAST_P is true, this conversion is taking place as part of a
8332    C-style cast.  */
8333 
8334 static tree
8335 get_delta_difference_1 (tree from, tree to, bool c_cast_p,
8336 			tsubst_flags_t complain)
8337 {
8338   tree binfo;
8339   base_kind kind;
8340 
8341   binfo = lookup_base (to, from, c_cast_p ? ba_unique : ba_check,
8342 		       &kind, complain);
8343 
8344   if (binfo == error_mark_node)
8345     {
8346       if (!(complain & tf_error))
8347 	return error_mark_node;
8348 
8349       error ("   in pointer to member function conversion");
8350       return size_zero_node;
8351     }
8352   else if (binfo)
8353     {
8354       if (kind != bk_via_virtual)
8355 	return BINFO_OFFSET (binfo);
8356       else
8357 	/* FROM is a virtual base class of TO.  Issue an error or warning
8358 	   depending on whether or not this is a reinterpret cast.  */
8359 	{
8360 	  if (!(complain & tf_error))
8361 	    return error_mark_node;
8362 
8363 	  error ("pointer to member conversion via virtual base %qT",
8364 		 BINFO_TYPE (binfo_from_vbase (binfo)));
8365 
8366 	  return size_zero_node;
8367 	}
8368       }
8369   else
8370     return NULL_TREE;
8371 }
8372 
8373 /* Get difference in deltas for different pointer to member function
8374    types.  If the conversion is invalid and tf_error is not set in
8375    COMPLAIN, returns error_mark_node, otherwise returns an integer
8376    constant of type PTRDIFF_TYPE_NODE and its value is zero if the
8377    conversion is invalid.  If ALLOW_INVERSE_P is true, then allow reverse
8378    conversions as well.  If C_CAST_P is true this conversion is taking
8379    place as part of a C-style cast.
8380 
8381    Note that the naming of FROM and TO is kind of backwards; the return
8382    value is what we add to a TO in order to get a FROM.  They are named
8383    this way because we call this function to find out how to convert from
8384    a pointer to member of FROM to a pointer to member of TO.  */
8385 
8386 static tree
8387 get_delta_difference (tree from, tree to,
8388 		      bool allow_inverse_p,
8389 		      bool c_cast_p, tsubst_flags_t complain)
8390 {
8391   tree result;
8392 
8393   if (same_type_ignoring_top_level_qualifiers_p (from, to))
8394     /* Pointer to member of incomplete class is permitted*/
8395     result = size_zero_node;
8396   else
8397     result = get_delta_difference_1 (from, to, c_cast_p, complain);
8398 
8399   if (result == error_mark_node)
8400     return error_mark_node;
8401 
8402   if (!result)
8403   {
8404     if (!allow_inverse_p)
8405       {
8406 	if (!(complain & tf_error))
8407 	  return error_mark_node;
8408 
8409 	error_not_base_type (from, to);
8410 	error ("   in pointer to member conversion");
8411       	result = size_zero_node;
8412       }
8413     else
8414       {
8415 	result = get_delta_difference_1 (to, from, c_cast_p, complain);
8416 
8417 	if (result == error_mark_node)
8418 	  return error_mark_node;
8419 
8420 	if (result)
8421 	  result = size_diffop_loc (input_location,
8422 				    size_zero_node, result);
8423 	else
8424 	  {
8425 	    if (!(complain & tf_error))
8426 	      return error_mark_node;
8427 
8428 	    error_not_base_type (from, to);
8429 	    error ("   in pointer to member conversion");
8430 	    result = size_zero_node;
8431 	  }
8432       }
8433   }
8434 
8435   return convert_to_integer (ptrdiff_type_node, result);
8436 }
8437 
8438 /* Return a constructor for the pointer-to-member-function TYPE using
8439    the other components as specified.  */
8440 
8441 tree
8442 build_ptrmemfunc1 (tree type, tree delta, tree pfn)
8443 {
8444   tree u = NULL_TREE;
8445   tree delta_field;
8446   tree pfn_field;
8447   vec<constructor_elt, va_gc> *v;
8448 
8449   /* Pull the FIELD_DECLs out of the type.  */
8450   pfn_field = TYPE_FIELDS (type);
8451   delta_field = DECL_CHAIN (pfn_field);
8452 
8453   /* Make sure DELTA has the type we want.  */
8454   delta = convert_and_check (input_location, delta_type_node, delta);
8455 
8456   /* Convert to the correct target type if necessary.  */
8457   pfn = fold_convert (TREE_TYPE (pfn_field), pfn);
8458 
8459   /* Finish creating the initializer.  */
8460   vec_alloc (v, 2);
8461   CONSTRUCTOR_APPEND_ELT(v, pfn_field, pfn);
8462   CONSTRUCTOR_APPEND_ELT(v, delta_field, delta);
8463   u = build_constructor (type, v);
8464   TREE_CONSTANT (u) = TREE_CONSTANT (pfn) & TREE_CONSTANT (delta);
8465   TREE_STATIC (u) = (TREE_CONSTANT (u)
8466 		     && (initializer_constant_valid_p (pfn, TREE_TYPE (pfn))
8467 			 != NULL_TREE)
8468 		     && (initializer_constant_valid_p (delta, TREE_TYPE (delta))
8469 			 != NULL_TREE));
8470   return u;
8471 }
8472 
8473 /* Build a constructor for a pointer to member function.  It can be
8474    used to initialize global variables, local variable, or used
8475    as a value in expressions.  TYPE is the POINTER to METHOD_TYPE we
8476    want to be.
8477 
8478    If FORCE is nonzero, then force this conversion, even if
8479    we would rather not do it.  Usually set when using an explicit
8480    cast.  A C-style cast is being processed iff C_CAST_P is true.
8481 
8482    Return error_mark_node, if something goes wrong.  */
8483 
8484 tree
8485 build_ptrmemfunc (tree type, tree pfn, int force, bool c_cast_p,
8486 		  tsubst_flags_t complain)
8487 {
8488   tree fn;
8489   tree pfn_type;
8490   tree to_type;
8491 
8492   if (error_operand_p (pfn))
8493     return error_mark_node;
8494 
8495   pfn_type = TREE_TYPE (pfn);
8496   to_type = build_ptrmemfunc_type (type);
8497 
8498   /* Handle multiple conversions of pointer to member functions.  */
8499   if (TYPE_PTRMEMFUNC_P (pfn_type))
8500     {
8501       tree delta = NULL_TREE;
8502       tree npfn = NULL_TREE;
8503       tree n;
8504 
8505       if (!force
8506 	  && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn,
8507 			       LOOKUP_NORMAL, complain))
8508 	{
8509 	  if (complain & tf_error)
8510 	    error ("invalid conversion to type %qT from type %qT",
8511 		   to_type, pfn_type);
8512 	  else
8513 	    return error_mark_node;
8514 	}
8515 
8516       n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type),
8517 				TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type),
8518 				force,
8519 				c_cast_p, complain);
8520       if (n == error_mark_node)
8521 	return error_mark_node;
8522 
8523       /* We don't have to do any conversion to convert a
8524 	 pointer-to-member to its own type.  But, we don't want to
8525 	 just return a PTRMEM_CST if there's an explicit cast; that
8526 	 cast should make the expression an invalid template argument.  */
8527       if (TREE_CODE (pfn) != PTRMEM_CST)
8528 	{
8529 	  if (same_type_p (to_type, pfn_type))
8530 	    return pfn;
8531 	  else if (integer_zerop (n) && TREE_CODE (pfn) != CONSTRUCTOR)
8532 	    return build_reinterpret_cast (to_type, pfn,
8533                                            complain);
8534 	}
8535 
8536       if (TREE_SIDE_EFFECTS (pfn))
8537 	pfn = save_expr (pfn);
8538 
8539       /* Obtain the function pointer and the current DELTA.  */
8540       if (TREE_CODE (pfn) == PTRMEM_CST)
8541 	expand_ptrmemfunc_cst (pfn, &delta, &npfn);
8542       else
8543 	{
8544 	  npfn = build_ptrmemfunc_access_expr (pfn, pfn_identifier);
8545 	  delta = build_ptrmemfunc_access_expr (pfn, delta_identifier);
8546 	}
8547 
8548       /* Just adjust the DELTA field.  */
8549       gcc_assert  (same_type_ignoring_top_level_qualifiers_p
8550 		   (TREE_TYPE (delta), ptrdiff_type_node));
8551       if (!integer_zerop (n))
8552 	{
8553 	  if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_delta)
8554 	    n = cp_build_binary_op (input_location,
8555 				    LSHIFT_EXPR, n, integer_one_node,
8556 				    complain);
8557 	  delta = cp_build_binary_op (input_location,
8558 				      PLUS_EXPR, delta, n, complain);
8559 	}
8560       return build_ptrmemfunc1 (to_type, delta, npfn);
8561     }
8562 
8563   /* Handle null pointer to member function conversions.  */
8564   if (null_ptr_cst_p (pfn))
8565     {
8566       pfn = cp_build_c_cast (type, pfn, complain);
8567       return build_ptrmemfunc1 (to_type,
8568 				integer_zero_node,
8569 				pfn);
8570     }
8571 
8572   if (type_unknown_p (pfn))
8573     return instantiate_type (type, pfn, complain);
8574 
8575   fn = TREE_OPERAND (pfn, 0);
8576   gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
8577 	      /* In a template, we will have preserved the
8578 		 OFFSET_REF.  */
8579 	      || (processing_template_decl && TREE_CODE (fn) == OFFSET_REF));
8580   return make_ptrmem_cst (to_type, fn);
8581 }
8582 
8583 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
8584    given by CST.
8585 
8586    ??? There is no consistency as to the types returned for the above
8587    values.  Some code acts as if it were a sizetype and some as if it were
8588    integer_type_node.  */
8589 
8590 void
8591 expand_ptrmemfunc_cst (tree cst, tree *delta, tree *pfn)
8592 {
8593   tree type = TREE_TYPE (cst);
8594   tree fn = PTRMEM_CST_MEMBER (cst);
8595   tree ptr_class, fn_class;
8596 
8597   gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
8598 
8599   /* The class that the function belongs to.  */
8600   fn_class = DECL_CONTEXT (fn);
8601 
8602   /* The class that we're creating a pointer to member of.  */
8603   ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type);
8604 
8605   /* First, calculate the adjustment to the function's class.  */
8606   *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0,
8607 				 /*c_cast_p=*/0, tf_warning_or_error);
8608 
8609   if (!DECL_VIRTUAL_P (fn))
8610     *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type),
8611 		    build_addr_func (fn, tf_warning_or_error));
8612   else
8613     {
8614       /* If we're dealing with a virtual function, we have to adjust 'this'
8615 	 again, to point to the base which provides the vtable entry for
8616 	 fn; the call will do the opposite adjustment.  */
8617       tree orig_class = DECL_CONTEXT (fn);
8618       tree binfo = binfo_or_else (orig_class, fn_class);
8619       *delta = fold_build2 (PLUS_EXPR, TREE_TYPE (*delta),
8620 			    *delta, BINFO_OFFSET (binfo));
8621 
8622       /* We set PFN to the vtable offset at which the function can be
8623 	 found, plus one (unless ptrmemfunc_vbit_in_delta, in which
8624 	 case delta is shifted left, and then incremented).  */
8625       *pfn = DECL_VINDEX (fn);
8626       *pfn = fold_build2 (MULT_EXPR, integer_type_node, *pfn,
8627 			  TYPE_SIZE_UNIT (vtable_entry_type));
8628 
8629       switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
8630 	{
8631 	case ptrmemfunc_vbit_in_pfn:
8632 	  *pfn = fold_build2 (PLUS_EXPR, integer_type_node, *pfn,
8633 			      integer_one_node);
8634 	  break;
8635 
8636 	case ptrmemfunc_vbit_in_delta:
8637 	  *delta = fold_build2 (LSHIFT_EXPR, TREE_TYPE (*delta),
8638 				*delta, integer_one_node);
8639 	  *delta = fold_build2 (PLUS_EXPR, TREE_TYPE (*delta),
8640 				*delta, integer_one_node);
8641 	  break;
8642 
8643 	default:
8644 	  gcc_unreachable ();
8645 	}
8646 
8647       *pfn = fold_convert (TYPE_PTRMEMFUNC_FN_TYPE (type), *pfn);
8648     }
8649 }
8650 
8651 /* Return an expression for PFN from the pointer-to-member function
8652    given by T.  */
8653 
8654 static tree
8655 pfn_from_ptrmemfunc (tree t)
8656 {
8657   if (TREE_CODE (t) == PTRMEM_CST)
8658     {
8659       tree delta;
8660       tree pfn;
8661 
8662       expand_ptrmemfunc_cst (t, &delta, &pfn);
8663       if (pfn)
8664 	return pfn;
8665     }
8666 
8667   return build_ptrmemfunc_access_expr (t, pfn_identifier);
8668 }
8669 
8670 /* Return an expression for DELTA from the pointer-to-member function
8671    given by T.  */
8672 
8673 static tree
8674 delta_from_ptrmemfunc (tree t)
8675 {
8676   if (TREE_CODE (t) == PTRMEM_CST)
8677     {
8678       tree delta;
8679       tree pfn;
8680 
8681       expand_ptrmemfunc_cst (t, &delta, &pfn);
8682       if (delta)
8683 	return delta;
8684     }
8685 
8686   return build_ptrmemfunc_access_expr (t, delta_identifier);
8687 }
8688 
8689 /* Convert value RHS to type TYPE as preparation for an assignment to
8690    an lvalue of type TYPE.  ERRTYPE indicates what kind of error the
8691    implicit conversion is.  If FNDECL is non-NULL, we are doing the
8692    conversion in order to pass the PARMNUMth argument of FNDECL.
8693    If FNDECL is NULL, we are doing the conversion in function pointer
8694    argument passing, conversion in initialization, etc. */
8695 
8696 static tree
8697 convert_for_assignment (tree type, tree rhs,
8698 			impl_conv_rhs errtype, tree fndecl, int parmnum,
8699 			tsubst_flags_t complain, int flags)
8700 {
8701   tree rhstype;
8702   enum tree_code coder;
8703 
8704   /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue.  */
8705   if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
8706     rhs = TREE_OPERAND (rhs, 0);
8707 
8708   /* Handle [dcl.init.list] direct-list-initialization from
8709      single element of enumeration with a fixed underlying type.  */
8710   if (is_direct_enum_init (type, rhs))
8711     {
8712       tree elt = CONSTRUCTOR_ELT (rhs, 0)->value;
8713       if (check_narrowing (ENUM_UNDERLYING_TYPE (type), elt, complain))
8714 	{
8715 	  warning_sentinel w (warn_useless_cast);
8716 	  warning_sentinel w2 (warn_ignored_qualifiers);
8717 	  rhs = cp_build_c_cast (type, elt, complain);
8718 	}
8719       else
8720 	rhs = error_mark_node;
8721     }
8722 
8723   rhstype = TREE_TYPE (rhs);
8724   coder = TREE_CODE (rhstype);
8725 
8726   if (VECTOR_TYPE_P (type) && coder == VECTOR_TYPE
8727       && vector_types_convertible_p (type, rhstype, true))
8728     {
8729       rhs = mark_rvalue_use (rhs);
8730       return convert (type, rhs);
8731     }
8732 
8733   if (rhs == error_mark_node || rhstype == error_mark_node)
8734     return error_mark_node;
8735   if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)
8736     return error_mark_node;
8737 
8738   /* The RHS of an assignment cannot have void type.  */
8739   if (coder == VOID_TYPE)
8740     {
8741       if (complain & tf_error)
8742 	error ("void value not ignored as it ought to be");
8743       return error_mark_node;
8744     }
8745 
8746   if (c_dialect_objc ())
8747     {
8748       int parmno;
8749       tree selector;
8750       tree rname = fndecl;
8751 
8752       switch (errtype)
8753         {
8754 	  case ICR_ASSIGN:
8755 	    parmno = -1;
8756 	    break;
8757 	  case ICR_INIT:
8758 	    parmno = -2;
8759 	    break;
8760 	  default:
8761 	    selector = objc_message_selector ();
8762 	    parmno = parmnum;
8763 	    if (selector && parmno > 1)
8764 	      {
8765 		rname = selector;
8766 		parmno -= 1;
8767 	      }
8768 	}
8769 
8770       if (objc_compare_types (type, rhstype, parmno, rname))
8771 	{
8772 	  rhs = mark_rvalue_use (rhs);
8773 	  return convert (type, rhs);
8774 	}
8775     }
8776 
8777   /* [expr.ass]
8778 
8779      The expression is implicitly converted (clause _conv_) to the
8780      cv-unqualified type of the left operand.
8781 
8782      We allow bad conversions here because by the time we get to this point
8783      we are committed to doing the conversion.  If we end up doing a bad
8784      conversion, convert_like will complain.  */
8785   if (!can_convert_arg_bad (type, rhstype, rhs, flags, complain))
8786     {
8787       /* When -Wno-pmf-conversions is use, we just silently allow
8788 	 conversions from pointers-to-members to plain pointers.  If
8789 	 the conversion doesn't work, cp_convert will complain.  */
8790       if (!warn_pmf2ptr
8791 	  && TYPE_PTR_P (type)
8792 	  && TYPE_PTRMEMFUNC_P (rhstype))
8793 	rhs = cp_convert (strip_top_quals (type), rhs, complain);
8794       else
8795 	{
8796 	  if (complain & tf_error)
8797 	    {
8798 	      /* If the right-hand side has unknown type, then it is an
8799 		 overloaded function.  Call instantiate_type to get error
8800 		 messages.  */
8801 	      if (rhstype == unknown_type_node)
8802 		{
8803 		  tree r = instantiate_type (type, rhs, tf_warning_or_error);
8804 		  /* -fpermissive might allow this; recurse.  */
8805 		  if (!seen_error ())
8806 		    return convert_for_assignment (type, r, errtype, fndecl,
8807 						   parmnum, complain, flags);
8808 		}
8809 	      else if (fndecl)
8810 		{
8811 		  error_at (EXPR_LOC_OR_LOC (rhs, input_location),
8812 			    "cannot convert %qH to %qI",
8813 			    rhstype, type);
8814 		  inform (get_fndecl_argument_location (fndecl, parmnum),
8815 			  "  initializing argument %P of %qD", parmnum, fndecl);
8816 		}
8817 	      else
8818 		switch (errtype)
8819 		  {
8820 		    case ICR_DEFAULT_ARGUMENT:
8821 		      error ("cannot convert %qH to %qI in default argument",
8822 			     rhstype, type);
8823 		      break;
8824 		    case ICR_ARGPASS:
8825 		      error ("cannot convert %qH to %qI in argument passing",
8826 			     rhstype, type);
8827 		      break;
8828 		    case ICR_CONVERTING:
8829 		      error ("cannot convert %qH to %qI",
8830 			     rhstype, type);
8831 		      break;
8832 		    case ICR_INIT:
8833 		      error ("cannot convert %qH to %qI in initialization",
8834 			     rhstype, type);
8835 		      break;
8836 		    case ICR_RETURN:
8837 		      error ("cannot convert %qH to %qI in return",
8838 			     rhstype, type);
8839 		      break;
8840 		    case ICR_ASSIGN:
8841 		      error ("cannot convert %qH to %qI in assignment",
8842 			     rhstype, type);
8843 		      break;
8844 		    default:
8845 		      gcc_unreachable();
8846 		  }
8847 	      if (TYPE_PTR_P (rhstype)
8848 		  && TYPE_PTR_P (type)
8849 		  && CLASS_TYPE_P (TREE_TYPE (rhstype))
8850 		  && CLASS_TYPE_P (TREE_TYPE (type))
8851 		  && !COMPLETE_TYPE_P (TREE_TYPE (rhstype)))
8852 		inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL
8853 					      (TREE_TYPE (rhstype))),
8854 			"class type %qT is incomplete", TREE_TYPE (rhstype));
8855 	    }
8856 	  return error_mark_node;
8857 	}
8858     }
8859   if (warn_suggest_attribute_format)
8860     {
8861       const enum tree_code codel = TREE_CODE (type);
8862       if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
8863 	  && coder == codel
8864 	  && check_missing_format_attribute (type, rhstype)
8865 	  && (complain & tf_warning))
8866 	switch (errtype)
8867 	  {
8868 	    case ICR_ARGPASS:
8869 	    case ICR_DEFAULT_ARGUMENT:
8870 	      if (fndecl)
8871 		warning (OPT_Wsuggest_attribute_format,
8872 			 "parameter %qP of %qD might be a candidate "
8873 			 "for a format attribute", parmnum, fndecl);
8874 	      else
8875 		warning (OPT_Wsuggest_attribute_format,
8876 			 "parameter might be a candidate "
8877 			 "for a format attribute");
8878 	      break;
8879 	    case ICR_CONVERTING:
8880 	      warning (OPT_Wsuggest_attribute_format,
8881 		       "target of conversion might be a candidate "
8882 		       "for a format attribute");
8883 	      break;
8884 	    case ICR_INIT:
8885 	      warning (OPT_Wsuggest_attribute_format,
8886 		       "target of initialization might be a candidate "
8887 		       "for a format attribute");
8888 	      break;
8889 	    case ICR_RETURN:
8890 	      warning (OPT_Wsuggest_attribute_format,
8891 		       "return type might be a candidate "
8892 		       "for a format attribute");
8893 	      break;
8894 	    case ICR_ASSIGN:
8895 	      warning (OPT_Wsuggest_attribute_format,
8896 		       "left-hand side of assignment might be a candidate "
8897 		       "for a format attribute");
8898 	      break;
8899 	    default:
8900 	      gcc_unreachable();
8901 	  }
8902     }
8903 
8904   /* If -Wparentheses, warn about a = b = c when a has type bool and b
8905      does not.  */
8906   if (warn_parentheses
8907       && TREE_CODE (type) == BOOLEAN_TYPE
8908       && TREE_CODE (rhs) == MODIFY_EXPR
8909       && !TREE_NO_WARNING (rhs)
8910       && TREE_CODE (TREE_TYPE (rhs)) != BOOLEAN_TYPE
8911       && (complain & tf_warning))
8912     {
8913       location_t loc = EXPR_LOC_OR_LOC (rhs, input_location);
8914 
8915       warning_at (loc, OPT_Wparentheses,
8916 		  "suggest parentheses around assignment used as truth value");
8917       TREE_NO_WARNING (rhs) = 1;
8918     }
8919 
8920   return perform_implicit_conversion_flags (strip_top_quals (type), rhs,
8921 					    complain, flags);
8922 }
8923 
8924 /* Convert RHS to be of type TYPE.
8925    If EXP is nonzero, it is the target of the initialization.
8926    ERRTYPE indicates what kind of error the implicit conversion is.
8927 
8928    Two major differences between the behavior of
8929    `convert_for_assignment' and `convert_for_initialization'
8930    are that references are bashed in the former, while
8931    copied in the latter, and aggregates are assigned in
8932    the former (operator=) while initialized in the
8933    latter (X(X&)).
8934 
8935    If using constructor make sure no conversion operator exists, if one does
8936    exist, an ambiguity exists.  */
8937 
8938 tree
8939 convert_for_initialization (tree exp, tree type, tree rhs, int flags,
8940 			    impl_conv_rhs errtype, tree fndecl, int parmnum,
8941                             tsubst_flags_t complain)
8942 {
8943   enum tree_code codel = TREE_CODE (type);
8944   tree rhstype;
8945   enum tree_code coder;
8946 
8947   /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
8948      Strip such NOP_EXPRs, since RHS is used in non-lvalue context.  */
8949   if (TREE_CODE (rhs) == NOP_EXPR
8950       && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
8951       && codel != REFERENCE_TYPE)
8952     rhs = TREE_OPERAND (rhs, 0);
8953 
8954   if (type == error_mark_node
8955       || rhs == error_mark_node
8956       || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
8957     return error_mark_node;
8958 
8959   if (MAYBE_CLASS_TYPE_P (non_reference (type)))
8960     ;
8961   else if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
8962 	    && TREE_CODE (type) != ARRAY_TYPE
8963 	    && (TREE_CODE (type) != REFERENCE_TYPE
8964 		|| TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
8965 	   || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
8966 	       && !TYPE_REFFN_P (type))
8967 	   || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
8968     rhs = decay_conversion (rhs, complain);
8969 
8970   rhstype = TREE_TYPE (rhs);
8971   coder = TREE_CODE (rhstype);
8972 
8973   if (coder == ERROR_MARK)
8974     return error_mark_node;
8975 
8976   /* We accept references to incomplete types, so we can
8977      return here before checking if RHS is of complete type.  */
8978 
8979   if (codel == REFERENCE_TYPE)
8980     {
8981       /* This should eventually happen in convert_arguments.  */
8982       int savew = 0, savee = 0;
8983 
8984       if (fndecl)
8985 	savew = warningcount + werrorcount, savee = errorcount;
8986       rhs = initialize_reference (type, rhs, flags, complain);
8987 
8988       if (fndecl
8989 	  && (warningcount + werrorcount > savew || errorcount > savee))
8990 	inform (DECL_SOURCE_LOCATION (fndecl),
8991 		"in passing argument %P of %qD", parmnum, fndecl);
8992 
8993       return rhs;
8994     }
8995 
8996   if (exp != 0)
8997     exp = require_complete_type_sfinae (exp, complain);
8998   if (exp == error_mark_node)
8999     return error_mark_node;
9000 
9001   rhstype = non_reference (rhstype);
9002 
9003   type = complete_type (type);
9004 
9005   if (DIRECT_INIT_EXPR_P (type, rhs))
9006     /* Don't try to do copy-initialization if we already have
9007        direct-initialization.  */
9008     return rhs;
9009 
9010   if (MAYBE_CLASS_TYPE_P (type))
9011     return perform_implicit_conversion_flags (type, rhs, complain, flags);
9012 
9013   return convert_for_assignment (type, rhs, errtype, fndecl, parmnum,
9014 				 complain, flags);
9015 }
9016 
9017 /* If RETVAL is the address of, or a reference to, a local variable or
9018    temporary give an appropriate warning and return true.  */
9019 
9020 static bool
9021 maybe_warn_about_returning_address_of_local (tree retval)
9022 {
9023   tree valtype = TREE_TYPE (DECL_RESULT (current_function_decl));
9024   tree whats_returned = fold_for_warn (retval);
9025 
9026   for (;;)
9027     {
9028       if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
9029 	whats_returned = TREE_OPERAND (whats_returned, 1);
9030       else if (CONVERT_EXPR_P (whats_returned)
9031 	       || TREE_CODE (whats_returned) == NON_LVALUE_EXPR)
9032 	whats_returned = TREE_OPERAND (whats_returned, 0);
9033       else
9034 	break;
9035     }
9036 
9037   if (TREE_CODE (whats_returned) != ADDR_EXPR)
9038     return false;
9039   whats_returned = TREE_OPERAND (whats_returned, 0);
9040 
9041   while (TREE_CODE (whats_returned) == COMPONENT_REF
9042 	 || TREE_CODE (whats_returned) == ARRAY_REF)
9043     whats_returned = TREE_OPERAND (whats_returned, 0);
9044 
9045   if (TREE_CODE (valtype) == REFERENCE_TYPE)
9046     {
9047       if (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
9048 	  || TREE_CODE (whats_returned) == TARGET_EXPR)
9049 	{
9050 	  warning (OPT_Wreturn_local_addr, "returning reference to temporary");
9051 	  return true;
9052 	}
9053       if (VAR_P (whats_returned)
9054 	  && DECL_NAME (whats_returned)
9055 	  && TEMP_NAME_P (DECL_NAME (whats_returned)))
9056 	{
9057 	  warning (OPT_Wreturn_local_addr, "reference to non-lvalue returned");
9058 	  return true;
9059 	}
9060     }
9061 
9062   if (DECL_P (whats_returned)
9063       && DECL_NAME (whats_returned)
9064       && DECL_FUNCTION_SCOPE_P (whats_returned)
9065       && !is_capture_proxy (whats_returned)
9066       && !(TREE_STATIC (whats_returned)
9067 	   || TREE_PUBLIC (whats_returned)))
9068     {
9069       if (TREE_CODE (valtype) == REFERENCE_TYPE)
9070 	warning_at (DECL_SOURCE_LOCATION (whats_returned),
9071 		    OPT_Wreturn_local_addr,
9072 		    "reference to local variable %qD returned",
9073 		    whats_returned);
9074       else if (TREE_CODE (whats_returned) == LABEL_DECL)
9075 	warning_at (DECL_SOURCE_LOCATION (whats_returned),
9076 		    OPT_Wreturn_local_addr, "address of label %qD returned",
9077 		    whats_returned);
9078       else
9079 	warning_at (DECL_SOURCE_LOCATION (whats_returned),
9080 		    OPT_Wreturn_local_addr, "address of local variable %qD "
9081 		    "returned", whats_returned);
9082       return true;
9083     }
9084 
9085   return false;
9086 }
9087 
9088 /* Check that returning RETVAL from the current function is valid.
9089    Return an expression explicitly showing all conversions required to
9090    change RETVAL into the function return type, and to assign it to
9091    the DECL_RESULT for the function.  Set *NO_WARNING to true if
9092    code reaches end of non-void function warning shouldn't be issued
9093    on this RETURN_EXPR.  */
9094 
9095 tree
9096 check_return_expr (tree retval, bool *no_warning)
9097 {
9098   tree result;
9099   /* The type actually returned by the function.  */
9100   tree valtype;
9101   /* The type the function is declared to return, or void if
9102      the declared type is incomplete.  */
9103   tree functype;
9104   int fn_returns_value_p;
9105   bool named_return_value_okay_p;
9106 
9107   *no_warning = false;
9108 
9109   /* A `volatile' function is one that isn't supposed to return, ever.
9110      (This is a G++ extension, used to get better code for functions
9111      that call the `volatile' function.)  */
9112   if (TREE_THIS_VOLATILE (current_function_decl))
9113     warning (0, "function declared %<noreturn%> has a %<return%> statement");
9114 
9115   /* Check for various simple errors.  */
9116   if (DECL_DESTRUCTOR_P (current_function_decl))
9117     {
9118       if (retval)
9119 	error ("returning a value from a destructor");
9120       return NULL_TREE;
9121     }
9122   else if (DECL_CONSTRUCTOR_P (current_function_decl))
9123     {
9124       if (in_function_try_handler)
9125 	/* If a return statement appears in a handler of the
9126 	   function-try-block of a constructor, the program is ill-formed.  */
9127 	error ("cannot return from a handler of a function-try-block of a constructor");
9128       else if (retval)
9129 	/* You can't return a value from a constructor.  */
9130 	error ("returning a value from a constructor");
9131       return NULL_TREE;
9132     }
9133 
9134   const tree saved_retval = retval;
9135 
9136   if (processing_template_decl)
9137     {
9138       current_function_returns_value = 1;
9139 
9140       if (check_for_bare_parameter_packs (retval))
9141 	return error_mark_node;
9142 
9143       /* If one of the types might be void, we can't tell whether we're
9144 	 returning a value.  */
9145       if ((WILDCARD_TYPE_P (TREE_TYPE (DECL_RESULT (current_function_decl)))
9146 	   && !current_function_auto_return_pattern)
9147 	  || (retval != NULL_TREE
9148 	      && (TREE_TYPE (retval) == NULL_TREE
9149 		  || WILDCARD_TYPE_P (TREE_TYPE (retval)))))
9150 	goto dependent;
9151     }
9152 
9153   functype = TREE_TYPE (TREE_TYPE (current_function_decl));
9154 
9155   /* Deduce auto return type from a return statement.  */
9156   if (current_function_auto_return_pattern)
9157     {
9158       tree auto_node;
9159       tree type;
9160 
9161       if (!retval && !is_auto (current_function_auto_return_pattern))
9162 	{
9163 	  /* Give a helpful error message.  */
9164 	  error ("return-statement with no value, in function returning %qT",
9165 		 current_function_auto_return_pattern);
9166 	  inform (input_location, "only plain %<auto%> return type can be "
9167 		  "deduced to %<void%>");
9168 	  type = error_mark_node;
9169 	}
9170       else if (retval && BRACE_ENCLOSED_INITIALIZER_P (retval))
9171 	{
9172 	  error ("returning initializer list");
9173 	  type = error_mark_node;
9174 	}
9175       else
9176 	{
9177 	  if (!retval)
9178 	    retval = void_node;
9179 	  auto_node = type_uses_auto (current_function_auto_return_pattern);
9180 	  type = do_auto_deduction (current_function_auto_return_pattern,
9181 				    retval, auto_node);
9182 	}
9183 
9184       if (type == error_mark_node)
9185 	/* Leave it.  */;
9186       else if (functype == current_function_auto_return_pattern)
9187 	apply_deduced_return_type (current_function_decl, type);
9188       else if (!same_type_p (type, functype))
9189 	{
9190 	  if (LAMBDA_FUNCTION_P (current_function_decl))
9191 	    error ("inconsistent types %qT and %qT deduced for "
9192 		   "lambda return type", functype, type);
9193 	  else
9194 	    error ("inconsistent deduction for auto return type: "
9195 		   "%qT and then %qT", functype, type);
9196 	}
9197       functype = type;
9198     }
9199 
9200   result = DECL_RESULT (current_function_decl);
9201   valtype = TREE_TYPE (result);
9202   gcc_assert (valtype != NULL_TREE);
9203   fn_returns_value_p = !VOID_TYPE_P (valtype);
9204 
9205   /* Check for a return statement with no return value in a function
9206      that's supposed to return a value.  */
9207   if (!retval && fn_returns_value_p)
9208     {
9209       if (functype != error_mark_node)
9210 	permerror (input_location, "return-statement with no value, in "
9211 		   "function returning %qT", valtype);
9212       /* Remember that this function did return.  */
9213       current_function_returns_value = 1;
9214       /* And signal caller that TREE_NO_WARNING should be set on the
9215 	 RETURN_EXPR to avoid control reaches end of non-void function
9216 	 warnings in tree-cfg.c.  */
9217       *no_warning = true;
9218     }
9219   /* Check for a return statement with a value in a function that
9220      isn't supposed to return a value.  */
9221   else if (retval && !fn_returns_value_p)
9222     {
9223       if (VOID_TYPE_P (TREE_TYPE (retval)))
9224 	/* You can return a `void' value from a function of `void'
9225 	   type.  In that case, we have to evaluate the expression for
9226 	   its side-effects.  */
9227 	finish_expr_stmt (retval);
9228       else
9229 	permerror (input_location,
9230 		   "return-statement with a value, in function "
9231 		   "returning %qT", valtype);
9232       current_function_returns_null = 1;
9233 
9234       /* There's really no value to return, after all.  */
9235       return NULL_TREE;
9236     }
9237   else if (!retval)
9238     /* Remember that this function can sometimes return without a
9239        value.  */
9240     current_function_returns_null = 1;
9241   else
9242     /* Remember that this function did return a value.  */
9243     current_function_returns_value = 1;
9244 
9245   /* Check for erroneous operands -- but after giving ourselves a
9246      chance to provide an error about returning a value from a void
9247      function.  */
9248   if (error_operand_p (retval))
9249     {
9250       current_function_return_value = error_mark_node;
9251       return error_mark_node;
9252     }
9253 
9254   /* Only operator new(...) throw(), can return NULL [expr.new/13].  */
9255   if (IDENTIFIER_NEW_OP_P (DECL_NAME (current_function_decl))
9256       && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl))
9257       && ! flag_check_new
9258       && retval && null_ptr_cst_p (retval))
9259     warning (0, "%<operator new%> must not return NULL unless it is "
9260 	     "declared %<throw()%> (or -fcheck-new is in effect)");
9261 
9262   /* Effective C++ rule 15.  See also start_function.  */
9263   if (warn_ecpp
9264       && DECL_NAME (current_function_decl) == assign_op_identifier
9265       && !type_dependent_expression_p (retval))
9266     {
9267       bool warn = true;
9268 
9269       /* The function return type must be a reference to the current
9270 	class.  */
9271       if (TREE_CODE (valtype) == REFERENCE_TYPE
9272 	  && same_type_ignoring_top_level_qualifiers_p
9273 	      (TREE_TYPE (valtype), TREE_TYPE (current_class_ref)))
9274 	{
9275 	  /* Returning '*this' is obviously OK.  */
9276 	  if (retval == current_class_ref)
9277 	    warn = false;
9278 	  /* If we are calling a function whose return type is the same of
9279 	     the current class reference, it is ok.  */
9280 	  else if (INDIRECT_REF_P (retval)
9281 		   && TREE_CODE (TREE_OPERAND (retval, 0)) == CALL_EXPR)
9282 	    warn = false;
9283 	}
9284 
9285       if (warn)
9286 	warning (OPT_Weffc__, "%<operator=%> should return a reference to %<*this%>");
9287     }
9288 
9289   if (dependent_type_p (functype)
9290       || type_dependent_expression_p (retval))
9291     {
9292     dependent:
9293       /* We should not have changed the return value.  */
9294       gcc_assert (retval == saved_retval);
9295       return retval;
9296     }
9297 
9298   /* The fabled Named Return Value optimization, as per [class.copy]/15:
9299 
9300      [...]      For  a function with a class return type, if the expression
9301      in the return statement is the name of a local  object,  and  the  cv-
9302      unqualified  type  of  the  local  object  is the same as the function
9303      return type, an implementation is permitted to omit creating the  tem-
9304      porary  object  to  hold  the function return value [...]
9305 
9306      So, if this is a value-returning function that always returns the same
9307      local variable, remember it.
9308 
9309      It might be nice to be more flexible, and choose the first suitable
9310      variable even if the function sometimes returns something else, but
9311      then we run the risk of clobbering the variable we chose if the other
9312      returned expression uses the chosen variable somehow.  And people expect
9313      this restriction, anyway.  (jason 2000-11-19)
9314 
9315      See finish_function and finalize_nrv for the rest of this optimization.  */
9316 
9317   named_return_value_okay_p =
9318     (retval != NULL_TREE
9319      && !processing_template_decl
9320      /* Must be a local, automatic variable.  */
9321      && VAR_P (retval)
9322      && DECL_CONTEXT (retval) == current_function_decl
9323      && ! TREE_STATIC (retval)
9324      /* And not a lambda or anonymous union proxy.  */
9325      && !DECL_HAS_VALUE_EXPR_P (retval)
9326      && (DECL_ALIGN (retval) <= DECL_ALIGN (result))
9327      /* The cv-unqualified type of the returned value must be the
9328         same as the cv-unqualified return type of the
9329         function.  */
9330      && same_type_p ((TYPE_MAIN_VARIANT (TREE_TYPE (retval))),
9331                      (TYPE_MAIN_VARIANT (functype)))
9332      /* And the returned value must be non-volatile.  */
9333      && ! TYPE_VOLATILE (TREE_TYPE (retval)));
9334 
9335   if (fn_returns_value_p && flag_elide_constructors)
9336     {
9337       if (named_return_value_okay_p
9338           && (current_function_return_value == NULL_TREE
9339               || current_function_return_value == retval))
9340 	current_function_return_value = retval;
9341       else
9342 	current_function_return_value = error_mark_node;
9343     }
9344 
9345   /* We don't need to do any conversions when there's nothing being
9346      returned.  */
9347   if (!retval)
9348     return NULL_TREE;
9349 
9350   /* Do any required conversions.  */
9351   if (retval == result || DECL_CONSTRUCTOR_P (current_function_decl))
9352     /* No conversions are required.  */
9353     ;
9354   else
9355     {
9356       int flags = LOOKUP_NORMAL | LOOKUP_ONLYCONVERTING;
9357 
9358       /* The functype's return type will have been set to void, if it
9359 	 was an incomplete type.  Just treat this as 'return;' */
9360       if (VOID_TYPE_P (functype))
9361 	return error_mark_node;
9362 
9363       /* If we had an id-expression obfuscated by force_paren_expr, we need
9364 	 to undo it so we can try to treat it as an rvalue below.  */
9365       retval = maybe_undo_parenthesized_ref (retval);
9366 
9367       if (processing_template_decl)
9368 	retval = build_non_dependent_expr (retval);
9369 
9370       /* Under C++11 [12.8/32 class.copy], a returned lvalue is sometimes
9371 	 treated as an rvalue for the purposes of overload resolution to
9372 	 favor move constructors over copy constructors.
9373 
9374          Note that these conditions are similar to, but not as strict as,
9375 	 the conditions for the named return value optimization.  */
9376       bool converted = false;
9377       if ((cxx_dialect != cxx98)
9378           && ((VAR_P (retval) && !DECL_HAS_VALUE_EXPR_P (retval))
9379 	      || TREE_CODE (retval) == PARM_DECL)
9380 	  && DECL_CONTEXT (retval) == current_function_decl
9381 	  && !TREE_STATIC (retval)
9382 	  /* This is only interesting for class type.  */
9383 	  && CLASS_TYPE_P (functype))
9384 	{
9385 	  tree moved = move (retval);
9386 	  moved = convert_for_initialization
9387 	    (NULL_TREE, functype, moved, flags|LOOKUP_PREFER_RVALUE,
9388 	     ICR_RETURN, NULL_TREE, 0, tf_none);
9389 	  if (moved != error_mark_node)
9390 	    {
9391 	      retval = moved;
9392 	      converted = true;
9393 	    }
9394 	}
9395 
9396       /* First convert the value to the function's return type, then
9397 	 to the type of return value's location to handle the
9398 	 case that functype is smaller than the valtype.  */
9399       if (!converted)
9400 	retval = convert_for_initialization
9401 	  (NULL_TREE, functype, retval, flags, ICR_RETURN, NULL_TREE, 0,
9402 	   tf_warning_or_error);
9403       retval = convert (valtype, retval);
9404 
9405       /* If the conversion failed, treat this just like `return;'.  */
9406       if (retval == error_mark_node)
9407 	return retval;
9408       /* We can't initialize a register from a AGGR_INIT_EXPR.  */
9409       else if (! cfun->returns_struct
9410 	       && TREE_CODE (retval) == TARGET_EXPR
9411 	       && TREE_CODE (TREE_OPERAND (retval, 1)) == AGGR_INIT_EXPR)
9412 	retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
9413 			 TREE_OPERAND (retval, 0));
9414       else if (!processing_template_decl
9415 	       && maybe_warn_about_returning_address_of_local (retval))
9416 	retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
9417 			 build_zero_cst (TREE_TYPE (retval)));
9418     }
9419 
9420   if (processing_template_decl)
9421     return saved_retval;
9422 
9423   /* Actually copy the value returned into the appropriate location.  */
9424   if (retval && retval != result)
9425     retval = build2 (INIT_EXPR, TREE_TYPE (result), result, retval);
9426 
9427   return retval;
9428 }
9429 
9430 
9431 /* Returns nonzero if the pointer-type FROM can be converted to the
9432    pointer-type TO via a qualification conversion.  If CONSTP is -1,
9433    then we return nonzero if the pointers are similar, and the
9434    cv-qualification signature of FROM is a proper subset of that of TO.
9435 
9436    If CONSTP is positive, then all outer pointers have been
9437    const-qualified.  */
9438 
9439 static int
9440 comp_ptr_ttypes_real (tree to, tree from, int constp)
9441 {
9442   bool to_more_cv_qualified = false;
9443   bool is_opaque_pointer = false;
9444 
9445   for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
9446     {
9447       if (TREE_CODE (to) != TREE_CODE (from))
9448 	return 0;
9449 
9450       if (TREE_CODE (from) == OFFSET_TYPE
9451 	  && !same_type_p (TYPE_OFFSET_BASETYPE (from),
9452 			   TYPE_OFFSET_BASETYPE (to)))
9453 	return 0;
9454 
9455       /* Const and volatile mean something different for function types,
9456 	 so the usual checks are not appropriate.  */
9457       if (TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
9458 	{
9459 	  if (!at_least_as_qualified_p (to, from))
9460 	    return 0;
9461 
9462 	  if (!at_least_as_qualified_p (from, to))
9463 	    {
9464 	      if (constp == 0)
9465 		return 0;
9466 	      to_more_cv_qualified = true;
9467 	    }
9468 
9469 	  if (constp > 0)
9470 	    constp &= TYPE_READONLY (to);
9471 	}
9472 
9473       if (VECTOR_TYPE_P (to))
9474 	is_opaque_pointer = vector_targets_convertible_p (to, from);
9475 
9476       if (!TYPE_PTR_P (to) && !TYPE_PTRDATAMEM_P (to))
9477 	return ((constp >= 0 || to_more_cv_qualified)
9478 		&& (is_opaque_pointer
9479 		    || same_type_ignoring_top_level_qualifiers_p (to, from)));
9480     }
9481 }
9482 
9483 /* When comparing, say, char ** to char const **, this function takes
9484    the 'char *' and 'char const *'.  Do not pass non-pointer/reference
9485    types to this function.  */
9486 
9487 int
9488 comp_ptr_ttypes (tree to, tree from)
9489 {
9490   return comp_ptr_ttypes_real (to, from, 1);
9491 }
9492 
9493 /* Returns true iff FNTYPE is a non-class type that involves
9494    error_mark_node.  We can get FUNCTION_TYPE with buried error_mark_node
9495    if a parameter type is ill-formed.  */
9496 
9497 bool
9498 error_type_p (const_tree type)
9499 {
9500   tree t;
9501 
9502   switch (TREE_CODE (type))
9503     {
9504     case ERROR_MARK:
9505       return true;
9506 
9507     case POINTER_TYPE:
9508     case REFERENCE_TYPE:
9509     case OFFSET_TYPE:
9510       return error_type_p (TREE_TYPE (type));
9511 
9512     case FUNCTION_TYPE:
9513     case METHOD_TYPE:
9514       if (error_type_p (TREE_TYPE (type)))
9515 	return true;
9516       for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
9517 	if (error_type_p (TREE_VALUE (t)))
9518 	  return true;
9519       return false;
9520 
9521     case RECORD_TYPE:
9522       if (TYPE_PTRMEMFUNC_P (type))
9523 	return error_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type));
9524       return false;
9525 
9526     default:
9527       return false;
9528     }
9529 }
9530 
9531 /* Returns true if to and from are (possibly multi-level) pointers to the same
9532    type or inheritance-related types, regardless of cv-quals.  */
9533 
9534 bool
9535 ptr_reasonably_similar (const_tree to, const_tree from)
9536 {
9537   for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
9538     {
9539       /* Any target type is similar enough to void.  */
9540       if (VOID_TYPE_P (to))
9541 	return !error_type_p (from);
9542       if (VOID_TYPE_P (from))
9543 	return !error_type_p (to);
9544 
9545       if (TREE_CODE (to) != TREE_CODE (from))
9546 	return false;
9547 
9548       if (TREE_CODE (from) == OFFSET_TYPE
9549 	  && comptypes (TYPE_OFFSET_BASETYPE (to),
9550 			TYPE_OFFSET_BASETYPE (from),
9551 			COMPARE_BASE | COMPARE_DERIVED))
9552 	continue;
9553 
9554       if (VECTOR_TYPE_P (to)
9555 	  && vector_types_convertible_p (to, from, false))
9556 	return true;
9557 
9558       if (TREE_CODE (to) == INTEGER_TYPE
9559 	  && TYPE_PRECISION (to) == TYPE_PRECISION (from))
9560 	return true;
9561 
9562       if (TREE_CODE (to) == FUNCTION_TYPE)
9563 	return !error_type_p (to) && !error_type_p (from);
9564 
9565       if (!TYPE_PTR_P (to))
9566 	{
9567 	  /* When either type is incomplete avoid DERIVED_FROM_P,
9568 	     which may call complete_type (c++/57942).  */
9569 	  bool b = !COMPLETE_TYPE_P (to) || !COMPLETE_TYPE_P (from);
9570 	  return comptypes
9571 	    (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from),
9572 	     b ? COMPARE_STRICT : COMPARE_BASE | COMPARE_DERIVED);
9573 	}
9574     }
9575 }
9576 
9577 /* Return true if TO and FROM (both of which are POINTER_TYPEs or
9578    pointer-to-member types) are the same, ignoring cv-qualification at
9579    all levels.  */
9580 
9581 bool
9582 comp_ptr_ttypes_const (tree to, tree from)
9583 {
9584   bool is_opaque_pointer = false;
9585 
9586   for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
9587     {
9588       if (TREE_CODE (to) != TREE_CODE (from))
9589 	return false;
9590 
9591       if (TREE_CODE (from) == OFFSET_TYPE
9592 	  && same_type_p (TYPE_OFFSET_BASETYPE (from),
9593 			  TYPE_OFFSET_BASETYPE (to)))
9594 	  continue;
9595 
9596       if (VECTOR_TYPE_P (to))
9597 	is_opaque_pointer = vector_targets_convertible_p (to, from);
9598 
9599       if (!TYPE_PTR_P (to))
9600 	return (is_opaque_pointer
9601 		|| same_type_ignoring_top_level_qualifiers_p (to, from));
9602     }
9603 }
9604 
9605 /* Returns the type qualifiers for this type, including the qualifiers on the
9606    elements for an array type.  */
9607 
9608 int
9609 cp_type_quals (const_tree type)
9610 {
9611   int quals;
9612   /* This CONST_CAST is okay because strip_array_types returns its
9613      argument unmodified and we assign it to a const_tree.  */
9614   type = strip_array_types (CONST_CAST_TREE (type));
9615   if (type == error_mark_node
9616       /* Quals on a FUNCTION_TYPE are memfn quals.  */
9617       || TREE_CODE (type) == FUNCTION_TYPE)
9618     return TYPE_UNQUALIFIED;
9619   quals = TYPE_QUALS (type);
9620   /* METHOD and REFERENCE_TYPEs should never have quals.  */
9621   gcc_assert ((TREE_CODE (type) != METHOD_TYPE
9622 	       && TREE_CODE (type) != REFERENCE_TYPE)
9623 	      || ((quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE))
9624 		  == TYPE_UNQUALIFIED));
9625   return quals;
9626 }
9627 
9628 /* Returns the function-ref-qualifier for TYPE */
9629 
9630 cp_ref_qualifier
9631 type_memfn_rqual (const_tree type)
9632 {
9633   gcc_assert (TREE_CODE (type) == FUNCTION_TYPE
9634               || TREE_CODE (type) == METHOD_TYPE);
9635 
9636   if (!FUNCTION_REF_QUALIFIED (type))
9637     return REF_QUAL_NONE;
9638   else if (FUNCTION_RVALUE_QUALIFIED (type))
9639     return REF_QUAL_RVALUE;
9640   else
9641     return REF_QUAL_LVALUE;
9642 }
9643 
9644 /* Returns the function-cv-quals for TYPE, which must be a FUNCTION_TYPE or
9645    METHOD_TYPE.  */
9646 
9647 int
9648 type_memfn_quals (const_tree type)
9649 {
9650   if (TREE_CODE (type) == FUNCTION_TYPE)
9651     return TYPE_QUALS (type);
9652   else if (TREE_CODE (type) == METHOD_TYPE)
9653     return cp_type_quals (class_of_this_parm (type));
9654   else
9655     gcc_unreachable ();
9656 }
9657 
9658 /* Returns the FUNCTION_TYPE TYPE with its function-cv-quals changed to
9659    MEMFN_QUALS and its ref-qualifier to RQUAL. */
9660 
9661 tree
9662 apply_memfn_quals (tree type, cp_cv_quals memfn_quals, cp_ref_qualifier rqual)
9663 {
9664   /* Could handle METHOD_TYPE here if necessary.  */
9665   gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
9666   if (TYPE_QUALS (type) == memfn_quals
9667       && type_memfn_rqual (type) == rqual)
9668     return type;
9669 
9670   /* This should really have a different TYPE_MAIN_VARIANT, but that gets
9671      complex.  */
9672   tree result = build_qualified_type (type, memfn_quals);
9673   return build_ref_qualified_type (result, rqual);
9674 }
9675 
9676 /* Returns nonzero if TYPE is const or volatile.  */
9677 
9678 bool
9679 cv_qualified_p (const_tree type)
9680 {
9681   int quals = cp_type_quals (type);
9682   return (quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)) != 0;
9683 }
9684 
9685 /* Returns nonzero if the TYPE contains a mutable member.  */
9686 
9687 bool
9688 cp_has_mutable_p (const_tree type)
9689 {
9690   /* This CONST_CAST is okay because strip_array_types returns its
9691      argument unmodified and we assign it to a const_tree.  */
9692   type = strip_array_types (CONST_CAST_TREE(type));
9693 
9694   return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type);
9695 }
9696 
9697 /* Set TREE_READONLY and TREE_VOLATILE on DECL as indicated by the
9698    TYPE_QUALS.  For a VAR_DECL, this may be an optimistic
9699    approximation.  In particular, consider:
9700 
9701      int f();
9702      struct S { int i; };
9703      const S s = { f(); }
9704 
9705    Here, we will make "s" as TREE_READONLY (because it is declared
9706    "const") -- only to reverse ourselves upon seeing that the
9707    initializer is non-constant.  */
9708 
9709 void
9710 cp_apply_type_quals_to_decl (int type_quals, tree decl)
9711 {
9712   tree type = TREE_TYPE (decl);
9713 
9714   if (type == error_mark_node)
9715     return;
9716 
9717   if (TREE_CODE (decl) == TYPE_DECL)
9718     return;
9719 
9720   gcc_assert (!(TREE_CODE (type) == FUNCTION_TYPE
9721 		&& type_quals != TYPE_UNQUALIFIED));
9722 
9723   /* Avoid setting TREE_READONLY incorrectly.  */
9724   /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
9725      constructor can produce constant init, so rely on cp_finish_decl to
9726      clear TREE_READONLY if the variable has non-constant init.  */
9727 
9728   /* If the type has (or might have) a mutable component, that component
9729      might be modified.  */
9730   if (TYPE_HAS_MUTABLE_P (type) || !COMPLETE_TYPE_P (type))
9731     type_quals &= ~TYPE_QUAL_CONST;
9732 
9733   c_apply_type_quals_to_decl (type_quals, decl);
9734 }
9735 
9736 /* Subroutine of casts_away_constness.  Make T1 and T2 point at
9737    exemplar types such that casting T1 to T2 is casting away constness
9738    if and only if there is no implicit conversion from T1 to T2.  */
9739 
9740 static void
9741 casts_away_constness_r (tree *t1, tree *t2, tsubst_flags_t complain)
9742 {
9743   int quals1;
9744   int quals2;
9745 
9746   /* [expr.const.cast]
9747 
9748      For multi-level pointer to members and multi-level mixed pointers
9749      and pointers to members (conv.qual), the "member" aspect of a
9750      pointer to member level is ignored when determining if a const
9751      cv-qualifier has been cast away.  */
9752   /* [expr.const.cast]
9753 
9754      For  two  pointer types:
9755 
9756 	    X1 is T1cv1,1 * ... cv1,N *   where T1 is not a pointer type
9757 	    X2 is T2cv2,1 * ... cv2,M *   where T2 is not a pointer type
9758 	    K is min(N,M)
9759 
9760      casting from X1 to X2 casts away constness if, for a non-pointer
9761      type T there does not exist an implicit conversion (clause
9762      _conv_) from:
9763 
9764 	    Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
9765 
9766      to
9767 
9768 	    Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *.  */
9769   if ((!TYPE_PTR_P (*t1) && !TYPE_PTRDATAMEM_P (*t1))
9770       || (!TYPE_PTR_P (*t2) && !TYPE_PTRDATAMEM_P (*t2)))
9771     {
9772       *t1 = cp_build_qualified_type (void_type_node,
9773 				     cp_type_quals (*t1));
9774       *t2 = cp_build_qualified_type (void_type_node,
9775 				     cp_type_quals (*t2));
9776       return;
9777     }
9778 
9779   quals1 = cp_type_quals (*t1);
9780   quals2 = cp_type_quals (*t2);
9781 
9782   if (TYPE_PTRDATAMEM_P (*t1))
9783     *t1 = TYPE_PTRMEM_POINTED_TO_TYPE (*t1);
9784   else
9785     *t1 = TREE_TYPE (*t1);
9786   if (TYPE_PTRDATAMEM_P (*t2))
9787     *t2 = TYPE_PTRMEM_POINTED_TO_TYPE (*t2);
9788   else
9789     *t2 = TREE_TYPE (*t2);
9790 
9791   casts_away_constness_r (t1, t2, complain);
9792   *t1 = build_pointer_type (*t1);
9793   *t2 = build_pointer_type (*t2);
9794   *t1 = cp_build_qualified_type (*t1, quals1);
9795   *t2 = cp_build_qualified_type (*t2, quals2);
9796 }
9797 
9798 /* Returns nonzero if casting from TYPE1 to TYPE2 casts away
9799    constness.
9800 
9801    ??? This function returns non-zero if casting away qualifiers not
9802    just const.  We would like to return to the caller exactly which
9803    qualifiers are casted away to give more accurate diagnostics.
9804 */
9805 
9806 static bool
9807 casts_away_constness (tree t1, tree t2, tsubst_flags_t complain)
9808 {
9809   if (TREE_CODE (t2) == REFERENCE_TYPE)
9810     {
9811       /* [expr.const.cast]
9812 
9813 	 Casting from an lvalue of type T1 to an lvalue of type T2
9814 	 using a reference cast casts away constness if a cast from an
9815 	 rvalue of type "pointer to T1" to the type "pointer to T2"
9816 	 casts away constness.  */
9817       t1 = (TREE_CODE (t1) == REFERENCE_TYPE ? TREE_TYPE (t1) : t1);
9818       return casts_away_constness (build_pointer_type (t1),
9819 				   build_pointer_type (TREE_TYPE (t2)),
9820 				   complain);
9821     }
9822 
9823   if (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
9824     /* [expr.const.cast]
9825 
9826        Casting from an rvalue of type "pointer to data member of X
9827        of type T1" to the type "pointer to data member of Y of type
9828        T2" casts away constness if a cast from an rvalue of type
9829        "pointer to T1" to the type "pointer to T2" casts away
9830        constness.  */
9831     return casts_away_constness
9832       (build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t1)),
9833        build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t2)),
9834        complain);
9835 
9836   /* Casting away constness is only something that makes sense for
9837      pointer or reference types.  */
9838   if (!TYPE_PTR_P (t1) || !TYPE_PTR_P (t2))
9839     return false;
9840 
9841   /* Top-level qualifiers don't matter.  */
9842   t1 = TYPE_MAIN_VARIANT (t1);
9843   t2 = TYPE_MAIN_VARIANT (t2);
9844   casts_away_constness_r (&t1, &t2, complain);
9845   if (!can_convert (t2, t1, complain))
9846     return true;
9847 
9848   return false;
9849 }
9850 
9851 /* If T is a REFERENCE_TYPE return the type to which T refers.
9852    Otherwise, return T itself.  */
9853 
9854 tree
9855 non_reference (tree t)
9856 {
9857   if (t && TREE_CODE (t) == REFERENCE_TYPE)
9858     t = TREE_TYPE (t);
9859   return t;
9860 }
9861 
9862 
9863 /* Return nonzero if REF is an lvalue valid for this language;
9864    otherwise, print an error message and return zero.  USE says
9865    how the lvalue is being used and so selects the error message.  */
9866 
9867 int
9868 lvalue_or_else (tree ref, enum lvalue_use use, tsubst_flags_t complain)
9869 {
9870   cp_lvalue_kind kind = lvalue_kind (ref);
9871 
9872   if (kind == clk_none)
9873     {
9874       if (complain & tf_error)
9875 	lvalue_error (input_location, use);
9876       return 0;
9877     }
9878   else if (kind & (clk_rvalueref|clk_class))
9879     {
9880       if (!(complain & tf_error))
9881 	return 0;
9882       if (kind & clk_class)
9883 	/* Make this a permerror because we used to accept it.  */
9884 	permerror (input_location, "using temporary as lvalue");
9885       else
9886 	error ("using xvalue (rvalue reference) as lvalue");
9887     }
9888   return 1;
9889 }
9890 
9891 /* Return true if a user-defined literal operator is a raw operator.  */
9892 
9893 bool
9894 check_raw_literal_operator (const_tree decl)
9895 {
9896   tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
9897   tree argtype;
9898   int arity;
9899   bool maybe_raw_p = false;
9900 
9901   /* Count the number and type of arguments and check for ellipsis.  */
9902   for (argtype = argtypes, arity = 0;
9903        argtype && argtype != void_list_node;
9904        ++arity, argtype = TREE_CHAIN (argtype))
9905     {
9906       tree t = TREE_VALUE (argtype);
9907 
9908       if (same_type_p (t, const_string_type_node))
9909 	maybe_raw_p = true;
9910     }
9911   if (!argtype)
9912     return false; /* Found ellipsis.  */
9913 
9914   if (!maybe_raw_p || arity != 1)
9915     return false;
9916 
9917   return true;
9918 }
9919 
9920 
9921 /* Return true if a user-defined literal operator has one of the allowed
9922    argument types.  */
9923 
9924 bool
9925 check_literal_operator_args (const_tree decl,
9926 			     bool *long_long_unsigned_p, bool *long_double_p)
9927 {
9928   tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
9929 
9930   *long_long_unsigned_p = false;
9931   *long_double_p = false;
9932   if (processing_template_decl || processing_specialization)
9933     return argtypes == void_list_node;
9934   else
9935     {
9936       tree argtype;
9937       int arity;
9938       int max_arity = 2;
9939 
9940       /* Count the number and type of arguments and check for ellipsis.  */
9941       for (argtype = argtypes, arity = 0;
9942 	   argtype && argtype != void_list_node;
9943 	   argtype = TREE_CHAIN (argtype))
9944 	{
9945 	  tree t = TREE_VALUE (argtype);
9946 	  ++arity;
9947 
9948 	  if (TYPE_PTR_P (t))
9949 	    {
9950 	      bool maybe_raw_p = false;
9951 	      t = TREE_TYPE (t);
9952 	      if (cp_type_quals (t) != TYPE_QUAL_CONST)
9953 		return false;
9954 	      t = TYPE_MAIN_VARIANT (t);
9955 	      if ((maybe_raw_p = same_type_p (t, char_type_node))
9956 		  || same_type_p (t, wchar_type_node)
9957 		  || same_type_p (t, char16_type_node)
9958 		  || same_type_p (t, char32_type_node))
9959 		{
9960 		  argtype = TREE_CHAIN (argtype);
9961 		  if (!argtype)
9962 		    return false;
9963 		  t = TREE_VALUE (argtype);
9964 		  if (maybe_raw_p && argtype == void_list_node)
9965 		    return true;
9966 		  else if (same_type_p (t, size_type_node))
9967 		    {
9968 		      ++arity;
9969 		      continue;
9970 		    }
9971 		  else
9972 		    return false;
9973 		}
9974 	    }
9975 	  else if (same_type_p (t, long_long_unsigned_type_node))
9976 	    {
9977 	      max_arity = 1;
9978 	      *long_long_unsigned_p = true;
9979 	    }
9980 	  else if (same_type_p (t, long_double_type_node))
9981 	    {
9982 	      max_arity = 1;
9983 	      *long_double_p = true;
9984 	    }
9985 	  else if (same_type_p (t, char_type_node))
9986 	    max_arity = 1;
9987 	  else if (same_type_p (t, wchar_type_node))
9988 	    max_arity = 1;
9989 	  else if (same_type_p (t, char16_type_node))
9990 	    max_arity = 1;
9991 	  else if (same_type_p (t, char32_type_node))
9992 	    max_arity = 1;
9993 	  else
9994 	    return false;
9995 	}
9996       if (!argtype)
9997 	return false; /* Found ellipsis.  */
9998 
9999       if (arity != max_arity)
10000 	return false;
10001 
10002       return true;
10003     }
10004 }
10005 
10006 /* Always returns false since unlike C90, C++ has no concept of implicit
10007    function declarations.  */
10008 
10009 bool
10010 c_decl_implicit (const_tree)
10011 {
10012   return false;
10013 }
10014