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