xref: /netbsd-src/external/gpl3/gcc.old/dist/gcc/cp/typeck.c (revision 23f5f46327e37e7811da3520f4bb933f9489322f)
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 			  && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (op0,
4783 								       0)))
4784 			  && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0)))
4785 			  && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0,
4786 								       0)))
4787 			      < TYPE_PRECISION (type0)))
4788 			 || (TREE_CODE (stripped_op1) == INTEGER_CST
4789 			     && ! integer_all_onesp (stripped_op1)));
4790 	    }
4791 
4792 	  common = 1;
4793 	}
4794       break;
4795 
4796     case BIT_AND_EXPR:
4797     case BIT_IOR_EXPR:
4798     case BIT_XOR_EXPR:
4799       if ((code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4800 	  || (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4801 	      && !VECTOR_FLOAT_TYPE_P (type0)
4802 	      && !VECTOR_FLOAT_TYPE_P (type1)))
4803 	shorten = -1;
4804       break;
4805 
4806     case TRUNC_MOD_EXPR:
4807     case FLOOR_MOD_EXPR:
4808       doing_div_or_mod = true;
4809       warn_for_div_by_zero (location, fold_for_warn (op1));
4810 
4811       if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4812 	  && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4813 	  && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
4814 	common = 1;
4815       else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4816 	{
4817 	  /* Although it would be tempting to shorten always here, that loses
4818 	     on some targets, since the modulo instruction is undefined if the
4819 	     quotient can't be represented in the computation mode.  We shorten
4820 	     only if unsigned or if dividing by something we know != -1.  */
4821 	  tree stripped_op1 = tree_strip_any_location_wrapper (op1);
4822 	  shorten = ((TREE_CODE (op0) == NOP_EXPR
4823 		      && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0)))
4824 		      && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0)))
4825 		      && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))
4826 			  < TYPE_PRECISION (type0)))
4827 		     || (TREE_CODE (stripped_op1) == INTEGER_CST
4828 			 && ! integer_all_onesp (stripped_op1)));
4829 	  common = 1;
4830 	}
4831       break;
4832 
4833     case TRUTH_ANDIF_EXPR:
4834     case TRUTH_ORIF_EXPR:
4835     case TRUTH_AND_EXPR:
4836     case TRUTH_OR_EXPR:
4837       if (!VECTOR_TYPE_P (type0) && gnu_vector_type_p (type1))
4838 	{
4839 	  if (!COMPARISON_CLASS_P (op1))
4840 	    op1 = cp_build_binary_op (EXPR_LOCATION (op1), NE_EXPR, op1,
4841 				      build_zero_cst (type1), complain);
4842 	  if (code == TRUTH_ANDIF_EXPR)
4843 	    {
4844 	      tree z = build_zero_cst (TREE_TYPE (op1));
4845 	      return build_conditional_expr (location, op0, op1, z, complain);
4846 	    }
4847 	  else if (code == TRUTH_ORIF_EXPR)
4848 	    {
4849 	      tree m1 = build_all_ones_cst (TREE_TYPE (op1));
4850 	      return build_conditional_expr (location, op0, m1, op1, complain);
4851 	    }
4852 	  else
4853 	    gcc_unreachable ();
4854 	}
4855       if (gnu_vector_type_p (type0)
4856 	  && (!VECTOR_TYPE_P (type1) || gnu_vector_type_p (type1)))
4857 	{
4858 	  if (!COMPARISON_CLASS_P (op0))
4859 	    op0 = cp_build_binary_op (EXPR_LOCATION (op0), NE_EXPR, op0,
4860 				      build_zero_cst (type0), complain);
4861 	  if (!VECTOR_TYPE_P (type1))
4862 	    {
4863 	      tree m1 = build_all_ones_cst (TREE_TYPE (op0));
4864 	      tree z = build_zero_cst (TREE_TYPE (op0));
4865 	      op1 = build_conditional_expr (location, op1, m1, z, complain);
4866 	    }
4867 	  else if (!COMPARISON_CLASS_P (op1))
4868 	    op1 = cp_build_binary_op (EXPR_LOCATION (op1), NE_EXPR, op1,
4869 				      build_zero_cst (type1), complain);
4870 
4871 	  if (code == TRUTH_ANDIF_EXPR)
4872 	    code = BIT_AND_EXPR;
4873 	  else if (code == TRUTH_ORIF_EXPR)
4874 	    code = BIT_IOR_EXPR;
4875 	  else
4876 	    gcc_unreachable ();
4877 
4878 	  return cp_build_binary_op (location, code, op0, op1, complain);
4879 	}
4880 
4881       result_type = boolean_type_node;
4882       break;
4883 
4884       /* Shift operations: result has same type as first operand;
4885 	 always convert second operand to int.
4886 	 Also set SHORT_SHIFT if shifting rightward.  */
4887 
4888     case RSHIFT_EXPR:
4889       if (gnu_vector_type_p (type0)
4890 	  && code1 == INTEGER_TYPE
4891 	  && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
4892         {
4893           result_type = type0;
4894           converted = 1;
4895         }
4896       else if (gnu_vector_type_p (type0)
4897 	       && gnu_vector_type_p (type1)
4898 	       && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4899 	       && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
4900 	       && known_eq (TYPE_VECTOR_SUBPARTS (type0),
4901 			    TYPE_VECTOR_SUBPARTS (type1)))
4902 	{
4903 	  result_type = type0;
4904 	  converted = 1;
4905 	}
4906       else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4907 	{
4908 	  tree const_op1 = fold_for_warn (op1);
4909 	  if (TREE_CODE (const_op1) != INTEGER_CST)
4910 	    const_op1 = op1;
4911 	  result_type = type0;
4912 	  doing_shift = true;
4913 	  if (TREE_CODE (const_op1) == INTEGER_CST)
4914 	    {
4915 	      if (tree_int_cst_lt (const_op1, integer_zero_node))
4916 		{
4917 		  if ((complain & tf_warning)
4918 		      && c_inhibit_evaluation_warnings == 0)
4919 		    warning_at (location, OPT_Wshift_count_negative,
4920 				"right shift count is negative");
4921 		}
4922 	      else
4923 		{
4924 		  if (!integer_zerop (const_op1))
4925 		    short_shift = 1;
4926 
4927 		  if (compare_tree_int (const_op1, TYPE_PRECISION (type0)) >= 0
4928 		      && (complain & tf_warning)
4929 		      && c_inhibit_evaluation_warnings == 0)
4930 		    warning_at (location, OPT_Wshift_count_overflow,
4931 				"right shift count >= width of type");
4932 		}
4933 	    }
4934 	  /* Avoid converting op1 to result_type later.  */
4935 	  converted = 1;
4936 	}
4937       break;
4938 
4939     case LSHIFT_EXPR:
4940       if (gnu_vector_type_p (type0)
4941 	  && code1 == INTEGER_TYPE
4942           && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
4943         {
4944           result_type = type0;
4945           converted = 1;
4946         }
4947       else if (gnu_vector_type_p (type0)
4948 	       && gnu_vector_type_p (type1)
4949 	       && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4950 	       && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
4951 	       && known_eq (TYPE_VECTOR_SUBPARTS (type0),
4952 			    TYPE_VECTOR_SUBPARTS (type1)))
4953 	{
4954 	  result_type = type0;
4955 	  converted = 1;
4956 	}
4957       else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4958 	{
4959 	  tree const_op0 = fold_for_warn (op0);
4960 	  if (TREE_CODE (const_op0) != INTEGER_CST)
4961 	    const_op0 = op0;
4962 	  tree const_op1 = fold_for_warn (op1);
4963 	  if (TREE_CODE (const_op1) != INTEGER_CST)
4964 	    const_op1 = op1;
4965 	  result_type = type0;
4966 	  doing_shift = true;
4967 	  if (TREE_CODE (const_op0) == INTEGER_CST
4968 	      && tree_int_cst_sgn (const_op0) < 0
4969 	      && !TYPE_OVERFLOW_WRAPS (type0)
4970 	      && (complain & tf_warning)
4971 	      && c_inhibit_evaluation_warnings == 0)
4972 	    warning_at (location, OPT_Wshift_negative_value,
4973 			"left shift of negative value");
4974 	  if (TREE_CODE (const_op1) == INTEGER_CST)
4975 	    {
4976 	      if (tree_int_cst_lt (const_op1, integer_zero_node))
4977 		{
4978 		  if ((complain & tf_warning)
4979 		      && c_inhibit_evaluation_warnings == 0)
4980 		    warning_at (location, OPT_Wshift_count_negative,
4981 				"left shift count is negative");
4982 		}
4983 	      else if (compare_tree_int (const_op1,
4984 					 TYPE_PRECISION (type0)) >= 0)
4985 		{
4986 		  if ((complain & tf_warning)
4987 		      && c_inhibit_evaluation_warnings == 0)
4988 		    warning_at (location, OPT_Wshift_count_overflow,
4989 				"left shift count >= width of type");
4990 		}
4991 	      else if (TREE_CODE (const_op0) == INTEGER_CST
4992 		       && (complain & tf_warning))
4993 		maybe_warn_shift_overflow (location, const_op0, const_op1);
4994 	    }
4995 	  /* Avoid converting op1 to result_type later.  */
4996 	  converted = 1;
4997 	}
4998       break;
4999 
5000     case EQ_EXPR:
5001     case NE_EXPR:
5002       if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
5003 	goto vector_compare;
5004       if ((complain & tf_warning)
5005 	  && c_inhibit_evaluation_warnings == 0
5006 	  && (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1)))
5007 	warning_at (location, OPT_Wfloat_equal,
5008 		    "comparing floating-point with %<==%> "
5009 		    "or %<!=%> is unsafe");
5010       if (complain & tf_warning)
5011 	{
5012 	  tree stripped_orig_op0 = tree_strip_any_location_wrapper (orig_op0);
5013 	  tree stripped_orig_op1 = tree_strip_any_location_wrapper (orig_op1);
5014 	  if ((TREE_CODE (stripped_orig_op0) == STRING_CST
5015 	       && !integer_zerop (cp_fully_fold (op1)))
5016 	      || (TREE_CODE (stripped_orig_op1) == STRING_CST
5017 		  && !integer_zerop (cp_fully_fold (op0))))
5018 	    warning_at (location, OPT_Waddress,
5019 			"comparison with string literal results in "
5020 			"unspecified behavior");
5021 	}
5022 
5023       build_type = boolean_type_node;
5024       if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
5025 	   || code0 == COMPLEX_TYPE || code0 == ENUMERAL_TYPE)
5026 	  && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
5027 	      || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE))
5028 	short_compare = 1;
5029       else if (((code0 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type0))
5030 		&& null_ptr_cst_p (orig_op1))
5031 	       /* Handle, eg, (void*)0 (c++/43906), and more.  */
5032 	       || (code0 == POINTER_TYPE
5033 		   && TYPE_PTR_P (type1) && integer_zerop (op1)))
5034 	{
5035 	  if (TYPE_PTR_P (type1))
5036 	    result_type = composite_pointer_type (location,
5037 						  type0, type1, op0, op1,
5038 						  CPO_COMPARISON, complain);
5039 	  else
5040 	    result_type = type0;
5041 
5042 	  if (char_type_p (TREE_TYPE (orig_op1)))
5043 	    {
5044 	      auto_diagnostic_group d;
5045 	      if (warning_at (location, OPT_Wpointer_compare,
5046 			      "comparison between pointer and zero character "
5047 			      "constant"))
5048 		inform (location,
5049 			"did you mean to dereference the pointer?");
5050 	    }
5051 	  warn_for_null_address (location, op0, complain);
5052 	}
5053       else if (((code1 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type1))
5054 		&& null_ptr_cst_p (orig_op0))
5055 	       /* Handle, eg, (void*)0 (c++/43906), and more.  */
5056 	       || (code1 == POINTER_TYPE
5057 		   && TYPE_PTR_P (type0) && integer_zerop (op0)))
5058 	{
5059 	  if (TYPE_PTR_P (type0))
5060 	    result_type = composite_pointer_type (location,
5061 						  type0, type1, op0, op1,
5062 						  CPO_COMPARISON, complain);
5063 	  else
5064 	    result_type = type1;
5065 
5066 	  if (char_type_p (TREE_TYPE (orig_op0)))
5067 	    {
5068 	      auto_diagnostic_group d;
5069 	      if (warning_at (location, OPT_Wpointer_compare,
5070 			     "comparison between pointer and zero character "
5071 			     "constant"))
5072 		inform (location,
5073 			"did you mean to dereference the pointer?");
5074 	    }
5075 	  warn_for_null_address (location, op1, complain);
5076 	}
5077       else if ((code0 == POINTER_TYPE && code1 == POINTER_TYPE)
5078 	       || (TYPE_PTRDATAMEM_P (type0) && TYPE_PTRDATAMEM_P (type1)))
5079 	result_type = composite_pointer_type (location,
5080 					      type0, type1, op0, op1,
5081 					      CPO_COMPARISON, complain);
5082       else if (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1))
5083 	/* One of the operands must be of nullptr_t type.  */
5084         result_type = TREE_TYPE (nullptr_node);
5085       else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
5086 	{
5087 	  result_type = type0;
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 (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
5095 	{
5096 	  result_type = type1;
5097 	  if (complain & tf_error)
5098 	    permerror (location, "ISO C++ forbids comparison between "
5099 		       "pointer and integer");
5100           else
5101             return error_mark_node;
5102 	}
5103       else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (orig_op1))
5104 	{
5105 	  if (TARGET_PTRMEMFUNC_VBIT_LOCATION
5106 	      == ptrmemfunc_vbit_in_delta)
5107 	    {
5108 	      tree pfn0, delta0, e1, e2;
5109 
5110 	      if (TREE_SIDE_EFFECTS (op0))
5111 		op0 = cp_save_expr (op0);
5112 
5113 	      pfn0 = pfn_from_ptrmemfunc (op0);
5114 	      delta0 = delta_from_ptrmemfunc (op0);
5115 	      e1 = cp_build_binary_op (location,
5116 				       EQ_EXPR,
5117 	  			       pfn0,
5118 				       build_zero_cst (TREE_TYPE (pfn0)),
5119 				       complain);
5120 	      e2 = cp_build_binary_op (location,
5121 				       BIT_AND_EXPR,
5122 				       delta0,
5123 				       integer_one_node,
5124 				       complain);
5125 
5126 	      if (complain & tf_warning)
5127 		maybe_warn_zero_as_null_pointer_constant (op1, input_location);
5128 
5129 	      e2 = cp_build_binary_op (location,
5130 				       EQ_EXPR, e2, integer_zero_node,
5131 				       complain);
5132 	      op0 = cp_build_binary_op (location,
5133 					TRUTH_ANDIF_EXPR, e1, e2,
5134 					complain);
5135 	      op1 = cp_convert (TREE_TYPE (op0), integer_one_node, complain);
5136 	    }
5137      	  else
5138 	    {
5139 	      op0 = build_ptrmemfunc_access_expr (op0, pfn_identifier);
5140 	      op1 = cp_convert (TREE_TYPE (op0), op1, complain);
5141 	    }
5142 	  result_type = TREE_TYPE (op0);
5143 	}
5144       else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (orig_op0))
5145 	return cp_build_binary_op (location, code, op1, op0, complain);
5146       else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1))
5147 	{
5148 	  tree type;
5149 	  /* E will be the final comparison.  */
5150 	  tree e;
5151 	  /* E1 and E2 are for scratch.  */
5152 	  tree e1;
5153 	  tree e2;
5154 	  tree pfn0;
5155 	  tree pfn1;
5156 	  tree delta0;
5157 	  tree delta1;
5158 
5159 	  type = composite_pointer_type (location, type0, type1, op0, op1,
5160 					 CPO_COMPARISON, complain);
5161 
5162 	  if (!same_type_p (TREE_TYPE (op0), type))
5163 	    op0 = cp_convert_and_check (type, op0, complain);
5164 	  if (!same_type_p (TREE_TYPE (op1), type))
5165 	    op1 = cp_convert_and_check (type, op1, complain);
5166 
5167 	  if (op0 == error_mark_node || op1 == error_mark_node)
5168 	    return error_mark_node;
5169 
5170 	  if (TREE_SIDE_EFFECTS (op0))
5171 	    op0 = save_expr (op0);
5172 	  if (TREE_SIDE_EFFECTS (op1))
5173 	    op1 = save_expr (op1);
5174 
5175 	  pfn0 = pfn_from_ptrmemfunc (op0);
5176 	  pfn0 = cp_fully_fold (pfn0);
5177 	  /* Avoid -Waddress warnings (c++/64877).  */
5178 	  if (TREE_CODE (pfn0) == ADDR_EXPR)
5179 	    TREE_NO_WARNING (pfn0) = 1;
5180 	  pfn1 = pfn_from_ptrmemfunc (op1);
5181 	  pfn1 = cp_fully_fold (pfn1);
5182 	  delta0 = delta_from_ptrmemfunc (op0);
5183 	  delta1 = delta_from_ptrmemfunc (op1);
5184 	  if (TARGET_PTRMEMFUNC_VBIT_LOCATION
5185 	      == ptrmemfunc_vbit_in_delta)
5186 	    {
5187 	      /* We generate:
5188 
5189 		 (op0.pfn == op1.pfn
5190 		  && ((op0.delta == op1.delta)
5191      		       || (!op0.pfn && op0.delta & 1 == 0
5192 			   && op1.delta & 1 == 0))
5193 
5194 	         The reason for the `!op0.pfn' bit is that a NULL
5195 	         pointer-to-member is any member with a zero PFN and
5196 	         LSB of the DELTA field is 0.  */
5197 
5198 	      e1 = cp_build_binary_op (location, BIT_AND_EXPR,
5199 				       delta0,
5200 				       integer_one_node,
5201 				       complain);
5202 	      e1 = cp_build_binary_op (location,
5203 				       EQ_EXPR, e1, integer_zero_node,
5204 				       complain);
5205 	      e2 = cp_build_binary_op (location, BIT_AND_EXPR,
5206 				       delta1,
5207 				       integer_one_node,
5208 				       complain);
5209 	      e2 = cp_build_binary_op (location,
5210 				       EQ_EXPR, e2, integer_zero_node,
5211 				       complain);
5212 	      e1 = cp_build_binary_op (location,
5213 				       TRUTH_ANDIF_EXPR, e2, e1,
5214 				       complain);
5215 	      e2 = cp_build_binary_op (location, EQ_EXPR,
5216 				       pfn0,
5217 				       build_zero_cst (TREE_TYPE (pfn0)),
5218 				       complain);
5219 	      e2 = cp_build_binary_op (location,
5220 				       TRUTH_ANDIF_EXPR, e2, e1, complain);
5221 	      e1 = cp_build_binary_op (location,
5222 				       EQ_EXPR, delta0, delta1, complain);
5223 	      e1 = cp_build_binary_op (location,
5224 				       TRUTH_ORIF_EXPR, e1, e2, complain);
5225 	    }
5226 	  else
5227 	    {
5228 	      /* We generate:
5229 
5230 	         (op0.pfn == op1.pfn
5231 	         && (!op0.pfn || op0.delta == op1.delta))
5232 
5233 	         The reason for the `!op0.pfn' bit is that a NULL
5234 	         pointer-to-member is any member with a zero PFN; the
5235 	         DELTA field is unspecified.  */
5236 
5237     	      e1 = cp_build_binary_op (location,
5238 				       EQ_EXPR, delta0, delta1, complain);
5239 	      e2 = cp_build_binary_op (location,
5240 				       EQ_EXPR,
5241 		      		       pfn0,
5242 			   	       build_zero_cst (TREE_TYPE (pfn0)),
5243 				       complain);
5244 	      e1 = cp_build_binary_op (location,
5245 				       TRUTH_ORIF_EXPR, e1, e2, complain);
5246 	    }
5247 	  e2 = build2 (EQ_EXPR, boolean_type_node, pfn0, pfn1);
5248 	  e = cp_build_binary_op (location,
5249 				  TRUTH_ANDIF_EXPR, e2, e1, complain);
5250 	  if (code == EQ_EXPR)
5251 	    return e;
5252 	  return cp_build_binary_op (location,
5253 				     EQ_EXPR, e, integer_zero_node, complain);
5254 	}
5255       else
5256 	{
5257 	  gcc_assert (!TYPE_PTRMEMFUNC_P (type0)
5258 		      || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0),
5259 				       type1));
5260 	  gcc_assert (!TYPE_PTRMEMFUNC_P (type1)
5261 		      || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1),
5262 				       type0));
5263 	}
5264 
5265       break;
5266 
5267     case MAX_EXPR:
5268     case MIN_EXPR:
5269       if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
5270 	   && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
5271 	shorten = 1;
5272       else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
5273 	result_type = composite_pointer_type (location,
5274 					      type0, type1, op0, op1,
5275 					      CPO_COMPARISON, complain);
5276       break;
5277 
5278     case LE_EXPR:
5279     case GE_EXPR:
5280     case LT_EXPR:
5281     case GT_EXPR:
5282     case SPACESHIP_EXPR:
5283       if (TREE_CODE (orig_op0) == STRING_CST
5284 	  || TREE_CODE (orig_op1) == STRING_CST)
5285 	{
5286 	  if (complain & tf_warning)
5287 	    warning_at (location, OPT_Waddress,
5288 			"comparison with string literal results "
5289 			"in unspecified behavior");
5290 	}
5291 
5292       if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
5293 	{
5294 	vector_compare:
5295 	  tree intt;
5296 	  if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
5297 							  TREE_TYPE (type1))
5298 	      && !vector_types_compatible_elements_p (type0, type1))
5299 	    {
5300 	      if (complain & tf_error)
5301 		{
5302 		  error_at (location, "comparing vectors with different "
5303 				      "element types");
5304 		  inform (location, "operand types are %qT and %qT",
5305 			  type0, type1);
5306 		}
5307 	      return error_mark_node;
5308 	    }
5309 
5310 	  if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
5311 			TYPE_VECTOR_SUBPARTS (type1)))
5312 	    {
5313 	      if (complain & tf_error)
5314 		{
5315 		  error_at (location, "comparing vectors with different "
5316 				      "number of elements");
5317 		  inform (location, "operand types are %qT and %qT",
5318 			  type0, type1);
5319 		}
5320 	      return error_mark_node;
5321 	    }
5322 
5323 	  /* It's not precisely specified how the usual arithmetic
5324 	     conversions apply to the vector types.  Here, we use
5325 	     the unsigned type if one of the operands is signed and
5326 	     the other one is unsigned.  */
5327 	  if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
5328 	    {
5329 	      if (!TYPE_UNSIGNED (type0))
5330 		op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
5331 	      else
5332 		op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
5333 	      warning_at (location, OPT_Wsign_compare, "comparison between "
5334 			  "types %qT and %qT", type0, type1);
5335 	    }
5336 
5337 	  if (resultcode == SPACESHIP_EXPR)
5338 	    {
5339 	      if (complain & tf_error)
5340 		sorry_at (location, "three-way comparison of vectors");
5341 	      return error_mark_node;
5342 	    }
5343 
5344 	  /* Always construct signed integer vector type.  */
5345 	  intt = c_common_type_for_size
5346 	    (GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (type0))), 0);
5347 	  if (!intt)
5348 	    {
5349 	      if (complain & tf_error)
5350 		error_at (location, "could not find an integer type "
5351 			  "of the same size as %qT", TREE_TYPE (type0));
5352 	      return error_mark_node;
5353 	    }
5354 	  result_type = build_opaque_vector_type (intt,
5355 						  TYPE_VECTOR_SUBPARTS (type0));
5356 	  return build_vec_cmp (resultcode, result_type, op0, op1);
5357 	}
5358       build_type = boolean_type_node;
5359       if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
5360 	   || code0 == ENUMERAL_TYPE)
5361 	   && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
5362 	       || code1 == ENUMERAL_TYPE))
5363 	short_compare = 1;
5364       else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
5365 	result_type = composite_pointer_type (location,
5366 					      type0, type1, op0, op1,
5367 					      CPO_COMPARISON, complain);
5368       else if (code0 == POINTER_TYPE && null_ptr_cst_p (orig_op1))
5369 	{
5370 	  result_type = type0;
5371 	  if (extra_warnings && (complain & tf_warning))
5372 	    warning_at (location, OPT_Wextra,
5373 			"ordered comparison of pointer with integer zero");
5374 	}
5375       else if (code1 == POINTER_TYPE && null_ptr_cst_p (orig_op0))
5376 	{
5377 	  result_type = type1;
5378 	  if (extra_warnings && (complain & tf_warning))
5379 	    warning_at (location, OPT_Wextra,
5380 			"ordered comparison of pointer with integer zero");
5381 	}
5382       else if (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1))
5383 	/* One of the operands must be of nullptr_t type.  */
5384         result_type = TREE_TYPE (nullptr_node);
5385       else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
5386 	{
5387 	  result_type = type0;
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       else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
5395 	{
5396 	  result_type = type1;
5397 	  if (complain & tf_error)
5398 	    permerror (location, "ISO C++ forbids comparison between "
5399 		       "pointer and integer");
5400 	  else
5401             return error_mark_node;
5402 	}
5403 
5404       if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
5405 	  && !processing_template_decl
5406 	  && sanitize_flags_p (SANITIZE_POINTER_COMPARE))
5407 	{
5408 	  op0 = save_expr (op0);
5409 	  op1 = save_expr (op1);
5410 
5411 	  tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_COMPARE);
5412 	  instrument_expr = build_call_expr_loc (location, tt, 2, op0, op1);
5413 	}
5414 
5415       break;
5416 
5417     case UNORDERED_EXPR:
5418     case ORDERED_EXPR:
5419     case UNLT_EXPR:
5420     case UNLE_EXPR:
5421     case UNGT_EXPR:
5422     case UNGE_EXPR:
5423     case UNEQ_EXPR:
5424       build_type = integer_type_node;
5425       if (code0 != REAL_TYPE || code1 != REAL_TYPE)
5426 	{
5427 	  if (complain & tf_error)
5428 	    error ("unordered comparison on non-floating-point argument");
5429 	  return error_mark_node;
5430 	}
5431       common = 1;
5432       break;
5433 
5434     default:
5435       break;
5436     }
5437 
5438   if (((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
5439 	|| code0 == ENUMERAL_TYPE)
5440        && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
5441 	   || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE)))
5442     arithmetic_types_p = 1;
5443   else
5444     {
5445       arithmetic_types_p = 0;
5446       /* Vector arithmetic is only allowed when both sides are vectors.  */
5447       if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
5448 	{
5449 	  if (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
5450 	      || !vector_types_compatible_elements_p (type0, type1))
5451 	    {
5452 	      if (complain & tf_error)
5453 		{
5454 		  /* "location" already embeds the locations of the
5455 		     operands, so we don't need to add them separately
5456 		     to richloc.  */
5457 		  rich_location richloc (line_table, location);
5458 		  binary_op_error (&richloc, code, type0, type1);
5459 		}
5460 	      return error_mark_node;
5461 	    }
5462 	  arithmetic_types_p = 1;
5463 	}
5464     }
5465   /* Determine the RESULT_TYPE, if it is not already known.  */
5466   if (!result_type
5467       && arithmetic_types_p
5468       && (shorten || common || short_compare))
5469     {
5470       result_type = cp_common_type (type0, type1);
5471       if (complain & tf_warning)
5472 	do_warn_double_promotion (result_type, type0, type1,
5473 				  "implicit conversion from %qH to %qI "
5474 				  "to match other operand of binary "
5475 				  "expression",
5476 				  location);
5477     }
5478 
5479   if (code == SPACESHIP_EXPR)
5480     {
5481       iloc_sentinel s (location);
5482 
5483       tree orig_type0 = TREE_TYPE (orig_op0);
5484       tree_code orig_code0 = TREE_CODE (orig_type0);
5485       tree orig_type1 = TREE_TYPE (orig_op1);
5486       tree_code orig_code1 = TREE_CODE (orig_type1);
5487       if (!result_type)
5488 	/* Nope.  */;
5489       else if ((orig_code0 == BOOLEAN_TYPE) != (orig_code1 == BOOLEAN_TYPE))
5490 	/* "If one of the operands is of type bool and the other is not, the
5491 	   program is ill-formed."  */
5492 	result_type = NULL_TREE;
5493       else if (code0 == POINTER_TYPE && orig_code0 != POINTER_TYPE
5494 	       && code1 == POINTER_TYPE && orig_code1 != POINTER_TYPE)
5495 	/* We only do array/function-to-pointer conversion if "at least one of
5496 	   the operands is of pointer type".  */
5497 	result_type = NULL_TREE;
5498       else if (TYPE_PTRFN_P (result_type) || NULLPTR_TYPE_P (result_type))
5499 	/* <=> no longer supports equality relations.  */
5500 	result_type = NULL_TREE;
5501       else if (orig_code0 == ENUMERAL_TYPE && orig_code1 == ENUMERAL_TYPE
5502 	       && !(same_type_ignoring_top_level_qualifiers_p
5503 		    (orig_type0, orig_type1)))
5504 	/* "If both operands have arithmetic types, or one operand has integral
5505 	   type and the other operand has unscoped enumeration type, the usual
5506 	   arithmetic conversions are applied to the operands."  So we don't do
5507 	   arithmetic conversions if the operands both have enumeral type.  */
5508 	result_type = NULL_TREE;
5509 
5510       if (result_type)
5511 	{
5512 	  build_type = spaceship_type (result_type, complain);
5513 	  if (build_type == error_mark_node)
5514 	    return error_mark_node;
5515 	}
5516 
5517       if (result_type && arithmetic_types_p)
5518 	{
5519 	  /* If a narrowing conversion is required, other than from an integral
5520 	     type to a floating point type, the program is ill-formed.  */
5521 	  bool ok = true;
5522 	  if (TREE_CODE (result_type) == REAL_TYPE
5523 	      && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (orig_op0)))
5524 	    /* OK */;
5525 	  else if (!check_narrowing (result_type, orig_op0, complain))
5526 	    ok = false;
5527 	  if (TREE_CODE (result_type) == REAL_TYPE
5528 	      && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (orig_op1)))
5529 	    /* OK */;
5530 	  else if (!check_narrowing (result_type, orig_op1, complain))
5531 	    ok = false;
5532 	  if (!ok && !(complain & tf_error))
5533 	    return error_mark_node;
5534 	}
5535     }
5536 
5537   if (!result_type)
5538     {
5539       if (complain & tf_error)
5540 	{
5541 	  binary_op_rich_location richloc (location,
5542 					   orig_op0, orig_op1, true);
5543 	  error_at (&richloc,
5544 		    "invalid operands of types %qT and %qT to binary %qO",
5545 		    TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), code);
5546 	}
5547       return error_mark_node;
5548     }
5549 
5550   /* If we're in a template, the only thing we need to know is the
5551      RESULT_TYPE.  */
5552   if (processing_template_decl)
5553     {
5554       /* Since the middle-end checks the type when doing a build2, we
5555 	 need to build the tree in pieces.  This built tree will never
5556 	 get out of the front-end as we replace it when instantiating
5557 	 the template.  */
5558       tree tmp = build2 (resultcode,
5559 			 build_type ? build_type : result_type,
5560 			 NULL_TREE, op1);
5561       TREE_OPERAND (tmp, 0) = op0;
5562       return tmp;
5563     }
5564 
5565   /* Remember the original type; RESULT_TYPE might be changed later on
5566      by shorten_binary_op.  */
5567   tree orig_type = result_type;
5568 
5569   if (arithmetic_types_p)
5570     {
5571       bool first_complex = (code0 == COMPLEX_TYPE);
5572       bool second_complex = (code1 == COMPLEX_TYPE);
5573       int none_complex = (!first_complex && !second_complex);
5574 
5575       /* Adapted from patch for c/24581.  */
5576       if (first_complex != second_complex
5577 	  && (code == PLUS_EXPR
5578 	      || code == MINUS_EXPR
5579 	      || code == MULT_EXPR
5580 	      || (code == TRUNC_DIV_EXPR && first_complex))
5581 	  && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
5582 	  && flag_signed_zeros)
5583 	{
5584 	  /* An operation on mixed real/complex operands must be
5585 	     handled specially, but the language-independent code can
5586 	     more easily optimize the plain complex arithmetic if
5587 	     -fno-signed-zeros.  */
5588 	  tree real_type = TREE_TYPE (result_type);
5589 	  tree real, imag;
5590 	  if (first_complex)
5591 	    {
5592 	      if (TREE_TYPE (op0) != result_type)
5593 		op0 = cp_convert_and_check (result_type, op0, complain);
5594 	      if (TREE_TYPE (op1) != real_type)
5595 		op1 = cp_convert_and_check (real_type, op1, complain);
5596 	    }
5597 	  else
5598 	    {
5599 	      if (TREE_TYPE (op0) != real_type)
5600 		op0 = cp_convert_and_check (real_type, op0, complain);
5601 	      if (TREE_TYPE (op1) != result_type)
5602 		op1 = cp_convert_and_check (result_type, op1, complain);
5603 	    }
5604 	  if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
5605 	    return error_mark_node;
5606 	  if (first_complex)
5607 	    {
5608 	      op0 = save_expr (op0);
5609 	      real = cp_build_unary_op (REALPART_EXPR, op0, true, complain);
5610 	      imag = cp_build_unary_op (IMAGPART_EXPR, op0, true, complain);
5611 	      switch (code)
5612 		{
5613 		case MULT_EXPR:
5614 		case TRUNC_DIV_EXPR:
5615 		  op1 = save_expr (op1);
5616 		  imag = build2 (resultcode, real_type, imag, op1);
5617 		  /* Fall through.  */
5618 		case PLUS_EXPR:
5619 		case MINUS_EXPR:
5620 		  real = build2 (resultcode, real_type, real, op1);
5621 		  break;
5622 		default:
5623 		  gcc_unreachable();
5624 		}
5625 	    }
5626 	  else
5627 	    {
5628 	      op1 = save_expr (op1);
5629 	      real = cp_build_unary_op (REALPART_EXPR, op1, true, complain);
5630 	      imag = cp_build_unary_op (IMAGPART_EXPR, op1, true, complain);
5631 	      switch (code)
5632 		{
5633 		case MULT_EXPR:
5634 		  op0 = save_expr (op0);
5635 		  imag = build2 (resultcode, real_type, op0, imag);
5636 		  /* Fall through.  */
5637 		case PLUS_EXPR:
5638 		  real = build2 (resultcode, real_type, op0, real);
5639 		  break;
5640 		case MINUS_EXPR:
5641 		  real = build2 (resultcode, real_type, op0, real);
5642 		  imag = build1 (NEGATE_EXPR, real_type, imag);
5643 		  break;
5644 		default:
5645 		  gcc_unreachable();
5646 		}
5647 	    }
5648 	  result = build2 (COMPLEX_EXPR, result_type, real, imag);
5649 	  return result;
5650 	}
5651 
5652       /* For certain operations (which identify themselves by shorten != 0)
5653 	 if both args were extended from the same smaller type,
5654 	 do the arithmetic in that type and then extend.
5655 
5656 	 shorten !=0 and !=1 indicates a bitwise operation.
5657 	 For them, this optimization is safe only if
5658 	 both args are zero-extended or both are sign-extended.
5659 	 Otherwise, we might change the result.
5660 	 E.g., (short)-1 | (unsigned short)-1 is (int)-1
5661 	 but calculated in (unsigned short) it would be (unsigned short)-1.  */
5662 
5663       if (shorten && none_complex)
5664 	{
5665 	  final_type = result_type;
5666 	  result_type = shorten_binary_op (result_type, op0, op1,
5667 					   shorten == -1);
5668 	}
5669 
5670       /* Shifts can be shortened if shifting right.  */
5671 
5672       if (short_shift)
5673 	{
5674 	  int unsigned_arg;
5675 	  tree arg0 = get_narrower (op0, &unsigned_arg);
5676 	  /* We're not really warning here but when we set short_shift we
5677 	     used fold_for_warn to fold the operand.  */
5678 	  tree const_op1 = fold_for_warn (op1);
5679 
5680 	  final_type = result_type;
5681 
5682 	  if (arg0 == op0 && final_type == TREE_TYPE (op0))
5683 	    unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
5684 
5685 	  if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
5686 	      && tree_int_cst_sgn (const_op1) > 0
5687 	      /* We can shorten only if the shift count is less than the
5688 		 number of bits in the smaller type size.  */
5689 	      && compare_tree_int (const_op1,
5690 				   TYPE_PRECISION (TREE_TYPE (arg0))) < 0
5691 	      /* We cannot drop an unsigned shift after sign-extension.  */
5692 	      && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
5693 	    {
5694 	      /* Do an unsigned shift if the operand was zero-extended.  */
5695 	      result_type
5696 		= c_common_signed_or_unsigned_type (unsigned_arg,
5697 						    TREE_TYPE (arg0));
5698 	      /* Convert value-to-be-shifted to that type.  */
5699 	      if (TREE_TYPE (op0) != result_type)
5700 		op0 = convert (result_type, op0);
5701 	      converted = 1;
5702 	    }
5703 	}
5704 
5705       /* Comparison operations are shortened too but differently.
5706 	 They identify themselves by setting short_compare = 1.  */
5707 
5708       if (short_compare)
5709 	{
5710 	  /* We call shorten_compare only for diagnostics.  */
5711 	  tree xop0 = fold_simple (op0);
5712 	  tree xop1 = fold_simple (op1);
5713 	  tree xresult_type = result_type;
5714 	  enum tree_code xresultcode = resultcode;
5715 	  shorten_compare (location, &xop0, &xop1, &xresult_type,
5716 			   &xresultcode);
5717 	}
5718 
5719       if ((short_compare || code == MIN_EXPR || code == MAX_EXPR)
5720 	  && warn_sign_compare
5721 	  /* Do not warn until the template is instantiated; we cannot
5722 	     bound the ranges of the arguments until that point.  */
5723 	  && !processing_template_decl
5724           && (complain & tf_warning)
5725 	  && c_inhibit_evaluation_warnings == 0
5726 	  /* Even unsigned enum types promote to signed int.  We don't
5727 	     want to issue -Wsign-compare warnings for this case.  */
5728 	  && !enum_cast_to_int (orig_op0)
5729 	  && !enum_cast_to_int (orig_op1))
5730 	{
5731 	  warn_for_sign_compare (location, orig_op0, orig_op1, op0, op1,
5732 				 result_type, resultcode);
5733 	}
5734     }
5735 
5736   /* If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
5737      Then the expression will be built.
5738      It will be given type FINAL_TYPE if that is nonzero;
5739      otherwise, it will be given type RESULT_TYPE.  */
5740   if (! converted)
5741     {
5742       warning_sentinel w (warn_sign_conversion, short_compare);
5743       if (!same_type_p (TREE_TYPE (op0), result_type))
5744 	op0 = cp_convert_and_check (result_type, op0, complain);
5745       if (!same_type_p (TREE_TYPE (op1), result_type))
5746 	op1 = cp_convert_and_check (result_type, op1, complain);
5747 
5748       if (op0 == error_mark_node || op1 == error_mark_node)
5749 	return error_mark_node;
5750     }
5751 
5752   if (build_type == NULL_TREE)
5753     build_type = result_type;
5754 
5755   if (doing_shift
5756       && flag_strong_eval_order == 2
5757       && TREE_SIDE_EFFECTS (op1)
5758       && !processing_template_decl)
5759     {
5760       /* In C++17, in both op0 << op1 and op0 >> op1 op0 is sequenced before
5761 	 op1, so if op1 has side-effects, use SAVE_EXPR around op0.  */
5762       op0 = cp_save_expr (op0);
5763       instrument_expr = op0;
5764     }
5765 
5766   if (sanitize_flags_p ((SANITIZE_SHIFT
5767 			 | SANITIZE_DIVIDE | SANITIZE_FLOAT_DIVIDE))
5768       && current_function_decl != NULL_TREE
5769       && !processing_template_decl
5770       && (doing_div_or_mod || doing_shift))
5771     {
5772       /* OP0 and/or OP1 might have side-effects.  */
5773       op0 = cp_save_expr (op0);
5774       op1 = cp_save_expr (op1);
5775       op0 = fold_non_dependent_expr (op0, complain);
5776       op1 = fold_non_dependent_expr (op1, complain);
5777       tree instrument_expr1 = NULL_TREE;
5778       if (doing_div_or_mod
5779 	  && sanitize_flags_p (SANITIZE_DIVIDE | SANITIZE_FLOAT_DIVIDE))
5780 	{
5781 	  /* For diagnostics we want to use the promoted types without
5782 	     shorten_binary_op.  So convert the arguments to the
5783 	     original result_type.  */
5784 	  tree cop0 = op0;
5785 	  tree cop1 = op1;
5786 	  if (TREE_TYPE (cop0) != orig_type)
5787 	    cop0 = cp_convert (orig_type, op0, complain);
5788 	  if (TREE_TYPE (cop1) != orig_type)
5789 	    cop1 = cp_convert (orig_type, op1, complain);
5790 	  instrument_expr1 = ubsan_instrument_division (location, cop0, cop1);
5791 	}
5792       else if (doing_shift && sanitize_flags_p (SANITIZE_SHIFT))
5793 	instrument_expr1 = ubsan_instrument_shift (location, code, op0, op1);
5794       if (instrument_expr != NULL)
5795 	instrument_expr = add_stmt_to_compound (instrument_expr,
5796 						instrument_expr1);
5797       else
5798 	instrument_expr = instrument_expr1;
5799     }
5800 
5801   result = build2_loc (location, resultcode, build_type, op0, op1);
5802   if (final_type != 0)
5803     result = cp_convert (final_type, result, complain);
5804 
5805   if (instrument_expr != NULL)
5806     result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
5807 		     instrument_expr, result);
5808 
5809   if (!processing_template_decl)
5810     {
5811       op0 = cp_fully_fold (op0);
5812       /* Only consider the second argument if the first isn't overflowed.  */
5813       if (!CONSTANT_CLASS_P (op0) || TREE_OVERFLOW_P (op0))
5814 	return result;
5815       op1 = cp_fully_fold (op1);
5816       if (!CONSTANT_CLASS_P (op1) || TREE_OVERFLOW_P (op1))
5817 	return result;
5818     }
5819   else if (!CONSTANT_CLASS_P (op0) || !CONSTANT_CLASS_P (op1)
5820 	   || TREE_OVERFLOW_P (op0) || TREE_OVERFLOW_P (op1))
5821     return result;
5822 
5823   result_ovl = fold_build2 (resultcode, build_type, op0, op1);
5824   if (TREE_OVERFLOW_P (result_ovl))
5825     overflow_warning (location, result_ovl);
5826 
5827   return result;
5828 }
5829 
5830 /* Build a VEC_PERM_EXPR.
5831    This is a simple wrapper for c_build_vec_perm_expr.  */
5832 tree
build_x_vec_perm_expr(location_t loc,tree arg0,tree arg1,tree arg2,tsubst_flags_t complain)5833 build_x_vec_perm_expr (location_t loc,
5834 			tree arg0, tree arg1, tree arg2,
5835 			tsubst_flags_t complain)
5836 {
5837   tree orig_arg0 = arg0;
5838   tree orig_arg1 = arg1;
5839   tree orig_arg2 = arg2;
5840   if (processing_template_decl)
5841     {
5842       if (type_dependent_expression_p (arg0)
5843 	  || type_dependent_expression_p (arg1)
5844 	  || type_dependent_expression_p (arg2))
5845 	return build_min_nt_loc (loc, VEC_PERM_EXPR, arg0, arg1, arg2);
5846       arg0 = build_non_dependent_expr (arg0);
5847       if (arg1)
5848 	arg1 = build_non_dependent_expr (arg1);
5849       arg2 = build_non_dependent_expr (arg2);
5850     }
5851   tree exp = c_build_vec_perm_expr (loc, arg0, arg1, arg2, complain & tf_error);
5852   if (processing_template_decl && exp != error_mark_node)
5853     return build_min_non_dep (VEC_PERM_EXPR, exp, orig_arg0,
5854 			      orig_arg1, orig_arg2);
5855   return exp;
5856 }
5857 
5858 /* Return a tree for the sum or difference (RESULTCODE says which)
5859    of pointer PTROP and integer INTOP.  */
5860 
5861 static tree
cp_pointer_int_sum(location_t loc,enum tree_code resultcode,tree ptrop,tree intop,tsubst_flags_t complain)5862 cp_pointer_int_sum (location_t loc, enum tree_code resultcode, tree ptrop,
5863 		    tree intop, tsubst_flags_t complain)
5864 {
5865   tree res_type = TREE_TYPE (ptrop);
5866 
5867   /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
5868      in certain circumstance (when it's valid to do so).  So we need
5869      to make sure it's complete.  We don't need to check here, if we
5870      can actually complete it at all, as those checks will be done in
5871      pointer_int_sum() anyway.  */
5872   complete_type (TREE_TYPE (res_type));
5873 
5874   return pointer_int_sum (loc, resultcode, ptrop,
5875 			  intop, complain & tf_warning_or_error);
5876 }
5877 
5878 /* Return a tree for the difference of pointers OP0 and OP1.
5879    The resulting tree has type int.  If POINTER_SUBTRACT sanitization is
5880    enabled, assign to INSTRUMENT_EXPR call to libsanitizer.  */
5881 
5882 static tree
pointer_diff(location_t loc,tree op0,tree op1,tree ptrtype,tsubst_flags_t complain,tree * instrument_expr)5883 pointer_diff (location_t loc, tree op0, tree op1, tree ptrtype,
5884 	      tsubst_flags_t complain, tree *instrument_expr)
5885 {
5886   tree result, inttype;
5887   tree restype = ptrdiff_type_node;
5888   tree target_type = TREE_TYPE (ptrtype);
5889 
5890   if (!complete_type_or_else (target_type, NULL_TREE))
5891     return error_mark_node;
5892 
5893   if (VOID_TYPE_P (target_type))
5894     {
5895       if (complain & tf_error)
5896 	permerror (loc, "ISO C++ forbids using pointer of "
5897 		   "type %<void *%> in subtraction");
5898       else
5899 	return error_mark_node;
5900     }
5901   if (TREE_CODE (target_type) == FUNCTION_TYPE)
5902     {
5903       if (complain & tf_error)
5904 	permerror (loc, "ISO C++ forbids using pointer to "
5905 		   "a function in subtraction");
5906       else
5907 	return error_mark_node;
5908     }
5909   if (TREE_CODE (target_type) == METHOD_TYPE)
5910     {
5911       if (complain & tf_error)
5912 	permerror (loc, "ISO C++ forbids using pointer to "
5913 		   "a method in subtraction");
5914       else
5915 	return error_mark_node;
5916     }
5917   else if (!verify_type_context (loc, TCTX_POINTER_ARITH,
5918 				 TREE_TYPE (TREE_TYPE (op0)),
5919 				 !(complain & tf_error))
5920 	   || !verify_type_context (loc, TCTX_POINTER_ARITH,
5921 				    TREE_TYPE (TREE_TYPE (op1)),
5922 				    !(complain & tf_error)))
5923     return error_mark_node;
5924 
5925   /* Determine integer type result of the subtraction.  This will usually
5926      be the same as the result type (ptrdiff_t), but may need to be a wider
5927      type if pointers for the address space are wider than ptrdiff_t.  */
5928   if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
5929     inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
5930   else
5931     inttype = restype;
5932 
5933   if (!processing_template_decl
5934       && sanitize_flags_p (SANITIZE_POINTER_SUBTRACT))
5935     {
5936       op0 = save_expr (op0);
5937       op1 = save_expr (op1);
5938 
5939       tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_SUBTRACT);
5940       *instrument_expr = build_call_expr_loc (loc, tt, 2, op0, op1);
5941     }
5942 
5943   /* First do the subtraction, then build the divide operator
5944      and only convert at the very end.
5945      Do not do default conversions in case restype is a short type.  */
5946 
5947   /* POINTER_DIFF_EXPR requires a signed integer type of the same size as
5948      pointers.  If some platform cannot provide that, or has a larger
5949      ptrdiff_type to support differences larger than half the address
5950      space, cast the pointers to some larger integer type and do the
5951      computations in that type.  */
5952   if (TYPE_PRECISION (inttype) > TYPE_PRECISION (TREE_TYPE (op0)))
5953     op0 = cp_build_binary_op (loc,
5954 			      MINUS_EXPR,
5955 			      cp_convert (inttype, op0, complain),
5956 			      cp_convert (inttype, op1, complain),
5957 			      complain);
5958   else
5959     op0 = build2_loc (loc, POINTER_DIFF_EXPR, inttype, op0, op1);
5960 
5961   /* This generates an error if op1 is a pointer to an incomplete type.  */
5962   if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1))))
5963     {
5964       if (complain & tf_error)
5965 	error_at (loc, "invalid use of a pointer to an incomplete type in "
5966 		  "pointer arithmetic");
5967       else
5968 	return error_mark_node;
5969     }
5970 
5971   if (pointer_to_zero_sized_aggr_p (TREE_TYPE (op1)))
5972     {
5973       if (complain & tf_error)
5974 	error_at (loc, "arithmetic on pointer to an empty aggregate");
5975       else
5976 	return error_mark_node;
5977     }
5978 
5979   op1 = (TYPE_PTROB_P (ptrtype)
5980 	 ? size_in_bytes_loc (loc, target_type)
5981 	 : integer_one_node);
5982 
5983   /* Do the division.  */
5984 
5985   result = build2_loc (loc, EXACT_DIV_EXPR, inttype, op0,
5986 		       cp_convert (inttype, op1, complain));
5987   return cp_convert (restype, result, complain);
5988 }
5989 
5990 /* Construct and perhaps optimize a tree representation
5991    for a unary operation.  CODE, a tree_code, specifies the operation
5992    and XARG is the operand.  */
5993 
5994 tree
build_x_unary_op(location_t loc,enum tree_code code,cp_expr xarg,tsubst_flags_t complain)5995 build_x_unary_op (location_t loc, enum tree_code code, cp_expr xarg,
5996 		  tsubst_flags_t complain)
5997 {
5998   tree orig_expr = xarg;
5999   tree exp;
6000   int ptrmem = 0;
6001   tree overload = NULL_TREE;
6002 
6003   if (processing_template_decl)
6004     {
6005       if (type_dependent_expression_p (xarg))
6006 	{
6007 	  tree e = build_min_nt_loc (loc, code, xarg.get_value (), NULL_TREE);
6008 	  maybe_save_operator_binding (e);
6009 	  return e;
6010 	}
6011 
6012       xarg = build_non_dependent_expr (xarg);
6013     }
6014 
6015   exp = NULL_TREE;
6016 
6017   /* [expr.unary.op] says:
6018 
6019        The address of an object of incomplete type can be taken.
6020 
6021      (And is just the ordinary address operator, not an overloaded
6022      "operator &".)  However, if the type is a template
6023      specialization, we must complete the type at this point so that
6024      an overloaded "operator &" will be available if required.  */
6025   if (code == ADDR_EXPR
6026       && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
6027       && ((CLASS_TYPE_P (TREE_TYPE (xarg))
6028 	   && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (xarg))))
6029 	  || (TREE_CODE (xarg) == OFFSET_REF)))
6030     /* Don't look for a function.  */;
6031   else
6032     exp = build_new_op (loc, code, LOOKUP_NORMAL, xarg, NULL_TREE,
6033 			NULL_TREE, &overload, complain);
6034 
6035   if (!exp && code == ADDR_EXPR)
6036     {
6037       if (is_overloaded_fn (xarg))
6038 	{
6039 	  tree fn = get_first_fn (xarg);
6040 	  if (DECL_CONSTRUCTOR_P (fn) || DECL_DESTRUCTOR_P (fn))
6041 	    {
6042 	      if (complain & tf_error)
6043 		error_at (loc, DECL_CONSTRUCTOR_P (fn)
6044 			  ? G_("taking address of constructor %qD")
6045 			  : G_("taking address of destructor %qD"),
6046 			  fn);
6047 	      return error_mark_node;
6048 	    }
6049 	}
6050 
6051       /* A pointer to member-function can be formed only by saying
6052 	 &X::mf.  */
6053       if (!flag_ms_extensions && TREE_CODE (TREE_TYPE (xarg)) == METHOD_TYPE
6054 	  && (TREE_CODE (xarg) != OFFSET_REF || !PTRMEM_OK_P (xarg)))
6055 	{
6056 	  if (TREE_CODE (xarg) != OFFSET_REF
6057 	      || !TYPE_P (TREE_OPERAND (xarg, 0)))
6058 	    {
6059 	      if (complain & tf_error)
6060 		{
6061 		  error_at (loc, "invalid use of %qE to form a "
6062 			    "pointer-to-member-function", xarg.get_value ());
6063 		  if (TREE_CODE (xarg) != OFFSET_REF)
6064 		    inform (loc, "  a qualified-id is required");
6065 		}
6066 	      return error_mark_node;
6067 	    }
6068 	  else
6069 	    {
6070 	      if (complain & tf_error)
6071 		error_at (loc, "parentheses around %qE cannot be used to "
6072 			  "form a pointer-to-member-function",
6073 			  xarg.get_value ());
6074 	      else
6075 		return error_mark_node;
6076 	      PTRMEM_OK_P (xarg) = 1;
6077 	    }
6078 	}
6079 
6080       if (TREE_CODE (xarg) == OFFSET_REF)
6081 	{
6082 	  ptrmem = PTRMEM_OK_P (xarg);
6083 
6084 	  if (!ptrmem && !flag_ms_extensions
6085 	      && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg, 1))) == METHOD_TYPE)
6086 	    {
6087 	      /* A single non-static member, make sure we don't allow a
6088 		 pointer-to-member.  */
6089 	      xarg = build2 (OFFSET_REF, TREE_TYPE (xarg),
6090 			     TREE_OPERAND (xarg, 0),
6091 			     ovl_make (TREE_OPERAND (xarg, 1)));
6092 	      PTRMEM_OK_P (xarg) = ptrmem;
6093 	    }
6094 	}
6095 
6096       exp = cp_build_addr_expr_strict (xarg, complain);
6097     }
6098 
6099   if (processing_template_decl && exp != error_mark_node)
6100     {
6101       if (overload != NULL_TREE)
6102 	return (build_min_non_dep_op_overload
6103 		(code, exp, overload, orig_expr, integer_zero_node));
6104 
6105       exp = build_min_non_dep (code, exp, orig_expr,
6106 			       /*For {PRE,POST}{INC,DEC}REMENT_EXPR*/NULL_TREE);
6107     }
6108   if (TREE_CODE (exp) == ADDR_EXPR)
6109     PTRMEM_OK_P (exp) = ptrmem;
6110   return exp;
6111 }
6112 
6113 /* Construct and perhaps optimize a tree representation
6114    for __builtin_addressof operation.  ARG specifies the operand.  */
6115 
6116 tree
cp_build_addressof(location_t loc,tree arg,tsubst_flags_t complain)6117 cp_build_addressof (location_t loc, tree arg, tsubst_flags_t complain)
6118 {
6119   tree orig_expr = arg;
6120 
6121   if (processing_template_decl)
6122     {
6123       if (type_dependent_expression_p (arg))
6124 	return build_min_nt_loc (loc, ADDRESSOF_EXPR, arg, NULL_TREE);
6125 
6126       arg = build_non_dependent_expr (arg);
6127     }
6128 
6129   tree exp = cp_build_addr_expr_strict (arg, complain);
6130 
6131   if (processing_template_decl && exp != error_mark_node)
6132     exp = build_min_non_dep (ADDRESSOF_EXPR, exp, orig_expr, NULL_TREE);
6133   return exp;
6134 }
6135 
6136 /* Like c_common_truthvalue_conversion, but handle pointer-to-member
6137    constants, where a null value is represented by an INTEGER_CST of
6138    -1.  */
6139 
6140 tree
cp_truthvalue_conversion(tree expr,tsubst_flags_t complain)6141 cp_truthvalue_conversion (tree expr, tsubst_flags_t complain)
6142 {
6143   tree type = TREE_TYPE (expr);
6144   location_t loc = cp_expr_loc_or_input_loc (expr);
6145   if (TYPE_PTR_OR_PTRMEM_P (type)
6146       /* Avoid ICE on invalid use of non-static member function.  */
6147       || TREE_CODE (expr) == FUNCTION_DECL)
6148     return cp_build_binary_op (loc, NE_EXPR, expr, nullptr_node, complain);
6149   else
6150     return c_common_truthvalue_conversion (loc, expr);
6151 }
6152 
6153 /* Returns EXPR contextually converted to bool.  */
6154 
6155 tree
contextual_conv_bool(tree expr,tsubst_flags_t complain)6156 contextual_conv_bool (tree expr, tsubst_flags_t complain)
6157 {
6158   return perform_implicit_conversion_flags (boolean_type_node, expr,
6159 					    complain, LOOKUP_NORMAL);
6160 }
6161 
6162 /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR.  This
6163    is a low-level function; most callers should use maybe_convert_cond.  */
6164 
6165 tree
condition_conversion(tree expr)6166 condition_conversion (tree expr)
6167 {
6168   tree t = contextual_conv_bool (expr, tf_warning_or_error);
6169   if (!processing_template_decl)
6170     t = fold_build_cleanup_point_expr (boolean_type_node, t);
6171   return t;
6172 }
6173 
6174 /* Returns the address of T.  This function will fold away
6175    ADDR_EXPR of INDIRECT_REF.  This is only for low-level usage;
6176    most places should use cp_build_addr_expr instead.  */
6177 
6178 tree
build_address(tree t)6179 build_address (tree t)
6180 {
6181   if (error_operand_p (t) || !cxx_mark_addressable (t))
6182     return error_mark_node;
6183   gcc_checking_assert (TREE_CODE (t) != CONSTRUCTOR
6184 		       || processing_template_decl);
6185   t = build_fold_addr_expr_loc (EXPR_LOCATION (t), t);
6186   if (TREE_CODE (t) != ADDR_EXPR)
6187     t = rvalue (t);
6188   return t;
6189 }
6190 
6191 /* Return a NOP_EXPR converting EXPR to TYPE.  */
6192 
6193 tree
build_nop(tree type,tree expr)6194 build_nop (tree type, tree expr)
6195 {
6196   if (type == error_mark_node || error_operand_p (expr))
6197     return expr;
6198   return build1_loc (EXPR_LOCATION (expr), NOP_EXPR, type, expr);
6199 }
6200 
6201 /* Take the address of ARG, whatever that means under C++ semantics.
6202    If STRICT_LVALUE is true, require an lvalue; otherwise, allow xvalues
6203    and class rvalues as well.
6204 
6205    Nothing should call this function directly; instead, callers should use
6206    cp_build_addr_expr or cp_build_addr_expr_strict.  */
6207 
6208 static tree
cp_build_addr_expr_1(tree arg,bool strict_lvalue,tsubst_flags_t complain)6209 cp_build_addr_expr_1 (tree arg, bool strict_lvalue, tsubst_flags_t complain)
6210 {
6211   tree argtype;
6212   tree val;
6213 
6214   if (!arg || error_operand_p (arg))
6215     return error_mark_node;
6216 
6217   arg = mark_lvalue_use (arg);
6218   if (error_operand_p (arg))
6219     return error_mark_node;
6220 
6221   argtype = lvalue_type (arg);
6222   location_t loc = cp_expr_loc_or_input_loc (arg);
6223 
6224   gcc_assert (!(identifier_p (arg) && IDENTIFIER_ANY_OP_P (arg)));
6225 
6226   if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg)
6227       && !really_overloaded_fn (arg))
6228     {
6229       /* They're trying to take the address of a unique non-static
6230 	 member function.  This is ill-formed (except in MS-land),
6231 	 but let's try to DTRT.
6232 	 Note: We only handle unique functions here because we don't
6233 	 want to complain if there's a static overload; non-unique
6234 	 cases will be handled by instantiate_type.  But we need to
6235 	 handle this case here to allow casts on the resulting PMF.
6236 	 We could defer this in non-MS mode, but it's easier to give
6237 	 a useful error here.  */
6238 
6239       /* Inside constant member functions, the `this' pointer
6240 	 contains an extra const qualifier.  TYPE_MAIN_VARIANT
6241 	 is used here to remove this const from the diagnostics
6242 	 and the created OFFSET_REF.  */
6243       tree base = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg, 0)));
6244       tree fn = get_first_fn (TREE_OPERAND (arg, 1));
6245       if (!mark_used (fn, complain) && !(complain & tf_error))
6246 	return error_mark_node;
6247 
6248       if (! flag_ms_extensions)
6249 	{
6250 	  tree name = DECL_NAME (fn);
6251 	  if (!(complain & tf_error))
6252 	    return error_mark_node;
6253 	  else if (current_class_type
6254 		   && TREE_OPERAND (arg, 0) == current_class_ref)
6255 	    /* An expression like &memfn.  */
6256 	    permerror (loc,
6257 		       "ISO C++ forbids taking the address of an unqualified"
6258 		       " or parenthesized non-static member function to form"
6259 		       " a pointer to member function.  Say %<&%T::%D%>",
6260 		       base, name);
6261 	  else
6262 	    permerror (loc,
6263 		       "ISO C++ forbids taking the address of a bound member"
6264 		       " function to form a pointer to member function."
6265 		       "  Say %<&%T::%D%>",
6266 		       base, name);
6267 	}
6268       arg = build_offset_ref (base, fn, /*address_p=*/true, complain);
6269     }
6270 
6271   /* Uninstantiated types are all functions.  Taking the
6272      address of a function is a no-op, so just return the
6273      argument.  */
6274   if (type_unknown_p (arg))
6275     return build1 (ADDR_EXPR, unknown_type_node, arg);
6276 
6277   if (TREE_CODE (arg) == OFFSET_REF)
6278     /* We want a pointer to member; bypass all the code for actually taking
6279        the address of something.  */
6280     goto offset_ref;
6281 
6282   /* Anything not already handled and not a true memory reference
6283      is an error.  */
6284   if (!FUNC_OR_METHOD_TYPE_P (argtype))
6285     {
6286       cp_lvalue_kind kind = lvalue_kind (arg);
6287       if (kind == clk_none)
6288 	{
6289 	  if (complain & tf_error)
6290 	    lvalue_error (loc, lv_addressof);
6291 	  return error_mark_node;
6292 	}
6293       if (strict_lvalue && (kind & (clk_rvalueref|clk_class)))
6294 	{
6295 	  if (!(complain & tf_error))
6296 	    return error_mark_node;
6297 	  /* Make this a permerror because we used to accept it.  */
6298 	  permerror (loc, "taking address of rvalue");
6299 	}
6300     }
6301 
6302   if (TYPE_REF_P (argtype))
6303     {
6304       tree type = build_pointer_type (TREE_TYPE (argtype));
6305       arg = build1 (CONVERT_EXPR, type, arg);
6306       return arg;
6307     }
6308   else if (pedantic && DECL_MAIN_P (tree_strip_any_location_wrapper (arg)))
6309     {
6310       /* ARM $3.4 */
6311       /* Apparently a lot of autoconf scripts for C++ packages do this,
6312 	 so only complain if -Wpedantic.  */
6313       if (complain & (flag_pedantic_errors ? tf_error : tf_warning))
6314 	pedwarn (loc, OPT_Wpedantic,
6315 		 "ISO C++ forbids taking address of function %<::main%>");
6316       else if (flag_pedantic_errors)
6317 	return error_mark_node;
6318     }
6319 
6320   /* Let &* cancel out to simplify resulting code.  */
6321   if (INDIRECT_REF_P (arg))
6322     {
6323       arg = TREE_OPERAND (arg, 0);
6324       if (TYPE_REF_P (TREE_TYPE (arg)))
6325 	{
6326 	  tree type = build_pointer_type (TREE_TYPE (TREE_TYPE (arg)));
6327 	  arg = build1 (CONVERT_EXPR, type, arg);
6328 	}
6329       else
6330 	/* Don't let this be an lvalue.  */
6331 	arg = rvalue (arg);
6332       return arg;
6333     }
6334 
6335   /* Handle complex lvalues (when permitted)
6336      by reduction to simpler cases.  */
6337   val = unary_complex_lvalue (ADDR_EXPR, arg);
6338   if (val != 0)
6339     return val;
6340 
6341   switch (TREE_CODE (arg))
6342     {
6343     CASE_CONVERT:
6344     case FLOAT_EXPR:
6345     case FIX_TRUNC_EXPR:
6346       /* We should have handled this above in the lvalue_kind check.  */
6347       gcc_unreachable ();
6348       break;
6349 
6350     case BASELINK:
6351       arg = BASELINK_FUNCTIONS (arg);
6352       /* Fall through.  */
6353 
6354     case OVERLOAD:
6355       arg = OVL_FIRST (arg);
6356       break;
6357 
6358     case OFFSET_REF:
6359     offset_ref:
6360       /* Turn a reference to a non-static data member into a
6361 	 pointer-to-member.  */
6362       {
6363 	tree type;
6364 	tree t;
6365 
6366 	gcc_assert (PTRMEM_OK_P (arg));
6367 
6368 	t = TREE_OPERAND (arg, 1);
6369 	if (TYPE_REF_P (TREE_TYPE (t)))
6370 	  {
6371 	    if (complain & tf_error)
6372 	      error_at (loc,
6373 			"cannot create pointer to reference member %qD", t);
6374 	    return error_mark_node;
6375 	  }
6376 
6377 	type = build_ptrmem_type (context_for_name_lookup (t),
6378 				  TREE_TYPE (t));
6379 	t = make_ptrmem_cst (type, TREE_OPERAND (arg, 1));
6380 	return t;
6381       }
6382 
6383     default:
6384       break;
6385     }
6386 
6387   if (argtype != error_mark_node)
6388     argtype = build_pointer_type (argtype);
6389 
6390   if (bitfield_p (arg))
6391     {
6392       if (complain & tf_error)
6393 	error_at (loc, "attempt to take address of bit-field");
6394       return error_mark_node;
6395     }
6396 
6397   /* In a template, we are processing a non-dependent expression
6398      so we can just form an ADDR_EXPR with the correct type.  */
6399   if (processing_template_decl || TREE_CODE (arg) != COMPONENT_REF)
6400     {
6401       tree stripped_arg = tree_strip_any_location_wrapper (arg);
6402       if (TREE_CODE (stripped_arg) == FUNCTION_DECL
6403 	  && DECL_IMMEDIATE_FUNCTION_P (stripped_arg)
6404 	  && cp_unevaluated_operand == 0
6405 	  && (current_function_decl == NULL_TREE
6406 	      || !DECL_IMMEDIATE_FUNCTION_P (current_function_decl)))
6407 	{
6408 	  if (complain & tf_error)
6409 	    error_at (loc, "taking address of an immediate function %qD",
6410 		      stripped_arg);
6411 	  return error_mark_node;
6412 	}
6413       if (TREE_CODE (stripped_arg) == FUNCTION_DECL
6414 	  && !mark_used (stripped_arg, complain) && !(complain & tf_error))
6415 	return error_mark_node;
6416       val = build_address (arg);
6417       if (TREE_CODE (arg) == OFFSET_REF)
6418 	PTRMEM_OK_P (val) = PTRMEM_OK_P (arg);
6419     }
6420   else if (BASELINK_P (TREE_OPERAND (arg, 1)))
6421     {
6422       tree fn = BASELINK_FUNCTIONS (TREE_OPERAND (arg, 1));
6423 
6424       /* We can only get here with a single static member
6425 	 function.  */
6426       gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
6427 		  && DECL_STATIC_FUNCTION_P (fn));
6428       if (!mark_used (fn, complain) && !(complain & tf_error))
6429 	return error_mark_node;
6430       val = build_address (fn);
6431       if (TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
6432 	/* Do not lose object's side effects.  */
6433 	val = build2 (COMPOUND_EXPR, TREE_TYPE (val),
6434 		      TREE_OPERAND (arg, 0), val);
6435     }
6436   else
6437     {
6438       tree object = TREE_OPERAND (arg, 0);
6439       tree field = TREE_OPERAND (arg, 1);
6440       gcc_assert (same_type_ignoring_top_level_qualifiers_p
6441 		  (TREE_TYPE (object), decl_type_context (field)));
6442       val = build_address (arg);
6443     }
6444 
6445   if (TYPE_PTR_P (argtype)
6446       && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
6447     {
6448       build_ptrmemfunc_type (argtype);
6449       val = build_ptrmemfunc (argtype, val, 0,
6450 			      /*c_cast_p=*/false,
6451 			      complain);
6452     }
6453 
6454   return val;
6455 }
6456 
6457 /* Take the address of ARG if it has one, even if it's an rvalue.  */
6458 
6459 tree
cp_build_addr_expr(tree arg,tsubst_flags_t complain)6460 cp_build_addr_expr (tree arg, tsubst_flags_t complain)
6461 {
6462   return cp_build_addr_expr_1 (arg, 0, complain);
6463 }
6464 
6465 /* Take the address of ARG, but only if it's an lvalue.  */
6466 
6467 static tree
cp_build_addr_expr_strict(tree arg,tsubst_flags_t complain)6468 cp_build_addr_expr_strict (tree arg, tsubst_flags_t complain)
6469 {
6470   return cp_build_addr_expr_1 (arg, 1, complain);
6471 }
6472 
6473 /* C++: Must handle pointers to members.
6474 
6475    Perhaps type instantiation should be extended to handle conversion
6476    from aggregates to types we don't yet know we want?  (Or are those
6477    cases typically errors which should be reported?)
6478 
6479    NOCONVERT suppresses the default promotions (such as from short to int).  */
6480 
6481 tree
cp_build_unary_op(enum tree_code code,tree xarg,bool noconvert,tsubst_flags_t complain)6482 cp_build_unary_op (enum tree_code code, tree xarg, bool noconvert,
6483                    tsubst_flags_t complain)
6484 {
6485   /* No default_conversion here.  It causes trouble for ADDR_EXPR.  */
6486   tree arg = xarg;
6487   location_t location = cp_expr_loc_or_input_loc (arg);
6488   tree argtype = 0;
6489   const char *errstring = NULL;
6490   tree val;
6491   const char *invalid_op_diag;
6492 
6493   if (!arg || error_operand_p (arg))
6494     return error_mark_node;
6495 
6496   arg = resolve_nondeduced_context (arg, complain);
6497 
6498   if ((invalid_op_diag
6499        = targetm.invalid_unary_op ((code == UNARY_PLUS_EXPR
6500 				    ? CONVERT_EXPR
6501 				    : code),
6502 				   TREE_TYPE (arg))))
6503     {
6504       if (complain & tf_error)
6505 	error (invalid_op_diag);
6506       return error_mark_node;
6507     }
6508 
6509   switch (code)
6510     {
6511     case UNARY_PLUS_EXPR:
6512     case NEGATE_EXPR:
6513       {
6514 	int flags = WANT_ARITH | WANT_ENUM;
6515 	/* Unary plus (but not unary minus) is allowed on pointers.  */
6516 	if (code == UNARY_PLUS_EXPR)
6517 	  flags |= WANT_POINTER;
6518 	arg = build_expr_type_conversion (flags, arg, true);
6519 	if (!arg)
6520 	  errstring = (code == NEGATE_EXPR
6521 		       ? _("wrong type argument to unary minus")
6522 		       : _("wrong type argument to unary plus"));
6523 	else
6524 	  {
6525 	    if (!noconvert && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (arg)))
6526 	      arg = cp_perform_integral_promotions (arg, complain);
6527 
6528 	    /* Make sure the result is not an lvalue: a unary plus or minus
6529 	       expression is always a rvalue.  */
6530 	    arg = rvalue (arg);
6531 	  }
6532       }
6533       break;
6534 
6535     case BIT_NOT_EXPR:
6536       if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
6537 	{
6538 	  code = CONJ_EXPR;
6539 	  if (!noconvert)
6540 	    {
6541 	      arg = cp_default_conversion (arg, complain);
6542 	      if (arg == error_mark_node)
6543 		return error_mark_node;
6544 	    }
6545 	}
6546       else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM
6547 						   | WANT_VECTOR_OR_COMPLEX,
6548 						   arg, true)))
6549 	errstring = _("wrong type argument to bit-complement");
6550       else if (!noconvert && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (arg)))
6551 	{
6552 	  /* Warn if the expression has boolean value.  */
6553 	  if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE
6554 	      && (complain & tf_warning)
6555 	      && warning_at (location, OPT_Wbool_operation,
6556 			     "%<~%> on an expression of type %<bool%>"))
6557 	    inform (location, "did you mean to use logical not (%<!%>)?");
6558 	  arg = cp_perform_integral_promotions (arg, complain);
6559 	}
6560       else if (!noconvert && VECTOR_TYPE_P (TREE_TYPE (arg)))
6561 	arg = mark_rvalue_use (arg);
6562       break;
6563 
6564     case ABS_EXPR:
6565       if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
6566 	errstring = _("wrong type argument to abs");
6567       else if (!noconvert)
6568 	{
6569 	  arg = cp_default_conversion (arg, complain);
6570 	  if (arg == error_mark_node)
6571 	    return error_mark_node;
6572 	}
6573       break;
6574 
6575     case CONJ_EXPR:
6576       /* Conjugating a real value is a no-op, but allow it anyway.  */
6577       if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
6578 	errstring = _("wrong type argument to conjugation");
6579       else if (!noconvert)
6580 	{
6581 	  arg = cp_default_conversion (arg, complain);
6582 	  if (arg == error_mark_node)
6583 	    return error_mark_node;
6584 	}
6585       break;
6586 
6587     case TRUTH_NOT_EXPR:
6588       if (gnu_vector_type_p (TREE_TYPE (arg)))
6589 	return cp_build_binary_op (input_location, EQ_EXPR, arg,
6590 				   build_zero_cst (TREE_TYPE (arg)), complain);
6591       arg = perform_implicit_conversion (boolean_type_node, arg,
6592 					 complain);
6593       if (arg != error_mark_node)
6594 	{
6595 	  val = invert_truthvalue_loc (location, arg);
6596 	  if (obvalue_p (val))
6597 	    val = non_lvalue_loc (location, val);
6598 	  return val;
6599 	}
6600       errstring = _("in argument to unary !");
6601       break;
6602 
6603     case NOP_EXPR:
6604       break;
6605 
6606     case REALPART_EXPR:
6607     case IMAGPART_EXPR:
6608       arg = build_real_imag_expr (input_location, code, arg);
6609       return arg;
6610 
6611     case PREINCREMENT_EXPR:
6612     case POSTINCREMENT_EXPR:
6613     case PREDECREMENT_EXPR:
6614     case POSTDECREMENT_EXPR:
6615       /* Handle complex lvalues (when permitted)
6616 	 by reduction to simpler cases.  */
6617 
6618       val = unary_complex_lvalue (code, arg);
6619       if (val != 0)
6620 	return val;
6621 
6622       arg = mark_lvalue_use (arg);
6623 
6624       /* Increment or decrement the real part of the value,
6625 	 and don't change the imaginary part.  */
6626       if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
6627 	{
6628 	  tree real, imag;
6629 
6630 	  arg = cp_stabilize_reference (arg);
6631 	  real = cp_build_unary_op (REALPART_EXPR, arg, true, complain);
6632 	  imag = cp_build_unary_op (IMAGPART_EXPR, arg, true, complain);
6633 	  real = cp_build_unary_op (code, real, true, complain);
6634 	  if (real == error_mark_node || imag == error_mark_node)
6635 	    return error_mark_node;
6636 	  return build2 (COMPLEX_EXPR, TREE_TYPE (arg),
6637 			 real, imag);
6638 	}
6639 
6640       /* Report invalid types.  */
6641 
6642       if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
6643 					      arg, true)))
6644 	{
6645 	  if (code == PREINCREMENT_EXPR)
6646 	    errstring = _("no pre-increment operator for type");
6647 	  else if (code == POSTINCREMENT_EXPR)
6648 	    errstring = _("no post-increment operator for type");
6649 	  else if (code == PREDECREMENT_EXPR)
6650 	    errstring = _("no pre-decrement operator for type");
6651 	  else
6652 	    errstring = _("no post-decrement operator for type");
6653 	  break;
6654 	}
6655       else if (arg == error_mark_node)
6656 	return error_mark_node;
6657 
6658       /* Report something read-only.  */
6659 
6660       if (CP_TYPE_CONST_P (TREE_TYPE (arg))
6661 	  || TREE_READONLY (arg))
6662         {
6663           if (complain & tf_error)
6664 	    cxx_readonly_error (location, arg,
6665 				((code == PREINCREMENT_EXPR
6666 				  || code == POSTINCREMENT_EXPR)
6667 				 ? lv_increment : lv_decrement));
6668           else
6669             return error_mark_node;
6670         }
6671 
6672       {
6673 	tree inc;
6674 	tree declared_type = unlowered_expr_type (arg);
6675 
6676 	argtype = TREE_TYPE (arg);
6677 
6678 	/* ARM $5.2.5 last annotation says this should be forbidden.  */
6679 	if (TREE_CODE (argtype) == ENUMERAL_TYPE)
6680           {
6681             if (complain & tf_error)
6682               permerror (location, (code == PREINCREMENT_EXPR
6683 				    || code == POSTINCREMENT_EXPR)
6684                          ? G_("ISO C++ forbids incrementing an enum")
6685                          : G_("ISO C++ forbids decrementing an enum"));
6686             else
6687               return error_mark_node;
6688           }
6689 
6690 	/* Compute the increment.  */
6691 
6692 	if (TYPE_PTR_P (argtype))
6693 	  {
6694 	    tree type = complete_type (TREE_TYPE (argtype));
6695 
6696 	    if (!COMPLETE_OR_VOID_TYPE_P (type))
6697               {
6698                 if (complain & tf_error)
6699                   error_at (location, ((code == PREINCREMENT_EXPR
6700 					|| code == POSTINCREMENT_EXPR))
6701 			    ? G_("cannot increment a pointer to incomplete "
6702 				 "type %qT")
6703 			    : G_("cannot decrement a pointer to incomplete "
6704 				 "type %qT"),
6705 			    TREE_TYPE (argtype));
6706                 else
6707                   return error_mark_node;
6708               }
6709 	    else if (!TYPE_PTROB_P (argtype))
6710               {
6711                 if (complain & tf_error)
6712                   pedwarn (location, OPT_Wpointer_arith,
6713 			   (code == PREINCREMENT_EXPR
6714                               || code == POSTINCREMENT_EXPR)
6715 			   ? G_("ISO C++ forbids incrementing a pointer "
6716 				"of type %qT")
6717 			   : G_("ISO C++ forbids decrementing a pointer "
6718 				"of type %qT"),
6719 			   argtype);
6720                 else
6721                   return error_mark_node;
6722               }
6723 	    else if (!verify_type_context (location, TCTX_POINTER_ARITH,
6724 					   TREE_TYPE (argtype),
6725 					   !(complain & tf_error)))
6726 	      return error_mark_node;
6727 
6728 	    inc = cxx_sizeof_nowarn (TREE_TYPE (argtype));
6729 	  }
6730 	else
6731 	  inc = VECTOR_TYPE_P (argtype)
6732 	    ? build_one_cst (argtype)
6733 	    : integer_one_node;
6734 
6735 	inc = cp_convert (argtype, inc, complain);
6736 
6737 	/* If 'arg' is an Objective-C PROPERTY_REF expression, then we
6738 	   need to ask Objective-C to build the increment or decrement
6739 	   expression for it.  */
6740 	if (objc_is_property_ref (arg))
6741 	  return objc_build_incr_expr_for_property_ref (input_location, code,
6742 							arg, inc);
6743 
6744 	/* Complain about anything else that is not a true lvalue.  */
6745 	if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
6746 				    || code == POSTINCREMENT_EXPR)
6747 				   ? lv_increment : lv_decrement),
6748                              complain))
6749 	  return error_mark_node;
6750 
6751 	/* [depr.volatile.type] "Postfix ++ and -- expressions and
6752 	   prefix ++ and -- expressions of volatile-qualified arithmetic
6753 	   and pointer types are deprecated."  */
6754 	if (TREE_THIS_VOLATILE (arg) || CP_TYPE_VOLATILE_P (TREE_TYPE (arg)))
6755 	  warning_at (location, OPT_Wvolatile,
6756 		      "%qs expression of %<volatile%>-qualified type is "
6757 		      "deprecated",
6758 		      ((code == PREINCREMENT_EXPR
6759 			|| code == POSTINCREMENT_EXPR)
6760 		       ? "++" : "--"));
6761 
6762 	/* Forbid using -- or ++ in C++17 on `bool'.  */
6763 	if (TREE_CODE (declared_type) == BOOLEAN_TYPE)
6764 	  {
6765 	    if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
6766 	      {
6767                 if (complain & tf_error)
6768 		  error_at (location,
6769 			    "use of an operand of type %qT in %<operator--%> "
6770 			    "is forbidden", boolean_type_node);
6771 		return error_mark_node;
6772 	      }
6773 	    else
6774 	      {
6775 		if (cxx_dialect >= cxx17)
6776 		  {
6777 		    if (complain & tf_error)
6778 		      error_at (location,
6779 				"use of an operand of type %qT in "
6780 				"%<operator++%> is forbidden in C++17",
6781 				boolean_type_node);
6782 		    return error_mark_node;
6783 		  }
6784 		/* Otherwise, [depr.incr.bool] says this is deprecated.  */
6785 		else
6786 		  warning_at (location, OPT_Wdeprecated,
6787 			      "use of an operand of type %qT "
6788 			      "in %<operator++%> is deprecated",
6789 			      boolean_type_node);
6790 	      }
6791 	    val = boolean_increment (code, arg);
6792 	  }
6793 	else if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
6794 	  /* An rvalue has no cv-qualifiers.  */
6795 	  val = build2 (code, cv_unqualified (TREE_TYPE (arg)), arg, inc);
6796 	else
6797 	  val = build2 (code, TREE_TYPE (arg), arg, inc);
6798 
6799 	TREE_SIDE_EFFECTS (val) = 1;
6800 	return val;
6801       }
6802 
6803     case ADDR_EXPR:
6804       /* Note that this operation never does default_conversion
6805 	 regardless of NOCONVERT.  */
6806       return cp_build_addr_expr (arg, complain);
6807 
6808     default:
6809       break;
6810     }
6811 
6812   if (!errstring)
6813     {
6814       if (argtype == 0)
6815 	argtype = TREE_TYPE (arg);
6816       return build1 (code, argtype, arg);
6817     }
6818 
6819   if (complain & tf_error)
6820     error_at (location, "%s", errstring);
6821   return error_mark_node;
6822 }
6823 
6824 /* Hook for the c-common bits that build a unary op.  */
6825 tree
build_unary_op(location_t,enum tree_code code,tree xarg,bool noconvert)6826 build_unary_op (location_t /*location*/,
6827 		enum tree_code code, tree xarg, bool noconvert)
6828 {
6829   return cp_build_unary_op (code, xarg, noconvert, tf_warning_or_error);
6830 }
6831 
6832 /* Adjust LVALUE, an MODIFY_EXPR, PREINCREMENT_EXPR or PREDECREMENT_EXPR,
6833    so that it is a valid lvalue even for GENERIC by replacing
6834    (lhs = rhs) with ((lhs = rhs), lhs)
6835    (--lhs) with ((--lhs), lhs)
6836    (++lhs) with ((++lhs), lhs)
6837    and if lhs has side-effects, calling cp_stabilize_reference on it, so
6838    that it can be evaluated multiple times.  */
6839 
6840 tree
genericize_compound_lvalue(tree lvalue)6841 genericize_compound_lvalue (tree lvalue)
6842 {
6843   if (TREE_SIDE_EFFECTS (TREE_OPERAND (lvalue, 0)))
6844     lvalue = build2 (TREE_CODE (lvalue), TREE_TYPE (lvalue),
6845 		     cp_stabilize_reference (TREE_OPERAND (lvalue, 0)),
6846 		     TREE_OPERAND (lvalue, 1));
6847   return build2 (COMPOUND_EXPR, TREE_TYPE (TREE_OPERAND (lvalue, 0)),
6848 		 lvalue, TREE_OPERAND (lvalue, 0));
6849 }
6850 
6851 /* Apply unary lvalue-demanding operator CODE to the expression ARG
6852    for certain kinds of expressions which are not really lvalues
6853    but which we can accept as lvalues.
6854 
6855    If ARG is not a kind of expression we can handle, return
6856    NULL_TREE.  */
6857 
6858 tree
unary_complex_lvalue(enum tree_code code,tree arg)6859 unary_complex_lvalue (enum tree_code code, tree arg)
6860 {
6861   /* Inside a template, making these kinds of adjustments is
6862      pointless; we are only concerned with the type of the
6863      expression.  */
6864   if (processing_template_decl)
6865     return NULL_TREE;
6866 
6867   /* Handle (a, b) used as an "lvalue".  */
6868   if (TREE_CODE (arg) == COMPOUND_EXPR)
6869     {
6870       tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 1), false,
6871                                             tf_warning_or_error);
6872       return build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
6873 		     TREE_OPERAND (arg, 0), real_result);
6874     }
6875 
6876   /* Handle (a ? b : c) used as an "lvalue".  */
6877   if (TREE_CODE (arg) == COND_EXPR
6878       || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
6879     return rationalize_conditional_expr (code, arg, tf_warning_or_error);
6880 
6881   /* Handle (a = b), (++a), and (--a) used as an "lvalue".  */
6882   if (TREE_CODE (arg) == MODIFY_EXPR
6883       || TREE_CODE (arg) == PREINCREMENT_EXPR
6884       || TREE_CODE (arg) == PREDECREMENT_EXPR)
6885     return unary_complex_lvalue (code, genericize_compound_lvalue (arg));
6886 
6887   if (code != ADDR_EXPR)
6888     return NULL_TREE;
6889 
6890   /* Handle (a = b) used as an "lvalue" for `&'.  */
6891   if (TREE_CODE (arg) == MODIFY_EXPR
6892       || TREE_CODE (arg) == INIT_EXPR)
6893     {
6894       tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 0), false,
6895                                             tf_warning_or_error);
6896       arg = build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
6897 		    arg, real_result);
6898       TREE_NO_WARNING (arg) = 1;
6899       return arg;
6900     }
6901 
6902   if (FUNC_OR_METHOD_TYPE_P (TREE_TYPE (arg))
6903       || TREE_CODE (arg) == OFFSET_REF)
6904     return NULL_TREE;
6905 
6906   /* We permit compiler to make function calls returning
6907      objects of aggregate type look like lvalues.  */
6908   {
6909     tree targ = arg;
6910 
6911     if (TREE_CODE (targ) == SAVE_EXPR)
6912       targ = TREE_OPERAND (targ, 0);
6913 
6914     if (TREE_CODE (targ) == CALL_EXPR && MAYBE_CLASS_TYPE_P (TREE_TYPE (targ)))
6915       {
6916 	if (TREE_CODE (arg) == SAVE_EXPR)
6917 	  targ = arg;
6918 	else
6919 	  targ = build_cplus_new (TREE_TYPE (arg), arg, tf_warning_or_error);
6920 	return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
6921       }
6922 
6923     if (TREE_CODE (arg) == SAVE_EXPR && INDIRECT_REF_P (targ))
6924       return build3 (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
6925 		     TREE_OPERAND (targ, 0), current_function_decl, NULL);
6926   }
6927 
6928   /* Don't let anything else be handled specially.  */
6929   return NULL_TREE;
6930 }
6931 
6932 /* Mark EXP saying that we need to be able to take the
6933    address of it; it should not be allocated in a register.
6934    Value is true if successful.  ARRAY_REF_P is true if this
6935    is for ARRAY_REF construction - in that case we don't want
6936    to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
6937    it is fine to use ARRAY_REFs for vector subscripts on vector
6938    register variables.
6939 
6940    C++: we do not allow `current_class_ptr' to be addressable.  */
6941 
6942 bool
cxx_mark_addressable(tree exp,bool array_ref_p)6943 cxx_mark_addressable (tree exp, bool array_ref_p)
6944 {
6945   tree x = exp;
6946 
6947   while (1)
6948     switch (TREE_CODE (x))
6949       {
6950       case VIEW_CONVERT_EXPR:
6951 	if (array_ref_p
6952 	    && TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
6953 	    && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x, 0))))
6954 	  return true;
6955 	x = TREE_OPERAND (x, 0);
6956 	break;
6957 
6958       case COMPONENT_REF:
6959 	if (bitfield_p (x))
6960 	  error ("attempt to take address of bit-field");
6961 	/* FALLTHRU */
6962       case ADDR_EXPR:
6963       case ARRAY_REF:
6964       case REALPART_EXPR:
6965       case IMAGPART_EXPR:
6966 	x = TREE_OPERAND (x, 0);
6967 	break;
6968 
6969       case PARM_DECL:
6970 	if (x == current_class_ptr)
6971 	  {
6972 	    error ("cannot take the address of %<this%>, which is an rvalue expression");
6973 	    TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later.  */
6974 	    return true;
6975 	  }
6976 	/* Fall through.  */
6977 
6978       case VAR_DECL:
6979 	/* Caller should not be trying to mark initialized
6980 	   constant fields addressable.  */
6981 	gcc_assert (DECL_LANG_SPECIFIC (x) == 0
6982 		    || DECL_IN_AGGR_P (x) == 0
6983 		    || TREE_STATIC (x)
6984 		    || DECL_EXTERNAL (x));
6985 	/* Fall through.  */
6986 
6987       case RESULT_DECL:
6988 	if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
6989 	    && !DECL_ARTIFICIAL (x))
6990 	  {
6991 	    if (VAR_P (x) && DECL_HARD_REGISTER (x))
6992 	      {
6993 		error
6994 		  ("address of explicit register variable %qD requested", x);
6995 		return false;
6996 	      }
6997 	    else if (extra_warnings)
6998 	      warning
6999 		(OPT_Wextra, "address requested for %qD, which is declared %<register%>", x);
7000 	  }
7001 	TREE_ADDRESSABLE (x) = 1;
7002 	return true;
7003 
7004       case CONST_DECL:
7005       case FUNCTION_DECL:
7006 	TREE_ADDRESSABLE (x) = 1;
7007 	return true;
7008 
7009       case CONSTRUCTOR:
7010 	TREE_ADDRESSABLE (x) = 1;
7011 	return true;
7012 
7013       case TARGET_EXPR:
7014 	TREE_ADDRESSABLE (x) = 1;
7015 	cxx_mark_addressable (TREE_OPERAND (x, 0));
7016 	return true;
7017 
7018       default:
7019 	return true;
7020     }
7021 }
7022 
7023 /* Build and return a conditional expression IFEXP ? OP1 : OP2.  */
7024 
7025 tree
build_x_conditional_expr(location_t loc,tree ifexp,tree op1,tree op2,tsubst_flags_t complain)7026 build_x_conditional_expr (location_t loc, tree ifexp, tree op1, tree op2,
7027                           tsubst_flags_t complain)
7028 {
7029   tree orig_ifexp = ifexp;
7030   tree orig_op1 = op1;
7031   tree orig_op2 = op2;
7032   tree expr;
7033 
7034   if (processing_template_decl)
7035     {
7036       /* The standard says that the expression is type-dependent if
7037 	 IFEXP is type-dependent, even though the eventual type of the
7038 	 expression doesn't dependent on IFEXP.  */
7039       if (type_dependent_expression_p (ifexp)
7040 	  /* As a GNU extension, the middle operand may be omitted.  */
7041 	  || (op1 && type_dependent_expression_p (op1))
7042 	  || type_dependent_expression_p (op2))
7043 	return build_min_nt_loc (loc, COND_EXPR, ifexp, op1, op2);
7044       ifexp = build_non_dependent_expr (ifexp);
7045       if (op1)
7046 	op1 = build_non_dependent_expr (op1);
7047       op2 = build_non_dependent_expr (op2);
7048     }
7049 
7050   expr = build_conditional_expr (loc, ifexp, op1, op2, complain);
7051   if (processing_template_decl && expr != error_mark_node)
7052     {
7053       tree min = build_min_non_dep (COND_EXPR, expr,
7054 				    orig_ifexp, orig_op1, orig_op2);
7055       expr = convert_from_reference (min);
7056     }
7057   return expr;
7058 }
7059 
7060 /* Given a list of expressions, return a compound expression
7061    that performs them all and returns the value of the last of them.  */
7062 
7063 tree
build_x_compound_expr_from_list(tree list,expr_list_kind exp,tsubst_flags_t complain)7064 build_x_compound_expr_from_list (tree list, expr_list_kind exp,
7065 				 tsubst_flags_t complain)
7066 {
7067   tree expr = TREE_VALUE (list);
7068 
7069   if (BRACE_ENCLOSED_INITIALIZER_P (expr)
7070       && !CONSTRUCTOR_IS_DIRECT_INIT (expr))
7071     {
7072       if (complain & tf_error)
7073 	pedwarn (cp_expr_loc_or_input_loc (expr), 0,
7074 		 "list-initializer for non-class type must not "
7075 		 "be parenthesized");
7076       else
7077 	return error_mark_node;
7078     }
7079 
7080   if (TREE_CHAIN (list))
7081     {
7082       if (complain & tf_error)
7083 	switch (exp)
7084 	  {
7085 	  case ELK_INIT:
7086 	    permerror (input_location, "expression list treated as compound "
7087 				       "expression in initializer");
7088 	    break;
7089 	  case ELK_MEM_INIT:
7090 	    permerror (input_location, "expression list treated as compound "
7091 				       "expression in mem-initializer");
7092 	    break;
7093 	  case ELK_FUNC_CAST:
7094 	    permerror (input_location, "expression list treated as compound "
7095 				       "expression in functional cast");
7096 	    break;
7097 	  default:
7098 	    gcc_unreachable ();
7099 	  }
7100       else
7101 	return error_mark_node;
7102 
7103       for (list = TREE_CHAIN (list); list; list = TREE_CHAIN (list))
7104 	expr = build_x_compound_expr (EXPR_LOCATION (TREE_VALUE (list)),
7105 				      expr, TREE_VALUE (list), complain);
7106     }
7107 
7108   return expr;
7109 }
7110 
7111 /* Like build_x_compound_expr_from_list, but using a VEC.  */
7112 
7113 tree
build_x_compound_expr_from_vec(vec<tree,va_gc> * vec,const char * msg,tsubst_flags_t complain)7114 build_x_compound_expr_from_vec (vec<tree, va_gc> *vec, const char *msg,
7115 				tsubst_flags_t complain)
7116 {
7117   if (vec_safe_is_empty (vec))
7118     return NULL_TREE;
7119   else if (vec->length () == 1)
7120     return (*vec)[0];
7121   else
7122     {
7123       tree expr;
7124       unsigned int ix;
7125       tree t;
7126 
7127       if (msg != NULL)
7128 	{
7129 	  if (complain & tf_error)
7130 	    permerror (input_location,
7131 		       "%s expression list treated as compound expression",
7132 		       msg);
7133 	  else
7134 	    return error_mark_node;
7135 	}
7136 
7137       expr = (*vec)[0];
7138       for (ix = 1; vec->iterate (ix, &t); ++ix)
7139 	expr = build_x_compound_expr (EXPR_LOCATION (t), expr,
7140 				      t, complain);
7141 
7142       return expr;
7143     }
7144 }
7145 
7146 /* Handle overloading of the ',' operator when needed.  */
7147 
7148 tree
build_x_compound_expr(location_t loc,tree op1,tree op2,tsubst_flags_t complain)7149 build_x_compound_expr (location_t loc, tree op1, tree op2,
7150 		       tsubst_flags_t complain)
7151 {
7152   tree result;
7153   tree orig_op1 = op1;
7154   tree orig_op2 = op2;
7155   tree overload = NULL_TREE;
7156 
7157   if (processing_template_decl)
7158     {
7159       if (type_dependent_expression_p (op1)
7160 	  || type_dependent_expression_p (op2))
7161 	return build_min_nt_loc (loc, COMPOUND_EXPR, op1, op2);
7162       op1 = build_non_dependent_expr (op1);
7163       op2 = build_non_dependent_expr (op2);
7164     }
7165 
7166   result = build_new_op (loc, COMPOUND_EXPR, LOOKUP_NORMAL, op1, op2,
7167 			 NULL_TREE, &overload, complain);
7168   if (!result)
7169     result = cp_build_compound_expr (op1, op2, complain);
7170 
7171   if (processing_template_decl && result != error_mark_node)
7172     {
7173       if (overload != NULL_TREE)
7174 	return (build_min_non_dep_op_overload
7175 		(COMPOUND_EXPR, result, overload, orig_op1, orig_op2));
7176 
7177       return build_min_non_dep (COMPOUND_EXPR, result, orig_op1, orig_op2);
7178     }
7179 
7180   return result;
7181 }
7182 
7183 /* Like cp_build_compound_expr, but for the c-common bits.  */
7184 
7185 tree
build_compound_expr(location_t,tree lhs,tree rhs)7186 build_compound_expr (location_t /*loc*/, tree lhs, tree rhs)
7187 {
7188   return cp_build_compound_expr (lhs, rhs, tf_warning_or_error);
7189 }
7190 
7191 /* Build a compound expression.  */
7192 
7193 tree
cp_build_compound_expr(tree lhs,tree rhs,tsubst_flags_t complain)7194 cp_build_compound_expr (tree lhs, tree rhs, tsubst_flags_t complain)
7195 {
7196   lhs = convert_to_void (lhs, ICV_LEFT_OF_COMMA, complain);
7197 
7198   if (lhs == error_mark_node || rhs == error_mark_node)
7199     return error_mark_node;
7200 
7201   if (TREE_CODE (rhs) == TARGET_EXPR)
7202     {
7203       /* If the rhs is a TARGET_EXPR, then build the compound
7204 	 expression inside the target_expr's initializer. This
7205 	 helps the compiler to eliminate unnecessary temporaries.  */
7206       tree init = TREE_OPERAND (rhs, 1);
7207 
7208       init = build2 (COMPOUND_EXPR, TREE_TYPE (init), lhs, init);
7209       TREE_OPERAND (rhs, 1) = init;
7210 
7211       return rhs;
7212     }
7213 
7214   if (type_unknown_p (rhs))
7215     {
7216       if (complain & tf_error)
7217 	error_at (cp_expr_loc_or_input_loc (rhs),
7218 		  "no context to resolve type of %qE", rhs);
7219       return error_mark_node;
7220     }
7221 
7222   return build2 (COMPOUND_EXPR, TREE_TYPE (rhs), lhs, rhs);
7223 }
7224 
7225 /* Issue a diagnostic message if casting from SRC_TYPE to DEST_TYPE
7226    casts away constness.  CAST gives the type of cast.  Returns true
7227    if the cast is ill-formed, false if it is well-formed.
7228 
7229    ??? This function warns for casting away any qualifier not just
7230    const.  We would like to specify exactly what qualifiers are casted
7231    away.
7232 */
7233 
7234 static bool
check_for_casting_away_constness(location_t loc,tree src_type,tree dest_type,enum tree_code cast,tsubst_flags_t complain)7235 check_for_casting_away_constness (location_t loc, tree src_type,
7236 				  tree dest_type, enum tree_code cast,
7237 				  tsubst_flags_t complain)
7238 {
7239   /* C-style casts are allowed to cast away constness.  With
7240      WARN_CAST_QUAL, we still want to issue a warning.  */
7241   if (cast == CAST_EXPR && !warn_cast_qual)
7242     return false;
7243 
7244   if (!casts_away_constness (src_type, dest_type, complain))
7245     return false;
7246 
7247   switch (cast)
7248     {
7249     case CAST_EXPR:
7250       if (complain & tf_warning)
7251 	warning_at (loc, OPT_Wcast_qual,
7252 		    "cast from type %qT to type %qT casts away qualifiers",
7253 		    src_type, dest_type);
7254       return false;
7255 
7256     case STATIC_CAST_EXPR:
7257       if (complain & tf_error)
7258 	error_at (loc, "%<static_cast%> from type %qT to type %qT casts "
7259 		  "away qualifiers",
7260 		  src_type, dest_type);
7261       return true;
7262 
7263     case REINTERPRET_CAST_EXPR:
7264       if (complain & tf_error)
7265 	error_at (loc, "%<reinterpret_cast%> from type %qT to type %qT "
7266 		  "casts away qualifiers",
7267 		  src_type, dest_type);
7268       return true;
7269 
7270     default:
7271       gcc_unreachable();
7272     }
7273 }
7274 
7275 /* Warns if the cast from expression EXPR to type TYPE is useless.  */
7276 void
maybe_warn_about_useless_cast(location_t loc,tree type,tree expr,tsubst_flags_t complain)7277 maybe_warn_about_useless_cast (location_t loc, tree type, tree expr,
7278 			       tsubst_flags_t complain)
7279 {
7280   if (warn_useless_cast
7281       && complain & tf_warning)
7282     {
7283       if ((TYPE_REF_P (type)
7284 	   && (TYPE_REF_IS_RVALUE (type)
7285 	       ? xvalue_p (expr) : lvalue_p (expr))
7286 	   && same_type_p (TREE_TYPE (expr), TREE_TYPE (type)))
7287 	  || same_type_p (TREE_TYPE (expr), type))
7288 	warning_at (loc, OPT_Wuseless_cast,
7289 		    "useless cast to type %q#T", type);
7290     }
7291 }
7292 
7293 /* Warns if the cast ignores cv-qualifiers on TYPE.  */
7294 static void
maybe_warn_about_cast_ignoring_quals(location_t loc,tree type,tsubst_flags_t complain)7295 maybe_warn_about_cast_ignoring_quals (location_t loc, tree type,
7296 				      tsubst_flags_t complain)
7297 {
7298   if (warn_ignored_qualifiers
7299       && complain & tf_warning
7300       && !CLASS_TYPE_P (type)
7301       && (cp_type_quals (type) & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)))
7302     warning_at (loc, OPT_Wignored_qualifiers,
7303 		"type qualifiers ignored on cast result type");
7304 }
7305 
7306 /* Convert EXPR (an expression with pointer-to-member type) to TYPE
7307    (another pointer-to-member type in the same hierarchy) and return
7308    the converted expression.  If ALLOW_INVERSE_P is permitted, a
7309    pointer-to-derived may be converted to pointer-to-base; otherwise,
7310    only the other direction is permitted.  If C_CAST_P is true, this
7311    conversion is taking place as part of a C-style cast.  */
7312 
7313 tree
convert_ptrmem(tree type,tree expr,bool allow_inverse_p,bool c_cast_p,tsubst_flags_t complain)7314 convert_ptrmem (tree type, tree expr, bool allow_inverse_p,
7315 		bool c_cast_p, tsubst_flags_t complain)
7316 {
7317   if (same_type_p (type, TREE_TYPE (expr)))
7318     return expr;
7319 
7320   if (TYPE_PTRDATAMEM_P (type))
7321     {
7322       tree obase = TYPE_PTRMEM_CLASS_TYPE (TREE_TYPE (expr));
7323       tree nbase = TYPE_PTRMEM_CLASS_TYPE (type);
7324       tree delta = (get_delta_difference
7325 		    (obase, nbase,
7326 		     allow_inverse_p, c_cast_p, complain));
7327 
7328       if (delta == error_mark_node)
7329 	return error_mark_node;
7330 
7331       if (!same_type_p (obase, nbase))
7332 	{
7333 	  if (TREE_CODE (expr) == PTRMEM_CST)
7334 	    expr = cplus_expand_constant (expr);
7335 
7336 	  tree cond = cp_build_binary_op (input_location, EQ_EXPR, expr,
7337 					  build_int_cst (TREE_TYPE (expr), -1),
7338 					  complain);
7339 	  tree op1 = build_nop (ptrdiff_type_node, expr);
7340 	  tree op2 = cp_build_binary_op (input_location, PLUS_EXPR, op1, delta,
7341 					 complain);
7342 
7343 	  expr = fold_build3_loc (input_location,
7344 				  COND_EXPR, ptrdiff_type_node, cond, op1, op2);
7345 	}
7346 
7347       return build_nop (type, expr);
7348     }
7349   else
7350     return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr,
7351 			     allow_inverse_p, c_cast_p, complain);
7352 }
7353 
7354 /* Perform a static_cast from EXPR to TYPE.  When C_CAST_P is true,
7355    this static_cast is being attempted as one of the possible casts
7356    allowed by a C-style cast.  (In that case, accessibility of base
7357    classes is not considered, and it is OK to cast away
7358    constness.)  Return the result of the cast.  *VALID_P is set to
7359    indicate whether or not the cast was valid.  */
7360 
7361 static tree
build_static_cast_1(location_t loc,tree type,tree expr,bool c_cast_p,bool * valid_p,tsubst_flags_t complain)7362 build_static_cast_1 (location_t loc, tree type, tree expr, bool c_cast_p,
7363 		     bool *valid_p, tsubst_flags_t complain)
7364 {
7365   tree intype;
7366   tree result;
7367   cp_lvalue_kind clk;
7368 
7369   /* Assume the cast is valid.  */
7370   *valid_p = true;
7371 
7372   intype = unlowered_expr_type (expr);
7373 
7374   /* Save casted types in the function's used types hash table.  */
7375   used_types_insert (type);
7376 
7377   /* A prvalue of non-class type is cv-unqualified.  */
7378   if (!CLASS_TYPE_P (type))
7379     type = cv_unqualified (type);
7380 
7381   /* [expr.static.cast]
7382 
7383      An lvalue of type "cv1 B", where B is a class type, can be cast
7384      to type "reference to cv2 D", where D is a class derived (clause
7385      _class.derived_) from B, if a valid standard conversion from
7386      "pointer to D" to "pointer to B" exists (_conv.ptr_), cv2 is the
7387      same cv-qualification as, or greater cv-qualification than, cv1,
7388      and B is not a virtual base class of D.  */
7389   /* We check this case before checking the validity of "TYPE t =
7390      EXPR;" below because for this case:
7391 
7392        struct B {};
7393        struct D : public B { D(const B&); };
7394        extern B& b;
7395        void f() { static_cast<const D&>(b); }
7396 
7397      we want to avoid constructing a new D.  The standard is not
7398      completely clear about this issue, but our interpretation is
7399      consistent with other compilers.  */
7400   if (TYPE_REF_P (type)
7401       && CLASS_TYPE_P (TREE_TYPE (type))
7402       && CLASS_TYPE_P (intype)
7403       && (TYPE_REF_IS_RVALUE (type) || lvalue_p (expr))
7404       && DERIVED_FROM_P (intype, TREE_TYPE (type))
7405       && can_convert (build_pointer_type (TYPE_MAIN_VARIANT (intype)),
7406 		      build_pointer_type (TYPE_MAIN_VARIANT
7407 					  (TREE_TYPE (type))),
7408 		      complain)
7409       && (c_cast_p
7410 	  || at_least_as_qualified_p (TREE_TYPE (type), intype)))
7411     {
7412       tree base;
7413 
7414       if (processing_template_decl)
7415 	return expr;
7416 
7417       /* There is a standard conversion from "D*" to "B*" even if "B"
7418 	 is ambiguous or inaccessible.  If this is really a
7419 	 static_cast, then we check both for inaccessibility and
7420 	 ambiguity.  However, if this is a static_cast being performed
7421 	 because the user wrote a C-style cast, then accessibility is
7422 	 not considered.  */
7423       base = lookup_base (TREE_TYPE (type), intype,
7424 			  c_cast_p ? ba_unique : ba_check,
7425 			  NULL, complain);
7426       expr = cp_build_addr_expr (expr, complain);
7427 
7428       if (sanitize_flags_p (SANITIZE_VPTR))
7429 	{
7430 	  tree ubsan_check
7431 	    = cp_ubsan_maybe_instrument_downcast (loc, type,
7432 						  intype, expr);
7433 	  if (ubsan_check)
7434 	    expr = ubsan_check;
7435 	}
7436 
7437       /* Convert from "B*" to "D*".  This function will check that "B"
7438 	 is not a virtual base of "D".  Even if we don't have a guarantee
7439 	 that expr is NULL, if the static_cast is to a reference type,
7440 	 it is UB if it would be NULL, so omit the non-NULL check.  */
7441       expr = build_base_path (MINUS_EXPR, expr, base,
7442 			      /*nonnull=*/flag_delete_null_pointer_checks,
7443 			      complain);
7444 
7445       /* Convert the pointer to a reference -- but then remember that
7446 	 there are no expressions with reference type in C++.
7447 
7448          We call rvalue so that there's an actual tree code
7449          (NON_LVALUE_EXPR) for the static_cast; otherwise, if the operand
7450          is a variable with the same type, the conversion would get folded
7451          away, leaving just the variable and causing lvalue_kind to give
7452          the wrong answer.  */
7453       expr = cp_fold_convert (type, expr);
7454 
7455       /* When -fsanitize=null, make sure to diagnose reference binding to
7456 	 NULL even when the reference is converted to pointer later on.  */
7457       if (sanitize_flags_p (SANITIZE_NULL)
7458 	  && TREE_CODE (expr) == COND_EXPR
7459 	  && TREE_OPERAND (expr, 2)
7460 	  && TREE_CODE (TREE_OPERAND (expr, 2)) == INTEGER_CST
7461 	  && TREE_TYPE (TREE_OPERAND (expr, 2)) == type)
7462 	ubsan_maybe_instrument_reference (&TREE_OPERAND (expr, 2));
7463 
7464       return convert_from_reference (rvalue (expr));
7465     }
7466 
7467   /* "A glvalue of type cv1 T1 can be cast to type rvalue reference to
7468      cv2 T2 if cv2 T2 is reference-compatible with cv1 T1 (8.5.3)."  */
7469   if (TYPE_REF_P (type)
7470       && TYPE_REF_IS_RVALUE (type)
7471       && (clk = real_lvalue_p (expr))
7472       && reference_compatible_p (TREE_TYPE (type), intype)
7473       && (c_cast_p || at_least_as_qualified_p (TREE_TYPE (type), intype)))
7474     {
7475       if (processing_template_decl)
7476 	return expr;
7477       if (clk == clk_ordinary)
7478 	{
7479 	  /* Handle the (non-bit-field) lvalue case here by casting to
7480 	     lvalue reference and then changing it to an rvalue reference.
7481 	     Casting an xvalue to rvalue reference will be handled by the
7482 	     main code path.  */
7483 	  tree lref = cp_build_reference_type (TREE_TYPE (type), false);
7484 	  result = (perform_direct_initialization_if_possible
7485 		    (lref, expr, c_cast_p, complain));
7486 	  result = build1 (NON_LVALUE_EXPR, type, result);
7487 	  return convert_from_reference (result);
7488 	}
7489       else
7490 	/* For a bit-field or packed field, bind to a temporary.  */
7491 	expr = rvalue (expr);
7492     }
7493 
7494   /* Resolve overloaded address here rather than once in
7495      implicit_conversion and again in the inverse code below.  */
7496   if (TYPE_PTRMEMFUNC_P (type) && type_unknown_p (expr))
7497     {
7498       expr = instantiate_type (type, expr, complain);
7499       intype = TREE_TYPE (expr);
7500     }
7501 
7502   /* [expr.static.cast]
7503 
7504      Any expression can be explicitly converted to type cv void.  */
7505   if (VOID_TYPE_P (type))
7506     return convert_to_void (expr, ICV_CAST, complain);
7507 
7508   /* [class.abstract]
7509      An abstract class shall not be used ... as the type of an explicit
7510      conversion.  */
7511   if (abstract_virtuals_error_sfinae (ACU_CAST, type, complain))
7512     return error_mark_node;
7513 
7514   /* [expr.static.cast]
7515 
7516      An expression e can be explicitly converted to a type T using a
7517      static_cast of the form static_cast<T>(e) if the declaration T
7518      t(e);" is well-formed, for some invented temporary variable
7519      t.  */
7520   result = perform_direct_initialization_if_possible (type, expr,
7521 						      c_cast_p, complain);
7522   if (result)
7523     {
7524       if (processing_template_decl)
7525 	return expr;
7526 
7527       result = convert_from_reference (result);
7528 
7529       /* [expr.static.cast]
7530 
7531 	 If T is a reference type, the result is an lvalue; otherwise,
7532 	 the result is an rvalue.  */
7533       if (!TYPE_REF_P (type))
7534 	{
7535 	  result = rvalue (result);
7536 
7537 	  if (result == expr && SCALAR_TYPE_P (type))
7538 	    /* Leave some record of the cast.  */
7539 	    result = build_nop (type, expr);
7540 	}
7541       return result;
7542     }
7543 
7544   /* [expr.static.cast]
7545 
7546      The inverse of any standard conversion sequence (clause _conv_),
7547      other than the lvalue-to-rvalue (_conv.lval_), array-to-pointer
7548      (_conv.array_), function-to-pointer (_conv.func_), and boolean
7549      (_conv.bool_) conversions, can be performed explicitly using
7550      static_cast subject to the restriction that the explicit
7551      conversion does not cast away constness (_expr.const.cast_), and
7552      the following additional rules for specific cases:  */
7553   /* For reference, the conversions not excluded are: integral
7554      promotions, floating-point promotion, integral conversions,
7555      floating-point conversions, floating-integral conversions,
7556      pointer conversions, and pointer to member conversions.  */
7557   /* DR 128
7558 
7559      A value of integral _or enumeration_ type can be explicitly
7560      converted to an enumeration type.  */
7561   /* The effect of all that is that any conversion between any two
7562      types which are integral, floating, or enumeration types can be
7563      performed.  */
7564   if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
7565        || SCALAR_FLOAT_TYPE_P (type))
7566       && (INTEGRAL_OR_ENUMERATION_TYPE_P (intype)
7567 	  || SCALAR_FLOAT_TYPE_P (intype)))
7568     {
7569       if (processing_template_decl)
7570 	return expr;
7571       return ocp_convert (type, expr, CONV_C_CAST, LOOKUP_NORMAL, complain);
7572     }
7573 
7574   if (TYPE_PTR_P (type) && TYPE_PTR_P (intype)
7575       && CLASS_TYPE_P (TREE_TYPE (type))
7576       && CLASS_TYPE_P (TREE_TYPE (intype))
7577       && can_convert (build_pointer_type (TYPE_MAIN_VARIANT
7578 					  (TREE_TYPE (intype))),
7579 		      build_pointer_type (TYPE_MAIN_VARIANT
7580 					  (TREE_TYPE (type))),
7581 		      complain))
7582     {
7583       tree base;
7584 
7585       if (processing_template_decl)
7586 	return expr;
7587 
7588       if (!c_cast_p
7589 	  && check_for_casting_away_constness (loc, intype, type,
7590 					       STATIC_CAST_EXPR,
7591 					       complain))
7592 	return error_mark_node;
7593       base = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
7594 			  c_cast_p ? ba_unique : ba_check,
7595 			  NULL, complain);
7596       expr = build_base_path (MINUS_EXPR, expr, base, /*nonnull=*/false,
7597 			      complain);
7598 
7599       if (sanitize_flags_p (SANITIZE_VPTR))
7600 	{
7601 	  tree ubsan_check
7602 	    = cp_ubsan_maybe_instrument_downcast (loc, type,
7603 						  intype, expr);
7604 	  if (ubsan_check)
7605 	    expr = ubsan_check;
7606 	}
7607 
7608       return cp_fold_convert (type, expr);
7609     }
7610 
7611   if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
7612       || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
7613     {
7614       tree c1;
7615       tree c2;
7616       tree t1;
7617       tree t2;
7618 
7619       c1 = TYPE_PTRMEM_CLASS_TYPE (intype);
7620       c2 = TYPE_PTRMEM_CLASS_TYPE (type);
7621 
7622       if (TYPE_PTRDATAMEM_P (type))
7623 	{
7624 	  t1 = (build_ptrmem_type
7625 		(c1,
7626 		 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (intype))));
7627 	  t2 = (build_ptrmem_type
7628 		(c2,
7629 		 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
7630 	}
7631       else
7632 	{
7633 	  t1 = intype;
7634 	  t2 = type;
7635 	}
7636       if (can_convert (t1, t2, complain) || can_convert (t2, t1, complain))
7637 	{
7638 	  if (!c_cast_p
7639 	      && check_for_casting_away_constness (loc, intype, type,
7640 						   STATIC_CAST_EXPR,
7641 						   complain))
7642 	    return error_mark_node;
7643 	  if (processing_template_decl)
7644 	    return expr;
7645 	  return convert_ptrmem (type, expr, /*allow_inverse_p=*/1,
7646 				 c_cast_p, complain);
7647 	}
7648     }
7649 
7650   /* [expr.static.cast]
7651 
7652      An rvalue of type "pointer to cv void" can be explicitly
7653      converted to a pointer to object type.  A value of type pointer
7654      to object converted to "pointer to cv void" and back to the
7655      original pointer type will have its original value.  */
7656   if (TYPE_PTR_P (intype)
7657       && VOID_TYPE_P (TREE_TYPE (intype))
7658       && TYPE_PTROB_P (type))
7659     {
7660       if (!c_cast_p
7661 	  && check_for_casting_away_constness (loc, intype, type,
7662 					       STATIC_CAST_EXPR,
7663 					       complain))
7664 	return error_mark_node;
7665       if (processing_template_decl)
7666 	return expr;
7667       return build_nop (type, expr);
7668     }
7669 
7670   *valid_p = false;
7671   return error_mark_node;
7672 }
7673 
7674 /* Return an expression representing static_cast<TYPE>(EXPR).  */
7675 
7676 tree
build_static_cast(location_t loc,tree type,tree oexpr,tsubst_flags_t complain)7677 build_static_cast (location_t loc, tree type, tree oexpr,
7678 		   tsubst_flags_t complain)
7679 {
7680   tree expr = oexpr;
7681   tree result;
7682   bool valid_p;
7683 
7684   if (type == error_mark_node || expr == error_mark_node)
7685     return error_mark_node;
7686 
7687   bool dependent = (dependent_type_p (type)
7688 		    || type_dependent_expression_p (expr));
7689   if (dependent)
7690     {
7691     tmpl:
7692       expr = build_min (STATIC_CAST_EXPR, type, oexpr);
7693       /* We don't know if it will or will not have side effects.  */
7694       TREE_SIDE_EFFECTS (expr) = 1;
7695       result = convert_from_reference (expr);
7696       protected_set_expr_location (result, loc);
7697       return result;
7698     }
7699   else if (processing_template_decl)
7700     expr = build_non_dependent_expr (expr);
7701 
7702   /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7703      Strip such NOP_EXPRs if VALUE is being used in non-lvalue context.  */
7704   if (!TYPE_REF_P (type)
7705       && TREE_CODE (expr) == NOP_EXPR
7706       && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
7707     expr = TREE_OPERAND (expr, 0);
7708 
7709   result = build_static_cast_1 (loc, type, expr, /*c_cast_p=*/false,
7710 				&valid_p, complain);
7711   if (valid_p)
7712     {
7713       if (result != error_mark_node)
7714 	{
7715 	  maybe_warn_about_useless_cast (loc, type, expr, complain);
7716 	  maybe_warn_about_cast_ignoring_quals (loc, type, complain);
7717 	}
7718       if (processing_template_decl)
7719 	goto tmpl;
7720       protected_set_expr_location (result, loc);
7721       return result;
7722     }
7723 
7724   if (complain & tf_error)
7725     {
7726       error_at (loc, "invalid %<static_cast%> from type %qT to type %qT",
7727 		TREE_TYPE (expr), type);
7728       if ((TYPE_PTR_P (type) || TYPE_REF_P (type))
7729 	  && CLASS_TYPE_P (TREE_TYPE (type))
7730 	    && !COMPLETE_TYPE_P (TREE_TYPE (type)))
7731 	inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (TREE_TYPE (type))),
7732 		"class type %qT is incomplete", TREE_TYPE (type));
7733       tree expr_type = TREE_TYPE (expr);
7734       if (TYPE_PTR_P (expr_type))
7735 	expr_type = TREE_TYPE (expr_type);
7736       if (CLASS_TYPE_P (expr_type) && !COMPLETE_TYPE_P (expr_type))
7737 	inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (expr_type)),
7738 		"class type %qT is incomplete", expr_type);
7739     }
7740   return error_mark_node;
7741 }
7742 
7743 /* EXPR is an expression with member function or pointer-to-member
7744    function type.  TYPE is a pointer type.  Converting EXPR to TYPE is
7745    not permitted by ISO C++, but we accept it in some modes.  If we
7746    are not in one of those modes, issue a diagnostic.  Return the
7747    converted expression.  */
7748 
7749 tree
convert_member_func_to_ptr(tree type,tree expr,tsubst_flags_t complain)7750 convert_member_func_to_ptr (tree type, tree expr, tsubst_flags_t complain)
7751 {
7752   tree intype;
7753   tree decl;
7754 
7755   intype = TREE_TYPE (expr);
7756   gcc_assert (TYPE_PTRMEMFUNC_P (intype)
7757 	      || TREE_CODE (intype) == METHOD_TYPE);
7758 
7759   if (!(complain & tf_warning_or_error))
7760     return error_mark_node;
7761 
7762   if (pedantic || warn_pmf2ptr)
7763     pedwarn (input_location, pedantic ? OPT_Wpedantic : OPT_Wpmf_conversions,
7764 	     "converting from %qH to %qI", intype, type);
7765 
7766   if (TREE_CODE (intype) == METHOD_TYPE)
7767     expr = build_addr_func (expr, complain);
7768   else if (TREE_CODE (expr) == PTRMEM_CST)
7769     expr = build_address (PTRMEM_CST_MEMBER (expr));
7770   else
7771     {
7772       decl = maybe_dummy_object (TYPE_PTRMEM_CLASS_TYPE (intype), 0);
7773       decl = build_address (decl);
7774       expr = get_member_function_from_ptrfunc (&decl, expr, complain);
7775     }
7776 
7777   if (expr == error_mark_node)
7778     return error_mark_node;
7779 
7780   return build_nop (type, expr);
7781 }
7782 
7783 /* Build a NOP_EXPR to TYPE, but mark it as a reinterpret_cast so that
7784    constexpr evaluation knows to reject it.  */
7785 
7786 static tree
build_nop_reinterpret(tree type,tree expr)7787 build_nop_reinterpret (tree type, tree expr)
7788 {
7789   tree ret = build_nop (type, expr);
7790   if (ret != expr)
7791     REINTERPRET_CAST_P (ret) = true;
7792   return ret;
7793 }
7794 
7795 /* Return a representation for a reinterpret_cast from EXPR to TYPE.
7796    If C_CAST_P is true, this reinterpret cast is being done as part of
7797    a C-style cast.  If VALID_P is non-NULL, *VALID_P is set to
7798    indicate whether or not reinterpret_cast was valid.  */
7799 
7800 static tree
build_reinterpret_cast_1(location_t loc,tree type,tree expr,bool c_cast_p,bool * valid_p,tsubst_flags_t complain)7801 build_reinterpret_cast_1 (location_t loc, tree type, tree expr,
7802 			  bool c_cast_p, bool *valid_p,
7803 			  tsubst_flags_t complain)
7804 {
7805   tree intype;
7806 
7807   /* Assume the cast is invalid.  */
7808   if (valid_p)
7809     *valid_p = true;
7810 
7811   if (type == error_mark_node || error_operand_p (expr))
7812     return error_mark_node;
7813 
7814   intype = TREE_TYPE (expr);
7815 
7816   /* Save casted types in the function's used types hash table.  */
7817   used_types_insert (type);
7818 
7819   /* A prvalue of non-class type is cv-unqualified.  */
7820   if (!CLASS_TYPE_P (type))
7821     type = cv_unqualified (type);
7822 
7823   /* [expr.reinterpret.cast]
7824      A glvalue expression of type T1 can be cast to the type
7825      "reference to T2" if an expression of type "pointer to T1" can be
7826      explicitly converted to the type "pointer to T2" using a
7827      reinterpret_cast.  */
7828   if (TYPE_REF_P (type))
7829     {
7830       if (TYPE_REF_IS_RVALUE (type) && !VOID_TYPE_P (intype))
7831 	{
7832 	  if (!obvalue_p (expr))
7833 	    /* Perform the temporary materialization conversion.  */
7834 	    expr = get_target_expr_sfinae (expr, complain);
7835 	}
7836       else if (!lvalue_p (expr))
7837 	{
7838           if (complain & tf_error)
7839             error_at (loc, "invalid cast of an rvalue expression of type "
7840 		      "%qT to type %qT",
7841 		      intype, type);
7842 	  return error_mark_node;
7843 	}
7844 
7845       /* Warn about a reinterpret_cast from "A*" to "B&" if "A" and
7846 	 "B" are related class types; the reinterpret_cast does not
7847 	 adjust the pointer.  */
7848       if (TYPE_PTR_P (intype)
7849           && (complain & tf_warning)
7850 	  && (comptypes (TREE_TYPE (intype), TREE_TYPE (type),
7851 			 COMPARE_BASE | COMPARE_DERIVED)))
7852 	warning_at (loc, 0, "casting %qT to %qT does not dereference pointer",
7853 		    intype, type);
7854 
7855       expr = cp_build_addr_expr (expr, complain);
7856 
7857       if (warn_strict_aliasing > 2)
7858 	cp_strict_aliasing_warning (EXPR_LOCATION (expr), type, expr);
7859 
7860       if (expr != error_mark_node)
7861 	expr = build_reinterpret_cast_1
7862 	  (loc, build_pointer_type (TREE_TYPE (type)), expr, c_cast_p,
7863 	   valid_p, complain);
7864       if (expr != error_mark_node)
7865 	/* cp_build_indirect_ref isn't right for rvalue refs.  */
7866 	expr = convert_from_reference (fold_convert (type, expr));
7867       return expr;
7868     }
7869 
7870   /* As a G++ extension, we consider conversions from member
7871      functions, and pointers to member functions to
7872      pointer-to-function and pointer-to-void types.  If
7873      -Wno-pmf-conversions has not been specified,
7874      convert_member_func_to_ptr will issue an error message.  */
7875   if ((TYPE_PTRMEMFUNC_P (intype)
7876        || TREE_CODE (intype) == METHOD_TYPE)
7877       && TYPE_PTR_P (type)
7878       && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
7879 	  || VOID_TYPE_P (TREE_TYPE (type))))
7880     return convert_member_func_to_ptr (type, expr, complain);
7881 
7882   /* If the cast is not to a reference type, the lvalue-to-rvalue,
7883      array-to-pointer, and function-to-pointer conversions are
7884      performed.  */
7885   expr = decay_conversion (expr, complain);
7886 
7887   /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7888      Strip such NOP_EXPRs if VALUE is being used in non-lvalue context.  */
7889   if (TREE_CODE (expr) == NOP_EXPR
7890       && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
7891     expr = TREE_OPERAND (expr, 0);
7892 
7893   if (error_operand_p (expr))
7894     return error_mark_node;
7895 
7896   intype = TREE_TYPE (expr);
7897 
7898   /* [expr.reinterpret.cast]
7899      A pointer can be converted to any integral type large enough to
7900      hold it. ... A value of type std::nullptr_t can be converted to
7901      an integral type; the conversion has the same meaning and
7902      validity as a conversion of (void*)0 to the integral type.  */
7903   if (CP_INTEGRAL_TYPE_P (type)
7904       && (TYPE_PTR_P (intype) || NULLPTR_TYPE_P (intype)))
7905     {
7906       if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
7907         {
7908           if (complain & tf_error)
7909             permerror (loc, "cast from %qH to %qI loses precision",
7910                        intype, type);
7911           else
7912             return error_mark_node;
7913         }
7914       if (NULLPTR_TYPE_P (intype))
7915         return build_int_cst (type, 0);
7916     }
7917   /* [expr.reinterpret.cast]
7918      A value of integral or enumeration type can be explicitly
7919      converted to a pointer.  */
7920   else if (TYPE_PTR_P (type) && INTEGRAL_OR_ENUMERATION_TYPE_P (intype))
7921     /* OK */
7922     ;
7923   else if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
7924 	    || TYPE_PTR_OR_PTRMEM_P (type))
7925 	   && same_type_p (type, intype))
7926     /* DR 799 */
7927     return rvalue (expr);
7928   else if (TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
7929     {
7930       if ((complain & tf_warning)
7931 	  && !cxx_safe_function_type_cast_p (TREE_TYPE (type),
7932 					     TREE_TYPE (intype)))
7933 	warning_at (loc, OPT_Wcast_function_type,
7934 		    "cast between incompatible function types"
7935 		    " from %qH to %qI", intype, type);
7936       return build_nop_reinterpret (type, expr);
7937     }
7938   else if (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype))
7939     {
7940       if ((complain & tf_warning)
7941 	  && !cxx_safe_function_type_cast_p
7942 		(TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (type)),
7943 		 TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (intype))))
7944 	warning_at (loc, OPT_Wcast_function_type,
7945 		    "cast between incompatible pointer to member types"
7946 		    " from %qH to %qI", intype, type);
7947       return build_nop_reinterpret (type, expr);
7948     }
7949   else if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
7950 	   || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
7951     {
7952       if (!c_cast_p
7953 	  && check_for_casting_away_constness (loc, intype, type,
7954 					       REINTERPRET_CAST_EXPR,
7955 					       complain))
7956 	return error_mark_node;
7957       /* Warn about possible alignment problems.  */
7958       if ((STRICT_ALIGNMENT || warn_cast_align == 2)
7959 	  && (complain & tf_warning)
7960 	  && !VOID_TYPE_P (type)
7961 	  && TREE_CODE (TREE_TYPE (intype)) != FUNCTION_TYPE
7962 	  && COMPLETE_TYPE_P (TREE_TYPE (type))
7963 	  && COMPLETE_TYPE_P (TREE_TYPE (intype))
7964 	  && min_align_of_type (TREE_TYPE (type))
7965 	     > min_align_of_type (TREE_TYPE (intype)))
7966 	warning_at (loc, OPT_Wcast_align, "cast from %qH to %qI "
7967 		    "increases required alignment of target type",
7968 		    intype, type);
7969 
7970       if (warn_strict_aliasing <= 2)
7971 	/* strict_aliasing_warning STRIP_NOPs its expr.  */
7972 	cp_strict_aliasing_warning (EXPR_LOCATION (expr), type, expr);
7973 
7974       return build_nop_reinterpret (type, expr);
7975     }
7976   else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
7977 	   || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
7978     {
7979       if (complain & tf_warning)
7980 	/* C++11 5.2.10 p8 says that "Converting a function pointer to an
7981 	   object pointer type or vice versa is conditionally-supported."  */
7982 	warning_at (loc, OPT_Wconditionally_supported,
7983 		    "casting between pointer-to-function and "
7984 		    "pointer-to-object is conditionally-supported");
7985       return build_nop_reinterpret (type, expr);
7986     }
7987   else if (gnu_vector_type_p (type))
7988     return convert_to_vector (type, rvalue (expr));
7989   else if (gnu_vector_type_p (intype)
7990 	   && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
7991     return convert_to_integer_nofold (type, expr);
7992   else
7993     {
7994       if (valid_p)
7995 	*valid_p = false;
7996       if (complain & tf_error)
7997         error_at (loc, "invalid cast from type %qT to type %qT",
7998 		  intype, type);
7999       return error_mark_node;
8000     }
8001 
8002   expr = cp_convert (type, expr, complain);
8003   if (TREE_CODE (expr) == NOP_EXPR)
8004     /* Mark any nop_expr that created as a reintepret_cast.  */
8005     REINTERPRET_CAST_P (expr) = true;
8006   return expr;
8007 }
8008 
8009 tree
build_reinterpret_cast(location_t loc,tree type,tree expr,tsubst_flags_t complain)8010 build_reinterpret_cast (location_t loc, tree type, tree expr,
8011 			tsubst_flags_t complain)
8012 {
8013   tree r;
8014 
8015   if (type == error_mark_node || expr == error_mark_node)
8016     return error_mark_node;
8017 
8018   if (processing_template_decl)
8019     {
8020       tree t = build_min (REINTERPRET_CAST_EXPR, type, expr);
8021 
8022       if (!TREE_SIDE_EFFECTS (t)
8023 	  && type_dependent_expression_p (expr))
8024 	/* There might turn out to be side effects inside expr.  */
8025 	TREE_SIDE_EFFECTS (t) = 1;
8026       r = convert_from_reference (t);
8027       protected_set_expr_location (r, loc);
8028       return r;
8029     }
8030 
8031   r = build_reinterpret_cast_1 (loc, type, expr, /*c_cast_p=*/false,
8032 				/*valid_p=*/NULL, complain);
8033   if (r != error_mark_node)
8034     {
8035       maybe_warn_about_useless_cast (loc, type, expr, complain);
8036       maybe_warn_about_cast_ignoring_quals (loc, type, complain);
8037     }
8038   protected_set_expr_location (r, loc);
8039   return r;
8040 }
8041 
8042 /* Perform a const_cast from EXPR to TYPE.  If the cast is valid,
8043    return an appropriate expression.  Otherwise, return
8044    error_mark_node.  If the cast is not valid, and COMPLAIN is true,
8045    then a diagnostic will be issued.  If VALID_P is non-NULL, we are
8046    performing a C-style cast, its value upon return will indicate
8047    whether or not the conversion succeeded.  */
8048 
8049 static tree
build_const_cast_1(location_t loc,tree dst_type,tree expr,tsubst_flags_t complain,bool * valid_p)8050 build_const_cast_1 (location_t loc, tree dst_type, tree expr,
8051 		    tsubst_flags_t complain, bool *valid_p)
8052 {
8053   tree src_type;
8054   tree reference_type;
8055 
8056   /* Callers are responsible for handling error_mark_node as a
8057      destination type.  */
8058   gcc_assert (dst_type != error_mark_node);
8059   /* In a template, callers should be building syntactic
8060      representations of casts, not using this machinery.  */
8061   gcc_assert (!processing_template_decl);
8062 
8063   /* Assume the conversion is invalid.  */
8064   if (valid_p)
8065     *valid_p = false;
8066 
8067   if (!INDIRECT_TYPE_P (dst_type) && !TYPE_PTRDATAMEM_P (dst_type))
8068     {
8069       if (complain & tf_error)
8070 	error_at (loc, "invalid use of %<const_cast%> with type %qT, "
8071 		  "which is not a pointer, reference, "
8072 		  "nor a pointer-to-data-member type", dst_type);
8073       return error_mark_node;
8074     }
8075 
8076   if (TREE_CODE (TREE_TYPE (dst_type)) == FUNCTION_TYPE)
8077     {
8078       if (complain & tf_error)
8079 	error_at (loc, "invalid use of %<const_cast%> with type %qT, "
8080 		  "which is a pointer or reference to a function type",
8081 		  dst_type);
8082        return error_mark_node;
8083     }
8084 
8085   /* A prvalue of non-class type is cv-unqualified.  */
8086   dst_type = cv_unqualified (dst_type);
8087 
8088   /* Save casted types in the function's used types hash table.  */
8089   used_types_insert (dst_type);
8090 
8091   src_type = TREE_TYPE (expr);
8092   /* Expressions do not really have reference types.  */
8093   if (TYPE_REF_P (src_type))
8094     src_type = TREE_TYPE (src_type);
8095 
8096   /* [expr.const.cast]
8097 
8098      For two object types T1 and T2, if a pointer to T1 can be explicitly
8099      converted to the type "pointer to T2" using a const_cast, then the
8100      following conversions can also be made:
8101 
8102      -- an lvalue of type T1 can be explicitly converted to an lvalue of
8103      type T2 using the cast const_cast<T2&>;
8104 
8105      -- a glvalue of type T1 can be explicitly converted to an xvalue of
8106      type T2 using the cast const_cast<T2&&>; and
8107 
8108      -- if T1 is a class type, a prvalue of type T1 can be explicitly
8109      converted to an xvalue of type T2 using the cast const_cast<T2&&>.  */
8110 
8111   if (TYPE_REF_P (dst_type))
8112     {
8113       reference_type = dst_type;
8114       if (!TYPE_REF_IS_RVALUE (dst_type)
8115 	  ? lvalue_p (expr)
8116 	  : obvalue_p (expr))
8117 	/* OK.  */;
8118       else
8119 	{
8120 	  if (complain & tf_error)
8121 	    error_at (loc, "invalid %<const_cast%> of an rvalue of type %qT "
8122 		      "to type %qT",
8123 		      src_type, dst_type);
8124  	  return error_mark_node;
8125 	}
8126       dst_type = build_pointer_type (TREE_TYPE (dst_type));
8127       src_type = build_pointer_type (src_type);
8128     }
8129   else
8130     {
8131       reference_type = NULL_TREE;
8132       /* If the destination type is not a reference type, the
8133 	 lvalue-to-rvalue, array-to-pointer, and function-to-pointer
8134 	 conversions are performed.  */
8135       src_type = type_decays_to (src_type);
8136       if (src_type == error_mark_node)
8137 	return error_mark_node;
8138     }
8139 
8140   if (TYPE_PTR_P (src_type) || TYPE_PTRDATAMEM_P (src_type))
8141     {
8142       if (comp_ptr_ttypes_const (dst_type, src_type, bounds_none))
8143 	{
8144 	  if (valid_p)
8145 	    {
8146 	      *valid_p = true;
8147 	      /* This cast is actually a C-style cast.  Issue a warning if
8148 		 the user is making a potentially unsafe cast.  */
8149 	      check_for_casting_away_constness (loc, src_type, dst_type,
8150 						CAST_EXPR, complain);
8151 	      /* ??? comp_ptr_ttypes_const ignores TYPE_ALIGN.  */
8152 	      if ((STRICT_ALIGNMENT || warn_cast_align == 2)
8153 		  && (complain & tf_warning)
8154 		  && min_align_of_type (TREE_TYPE (dst_type))
8155 		     > min_align_of_type (TREE_TYPE (src_type)))
8156 		warning_at (loc, OPT_Wcast_align, "cast from %qH to %qI "
8157 			    "increases required alignment of target type",
8158 			    src_type, dst_type);
8159 	    }
8160 	  if (reference_type)
8161 	    {
8162 	      expr = cp_build_addr_expr (expr, complain);
8163 	      if (expr == error_mark_node)
8164 		return error_mark_node;
8165 	      expr = build_nop (reference_type, expr);
8166 	      return convert_from_reference (expr);
8167 	    }
8168 	  else
8169 	    {
8170 	      expr = decay_conversion (expr, complain);
8171 	      if (expr == error_mark_node)
8172 		return error_mark_node;
8173 
8174 	      /* build_c_cast puts on a NOP_EXPR to make the result not an
8175 		 lvalue.  Strip such NOP_EXPRs if VALUE is being used in
8176 		 non-lvalue context.  */
8177 	      if (TREE_CODE (expr) == NOP_EXPR
8178 		  && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
8179 		expr = TREE_OPERAND (expr, 0);
8180 	      return build_nop (dst_type, expr);
8181 	    }
8182 	}
8183       else if (valid_p
8184 	       && !at_least_as_qualified_p (TREE_TYPE (dst_type),
8185 					    TREE_TYPE (src_type)))
8186 	check_for_casting_away_constness (loc, src_type, dst_type,
8187 					  CAST_EXPR, complain);
8188     }
8189 
8190   if (complain & tf_error)
8191     error_at (loc, "invalid %<const_cast%> from type %qT to type %qT",
8192 	      src_type, dst_type);
8193   return error_mark_node;
8194 }
8195 
8196 tree
build_const_cast(location_t loc,tree type,tree expr,tsubst_flags_t complain)8197 build_const_cast (location_t loc, tree type, tree expr,
8198 		  tsubst_flags_t complain)
8199 {
8200   tree r;
8201 
8202   if (type == error_mark_node || error_operand_p (expr))
8203     return error_mark_node;
8204 
8205   if (processing_template_decl)
8206     {
8207       tree t = build_min (CONST_CAST_EXPR, type, expr);
8208 
8209       if (!TREE_SIDE_EFFECTS (t)
8210 	  && type_dependent_expression_p (expr))
8211 	/* There might turn out to be side effects inside expr.  */
8212 	TREE_SIDE_EFFECTS (t) = 1;
8213       r = convert_from_reference (t);
8214       protected_set_expr_location (r, loc);
8215       return r;
8216     }
8217 
8218   r = build_const_cast_1 (loc, type, expr, complain, /*valid_p=*/NULL);
8219   if (r != error_mark_node)
8220     {
8221       maybe_warn_about_useless_cast (loc, type, expr, complain);
8222       maybe_warn_about_cast_ignoring_quals (loc, type, complain);
8223     }
8224   protected_set_expr_location (r, loc);
8225   return r;
8226 }
8227 
8228 /* Like cp_build_c_cast, but for the c-common bits.  */
8229 
8230 tree
build_c_cast(location_t loc,tree type,tree expr)8231 build_c_cast (location_t loc, tree type, tree expr)
8232 {
8233   return cp_build_c_cast (loc, type, expr, tf_warning_or_error);
8234 }
8235 
8236 /* Like the "build_c_cast" used for c-common, but using cp_expr to
8237    preserve location information even for tree nodes that don't
8238    support it.  */
8239 
8240 cp_expr
build_c_cast(location_t loc,tree type,cp_expr expr)8241 build_c_cast (location_t loc, tree type, cp_expr expr)
8242 {
8243   cp_expr result = cp_build_c_cast (loc, type, expr, tf_warning_or_error);
8244   result.set_location (loc);
8245   return result;
8246 }
8247 
8248 /* Build an expression representing an explicit C-style cast to type
8249    TYPE of expression EXPR.  */
8250 
8251 tree
cp_build_c_cast(location_t loc,tree type,tree expr,tsubst_flags_t complain)8252 cp_build_c_cast (location_t loc, tree type, tree expr,
8253 		 tsubst_flags_t complain)
8254 {
8255   tree value = expr;
8256   tree result;
8257   bool valid_p;
8258 
8259   if (type == error_mark_node || error_operand_p (expr))
8260     return error_mark_node;
8261 
8262   if (processing_template_decl)
8263     {
8264       tree t = build_min (CAST_EXPR, type,
8265 			  tree_cons (NULL_TREE, value, NULL_TREE));
8266       /* We don't know if it will or will not have side effects.  */
8267       TREE_SIDE_EFFECTS (t) = 1;
8268       return convert_from_reference (t);
8269     }
8270 
8271   /* Casts to a (pointer to a) specific ObjC class (or 'id' or
8272      'Class') should always be retained, because this information aids
8273      in method lookup.  */
8274   if (objc_is_object_ptr (type)
8275       && objc_is_object_ptr (TREE_TYPE (expr)))
8276     return build_nop (type, expr);
8277 
8278   /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
8279      Strip such NOP_EXPRs if VALUE is being used in non-lvalue context.  */
8280   if (!TYPE_REF_P (type)
8281       && TREE_CODE (value) == NOP_EXPR
8282       && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
8283     value = TREE_OPERAND (value, 0);
8284 
8285   if (TREE_CODE (type) == ARRAY_TYPE)
8286     {
8287       /* Allow casting from T1* to T2[] because Cfront allows it.
8288 	 NIHCL uses it. It is not valid ISO C++ however.  */
8289       if (TYPE_PTR_P (TREE_TYPE (expr)))
8290 	{
8291           if (complain & tf_error)
8292             permerror (loc, "ISO C++ forbids casting to an array type %qT",
8293 		       type);
8294           else
8295             return error_mark_node;
8296 	  type = build_pointer_type (TREE_TYPE (type));
8297 	}
8298       else
8299 	{
8300           if (complain & tf_error)
8301             error_at (loc, "ISO C++ forbids casting to an array type %qT",
8302 		      type);
8303 	  return error_mark_node;
8304 	}
8305     }
8306 
8307   if (FUNC_OR_METHOD_TYPE_P (type))
8308     {
8309       if (complain & tf_error)
8310         error_at (loc, "invalid cast to function type %qT", type);
8311       return error_mark_node;
8312     }
8313 
8314   if (TYPE_PTR_P (type)
8315       && TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
8316       /* Casting to an integer of smaller size is an error detected elsewhere.  */
8317       && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (value))
8318       /* Don't warn about converting any constant.  */
8319       && !TREE_CONSTANT (value))
8320     warning_at (loc, OPT_Wint_to_pointer_cast,
8321 		"cast to pointer from integer of different size");
8322 
8323   /* A C-style cast can be a const_cast.  */
8324   result = build_const_cast_1 (loc, type, value, complain & tf_warning,
8325 			       &valid_p);
8326   if (valid_p)
8327     {
8328       if (result != error_mark_node)
8329 	{
8330 	  maybe_warn_about_useless_cast (loc, type, value, complain);
8331 	  maybe_warn_about_cast_ignoring_quals (loc, type, complain);
8332 	}
8333       return result;
8334     }
8335 
8336   /* Or a static cast.  */
8337   result = build_static_cast_1 (loc, type, value, /*c_cast_p=*/true,
8338 				&valid_p, complain);
8339   /* Or a reinterpret_cast.  */
8340   if (!valid_p)
8341     result = build_reinterpret_cast_1 (loc, type, value, /*c_cast_p=*/true,
8342 				       &valid_p, complain);
8343   /* The static_cast or reinterpret_cast may be followed by a
8344      const_cast.  */
8345   if (valid_p
8346       /* A valid cast may result in errors if, for example, a
8347 	 conversion to an ambiguous base class is required.  */
8348       && !error_operand_p (result))
8349     {
8350       tree result_type;
8351 
8352       maybe_warn_about_useless_cast (loc, type, value, complain);
8353       maybe_warn_about_cast_ignoring_quals (loc, type, complain);
8354 
8355       /* Non-class rvalues always have cv-unqualified type.  */
8356       if (!CLASS_TYPE_P (type))
8357 	type = TYPE_MAIN_VARIANT (type);
8358       result_type = TREE_TYPE (result);
8359       if (!CLASS_TYPE_P (result_type) && !TYPE_REF_P (type))
8360 	result_type = TYPE_MAIN_VARIANT (result_type);
8361       /* If the type of RESULT does not match TYPE, perform a
8362 	 const_cast to make it match.  If the static_cast or
8363 	 reinterpret_cast succeeded, we will differ by at most
8364 	 cv-qualification, so the follow-on const_cast is guaranteed
8365 	 to succeed.  */
8366       if (!same_type_p (non_reference (type), non_reference (result_type)))
8367 	{
8368 	  result = build_const_cast_1 (loc, type, result, false, &valid_p);
8369 	  gcc_assert (valid_p);
8370 	}
8371       return result;
8372     }
8373 
8374   return error_mark_node;
8375 }
8376 
8377 /* For use from the C common bits.  */
8378 tree
build_modify_expr(location_t location,tree lhs,tree,enum tree_code modifycode,location_t,tree rhs,tree)8379 build_modify_expr (location_t location,
8380 		   tree lhs, tree /*lhs_origtype*/,
8381 		   enum tree_code modifycode,
8382 		   location_t /*rhs_location*/, tree rhs,
8383 		   tree /*rhs_origtype*/)
8384 {
8385   return cp_build_modify_expr (location, lhs, modifycode, rhs,
8386 			       tf_warning_or_error);
8387 }
8388 
8389 /* Build an assignment expression of lvalue LHS from value RHS.
8390    MODIFYCODE is the code for a binary operator that we use
8391    to combine the old value of LHS with RHS to get the new value.
8392    Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
8393 
8394    C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed.  */
8395 
8396 tree
cp_build_modify_expr(location_t loc,tree lhs,enum tree_code modifycode,tree rhs,tsubst_flags_t complain)8397 cp_build_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
8398 		      tree rhs, tsubst_flags_t complain)
8399 {
8400   lhs = mark_lvalue_use_nonread (lhs);
8401 
8402   tree result = NULL_TREE;
8403   tree newrhs = rhs;
8404   tree lhstype = TREE_TYPE (lhs);
8405   tree olhs = lhs;
8406   tree olhstype = lhstype;
8407   bool plain_assign = (modifycode == NOP_EXPR);
8408   bool compound_side_effects_p = false;
8409   tree preeval = NULL_TREE;
8410 
8411   /* Avoid duplicate error messages from operands that had errors.  */
8412   if (error_operand_p (lhs) || error_operand_p (rhs))
8413     return error_mark_node;
8414 
8415   while (TREE_CODE (lhs) == COMPOUND_EXPR)
8416     {
8417       if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
8418 	compound_side_effects_p = true;
8419       lhs = TREE_OPERAND (lhs, 1);
8420     }
8421 
8422   /* Handle control structure constructs used as "lvalues".  Note that we
8423      leave COMPOUND_EXPR on the LHS because it is sequenced after the RHS.  */
8424   switch (TREE_CODE (lhs))
8425     {
8426       /* Handle --foo = 5; as these are valid constructs in C++.  */
8427     case PREDECREMENT_EXPR:
8428     case PREINCREMENT_EXPR:
8429       if (compound_side_effects_p)
8430 	newrhs = rhs = stabilize_expr (rhs, &preeval);
8431       lhs = genericize_compound_lvalue (lhs);
8432     maybe_add_compound:
8433       /* If we had (bar, --foo) = 5; or (bar, (baz, --foo)) = 5;
8434 	 and looked through the COMPOUND_EXPRs, readd them now around
8435 	 the resulting lhs.  */
8436       if (TREE_CODE (olhs) == COMPOUND_EXPR)
8437 	{
8438 	  lhs = build2 (COMPOUND_EXPR, lhstype, TREE_OPERAND (olhs, 0), lhs);
8439 	  tree *ptr = &TREE_OPERAND (lhs, 1);
8440 	  for (olhs = TREE_OPERAND (olhs, 1);
8441 	       TREE_CODE (olhs) == COMPOUND_EXPR;
8442 	       olhs = TREE_OPERAND (olhs, 1))
8443 	    {
8444 	      *ptr = build2 (COMPOUND_EXPR, lhstype,
8445 			     TREE_OPERAND (olhs, 0), *ptr);
8446 	      ptr = &TREE_OPERAND (*ptr, 1);
8447 	    }
8448 	}
8449       break;
8450 
8451     case MODIFY_EXPR:
8452       if (compound_side_effects_p)
8453 	newrhs = rhs = stabilize_expr (rhs, &preeval);
8454       lhs = genericize_compound_lvalue (lhs);
8455       goto maybe_add_compound;
8456 
8457     case MIN_EXPR:
8458     case MAX_EXPR:
8459       /* MIN_EXPR and MAX_EXPR are currently only permitted as lvalues,
8460 	 when neither operand has side-effects.  */
8461       if (!lvalue_or_else (lhs, lv_assign, complain))
8462 	return error_mark_node;
8463 
8464       gcc_assert (!TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0))
8465 		  && !TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 1)));
8466 
8467       lhs = build3 (COND_EXPR, TREE_TYPE (lhs),
8468 		    build2 (TREE_CODE (lhs) == MIN_EXPR ? LE_EXPR : GE_EXPR,
8469 			    boolean_type_node,
8470 			    TREE_OPERAND (lhs, 0),
8471 			    TREE_OPERAND (lhs, 1)),
8472 		    TREE_OPERAND (lhs, 0),
8473 		    TREE_OPERAND (lhs, 1));
8474       gcc_fallthrough ();
8475 
8476       /* Handle (a ? b : c) used as an "lvalue".  */
8477     case COND_EXPR:
8478       {
8479 	/* Produce (a ? (b = rhs) : (c = rhs))
8480 	   except that the RHS goes through a save-expr
8481 	   so the code to compute it is only emitted once.  */
8482 	if (VOID_TYPE_P (TREE_TYPE (rhs)))
8483 	  {
8484 	    if (complain & tf_error)
8485 	      error_at (cp_expr_loc_or_loc (rhs, loc),
8486 			"void value not ignored as it ought to be");
8487 	    return error_mark_node;
8488 	  }
8489 
8490 	rhs = stabilize_expr (rhs, &preeval);
8491 
8492 	/* Check this here to avoid odd errors when trying to convert
8493 	   a throw to the type of the COND_EXPR.  */
8494 	if (!lvalue_or_else (lhs, lv_assign, complain))
8495 	  return error_mark_node;
8496 
8497 	tree op1 = TREE_OPERAND (lhs, 1);
8498 	if (TREE_CODE (op1) != THROW_EXPR)
8499 	  op1 = cp_build_modify_expr (loc, op1, modifycode, rhs, complain);
8500 	/* When sanitizing undefined behavior, even when rhs doesn't need
8501 	   stabilization at this point, the sanitization might add extra
8502 	   SAVE_EXPRs in there and so make sure there is no tree sharing
8503 	   in the rhs, otherwise those SAVE_EXPRs will have initialization
8504 	   only in one of the two branches.  */
8505 	if (sanitize_flags_p (SANITIZE_UNDEFINED
8506 			      | SANITIZE_UNDEFINED_NONDEFAULT))
8507 	  rhs = unshare_expr (rhs);
8508 	tree op2 = TREE_OPERAND (lhs, 2);
8509 	if (TREE_CODE (op2) != THROW_EXPR)
8510 	  op2 = cp_build_modify_expr (loc, op2, modifycode, rhs, complain);
8511 	tree cond = build_conditional_expr (input_location,
8512 					    TREE_OPERAND (lhs, 0), op1, op2,
8513 					    complain);
8514 
8515 	if (cond == error_mark_node)
8516 	  return cond;
8517 	/* If we had (e, (a ? b : c)) = d; or (e, (f, (a ? b : c))) = d;
8518 	   and looked through the COMPOUND_EXPRs, readd them now around
8519 	   the resulting cond before adding the preevaluated rhs.  */
8520 	if (TREE_CODE (olhs) == COMPOUND_EXPR)
8521 	  {
8522 	    cond = build2 (COMPOUND_EXPR, TREE_TYPE (cond),
8523 			   TREE_OPERAND (olhs, 0), cond);
8524 	    tree *ptr = &TREE_OPERAND (cond, 1);
8525 	    for (olhs = TREE_OPERAND (olhs, 1);
8526 		 TREE_CODE (olhs) == COMPOUND_EXPR;
8527 		 olhs = TREE_OPERAND (olhs, 1))
8528 	      {
8529 		*ptr = build2 (COMPOUND_EXPR, TREE_TYPE (cond),
8530 			       TREE_OPERAND (olhs, 0), *ptr);
8531 		ptr = &TREE_OPERAND (*ptr, 1);
8532 	      }
8533 	  }
8534 	/* Make sure the code to compute the rhs comes out
8535 	   before the split.  */
8536 	result = cond;
8537 	goto ret;
8538       }
8539 
8540     default:
8541       lhs = olhs;
8542       break;
8543     }
8544 
8545   if (modifycode == INIT_EXPR)
8546     {
8547       if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
8548 	/* Do the default thing.  */;
8549       else if (TREE_CODE (rhs) == CONSTRUCTOR)
8550 	{
8551 	  /* Compound literal.  */
8552 	  if (! same_type_p (TREE_TYPE (rhs), lhstype))
8553 	    /* Call convert to generate an error; see PR 11063.  */
8554 	    rhs = convert (lhstype, rhs);
8555 	  result = build2 (INIT_EXPR, lhstype, lhs, rhs);
8556 	  TREE_SIDE_EFFECTS (result) = 1;
8557 	  goto ret;
8558 	}
8559       else if (! MAYBE_CLASS_TYPE_P (lhstype))
8560 	/* Do the default thing.  */;
8561       else
8562 	{
8563 	  releasing_vec rhs_vec = make_tree_vector_single (rhs);
8564 	  result = build_special_member_call (lhs, complete_ctor_identifier,
8565 					      &rhs_vec, lhstype, LOOKUP_NORMAL,
8566                                               complain);
8567 	  if (result == NULL_TREE)
8568 	    return error_mark_node;
8569 	  goto ret;
8570 	}
8571     }
8572   else
8573     {
8574       lhs = require_complete_type_sfinae (lhs, complain);
8575       if (lhs == error_mark_node)
8576 	return error_mark_node;
8577 
8578       if (modifycode == NOP_EXPR)
8579 	{
8580 	  if (c_dialect_objc ())
8581 	    {
8582 	      result = objc_maybe_build_modify_expr (lhs, rhs);
8583 	      if (result)
8584 		goto ret;
8585 	    }
8586 
8587 	  /* `operator=' is not an inheritable operator.  */
8588 	  if (! MAYBE_CLASS_TYPE_P (lhstype))
8589 	    /* Do the default thing.  */;
8590 	  else
8591 	    {
8592 	      result = build_new_op (input_location, MODIFY_EXPR,
8593 				     LOOKUP_NORMAL, lhs, rhs,
8594 				     make_node (NOP_EXPR), /*overload=*/NULL,
8595 				     complain);
8596 	      if (result == NULL_TREE)
8597 		return error_mark_node;
8598 	      goto ret;
8599 	    }
8600 	  lhstype = olhstype;
8601 	}
8602       else
8603 	{
8604 	  tree init = NULL_TREE;
8605 
8606 	  /* A binary op has been requested.  Combine the old LHS
8607 	     value with the RHS producing the value we should actually
8608 	     store into the LHS.  */
8609 	  gcc_assert (!((TYPE_REF_P (lhstype)
8610 			 && MAYBE_CLASS_TYPE_P (TREE_TYPE (lhstype)))
8611 			|| MAYBE_CLASS_TYPE_P (lhstype)));
8612 
8613 	  /* An expression of the form E1 op= E2.  [expr.ass] says:
8614 	     "Such expressions are deprecated if E1 has volatile-qualified
8615 	     type and op is not one of the bitwise operators |, &, ^."
8616 	     We warn here rather than in cp_genericize_r because
8617 	     for compound assignments we are supposed to warn even if the
8618 	     assignment is a discarded-value expression.  */
8619 	  if (modifycode != BIT_AND_EXPR
8620 	      && modifycode != BIT_IOR_EXPR
8621 	      && modifycode != BIT_XOR_EXPR
8622 	      && (TREE_THIS_VOLATILE (lhs) || CP_TYPE_VOLATILE_P (lhstype)))
8623 	    warning_at (loc, OPT_Wvolatile,
8624 			"compound assignment with %<volatile%>-qualified left "
8625 			"operand is deprecated");
8626 	  /* Preevaluate the RHS to make sure its evaluation is complete
8627 	     before the lvalue-to-rvalue conversion of the LHS:
8628 
8629 	     [expr.ass] With respect to an indeterminately-sequenced
8630 	     function call, the operation of a compound assignment is a
8631 	     single evaluation. [ Note: Therefore, a function call shall
8632 	     not intervene between the lvalue-to-rvalue conversion and the
8633 	     side effect associated with any single compound assignment
8634 	     operator. -- end note ]  */
8635 	  lhs = cp_stabilize_reference (lhs);
8636 	  rhs = decay_conversion (rhs, complain);
8637 	  if (rhs == error_mark_node)
8638 	    return error_mark_node;
8639 	  rhs = stabilize_expr (rhs, &init);
8640 	  newrhs = cp_build_binary_op (loc, modifycode, lhs, rhs, complain);
8641 	  if (newrhs == error_mark_node)
8642 	    {
8643 	      if (complain & tf_error)
8644 		inform (loc, "  in evaluation of %<%Q(%#T, %#T)%>",
8645 			modifycode, TREE_TYPE (lhs), TREE_TYPE (rhs));
8646 	      return error_mark_node;
8647 	    }
8648 
8649 	  if (init)
8650 	    newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), init, newrhs);
8651 
8652 	  /* Now it looks like a plain assignment.  */
8653 	  modifycode = NOP_EXPR;
8654 	  if (c_dialect_objc ())
8655 	    {
8656 	      result = objc_maybe_build_modify_expr (lhs, newrhs);
8657 	      if (result)
8658 		goto ret;
8659 	    }
8660 	}
8661       gcc_assert (!TYPE_REF_P (lhstype));
8662       gcc_assert (!TYPE_REF_P (TREE_TYPE (newrhs)));
8663     }
8664 
8665   /* The left-hand side must be an lvalue.  */
8666   if (!lvalue_or_else (lhs, lv_assign, complain))
8667     return error_mark_node;
8668 
8669   /* Warn about modifying something that is `const'.  Don't warn if
8670      this is initialization.  */
8671   if (modifycode != INIT_EXPR
8672       && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype)
8673 	  /* Functions are not modifiable, even though they are
8674 	     lvalues.  */
8675 	  || FUNC_OR_METHOD_TYPE_P (TREE_TYPE (lhs))
8676 	  /* If it's an aggregate and any field is const, then it is
8677 	     effectively const.  */
8678 	  || (CLASS_TYPE_P (lhstype)
8679 	      && C_TYPE_FIELDS_READONLY (lhstype))))
8680     {
8681       if (complain & tf_error)
8682 	cxx_readonly_error (loc, lhs, lv_assign);
8683       return error_mark_node;
8684     }
8685 
8686   /* If storing into a structure or union member, it may have been given a
8687      lowered bitfield type.  We need to convert to the declared type first,
8688      so retrieve it now.  */
8689 
8690   olhstype = unlowered_expr_type (lhs);
8691 
8692   /* Convert new value to destination type.  */
8693 
8694   if (TREE_CODE (lhstype) == ARRAY_TYPE)
8695     {
8696       int from_array;
8697 
8698       if (BRACE_ENCLOSED_INITIALIZER_P (newrhs))
8699 	{
8700 	  if (modifycode != INIT_EXPR)
8701 	    {
8702 	      if (complain & tf_error)
8703 		error_at (loc,
8704 			  "assigning to an array from an initializer list");
8705 	      return error_mark_node;
8706 	    }
8707 	  if (check_array_initializer (lhs, lhstype, newrhs))
8708 	    return error_mark_node;
8709 	  newrhs = digest_init (lhstype, newrhs, complain);
8710 	  if (newrhs == error_mark_node)
8711 	    return error_mark_node;
8712 	}
8713 
8714       /* C++11 8.5/17: "If the destination type is an array of characters,
8715 	 an array of char16_t, an array of char32_t, or an array of wchar_t,
8716 	 and the initializer is a string literal...".  */
8717       else if ((TREE_CODE (tree_strip_any_location_wrapper (newrhs))
8718 		== STRING_CST)
8719 	       && char_type_p (TREE_TYPE (TYPE_MAIN_VARIANT (lhstype)))
8720 	       && modifycode == INIT_EXPR)
8721 	{
8722 	  newrhs = digest_init (lhstype, newrhs, complain);
8723 	  if (newrhs == error_mark_node)
8724 	    return error_mark_node;
8725 	}
8726 
8727       else if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype),
8728 				     TYPE_MAIN_VARIANT (TREE_TYPE (newrhs))))
8729 	{
8730 	  if (complain & tf_error)
8731 	    error_at (loc, "incompatible types in assignment of %qT to %qT",
8732 		      TREE_TYPE (rhs), lhstype);
8733 	  return error_mark_node;
8734 	}
8735 
8736       /* Allow array assignment in compiler-generated code.  */
8737       else if (!current_function_decl
8738 	       || !DECL_DEFAULTED_FN (current_function_decl))
8739 	{
8740           /* This routine is used for both initialization and assignment.
8741              Make sure the diagnostic message differentiates the context.  */
8742 	  if (complain & tf_error)
8743 	    {
8744 	      if (modifycode == INIT_EXPR)
8745 		error_at (loc, "array used as initializer");
8746 	      else
8747 		error_at (loc, "invalid array assignment");
8748 	    }
8749 	  return error_mark_node;
8750 	}
8751 
8752       from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
8753 		   ? 1 + (modifycode != INIT_EXPR): 0;
8754       result = build_vec_init (lhs, NULL_TREE, newrhs,
8755 			       /*explicit_value_init_p=*/false,
8756 			       from_array, complain);
8757       goto ret;
8758     }
8759 
8760   if (modifycode == INIT_EXPR)
8761     /* Calls with INIT_EXPR are all direct-initialization, so don't set
8762        LOOKUP_ONLYCONVERTING.  */
8763     newrhs = convert_for_initialization (lhs, olhstype, newrhs, LOOKUP_NORMAL,
8764 					 ICR_INIT, NULL_TREE, 0,
8765                                          complain);
8766   else
8767     newrhs = convert_for_assignment (olhstype, newrhs, ICR_ASSIGN,
8768 				     NULL_TREE, 0, complain, LOOKUP_IMPLICIT);
8769 
8770   if (!same_type_p (lhstype, olhstype))
8771     newrhs = cp_convert_and_check (lhstype, newrhs, complain);
8772 
8773   if (modifycode != INIT_EXPR)
8774     {
8775       if (TREE_CODE (newrhs) == CALL_EXPR
8776 	  && TYPE_NEEDS_CONSTRUCTING (lhstype))
8777 	newrhs = build_cplus_new (lhstype, newrhs, complain);
8778 
8779       /* Can't initialize directly from a TARGET_EXPR, since that would
8780 	 cause the lhs to be constructed twice, and possibly result in
8781 	 accidental self-initialization.  So we force the TARGET_EXPR to be
8782 	 expanded without a target.  */
8783       if (TREE_CODE (newrhs) == TARGET_EXPR)
8784 	newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
8785 			 TREE_OPERAND (newrhs, 0));
8786     }
8787 
8788   if (newrhs == error_mark_node)
8789     return error_mark_node;
8790 
8791   if (c_dialect_objc () && flag_objc_gc)
8792     {
8793       result = objc_generate_write_barrier (lhs, modifycode, newrhs);
8794 
8795       if (result)
8796 	goto ret;
8797     }
8798 
8799   result = build2_loc (loc, modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
8800 		       lhstype, lhs, newrhs);
8801 
8802   TREE_SIDE_EFFECTS (result) = 1;
8803   if (!plain_assign)
8804     TREE_NO_WARNING (result) = 1;
8805 
8806  ret:
8807   if (preeval)
8808     result = build2 (COMPOUND_EXPR, TREE_TYPE (result), preeval, result);
8809   return result;
8810 }
8811 
8812 cp_expr
build_x_modify_expr(location_t loc,tree lhs,enum tree_code modifycode,tree rhs,tsubst_flags_t complain)8813 build_x_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
8814 		     tree rhs, tsubst_flags_t complain)
8815 {
8816   tree orig_lhs = lhs;
8817   tree orig_rhs = rhs;
8818   tree overload = NULL_TREE;
8819   tree op = build_nt (modifycode, NULL_TREE, NULL_TREE);
8820 
8821   if (processing_template_decl)
8822     {
8823       if (modifycode == NOP_EXPR
8824 	  || type_dependent_expression_p (lhs)
8825 	  || type_dependent_expression_p (rhs))
8826         return build_min_nt_loc (loc, MODOP_EXPR, lhs,
8827 				 build_min_nt_loc (loc, modifycode, NULL_TREE,
8828 						   NULL_TREE), rhs);
8829 
8830       lhs = build_non_dependent_expr (lhs);
8831       rhs = build_non_dependent_expr (rhs);
8832     }
8833 
8834   if (modifycode != NOP_EXPR)
8835     {
8836       tree rval = build_new_op (loc, MODIFY_EXPR, LOOKUP_NORMAL,
8837 				lhs, rhs, op, &overload, complain);
8838       if (rval)
8839 	{
8840 	  if (rval == error_mark_node)
8841 	    return rval;
8842 	  TREE_NO_WARNING (rval) = 1;
8843 	  if (processing_template_decl)
8844 	    {
8845 	      if (overload != NULL_TREE)
8846 		return (build_min_non_dep_op_overload
8847 			(MODIFY_EXPR, rval, overload, orig_lhs, orig_rhs));
8848 
8849 	      return (build_min_non_dep
8850 		      (MODOP_EXPR, rval, orig_lhs, op, orig_rhs));
8851 	    }
8852 	  return rval;
8853 	}
8854     }
8855   return cp_build_modify_expr (loc, lhs, modifycode, rhs, complain);
8856 }
8857 
8858 /* Helper function for get_delta_difference which assumes FROM is a base
8859    class of TO.  Returns a delta for the conversion of pointer-to-member
8860    of FROM to pointer-to-member of TO.  If the conversion is invalid and
8861    tf_error is not set in COMPLAIN returns error_mark_node, otherwise
8862    returns zero.  If FROM is not a base class of TO, returns NULL_TREE.
8863    If C_CAST_P is true, this conversion is taking place as part of a
8864    C-style cast.  */
8865 
8866 static tree
get_delta_difference_1(tree from,tree to,bool c_cast_p,tsubst_flags_t complain)8867 get_delta_difference_1 (tree from, tree to, bool c_cast_p,
8868 			tsubst_flags_t complain)
8869 {
8870   tree binfo;
8871   base_kind kind;
8872 
8873   binfo = lookup_base (to, from, c_cast_p ? ba_unique : ba_check,
8874 		       &kind, complain);
8875 
8876   if (binfo == error_mark_node)
8877     {
8878       if (!(complain & tf_error))
8879 	return error_mark_node;
8880 
8881       inform (input_location, "   in pointer to member function conversion");
8882       return size_zero_node;
8883     }
8884   else if (binfo)
8885     {
8886       if (kind != bk_via_virtual)
8887 	return BINFO_OFFSET (binfo);
8888       else
8889 	/* FROM is a virtual base class of TO.  Issue an error or warning
8890 	   depending on whether or not this is a reinterpret cast.  */
8891 	{
8892 	  if (!(complain & tf_error))
8893 	    return error_mark_node;
8894 
8895 	  error ("pointer to member conversion via virtual base %qT",
8896 		 BINFO_TYPE (binfo_from_vbase (binfo)));
8897 
8898 	  return size_zero_node;
8899 	}
8900       }
8901   else
8902     return NULL_TREE;
8903 }
8904 
8905 /* Get difference in deltas for different pointer to member function
8906    types.  If the conversion is invalid and tf_error is not set in
8907    COMPLAIN, returns error_mark_node, otherwise returns an integer
8908    constant of type PTRDIFF_TYPE_NODE and its value is zero if the
8909    conversion is invalid.  If ALLOW_INVERSE_P is true, then allow reverse
8910    conversions as well.  If C_CAST_P is true this conversion is taking
8911    place as part of a C-style cast.
8912 
8913    Note that the naming of FROM and TO is kind of backwards; the return
8914    value is what we add to a TO in order to get a FROM.  They are named
8915    this way because we call this function to find out how to convert from
8916    a pointer to member of FROM to a pointer to member of TO.  */
8917 
8918 static tree
get_delta_difference(tree from,tree to,bool allow_inverse_p,bool c_cast_p,tsubst_flags_t complain)8919 get_delta_difference (tree from, tree to,
8920 		      bool allow_inverse_p,
8921 		      bool c_cast_p, tsubst_flags_t complain)
8922 {
8923   tree result;
8924 
8925   if (same_type_ignoring_top_level_qualifiers_p (from, to))
8926     /* Pointer to member of incomplete class is permitted*/
8927     result = size_zero_node;
8928   else
8929     result = get_delta_difference_1 (from, to, c_cast_p, complain);
8930 
8931   if (result == error_mark_node)
8932     return error_mark_node;
8933 
8934   if (!result)
8935   {
8936     if (!allow_inverse_p)
8937       {
8938 	if (!(complain & tf_error))
8939 	  return error_mark_node;
8940 
8941 	error_not_base_type (from, to);
8942 	inform (input_location, "   in pointer to member conversion");
8943       	result = size_zero_node;
8944       }
8945     else
8946       {
8947 	result = get_delta_difference_1 (to, from, c_cast_p, complain);
8948 
8949 	if (result == error_mark_node)
8950 	  return error_mark_node;
8951 
8952 	if (result)
8953 	  result = size_diffop_loc (input_location,
8954 				    size_zero_node, result);
8955 	else
8956 	  {
8957 	    if (!(complain & tf_error))
8958 	      return error_mark_node;
8959 
8960 	    error_not_base_type (from, to);
8961 	    inform (input_location, "   in pointer to member conversion");
8962 	    result = size_zero_node;
8963 	  }
8964       }
8965   }
8966 
8967   return convert_to_integer (ptrdiff_type_node, result);
8968 }
8969 
8970 /* Return a constructor for the pointer-to-member-function TYPE using
8971    the other components as specified.  */
8972 
8973 tree
build_ptrmemfunc1(tree type,tree delta,tree pfn)8974 build_ptrmemfunc1 (tree type, tree delta, tree pfn)
8975 {
8976   tree u = NULL_TREE;
8977   tree delta_field;
8978   tree pfn_field;
8979   vec<constructor_elt, va_gc> *v;
8980 
8981   /* Pull the FIELD_DECLs out of the type.  */
8982   pfn_field = TYPE_FIELDS (type);
8983   delta_field = DECL_CHAIN (pfn_field);
8984 
8985   /* Make sure DELTA has the type we want.  */
8986   delta = convert_and_check (input_location, delta_type_node, delta);
8987 
8988   /* Convert to the correct target type if necessary.  */
8989   pfn = fold_convert (TREE_TYPE (pfn_field), pfn);
8990 
8991   /* Finish creating the initializer.  */
8992   vec_alloc (v, 2);
8993   CONSTRUCTOR_APPEND_ELT(v, pfn_field, pfn);
8994   CONSTRUCTOR_APPEND_ELT(v, delta_field, delta);
8995   u = build_constructor (type, v);
8996   TREE_CONSTANT (u) = TREE_CONSTANT (pfn) & TREE_CONSTANT (delta);
8997   TREE_STATIC (u) = (TREE_CONSTANT (u)
8998 		     && (initializer_constant_valid_p (pfn, TREE_TYPE (pfn))
8999 			 != NULL_TREE)
9000 		     && (initializer_constant_valid_p (delta, TREE_TYPE (delta))
9001 			 != NULL_TREE));
9002   return u;
9003 }
9004 
9005 /* Build a constructor for a pointer to member function.  It can be
9006    used to initialize global variables, local variable, or used
9007    as a value in expressions.  TYPE is the POINTER to METHOD_TYPE we
9008    want to be.
9009 
9010    If FORCE is nonzero, then force this conversion, even if
9011    we would rather not do it.  Usually set when using an explicit
9012    cast.  A C-style cast is being processed iff C_CAST_P is true.
9013 
9014    Return error_mark_node, if something goes wrong.  */
9015 
9016 tree
build_ptrmemfunc(tree type,tree pfn,int force,bool c_cast_p,tsubst_flags_t complain)9017 build_ptrmemfunc (tree type, tree pfn, int force, bool c_cast_p,
9018 		  tsubst_flags_t complain)
9019 {
9020   tree fn;
9021   tree pfn_type;
9022   tree to_type;
9023 
9024   if (error_operand_p (pfn))
9025     return error_mark_node;
9026 
9027   pfn_type = TREE_TYPE (pfn);
9028   to_type = build_ptrmemfunc_type (type);
9029 
9030   /* Handle multiple conversions of pointer to member functions.  */
9031   if (TYPE_PTRMEMFUNC_P (pfn_type))
9032     {
9033       tree delta = NULL_TREE;
9034       tree npfn = NULL_TREE;
9035       tree n;
9036 
9037       if (!force
9038 	  && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn,
9039 			       LOOKUP_NORMAL, complain))
9040 	{
9041 	  if (complain & tf_error)
9042 	    error ("invalid conversion to type %qT from type %qT",
9043 		   to_type, pfn_type);
9044 	  else
9045 	    return error_mark_node;
9046 	}
9047 
9048       n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type),
9049 				TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type),
9050 				force,
9051 				c_cast_p, complain);
9052       if (n == error_mark_node)
9053 	return error_mark_node;
9054 
9055       STRIP_ANY_LOCATION_WRAPPER (pfn);
9056 
9057       /* We don't have to do any conversion to convert a
9058 	 pointer-to-member to its own type.  But, we don't want to
9059 	 just return a PTRMEM_CST if there's an explicit cast; that
9060 	 cast should make the expression an invalid template argument.  */
9061       if (TREE_CODE (pfn) != PTRMEM_CST
9062 	  && same_type_p (to_type, pfn_type))
9063 	return pfn;
9064 
9065       if (TREE_SIDE_EFFECTS (pfn))
9066 	pfn = save_expr (pfn);
9067 
9068       /* Obtain the function pointer and the current DELTA.  */
9069       if (TREE_CODE (pfn) == PTRMEM_CST)
9070 	expand_ptrmemfunc_cst (pfn, &delta, &npfn);
9071       else
9072 	{
9073 	  npfn = build_ptrmemfunc_access_expr (pfn, pfn_identifier);
9074 	  delta = build_ptrmemfunc_access_expr (pfn, delta_identifier);
9075 	}
9076 
9077       /* Just adjust the DELTA field.  */
9078       gcc_assert  (same_type_ignoring_top_level_qualifiers_p
9079 		   (TREE_TYPE (delta), ptrdiff_type_node));
9080       if (!integer_zerop (n))
9081 	{
9082 	  if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_delta)
9083 	    n = cp_build_binary_op (input_location,
9084 				    LSHIFT_EXPR, n, integer_one_node,
9085 				    complain);
9086 	  delta = cp_build_binary_op (input_location,
9087 				      PLUS_EXPR, delta, n, complain);
9088 	}
9089       return build_ptrmemfunc1 (to_type, delta, npfn);
9090     }
9091 
9092   /* Handle null pointer to member function conversions.  */
9093   if (null_ptr_cst_p (pfn))
9094     {
9095       pfn = cp_build_c_cast (input_location,
9096 			     TYPE_PTRMEMFUNC_FN_TYPE_RAW (to_type),
9097 			     pfn, complain);
9098       return build_ptrmemfunc1 (to_type,
9099 				integer_zero_node,
9100 				pfn);
9101     }
9102 
9103   if (type_unknown_p (pfn))
9104     return instantiate_type (type, pfn, complain);
9105 
9106   fn = TREE_OPERAND (pfn, 0);
9107   gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
9108 	      /* In a template, we will have preserved the
9109 		 OFFSET_REF.  */
9110 	      || (processing_template_decl && TREE_CODE (fn) == OFFSET_REF));
9111   return make_ptrmem_cst (to_type, fn);
9112 }
9113 
9114 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
9115    given by CST.
9116 
9117    ??? There is no consistency as to the types returned for the above
9118    values.  Some code acts as if it were a sizetype and some as if it were
9119    integer_type_node.  */
9120 
9121 void
expand_ptrmemfunc_cst(tree cst,tree * delta,tree * pfn)9122 expand_ptrmemfunc_cst (tree cst, tree *delta, tree *pfn)
9123 {
9124   tree type = TREE_TYPE (cst);
9125   tree fn = PTRMEM_CST_MEMBER (cst);
9126   tree ptr_class, fn_class;
9127 
9128   gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
9129 
9130   /* The class that the function belongs to.  */
9131   fn_class = DECL_CONTEXT (fn);
9132 
9133   /* The class that we're creating a pointer to member of.  */
9134   ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type);
9135 
9136   /* First, calculate the adjustment to the function's class.  */
9137   *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0,
9138 				 /*c_cast_p=*/0, tf_warning_or_error);
9139 
9140   if (!DECL_VIRTUAL_P (fn))
9141     *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type),
9142 		    build_addr_func (fn, tf_warning_or_error));
9143   else
9144     {
9145       /* If we're dealing with a virtual function, we have to adjust 'this'
9146 	 again, to point to the base which provides the vtable entry for
9147 	 fn; the call will do the opposite adjustment.  */
9148       tree orig_class = DECL_CONTEXT (fn);
9149       tree binfo = binfo_or_else (orig_class, fn_class);
9150       *delta = fold_build2 (PLUS_EXPR, TREE_TYPE (*delta),
9151 			    *delta, BINFO_OFFSET (binfo));
9152 
9153       /* We set PFN to the vtable offset at which the function can be
9154 	 found, plus one (unless ptrmemfunc_vbit_in_delta, in which
9155 	 case delta is shifted left, and then incremented).  */
9156       *pfn = DECL_VINDEX (fn);
9157       *pfn = fold_build2 (MULT_EXPR, integer_type_node, *pfn,
9158 			  TYPE_SIZE_UNIT (vtable_entry_type));
9159 
9160       switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
9161 	{
9162 	case ptrmemfunc_vbit_in_pfn:
9163 	  *pfn = fold_build2 (PLUS_EXPR, integer_type_node, *pfn,
9164 			      integer_one_node);
9165 	  break;
9166 
9167 	case ptrmemfunc_vbit_in_delta:
9168 	  *delta = fold_build2 (LSHIFT_EXPR, TREE_TYPE (*delta),
9169 				*delta, integer_one_node);
9170 	  *delta = fold_build2 (PLUS_EXPR, TREE_TYPE (*delta),
9171 				*delta, integer_one_node);
9172 	  break;
9173 
9174 	default:
9175 	  gcc_unreachable ();
9176 	}
9177 
9178       *pfn = fold_convert (TYPE_PTRMEMFUNC_FN_TYPE (type), *pfn);
9179     }
9180 }
9181 
9182 /* Return an expression for PFN from the pointer-to-member function
9183    given by T.  */
9184 
9185 static tree
pfn_from_ptrmemfunc(tree t)9186 pfn_from_ptrmemfunc (tree t)
9187 {
9188   if (TREE_CODE (t) == PTRMEM_CST)
9189     {
9190       tree delta;
9191       tree pfn;
9192 
9193       expand_ptrmemfunc_cst (t, &delta, &pfn);
9194       if (pfn)
9195 	return pfn;
9196     }
9197 
9198   return build_ptrmemfunc_access_expr (t, pfn_identifier);
9199 }
9200 
9201 /* Return an expression for DELTA from the pointer-to-member function
9202    given by T.  */
9203 
9204 static tree
delta_from_ptrmemfunc(tree t)9205 delta_from_ptrmemfunc (tree t)
9206 {
9207   if (TREE_CODE (t) == PTRMEM_CST)
9208     {
9209       tree delta;
9210       tree pfn;
9211 
9212       expand_ptrmemfunc_cst (t, &delta, &pfn);
9213       if (delta)
9214 	return delta;
9215     }
9216 
9217   return build_ptrmemfunc_access_expr (t, delta_identifier);
9218 }
9219 
9220 /* Convert value RHS to type TYPE as preparation for an assignment to
9221    an lvalue of type TYPE.  ERRTYPE indicates what kind of error the
9222    implicit conversion is.  If FNDECL is non-NULL, we are doing the
9223    conversion in order to pass the PARMNUMth argument of FNDECL.
9224    If FNDECL is NULL, we are doing the conversion in function pointer
9225    argument passing, conversion in initialization, etc. */
9226 
9227 static tree
convert_for_assignment(tree type,tree rhs,impl_conv_rhs errtype,tree fndecl,int parmnum,tsubst_flags_t complain,int flags)9228 convert_for_assignment (tree type, tree rhs,
9229 			impl_conv_rhs errtype, tree fndecl, int parmnum,
9230 			tsubst_flags_t complain, int flags)
9231 {
9232   tree rhstype;
9233   enum tree_code coder;
9234 
9235   location_t rhs_loc = EXPR_LOC_OR_LOC (rhs, input_location);
9236   bool has_loc = EXPR_LOCATION (rhs) != UNKNOWN_LOCATION;
9237   /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue,
9238      but preserve location wrappers.  */
9239   if (TREE_CODE (rhs) == NON_LVALUE_EXPR
9240       && !location_wrapper_p (rhs))
9241     rhs = TREE_OPERAND (rhs, 0);
9242 
9243   /* Handle [dcl.init.list] direct-list-initialization from
9244      single element of enumeration with a fixed underlying type.  */
9245   if (is_direct_enum_init (type, rhs))
9246     {
9247       tree elt = CONSTRUCTOR_ELT (rhs, 0)->value;
9248       if (check_narrowing (ENUM_UNDERLYING_TYPE (type), elt, complain))
9249 	{
9250 	  warning_sentinel w (warn_useless_cast);
9251 	  warning_sentinel w2 (warn_ignored_qualifiers);
9252 	  rhs = cp_build_c_cast (rhs_loc, type, elt, complain);
9253 	}
9254       else
9255 	rhs = error_mark_node;
9256     }
9257 
9258   rhstype = TREE_TYPE (rhs);
9259   coder = TREE_CODE (rhstype);
9260 
9261   if (VECTOR_TYPE_P (type) && coder == VECTOR_TYPE
9262       && vector_types_convertible_p (type, rhstype, true))
9263     {
9264       rhs = mark_rvalue_use (rhs);
9265       return convert (type, rhs);
9266     }
9267 
9268   if (rhs == error_mark_node || rhstype == error_mark_node)
9269     return error_mark_node;
9270   if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)
9271     return error_mark_node;
9272 
9273   /* The RHS of an assignment cannot have void type.  */
9274   if (coder == VOID_TYPE)
9275     {
9276       if (complain & tf_error)
9277 	error_at (rhs_loc, "void value not ignored as it ought to be");
9278       return error_mark_node;
9279     }
9280 
9281   if (c_dialect_objc ())
9282     {
9283       int parmno;
9284       tree selector;
9285       tree rname = fndecl;
9286 
9287       switch (errtype)
9288         {
9289 	  case ICR_ASSIGN:
9290 	    parmno = -1;
9291 	    break;
9292 	  case ICR_INIT:
9293 	    parmno = -2;
9294 	    break;
9295 	  default:
9296 	    selector = objc_message_selector ();
9297 	    parmno = parmnum;
9298 	    if (selector && parmno > 1)
9299 	      {
9300 		rname = selector;
9301 		parmno -= 1;
9302 	      }
9303 	}
9304 
9305       if (objc_compare_types (type, rhstype, parmno, rname))
9306 	{
9307 	  rhs = mark_rvalue_use (rhs);
9308 	  return convert (type, rhs);
9309 	}
9310     }
9311 
9312   /* [expr.ass]
9313 
9314      The expression is implicitly converted (clause _conv_) to the
9315      cv-unqualified type of the left operand.
9316 
9317      We allow bad conversions here because by the time we get to this point
9318      we are committed to doing the conversion.  If we end up doing a bad
9319      conversion, convert_like will complain.  */
9320   if (!can_convert_arg_bad (type, rhstype, rhs, flags, complain))
9321     {
9322       /* When -Wno-pmf-conversions is use, we just silently allow
9323 	 conversions from pointers-to-members to plain pointers.  If
9324 	 the conversion doesn't work, cp_convert will complain.  */
9325       if (!warn_pmf2ptr
9326 	  && TYPE_PTR_P (type)
9327 	  && TYPE_PTRMEMFUNC_P (rhstype))
9328 	rhs = cp_convert (strip_top_quals (type), rhs, complain);
9329       else
9330 	{
9331 	  if (complain & tf_error)
9332 	    {
9333 	      /* If the right-hand side has unknown type, then it is an
9334 		 overloaded function.  Call instantiate_type to get error
9335 		 messages.  */
9336 	      if (rhstype == unknown_type_node)
9337 		{
9338 		  tree r = instantiate_type (type, rhs, tf_warning_or_error);
9339 		  /* -fpermissive might allow this; recurse.  */
9340 		  if (!seen_error ())
9341 		    return convert_for_assignment (type, r, errtype, fndecl,
9342 						   parmnum, complain, flags);
9343 		}
9344 	      else if (fndecl)
9345 		complain_about_bad_argument (rhs_loc,
9346 					     rhstype, type,
9347 					     fndecl, parmnum);
9348 	      else
9349 		{
9350 		  range_label_for_type_mismatch label (rhstype, type);
9351 		  gcc_rich_location richloc (rhs_loc, has_loc ? &label : NULL);
9352 		  switch (errtype)
9353 		    {
9354 		    case ICR_DEFAULT_ARGUMENT:
9355 		      error_at (&richloc,
9356 				"cannot convert %qH to %qI in default argument",
9357 				rhstype, type);
9358 		      break;
9359 		    case ICR_ARGPASS:
9360 		      error_at (&richloc,
9361 				"cannot convert %qH to %qI in argument passing",
9362 				rhstype, type);
9363 		      break;
9364 		    case ICR_CONVERTING:
9365 		      error_at (&richloc, "cannot convert %qH to %qI",
9366 				rhstype, type);
9367 		      break;
9368 		    case ICR_INIT:
9369 		      error_at (&richloc,
9370 				"cannot convert %qH to %qI in initialization",
9371 				rhstype, type);
9372 		      break;
9373 		    case ICR_RETURN:
9374 		      error_at (&richloc, "cannot convert %qH to %qI in return",
9375 				rhstype, type);
9376 		      break;
9377 		    case ICR_ASSIGN:
9378 		      error_at (&richloc,
9379 				"cannot convert %qH to %qI in assignment",
9380 				rhstype, type);
9381 		      break;
9382 		    default:
9383 		      gcc_unreachable();
9384 		  }
9385 		}
9386 	      if (TYPE_PTR_P (rhstype)
9387 		  && TYPE_PTR_P (type)
9388 		  && CLASS_TYPE_P (TREE_TYPE (rhstype))
9389 		  && CLASS_TYPE_P (TREE_TYPE (type))
9390 		  && !COMPLETE_TYPE_P (TREE_TYPE (rhstype)))
9391 		inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL
9392 					      (TREE_TYPE (rhstype))),
9393 			"class type %qT is incomplete", TREE_TYPE (rhstype));
9394 	    }
9395 	  return error_mark_node;
9396 	}
9397     }
9398   if (warn_suggest_attribute_format)
9399     {
9400       const enum tree_code codel = TREE_CODE (type);
9401       if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
9402 	  && coder == codel
9403 	  && check_missing_format_attribute (type, rhstype)
9404 	  && (complain & tf_warning))
9405 	switch (errtype)
9406 	  {
9407 	    case ICR_ARGPASS:
9408 	    case ICR_DEFAULT_ARGUMENT:
9409 	      if (fndecl)
9410 		warning (OPT_Wsuggest_attribute_format,
9411 			 "parameter %qP of %qD might be a candidate "
9412 			 "for a format attribute", parmnum, fndecl);
9413 	      else
9414 		warning (OPT_Wsuggest_attribute_format,
9415 			 "parameter might be a candidate "
9416 			 "for a format attribute");
9417 	      break;
9418 	    case ICR_CONVERTING:
9419 	      warning (OPT_Wsuggest_attribute_format,
9420 		       "target of conversion might be a candidate "
9421 		       "for a format attribute");
9422 	      break;
9423 	    case ICR_INIT:
9424 	      warning (OPT_Wsuggest_attribute_format,
9425 		       "target of initialization might be a candidate "
9426 		       "for a format attribute");
9427 	      break;
9428 	    case ICR_RETURN:
9429 	      warning (OPT_Wsuggest_attribute_format,
9430 		       "return type might be a candidate "
9431 		       "for a format attribute");
9432 	      break;
9433 	    case ICR_ASSIGN:
9434 	      warning (OPT_Wsuggest_attribute_format,
9435 		       "left-hand side of assignment might be a candidate "
9436 		       "for a format attribute");
9437 	      break;
9438 	    default:
9439 	      gcc_unreachable();
9440 	  }
9441     }
9442 
9443   /* If -Wparentheses, warn about a = b = c when a has type bool and b
9444      does not.  */
9445   if (warn_parentheses
9446       && TREE_CODE (type) == BOOLEAN_TYPE
9447       && TREE_CODE (rhs) == MODIFY_EXPR
9448       && !TREE_NO_WARNING (rhs)
9449       && TREE_CODE (TREE_TYPE (rhs)) != BOOLEAN_TYPE
9450       && (complain & tf_warning)
9451       && warning_at (rhs_loc, OPT_Wparentheses,
9452 		     "suggest parentheses around assignment used as "
9453 		     "truth value"))
9454     TREE_NO_WARNING (rhs) = 1;
9455 
9456   if (complain & tf_warning)
9457     warn_for_address_or_pointer_of_packed_member (type, rhs);
9458 
9459   return perform_implicit_conversion_flags (strip_top_quals (type), rhs,
9460 					    complain, flags);
9461 }
9462 
9463 /* Convert RHS to be of type TYPE.
9464    If EXP is nonzero, it is the target of the initialization.
9465    ERRTYPE indicates what kind of error the implicit conversion is.
9466 
9467    Two major differences between the behavior of
9468    `convert_for_assignment' and `convert_for_initialization'
9469    are that references are bashed in the former, while
9470    copied in the latter, and aggregates are assigned in
9471    the former (operator=) while initialized in the
9472    latter (X(X&)).
9473 
9474    If using constructor make sure no conversion operator exists, if one does
9475    exist, an ambiguity exists.  */
9476 
9477 tree
convert_for_initialization(tree exp,tree type,tree rhs,int flags,impl_conv_rhs errtype,tree fndecl,int parmnum,tsubst_flags_t complain)9478 convert_for_initialization (tree exp, tree type, tree rhs, int flags,
9479 			    impl_conv_rhs errtype, tree fndecl, int parmnum,
9480                             tsubst_flags_t complain)
9481 {
9482   enum tree_code codel = TREE_CODE (type);
9483   tree rhstype;
9484   enum tree_code coder;
9485 
9486   /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
9487      Strip such NOP_EXPRs, since RHS is used in non-lvalue context.  */
9488   if (TREE_CODE (rhs) == NOP_EXPR
9489       && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
9490       && codel != REFERENCE_TYPE)
9491     rhs = TREE_OPERAND (rhs, 0);
9492 
9493   if (type == error_mark_node
9494       || rhs == error_mark_node
9495       || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
9496     return error_mark_node;
9497 
9498   if (MAYBE_CLASS_TYPE_P (non_reference (type)))
9499     ;
9500   else if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
9501 	    && TREE_CODE (type) != ARRAY_TYPE
9502 	    && (!TYPE_REF_P (type)
9503 		|| TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
9504 	   || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
9505 	       && !TYPE_REFFN_P (type))
9506 	   || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
9507     rhs = decay_conversion (rhs, complain);
9508 
9509   rhstype = TREE_TYPE (rhs);
9510   coder = TREE_CODE (rhstype);
9511 
9512   if (coder == ERROR_MARK)
9513     return error_mark_node;
9514 
9515   /* We accept references to incomplete types, so we can
9516      return here before checking if RHS is of complete type.  */
9517 
9518   if (codel == REFERENCE_TYPE)
9519     {
9520       auto_diagnostic_group d;
9521       /* This should eventually happen in convert_arguments.  */
9522       int savew = 0, savee = 0;
9523 
9524       if (fndecl)
9525 	savew = warningcount + werrorcount, savee = errorcount;
9526       rhs = initialize_reference (type, rhs, flags, complain);
9527 
9528       if (fndecl
9529 	  && (warningcount + werrorcount > savew || errorcount > savee))
9530 	inform (get_fndecl_argument_location (fndecl, parmnum),
9531 		"in passing argument %P of %qD", parmnum, fndecl);
9532       return rhs;
9533     }
9534 
9535   if (exp != 0)
9536     exp = require_complete_type_sfinae (exp, complain);
9537   if (exp == error_mark_node)
9538     return error_mark_node;
9539 
9540   type = complete_type (type);
9541 
9542   if (DIRECT_INIT_EXPR_P (type, rhs))
9543     /* Don't try to do copy-initialization if we already have
9544        direct-initialization.  */
9545     return rhs;
9546 
9547   if (MAYBE_CLASS_TYPE_P (type))
9548     return perform_implicit_conversion_flags (type, rhs, complain, flags);
9549 
9550   return convert_for_assignment (type, rhs, errtype, fndecl, parmnum,
9551 				 complain, flags);
9552 }
9553 
9554 /* If RETVAL is the address of, or a reference to, a local variable or
9555    temporary give an appropriate warning and return true.  */
9556 
9557 static bool
maybe_warn_about_returning_address_of_local(tree retval,location_t loc)9558 maybe_warn_about_returning_address_of_local (tree retval, location_t loc)
9559 {
9560   tree valtype = TREE_TYPE (DECL_RESULT (current_function_decl));
9561   tree whats_returned = fold_for_warn (retval);
9562   if (!loc)
9563     loc = cp_expr_loc_or_input_loc (retval);
9564 
9565   for (;;)
9566     {
9567       if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
9568 	whats_returned = TREE_OPERAND (whats_returned, 1);
9569       else if (CONVERT_EXPR_P (whats_returned)
9570 	       || TREE_CODE (whats_returned) == NON_LVALUE_EXPR)
9571 	whats_returned = TREE_OPERAND (whats_returned, 0);
9572       else
9573 	break;
9574     }
9575 
9576   if (TREE_CODE (whats_returned) == TARGET_EXPR
9577       && is_std_init_list (TREE_TYPE (whats_returned)))
9578     {
9579       tree init = TARGET_EXPR_INITIAL (whats_returned);
9580       if (TREE_CODE (init) == CONSTRUCTOR)
9581 	/* Pull out the array address.  */
9582 	whats_returned = CONSTRUCTOR_ELT (init, 0)->value;
9583       else if (TREE_CODE (init) == INDIRECT_REF)
9584 	/* The source of a trivial copy looks like *(T*)&var.  */
9585 	whats_returned = TREE_OPERAND (init, 0);
9586       else
9587 	return false;
9588       STRIP_NOPS (whats_returned);
9589     }
9590 
9591   /* As a special case, we handle a call to std::move or std::forward.  */
9592   if (TREE_CODE (whats_returned) == CALL_EXPR
9593       && (is_std_move_p (whats_returned)
9594 	  || is_std_forward_p (whats_returned)))
9595     {
9596       tree arg = CALL_EXPR_ARG (whats_returned, 0);
9597       return maybe_warn_about_returning_address_of_local (arg, loc);
9598     }
9599 
9600   if (TREE_CODE (whats_returned) != ADDR_EXPR)
9601     return false;
9602   whats_returned = TREE_OPERAND (whats_returned, 0);
9603 
9604   while (TREE_CODE (whats_returned) == COMPONENT_REF
9605 	 || TREE_CODE (whats_returned) == ARRAY_REF)
9606     whats_returned = TREE_OPERAND (whats_returned, 0);
9607 
9608   if (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
9609       || TREE_CODE (whats_returned) == TARGET_EXPR)
9610     {
9611       if (TYPE_REF_P (valtype))
9612 	warning_at (loc, OPT_Wreturn_local_addr,
9613 		    "returning reference to temporary");
9614       else if (is_std_init_list (valtype))
9615 	warning_at (loc, OPT_Winit_list_lifetime,
9616 		    "returning temporary %<initializer_list%> does not extend "
9617 		    "the lifetime of the underlying array");
9618       return true;
9619     }
9620 
9621   STRIP_ANY_LOCATION_WRAPPER (whats_returned);
9622 
9623   if (DECL_P (whats_returned)
9624       && DECL_NAME (whats_returned)
9625       && DECL_FUNCTION_SCOPE_P (whats_returned)
9626       && !is_capture_proxy (whats_returned)
9627       && !(TREE_STATIC (whats_returned)
9628 	   || TREE_PUBLIC (whats_returned)))
9629     {
9630       if (VAR_P (whats_returned)
9631 	  && DECL_DECOMPOSITION_P (whats_returned)
9632 	  && DECL_DECOMP_BASE (whats_returned)
9633 	  && DECL_HAS_VALUE_EXPR_P (whats_returned))
9634 	{
9635 	  /* When returning address of a structured binding, if the structured
9636 	     binding is not a reference, continue normally, if it is a
9637 	     reference, recurse on the initializer of the structured
9638 	     binding.  */
9639 	  tree base = DECL_DECOMP_BASE (whats_returned);
9640 	  if (TYPE_REF_P (TREE_TYPE (base)))
9641 	    {
9642 	      if (tree init = DECL_INITIAL (base))
9643 		return maybe_warn_about_returning_address_of_local (init, loc);
9644 	      else
9645 		return false;
9646 	    }
9647 	}
9648       bool w = false;
9649       auto_diagnostic_group d;
9650       if (TYPE_REF_P (valtype))
9651 	w = warning_at (loc, OPT_Wreturn_local_addr,
9652 			"reference to local variable %qD returned",
9653 			whats_returned);
9654       else if (is_std_init_list (valtype))
9655 	w = warning_at (loc, OPT_Winit_list_lifetime,
9656 			"returning local %<initializer_list%> variable %qD "
9657 			"does not extend the lifetime of the underlying array",
9658 			whats_returned);
9659       else if (POINTER_TYPE_P (valtype)
9660 	       && TREE_CODE (whats_returned) == LABEL_DECL)
9661 	w = warning_at (loc, OPT_Wreturn_local_addr,
9662 			"address of label %qD returned",
9663 			whats_returned);
9664       else if (POINTER_TYPE_P (valtype))
9665 	w = warning_at (loc, OPT_Wreturn_local_addr,
9666 			"address of local variable %qD returned",
9667 			whats_returned);
9668       if (w)
9669 	inform (DECL_SOURCE_LOCATION (whats_returned),
9670 		"declared here");
9671       return true;
9672     }
9673 
9674   return false;
9675 }
9676 
9677 /* Returns true if DECL is in the std namespace.  */
9678 
9679 bool
decl_in_std_namespace_p(tree decl)9680 decl_in_std_namespace_p (tree decl)
9681 {
9682   while (decl)
9683     {
9684       decl = decl_namespace_context (decl);
9685       if (DECL_NAMESPACE_STD_P (decl))
9686 	return true;
9687       /* Allow inline namespaces inside of std namespace, e.g. with
9688 	 --enable-symvers=gnu-versioned-namespace std::forward would be
9689 	 actually std::_8::forward.  */
9690       if (!DECL_NAMESPACE_INLINE_P (decl))
9691 	return false;
9692       decl = CP_DECL_CONTEXT (decl);
9693     }
9694   return false;
9695 }
9696 
9697 /* Returns true if FN, a CALL_EXPR, is a call to std::forward.  */
9698 
9699 static bool
is_std_forward_p(tree fn)9700 is_std_forward_p (tree fn)
9701 {
9702   /* std::forward only takes one argument.  */
9703   if (call_expr_nargs (fn) != 1)
9704     return false;
9705 
9706   tree fndecl = cp_get_callee_fndecl_nofold (fn);
9707   if (!decl_in_std_namespace_p (fndecl))
9708     return false;
9709 
9710   tree name = DECL_NAME (fndecl);
9711   return name && id_equal (name, "forward");
9712 }
9713 
9714 /* Returns true if FN, a CALL_EXPR, is a call to std::move.  */
9715 
9716 static bool
is_std_move_p(tree fn)9717 is_std_move_p (tree fn)
9718 {
9719   /* std::move only takes one argument.  */
9720   if (call_expr_nargs (fn) != 1)
9721     return false;
9722 
9723   tree fndecl = cp_get_callee_fndecl_nofold (fn);
9724   if (!decl_in_std_namespace_p (fndecl))
9725     return false;
9726 
9727   tree name = DECL_NAME (fndecl);
9728   return name && id_equal (name, "move");
9729 }
9730 
9731 /* Returns true if RETVAL is a good candidate for the NRVO as per
9732    [class.copy.elision].  FUNCTYPE is the type the function is declared
9733    to return.  */
9734 
9735 static bool
can_do_nrvo_p(tree retval,tree functype)9736 can_do_nrvo_p (tree retval, tree functype)
9737 {
9738   if (functype == error_mark_node)
9739     return false;
9740   if (retval)
9741     STRIP_ANY_LOCATION_WRAPPER (retval);
9742   tree result = DECL_RESULT (current_function_decl);
9743   return (retval != NULL_TREE
9744 	  && !processing_template_decl
9745 	  /* Must be a local, automatic variable.  */
9746 	  && VAR_P (retval)
9747 	  && DECL_CONTEXT (retval) == current_function_decl
9748 	  && !TREE_STATIC (retval)
9749 	  /* And not a lambda or anonymous union proxy.  */
9750 	  && !DECL_HAS_VALUE_EXPR_P (retval)
9751 	  && (DECL_ALIGN (retval) <= DECL_ALIGN (result))
9752 	  /* The cv-unqualified type of the returned value must be the
9753 	     same as the cv-unqualified return type of the
9754 	     function.  */
9755 	  && same_type_p ((TYPE_MAIN_VARIANT (TREE_TYPE (retval))),
9756 			  (TYPE_MAIN_VARIANT (functype)))
9757 	  /* And the returned value must be non-volatile.  */
9758 	  && !TYPE_VOLATILE (TREE_TYPE (retval)));
9759 }
9760 
9761 /* Returns true if we should treat RETVAL, an expression being returned,
9762    as if it were designated by an rvalue.  See [class.copy.elision].
9763    PARM_P is true if a function parameter is OK in this context.  */
9764 
9765 bool
treat_lvalue_as_rvalue_p(tree retval,bool parm_ok)9766 treat_lvalue_as_rvalue_p (tree retval, bool parm_ok)
9767 {
9768   STRIP_ANY_LOCATION_WRAPPER (retval);
9769   return ((cxx_dialect != cxx98)
9770 	  && ((VAR_P (retval) && !DECL_HAS_VALUE_EXPR_P (retval))
9771 	      || (parm_ok && TREE_CODE (retval) == PARM_DECL))
9772 	  && DECL_CONTEXT (retval) == current_function_decl
9773 	  && !TREE_STATIC (retval));
9774 }
9775 
9776 /* Warn about wrong usage of std::move in a return statement.  RETVAL
9777    is the expression we are returning; FUNCTYPE is the type the function
9778    is declared to return.  */
9779 
9780 static void
maybe_warn_pessimizing_move(tree retval,tree functype)9781 maybe_warn_pessimizing_move (tree retval, tree functype)
9782 {
9783   if (!(warn_pessimizing_move || warn_redundant_move))
9784     return;
9785 
9786   location_t loc = cp_expr_loc_or_input_loc (retval);
9787 
9788   /* C++98 doesn't know move.  */
9789   if (cxx_dialect < cxx11)
9790     return;
9791 
9792   /* Wait until instantiation time, since we can't gauge if we should do
9793      the NRVO until then.  */
9794   if (processing_template_decl)
9795     return;
9796 
9797   /* This is only interesting for class types.  */
9798   if (!CLASS_TYPE_P (functype))
9799     return;
9800 
9801   /* We're looking for *std::move<T&> ((T &) &arg).  */
9802   if (REFERENCE_REF_P (retval)
9803       && TREE_CODE (TREE_OPERAND (retval, 0)) == CALL_EXPR)
9804     {
9805       tree fn = TREE_OPERAND (retval, 0);
9806       if (is_std_move_p (fn))
9807 	{
9808 	  tree arg = CALL_EXPR_ARG (fn, 0);
9809 	  if (TREE_CODE (arg) != NOP_EXPR)
9810 	    return;
9811 	  arg = TREE_OPERAND (arg, 0);
9812 	  if (TREE_CODE (arg) != ADDR_EXPR)
9813 	    return;
9814 	  arg = TREE_OPERAND (arg, 0);
9815 	  arg = convert_from_reference (arg);
9816 	  /* Warn if we could do copy elision were it not for the move.  */
9817 	  if (can_do_nrvo_p (arg, functype))
9818 	    {
9819 	      auto_diagnostic_group d;
9820 	      if (warning_at (loc, OPT_Wpessimizing_move,
9821 			      "moving a local object in a return statement "
9822 			      "prevents copy elision"))
9823 		inform (loc, "remove %<std::move%> call");
9824 	    }
9825 	  /* Warn if the move is redundant.  It is redundant when we would
9826 	     do maybe-rvalue overload resolution even without std::move.  */
9827 	  else if (warn_redundant_move
9828 		   && treat_lvalue_as_rvalue_p (arg, /*parm_ok*/true))
9829 	    {
9830 	      /* Make sure that the overload resolution would actually succeed
9831 		 if we removed the std::move call.  */
9832 	      tree t = convert_for_initialization (NULL_TREE, functype,
9833 						   move (arg),
9834 						   (LOOKUP_NORMAL
9835 						    | LOOKUP_ONLYCONVERTING
9836 						    | LOOKUP_PREFER_RVALUE),
9837 						   ICR_RETURN, NULL_TREE, 0,
9838 						   tf_none);
9839 	      /* If this worked, implicit rvalue would work, so the call to
9840 		 std::move is redundant.  */
9841 	      if (t != error_mark_node)
9842 		{
9843 		  auto_diagnostic_group d;
9844 		  if (warning_at (loc, OPT_Wredundant_move,
9845 				  "redundant move in return statement"))
9846 		    inform (loc, "remove %<std::move%> call");
9847 		}
9848 	    }
9849 	}
9850     }
9851 }
9852 
9853 /* Check that returning RETVAL from the current function is valid.
9854    Return an expression explicitly showing all conversions required to
9855    change RETVAL into the function return type, and to assign it to
9856    the DECL_RESULT for the function.  Set *NO_WARNING to true if
9857    code reaches end of non-void function warning shouldn't be issued
9858    on this RETURN_EXPR.  */
9859 
9860 tree
check_return_expr(tree retval,bool * no_warning)9861 check_return_expr (tree retval, bool *no_warning)
9862 {
9863   tree result;
9864   /* The type actually returned by the function.  */
9865   tree valtype;
9866   /* The type the function is declared to return, or void if
9867      the declared type is incomplete.  */
9868   tree functype;
9869   int fn_returns_value_p;
9870   location_t loc = cp_expr_loc_or_input_loc (retval);
9871 
9872   *no_warning = false;
9873 
9874   /* A `volatile' function is one that isn't supposed to return, ever.
9875      (This is a G++ extension, used to get better code for functions
9876      that call the `volatile' function.)  */
9877   if (TREE_THIS_VOLATILE (current_function_decl))
9878     warning (0, "function declared %<noreturn%> has a %<return%> statement");
9879 
9880   /* Check for various simple errors.  */
9881   if (DECL_DESTRUCTOR_P (current_function_decl))
9882     {
9883       if (retval)
9884 	error_at (loc, "returning a value from a destructor");
9885       return NULL_TREE;
9886     }
9887   else if (DECL_CONSTRUCTOR_P (current_function_decl))
9888     {
9889       if (in_function_try_handler)
9890 	/* If a return statement appears in a handler of the
9891 	   function-try-block of a constructor, the program is ill-formed.  */
9892 	error ("cannot return from a handler of a function-try-block of a constructor");
9893       else if (retval)
9894 	/* You can't return a value from a constructor.  */
9895 	error_at (loc, "returning a value from a constructor");
9896       return NULL_TREE;
9897     }
9898 
9899   const tree saved_retval = retval;
9900 
9901   if (processing_template_decl)
9902     {
9903       current_function_returns_value = 1;
9904 
9905       if (check_for_bare_parameter_packs (retval))
9906 	return error_mark_node;
9907 
9908       /* If one of the types might be void, we can't tell whether we're
9909 	 returning a value.  */
9910       if ((WILDCARD_TYPE_P (TREE_TYPE (DECL_RESULT (current_function_decl)))
9911 	   && !FNDECL_USED_AUTO (current_function_decl))
9912 	  || (retval != NULL_TREE
9913 	      && (TREE_TYPE (retval) == NULL_TREE
9914 		  || WILDCARD_TYPE_P (TREE_TYPE (retval)))))
9915 	goto dependent;
9916     }
9917 
9918   functype = TREE_TYPE (TREE_TYPE (current_function_decl));
9919 
9920   /* Deduce auto return type from a return statement.  */
9921   if (FNDECL_USED_AUTO (current_function_decl))
9922     {
9923       tree pattern = DECL_SAVED_AUTO_RETURN_TYPE (current_function_decl);
9924       tree auto_node;
9925       tree type;
9926 
9927       if (!retval && !is_auto (pattern))
9928 	{
9929 	  /* Give a helpful error message.  */
9930 	  error ("return-statement with no value, in function returning %qT",
9931 		 pattern);
9932 	  inform (input_location, "only plain %<auto%> return type can be "
9933 		  "deduced to %<void%>");
9934 	  type = error_mark_node;
9935 	}
9936       else if (retval && BRACE_ENCLOSED_INITIALIZER_P (retval))
9937 	{
9938 	  error ("returning initializer list");
9939 	  type = error_mark_node;
9940 	}
9941       else
9942 	{
9943 	  if (!retval)
9944 	    retval = void_node;
9945 	  auto_node = type_uses_auto (pattern);
9946 	  type = do_auto_deduction (pattern, retval, auto_node,
9947 				    tf_warning_or_error, adc_return_type);
9948 	}
9949 
9950       if (type == error_mark_node)
9951 	/* Leave it.  */;
9952       else if (functype == pattern)
9953 	apply_deduced_return_type (current_function_decl, type);
9954       else if (!same_type_p (type, functype))
9955 	{
9956 	  if (LAMBDA_FUNCTION_P (current_function_decl))
9957 	    error_at (loc, "inconsistent types %qT and %qT deduced for "
9958 		      "lambda return type", functype, type);
9959 	  else
9960 	    error_at (loc, "inconsistent deduction for auto return type: "
9961 		      "%qT and then %qT", functype, type);
9962 	}
9963       functype = type;
9964     }
9965 
9966   result = DECL_RESULT (current_function_decl);
9967   valtype = TREE_TYPE (result);
9968   gcc_assert (valtype != NULL_TREE);
9969   fn_returns_value_p = !VOID_TYPE_P (valtype);
9970 
9971   /* Check for a return statement with no return value in a function
9972      that's supposed to return a value.  */
9973   if (!retval && fn_returns_value_p)
9974     {
9975       if (functype != error_mark_node)
9976 	permerror (input_location, "return-statement with no value, in "
9977 		   "function returning %qT", valtype);
9978       /* Remember that this function did return.  */
9979       current_function_returns_value = 1;
9980       /* And signal caller that TREE_NO_WARNING should be set on the
9981 	 RETURN_EXPR to avoid control reaches end of non-void function
9982 	 warnings in tree-cfg.c.  */
9983       *no_warning = true;
9984     }
9985   /* Check for a return statement with a value in a function that
9986      isn't supposed to return a value.  */
9987   else if (retval && !fn_returns_value_p)
9988     {
9989       if (VOID_TYPE_P (TREE_TYPE (retval)))
9990 	/* You can return a `void' value from a function of `void'
9991 	   type.  In that case, we have to evaluate the expression for
9992 	   its side-effects.  */
9993 	finish_expr_stmt (retval);
9994       else if (retval != error_mark_node)
9995 	permerror (loc, "return-statement with a value, in function "
9996 		   "returning %qT", valtype);
9997       current_function_returns_null = 1;
9998 
9999       /* There's really no value to return, after all.  */
10000       return NULL_TREE;
10001     }
10002   else if (!retval)
10003     /* Remember that this function can sometimes return without a
10004        value.  */
10005     current_function_returns_null = 1;
10006   else
10007     /* Remember that this function did return a value.  */
10008     current_function_returns_value = 1;
10009 
10010   /* Check for erroneous operands -- but after giving ourselves a
10011      chance to provide an error about returning a value from a void
10012      function.  */
10013   if (error_operand_p (retval))
10014     {
10015       current_function_return_value = error_mark_node;
10016       return error_mark_node;
10017     }
10018 
10019   /* Only operator new(...) throw(), can return NULL [expr.new/13].  */
10020   if (IDENTIFIER_NEW_OP_P (DECL_NAME (current_function_decl))
10021       && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl))
10022       && ! flag_check_new
10023       && retval && null_ptr_cst_p (retval))
10024     warning (0, "%<operator new%> must not return NULL unless it is "
10025 	     "declared %<throw()%> (or %<-fcheck-new%> is in effect)");
10026 
10027   /* Effective C++ rule 15.  See also start_function.  */
10028   if (warn_ecpp
10029       && DECL_NAME (current_function_decl) == assign_op_identifier
10030       && !type_dependent_expression_p (retval))
10031     {
10032       bool warn = true;
10033 
10034       /* The function return type must be a reference to the current
10035 	class.  */
10036       if (TYPE_REF_P (valtype)
10037 	  && same_type_ignoring_top_level_qualifiers_p
10038 	      (TREE_TYPE (valtype), TREE_TYPE (current_class_ref)))
10039 	{
10040 	  /* Returning '*this' is obviously OK.  */
10041 	  if (retval == current_class_ref)
10042 	    warn = false;
10043 	  /* If we are calling a function whose return type is the same of
10044 	     the current class reference, it is ok.  */
10045 	  else if (INDIRECT_REF_P (retval)
10046 		   && TREE_CODE (TREE_OPERAND (retval, 0)) == CALL_EXPR)
10047 	    warn = false;
10048 	}
10049 
10050       if (warn)
10051 	warning_at (loc, OPT_Weffc__,
10052 		    "%<operator=%> should return a reference to %<*this%>");
10053     }
10054 
10055   if (dependent_type_p (functype)
10056       || type_dependent_expression_p (retval))
10057     {
10058     dependent:
10059       /* We should not have changed the return value.  */
10060       gcc_assert (retval == saved_retval);
10061       /* We don't know if this is an lvalue or rvalue use, but
10062 	 either way we can mark it as read.  */
10063       mark_exp_read (retval);
10064       return retval;
10065     }
10066 
10067   /* The fabled Named Return Value optimization, as per [class.copy]/15:
10068 
10069      [...]      For  a function with a class return type, if the expression
10070      in the return statement is the name of a local  object,  and  the  cv-
10071      unqualified  type  of  the  local  object  is the same as the function
10072      return type, an implementation is permitted to omit creating the  tem-
10073      porary  object  to  hold  the function return value [...]
10074 
10075      So, if this is a value-returning function that always returns the same
10076      local variable, remember it.
10077 
10078      It might be nice to be more flexible, and choose the first suitable
10079      variable even if the function sometimes returns something else, but
10080      then we run the risk of clobbering the variable we chose if the other
10081      returned expression uses the chosen variable somehow.  And people expect
10082      this restriction, anyway.  (jason 2000-11-19)
10083 
10084      See finish_function and finalize_nrv for the rest of this optimization.  */
10085   if (retval)
10086     STRIP_ANY_LOCATION_WRAPPER (retval);
10087 
10088   bool named_return_value_okay_p = can_do_nrvo_p (retval, functype);
10089   if (fn_returns_value_p && flag_elide_constructors)
10090     {
10091       if (named_return_value_okay_p
10092           && (current_function_return_value == NULL_TREE
10093               || current_function_return_value == retval))
10094 	current_function_return_value = retval;
10095       else
10096 	current_function_return_value = error_mark_node;
10097     }
10098 
10099   /* We don't need to do any conversions when there's nothing being
10100      returned.  */
10101   if (!retval)
10102     return NULL_TREE;
10103 
10104   if (!named_return_value_okay_p)
10105     maybe_warn_pessimizing_move (retval, functype);
10106 
10107   /* Do any required conversions.  */
10108   if (retval == result || DECL_CONSTRUCTOR_P (current_function_decl))
10109     /* No conversions are required.  */
10110     ;
10111   else
10112     {
10113       int flags = LOOKUP_NORMAL | LOOKUP_ONLYCONVERTING;
10114 
10115       /* The functype's return type will have been set to void, if it
10116 	 was an incomplete type.  Just treat this as 'return;' */
10117       if (VOID_TYPE_P (functype))
10118 	return error_mark_node;
10119 
10120       /* If we had an id-expression obfuscated by force_paren_expr, we need
10121 	 to undo it so we can try to treat it as an rvalue below.  */
10122       retval = maybe_undo_parenthesized_ref (retval);
10123 
10124       if (processing_template_decl)
10125 	retval = build_non_dependent_expr (retval);
10126 
10127       /* Under C++11 [12.8/32 class.copy], a returned lvalue is sometimes
10128 	 treated as an rvalue for the purposes of overload resolution to
10129 	 favor move constructors over copy constructors.
10130 
10131          Note that these conditions are similar to, but not as strict as,
10132 	 the conditions for the named return value optimization.  */
10133       bool converted = false;
10134       if (treat_lvalue_as_rvalue_p (retval, /*parm_ok*/true)
10135 	  /* This is only interesting for class type.  */
10136 	  && CLASS_TYPE_P (functype))
10137 	{
10138 	  tree moved = move (retval);
10139 	  moved = convert_for_initialization
10140 	    (NULL_TREE, functype, moved, flags|LOOKUP_PREFER_RVALUE,
10141 	     ICR_RETURN, NULL_TREE, 0, tf_none);
10142 	  if (moved != error_mark_node)
10143 	    {
10144 	      retval = moved;
10145 	      converted = true;
10146 	    }
10147 	}
10148 
10149       /* The call in a (lambda) thunk needs no conversions.  */
10150       if (TREE_CODE (retval) == CALL_EXPR
10151 	  && CALL_FROM_THUNK_P (retval))
10152 	converted = true;
10153 
10154       /* First convert the value to the function's return type, then
10155 	 to the type of return value's location to handle the
10156 	 case that functype is smaller than the valtype.  */
10157       if (!converted)
10158 	retval = convert_for_initialization
10159 	  (NULL_TREE, functype, retval, flags, ICR_RETURN, NULL_TREE, 0,
10160 	   tf_warning_or_error);
10161       retval = convert (valtype, retval);
10162 
10163       /* If the conversion failed, treat this just like `return;'.  */
10164       if (retval == error_mark_node)
10165 	return retval;
10166       /* We can't initialize a register from a AGGR_INIT_EXPR.  */
10167       else if (! cfun->returns_struct
10168 	       && TREE_CODE (retval) == TARGET_EXPR
10169 	       && TREE_CODE (TREE_OPERAND (retval, 1)) == AGGR_INIT_EXPR)
10170 	retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
10171 			 TREE_OPERAND (retval, 0));
10172       else if (!processing_template_decl
10173 	       && maybe_warn_about_returning_address_of_local (retval, loc)
10174 	       && INDIRECT_TYPE_P (valtype))
10175 	retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
10176 			 build_zero_cst (TREE_TYPE (retval)));
10177     }
10178 
10179   if (processing_template_decl)
10180     return saved_retval;
10181 
10182   /* Actually copy the value returned into the appropriate location.  */
10183   if (retval && retval != result)
10184     retval = build2 (INIT_EXPR, TREE_TYPE (result), result, retval);
10185 
10186   if (tree set = maybe_set_retval_sentinel ())
10187     retval = build2 (COMPOUND_EXPR, void_type_node, retval, set);
10188 
10189   return retval;
10190 }
10191 
10192 
10193 /* Returns nonzero if the pointer-type FROM can be converted to the
10194    pointer-type TO via a qualification conversion.  If CONSTP is -1,
10195    then we return nonzero if the pointers are similar, and the
10196    cv-qualification signature of FROM is a proper subset of that of TO.
10197 
10198    If CONSTP is positive, then all outer pointers have been
10199    const-qualified.  */
10200 
10201 static bool
comp_ptr_ttypes_real(tree to,tree from,int constp)10202 comp_ptr_ttypes_real (tree to, tree from, int constp)
10203 {
10204   bool to_more_cv_qualified = false;
10205   bool is_opaque_pointer = false;
10206 
10207   for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
10208     {
10209       if (TREE_CODE (to) != TREE_CODE (from))
10210 	return false;
10211 
10212       if (TREE_CODE (from) == OFFSET_TYPE
10213 	  && !same_type_p (TYPE_OFFSET_BASETYPE (from),
10214 			   TYPE_OFFSET_BASETYPE (to)))
10215 	return false;
10216 
10217       /* Const and volatile mean something different for function and
10218 	 array types, so the usual checks are not appropriate.  We'll
10219 	 check the array type elements in further iterations.  */
10220       if (!FUNC_OR_METHOD_TYPE_P (to) && TREE_CODE (to) != ARRAY_TYPE)
10221 	{
10222 	  if (!at_least_as_qualified_p (to, from))
10223 	    return false;
10224 
10225 	  if (!at_least_as_qualified_p (from, to))
10226 	    {
10227 	      if (constp == 0)
10228 		return false;
10229 	      to_more_cv_qualified = true;
10230 	    }
10231 
10232 	  if (constp > 0)
10233 	    constp &= TYPE_READONLY (to);
10234 	}
10235 
10236       if (VECTOR_TYPE_P (to))
10237 	is_opaque_pointer = vector_targets_convertible_p (to, from);
10238 
10239       /* P0388R4 allows a conversion from int[N] to int[] but not the
10240 	 other way round.  When both arrays have bounds but they do
10241 	 not match, then no conversion is possible.  */
10242       if (TREE_CODE (to) == ARRAY_TYPE
10243 	  && !comp_array_types (to, from, bounds_first, /*strict=*/false))
10244 	return false;
10245 
10246       if (!TYPE_PTR_P (to)
10247 	  && !TYPE_PTRDATAMEM_P (to)
10248 	  /* CWG 330 says we need to look through arrays.  */
10249 	  && TREE_CODE (to) != ARRAY_TYPE)
10250 	return ((constp >= 0 || to_more_cv_qualified)
10251 		&& (is_opaque_pointer
10252 		    || same_type_ignoring_top_level_qualifiers_p (to, from)));
10253     }
10254 }
10255 
10256 /* When comparing, say, char ** to char const **, this function takes
10257    the 'char *' and 'char const *'.  Do not pass non-pointer/reference
10258    types to this function.  */
10259 
10260 int
comp_ptr_ttypes(tree to,tree from)10261 comp_ptr_ttypes (tree to, tree from)
10262 {
10263   return comp_ptr_ttypes_real (to, from, 1);
10264 }
10265 
10266 /* Returns true iff FNTYPE is a non-class type that involves
10267    error_mark_node.  We can get FUNCTION_TYPE with buried error_mark_node
10268    if a parameter type is ill-formed.  */
10269 
10270 bool
error_type_p(const_tree type)10271 error_type_p (const_tree type)
10272 {
10273   tree t;
10274 
10275   switch (TREE_CODE (type))
10276     {
10277     case ERROR_MARK:
10278       return true;
10279 
10280     case POINTER_TYPE:
10281     case REFERENCE_TYPE:
10282     case OFFSET_TYPE:
10283       return error_type_p (TREE_TYPE (type));
10284 
10285     case FUNCTION_TYPE:
10286     case METHOD_TYPE:
10287       if (error_type_p (TREE_TYPE (type)))
10288 	return true;
10289       for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
10290 	if (error_type_p (TREE_VALUE (t)))
10291 	  return true;
10292       return false;
10293 
10294     case RECORD_TYPE:
10295       if (TYPE_PTRMEMFUNC_P (type))
10296 	return error_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type));
10297       return false;
10298 
10299     default:
10300       return false;
10301     }
10302 }
10303 
10304 /* Returns true if to and from are (possibly multi-level) pointers to the same
10305    type or inheritance-related types, regardless of cv-quals.  */
10306 
10307 bool
ptr_reasonably_similar(const_tree to,const_tree from)10308 ptr_reasonably_similar (const_tree to, const_tree from)
10309 {
10310   for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
10311     {
10312       /* Any target type is similar enough to void.  */
10313       if (VOID_TYPE_P (to))
10314 	return !error_type_p (from);
10315       if (VOID_TYPE_P (from))
10316 	return !error_type_p (to);
10317 
10318       if (TREE_CODE (to) != TREE_CODE (from))
10319 	return false;
10320 
10321       if (TREE_CODE (from) == OFFSET_TYPE
10322 	  && comptypes (TYPE_OFFSET_BASETYPE (to),
10323 			TYPE_OFFSET_BASETYPE (from),
10324 			COMPARE_BASE | COMPARE_DERIVED))
10325 	continue;
10326 
10327       if (VECTOR_TYPE_P (to)
10328 	  && vector_types_convertible_p (to, from, false))
10329 	return true;
10330 
10331       if (TREE_CODE (to) == INTEGER_TYPE
10332 	  && TYPE_PRECISION (to) == TYPE_PRECISION (from))
10333 	return true;
10334 
10335       if (TREE_CODE (to) == FUNCTION_TYPE)
10336 	return !error_type_p (to) && !error_type_p (from);
10337 
10338       if (!TYPE_PTR_P (to))
10339 	{
10340 	  /* When either type is incomplete avoid DERIVED_FROM_P,
10341 	     which may call complete_type (c++/57942).  */
10342 	  bool b = !COMPLETE_TYPE_P (to) || !COMPLETE_TYPE_P (from);
10343 	  return comptypes
10344 	    (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from),
10345 	     b ? COMPARE_STRICT : COMPARE_BASE | COMPARE_DERIVED);
10346 	}
10347     }
10348 }
10349 
10350 /* Return true if TO and FROM (both of which are POINTER_TYPEs or
10351    pointer-to-member types) are the same, ignoring cv-qualification at
10352    all levels.  CB says how we should behave when comparing array bounds.  */
10353 
10354 bool
comp_ptr_ttypes_const(tree to,tree from,compare_bounds_t cb)10355 comp_ptr_ttypes_const (tree to, tree from, compare_bounds_t cb)
10356 {
10357   bool is_opaque_pointer = false;
10358 
10359   for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
10360     {
10361       if (TREE_CODE (to) != TREE_CODE (from))
10362 	return false;
10363 
10364       if (TREE_CODE (from) == OFFSET_TYPE
10365 	  && same_type_p (TYPE_OFFSET_BASETYPE (from),
10366 			  TYPE_OFFSET_BASETYPE (to)))
10367 	  continue;
10368 
10369       if (VECTOR_TYPE_P (to))
10370 	is_opaque_pointer = vector_targets_convertible_p (to, from);
10371 
10372       if (TREE_CODE (to) == ARRAY_TYPE
10373 	  /* Ignore cv-qualification, but if we see e.g. int[3] and int[4],
10374 	     we must fail.  */
10375 	  && !comp_array_types (to, from, cb, /*strict=*/false))
10376 	return false;
10377 
10378       /* CWG 330 says we need to look through arrays.  */
10379       if (!TYPE_PTR_P (to) && TREE_CODE (to) != ARRAY_TYPE)
10380 	return (is_opaque_pointer
10381 		|| same_type_ignoring_top_level_qualifiers_p (to, from));
10382     }
10383 }
10384 
10385 /* Returns the type qualifiers for this type, including the qualifiers on the
10386    elements for an array type.  */
10387 
10388 int
cp_type_quals(const_tree type)10389 cp_type_quals (const_tree type)
10390 {
10391   int quals;
10392   /* This CONST_CAST is okay because strip_array_types returns its
10393      argument unmodified and we assign it to a const_tree.  */
10394   type = strip_array_types (CONST_CAST_TREE (type));
10395   if (type == error_mark_node
10396       /* Quals on a FUNCTION_TYPE are memfn quals.  */
10397       || TREE_CODE (type) == FUNCTION_TYPE)
10398     return TYPE_UNQUALIFIED;
10399   quals = TYPE_QUALS (type);
10400   /* METHOD and REFERENCE_TYPEs should never have quals.  */
10401   gcc_assert ((TREE_CODE (type) != METHOD_TYPE
10402 	       && !TYPE_REF_P (type))
10403 	      || ((quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE))
10404 		  == TYPE_UNQUALIFIED));
10405   return quals;
10406 }
10407 
10408 /* Returns the function-ref-qualifier for TYPE */
10409 
10410 cp_ref_qualifier
type_memfn_rqual(const_tree type)10411 type_memfn_rqual (const_tree type)
10412 {
10413   gcc_assert (FUNC_OR_METHOD_TYPE_P (type));
10414 
10415   if (!FUNCTION_REF_QUALIFIED (type))
10416     return REF_QUAL_NONE;
10417   else if (FUNCTION_RVALUE_QUALIFIED (type))
10418     return REF_QUAL_RVALUE;
10419   else
10420     return REF_QUAL_LVALUE;
10421 }
10422 
10423 /* Returns the function-cv-quals for TYPE, which must be a FUNCTION_TYPE or
10424    METHOD_TYPE.  */
10425 
10426 int
type_memfn_quals(const_tree type)10427 type_memfn_quals (const_tree type)
10428 {
10429   if (TREE_CODE (type) == FUNCTION_TYPE)
10430     return TYPE_QUALS (type);
10431   else if (TREE_CODE (type) == METHOD_TYPE)
10432     return cp_type_quals (class_of_this_parm (type));
10433   else
10434     gcc_unreachable ();
10435 }
10436 
10437 /* Returns the FUNCTION_TYPE TYPE with its function-cv-quals changed to
10438    MEMFN_QUALS and its ref-qualifier to RQUAL. */
10439 
10440 tree
apply_memfn_quals(tree type,cp_cv_quals memfn_quals,cp_ref_qualifier rqual)10441 apply_memfn_quals (tree type, cp_cv_quals memfn_quals, cp_ref_qualifier rqual)
10442 {
10443   /* Could handle METHOD_TYPE here if necessary.  */
10444   gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
10445   if (TYPE_QUALS (type) == memfn_quals
10446       && type_memfn_rqual (type) == rqual)
10447     return type;
10448 
10449   /* This should really have a different TYPE_MAIN_VARIANT, but that gets
10450      complex.  */
10451   tree result = build_qualified_type (type, memfn_quals);
10452   return build_ref_qualified_type (result, rqual);
10453 }
10454 
10455 /* Returns nonzero if TYPE is const or volatile.  */
10456 
10457 bool
cv_qualified_p(const_tree type)10458 cv_qualified_p (const_tree type)
10459 {
10460   int quals = cp_type_quals (type);
10461   return (quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)) != 0;
10462 }
10463 
10464 /* Returns nonzero if the TYPE contains a mutable member.  */
10465 
10466 bool
cp_has_mutable_p(const_tree type)10467 cp_has_mutable_p (const_tree type)
10468 {
10469   /* This CONST_CAST is okay because strip_array_types returns its
10470      argument unmodified and we assign it to a const_tree.  */
10471   type = strip_array_types (CONST_CAST_TREE(type));
10472 
10473   return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type);
10474 }
10475 
10476 /* Set TREE_READONLY and TREE_VOLATILE on DECL as indicated by the
10477    TYPE_QUALS.  For a VAR_DECL, this may be an optimistic
10478    approximation.  In particular, consider:
10479 
10480      int f();
10481      struct S { int i; };
10482      const S s = { f(); }
10483 
10484    Here, we will make "s" as TREE_READONLY (because it is declared
10485    "const") -- only to reverse ourselves upon seeing that the
10486    initializer is non-constant.  */
10487 
10488 void
cp_apply_type_quals_to_decl(int type_quals,tree decl)10489 cp_apply_type_quals_to_decl (int type_quals, tree decl)
10490 {
10491   tree type = TREE_TYPE (decl);
10492 
10493   if (type == error_mark_node)
10494     return;
10495 
10496   if (TREE_CODE (decl) == TYPE_DECL)
10497     return;
10498 
10499   gcc_assert (!(TREE_CODE (type) == FUNCTION_TYPE
10500 		&& type_quals != TYPE_UNQUALIFIED));
10501 
10502   /* Avoid setting TREE_READONLY incorrectly.  */
10503   /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
10504      constructor can produce constant init, so rely on cp_finish_decl to
10505      clear TREE_READONLY if the variable has non-constant init.  */
10506 
10507   /* If the type has (or might have) a mutable component, that component
10508      might be modified.  */
10509   if (TYPE_HAS_MUTABLE_P (type) || !COMPLETE_TYPE_P (type))
10510     type_quals &= ~TYPE_QUAL_CONST;
10511 
10512   c_apply_type_quals_to_decl (type_quals, decl);
10513 }
10514 
10515 /* Subroutine of casts_away_constness.  Make T1 and T2 point at
10516    exemplar types such that casting T1 to T2 is casting away constness
10517    if and only if there is no implicit conversion from T1 to T2.  */
10518 
10519 static void
casts_away_constness_r(tree * t1,tree * t2,tsubst_flags_t complain)10520 casts_away_constness_r (tree *t1, tree *t2, tsubst_flags_t complain)
10521 {
10522   int quals1;
10523   int quals2;
10524 
10525   /* [expr.const.cast]
10526 
10527      For multi-level pointer to members and multi-level mixed pointers
10528      and pointers to members (conv.qual), the "member" aspect of a
10529      pointer to member level is ignored when determining if a const
10530      cv-qualifier has been cast away.  */
10531   /* [expr.const.cast]
10532 
10533      For  two  pointer types:
10534 
10535 	    X1 is T1cv1,1 * ... cv1,N *   where T1 is not a pointer type
10536 	    X2 is T2cv2,1 * ... cv2,M *   where T2 is not a pointer type
10537 	    K is min(N,M)
10538 
10539      casting from X1 to X2 casts away constness if, for a non-pointer
10540      type T there does not exist an implicit conversion (clause
10541      _conv_) from:
10542 
10543 	    Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
10544 
10545      to
10546 
10547 	    Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *.  */
10548   if ((!TYPE_PTR_P (*t1) && !TYPE_PTRDATAMEM_P (*t1))
10549       || (!TYPE_PTR_P (*t2) && !TYPE_PTRDATAMEM_P (*t2)))
10550     {
10551       *t1 = cp_build_qualified_type (void_type_node,
10552 				     cp_type_quals (*t1));
10553       *t2 = cp_build_qualified_type (void_type_node,
10554 				     cp_type_quals (*t2));
10555       return;
10556     }
10557 
10558   quals1 = cp_type_quals (*t1);
10559   quals2 = cp_type_quals (*t2);
10560 
10561   if (TYPE_PTRDATAMEM_P (*t1))
10562     *t1 = TYPE_PTRMEM_POINTED_TO_TYPE (*t1);
10563   else
10564     *t1 = TREE_TYPE (*t1);
10565   if (TYPE_PTRDATAMEM_P (*t2))
10566     *t2 = TYPE_PTRMEM_POINTED_TO_TYPE (*t2);
10567   else
10568     *t2 = TREE_TYPE (*t2);
10569 
10570   casts_away_constness_r (t1, t2, complain);
10571   *t1 = build_pointer_type (*t1);
10572   *t2 = build_pointer_type (*t2);
10573   *t1 = cp_build_qualified_type (*t1, quals1);
10574   *t2 = cp_build_qualified_type (*t2, quals2);
10575 }
10576 
10577 /* Returns nonzero if casting from TYPE1 to TYPE2 casts away
10578    constness.
10579 
10580    ??? This function returns non-zero if casting away qualifiers not
10581    just const.  We would like to return to the caller exactly which
10582    qualifiers are casted away to give more accurate diagnostics.
10583 */
10584 
10585 static bool
casts_away_constness(tree t1,tree t2,tsubst_flags_t complain)10586 casts_away_constness (tree t1, tree t2, tsubst_flags_t complain)
10587 {
10588   if (TYPE_REF_P (t2))
10589     {
10590       /* [expr.const.cast]
10591 
10592 	 Casting from an lvalue of type T1 to an lvalue of type T2
10593 	 using a reference cast casts away constness if a cast from an
10594 	 rvalue of type "pointer to T1" to the type "pointer to T2"
10595 	 casts away constness.  */
10596       t1 = (TYPE_REF_P (t1) ? TREE_TYPE (t1) : t1);
10597       return casts_away_constness (build_pointer_type (t1),
10598 				   build_pointer_type (TREE_TYPE (t2)),
10599 				   complain);
10600     }
10601 
10602   if (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
10603     /* [expr.const.cast]
10604 
10605        Casting from an rvalue of type "pointer to data member of X
10606        of type T1" to the type "pointer to data member of Y of type
10607        T2" casts away constness if a cast from an rvalue of type
10608        "pointer to T1" to the type "pointer to T2" casts away
10609        constness.  */
10610     return casts_away_constness
10611       (build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t1)),
10612        build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t2)),
10613        complain);
10614 
10615   /* Casting away constness is only something that makes sense for
10616      pointer or reference types.  */
10617   if (!TYPE_PTR_P (t1) || !TYPE_PTR_P (t2))
10618     return false;
10619 
10620   /* Top-level qualifiers don't matter.  */
10621   t1 = TYPE_MAIN_VARIANT (t1);
10622   t2 = TYPE_MAIN_VARIANT (t2);
10623   casts_away_constness_r (&t1, &t2, complain);
10624   if (!can_convert (t2, t1, complain))
10625     return true;
10626 
10627   return false;
10628 }
10629 
10630 /* If T is a REFERENCE_TYPE return the type to which T refers.
10631    Otherwise, return T itself.  */
10632 
10633 tree
non_reference(tree t)10634 non_reference (tree t)
10635 {
10636   if (t && TYPE_REF_P (t))
10637     t = TREE_TYPE (t);
10638   return t;
10639 }
10640 
10641 
10642 /* Return nonzero if REF is an lvalue valid for this language;
10643    otherwise, print an error message and return zero.  USE says
10644    how the lvalue is being used and so selects the error message.  */
10645 
10646 int
lvalue_or_else(tree ref,enum lvalue_use use,tsubst_flags_t complain)10647 lvalue_or_else (tree ref, enum lvalue_use use, tsubst_flags_t complain)
10648 {
10649   cp_lvalue_kind kind = lvalue_kind (ref);
10650 
10651   if (kind == clk_none)
10652     {
10653       if (complain & tf_error)
10654 	lvalue_error (cp_expr_loc_or_input_loc (ref), use);
10655       return 0;
10656     }
10657   else if (kind & (clk_rvalueref|clk_class))
10658     {
10659       if (!(complain & tf_error))
10660 	return 0;
10661       /* Make this a permerror because we used to accept it.  */
10662       permerror (cp_expr_loc_or_input_loc (ref),
10663 		 "using rvalue as lvalue");
10664     }
10665   return 1;
10666 }
10667 
10668 /* Return true if a user-defined literal operator is a raw operator.  */
10669 
10670 bool
check_raw_literal_operator(const_tree decl)10671 check_raw_literal_operator (const_tree decl)
10672 {
10673   tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
10674   tree argtype;
10675   int arity;
10676   bool maybe_raw_p = false;
10677 
10678   /* Count the number and type of arguments and check for ellipsis.  */
10679   for (argtype = argtypes, arity = 0;
10680        argtype && argtype != void_list_node;
10681        ++arity, argtype = TREE_CHAIN (argtype))
10682     {
10683       tree t = TREE_VALUE (argtype);
10684 
10685       if (same_type_p (t, const_string_type_node))
10686 	maybe_raw_p = true;
10687     }
10688   if (!argtype)
10689     return false; /* Found ellipsis.  */
10690 
10691   if (!maybe_raw_p || arity != 1)
10692     return false;
10693 
10694   return true;
10695 }
10696 
10697 
10698 /* Return true if a user-defined literal operator has one of the allowed
10699    argument types.  */
10700 
10701 bool
check_literal_operator_args(const_tree decl,bool * long_long_unsigned_p,bool * long_double_p)10702 check_literal_operator_args (const_tree decl,
10703 			     bool *long_long_unsigned_p, bool *long_double_p)
10704 {
10705   tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
10706 
10707   *long_long_unsigned_p = false;
10708   *long_double_p = false;
10709   if (processing_template_decl || processing_specialization)
10710     return argtypes == void_list_node;
10711   else
10712     {
10713       tree argtype;
10714       int arity;
10715       int max_arity = 2;
10716 
10717       /* Count the number and type of arguments and check for ellipsis.  */
10718       for (argtype = argtypes, arity = 0;
10719 	   argtype && argtype != void_list_node;
10720 	   argtype = TREE_CHAIN (argtype))
10721 	{
10722 	  tree t = TREE_VALUE (argtype);
10723 	  ++arity;
10724 
10725 	  if (TYPE_PTR_P (t))
10726 	    {
10727 	      bool maybe_raw_p = false;
10728 	      t = TREE_TYPE (t);
10729 	      if (cp_type_quals (t) != TYPE_QUAL_CONST)
10730 		return false;
10731 	      t = TYPE_MAIN_VARIANT (t);
10732 	      if ((maybe_raw_p = same_type_p (t, char_type_node))
10733 		  || same_type_p (t, wchar_type_node)
10734 		  || same_type_p (t, char8_type_node)
10735 		  || same_type_p (t, char16_type_node)
10736 		  || same_type_p (t, char32_type_node))
10737 		{
10738 		  argtype = TREE_CHAIN (argtype);
10739 		  if (!argtype)
10740 		    return false;
10741 		  t = TREE_VALUE (argtype);
10742 		  if (maybe_raw_p && argtype == void_list_node)
10743 		    return true;
10744 		  else if (same_type_p (t, size_type_node))
10745 		    {
10746 		      ++arity;
10747 		      continue;
10748 		    }
10749 		  else
10750 		    return false;
10751 		}
10752 	    }
10753 	  else if (same_type_p (t, long_long_unsigned_type_node))
10754 	    {
10755 	      max_arity = 1;
10756 	      *long_long_unsigned_p = true;
10757 	    }
10758 	  else if (same_type_p (t, long_double_type_node))
10759 	    {
10760 	      max_arity = 1;
10761 	      *long_double_p = true;
10762 	    }
10763 	  else if (same_type_p (t, char_type_node))
10764 	    max_arity = 1;
10765 	  else if (same_type_p (t, wchar_type_node))
10766 	    max_arity = 1;
10767 	  else if (same_type_p (t, char8_type_node))
10768 	    max_arity = 1;
10769 	  else if (same_type_p (t, char16_type_node))
10770 	    max_arity = 1;
10771 	  else if (same_type_p (t, char32_type_node))
10772 	    max_arity = 1;
10773 	  else
10774 	    return false;
10775 	}
10776       if (!argtype)
10777 	return false; /* Found ellipsis.  */
10778 
10779       if (arity != max_arity)
10780 	return false;
10781 
10782       return true;
10783     }
10784 }
10785 
10786 /* Always returns false since unlike C90, C++ has no concept of implicit
10787    function declarations.  */
10788 
10789 bool
c_decl_implicit(const_tree)10790 c_decl_implicit (const_tree)
10791 {
10792   return false;
10793 }
10794