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