xref: /dragonfly/contrib/gcc-8.0/gcc/cp/typeck.c (revision 7ff0fc30)
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       {
2425 	temp = cp_build_fold_indirect_ref (temp);
2426 	if (xvalue_p (object) && !xvalue_p (temp))
2427 	  /* Preserve xvalue kind.  */
2428 	  temp = move (temp);
2429 	object = temp;
2430       }
2431   }
2432 
2433   /* In [expr.ref], there is an explicit list of the valid choices for
2434      MEMBER.  We check for each of those cases here.  */
2435   if (VAR_P (member))
2436     {
2437       /* A static data member.  */
2438       result = member;
2439       mark_exp_read (object);
2440       /* If OBJECT has side-effects, they are supposed to occur.  */
2441       if (TREE_SIDE_EFFECTS (object))
2442 	result = build2 (COMPOUND_EXPR, TREE_TYPE (result), object, result);
2443     }
2444   else if (TREE_CODE (member) == FIELD_DECL)
2445     {
2446       /* A non-static data member.  */
2447       bool null_object_p;
2448       int type_quals;
2449       tree member_type;
2450 
2451       if (INDIRECT_REF_P (object))
2452 	null_object_p =
2453 	  integer_zerop (tree_strip_nop_conversions (TREE_OPERAND (object, 0)));
2454       else
2455 	null_object_p = false;
2456 
2457       /* Convert OBJECT to the type of MEMBER.  */
2458       if (!same_type_p (TYPE_MAIN_VARIANT (object_type),
2459 			TYPE_MAIN_VARIANT (member_scope)))
2460 	{
2461 	  tree binfo;
2462 	  base_kind kind;
2463 
2464 	  binfo = lookup_base (access_path ? access_path : object_type,
2465 			       member_scope, ba_unique, &kind, complain);
2466 	  if (binfo == error_mark_node)
2467 	    return error_mark_node;
2468 
2469 	  /* It is invalid to try to get to a virtual base of a
2470 	     NULL object.  The most common cause is invalid use of
2471 	     offsetof macro.  */
2472 	  if (null_object_p && kind == bk_via_virtual)
2473 	    {
2474 	      if (complain & tf_error)
2475 		{
2476 		  error ("invalid access to non-static data member %qD in "
2477 			 "virtual base of NULL object", member);
2478 		}
2479 	      return error_mark_node;
2480 	    }
2481 
2482 	  /* Convert to the base.  */
2483 	  object = build_base_path (PLUS_EXPR, object, binfo,
2484 				    /*nonnull=*/1, complain);
2485 	  /* If we found the base successfully then we should be able
2486 	     to convert to it successfully.  */
2487 	  gcc_assert (object != error_mark_node);
2488 	}
2489 
2490       /* If MEMBER is from an anonymous aggregate, we have converted
2491 	 OBJECT so that it refers to the class containing the
2492 	 anonymous union.  Generate a reference to the anonymous union
2493 	 itself, and recur to find MEMBER.  */
2494       if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member))
2495 	  /* When this code is called from build_field_call, the
2496 	     object already has the type of the anonymous union.
2497 	     That is because the COMPONENT_REF was already
2498 	     constructed, and was then disassembled before calling
2499 	     build_field_call.  After the function-call code is
2500 	     cleaned up, this waste can be eliminated.  */
2501 	  && (!same_type_ignoring_top_level_qualifiers_p
2502 	      (TREE_TYPE (object), DECL_CONTEXT (member))))
2503 	{
2504 	  tree anonymous_union;
2505 
2506 	  anonymous_union = lookup_anon_field (TREE_TYPE (object),
2507 					       DECL_CONTEXT (member));
2508 	  object = build_class_member_access_expr (object,
2509 						   anonymous_union,
2510 						   /*access_path=*/NULL_TREE,
2511 						   preserve_reference,
2512 						   complain);
2513 	}
2514 
2515       /* Compute the type of the field, as described in [expr.ref].  */
2516       type_quals = TYPE_UNQUALIFIED;
2517       member_type = TREE_TYPE (member);
2518       if (TREE_CODE (member_type) != REFERENCE_TYPE)
2519 	{
2520 	  type_quals = (cp_type_quals (member_type)
2521 			| cp_type_quals (object_type));
2522 
2523 	  /* A field is const (volatile) if the enclosing object, or the
2524 	     field itself, is const (volatile).  But, a mutable field is
2525 	     not const, even within a const object.  */
2526 	  if (DECL_MUTABLE_P (member))
2527 	    type_quals &= ~TYPE_QUAL_CONST;
2528 	  member_type = cp_build_qualified_type (member_type, type_quals);
2529 	}
2530 
2531       result = build3_loc (input_location, COMPONENT_REF, member_type,
2532 			   object, member, NULL_TREE);
2533 
2534       /* Mark the expression const or volatile, as appropriate.  Even
2535 	 though we've dealt with the type above, we still have to mark the
2536 	 expression itself.  */
2537       if (type_quals & TYPE_QUAL_CONST)
2538 	TREE_READONLY (result) = 1;
2539       if (type_quals & TYPE_QUAL_VOLATILE)
2540 	TREE_THIS_VOLATILE (result) = 1;
2541     }
2542   else if (BASELINK_P (member))
2543     {
2544       /* The member is a (possibly overloaded) member function.  */
2545       tree functions;
2546       tree type;
2547 
2548       /* If the MEMBER is exactly one static member function, then we
2549 	 know the type of the expression.  Otherwise, we must wait
2550 	 until overload resolution has been performed.  */
2551       functions = BASELINK_FUNCTIONS (member);
2552       if (TREE_CODE (functions) == FUNCTION_DECL
2553 	  && DECL_STATIC_FUNCTION_P (functions))
2554 	type = TREE_TYPE (functions);
2555       else
2556 	type = unknown_type_node;
2557       /* Note that we do not convert OBJECT to the BASELINK_BINFO
2558 	 base.  That will happen when the function is called.  */
2559       result = build3 (COMPONENT_REF, type, object, member, NULL_TREE);
2560     }
2561   else if (TREE_CODE (member) == CONST_DECL)
2562     {
2563       /* The member is an enumerator.  */
2564       result = member;
2565       /* If OBJECT has side-effects, they are supposed to occur.  */
2566       if (TREE_SIDE_EFFECTS (object))
2567 	result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
2568 			 object, result);
2569     }
2570   else if ((using_decl = strip_using_decl (member)) != member)
2571     result = build_class_member_access_expr (object,
2572 					     using_decl,
2573 					     access_path, preserve_reference,
2574 					     complain);
2575   else
2576     {
2577       if (complain & tf_error)
2578 	error ("invalid use of %qD", member);
2579       return error_mark_node;
2580     }
2581 
2582   if (!preserve_reference)
2583     /* [expr.ref]
2584 
2585        If E2 is declared to have type "reference to T", then ... the
2586        type of E1.E2 is T.  */
2587     result = convert_from_reference (result);
2588 
2589   return result;
2590 }
2591 
2592 /* Return the destructor denoted by OBJECT.SCOPE::DTOR_NAME, or, if
2593    SCOPE is NULL, by OBJECT.DTOR_NAME, where DTOR_NAME is ~type.  */
2594 
2595 static tree
2596 lookup_destructor (tree object, tree scope, tree dtor_name,
2597 		   tsubst_flags_t complain)
2598 {
2599   tree object_type = TREE_TYPE (object);
2600   tree dtor_type = TREE_OPERAND (dtor_name, 0);
2601   tree expr;
2602 
2603   /* We've already complained about this destructor.  */
2604   if (dtor_type == error_mark_node)
2605     return error_mark_node;
2606 
2607   if (scope && !check_dtor_name (scope, dtor_type))
2608     {
2609       if (complain & tf_error)
2610 	error ("qualified type %qT does not match destructor name ~%qT",
2611 	       scope, dtor_type);
2612       return error_mark_node;
2613     }
2614   if (is_auto (dtor_type))
2615     dtor_type = object_type;
2616   else if (identifier_p (dtor_type))
2617     {
2618       /* In a template, names we can't find a match for are still accepted
2619 	 destructor names, and we check them here.  */
2620       if (check_dtor_name (object_type, dtor_type))
2621 	dtor_type = object_type;
2622       else
2623 	{
2624 	  if (complain & tf_error)
2625 	    error ("object type %qT does not match destructor name ~%qT",
2626 		   object_type, dtor_type);
2627 	  return error_mark_node;
2628 	}
2629 
2630     }
2631   else if (!DERIVED_FROM_P (dtor_type, TYPE_MAIN_VARIANT (object_type)))
2632     {
2633       if (complain & tf_error)
2634 	error ("the type being destroyed is %qT, but the destructor "
2635 	       "refers to %qT", TYPE_MAIN_VARIANT (object_type), dtor_type);
2636       return error_mark_node;
2637     }
2638   expr = lookup_member (dtor_type, complete_dtor_identifier,
2639 			/*protect=*/1, /*want_type=*/false,
2640 			tf_warning_or_error);
2641   if (!expr)
2642     {
2643       if (complain & tf_error)
2644 	cxx_incomplete_type_error (dtor_name, dtor_type);
2645       return error_mark_node;
2646     }
2647   expr = (adjust_result_of_qualified_name_lookup
2648 	  (expr, dtor_type, object_type));
2649   if (scope == NULL_TREE)
2650     /* We need to call adjust_result_of_qualified_name_lookup in case the
2651        destructor names a base class, but we unset BASELINK_QUALIFIED_P so
2652        that we still get virtual function binding.  */
2653     BASELINK_QUALIFIED_P (expr) = false;
2654   return expr;
2655 }
2656 
2657 /* An expression of the form "A::template B" has been resolved to
2658    DECL.  Issue a diagnostic if B is not a template or template
2659    specialization.  */
2660 
2661 void
2662 check_template_keyword (tree decl)
2663 {
2664   /* The standard says:
2665 
2666       [temp.names]
2667 
2668       If a name prefixed by the keyword template is not a member
2669       template, the program is ill-formed.
2670 
2671      DR 228 removed the restriction that the template be a member
2672      template.
2673 
2674      DR 96, if accepted would add the further restriction that explicit
2675      template arguments must be provided if the template keyword is
2676      used, but, as of 2005-10-16, that DR is still in "drafting".  If
2677      this DR is accepted, then the semantic checks here can be
2678      simplified, as the entity named must in fact be a template
2679      specialization, rather than, as at present, a set of overloaded
2680      functions containing at least one template function.  */
2681   if (TREE_CODE (decl) != TEMPLATE_DECL
2682       && TREE_CODE (decl) != TEMPLATE_ID_EXPR)
2683     {
2684       if (VAR_P (decl))
2685 	{
2686 	  if (DECL_USE_TEMPLATE (decl)
2687 	      && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
2688 	    ;
2689 	  else
2690 	    permerror (input_location, "%qD is not a template", decl);
2691 	}
2692       else if (!is_overloaded_fn (decl))
2693 	permerror (input_location, "%qD is not a template", decl);
2694       else
2695 	{
2696 	  bool found = false;
2697 
2698 	  for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
2699 	       !found && iter; ++iter)
2700 	    {
2701 	      tree fn = *iter;
2702 	      if (TREE_CODE (fn) == TEMPLATE_DECL
2703 		  || TREE_CODE (fn) == TEMPLATE_ID_EXPR
2704 		  || (TREE_CODE (fn) == FUNCTION_DECL
2705 		      && DECL_USE_TEMPLATE (fn)
2706 		      && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (fn))))
2707 		found = true;
2708 	    }
2709 	  if (!found)
2710 	    permerror (input_location, "%qD is not a template", decl);
2711 	}
2712     }
2713 }
2714 
2715 /* Record that an access failure occurred on BASETYPE_PATH attempting
2716    to access FIELD_DECL.  */
2717 
2718 void
2719 access_failure_info::record_access_failure (tree basetype_path,
2720 					    tree field_decl)
2721 {
2722   m_was_inaccessible = true;
2723   m_basetype_path = basetype_path;
2724   m_field_decl = field_decl;
2725 }
2726 
2727 /* If an access failure was recorded, then attempt to locate an
2728    accessor function for the pertinent field, and if one is
2729    available, add a note and fix-it hint suggesting using it.  */
2730 
2731 void
2732 access_failure_info::maybe_suggest_accessor (bool const_p) const
2733 {
2734   if (!m_was_inaccessible)
2735     return;
2736 
2737   tree accessor
2738     = locate_field_accessor (m_basetype_path, m_field_decl, const_p);
2739   if (!accessor)
2740     return;
2741 
2742   /* The accessor must itself be accessible for it to be a reasonable
2743      suggestion.  */
2744   if (!accessible_p (m_basetype_path, accessor, true))
2745     return;
2746 
2747   rich_location richloc (line_table, input_location);
2748   pretty_printer pp;
2749   pp_printf (&pp, "%s()", IDENTIFIER_POINTER (DECL_NAME (accessor)));
2750   richloc.add_fixit_replace (pp_formatted_text (&pp));
2751   inform (&richloc, "field %q#D can be accessed via %q#D",
2752 	  m_field_decl, accessor);
2753 }
2754 
2755 /* This function is called by the parser to process a class member
2756    access expression of the form OBJECT.NAME.  NAME is a node used by
2757    the parser to represent a name; it is not yet a DECL.  It may,
2758    however, be a BASELINK where the BASELINK_FUNCTIONS is a
2759    TEMPLATE_ID_EXPR.  Templates must be looked up by the parser, and
2760    there is no reason to do the lookup twice, so the parser keeps the
2761    BASELINK.  TEMPLATE_P is true iff NAME was explicitly declared to
2762    be a template via the use of the "A::template B" syntax.  */
2763 
2764 tree
2765 finish_class_member_access_expr (cp_expr object, tree name, bool template_p,
2766 				 tsubst_flags_t complain)
2767 {
2768   tree expr;
2769   tree object_type;
2770   tree member;
2771   tree access_path = NULL_TREE;
2772   tree orig_object = object;
2773   tree orig_name = name;
2774 
2775   if (object == error_mark_node || name == error_mark_node)
2776     return error_mark_node;
2777 
2778   /* If OBJECT is an ObjC class instance, we must obey ObjC access rules.  */
2779   if (!objc_is_public (object, name))
2780     return error_mark_node;
2781 
2782   object_type = TREE_TYPE (object);
2783 
2784   if (processing_template_decl)
2785     {
2786       if (/* If OBJECT is dependent, so is OBJECT.NAME.  */
2787 	  type_dependent_object_expression_p (object)
2788 	  /* If NAME is "f<args>", where either 'f' or 'args' is
2789 	     dependent, then the expression is dependent.  */
2790 	  || (TREE_CODE (name) == TEMPLATE_ID_EXPR
2791 	      && dependent_template_id_p (TREE_OPERAND (name, 0),
2792 					  TREE_OPERAND (name, 1)))
2793 	  /* If NAME is "T::X" where "T" is dependent, then the
2794 	     expression is dependent.  */
2795 	  || (TREE_CODE (name) == SCOPE_REF
2796 	      && TYPE_P (TREE_OPERAND (name, 0))
2797 	      && dependent_scope_p (TREE_OPERAND (name, 0)))
2798 	  /* If NAME is operator T where "T" is dependent, we can't
2799 	     lookup until we instantiate the T.  */
2800 	  || (TREE_CODE (name) == IDENTIFIER_NODE
2801 	      && IDENTIFIER_CONV_OP_P (name)
2802 	      && dependent_type_p (TREE_TYPE (name))))
2803 	{
2804 	dependent:
2805 	  return build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF,
2806 				   orig_object, orig_name, NULL_TREE);
2807 	}
2808       object = build_non_dependent_expr (object);
2809     }
2810   else if (c_dialect_objc ()
2811 	   && identifier_p (name)
2812 	   && (expr = objc_maybe_build_component_ref (object, name)))
2813     return expr;
2814 
2815   /* [expr.ref]
2816 
2817      The type of the first expression shall be "class object" (of a
2818      complete type).  */
2819   if (!currently_open_class (object_type)
2820       && !complete_type_or_maybe_complain (object_type, object, complain))
2821     return error_mark_node;
2822   if (!CLASS_TYPE_P (object_type))
2823     {
2824       if (complain & tf_error)
2825 	{
2826 	  if (POINTER_TYPE_P (object_type)
2827 	      && CLASS_TYPE_P (TREE_TYPE (object_type)))
2828 	    error ("request for member %qD in %qE, which is of pointer "
2829 		   "type %qT (maybe you meant to use %<->%> ?)",
2830 		   name, object.get_value (), object_type);
2831 	  else
2832 	    error ("request for member %qD in %qE, which is of non-class "
2833 		   "type %qT", name, object.get_value (), object_type);
2834 	}
2835       return error_mark_node;
2836     }
2837 
2838   if (BASELINK_P (name))
2839     /* A member function that has already been looked up.  */
2840     member = name;
2841   else
2842     {
2843       bool is_template_id = false;
2844       tree template_args = NULL_TREE;
2845       tree scope = NULL_TREE;
2846 
2847       access_path = object_type;
2848 
2849       if (TREE_CODE (name) == SCOPE_REF)
2850 	{
2851 	  /* A qualified name.  The qualifying class or namespace `S'
2852 	     has already been looked up; it is either a TYPE or a
2853 	     NAMESPACE_DECL.  */
2854 	  scope = TREE_OPERAND (name, 0);
2855 	  name = TREE_OPERAND (name, 1);
2856 
2857 	  /* If SCOPE is a namespace, then the qualified name does not
2858 	     name a member of OBJECT_TYPE.  */
2859 	  if (TREE_CODE (scope) == NAMESPACE_DECL)
2860 	    {
2861 	      if (complain & tf_error)
2862 		error ("%<%D::%D%> is not a member of %qT",
2863 		       scope, name, object_type);
2864 	      return error_mark_node;
2865 	    }
2866 	}
2867 
2868       if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
2869 	{
2870 	  is_template_id = true;
2871 	  template_args = TREE_OPERAND (name, 1);
2872 	  name = TREE_OPERAND (name, 0);
2873 
2874 	  if (!identifier_p (name))
2875 	    name = OVL_NAME (name);
2876 	}
2877 
2878       if (scope)
2879 	{
2880 	  if (TREE_CODE (scope) == ENUMERAL_TYPE)
2881 	    {
2882 	      gcc_assert (!is_template_id);
2883 	      /* Looking up a member enumerator (c++/56793).  */
2884 	      if (!TYPE_CLASS_SCOPE_P (scope)
2885 		  || !DERIVED_FROM_P (TYPE_CONTEXT (scope), object_type))
2886 		{
2887 		  if (complain & tf_error)
2888 		    error ("%<%D::%D%> is not a member of %qT",
2889 			   scope, name, object_type);
2890 		  return error_mark_node;
2891 		}
2892 	      tree val = lookup_enumerator (scope, name);
2893 	      if (!val)
2894 		{
2895 		  if (complain & tf_error)
2896 		    error ("%qD is not a member of %qD",
2897 			   name, scope);
2898 		  return error_mark_node;
2899 		}
2900 
2901 	      if (TREE_SIDE_EFFECTS (object))
2902 		val = build2 (COMPOUND_EXPR, TREE_TYPE (val), object, val);
2903 	      return val;
2904 	    }
2905 
2906 	  gcc_assert (CLASS_TYPE_P (scope));
2907 	  gcc_assert (identifier_p (name) || TREE_CODE (name) == BIT_NOT_EXPR);
2908 
2909 	  if (constructor_name_p (name, scope))
2910 	    {
2911 	      if (complain & tf_error)
2912 		error ("cannot call constructor %<%T::%D%> directly",
2913 		       scope, name);
2914 	      return error_mark_node;
2915 	    }
2916 
2917 	  /* Find the base of OBJECT_TYPE corresponding to SCOPE.  */
2918 	  access_path = lookup_base (object_type, scope, ba_check,
2919 				     NULL, complain);
2920 	  if (access_path == error_mark_node)
2921 	    return error_mark_node;
2922 	  if (!access_path)
2923 	    {
2924 	      if (any_dependent_bases_p (object_type))
2925 		goto dependent;
2926 	      if (complain & tf_error)
2927 		error ("%qT is not a base of %qT", scope, object_type);
2928 	      return error_mark_node;
2929 	    }
2930 	}
2931 
2932       if (TREE_CODE (name) == BIT_NOT_EXPR)
2933 	{
2934 	  if (dependent_type_p (object_type))
2935 	    /* The destructor isn't declared yet.  */
2936 	    goto dependent;
2937 	  member = lookup_destructor (object, scope, name, complain);
2938 	}
2939       else
2940 	{
2941 	  /* Look up the member.  */
2942 	  access_failure_info afi;
2943 	  member = lookup_member (access_path, name, /*protect=*/1,
2944 				  /*want_type=*/false, complain,
2945 				  &afi);
2946 	  afi.maybe_suggest_accessor (TYPE_READONLY (object_type));
2947 	  if (member == NULL_TREE)
2948 	    {
2949 	      if (dependent_type_p (object_type))
2950 		/* Try again at instantiation time.  */
2951 		goto dependent;
2952 	      if (complain & tf_error)
2953 		{
2954 		  tree guessed_id = lookup_member_fuzzy (access_path, name,
2955 							 /*want_type=*/false);
2956 		  if (guessed_id)
2957 		    {
2958 		      location_t bogus_component_loc = input_location;
2959 		      gcc_rich_location rich_loc (bogus_component_loc);
2960 		      rich_loc.add_fixit_misspelled_id (bogus_component_loc,
2961 							guessed_id);
2962 		      error_at (&rich_loc,
2963 				"%q#T has no member named %qE;"
2964 				" did you mean %qE?",
2965 				TREE_CODE (access_path) == TREE_BINFO
2966 				? TREE_TYPE (access_path) : object_type,
2967 				name, guessed_id);
2968 		    }
2969 		  else
2970 		    error ("%q#T has no member named %qE",
2971 			   TREE_CODE (access_path) == TREE_BINFO
2972 			   ? TREE_TYPE (access_path) : object_type, name);
2973 		}
2974 	      return error_mark_node;
2975 	    }
2976 	  if (member == error_mark_node)
2977 	    return error_mark_node;
2978 	  if (DECL_P (member)
2979 	      && any_dependent_type_attributes_p (DECL_ATTRIBUTES (member)))
2980 	    /* Dependent type attributes on the decl mean that the TREE_TYPE is
2981 	       wrong, so don't use it.  */
2982 	    goto dependent;
2983 	  if (TREE_CODE (member) == USING_DECL && DECL_DEPENDENT_P (member))
2984 	    goto dependent;
2985 	}
2986 
2987       if (is_template_id)
2988 	{
2989 	  tree templ = member;
2990 
2991 	  if (BASELINK_P (templ))
2992 	    member = lookup_template_function (templ, template_args);
2993 	  else if (variable_template_p (templ))
2994 	    member = (lookup_and_finish_template_variable
2995 		      (templ, template_args, complain));
2996 	  else
2997 	    {
2998 	      if (complain & tf_error)
2999 		error ("%qD is not a member template function", name);
3000 	      return error_mark_node;
3001 	    }
3002 	}
3003     }
3004 
3005   if (TREE_DEPRECATED (member))
3006     warn_deprecated_use (member, NULL_TREE);
3007 
3008   if (template_p)
3009     check_template_keyword (member);
3010 
3011   expr = build_class_member_access_expr (object, member, access_path,
3012 					 /*preserve_reference=*/false,
3013 					 complain);
3014   if (processing_template_decl && expr != error_mark_node)
3015     {
3016       if (BASELINK_P (member))
3017 	{
3018 	  if (TREE_CODE (orig_name) == SCOPE_REF)
3019 	    BASELINK_QUALIFIED_P (member) = 1;
3020 	  orig_name = member;
3021 	}
3022       return build_min_non_dep (COMPONENT_REF, expr,
3023 				orig_object, orig_name,
3024 				NULL_TREE);
3025     }
3026 
3027   return expr;
3028 }
3029 
3030 /* Build a COMPONENT_REF of OBJECT and MEMBER with the appropriate
3031    type.  */
3032 
3033 tree
3034 build_simple_component_ref (tree object, tree member)
3035 {
3036   tree type = cp_build_qualified_type (TREE_TYPE (member),
3037 				       cp_type_quals (TREE_TYPE (object)));
3038   return build3_loc (input_location,
3039 		     COMPONENT_REF, type,
3040 		     object, member, NULL_TREE);
3041 }
3042 
3043 /* Return an expression for the MEMBER_NAME field in the internal
3044    representation of PTRMEM, a pointer-to-member function.  (Each
3045    pointer-to-member function type gets its own RECORD_TYPE so it is
3046    more convenient to access the fields by name than by FIELD_DECL.)
3047    This routine converts the NAME to a FIELD_DECL and then creates the
3048    node for the complete expression.  */
3049 
3050 tree
3051 build_ptrmemfunc_access_expr (tree ptrmem, tree member_name)
3052 {
3053   tree ptrmem_type;
3054   tree member;
3055 
3056   if (TREE_CODE (ptrmem) == CONSTRUCTOR)
3057     {
3058       unsigned int ix;
3059       tree index, value;
3060       FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ptrmem),
3061 				ix, index, value)
3062 	if (index && DECL_P (index) && DECL_NAME (index) == member_name)
3063 	  return value;
3064       gcc_unreachable ();
3065     }
3066 
3067   /* This code is a stripped down version of
3068      build_class_member_access_expr.  It does not work to use that
3069      routine directly because it expects the object to be of class
3070      type.  */
3071   ptrmem_type = TREE_TYPE (ptrmem);
3072   gcc_assert (TYPE_PTRMEMFUNC_P (ptrmem_type));
3073   for (member = TYPE_FIELDS (ptrmem_type); member;
3074        member = DECL_CHAIN (member))
3075     if (DECL_NAME (member) == member_name)
3076       break;
3077   tree res = build_simple_component_ref (ptrmem, member);
3078 
3079   TREE_NO_WARNING (res) = 1;
3080   return res;
3081 }
3082 
3083 /* Given an expression PTR for a pointer, return an expression
3084    for the value pointed to.
3085    ERRORSTRING is the name of the operator to appear in error messages.
3086 
3087    This function may need to overload OPERATOR_FNNAME.
3088    Must also handle REFERENCE_TYPEs for C++.  */
3089 
3090 tree
3091 build_x_indirect_ref (location_t loc, tree expr, ref_operator errorstring,
3092                       tsubst_flags_t complain)
3093 {
3094   tree orig_expr = expr;
3095   tree rval;
3096   tree overload = NULL_TREE;
3097 
3098   if (processing_template_decl)
3099     {
3100       /* Retain the type if we know the operand is a pointer.  */
3101       if (TREE_TYPE (expr) && POINTER_TYPE_P (TREE_TYPE (expr)))
3102 	return build_min (INDIRECT_REF, TREE_TYPE (TREE_TYPE (expr)), expr);
3103       if (type_dependent_expression_p (expr))
3104 	return build_min_nt_loc (loc, INDIRECT_REF, expr);
3105       expr = build_non_dependent_expr (expr);
3106     }
3107 
3108   rval = build_new_op (loc, INDIRECT_REF, LOOKUP_NORMAL, expr,
3109 		       NULL_TREE, NULL_TREE, &overload, complain);
3110   if (!rval)
3111     rval = cp_build_indirect_ref (expr, errorstring, complain);
3112 
3113   if (processing_template_decl && rval != error_mark_node)
3114     {
3115       if (overload != NULL_TREE)
3116 	return (build_min_non_dep_op_overload
3117 		(INDIRECT_REF, rval, overload, orig_expr));
3118 
3119       return build_min_non_dep (INDIRECT_REF, rval, orig_expr);
3120     }
3121   else
3122     return rval;
3123 }
3124 
3125 /* The implementation of the above, and of indirection implied by other
3126    constructs.  If DO_FOLD is true, fold away INDIRECT_REF of ADDR_EXPR.  */
3127 
3128 static tree
3129 cp_build_indirect_ref_1 (tree ptr, ref_operator errorstring,
3130 			 tsubst_flags_t complain, bool do_fold)
3131 {
3132   tree pointer, type;
3133 
3134   /* RO_NULL should only be used with the folding entry points below, not
3135      cp_build_indirect_ref.  */
3136   gcc_checking_assert (errorstring != RO_NULL || do_fold);
3137 
3138   if (ptr == current_class_ptr
3139       || (TREE_CODE (ptr) == NOP_EXPR
3140 	  && TREE_OPERAND (ptr, 0) == current_class_ptr
3141 	  && (same_type_ignoring_top_level_qualifiers_p
3142 	      (TREE_TYPE (ptr), TREE_TYPE (current_class_ptr)))))
3143     return current_class_ref;
3144 
3145   pointer = (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
3146 	     ? ptr : decay_conversion (ptr, complain));
3147   if (pointer == error_mark_node)
3148     return error_mark_node;
3149 
3150   type = TREE_TYPE (pointer);
3151 
3152   if (POINTER_TYPE_P (type))
3153     {
3154       /* [expr.unary.op]
3155 
3156 	 If the type of the expression is "pointer to T," the type
3157 	 of  the  result  is  "T."  */
3158       tree t = TREE_TYPE (type);
3159 
3160       if ((CONVERT_EXPR_P (ptr)
3161 	   || TREE_CODE (ptr) == VIEW_CONVERT_EXPR)
3162 	  && (!CLASS_TYPE_P (t) || !CLASSTYPE_EMPTY_P (t)))
3163 	{
3164 	  /* If a warning is issued, mark it to avoid duplicates from
3165 	     the backend.  This only needs to be done at
3166 	     warn_strict_aliasing > 2.  */
3167 	  if (warn_strict_aliasing > 2)
3168 	    if (strict_aliasing_warning (EXPR_LOCATION (ptr),
3169 					 type, TREE_OPERAND (ptr, 0)))
3170 	      TREE_NO_WARNING (ptr) = 1;
3171 	}
3172 
3173       if (VOID_TYPE_P (t))
3174 	{
3175 	  /* A pointer to incomplete type (other than cv void) can be
3176 	     dereferenced [expr.unary.op]/1  */
3177           if (complain & tf_error)
3178             error ("%qT is not a pointer-to-object type", type);
3179 	  return error_mark_node;
3180 	}
3181       else if (do_fold && TREE_CODE (pointer) == ADDR_EXPR
3182 	       && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
3183 	/* The POINTER was something like `&x'.  We simplify `*&x' to
3184 	   `x'.  */
3185 	return TREE_OPERAND (pointer, 0);
3186       else
3187 	{
3188 	  tree ref = build1 (INDIRECT_REF, t, pointer);
3189 
3190 	  /* We *must* set TREE_READONLY when dereferencing a pointer to const,
3191 	     so that we get the proper error message if the result is used
3192 	     to assign to.  Also, &* is supposed to be a no-op.  */
3193 	  TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
3194 	  TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
3195 	  TREE_SIDE_EFFECTS (ref)
3196 	    = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer));
3197 	  return ref;
3198 	}
3199     }
3200   else if (!(complain & tf_error))
3201     /* Don't emit any errors; we'll just return ERROR_MARK_NODE later.  */
3202     ;
3203   /* `pointer' won't be an error_mark_node if we were given a
3204      pointer to member, so it's cool to check for this here.  */
3205   else if (TYPE_PTRMEM_P (type))
3206     switch (errorstring)
3207       {
3208          case RO_ARRAY_INDEXING:
3209            error ("invalid use of array indexing on pointer to member");
3210            break;
3211          case RO_UNARY_STAR:
3212            error ("invalid use of unary %<*%> on pointer to member");
3213            break;
3214          case RO_IMPLICIT_CONVERSION:
3215            error ("invalid use of implicit conversion on pointer to member");
3216            break;
3217          case RO_ARROW_STAR:
3218            error ("left hand operand of %<->*%> must be a pointer to class, "
3219 		  "but is a pointer to member of type %qT", type);
3220            break;
3221          default:
3222            gcc_unreachable ();
3223       }
3224   else if (pointer != error_mark_node)
3225     invalid_indirection_error (input_location, type, errorstring);
3226 
3227   return error_mark_node;
3228 }
3229 
3230 /* Entry point used by c-common, which expects folding.  */
3231 
3232 tree
3233 build_indirect_ref (location_t /*loc*/,
3234 		    tree ptr, ref_operator errorstring)
3235 {
3236   return cp_build_indirect_ref_1 (ptr, errorstring, tf_warning_or_error, true);
3237 }
3238 
3239 /* Entry point used by internal indirection needs that don't correspond to any
3240    syntactic construct.  */
3241 
3242 tree
3243 cp_build_fold_indirect_ref (tree pointer)
3244 {
3245   return cp_build_indirect_ref_1 (pointer, RO_NULL, tf_warning_or_error, true);
3246 }
3247 
3248 /* Entry point used by indirection needs that correspond to some syntactic
3249    construct.  */
3250 
3251 tree
3252 cp_build_indirect_ref (tree ptr, ref_operator errorstring,
3253 		       tsubst_flags_t complain)
3254 {
3255   return cp_build_indirect_ref_1 (ptr, errorstring, complain, false);
3256 }
3257 
3258 /* This handles expressions of the form "a[i]", which denotes
3259    an array reference.
3260 
3261    This is logically equivalent in C to *(a+i), but we may do it differently.
3262    If A is a variable or a member, we generate a primitive ARRAY_REF.
3263    This avoids forcing the array out of registers, and can work on
3264    arrays that are not lvalues (for example, members of structures returned
3265    by functions).
3266 
3267    If INDEX is of some user-defined type, it must be converted to
3268    integer type.  Otherwise, to make a compatible PLUS_EXPR, it
3269    will inherit the type of the array, which will be some pointer type.
3270 
3271    LOC is the location to use in building the array reference.  */
3272 
3273 tree
3274 cp_build_array_ref (location_t loc, tree array, tree idx,
3275 		    tsubst_flags_t complain)
3276 {
3277   tree ret;
3278 
3279   if (idx == 0)
3280     {
3281       if (complain & tf_error)
3282 	error_at (loc, "subscript missing in array reference");
3283       return error_mark_node;
3284     }
3285 
3286   if (TREE_TYPE (array) == error_mark_node
3287       || TREE_TYPE (idx) == error_mark_node)
3288     return error_mark_node;
3289 
3290   /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
3291      inside it.  */
3292   switch (TREE_CODE (array))
3293     {
3294     case COMPOUND_EXPR:
3295       {
3296 	tree value = cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
3297 					 complain);
3298 	ret = build2 (COMPOUND_EXPR, TREE_TYPE (value),
3299 		      TREE_OPERAND (array, 0), value);
3300 	SET_EXPR_LOCATION (ret, loc);
3301 	return ret;
3302       }
3303 
3304     case COND_EXPR:
3305       ret = build_conditional_expr
3306 	       (loc, TREE_OPERAND (array, 0),
3307 	       cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
3308 				   complain),
3309 	       cp_build_array_ref (loc, TREE_OPERAND (array, 2), idx,
3310 				   complain),
3311 	       complain);
3312       protected_set_expr_location (ret, loc);
3313       return ret;
3314 
3315     default:
3316       break;
3317     }
3318 
3319   bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, idx);
3320 
3321   if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
3322     {
3323       tree rval, type;
3324 
3325       warn_array_subscript_with_type_char (loc, idx);
3326 
3327       if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (idx)))
3328 	{
3329 	  if (complain & tf_error)
3330 	    error_at (loc, "array subscript is not an integer");
3331 	  return error_mark_node;
3332 	}
3333 
3334       /* Apply integral promotions *after* noticing character types.
3335 	 (It is unclear why we do these promotions -- the standard
3336 	 does not say that we should.  In fact, the natural thing would
3337 	 seem to be to convert IDX to ptrdiff_t; we're performing
3338 	 pointer arithmetic.)  */
3339       idx = cp_perform_integral_promotions (idx, complain);
3340 
3341       /* An array that is indexed by a non-constant
3342 	 cannot be stored in a register; we must be able to do
3343 	 address arithmetic on its address.
3344 	 Likewise an array of elements of variable size.  */
3345       if (TREE_CODE (idx) != INTEGER_CST
3346 	  || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
3347 	      && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
3348 		  != INTEGER_CST)))
3349 	{
3350 	  if (!cxx_mark_addressable (array, true))
3351 	    return error_mark_node;
3352 	}
3353 
3354       /* An array that is indexed by a constant value which is not within
3355 	 the array bounds cannot be stored in a register either; because we
3356 	 would get a crash in store_bit_field/extract_bit_field when trying
3357 	 to access a non-existent part of the register.  */
3358       if (TREE_CODE (idx) == INTEGER_CST
3359 	  && TYPE_DOMAIN (TREE_TYPE (array))
3360 	  && ! int_fits_type_p (idx, TYPE_DOMAIN (TREE_TYPE (array))))
3361 	{
3362 	  if (!cxx_mark_addressable (array))
3363 	    return error_mark_node;
3364 	}
3365 
3366       /* Note in C++ it is valid to subscript a `register' array, since
3367 	 it is valid to take the address of something with that
3368 	 storage specification.  */
3369       if (extra_warnings)
3370 	{
3371 	  tree foo = array;
3372 	  while (TREE_CODE (foo) == COMPONENT_REF)
3373 	    foo = TREE_OPERAND (foo, 0);
3374 	  if (VAR_P (foo) && DECL_REGISTER (foo)
3375 	      && (complain & tf_warning))
3376 	    warning_at (loc, OPT_Wextra,
3377 			"subscripting array declared %<register%>");
3378 	}
3379 
3380       type = TREE_TYPE (TREE_TYPE (array));
3381       rval = build4 (ARRAY_REF, type, array, idx, NULL_TREE, NULL_TREE);
3382       /* Array ref is const/volatile if the array elements are
3383 	 or if the array is..  */
3384       TREE_READONLY (rval)
3385 	|= (CP_TYPE_CONST_P (type) | TREE_READONLY (array));
3386       TREE_SIDE_EFFECTS (rval)
3387 	|= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array));
3388       TREE_THIS_VOLATILE (rval)
3389 	|= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array));
3390       ret = require_complete_type_sfinae (rval, complain);
3391       protected_set_expr_location (ret, loc);
3392       if (non_lvalue)
3393 	ret = non_lvalue_loc (loc, ret);
3394       return ret;
3395     }
3396 
3397   {
3398     tree ar = cp_default_conversion (array, complain);
3399     tree ind = cp_default_conversion (idx, complain);
3400 
3401     /* Put the integer in IND to simplify error checking.  */
3402     if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
3403       std::swap (ar, ind);
3404 
3405     if (ar == error_mark_node || ind == error_mark_node)
3406       return error_mark_node;
3407 
3408     if (!TYPE_PTR_P (TREE_TYPE (ar)))
3409       {
3410 	if (complain & tf_error)
3411 	  error_at (loc, "subscripted value is neither array nor pointer");
3412 	return error_mark_node;
3413       }
3414     if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
3415       {
3416 	if (complain & tf_error)
3417 	  error_at (loc, "array subscript is not an integer");
3418 	return error_mark_node;
3419       }
3420 
3421     warn_array_subscript_with_type_char (loc, idx);
3422 
3423     ret = cp_build_indirect_ref (cp_build_binary_op (input_location,
3424 						     PLUS_EXPR, ar, ind,
3425 						     complain),
3426                                  RO_ARRAY_INDEXING,
3427                                  complain);
3428     protected_set_expr_location (ret, loc);
3429     if (non_lvalue)
3430       ret = non_lvalue_loc (loc, ret);
3431     return ret;
3432   }
3433 }
3434 
3435 /* Entry point for Obj-C++.  */
3436 
3437 tree
3438 build_array_ref (location_t loc, tree array, tree idx)
3439 {
3440   return cp_build_array_ref (loc, array, idx, tf_warning_or_error);
3441 }
3442 
3443 /* Resolve a pointer to member function.  INSTANCE is the object
3444    instance to use, if the member points to a virtual member.
3445 
3446    This used to avoid checking for virtual functions if basetype
3447    has no virtual functions, according to an earlier ANSI draft.
3448    With the final ISO C++ rules, such an optimization is
3449    incorrect: A pointer to a derived member can be static_cast
3450    to pointer-to-base-member, as long as the dynamic object
3451    later has the right member.  So now we only do this optimization
3452    when we know the dynamic type of the object.  */
3453 
3454 tree
3455 get_member_function_from_ptrfunc (tree *instance_ptrptr, tree function,
3456 				  tsubst_flags_t complain)
3457 {
3458   if (TREE_CODE (function) == OFFSET_REF)
3459     function = TREE_OPERAND (function, 1);
3460 
3461   if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
3462     {
3463       tree idx, delta, e1, e2, e3, vtbl;
3464       bool nonvirtual;
3465       tree fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
3466       tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
3467 
3468       tree instance_ptr = *instance_ptrptr;
3469       tree instance_save_expr = 0;
3470       if (instance_ptr == error_mark_node)
3471 	{
3472 	  if (TREE_CODE (function) == PTRMEM_CST)
3473 	    {
3474 	      /* Extracting the function address from a pmf is only
3475 		 allowed with -Wno-pmf-conversions. It only works for
3476 		 pmf constants.  */
3477 	      e1 = build_addr_func (PTRMEM_CST_MEMBER (function), complain);
3478 	      e1 = convert (fntype, e1);
3479 	      return e1;
3480 	    }
3481 	  else
3482 	    {
3483 	      if (complain & tf_error)
3484 		error ("object missing in use of %qE", function);
3485 	      return error_mark_node;
3486 	    }
3487 	}
3488 
3489       /* True if we know that the dynamic type of the object doesn't have
3490 	 virtual functions, so we can assume the PFN field is a pointer.  */
3491       nonvirtual = (COMPLETE_TYPE_P (basetype)
3492 		    && !TYPE_POLYMORPHIC_P (basetype)
3493 		    && resolves_to_fixed_type_p (instance_ptr, 0));
3494 
3495       /* If we don't really have an object (i.e. in an ill-formed
3496 	 conversion from PMF to pointer), we can't resolve virtual
3497 	 functions anyway.  */
3498       if (!nonvirtual && is_dummy_object (instance_ptr))
3499 	nonvirtual = true;
3500 
3501       if (TREE_SIDE_EFFECTS (instance_ptr))
3502 	instance_ptr = instance_save_expr = save_expr (instance_ptr);
3503 
3504       if (TREE_SIDE_EFFECTS (function))
3505 	function = save_expr (function);
3506 
3507       /* Start by extracting all the information from the PMF itself.  */
3508       e3 = pfn_from_ptrmemfunc (function);
3509       delta = delta_from_ptrmemfunc (function);
3510       idx = build1 (NOP_EXPR, vtable_index_type, e3);
3511       switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
3512 	{
3513 	  int flag_sanitize_save;
3514 	case ptrmemfunc_vbit_in_pfn:
3515 	  e1 = cp_build_binary_op (input_location,
3516 				   BIT_AND_EXPR, idx, integer_one_node,
3517 				   complain);
3518 	  idx = cp_build_binary_op (input_location,
3519 				    MINUS_EXPR, idx, integer_one_node,
3520 				    complain);
3521 	  if (idx == error_mark_node)
3522 	    return error_mark_node;
3523 	  break;
3524 
3525 	case ptrmemfunc_vbit_in_delta:
3526 	  e1 = cp_build_binary_op (input_location,
3527 				   BIT_AND_EXPR, delta, integer_one_node,
3528 				   complain);
3529 	  /* Don't instrument the RSHIFT_EXPR we're about to create because
3530 	     we're going to use DELTA number of times, and that wouldn't play
3531 	     well with SAVE_EXPRs therein.  */
3532 	  flag_sanitize_save = flag_sanitize;
3533 	  flag_sanitize = 0;
3534 	  delta = cp_build_binary_op (input_location,
3535 				      RSHIFT_EXPR, delta, integer_one_node,
3536 				      complain);
3537 	  flag_sanitize = flag_sanitize_save;
3538 	  if (delta == error_mark_node)
3539 	    return error_mark_node;
3540 	  break;
3541 
3542 	default:
3543 	  gcc_unreachable ();
3544 	}
3545 
3546       if (e1 == error_mark_node)
3547 	return error_mark_node;
3548 
3549       /* Convert down to the right base before using the instance.  A
3550 	 special case is that in a pointer to member of class C, C may
3551 	 be incomplete.  In that case, the function will of course be
3552 	 a member of C, and no conversion is required.  In fact,
3553 	 lookup_base will fail in that case, because incomplete
3554 	 classes do not have BINFOs.  */
3555       if (!same_type_ignoring_top_level_qualifiers_p
3556 	  (basetype, TREE_TYPE (TREE_TYPE (instance_ptr))))
3557 	{
3558 	  basetype = lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr)),
3559 				  basetype, ba_check, NULL, complain);
3560 	  instance_ptr = build_base_path (PLUS_EXPR, instance_ptr, basetype,
3561 					  1, complain);
3562 	  if (instance_ptr == error_mark_node)
3563 	    return error_mark_node;
3564 	}
3565       /* ...and then the delta in the PMF.  */
3566       instance_ptr = fold_build_pointer_plus (instance_ptr, delta);
3567 
3568       /* Hand back the adjusted 'this' argument to our caller.  */
3569       *instance_ptrptr = instance_ptr;
3570 
3571       if (nonvirtual)
3572 	/* Now just return the pointer.  */
3573 	return e3;
3574 
3575       /* Next extract the vtable pointer from the object.  */
3576       vtbl = build1 (NOP_EXPR, build_pointer_type (vtbl_ptr_type_node),
3577 		     instance_ptr);
3578       vtbl = cp_build_fold_indirect_ref (vtbl);
3579       if (vtbl == error_mark_node)
3580 	return error_mark_node;
3581 
3582       /* Finally, extract the function pointer from the vtable.  */
3583       e2 = fold_build_pointer_plus_loc (input_location, vtbl, idx);
3584       e2 = cp_build_fold_indirect_ref (e2);
3585       if (e2 == error_mark_node)
3586 	return error_mark_node;
3587       TREE_CONSTANT (e2) = 1;
3588 
3589       /* When using function descriptors, the address of the
3590 	 vtable entry is treated as a function pointer.  */
3591       if (TARGET_VTABLE_USES_DESCRIPTORS)
3592 	e2 = build1 (NOP_EXPR, TREE_TYPE (e2),
3593 		     cp_build_addr_expr (e2, complain));
3594 
3595       e2 = fold_convert (TREE_TYPE (e3), e2);
3596       e1 = build_conditional_expr (input_location, e1, e2, e3, complain);
3597       if (e1 == error_mark_node)
3598 	return error_mark_node;
3599 
3600       /* Make sure this doesn't get evaluated first inside one of the
3601 	 branches of the COND_EXPR.  */
3602       if (instance_save_expr)
3603 	e1 = build2 (COMPOUND_EXPR, TREE_TYPE (e1),
3604 		     instance_save_expr, e1);
3605 
3606       function = e1;
3607     }
3608   return function;
3609 }
3610 
3611 /* Used by the C-common bits.  */
3612 tree
3613 build_function_call (location_t /*loc*/,
3614 		     tree function, tree params)
3615 {
3616   return cp_build_function_call (function, params, tf_warning_or_error);
3617 }
3618 
3619 /* Used by the C-common bits.  */
3620 tree
3621 build_function_call_vec (location_t /*loc*/, vec<location_t> /*arg_loc*/,
3622 			 tree function, vec<tree, va_gc> *params,
3623 			 vec<tree, va_gc> * /*origtypes*/)
3624 {
3625   vec<tree, va_gc> *orig_params = params;
3626   tree ret = cp_build_function_call_vec (function, &params,
3627 					 tf_warning_or_error);
3628 
3629   /* cp_build_function_call_vec can reallocate PARAMS by adding
3630      default arguments.  That should never happen here.  Verify
3631      that.  */
3632   gcc_assert (params == orig_params);
3633 
3634   return ret;
3635 }
3636 
3637 /* Build a function call using a tree list of arguments.  */
3638 
3639 static tree
3640 cp_build_function_call (tree function, tree params, tsubst_flags_t complain)
3641 {
3642   vec<tree, va_gc> *vec;
3643   tree ret;
3644 
3645   vec = make_tree_vector ();
3646   for (; params != NULL_TREE; params = TREE_CHAIN (params))
3647     vec_safe_push (vec, TREE_VALUE (params));
3648   ret = cp_build_function_call_vec (function, &vec, complain);
3649   release_tree_vector (vec);
3650   return ret;
3651 }
3652 
3653 /* Build a function call using varargs.  */
3654 
3655 tree
3656 cp_build_function_call_nary (tree function, tsubst_flags_t complain, ...)
3657 {
3658   vec<tree, va_gc> *vec;
3659   va_list args;
3660   tree ret, t;
3661 
3662   vec = make_tree_vector ();
3663   va_start (args, complain);
3664   for (t = va_arg (args, tree); t != NULL_TREE; t = va_arg (args, tree))
3665     vec_safe_push (vec, t);
3666   va_end (args);
3667   ret = cp_build_function_call_vec (function, &vec, complain);
3668   release_tree_vector (vec);
3669   return ret;
3670 }
3671 
3672 /* Build a function call using a vector of arguments.  PARAMS may be
3673    NULL if there are no parameters.  This changes the contents of
3674    PARAMS.  */
3675 
3676 tree
3677 cp_build_function_call_vec (tree function, vec<tree, va_gc> **params,
3678 			    tsubst_flags_t complain)
3679 {
3680   tree fntype, fndecl;
3681   int is_method;
3682   tree original = function;
3683   int nargs;
3684   tree *argarray;
3685   tree parm_types;
3686   vec<tree, va_gc> *allocated = NULL;
3687   tree ret;
3688 
3689   /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3690      expressions, like those used for ObjC messenger dispatches.  */
3691   if (params != NULL && !vec_safe_is_empty (*params))
3692     function = objc_rewrite_function_call (function, (**params)[0]);
3693 
3694   /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3695      Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context.  */
3696   if (TREE_CODE (function) == NOP_EXPR
3697       && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
3698     function = TREE_OPERAND (function, 0);
3699 
3700   if (TREE_CODE (function) == FUNCTION_DECL)
3701     {
3702       /* If the function is a non-template member function
3703          or a non-template friend, then we need to check the
3704          constraints.
3705 
3706         Note that if overload resolution failed with a single
3707         candidate this function will be used to explicitly diagnose
3708         the failure for the single call expression. The check is
3709         technically redundant since we also would have failed in
3710         add_function_candidate. */
3711       if (flag_concepts
3712           && (complain & tf_error)
3713           && !constraints_satisfied_p (function))
3714         {
3715           error ("cannot call function %qD", function);
3716           location_t loc = DECL_SOURCE_LOCATION (function);
3717           diagnose_constraints (loc, function, NULL_TREE);
3718           return error_mark_node;
3719         }
3720 
3721       if (!mark_used (function, complain) && !(complain & tf_error))
3722 	return error_mark_node;
3723       fndecl = function;
3724 
3725       /* Convert anything with function type to a pointer-to-function.  */
3726       if (DECL_MAIN_P (function))
3727 	{
3728 	  if (complain & tf_error)
3729 	    pedwarn (input_location, OPT_Wpedantic,
3730 		     "ISO C++ forbids calling %<::main%> from within program");
3731 	  else
3732 	    return error_mark_node;
3733 	}
3734       function = build_addr_func (function, complain);
3735     }
3736   else
3737     {
3738       fndecl = NULL_TREE;
3739 
3740       function = build_addr_func (function, complain);
3741     }
3742 
3743   if (function == error_mark_node)
3744     return error_mark_node;
3745 
3746   fntype = TREE_TYPE (function);
3747 
3748   if (TYPE_PTRMEMFUNC_P (fntype))
3749     {
3750       if (complain & tf_error)
3751 	error ("must use %<.*%> or %<->*%> to call pointer-to-member "
3752 	       "function in %<%E (...)%>, e.g. %<(... ->* %E) (...)%>",
3753 	       original, original);
3754       return error_mark_node;
3755     }
3756 
3757   is_method = (TYPE_PTR_P (fntype)
3758 	       && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
3759 
3760   if (!(TYPE_PTRFN_P (fntype)
3761 	|| is_method
3762 	|| TREE_CODE (function) == TEMPLATE_ID_EXPR))
3763     {
3764       if (complain & tf_error)
3765 	{
3766 	  if (!flag_diagnostics_show_caret)
3767 	    error_at (input_location,
3768 		      "%qE cannot be used as a function", original);
3769 	  else if (DECL_P (original))
3770 	    error_at (input_location,
3771 		      "%qD cannot be used as a function", original);
3772 	  else
3773 	    error_at (input_location,
3774 		      "expression cannot be used as a function");
3775 	}
3776 
3777       return error_mark_node;
3778     }
3779 
3780   /* fntype now gets the type of function pointed to.  */
3781   fntype = TREE_TYPE (fntype);
3782   parm_types = TYPE_ARG_TYPES (fntype);
3783 
3784   if (params == NULL)
3785     {
3786       allocated = make_tree_vector ();
3787       params = &allocated;
3788     }
3789 
3790     nargs = convert_arguments (parm_types, params, fndecl, LOOKUP_NORMAL,
3791 			       complain);
3792   if (nargs < 0)
3793     return error_mark_node;
3794 
3795   argarray = (*params)->address ();
3796 
3797   /* Check for errors in format strings and inappropriately
3798      null parameters.  */
3799   bool warned_p = check_function_arguments (input_location, fndecl, fntype,
3800 					    nargs, argarray, NULL);
3801 
3802   ret = build_cxx_call (function, nargs, argarray, complain);
3803 
3804   if (warned_p)
3805     {
3806       tree c = extract_call_expr (ret);
3807       if (TREE_CODE (c) == CALL_EXPR)
3808 	TREE_NO_WARNING (c) = 1;
3809     }
3810 
3811   if (allocated != NULL)
3812     release_tree_vector (allocated);
3813 
3814   return ret;
3815 }
3816 
3817 /* Subroutine of convert_arguments.
3818    Print an error message about a wrong number of arguments.  */
3819 
3820 static void
3821 error_args_num (location_t loc, tree fndecl, bool too_many_p)
3822 {
3823   if (fndecl)
3824     {
3825       if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
3826 	{
3827 	  if (DECL_NAME (fndecl) == NULL_TREE
3828 	      || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl)))
3829 	    error_at (loc,
3830 		      too_many_p
3831 		      ? G_("too many arguments to constructor %q#D")
3832 		      : G_("too few arguments to constructor %q#D"),
3833 		      fndecl);
3834 	  else
3835 	    error_at (loc,
3836 		      too_many_p
3837 		      ? G_("too many arguments to member function %q#D")
3838 		      : G_("too few arguments to member function %q#D"),
3839 		      fndecl);
3840 	}
3841       else
3842 	error_at (loc,
3843 		  too_many_p
3844 		  ? G_("too many arguments to function %q#D")
3845 		  : G_("too few arguments to function %q#D"),
3846 		  fndecl);
3847       if (!DECL_IS_BUILTIN (fndecl))
3848 	inform (DECL_SOURCE_LOCATION (fndecl), "declared here");
3849     }
3850   else
3851     {
3852       if (c_dialect_objc ()  &&  objc_message_selector ())
3853 	error_at (loc,
3854 		  too_many_p
3855 		  ? G_("too many arguments to method %q#D")
3856 		  : G_("too few arguments to method %q#D"),
3857 		  objc_message_selector ());
3858       else
3859 	error_at (loc, too_many_p ? G_("too many arguments to function")
3860 		                  : G_("too few arguments to function"));
3861     }
3862 }
3863 
3864 /* Convert the actual parameter expressions in the list VALUES to the
3865    types in the list TYPELIST.  The converted expressions are stored
3866    back in the VALUES vector.
3867    If parmdecls is exhausted, or when an element has NULL as its type,
3868    perform the default conversions.
3869 
3870    NAME is an IDENTIFIER_NODE or 0.  It is used only for error messages.
3871 
3872    This is also where warnings about wrong number of args are generated.
3873 
3874    Returns the actual number of arguments processed (which might be less
3875    than the length of the vector), or -1 on error.
3876 
3877    In C++, unspecified trailing parameters can be filled in with their
3878    default arguments, if such were specified.  Do so here.  */
3879 
3880 static int
3881 convert_arguments (tree typelist, vec<tree, va_gc> **values, tree fndecl,
3882 		   int flags, tsubst_flags_t complain)
3883 {
3884   tree typetail;
3885   unsigned int i;
3886 
3887   /* Argument passing is always copy-initialization.  */
3888   flags |= LOOKUP_ONLYCONVERTING;
3889 
3890   for (i = 0, typetail = typelist;
3891        i < vec_safe_length (*values);
3892        i++)
3893     {
3894       tree type = typetail ? TREE_VALUE (typetail) : 0;
3895       tree val = (**values)[i];
3896 
3897       if (val == error_mark_node || type == error_mark_node)
3898 	return -1;
3899 
3900       if (type == void_type_node)
3901 	{
3902           if (complain & tf_error)
3903             {
3904 	      error_args_num (input_location, fndecl, /*too_many_p=*/true);
3905               return i;
3906             }
3907           else
3908             return -1;
3909 	}
3910 
3911       /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3912 	 Strip such NOP_EXPRs, since VAL is used in non-lvalue context.  */
3913       if (TREE_CODE (val) == NOP_EXPR
3914 	  && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
3915 	  && (type == 0 || TREE_CODE (type) != REFERENCE_TYPE))
3916 	val = TREE_OPERAND (val, 0);
3917 
3918       if (type == 0 || TREE_CODE (type) != REFERENCE_TYPE)
3919 	{
3920 	  if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
3921 	      || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE
3922 	      || TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
3923 	    val = decay_conversion (val, complain);
3924 	}
3925 
3926       if (val == error_mark_node)
3927 	return -1;
3928 
3929       if (type != 0)
3930 	{
3931 	  /* Formal parm type is specified by a function prototype.  */
3932 	  tree parmval;
3933 
3934 	  if (!COMPLETE_TYPE_P (complete_type (type)))
3935 	    {
3936               if (complain & tf_error)
3937                 {
3938                   if (fndecl)
3939                     error ("parameter %P of %qD has incomplete type %qT",
3940                            i, fndecl, type);
3941                   else
3942                     error ("parameter %P has incomplete type %qT", i, type);
3943                 }
3944 	      parmval = error_mark_node;
3945 	    }
3946 	  else
3947 	    {
3948 	      parmval = convert_for_initialization
3949 		(NULL_TREE, type, val, flags,
3950 		 ICR_ARGPASS, fndecl, i, complain);
3951 	      parmval = convert_for_arg_passing (type, parmval, complain);
3952 	    }
3953 
3954 	  if (parmval == error_mark_node)
3955 	    return -1;
3956 
3957 	  (**values)[i] = parmval;
3958 	}
3959       else
3960 	{
3961 	  if (fndecl && magic_varargs_p (fndecl))
3962 	    /* Don't do ellipsis conversion for __built_in_constant_p
3963 	       as this will result in spurious errors for non-trivial
3964 	       types.  */
3965 	    val = require_complete_type_sfinae (val, complain);
3966 	  else
3967 	    val = convert_arg_to_ellipsis (val, complain);
3968 
3969 	  (**values)[i] = val;
3970 	}
3971 
3972       if (typetail)
3973 	typetail = TREE_CHAIN (typetail);
3974     }
3975 
3976   if (typetail != 0 && typetail != void_list_node)
3977     {
3978       /* See if there are default arguments that can be used.  Because
3979 	 we hold default arguments in the FUNCTION_TYPE (which is so
3980 	 wrong), we can see default parameters here from deduced
3981 	 contexts (and via typeof) for indirect function calls.
3982 	 Fortunately we know whether we have a function decl to
3983 	 provide default arguments in a language conformant
3984 	 manner.  */
3985       if (fndecl && TREE_PURPOSE (typetail)
3986 	  && TREE_CODE (TREE_PURPOSE (typetail)) != DEFAULT_ARG)
3987 	{
3988 	  for (; typetail != void_list_node; ++i)
3989 	    {
3990 	      /* After DR777, with explicit template args we can end up with a
3991 		 default argument followed by no default argument.  */
3992 	      if (!TREE_PURPOSE (typetail))
3993 		break;
3994 	      tree parmval
3995 		= convert_default_arg (TREE_VALUE (typetail),
3996 				       TREE_PURPOSE (typetail),
3997 				       fndecl, i, complain);
3998 
3999 	      if (parmval == error_mark_node)
4000 		return -1;
4001 
4002 	      vec_safe_push (*values, parmval);
4003 	      typetail = TREE_CHAIN (typetail);
4004 	      /* ends with `...'.  */
4005 	      if (typetail == NULL_TREE)
4006 		break;
4007 	    }
4008 	}
4009 
4010       if (typetail && typetail != void_list_node)
4011 	{
4012 	  if (complain & tf_error)
4013 	    error_args_num (input_location, fndecl, /*too_many_p=*/false);
4014 	  return -1;
4015 	}
4016     }
4017 
4018   return (int) i;
4019 }
4020 
4021 /* Build a binary-operation expression, after performing default
4022    conversions on the operands.  CODE is the kind of expression to
4023    build.  ARG1 and ARG2 are the arguments.  ARG1_CODE and ARG2_CODE
4024    are the tree codes which correspond to ARG1 and ARG2 when issuing
4025    warnings about possibly misplaced parentheses.  They may differ
4026    from the TREE_CODE of ARG1 and ARG2 if the parser has done constant
4027    folding (e.g., if the parser sees "a | 1 + 1", it may call this
4028    routine with ARG2 being an INTEGER_CST and ARG2_CODE == PLUS_EXPR).
4029    To avoid issuing any parentheses warnings, pass ARG1_CODE and/or
4030    ARG2_CODE as ERROR_MARK.  */
4031 
4032 tree
4033 build_x_binary_op (location_t loc, enum tree_code code, tree arg1,
4034 		   enum tree_code arg1_code, tree arg2,
4035 		   enum tree_code arg2_code, tree *overload_p,
4036 		   tsubst_flags_t complain)
4037 {
4038   tree orig_arg1;
4039   tree orig_arg2;
4040   tree expr;
4041   tree overload = NULL_TREE;
4042 
4043   orig_arg1 = arg1;
4044   orig_arg2 = arg2;
4045 
4046   if (processing_template_decl)
4047     {
4048       if (type_dependent_expression_p (arg1)
4049 	  || type_dependent_expression_p (arg2))
4050 	return build_min_nt_loc (loc, code, arg1, arg2);
4051       arg1 = build_non_dependent_expr (arg1);
4052       arg2 = build_non_dependent_expr (arg2);
4053     }
4054 
4055   if (code == DOTSTAR_EXPR)
4056     expr = build_m_component_ref (arg1, arg2, complain);
4057   else
4058     expr = build_new_op (loc, code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE,
4059 			 &overload, complain);
4060 
4061   if (overload_p != NULL)
4062     *overload_p = overload;
4063 
4064   /* Check for cases such as x+y<<z which users are likely to
4065      misinterpret.  But don't warn about obj << x + y, since that is a
4066      common idiom for I/O.  */
4067   if (warn_parentheses
4068       && (complain & tf_warning)
4069       && !processing_template_decl
4070       && !error_operand_p (arg1)
4071       && !error_operand_p (arg2)
4072       && (code != LSHIFT_EXPR
4073 	  || !CLASS_TYPE_P (TREE_TYPE (arg1))))
4074     warn_about_parentheses (loc, code, arg1_code, orig_arg1,
4075 			    arg2_code, orig_arg2);
4076 
4077   if (processing_template_decl && expr != error_mark_node)
4078     {
4079       if (overload != NULL_TREE)
4080 	return (build_min_non_dep_op_overload
4081 		(code, expr, overload, orig_arg1, orig_arg2));
4082 
4083       return build_min_non_dep (code, expr, orig_arg1, orig_arg2);
4084     }
4085 
4086   return expr;
4087 }
4088 
4089 /* Build and return an ARRAY_REF expression.  */
4090 
4091 tree
4092 build_x_array_ref (location_t loc, tree arg1, tree arg2,
4093 		   tsubst_flags_t complain)
4094 {
4095   tree orig_arg1 = arg1;
4096   tree orig_arg2 = arg2;
4097   tree expr;
4098   tree overload = NULL_TREE;
4099 
4100   if (processing_template_decl)
4101     {
4102       if (type_dependent_expression_p (arg1)
4103 	  || type_dependent_expression_p (arg2))
4104 	return build_min_nt_loc (loc, ARRAY_REF, arg1, arg2,
4105 				 NULL_TREE, NULL_TREE);
4106       arg1 = build_non_dependent_expr (arg1);
4107       arg2 = build_non_dependent_expr (arg2);
4108     }
4109 
4110   expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, arg1, arg2,
4111 		       NULL_TREE, &overload, complain);
4112 
4113   if (processing_template_decl && expr != error_mark_node)
4114     {
4115       if (overload != NULL_TREE)
4116 	return (build_min_non_dep_op_overload
4117 		(ARRAY_REF, expr, overload, orig_arg1, orig_arg2));
4118 
4119       return build_min_non_dep (ARRAY_REF, expr, orig_arg1, orig_arg2,
4120 				NULL_TREE, NULL_TREE);
4121     }
4122   return expr;
4123 }
4124 
4125 /* Return whether OP is an expression of enum type cast to integer
4126    type.  In C++ even unsigned enum types are cast to signed integer
4127    types.  We do not want to issue warnings about comparisons between
4128    signed and unsigned types when one of the types is an enum type.
4129    Those warnings are always false positives in practice.  */
4130 
4131 static bool
4132 enum_cast_to_int (tree op)
4133 {
4134   if (CONVERT_EXPR_P (op)
4135       && TREE_TYPE (op) == integer_type_node
4136       && TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == ENUMERAL_TYPE
4137       && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 0))))
4138     return true;
4139 
4140   /* The cast may have been pushed into a COND_EXPR.  */
4141   if (TREE_CODE (op) == COND_EXPR)
4142     return (enum_cast_to_int (TREE_OPERAND (op, 1))
4143 	    || enum_cast_to_int (TREE_OPERAND (op, 2)));
4144 
4145   return false;
4146 }
4147 
4148 /* For the c-common bits.  */
4149 tree
4150 build_binary_op (location_t location, enum tree_code code, tree op0, tree op1,
4151 		 bool /*convert_p*/)
4152 {
4153   return cp_build_binary_op (location, code, op0, op1, tf_warning_or_error);
4154 }
4155 
4156 /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
4157    into a value of TYPE type.  Comparison is done via VEC_COND_EXPR.  */
4158 
4159 static tree
4160 build_vec_cmp (tree_code code, tree type,
4161 	       tree arg0, tree arg1)
4162 {
4163   tree zero_vec = build_zero_cst (type);
4164   tree minus_one_vec = build_minus_one_cst (type);
4165   tree cmp_type = build_same_sized_truth_vector_type(type);
4166   tree cmp = build2 (code, cmp_type, arg0, arg1);
4167   return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
4168 }
4169 
4170 /* Possibly warn about an address never being NULL.  */
4171 
4172 static void
4173 warn_for_null_address (location_t location, tree op, tsubst_flags_t complain)
4174 {
4175   if (!warn_address
4176       || (complain & tf_warning) == 0
4177       || c_inhibit_evaluation_warnings != 0
4178       || TREE_NO_WARNING (op))
4179     return;
4180 
4181   tree cop = fold_non_dependent_expr (op);
4182 
4183   if (TREE_CODE (cop) == ADDR_EXPR
4184       && decl_with_nonnull_addr_p (TREE_OPERAND (cop, 0))
4185       && !TREE_NO_WARNING (cop))
4186     warning_at (location, OPT_Waddress, "the address of %qD will never "
4187 		"be NULL", TREE_OPERAND (cop, 0));
4188 
4189   if (CONVERT_EXPR_P (op)
4190       && TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == REFERENCE_TYPE)
4191     {
4192       tree inner_op = op;
4193       STRIP_NOPS (inner_op);
4194 
4195       if (DECL_P (inner_op))
4196 	warning_at (location, OPT_Waddress,
4197 		    "the compiler can assume that the address of "
4198 		    "%qD will never be NULL", inner_op);
4199     }
4200 }
4201 
4202 /* Build a binary-operation expression without default conversions.
4203    CODE is the kind of expression to build.
4204    LOCATION is the location_t of the operator in the source code.
4205    This function differs from `build' in several ways:
4206    the data type of the result is computed and recorded in it,
4207    warnings are generated if arg data types are invalid,
4208    special handling for addition and subtraction of pointers is known,
4209    and some optimization is done (operations on narrow ints
4210    are done in the narrower type when that gives the same result).
4211    Constant folding is also done before the result is returned.
4212 
4213    Note that the operands will never have enumeral types
4214    because either they have just had the default conversions performed
4215    or they have both just been converted to some other type in which
4216    the arithmetic is to be done.
4217 
4218    C++: must do special pointer arithmetic when implementing
4219    multiple inheritance, and deal with pointer to member functions.  */
4220 
4221 tree
4222 cp_build_binary_op (location_t location,
4223 		    enum tree_code code, tree orig_op0, tree orig_op1,
4224 		    tsubst_flags_t complain)
4225 {
4226   tree op0, op1;
4227   enum tree_code code0, code1;
4228   tree type0, type1;
4229   const char *invalid_op_diag;
4230 
4231   /* Expression code to give to the expression when it is built.
4232      Normally this is CODE, which is what the caller asked for,
4233      but in some special cases we change it.  */
4234   enum tree_code resultcode = code;
4235 
4236   /* Data type in which the computation is to be performed.
4237      In the simplest cases this is the common type of the arguments.  */
4238   tree result_type = NULL_TREE;
4239 
4240   /* Nonzero means operands have already been type-converted
4241      in whatever way is necessary.
4242      Zero means they need to be converted to RESULT_TYPE.  */
4243   int converted = 0;
4244 
4245   /* Nonzero means create the expression with this type, rather than
4246      RESULT_TYPE.  */
4247   tree build_type = 0;
4248 
4249   /* Nonzero means after finally constructing the expression
4250      convert it to this type.  */
4251   tree final_type = 0;
4252 
4253   tree result, result_ovl;
4254 
4255   /* Nonzero if this is an operation like MIN or MAX which can
4256      safely be computed in short if both args are promoted shorts.
4257      Also implies COMMON.
4258      -1 indicates a bitwise operation; this makes a difference
4259      in the exact conditions for when it is safe to do the operation
4260      in a narrower mode.  */
4261   int shorten = 0;
4262 
4263   /* Nonzero if this is a comparison operation;
4264      if both args are promoted shorts, compare the original shorts.
4265      Also implies COMMON.  */
4266   int short_compare = 0;
4267 
4268   /* Nonzero means set RESULT_TYPE to the common type of the args.  */
4269   int common = 0;
4270 
4271   /* True if both operands have arithmetic type.  */
4272   bool arithmetic_types_p;
4273 
4274   /* Apply default conversions.  */
4275   op0 = orig_op0;
4276   op1 = orig_op1;
4277 
4278   /* Remember whether we're doing / or %.  */
4279   bool doing_div_or_mod = false;
4280 
4281   /* Remember whether we're doing << or >>.  */
4282   bool doing_shift = false;
4283 
4284   /* Tree holding instrumentation expression.  */
4285   tree instrument_expr = NULL_TREE;
4286 
4287   if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
4288       || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
4289       || code == TRUTH_XOR_EXPR)
4290     {
4291       if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
4292 	op0 = decay_conversion (op0, complain);
4293       if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
4294 	op1 = decay_conversion (op1, complain);
4295     }
4296   else
4297     {
4298       if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
4299 	op0 = cp_default_conversion (op0, complain);
4300       if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
4301 	op1 = cp_default_conversion (op1, complain);
4302     }
4303 
4304   /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue.  */
4305   STRIP_TYPE_NOPS (op0);
4306   STRIP_TYPE_NOPS (op1);
4307 
4308   /* DTRT if one side is an overloaded function, but complain about it.  */
4309   if (type_unknown_p (op0))
4310     {
4311       tree t = instantiate_type (TREE_TYPE (op1), op0, 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 	  op0 = t;
4318 	}
4319     }
4320   if (type_unknown_p (op1))
4321     {
4322       tree t = instantiate_type (TREE_TYPE (op0), op1, tf_none);
4323       if (t != error_mark_node)
4324 	{
4325 	  if (complain & tf_error)
4326 	    permerror (input_location, "assuming cast to type %qT from overloaded function",
4327 		       TREE_TYPE (t));
4328 	  op1 = t;
4329 	}
4330     }
4331 
4332   type0 = TREE_TYPE (op0);
4333   type1 = TREE_TYPE (op1);
4334 
4335   /* The expression codes of the data types of the arguments tell us
4336      whether the arguments are integers, floating, pointers, etc.  */
4337   code0 = TREE_CODE (type0);
4338   code1 = TREE_CODE (type1);
4339 
4340   /* If an error was already reported for one of the arguments,
4341      avoid reporting another error.  */
4342   if (code0 == ERROR_MARK || code1 == ERROR_MARK)
4343     return error_mark_node;
4344 
4345   if ((invalid_op_diag
4346        = targetm.invalid_binary_op (code, type0, type1)))
4347     {
4348       if (complain & tf_error)
4349 	error (invalid_op_diag);
4350       return error_mark_node;
4351     }
4352 
4353   /* Issue warnings about peculiar, but valid, uses of NULL.  */
4354   if ((null_node_p (orig_op0) || null_node_p (orig_op1))
4355       /* It's reasonable to use pointer values as operands of &&
4356 	 and ||, so NULL is no exception.  */
4357       && code != TRUTH_ANDIF_EXPR && code != TRUTH_ORIF_EXPR
4358       && ( /* Both are NULL (or 0) and the operation was not a
4359 	      comparison or a pointer subtraction.  */
4360 	  (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1)
4361 	   && code != EQ_EXPR && code != NE_EXPR && code != MINUS_EXPR)
4362 	  /* Or if one of OP0 or OP1 is neither a pointer nor NULL.  */
4363 	  || (!null_ptr_cst_p (orig_op0)
4364 	      && !TYPE_PTR_OR_PTRMEM_P (type0))
4365 	  || (!null_ptr_cst_p (orig_op1)
4366 	      && !TYPE_PTR_OR_PTRMEM_P (type1)))
4367       && (complain & tf_warning))
4368     {
4369       source_location loc =
4370 	expansion_point_location_if_in_system_header (input_location);
4371 
4372       warning_at (loc, OPT_Wpointer_arith, "NULL used in arithmetic");
4373     }
4374 
4375   /* In case when one of the operands of the binary operation is
4376      a vector and another is a scalar -- convert scalar to vector.  */
4377   if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
4378     {
4379       enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
4380 						     complain & tf_error);
4381 
4382       switch (convert_flag)
4383         {
4384           case stv_error:
4385             return error_mark_node;
4386           case stv_firstarg:
4387             {
4388               op0 = convert (TREE_TYPE (type1), op0);
4389 	      op0 = save_expr (op0);
4390               op0 = build_vector_from_val (type1, op0);
4391               type0 = TREE_TYPE (op0);
4392               code0 = TREE_CODE (type0);
4393               converted = 1;
4394               break;
4395             }
4396           case stv_secondarg:
4397             {
4398               op1 = convert (TREE_TYPE (type0), op1);
4399 	      op1 = save_expr (op1);
4400               op1 = build_vector_from_val (type0, op1);
4401               type1 = TREE_TYPE (op1);
4402               code1 = TREE_CODE (type1);
4403               converted = 1;
4404               break;
4405             }
4406           default:
4407             break;
4408         }
4409     }
4410 
4411   switch (code)
4412     {
4413     case MINUS_EXPR:
4414       /* Subtraction of two similar pointers.
4415 	 We must subtract them as integers, then divide by object size.  */
4416       if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
4417 	  && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
4418 							TREE_TYPE (type1)))
4419 	{
4420 	  result = pointer_diff (location, op0, op1,
4421 				 common_pointer_type (type0, type1), complain,
4422 				 &instrument_expr);
4423 	  if (instrument_expr != NULL)
4424 	    result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
4425 			     instrument_expr, result);
4426 
4427 	  return result;
4428 	}
4429       /* In all other cases except pointer - int, the usual arithmetic
4430 	 rules apply.  */
4431       else if (!(code0 == POINTER_TYPE && code1 == INTEGER_TYPE))
4432 	{
4433 	  common = 1;
4434 	  break;
4435 	}
4436       /* The pointer - int case is just like pointer + int; fall
4437 	 through.  */
4438       gcc_fallthrough ();
4439     case PLUS_EXPR:
4440       if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
4441 	  && (code0 == INTEGER_TYPE || code1 == INTEGER_TYPE))
4442 	{
4443 	  tree ptr_operand;
4444 	  tree int_operand;
4445 	  ptr_operand = ((code0 == POINTER_TYPE) ? op0 : op1);
4446 	  int_operand = ((code0 == INTEGER_TYPE) ? op0 : op1);
4447 	  if (processing_template_decl)
4448 	    {
4449 	      result_type = TREE_TYPE (ptr_operand);
4450 	      break;
4451 	    }
4452 	  return cp_pointer_int_sum (location, code,
4453 				     ptr_operand,
4454 				     int_operand,
4455 				     complain);
4456 	}
4457       common = 1;
4458       break;
4459 
4460     case MULT_EXPR:
4461       common = 1;
4462       break;
4463 
4464     case TRUNC_DIV_EXPR:
4465     case CEIL_DIV_EXPR:
4466     case FLOOR_DIV_EXPR:
4467     case ROUND_DIV_EXPR:
4468     case EXACT_DIV_EXPR:
4469       if (TREE_CODE (op0) == SIZEOF_EXPR && TREE_CODE (op1) == SIZEOF_EXPR)
4470 	{
4471 	  tree type0 = TREE_OPERAND (op0, 0);
4472 	  tree type1 = TREE_OPERAND (op1, 0);
4473 	  tree first_arg = type0;
4474 	  if (!TYPE_P (type0))
4475 	    type0 = TREE_TYPE (type0);
4476 	  if (!TYPE_P (type1))
4477 	    type1 = TREE_TYPE (type1);
4478 	  if (POINTER_TYPE_P (type0) && same_type_p (TREE_TYPE (type0), type1)
4479 	      && !(TREE_CODE (first_arg) == PARM_DECL
4480 		   && DECL_ARRAY_PARAMETER_P (first_arg)
4481 		   && warn_sizeof_array_argument)
4482 	      && (complain & tf_warning))
4483 	    if (warning_at (location, OPT_Wsizeof_pointer_div,
4484 			    "division %<sizeof (%T) / sizeof (%T)%> does "
4485 			    "not compute the number of array elements",
4486 			    type0, type1))
4487 	      if (DECL_P (first_arg))
4488 		inform (DECL_SOURCE_LOCATION (first_arg),
4489 			"first %<sizeof%> operand was declared here");
4490 	}
4491 
4492       if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4493 	   || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
4494 	  && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4495 	      || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
4496 	{
4497 	  enum tree_code tcode0 = code0, tcode1 = code1;
4498 	  tree cop1 = fold_non_dependent_expr (op1);
4499 	  doing_div_or_mod = true;
4500 	  warn_for_div_by_zero (location, cop1);
4501 
4502 	  if (tcode0 == COMPLEX_TYPE || tcode0 == VECTOR_TYPE)
4503 	    tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
4504 	  if (tcode1 == COMPLEX_TYPE || tcode1 == VECTOR_TYPE)
4505 	    tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
4506 
4507 	  if (!(tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE))
4508 	    resultcode = RDIV_EXPR;
4509 	  else
4510 	    /* When dividing two signed integers, we have to promote to int.
4511 	       unless we divide by a constant != -1.  Note that default
4512 	       conversion will have been performed on the operands at this
4513 	       point, so we have to dig out the original type to find out if
4514 	       it was unsigned.  */
4515 	    shorten = ((TREE_CODE (op0) == NOP_EXPR
4516 			&& TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
4517 		       || (TREE_CODE (op1) == INTEGER_CST
4518 			   && ! integer_all_onesp (op1)));
4519 
4520 	  common = 1;
4521 	}
4522       break;
4523 
4524     case BIT_AND_EXPR:
4525     case BIT_IOR_EXPR:
4526     case BIT_XOR_EXPR:
4527       if ((code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4528 	  || (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4529 	      && !VECTOR_FLOAT_TYPE_P (type0)
4530 	      && !VECTOR_FLOAT_TYPE_P (type1)))
4531 	shorten = -1;
4532       break;
4533 
4534     case TRUNC_MOD_EXPR:
4535     case FLOOR_MOD_EXPR:
4536       {
4537 	tree cop1 = fold_non_dependent_expr (op1);
4538 	doing_div_or_mod = true;
4539 	warn_for_div_by_zero (location, cop1);
4540       }
4541 
4542       if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4543 	  && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4544 	  && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
4545 	common = 1;
4546       else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4547 	{
4548 	  /* Although it would be tempting to shorten always here, that loses
4549 	     on some targets, since the modulo instruction is undefined if the
4550 	     quotient can't be represented in the computation mode.  We shorten
4551 	     only if unsigned or if dividing by something we know != -1.  */
4552 	  shorten = ((TREE_CODE (op0) == NOP_EXPR
4553 		      && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
4554 		     || (TREE_CODE (op1) == INTEGER_CST
4555 			 && ! integer_all_onesp (op1)));
4556 	  common = 1;
4557 	}
4558       break;
4559 
4560     case TRUTH_ANDIF_EXPR:
4561     case TRUTH_ORIF_EXPR:
4562     case TRUTH_AND_EXPR:
4563     case TRUTH_OR_EXPR:
4564       if (!VECTOR_TYPE_P (type0) && VECTOR_TYPE_P (type1))
4565 	{
4566 	  if (!COMPARISON_CLASS_P (op1))
4567 	    op1 = cp_build_binary_op (EXPR_LOCATION (op1), NE_EXPR, op1,
4568 				      build_zero_cst (type1), complain);
4569 	  if (code == TRUTH_ANDIF_EXPR)
4570 	    {
4571 	      tree z = build_zero_cst (TREE_TYPE (op1));
4572 	      return build_conditional_expr (location, op0, op1, z, complain);
4573 	    }
4574 	  else if (code == TRUTH_ORIF_EXPR)
4575 	    {
4576 	      tree m1 = build_all_ones_cst (TREE_TYPE (op1));
4577 	      return build_conditional_expr (location, op0, m1, op1, complain);
4578 	    }
4579 	  else
4580 	    gcc_unreachable ();
4581 	}
4582       if (VECTOR_TYPE_P (type0))
4583 	{
4584 	  if (!COMPARISON_CLASS_P (op0))
4585 	    op0 = cp_build_binary_op (EXPR_LOCATION (op0), NE_EXPR, op0,
4586 				      build_zero_cst (type0), complain);
4587 	  if (!VECTOR_TYPE_P (type1))
4588 	    {
4589 	      tree m1 = build_all_ones_cst (TREE_TYPE (op0));
4590 	      tree z = build_zero_cst (TREE_TYPE (op0));
4591 	      op1 = build_conditional_expr (location, op1, m1, z, complain);
4592 	    }
4593 	  else if (!COMPARISON_CLASS_P (op1))
4594 	    op1 = cp_build_binary_op (EXPR_LOCATION (op1), NE_EXPR, op1,
4595 				      build_zero_cst (type1), complain);
4596 
4597 	  if (code == TRUTH_ANDIF_EXPR)
4598 	    code = BIT_AND_EXPR;
4599 	  else if (code == TRUTH_ORIF_EXPR)
4600 	    code = BIT_IOR_EXPR;
4601 	  else
4602 	    gcc_unreachable ();
4603 
4604 	  return cp_build_binary_op (location, code, op0, op1, complain);
4605 	}
4606 
4607       result_type = boolean_type_node;
4608       break;
4609 
4610       /* Shift operations: result has same type as first operand;
4611 	 always convert second operand to int.
4612 	 Also set SHORT_SHIFT if shifting rightward.  */
4613 
4614     case RSHIFT_EXPR:
4615       if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
4616           && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
4617         {
4618           result_type = type0;
4619           converted = 1;
4620         }
4621       else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4622 	       && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4623 	       && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
4624 	       && known_eq (TYPE_VECTOR_SUBPARTS (type0),
4625 			    TYPE_VECTOR_SUBPARTS (type1)))
4626 	{
4627 	  result_type = type0;
4628 	  converted = 1;
4629 	}
4630       else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4631 	{
4632 	  tree const_op1 = fold_non_dependent_expr (op1);
4633 	  if (TREE_CODE (const_op1) != INTEGER_CST)
4634 	    const_op1 = op1;
4635 	  result_type = type0;
4636 	  doing_shift = true;
4637 	  if (TREE_CODE (const_op1) == INTEGER_CST)
4638 	    {
4639 	      if (tree_int_cst_lt (const_op1, integer_zero_node))
4640 		{
4641 		  if ((complain & tf_warning)
4642 		      && c_inhibit_evaluation_warnings == 0)
4643 		    warning (OPT_Wshift_count_negative,
4644 			     "right shift count is negative");
4645 		}
4646 	      else
4647 		{
4648 		  if (compare_tree_int (const_op1, TYPE_PRECISION (type0)) >= 0
4649 		      && (complain & tf_warning)
4650 		      && c_inhibit_evaluation_warnings == 0)
4651 		    warning (OPT_Wshift_count_overflow,
4652 			     "right shift count >= width of type");
4653 		}
4654 	    }
4655 	  /* Avoid converting op1 to result_type later.  */
4656 	  converted = 1;
4657 	}
4658       break;
4659 
4660     case LSHIFT_EXPR:
4661       if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
4662           && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
4663         {
4664           result_type = type0;
4665           converted = 1;
4666         }
4667       else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4668 	       && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4669 	       && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
4670 	       && known_eq (TYPE_VECTOR_SUBPARTS (type0),
4671 			    TYPE_VECTOR_SUBPARTS (type1)))
4672 	{
4673 	  result_type = type0;
4674 	  converted = 1;
4675 	}
4676       else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4677 	{
4678 	  tree const_op0 = fold_non_dependent_expr (op0);
4679 	  if (TREE_CODE (const_op0) != INTEGER_CST)
4680 	    const_op0 = op0;
4681 	  tree const_op1 = fold_non_dependent_expr (op1);
4682 	  if (TREE_CODE (const_op1) != INTEGER_CST)
4683 	    const_op1 = op1;
4684 	  result_type = type0;
4685 	  doing_shift = true;
4686 	  if (TREE_CODE (const_op0) == INTEGER_CST
4687 	      && tree_int_cst_sgn (const_op0) < 0
4688 	      && (complain & tf_warning)
4689 	      && c_inhibit_evaluation_warnings == 0)
4690 	    warning (OPT_Wshift_negative_value,
4691 		     "left shift of negative value");
4692 	  if (TREE_CODE (const_op1) == INTEGER_CST)
4693 	    {
4694 	      if (tree_int_cst_lt (const_op1, integer_zero_node))
4695 		{
4696 		  if ((complain & tf_warning)
4697 		      && c_inhibit_evaluation_warnings == 0)
4698 		    warning (OPT_Wshift_count_negative,
4699 			     "left shift count is negative");
4700 		}
4701 	      else if (compare_tree_int (const_op1,
4702 					 TYPE_PRECISION (type0)) >= 0)
4703 		{
4704 		  if ((complain & tf_warning)
4705 		      && c_inhibit_evaluation_warnings == 0)
4706 		    warning (OPT_Wshift_count_overflow,
4707 			     "left shift count >= width of type");
4708 		}
4709 	      else if (TREE_CODE (const_op0) == INTEGER_CST
4710 		       && (complain & tf_warning))
4711 		maybe_warn_shift_overflow (location, const_op0, const_op1);
4712 	    }
4713 	  /* Avoid converting op1 to result_type later.  */
4714 	  converted = 1;
4715 	}
4716       break;
4717 
4718     case RROTATE_EXPR:
4719     case LROTATE_EXPR:
4720       if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4721 	{
4722 	  result_type = type0;
4723 	  if (TREE_CODE (op1) == INTEGER_CST)
4724 	    {
4725 	      if (tree_int_cst_lt (op1, integer_zero_node))
4726 		{
4727 		  if (complain & tf_warning)
4728 		    warning (0, (code == LROTATE_EXPR)
4729 			          ? G_("left rotate count is negative")
4730    			          : G_("right rotate count is negative"));
4731 		}
4732 	      else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
4733 		{
4734 		  if (complain & tf_warning)
4735 		    warning (0, (code == LROTATE_EXPR)
4736                                   ? G_("left rotate count >= width of type")
4737                                   : G_("right rotate count >= width of type"));
4738 		}
4739 	    }
4740 	  /* Convert the shift-count to an integer, regardless of
4741 	     size of value being shifted.  */
4742 	  if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
4743 	    op1 = cp_convert (integer_type_node, op1, complain);
4744 	}
4745       break;
4746 
4747     case EQ_EXPR:
4748     case NE_EXPR:
4749       if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4750 	goto vector_compare;
4751       if ((complain & tf_warning)
4752 	  && (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1)))
4753 	warning (OPT_Wfloat_equal,
4754 		 "comparing floating point with == or != is unsafe");
4755       if ((complain & tf_warning)
4756 	  && ((TREE_CODE (orig_op0) == STRING_CST
4757 	       && !integer_zerop (cp_fully_fold (op1)))
4758 	      || (TREE_CODE (orig_op1) == STRING_CST
4759 		  && !integer_zerop (cp_fully_fold (op0)))))
4760 	warning (OPT_Waddress, "comparison with string literal results "
4761 			       "in unspecified behavior");
4762 
4763       build_type = boolean_type_node;
4764       if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4765 	   || code0 == COMPLEX_TYPE || code0 == ENUMERAL_TYPE)
4766 	  && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4767 	      || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE))
4768 	short_compare = 1;
4769       else if (((code0 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type0))
4770 		&& null_ptr_cst_p (orig_op1))
4771 	       /* Handle, eg, (void*)0 (c++/43906), and more.  */
4772 	       || (code0 == POINTER_TYPE
4773 		   && TYPE_PTR_P (type1) && integer_zerop (op1)))
4774 	{
4775 	  if (TYPE_PTR_P (type1))
4776 	    result_type = composite_pointer_type (type0, type1, op0, op1,
4777 						  CPO_COMPARISON, complain);
4778 	  else
4779 	    result_type = type0;
4780 
4781 	  if (char_type_p (TREE_TYPE (orig_op1))
4782 	      && warning (OPT_Wpointer_compare,
4783 			  "comparison between pointer and zero character "
4784 			  "constant"))
4785 	    inform (input_location,
4786 		    "did you mean to dereference the pointer?");
4787 	  warn_for_null_address (location, op0, complain);
4788 	}
4789       else if (((code1 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type1))
4790 		&& null_ptr_cst_p (orig_op0))
4791 	       /* Handle, eg, (void*)0 (c++/43906), and more.  */
4792 	       || (code1 == POINTER_TYPE
4793 		   && TYPE_PTR_P (type0) && integer_zerop (op0)))
4794 	{
4795 	  if (TYPE_PTR_P (type0))
4796 	    result_type = composite_pointer_type (type0, type1, op0, op1,
4797 						  CPO_COMPARISON, complain);
4798 	  else
4799 	    result_type = type1;
4800 
4801 	  if (char_type_p (TREE_TYPE (orig_op0))
4802 	      && warning (OPT_Wpointer_compare,
4803 			  "comparison between pointer and zero character "
4804 			  "constant"))
4805 	    inform (input_location,
4806 		    "did you mean to dereference the pointer?");
4807 	  warn_for_null_address (location, op1, complain);
4808 	}
4809       else if ((code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4810 	       || (TYPE_PTRDATAMEM_P (type0) && TYPE_PTRDATAMEM_P (type1)))
4811 	result_type = composite_pointer_type (type0, type1, op0, op1,
4812 					      CPO_COMPARISON, complain);
4813       else if (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1))
4814 	/* One of the operands must be of nullptr_t type.  */
4815         result_type = TREE_TYPE (nullptr_node);
4816       else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
4817 	{
4818 	  result_type = type0;
4819 	  if (complain & tf_error)
4820             permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4821           else
4822             return error_mark_node;
4823 	}
4824       else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
4825 	{
4826 	  result_type = type1;
4827 	  if (complain & tf_error)
4828 	    permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4829           else
4830             return error_mark_node;
4831 	}
4832       else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (orig_op1))
4833 	{
4834 	  if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4835 	      == ptrmemfunc_vbit_in_delta)
4836 	    {
4837 	      tree pfn0, delta0, e1, e2;
4838 
4839 	      if (TREE_SIDE_EFFECTS (op0))
4840 		op0 = cp_save_expr (op0);
4841 
4842 	      pfn0 = pfn_from_ptrmemfunc (op0);
4843 	      delta0 = delta_from_ptrmemfunc (op0);
4844 	      e1 = cp_build_binary_op (location,
4845 				       EQ_EXPR,
4846 	  			       pfn0,
4847 				       build_zero_cst (TREE_TYPE (pfn0)),
4848 				       complain);
4849 	      e2 = cp_build_binary_op (location,
4850 				       BIT_AND_EXPR,
4851 				       delta0,
4852 				       integer_one_node,
4853 				       complain);
4854 
4855 	      if (complain & tf_warning)
4856 		maybe_warn_zero_as_null_pointer_constant (op1, input_location);
4857 
4858 	      e2 = cp_build_binary_op (location,
4859 				       EQ_EXPR, e2, integer_zero_node,
4860 				       complain);
4861 	      op0 = cp_build_binary_op (location,
4862 					TRUTH_ANDIF_EXPR, e1, e2,
4863 					complain);
4864 	      op1 = cp_convert (TREE_TYPE (op0), integer_one_node, complain);
4865 	    }
4866      	  else
4867 	    {
4868 	      op0 = build_ptrmemfunc_access_expr (op0, pfn_identifier);
4869 	      op1 = cp_convert (TREE_TYPE (op0), op1, complain);
4870 	    }
4871 	  result_type = TREE_TYPE (op0);
4872 	}
4873       else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (orig_op0))
4874 	return cp_build_binary_op (location, code, op1, op0, complain);
4875       else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1))
4876 	{
4877 	  tree type;
4878 	  /* E will be the final comparison.  */
4879 	  tree e;
4880 	  /* E1 and E2 are for scratch.  */
4881 	  tree e1;
4882 	  tree e2;
4883 	  tree pfn0;
4884 	  tree pfn1;
4885 	  tree delta0;
4886 	  tree delta1;
4887 
4888 	  type = composite_pointer_type (type0, type1, op0, op1,
4889 					 CPO_COMPARISON, complain);
4890 
4891 	  if (!same_type_p (TREE_TYPE (op0), type))
4892 	    op0 = cp_convert_and_check (type, op0, complain);
4893 	  if (!same_type_p (TREE_TYPE (op1), type))
4894 	    op1 = cp_convert_and_check (type, op1, complain);
4895 
4896 	  if (op0 == error_mark_node || op1 == error_mark_node)
4897 	    return error_mark_node;
4898 
4899 	  if (TREE_SIDE_EFFECTS (op0))
4900 	    op0 = save_expr (op0);
4901 	  if (TREE_SIDE_EFFECTS (op1))
4902 	    op1 = save_expr (op1);
4903 
4904 	  pfn0 = pfn_from_ptrmemfunc (op0);
4905 	  pfn0 = cp_fully_fold (pfn0);
4906 	  /* Avoid -Waddress warnings (c++/64877).  */
4907 	  if (TREE_CODE (pfn0) == ADDR_EXPR)
4908 	    TREE_NO_WARNING (pfn0) = 1;
4909 	  pfn1 = pfn_from_ptrmemfunc (op1);
4910 	  pfn1 = cp_fully_fold (pfn1);
4911 	  delta0 = delta_from_ptrmemfunc (op0);
4912 	  delta1 = delta_from_ptrmemfunc (op1);
4913 	  if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4914 	      == ptrmemfunc_vbit_in_delta)
4915 	    {
4916 	      /* We generate:
4917 
4918 		 (op0.pfn == op1.pfn
4919 		  && ((op0.delta == op1.delta)
4920      		       || (!op0.pfn && op0.delta & 1 == 0
4921 			   && op1.delta & 1 == 0))
4922 
4923 	         The reason for the `!op0.pfn' bit is that a NULL
4924 	         pointer-to-member is any member with a zero PFN and
4925 	         LSB of the DELTA field is 0.  */
4926 
4927 	      e1 = cp_build_binary_op (location, BIT_AND_EXPR,
4928 				       delta0,
4929 				       integer_one_node,
4930 				       complain);
4931 	      e1 = cp_build_binary_op (location,
4932 				       EQ_EXPR, e1, integer_zero_node,
4933 				       complain);
4934 	      e2 = cp_build_binary_op (location, BIT_AND_EXPR,
4935 				       delta1,
4936 				       integer_one_node,
4937 				       complain);
4938 	      e2 = cp_build_binary_op (location,
4939 				       EQ_EXPR, e2, integer_zero_node,
4940 				       complain);
4941 	      e1 = cp_build_binary_op (location,
4942 				       TRUTH_ANDIF_EXPR, e2, e1,
4943 				       complain);
4944 	      e2 = cp_build_binary_op (location, EQ_EXPR,
4945 				       pfn0,
4946 				       build_zero_cst (TREE_TYPE (pfn0)),
4947 				       complain);
4948 	      e2 = cp_build_binary_op (location,
4949 				       TRUTH_ANDIF_EXPR, e2, e1, complain);
4950 	      e1 = cp_build_binary_op (location,
4951 				       EQ_EXPR, delta0, delta1, complain);
4952 	      e1 = cp_build_binary_op (location,
4953 				       TRUTH_ORIF_EXPR, e1, e2, complain);
4954 	    }
4955 	  else
4956 	    {
4957 	      /* We generate:
4958 
4959 	         (op0.pfn == op1.pfn
4960 	         && (!op0.pfn || op0.delta == op1.delta))
4961 
4962 	         The reason for the `!op0.pfn' bit is that a NULL
4963 	         pointer-to-member is any member with a zero PFN; the
4964 	         DELTA field is unspecified.  */
4965 
4966     	      e1 = cp_build_binary_op (location,
4967 				       EQ_EXPR, delta0, delta1, complain);
4968 	      e2 = cp_build_binary_op (location,
4969 				       EQ_EXPR,
4970 		      		       pfn0,
4971 			   	       build_zero_cst (TREE_TYPE (pfn0)),
4972 				       complain);
4973 	      e1 = cp_build_binary_op (location,
4974 				       TRUTH_ORIF_EXPR, e1, e2, complain);
4975 	    }
4976 	  e2 = build2 (EQ_EXPR, boolean_type_node, pfn0, pfn1);
4977 	  e = cp_build_binary_op (location,
4978 				  TRUTH_ANDIF_EXPR, e2, e1, complain);
4979 	  if (code == EQ_EXPR)
4980 	    return e;
4981 	  return cp_build_binary_op (location,
4982 				     EQ_EXPR, e, integer_zero_node, complain);
4983 	}
4984       else
4985 	{
4986 	  gcc_assert (!TYPE_PTRMEMFUNC_P (type0)
4987 		      || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0),
4988 				       type1));
4989 	  gcc_assert (!TYPE_PTRMEMFUNC_P (type1)
4990 		      || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1),
4991 				       type0));
4992 	}
4993 
4994       break;
4995 
4996     case MAX_EXPR:
4997     case MIN_EXPR:
4998       if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
4999 	   && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
5000 	shorten = 1;
5001       else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
5002 	result_type = composite_pointer_type (type0, type1, op0, op1,
5003 					      CPO_COMPARISON, complain);
5004       break;
5005 
5006     case LE_EXPR:
5007     case GE_EXPR:
5008     case LT_EXPR:
5009     case GT_EXPR:
5010       if (TREE_CODE (orig_op0) == STRING_CST
5011 	  || TREE_CODE (orig_op1) == STRING_CST)
5012 	{
5013 	  if (complain & tf_warning)
5014 	    warning (OPT_Waddress, "comparison with string literal results "
5015 				   "in unspecified behavior");
5016 	}
5017 
5018       if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
5019 	{
5020 	vector_compare:
5021 	  tree intt;
5022 	  if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
5023 							  TREE_TYPE (type1))
5024 	      && !vector_types_compatible_elements_p (type0, type1))
5025 	    {
5026 	      if (complain & tf_error)
5027 		{
5028 		  error_at (location, "comparing vectors with different "
5029 				      "element types");
5030 		  inform (location, "operand types are %qT and %qT",
5031 			  type0, type1);
5032 		}
5033 	      return error_mark_node;
5034 	    }
5035 
5036 	  if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
5037 			TYPE_VECTOR_SUBPARTS (type1)))
5038 	    {
5039 	      if (complain & tf_error)
5040 		{
5041 		  error_at (location, "comparing vectors with different "
5042 				      "number of elements");
5043 		  inform (location, "operand types are %qT and %qT",
5044 			  type0, type1);
5045 		}
5046 	      return error_mark_node;
5047 	    }
5048 
5049 	  /* It's not precisely specified how the usual arithmetic
5050 	     conversions apply to the vector types.  Here, we use
5051 	     the unsigned type if one of the operands is signed and
5052 	     the other one is unsigned.  */
5053 	  if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
5054 	    {
5055 	      if (!TYPE_UNSIGNED (type0))
5056 		op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
5057 	      else
5058 		op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
5059 	      warning_at (location, OPT_Wsign_compare, "comparison between "
5060 			  "types %qT and %qT", type0, type1);
5061 	    }
5062 
5063 	  /* Always construct signed integer vector type.  */
5064 	  intt = c_common_type_for_size
5065 	    (GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (type0))), 0);
5066 	  if (!intt)
5067 	    {
5068 	      if (complain & tf_error)
5069 		error_at (location, "could not find an integer type "
5070 			  "of the same size as %qT", TREE_TYPE (type0));
5071 	      return error_mark_node;
5072 	    }
5073 	  result_type = build_opaque_vector_type (intt,
5074 						  TYPE_VECTOR_SUBPARTS (type0));
5075 	  converted = 1;
5076 	  return build_vec_cmp (resultcode, result_type, op0, op1);
5077 	}
5078       build_type = boolean_type_node;
5079       if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
5080 	   || code0 == ENUMERAL_TYPE)
5081 	   && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
5082 	       || code1 == ENUMERAL_TYPE))
5083 	short_compare = 1;
5084       else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
5085 	result_type = composite_pointer_type (type0, type1, op0, op1,
5086 					      CPO_COMPARISON, complain);
5087       else if (code0 == POINTER_TYPE && null_ptr_cst_p (orig_op1))
5088 	{
5089 	  result_type = type0;
5090 	  if (extra_warnings && (complain & tf_warning))
5091 	    warning (OPT_Wextra,
5092 		     "ordered comparison of pointer with integer zero");
5093 	}
5094       else if (code1 == POINTER_TYPE && null_ptr_cst_p (orig_op0))
5095 	{
5096 	  result_type = type1;
5097 	  if (extra_warnings && (complain & tf_warning))
5098 	    warning (OPT_Wextra,
5099 		     "ordered comparison of pointer with integer zero");
5100 	}
5101       else if (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1))
5102 	/* One of the operands must be of nullptr_t type.  */
5103         result_type = TREE_TYPE (nullptr_node);
5104       else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
5105 	{
5106 	  result_type = type0;
5107 	  if (complain & tf_error)
5108 	    permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
5109           else
5110             return error_mark_node;
5111 	}
5112       else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
5113 	{
5114 	  result_type = type1;
5115 	  if (complain & tf_error)
5116 	    permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
5117           else
5118             return error_mark_node;
5119 	}
5120 
5121       if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
5122 	  && !processing_template_decl
5123 	  && sanitize_flags_p (SANITIZE_POINTER_COMPARE))
5124 	{
5125 	  op0 = save_expr (op0);
5126 	  op1 = save_expr (op1);
5127 
5128 	  tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_COMPARE);
5129 	  instrument_expr = build_call_expr_loc (location, tt, 2, op0, op1);
5130 	}
5131 
5132       break;
5133 
5134     case UNORDERED_EXPR:
5135     case ORDERED_EXPR:
5136     case UNLT_EXPR:
5137     case UNLE_EXPR:
5138     case UNGT_EXPR:
5139     case UNGE_EXPR:
5140     case UNEQ_EXPR:
5141       build_type = integer_type_node;
5142       if (code0 != REAL_TYPE || code1 != REAL_TYPE)
5143 	{
5144 	  if (complain & tf_error)
5145 	    error ("unordered comparison on non-floating point argument");
5146 	  return error_mark_node;
5147 	}
5148       common = 1;
5149       break;
5150 
5151     default:
5152       break;
5153     }
5154 
5155   if (((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
5156 	|| code0 == ENUMERAL_TYPE)
5157        && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
5158 	   || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE)))
5159     arithmetic_types_p = 1;
5160   else
5161     {
5162       arithmetic_types_p = 0;
5163       /* Vector arithmetic is only allowed when both sides are vectors.  */
5164       if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
5165 	{
5166 	  if (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
5167 	      || !vector_types_compatible_elements_p (type0, type1))
5168 	    {
5169 	      if (complain & tf_error)
5170 		{
5171 		  /* "location" already embeds the locations of the
5172 		     operands, so we don't need to add them separately
5173 		     to richloc.  */
5174 		  rich_location richloc (line_table, location);
5175 		  binary_op_error (&richloc, code, type0, type1);
5176 		}
5177 	      return error_mark_node;
5178 	    }
5179 	  arithmetic_types_p = 1;
5180 	}
5181     }
5182   /* Determine the RESULT_TYPE, if it is not already known.  */
5183   if (!result_type
5184       && arithmetic_types_p
5185       && (shorten || common || short_compare))
5186     {
5187       result_type = cp_common_type (type0, type1);
5188       if (complain & tf_warning)
5189 	do_warn_double_promotion (result_type, type0, type1,
5190 				  "implicit conversion from %qH to %qI "
5191 				  "to match other operand of binary "
5192 				  "expression",
5193 				  location);
5194     }
5195 
5196   if (!result_type)
5197     {
5198       if (complain & tf_error)
5199 	error_at (location,
5200 		  "invalid operands of types %qT and %qT to binary %qO",
5201 		  TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), code);
5202       return error_mark_node;
5203     }
5204 
5205   /* If we're in a template, the only thing we need to know is the
5206      RESULT_TYPE.  */
5207   if (processing_template_decl)
5208     {
5209       /* Since the middle-end checks the type when doing a build2, we
5210 	 need to build the tree in pieces.  This built tree will never
5211 	 get out of the front-end as we replace it when instantiating
5212 	 the template.  */
5213       tree tmp = build2 (resultcode,
5214 			 build_type ? build_type : result_type,
5215 			 NULL_TREE, op1);
5216       TREE_OPERAND (tmp, 0) = op0;
5217       return tmp;
5218     }
5219 
5220   /* Remember the original type; RESULT_TYPE might be changed later on
5221      by shorten_binary_op.  */
5222   tree orig_type = result_type;
5223 
5224   if (arithmetic_types_p)
5225     {
5226       bool first_complex = (code0 == COMPLEX_TYPE);
5227       bool second_complex = (code1 == COMPLEX_TYPE);
5228       int none_complex = (!first_complex && !second_complex);
5229 
5230       /* Adapted from patch for c/24581.  */
5231       if (first_complex != second_complex
5232 	  && (code == PLUS_EXPR
5233 	      || code == MINUS_EXPR
5234 	      || code == MULT_EXPR
5235 	      || (code == TRUNC_DIV_EXPR && first_complex))
5236 	  && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
5237 	  && flag_signed_zeros)
5238 	{
5239 	  /* An operation on mixed real/complex operands must be
5240 	     handled specially, but the language-independent code can
5241 	     more easily optimize the plain complex arithmetic if
5242 	     -fno-signed-zeros.  */
5243 	  tree real_type = TREE_TYPE (result_type);
5244 	  tree real, imag;
5245 	  if (first_complex)
5246 	    {
5247 	      if (TREE_TYPE (op0) != result_type)
5248 		op0 = cp_convert_and_check (result_type, op0, complain);
5249 	      if (TREE_TYPE (op1) != real_type)
5250 		op1 = cp_convert_and_check (real_type, op1, complain);
5251 	    }
5252 	  else
5253 	    {
5254 	      if (TREE_TYPE (op0) != real_type)
5255 		op0 = cp_convert_and_check (real_type, op0, complain);
5256 	      if (TREE_TYPE (op1) != result_type)
5257 		op1 = cp_convert_and_check (result_type, op1, complain);
5258 	    }
5259 	  if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
5260 	    return error_mark_node;
5261 	  if (first_complex)
5262 	    {
5263 	      op0 = save_expr (op0);
5264 	      real = cp_build_unary_op (REALPART_EXPR, op0, true, complain);
5265 	      imag = cp_build_unary_op (IMAGPART_EXPR, op0, true, complain);
5266 	      switch (code)
5267 		{
5268 		case MULT_EXPR:
5269 		case TRUNC_DIV_EXPR:
5270 		  op1 = save_expr (op1);
5271 		  imag = build2 (resultcode, real_type, imag, op1);
5272 		  /* Fall through.  */
5273 		case PLUS_EXPR:
5274 		case MINUS_EXPR:
5275 		  real = build2 (resultcode, real_type, real, op1);
5276 		  break;
5277 		default:
5278 		  gcc_unreachable();
5279 		}
5280 	    }
5281 	  else
5282 	    {
5283 	      op1 = save_expr (op1);
5284 	      real = cp_build_unary_op (REALPART_EXPR, op1, true, complain);
5285 	      imag = cp_build_unary_op (IMAGPART_EXPR, op1, true, complain);
5286 	      switch (code)
5287 		{
5288 		case MULT_EXPR:
5289 		  op0 = save_expr (op0);
5290 		  imag = build2 (resultcode, real_type, op0, imag);
5291 		  /* Fall through.  */
5292 		case PLUS_EXPR:
5293 		  real = build2 (resultcode, real_type, op0, real);
5294 		  break;
5295 		case MINUS_EXPR:
5296 		  real = build2 (resultcode, real_type, op0, real);
5297 		  imag = build1 (NEGATE_EXPR, real_type, imag);
5298 		  break;
5299 		default:
5300 		  gcc_unreachable();
5301 		}
5302 	    }
5303 	  result = build2 (COMPLEX_EXPR, result_type, real, imag);
5304 	  return result;
5305 	}
5306 
5307       /* For certain operations (which identify themselves by shorten != 0)
5308 	 if both args were extended from the same smaller type,
5309 	 do the arithmetic in that type and then extend.
5310 
5311 	 shorten !=0 and !=1 indicates a bitwise operation.
5312 	 For them, this optimization is safe only if
5313 	 both args are zero-extended or both are sign-extended.
5314 	 Otherwise, we might change the result.
5315 	 E.g., (short)-1 | (unsigned short)-1 is (int)-1
5316 	 but calculated in (unsigned short) it would be (unsigned short)-1.  */
5317 
5318       if (shorten && none_complex)
5319 	{
5320 	  final_type = result_type;
5321 	  result_type = shorten_binary_op (result_type, op0, op1,
5322 					   shorten == -1);
5323 	}
5324 
5325       /* Comparison operations are shortened too but differently.
5326 	 They identify themselves by setting short_compare = 1.  */
5327 
5328       if (short_compare)
5329 	{
5330 	  /* We call shorten_compare only for diagnostic-reason.  */
5331 	  tree xop0 = fold_simple (op0), xop1 = fold_simple (op1),
5332 	       xresult_type = result_type;
5333 	  enum tree_code xresultcode = resultcode;
5334 	  shorten_compare (location, &xop0, &xop1, &xresult_type,
5335 			       &xresultcode);
5336 	}
5337 
5338       if ((short_compare || code == MIN_EXPR || code == MAX_EXPR)
5339 	  && warn_sign_compare
5340 	  /* Do not warn until the template is instantiated; we cannot
5341 	     bound the ranges of the arguments until that point.  */
5342 	  && !processing_template_decl
5343           && (complain & tf_warning)
5344 	  && c_inhibit_evaluation_warnings == 0
5345 	  /* Even unsigned enum types promote to signed int.  We don't
5346 	     want to issue -Wsign-compare warnings for this case.  */
5347 	  && !enum_cast_to_int (orig_op0)
5348 	  && !enum_cast_to_int (orig_op1))
5349 	{
5350 	  tree oop0 = maybe_constant_value (orig_op0);
5351 	  tree oop1 = maybe_constant_value (orig_op1);
5352 
5353 	  if (TREE_CODE (oop0) != INTEGER_CST)
5354 	    oop0 = cp_fully_fold (orig_op0);
5355 	  if (TREE_CODE (oop1) != INTEGER_CST)
5356 	    oop1 = cp_fully_fold (orig_op1);
5357 	  warn_for_sign_compare (location, oop0, oop1, op0, op1,
5358 				 result_type, resultcode);
5359 	}
5360     }
5361 
5362   /* If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
5363      Then the expression will be built.
5364      It will be given type FINAL_TYPE if that is nonzero;
5365      otherwise, it will be given type RESULT_TYPE.  */
5366   if (! converted)
5367     {
5368       warning_sentinel w (warn_sign_conversion, short_compare);
5369       if (TREE_TYPE (op0) != result_type)
5370 	op0 = cp_convert_and_check (result_type, op0, complain);
5371       if (TREE_TYPE (op1) != result_type)
5372 	op1 = cp_convert_and_check (result_type, op1, complain);
5373 
5374       if (op0 == error_mark_node || op1 == error_mark_node)
5375 	return error_mark_node;
5376     }
5377 
5378   if (build_type == NULL_TREE)
5379     build_type = result_type;
5380 
5381   if (sanitize_flags_p ((SANITIZE_SHIFT
5382 			 | SANITIZE_DIVIDE | SANITIZE_FLOAT_DIVIDE))
5383       && current_function_decl != NULL_TREE
5384       && !processing_template_decl
5385       && (doing_div_or_mod || doing_shift))
5386     {
5387       /* OP0 and/or OP1 might have side-effects.  */
5388       op0 = cp_save_expr (op0);
5389       op1 = cp_save_expr (op1);
5390       op0 = fold_non_dependent_expr (op0);
5391       op1 = fold_non_dependent_expr (op1);
5392       if (doing_div_or_mod
5393 	  && sanitize_flags_p (SANITIZE_DIVIDE | SANITIZE_FLOAT_DIVIDE))
5394 	{
5395 	  /* For diagnostics we want to use the promoted types without
5396 	     shorten_binary_op.  So convert the arguments to the
5397 	     original result_type.  */
5398 	  tree cop0 = op0;
5399 	  tree cop1 = op1;
5400 	  if (TREE_TYPE (cop0) != orig_type)
5401 	    cop0 = cp_convert (orig_type, op0, complain);
5402 	  if (TREE_TYPE (cop1) != orig_type)
5403 	    cop1 = cp_convert (orig_type, op1, complain);
5404 	  instrument_expr = ubsan_instrument_division (location, cop0, cop1);
5405 	}
5406       else if (doing_shift && sanitize_flags_p (SANITIZE_SHIFT))
5407 	instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
5408     }
5409 
5410   result = build2_loc (location, resultcode, build_type, op0, op1);
5411   if (final_type != 0)
5412     result = cp_convert (final_type, result, complain);
5413 
5414   if (instrument_expr != NULL)
5415     result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
5416 		     instrument_expr, result);
5417 
5418   if (!processing_template_decl)
5419     {
5420       op0 = cp_fully_fold (op0);
5421       /* Only consider the second argument if the first isn't overflowed.  */
5422       if (!CONSTANT_CLASS_P (op0) || TREE_OVERFLOW_P (op0))
5423 	return result;
5424       op1 = cp_fully_fold (op1);
5425       if (!CONSTANT_CLASS_P (op1) || TREE_OVERFLOW_P (op1))
5426 	return result;
5427     }
5428   else if (!CONSTANT_CLASS_P (op0) || !CONSTANT_CLASS_P (op1)
5429 	   || TREE_OVERFLOW_P (op0) || TREE_OVERFLOW_P (op1))
5430     return result;
5431 
5432   result_ovl = fold_build2 (resultcode, build_type, op0, op1);
5433   if (TREE_OVERFLOW_P (result_ovl))
5434     overflow_warning (location, result_ovl);
5435 
5436   return result;
5437 }
5438 
5439 /* Build a VEC_PERM_EXPR.
5440    This is a simple wrapper for c_build_vec_perm_expr.  */
5441 tree
5442 build_x_vec_perm_expr (location_t loc,
5443 			tree arg0, tree arg1, tree arg2,
5444 			tsubst_flags_t complain)
5445 {
5446   tree orig_arg0 = arg0;
5447   tree orig_arg1 = arg1;
5448   tree orig_arg2 = arg2;
5449   if (processing_template_decl)
5450     {
5451       if (type_dependent_expression_p (arg0)
5452 	  || type_dependent_expression_p (arg1)
5453 	  || type_dependent_expression_p (arg2))
5454 	return build_min_nt_loc (loc, VEC_PERM_EXPR, arg0, arg1, arg2);
5455       arg0 = build_non_dependent_expr (arg0);
5456       if (arg1)
5457 	arg1 = build_non_dependent_expr (arg1);
5458       arg2 = build_non_dependent_expr (arg2);
5459     }
5460   tree exp = c_build_vec_perm_expr (loc, arg0, arg1, arg2, complain & tf_error);
5461   if (processing_template_decl && exp != error_mark_node)
5462     return build_min_non_dep (VEC_PERM_EXPR, exp, orig_arg0,
5463 			      orig_arg1, orig_arg2);
5464   return exp;
5465 }
5466 
5467 /* Return a tree for the sum or difference (RESULTCODE says which)
5468    of pointer PTROP and integer INTOP.  */
5469 
5470 static tree
5471 cp_pointer_int_sum (location_t loc, enum tree_code resultcode, tree ptrop,
5472 		    tree intop, tsubst_flags_t complain)
5473 {
5474   tree res_type = TREE_TYPE (ptrop);
5475 
5476   /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
5477      in certain circumstance (when it's valid to do so).  So we need
5478      to make sure it's complete.  We don't need to check here, if we
5479      can actually complete it at all, as those checks will be done in
5480      pointer_int_sum() anyway.  */
5481   complete_type (TREE_TYPE (res_type));
5482 
5483   return pointer_int_sum (loc, resultcode, ptrop,
5484 			  intop, complain & tf_warning_or_error);
5485 }
5486 
5487 /* Return a tree for the difference of pointers OP0 and OP1.
5488    The resulting tree has type int.  If POINTER_SUBTRACT sanitization is
5489    enabled, assign to INSTRUMENT_EXPR call to libsanitizer.  */
5490 
5491 static tree
5492 pointer_diff (location_t loc, tree op0, tree op1, tree ptrtype,
5493 	      tsubst_flags_t complain, tree *instrument_expr)
5494 {
5495   tree result, inttype;
5496   tree restype = ptrdiff_type_node;
5497   tree target_type = TREE_TYPE (ptrtype);
5498 
5499   if (!complete_type_or_else (target_type, NULL_TREE))
5500     return error_mark_node;
5501 
5502   if (VOID_TYPE_P (target_type))
5503     {
5504       if (complain & tf_error)
5505 	permerror (loc, "ISO C++ forbids using pointer of "
5506 		   "type %<void *%> in subtraction");
5507       else
5508 	return error_mark_node;
5509     }
5510   if (TREE_CODE (target_type) == FUNCTION_TYPE)
5511     {
5512       if (complain & tf_error)
5513 	permerror (loc, "ISO C++ forbids using pointer to "
5514 		   "a function in subtraction");
5515       else
5516 	return error_mark_node;
5517     }
5518   if (TREE_CODE (target_type) == METHOD_TYPE)
5519     {
5520       if (complain & tf_error)
5521 	permerror (loc, "ISO C++ forbids using pointer to "
5522 		   "a method in subtraction");
5523       else
5524 	return error_mark_node;
5525     }
5526 
5527   /* Determine integer type result of the subtraction.  This will usually
5528      be the same as the result type (ptrdiff_t), but may need to be a wider
5529      type if pointers for the address space are wider than ptrdiff_t.  */
5530   if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
5531     inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
5532   else
5533     inttype = restype;
5534 
5535   if (!processing_template_decl
5536       && sanitize_flags_p (SANITIZE_POINTER_SUBTRACT))
5537     {
5538       op0 = save_expr (op0);
5539       op1 = save_expr (op1);
5540 
5541       tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_SUBTRACT);
5542       *instrument_expr = build_call_expr_loc (loc, tt, 2, op0, op1);
5543     }
5544 
5545   /* First do the subtraction, then build the divide operator
5546      and only convert at the very end.
5547      Do not do default conversions in case restype is a short type.  */
5548 
5549   /* POINTER_DIFF_EXPR requires a signed integer type of the same size as
5550      pointers.  If some platform cannot provide that, or has a larger
5551      ptrdiff_type to support differences larger than half the address
5552      space, cast the pointers to some larger integer type and do the
5553      computations in that type.  */
5554   if (TYPE_PRECISION (inttype) > TYPE_PRECISION (TREE_TYPE (op0)))
5555     op0 = cp_build_binary_op (loc,
5556 			      MINUS_EXPR,
5557 			      cp_convert (inttype, op0, complain),
5558 			      cp_convert (inttype, op1, complain),
5559 			      complain);
5560   else
5561     op0 = build2_loc (loc, POINTER_DIFF_EXPR, inttype, op0, op1);
5562 
5563   /* This generates an error if op1 is a pointer to an incomplete type.  */
5564   if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1))))
5565     {
5566       if (complain & tf_error)
5567 	error_at (loc, "invalid use of a pointer to an incomplete type in "
5568 		  "pointer arithmetic");
5569       else
5570 	return error_mark_node;
5571     }
5572 
5573   if (pointer_to_zero_sized_aggr_p (TREE_TYPE (op1)))
5574     {
5575       if (complain & tf_error)
5576 	error_at (loc, "arithmetic on pointer to an empty aggregate");
5577       else
5578 	return error_mark_node;
5579     }
5580 
5581   op1 = (TYPE_PTROB_P (ptrtype)
5582 	 ? size_in_bytes_loc (loc, target_type)
5583 	 : integer_one_node);
5584 
5585   /* Do the division.  */
5586 
5587   result = build2_loc (loc, EXACT_DIV_EXPR, inttype, op0,
5588 		       cp_convert (inttype, op1, complain));
5589   return cp_convert (restype, result, complain);
5590 }
5591 
5592 /* Construct and perhaps optimize a tree representation
5593    for a unary operation.  CODE, a tree_code, specifies the operation
5594    and XARG is the operand.  */
5595 
5596 tree
5597 build_x_unary_op (location_t loc, enum tree_code code, cp_expr xarg,
5598 		  tsubst_flags_t complain)
5599 {
5600   tree orig_expr = xarg;
5601   tree exp;
5602   int ptrmem = 0;
5603   tree overload = NULL_TREE;
5604 
5605   if (processing_template_decl)
5606     {
5607       if (type_dependent_expression_p (xarg))
5608 	return build_min_nt_loc (loc, code, xarg.get_value (), NULL_TREE);
5609 
5610       xarg = build_non_dependent_expr (xarg);
5611     }
5612 
5613   exp = NULL_TREE;
5614 
5615   /* [expr.unary.op] says:
5616 
5617        The address of an object of incomplete type can be taken.
5618 
5619      (And is just the ordinary address operator, not an overloaded
5620      "operator &".)  However, if the type is a template
5621      specialization, we must complete the type at this point so that
5622      an overloaded "operator &" will be available if required.  */
5623   if (code == ADDR_EXPR
5624       && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
5625       && ((CLASS_TYPE_P (TREE_TYPE (xarg))
5626 	   && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (xarg))))
5627 	  || (TREE_CODE (xarg) == OFFSET_REF)))
5628     /* Don't look for a function.  */;
5629   else
5630     exp = build_new_op (loc, code, LOOKUP_NORMAL, xarg, NULL_TREE,
5631 			NULL_TREE, &overload, complain);
5632 
5633   if (!exp && code == ADDR_EXPR)
5634     {
5635       if (is_overloaded_fn (xarg))
5636 	{
5637 	  tree fn = get_first_fn (xarg);
5638 	  if (DECL_CONSTRUCTOR_P (fn) || DECL_DESTRUCTOR_P (fn))
5639 	    {
5640 	      if (complain & tf_error)
5641 		error (DECL_CONSTRUCTOR_P (fn)
5642 		       ? G_("taking address of constructor %qD")
5643 		       : G_("taking address of destructor %qD"),
5644 		       fn);
5645 	      return error_mark_node;
5646 	    }
5647 	}
5648 
5649       /* A pointer to member-function can be formed only by saying
5650 	 &X::mf.  */
5651       if (!flag_ms_extensions && TREE_CODE (TREE_TYPE (xarg)) == METHOD_TYPE
5652 	  && (TREE_CODE (xarg) != OFFSET_REF || !PTRMEM_OK_P (xarg)))
5653 	{
5654 	  if (TREE_CODE (xarg) != OFFSET_REF
5655 	      || !TYPE_P (TREE_OPERAND (xarg, 0)))
5656 	    {
5657 	      if (complain & tf_error)
5658 		{
5659 		  error ("invalid use of %qE to form a "
5660 			 "pointer-to-member-function", xarg.get_value ());
5661 		  if (TREE_CODE (xarg) != OFFSET_REF)
5662 		    inform (input_location, "  a qualified-id is required");
5663 		}
5664 	      return error_mark_node;
5665 	    }
5666 	  else
5667 	    {
5668 	      if (complain & tf_error)
5669 		error ("parentheses around %qE cannot be used to form a"
5670 		       " pointer-to-member-function",
5671 		       xarg.get_value ());
5672 	      else
5673 		return error_mark_node;
5674 	      PTRMEM_OK_P (xarg) = 1;
5675 	    }
5676 	}
5677 
5678       if (TREE_CODE (xarg) == OFFSET_REF)
5679 	{
5680 	  ptrmem = PTRMEM_OK_P (xarg);
5681 
5682 	  if (!ptrmem && !flag_ms_extensions
5683 	      && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg, 1))) == METHOD_TYPE)
5684 	    {
5685 	      /* A single non-static member, make sure we don't allow a
5686 		 pointer-to-member.  */
5687 	      xarg = build2 (OFFSET_REF, TREE_TYPE (xarg),
5688 			     TREE_OPERAND (xarg, 0),
5689 			     ovl_make (TREE_OPERAND (xarg, 1)));
5690 	      PTRMEM_OK_P (xarg) = ptrmem;
5691 	    }
5692 	}
5693 
5694       exp = cp_build_addr_expr_strict (xarg, complain);
5695     }
5696 
5697   if (processing_template_decl && exp != error_mark_node)
5698     {
5699       if (overload != NULL_TREE)
5700 	return (build_min_non_dep_op_overload
5701 		(code, exp, overload, orig_expr, integer_zero_node));
5702 
5703       exp = build_min_non_dep (code, exp, orig_expr,
5704 			       /*For {PRE,POST}{INC,DEC}REMENT_EXPR*/NULL_TREE);
5705     }
5706   if (TREE_CODE (exp) == ADDR_EXPR)
5707     PTRMEM_OK_P (exp) = ptrmem;
5708   return exp;
5709 }
5710 
5711 /* Construct and perhaps optimize a tree representation
5712    for __builtin_addressof operation.  ARG specifies the operand.  */
5713 
5714 tree
5715 cp_build_addressof (location_t loc, tree arg, tsubst_flags_t complain)
5716 {
5717   tree orig_expr = arg;
5718 
5719   if (processing_template_decl)
5720     {
5721       if (type_dependent_expression_p (arg))
5722 	return build_min_nt_loc (loc, ADDRESSOF_EXPR, arg, NULL_TREE);
5723 
5724       arg = build_non_dependent_expr (arg);
5725     }
5726 
5727   tree exp = cp_build_addr_expr_strict (arg, complain);
5728 
5729   if (processing_template_decl && exp != error_mark_node)
5730     exp = build_min_non_dep (ADDRESSOF_EXPR, exp, orig_expr, NULL_TREE);
5731   return exp;
5732 }
5733 
5734 /* Like c_common_truthvalue_conversion, but handle pointer-to-member
5735    constants, where a null value is represented by an INTEGER_CST of
5736    -1.  */
5737 
5738 tree
5739 cp_truthvalue_conversion (tree expr)
5740 {
5741   tree type = TREE_TYPE (expr);
5742   if (TYPE_PTR_OR_PTRMEM_P (type)
5743       /* Avoid ICE on invalid use of non-static member function.  */
5744       || TREE_CODE (expr) == FUNCTION_DECL)
5745     return build_binary_op (input_location, NE_EXPR, expr, nullptr_node, true);
5746   else
5747     return c_common_truthvalue_conversion (input_location, expr);
5748 }
5749 
5750 /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR.  This
5751    is a low-level function; most callers should use maybe_convert_cond.  */
5752 
5753 tree
5754 condition_conversion (tree expr)
5755 {
5756   tree t;
5757   t = perform_implicit_conversion_flags (boolean_type_node, expr,
5758 					 tf_warning_or_error, LOOKUP_NORMAL);
5759   if (!processing_template_decl)
5760     t = fold_build_cleanup_point_expr (boolean_type_node, t);
5761   return t;
5762 }
5763 
5764 /* Returns the address of T.  This function will fold away
5765    ADDR_EXPR of INDIRECT_REF.  */
5766 
5767 tree
5768 build_address (tree t)
5769 {
5770   if (error_operand_p (t) || !cxx_mark_addressable (t))
5771     return error_mark_node;
5772   gcc_checking_assert (TREE_CODE (t) != CONSTRUCTOR
5773 		       || processing_template_decl);
5774   t = build_fold_addr_expr_loc (EXPR_LOCATION (t), t);
5775   if (TREE_CODE (t) != ADDR_EXPR)
5776     t = rvalue (t);
5777   return t;
5778 }
5779 
5780 /* Return a NOP_EXPR converting EXPR to TYPE.  */
5781 
5782 tree
5783 build_nop (tree type, tree expr)
5784 {
5785   if (type == error_mark_node || error_operand_p (expr))
5786     return expr;
5787   return build1_loc (EXPR_LOCATION (expr), NOP_EXPR, type, expr);
5788 }
5789 
5790 /* Take the address of ARG, whatever that means under C++ semantics.
5791    If STRICT_LVALUE is true, require an lvalue; otherwise, allow xvalues
5792    and class rvalues as well.
5793 
5794    Nothing should call this function directly; instead, callers should use
5795    cp_build_addr_expr or cp_build_addr_expr_strict.  */
5796 
5797 static tree
5798 cp_build_addr_expr_1 (tree arg, bool strict_lvalue, tsubst_flags_t complain)
5799 {
5800   tree argtype;
5801   tree val;
5802 
5803   if (!arg || error_operand_p (arg))
5804     return error_mark_node;
5805 
5806   arg = mark_lvalue_use (arg);
5807   if (error_operand_p (arg))
5808     return error_mark_node;
5809 
5810   argtype = lvalue_type (arg);
5811 
5812   gcc_assert (!(identifier_p (arg) && IDENTIFIER_ANY_OP_P (arg)));
5813 
5814   if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg)
5815       && !really_overloaded_fn (arg))
5816     {
5817       /* They're trying to take the address of a unique non-static
5818 	 member function.  This is ill-formed (except in MS-land),
5819 	 but let's try to DTRT.
5820 	 Note: We only handle unique functions here because we don't
5821 	 want to complain if there's a static overload; non-unique
5822 	 cases will be handled by instantiate_type.  But we need to
5823 	 handle this case here to allow casts on the resulting PMF.
5824 	 We could defer this in non-MS mode, but it's easier to give
5825 	 a useful error here.  */
5826 
5827       /* Inside constant member functions, the `this' pointer
5828 	 contains an extra const qualifier.  TYPE_MAIN_VARIANT
5829 	 is used here to remove this const from the diagnostics
5830 	 and the created OFFSET_REF.  */
5831       tree base = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg, 0)));
5832       tree fn = get_first_fn (TREE_OPERAND (arg, 1));
5833       if (!mark_used (fn, complain) && !(complain & tf_error))
5834 	return error_mark_node;
5835 
5836       if (! flag_ms_extensions)
5837 	{
5838 	  tree name = DECL_NAME (fn);
5839 	  if (!(complain & tf_error))
5840 	    return error_mark_node;
5841 	  else if (current_class_type
5842 		   && TREE_OPERAND (arg, 0) == current_class_ref)
5843 	    /* An expression like &memfn.  */
5844 	    permerror (input_location, "ISO C++ forbids taking the address of an unqualified"
5845 		       " or parenthesized non-static member function to form"
5846 		       " a pointer to member function.  Say %<&%T::%D%>",
5847 		       base, name);
5848 	  else
5849 	    permerror (input_location, "ISO C++ forbids taking the address of a bound member"
5850 		       " function to form a pointer to member function."
5851 		       "  Say %<&%T::%D%>",
5852 		       base, name);
5853 	}
5854       arg = build_offset_ref (base, fn, /*address_p=*/true, complain);
5855     }
5856 
5857   /* Uninstantiated types are all functions.  Taking the
5858      address of a function is a no-op, so just return the
5859      argument.  */
5860   if (type_unknown_p (arg))
5861     return build1 (ADDR_EXPR, unknown_type_node, arg);
5862 
5863   if (TREE_CODE (arg) == OFFSET_REF)
5864     /* We want a pointer to member; bypass all the code for actually taking
5865        the address of something.  */
5866     goto offset_ref;
5867 
5868   /* Anything not already handled and not a true memory reference
5869      is an error.  */
5870   if (TREE_CODE (argtype) != FUNCTION_TYPE
5871       && TREE_CODE (argtype) != METHOD_TYPE)
5872     {
5873       cp_lvalue_kind kind = lvalue_kind (arg);
5874       if (kind == clk_none)
5875 	{
5876 	  if (complain & tf_error)
5877 	    lvalue_error (input_location, lv_addressof);
5878 	  return error_mark_node;
5879 	}
5880       if (strict_lvalue && (kind & (clk_rvalueref|clk_class)))
5881 	{
5882 	  if (!(complain & tf_error))
5883 	    return error_mark_node;
5884 	  if (kind & clk_class)
5885 	    /* Make this a permerror because we used to accept it.  */
5886 	    permerror (input_location, "taking address of temporary");
5887 	  else
5888 	    error ("taking address of xvalue (rvalue reference)");
5889 	}
5890     }
5891 
5892   if (TREE_CODE (argtype) == REFERENCE_TYPE)
5893     {
5894       tree type = build_pointer_type (TREE_TYPE (argtype));
5895       arg = build1 (CONVERT_EXPR, type, arg);
5896       return arg;
5897     }
5898   else if (pedantic && DECL_MAIN_P (arg))
5899     {
5900       /* ARM $3.4 */
5901       /* Apparently a lot of autoconf scripts for C++ packages do this,
5902 	 so only complain if -Wpedantic.  */
5903       if (complain & (flag_pedantic_errors ? tf_error : tf_warning))
5904 	pedwarn (input_location, OPT_Wpedantic,
5905 		 "ISO C++ forbids taking address of function %<::main%>");
5906       else if (flag_pedantic_errors)
5907 	return error_mark_node;
5908     }
5909 
5910   /* Let &* cancel out to simplify resulting code.  */
5911   if (INDIRECT_REF_P (arg))
5912     {
5913       arg = TREE_OPERAND (arg, 0);
5914       if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE)
5915 	{
5916 	  tree type = build_pointer_type (TREE_TYPE (TREE_TYPE (arg)));
5917 	  arg = build1 (CONVERT_EXPR, type, arg);
5918 	}
5919       else
5920 	/* Don't let this be an lvalue.  */
5921 	arg = rvalue (arg);
5922       return arg;
5923     }
5924 
5925   /* Handle complex lvalues (when permitted)
5926      by reduction to simpler cases.  */
5927   val = unary_complex_lvalue (ADDR_EXPR, arg);
5928   if (val != 0)
5929     return val;
5930 
5931   switch (TREE_CODE (arg))
5932     {
5933     CASE_CONVERT:
5934     case FLOAT_EXPR:
5935     case FIX_TRUNC_EXPR:
5936       /* We should have handled this above in the lvalue_kind check.  */
5937       gcc_unreachable ();
5938       break;
5939 
5940     case BASELINK:
5941       arg = BASELINK_FUNCTIONS (arg);
5942       /* Fall through.  */
5943 
5944     case OVERLOAD:
5945       arg = OVL_FIRST (arg);
5946       break;
5947 
5948     case OFFSET_REF:
5949     offset_ref:
5950       /* Turn a reference to a non-static data member into a
5951 	 pointer-to-member.  */
5952       {
5953 	tree type;
5954 	tree t;
5955 
5956 	gcc_assert (PTRMEM_OK_P (arg));
5957 
5958 	t = TREE_OPERAND (arg, 1);
5959 	if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
5960 	  {
5961 	    if (complain & tf_error)
5962 	      error ("cannot create pointer to reference member %qD", t);
5963 	    return error_mark_node;
5964 	  }
5965 
5966 	type = build_ptrmem_type (context_for_name_lookup (t),
5967 				  TREE_TYPE (t));
5968 	t = make_ptrmem_cst (type, TREE_OPERAND (arg, 1));
5969 	return t;
5970       }
5971 
5972     default:
5973       break;
5974     }
5975 
5976   if (argtype != error_mark_node)
5977     argtype = build_pointer_type (argtype);
5978 
5979   if (bitfield_p (arg))
5980     {
5981       if (complain & tf_error)
5982 	error ("attempt to take address of bit-field");
5983       return error_mark_node;
5984     }
5985 
5986   /* In a template, we are processing a non-dependent expression
5987      so we can just form an ADDR_EXPR with the correct type.  */
5988   if (processing_template_decl || TREE_CODE (arg) != COMPONENT_REF)
5989     {
5990       if (TREE_CODE (arg) == FUNCTION_DECL
5991 	  && !mark_used (arg, complain) && !(complain & tf_error))
5992 	return error_mark_node;
5993       val = build_address (arg);
5994       if (TREE_CODE (arg) == OFFSET_REF)
5995 	PTRMEM_OK_P (val) = PTRMEM_OK_P (arg);
5996     }
5997   else if (BASELINK_P (TREE_OPERAND (arg, 1)))
5998     {
5999       tree fn = BASELINK_FUNCTIONS (TREE_OPERAND (arg, 1));
6000 
6001       /* We can only get here with a single static member
6002 	 function.  */
6003       gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
6004 		  && DECL_STATIC_FUNCTION_P (fn));
6005       if (!mark_used (fn, complain) && !(complain & tf_error))
6006 	return error_mark_node;
6007       val = build_address (fn);
6008       if (TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
6009 	/* Do not lose object's side effects.  */
6010 	val = build2 (COMPOUND_EXPR, TREE_TYPE (val),
6011 		      TREE_OPERAND (arg, 0), val);
6012     }
6013   else
6014     {
6015       tree object = TREE_OPERAND (arg, 0);
6016       tree field = TREE_OPERAND (arg, 1);
6017       gcc_assert (same_type_ignoring_top_level_qualifiers_p
6018 		  (TREE_TYPE (object), decl_type_context (field)));
6019       val = build_address (arg);
6020     }
6021 
6022   if (TYPE_PTR_P (argtype)
6023       && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
6024     {
6025       build_ptrmemfunc_type (argtype);
6026       val = build_ptrmemfunc (argtype, val, 0,
6027 			      /*c_cast_p=*/false,
6028 			      complain);
6029     }
6030 
6031   return val;
6032 }
6033 
6034 /* Take the address of ARG if it has one, even if it's an rvalue.  */
6035 
6036 tree
6037 cp_build_addr_expr (tree arg, tsubst_flags_t complain)
6038 {
6039   return cp_build_addr_expr_1 (arg, 0, complain);
6040 }
6041 
6042 /* Take the address of ARG, but only if it's an lvalue.  */
6043 
6044 static tree
6045 cp_build_addr_expr_strict (tree arg, tsubst_flags_t complain)
6046 {
6047   return cp_build_addr_expr_1 (arg, 1, complain);
6048 }
6049 
6050 /* C++: Must handle pointers to members.
6051 
6052    Perhaps type instantiation should be extended to handle conversion
6053    from aggregates to types we don't yet know we want?  (Or are those
6054    cases typically errors which should be reported?)
6055 
6056    NOCONVERT suppresses the default promotions (such as from short to int).  */
6057 
6058 tree
6059 cp_build_unary_op (enum tree_code code, tree xarg, bool noconvert,
6060                    tsubst_flags_t complain)
6061 {
6062   /* No default_conversion here.  It causes trouble for ADDR_EXPR.  */
6063   tree arg = xarg;
6064   location_t location = EXPR_LOC_OR_LOC (arg, input_location);
6065   tree argtype = 0;
6066   const char *errstring = NULL;
6067   tree val;
6068   const char *invalid_op_diag;
6069 
6070   if (!arg || error_operand_p (arg))
6071     return error_mark_node;
6072 
6073   if ((invalid_op_diag
6074        = targetm.invalid_unary_op ((code == UNARY_PLUS_EXPR
6075 				    ? CONVERT_EXPR
6076 				    : code),
6077 				   TREE_TYPE (xarg))))
6078     {
6079       if (complain & tf_error)
6080 	error (invalid_op_diag);
6081       return error_mark_node;
6082     }
6083 
6084   switch (code)
6085     {
6086     case UNARY_PLUS_EXPR:
6087     case NEGATE_EXPR:
6088       {
6089 	int flags = WANT_ARITH | WANT_ENUM;
6090 	/* Unary plus (but not unary minus) is allowed on pointers.  */
6091 	if (code == UNARY_PLUS_EXPR)
6092 	  flags |= WANT_POINTER;
6093 	arg = build_expr_type_conversion (flags, arg, true);
6094 	if (!arg)
6095 	  errstring = (code == NEGATE_EXPR
6096 		       ? _("wrong type argument to unary minus")
6097 		       : _("wrong type argument to unary plus"));
6098 	else
6099 	  {
6100 	    if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
6101 	      arg = cp_perform_integral_promotions (arg, complain);
6102 
6103 	    /* Make sure the result is not an lvalue: a unary plus or minus
6104 	       expression is always a rvalue.  */
6105 	    arg = rvalue (arg);
6106 	  }
6107       }
6108       break;
6109 
6110     case BIT_NOT_EXPR:
6111       if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
6112 	{
6113 	  code = CONJ_EXPR;
6114 	  if (!noconvert)
6115 	    {
6116 	      arg = cp_default_conversion (arg, complain);
6117 	      if (arg == error_mark_node)
6118 		return error_mark_node;
6119 	    }
6120 	}
6121       else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM
6122 						   | WANT_VECTOR_OR_COMPLEX,
6123 						   arg, true)))
6124 	errstring = _("wrong type argument to bit-complement");
6125       else if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
6126 	{
6127 	  /* Warn if the expression has boolean value.  */
6128 	  if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE
6129 	      && (complain & tf_warning)
6130 	      && warning_at (location, OPT_Wbool_operation,
6131 			     "%<~%> on an expression of type bool"))
6132 	    inform (location, "did you mean to use logical not (%<!%>)?");
6133 	  arg = cp_perform_integral_promotions (arg, complain);
6134 	}
6135       else if (!noconvert && VECTOR_TYPE_P (TREE_TYPE (arg)))
6136 	arg = mark_rvalue_use (arg);
6137       break;
6138 
6139     case ABS_EXPR:
6140       if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
6141 	errstring = _("wrong type argument to abs");
6142       else if (!noconvert)
6143 	{
6144 	  arg = cp_default_conversion (arg, complain);
6145 	  if (arg == error_mark_node)
6146 	    return error_mark_node;
6147 	}
6148       break;
6149 
6150     case CONJ_EXPR:
6151       /* Conjugating a real value is a no-op, but allow it anyway.  */
6152       if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
6153 	errstring = _("wrong type argument to conjugation");
6154       else if (!noconvert)
6155 	{
6156 	  arg = cp_default_conversion (arg, complain);
6157 	  if (arg == error_mark_node)
6158 	    return error_mark_node;
6159 	}
6160       break;
6161 
6162     case TRUTH_NOT_EXPR:
6163       if (VECTOR_TYPE_P (TREE_TYPE (arg)))
6164 	return cp_build_binary_op (input_location, EQ_EXPR, arg,
6165 				   build_zero_cst (TREE_TYPE (arg)), complain);
6166       arg = perform_implicit_conversion (boolean_type_node, arg,
6167 					 complain);
6168       val = invert_truthvalue_loc (input_location, arg);
6169       if (arg != error_mark_node)
6170 	return val;
6171       errstring = _("in argument to unary !");
6172       break;
6173 
6174     case NOP_EXPR:
6175       break;
6176 
6177     case REALPART_EXPR:
6178     case IMAGPART_EXPR:
6179       arg = build_real_imag_expr (input_location, code, arg);
6180       return arg;
6181 
6182     case PREINCREMENT_EXPR:
6183     case POSTINCREMENT_EXPR:
6184     case PREDECREMENT_EXPR:
6185     case POSTDECREMENT_EXPR:
6186       /* Handle complex lvalues (when permitted)
6187 	 by reduction to simpler cases.  */
6188 
6189       val = unary_complex_lvalue (code, arg);
6190       if (val != 0)
6191 	return val;
6192 
6193       arg = mark_lvalue_use (arg);
6194 
6195       /* Increment or decrement the real part of the value,
6196 	 and don't change the imaginary part.  */
6197       if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
6198 	{
6199 	  tree real, imag;
6200 
6201 	  arg = cp_stabilize_reference (arg);
6202 	  real = cp_build_unary_op (REALPART_EXPR, arg, true, complain);
6203 	  imag = cp_build_unary_op (IMAGPART_EXPR, arg, true, complain);
6204 	  real = cp_build_unary_op (code, real, true, complain);
6205 	  if (real == error_mark_node || imag == error_mark_node)
6206 	    return error_mark_node;
6207 	  return build2 (COMPLEX_EXPR, TREE_TYPE (arg),
6208 			 real, imag);
6209 	}
6210 
6211       /* Report invalid types.  */
6212 
6213       if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
6214 					      arg, true)))
6215 	{
6216 	  if (code == PREINCREMENT_EXPR)
6217 	    errstring = _("no pre-increment operator for type");
6218 	  else if (code == POSTINCREMENT_EXPR)
6219 	    errstring = _("no post-increment operator for type");
6220 	  else if (code == PREDECREMENT_EXPR)
6221 	    errstring = _("no pre-decrement operator for type");
6222 	  else
6223 	    errstring = _("no post-decrement operator for type");
6224 	  break;
6225 	}
6226       else if (arg == error_mark_node)
6227 	return error_mark_node;
6228 
6229       /* Report something read-only.  */
6230 
6231       if (CP_TYPE_CONST_P (TREE_TYPE (arg))
6232 	  || TREE_READONLY (arg))
6233         {
6234           if (complain & tf_error)
6235             cxx_readonly_error (arg, ((code == PREINCREMENT_EXPR
6236 				      || code == POSTINCREMENT_EXPR)
6237 				     ? lv_increment : lv_decrement));
6238           else
6239             return error_mark_node;
6240         }
6241 
6242       {
6243 	tree inc;
6244 	tree declared_type = unlowered_expr_type (arg);
6245 
6246 	argtype = TREE_TYPE (arg);
6247 
6248 	/* ARM $5.2.5 last annotation says this should be forbidden.  */
6249 	if (TREE_CODE (argtype) == ENUMERAL_TYPE)
6250           {
6251             if (complain & tf_error)
6252               permerror (input_location, (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
6253                          ? G_("ISO C++ forbids incrementing an enum")
6254                          : G_("ISO C++ forbids decrementing an enum"));
6255             else
6256               return error_mark_node;
6257           }
6258 
6259 	/* Compute the increment.  */
6260 
6261 	if (TYPE_PTR_P (argtype))
6262 	  {
6263 	    tree type = complete_type (TREE_TYPE (argtype));
6264 
6265 	    if (!COMPLETE_OR_VOID_TYPE_P (type))
6266               {
6267                 if (complain & tf_error)
6268                   error (((code == PREINCREMENT_EXPR
6269                            || code == POSTINCREMENT_EXPR))
6270                          ? G_("cannot increment a pointer to incomplete type %qT")
6271                          : G_("cannot decrement a pointer to incomplete type %qT"),
6272                          TREE_TYPE (argtype));
6273                 else
6274                   return error_mark_node;
6275               }
6276 	    else if (!TYPE_PTROB_P (argtype))
6277               {
6278                 if (complain & tf_error)
6279                   pedwarn (input_location, OPT_Wpointer_arith,
6280 			   (code == PREINCREMENT_EXPR
6281                               || code == POSTINCREMENT_EXPR)
6282 			   ? G_("ISO C++ forbids incrementing a pointer of type %qT")
6283 			   : G_("ISO C++ forbids decrementing a pointer of type %qT"),
6284 			   argtype);
6285                 else
6286                   return error_mark_node;
6287               }
6288 
6289 	    inc = cxx_sizeof_nowarn (TREE_TYPE (argtype));
6290 	  }
6291 	else
6292 	  inc = VECTOR_TYPE_P (argtype)
6293 	    ? build_one_cst (argtype)
6294 	    : integer_one_node;
6295 
6296 	inc = cp_convert (argtype, inc, complain);
6297 
6298 	/* If 'arg' is an Objective-C PROPERTY_REF expression, then we
6299 	   need to ask Objective-C to build the increment or decrement
6300 	   expression for it.  */
6301 	if (objc_is_property_ref (arg))
6302 	  return objc_build_incr_expr_for_property_ref (input_location, code,
6303 							arg, inc);
6304 
6305 	/* Complain about anything else that is not a true lvalue.  */
6306 	if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
6307 				    || code == POSTINCREMENT_EXPR)
6308 				   ? lv_increment : lv_decrement),
6309                              complain))
6310 	  return error_mark_node;
6311 
6312 	/* Forbid using -- or ++ in C++17 on `bool'.  */
6313 	if (TREE_CODE (declared_type) == BOOLEAN_TYPE)
6314 	  {
6315 	    if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
6316 	      {
6317                 if (complain & tf_error)
6318 		  error ("use of an operand of type %qT in %<operator--%> "
6319 			 "is forbidden", boolean_type_node);
6320 		return error_mark_node;
6321 	      }
6322 	    else
6323 	      {
6324 		if (cxx_dialect >= cxx17)
6325 		  {
6326 		    if (complain & tf_error)
6327 		      error ("use of an operand of type %qT in "
6328 			     "%<operator++%> is forbidden in C++17",
6329 			     boolean_type_node);
6330 		    return error_mark_node;
6331 		  }
6332 		/* Otherwise, [depr.incr.bool] says this is deprecated.  */
6333 		else if (!in_system_header_at (input_location))
6334 		  warning (OPT_Wdeprecated, "use of an operand of type %qT "
6335 			   "in %<operator++%> is deprecated",
6336 			   boolean_type_node);
6337 	      }
6338 	    val = boolean_increment (code, arg);
6339 	  }
6340 	else if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
6341 	  /* An rvalue has no cv-qualifiers.  */
6342 	  val = build2 (code, cv_unqualified (TREE_TYPE (arg)), arg, inc);
6343 	else
6344 	  val = build2 (code, TREE_TYPE (arg), arg, inc);
6345 
6346 	TREE_SIDE_EFFECTS (val) = 1;
6347 	return val;
6348       }
6349 
6350     case ADDR_EXPR:
6351       /* Note that this operation never does default_conversion
6352 	 regardless of NOCONVERT.  */
6353       return cp_build_addr_expr (arg, complain);
6354 
6355     default:
6356       break;
6357     }
6358 
6359   if (!errstring)
6360     {
6361       if (argtype == 0)
6362 	argtype = TREE_TYPE (arg);
6363       return build1 (code, argtype, arg);
6364     }
6365 
6366   if (complain & tf_error)
6367     error ("%s", errstring);
6368   return error_mark_node;
6369 }
6370 
6371 /* Hook for the c-common bits that build a unary op.  */
6372 tree
6373 build_unary_op (location_t /*location*/,
6374 		enum tree_code code, tree xarg, bool noconvert)
6375 {
6376   return cp_build_unary_op (code, xarg, noconvert, tf_warning_or_error);
6377 }
6378 
6379 /* Adjust LVALUE, an MODIFY_EXPR, PREINCREMENT_EXPR or PREDECREMENT_EXPR,
6380    so that it is a valid lvalue even for GENERIC by replacing
6381    (lhs = rhs) with ((lhs = rhs), lhs)
6382    (--lhs) with ((--lhs), lhs)
6383    (++lhs) with ((++lhs), lhs)
6384    and if lhs has side-effects, calling cp_stabilize_reference on it, so
6385    that it can be evaluated multiple times.  */
6386 
6387 tree
6388 genericize_compound_lvalue (tree lvalue)
6389 {
6390   if (TREE_SIDE_EFFECTS (TREE_OPERAND (lvalue, 0)))
6391     lvalue = build2 (TREE_CODE (lvalue), TREE_TYPE (lvalue),
6392 		     cp_stabilize_reference (TREE_OPERAND (lvalue, 0)),
6393 		     TREE_OPERAND (lvalue, 1));
6394   return build2 (COMPOUND_EXPR, TREE_TYPE (TREE_OPERAND (lvalue, 0)),
6395 		 lvalue, TREE_OPERAND (lvalue, 0));
6396 }
6397 
6398 /* Apply unary lvalue-demanding operator CODE to the expression ARG
6399    for certain kinds of expressions which are not really lvalues
6400    but which we can accept as lvalues.
6401 
6402    If ARG is not a kind of expression we can handle, return
6403    NULL_TREE.  */
6404 
6405 tree
6406 unary_complex_lvalue (enum tree_code code, tree arg)
6407 {
6408   /* Inside a template, making these kinds of adjustments is
6409      pointless; we are only concerned with the type of the
6410      expression.  */
6411   if (processing_template_decl)
6412     return NULL_TREE;
6413 
6414   /* Handle (a, b) used as an "lvalue".  */
6415   if (TREE_CODE (arg) == COMPOUND_EXPR)
6416     {
6417       tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 1), false,
6418                                             tf_warning_or_error);
6419       return build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
6420 		     TREE_OPERAND (arg, 0), real_result);
6421     }
6422 
6423   /* Handle (a ? b : c) used as an "lvalue".  */
6424   if (TREE_CODE (arg) == COND_EXPR
6425       || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
6426     return rationalize_conditional_expr (code, arg, tf_warning_or_error);
6427 
6428   /* Handle (a = b), (++a), and (--a) used as an "lvalue".  */
6429   if (TREE_CODE (arg) == MODIFY_EXPR
6430       || TREE_CODE (arg) == PREINCREMENT_EXPR
6431       || TREE_CODE (arg) == PREDECREMENT_EXPR)
6432     return unary_complex_lvalue (code, genericize_compound_lvalue (arg));
6433 
6434   if (code != ADDR_EXPR)
6435     return NULL_TREE;
6436 
6437   /* Handle (a = b) used as an "lvalue" for `&'.  */
6438   if (TREE_CODE (arg) == MODIFY_EXPR
6439       || TREE_CODE (arg) == INIT_EXPR)
6440     {
6441       tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 0), false,
6442                                             tf_warning_or_error);
6443       arg = build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
6444 		    arg, real_result);
6445       TREE_NO_WARNING (arg) = 1;
6446       return arg;
6447     }
6448 
6449   if (TREE_CODE (TREE_TYPE (arg)) == FUNCTION_TYPE
6450       || TREE_CODE (TREE_TYPE (arg)) == METHOD_TYPE
6451       || TREE_CODE (arg) == OFFSET_REF)
6452     return NULL_TREE;
6453 
6454   /* We permit compiler to make function calls returning
6455      objects of aggregate type look like lvalues.  */
6456   {
6457     tree targ = arg;
6458 
6459     if (TREE_CODE (targ) == SAVE_EXPR)
6460       targ = TREE_OPERAND (targ, 0);
6461 
6462     if (TREE_CODE (targ) == CALL_EXPR && MAYBE_CLASS_TYPE_P (TREE_TYPE (targ)))
6463       {
6464 	if (TREE_CODE (arg) == SAVE_EXPR)
6465 	  targ = arg;
6466 	else
6467 	  targ = build_cplus_new (TREE_TYPE (arg), arg, tf_warning_or_error);
6468 	return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
6469       }
6470 
6471     if (TREE_CODE (arg) == SAVE_EXPR && INDIRECT_REF_P (targ))
6472       return build3 (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
6473 		     TREE_OPERAND (targ, 0), current_function_decl, NULL);
6474   }
6475 
6476   /* Don't let anything else be handled specially.  */
6477   return NULL_TREE;
6478 }
6479 
6480 /* Mark EXP saying that we need to be able to take the
6481    address of it; it should not be allocated in a register.
6482    Value is true if successful.  ARRAY_REF_P is true if this
6483    is for ARRAY_REF construction - in that case we don't want
6484    to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
6485    it is fine to use ARRAY_REFs for vector subscripts on vector
6486    register variables.
6487 
6488    C++: we do not allow `current_class_ptr' to be addressable.  */
6489 
6490 bool
6491 cxx_mark_addressable (tree exp, bool array_ref_p)
6492 {
6493   tree x = exp;
6494 
6495   while (1)
6496     switch (TREE_CODE (x))
6497       {
6498       case VIEW_CONVERT_EXPR:
6499 	if (array_ref_p
6500 	    && TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
6501 	    && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x, 0))))
6502 	  return true;
6503 	/* FALLTHRU */
6504       case ADDR_EXPR:
6505       case COMPONENT_REF:
6506       case ARRAY_REF:
6507       case REALPART_EXPR:
6508       case IMAGPART_EXPR:
6509 	x = TREE_OPERAND (x, 0);
6510 	break;
6511 
6512       case PARM_DECL:
6513 	if (x == current_class_ptr)
6514 	  {
6515 	    error ("cannot take the address of %<this%>, which is an rvalue expression");
6516 	    TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later.  */
6517 	    return true;
6518 	  }
6519 	/* Fall through.  */
6520 
6521       case VAR_DECL:
6522 	/* Caller should not be trying to mark initialized
6523 	   constant fields addressable.  */
6524 	gcc_assert (DECL_LANG_SPECIFIC (x) == 0
6525 		    || DECL_IN_AGGR_P (x) == 0
6526 		    || TREE_STATIC (x)
6527 		    || DECL_EXTERNAL (x));
6528 	/* Fall through.  */
6529 
6530       case RESULT_DECL:
6531 	if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
6532 	    && !DECL_ARTIFICIAL (x))
6533 	  {
6534 	    if (VAR_P (x) && DECL_HARD_REGISTER (x))
6535 	      {
6536 		error
6537 		  ("address of explicit register variable %qD requested", x);
6538 		return false;
6539 	      }
6540 	    else if (extra_warnings)
6541 	      warning
6542 		(OPT_Wextra, "address requested for %qD, which is declared %<register%>", x);
6543 	  }
6544 	TREE_ADDRESSABLE (x) = 1;
6545 	return true;
6546 
6547       case CONST_DECL:
6548       case FUNCTION_DECL:
6549 	TREE_ADDRESSABLE (x) = 1;
6550 	return true;
6551 
6552       case CONSTRUCTOR:
6553 	TREE_ADDRESSABLE (x) = 1;
6554 	return true;
6555 
6556       case TARGET_EXPR:
6557 	TREE_ADDRESSABLE (x) = 1;
6558 	cxx_mark_addressable (TREE_OPERAND (x, 0));
6559 	return true;
6560 
6561       default:
6562 	return true;
6563     }
6564 }
6565 
6566 /* Build and return a conditional expression IFEXP ? OP1 : OP2.  */
6567 
6568 tree
6569 build_x_conditional_expr (location_t loc, tree ifexp, tree op1, tree op2,
6570                           tsubst_flags_t complain)
6571 {
6572   tree orig_ifexp = ifexp;
6573   tree orig_op1 = op1;
6574   tree orig_op2 = op2;
6575   tree expr;
6576 
6577   if (processing_template_decl)
6578     {
6579       /* The standard says that the expression is type-dependent if
6580 	 IFEXP is type-dependent, even though the eventual type of the
6581 	 expression doesn't dependent on IFEXP.  */
6582       if (type_dependent_expression_p (ifexp)
6583 	  /* As a GNU extension, the middle operand may be omitted.  */
6584 	  || (op1 && type_dependent_expression_p (op1))
6585 	  || type_dependent_expression_p (op2))
6586 	return build_min_nt_loc (loc, COND_EXPR, ifexp, op1, op2);
6587       ifexp = build_non_dependent_expr (ifexp);
6588       if (op1)
6589 	op1 = build_non_dependent_expr (op1);
6590       op2 = build_non_dependent_expr (op2);
6591     }
6592 
6593   expr = build_conditional_expr (loc, ifexp, op1, op2, complain);
6594   if (processing_template_decl && expr != error_mark_node)
6595     {
6596       tree min = build_min_non_dep (COND_EXPR, expr,
6597 				    orig_ifexp, orig_op1, orig_op2);
6598       expr = convert_from_reference (min);
6599     }
6600   return expr;
6601 }
6602 
6603 /* Given a list of expressions, return a compound expression
6604    that performs them all and returns the value of the last of them.  */
6605 
6606 tree
6607 build_x_compound_expr_from_list (tree list, expr_list_kind exp,
6608 				 tsubst_flags_t complain)
6609 {
6610   tree expr = TREE_VALUE (list);
6611 
6612   if (BRACE_ENCLOSED_INITIALIZER_P (expr)
6613       && !CONSTRUCTOR_IS_DIRECT_INIT (expr))
6614     {
6615       if (complain & tf_error)
6616 	pedwarn (EXPR_LOC_OR_LOC (expr, input_location), 0,
6617 		 "list-initializer for non-class type must not "
6618 		 "be parenthesized");
6619       else
6620 	return error_mark_node;
6621     }
6622 
6623   if (TREE_CHAIN (list))
6624     {
6625       if (complain & tf_error)
6626 	switch (exp)
6627 	  {
6628 	  case ELK_INIT:
6629 	    permerror (input_location, "expression list treated as compound "
6630 				       "expression in initializer");
6631 	    break;
6632 	  case ELK_MEM_INIT:
6633 	    permerror (input_location, "expression list treated as compound "
6634 				       "expression in mem-initializer");
6635 	    break;
6636 	  case ELK_FUNC_CAST:
6637 	    permerror (input_location, "expression list treated as compound "
6638 				       "expression in functional cast");
6639 	    break;
6640 	  default:
6641 	    gcc_unreachable ();
6642 	  }
6643       else
6644 	return error_mark_node;
6645 
6646       for (list = TREE_CHAIN (list); list; list = TREE_CHAIN (list))
6647 	expr = build_x_compound_expr (EXPR_LOCATION (TREE_VALUE (list)),
6648 				      expr, TREE_VALUE (list), complain);
6649     }
6650 
6651   return expr;
6652 }
6653 
6654 /* Like build_x_compound_expr_from_list, but using a VEC.  */
6655 
6656 tree
6657 build_x_compound_expr_from_vec (vec<tree, va_gc> *vec, const char *msg,
6658 				tsubst_flags_t complain)
6659 {
6660   if (vec_safe_is_empty (vec))
6661     return NULL_TREE;
6662   else if (vec->length () == 1)
6663     return (*vec)[0];
6664   else
6665     {
6666       tree expr;
6667       unsigned int ix;
6668       tree t;
6669 
6670       if (msg != NULL)
6671 	{
6672 	  if (complain & tf_error)
6673 	    permerror (input_location,
6674 		       "%s expression list treated as compound expression",
6675 		       msg);
6676 	  else
6677 	    return error_mark_node;
6678 	}
6679 
6680       expr = (*vec)[0];
6681       for (ix = 1; vec->iterate (ix, &t); ++ix)
6682 	expr = build_x_compound_expr (EXPR_LOCATION (t), expr,
6683 				      t, complain);
6684 
6685       return expr;
6686     }
6687 }
6688 
6689 /* Handle overloading of the ',' operator when needed.  */
6690 
6691 tree
6692 build_x_compound_expr (location_t loc, tree op1, tree op2,
6693 		       tsubst_flags_t complain)
6694 {
6695   tree result;
6696   tree orig_op1 = op1;
6697   tree orig_op2 = op2;
6698   tree overload = NULL_TREE;
6699 
6700   if (processing_template_decl)
6701     {
6702       if (type_dependent_expression_p (op1)
6703 	  || type_dependent_expression_p (op2))
6704 	return build_min_nt_loc (loc, COMPOUND_EXPR, op1, op2);
6705       op1 = build_non_dependent_expr (op1);
6706       op2 = build_non_dependent_expr (op2);
6707     }
6708 
6709   result = build_new_op (loc, COMPOUND_EXPR, LOOKUP_NORMAL, op1, op2,
6710 			 NULL_TREE, &overload, complain);
6711   if (!result)
6712     result = cp_build_compound_expr (op1, op2, complain);
6713 
6714   if (processing_template_decl && result != error_mark_node)
6715     {
6716       if (overload != NULL_TREE)
6717 	return (build_min_non_dep_op_overload
6718 		(COMPOUND_EXPR, result, overload, orig_op1, orig_op2));
6719 
6720       return build_min_non_dep (COMPOUND_EXPR, result, orig_op1, orig_op2);
6721     }
6722 
6723   return result;
6724 }
6725 
6726 /* Like cp_build_compound_expr, but for the c-common bits.  */
6727 
6728 tree
6729 build_compound_expr (location_t /*loc*/, tree lhs, tree rhs)
6730 {
6731   return cp_build_compound_expr (lhs, rhs, tf_warning_or_error);
6732 }
6733 
6734 /* Build a compound expression.  */
6735 
6736 tree
6737 cp_build_compound_expr (tree lhs, tree rhs, tsubst_flags_t complain)
6738 {
6739   lhs = convert_to_void (lhs, ICV_LEFT_OF_COMMA, complain);
6740 
6741   if (lhs == error_mark_node || rhs == error_mark_node)
6742     return error_mark_node;
6743 
6744   if (TREE_CODE (rhs) == TARGET_EXPR)
6745     {
6746       /* If the rhs is a TARGET_EXPR, then build the compound
6747 	 expression inside the target_expr's initializer. This
6748 	 helps the compiler to eliminate unnecessary temporaries.  */
6749       tree init = TREE_OPERAND (rhs, 1);
6750 
6751       init = build2 (COMPOUND_EXPR, TREE_TYPE (init), lhs, init);
6752       TREE_OPERAND (rhs, 1) = init;
6753 
6754       return rhs;
6755     }
6756 
6757   if (type_unknown_p (rhs))
6758     {
6759       if (complain & tf_error)
6760 	error ("no context to resolve type of %qE", rhs);
6761       return error_mark_node;
6762     }
6763 
6764   return build2 (COMPOUND_EXPR, TREE_TYPE (rhs), lhs, rhs);
6765 }
6766 
6767 /* Issue a diagnostic message if casting from SRC_TYPE to DEST_TYPE
6768    casts away constness.  CAST gives the type of cast.  Returns true
6769    if the cast is ill-formed, false if it is well-formed.
6770 
6771    ??? This function warns for casting away any qualifier not just
6772    const.  We would like to specify exactly what qualifiers are casted
6773    away.
6774 */
6775 
6776 static bool
6777 check_for_casting_away_constness (tree src_type, tree dest_type,
6778 				  enum tree_code cast, tsubst_flags_t complain)
6779 {
6780   /* C-style casts are allowed to cast away constness.  With
6781      WARN_CAST_QUAL, we still want to issue a warning.  */
6782   if (cast == CAST_EXPR && !warn_cast_qual)
6783     return false;
6784 
6785   if (!casts_away_constness (src_type, dest_type, complain))
6786     return false;
6787 
6788   switch (cast)
6789     {
6790     case CAST_EXPR:
6791       if (complain & tf_warning)
6792 	warning (OPT_Wcast_qual,
6793 		 "cast from type %qT to type %qT casts away qualifiers",
6794 		 src_type, dest_type);
6795       return false;
6796 
6797     case STATIC_CAST_EXPR:
6798       if (complain & tf_error)
6799 	error ("static_cast from type %qT to type %qT casts away qualifiers",
6800 	       src_type, dest_type);
6801       return true;
6802 
6803     case REINTERPRET_CAST_EXPR:
6804       if (complain & tf_error)
6805 	error ("reinterpret_cast from type %qT to type %qT casts away qualifiers",
6806 	       src_type, dest_type);
6807       return true;
6808 
6809     default:
6810       gcc_unreachable();
6811     }
6812 }
6813 
6814 /* Warns if the cast from expression EXPR to type TYPE is useless.  */
6815 void
6816 maybe_warn_about_useless_cast (tree type, tree expr, tsubst_flags_t complain)
6817 {
6818   if (warn_useless_cast
6819       && complain & tf_warning)
6820     {
6821       if ((TREE_CODE (type) == REFERENCE_TYPE
6822 	   && (TYPE_REF_IS_RVALUE (type)
6823 	       ? xvalue_p (expr) : lvalue_p (expr))
6824 	   && same_type_p (TREE_TYPE (expr), TREE_TYPE (type)))
6825 	  || same_type_p (TREE_TYPE (expr), type))
6826 	warning (OPT_Wuseless_cast, "useless cast to type %q#T", type);
6827     }
6828 }
6829 
6830 /* Warns if the cast ignores cv-qualifiers on TYPE.  */
6831 void
6832 maybe_warn_about_cast_ignoring_quals (tree type, tsubst_flags_t complain)
6833 {
6834   if (warn_ignored_qualifiers
6835       && complain & tf_warning
6836       && !CLASS_TYPE_P (type)
6837       && (cp_type_quals (type) & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)))
6838     {
6839       warning (OPT_Wignored_qualifiers, "type qualifiers ignored on cast "
6840 	       "result type");
6841     }
6842 }
6843 
6844 /* Convert EXPR (an expression with pointer-to-member type) to TYPE
6845    (another pointer-to-member type in the same hierarchy) and return
6846    the converted expression.  If ALLOW_INVERSE_P is permitted, a
6847    pointer-to-derived may be converted to pointer-to-base; otherwise,
6848    only the other direction is permitted.  If C_CAST_P is true, this
6849    conversion is taking place as part of a C-style cast.  */
6850 
6851 tree
6852 convert_ptrmem (tree type, tree expr, bool allow_inverse_p,
6853 		bool c_cast_p, tsubst_flags_t complain)
6854 {
6855   if (same_type_p (type, TREE_TYPE (expr)))
6856     return expr;
6857 
6858   if (TYPE_PTRDATAMEM_P (type))
6859     {
6860       tree delta;
6861 
6862       delta = get_delta_difference (TYPE_PTRMEM_CLASS_TYPE (TREE_TYPE (expr)),
6863 				    TYPE_PTRMEM_CLASS_TYPE (type),
6864 				    allow_inverse_p,
6865 				    c_cast_p, complain);
6866       if (delta == error_mark_node)
6867 	return error_mark_node;
6868 
6869       if (!integer_zerop (delta))
6870 	{
6871 	  tree cond, op1, op2;
6872 
6873 	  if (TREE_CODE (expr) == PTRMEM_CST)
6874 	    expr = cplus_expand_constant (expr);
6875 	  cond = cp_build_binary_op (input_location,
6876 				     EQ_EXPR,
6877 				     expr,
6878 				     build_int_cst (TREE_TYPE (expr), -1),
6879 				     complain);
6880 	  op1 = build_nop (ptrdiff_type_node, expr);
6881 	  op2 = cp_build_binary_op (input_location,
6882 				    PLUS_EXPR, op1, delta,
6883 				    complain);
6884 
6885 	  expr = fold_build3_loc (input_location,
6886 			      COND_EXPR, ptrdiff_type_node, cond, op1, op2);
6887 
6888 	}
6889 
6890       return build_nop (type, expr);
6891     }
6892   else
6893     return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr,
6894 			     allow_inverse_p, c_cast_p, complain);
6895 }
6896 
6897 /* Perform a static_cast from EXPR to TYPE.  When C_CAST_P is true,
6898    this static_cast is being attempted as one of the possible casts
6899    allowed by a C-style cast.  (In that case, accessibility of base
6900    classes is not considered, and it is OK to cast away
6901    constness.)  Return the result of the cast.  *VALID_P is set to
6902    indicate whether or not the cast was valid.  */
6903 
6904 static tree
6905 build_static_cast_1 (tree type, tree expr, bool c_cast_p,
6906 		     bool *valid_p, tsubst_flags_t complain)
6907 {
6908   tree intype;
6909   tree result;
6910   cp_lvalue_kind clk;
6911 
6912   /* Assume the cast is valid.  */
6913   *valid_p = true;
6914 
6915   intype = unlowered_expr_type (expr);
6916 
6917   /* Save casted types in the function's used types hash table.  */
6918   used_types_insert (type);
6919 
6920   /* A prvalue of non-class type is cv-unqualified.  */
6921   if (!CLASS_TYPE_P (type))
6922     type = cv_unqualified (type);
6923 
6924   /* [expr.static.cast]
6925 
6926      An lvalue of type "cv1 B", where B is a class type, can be cast
6927      to type "reference to cv2 D", where D is a class derived (clause
6928      _class.derived_) from B, if a valid standard conversion from
6929      "pointer to D" to "pointer to B" exists (_conv.ptr_), cv2 is the
6930      same cv-qualification as, or greater cv-qualification than, cv1,
6931      and B is not a virtual base class of D.  */
6932   /* We check this case before checking the validity of "TYPE t =
6933      EXPR;" below because for this case:
6934 
6935        struct B {};
6936        struct D : public B { D(const B&); };
6937        extern B& b;
6938        void f() { static_cast<const D&>(b); }
6939 
6940      we want to avoid constructing a new D.  The standard is not
6941      completely clear about this issue, but our interpretation is
6942      consistent with other compilers.  */
6943   if (TREE_CODE (type) == REFERENCE_TYPE
6944       && CLASS_TYPE_P (TREE_TYPE (type))
6945       && CLASS_TYPE_P (intype)
6946       && (TYPE_REF_IS_RVALUE (type) || lvalue_p (expr))
6947       && DERIVED_FROM_P (intype, TREE_TYPE (type))
6948       && can_convert (build_pointer_type (TYPE_MAIN_VARIANT (intype)),
6949 		      build_pointer_type (TYPE_MAIN_VARIANT
6950 					  (TREE_TYPE (type))),
6951 		      complain)
6952       && (c_cast_p
6953 	  || at_least_as_qualified_p (TREE_TYPE (type), intype)))
6954     {
6955       tree base;
6956 
6957       if (processing_template_decl)
6958 	return expr;
6959 
6960       /* There is a standard conversion from "D*" to "B*" even if "B"
6961 	 is ambiguous or inaccessible.  If this is really a
6962 	 static_cast, then we check both for inaccessibility and
6963 	 ambiguity.  However, if this is a static_cast being performed
6964 	 because the user wrote a C-style cast, then accessibility is
6965 	 not considered.  */
6966       base = lookup_base (TREE_TYPE (type), intype,
6967 			  c_cast_p ? ba_unique : ba_check,
6968 			  NULL, complain);
6969       expr = build_address (expr);
6970 
6971       if (sanitize_flags_p (SANITIZE_VPTR))
6972 	{
6973 	  tree ubsan_check
6974 	    = cp_ubsan_maybe_instrument_downcast (input_location, type,
6975 						  intype, expr);
6976 	  if (ubsan_check)
6977 	    expr = ubsan_check;
6978 	}
6979 
6980       /* Convert from "B*" to "D*".  This function will check that "B"
6981 	 is not a virtual base of "D".  Even if we don't have a guarantee
6982 	 that expr is NULL, if the static_cast is to a reference type,
6983 	 it is UB if it would be NULL, so omit the non-NULL check.  */
6984       expr = build_base_path (MINUS_EXPR, expr, base,
6985 			      /*nonnull=*/flag_delete_null_pointer_checks,
6986 			      complain);
6987 
6988       /* Convert the pointer to a reference -- but then remember that
6989 	 there are no expressions with reference type in C++.
6990 
6991          We call rvalue so that there's an actual tree code
6992          (NON_LVALUE_EXPR) for the static_cast; otherwise, if the operand
6993          is a variable with the same type, the conversion would get folded
6994          away, leaving just the variable and causing lvalue_kind to give
6995          the wrong answer.  */
6996       expr = cp_fold_convert (type, expr);
6997 
6998       /* When -fsanitize=null, make sure to diagnose reference binding to
6999 	 NULL even when the reference is converted to pointer later on.  */
7000       if (sanitize_flags_p (SANITIZE_NULL)
7001 	  && TREE_CODE (expr) == COND_EXPR
7002 	  && TREE_OPERAND (expr, 2)
7003 	  && TREE_CODE (TREE_OPERAND (expr, 2)) == INTEGER_CST
7004 	  && TREE_TYPE (TREE_OPERAND (expr, 2)) == type)
7005 	ubsan_maybe_instrument_reference (&TREE_OPERAND (expr, 2));
7006 
7007       return convert_from_reference (rvalue (expr));
7008     }
7009 
7010   /* "A glvalue of type cv1 T1 can be cast to type rvalue reference to
7011      cv2 T2 if cv2 T2 is reference-compatible with cv1 T1 (8.5.3)."  */
7012   if (TREE_CODE (type) == REFERENCE_TYPE
7013       && TYPE_REF_IS_RVALUE (type)
7014       && (clk = real_lvalue_p (expr))
7015       && reference_related_p (TREE_TYPE (type), intype)
7016       && (c_cast_p || at_least_as_qualified_p (TREE_TYPE (type), intype)))
7017     {
7018       if (processing_template_decl)
7019 	return expr;
7020       if (clk == clk_ordinary)
7021 	{
7022 	  /* Handle the (non-bit-field) lvalue case here by casting to
7023 	     lvalue reference and then changing it to an rvalue reference.
7024 	     Casting an xvalue to rvalue reference will be handled by the
7025 	     main code path.  */
7026 	  tree lref = cp_build_reference_type (TREE_TYPE (type), false);
7027 	  result = (perform_direct_initialization_if_possible
7028 		    (lref, expr, c_cast_p, complain));
7029 	  result = build1 (NON_LVALUE_EXPR, type, result);
7030 	  return convert_from_reference (result);
7031 	}
7032       else
7033 	/* For a bit-field or packed field, bind to a temporary.  */
7034 	expr = rvalue (expr);
7035     }
7036 
7037   /* Resolve overloaded address here rather than once in
7038      implicit_conversion and again in the inverse code below.  */
7039   if (TYPE_PTRMEMFUNC_P (type) && type_unknown_p (expr))
7040     {
7041       expr = instantiate_type (type, expr, complain);
7042       intype = TREE_TYPE (expr);
7043     }
7044 
7045   /* [expr.static.cast]
7046 
7047      Any expression can be explicitly converted to type cv void.  */
7048   if (VOID_TYPE_P (type))
7049     return convert_to_void (expr, ICV_CAST, complain);
7050 
7051   /* [class.abstract]
7052      An abstract class shall not be used ... as the type of an explicit
7053      conversion.  */
7054   if (abstract_virtuals_error_sfinae (ACU_CAST, type, complain))
7055     return error_mark_node;
7056 
7057   /* [expr.static.cast]
7058 
7059      An expression e can be explicitly converted to a type T using a
7060      static_cast of the form static_cast<T>(e) if the declaration T
7061      t(e);" is well-formed, for some invented temporary variable
7062      t.  */
7063   result = perform_direct_initialization_if_possible (type, expr,
7064 						      c_cast_p, complain);
7065   if (result)
7066     {
7067       if (processing_template_decl)
7068 	return expr;
7069 
7070       result = convert_from_reference (result);
7071 
7072       /* [expr.static.cast]
7073 
7074 	 If T is a reference type, the result is an lvalue; otherwise,
7075 	 the result is an rvalue.  */
7076       if (TREE_CODE (type) != REFERENCE_TYPE)
7077 	{
7078 	  result = rvalue (result);
7079 
7080 	  if (result == expr && SCALAR_TYPE_P (type))
7081 	    /* Leave some record of the cast.  */
7082 	    result = build_nop (type, expr);
7083 	}
7084       return result;
7085     }
7086 
7087   /* [expr.static.cast]
7088 
7089      The inverse of any standard conversion sequence (clause _conv_),
7090      other than the lvalue-to-rvalue (_conv.lval_), array-to-pointer
7091      (_conv.array_), function-to-pointer (_conv.func_), and boolean
7092      (_conv.bool_) conversions, can be performed explicitly using
7093      static_cast subject to the restriction that the explicit
7094      conversion does not cast away constness (_expr.const.cast_), and
7095      the following additional rules for specific cases:  */
7096   /* For reference, the conversions not excluded are: integral
7097      promotions, floating point promotion, integral conversions,
7098      floating point conversions, floating-integral conversions,
7099      pointer conversions, and pointer to member conversions.  */
7100   /* DR 128
7101 
7102      A value of integral _or enumeration_ type can be explicitly
7103      converted to an enumeration type.  */
7104   /* The effect of all that is that any conversion between any two
7105      types which are integral, floating, or enumeration types can be
7106      performed.  */
7107   if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
7108        || SCALAR_FLOAT_TYPE_P (type))
7109       && (INTEGRAL_OR_ENUMERATION_TYPE_P (intype)
7110 	  || SCALAR_FLOAT_TYPE_P (intype)))
7111     {
7112       if (processing_template_decl)
7113 	return expr;
7114       return ocp_convert (type, expr, CONV_C_CAST, LOOKUP_NORMAL, complain);
7115     }
7116 
7117   if (TYPE_PTR_P (type) && TYPE_PTR_P (intype)
7118       && CLASS_TYPE_P (TREE_TYPE (type))
7119       && CLASS_TYPE_P (TREE_TYPE (intype))
7120       && can_convert (build_pointer_type (TYPE_MAIN_VARIANT
7121 					  (TREE_TYPE (intype))),
7122 		      build_pointer_type (TYPE_MAIN_VARIANT
7123 					  (TREE_TYPE (type))),
7124 		      complain))
7125     {
7126       tree base;
7127 
7128       if (processing_template_decl)
7129 	return expr;
7130 
7131       if (!c_cast_p
7132 	  && check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR,
7133 					       complain))
7134 	return error_mark_node;
7135       base = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
7136 			  c_cast_p ? ba_unique : ba_check,
7137 			  NULL, complain);
7138       expr = build_base_path (MINUS_EXPR, expr, base, /*nonnull=*/false,
7139 			      complain);
7140 
7141       if (sanitize_flags_p (SANITIZE_VPTR))
7142 	{
7143 	  tree ubsan_check
7144 	    = cp_ubsan_maybe_instrument_downcast (input_location, type,
7145 						  intype, expr);
7146 	  if (ubsan_check)
7147 	    expr = ubsan_check;
7148 	}
7149 
7150       return cp_fold_convert (type, expr);
7151     }
7152 
7153   if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
7154       || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
7155     {
7156       tree c1;
7157       tree c2;
7158       tree t1;
7159       tree t2;
7160 
7161       c1 = TYPE_PTRMEM_CLASS_TYPE (intype);
7162       c2 = TYPE_PTRMEM_CLASS_TYPE (type);
7163 
7164       if (TYPE_PTRDATAMEM_P (type))
7165 	{
7166 	  t1 = (build_ptrmem_type
7167 		(c1,
7168 		 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (intype))));
7169 	  t2 = (build_ptrmem_type
7170 		(c2,
7171 		 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
7172 	}
7173       else
7174 	{
7175 	  t1 = intype;
7176 	  t2 = type;
7177 	}
7178       if (can_convert (t1, t2, complain) || can_convert (t2, t1, complain))
7179 	{
7180 	  if (!c_cast_p
7181 	      && check_for_casting_away_constness (intype, type,
7182 						   STATIC_CAST_EXPR,
7183 						   complain))
7184 	    return error_mark_node;
7185 	  if (processing_template_decl)
7186 	    return expr;
7187 	  return convert_ptrmem (type, expr, /*allow_inverse_p=*/1,
7188 				 c_cast_p, complain);
7189 	}
7190     }
7191 
7192   /* [expr.static.cast]
7193 
7194      An rvalue of type "pointer to cv void" can be explicitly
7195      converted to a pointer to object type.  A value of type pointer
7196      to object converted to "pointer to cv void" and back to the
7197      original pointer type will have its original value.  */
7198   if (TYPE_PTR_P (intype)
7199       && VOID_TYPE_P (TREE_TYPE (intype))
7200       && TYPE_PTROB_P (type))
7201     {
7202       if (!c_cast_p
7203 	  && check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR,
7204 					       complain))
7205 	return error_mark_node;
7206       if (processing_template_decl)
7207 	return expr;
7208       return build_nop (type, expr);
7209     }
7210 
7211   *valid_p = false;
7212   return error_mark_node;
7213 }
7214 
7215 /* Return an expression representing static_cast<TYPE>(EXPR).  */
7216 
7217 tree
7218 build_static_cast (tree type, tree oexpr, tsubst_flags_t complain)
7219 {
7220   tree expr = oexpr;
7221   tree result;
7222   bool valid_p;
7223 
7224   if (type == error_mark_node || expr == error_mark_node)
7225     return error_mark_node;
7226 
7227   bool dependent = (dependent_type_p (type)
7228 		    || type_dependent_expression_p (expr));
7229   if (dependent)
7230     {
7231     tmpl:
7232       expr = build_min (STATIC_CAST_EXPR, type, oexpr);
7233       /* We don't know if it will or will not have side effects.  */
7234       TREE_SIDE_EFFECTS (expr) = 1;
7235       return convert_from_reference (expr);
7236     }
7237   else if (processing_template_decl)
7238     expr = build_non_dependent_expr (expr);
7239 
7240   /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7241      Strip such NOP_EXPRs if VALUE is being used in non-lvalue context.  */
7242   if (TREE_CODE (type) != REFERENCE_TYPE
7243       && TREE_CODE (expr) == NOP_EXPR
7244       && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
7245     expr = TREE_OPERAND (expr, 0);
7246 
7247   result = build_static_cast_1 (type, expr, /*c_cast_p=*/false, &valid_p,
7248                                 complain);
7249   if (valid_p)
7250     {
7251       if (result != error_mark_node)
7252 	{
7253 	  maybe_warn_about_useless_cast (type, expr, complain);
7254 	  maybe_warn_about_cast_ignoring_quals (type, complain);
7255 	}
7256       if (processing_template_decl)
7257 	goto tmpl;
7258       return result;
7259     }
7260 
7261   if (complain & tf_error)
7262     error ("invalid static_cast from type %qT to type %qT",
7263            TREE_TYPE (expr), type);
7264   return error_mark_node;
7265 }
7266 
7267 /* EXPR is an expression with member function or pointer-to-member
7268    function type.  TYPE is a pointer type.  Converting EXPR to TYPE is
7269    not permitted by ISO C++, but we accept it in some modes.  If we
7270    are not in one of those modes, issue a diagnostic.  Return the
7271    converted expression.  */
7272 
7273 tree
7274 convert_member_func_to_ptr (tree type, tree expr, tsubst_flags_t complain)
7275 {
7276   tree intype;
7277   tree decl;
7278 
7279   intype = TREE_TYPE (expr);
7280   gcc_assert (TYPE_PTRMEMFUNC_P (intype)
7281 	      || TREE_CODE (intype) == METHOD_TYPE);
7282 
7283   if (!(complain & tf_warning_or_error))
7284     return error_mark_node;
7285 
7286   if (pedantic || warn_pmf2ptr)
7287     pedwarn (input_location, pedantic ? OPT_Wpedantic : OPT_Wpmf_conversions,
7288 	     "converting from %qH to %qI", intype, type);
7289 
7290   if (TREE_CODE (intype) == METHOD_TYPE)
7291     expr = build_addr_func (expr, complain);
7292   else if (TREE_CODE (expr) == PTRMEM_CST)
7293     expr = build_address (PTRMEM_CST_MEMBER (expr));
7294   else
7295     {
7296       decl = maybe_dummy_object (TYPE_PTRMEM_CLASS_TYPE (intype), 0);
7297       decl = build_address (decl);
7298       expr = get_member_function_from_ptrfunc (&decl, expr, complain);
7299     }
7300 
7301   if (expr == error_mark_node)
7302     return error_mark_node;
7303 
7304   return build_nop (type, expr);
7305 }
7306 
7307 /* Build a NOP_EXPR to TYPE, but mark it as a reinterpret_cast so that
7308    constexpr evaluation knows to reject it.  */
7309 
7310 static tree
7311 build_nop_reinterpret (tree type, tree expr)
7312 {
7313   tree ret = build_nop (type, expr);
7314   if (ret != expr)
7315     REINTERPRET_CAST_P (ret) = true;
7316   return ret;
7317 }
7318 
7319 /* Return a representation for a reinterpret_cast from EXPR to TYPE.
7320    If C_CAST_P is true, this reinterpret cast is being done as part of
7321    a C-style cast.  If VALID_P is non-NULL, *VALID_P is set to
7322    indicate whether or not reinterpret_cast was valid.  */
7323 
7324 static tree
7325 build_reinterpret_cast_1 (tree type, tree expr, bool c_cast_p,
7326 			  bool *valid_p, tsubst_flags_t complain)
7327 {
7328   tree intype;
7329 
7330   /* Assume the cast is invalid.  */
7331   if (valid_p)
7332     *valid_p = true;
7333 
7334   if (type == error_mark_node || error_operand_p (expr))
7335     return error_mark_node;
7336 
7337   intype = TREE_TYPE (expr);
7338 
7339   /* Save casted types in the function's used types hash table.  */
7340   used_types_insert (type);
7341 
7342   /* A prvalue of non-class type is cv-unqualified.  */
7343   if (!CLASS_TYPE_P (type))
7344     type = cv_unqualified (type);
7345 
7346   /* [expr.reinterpret.cast]
7347      An lvalue expression of type T1 can be cast to the type
7348      "reference to T2" if an expression of type "pointer to T1" can be
7349      explicitly converted to the type "pointer to T2" using a
7350      reinterpret_cast.  */
7351   if (TREE_CODE (type) == REFERENCE_TYPE)
7352     {
7353       if (! lvalue_p (expr))
7354 	{
7355           if (complain & tf_error)
7356             error ("invalid cast of an rvalue expression of type "
7357                    "%qT to type %qT",
7358                    intype, type);
7359 	  return error_mark_node;
7360 	}
7361 
7362       /* Warn about a reinterpret_cast from "A*" to "B&" if "A" and
7363 	 "B" are related class types; the reinterpret_cast does not
7364 	 adjust the pointer.  */
7365       if (TYPE_PTR_P (intype)
7366           && (complain & tf_warning)
7367 	  && (comptypes (TREE_TYPE (intype), TREE_TYPE (type),
7368 			 COMPARE_BASE | COMPARE_DERIVED)))
7369 	warning (0, "casting %qT to %qT does not dereference pointer",
7370 		 intype, type);
7371 
7372       expr = cp_build_addr_expr (expr, complain);
7373 
7374       if (warn_strict_aliasing > 2)
7375 	strict_aliasing_warning (EXPR_LOCATION (expr), type, expr);
7376 
7377       if (expr != error_mark_node)
7378 	expr = build_reinterpret_cast_1
7379 	  (build_pointer_type (TREE_TYPE (type)), expr, c_cast_p,
7380 	   valid_p, complain);
7381       if (expr != error_mark_node)
7382 	/* cp_build_indirect_ref isn't right for rvalue refs.  */
7383 	expr = convert_from_reference (fold_convert (type, expr));
7384       return expr;
7385     }
7386 
7387   /* As a G++ extension, we consider conversions from member
7388      functions, and pointers to member functions to
7389      pointer-to-function and pointer-to-void types.  If
7390      -Wno-pmf-conversions has not been specified,
7391      convert_member_func_to_ptr will issue an error message.  */
7392   if ((TYPE_PTRMEMFUNC_P (intype)
7393        || TREE_CODE (intype) == METHOD_TYPE)
7394       && TYPE_PTR_P (type)
7395       && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
7396 	  || VOID_TYPE_P (TREE_TYPE (type))))
7397     return convert_member_func_to_ptr (type, expr, complain);
7398 
7399   /* If the cast is not to a reference type, the lvalue-to-rvalue,
7400      array-to-pointer, and function-to-pointer conversions are
7401      performed.  */
7402   expr = decay_conversion (expr, complain);
7403 
7404   /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7405      Strip such NOP_EXPRs if VALUE is being used in non-lvalue context.  */
7406   if (TREE_CODE (expr) == NOP_EXPR
7407       && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
7408     expr = TREE_OPERAND (expr, 0);
7409 
7410   if (error_operand_p (expr))
7411     return error_mark_node;
7412 
7413   intype = TREE_TYPE (expr);
7414 
7415   /* [expr.reinterpret.cast]
7416      A pointer can be converted to any integral type large enough to
7417      hold it. ... A value of type std::nullptr_t can be converted to
7418      an integral type; the conversion has the same meaning and
7419      validity as a conversion of (void*)0 to the integral type.  */
7420   if (CP_INTEGRAL_TYPE_P (type)
7421       && (TYPE_PTR_P (intype) || NULLPTR_TYPE_P (intype)))
7422     {
7423       if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
7424         {
7425           if (complain & tf_error)
7426             permerror (input_location, "cast from %qH to %qI loses precision",
7427                        intype, type);
7428           else
7429             return error_mark_node;
7430         }
7431       if (NULLPTR_TYPE_P (intype))
7432         return build_int_cst (type, 0);
7433     }
7434   /* [expr.reinterpret.cast]
7435      A value of integral or enumeration type can be explicitly
7436      converted to a pointer.  */
7437   else if (TYPE_PTR_P (type) && INTEGRAL_OR_ENUMERATION_TYPE_P (intype))
7438     /* OK */
7439     ;
7440   else if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
7441 	    || TYPE_PTR_OR_PTRMEM_P (type))
7442 	   && same_type_p (type, intype))
7443     /* DR 799 */
7444     return rvalue (expr);
7445   else if (TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
7446     {
7447       if ((complain & tf_warning)
7448 	  && !cxx_safe_function_type_cast_p (TREE_TYPE (type),
7449 					     TREE_TYPE (intype)))
7450 	warning (OPT_Wcast_function_type,
7451 		 "cast between incompatible function types"
7452 		 " from %qH to %qI", intype, type);
7453       return build_nop_reinterpret (type, expr);
7454     }
7455   else if (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype))
7456     {
7457       if ((complain & tf_warning)
7458 	  && !cxx_safe_function_type_cast_p
7459 		(TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (type)),
7460 		 TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (intype))))
7461 	warning (OPT_Wcast_function_type,
7462 		 "cast between incompatible pointer to member types"
7463 		 " from %qH to %qI", intype, type);
7464       return build_nop_reinterpret (type, expr);
7465     }
7466   else if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
7467 	   || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
7468     {
7469       if (!c_cast_p
7470 	  && check_for_casting_away_constness (intype, type,
7471 					       REINTERPRET_CAST_EXPR,
7472 					       complain))
7473 	return error_mark_node;
7474       /* Warn about possible alignment problems.  */
7475       if ((STRICT_ALIGNMENT || warn_cast_align == 2)
7476 	  && (complain & tf_warning)
7477 	  && !VOID_TYPE_P (type)
7478 	  && TREE_CODE (TREE_TYPE (intype)) != FUNCTION_TYPE
7479 	  && COMPLETE_TYPE_P (TREE_TYPE (type))
7480 	  && COMPLETE_TYPE_P (TREE_TYPE (intype))
7481 	  && min_align_of_type (TREE_TYPE (type))
7482 	     > min_align_of_type (TREE_TYPE (intype)))
7483 	warning (OPT_Wcast_align, "cast from %qH to %qI "
7484 		 "increases required alignment of target type", intype, type);
7485 
7486       if (warn_strict_aliasing <= 2)
7487 	/* strict_aliasing_warning STRIP_NOPs its expr.  */
7488 	strict_aliasing_warning (EXPR_LOCATION (expr), type, expr);
7489 
7490       return build_nop_reinterpret (type, expr);
7491     }
7492   else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
7493 	   || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
7494     {
7495       if (complain & tf_warning)
7496 	/* C++11 5.2.10 p8 says that "Converting a function pointer to an
7497 	   object pointer type or vice versa is conditionally-supported."  */
7498 	warning (OPT_Wconditionally_supported,
7499 		 "casting between pointer-to-function and pointer-to-object "
7500 		 "is conditionally-supported");
7501       return build_nop_reinterpret (type, expr);
7502     }
7503   else if (VECTOR_TYPE_P (type))
7504     return convert_to_vector (type, expr);
7505   else if (VECTOR_TYPE_P (intype)
7506 	   && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
7507     return convert_to_integer_nofold (type, expr);
7508   else
7509     {
7510       if (valid_p)
7511 	*valid_p = false;
7512       if (complain & tf_error)
7513         error ("invalid cast from type %qT to type %qT", intype, type);
7514       return error_mark_node;
7515     }
7516 
7517   expr = cp_convert (type, expr, complain);
7518   if (TREE_CODE (expr) == NOP_EXPR)
7519     /* Mark any nop_expr that created as a reintepret_cast.  */
7520     REINTERPRET_CAST_P (expr) = true;
7521   return expr;
7522 }
7523 
7524 tree
7525 build_reinterpret_cast (tree type, tree expr, tsubst_flags_t complain)
7526 {
7527   tree r;
7528 
7529   if (type == error_mark_node || expr == error_mark_node)
7530     return error_mark_node;
7531 
7532   if (processing_template_decl)
7533     {
7534       tree t = build_min (REINTERPRET_CAST_EXPR, type, expr);
7535 
7536       if (!TREE_SIDE_EFFECTS (t)
7537 	  && type_dependent_expression_p (expr))
7538 	/* There might turn out to be side effects inside expr.  */
7539 	TREE_SIDE_EFFECTS (t) = 1;
7540       return convert_from_reference (t);
7541     }
7542 
7543   r = build_reinterpret_cast_1 (type, expr, /*c_cast_p=*/false,
7544 				/*valid_p=*/NULL, complain);
7545   if (r != error_mark_node)
7546     {
7547       maybe_warn_about_useless_cast (type, expr, complain);
7548       maybe_warn_about_cast_ignoring_quals (type, complain);
7549     }
7550   return r;
7551 }
7552 
7553 /* Perform a const_cast from EXPR to TYPE.  If the cast is valid,
7554    return an appropriate expression.  Otherwise, return
7555    error_mark_node.  If the cast is not valid, and COMPLAIN is true,
7556    then a diagnostic will be issued.  If VALID_P is non-NULL, we are
7557    performing a C-style cast, its value upon return will indicate
7558    whether or not the conversion succeeded.  */
7559 
7560 static tree
7561 build_const_cast_1 (tree dst_type, tree expr, tsubst_flags_t complain,
7562 		    bool *valid_p)
7563 {
7564   tree src_type;
7565   tree reference_type;
7566 
7567   /* Callers are responsible for handling error_mark_node as a
7568      destination type.  */
7569   gcc_assert (dst_type != error_mark_node);
7570   /* In a template, callers should be building syntactic
7571      representations of casts, not using this machinery.  */
7572   gcc_assert (!processing_template_decl);
7573 
7574   /* Assume the conversion is invalid.  */
7575   if (valid_p)
7576     *valid_p = false;
7577 
7578   if (!POINTER_TYPE_P (dst_type) && !TYPE_PTRDATAMEM_P (dst_type))
7579     {
7580       if (complain & tf_error)
7581 	error ("invalid use of const_cast with type %qT, "
7582 	       "which is not a pointer, "
7583 	       "reference, nor a pointer-to-data-member type", dst_type);
7584       return error_mark_node;
7585     }
7586 
7587   if (TREE_CODE (TREE_TYPE (dst_type)) == FUNCTION_TYPE)
7588     {
7589       if (complain & tf_error)
7590 	error ("invalid use of const_cast with type %qT, which is a pointer "
7591 	       "or reference to a function type", dst_type);
7592       return error_mark_node;
7593     }
7594 
7595   /* A prvalue of non-class type is cv-unqualified.  */
7596   dst_type = cv_unqualified (dst_type);
7597 
7598   /* Save casted types in the function's used types hash table.  */
7599   used_types_insert (dst_type);
7600 
7601   src_type = TREE_TYPE (expr);
7602   /* Expressions do not really have reference types.  */
7603   if (TREE_CODE (src_type) == REFERENCE_TYPE)
7604     src_type = TREE_TYPE (src_type);
7605 
7606   /* [expr.const.cast]
7607 
7608      For two object types T1 and T2, if a pointer to T1 can be explicitly
7609      converted to the type "pointer to T2" using a const_cast, then the
7610      following conversions can also be made:
7611 
7612      -- an lvalue of type T1 can be explicitly converted to an lvalue of
7613      type T2 using the cast const_cast<T2&>;
7614 
7615      -- a glvalue of type T1 can be explicitly converted to an xvalue of
7616      type T2 using the cast const_cast<T2&&>; and
7617 
7618      -- if T1 is a class type, a prvalue of type T1 can be explicitly
7619      converted to an xvalue of type T2 using the cast const_cast<T2&&>.  */
7620 
7621   if (TREE_CODE (dst_type) == REFERENCE_TYPE)
7622     {
7623       reference_type = dst_type;
7624       if (!TYPE_REF_IS_RVALUE (dst_type)
7625 	  ? lvalue_p (expr)
7626 	  : obvalue_p (expr))
7627 	/* OK.  */;
7628       else
7629 	{
7630 	  if (complain & tf_error)
7631 	    error ("invalid const_cast of an rvalue of type %qT to type %qT",
7632 		   src_type, dst_type);
7633 	  return error_mark_node;
7634 	}
7635       dst_type = build_pointer_type (TREE_TYPE (dst_type));
7636       src_type = build_pointer_type (src_type);
7637     }
7638   else
7639     {
7640       reference_type = NULL_TREE;
7641       /* If the destination type is not a reference type, the
7642 	 lvalue-to-rvalue, array-to-pointer, and function-to-pointer
7643 	 conversions are performed.  */
7644       src_type = type_decays_to (src_type);
7645       if (src_type == error_mark_node)
7646 	return error_mark_node;
7647     }
7648 
7649   if (TYPE_PTR_P (src_type) || TYPE_PTRDATAMEM_P (src_type))
7650     {
7651       if (comp_ptr_ttypes_const (dst_type, src_type))
7652 	{
7653 	  if (valid_p)
7654 	    {
7655 	      *valid_p = true;
7656 	      /* This cast is actually a C-style cast.  Issue a warning if
7657 		 the user is making a potentially unsafe cast.  */
7658 	      check_for_casting_away_constness (src_type, dst_type,
7659 						CAST_EXPR, complain);
7660 	      /* ??? comp_ptr_ttypes_const ignores TYPE_ALIGN.  */
7661 	      if ((STRICT_ALIGNMENT || warn_cast_align == 2)
7662 		  && (complain & tf_warning)
7663 		  && min_align_of_type (TREE_TYPE (dst_type))
7664 		     > min_align_of_type (TREE_TYPE (src_type)))
7665 		warning (OPT_Wcast_align, "cast from %qH to %qI "
7666 			 "increases required alignment of target type",
7667 			 src_type, dst_type);
7668 	    }
7669 	  if (reference_type)
7670 	    {
7671 	      expr = cp_build_addr_expr (expr, complain);
7672 	      if (expr == error_mark_node)
7673 		return error_mark_node;
7674 	      expr = build_nop (reference_type, expr);
7675 	      return convert_from_reference (expr);
7676 	    }
7677 	  else
7678 	    {
7679 	      expr = decay_conversion (expr, complain);
7680 	      if (expr == error_mark_node)
7681 		return error_mark_node;
7682 
7683 	      /* build_c_cast puts on a NOP_EXPR to make the result not an
7684 		 lvalue.  Strip such NOP_EXPRs if VALUE is being used in
7685 		 non-lvalue context.  */
7686 	      if (TREE_CODE (expr) == NOP_EXPR
7687 		  && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
7688 		expr = TREE_OPERAND (expr, 0);
7689 	      return build_nop (dst_type, expr);
7690 	    }
7691 	}
7692       else if (valid_p
7693 	       && !at_least_as_qualified_p (TREE_TYPE (dst_type),
7694 					    TREE_TYPE (src_type)))
7695 	check_for_casting_away_constness (src_type, dst_type, CAST_EXPR,
7696 					  complain);
7697     }
7698 
7699   if (complain & tf_error)
7700     error ("invalid const_cast from type %qT to type %qT",
7701 	   src_type, dst_type);
7702   return error_mark_node;
7703 }
7704 
7705 tree
7706 build_const_cast (tree type, tree expr, tsubst_flags_t complain)
7707 {
7708   tree r;
7709 
7710   if (type == error_mark_node || error_operand_p (expr))
7711     return error_mark_node;
7712 
7713   if (processing_template_decl)
7714     {
7715       tree t = build_min (CONST_CAST_EXPR, type, expr);
7716 
7717       if (!TREE_SIDE_EFFECTS (t)
7718 	  && type_dependent_expression_p (expr))
7719 	/* There might turn out to be side effects inside expr.  */
7720 	TREE_SIDE_EFFECTS (t) = 1;
7721       return convert_from_reference (t);
7722     }
7723 
7724   r = build_const_cast_1 (type, expr, complain, /*valid_p=*/NULL);
7725   if (r != error_mark_node)
7726     {
7727       maybe_warn_about_useless_cast (type, expr, complain);
7728       maybe_warn_about_cast_ignoring_quals (type, complain);
7729     }
7730   return r;
7731 }
7732 
7733 /* Like cp_build_c_cast, but for the c-common bits.  */
7734 
7735 tree
7736 build_c_cast (location_t /*loc*/, tree type, tree expr)
7737 {
7738   return cp_build_c_cast (type, expr, tf_warning_or_error);
7739 }
7740 
7741 /* Like the "build_c_cast" used for c-common, but using cp_expr to
7742    preserve location information even for tree nodes that don't
7743    support it.  */
7744 
7745 cp_expr
7746 build_c_cast (location_t loc, tree type, cp_expr expr)
7747 {
7748   cp_expr result = cp_build_c_cast (type, expr, tf_warning_or_error);
7749   result.set_location (loc);
7750   return result;
7751 }
7752 
7753 /* Build an expression representing an explicit C-style cast to type
7754    TYPE of expression EXPR.  */
7755 
7756 tree
7757 cp_build_c_cast (tree type, tree expr, tsubst_flags_t complain)
7758 {
7759   tree value = expr;
7760   tree result;
7761   bool valid_p;
7762 
7763   if (type == error_mark_node || error_operand_p (expr))
7764     return error_mark_node;
7765 
7766   if (processing_template_decl)
7767     {
7768       tree t = build_min (CAST_EXPR, type,
7769 			  tree_cons (NULL_TREE, value, NULL_TREE));
7770       /* We don't know if it will or will not have side effects.  */
7771       TREE_SIDE_EFFECTS (t) = 1;
7772       return convert_from_reference (t);
7773     }
7774 
7775   /* Casts to a (pointer to a) specific ObjC class (or 'id' or
7776      'Class') should always be retained, because this information aids
7777      in method lookup.  */
7778   if (objc_is_object_ptr (type)
7779       && objc_is_object_ptr (TREE_TYPE (expr)))
7780     return build_nop (type, expr);
7781 
7782   /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7783      Strip such NOP_EXPRs if VALUE is being used in non-lvalue context.  */
7784   if (TREE_CODE (type) != REFERENCE_TYPE
7785       && TREE_CODE (value) == NOP_EXPR
7786       && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
7787     value = TREE_OPERAND (value, 0);
7788 
7789   if (TREE_CODE (type) == ARRAY_TYPE)
7790     {
7791       /* Allow casting from T1* to T2[] because Cfront allows it.
7792 	 NIHCL uses it. It is not valid ISO C++ however.  */
7793       if (TYPE_PTR_P (TREE_TYPE (expr)))
7794 	{
7795           if (complain & tf_error)
7796             permerror (input_location, "ISO C++ forbids casting to an array type %qT", type);
7797           else
7798             return error_mark_node;
7799 	  type = build_pointer_type (TREE_TYPE (type));
7800 	}
7801       else
7802 	{
7803           if (complain & tf_error)
7804             error ("ISO C++ forbids casting to an array type %qT", type);
7805 	  return error_mark_node;
7806 	}
7807     }
7808 
7809   if (TREE_CODE (type) == FUNCTION_TYPE
7810       || TREE_CODE (type) == METHOD_TYPE)
7811     {
7812       if (complain & tf_error)
7813         error ("invalid cast to function type %qT", type);
7814       return error_mark_node;
7815     }
7816 
7817   if (TYPE_PTR_P (type)
7818       && TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
7819       /* Casting to an integer of smaller size is an error detected elsewhere.  */
7820       && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (value))
7821       /* Don't warn about converting any constant.  */
7822       && !TREE_CONSTANT (value))
7823     warning_at (input_location, OPT_Wint_to_pointer_cast,
7824 		"cast to pointer from integer of different size");
7825 
7826   /* A C-style cast can be a const_cast.  */
7827   result = build_const_cast_1 (type, value, complain & tf_warning,
7828 			       &valid_p);
7829   if (valid_p)
7830     {
7831       if (result != error_mark_node)
7832 	{
7833 	  maybe_warn_about_useless_cast (type, value, complain);
7834 	  maybe_warn_about_cast_ignoring_quals (type, complain);
7835 	}
7836       return result;
7837     }
7838 
7839   /* Or a static cast.  */
7840   result = build_static_cast_1 (type, value, /*c_cast_p=*/true,
7841 				&valid_p, complain);
7842   /* Or a reinterpret_cast.  */
7843   if (!valid_p)
7844     result = build_reinterpret_cast_1 (type, value, /*c_cast_p=*/true,
7845 				       &valid_p, complain);
7846   /* The static_cast or reinterpret_cast may be followed by a
7847      const_cast.  */
7848   if (valid_p
7849       /* A valid cast may result in errors if, for example, a
7850 	 conversion to an ambiguous base class is required.  */
7851       && !error_operand_p (result))
7852     {
7853       tree result_type;
7854 
7855       maybe_warn_about_useless_cast (type, value, complain);
7856       maybe_warn_about_cast_ignoring_quals (type, complain);
7857 
7858       /* Non-class rvalues always have cv-unqualified type.  */
7859       if (!CLASS_TYPE_P (type))
7860 	type = TYPE_MAIN_VARIANT (type);
7861       result_type = TREE_TYPE (result);
7862       if (!CLASS_TYPE_P (result_type) && TREE_CODE (type) != REFERENCE_TYPE)
7863 	result_type = TYPE_MAIN_VARIANT (result_type);
7864       /* If the type of RESULT does not match TYPE, perform a
7865 	 const_cast to make it match.  If the static_cast or
7866 	 reinterpret_cast succeeded, we will differ by at most
7867 	 cv-qualification, so the follow-on const_cast is guaranteed
7868 	 to succeed.  */
7869       if (!same_type_p (non_reference (type), non_reference (result_type)))
7870 	{
7871 	  result = build_const_cast_1 (type, result, false, &valid_p);
7872 	  gcc_assert (valid_p);
7873 	}
7874       return result;
7875     }
7876 
7877   return error_mark_node;
7878 }
7879 
7880 /* For use from the C common bits.  */
7881 tree
7882 build_modify_expr (location_t location,
7883 		   tree lhs, tree /*lhs_origtype*/,
7884 		   enum tree_code modifycode,
7885 		   location_t /*rhs_location*/, tree rhs,
7886 		   tree /*rhs_origtype*/)
7887 {
7888   return cp_build_modify_expr (location, lhs, modifycode, rhs,
7889 			       tf_warning_or_error);
7890 }
7891 
7892 /* Build an assignment expression of lvalue LHS from value RHS.
7893    MODIFYCODE is the code for a binary operator that we use
7894    to combine the old value of LHS with RHS to get the new value.
7895    Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
7896 
7897    C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed.  */
7898 
7899 tree
7900 cp_build_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
7901 		      tree rhs, tsubst_flags_t complain)
7902 {
7903   lhs = mark_lvalue_use_nonread (lhs);
7904 
7905   tree result = NULL_TREE;
7906   tree newrhs = rhs;
7907   tree lhstype = TREE_TYPE (lhs);
7908   tree olhs = lhs;
7909   tree olhstype = lhstype;
7910   bool plain_assign = (modifycode == NOP_EXPR);
7911   bool compound_side_effects_p = false;
7912   tree preeval = NULL_TREE;
7913 
7914   /* Avoid duplicate error messages from operands that had errors.  */
7915   if (error_operand_p (lhs) || error_operand_p (rhs))
7916     return error_mark_node;
7917 
7918   while (TREE_CODE (lhs) == COMPOUND_EXPR)
7919     {
7920       if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
7921 	compound_side_effects_p = true;
7922       lhs = TREE_OPERAND (lhs, 1);
7923     }
7924 
7925   /* Handle control structure constructs used as "lvalues".  Note that we
7926      leave COMPOUND_EXPR on the LHS because it is sequenced after the RHS.  */
7927   switch (TREE_CODE (lhs))
7928     {
7929       /* Handle --foo = 5; as these are valid constructs in C++.  */
7930     case PREDECREMENT_EXPR:
7931     case PREINCREMENT_EXPR:
7932       if (compound_side_effects_p)
7933 	newrhs = rhs = stabilize_expr (rhs, &preeval);
7934       lhs = genericize_compound_lvalue (lhs);
7935     maybe_add_compound:
7936       /* If we had (bar, --foo) = 5; or (bar, (baz, --foo)) = 5;
7937 	 and looked through the COMPOUND_EXPRs, readd them now around
7938 	 the resulting lhs.  */
7939       if (TREE_CODE (olhs) == COMPOUND_EXPR)
7940 	{
7941 	  lhs = build2 (COMPOUND_EXPR, lhstype, TREE_OPERAND (olhs, 0), lhs);
7942 	  tree *ptr = &TREE_OPERAND (lhs, 1);
7943 	  for (olhs = TREE_OPERAND (olhs, 1);
7944 	       TREE_CODE (olhs) == COMPOUND_EXPR;
7945 	       olhs = TREE_OPERAND (olhs, 1))
7946 	    {
7947 	      *ptr = build2 (COMPOUND_EXPR, lhstype,
7948 			     TREE_OPERAND (olhs, 0), *ptr);
7949 	      ptr = &TREE_OPERAND (*ptr, 1);
7950 	    }
7951 	}
7952       break;
7953 
7954     case MODIFY_EXPR:
7955       if (compound_side_effects_p)
7956 	newrhs = rhs = stabilize_expr (rhs, &preeval);
7957       lhs = genericize_compound_lvalue (lhs);
7958       goto maybe_add_compound;
7959 
7960     case MIN_EXPR:
7961     case MAX_EXPR:
7962       /* MIN_EXPR and MAX_EXPR are currently only permitted as lvalues,
7963 	 when neither operand has side-effects.  */
7964       if (!lvalue_or_else (lhs, lv_assign, complain))
7965 	return error_mark_node;
7966 
7967       gcc_assert (!TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0))
7968 		  && !TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 1)));
7969 
7970       lhs = build3 (COND_EXPR, TREE_TYPE (lhs),
7971 		    build2 (TREE_CODE (lhs) == MIN_EXPR ? LE_EXPR : GE_EXPR,
7972 			    boolean_type_node,
7973 			    TREE_OPERAND (lhs, 0),
7974 			    TREE_OPERAND (lhs, 1)),
7975 		    TREE_OPERAND (lhs, 0),
7976 		    TREE_OPERAND (lhs, 1));
7977       gcc_fallthrough ();
7978 
7979       /* Handle (a ? b : c) used as an "lvalue".  */
7980     case COND_EXPR:
7981       {
7982 	/* Produce (a ? (b = rhs) : (c = rhs))
7983 	   except that the RHS goes through a save-expr
7984 	   so the code to compute it is only emitted once.  */
7985 	tree cond;
7986 
7987 	if (VOID_TYPE_P (TREE_TYPE (rhs)))
7988 	  {
7989 	    if (complain & tf_error)
7990 	      error ("void value not ignored as it ought to be");
7991 	    return error_mark_node;
7992 	  }
7993 
7994 	rhs = stabilize_expr (rhs, &preeval);
7995 
7996 	/* Check this here to avoid odd errors when trying to convert
7997 	   a throw to the type of the COND_EXPR.  */
7998 	if (!lvalue_or_else (lhs, lv_assign, complain))
7999 	  return error_mark_node;
8000 
8001 	cond = build_conditional_expr
8002 	  (input_location, TREE_OPERAND (lhs, 0),
8003 	   cp_build_modify_expr (loc, TREE_OPERAND (lhs, 1),
8004 				 modifycode, rhs, complain),
8005 	   cp_build_modify_expr (loc, TREE_OPERAND (lhs, 2),
8006 				 modifycode, rhs, complain),
8007            complain);
8008 
8009 	if (cond == error_mark_node)
8010 	  return cond;
8011 	/* If we had (e, (a ? b : c)) = d; or (e, (f, (a ? b : c))) = d;
8012 	   and looked through the COMPOUND_EXPRs, readd them now around
8013 	   the resulting cond before adding the preevaluated rhs.  */
8014 	if (TREE_CODE (olhs) == COMPOUND_EXPR)
8015 	  {
8016 	    cond = build2 (COMPOUND_EXPR, TREE_TYPE (cond),
8017 			   TREE_OPERAND (olhs, 0), cond);
8018 	    tree *ptr = &TREE_OPERAND (cond, 1);
8019 	    for (olhs = TREE_OPERAND (olhs, 1);
8020 		 TREE_CODE (olhs) == COMPOUND_EXPR;
8021 		 olhs = TREE_OPERAND (olhs, 1))
8022 	      {
8023 		*ptr = build2 (COMPOUND_EXPR, TREE_TYPE (cond),
8024 			       TREE_OPERAND (olhs, 0), *ptr);
8025 		ptr = &TREE_OPERAND (*ptr, 1);
8026 	      }
8027 	  }
8028 	/* Make sure the code to compute the rhs comes out
8029 	   before the split.  */
8030 	result = cond;
8031 	goto ret;
8032       }
8033 
8034     default:
8035       lhs = olhs;
8036       break;
8037     }
8038 
8039   if (modifycode == INIT_EXPR)
8040     {
8041       if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
8042 	/* Do the default thing.  */;
8043       else if (TREE_CODE (rhs) == CONSTRUCTOR)
8044 	{
8045 	  /* Compound literal.  */
8046 	  if (! same_type_p (TREE_TYPE (rhs), lhstype))
8047 	    /* Call convert to generate an error; see PR 11063.  */
8048 	    rhs = convert (lhstype, rhs);
8049 	  result = build2 (INIT_EXPR, lhstype, lhs, rhs);
8050 	  TREE_SIDE_EFFECTS (result) = 1;
8051 	  goto ret;
8052 	}
8053       else if (! MAYBE_CLASS_TYPE_P (lhstype))
8054 	/* Do the default thing.  */;
8055       else
8056 	{
8057 	  vec<tree, va_gc> *rhs_vec = make_tree_vector_single (rhs);
8058 	  result = build_special_member_call (lhs, complete_ctor_identifier,
8059 					      &rhs_vec, lhstype, LOOKUP_NORMAL,
8060                                               complain);
8061 	  release_tree_vector (rhs_vec);
8062 	  if (result == NULL_TREE)
8063 	    return error_mark_node;
8064 	  goto ret;
8065 	}
8066     }
8067   else
8068     {
8069       lhs = require_complete_type_sfinae (lhs, complain);
8070       if (lhs == error_mark_node)
8071 	return error_mark_node;
8072 
8073       if (modifycode == NOP_EXPR)
8074 	{
8075 	  if (c_dialect_objc ())
8076 	    {
8077 	      result = objc_maybe_build_modify_expr (lhs, rhs);
8078 	      if (result)
8079 		goto ret;
8080 	    }
8081 
8082 	  /* `operator=' is not an inheritable operator.  */
8083 	  if (! MAYBE_CLASS_TYPE_P (lhstype))
8084 	    /* Do the default thing.  */;
8085 	  else
8086 	    {
8087 	      result = build_new_op (input_location, MODIFY_EXPR,
8088 				     LOOKUP_NORMAL, lhs, rhs,
8089 				     make_node (NOP_EXPR), /*overload=*/NULL,
8090 				     complain);
8091 	      if (result == NULL_TREE)
8092 		return error_mark_node;
8093 	      goto ret;
8094 	    }
8095 	  lhstype = olhstype;
8096 	}
8097       else
8098 	{
8099 	  tree init = NULL_TREE;
8100 
8101 	  /* A binary op has been requested.  Combine the old LHS
8102 	     value with the RHS producing the value we should actually
8103 	     store into the LHS.  */
8104 	  gcc_assert (!((TREE_CODE (lhstype) == REFERENCE_TYPE
8105 			 && MAYBE_CLASS_TYPE_P (TREE_TYPE (lhstype)))
8106 			|| MAYBE_CLASS_TYPE_P (lhstype)));
8107 
8108 	  /* Preevaluate the RHS to make sure its evaluation is complete
8109 	     before the lvalue-to-rvalue conversion of the LHS:
8110 
8111 	     [expr.ass] With respect to an indeterminately-sequenced
8112 	     function call, the operation of a compound assignment is a
8113 	     single evaluation. [ Note: Therefore, a function call shall
8114 	     not intervene between the lvalue-to-rvalue conversion and the
8115 	     side effect associated with any single compound assignment
8116 	     operator. -- end note ]  */
8117 	  lhs = cp_stabilize_reference (lhs);
8118 	  rhs = decay_conversion (rhs, complain);
8119 	  if (rhs == error_mark_node)
8120 	    return error_mark_node;
8121 	  rhs = stabilize_expr (rhs, &init);
8122 	  newrhs = cp_build_binary_op (loc, modifycode, lhs, rhs, complain);
8123 	  if (newrhs == error_mark_node)
8124 	    {
8125 	      if (complain & tf_error)
8126 		error ("  in evaluation of %<%Q(%#T, %#T)%>", modifycode,
8127 		       TREE_TYPE (lhs), TREE_TYPE (rhs));
8128 	      return error_mark_node;
8129 	    }
8130 
8131 	  if (init)
8132 	    newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), init, newrhs);
8133 
8134 	  /* Now it looks like a plain assignment.  */
8135 	  modifycode = NOP_EXPR;
8136 	  if (c_dialect_objc ())
8137 	    {
8138 	      result = objc_maybe_build_modify_expr (lhs, newrhs);
8139 	      if (result)
8140 		goto ret;
8141 	    }
8142 	}
8143       gcc_assert (TREE_CODE (lhstype) != REFERENCE_TYPE);
8144       gcc_assert (TREE_CODE (TREE_TYPE (newrhs)) != REFERENCE_TYPE);
8145     }
8146 
8147   /* The left-hand side must be an lvalue.  */
8148   if (!lvalue_or_else (lhs, lv_assign, complain))
8149     return error_mark_node;
8150 
8151   /* Warn about modifying something that is `const'.  Don't warn if
8152      this is initialization.  */
8153   if (modifycode != INIT_EXPR
8154       && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype)
8155 	  /* Functions are not modifiable, even though they are
8156 	     lvalues.  */
8157 	  || TREE_CODE (TREE_TYPE (lhs)) == FUNCTION_TYPE
8158 	  || TREE_CODE (TREE_TYPE (lhs)) == METHOD_TYPE
8159 	  /* If it's an aggregate and any field is const, then it is
8160 	     effectively const.  */
8161 	  || (CLASS_TYPE_P (lhstype)
8162 	      && C_TYPE_FIELDS_READONLY (lhstype))))
8163     {
8164       if (complain & tf_error)
8165 	cxx_readonly_error (lhs, lv_assign);
8166       return error_mark_node;
8167     }
8168 
8169   /* If storing into a structure or union member, it may have been given a
8170      lowered bitfield type.  We need to convert to the declared type first,
8171      so retrieve it now.  */
8172 
8173   olhstype = unlowered_expr_type (lhs);
8174 
8175   /* Convert new value to destination type.  */
8176 
8177   if (TREE_CODE (lhstype) == ARRAY_TYPE)
8178     {
8179       int from_array;
8180 
8181       if (BRACE_ENCLOSED_INITIALIZER_P (newrhs))
8182 	{
8183 	  if (modifycode != INIT_EXPR)
8184 	    {
8185 	      if (complain & tf_error)
8186 		error ("assigning to an array from an initializer list");
8187 	      return error_mark_node;
8188 	    }
8189 	  if (check_array_initializer (lhs, lhstype, newrhs))
8190 	    return error_mark_node;
8191 	  newrhs = digest_init (lhstype, newrhs, complain);
8192 	  if (newrhs == error_mark_node)
8193 	    return error_mark_node;
8194 	}
8195 
8196       /* C++11 8.5/17: "If the destination type is an array of characters,
8197 	 an array of char16_t, an array of char32_t, or an array of wchar_t,
8198 	 and the initializer is a string literal...".  */
8199       else if (TREE_CODE (newrhs) == STRING_CST
8200 	       && char_type_p (TREE_TYPE (TYPE_MAIN_VARIANT (lhstype)))
8201 	       && modifycode == INIT_EXPR)
8202 	{
8203 	  newrhs = digest_init (lhstype, newrhs, complain);
8204 	  if (newrhs == error_mark_node)
8205 	    return error_mark_node;
8206 	}
8207 
8208       else if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype),
8209 				     TYPE_MAIN_VARIANT (TREE_TYPE (newrhs))))
8210 	{
8211 	  if (complain & tf_error)
8212 	    error ("incompatible types in assignment of %qT to %qT",
8213 		   TREE_TYPE (rhs), lhstype);
8214 	  return error_mark_node;
8215 	}
8216 
8217       /* Allow array assignment in compiler-generated code.  */
8218       else if (!current_function_decl
8219 	       || !DECL_DEFAULTED_FN (current_function_decl))
8220 	{
8221           /* This routine is used for both initialization and assignment.
8222              Make sure the diagnostic message differentiates the context.  */
8223 	  if (complain & tf_error)
8224 	    {
8225 	      if (modifycode == INIT_EXPR)
8226 		error ("array used as initializer");
8227 	      else
8228 		error ("invalid array assignment");
8229 	    }
8230 	  return error_mark_node;
8231 	}
8232 
8233       from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
8234 		   ? 1 + (modifycode != INIT_EXPR): 0;
8235       result = build_vec_init (lhs, NULL_TREE, newrhs,
8236 			       /*explicit_value_init_p=*/false,
8237 			       from_array, complain);
8238       goto ret;
8239     }
8240 
8241   if (modifycode == INIT_EXPR)
8242     /* Calls with INIT_EXPR are all direct-initialization, so don't set
8243        LOOKUP_ONLYCONVERTING.  */
8244     newrhs = convert_for_initialization (lhs, olhstype, newrhs, LOOKUP_NORMAL,
8245 					 ICR_INIT, NULL_TREE, 0,
8246                                          complain);
8247   else
8248     newrhs = convert_for_assignment (olhstype, newrhs, ICR_ASSIGN,
8249 				     NULL_TREE, 0, complain, LOOKUP_IMPLICIT);
8250 
8251   if (!same_type_p (lhstype, olhstype))
8252     newrhs = cp_convert_and_check (lhstype, newrhs, complain);
8253 
8254   if (modifycode != INIT_EXPR)
8255     {
8256       if (TREE_CODE (newrhs) == CALL_EXPR
8257 	  && TYPE_NEEDS_CONSTRUCTING (lhstype))
8258 	newrhs = build_cplus_new (lhstype, newrhs, complain);
8259 
8260       /* Can't initialize directly from a TARGET_EXPR, since that would
8261 	 cause the lhs to be constructed twice, and possibly result in
8262 	 accidental self-initialization.  So we force the TARGET_EXPR to be
8263 	 expanded without a target.  */
8264       if (TREE_CODE (newrhs) == TARGET_EXPR)
8265 	newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
8266 			 TREE_OPERAND (newrhs, 0));
8267     }
8268 
8269   if (newrhs == error_mark_node)
8270     return error_mark_node;
8271 
8272   if (c_dialect_objc () && flag_objc_gc)
8273     {
8274       result = objc_generate_write_barrier (lhs, modifycode, newrhs);
8275 
8276       if (result)
8277 	goto ret;
8278     }
8279 
8280   result = build2 (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
8281 		   lhstype, lhs, newrhs);
8282 
8283   TREE_SIDE_EFFECTS (result) = 1;
8284   if (!plain_assign)
8285     TREE_NO_WARNING (result) = 1;
8286 
8287  ret:
8288   if (preeval)
8289     result = build2 (COMPOUND_EXPR, TREE_TYPE (result), preeval, result);
8290   return result;
8291 }
8292 
8293 cp_expr
8294 build_x_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
8295 		     tree rhs, tsubst_flags_t complain)
8296 {
8297   tree orig_lhs = lhs;
8298   tree orig_rhs = rhs;
8299   tree overload = NULL_TREE;
8300   tree op = build_nt (modifycode, NULL_TREE, NULL_TREE);
8301 
8302   if (processing_template_decl)
8303     {
8304       if (modifycode == NOP_EXPR
8305 	  || type_dependent_expression_p (lhs)
8306 	  || type_dependent_expression_p (rhs))
8307         return build_min_nt_loc (loc, MODOP_EXPR, lhs,
8308 				 build_min_nt_loc (loc, modifycode, NULL_TREE,
8309 						   NULL_TREE), rhs);
8310 
8311       lhs = build_non_dependent_expr (lhs);
8312       rhs = build_non_dependent_expr (rhs);
8313     }
8314 
8315   if (modifycode != NOP_EXPR)
8316     {
8317       tree rval = build_new_op (loc, MODIFY_EXPR, LOOKUP_NORMAL,
8318 				lhs, rhs, op, &overload, complain);
8319       if (rval)
8320 	{
8321 	  if (rval == error_mark_node)
8322 	    return rval;
8323 	  TREE_NO_WARNING (rval) = 1;
8324 	  if (processing_template_decl)
8325 	    {
8326 	      if (overload != NULL_TREE)
8327 		return (build_min_non_dep_op_overload
8328 			(MODIFY_EXPR, rval, overload, orig_lhs, orig_rhs));
8329 
8330 	      return (build_min_non_dep
8331 		      (MODOP_EXPR, rval, orig_lhs, op, orig_rhs));
8332 	    }
8333 	  return rval;
8334 	}
8335     }
8336   return cp_build_modify_expr (loc, lhs, modifycode, rhs, complain);
8337 }
8338 
8339 /* Helper function for get_delta_difference which assumes FROM is a base
8340    class of TO.  Returns a delta for the conversion of pointer-to-member
8341    of FROM to pointer-to-member of TO.  If the conversion is invalid and
8342    tf_error is not set in COMPLAIN returns error_mark_node, otherwise
8343    returns zero.  If FROM is not a base class of TO, returns NULL_TREE.
8344    If C_CAST_P is true, this conversion is taking place as part of a
8345    C-style cast.  */
8346 
8347 static tree
8348 get_delta_difference_1 (tree from, tree to, bool c_cast_p,
8349 			tsubst_flags_t complain)
8350 {
8351   tree binfo;
8352   base_kind kind;
8353 
8354   binfo = lookup_base (to, from, c_cast_p ? ba_unique : ba_check,
8355 		       &kind, complain);
8356 
8357   if (binfo == error_mark_node)
8358     {
8359       if (!(complain & tf_error))
8360 	return error_mark_node;
8361 
8362       error ("   in pointer to member function conversion");
8363       return size_zero_node;
8364     }
8365   else if (binfo)
8366     {
8367       if (kind != bk_via_virtual)
8368 	return BINFO_OFFSET (binfo);
8369       else
8370 	/* FROM is a virtual base class of TO.  Issue an error or warning
8371 	   depending on whether or not this is a reinterpret cast.  */
8372 	{
8373 	  if (!(complain & tf_error))
8374 	    return error_mark_node;
8375 
8376 	  error ("pointer to member conversion via virtual base %qT",
8377 		 BINFO_TYPE (binfo_from_vbase (binfo)));
8378 
8379 	  return size_zero_node;
8380 	}
8381       }
8382   else
8383     return NULL_TREE;
8384 }
8385 
8386 /* Get difference in deltas for different pointer to member function
8387    types.  If the conversion is invalid and tf_error is not set in
8388    COMPLAIN, returns error_mark_node, otherwise returns an integer
8389    constant of type PTRDIFF_TYPE_NODE and its value is zero if the
8390    conversion is invalid.  If ALLOW_INVERSE_P is true, then allow reverse
8391    conversions as well.  If C_CAST_P is true this conversion is taking
8392    place as part of a C-style cast.
8393 
8394    Note that the naming of FROM and TO is kind of backwards; the return
8395    value is what we add to a TO in order to get a FROM.  They are named
8396    this way because we call this function to find out how to convert from
8397    a pointer to member of FROM to a pointer to member of TO.  */
8398 
8399 static tree
8400 get_delta_difference (tree from, tree to,
8401 		      bool allow_inverse_p,
8402 		      bool c_cast_p, tsubst_flags_t complain)
8403 {
8404   tree result;
8405 
8406   if (same_type_ignoring_top_level_qualifiers_p (from, to))
8407     /* Pointer to member of incomplete class is permitted*/
8408     result = size_zero_node;
8409   else
8410     result = get_delta_difference_1 (from, to, c_cast_p, complain);
8411 
8412   if (result == error_mark_node)
8413     return error_mark_node;
8414 
8415   if (!result)
8416   {
8417     if (!allow_inverse_p)
8418       {
8419 	if (!(complain & tf_error))
8420 	  return error_mark_node;
8421 
8422 	error_not_base_type (from, to);
8423 	error ("   in pointer to member conversion");
8424       	result = size_zero_node;
8425       }
8426     else
8427       {
8428 	result = get_delta_difference_1 (to, from, c_cast_p, complain);
8429 
8430 	if (result == error_mark_node)
8431 	  return error_mark_node;
8432 
8433 	if (result)
8434 	  result = size_diffop_loc (input_location,
8435 				    size_zero_node, result);
8436 	else
8437 	  {
8438 	    if (!(complain & tf_error))
8439 	      return error_mark_node;
8440 
8441 	    error_not_base_type (from, to);
8442 	    error ("   in pointer to member conversion");
8443 	    result = size_zero_node;
8444 	  }
8445       }
8446   }
8447 
8448   return convert_to_integer (ptrdiff_type_node, result);
8449 }
8450 
8451 /* Return a constructor for the pointer-to-member-function TYPE using
8452    the other components as specified.  */
8453 
8454 tree
8455 build_ptrmemfunc1 (tree type, tree delta, tree pfn)
8456 {
8457   tree u = NULL_TREE;
8458   tree delta_field;
8459   tree pfn_field;
8460   vec<constructor_elt, va_gc> *v;
8461 
8462   /* Pull the FIELD_DECLs out of the type.  */
8463   pfn_field = TYPE_FIELDS (type);
8464   delta_field = DECL_CHAIN (pfn_field);
8465 
8466   /* Make sure DELTA has the type we want.  */
8467   delta = convert_and_check (input_location, delta_type_node, delta);
8468 
8469   /* Convert to the correct target type if necessary.  */
8470   pfn = fold_convert (TREE_TYPE (pfn_field), pfn);
8471 
8472   /* Finish creating the initializer.  */
8473   vec_alloc (v, 2);
8474   CONSTRUCTOR_APPEND_ELT(v, pfn_field, pfn);
8475   CONSTRUCTOR_APPEND_ELT(v, delta_field, delta);
8476   u = build_constructor (type, v);
8477   TREE_CONSTANT (u) = TREE_CONSTANT (pfn) & TREE_CONSTANT (delta);
8478   TREE_STATIC (u) = (TREE_CONSTANT (u)
8479 		     && (initializer_constant_valid_p (pfn, TREE_TYPE (pfn))
8480 			 != NULL_TREE)
8481 		     && (initializer_constant_valid_p (delta, TREE_TYPE (delta))
8482 			 != NULL_TREE));
8483   return u;
8484 }
8485 
8486 /* Build a constructor for a pointer to member function.  It can be
8487    used to initialize global variables, local variable, or used
8488    as a value in expressions.  TYPE is the POINTER to METHOD_TYPE we
8489    want to be.
8490 
8491    If FORCE is nonzero, then force this conversion, even if
8492    we would rather not do it.  Usually set when using an explicit
8493    cast.  A C-style cast is being processed iff C_CAST_P is true.
8494 
8495    Return error_mark_node, if something goes wrong.  */
8496 
8497 tree
8498 build_ptrmemfunc (tree type, tree pfn, int force, bool c_cast_p,
8499 		  tsubst_flags_t complain)
8500 {
8501   tree fn;
8502   tree pfn_type;
8503   tree to_type;
8504 
8505   if (error_operand_p (pfn))
8506     return error_mark_node;
8507 
8508   pfn_type = TREE_TYPE (pfn);
8509   to_type = build_ptrmemfunc_type (type);
8510 
8511   /* Handle multiple conversions of pointer to member functions.  */
8512   if (TYPE_PTRMEMFUNC_P (pfn_type))
8513     {
8514       tree delta = NULL_TREE;
8515       tree npfn = NULL_TREE;
8516       tree n;
8517 
8518       if (!force
8519 	  && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn,
8520 			       LOOKUP_NORMAL, complain))
8521 	{
8522 	  if (complain & tf_error)
8523 	    error ("invalid conversion to type %qT from type %qT",
8524 		   to_type, pfn_type);
8525 	  else
8526 	    return error_mark_node;
8527 	}
8528 
8529       n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type),
8530 				TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type),
8531 				force,
8532 				c_cast_p, complain);
8533       if (n == error_mark_node)
8534 	return error_mark_node;
8535 
8536       /* We don't have to do any conversion to convert a
8537 	 pointer-to-member to its own type.  But, we don't want to
8538 	 just return a PTRMEM_CST if there's an explicit cast; that
8539 	 cast should make the expression an invalid template argument.  */
8540       if (TREE_CODE (pfn) != PTRMEM_CST)
8541 	{
8542 	  if (same_type_p (to_type, pfn_type))
8543 	    return pfn;
8544 	  else if (integer_zerop (n) && TREE_CODE (pfn) != CONSTRUCTOR)
8545 	    return build_reinterpret_cast (to_type, pfn,
8546                                            complain);
8547 	}
8548 
8549       if (TREE_SIDE_EFFECTS (pfn))
8550 	pfn = save_expr (pfn);
8551 
8552       /* Obtain the function pointer and the current DELTA.  */
8553       if (TREE_CODE (pfn) == PTRMEM_CST)
8554 	expand_ptrmemfunc_cst (pfn, &delta, &npfn);
8555       else
8556 	{
8557 	  npfn = build_ptrmemfunc_access_expr (pfn, pfn_identifier);
8558 	  delta = build_ptrmemfunc_access_expr (pfn, delta_identifier);
8559 	}
8560 
8561       /* Just adjust the DELTA field.  */
8562       gcc_assert  (same_type_ignoring_top_level_qualifiers_p
8563 		   (TREE_TYPE (delta), ptrdiff_type_node));
8564       if (!integer_zerop (n))
8565 	{
8566 	  if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_delta)
8567 	    n = cp_build_binary_op (input_location,
8568 				    LSHIFT_EXPR, n, integer_one_node,
8569 				    complain);
8570 	  delta = cp_build_binary_op (input_location,
8571 				      PLUS_EXPR, delta, n, complain);
8572 	}
8573       return build_ptrmemfunc1 (to_type, delta, npfn);
8574     }
8575 
8576   /* Handle null pointer to member function conversions.  */
8577   if (null_ptr_cst_p (pfn))
8578     {
8579       pfn = cp_build_c_cast (type, pfn, complain);
8580       return build_ptrmemfunc1 (to_type,
8581 				integer_zero_node,
8582 				pfn);
8583     }
8584 
8585   if (type_unknown_p (pfn))
8586     return instantiate_type (type, pfn, complain);
8587 
8588   fn = TREE_OPERAND (pfn, 0);
8589   gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
8590 	      /* In a template, we will have preserved the
8591 		 OFFSET_REF.  */
8592 	      || (processing_template_decl && TREE_CODE (fn) == OFFSET_REF));
8593   return make_ptrmem_cst (to_type, fn);
8594 }
8595 
8596 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
8597    given by CST.
8598 
8599    ??? There is no consistency as to the types returned for the above
8600    values.  Some code acts as if it were a sizetype and some as if it were
8601    integer_type_node.  */
8602 
8603 void
8604 expand_ptrmemfunc_cst (tree cst, tree *delta, tree *pfn)
8605 {
8606   tree type = TREE_TYPE (cst);
8607   tree fn = PTRMEM_CST_MEMBER (cst);
8608   tree ptr_class, fn_class;
8609 
8610   gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
8611 
8612   /* The class that the function belongs to.  */
8613   fn_class = DECL_CONTEXT (fn);
8614 
8615   /* The class that we're creating a pointer to member of.  */
8616   ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type);
8617 
8618   /* First, calculate the adjustment to the function's class.  */
8619   *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0,
8620 				 /*c_cast_p=*/0, tf_warning_or_error);
8621 
8622   if (!DECL_VIRTUAL_P (fn))
8623     *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type),
8624 		    build_addr_func (fn, tf_warning_or_error));
8625   else
8626     {
8627       /* If we're dealing with a virtual function, we have to adjust 'this'
8628 	 again, to point to the base which provides the vtable entry for
8629 	 fn; the call will do the opposite adjustment.  */
8630       tree orig_class = DECL_CONTEXT (fn);
8631       tree binfo = binfo_or_else (orig_class, fn_class);
8632       *delta = fold_build2 (PLUS_EXPR, TREE_TYPE (*delta),
8633 			    *delta, BINFO_OFFSET (binfo));
8634 
8635       /* We set PFN to the vtable offset at which the function can be
8636 	 found, plus one (unless ptrmemfunc_vbit_in_delta, in which
8637 	 case delta is shifted left, and then incremented).  */
8638       *pfn = DECL_VINDEX (fn);
8639       *pfn = fold_build2 (MULT_EXPR, integer_type_node, *pfn,
8640 			  TYPE_SIZE_UNIT (vtable_entry_type));
8641 
8642       switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
8643 	{
8644 	case ptrmemfunc_vbit_in_pfn:
8645 	  *pfn = fold_build2 (PLUS_EXPR, integer_type_node, *pfn,
8646 			      integer_one_node);
8647 	  break;
8648 
8649 	case ptrmemfunc_vbit_in_delta:
8650 	  *delta = fold_build2 (LSHIFT_EXPR, TREE_TYPE (*delta),
8651 				*delta, integer_one_node);
8652 	  *delta = fold_build2 (PLUS_EXPR, TREE_TYPE (*delta),
8653 				*delta, integer_one_node);
8654 	  break;
8655 
8656 	default:
8657 	  gcc_unreachable ();
8658 	}
8659 
8660       *pfn = fold_convert (TYPE_PTRMEMFUNC_FN_TYPE (type), *pfn);
8661     }
8662 }
8663 
8664 /* Return an expression for PFN from the pointer-to-member function
8665    given by T.  */
8666 
8667 static tree
8668 pfn_from_ptrmemfunc (tree t)
8669 {
8670   if (TREE_CODE (t) == PTRMEM_CST)
8671     {
8672       tree delta;
8673       tree pfn;
8674 
8675       expand_ptrmemfunc_cst (t, &delta, &pfn);
8676       if (pfn)
8677 	return pfn;
8678     }
8679 
8680   return build_ptrmemfunc_access_expr (t, pfn_identifier);
8681 }
8682 
8683 /* Return an expression for DELTA from the pointer-to-member function
8684    given by T.  */
8685 
8686 static tree
8687 delta_from_ptrmemfunc (tree t)
8688 {
8689   if (TREE_CODE (t) == PTRMEM_CST)
8690     {
8691       tree delta;
8692       tree pfn;
8693 
8694       expand_ptrmemfunc_cst (t, &delta, &pfn);
8695       if (delta)
8696 	return delta;
8697     }
8698 
8699   return build_ptrmemfunc_access_expr (t, delta_identifier);
8700 }
8701 
8702 /* Convert value RHS to type TYPE as preparation for an assignment to
8703    an lvalue of type TYPE.  ERRTYPE indicates what kind of error the
8704    implicit conversion is.  If FNDECL is non-NULL, we are doing the
8705    conversion in order to pass the PARMNUMth argument of FNDECL.
8706    If FNDECL is NULL, we are doing the conversion in function pointer
8707    argument passing, conversion in initialization, etc. */
8708 
8709 static tree
8710 convert_for_assignment (tree type, tree rhs,
8711 			impl_conv_rhs errtype, tree fndecl, int parmnum,
8712 			tsubst_flags_t complain, int flags)
8713 {
8714   tree rhstype;
8715   enum tree_code coder;
8716 
8717   /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue.  */
8718   if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
8719     rhs = TREE_OPERAND (rhs, 0);
8720 
8721   /* Handle [dcl.init.list] direct-list-initialization from
8722      single element of enumeration with a fixed underlying type.  */
8723   if (is_direct_enum_init (type, rhs))
8724     {
8725       tree elt = CONSTRUCTOR_ELT (rhs, 0)->value;
8726       if (check_narrowing (ENUM_UNDERLYING_TYPE (type), elt, complain))
8727 	{
8728 	  warning_sentinel w (warn_useless_cast);
8729 	  warning_sentinel w2 (warn_ignored_qualifiers);
8730 	  rhs = cp_build_c_cast (type, elt, complain);
8731 	}
8732       else
8733 	rhs = error_mark_node;
8734     }
8735 
8736   rhstype = TREE_TYPE (rhs);
8737   coder = TREE_CODE (rhstype);
8738 
8739   if (VECTOR_TYPE_P (type) && coder == VECTOR_TYPE
8740       && vector_types_convertible_p (type, rhstype, true))
8741     {
8742       rhs = mark_rvalue_use (rhs);
8743       return convert (type, rhs);
8744     }
8745 
8746   if (rhs == error_mark_node || rhstype == error_mark_node)
8747     return error_mark_node;
8748   if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)
8749     return error_mark_node;
8750 
8751   /* The RHS of an assignment cannot have void type.  */
8752   if (coder == VOID_TYPE)
8753     {
8754       if (complain & tf_error)
8755 	error ("void value not ignored as it ought to be");
8756       return error_mark_node;
8757     }
8758 
8759   if (c_dialect_objc ())
8760     {
8761       int parmno;
8762       tree selector;
8763       tree rname = fndecl;
8764 
8765       switch (errtype)
8766         {
8767 	  case ICR_ASSIGN:
8768 	    parmno = -1;
8769 	    break;
8770 	  case ICR_INIT:
8771 	    parmno = -2;
8772 	    break;
8773 	  default:
8774 	    selector = objc_message_selector ();
8775 	    parmno = parmnum;
8776 	    if (selector && parmno > 1)
8777 	      {
8778 		rname = selector;
8779 		parmno -= 1;
8780 	      }
8781 	}
8782 
8783       if (objc_compare_types (type, rhstype, parmno, rname))
8784 	{
8785 	  rhs = mark_rvalue_use (rhs);
8786 	  return convert (type, rhs);
8787 	}
8788     }
8789 
8790   /* [expr.ass]
8791 
8792      The expression is implicitly converted (clause _conv_) to the
8793      cv-unqualified type of the left operand.
8794 
8795      We allow bad conversions here because by the time we get to this point
8796      we are committed to doing the conversion.  If we end up doing a bad
8797      conversion, convert_like will complain.  */
8798   if (!can_convert_arg_bad (type, rhstype, rhs, flags, complain))
8799     {
8800       /* When -Wno-pmf-conversions is use, we just silently allow
8801 	 conversions from pointers-to-members to plain pointers.  If
8802 	 the conversion doesn't work, cp_convert will complain.  */
8803       if (!warn_pmf2ptr
8804 	  && TYPE_PTR_P (type)
8805 	  && TYPE_PTRMEMFUNC_P (rhstype))
8806 	rhs = cp_convert (strip_top_quals (type), rhs, complain);
8807       else
8808 	{
8809 	  if (complain & tf_error)
8810 	    {
8811 	      /* If the right-hand side has unknown type, then it is an
8812 		 overloaded function.  Call instantiate_type to get error
8813 		 messages.  */
8814 	      if (rhstype == unknown_type_node)
8815 		{
8816 		  tree r = instantiate_type (type, rhs, tf_warning_or_error);
8817 		  /* -fpermissive might allow this; recurse.  */
8818 		  if (!seen_error ())
8819 		    return convert_for_assignment (type, r, errtype, fndecl,
8820 						   parmnum, complain, flags);
8821 		}
8822 	      else if (fndecl)
8823 		{
8824 		  error_at (EXPR_LOC_OR_LOC (rhs, input_location),
8825 			    "cannot convert %qH to %qI",
8826 			    rhstype, type);
8827 		  inform (get_fndecl_argument_location (fndecl, parmnum),
8828 			  "  initializing argument %P of %qD", parmnum, fndecl);
8829 		}
8830 	      else
8831 		switch (errtype)
8832 		  {
8833 		    case ICR_DEFAULT_ARGUMENT:
8834 		      error ("cannot convert %qH to %qI in default argument",
8835 			     rhstype, type);
8836 		      break;
8837 		    case ICR_ARGPASS:
8838 		      error ("cannot convert %qH to %qI in argument passing",
8839 			     rhstype, type);
8840 		      break;
8841 		    case ICR_CONVERTING:
8842 		      error ("cannot convert %qH to %qI",
8843 			     rhstype, type);
8844 		      break;
8845 		    case ICR_INIT:
8846 		      error ("cannot convert %qH to %qI in initialization",
8847 			     rhstype, type);
8848 		      break;
8849 		    case ICR_RETURN:
8850 		      error ("cannot convert %qH to %qI in return",
8851 			     rhstype, type);
8852 		      break;
8853 		    case ICR_ASSIGN:
8854 		      error ("cannot convert %qH to %qI in assignment",
8855 			     rhstype, type);
8856 		      break;
8857 		    default:
8858 		      gcc_unreachable();
8859 		  }
8860 	      if (TYPE_PTR_P (rhstype)
8861 		  && TYPE_PTR_P (type)
8862 		  && CLASS_TYPE_P (TREE_TYPE (rhstype))
8863 		  && CLASS_TYPE_P (TREE_TYPE (type))
8864 		  && !COMPLETE_TYPE_P (TREE_TYPE (rhstype)))
8865 		inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL
8866 					      (TREE_TYPE (rhstype))),
8867 			"class type %qT is incomplete", TREE_TYPE (rhstype));
8868 	    }
8869 	  return error_mark_node;
8870 	}
8871     }
8872   if (warn_suggest_attribute_format)
8873     {
8874       const enum tree_code codel = TREE_CODE (type);
8875       if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
8876 	  && coder == codel
8877 	  && check_missing_format_attribute (type, rhstype)
8878 	  && (complain & tf_warning))
8879 	switch (errtype)
8880 	  {
8881 	    case ICR_ARGPASS:
8882 	    case ICR_DEFAULT_ARGUMENT:
8883 	      if (fndecl)
8884 		warning (OPT_Wsuggest_attribute_format,
8885 			 "parameter %qP of %qD might be a candidate "
8886 			 "for a format attribute", parmnum, fndecl);
8887 	      else
8888 		warning (OPT_Wsuggest_attribute_format,
8889 			 "parameter might be a candidate "
8890 			 "for a format attribute");
8891 	      break;
8892 	    case ICR_CONVERTING:
8893 	      warning (OPT_Wsuggest_attribute_format,
8894 		       "target of conversion might be a candidate "
8895 		       "for a format attribute");
8896 	      break;
8897 	    case ICR_INIT:
8898 	      warning (OPT_Wsuggest_attribute_format,
8899 		       "target of initialization might be a candidate "
8900 		       "for a format attribute");
8901 	      break;
8902 	    case ICR_RETURN:
8903 	      warning (OPT_Wsuggest_attribute_format,
8904 		       "return type might be a candidate "
8905 		       "for a format attribute");
8906 	      break;
8907 	    case ICR_ASSIGN:
8908 	      warning (OPT_Wsuggest_attribute_format,
8909 		       "left-hand side of assignment might be a candidate "
8910 		       "for a format attribute");
8911 	      break;
8912 	    default:
8913 	      gcc_unreachable();
8914 	  }
8915     }
8916 
8917   /* If -Wparentheses, warn about a = b = c when a has type bool and b
8918      does not.  */
8919   if (warn_parentheses
8920       && TREE_CODE (type) == BOOLEAN_TYPE
8921       && TREE_CODE (rhs) == MODIFY_EXPR
8922       && !TREE_NO_WARNING (rhs)
8923       && TREE_CODE (TREE_TYPE (rhs)) != BOOLEAN_TYPE
8924       && (complain & tf_warning))
8925     {
8926       location_t loc = EXPR_LOC_OR_LOC (rhs, input_location);
8927 
8928       warning_at (loc, OPT_Wparentheses,
8929 		  "suggest parentheses around assignment used as truth value");
8930       TREE_NO_WARNING (rhs) = 1;
8931     }
8932 
8933   return perform_implicit_conversion_flags (strip_top_quals (type), rhs,
8934 					    complain, flags);
8935 }
8936 
8937 /* Convert RHS to be of type TYPE.
8938    If EXP is nonzero, it is the target of the initialization.
8939    ERRTYPE indicates what kind of error the implicit conversion is.
8940 
8941    Two major differences between the behavior of
8942    `convert_for_assignment' and `convert_for_initialization'
8943    are that references are bashed in the former, while
8944    copied in the latter, and aggregates are assigned in
8945    the former (operator=) while initialized in the
8946    latter (X(X&)).
8947 
8948    If using constructor make sure no conversion operator exists, if one does
8949    exist, an ambiguity exists.  */
8950 
8951 tree
8952 convert_for_initialization (tree exp, tree type, tree rhs, int flags,
8953 			    impl_conv_rhs errtype, tree fndecl, int parmnum,
8954                             tsubst_flags_t complain)
8955 {
8956   enum tree_code codel = TREE_CODE (type);
8957   tree rhstype;
8958   enum tree_code coder;
8959 
8960   /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
8961      Strip such NOP_EXPRs, since RHS is used in non-lvalue context.  */
8962   if (TREE_CODE (rhs) == NOP_EXPR
8963       && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
8964       && codel != REFERENCE_TYPE)
8965     rhs = TREE_OPERAND (rhs, 0);
8966 
8967   if (type == error_mark_node
8968       || rhs == error_mark_node
8969       || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
8970     return error_mark_node;
8971 
8972   if (MAYBE_CLASS_TYPE_P (non_reference (type)))
8973     ;
8974   else if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
8975 	    && TREE_CODE (type) != ARRAY_TYPE
8976 	    && (TREE_CODE (type) != REFERENCE_TYPE
8977 		|| TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
8978 	   || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
8979 	       && !TYPE_REFFN_P (type))
8980 	   || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
8981     rhs = decay_conversion (rhs, complain);
8982 
8983   rhstype = TREE_TYPE (rhs);
8984   coder = TREE_CODE (rhstype);
8985 
8986   if (coder == ERROR_MARK)
8987     return error_mark_node;
8988 
8989   /* We accept references to incomplete types, so we can
8990      return here before checking if RHS is of complete type.  */
8991 
8992   if (codel == REFERENCE_TYPE)
8993     {
8994       /* This should eventually happen in convert_arguments.  */
8995       int savew = 0, savee = 0;
8996 
8997       if (fndecl)
8998 	savew = warningcount + werrorcount, savee = errorcount;
8999       rhs = initialize_reference (type, rhs, flags, complain);
9000 
9001       if (fndecl
9002 	  && (warningcount + werrorcount > savew || errorcount > savee))
9003 	inform (DECL_SOURCE_LOCATION (fndecl),
9004 		"in passing argument %P of %qD", parmnum, fndecl);
9005 
9006       return rhs;
9007     }
9008 
9009   if (exp != 0)
9010     exp = require_complete_type_sfinae (exp, complain);
9011   if (exp == error_mark_node)
9012     return error_mark_node;
9013 
9014   rhstype = non_reference (rhstype);
9015 
9016   type = complete_type (type);
9017 
9018   if (DIRECT_INIT_EXPR_P (type, rhs))
9019     /* Don't try to do copy-initialization if we already have
9020        direct-initialization.  */
9021     return rhs;
9022 
9023   if (MAYBE_CLASS_TYPE_P (type))
9024     return perform_implicit_conversion_flags (type, rhs, complain, flags);
9025 
9026   return convert_for_assignment (type, rhs, errtype, fndecl, parmnum,
9027 				 complain, flags);
9028 }
9029 
9030 /* If RETVAL is the address of, or a reference to, a local variable or
9031    temporary give an appropriate warning and return true.  */
9032 
9033 static bool
9034 maybe_warn_about_returning_address_of_local (tree retval)
9035 {
9036   tree valtype = TREE_TYPE (DECL_RESULT (current_function_decl));
9037   tree whats_returned = fold_for_warn (retval);
9038 
9039   for (;;)
9040     {
9041       if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
9042 	whats_returned = TREE_OPERAND (whats_returned, 1);
9043       else if (CONVERT_EXPR_P (whats_returned)
9044 	       || TREE_CODE (whats_returned) == NON_LVALUE_EXPR)
9045 	whats_returned = TREE_OPERAND (whats_returned, 0);
9046       else
9047 	break;
9048     }
9049 
9050   if (TREE_CODE (whats_returned) != ADDR_EXPR)
9051     return false;
9052   whats_returned = TREE_OPERAND (whats_returned, 0);
9053 
9054   while (TREE_CODE (whats_returned) == COMPONENT_REF
9055 	 || TREE_CODE (whats_returned) == ARRAY_REF)
9056     whats_returned = TREE_OPERAND (whats_returned, 0);
9057 
9058   if (TREE_CODE (valtype) == REFERENCE_TYPE)
9059     {
9060       if (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
9061 	  || TREE_CODE (whats_returned) == TARGET_EXPR)
9062 	{
9063 	  warning (OPT_Wreturn_local_addr, "returning reference to temporary");
9064 	  return true;
9065 	}
9066       if (VAR_P (whats_returned)
9067 	  && DECL_NAME (whats_returned)
9068 	  && TEMP_NAME_P (DECL_NAME (whats_returned)))
9069 	{
9070 	  warning (OPT_Wreturn_local_addr, "reference to non-lvalue returned");
9071 	  return true;
9072 	}
9073     }
9074 
9075   if (DECL_P (whats_returned)
9076       && DECL_NAME (whats_returned)
9077       && DECL_FUNCTION_SCOPE_P (whats_returned)
9078       && !is_capture_proxy (whats_returned)
9079       && !(TREE_STATIC (whats_returned)
9080 	   || TREE_PUBLIC (whats_returned)))
9081     {
9082       if (VAR_P (whats_returned)
9083 	  && DECL_DECOMPOSITION_P (whats_returned)
9084 	  && DECL_DECOMP_BASE (whats_returned)
9085 	  && DECL_HAS_VALUE_EXPR_P (whats_returned))
9086 	{
9087 	  /* When returning address of a structured binding, if the structured
9088 	     binding is not a reference, continue normally, if it is a
9089 	     reference, recurse on the initializer of the structured
9090 	     binding.  */
9091 	  tree base = DECL_DECOMP_BASE (whats_returned);
9092 	  if (TREE_CODE (TREE_TYPE (base)) == REFERENCE_TYPE)
9093 	    {
9094 	      tree init = DECL_INITIAL (base);
9095 	      return maybe_warn_about_returning_address_of_local (init);
9096 	    }
9097 	}
9098       if (TREE_CODE (valtype) == REFERENCE_TYPE)
9099 	warning_at (DECL_SOURCE_LOCATION (whats_returned),
9100 		    OPT_Wreturn_local_addr,
9101 		    "reference to local variable %qD returned",
9102 		    whats_returned);
9103       else if (TREE_CODE (whats_returned) == LABEL_DECL)
9104 	warning_at (DECL_SOURCE_LOCATION (whats_returned),
9105 		    OPT_Wreturn_local_addr, "address of label %qD returned",
9106 		    whats_returned);
9107       else
9108 	warning_at (DECL_SOURCE_LOCATION (whats_returned),
9109 		    OPT_Wreturn_local_addr, "address of local variable %qD "
9110 		    "returned", whats_returned);
9111       return true;
9112     }
9113 
9114   return false;
9115 }
9116 
9117 /* Check that returning RETVAL from the current function is valid.
9118    Return an expression explicitly showing all conversions required to
9119    change RETVAL into the function return type, and to assign it to
9120    the DECL_RESULT for the function.  Set *NO_WARNING to true if
9121    code reaches end of non-void function warning shouldn't be issued
9122    on this RETURN_EXPR.  */
9123 
9124 tree
9125 check_return_expr (tree retval, bool *no_warning)
9126 {
9127   tree result;
9128   /* The type actually returned by the function.  */
9129   tree valtype;
9130   /* The type the function is declared to return, or void if
9131      the declared type is incomplete.  */
9132   tree functype;
9133   int fn_returns_value_p;
9134   bool named_return_value_okay_p;
9135 
9136   *no_warning = false;
9137 
9138   /* A `volatile' function is one that isn't supposed to return, ever.
9139      (This is a G++ extension, used to get better code for functions
9140      that call the `volatile' function.)  */
9141   if (TREE_THIS_VOLATILE (current_function_decl))
9142     warning (0, "function declared %<noreturn%> has a %<return%> statement");
9143 
9144   /* Check for various simple errors.  */
9145   if (DECL_DESTRUCTOR_P (current_function_decl))
9146     {
9147       if (retval)
9148 	error ("returning a value from a destructor");
9149       return NULL_TREE;
9150     }
9151   else if (DECL_CONSTRUCTOR_P (current_function_decl))
9152     {
9153       if (in_function_try_handler)
9154 	/* If a return statement appears in a handler of the
9155 	   function-try-block of a constructor, the program is ill-formed.  */
9156 	error ("cannot return from a handler of a function-try-block of a constructor");
9157       else if (retval)
9158 	/* You can't return a value from a constructor.  */
9159 	error ("returning a value from a constructor");
9160       return NULL_TREE;
9161     }
9162 
9163   const tree saved_retval = retval;
9164 
9165   if (processing_template_decl)
9166     {
9167       current_function_returns_value = 1;
9168 
9169       if (check_for_bare_parameter_packs (retval))
9170 	return error_mark_node;
9171 
9172       /* If one of the types might be void, we can't tell whether we're
9173 	 returning a value.  */
9174       if ((WILDCARD_TYPE_P (TREE_TYPE (DECL_RESULT (current_function_decl)))
9175 	   && !current_function_auto_return_pattern)
9176 	  || (retval != NULL_TREE
9177 	      && (TREE_TYPE (retval) == NULL_TREE
9178 		  || WILDCARD_TYPE_P (TREE_TYPE (retval)))))
9179 	goto dependent;
9180     }
9181 
9182   functype = TREE_TYPE (TREE_TYPE (current_function_decl));
9183 
9184   /* Deduce auto return type from a return statement.  */
9185   if (current_function_auto_return_pattern)
9186     {
9187       tree auto_node;
9188       tree type;
9189 
9190       if (!retval && !is_auto (current_function_auto_return_pattern))
9191 	{
9192 	  /* Give a helpful error message.  */
9193 	  error ("return-statement with no value, in function returning %qT",
9194 		 current_function_auto_return_pattern);
9195 	  inform (input_location, "only plain %<auto%> return type can be "
9196 		  "deduced to %<void%>");
9197 	  type = error_mark_node;
9198 	}
9199       else if (retval && BRACE_ENCLOSED_INITIALIZER_P (retval))
9200 	{
9201 	  error ("returning initializer list");
9202 	  type = error_mark_node;
9203 	}
9204       else
9205 	{
9206 	  if (!retval)
9207 	    retval = void_node;
9208 	  auto_node = type_uses_auto (current_function_auto_return_pattern);
9209 	  type = do_auto_deduction (current_function_auto_return_pattern,
9210 				    retval, auto_node);
9211 	}
9212 
9213       if (type == error_mark_node)
9214 	/* Leave it.  */;
9215       else if (functype == current_function_auto_return_pattern)
9216 	apply_deduced_return_type (current_function_decl, type);
9217       else if (!same_type_p (type, functype))
9218 	{
9219 	  if (LAMBDA_FUNCTION_P (current_function_decl))
9220 	    error ("inconsistent types %qT and %qT deduced for "
9221 		   "lambda return type", functype, type);
9222 	  else
9223 	    error ("inconsistent deduction for auto return type: "
9224 		   "%qT and then %qT", functype, type);
9225 	}
9226       functype = type;
9227     }
9228 
9229   result = DECL_RESULT (current_function_decl);
9230   valtype = TREE_TYPE (result);
9231   gcc_assert (valtype != NULL_TREE);
9232   fn_returns_value_p = !VOID_TYPE_P (valtype);
9233 
9234   /* Check for a return statement with no return value in a function
9235      that's supposed to return a value.  */
9236   if (!retval && fn_returns_value_p)
9237     {
9238       if (functype != error_mark_node)
9239 	permerror (input_location, "return-statement with no value, in "
9240 		   "function returning %qT", valtype);
9241       /* Remember that this function did return.  */
9242       current_function_returns_value = 1;
9243       /* And signal caller that TREE_NO_WARNING should be set on the
9244 	 RETURN_EXPR to avoid control reaches end of non-void function
9245 	 warnings in tree-cfg.c.  */
9246       *no_warning = true;
9247     }
9248   /* Check for a return statement with a value in a function that
9249      isn't supposed to return a value.  */
9250   else if (retval && !fn_returns_value_p)
9251     {
9252       if (VOID_TYPE_P (TREE_TYPE (retval)))
9253 	/* You can return a `void' value from a function of `void'
9254 	   type.  In that case, we have to evaluate the expression for
9255 	   its side-effects.  */
9256 	finish_expr_stmt (retval);
9257       else
9258 	permerror (input_location,
9259 		   "return-statement with a value, in function "
9260 		   "returning %qT", valtype);
9261       current_function_returns_null = 1;
9262 
9263       /* There's really no value to return, after all.  */
9264       return NULL_TREE;
9265     }
9266   else if (!retval)
9267     /* Remember that this function can sometimes return without a
9268        value.  */
9269     current_function_returns_null = 1;
9270   else
9271     /* Remember that this function did return a value.  */
9272     current_function_returns_value = 1;
9273 
9274   /* Check for erroneous operands -- but after giving ourselves a
9275      chance to provide an error about returning a value from a void
9276      function.  */
9277   if (error_operand_p (retval))
9278     {
9279       current_function_return_value = error_mark_node;
9280       return error_mark_node;
9281     }
9282 
9283   /* Only operator new(...) throw(), can return NULL [expr.new/13].  */
9284   if (IDENTIFIER_NEW_OP_P (DECL_NAME (current_function_decl))
9285       && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl))
9286       && ! flag_check_new
9287       && retval && null_ptr_cst_p (retval))
9288     warning (0, "%<operator new%> must not return NULL unless it is "
9289 	     "declared %<throw()%> (or -fcheck-new is in effect)");
9290 
9291   /* Effective C++ rule 15.  See also start_function.  */
9292   if (warn_ecpp
9293       && DECL_NAME (current_function_decl) == assign_op_identifier
9294       && !type_dependent_expression_p (retval))
9295     {
9296       bool warn = true;
9297 
9298       /* The function return type must be a reference to the current
9299 	class.  */
9300       if (TREE_CODE (valtype) == REFERENCE_TYPE
9301 	  && same_type_ignoring_top_level_qualifiers_p
9302 	      (TREE_TYPE (valtype), TREE_TYPE (current_class_ref)))
9303 	{
9304 	  /* Returning '*this' is obviously OK.  */
9305 	  if (retval == current_class_ref)
9306 	    warn = false;
9307 	  /* If we are calling a function whose return type is the same of
9308 	     the current class reference, it is ok.  */
9309 	  else if (INDIRECT_REF_P (retval)
9310 		   && TREE_CODE (TREE_OPERAND (retval, 0)) == CALL_EXPR)
9311 	    warn = false;
9312 	}
9313 
9314       if (warn)
9315 	warning (OPT_Weffc__, "%<operator=%> should return a reference to %<*this%>");
9316     }
9317 
9318   if (dependent_type_p (functype)
9319       || type_dependent_expression_p (retval))
9320     {
9321     dependent:
9322       /* We should not have changed the return value.  */
9323       gcc_assert (retval == saved_retval);
9324       return retval;
9325     }
9326 
9327   /* The fabled Named Return Value optimization, as per [class.copy]/15:
9328 
9329      [...]      For  a function with a class return type, if the expression
9330      in the return statement is the name of a local  object,  and  the  cv-
9331      unqualified  type  of  the  local  object  is the same as the function
9332      return type, an implementation is permitted to omit creating the  tem-
9333      porary  object  to  hold  the function return value [...]
9334 
9335      So, if this is a value-returning function that always returns the same
9336      local variable, remember it.
9337 
9338      It might be nice to be more flexible, and choose the first suitable
9339      variable even if the function sometimes returns something else, but
9340      then we run the risk of clobbering the variable we chose if the other
9341      returned expression uses the chosen variable somehow.  And people expect
9342      this restriction, anyway.  (jason 2000-11-19)
9343 
9344      See finish_function and finalize_nrv for the rest of this optimization.  */
9345 
9346   named_return_value_okay_p =
9347     (retval != NULL_TREE
9348      && !processing_template_decl
9349      /* Must be a local, automatic variable.  */
9350      && VAR_P (retval)
9351      && DECL_CONTEXT (retval) == current_function_decl
9352      && ! TREE_STATIC (retval)
9353      /* And not a lambda or anonymous union proxy.  */
9354      && !DECL_HAS_VALUE_EXPR_P (retval)
9355      && (DECL_ALIGN (retval) <= DECL_ALIGN (result))
9356      /* The cv-unqualified type of the returned value must be the
9357         same as the cv-unqualified return type of the
9358         function.  */
9359      && same_type_p ((TYPE_MAIN_VARIANT (TREE_TYPE (retval))),
9360                      (TYPE_MAIN_VARIANT (functype)))
9361      /* And the returned value must be non-volatile.  */
9362      && ! TYPE_VOLATILE (TREE_TYPE (retval)));
9363 
9364   if (fn_returns_value_p && flag_elide_constructors)
9365     {
9366       if (named_return_value_okay_p
9367           && (current_function_return_value == NULL_TREE
9368               || current_function_return_value == retval))
9369 	current_function_return_value = retval;
9370       else
9371 	current_function_return_value = error_mark_node;
9372     }
9373 
9374   /* We don't need to do any conversions when there's nothing being
9375      returned.  */
9376   if (!retval)
9377     return NULL_TREE;
9378 
9379   /* Do any required conversions.  */
9380   if (retval == result || DECL_CONSTRUCTOR_P (current_function_decl))
9381     /* No conversions are required.  */
9382     ;
9383   else
9384     {
9385       int flags = LOOKUP_NORMAL | LOOKUP_ONLYCONVERTING;
9386 
9387       /* The functype's return type will have been set to void, if it
9388 	 was an incomplete type.  Just treat this as 'return;' */
9389       if (VOID_TYPE_P (functype))
9390 	return error_mark_node;
9391 
9392       /* If we had an id-expression obfuscated by force_paren_expr, we need
9393 	 to undo it so we can try to treat it as an rvalue below.  */
9394       retval = maybe_undo_parenthesized_ref (retval);
9395 
9396       if (processing_template_decl)
9397 	retval = build_non_dependent_expr (retval);
9398 
9399       /* Under C++11 [12.8/32 class.copy], a returned lvalue is sometimes
9400 	 treated as an rvalue for the purposes of overload resolution to
9401 	 favor move constructors over copy constructors.
9402 
9403          Note that these conditions are similar to, but not as strict as,
9404 	 the conditions for the named return value optimization.  */
9405       bool converted = false;
9406       if ((cxx_dialect != cxx98)
9407           && ((VAR_P (retval) && !DECL_HAS_VALUE_EXPR_P (retval))
9408 	      || TREE_CODE (retval) == PARM_DECL)
9409 	  && DECL_CONTEXT (retval) == current_function_decl
9410 	  && !TREE_STATIC (retval)
9411 	  /* This is only interesting for class type.  */
9412 	  && CLASS_TYPE_P (functype))
9413 	{
9414 	  tree moved = move (retval);
9415 	  moved = convert_for_initialization
9416 	    (NULL_TREE, functype, moved, flags|LOOKUP_PREFER_RVALUE,
9417 	     ICR_RETURN, NULL_TREE, 0, tf_none);
9418 	  if (moved != error_mark_node)
9419 	    {
9420 	      retval = moved;
9421 	      converted = true;
9422 	    }
9423 	}
9424 
9425       /* First convert the value to the function's return type, then
9426 	 to the type of return value's location to handle the
9427 	 case that functype is smaller than the valtype.  */
9428       if (!converted)
9429 	retval = convert_for_initialization
9430 	  (NULL_TREE, functype, retval, flags, ICR_RETURN, NULL_TREE, 0,
9431 	   tf_warning_or_error);
9432       retval = convert (valtype, retval);
9433 
9434       /* If the conversion failed, treat this just like `return;'.  */
9435       if (retval == error_mark_node)
9436 	return retval;
9437       /* We can't initialize a register from a AGGR_INIT_EXPR.  */
9438       else if (! cfun->returns_struct
9439 	       && TREE_CODE (retval) == TARGET_EXPR
9440 	       && TREE_CODE (TREE_OPERAND (retval, 1)) == AGGR_INIT_EXPR)
9441 	retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
9442 			 TREE_OPERAND (retval, 0));
9443       else if (!processing_template_decl
9444 	       && maybe_warn_about_returning_address_of_local (retval))
9445 	retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
9446 			 build_zero_cst (TREE_TYPE (retval)));
9447     }
9448 
9449   if (processing_template_decl)
9450     return saved_retval;
9451 
9452   /* Actually copy the value returned into the appropriate location.  */
9453   if (retval && retval != result)
9454     retval = build2 (INIT_EXPR, TREE_TYPE (result), result, retval);
9455 
9456   return retval;
9457 }
9458 
9459 
9460 /* Returns nonzero if the pointer-type FROM can be converted to the
9461    pointer-type TO via a qualification conversion.  If CONSTP is -1,
9462    then we return nonzero if the pointers are similar, and the
9463    cv-qualification signature of FROM is a proper subset of that of TO.
9464 
9465    If CONSTP is positive, then all outer pointers have been
9466    const-qualified.  */
9467 
9468 static int
9469 comp_ptr_ttypes_real (tree to, tree from, int constp)
9470 {
9471   bool to_more_cv_qualified = false;
9472   bool is_opaque_pointer = false;
9473 
9474   for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
9475     {
9476       if (TREE_CODE (to) != TREE_CODE (from))
9477 	return 0;
9478 
9479       if (TREE_CODE (from) == OFFSET_TYPE
9480 	  && !same_type_p (TYPE_OFFSET_BASETYPE (from),
9481 			   TYPE_OFFSET_BASETYPE (to)))
9482 	return 0;
9483 
9484       /* Const and volatile mean something different for function types,
9485 	 so the usual checks are not appropriate.  */
9486       if (TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
9487 	{
9488 	  if (!at_least_as_qualified_p (to, from))
9489 	    return 0;
9490 
9491 	  if (!at_least_as_qualified_p (from, to))
9492 	    {
9493 	      if (constp == 0)
9494 		return 0;
9495 	      to_more_cv_qualified = true;
9496 	    }
9497 
9498 	  if (constp > 0)
9499 	    constp &= TYPE_READONLY (to);
9500 	}
9501 
9502       if (VECTOR_TYPE_P (to))
9503 	is_opaque_pointer = vector_targets_convertible_p (to, from);
9504 
9505       if (!TYPE_PTR_P (to) && !TYPE_PTRDATAMEM_P (to))
9506 	return ((constp >= 0 || to_more_cv_qualified)
9507 		&& (is_opaque_pointer
9508 		    || same_type_ignoring_top_level_qualifiers_p (to, from)));
9509     }
9510 }
9511 
9512 /* When comparing, say, char ** to char const **, this function takes
9513    the 'char *' and 'char const *'.  Do not pass non-pointer/reference
9514    types to this function.  */
9515 
9516 int
9517 comp_ptr_ttypes (tree to, tree from)
9518 {
9519   return comp_ptr_ttypes_real (to, from, 1);
9520 }
9521 
9522 /* Returns true iff FNTYPE is a non-class type that involves
9523    error_mark_node.  We can get FUNCTION_TYPE with buried error_mark_node
9524    if a parameter type is ill-formed.  */
9525 
9526 bool
9527 error_type_p (const_tree type)
9528 {
9529   tree t;
9530 
9531   switch (TREE_CODE (type))
9532     {
9533     case ERROR_MARK:
9534       return true;
9535 
9536     case POINTER_TYPE:
9537     case REFERENCE_TYPE:
9538     case OFFSET_TYPE:
9539       return error_type_p (TREE_TYPE (type));
9540 
9541     case FUNCTION_TYPE:
9542     case METHOD_TYPE:
9543       if (error_type_p (TREE_TYPE (type)))
9544 	return true;
9545       for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
9546 	if (error_type_p (TREE_VALUE (t)))
9547 	  return true;
9548       return false;
9549 
9550     case RECORD_TYPE:
9551       if (TYPE_PTRMEMFUNC_P (type))
9552 	return error_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type));
9553       return false;
9554 
9555     default:
9556       return false;
9557     }
9558 }
9559 
9560 /* Returns true if to and from are (possibly multi-level) pointers to the same
9561    type or inheritance-related types, regardless of cv-quals.  */
9562 
9563 bool
9564 ptr_reasonably_similar (const_tree to, const_tree from)
9565 {
9566   for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
9567     {
9568       /* Any target type is similar enough to void.  */
9569       if (VOID_TYPE_P (to))
9570 	return !error_type_p (from);
9571       if (VOID_TYPE_P (from))
9572 	return !error_type_p (to);
9573 
9574       if (TREE_CODE (to) != TREE_CODE (from))
9575 	return false;
9576 
9577       if (TREE_CODE (from) == OFFSET_TYPE
9578 	  && comptypes (TYPE_OFFSET_BASETYPE (to),
9579 			TYPE_OFFSET_BASETYPE (from),
9580 			COMPARE_BASE | COMPARE_DERIVED))
9581 	continue;
9582 
9583       if (VECTOR_TYPE_P (to)
9584 	  && vector_types_convertible_p (to, from, false))
9585 	return true;
9586 
9587       if (TREE_CODE (to) == INTEGER_TYPE
9588 	  && TYPE_PRECISION (to) == TYPE_PRECISION (from))
9589 	return true;
9590 
9591       if (TREE_CODE (to) == FUNCTION_TYPE)
9592 	return !error_type_p (to) && !error_type_p (from);
9593 
9594       if (!TYPE_PTR_P (to))
9595 	{
9596 	  /* When either type is incomplete avoid DERIVED_FROM_P,
9597 	     which may call complete_type (c++/57942).  */
9598 	  bool b = !COMPLETE_TYPE_P (to) || !COMPLETE_TYPE_P (from);
9599 	  return comptypes
9600 	    (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from),
9601 	     b ? COMPARE_STRICT : COMPARE_BASE | COMPARE_DERIVED);
9602 	}
9603     }
9604 }
9605 
9606 /* Return true if TO and FROM (both of which are POINTER_TYPEs or
9607    pointer-to-member types) are the same, ignoring cv-qualification at
9608    all levels.  */
9609 
9610 bool
9611 comp_ptr_ttypes_const (tree to, tree from)
9612 {
9613   bool is_opaque_pointer = false;
9614 
9615   for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
9616     {
9617       if (TREE_CODE (to) != TREE_CODE (from))
9618 	return false;
9619 
9620       if (TREE_CODE (from) == OFFSET_TYPE
9621 	  && same_type_p (TYPE_OFFSET_BASETYPE (from),
9622 			  TYPE_OFFSET_BASETYPE (to)))
9623 	  continue;
9624 
9625       if (VECTOR_TYPE_P (to))
9626 	is_opaque_pointer = vector_targets_convertible_p (to, from);
9627 
9628       if (!TYPE_PTR_P (to))
9629 	return (is_opaque_pointer
9630 		|| same_type_ignoring_top_level_qualifiers_p (to, from));
9631     }
9632 }
9633 
9634 /* Returns the type qualifiers for this type, including the qualifiers on the
9635    elements for an array type.  */
9636 
9637 int
9638 cp_type_quals (const_tree type)
9639 {
9640   int quals;
9641   /* This CONST_CAST is okay because strip_array_types returns its
9642      argument unmodified and we assign it to a const_tree.  */
9643   type = strip_array_types (CONST_CAST_TREE (type));
9644   if (type == error_mark_node
9645       /* Quals on a FUNCTION_TYPE are memfn quals.  */
9646       || TREE_CODE (type) == FUNCTION_TYPE)
9647     return TYPE_UNQUALIFIED;
9648   quals = TYPE_QUALS (type);
9649   /* METHOD and REFERENCE_TYPEs should never have quals.  */
9650   gcc_assert ((TREE_CODE (type) != METHOD_TYPE
9651 	       && TREE_CODE (type) != REFERENCE_TYPE)
9652 	      || ((quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE))
9653 		  == TYPE_UNQUALIFIED));
9654   return quals;
9655 }
9656 
9657 /* Returns the function-ref-qualifier for TYPE */
9658 
9659 cp_ref_qualifier
9660 type_memfn_rqual (const_tree type)
9661 {
9662   gcc_assert (TREE_CODE (type) == FUNCTION_TYPE
9663               || TREE_CODE (type) == METHOD_TYPE);
9664 
9665   if (!FUNCTION_REF_QUALIFIED (type))
9666     return REF_QUAL_NONE;
9667   else if (FUNCTION_RVALUE_QUALIFIED (type))
9668     return REF_QUAL_RVALUE;
9669   else
9670     return REF_QUAL_LVALUE;
9671 }
9672 
9673 /* Returns the function-cv-quals for TYPE, which must be a FUNCTION_TYPE or
9674    METHOD_TYPE.  */
9675 
9676 int
9677 type_memfn_quals (const_tree type)
9678 {
9679   if (TREE_CODE (type) == FUNCTION_TYPE)
9680     return TYPE_QUALS (type);
9681   else if (TREE_CODE (type) == METHOD_TYPE)
9682     return cp_type_quals (class_of_this_parm (type));
9683   else
9684     gcc_unreachable ();
9685 }
9686 
9687 /* Returns the FUNCTION_TYPE TYPE with its function-cv-quals changed to
9688    MEMFN_QUALS and its ref-qualifier to RQUAL. */
9689 
9690 tree
9691 apply_memfn_quals (tree type, cp_cv_quals memfn_quals, cp_ref_qualifier rqual)
9692 {
9693   /* Could handle METHOD_TYPE here if necessary.  */
9694   gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
9695   if (TYPE_QUALS (type) == memfn_quals
9696       && type_memfn_rqual (type) == rqual)
9697     return type;
9698 
9699   /* This should really have a different TYPE_MAIN_VARIANT, but that gets
9700      complex.  */
9701   tree result = build_qualified_type (type, memfn_quals);
9702   return build_ref_qualified_type (result, rqual);
9703 }
9704 
9705 /* Returns nonzero if TYPE is const or volatile.  */
9706 
9707 bool
9708 cv_qualified_p (const_tree type)
9709 {
9710   int quals = cp_type_quals (type);
9711   return (quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)) != 0;
9712 }
9713 
9714 /* Returns nonzero if the TYPE contains a mutable member.  */
9715 
9716 bool
9717 cp_has_mutable_p (const_tree type)
9718 {
9719   /* This CONST_CAST is okay because strip_array_types returns its
9720      argument unmodified and we assign it to a const_tree.  */
9721   type = strip_array_types (CONST_CAST_TREE(type));
9722 
9723   return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type);
9724 }
9725 
9726 /* Set TREE_READONLY and TREE_VOLATILE on DECL as indicated by the
9727    TYPE_QUALS.  For a VAR_DECL, this may be an optimistic
9728    approximation.  In particular, consider:
9729 
9730      int f();
9731      struct S { int i; };
9732      const S s = { f(); }
9733 
9734    Here, we will make "s" as TREE_READONLY (because it is declared
9735    "const") -- only to reverse ourselves upon seeing that the
9736    initializer is non-constant.  */
9737 
9738 void
9739 cp_apply_type_quals_to_decl (int type_quals, tree decl)
9740 {
9741   tree type = TREE_TYPE (decl);
9742 
9743   if (type == error_mark_node)
9744     return;
9745 
9746   if (TREE_CODE (decl) == TYPE_DECL)
9747     return;
9748 
9749   gcc_assert (!(TREE_CODE (type) == FUNCTION_TYPE
9750 		&& type_quals != TYPE_UNQUALIFIED));
9751 
9752   /* Avoid setting TREE_READONLY incorrectly.  */
9753   /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
9754      constructor can produce constant init, so rely on cp_finish_decl to
9755      clear TREE_READONLY if the variable has non-constant init.  */
9756 
9757   /* If the type has (or might have) a mutable component, that component
9758      might be modified.  */
9759   if (TYPE_HAS_MUTABLE_P (type) || !COMPLETE_TYPE_P (type))
9760     type_quals &= ~TYPE_QUAL_CONST;
9761 
9762   c_apply_type_quals_to_decl (type_quals, decl);
9763 }
9764 
9765 /* Subroutine of casts_away_constness.  Make T1 and T2 point at
9766    exemplar types such that casting T1 to T2 is casting away constness
9767    if and only if there is no implicit conversion from T1 to T2.  */
9768 
9769 static void
9770 casts_away_constness_r (tree *t1, tree *t2, tsubst_flags_t complain)
9771 {
9772   int quals1;
9773   int quals2;
9774 
9775   /* [expr.const.cast]
9776 
9777      For multi-level pointer to members and multi-level mixed pointers
9778      and pointers to members (conv.qual), the "member" aspect of a
9779      pointer to member level is ignored when determining if a const
9780      cv-qualifier has been cast away.  */
9781   /* [expr.const.cast]
9782 
9783      For  two  pointer types:
9784 
9785 	    X1 is T1cv1,1 * ... cv1,N *   where T1 is not a pointer type
9786 	    X2 is T2cv2,1 * ... cv2,M *   where T2 is not a pointer type
9787 	    K is min(N,M)
9788 
9789      casting from X1 to X2 casts away constness if, for a non-pointer
9790      type T there does not exist an implicit conversion (clause
9791      _conv_) from:
9792 
9793 	    Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
9794 
9795      to
9796 
9797 	    Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *.  */
9798   if ((!TYPE_PTR_P (*t1) && !TYPE_PTRDATAMEM_P (*t1))
9799       || (!TYPE_PTR_P (*t2) && !TYPE_PTRDATAMEM_P (*t2)))
9800     {
9801       *t1 = cp_build_qualified_type (void_type_node,
9802 				     cp_type_quals (*t1));
9803       *t2 = cp_build_qualified_type (void_type_node,
9804 				     cp_type_quals (*t2));
9805       return;
9806     }
9807 
9808   quals1 = cp_type_quals (*t1);
9809   quals2 = cp_type_quals (*t2);
9810 
9811   if (TYPE_PTRDATAMEM_P (*t1))
9812     *t1 = TYPE_PTRMEM_POINTED_TO_TYPE (*t1);
9813   else
9814     *t1 = TREE_TYPE (*t1);
9815   if (TYPE_PTRDATAMEM_P (*t2))
9816     *t2 = TYPE_PTRMEM_POINTED_TO_TYPE (*t2);
9817   else
9818     *t2 = TREE_TYPE (*t2);
9819 
9820   casts_away_constness_r (t1, t2, complain);
9821   *t1 = build_pointer_type (*t1);
9822   *t2 = build_pointer_type (*t2);
9823   *t1 = cp_build_qualified_type (*t1, quals1);
9824   *t2 = cp_build_qualified_type (*t2, quals2);
9825 }
9826 
9827 /* Returns nonzero if casting from TYPE1 to TYPE2 casts away
9828    constness.
9829 
9830    ??? This function returns non-zero if casting away qualifiers not
9831    just const.  We would like to return to the caller exactly which
9832    qualifiers are casted away to give more accurate diagnostics.
9833 */
9834 
9835 static bool
9836 casts_away_constness (tree t1, tree t2, tsubst_flags_t complain)
9837 {
9838   if (TREE_CODE (t2) == REFERENCE_TYPE)
9839     {
9840       /* [expr.const.cast]
9841 
9842 	 Casting from an lvalue of type T1 to an lvalue of type T2
9843 	 using a reference cast casts away constness if a cast from an
9844 	 rvalue of type "pointer to T1" to the type "pointer to T2"
9845 	 casts away constness.  */
9846       t1 = (TREE_CODE (t1) == REFERENCE_TYPE ? TREE_TYPE (t1) : t1);
9847       return casts_away_constness (build_pointer_type (t1),
9848 				   build_pointer_type (TREE_TYPE (t2)),
9849 				   complain);
9850     }
9851 
9852   if (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
9853     /* [expr.const.cast]
9854 
9855        Casting from an rvalue of type "pointer to data member of X
9856        of type T1" to the type "pointer to data member of Y of type
9857        T2" casts away constness if a cast from an rvalue of type
9858        "pointer to T1" to the type "pointer to T2" casts away
9859        constness.  */
9860     return casts_away_constness
9861       (build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t1)),
9862        build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t2)),
9863        complain);
9864 
9865   /* Casting away constness is only something that makes sense for
9866      pointer or reference types.  */
9867   if (!TYPE_PTR_P (t1) || !TYPE_PTR_P (t2))
9868     return false;
9869 
9870   /* Top-level qualifiers don't matter.  */
9871   t1 = TYPE_MAIN_VARIANT (t1);
9872   t2 = TYPE_MAIN_VARIANT (t2);
9873   casts_away_constness_r (&t1, &t2, complain);
9874   if (!can_convert (t2, t1, complain))
9875     return true;
9876 
9877   return false;
9878 }
9879 
9880 /* If T is a REFERENCE_TYPE return the type to which T refers.
9881    Otherwise, return T itself.  */
9882 
9883 tree
9884 non_reference (tree t)
9885 {
9886   if (t && TREE_CODE (t) == REFERENCE_TYPE)
9887     t = TREE_TYPE (t);
9888   return t;
9889 }
9890 
9891 
9892 /* Return nonzero if REF is an lvalue valid for this language;
9893    otherwise, print an error message and return zero.  USE says
9894    how the lvalue is being used and so selects the error message.  */
9895 
9896 int
9897 lvalue_or_else (tree ref, enum lvalue_use use, tsubst_flags_t complain)
9898 {
9899   cp_lvalue_kind kind = lvalue_kind (ref);
9900 
9901   if (kind == clk_none)
9902     {
9903       if (complain & tf_error)
9904 	lvalue_error (input_location, use);
9905       return 0;
9906     }
9907   else if (kind & (clk_rvalueref|clk_class))
9908     {
9909       if (!(complain & tf_error))
9910 	return 0;
9911       if (kind & clk_class)
9912 	/* Make this a permerror because we used to accept it.  */
9913 	permerror (input_location, "using temporary as lvalue");
9914       else
9915 	error ("using xvalue (rvalue reference) as lvalue");
9916     }
9917   return 1;
9918 }
9919 
9920 /* Return true if a user-defined literal operator is a raw operator.  */
9921 
9922 bool
9923 check_raw_literal_operator (const_tree decl)
9924 {
9925   tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
9926   tree argtype;
9927   int arity;
9928   bool maybe_raw_p = false;
9929 
9930   /* Count the number and type of arguments and check for ellipsis.  */
9931   for (argtype = argtypes, arity = 0;
9932        argtype && argtype != void_list_node;
9933        ++arity, argtype = TREE_CHAIN (argtype))
9934     {
9935       tree t = TREE_VALUE (argtype);
9936 
9937       if (same_type_p (t, const_string_type_node))
9938 	maybe_raw_p = true;
9939     }
9940   if (!argtype)
9941     return false; /* Found ellipsis.  */
9942 
9943   if (!maybe_raw_p || arity != 1)
9944     return false;
9945 
9946   return true;
9947 }
9948 
9949 
9950 /* Return true if a user-defined literal operator has one of the allowed
9951    argument types.  */
9952 
9953 bool
9954 check_literal_operator_args (const_tree decl,
9955 			     bool *long_long_unsigned_p, bool *long_double_p)
9956 {
9957   tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
9958 
9959   *long_long_unsigned_p = false;
9960   *long_double_p = false;
9961   if (processing_template_decl || processing_specialization)
9962     return argtypes == void_list_node;
9963   else
9964     {
9965       tree argtype;
9966       int arity;
9967       int max_arity = 2;
9968 
9969       /* Count the number and type of arguments and check for ellipsis.  */
9970       for (argtype = argtypes, arity = 0;
9971 	   argtype && argtype != void_list_node;
9972 	   argtype = TREE_CHAIN (argtype))
9973 	{
9974 	  tree t = TREE_VALUE (argtype);
9975 	  ++arity;
9976 
9977 	  if (TYPE_PTR_P (t))
9978 	    {
9979 	      bool maybe_raw_p = false;
9980 	      t = TREE_TYPE (t);
9981 	      if (cp_type_quals (t) != TYPE_QUAL_CONST)
9982 		return false;
9983 	      t = TYPE_MAIN_VARIANT (t);
9984 	      if ((maybe_raw_p = same_type_p (t, char_type_node))
9985 		  || same_type_p (t, wchar_type_node)
9986 		  || same_type_p (t, char16_type_node)
9987 		  || same_type_p (t, char32_type_node))
9988 		{
9989 		  argtype = TREE_CHAIN (argtype);
9990 		  if (!argtype)
9991 		    return false;
9992 		  t = TREE_VALUE (argtype);
9993 		  if (maybe_raw_p && argtype == void_list_node)
9994 		    return true;
9995 		  else if (same_type_p (t, size_type_node))
9996 		    {
9997 		      ++arity;
9998 		      continue;
9999 		    }
10000 		  else
10001 		    return false;
10002 		}
10003 	    }
10004 	  else if (same_type_p (t, long_long_unsigned_type_node))
10005 	    {
10006 	      max_arity = 1;
10007 	      *long_long_unsigned_p = true;
10008 	    }
10009 	  else if (same_type_p (t, long_double_type_node))
10010 	    {
10011 	      max_arity = 1;
10012 	      *long_double_p = true;
10013 	    }
10014 	  else if (same_type_p (t, char_type_node))
10015 	    max_arity = 1;
10016 	  else if (same_type_p (t, wchar_type_node))
10017 	    max_arity = 1;
10018 	  else if (same_type_p (t, char16_type_node))
10019 	    max_arity = 1;
10020 	  else if (same_type_p (t, char32_type_node))
10021 	    max_arity = 1;
10022 	  else
10023 	    return false;
10024 	}
10025       if (!argtype)
10026 	return false; /* Found ellipsis.  */
10027 
10028       if (arity != max_arity)
10029 	return false;
10030 
10031       return true;
10032     }
10033 }
10034 
10035 /* Always returns false since unlike C90, C++ has no concept of implicit
10036    function declarations.  */
10037 
10038 bool
10039 c_decl_implicit (const_tree)
10040 {
10041   return false;
10042 }
10043