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