118334Speter/* Build expressions with type checking for C++ compiler.
290075Sobrien   Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3146895Skan   1999, 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
418334Speter   Hacked by Michael Tiemann (tiemann@cygnus.com)
518334Speter
6132718SkanThis file is part of GCC.
718334Speter
8132718SkanGCC is free software; you can redistribute it and/or modify
918334Speterit under the terms of the GNU General Public License as published by
1018334Speterthe Free Software Foundation; either version 2, or (at your option)
1118334Speterany later version.
1218334Speter
13132718SkanGCC is distributed in the hope that it will be useful,
1418334Speterbut WITHOUT ANY WARRANTY; without even the implied warranty of
1518334SpeterMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1618334SpeterGNU General Public License for more details.
1718334Speter
1818334SpeterYou should have received a copy of the GNU General Public License
19132718Skanalong with GCC; see the file COPYING.  If not, write to
20169689Skanthe Free Software Foundation, 51 Franklin Street, Fifth Floor,
21169689SkanBoston, MA 02110-1301, USA.  */
2218334Speter
2318334Speter
2418334Speter/* This file is part of the C++ front end.
2518334Speter   It contains routines to build C++ expressions given their operands,
2618334Speter   including computing the types of the result, C and C++ specific error
27169689Skan   checks, and some optimization.  */
2818334Speter
2918334Speter#include "config.h"
3050397Sobrien#include "system.h"
31132718Skan#include "coretypes.h"
32132718Skan#include "tm.h"
3318334Speter#include "tree.h"
3418334Speter#include "rtl.h"
3590075Sobrien#include "expr.h"
3618334Speter#include "cp-tree.h"
3790075Sobrien#include "tm_p.h"
3818334Speter#include "flags.h"
3918334Speter#include "output.h"
4050397Sobrien#include "toplev.h"
4190075Sobrien#include "diagnostic.h"
4290075Sobrien#include "target.h"
43132718Skan#include "convert.h"
44169689Skan#include "c-common.h"
4518334Speter
46169689Skanstatic tree pfn_from_ptrmemfunc (tree);
47132718Skanstatic tree convert_for_assignment (tree, tree, const char *, tree, int);
48132718Skanstatic tree cp_pointer_int_sum (enum tree_code, tree, tree);
49132718Skanstatic tree rationalize_conditional_expr (enum tree_code, tree);
50132718Skanstatic int comp_ptr_ttypes_real (tree, tree, int);
51132718Skanstatic bool comp_except_types (tree, tree, bool);
52132718Skanstatic bool comp_array_types (tree, tree, bool);
53132718Skanstatic tree common_base_type (tree, tree);
54132718Skanstatic tree pointer_diff (tree, tree, tree);
55169689Skanstatic tree get_delta_difference (tree, tree, bool, bool);
56132718Skanstatic void casts_away_constness_r (tree *, tree *);
57132718Skanstatic bool casts_away_constness (tree, tree);
58132718Skanstatic void maybe_warn_about_returning_address_of_local (tree);
59132718Skanstatic tree lookup_destructor (tree, tree, tree);
60260311Spfg/* APPLE LOCAL radar 6087117 */
61260311Spfgstatic tree convert_arguments (tree, tree, tree, int, int);
6218334Speter
6318334Speter/* Do `exp = require_complete_type (exp);' to make sure exp
6452284Sobrien   does not have an incomplete type.  (That includes void types.)
6552284Sobrien   Returns the error_mark_node if the VALUE does not have
6652284Sobrien   complete type when this function returns.  */
6718334Speter
6818334Spetertree
69132718Skanrequire_complete_type (tree value)
7018334Speter{
7150397Sobrien  tree type;
7218334Speter
7352284Sobrien  if (processing_template_decl || value == error_mark_node)
7450397Sobrien    return value;
7550397Sobrien
7650397Sobrien  if (TREE_CODE (value) == OVERLOAD)
7750397Sobrien    type = unknown_type_node;
7850397Sobrien  else
7950397Sobrien    type = TREE_TYPE (value);
8050397Sobrien
81161651Skan  if (type == error_mark_node)
82161651Skan    return error_mark_node;
83161651Skan
8418334Speter  /* First, detect a valid value with a complete type.  */
8590075Sobrien  if (COMPLETE_TYPE_P (type))
8618334Speter    return value;
8718334Speter
8852284Sobrien  if (complete_type_or_else (type, value))
8950397Sobrien    return value;
9050397Sobrien  else
9152284Sobrien    return error_mark_node;
9252284Sobrien}
9352284Sobrien
9450397Sobrien/* Try to complete TYPE, if it is incomplete.  For example, if TYPE is
9550397Sobrien   a template instantiation, do the instantiation.  Returns TYPE,
9650397Sobrien   whether or not it could be completed, unless something goes
9750397Sobrien   horribly wrong, in which case the error_mark_node is returned.  */
9850397Sobrien
9950397Sobrientree
100132718Skancomplete_type (tree type)
10150397Sobrien{
10250397Sobrien  if (type == NULL_TREE)
10350397Sobrien    /* Rather than crash, we return something sure to cause an error
10450397Sobrien       at some point.  */
10550397Sobrien    return error_mark_node;
10650397Sobrien
10790075Sobrien  if (type == error_mark_node || COMPLETE_TYPE_P (type))
10850397Sobrien    ;
10950397Sobrien  else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
11050397Sobrien    {
11150397Sobrien      tree t = complete_type (TREE_TYPE (type));
112169689Skan      unsigned int needs_constructing, has_nontrivial_dtor;
113132718Skan      if (COMPLETE_TYPE_P (t) && !dependent_type_p (type))
11450397Sobrien	layout_type (type);
115169689Skan      needs_constructing
11650397Sobrien	= TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t));
117169689Skan      has_nontrivial_dtor
11890075Sobrien	= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t));
119169689Skan      for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
120169689Skan	{
121169689Skan	  TYPE_NEEDS_CONSTRUCTING (t) = needs_constructing;
122169689Skan	  TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = has_nontrivial_dtor;
123169689Skan	}
12450397Sobrien    }
12552284Sobrien  else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INSTANTIATION (type))
12650397Sobrien    instantiate_class_template (TYPE_MAIN_VARIANT (type));
12750397Sobrien
12850397Sobrien  return type;
12950397Sobrien}
13050397Sobrien
131117395Skan/* Like complete_type, but issue an error if the TYPE cannot be completed.
132169689Skan   VALUE is used for informative diagnostics.
13352284Sobrien   Returns NULL_TREE if the type cannot be made complete.  */
13450397Sobrien
13550397Sobrientree
136169689Skancomplete_type_or_else (tree type, tree value)
13750397Sobrien{
13850397Sobrien  type = complete_type (type);
13952284Sobrien  if (type == error_mark_node)
14052284Sobrien    /* We already issued an error.  */
14152284Sobrien    return NULL_TREE;
14290075Sobrien  else if (!COMPLETE_TYPE_P (type))
14350397Sobrien    {
144169689Skan      cxx_incomplete_type_diagnostic (value, type, 0);
14550397Sobrien      return NULL_TREE;
14650397Sobrien    }
14750397Sobrien  else
14850397Sobrien    return type;
14950397Sobrien}
15050397Sobrien
15118334Speter/* Return truthvalue of whether type of EXP is instantiated.  */
15250397Sobrien
15318334Speterint
154132718Skantype_unknown_p (tree exp)
15518334Speter{
156132718Skan  return (TREE_CODE (exp) == TREE_LIST
157132718Skan	  || TREE_TYPE (exp) == unknown_type_node);
15818334Speter}
15918334Speter
16018334Speter
16118334Speter/* Return the common type of two parameter lists.
16218334Speter   We assume that comptypes has already been done and returned 1;
16318334Speter   if that isn't so, this may crash.
16418334Speter
16518334Speter   As an optimization, free the space we allocate if the parameter
16618334Speter   lists are already common.  */
16718334Speter
168169689Skanstatic tree
169132718Skancommonparms (tree p1, tree p2)
17018334Speter{
17118334Speter  tree oldargs = p1, newargs, n;
17218334Speter  int i, len;
17318334Speter  int any_change = 0;
17418334Speter
17518334Speter  len = list_length (p1);
17618334Speter  newargs = tree_last (p1);
17718334Speter
17818334Speter  if (newargs == void_list_node)
17918334Speter    i = 1;
18018334Speter  else
18118334Speter    {
18218334Speter      i = 0;
18318334Speter      newargs = 0;
18418334Speter    }
18518334Speter
18618334Speter  for (; i < len; i++)
18718334Speter    newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
18818334Speter
18918334Speter  n = newargs;
19018334Speter
19118334Speter  for (i = 0; p1;
19218334Speter       p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++)
19318334Speter    {
19418334Speter      if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2))
19518334Speter	{
19618334Speter	  TREE_PURPOSE (n) = TREE_PURPOSE (p1);
19718334Speter	  any_change = 1;
19818334Speter	}
19918334Speter      else if (! TREE_PURPOSE (p1))
20018334Speter	{
20118334Speter	  if (TREE_PURPOSE (p2))
20218334Speter	    {
20318334Speter	      TREE_PURPOSE (n) = TREE_PURPOSE (p2);
20418334Speter	      any_change = 1;
20518334Speter	    }
20618334Speter	}
20718334Speter      else
20818334Speter	{
20918334Speter	  if (1 != simple_cst_equal (TREE_PURPOSE (p1), TREE_PURPOSE (p2)))
21018334Speter	    any_change = 1;
21118334Speter	  TREE_PURPOSE (n) = TREE_PURPOSE (p2);
21218334Speter	}
21318334Speter      if (TREE_VALUE (p1) != TREE_VALUE (p2))
21418334Speter	{
21518334Speter	  any_change = 1;
21696263Sobrien	  TREE_VALUE (n) = merge_types (TREE_VALUE (p1), TREE_VALUE (p2));
21718334Speter	}
21818334Speter      else
21918334Speter	TREE_VALUE (n) = TREE_VALUE (p1);
22018334Speter    }
22118334Speter  if (! any_change)
22290075Sobrien    return oldargs;
22318334Speter
22418334Speter  return newargs;
22518334Speter}
22618334Speter
22750397Sobrien/* Given a type, perhaps copied for a typedef,
22850397Sobrien   find the "original" version of it.  */
229169689Skanstatic tree
230132718Skanoriginal_type (tree t)
23150397Sobrien{
232169689Skan  int quals = cp_type_quals (t);
233169689Skan  while (t != error_mark_node
234169689Skan	 && TYPE_NAME (t) != NULL_TREE)
23550397Sobrien    {
23650397Sobrien      tree x = TYPE_NAME (t);
23750397Sobrien      if (TREE_CODE (x) != TYPE_DECL)
23850397Sobrien	break;
23950397Sobrien      x = DECL_ORIGINAL_TYPE (x);
24050397Sobrien      if (x == NULL_TREE)
24150397Sobrien	break;
24250397Sobrien      t = x;
24350397Sobrien    }
244169689Skan  return cp_build_qualified_type (t, quals);
24550397Sobrien}
24650397Sobrien
24790075Sobrien/* T1 and T2 are arithmetic or enumeration types.  Return the type
24890075Sobrien   that will result from the "usual arithmetic conversions" on T1 and
24990075Sobrien   T2 as described in [expr].  */
25090075Sobrien
25190075Sobrientree
252132718Skantype_after_usual_arithmetic_conversions (tree t1, tree t2)
25390075Sobrien{
25490075Sobrien  enum tree_code code1 = TREE_CODE (t1);
25590075Sobrien  enum tree_code code2 = TREE_CODE (t2);
25690075Sobrien  tree attributes;
25790075Sobrien
25890075Sobrien  /* FIXME: Attributes.  */
259169689Skan  gcc_assert (ARITHMETIC_TYPE_P (t1)
260169689Skan	      || TREE_CODE (t1) == VECTOR_TYPE
261169689Skan	      || TREE_CODE (t1) == ENUMERAL_TYPE);
262169689Skan  gcc_assert (ARITHMETIC_TYPE_P (t2)
263169689Skan	      || TREE_CODE (t2) == VECTOR_TYPE
264169689Skan	      || TREE_CODE (t2) == ENUMERAL_TYPE);
26590075Sobrien
26696263Sobrien  /* In what follows, we slightly generalize the rules given in [expr] so
26796263Sobrien     as to deal with `long long' and `complex'.  First, merge the
26896263Sobrien     attributes.  */
26990075Sobrien  attributes = (*targetm.merge_type_attributes) (t1, t2);
27090075Sobrien
27196263Sobrien  /* If one type is complex, form the common type of the non-complex
27296263Sobrien     components, then make that complex.  Use T1 or T2 if it is the
27396263Sobrien     required type.  */
27496263Sobrien  if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
27596263Sobrien    {
27696263Sobrien      tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
27796263Sobrien      tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
27896263Sobrien      tree subtype
27996263Sobrien	= type_after_usual_arithmetic_conversions (subtype1, subtype2);
28096263Sobrien
28196263Sobrien      if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
28296263Sobrien	return build_type_attribute_variant (t1, attributes);
28396263Sobrien      else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
28496263Sobrien	return build_type_attribute_variant (t2, attributes);
28596263Sobrien      else
28696263Sobrien	return build_type_attribute_variant (build_complex_type (subtype),
28796263Sobrien					     attributes);
28896263Sobrien    }
28996263Sobrien
290161651Skan  if (code1 == VECTOR_TYPE)
291161651Skan    {
292161651Skan      /* When we get here we should have two vectors of the same size.
293161651Skan	 Just prefer the unsigned one if present.  */
294169689Skan      if (TYPE_UNSIGNED (t1))
295161651Skan	return build_type_attribute_variant (t1, attributes);
296161651Skan      else
297161651Skan	return build_type_attribute_variant (t2, attributes);
298161651Skan    }
299161651Skan
30090075Sobrien  /* If only one is real, use it as the result.  */
30190075Sobrien  if (code1 == REAL_TYPE && code2 != REAL_TYPE)
30290075Sobrien    return build_type_attribute_variant (t1, attributes);
30390075Sobrien  if (code2 == REAL_TYPE && code1 != REAL_TYPE)
30490075Sobrien    return build_type_attribute_variant (t2, attributes);
30590075Sobrien
30690075Sobrien  /* Perform the integral promotions.  */
30790075Sobrien  if (code1 != REAL_TYPE)
30890075Sobrien    {
30990075Sobrien      t1 = type_promotes_to (t1);
31090075Sobrien      t2 = type_promotes_to (t2);
31190075Sobrien    }
31290075Sobrien
31390075Sobrien  /* Both real or both integers; use the one with greater precision.  */
31490075Sobrien  if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
31590075Sobrien    return build_type_attribute_variant (t1, attributes);
31690075Sobrien  else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
31790075Sobrien    return build_type_attribute_variant (t2, attributes);
31890075Sobrien
31996263Sobrien  /* The types are the same; no need to do anything fancy.  */
32096263Sobrien  if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
32196263Sobrien    return build_type_attribute_variant (t1, attributes);
32296263Sobrien
32390075Sobrien  if (code1 != REAL_TYPE)
32490075Sobrien    {
32590075Sobrien      /* If one is a sizetype, use it so size_binop doesn't blow up.  */
32690075Sobrien      if (TYPE_IS_SIZETYPE (t1) > TYPE_IS_SIZETYPE (t2))
32790075Sobrien	return build_type_attribute_variant (t1, attributes);
32890075Sobrien      if (TYPE_IS_SIZETYPE (t2) > TYPE_IS_SIZETYPE (t1))
32990075Sobrien	return build_type_attribute_variant (t2, attributes);
33090075Sobrien
33190075Sobrien      /* If one is unsigned long long, then convert the other to unsigned
33290075Sobrien	 long long.  */
33390075Sobrien      if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_unsigned_type_node)
33490075Sobrien	  || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_unsigned_type_node))
33590075Sobrien	return build_type_attribute_variant (long_long_unsigned_type_node,
33690075Sobrien					     attributes);
33790075Sobrien      /* If one is a long long, and the other is an unsigned long, and
33890075Sobrien	 long long can represent all the values of an unsigned long, then
33990075Sobrien	 convert to a long long.  Otherwise, convert to an unsigned long
34090075Sobrien	 long.  Otherwise, if either operand is long long, convert the
34190075Sobrien	 other to long long.
342169689Skan
34390075Sobrien	 Since we're here, we know the TYPE_PRECISION is the same;
34490075Sobrien	 therefore converting to long long cannot represent all the values
34590075Sobrien	 of an unsigned long, so we choose unsigned long long in that
34690075Sobrien	 case.  */
34790075Sobrien      if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_integer_type_node)
34890075Sobrien	  || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_integer_type_node))
34990075Sobrien	{
350169689Skan	  tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
351169689Skan		    ? long_long_unsigned_type_node
35290075Sobrien		    : long_long_integer_type_node);
35390075Sobrien	  return build_type_attribute_variant (t, attributes);
35490075Sobrien	}
355169689Skan
35690075Sobrien      /* Go through the same procedure, but for longs.  */
35790075Sobrien      if (same_type_p (TYPE_MAIN_VARIANT (t1), long_unsigned_type_node)
35890075Sobrien	  || same_type_p (TYPE_MAIN_VARIANT (t2), long_unsigned_type_node))
35990075Sobrien	return build_type_attribute_variant (long_unsigned_type_node,
36090075Sobrien					     attributes);
36190075Sobrien      if (same_type_p (TYPE_MAIN_VARIANT (t1), long_integer_type_node)
36290075Sobrien	  || same_type_p (TYPE_MAIN_VARIANT (t2), long_integer_type_node))
36390075Sobrien	{
364169689Skan	  tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
36590075Sobrien		    ? long_unsigned_type_node : long_integer_type_node);
36690075Sobrien	  return build_type_attribute_variant (t, attributes);
36790075Sobrien	}
36890075Sobrien      /* Otherwise prefer the unsigned one.  */
369169689Skan      if (TYPE_UNSIGNED (t1))
37090075Sobrien	return build_type_attribute_variant (t1, attributes);
37190075Sobrien      else
37290075Sobrien	return build_type_attribute_variant (t2, attributes);
37390075Sobrien    }
37490075Sobrien  else
37590075Sobrien    {
37690075Sobrien      if (same_type_p (TYPE_MAIN_VARIANT (t1), long_double_type_node)
37790075Sobrien	  || same_type_p (TYPE_MAIN_VARIANT (t2), long_double_type_node))
37890075Sobrien	return build_type_attribute_variant (long_double_type_node,
37990075Sobrien					     attributes);
38090075Sobrien      if (same_type_p (TYPE_MAIN_VARIANT (t1), double_type_node)
38190075Sobrien	  || same_type_p (TYPE_MAIN_VARIANT (t2), double_type_node))
38290075Sobrien	return build_type_attribute_variant (double_type_node,
38390075Sobrien					     attributes);
38496263Sobrien      if (same_type_p (TYPE_MAIN_VARIANT (t1), float_type_node)
38596263Sobrien	  || same_type_p (TYPE_MAIN_VARIANT (t2), float_type_node))
38690075Sobrien	return build_type_attribute_variant (float_type_node,
38790075Sobrien					     attributes);
388169689Skan
38996263Sobrien      /* Two floating-point types whose TYPE_MAIN_VARIANTs are none of
390169689Skan	 the standard C++ floating-point types.  Logic earlier in this
391169689Skan	 function has already eliminated the possibility that
392169689Skan	 TYPE_PRECISION (t2) != TYPE_PRECISION (t1), so there's no
393169689Skan	 compelling reason to choose one or the other.  */
39496263Sobrien      return build_type_attribute_variant (t1, attributes);
39590075Sobrien    }
39690075Sobrien}
39790075Sobrien
398132718Skan/* Subroutine of composite_pointer_type to implement the recursive
399132718Skan   case.  See that function for documentation fo the parameters.  */
400132718Skan
401132718Skanstatic tree
402132718Skancomposite_pointer_type_r (tree t1, tree t2, const char* location)
403132718Skan{
404132718Skan  tree pointee1;
405132718Skan  tree pointee2;
406132718Skan  tree result_type;
407132718Skan  tree attributes;
408132718Skan
409132718Skan  /* Determine the types pointed to by T1 and T2.  */
410260311Spfg  /* APPLE LOCAL blocks 6040305 */
411260311Spfg  if (TREE_CODE (t1) == POINTER_TYPE || TREE_CODE (t1) == BLOCK_POINTER_TYPE)
412132718Skan    {
413132718Skan      pointee1 = TREE_TYPE (t1);
414132718Skan      pointee2 = TREE_TYPE (t2);
415132718Skan    }
416132718Skan  else
417132718Skan    {
418132718Skan      pointee1 = TYPE_PTRMEM_POINTED_TO_TYPE (t1);
419132718Skan      pointee2 = TYPE_PTRMEM_POINTED_TO_TYPE (t2);
420132718Skan    }
421132718Skan
422132718Skan  /* [expr.rel]
423132718Skan
424132718Skan     Otherwise, the composite pointer type is a pointer type
425132718Skan     similar (_conv.qual_) to the type of one of the operands,
426132718Skan     with a cv-qualification signature (_conv.qual_) that is the
427132718Skan     union of the cv-qualification signatures of the operand
428132718Skan     types.  */
429132718Skan  if (same_type_ignoring_top_level_qualifiers_p (pointee1, pointee2))
430132718Skan    result_type = pointee1;
431132718Skan  else if ((TREE_CODE (pointee1) == POINTER_TYPE
432132718Skan	    && TREE_CODE (pointee2) == POINTER_TYPE)
433132718Skan	   || (TYPE_PTR_TO_MEMBER_P (pointee1)
434132718Skan	       && TYPE_PTR_TO_MEMBER_P (pointee2)))
435132718Skan    result_type = composite_pointer_type_r (pointee1, pointee2, location);
436132718Skan  else
437132718Skan    {
438169689Skan      pedwarn ("%s between distinct pointer types %qT and %qT "
439132718Skan	       "lacks a cast",
440132718Skan	       location, t1, t2);
441132718Skan      result_type = void_type_node;
442132718Skan    }
443132718Skan  result_type = cp_build_qualified_type (result_type,
444132718Skan					 (cp_type_quals (pointee1)
445132718Skan					  | cp_type_quals (pointee2)));
446132718Skan  /* If the original types were pointers to members, so is the
447132718Skan     result.  */
448132718Skan  if (TYPE_PTR_TO_MEMBER_P (t1))
449132718Skan    {
450132718Skan      if (!same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
451132718Skan			TYPE_PTRMEM_CLASS_TYPE (t2)))
452169689Skan	pedwarn ("%s between distinct pointer types %qT and %qT "
453132718Skan		 "lacks a cast",
454132718Skan		 location, t1, t2);
455132718Skan      result_type = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
456132718Skan				       result_type);
457132718Skan    }
458260311Spfg  /* APPLE LOCAL begin blocks 6065211 */
459260311Spfg  else if (TREE_CODE (t1) == BLOCK_POINTER_TYPE
460260311Spfg     && result_type != void_type_node)
461260311Spfg    result_type = build_block_pointer_type (result_type);
462132718Skan  else
463132718Skan    result_type = build_pointer_type (result_type);
464260311Spfg  /* APPLE LOCAL end blocks 6065211 */
465132718Skan
466132718Skan  /* Merge the attributes.  */
467132718Skan  attributes = (*targetm.merge_type_attributes) (t1, t2);
468132718Skan  return build_type_attribute_variant (result_type, attributes);
469132718Skan}
470132718Skan
47190075Sobrien/* Return the composite pointer type (see [expr.rel]) for T1 and T2.
47290075Sobrien   ARG1 and ARG2 are the values with those types.  The LOCATION is a
473169689Skan   string describing the current location, in case an error occurs.
47490075Sobrien
475132718Skan   This routine also implements the computation of a common type for
476132718Skan   pointers-to-members as per [expr.eq].  */
477132718Skan
478169689Skantree
479132718Skancomposite_pointer_type (tree t1, tree t2, tree arg1, tree arg2,
480132718Skan			const char* location)
48190075Sobrien{
482132718Skan  tree class1;
483132718Skan  tree class2;
48490075Sobrien
48590075Sobrien  /* [expr.rel]
48690075Sobrien
48790075Sobrien     If one operand is a null pointer constant, the composite pointer
48890075Sobrien     type is the type of the other operand.  */
48990075Sobrien  if (null_ptr_cst_p (arg1))
49090075Sobrien    return t2;
49190075Sobrien  if (null_ptr_cst_p (arg2))
49290075Sobrien    return t1;
493169689Skan
49490075Sobrien  /* We have:
49590075Sobrien
49690075Sobrien       [expr.rel]
49790075Sobrien
49890075Sobrien       If one of the operands has type "pointer to cv1 void*", then
49990075Sobrien       the other has type "pointer to cv2T", and the composite pointer
50090075Sobrien       type is "pointer to cv12 void", where cv12 is the union of cv1
50190075Sobrien       and cv2.
50290075Sobrien
50390075Sobrien    If either type is a pointer to void, make sure it is T1.  */
504132718Skan  if (TREE_CODE (t2) == POINTER_TYPE && VOID_TYPE_P (TREE_TYPE (t2)))
50590075Sobrien    {
50690075Sobrien      tree t;
50790075Sobrien      t = t1;
50890075Sobrien      t1 = t2;
50990075Sobrien      t2 = t;
51090075Sobrien    }
511132718Skan
51290075Sobrien  /* Now, if T1 is a pointer to void, merge the qualifiers.  */
513132718Skan  if (TREE_CODE (t1) == POINTER_TYPE && VOID_TYPE_P (TREE_TYPE (t1)))
51490075Sobrien    {
515132718Skan      tree attributes;
516132718Skan      tree result_type;
517132718Skan
51890075Sobrien      if (pedantic && TYPE_PTRFN_P (t2))
519169689Skan	pedwarn ("ISO C++ forbids %s between pointer of type %<void *%> "
520169689Skan		 "and pointer-to-function", location);
521169689Skan      result_type
522132718Skan	= cp_build_qualified_type (void_type_node,
523132718Skan				   (cp_type_quals (TREE_TYPE (t1))
524132718Skan				    | cp_type_quals (TREE_TYPE (t2))));
52590075Sobrien      result_type = build_pointer_type (result_type);
526132718Skan      /* Merge the attributes.  */
527132718Skan      attributes = (*targetm.merge_type_attributes) (t1, t2);
528132718Skan      return build_type_attribute_variant (result_type, attributes);
52990075Sobrien    }
530132718Skan
531169689Skan  if (c_dialect_objc () && TREE_CODE (t1) == POINTER_TYPE
532169689Skan      && TREE_CODE (t2) == POINTER_TYPE)
533169689Skan    {
534260311Spfg      /* APPLE LOCAL radar 4229905 - radar 6231433 */
535260311Spfg      if (objc_have_common_type (t1, t2, -3, NULL_TREE, location))
536260311Spfg      /* APPLE LOCAL 4154928 */
537260311Spfg	return objc_common_type (t1, t2);
538169689Skan    }
539169689Skan
540132718Skan  /* [expr.eq] permits the application of a pointer conversion to
541132718Skan     bring the pointers to a common type.  */
542132718Skan  if (TREE_CODE (t1) == POINTER_TYPE && TREE_CODE (t2) == POINTER_TYPE
543132718Skan      && CLASS_TYPE_P (TREE_TYPE (t1))
544132718Skan      && CLASS_TYPE_P (TREE_TYPE (t2))
545132718Skan      && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (t1),
546132718Skan						     TREE_TYPE (t2)))
54790075Sobrien    {
548132718Skan      class1 = TREE_TYPE (t1);
549132718Skan      class2 = TREE_TYPE (t2);
55090075Sobrien
551132718Skan      if (DERIVED_FROM_P (class1, class2))
552169689Skan	t2 = (build_pointer_type
553132718Skan	      (cp_build_qualified_type (class1, TYPE_QUALS (class2))));
554132718Skan      else if (DERIVED_FROM_P (class2, class1))
555169689Skan	t1 = (build_pointer_type
556132718Skan	      (cp_build_qualified_type (class2, TYPE_QUALS (class1))));
557132718Skan      else
558132718Skan	{
559169689Skan	  error ("%s between distinct pointer types %qT and %qT "
560132718Skan		 "lacks a cast", location, t1, t2);
561132718Skan	  return error_mark_node;
562132718Skan	}
563132718Skan    }
564132718Skan  /* [expr.eq] permits the application of a pointer-to-member
565132718Skan     conversion to change the class type of one of the types.  */
566132718Skan  else if (TYPE_PTR_TO_MEMBER_P (t1)
567132718Skan	   && !same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
568132718Skan			    TYPE_PTRMEM_CLASS_TYPE (t2)))
569132718Skan    {
570132718Skan      class1 = TYPE_PTRMEM_CLASS_TYPE (t1);
571132718Skan      class2 = TYPE_PTRMEM_CLASS_TYPE (t2);
57290075Sobrien
573132718Skan      if (DERIVED_FROM_P (class1, class2))
574132718Skan	t1 = build_ptrmem_type (class2, TYPE_PTRMEM_POINTED_TO_TYPE (t1));
575132718Skan      else if (DERIVED_FROM_P (class2, class1))
576132718Skan	t2 = build_ptrmem_type (class1, TYPE_PTRMEM_POINTED_TO_TYPE (t2));
57790075Sobrien      else
57890075Sobrien	{
579169689Skan	  error ("%s between distinct pointer-to-member types %qT and %qT "
580132718Skan		 "lacks a cast", location, t1, t2);
581132718Skan	  return error_mark_node;
58290075Sobrien	}
58390075Sobrien    }
584260311Spfg  /* APPLE LOCAL begin blocks 6065211 */
585260311Spfg  else if (TREE_CODE (t1) != TREE_CODE (t2))
586260311Spfg    {
587260311Spfg      error ("%s between distinct pointer types %qT and %qT "
588260311Spfg	     "lacks a cast", location, t1, t2);
589260311Spfg      return error_mark_node;
590260311Spfg    }
591260311Spfg  /* APPLE LOCAL end blocks 6065211 */
59290075Sobrien
593132718Skan  return composite_pointer_type_r (t1, t2, location);
59490075Sobrien}
59590075Sobrien
59696263Sobrien/* Return the merged type of two types.
59718334Speter   We assume that comptypes has already been done and returned 1;
59818334Speter   if that isn't so, this may crash.
59918334Speter
60096263Sobrien   This just combines attributes and default arguments; any other
60196263Sobrien   differences would cause the two types to compare unalike.  */
60218334Speter
60318334Spetertree
604132718Skanmerge_types (tree t1, tree t2)
60518334Speter{
606132718Skan  enum tree_code code1;
607132718Skan  enum tree_code code2;
60818334Speter  tree attributes;
60918334Speter
61018334Speter  /* Save time if the two types are the same.  */
61150397Sobrien  if (t1 == t2)
61250397Sobrien    return t1;
61396263Sobrien  if (original_type (t1) == original_type (t2))
61450397Sobrien    return t1;
61518334Speter
61618334Speter  /* If one type is nonsense, use the other.  */
61718334Speter  if (t1 == error_mark_node)
61818334Speter    return t2;
61918334Speter  if (t2 == error_mark_node)
62018334Speter    return t1;
62118334Speter
62250397Sobrien  /* Merge the attributes.  */
62390075Sobrien  attributes = (*targetm.merge_type_attributes) (t1, t2);
62418334Speter
62550397Sobrien  if (TYPE_PTRMEMFUNC_P (t1))
62650397Sobrien    t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
62750397Sobrien  if (TYPE_PTRMEMFUNC_P (t2))
62850397Sobrien    t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
62950397Sobrien
63018334Speter  code1 = TREE_CODE (t1);
63118334Speter  code2 = TREE_CODE (t2);
63218334Speter
63318334Speter  switch (code1)
63418334Speter    {
63518334Speter    case POINTER_TYPE:
63618334Speter    case REFERENCE_TYPE:
63796263Sobrien      /* For two pointers, do this recursively on the target type.  */
63818334Speter      {
63996263Sobrien	tree target = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
64096263Sobrien	int quals = cp_type_quals (t1);
64118334Speter
64218334Speter	if (code1 == POINTER_TYPE)
64318334Speter	  t1 = build_pointer_type (target);
64418334Speter	else
64518334Speter	  t1 = build_reference_type (target);
64618334Speter	t1 = build_type_attribute_variant (t1, attributes);
64796263Sobrien	t1 = cp_build_qualified_type (t1, quals);
64818334Speter
64918334Speter	if (TREE_CODE (target) == METHOD_TYPE)
65018334Speter	  t1 = build_ptrmemfunc_type (t1);
65118334Speter
65218334Speter	return t1;
65318334Speter      }
65418334Speter
65596263Sobrien    case OFFSET_TYPE:
65696263Sobrien      {
657132718Skan	int quals;
658132718Skan	tree pointee;
659132718Skan	quals = cp_type_quals (t1);
660132718Skan	pointee = merge_types (TYPE_PTRMEM_POINTED_TO_TYPE (t1),
661132718Skan			       TYPE_PTRMEM_POINTED_TO_TYPE (t2));
662132718Skan	t1 = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
663132718Skan				pointee);
664132718Skan	t1 = cp_build_qualified_type (t1, quals);
66596263Sobrien	break;
66696263Sobrien      }
66796263Sobrien
66818334Speter    case ARRAY_TYPE:
66918334Speter      {
67096263Sobrien	tree elt = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
67118334Speter	/* Save space: see if the result is identical to one of the args.  */
67218334Speter	if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
67318334Speter	  return build_type_attribute_variant (t1, attributes);
67418334Speter	if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
67518334Speter	  return build_type_attribute_variant (t2, attributes);
67618334Speter	/* Merge the element types, and have a size if either arg has one.  */
67750397Sobrien	t1 = build_cplus_array_type
67850397Sobrien	  (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
67996263Sobrien	break;
68018334Speter      }
68118334Speter
68218334Speter    case FUNCTION_TYPE:
68318334Speter      /* Function types: prefer the one that specified arg types.
68418334Speter	 If both do, merge the arg types.  Also merge the return types.  */
68518334Speter      {
68696263Sobrien	tree valtype = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
68718334Speter	tree p1 = TYPE_ARG_TYPES (t1);
68818334Speter	tree p2 = TYPE_ARG_TYPES (t2);
68918334Speter	tree rval, raises;
69018334Speter
69118334Speter	/* Save space: see if the result is identical to one of the args.  */
69218334Speter	if (valtype == TREE_TYPE (t1) && ! p2)
693132718Skan	  return cp_build_type_attribute_variant (t1, attributes);
69418334Speter	if (valtype == TREE_TYPE (t2) && ! p1)
695132718Skan	  return cp_build_type_attribute_variant (t2, attributes);
69618334Speter
69718334Speter	/* Simple way if one arg fails to specify argument types.  */
69818334Speter	if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node)
69918334Speter	  {
70018334Speter	    rval = build_function_type (valtype, p2);
70118334Speter	    if ((raises = TYPE_RAISES_EXCEPTIONS (t2)))
70218334Speter	      rval = build_exception_variant (rval, raises);
703132718Skan	    return cp_build_type_attribute_variant (rval, attributes);
70418334Speter	  }
70518334Speter	raises = TYPE_RAISES_EXCEPTIONS (t1);
70618334Speter	if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node)
70718334Speter	  {
70818334Speter	    rval = build_function_type (valtype, p1);
70918334Speter	    if (raises)
71018334Speter	      rval = build_exception_variant (rval, raises);
711132718Skan	    return cp_build_type_attribute_variant (rval, attributes);
71218334Speter	  }
71318334Speter
71418334Speter	rval = build_function_type (valtype, commonparms (p1, p2));
71596263Sobrien	t1 = build_exception_variant (rval, raises);
71696263Sobrien	break;
71718334Speter      }
71818334Speter
71996263Sobrien    case METHOD_TYPE:
72096263Sobrien      {
72196263Sobrien	/* Get this value the long way, since TYPE_METHOD_BASETYPE
72296263Sobrien	   is just the main variant of this.  */
72396263Sobrien	tree basetype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t2)));
72496263Sobrien	tree raises = TYPE_RAISES_EXCEPTIONS (t1);
72596263Sobrien	tree t3;
72618334Speter
72796263Sobrien	/* If this was a member function type, get back to the
72896263Sobrien	   original type of type member function (i.e., without
72996263Sobrien	   the class instance variable up front.  */
73096263Sobrien	t1 = build_function_type (TREE_TYPE (t1),
73196263Sobrien				  TREE_CHAIN (TYPE_ARG_TYPES (t1)));
73296263Sobrien	t2 = build_function_type (TREE_TYPE (t2),
73396263Sobrien				  TREE_CHAIN (TYPE_ARG_TYPES (t2)));
73496263Sobrien	t3 = merge_types (t1, t2);
735132718Skan	t3 = build_method_type_directly (basetype, TREE_TYPE (t3),
736132718Skan					 TYPE_ARG_TYPES (t3));
73796263Sobrien	t1 = build_exception_variant (t3, raises);
73896263Sobrien	break;
73996263Sobrien      }
74018334Speter
741132718Skan    case TYPENAME_TYPE:
742132718Skan      /* There is no need to merge attributes into a TYPENAME_TYPE.
743132718Skan	 When the type is instantiated it will have whatever
744132718Skan	 attributes result from the instantiation.  */
745132718Skan      return t1;
746132718Skan
74796263Sobrien    default:;
74896263Sobrien    }
749169689Skan
750169689Skan  if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
751169689Skan    return t1;
752169689Skan  else if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
753169689Skan    return t2;
754169689Skan  else
755169689Skan    return cp_build_type_attribute_variant (t1, attributes);
75696263Sobrien}
75718334Speter
75896263Sobrien/* Return the common type of two types.
75996263Sobrien   We assume that comptypes has already been done and returned 1;
76096263Sobrien   if that isn't so, this may crash.
76118334Speter
76296263Sobrien   This is the type for the result of most arithmetic operations
76396263Sobrien   if the operands have the given two types.  */
76418334Speter
76596263Sobrientree
766132718Skancommon_type (tree t1, tree t2)
76796263Sobrien{
76896263Sobrien  enum tree_code code1;
76996263Sobrien  enum tree_code code2;
77018334Speter
77196263Sobrien  /* If one type is nonsense, bail.  */
77296263Sobrien  if (t1 == error_mark_node || t2 == error_mark_node)
77396263Sobrien    return error_mark_node;
77418334Speter
77596263Sobrien  code1 = TREE_CODE (t1);
77696263Sobrien  code2 = TREE_CODE (t2);
77718334Speter
77896263Sobrien  if ((ARITHMETIC_TYPE_P (t1) || code1 == ENUMERAL_TYPE
779171825Skan       || code1 == VECTOR_TYPE)
78096263Sobrien      && (ARITHMETIC_TYPE_P (t2) || code2 == ENUMERAL_TYPE
781171825Skan	  || code2 == VECTOR_TYPE))
78296263Sobrien    return type_after_usual_arithmetic_conversions (t1, t2);
78318334Speter
78496263Sobrien  else if ((TYPE_PTR_P (t1) && TYPE_PTR_P (t2))
78596263Sobrien	   || (TYPE_PTRMEM_P (t1) && TYPE_PTRMEM_P (t2))
78696263Sobrien	   || (TYPE_PTRMEMFUNC_P (t1) && TYPE_PTRMEMFUNC_P (t2)))
78796263Sobrien    return composite_pointer_type (t1, t2, error_mark_node, error_mark_node,
78896263Sobrien				   "conversion");
78996263Sobrien  else
790169689Skan    gcc_unreachable ();
79118334Speter}
79218334Speter
79390075Sobrien/* Compare two exception specifier types for exactness or subsetness, if
794132718Skan   allowed. Returns false for mismatch, true for match (same, or
795132718Skan   derived and !exact).
796169689Skan
79790075Sobrien   [except.spec] "If a class X ... objects of class X or any class publicly
798132718Skan   and unambiguously derived from X. Similarly, if a pointer type Y * ...
79990075Sobrien   exceptions of type Y * or that are pointers to any type publicly and
800132718Skan   unambiguously derived from Y. Otherwise a function only allows exceptions
80190075Sobrien   that have the same type ..."
80290075Sobrien   This does not mention cv qualifiers and is different to what throw
80390075Sobrien   [except.throw] and catch [except.catch] will do. They will ignore the
80490075Sobrien   top level cv qualifiers, and allow qualifiers in the pointer to class
80590075Sobrien   example.
806169689Skan
80790075Sobrien   We implement the letter of the standard.  */
80850397Sobrien
809132718Skanstatic bool
810132718Skancomp_except_types (tree a, tree b, bool exact)
81190075Sobrien{
81290075Sobrien  if (same_type_p (a, b))
813132718Skan    return true;
81490075Sobrien  else if (!exact)
81590075Sobrien    {
81690075Sobrien      if (cp_type_quals (a) || cp_type_quals (b))
817169689Skan	return false;
818169689Skan
81990075Sobrien      if (TREE_CODE (a) == POINTER_TYPE
820169689Skan	  && TREE_CODE (b) == POINTER_TYPE)
821169689Skan	{
822169689Skan	  a = TREE_TYPE (a);
823169689Skan	  b = TREE_TYPE (b);
824169689Skan	  if (cp_type_quals (a) || cp_type_quals (b))
825169689Skan	    return false;
826169689Skan	}
827169689Skan
82890075Sobrien      if (TREE_CODE (a) != RECORD_TYPE
829169689Skan	  || TREE_CODE (b) != RECORD_TYPE)
830169689Skan	return false;
831169689Skan
832169689Skan      if (PUBLICLY_UNIQUELY_DERIVED_P (a, b))
833169689Skan	return true;
83490075Sobrien    }
835132718Skan  return false;
83690075Sobrien}
83790075Sobrien
838132718Skan/* Return true if TYPE1 and TYPE2 are equivalent exception specifiers.
839132718Skan   If EXACT is false, T2 can be stricter than T1 (according to 15.4/7),
84090075Sobrien   otherwise it must be exact. Exception lists are unordered, but
84190075Sobrien   we've already filtered out duplicates. Most lists will be in order,
84290075Sobrien   we should try to make use of that.  */
84390075Sobrien
844132718Skanbool
845132718Skancomp_except_specs (tree t1, tree t2, bool exact)
84618334Speter{
84790075Sobrien  tree probe;
84890075Sobrien  tree base;
84990075Sobrien  int  length = 0;
85090075Sobrien
85190075Sobrien  if (t1 == t2)
852132718Skan    return true;
853169689Skan
854169689Skan  if (t1 == NULL_TREE)			   /* T1 is ...  */
85590075Sobrien    return t2 == NULL_TREE || !exact;
856169689Skan  if (!TREE_VALUE (t1))			   /* t1 is EMPTY */
85790075Sobrien    return t2 != NULL_TREE && !TREE_VALUE (t2);
858169689Skan  if (t2 == NULL_TREE)			   /* T2 is ...  */
859132718Skan    return false;
860117395Skan  if (TREE_VALUE (t1) && !TREE_VALUE (t2)) /* T2 is EMPTY, T1 is not */
86190075Sobrien    return !exact;
862169689Skan
86390075Sobrien  /* Neither set is ... or EMPTY, make sure each part of T2 is in T1.
86490075Sobrien     Count how many we find, to determine exactness. For exact matching and
86590075Sobrien     ordered T1, T2, this is an O(n) operation, otherwise its worst case is
86690075Sobrien     O(nm).  */
86790075Sobrien  for (base = t1; t2 != NULL_TREE; t2 = TREE_CHAIN (t2))
86890075Sobrien    {
86990075Sobrien      for (probe = base; probe != NULL_TREE; probe = TREE_CHAIN (probe))
870169689Skan	{
871169689Skan	  tree a = TREE_VALUE (probe);
872169689Skan	  tree b = TREE_VALUE (t2);
873169689Skan
874169689Skan	  if (comp_except_types (a, b, exact))
875169689Skan	    {
876169689Skan	      if (probe == base && exact)
877169689Skan		base = TREE_CHAIN (probe);
878169689Skan	      length++;
879169689Skan	      break;
880169689Skan	    }
881169689Skan	}
88290075Sobrien      if (probe == NULL_TREE)
883169689Skan	return false;
88490075Sobrien    }
88590075Sobrien  return !exact || base == NULL_TREE || length == list_length (t1);
88618334Speter}
88718334Speter
888132718Skan/* Compare the array types T1 and T2.  ALLOW_REDECLARATION is true if
889132718Skan   [] can match [size].  */
89052284Sobrien
891132718Skanstatic bool
892132718Skancomp_array_types (tree t1, tree t2, bool allow_redeclaration)
89318334Speter{
89452284Sobrien  tree d1;
89552284Sobrien  tree d2;
896132718Skan  tree max1, max2;
89718334Speter
89852284Sobrien  if (t1 == t2)
899132718Skan    return true;
90052284Sobrien
90152284Sobrien  /* The type of the array elements must be the same.  */
902132718Skan  if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
903132718Skan    return false;
90418334Speter
90552284Sobrien  d1 = TYPE_DOMAIN (t1);
90652284Sobrien  d2 = TYPE_DOMAIN (t2);
90752284Sobrien
90852284Sobrien  if (d1 == d2)
909132718Skan    return true;
91018334Speter
91152284Sobrien  /* If one of the arrays is dimensionless, and the other has a
912117395Skan     dimension, they are of different types.  However, it is valid to
91352284Sobrien     write:
91418334Speter
91552284Sobrien       extern int a[];
91652284Sobrien       int a[3];
91718334Speter
918169689Skan     by [basic.link]:
91918334Speter
92052284Sobrien       declarations for an array object can specify
92152284Sobrien       array types that differ by the presence or absence of a major
92252284Sobrien       array bound (_dcl.array_).  */
92352284Sobrien  if (!d1 || !d2)
924132718Skan    return allow_redeclaration;
92518334Speter
92652284Sobrien  /* Check that the dimensions are the same.  */
92750397Sobrien
928132718Skan  if (!cp_tree_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2)))
929132718Skan    return false;
930132718Skan  max1 = TYPE_MAX_VALUE (d1);
931132718Skan  max2 = TYPE_MAX_VALUE (d2);
932132718Skan  if (processing_template_decl && !abi_version_at_least (2)
933132718Skan      && !value_dependent_expression_p (max1)
934132718Skan      && !value_dependent_expression_p (max2))
935132718Skan    {
936132718Skan      /* With abi-1 we do not fold non-dependent array bounds, (and
937169689Skan	 consequently mangle them incorrectly).  We must therefore
938169689Skan	 fold them here, to verify the domains have the same
939169689Skan	 value.  */
940132718Skan      max1 = fold (max1);
941132718Skan      max2 = fold (max2);
942132718Skan    }
94352284Sobrien
944132718Skan  if (!cp_tree_equal (max1, max2))
945132718Skan    return false;
94618334Speter
947132718Skan  return true;
948132718Skan}
94952284Sobrien
950132718Skan/* Return true if T1 and T2 are related as allowed by STRICT.  STRICT
951132718Skan   is a bitwise-or of the COMPARE_* flags.  */
95252284Sobrien
953132718Skanbool
954132718Skancomptypes (tree t1, tree t2, int strict)
955132718Skan{
95618334Speter  if (t1 == t2)
957132718Skan    return true;
95818334Speter
959132718Skan  /* Suppress errors caused by previously reported errors.  */
960122180Skan  if (t1 == error_mark_node || t2 == error_mark_node)
961132718Skan    return false;
962169689Skan
963169689Skan  gcc_assert (TYPE_P (t1) && TYPE_P (t2));
964169689Skan
965132718Skan  /* TYPENAME_TYPEs should be resolved if the qualifying scope is the
966132718Skan     current instantiation.  */
967132718Skan  if (TREE_CODE (t1) == TYPENAME_TYPE)
968132718Skan    {
969132718Skan      tree resolved = resolve_typename_type (t1, /*only_current_p=*/true);
97018334Speter
971132718Skan      if (resolved != error_mark_node)
972132718Skan	t1 = resolved;
973132718Skan    }
974169689Skan
975132718Skan  if (TREE_CODE (t2) == TYPENAME_TYPE)
976132718Skan    {
977132718Skan      tree resolved = resolve_typename_type (t2, /*only_current_p=*/true);
978132718Skan
979132718Skan      if (resolved != error_mark_node)
980132718Skan	t2 = resolved;
981132718Skan    }
982132718Skan
983132718Skan  /* If either type is the internal version of sizetype, use the
98490075Sobrien     language version.  */
98590075Sobrien  if (TREE_CODE (t1) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t1)
986169689Skan      && TYPE_ORIG_SIZE_TYPE (t1))
987169689Skan    t1 = TYPE_ORIG_SIZE_TYPE (t1);
98890075Sobrien
98990075Sobrien  if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2)
990169689Skan      && TYPE_ORIG_SIZE_TYPE (t2))
991169689Skan    t2 = TYPE_ORIG_SIZE_TYPE (t2);
99290075Sobrien
99350397Sobrien  if (TYPE_PTRMEMFUNC_P (t1))
99450397Sobrien    t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
99550397Sobrien  if (TYPE_PTRMEMFUNC_P (t2))
99650397Sobrien    t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
99750397Sobrien
99818334Speter  /* Different classes of types can't be compatible.  */
99918334Speter  if (TREE_CODE (t1) != TREE_CODE (t2))
1000132718Skan    return false;
100118334Speter
1002132718Skan  /* Qualifiers must match.  For array types, we will check when we
1003132718Skan     recur on the array element types.  */
1004132718Skan  if (TREE_CODE (t1) != ARRAY_TYPE
1005132718Skan      && TYPE_QUALS (t1) != TYPE_QUALS (t2))
1006132718Skan    return false;
1007132718Skan  if (TYPE_FOR_JAVA (t1) != TYPE_FOR_JAVA (t2))
1008132718Skan    return false;
100918334Speter
101018334Speter  /* Allow for two different type nodes which have essentially the same
101118334Speter     definition.  Note that we already checked for equality of the type
101250397Sobrien     qualifiers (just above).  */
101318334Speter
1014132718Skan  if (TREE_CODE (t1) != ARRAY_TYPE
1015132718Skan      && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1016132718Skan    return true;
101718334Speter
1018169689Skan  /* Compare the types.  Break out if they could be the same.  */
101918334Speter  switch (TREE_CODE (t1))
102018334Speter    {
102150397Sobrien    case TEMPLATE_TEMPLATE_PARM:
102290075Sobrien    case BOUND_TEMPLATE_TEMPLATE_PARM:
102350397Sobrien      if (TEMPLATE_TYPE_IDX (t1) != TEMPLATE_TYPE_IDX (t2)
102450397Sobrien	  || TEMPLATE_TYPE_LEVEL (t1) != TEMPLATE_TYPE_LEVEL (t2))
1025132718Skan	return false;
1026132718Skan      if (!comp_template_parms
1027132718Skan	  (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1)),
1028132718Skan	   DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2))))
1029132718Skan	return false;
103090075Sobrien      if (TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM)
1031169689Skan	break;
103250397Sobrien      /* Don't check inheritance.  */
103352284Sobrien      strict = COMPARE_STRICT;
1034132718Skan      /* Fall through.  */
103550397Sobrien
103618334Speter    case RECORD_TYPE:
103718334Speter    case UNION_TYPE:
103852284Sobrien      if (TYPE_TEMPLATE_INFO (t1) && TYPE_TEMPLATE_INFO (t2)
103952284Sobrien	  && (TYPE_TI_TEMPLATE (t1) == TYPE_TI_TEMPLATE (t2)
1040132718Skan	      || TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM)
1041132718Skan	  && comp_template_args (TYPE_TI_ARGS (t1), TYPE_TI_ARGS (t2)))
1042169689Skan	break;
1043169689Skan
104452284Sobrien      if ((strict & COMPARE_BASE) && DERIVED_FROM_P (t1, t2))
1045169689Skan	break;
1046132718Skan      else if ((strict & COMPARE_DERIVED) && DERIVED_FROM_P (t2, t1))
1047169689Skan	break;
1048169689Skan
1049132718Skan      return false;
105018334Speter
105118334Speter    case OFFSET_TYPE:
1052132718Skan      if (!comptypes (TYPE_OFFSET_BASETYPE (t1), TYPE_OFFSET_BASETYPE (t2),
1053132718Skan		      strict & ~COMPARE_REDECLARATION))
1054132718Skan	return false;
1055169689Skan      if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1056169689Skan	return false;
1057169689Skan      break;
105818334Speter
1059260311Spfg      /* APPLE LOCAL begin blocks 6040305 */
1060260311Spfg      case BLOCK_POINTER_TYPE:
1061260311Spfg	 if (TREE_CODE (t2) == BLOCK_POINTER_TYPE)
1062260311Spfg	 {
1063260311Spfg	   tree pt1 = TREE_TYPE (t1);
1064260311Spfg	   tree pt2 = TREE_TYPE (t2);
1065260311Spfg	   if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (pt1),
1066260311Spfg							  TREE_TYPE (pt2)))
1067260311Spfg	     return false;
1068260311Spfg	   if (!compparms (TYPE_ARG_TYPES (pt1), TYPE_ARG_TYPES (pt2)))
1069260311Spfg	     return false;
1070260311Spfg	   break;
1071260311Spfg	 }
1072260311Spfg	 /* APPLE LOCAL end blocks 6040305 */
1073260311Spfg
1074259660Spfg    case POINTER_TYPE:
1075259655Spfg    case REFERENCE_TYPE:
1076169689Skan      if (TYPE_MODE (t1) != TYPE_MODE (t2)
1077169689Skan	  || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2)
1078169689Skan	  || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1079169689Skan	return false;
1080169689Skan      break;
108118334Speter
108296263Sobrien    case METHOD_TYPE:
108318334Speter    case FUNCTION_TYPE:
1084132718Skan      if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1085132718Skan	return false;
1086169689Skan      if (!compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)))
1087169689Skan	return false;
1088169689Skan      break;
108918334Speter
109018334Speter    case ARRAY_TYPE:
1091132718Skan      /* Target types must match incl. qualifiers.  */
1092169689Skan      if (!comp_array_types (t1, t2, !!(strict & COMPARE_REDECLARATION)))
1093169689Skan	return false;
1094169689Skan      break;
109518334Speter
109618334Speter    case TEMPLATE_TYPE_PARM:
1097169689Skan      if (TEMPLATE_TYPE_IDX (t1) != TEMPLATE_TYPE_IDX (t2)
1098169689Skan	  || TEMPLATE_TYPE_LEVEL (t1) != TEMPLATE_TYPE_LEVEL (t2))
1099169689Skan	return false;
1100169689Skan      break;
110118334Speter
110250397Sobrien    case TYPENAME_TYPE:
1103132718Skan      if (!cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1),
1104132718Skan			  TYPENAME_TYPE_FULLNAME (t2)))
1105169689Skan	return false;
1106169689Skan      if (!same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2)))
1107169689Skan	return false;
1108169689Skan      break;
110950397Sobrien
111090075Sobrien    case UNBOUND_CLASS_TEMPLATE:
1111132718Skan      if (!cp_tree_equal (TYPE_IDENTIFIER (t1), TYPE_IDENTIFIER (t2)))
1112169689Skan	return false;
1113169689Skan      if (!same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2)))
1114169689Skan	return false;
1115169689Skan      break;
111690075Sobrien
111790075Sobrien    case COMPLEX_TYPE:
1118169689Skan      if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1119169689Skan	return false;
1120169689Skan      break;
112190075Sobrien
1122169689Skan    case VECTOR_TYPE:
1123169689Skan      if (TYPE_VECTOR_SUBPARTS (t1) != TYPE_VECTOR_SUBPARTS (t2)
1124169689Skan	  || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1125169689Skan	return false;
1126169689Skan      break;
1127169689Skan
112850397Sobrien    default:
1129169689Skan      return false;
113018334Speter    }
1131169689Skan
1132169689Skan  /* If we get here, we know that from a target independent POV the
1133169689Skan     types are the same.  Make sure the target attributes are also
1134169689Skan     the same.  */
1135169689Skan  return targetm.comp_type_attributes (t1, t2);
113618334Speter}
113718334Speter
113852284Sobrien/* Returns 1 if TYPE1 is at least as qualified as TYPE2.  */
113952284Sobrien
1140132718Skanbool
1141132718Skanat_least_as_qualified_p (tree type1, tree type2)
114252284Sobrien{
1143132718Skan  int q1 = cp_type_quals (type1);
1144132718Skan  int q2 = cp_type_quals (type2);
1145169689Skan
114652284Sobrien  /* All qualifiers for TYPE2 must also appear in TYPE1.  */
1147132718Skan  return (q1 & q2) == q2;
114852284Sobrien}
114952284Sobrien
115050397Sobrien/* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
115150397Sobrien   more cv-qualified that TYPE1, and 0 otherwise.  */
115250397Sobrien
115350397Sobrienint
1154132718Skancomp_cv_qualification (tree type1, tree type2)
115550397Sobrien{
1156132718Skan  int q1 = cp_type_quals (type1);
1157132718Skan  int q2 = cp_type_quals (type2);
1158132718Skan
1159132718Skan  if (q1 == q2)
116050397Sobrien    return 0;
116150397Sobrien
1162132718Skan  if ((q1 & q2) == q2)
116350397Sobrien    return 1;
1164132718Skan  else if ((q1 & q2) == q1)
116550397Sobrien    return -1;
116650397Sobrien
116750397Sobrien  return 0;
116850397Sobrien}
116950397Sobrien
117050397Sobrien/* Returns 1 if the cv-qualification signature of TYPE1 is a proper
117150397Sobrien   subset of the cv-qualification signature of TYPE2, and the types
117250397Sobrien   are similar.  Returns -1 if the other way 'round, and 0 otherwise.  */
117350397Sobrien
117450397Sobrienint
1175132718Skancomp_cv_qual_signature (tree type1, tree type2)
117650397Sobrien{
117750397Sobrien  if (comp_ptr_ttypes_real (type2, type1, -1))
117850397Sobrien    return 1;
117950397Sobrien  else if (comp_ptr_ttypes_real (type1, type2, -1))
118050397Sobrien    return -1;
118150397Sobrien  else
118250397Sobrien    return 0;
118350397Sobrien}
118450397Sobrien
118518334Speter/* If two types share a common base type, return that basetype.
118618334Speter   If there is not a unique most-derived base type, this function
118718334Speter   returns ERROR_MARK_NODE.  */
118850397Sobrien
118950397Sobrienstatic tree
1190132718Skancommon_base_type (tree tt1, tree tt2)
119118334Speter{
119250397Sobrien  tree best = NULL_TREE;
119318334Speter  int i;
119418334Speter
119518334Speter  /* If one is a baseclass of another, that's good enough.  */
119618334Speter  if (UNIQUELY_DERIVED_FROM_P (tt1, tt2))
119718334Speter    return tt1;
119818334Speter  if (UNIQUELY_DERIVED_FROM_P (tt2, tt1))
119918334Speter    return tt2;
120018334Speter
120118334Speter  /* Otherwise, try to find a unique baseclass of TT1
120218334Speter     that is shared by TT2, and follow that down.  */
1203169689Skan  for (i = BINFO_N_BASE_BINFOS (TYPE_BINFO (tt1))-1; i >= 0; i--)
120418334Speter    {
1205169689Skan      tree basetype = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (tt1), i));
120618334Speter      tree trial = common_base_type (basetype, tt2);
1207169689Skan
120818334Speter      if (trial)
120918334Speter	{
121018334Speter	  if (trial == error_mark_node)
121118334Speter	    return trial;
121218334Speter	  if (best == NULL_TREE)
121318334Speter	    best = trial;
121418334Speter	  else if (best != trial)
121518334Speter	    return error_mark_node;
121618334Speter	}
121718334Speter    }
121818334Speter
121918334Speter  /* Same for TT2.  */
1220169689Skan  for (i = BINFO_N_BASE_BINFOS (TYPE_BINFO (tt2))-1; i >= 0; i--)
122118334Speter    {
1222169689Skan      tree basetype = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (tt2), i));
122318334Speter      tree trial = common_base_type (tt1, basetype);
1224169689Skan
122518334Speter      if (trial)
122618334Speter	{
122718334Speter	  if (trial == error_mark_node)
122818334Speter	    return trial;
122918334Speter	  if (best == NULL_TREE)
123018334Speter	    best = trial;
123118334Speter	  else if (best != trial)
123218334Speter	    return error_mark_node;
123318334Speter	}
123418334Speter    }
123518334Speter  return best;
123618334Speter}
123718334Speter
123818334Speter/* Subroutines of `comptypes'.  */
123918334Speter
1240132718Skan/* Return true if two parameter type lists PARMS1 and PARMS2 are
124152284Sobrien   equivalent in the sense that functions with those parameter types
124252284Sobrien   can have equivalent types.  The two lists must be equivalent,
1243132718Skan   element by element.  */
124418334Speter
1245132718Skanbool
1246132718Skancompparms (tree parms1, tree parms2)
124718334Speter{
1248132718Skan  tree t1, t2;
124918334Speter
125018334Speter  /* An unspecified parmlist matches any specified parmlist
125118334Speter     whose argument types don't need default promotions.  */
125218334Speter
1253132718Skan  for (t1 = parms1, t2 = parms2;
1254132718Skan       t1 || t2;
1255132718Skan       t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
125618334Speter    {
125718334Speter      /* If one parmlist is shorter than the other,
125850397Sobrien	 they fail to match.  */
1259132718Skan      if (!t1 || !t2)
1260132718Skan	return false;
1261122180Skan      if (!same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1262132718Skan	return false;
126318334Speter    }
1264132718Skan  return true;
126518334Speter}
126618334Speter
1267132718Skan
1268132718Skan/* Process a sizeof or alignof expression where the operand is a
1269132718Skan   type.  */
127050397Sobrien
127118334Spetertree
1272132718Skancxx_sizeof_or_alignof_type (tree type, enum tree_code op, bool complain)
127318334Speter{
1274117395Skan  tree value;
1275169689Skan  bool dependent_p;
127618334Speter
1277169689Skan  gcc_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR);
1278132718Skan  if (type == error_mark_node)
1279132718Skan    return error_mark_node;
1280132718Skan
1281132718Skan  type = non_reference (type);
1282169689Skan  if (TREE_CODE (type) == METHOD_TYPE)
128318334Speter    {
1284117395Skan      if (complain && (pedantic || warn_pointer_arith))
1285169689Skan	pedwarn ("invalid application of %qs to a member function",
1286169689Skan		 operator_name_info[(int) op].name);
1287117395Skan      value = size_one_node;
128818334Speter    }
128918334Speter
1290169689Skan  dependent_p = dependent_type_p (type);
1291169689Skan  if (!dependent_p)
1292169689Skan    complete_type (type);
1293169689Skan  if (dependent_p
1294169689Skan      /* VLA types will have a non-constant size.  In the body of an
1295169689Skan	 uninstantiated template, we don't need to try to compute the
1296169689Skan	 value, because the sizeof expression is not an integral
1297169689Skan	 constant expression in that case.  And, if we do try to
1298169689Skan	 compute the value, we'll likely end up with SAVE_EXPRs, which
1299169689Skan	 the template substitution machinery does not expect to see.  */
1300169689Skan      || (processing_template_decl
1301169689Skan	  && COMPLETE_TYPE_P (type)
1302169689Skan	  && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST))
1303169689Skan    {
1304169689Skan      value = build_min (op, size_type_node, type);
1305169689Skan      TREE_READONLY (value) = 1;
1306169689Skan      return value;
1307169689Skan    }
1308169689Skan
1309169689Skan  return c_sizeof_or_alignof_type (complete_type (type),
1310169689Skan				   op == SIZEOF_EXPR,
1311169689Skan				   complain);
131218334Speter}
131318334Speter
1314169689Skan/* Process a sizeof expression where the operand is an expression.  */
1315132718Skan
1316169689Skanstatic tree
1317169689Skancxx_sizeof_expr (tree e)
131850397Sobrien{
1319132718Skan  if (e == error_mark_node)
1320132718Skan    return error_mark_node;
1321169689Skan
132250397Sobrien  if (processing_template_decl)
1323132718Skan    {
1324169689Skan      e = build_min (SIZEOF_EXPR, size_type_node, e);
1325132718Skan      TREE_SIDE_EFFECTS (e) = 0;
1326132718Skan      TREE_READONLY (e) = 1;
1327169689Skan
1328132718Skan      return e;
1329132718Skan    }
1330169689Skan
133150397Sobrien  if (TREE_CODE (e) == COMPONENT_REF
1332132718Skan      && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL
133352284Sobrien      && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1)))
133450397Sobrien    {
1335169689Skan      error ("invalid application of %<sizeof%> to a bit-field");
1336132718Skan      e = char_type_node;
133750397Sobrien    }
1338132718Skan  else if (is_overloaded_fn (e))
1339132718Skan    {
1340169689Skan      pedwarn ("ISO C++ forbids applying %<sizeof%> to an expression of "
1341169689Skan	       "function type");
1342132718Skan      e = char_type_node;
1343132718Skan    }
134452284Sobrien  else if (type_unknown_p (e))
134552284Sobrien    {
1346117395Skan      cxx_incomplete_type_error (e, TREE_TYPE (e));
1347132718Skan      e = char_type_node;
134852284Sobrien    }
1349132718Skan  else
1350132718Skan    e = TREE_TYPE (e);
1351169689Skan
1352169689Skan  return cxx_sizeof_or_alignof_type (e, SIZEOF_EXPR, true);
135350397Sobrien}
1354169689Skan
1355169689Skan/* Implement the __alignof keyword: Return the minimum required
1356169689Skan   alignment of E, measured in bytes.  For VAR_DECL's and
1357169689Skan   FIELD_DECL's return DECL_ALIGN (which can be set from an
1358169689Skan   "aligned" __attribute__ specification).  */
1359169689Skan
1360169689Skanstatic tree
1361169689Skancxx_alignof_expr (tree e)
1362169689Skan{
1363169689Skan  tree t;
1364169689Skan
1365169689Skan  if (e == error_mark_node)
1366169689Skan    return error_mark_node;
1367169689Skan
1368169689Skan  if (processing_template_decl)
1369169689Skan    {
1370169689Skan      e = build_min (ALIGNOF_EXPR, size_type_node, e);
1371169689Skan      TREE_SIDE_EFFECTS (e) = 0;
1372169689Skan      TREE_READONLY (e) = 1;
1373169689Skan
1374169689Skan      return e;
1375169689Skan    }
1376169689Skan
1377169689Skan  if (TREE_CODE (e) == VAR_DECL)
1378169689Skan    t = size_int (DECL_ALIGN_UNIT (e));
1379169689Skan  else if (TREE_CODE (e) == COMPONENT_REF
1380169689Skan	   && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL
1381169689Skan	   && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1)))
1382169689Skan    {
1383169689Skan      error ("invalid application of %<__alignof%> to a bit-field");
1384169689Skan      t = size_one_node;
1385169689Skan    }
1386169689Skan  else if (TREE_CODE (e) == COMPONENT_REF
1387169689Skan	   && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL)
1388169689Skan    t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (e, 1)));
1389169689Skan  else if (is_overloaded_fn (e))
1390169689Skan    {
1391169689Skan      pedwarn ("ISO C++ forbids applying %<__alignof%> to an expression of "
1392169689Skan	       "function type");
1393258748Spfg      if (TREE_CODE (e) == FUNCTION_DECL)
1394258748Spfg	t = size_int (DECL_ALIGN_UNIT (e));
1395258748Spfg      else
1396258748Spfg	t = size_one_node;
1397169689Skan    }
1398169689Skan  else if (type_unknown_p (e))
1399169689Skan    {
1400169689Skan      cxx_incomplete_type_error (e, TREE_TYPE (e));
1401169689Skan      t = size_one_node;
1402169689Skan    }
1403169689Skan  else
1404169689Skan    return cxx_sizeof_or_alignof_type (TREE_TYPE (e), ALIGNOF_EXPR, true);
1405169689Skan
1406169689Skan  return fold_convert (size_type_node, t);
1407169689Skan}
1408169689Skan
1409169689Skan/* Process a sizeof or alignof expression E with code OP where the operand
1410169689Skan   is an expression.  */
1411169689Skan
1412169689Skantree
1413169689Skancxx_sizeof_or_alignof_expr (tree e, enum tree_code op)
1414169689Skan{
1415169689Skan  if (op == SIZEOF_EXPR)
1416169689Skan    return cxx_sizeof_expr (e);
1417169689Skan  else
1418169689Skan    return cxx_alignof_expr (e);
1419169689Skan}
142018334Speter
1421132718Skan/* EXPR is being used in a context that is not a function call.
1422132718Skan   Enforce:
142318334Speter
1424169689Skan     [expr.ref]
142518334Speter
1426132718Skan     The expression can be used only as the left-hand operand of a
1427169689Skan     member function call.
1428132718Skan
1429132718Skan     [expr.mptr.operator]
1430132718Skan
1431132718Skan     If the result of .* or ->* is a function, then that result can be
1432169689Skan     used only as the operand for the function call operator ().
1433132718Skan
1434132718Skan   by issuing an error message if appropriate.  Returns true iff EXPR
1435132718Skan   violates these rules.  */
1436132718Skan
1437132718Skanbool
1438132718Skaninvalid_nonstatic_memfn_p (tree expr)
1439132718Skan{
1440132718Skan  if (TREE_CODE (TREE_TYPE (expr)) == METHOD_TYPE)
1441132718Skan    {
1442132718Skan      error ("invalid use of non-static member function");
1443132718Skan      return true;
1444132718Skan    }
1445132718Skan  return false;
1446132718Skan}
1447132718Skan
1448169689Skan/* If EXP is a reference to a bitfield, and the type of EXP does not
1449169689Skan   match the declared type of the bitfield, return the declared type
1450169689Skan   of the bitfield.  Otherwise, return NULL_TREE.  */
1451169689Skan
1452169689Skantree
1453169689Skanis_bitfield_expr_with_lowered_type (tree exp)
1454169689Skan{
1455169689Skan  switch (TREE_CODE (exp))
1456169689Skan    {
1457169689Skan    case COND_EXPR:
1458169689Skan      if (!is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1)))
1459169689Skan	return NULL_TREE;
1460169689Skan      return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 2));
1461169689Skan
1462169689Skan    case COMPOUND_EXPR:
1463169689Skan      return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1));
1464169689Skan
1465169689Skan    case MODIFY_EXPR:
1466169689Skan    case SAVE_EXPR:
1467169689Skan      return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
1468169689Skan
1469169689Skan    case COMPONENT_REF:
1470169689Skan      {
1471169689Skan	tree field;
1472169689Skan
1473169689Skan	field = TREE_OPERAND (exp, 1);
1474169689Skan	if (TREE_CODE (field) != FIELD_DECL || !DECL_C_BIT_FIELD (field))
1475169689Skan	  return NULL_TREE;
1476169689Skan	if (same_type_ignoring_top_level_qualifiers_p
1477169689Skan	    (TREE_TYPE (exp), DECL_BIT_FIELD_TYPE (field)))
1478169689Skan	  return NULL_TREE;
1479169689Skan	return DECL_BIT_FIELD_TYPE (field);
1480169689Skan      }
1481169689Skan
1482169689Skan    default:
1483169689Skan      return NULL_TREE;
1484169689Skan    }
1485169689Skan}
1486169689Skan
1487169689Skan/* Like is_bitfield_with_lowered_type, except that if EXP is not a
1488169689Skan   bitfield with a lowered type, the type of EXP is returned, rather
1489169689Skan   than NULL_TREE.  */
1490169689Skan
1491169689Skantree
1492169689Skanunlowered_expr_type (tree exp)
1493169689Skan{
1494169689Skan  tree type;
1495169689Skan
1496169689Skan  type = is_bitfield_expr_with_lowered_type (exp);
1497169689Skan  if (!type)
1498169689Skan    type = TREE_TYPE (exp);
1499169689Skan
1500169689Skan  return type;
1501169689Skan}
1502169689Skan
1503132718Skan/* Perform the conversions in [expr] that apply when an lvalue appears
1504132718Skan   in an rvalue context: the lvalue-to-rvalue, array-to-pointer, and
1505169689Skan   function-to-pointer conversions.  In addition, manifest constants
1506169689Skan   are replaced by their values, and bitfield references are converted
1507169689Skan   to their declared types.
1508132718Skan
1509169689Skan   Although the returned value is being used as an rvalue, this
1510169689Skan   function does not wrap the returned expression in a
1511169689Skan   NON_LVALUE_EXPR; the caller is expected to be mindful of the fact
1512169689Skan   that the return value is no longer an lvalue.  */
1513132718Skan
151418334Spetertree
1515132718Skandecay_conversion (tree exp)
151618334Speter{
1517132718Skan  tree type;
1518132718Skan  enum tree_code code;
151918334Speter
152052284Sobrien  type = TREE_TYPE (exp);
152190075Sobrien  if (type == error_mark_node)
152290075Sobrien    return error_mark_node;
152390075Sobrien
152490075Sobrien  if (type_unknown_p (exp))
152590075Sobrien    {
1526117395Skan      cxx_incomplete_type_error (exp, TREE_TYPE (exp));
152790075Sobrien      return error_mark_node;
152890075Sobrien    }
152918334Speter
1530169689Skan  exp = decl_constant_value (exp);
1531169689Skan  if (error_operand_p (exp))
1532169689Skan    return error_mark_node;
1533169689Skan
153418334Speter  /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
153518334Speter     Leave such NOP_EXPRs, since RHS is being used in non-lvalue context.  */
1536169689Skan  code = TREE_CODE (type);
153718334Speter  if (code == VOID_TYPE)
153818334Speter    {
153918334Speter      error ("void value not ignored as it ought to be");
154018334Speter      return error_mark_node;
154118334Speter    }
1542132718Skan  if (invalid_nonstatic_memfn_p (exp))
1543132718Skan    return error_mark_node;
154452284Sobrien  if (code == FUNCTION_TYPE || is_overloaded_fn (exp))
154552284Sobrien    return build_unary_op (ADDR_EXPR, exp, 0);
154618334Speter  if (code == ARRAY_TYPE)
154718334Speter    {
1548132718Skan      tree adr;
154918334Speter      tree ptrtype;
155018334Speter
155118334Speter      if (TREE_CODE (exp) == INDIRECT_REF)
1552169689Skan	return build_nop (build_pointer_type (TREE_TYPE (type)),
1553132718Skan			  TREE_OPERAND (exp, 0));
155418334Speter
155518334Speter      if (TREE_CODE (exp) == COMPOUND_EXPR)
155618334Speter	{
155718334Speter	  tree op1 = decay_conversion (TREE_OPERAND (exp, 1));
1558169689Skan	  return build2 (COMPOUND_EXPR, TREE_TYPE (op1),
1559169689Skan			 TREE_OPERAND (exp, 0), op1);
156018334Speter	}
156118334Speter
156218334Speter      if (!lvalue_p (exp)
156318334Speter	  && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
156418334Speter	{
156518334Speter	  error ("invalid use of non-lvalue array");
156618334Speter	  return error_mark_node;
156718334Speter	}
156818334Speter
156952284Sobrien      ptrtype = build_pointer_type (TREE_TYPE (type));
157018334Speter
157118334Speter      if (TREE_CODE (exp) == VAR_DECL)
157218334Speter	{
1573117395Skan	  if (!cxx_mark_addressable (exp))
157418334Speter	    return error_mark_node;
1575132718Skan	  adr = build_nop (ptrtype, build_address (exp));
157618334Speter	  return adr;
157718334Speter	}
157818334Speter      /* This way is better for a COMPONENT_REF since it can
157918334Speter	 simplify the offset for a component.  */
158018334Speter      adr = build_unary_op (ADDR_EXPR, exp, 1);
158150397Sobrien      return cp_convert (ptrtype, adr);
158218334Speter    }
158318334Speter
1584169689Skan  /* If a bitfield is used in a context where integral promotion
1585169689Skan     applies, then the caller is expected to have used
1586169689Skan     default_conversion.  That function promotes bitfields correctly
1587169689Skan     before calling this function.  At this point, if we have a
1588169689Skan     bitfield referenced, we may assume that is not subject to
1589169689Skan     promotion, and that, therefore, the type of the resulting rvalue
1590169689Skan     is the declared type of the bitfield.  */
1591169689Skan  exp = convert_bitfield_to_declared_type (exp);
159290075Sobrien
1593169689Skan  /* We do not call rvalue() here because we do not want to wrap EXP
1594169689Skan     in a NON_LVALUE_EXPR.  */
1595169689Skan
1596169689Skan  /* [basic.lval]
1597169689Skan
1598169689Skan     Non-class rvalues always have cv-unqualified types.  */
1599169689Skan  type = TREE_TYPE (exp);
1600169689Skan  if (!CLASS_TYPE_P (type) && cp_type_quals (type))
1601169689Skan    exp = build_nop (TYPE_MAIN_VARIANT (type), exp);
1602169689Skan
160318334Speter  return exp;
160418334Speter}
160518334Speter
1606169689Skan/* Perform prepatory conversions, as part of the "usual arithmetic
1607169689Skan   conversions".  In particular, as per [expr]:
1608169689Skan
1609169689Skan     Whenever an lvalue expression appears as an operand of an
1610169689Skan     operator that expects the rvalue for that operand, the
1611169689Skan     lvalue-to-rvalue, array-to-pointer, or function-to-pointer
1612169689Skan     standard conversions are applied to convert the expression to an
1613169689Skan     rvalue.
1614169689Skan
1615169689Skan   In addition, we perform integral promotions here, as those are
1616169689Skan   applied to both operands to a binary operator before determining
1617169689Skan   what additional conversions should apply.  */
1618169689Skan
161918334Spetertree
1620132718Skandefault_conversion (tree exp)
162118334Speter{
1622169689Skan  /* Perform the integral promotions first so that bitfield
1623169689Skan     expressions (which may promote to "int", even if the bitfield is
1624169689Skan     declared "unsigned") are promoted correctly.  */
1625132718Skan  if (INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (exp)))
1626132718Skan    exp = perform_integral_promotions (exp);
1627169689Skan  /* Perform the other conversions.  */
1628169689Skan  exp = decay_conversion (exp);
162918334Speter
163018334Speter  return exp;
163118334Speter}
163250397Sobrien
1633132718Skan/* EXPR is an expression with an integral or enumeration type.
1634132718Skan   Perform the integral promotions in [conv.prom], and return the
1635132718Skan   converted value.  */
1636132718Skan
1637132718Skantree
1638132718Skanperform_integral_promotions (tree expr)
1639132718Skan{
1640132718Skan  tree type;
1641132718Skan  tree promoted_type;
1642132718Skan
1643169689Skan  /* [conv.prom]
1644169689Skan
1645169689Skan     If the bitfield has an enumerated type, it is treated as any
1646169689Skan     other value of that type for promotion purposes.  */
1647169689Skan  type = is_bitfield_expr_with_lowered_type (expr);
1648169689Skan  if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
1649169689Skan    type = TREE_TYPE (expr);
1650169689Skan  gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
1651132718Skan  promoted_type = type_promotes_to (type);
1652132718Skan  if (type != promoted_type)
1653132718Skan    expr = cp_convert (promoted_type, expr);
1654132718Skan  return expr;
1655132718Skan}
1656132718Skan
165750397Sobrien/* Take the address of an inline function without setting TREE_ADDRESSABLE
165850397Sobrien   or TREE_USED.  */
165950397Sobrien
166050397Sobrientree
1661132718Skaninline_conversion (tree exp)
166250397Sobrien{
166350397Sobrien  if (TREE_CODE (exp) == FUNCTION_DECL)
166452284Sobrien    exp = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (exp)), exp);
166552284Sobrien
166652284Sobrien  return exp;
166752284Sobrien}
166852284Sobrien
166952284Sobrien/* Returns nonzero iff exp is a STRING_CST or the result of applying
167052284Sobrien   decay_conversion to one.  */
167152284Sobrien
167252284Sobrienint
1673132718Skanstring_conv_p (tree totype, tree exp, int warn)
167452284Sobrien{
167552284Sobrien  tree t;
167652284Sobrien
1677169689Skan  if (TREE_CODE (totype) != POINTER_TYPE)
167852284Sobrien    return 0;
167952284Sobrien
168052284Sobrien  t = TREE_TYPE (totype);
168152284Sobrien  if (!same_type_p (t, char_type_node)
168252284Sobrien      && !same_type_p (t, wchar_type_node))
168352284Sobrien    return 0;
168452284Sobrien
168552284Sobrien  if (TREE_CODE (exp) == STRING_CST)
168650397Sobrien    {
168752284Sobrien      /* Make sure that we don't try to convert between char and wchar_t.  */
168852284Sobrien      if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t))
168952284Sobrien	return 0;
169050397Sobrien    }
169152284Sobrien  else
169252284Sobrien    {
169352284Sobrien      /* Is this a string constant which has decayed to 'const char *'?  */
169452284Sobrien      t = build_pointer_type (build_qualified_type (t, TYPE_QUAL_CONST));
169552284Sobrien      if (!same_type_p (TREE_TYPE (exp), t))
169652284Sobrien	return 0;
169752284Sobrien      STRIP_NOPS (exp);
169852284Sobrien      if (TREE_CODE (exp) != ADDR_EXPR
169952284Sobrien	  || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST)
170052284Sobrien	return 0;
170152284Sobrien    }
170252284Sobrien
170352284Sobrien  /* This warning is not very useful, as it complains about printf.  */
1704169689Skan  if (warn)
1705169689Skan    warning (OPT_Wwrite_strings,
1706169689Skan	     "deprecated conversion from string constant to %qT",
1707169689Skan	     totype);
170852284Sobrien
170952284Sobrien  return 1;
171050397Sobrien}
171118334Speter
171250397Sobrien/* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
171350397Sobrien   can, for example, use as an lvalue.  This code used to be in
171450397Sobrien   unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
171550397Sobrien   expressions, where we're dealing with aggregates.  But now it's again only
171650397Sobrien   called from unary_complex_lvalue.  The case (in particular) that led to
171750397Sobrien   this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
171850397Sobrien   get it there.  */
171918334Speter
172050397Sobrienstatic tree
1721132718Skanrationalize_conditional_expr (enum tree_code code, tree t)
172250397Sobrien{
172350397Sobrien  /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
172450397Sobrien     the first operand is always the one to be used if both operands
172550397Sobrien     are equal, so we know what conditional expression this used to be.  */
172650397Sobrien  if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR)
172718334Speter    {
1728258081Spfg      tree op0 = TREE_OPERAND (t, 0);
1729258081Spfg      tree op1 = TREE_OPERAND (t, 1);
1730258081Spfg
1731169689Skan      /* The following code is incorrect if either operand side-effects.  */
1732258081Spfg      gcc_assert (!TREE_SIDE_EFFECTS (op0)
1733258081Spfg		  && !TREE_SIDE_EFFECTS (op1));
173450397Sobrien      return
173550397Sobrien	build_conditional_expr (build_x_binary_op ((TREE_CODE (t) == MIN_EXPR
173650397Sobrien						    ? LE_EXPR : GE_EXPR),
1737258081Spfg						   op0, TREE_CODE (op0),
1738258081Spfg						   op1, TREE_CODE (op1),
1739132718Skan						   /*overloaded_p=*/NULL),
1740258081Spfg			    build_unary_op (code, op0, 0),
1741258081Spfg			    build_unary_op (code, op1, 0));
174218334Speter    }
174318334Speter
174450397Sobrien  return
174550397Sobrien    build_conditional_expr (TREE_OPERAND (t, 0),
174650397Sobrien			    build_unary_op (code, TREE_OPERAND (t, 1), 0),
174750397Sobrien			    build_unary_op (code, TREE_OPERAND (t, 2), 0));
174850397Sobrien}
174918334Speter
175050397Sobrien/* Given the TYPE of an anonymous union field inside T, return the
175150397Sobrien   FIELD_DECL for the field.  If not found return NULL_TREE.  Because
175250397Sobrien   anonymous unions can nest, we must also search all anonymous unions
175350397Sobrien   that are directly reachable.  */
175418334Speter
1755132718Skantree
1756132718Skanlookup_anon_field (tree t, tree type)
175750397Sobrien{
175850397Sobrien  tree field;
175918334Speter
176050397Sobrien  for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
176118334Speter    {
176250397Sobrien      if (TREE_STATIC (field))
176350397Sobrien	continue;
1764117395Skan      if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
176550397Sobrien	continue;
176618334Speter
176750397Sobrien      /* If we find it directly, return the field.  */
176850397Sobrien      if (DECL_NAME (field) == NULL_TREE
176990075Sobrien	  && type == TYPE_MAIN_VARIANT (TREE_TYPE (field)))
177018334Speter	{
177150397Sobrien	  return field;
177218334Speter	}
177350397Sobrien
177450397Sobrien      /* Otherwise, it could be nested, search harder.  */
177550397Sobrien      if (DECL_NAME (field) == NULL_TREE
177690075Sobrien	  && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
177718334Speter	{
177850397Sobrien	  tree subfield = lookup_anon_field (TREE_TYPE (field), type);
177950397Sobrien	  if (subfield)
178050397Sobrien	    return subfield;
178118334Speter	}
178218334Speter    }
178350397Sobrien  return NULL_TREE;
178418334Speter}
178518334Speter
1786117395Skan/* Build an expression representing OBJECT.MEMBER.  OBJECT is an
1787117395Skan   expression; MEMBER is a DECL or baselink.  If ACCESS_PATH is
1788117395Skan   non-NULL, it indicates the path to the base used to name MEMBER.
1789117395Skan   If PRESERVE_REFERENCE is true, the expression returned will have
1790117395Skan   REFERENCE_TYPE if the MEMBER does.  Otherwise, the expression
1791169689Skan   returned will have the type referred to by the reference.
179218334Speter
1793117395Skan   This function does not perform access control; that is either done
1794117395Skan   earlier by the parser when the name of MEMBER is resolved to MEMBER
1795117395Skan   itself, or later when overload resolution selects one of the
1796117395Skan   functions indicated by MEMBER.  */
1797117395Skan
179818334Spetertree
1799169689Skanbuild_class_member_access_expr (tree object, tree member,
1800117395Skan				tree access_path, bool preserve_reference)
180118334Speter{
1802117395Skan  tree object_type;
1803117395Skan  tree member_scope;
1804117395Skan  tree result = NULL_TREE;
180518334Speter
1806169689Skan  if (error_operand_p (object) || error_operand_p (member))
180752284Sobrien    return error_mark_node;
180850397Sobrien
1809169689Skan  gcc_assert (DECL_P (member) || BASELINK_P (member));
1810132718Skan
1811117395Skan  /* [expr.ref]
181250397Sobrien
1813117395Skan     The type of the first expression shall be "class object" (of a
1814117395Skan     complete type).  */
1815117395Skan  object_type = TREE_TYPE (object);
1816169689Skan  if (!currently_open_class (object_type)
1817132718Skan      && !complete_type_or_else (object_type, object))
1818117395Skan    return error_mark_node;
1819117395Skan  if (!CLASS_TYPE_P (object_type))
1820117395Skan    {
1821169689Skan      error ("request for member %qD in %qE, which is of non-class type %qT",
1822117395Skan	     member, object, object_type);
1823117395Skan      return error_mark_node;
182418334Speter    }
182518334Speter
1826117395Skan  /* The standard does not seem to actually say that MEMBER must be a
1827117395Skan     member of OBJECT_TYPE.  However, that is clearly what is
1828117395Skan     intended.  */
1829117395Skan  if (DECL_P (member))
183018334Speter    {
1831117395Skan      member_scope = DECL_CLASS_CONTEXT (member);
1832117395Skan      mark_used (member);
1833117395Skan      if (TREE_DEPRECATED (member))
1834117395Skan	warn_deprecated_use (member);
183518334Speter    }
1836117395Skan  else
1837117395Skan    member_scope = BINFO_TYPE (BASELINK_BINFO (member));
1838117395Skan  /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will
1839117395Skan     presently be the anonymous union.  Go outwards until we find a
1840117395Skan     type related to OBJECT_TYPE.  */
1841117395Skan  while (ANON_AGGR_TYPE_P (member_scope)
1842117395Skan	 && !same_type_ignoring_top_level_qualifiers_p (member_scope,
1843117395Skan							object_type))
1844117395Skan    member_scope = TYPE_CONTEXT (member_scope);
1845117395Skan  if (!member_scope || !DERIVED_FROM_P (member_scope, object_type))
184650397Sobrien    {
1847132718Skan      if (TREE_CODE (member) == FIELD_DECL)
1848169689Skan	error ("invalid use of nonstatic data member %qE", member);
1849132718Skan      else
1850169689Skan	error ("%qD is not a member of %qT", member, object_type);
1851117395Skan      return error_mark_node;
185250397Sobrien    }
185318334Speter
1854132718Skan  /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into
1855132718Skan     `(*(a ?  &b : &c)).x', and so on.  A COND_EXPR is only an lvalue
1856132718Skan     in the frontend; only _DECLs and _REFs are lvalues in the backend.  */
1857132718Skan  {
1858132718Skan    tree temp = unary_complex_lvalue (ADDR_EXPR, object);
1859132718Skan    if (temp)
1860132718Skan      object = build_indirect_ref (temp, NULL);
1861132718Skan  }
1862132718Skan
1863117395Skan  /* In [expr.ref], there is an explicit list of the valid choices for
1864117395Skan     MEMBER.  We check for each of those cases here.  */
1865117395Skan  if (TREE_CODE (member) == VAR_DECL)
186618334Speter    {
1867117395Skan      /* A static data member.  */
1868117395Skan      result = member;
1869117395Skan      /* If OBJECT has side-effects, they are supposed to occur.  */
1870117395Skan      if (TREE_SIDE_EFFECTS (object))
1871169689Skan	result = build2 (COMPOUND_EXPR, TREE_TYPE (result), object, result);
187218334Speter    }
1873117395Skan  else if (TREE_CODE (member) == FIELD_DECL)
187418334Speter    {
1875117395Skan      /* A non-static data member.  */
1876117395Skan      bool null_object_p;
1877117395Skan      int type_quals;
1878117395Skan      tree member_type;
187918334Speter
1880117395Skan      null_object_p = (TREE_CODE (object) == INDIRECT_REF
1881117395Skan		       && integer_zerop (TREE_OPERAND (object, 0)));
188218334Speter
1883117395Skan      /* Convert OBJECT to the type of MEMBER.  */
1884117395Skan      if (!same_type_p (TYPE_MAIN_VARIANT (object_type),
1885117395Skan			TYPE_MAIN_VARIANT (member_scope)))
188618334Speter	{
1887117395Skan	  tree binfo;
1888117395Skan	  base_kind kind;
1889117395Skan
1890117395Skan	  binfo = lookup_base (access_path ? access_path : object_type,
1891169689Skan			       member_scope, ba_unique,  &kind);
1892117395Skan	  if (binfo == error_mark_node)
1893117395Skan	    return error_mark_node;
1894117395Skan
1895117395Skan	  /* It is invalid to try to get to a virtual base of a
1896117395Skan	     NULL object.  The most common cause is invalid use of
1897117395Skan	     offsetof macro.  */
1898117395Skan	  if (null_object_p && kind == bk_via_virtual)
1899117395Skan	    {
1900169689Skan	      error ("invalid access to non-static data member %qD of "
1901169689Skan		     "NULL object",
1902117395Skan		     member);
1903169689Skan	      error ("(perhaps the %<offsetof%> macro was used incorrectly)");
1904117395Skan	      return error_mark_node;
1905117395Skan	    }
1906117395Skan
1907117395Skan	  /* Convert to the base.  */
1908169689Skan	  object = build_base_path (PLUS_EXPR, object, binfo,
1909117395Skan				    /*nonnull=*/1);
1910117395Skan	  /* If we found the base successfully then we should be able
1911117395Skan	     to convert to it successfully.  */
1912169689Skan	  gcc_assert (object != error_mark_node);
191318334Speter	}
1914117395Skan
1915117395Skan      /* Complain about other invalid uses of offsetof, even though they will
1916117395Skan	 give the right answer.  Note that we complain whether or not they
1917117395Skan	 actually used the offsetof macro, since there's no way to know at this
1918117395Skan	 point.  So we just give a warning, instead of a pedwarn.  */
1919169689Skan      /* Do not produce this warning for base class field references, because
1920169689Skan	 we know for a fact that didn't come from offsetof.  This does occur
1921169689Skan	 in various testsuite cases where a null object is passed where a
1922169689Skan	 vtable access is required.  */
1923132718Skan      if (null_object_p && warn_invalid_offsetof
1924169689Skan	  && CLASSTYPE_NON_POD_P (object_type)
1925169689Skan	  && !DECL_FIELD_IS_BASE (member)
1926169689Skan	  && !skip_evaluation)
192718334Speter	{
1928169689Skan	  warning (0, "invalid access to non-static data member %qD of NULL object",
1929117395Skan		   member);
1930169689Skan	  warning (0, "(perhaps the %<offsetof%> macro was used incorrectly)");
193118334Speter	}
1932117395Skan
1933117395Skan      /* If MEMBER is from an anonymous aggregate, we have converted
1934117395Skan	 OBJECT so that it refers to the class containing the
1935117395Skan	 anonymous union.  Generate a reference to the anonymous union
1936117395Skan	 itself, and recur to find MEMBER.  */
1937117395Skan      if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member))
1938117395Skan	  /* When this code is called from build_field_call, the
1939117395Skan	     object already has the type of the anonymous union.
1940117395Skan	     That is because the COMPONENT_REF was already
1941117395Skan	     constructed, and was then disassembled before calling
1942117395Skan	     build_field_call.  After the function-call code is
1943117395Skan	     cleaned up, this waste can be eliminated.  */
1944169689Skan	  && (!same_type_ignoring_top_level_qualifiers_p
1945117395Skan	      (TREE_TYPE (object), DECL_CONTEXT (member))))
1946117395Skan	{
1947117395Skan	  tree anonymous_union;
1948117395Skan
1949117395Skan	  anonymous_union = lookup_anon_field (TREE_TYPE (object),
1950117395Skan					       DECL_CONTEXT (member));
1951117395Skan	  object = build_class_member_access_expr (object,
1952117395Skan						   anonymous_union,
1953117395Skan						   /*access_path=*/NULL_TREE,
1954117395Skan						   preserve_reference);
1955117395Skan	}
1956117395Skan
1957117395Skan      /* Compute the type of the field, as described in [expr.ref].  */
1958117395Skan      type_quals = TYPE_UNQUALIFIED;
1959117395Skan      member_type = TREE_TYPE (member);
1960117395Skan      if (TREE_CODE (member_type) != REFERENCE_TYPE)
1961117395Skan	{
1962169689Skan	  type_quals = (cp_type_quals (member_type)
1963117395Skan			| cp_type_quals (object_type));
1964169689Skan
1965117395Skan	  /* A field is const (volatile) if the enclosing object, or the
1966117395Skan	     field itself, is const (volatile).  But, a mutable field is
1967117395Skan	     not const, even within a const object.  */
1968117395Skan	  if (DECL_MUTABLE_P (member))
1969117395Skan	    type_quals &= ~TYPE_QUAL_CONST;
1970117395Skan	  member_type = cp_build_qualified_type (member_type, type_quals);
1971117395Skan	}
1972117395Skan
1973169689Skan      result = build3 (COMPONENT_REF, member_type, object, member,
1974169689Skan		       NULL_TREE);
1975169689Skan      result = fold_if_not_in_template (result);
1976117395Skan
1977117395Skan      /* Mark the expression const or volatile, as appropriate.  Even
1978117395Skan	 though we've dealt with the type above, we still have to mark the
1979117395Skan	 expression itself.  */
1980117395Skan      if (type_quals & TYPE_QUAL_CONST)
1981117395Skan	TREE_READONLY (result) = 1;
1982169689Skan      if (type_quals & TYPE_QUAL_VOLATILE)
1983117395Skan	TREE_THIS_VOLATILE (result) = 1;
198418334Speter    }
1985117395Skan  else if (BASELINK_P (member))
1986117395Skan    {
1987117395Skan      /* The member is a (possibly overloaded) member function.  */
1988117395Skan      tree functions;
1989132718Skan      tree type;
199018334Speter
1991117395Skan      /* If the MEMBER is exactly one static member function, then we
1992117395Skan	 know the type of the expression.  Otherwise, we must wait
1993117395Skan	 until overload resolution has been performed.  */
1994117395Skan      functions = BASELINK_FUNCTIONS (member);
1995117395Skan      if (TREE_CODE (functions) == FUNCTION_DECL
1996117395Skan	  && DECL_STATIC_FUNCTION_P (functions))
1997132718Skan	type = TREE_TYPE (functions);
1998117395Skan      else
1999132718Skan	type = unknown_type_node;
2000132718Skan      /* Note that we do not convert OBJECT to the BASELINK_BINFO
2001132718Skan	 base.  That will happen when the function is called.  */
2002169689Skan      result = build3 (COMPONENT_REF, type, object, member, NULL_TREE);
2003117395Skan    }
2004117395Skan  else if (TREE_CODE (member) == CONST_DECL)
200550397Sobrien    {
2006117395Skan      /* The member is an enumerator.  */
2007117395Skan      result = member;
2008117395Skan      /* If OBJECT has side-effects, they are supposed to occur.  */
2009117395Skan      if (TREE_SIDE_EFFECTS (object))
2010169689Skan	result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
2011169689Skan			 object, result);
201250397Sobrien    }
2013117395Skan  else
201490075Sobrien    {
2015169689Skan      error ("invalid use of %qD", member);
201690075Sobrien      return error_mark_node;
201790075Sobrien    }
201818334Speter
2019117395Skan  if (!preserve_reference)
2020117395Skan    /* [expr.ref]
2021169689Skan
2022117395Skan       If E2 is declared to have type "reference to T", then ... the
2023117395Skan       type of E1.E2 is T.  */
2024117395Skan    result = convert_from_reference (result);
202552284Sobrien
2026117395Skan  return result;
2027117395Skan}
202818334Speter
2029132718Skan/* Return the destructor denoted by OBJECT.SCOPE::~DTOR_NAME, or, if
2030132718Skan   SCOPE is NULL, by OBJECT.~DTOR_NAME.  */
2031132718Skan
2032132718Skanstatic tree
2033132718Skanlookup_destructor (tree object, tree scope, tree dtor_name)
2034132718Skan{
2035132718Skan  tree object_type = TREE_TYPE (object);
2036132718Skan  tree dtor_type = TREE_OPERAND (dtor_name, 0);
2037132718Skan  tree expr;
2038132718Skan
2039169689Skan  if (scope && !check_dtor_name (scope, dtor_type))
2040132718Skan    {
2041169689Skan      error ("qualified type %qT does not match destructor name ~%qT",
2042132718Skan	     scope, dtor_type);
2043132718Skan      return error_mark_node;
2044132718Skan    }
2045132718Skan  if (!DERIVED_FROM_P (dtor_type, TYPE_MAIN_VARIANT (object_type)))
2046132718Skan    {
2047169689Skan      error ("the type being destroyed is %qT, but the destructor refers to %qT",
2048132718Skan	     TYPE_MAIN_VARIANT (object_type), dtor_type);
2049132718Skan      return error_mark_node;
2050132718Skan    }
2051132718Skan  expr = lookup_member (dtor_type, complete_dtor_identifier,
2052132718Skan			/*protect=*/1, /*want_type=*/false);
2053132718Skan  expr = (adjust_result_of_qualified_name_lookup
2054132718Skan	  (expr, dtor_type, object_type));
2055132718Skan  return expr;
2056132718Skan}
2057132718Skan
2058169689Skan/* An expression of the form "A::template B" has been resolved to
2059169689Skan   DECL.  Issue a diagnostic if B is not a template or template
2060169689Skan   specialization.  */
2061169689Skan
2062169689Skanvoid
2063169689Skancheck_template_keyword (tree decl)
2064169689Skan{
2065169689Skan  /* The standard says:
2066169689Skan
2067169689Skan      [temp.names]
2068169689Skan
2069169689Skan      If a name prefixed by the keyword template is not a member
2070169689Skan      template, the program is ill-formed.
2071169689Skan
2072169689Skan     DR 228 removed the restriction that the template be a member
2073169689Skan     template.
2074169689Skan
2075169689Skan     DR 96, if accepted would add the further restriction that explicit
2076169689Skan     template arguments must be provided if the template keyword is
2077169689Skan     used, but, as of 2005-10-16, that DR is still in "drafting".  If
2078169689Skan     this DR is accepted, then the semantic checks here can be
2079169689Skan     simplified, as the entity named must in fact be a template
2080169689Skan     specialization, rather than, as at present, a set of overloaded
2081169689Skan     functions containing at least one template function.  */
2082169689Skan  if (TREE_CODE (decl) != TEMPLATE_DECL
2083169689Skan      && TREE_CODE (decl) != TEMPLATE_ID_EXPR)
2084169689Skan    {
2085169689Skan      if (!is_overloaded_fn (decl))
2086169689Skan	pedwarn ("%qD is not a template", decl);
2087169689Skan      else
2088169689Skan	{
2089169689Skan	  tree fns;
2090169689Skan	  fns = decl;
2091169689Skan	  if (BASELINK_P (fns))
2092169689Skan	    fns = BASELINK_FUNCTIONS (fns);
2093169689Skan	  while (fns)
2094169689Skan	    {
2095169689Skan	      tree fn = OVL_CURRENT (fns);
2096169689Skan	      if (TREE_CODE (fn) == TEMPLATE_DECL
2097169689Skan		  || TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2098169689Skan		break;
2099169689Skan	      if (TREE_CODE (fn) == FUNCTION_DECL
2100169689Skan		  && DECL_USE_TEMPLATE (fn)
2101169689Skan		  && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (fn)))
2102169689Skan		break;
2103169689Skan	      fns = OVL_NEXT (fns);
2104169689Skan	    }
2105169689Skan	  if (!fns)
2106169689Skan	    pedwarn ("%qD is not a template", decl);
2107169689Skan	}
2108169689Skan    }
2109169689Skan}
2110169689Skan
2111117395Skan/* This function is called by the parser to process a class member
2112117395Skan   access expression of the form OBJECT.NAME.  NAME is a node used by
2113117395Skan   the parser to represent a name; it is not yet a DECL.  It may,
2114117395Skan   however, be a BASELINK where the BASELINK_FUNCTIONS is a
2115117395Skan   TEMPLATE_ID_EXPR.  Templates must be looked up by the parser, and
2116117395Skan   there is no reason to do the lookup twice, so the parser keeps the
2117169689Skan   BASELINK.  TEMPLATE_P is true iff NAME was explicitly declared to
2118169689Skan   be a template via the use of the "A::template B" syntax.  */
211918334Speter
2120117395Skantree
2121169689Skanfinish_class_member_access_expr (tree object, tree name, bool template_p)
2122117395Skan{
2123132718Skan  tree expr;
2124117395Skan  tree object_type;
2125117395Skan  tree member;
2126117395Skan  tree access_path = NULL_TREE;
2127132718Skan  tree orig_object = object;
2128132718Skan  tree orig_name = name;
212918334Speter
2130117395Skan  if (object == error_mark_node || name == error_mark_node)
2131117395Skan    return error_mark_node;
213290075Sobrien
2133169689Skan  /* If OBJECT is an ObjC class instance, we must obey ObjC access rules.  */
2134169689Skan  if (!objc_is_public (object, name))
2135169689Skan    return error_mark_node;
2136169689Skan
2137132718Skan  object_type = TREE_TYPE (object);
2138132718Skan
2139117395Skan  if (processing_template_decl)
2140132718Skan    {
2141132718Skan      if (/* If OBJECT_TYPE is dependent, so is OBJECT.NAME.  */
2142132718Skan	  dependent_type_p (object_type)
2143132718Skan	  /* If NAME is just an IDENTIFIER_NODE, then the expression
2144132718Skan	     is dependent.  */
2145132718Skan	  || TREE_CODE (object) == IDENTIFIER_NODE
2146132718Skan	  /* If NAME is "f<args>", where either 'f' or 'args' is
2147132718Skan	     dependent, then the expression is dependent.  */
2148132718Skan	  || (TREE_CODE (name) == TEMPLATE_ID_EXPR
2149132718Skan	      && dependent_template_id_p (TREE_OPERAND (name, 0),
2150132718Skan					  TREE_OPERAND (name, 1)))
2151132718Skan	  /* If NAME is "T::X" where "T" is dependent, then the
2152132718Skan	     expression is dependent.  */
2153132718Skan	  || (TREE_CODE (name) == SCOPE_REF
2154132718Skan	      && TYPE_P (TREE_OPERAND (name, 0))
2155132718Skan	      && dependent_type_p (TREE_OPERAND (name, 0))))
2156169689Skan	return build_min_nt (COMPONENT_REF, object, name, NULL_TREE);
2157132718Skan      object = build_non_dependent_expr (object);
2158132718Skan    }
215918334Speter
2160117395Skan  /* [expr.ref]
216190075Sobrien
2162117395Skan     The type of the first expression shall be "class object" (of a
2163117395Skan     complete type).  */
2164169689Skan  if (!currently_open_class (object_type)
2165132718Skan      && !complete_type_or_else (object_type, object))
2166117395Skan    return error_mark_node;
2167117395Skan  if (!CLASS_TYPE_P (object_type))
2168117395Skan    {
2169169689Skan      error ("request for member %qD in %qE, which is of non-class type %qT",
2170117395Skan	     name, object, object_type);
2171117395Skan      return error_mark_node;
2172117395Skan    }
217396263Sobrien
2174117395Skan  if (BASELINK_P (name))
2175169689Skan    /* A member function that has already been looked up.  */
2176169689Skan    member = name;
2177117395Skan  else
2178117395Skan    {
2179117395Skan      bool is_template_id = false;
2180117395Skan      tree template_args = NULL_TREE;
2181132718Skan      tree scope;
218250397Sobrien
2183117395Skan      if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
218450397Sobrien	{
2185117395Skan	  is_template_id = true;
2186117395Skan	  template_args = TREE_OPERAND (name, 1);
2187117395Skan	  name = TREE_OPERAND (name, 0);
2188132718Skan
2189132718Skan	  if (TREE_CODE (name) == OVERLOAD)
2190132718Skan	    name = DECL_NAME (get_first_fn (name));
2191132718Skan	  else if (DECL_P (name))
2192132718Skan	    name = DECL_NAME (name);
2193117395Skan	}
219496263Sobrien
2195117395Skan      if (TREE_CODE (name) == SCOPE_REF)
2196117395Skan	{
2197169689Skan	  /* A qualified name.  The qualifying class or namespace `S'
2198169689Skan	     has already been looked up; it is either a TYPE or a
2199169689Skan	     NAMESPACE_DECL.  */
2200117395Skan	  scope = TREE_OPERAND (name, 0);
2201117395Skan	  name = TREE_OPERAND (name, 1);
2202117395Skan
2203117395Skan	  /* If SCOPE is a namespace, then the qualified name does not
2204117395Skan	     name a member of OBJECT_TYPE.  */
2205117395Skan	  if (TREE_CODE (scope) == NAMESPACE_DECL)
220650397Sobrien	    {
2207169689Skan	      error ("%<%D::%D%> is not a member of %qT",
2208117395Skan		     scope, name, object_type);
220950397Sobrien	      return error_mark_node;
221050397Sobrien	    }
2211117395Skan
2212169689Skan	  gcc_assert (CLASS_TYPE_P (scope));
2213169689Skan	  gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE
2214169689Skan		      || TREE_CODE (name) == BIT_NOT_EXPR);
2215169689Skan
2216117395Skan	  /* Find the base of OBJECT_TYPE corresponding to SCOPE.  */
2217117395Skan	  access_path = lookup_base (object_type, scope, ba_check, NULL);
2218122180Skan	  if (access_path == error_mark_node)
221990075Sobrien	    return error_mark_node;
2220122180Skan	  if (!access_path)
2221122180Skan	    {
2222169689Skan	      error ("%qT is not a base of %qT", scope, object_type);
2223122180Skan	      return error_mark_node;
2224122180Skan	    }
222550397Sobrien	}
2226132718Skan      else
222750397Sobrien	{
2228132718Skan	  scope = NULL_TREE;
2229132718Skan	  access_path = object_type;
223050397Sobrien	}
2231132718Skan
2232132718Skan      if (TREE_CODE (name) == BIT_NOT_EXPR)
2233132718Skan	member = lookup_destructor (object, scope, name);
2234132718Skan      else
2235117395Skan	{
2236132718Skan	  /* Look up the member.  */
2237169689Skan	  member = lookup_member (access_path, name, /*protect=*/1,
2238132718Skan				  /*want_type=*/false);
2239117395Skan	  if (member == NULL_TREE)
2240117395Skan	    {
2241169689Skan	      error ("%qD has no member named %qE", object_type, name);
2242117395Skan	      return error_mark_node;
2243117395Skan	    }
2244132718Skan	  if (member == error_mark_node)
2245117395Skan	    return error_mark_node;
2246117395Skan	}
2247169689Skan
2248117395Skan      if (is_template_id)
2249117395Skan	{
2250117395Skan	  tree template = member;
2251169689Skan
2252117395Skan	  if (BASELINK_P (template))
2253132718Skan	    template = lookup_template_function (template, template_args);
2254117395Skan	  else
2255117395Skan	    {
2256169689Skan	      error ("%qD is not a member template function", name);
2257117395Skan	      return error_mark_node;
2258117395Skan	    }
2259117395Skan	}
226018334Speter    }
226150397Sobrien
2262132718Skan  if (TREE_DEPRECATED (member))
2263132718Skan    warn_deprecated_use (member);
2264132718Skan
2265169689Skan  if (template_p)
2266169689Skan    check_template_keyword (member);
2267169689Skan
2268132718Skan  expr = build_class_member_access_expr (object, member, access_path,
2269117395Skan					 /*preserve_reference=*/false);
2270132718Skan  if (processing_template_decl && expr != error_mark_node)
2271169689Skan    {
2272169689Skan      if (BASELINK_P (member))
2273169689Skan	{
2274169689Skan	  if (TREE_CODE (orig_name) == SCOPE_REF)
2275169689Skan	    BASELINK_QUALIFIED_P (member) = 1;
2276169689Skan	  orig_name = member;
2277169689Skan	}
2278169689Skan      return build_min_non_dep (COMPONENT_REF, expr,
2279169689Skan				orig_object, orig_name,
2280169689Skan				NULL_TREE);
2281169689Skan    }
2282169689Skan
2283132718Skan  return expr;
228418334Speter}
228550397Sobrien
2286117395Skan/* Return an expression for the MEMBER_NAME field in the internal
2287117395Skan   representation of PTRMEM, a pointer-to-member function.  (Each
2288117395Skan   pointer-to-member function type gets its own RECORD_TYPE so it is
2289117395Skan   more convenient to access the fields by name than by FIELD_DECL.)
2290117395Skan   This routine converts the NAME to a FIELD_DECL and then creates the
2291117395Skan   node for the complete expression.  */
229250397Sobrien
229350397Sobrientree
2294117395Skanbuild_ptrmemfunc_access_expr (tree ptrmem, tree member_name)
229550397Sobrien{
2296117395Skan  tree ptrmem_type;
2297117395Skan  tree member;
2298117395Skan  tree member_type;
229950397Sobrien
2300117395Skan  /* This code is a stripped down version of
2301117395Skan     build_class_member_access_expr.  It does not work to use that
2302117395Skan     routine directly because it expects the object to be of class
2303117395Skan     type.  */
2304117395Skan  ptrmem_type = TREE_TYPE (ptrmem);
2305169689Skan  gcc_assert (TYPE_PTRMEMFUNC_P (ptrmem_type));
2306117395Skan  member = lookup_member (ptrmem_type, member_name, /*protect=*/0,
2307132718Skan			  /*want_type=*/false);
2308117395Skan  member_type = cp_build_qualified_type (TREE_TYPE (member),
2309117395Skan					 cp_type_quals (ptrmem_type));
2310169689Skan  return fold_build3 (COMPONENT_REF, member_type,
2311169689Skan		      ptrmem, member, NULL_TREE);
2312117395Skan}
231350397Sobrien
231418334Speter/* Given an expression PTR for a pointer, return an expression
231518334Speter   for the value pointed to.
231618334Speter   ERRORSTRING is the name of the operator to appear in error messages.
231718334Speter
231818334Speter   This function may need to overload OPERATOR_FNNAME.
231918334Speter   Must also handle REFERENCE_TYPEs for C++.  */
232018334Speter
232118334Spetertree
2322132718Skanbuild_x_indirect_ref (tree expr, const char *errorstring)
232318334Speter{
2324132718Skan  tree orig_expr = expr;
232550397Sobrien  tree rval;
232650397Sobrien
232750397Sobrien  if (processing_template_decl)
2328132718Skan    {
2329132718Skan      if (type_dependent_expression_p (expr))
2330132718Skan	return build_min_nt (INDIRECT_REF, expr);
2331132718Skan      expr = build_non_dependent_expr (expr);
2332132718Skan    }
233350397Sobrien
2334132718Skan  rval = build_new_op (INDIRECT_REF, LOOKUP_NORMAL, expr, NULL_TREE,
2335132718Skan		       NULL_TREE, /*overloaded_p=*/NULL);
2336132718Skan  if (!rval)
2337132718Skan    rval = build_indirect_ref (expr, errorstring);
2338132718Skan
2339132718Skan  if (processing_template_decl && rval != error_mark_node)
2340132718Skan    return build_min_non_dep (INDIRECT_REF, rval, orig_expr);
2341132718Skan  else
234218334Speter    return rval;
234318334Speter}
234418334Speter
234518334Spetertree
2346132718Skanbuild_indirect_ref (tree ptr, const char *errorstring)
234718334Speter{
2348132718Skan  tree pointer, type;
234918334Speter
235050397Sobrien  if (ptr == error_mark_node)
235150397Sobrien    return error_mark_node;
235218334Speter
235352284Sobrien  if (ptr == current_class_ptr)
235452284Sobrien    return current_class_ref;
235552284Sobrien
235650397Sobrien  pointer = (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
2357132718Skan	     ? ptr : decay_conversion (ptr));
235850397Sobrien  type = TREE_TYPE (pointer);
235918334Speter
2360169689Skan  if (POINTER_TYPE_P (type))
236152284Sobrien    {
236252284Sobrien      /* [expr.unary.op]
2363169689Skan
236452284Sobrien	 If the type of the expression is "pointer to T," the type
2365169689Skan	 of  the  result  is  "T."
236618334Speter
2367169689Skan	 We must use the canonical variant because certain parts of
236852284Sobrien	 the back end, like fold, do pointer comparisons between
236952284Sobrien	 types.  */
237052284Sobrien      tree t = canonical_type_variant (TREE_TYPE (type));
237152284Sobrien
2372258501Spfg      if (TREE_CODE (ptr) == CONVERT_EXPR
2373258501Spfg          || TREE_CODE (ptr) == NOP_EXPR
2374258501Spfg          || TREE_CODE (ptr) == VIEW_CONVERT_EXPR)
2375258501Spfg	{
2376258501Spfg	  /* If a warning is issued, mark it to avoid duplicates from
2377258501Spfg	     the backend.  This only needs to be done at
2378258501Spfg	     warn_strict_aliasing > 2.  */
2379258501Spfg	  if (warn_strict_aliasing > 2)
2380258501Spfg	    if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (ptr, 0)),
2381258501Spfg					 type, TREE_OPERAND (ptr, 0)))
2382258501Spfg	      TREE_NO_WARNING (ptr) = 1;
2383258501Spfg	}
2384258501Spfg
238590075Sobrien      if (VOID_TYPE_P (t))
2386169689Skan	{
2387169689Skan	  /* A pointer to incomplete type (other than cv void) can be
2388169689Skan	     dereferenced [expr.unary.op]/1  */
2389169689Skan	  error ("%qT is not a pointer-to-object type", type);
2390169689Skan	  return error_mark_node;
2391169689Skan	}
239290075Sobrien      else if (TREE_CODE (pointer) == ADDR_EXPR
2393117395Skan	       && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
239452284Sobrien	/* The POINTER was something like `&x'.  We simplify `*&x' to
239552284Sobrien	   `x'.  */
239618334Speter	return TREE_OPERAND (pointer, 0);
239718334Speter      else
239818334Speter	{
239952284Sobrien	  tree ref = build1 (INDIRECT_REF, t, pointer);
240018334Speter
240150397Sobrien	  /* We *must* set TREE_READONLY when dereferencing a pointer to const,
240250397Sobrien	     so that we get the proper error message if the result is used
240350397Sobrien	     to assign to.  Also, &* is supposed to be a no-op.  */
240452284Sobrien	  TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
240552284Sobrien	  TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
240650397Sobrien	  TREE_SIDE_EFFECTS (ref)
2407132718Skan	    = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer));
240818334Speter	  return ref;
240918334Speter	}
241018334Speter    }
241118334Speter  /* `pointer' won't be an error_mark_node if we were given a
241218334Speter     pointer to member, so it's cool to check for this here.  */
2413132718Skan  else if (TYPE_PTR_TO_MEMBER_P (type))
2414169689Skan    error ("invalid use of %qs on pointer to member", errorstring);
241518334Speter  else if (pointer != error_mark_node)
241618334Speter    {
241718334Speter      if (errorstring)
2418169689Skan	error ("invalid type argument of %qs", errorstring);
241918334Speter      else
242018334Speter	error ("invalid type argument");
242118334Speter    }
242218334Speter  return error_mark_node;
242318334Speter}
242418334Speter
242518334Speter/* This handles expressions of the form "a[i]", which denotes
242618334Speter   an array reference.
242718334Speter
242818334Speter   This is logically equivalent in C to *(a+i), but we may do it differently.
242918334Speter   If A is a variable or a member, we generate a primitive ARRAY_REF.
243018334Speter   This avoids forcing the array out of registers, and can work on
243118334Speter   arrays that are not lvalues (for example, members of structures returned
243218334Speter   by functions).
243318334Speter
243418334Speter   If INDEX is of some user-defined type, it must be converted to
243518334Speter   integer type.  Otherwise, to make a compatible PLUS_EXPR, it
243618334Speter   will inherit the type of the array, which will be some pointer type.  */
243718334Speter
243818334Spetertree
2439132718Skanbuild_array_ref (tree array, tree idx)
244018334Speter{
244118334Speter  if (idx == 0)
244218334Speter    {
244318334Speter      error ("subscript missing in array reference");
244418334Speter      return error_mark_node;
244518334Speter    }
244618334Speter
244718334Speter  if (TREE_TYPE (array) == error_mark_node
244818334Speter      || TREE_TYPE (idx) == error_mark_node)
244918334Speter    return error_mark_node;
245018334Speter
245190075Sobrien  /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
245290075Sobrien     inside it.  */
245390075Sobrien  switch (TREE_CODE (array))
245490075Sobrien    {
245590075Sobrien    case COMPOUND_EXPR:
245690075Sobrien      {
245790075Sobrien	tree value = build_array_ref (TREE_OPERAND (array, 1), idx);
2458169689Skan	return build2 (COMPOUND_EXPR, TREE_TYPE (value),
2459169689Skan		       TREE_OPERAND (array, 0), value);
246090075Sobrien      }
246190075Sobrien
246290075Sobrien    case COND_EXPR:
246390075Sobrien      return build_conditional_expr
246490075Sobrien	(TREE_OPERAND (array, 0),
246590075Sobrien	 build_array_ref (TREE_OPERAND (array, 1), idx),
246690075Sobrien	 build_array_ref (TREE_OPERAND (array, 2), idx));
246790075Sobrien
246890075Sobrien    default:
246990075Sobrien      break;
247090075Sobrien    }
247190075Sobrien
2472169689Skan  if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
247318334Speter    {
247418334Speter      tree rval, type;
247518334Speter
2476169689Skan      warn_array_subscript_with_type_char (idx);
247718334Speter
2478132718Skan      if (!INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (idx)))
247918334Speter	{
248018334Speter	  error ("array subscript is not an integer");
248118334Speter	  return error_mark_node;
248218334Speter	}
248318334Speter
2484132718Skan      /* Apply integral promotions *after* noticing character types.
2485132718Skan	 (It is unclear why we do these promotions -- the standard
2486169689Skan	 does not say that we should.  In fact, the natural thing would
2487132718Skan	 seem to be to convert IDX to ptrdiff_t; we're performing
2488132718Skan	 pointer arithmetic.)  */
2489132718Skan      idx = perform_integral_promotions (idx);
2490132718Skan
249118334Speter      /* An array that is indexed by a non-constant
249218334Speter	 cannot be stored in a register; we must be able to do
249318334Speter	 address arithmetic on its address.
249418334Speter	 Likewise an array of elements of variable size.  */
249518334Speter      if (TREE_CODE (idx) != INTEGER_CST
249690075Sobrien	  || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
249750397Sobrien	      && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
249850397Sobrien		  != INTEGER_CST)))
249918334Speter	{
2500117395Skan	  if (!cxx_mark_addressable (array))
250118334Speter	    return error_mark_node;
250218334Speter	}
250390075Sobrien
250418334Speter      /* An array that is indexed by a constant value which is not within
250518334Speter	 the array bounds cannot be stored in a register either; because we
250618334Speter	 would get a crash in store_bit_field/extract_bit_field when trying
250718334Speter	 to access a non-existent part of the register.  */
250818334Speter      if (TREE_CODE (idx) == INTEGER_CST
2509169689Skan	  && TYPE_DOMAIN (TREE_TYPE (array))
2510169689Skan	  && ! int_fits_type_p (idx, TYPE_DOMAIN (TREE_TYPE (array))))
251118334Speter	{
2512117395Skan	  if (!cxx_mark_addressable (array))
251318334Speter	    return error_mark_node;
251418334Speter	}
251518334Speter
251618334Speter      if (pedantic && !lvalue_p (array))
251790075Sobrien	pedwarn ("ISO C++ forbids subscripting non-lvalue array");
251818334Speter
251918334Speter      /* Note in C++ it is valid to subscript a `register' array, since
252018334Speter	 it is valid to take the address of something with that
252118334Speter	 storage specification.  */
252218334Speter      if (extra_warnings)
252318334Speter	{
252418334Speter	  tree foo = array;
252518334Speter	  while (TREE_CODE (foo) == COMPONENT_REF)
252618334Speter	    foo = TREE_OPERAND (foo, 0);
252718334Speter	  if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo))
2528169689Skan	    warning (OPT_Wextra, "subscripting array declared %<register%>");
252918334Speter	}
253018334Speter
253152284Sobrien      type = TREE_TYPE (TREE_TYPE (array));
2532169689Skan      rval = build4 (ARRAY_REF, type, array, idx, NULL_TREE, NULL_TREE);
253318334Speter      /* Array ref is const/volatile if the array elements are
253418334Speter	 or if the array is..  */
253518334Speter      TREE_READONLY (rval)
253652284Sobrien	|= (CP_TYPE_CONST_P (type) | TREE_READONLY (array));
253718334Speter      TREE_SIDE_EFFECTS (rval)
253852284Sobrien	|= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array));
253918334Speter      TREE_THIS_VOLATILE (rval)
254052284Sobrien	|= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array));
2541169689Skan      return require_complete_type (fold_if_not_in_template (rval));
254218334Speter    }
254318334Speter
254418334Speter  {
254518334Speter    tree ar = default_conversion (array);
254618334Speter    tree ind = default_conversion (idx);
254718334Speter
254818334Speter    /* Put the integer in IND to simplify error checking.  */
254918334Speter    if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
255018334Speter      {
255118334Speter	tree temp = ar;
255218334Speter	ar = ind;
255318334Speter	ind = temp;
255418334Speter      }
255518334Speter
255618334Speter    if (ar == error_mark_node)
255718334Speter      return ar;
255818334Speter
255918334Speter    if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE)
256018334Speter      {
256118334Speter	error ("subscripted value is neither array nor pointer");
256218334Speter	return error_mark_node;
256318334Speter      }
256418334Speter    if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
256518334Speter      {
256618334Speter	error ("array subscript is not an integer");
256718334Speter	return error_mark_node;
256818334Speter      }
256918334Speter
257090075Sobrien    return build_indirect_ref (cp_build_binary_op (PLUS_EXPR, ar, ind),
257118334Speter			       "array indexing");
257218334Speter  }
257318334Speter}
257418334Speter
2575117395Skan/* Resolve a pointer to member function.  INSTANCE is the object
2576117395Skan   instance to use, if the member points to a virtual member.
257718334Speter
2578117395Skan   This used to avoid checking for virtual functions if basetype
2579117395Skan   has no virtual functions, according to an earlier ANSI draft.
2580117395Skan   With the final ISO C++ rules, such an optimization is
2581117395Skan   incorrect: A pointer to a derived member can be static_cast
2582117395Skan   to pointer-to-base-member, as long as the dynamic object
2583117395Skan   later has the right member.  */
258418334Speter
258518334Spetertree
2586132718Skanget_member_function_from_ptrfunc (tree *instance_ptrptr, tree function)
258718334Speter{
258818334Speter  if (TREE_CODE (function) == OFFSET_REF)
258990075Sobrien    function = TREE_OPERAND (function, 1);
259018334Speter
259118334Speter  if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
259218334Speter    {
2593117395Skan      tree idx, delta, e1, e2, e3, vtbl, basetype;
2594117395Skan      tree fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
259518334Speter
259618334Speter      tree instance_ptr = *instance_ptrptr;
2597117395Skan      tree instance_save_expr = 0;
2598117395Skan      if (instance_ptr == error_mark_node)
259990075Sobrien	{
2600117395Skan	  if (TREE_CODE (function) == PTRMEM_CST)
2601117395Skan	    {
2602117395Skan	      /* Extracting the function address from a pmf is only
2603117395Skan		 allowed with -Wno-pmf-conversions. It only works for
2604117395Skan		 pmf constants.  */
2605117395Skan	      e1 = build_addr_func (PTRMEM_CST_MEMBER (function));
2606117395Skan	      e1 = convert (fntype, e1);
2607117395Skan	      return e1;
2608117395Skan	    }
2609117395Skan	  else
2610117395Skan	    {
2611169689Skan	      error ("object missing in use of %qE", function);
2612117395Skan	      return error_mark_node;
2613117395Skan	    }
261490075Sobrien	}
261590075Sobrien
261618334Speter      if (TREE_SIDE_EFFECTS (instance_ptr))
2617117395Skan	instance_ptr = instance_save_expr = save_expr (instance_ptr);
261818334Speter
261918334Speter      if (TREE_SIDE_EFFECTS (function))
262018334Speter	function = save_expr (function);
262118334Speter
2622117395Skan      /* Start by extracting all the information from the PMF itself.  */
2623169689Skan      e3 = pfn_from_ptrmemfunc (function);
2624117395Skan      delta = build_ptrmemfunc_access_expr (function, delta_identifier);
2625117395Skan      idx = build1 (NOP_EXPR, vtable_index_type, e3);
262690075Sobrien      switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
262718334Speter	{
262890075Sobrien	case ptrmemfunc_vbit_in_pfn:
2629117395Skan	  e1 = cp_build_binary_op (BIT_AND_EXPR, idx, integer_one_node);
2630117395Skan	  idx = cp_build_binary_op (MINUS_EXPR, idx, integer_one_node);
263190075Sobrien	  break;
263218334Speter
263390075Sobrien	case ptrmemfunc_vbit_in_delta:
2634117395Skan	  e1 = cp_build_binary_op (BIT_AND_EXPR, delta, integer_one_node);
2635117395Skan	  delta = cp_build_binary_op (RSHIFT_EXPR, delta, integer_one_node);
263690075Sobrien	  break;
263752284Sobrien
263890075Sobrien	default:
2639169689Skan	  gcc_unreachable ();
264090075Sobrien	}
264152284Sobrien
2642169689Skan      /* Convert down to the right base before using the instance.  A
2643169689Skan	 special case is that in a pointer to member of class C, C may
2644169689Skan	 be incomplete.  In that case, the function will of course be
2645169689Skan	 a member of C, and no conversion is required.  In fact,
2646169689Skan	 lookup_base will fail in that case, because incomplete
2647169689Skan	 classes do not have BINFOs.  */
2648117395Skan      basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
2649169689Skan      if (!same_type_ignoring_top_level_qualifiers_p
2650169689Skan	  (basetype, TREE_TYPE (TREE_TYPE (instance_ptr))))
2651169689Skan	{
2652169689Skan	  basetype = lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr)),
2653169689Skan				  basetype, ba_check, NULL);
2654169689Skan	  instance_ptr = build_base_path (PLUS_EXPR, instance_ptr, basetype,
2655169689Skan					  1);
2656169689Skan	  if (instance_ptr == error_mark_node)
2657169689Skan	    return error_mark_node;
2658169689Skan	}
2659117395Skan      /* ...and then the delta in the PMF.  */
2660169689Skan      instance_ptr = build2 (PLUS_EXPR, TREE_TYPE (instance_ptr),
2661169689Skan			     instance_ptr, delta);
2662117395Skan
2663117395Skan      /* Hand back the adjusted 'this' argument to our caller.  */
2664117395Skan      *instance_ptrptr = instance_ptr;
2665117395Skan
2666117395Skan      /* Next extract the vtable pointer from the object.  */
2667117395Skan      vtbl = build1 (NOP_EXPR, build_pointer_type (vtbl_ptr_type_node),
2668117395Skan		     instance_ptr);
266990075Sobrien      vtbl = build_indirect_ref (vtbl, NULL);
267052284Sobrien
2671117395Skan      /* Finally, extract the function pointer from the vtable.  */
2672169689Skan      e2 = fold_build2 (PLUS_EXPR, TREE_TYPE (vtbl), vtbl, idx);
2673117395Skan      e2 = build_indirect_ref (e2, NULL);
2674117395Skan      TREE_CONSTANT (e2) = 1;
2675169689Skan      TREE_INVARIANT (e2) = 1;
2676117395Skan
267790075Sobrien      /* When using function descriptors, the address of the
267890075Sobrien	 vtable entry is treated as a function pointer.  */
267990075Sobrien      if (TARGET_VTABLE_USES_DESCRIPTORS)
268090075Sobrien	e2 = build1 (NOP_EXPR, TREE_TYPE (e2),
268190075Sobrien		     build_unary_op (ADDR_EXPR, e2, /*noconvert=*/1));
268252284Sobrien
268390075Sobrien      TREE_TYPE (e2) = TREE_TYPE (e3);
268490075Sobrien      e1 = build_conditional_expr (e1, e2, e3);
2685169689Skan
268690075Sobrien      /* Make sure this doesn't get evaluated first inside one of the
268790075Sobrien	 branches of the COND_EXPR.  */
2688117395Skan      if (instance_save_expr)
2689169689Skan	e1 = build2 (COMPOUND_EXPR, TREE_TYPE (e1),
2690169689Skan		     instance_save_expr, e1);
269118334Speter
269250397Sobrien      function = e1;
269318334Speter    }
269418334Speter  return function;
269518334Speter}
269618334Speter
2697260311Spfg/* APPLE LOCAL begin blocks 6040305 (cm) */
2698260311Spfg/* APPLE LOCAL begin radar 5847213 - radar 6329245 */
2699260311Spfg/**
2700260311Spfg build_block_call - Routine to build a block call; as in:
2701260311Spfg  ((double(*)(void *, int))(BLOCK_PTR_EXP->__FuncPtr))(I, 42);
2702260311Spfg FNTYPE is the original function type derived from the syntax.
2703260311Spfg BLOCK_PTR_EXP is the block pointer variable.
2704260311Spfg PARAMS is the parameter list.
2705260311Spfg*/
2706260311Spfgstatic tree
2707260311Spfgbuild_block_call (tree fntype, tree block_ptr_exp, tree params)
2708260311Spfg{
2709260311Spfg  tree function_ptr_exp;
2710260311Spfg  tree typelist;
2711260311Spfg  tree result;
2712260311Spfg  /* APPLE LOCAL radar 6396238 */
2713260311Spfg  bool block_ptr_exp_side_effect = TREE_SIDE_EFFECTS (block_ptr_exp);
2714260311Spfg
2715260311Spfg  /* First convert it to 'void *'. */
2716260311Spfg  block_ptr_exp = convert (ptr_type_node, block_ptr_exp);
2717260311Spfg  gcc_assert (generic_block_literal_struct_type);
2718260311Spfg  block_ptr_exp = convert (build_pointer_type (generic_block_literal_struct_type),
2719260311Spfg			    block_ptr_exp);
2720260311Spfg  if (block_ptr_exp_side_effect)
2721260311Spfg    block_ptr_exp = save_expr (block_ptr_exp);
2722260311Spfg
2723260311Spfg  /* BLOCK_PTR_VAR->__FuncPtr */
2724260311Spfg  function_ptr_exp =
2725260311Spfg    finish_class_member_access_expr (build_indirect_ref (block_ptr_exp, "->"),
2726260311Spfg					     get_identifier ("__FuncPtr"), false);
2727260311Spfg  gcc_assert (function_ptr_exp);
2728260311Spfg
2729260311Spfg  /* Build: result_type(*)(void *, function-arg-type-list) */
2730260311Spfg  typelist = TYPE_ARG_TYPES (fntype);
2731260311Spfg  typelist = tree_cons (NULL_TREE, ptr_type_node, typelist);
2732260311Spfg  fntype = build_function_type (TREE_TYPE (fntype), typelist);
2733260311Spfg  function_ptr_exp = convert (build_pointer_type (fntype), function_ptr_exp);
2734260311Spfg  params = tree_cons (NULL_TREE, block_ptr_exp, params);
2735260311Spfg  result = build3 (CALL_EXPR, TREE_TYPE (fntype),
2736260311Spfg		   function_ptr_exp, params, NULL_TREE);
2737260311Spfg  /* FIXME: should do more from build_cxx_call */
2738260311Spfg  result = convert_from_reference (result);
2739260311Spfg  return result;
2740260311Spfg}
2741260311Spfg/* APPLE LOCAL end radar 5847213 - radar 6329245 */
2742260311Spfg/* APPLE LOCAL end blocks 6040305 (cm) */
2743260311Spfg
274418334Spetertree
2745132718Skanbuild_function_call (tree function, tree params)
274618334Speter{
2747132718Skan  tree fntype, fndecl;
2748132718Skan  tree coerced_params;
2749169689Skan  tree name = NULL_TREE;
275018334Speter  int is_method;
275190075Sobrien  tree original = function;
275218334Speter
2753169689Skan  /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2754169689Skan     expressions, like those used for ObjC messenger dispatches.  */
2755169689Skan  function = objc_rewrite_function_call (function, params);
2756169689Skan
275718334Speter  /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
275818334Speter     Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context.  */
275918334Speter  if (TREE_CODE (function) == NOP_EXPR
276018334Speter      && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
276118334Speter    function = TREE_OPERAND (function, 0);
276218334Speter
276318334Speter  if (TREE_CODE (function) == FUNCTION_DECL)
276418334Speter    {
276518334Speter      name = DECL_NAME (function);
276618334Speter
276750397Sobrien      mark_used (function);
276818334Speter      fndecl = function;
276918334Speter
277018334Speter      /* Convert anything with function type to a pointer-to-function.  */
277150397Sobrien      if (pedantic && DECL_MAIN_P (function))
2772169689Skan	pedwarn ("ISO C++ forbids calling %<::main%> from within program");
277318334Speter
277418334Speter      /* Differs from default_conversion by not setting TREE_ADDRESSABLE
277518334Speter	 (because calling an inline function does not mean the function
277618334Speter	 needs to be separately compiled).  */
2777169689Skan
277818334Speter      if (DECL_INLINE (function))
277950397Sobrien	function = inline_conversion (function);
278018334Speter      else
278150397Sobrien	function = build_addr_func (function);
278218334Speter    }
278318334Speter  else
278418334Speter    {
278518334Speter      fndecl = NULL_TREE;
278618334Speter
278750397Sobrien      function = build_addr_func (function);
278818334Speter    }
278918334Speter
279050397Sobrien  if (function == error_mark_node)
279150397Sobrien    return error_mark_node;
279250397Sobrien
279318334Speter  fntype = TREE_TYPE (function);
279418334Speter
279518334Speter  if (TYPE_PTRMEMFUNC_P (fntype))
279618334Speter    {
2797169689Skan      error ("must use %<.*%> or %<->*%> to call pointer-to-member "
2798169689Skan	     "function in %<%E (...)%>",
2799169689Skan	     original);
280050397Sobrien      return error_mark_node;
280118334Speter    }
280218334Speter
280318334Speter  is_method = (TREE_CODE (fntype) == POINTER_TYPE
280418334Speter	       && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
280518334Speter
2806260311Spfg  /* APPLE LOCAL blocks 6040305 */
2807260311Spfg  if (!(((TREE_CODE (fntype) == POINTER_TYPE || TREE_CODE (fntype) == BLOCK_POINTER_TYPE)
280818334Speter	 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE)
280950397Sobrien	|| is_method
281050397Sobrien	|| TREE_CODE (function) == TEMPLATE_ID_EXPR))
281118334Speter    {
2812169689Skan      error ("%qE cannot be used as a function", original);
281318334Speter      return error_mark_node;
281418334Speter    }
281518334Speter
281618334Speter  /* fntype now gets the type of function pointed to.  */
281718334Speter  fntype = TREE_TYPE (fntype);
281818334Speter
281918334Speter  /* Convert the parameters to the types declared in the
282018334Speter     function prototype, or apply default promotions.  */
282118334Speter
2822260311Spfg  /* APPLE LOCAL begin radar 6087117 */
2823132718Skan  coerced_params = convert_arguments (TYPE_ARG_TYPES (fntype),
2824260311Spfg				      params, fndecl, LOOKUP_NORMAL,
2825260311Spfg				      (TREE_CODE (TREE_TYPE (function)) == BLOCK_POINTER_TYPE));
2826260311Spfg  /* APPLE LOCAL end radar 6087117 */
282718334Speter  if (coerced_params == error_mark_node)
2828132718Skan    return error_mark_node;
282918334Speter
2830169689Skan  /* Check for errors in format strings and inappropriately
2831169689Skan     null parameters.  */
283218334Speter
2833169689Skan  check_function_arguments (TYPE_ATTRIBUTES (fntype), coerced_params,
2834169689Skan			    TYPE_ARG_TYPES (fntype));
2835260311Spfg  /* APPLE LOCAL begin blocks 6040305 */
2836260311Spfg  if (TREE_CODE (TREE_TYPE (function)) == BLOCK_POINTER_TYPE)
2837260311Spfg    return build_block_call (fntype, function, coerced_params);
2838260311Spfg  /* APPLE LOCAL end blocks 6040305 */
283918334Speter
2840169689Skan  return build_cxx_call (function, coerced_params);
284118334Speter}
2842260311Spfg
284318334Speter/* Convert the actual parameter expressions in the list VALUES
284418334Speter   to the types in the list TYPELIST.
284518334Speter   If parmdecls is exhausted, or when an element has NULL as its type,
284618334Speter   perform the default conversions.
284718334Speter
284818334Speter   NAME is an IDENTIFIER_NODE or 0.  It is used only for error messages.
284918334Speter
285018334Speter   This is also where warnings about wrong number of args are generated.
2851169689Skan
285218334Speter   Return a list of expressions for the parameters as converted.
285318334Speter
285418334Speter   Both VALUES and the returned value are chains of TREE_LIST nodes
285518334Speter   with the elements of the list in the TREE_VALUE slots of those nodes.
285618334Speter
285718334Speter   In C++, unspecified trailing parameters can be filled in with their
285818334Speter   default arguments, if such were specified.  Do so here.  */
285918334Speter
2860169689Skanstatic tree
2861260311Spfg/* APPLE LOCAL radar 6087117 */
2862260311Spfgconvert_arguments (tree typelist, tree values, tree fndecl, int flags, int block_call)
286318334Speter{
2864132718Skan  tree typetail, valtail;
2865132718Skan  tree result = NULL_TREE;
286652284Sobrien  const char *called_thing = 0;
286718334Speter  int i = 0;
286818334Speter
286950397Sobrien  /* Argument passing is always copy-initialization.  */
287050397Sobrien  flags |= LOOKUP_ONLYCONVERTING;
287150397Sobrien
287218334Speter  if (fndecl)
287318334Speter    {
287418334Speter      if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
287518334Speter	{
287618334Speter	  if (DECL_NAME (fndecl) == NULL_TREE
287718334Speter	      || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl)))
287818334Speter	    called_thing = "constructor";
287918334Speter	  else
288018334Speter	    called_thing = "member function";
288118334Speter	}
288218334Speter      else
288318334Speter	called_thing = "function";
288418334Speter    }
288518334Speter
288618334Speter  for (valtail = values, typetail = typelist;
288718334Speter       valtail;
288818334Speter       valtail = TREE_CHAIN (valtail), i++)
288918334Speter    {
2890132718Skan      tree type = typetail ? TREE_VALUE (typetail) : 0;
2891132718Skan      tree val = TREE_VALUE (valtail);
289218334Speter
2893169689Skan      if (val == error_mark_node || type == error_mark_node)
289418334Speter	return error_mark_node;
289518334Speter
289618334Speter      if (type == void_type_node)
289718334Speter	{
289818334Speter	  if (fndecl)
289918334Speter	    {
2900169689Skan	      error ("too many arguments to %s %q+#D", called_thing, fndecl);
290118334Speter	      error ("at this point in file");
290218334Speter	    }
290318334Speter	  else
2904260311Spfg	    /* APPLE LOCAL radar 6087117 */
2905260311Spfg	    error ("too many arguments to %s", (block_call ? "block call" : "function"));
290618334Speter	  /* In case anybody wants to know if this argument
290718334Speter	     list is valid.  */
290818334Speter	  if (result)
290918334Speter	    TREE_TYPE (tree_last (result)) = error_mark_node;
291018334Speter	  break;
291118334Speter	}
291218334Speter
291318334Speter      /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
291418334Speter	 Strip such NOP_EXPRs, since VAL is used in non-lvalue context.  */
291518334Speter      if (TREE_CODE (val) == NOP_EXPR
291618334Speter	  && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
291718334Speter	  && (type == 0 || TREE_CODE (type) != REFERENCE_TYPE))
291818334Speter	val = TREE_OPERAND (val, 0);
291918334Speter
292018334Speter      if (type == 0 || TREE_CODE (type) != REFERENCE_TYPE)
292118334Speter	{
292218334Speter	  if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
292318334Speter	      || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE
292418334Speter	      || TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
2925132718Skan	    val = decay_conversion (val);
292618334Speter	}
292718334Speter
292818334Speter      if (val == error_mark_node)
292918334Speter	return error_mark_node;
293018334Speter
293118334Speter      if (type != 0)
293218334Speter	{
293318334Speter	  /* Formal parm type is specified by a function prototype.  */
293418334Speter	  tree parmval;
293518334Speter
293690075Sobrien	  if (!COMPLETE_TYPE_P (complete_type (type)))
293718334Speter	    {
2938132718Skan	      if (fndecl)
2939169689Skan		error ("parameter %P of %qD has incomplete type %qT",
2940132718Skan		       i, fndecl, type);
2941132718Skan	      else
2942169689Skan		error ("parameter %P has incomplete type %qT", i, type);
2943132718Skan	      parmval = error_mark_node;
294418334Speter	    }
294518334Speter	  else
294618334Speter	    {
294750397Sobrien	      parmval = convert_for_initialization
294852284Sobrien		(NULL_TREE, type, val, flags,
294950397Sobrien		 "argument passing", fndecl, i);
2950117395Skan	      parmval = convert_for_arg_passing (type, parmval);
295118334Speter	    }
295218334Speter
295318334Speter	  if (parmval == error_mark_node)
295418334Speter	    return error_mark_node;
295518334Speter
295690075Sobrien	  result = tree_cons (NULL_TREE, parmval, result);
295718334Speter	}
295818334Speter      else
295918334Speter	{
296090075Sobrien	  if (fndecl && DECL_BUILT_IN (fndecl)
296190075Sobrien	      && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CONSTANT_P)
296290075Sobrien	    /* Don't do ellipsis conversion for __built_in_constant_p
296390075Sobrien	       as this will result in spurious warnings for non-POD
296490075Sobrien	       types.  */
296590075Sobrien	    val = require_complete_type (val);
296690075Sobrien	  else
296790075Sobrien	    val = convert_arg_to_ellipsis (val);
296890075Sobrien
296990075Sobrien	  result = tree_cons (NULL_TREE, val, result);
297018334Speter	}
297118334Speter
297218334Speter      if (typetail)
297318334Speter	typetail = TREE_CHAIN (typetail);
297418334Speter    }
297518334Speter
297618334Speter  if (typetail != 0 && typetail != void_list_node)
297718334Speter    {
2978132718Skan      /* See if there are default arguments that can be used.  */
2979169689Skan      if (TREE_PURPOSE (typetail)
2980132718Skan	  && TREE_CODE (TREE_PURPOSE (typetail)) != DEFAULT_ARG)
298118334Speter	{
298218334Speter	  for (; typetail != void_list_node; ++i)
298318334Speter	    {
2984169689Skan	      tree parmval
2985169689Skan		= convert_default_arg (TREE_VALUE (typetail),
2986169689Skan				       TREE_PURPOSE (typetail),
298790075Sobrien				       fndecl, i);
298818334Speter
298918334Speter	      if (parmval == error_mark_node)
299018334Speter		return error_mark_node;
299118334Speter
299290075Sobrien	      result = tree_cons (0, parmval, result);
299318334Speter	      typetail = TREE_CHAIN (typetail);
299418334Speter	      /* ends with `...'.  */
299518334Speter	      if (typetail == NULL_TREE)
299618334Speter		break;
299718334Speter	    }
299818334Speter	}
299918334Speter      else
300018334Speter	{
300118334Speter	  if (fndecl)
300218334Speter	    {
3003169689Skan	      error ("too few arguments to %s %q+#D", called_thing, fndecl);
300418334Speter	      error ("at this point in file");
300518334Speter	    }
300618334Speter	  else
3007260311Spfg	    /* APPLE LOCAL radar 6087117 */
3008260311Spfg	    error ("too few arguments to %s", (block_call ? "block call" : "function"));
3009169689Skan	  return error_mark_node;
301018334Speter	}
301118334Speter    }
301218334Speter
301318334Speter  return nreverse (result);
301418334Speter}
301518334Speter
301618334Speter/* Build a binary-operation expression, after performing default
301718334Speter   conversions on the operands.  CODE is the kind of expression to build.  */
301818334Speter
301918334Spetertree
3020258081Spfgbuild_x_binary_op (enum tree_code code, tree arg1, enum tree_code arg1_code,
3021258081Spfg		   tree arg2, enum tree_code arg2_code, bool *overloaded_p)
302218334Speter{
3023132718Skan  tree orig_arg1;
3024132718Skan  tree orig_arg2;
3025132718Skan  tree expr;
3026132718Skan
3027132718Skan  orig_arg1 = arg1;
3028132718Skan  orig_arg2 = arg2;
3029132718Skan
303050397Sobrien  if (processing_template_decl)
3031132718Skan    {
3032132718Skan      if (type_dependent_expression_p (arg1)
3033132718Skan	  || type_dependent_expression_p (arg2))
3034132718Skan	return build_min_nt (code, arg1, arg2);
3035132718Skan      arg1 = build_non_dependent_expr (arg1);
3036132718Skan      arg2 = build_non_dependent_expr (arg2);
3037132718Skan    }
303850397Sobrien
3039132718Skan  if (code == DOTSTAR_EXPR)
3040132718Skan    expr = build_m_component_ref (arg1, arg2);
3041132718Skan  else
3042169689Skan    expr = build_new_op (code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE,
3043132718Skan			 overloaded_p);
3044132718Skan
3045258081Spfg  /* Check for cases such as x+y<<z which users are likely to
3046258081Spfg     misinterpret.  But don't warn about obj << x + y, since that is a
3047258081Spfg     common idiom for I/O.  */
3048258081Spfg  if (warn_parentheses
3049258081Spfg      && !processing_template_decl
3050258081Spfg      && !error_operand_p (arg1)
3051258081Spfg      && !error_operand_p (arg2)
3052258081Spfg      && (code != LSHIFT_EXPR
3053258081Spfg	  || !IS_AGGR_TYPE (TREE_TYPE (arg1))))
3054258081Spfg    warn_about_parentheses (code, arg1_code, arg2_code);
3055258081Spfg
3056132718Skan  if (processing_template_decl && expr != error_mark_node)
3057132718Skan    return build_min_non_dep (code, expr, orig_arg1, orig_arg2);
3058169689Skan
3059132718Skan  return expr;
306018334Speter}
306118334Speter
306218334Speter/* Build a binary-operation expression without default conversions.
306318334Speter   CODE is the kind of expression to build.
306418334Speter   This function differs from `build' in several ways:
306518334Speter   the data type of the result is computed and recorded in it,
306618334Speter   warnings are generated if arg data types are invalid,
306718334Speter   special handling for addition and subtraction of pointers is known,
306818334Speter   and some optimization is done (operations on narrow ints
306918334Speter   are done in the narrower type when that gives the same result).
307018334Speter   Constant folding is also done before the result is returned.
307118334Speter
307218334Speter   Note that the operands will never have enumeral types
307318334Speter   because either they have just had the default conversions performed
307418334Speter   or they have both just been converted to some other type in which
307518334Speter   the arithmetic is to be done.
307618334Speter
307718334Speter   C++: must do special pointer arithmetic when implementing
307818334Speter   multiple inheritance, and deal with pointer to member functions.  */
307918334Speter
308018334Spetertree
3081132718Skanbuild_binary_op (enum tree_code code, tree orig_op0, tree orig_op1,
3082132718Skan		 int convert_p ATTRIBUTE_UNUSED)
308318334Speter{
308418334Speter  tree op0, op1;
3085132718Skan  enum tree_code code0, code1;
308618334Speter  tree type0, type1;
3087169689Skan  const char *invalid_op_diag;
308818334Speter
308918334Speter  /* Expression code to give to the expression when it is built.
309018334Speter     Normally this is CODE, which is what the caller asked for,
309118334Speter     but in some special cases we change it.  */
3092132718Skan  enum tree_code resultcode = code;
309318334Speter
309418334Speter  /* Data type in which the computation is to be performed.
309518334Speter     In the simplest cases this is the common type of the arguments.  */
3096132718Skan  tree result_type = NULL;
309718334Speter
309818334Speter  /* Nonzero means operands have already been type-converted
309918334Speter     in whatever way is necessary.
310018334Speter     Zero means they need to be converted to RESULT_TYPE.  */
310118334Speter  int converted = 0;
310218334Speter
310318334Speter  /* Nonzero means create the expression with this type, rather than
310418334Speter     RESULT_TYPE.  */
310518334Speter  tree build_type = 0;
310618334Speter
310718334Speter  /* Nonzero means after finally constructing the expression
310818334Speter     convert it to this type.  */
310918334Speter  tree final_type = 0;
311018334Speter
3111169689Skan  tree result;
3112169689Skan
311318334Speter  /* Nonzero if this is an operation like MIN or MAX which can
311418334Speter     safely be computed in short if both args are promoted shorts.
311518334Speter     Also implies COMMON.
311618334Speter     -1 indicates a bitwise operation; this makes a difference
311718334Speter     in the exact conditions for when it is safe to do the operation
311818334Speter     in a narrower mode.  */
311918334Speter  int shorten = 0;
312018334Speter
312118334Speter  /* Nonzero if this is a comparison operation;
312218334Speter     if both args are promoted shorts, compare the original shorts.
312318334Speter     Also implies COMMON.  */
312418334Speter  int short_compare = 0;
312518334Speter
312618334Speter  /* Nonzero if this is a right-shift operation, which can be computed on the
312718334Speter     original short and then promoted if the operand is a promoted short.  */
312818334Speter  int short_shift = 0;
312918334Speter
313018334Speter  /* Nonzero means set RESULT_TYPE to the common type of the args.  */
313118334Speter  int common = 0;
313218334Speter
3133169689Skan  /* True if both operands have arithmetic type.  */
3134169689Skan  bool arithmetic_types_p;
3135169689Skan
313618334Speter  /* Apply default conversions.  */
313790075Sobrien  op0 = orig_op0;
313890075Sobrien  op1 = orig_op1;
3139169689Skan
314018334Speter  if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
314118334Speter      || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
314218334Speter      || code == TRUTH_XOR_EXPR)
314318334Speter    {
314490075Sobrien      if (!really_overloaded_fn (op0))
314590075Sobrien	op0 = decay_conversion (op0);
314690075Sobrien      if (!really_overloaded_fn (op1))
314790075Sobrien	op1 = decay_conversion (op1);
314818334Speter    }
314918334Speter  else
315018334Speter    {
315190075Sobrien      if (!really_overloaded_fn (op0))
315290075Sobrien	op0 = default_conversion (op0);
315390075Sobrien      if (!really_overloaded_fn (op1))
315490075Sobrien	op1 = default_conversion (op1);
315518334Speter    }
315618334Speter
315752284Sobrien  /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue.  */
315852284Sobrien  STRIP_TYPE_NOPS (op0);
315952284Sobrien  STRIP_TYPE_NOPS (op1);
316052284Sobrien
316152284Sobrien  /* DTRT if one side is an overloaded function, but complain about it.  */
316252284Sobrien  if (type_unknown_p (op0))
316352284Sobrien    {
316496263Sobrien      tree t = instantiate_type (TREE_TYPE (op1), op0, tf_none);
316552284Sobrien      if (t != error_mark_node)
316652284Sobrien	{
3167169689Skan	  pedwarn ("assuming cast to type %qT from overloaded function",
3168169689Skan		   TREE_TYPE (t));
316952284Sobrien	  op0 = t;
317052284Sobrien	}
317152284Sobrien    }
317252284Sobrien  if (type_unknown_p (op1))
317352284Sobrien    {
317496263Sobrien      tree t = instantiate_type (TREE_TYPE (op0), op1, tf_none);
317552284Sobrien      if (t != error_mark_node)
317652284Sobrien	{
3177169689Skan	  pedwarn ("assuming cast to type %qT from overloaded function",
3178169689Skan		   TREE_TYPE (t));
317952284Sobrien	  op1 = t;
318052284Sobrien	}
318152284Sobrien    }
318252284Sobrien
318318334Speter  type0 = TREE_TYPE (op0);
318418334Speter  type1 = TREE_TYPE (op1);
318518334Speter
318618334Speter  /* The expression codes of the data types of the arguments tell us
318718334Speter     whether the arguments are integers, floating, pointers, etc.  */
318818334Speter  code0 = TREE_CODE (type0);
318918334Speter  code1 = TREE_CODE (type1);
319018334Speter
319118334Speter  /* If an error was already reported for one of the arguments,
319218334Speter     avoid reporting another error.  */
319318334Speter
319418334Speter  if (code0 == ERROR_MARK || code1 == ERROR_MARK)
319518334Speter    return error_mark_node;
319618334Speter
3197169689Skan  if ((invalid_op_diag
3198169689Skan       = targetm.invalid_binary_op (code, type0, type1)))
3199169689Skan    {
3200259666Spfg      error (invalid_op_diag, "");
3201169689Skan      return error_mark_node;
3202169689Skan    }
3203169689Skan
320418334Speter  switch (code)
320518334Speter    {
320618334Speter    case MINUS_EXPR:
320718334Speter      /* Subtraction of two similar pointers.
320818334Speter	 We must subtract them as integers, then divide by object size.  */
320918334Speter      if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
3210132718Skan	  && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
3211132718Skan							TREE_TYPE (type1)))
321250397Sobrien	return pointer_diff (op0, op1, common_type (type0, type1));
3213169689Skan      /* In all other cases except pointer - int, the usual arithmetic
3214169689Skan	 rules aply.  */
3215169689Skan      else if (!(code0 == POINTER_TYPE && code1 == INTEGER_TYPE))
3216169689Skan	{
3217169689Skan	  common = 1;
3218169689Skan	  break;
3219169689Skan	}
3220169689Skan      /* The pointer - int case is just like pointer + int; fall
3221169689Skan	 through.  */
3222169689Skan    case PLUS_EXPR:
3223169689Skan      if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
3224169689Skan	  && (code0 == INTEGER_TYPE || code1 == INTEGER_TYPE))
3225169689Skan	{
3226169689Skan	  tree ptr_operand;
3227169689Skan	  tree int_operand;
3228169689Skan	  ptr_operand = ((code0 == POINTER_TYPE) ? op0 : op1);
3229169689Skan	  int_operand = ((code0 == INTEGER_TYPE) ? op0 : op1);
3230169689Skan	  if (processing_template_decl)
3231169689Skan	    {
3232169689Skan	      result_type = TREE_TYPE (ptr_operand);
3233169689Skan	      break;
3234169689Skan	    }
3235169689Skan	  return cp_pointer_int_sum (code,
3236169689Skan				     ptr_operand,
3237169689Skan				     int_operand);
3238169689Skan	}
3239169689Skan      common = 1;
324018334Speter      break;
324118334Speter
324218334Speter    case MULT_EXPR:
324318334Speter      common = 1;
324418334Speter      break;
324518334Speter
324618334Speter    case TRUNC_DIV_EXPR:
324718334Speter    case CEIL_DIV_EXPR:
324818334Speter    case FLOOR_DIV_EXPR:
324918334Speter    case ROUND_DIV_EXPR:
325018334Speter    case EXACT_DIV_EXPR:
325150397Sobrien      if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
3252161651Skan	   || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
325350397Sobrien	  && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
3254161651Skan	      || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
325518334Speter	{
3256169689Skan	  enum tree_code tcode0 = code0, tcode1 = code1;
3257169689Skan
325818334Speter	  if (TREE_CODE (op1) == INTEGER_CST && integer_zerop (op1))
3259169689Skan	    warning (OPT_Wdiv_by_zero, "division by zero in %<%E / 0%>", op0);
326018334Speter	  else if (TREE_CODE (op1) == REAL_CST && real_zerop (op1))
3261169689Skan	    warning (OPT_Wdiv_by_zero, "division by zero in %<%E / 0.%>", op0);
3262169689Skan
3263169689Skan	  if (tcode0 == COMPLEX_TYPE || tcode0 == VECTOR_TYPE)
3264169689Skan	    tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
3265169689Skan	  if (tcode1 == COMPLEX_TYPE || tcode1 == VECTOR_TYPE)
3266169689Skan	    tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
3267169689Skan
3268169689Skan	  if (!(tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE))
326918334Speter	    resultcode = RDIV_EXPR;
327018334Speter	  else
327118334Speter	    /* When dividing two signed integers, we have to promote to int.
327218334Speter	       unless we divide by a constant != -1.  Note that default
327318334Speter	       conversion will have been performed on the operands at this
327418334Speter	       point, so we have to dig out the original type to find out if
327518334Speter	       it was unsigned.  */
327618334Speter	    shorten = ((TREE_CODE (op0) == NOP_EXPR
3277169689Skan			&& TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
327818334Speter		       || (TREE_CODE (op1) == INTEGER_CST
327990075Sobrien			   && ! integer_all_onesp (op1)));
328090075Sobrien
328118334Speter	  common = 1;
328218334Speter	}
328318334Speter      break;
328418334Speter
328518334Speter    case BIT_AND_EXPR:
328618334Speter    case BIT_IOR_EXPR:
328718334Speter    case BIT_XOR_EXPR:
3288161651Skan      if ((code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3289161651Skan	  || (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE))
329018334Speter	shorten = -1;
329118334Speter      break;
329218334Speter
329318334Speter    case TRUNC_MOD_EXPR:
329418334Speter    case FLOOR_MOD_EXPR:
329518334Speter      if (code1 == INTEGER_TYPE && integer_zerop (op1))
3296169689Skan	warning (OPT_Wdiv_by_zero, "division by zero in %<%E %% 0%>", op0);
329718334Speter      else if (code1 == REAL_TYPE && real_zerop (op1))
3298169689Skan	warning (OPT_Wdiv_by_zero, "division by zero in %<%E %% 0.%>", op0);
3299169689Skan
330018334Speter      if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
330118334Speter	{
330218334Speter	  /* Although it would be tempting to shorten always here, that loses
330318334Speter	     on some targets, since the modulo instruction is undefined if the
330418334Speter	     quotient can't be represented in the computation mode.  We shorten
330518334Speter	     only if unsigned or if dividing by something we know != -1.  */
330618334Speter	  shorten = ((TREE_CODE (op0) == NOP_EXPR
3307169689Skan		      && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
330818334Speter		     || (TREE_CODE (op1) == INTEGER_CST
330990075Sobrien			 && ! integer_all_onesp (op1)));
331018334Speter	  common = 1;
331118334Speter	}
331218334Speter      break;
331318334Speter
331418334Speter    case TRUTH_ANDIF_EXPR:
331518334Speter    case TRUTH_ORIF_EXPR:
331618334Speter    case TRUTH_AND_EXPR:
331718334Speter    case TRUTH_OR_EXPR:
331818334Speter      result_type = boolean_type_node;
331918334Speter      break;
332018334Speter
332118334Speter      /* Shift operations: result has same type as first operand;
332218334Speter	 always convert second operand to int.
332318334Speter	 Also set SHORT_SHIFT if shifting rightward.  */
332418334Speter
332518334Speter    case RSHIFT_EXPR:
332618334Speter      if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
332718334Speter	{
332818334Speter	  result_type = type0;
332918334Speter	  if (TREE_CODE (op1) == INTEGER_CST)
333018334Speter	    {
333118334Speter	      if (tree_int_cst_lt (op1, integer_zero_node))
3332169689Skan		warning (0, "right shift count is negative");
333318334Speter	      else
333418334Speter		{
333590075Sobrien		  if (! integer_zerop (op1))
333618334Speter		    short_shift = 1;
333790075Sobrien		  if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
3338169689Skan		    warning (0, "right shift count >= width of type");
333918334Speter		}
334018334Speter	    }
334118334Speter	  /* Convert the shift-count to an integer, regardless of
334218334Speter	     size of value being shifted.  */
334318334Speter	  if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
334450397Sobrien	    op1 = cp_convert (integer_type_node, op1);
334518334Speter	  /* Avoid converting op1 to result_type later.  */
334618334Speter	  converted = 1;
334718334Speter	}
334818334Speter      break;
334918334Speter
335018334Speter    case LSHIFT_EXPR:
335118334Speter      if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
335218334Speter	{
335318334Speter	  result_type = type0;
335418334Speter	  if (TREE_CODE (op1) == INTEGER_CST)
335518334Speter	    {
335618334Speter	      if (tree_int_cst_lt (op1, integer_zero_node))
3357169689Skan		warning (0, "left shift count is negative");
335890075Sobrien	      else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
3359169689Skan		warning (0, "left shift count >= width of type");
336018334Speter	    }
336118334Speter	  /* Convert the shift-count to an integer, regardless of
336218334Speter	     size of value being shifted.  */
336318334Speter	  if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
336450397Sobrien	    op1 = cp_convert (integer_type_node, op1);
336518334Speter	  /* Avoid converting op1 to result_type later.  */
336618334Speter	  converted = 1;
336718334Speter	}
336818334Speter      break;
336918334Speter
337018334Speter    case RROTATE_EXPR:
337118334Speter    case LROTATE_EXPR:
337218334Speter      if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
337318334Speter	{
337418334Speter	  result_type = type0;
337518334Speter	  if (TREE_CODE (op1) == INTEGER_CST)
337618334Speter	    {
337718334Speter	      if (tree_int_cst_lt (op1, integer_zero_node))
3378169689Skan		warning (0, "%s rotate count is negative",
337918334Speter			 (code == LROTATE_EXPR) ? "left" : "right");
338090075Sobrien	      else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
3381169689Skan		warning (0, "%s rotate count >= width of type",
338218334Speter			 (code == LROTATE_EXPR) ? "left" : "right");
338318334Speter	    }
338418334Speter	  /* Convert the shift-count to an integer, regardless of
338518334Speter	     size of value being shifted.  */
338618334Speter	  if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
338750397Sobrien	    op1 = cp_convert (integer_type_node, op1);
338818334Speter	}
338918334Speter      break;
339018334Speter
339118334Speter    case EQ_EXPR:
339218334Speter    case NE_EXPR:
3393169689Skan      if (code0 == REAL_TYPE || code1 == REAL_TYPE)
3394169689Skan	warning (OPT_Wfloat_equal,
3395169689Skan		 "comparing floating point with == or != is unsafe");
3396169689Skan      if ((TREE_CODE (orig_op0) == STRING_CST && !integer_zerop (op1))
3397169689Skan	  || (TREE_CODE (orig_op1) == STRING_CST && !integer_zerop (op0)))
3398169689Skan	warning (OPT_Waddress,
3399169689Skan                 "comparison with string literal results in unspecified behaviour");
340090075Sobrien
3401169689Skan      build_type = boolean_type_node;
340250397Sobrien      if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
340350397Sobrien	   || code0 == COMPLEX_TYPE)
340450397Sobrien	  && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
340550397Sobrien	      || code1 == COMPLEX_TYPE))
340618334Speter	short_compare = 1;
3407260311Spfg      /* APPLE LOCAL begin blocks 6040305 */
3408260311Spfg      else if (((code0 == POINTER_TYPE || code0 == BLOCK_POINTER_TYPE)
3409260311Spfg      && (code1 == POINTER_TYPE || code1 == BLOCK_POINTER_TYPE))
3410132718Skan	       || (TYPE_PTRMEM_P (type0) && TYPE_PTRMEM_P (type1)))
3411260311Spfg      /* APPLE LOCAL end blocks 6040305 */
341290075Sobrien	result_type = composite_pointer_type (type0, type1, op0, op1,
341390075Sobrien					      "comparison");
3414260311Spfg      /* APPLE LOCAL blocks 6040305 (cl) */
3415260311Spfg      else if ((code0 == POINTER_TYPE || code0 == BLOCK_POINTER_TYPE || TYPE_PTRMEM_P (type0))
3416132718Skan	       && null_ptr_cst_p (op1))
341718334Speter	result_type = type0;
3418260311Spfg      /* APPLE LOCAL blocks 6040305 (cl) */
3419260311Spfg      else if ((code1 == POINTER_TYPE || code1 == BLOCK_POINTER_TYPE || TYPE_PTRMEM_P (type1))
3420132718Skan	       && null_ptr_cst_p (op0))
342118334Speter	result_type = type1;
3422260311Spfg      /* APPLE LOCAL blocks 6040305 (cl) */
3423260311Spfg      else if ((code0 == POINTER_TYPE || code0 == BLOCK_POINTER_TYPE) && code1 == INTEGER_TYPE)
342418334Speter	{
342518334Speter	  result_type = type0;
342690075Sobrien	  error ("ISO C++ forbids comparison between pointer and integer");
342718334Speter	}
3428260311Spfg      /* APPLE LOCAL blocks 6040305 (cl) */
3429260311Spfg      else if (code0 == INTEGER_TYPE && (code1 == POINTER_TYPE || code1 == BLOCK_POINTER_TYPE))
343018334Speter	{
343118334Speter	  result_type = type1;
343290075Sobrien	  error ("ISO C++ forbids comparison between pointer and integer");
343318334Speter	}
343490075Sobrien      else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (op1))
343518334Speter	{
3436117395Skan	  op0 = build_ptrmemfunc_access_expr (op0, pfn_identifier);
343790075Sobrien	  op1 = cp_convert (TREE_TYPE (op0), integer_zero_node);
343818334Speter	  result_type = TREE_TYPE (op0);
343918334Speter	}
344090075Sobrien      else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (op0))
344190075Sobrien	return cp_build_binary_op (code, op1, op0);
344218334Speter      else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1)
344352284Sobrien	       && same_type_p (type0, type1))
344418334Speter	{
344590075Sobrien	  /* E will be the final comparison.  */
344690075Sobrien	  tree e;
344790075Sobrien	  /* E1 and E2 are for scratch.  */
344890075Sobrien	  tree e1;
344990075Sobrien	  tree e2;
345090075Sobrien	  tree pfn0;
345190075Sobrien	  tree pfn1;
345290075Sobrien	  tree delta0;
345390075Sobrien	  tree delta1;
345418334Speter
345590075Sobrien	  if (TREE_SIDE_EFFECTS (op0))
345690075Sobrien	    op0 = save_expr (op0);
345790075Sobrien	  if (TREE_SIDE_EFFECTS (op1))
345890075Sobrien	    op1 = save_expr (op1);
345918334Speter
346090075Sobrien	  /* We generate:
346190075Sobrien
3462169689Skan	     (op0.pfn == op1.pfn
346390075Sobrien	      && (!op0.pfn || op0.delta == op1.delta))
3464169689Skan
346590075Sobrien	     The reason for the `!op0.pfn' bit is that a NULL
346690075Sobrien	     pointer-to-member is any member with a zero PFN; the
346790075Sobrien	     DELTA field is unspecified.  */
346890075Sobrien	  pfn0 = pfn_from_ptrmemfunc (op0);
346990075Sobrien	  pfn1 = pfn_from_ptrmemfunc (op1);
3470117395Skan	  delta0 = build_ptrmemfunc_access_expr (op0,
3471117395Skan						 delta_identifier);
3472117395Skan	  delta1 = build_ptrmemfunc_access_expr (op1,
3473117395Skan						 delta_identifier);
347490075Sobrien	  e1 = cp_build_binary_op (EQ_EXPR, delta0, delta1);
3475169689Skan	  e2 = cp_build_binary_op (EQ_EXPR,
347690075Sobrien				   pfn0,
347790075Sobrien				   cp_convert (TREE_TYPE (pfn0),
347890075Sobrien					       integer_zero_node));
347990075Sobrien	  e1 = cp_build_binary_op (TRUTH_ORIF_EXPR, e1, e2);
3480169689Skan	  e2 = build2 (EQ_EXPR, boolean_type_node, pfn0, pfn1);
348190075Sobrien	  e = cp_build_binary_op (TRUTH_ANDIF_EXPR, e2, e1);
348218334Speter	  if (code == EQ_EXPR)
348390075Sobrien	    return e;
348490075Sobrien	  return cp_build_binary_op (EQ_EXPR, e, integer_zero_node);
348518334Speter	}
3486169689Skan      else
3487169689Skan	{
3488169689Skan	  gcc_assert (!TYPE_PTRMEMFUNC_P (type0)
3489169689Skan		      || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0),
3490169689Skan				       type1));
3491169689Skan	  gcc_assert (!TYPE_PTRMEMFUNC_P (type1)
3492169689Skan		      || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1),
3493169689Skan				       type0));
3494169689Skan	}
3495169689Skan
349618334Speter      break;
349718334Speter
349818334Speter    case MAX_EXPR:
349918334Speter    case MIN_EXPR:
350018334Speter      if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
350118334Speter	   && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
350218334Speter	shorten = 1;
350318334Speter      else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
350490075Sobrien	result_type = composite_pointer_type (type0, type1, op0, op1,
350590075Sobrien					      "comparison");
350618334Speter      break;
350718334Speter
350818334Speter    case LE_EXPR:
350918334Speter    case GE_EXPR:
351018334Speter    case LT_EXPR:
351118334Speter    case GT_EXPR:
3512169689Skan      if (TREE_CODE (orig_op0) == STRING_CST
3513169689Skan	  || TREE_CODE (orig_op1) == STRING_CST)
3514169689Skan	warning (OPT_Waddress,
3515169689Skan                 "comparison with string literal results in unspecified behaviour");
3516169689Skan
351718334Speter      build_type = boolean_type_node;
351818334Speter      if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
351918334Speter	   && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
352018334Speter	short_compare = 1;
352118334Speter      else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
352290075Sobrien	result_type = composite_pointer_type (type0, type1, op0, op1,
352390075Sobrien					      "comparison");
352418334Speter      else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
352518334Speter	       && integer_zerop (op1))
352618334Speter	result_type = type0;
352718334Speter      else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
352818334Speter	       && integer_zerop (op0))
352918334Speter	result_type = type1;
353018334Speter      else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
353118334Speter	{
353218334Speter	  result_type = type0;
353390075Sobrien	  pedwarn ("ISO C++ forbids comparison between pointer and integer");
353418334Speter	}
353518334Speter      else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
353618334Speter	{
353718334Speter	  result_type = type1;
353890075Sobrien	  pedwarn ("ISO C++ forbids comparison between pointer and integer");
353918334Speter	}
354018334Speter      break;
354150397Sobrien
354290075Sobrien    case UNORDERED_EXPR:
354390075Sobrien    case ORDERED_EXPR:
354490075Sobrien    case UNLT_EXPR:
354590075Sobrien    case UNLE_EXPR:
354690075Sobrien    case UNGT_EXPR:
354790075Sobrien    case UNGE_EXPR:
354890075Sobrien    case UNEQ_EXPR:
354990075Sobrien      build_type = integer_type_node;
355090075Sobrien      if (code0 != REAL_TYPE || code1 != REAL_TYPE)
355190075Sobrien	{
355290075Sobrien	  error ("unordered comparison on non-floating point argument");
355390075Sobrien	  return error_mark_node;
355490075Sobrien	}
355590075Sobrien      common = 1;
355690075Sobrien      break;
355790075Sobrien
355850397Sobrien    default:
355950397Sobrien      break;
356018334Speter    }
356118334Speter
3562161651Skan  if (((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
3563169689Skan       && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
3564169689Skan	   || code1 == COMPLEX_TYPE)))
3565169689Skan    arithmetic_types_p = 1;
3566169689Skan  else
356718334Speter    {
3568169689Skan      arithmetic_types_p = 0;
3569169689Skan      /* Vector arithmetic is only allowed when both sides are vectors.  */
3570169689Skan      if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
3571169689Skan	{
3572169689Skan	  if (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
3573169689Skan	      || !same_scalar_type_ignoring_signedness (TREE_TYPE (type0),
3574169689Skan							TREE_TYPE (type1)))
3575169689Skan	    {
3576259022Spfg	      binary_op_error (code, type0, type1);
3577169689Skan	      return error_mark_node;
3578169689Skan	    }
3579169689Skan	  arithmetic_types_p = 1;
3580169689Skan	}
3581169689Skan    }
3582169689Skan  /* Determine the RESULT_TYPE, if it is not already known.  */
3583169689Skan  if (!result_type
3584169689Skan      && arithmetic_types_p
3585169689Skan      && (shorten || common || short_compare))
3586169689Skan    result_type = common_type (type0, type1);
358750397Sobrien
3588169689Skan  if (!result_type)
3589169689Skan    {
3590169689Skan      error ("invalid operands of types %qT and %qT to binary %qO",
3591169689Skan	     TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), code);
3592169689Skan      return error_mark_node;
3593169689Skan    }
3594161651Skan
3595169689Skan  /* If we're in a template, the only thing we need to know is the
3596169689Skan     RESULT_TYPE.  */
3597169689Skan  if (processing_template_decl)
3598169689Skan    return build2 (resultcode,
3599169689Skan		   build_type ? build_type : result_type,
3600169689Skan		   op0, op1);
360118334Speter
3602169689Skan  if (arithmetic_types_p)
3603169689Skan    {
3604169689Skan      int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
3605169689Skan
360618334Speter      /* For certain operations (which identify themselves by shorten != 0)
360718334Speter	 if both args were extended from the same smaller type,
360818334Speter	 do the arithmetic in that type and then extend.
360918334Speter
361018334Speter	 shorten !=0 and !=1 indicates a bitwise operation.
361118334Speter	 For them, this optimization is safe only if
361218334Speter	 both args are zero-extended or both are sign-extended.
361318334Speter	 Otherwise, we might change the result.
361418334Speter	 Eg, (short)-1 | (unsigned short)-1 is (int)-1
361518334Speter	 but calculated in (unsigned short) it would be (unsigned short)-1.  */
361618334Speter
361750397Sobrien      if (shorten && none_complex)
361818334Speter	{
361918334Speter	  int unsigned0, unsigned1;
362018334Speter	  tree arg0 = get_narrower (op0, &unsigned0);
362118334Speter	  tree arg1 = get_narrower (op1, &unsigned1);
362218334Speter	  /* UNS is 1 if the operation to be done is an unsigned one.  */
3623169689Skan	  int uns = TYPE_UNSIGNED (result_type);
362418334Speter	  tree type;
362518334Speter
362618334Speter	  final_type = result_type;
362718334Speter
362818334Speter	  /* Handle the case that OP0 does not *contain* a conversion
362918334Speter	     but it *requires* conversion to FINAL_TYPE.  */
363018334Speter
363118334Speter	  if (op0 == arg0 && TREE_TYPE (op0) != final_type)
3632169689Skan	    unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
363318334Speter	  if (op1 == arg1 && TREE_TYPE (op1) != final_type)
3634169689Skan	    unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
363518334Speter
363618334Speter	  /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE.  */
363718334Speter
363818334Speter	  /* For bitwise operations, signedness of nominal type
363918334Speter	     does not matter.  Consider only how operands were extended.  */
364018334Speter	  if (shorten == -1)
364118334Speter	    uns = unsigned0;
364218334Speter
364318334Speter	  /* Note that in all three cases below we refrain from optimizing
364418334Speter	     an unsigned operation on sign-extended args.
364518334Speter	     That would not be valid.  */
364618334Speter
364718334Speter	  /* Both args variable: if both extended in same way
364818334Speter	     from same width, do it in that width.
364918334Speter	     Do it unsigned if args were zero-extended.  */
365018334Speter	  if ((TYPE_PRECISION (TREE_TYPE (arg0))
365118334Speter	       < TYPE_PRECISION (result_type))
365218334Speter	      && (TYPE_PRECISION (TREE_TYPE (arg1))
365318334Speter		  == TYPE_PRECISION (TREE_TYPE (arg0)))
365418334Speter	      && unsigned0 == unsigned1
365518334Speter	      && (unsigned0 || !uns))
3656117395Skan	    result_type = c_common_signed_or_unsigned_type
3657117395Skan	      (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
365818334Speter	  else if (TREE_CODE (arg0) == INTEGER_CST
365918334Speter		   && (unsigned1 || !uns)
366018334Speter		   && (TYPE_PRECISION (TREE_TYPE (arg1))
366118334Speter		       < TYPE_PRECISION (result_type))
3662117395Skan		   && (type = c_common_signed_or_unsigned_type
3663117395Skan		       (unsigned1, TREE_TYPE (arg1)),
366418334Speter		       int_fits_type_p (arg0, type)))
366518334Speter	    result_type = type;
366618334Speter	  else if (TREE_CODE (arg1) == INTEGER_CST
366718334Speter		   && (unsigned0 || !uns)
366818334Speter		   && (TYPE_PRECISION (TREE_TYPE (arg0))
366918334Speter		       < TYPE_PRECISION (result_type))
3670117395Skan		   && (type = c_common_signed_or_unsigned_type
3671117395Skan		       (unsigned0, TREE_TYPE (arg0)),
367218334Speter		       int_fits_type_p (arg1, type)))
367318334Speter	    result_type = type;
367418334Speter	}
367518334Speter
367618334Speter      /* Shifts can be shortened if shifting right.  */
367718334Speter
367818334Speter      if (short_shift)
367918334Speter	{
368018334Speter	  int unsigned_arg;
368118334Speter	  tree arg0 = get_narrower (op0, &unsigned_arg);
368218334Speter
368318334Speter	  final_type = result_type;
368418334Speter
368518334Speter	  if (arg0 == op0 && final_type == TREE_TYPE (op0))
3686169689Skan	    unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
368718334Speter
368818334Speter	  if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
368918334Speter	      /* We can shorten only if the shift count is less than the
369018334Speter		 number of bits in the smaller type size.  */
369190075Sobrien	      && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
369218334Speter	      /* If arg is sign-extended and then unsigned-shifted,
369318334Speter		 we can simulate this with a signed shift in arg's type
369418334Speter		 only if the extended result is at least twice as wide
369518334Speter		 as the arg.  Otherwise, the shift could use up all the
369618334Speter		 ones made by sign-extension and bring in zeros.
369718334Speter		 We can't optimize that case at all, but in most machines
369818334Speter		 it never happens because available widths are 2**N.  */
3699169689Skan	      && (!TYPE_UNSIGNED (final_type)
370018334Speter		  || unsigned_arg
370150397Sobrien		  || (((unsigned) 2 * TYPE_PRECISION (TREE_TYPE (arg0)))
370218334Speter		      <= TYPE_PRECISION (result_type))))
370318334Speter	    {
370418334Speter	      /* Do an unsigned shift if the operand was zero-extended.  */
370518334Speter	      result_type
3706117395Skan		= c_common_signed_or_unsigned_type (unsigned_arg,
3707117395Skan						    TREE_TYPE (arg0));
370818334Speter	      /* Convert value-to-be-shifted to that type.  */
370918334Speter	      if (TREE_TYPE (op0) != result_type)
371050397Sobrien		op0 = cp_convert (result_type, op0);
371118334Speter	      converted = 1;
371218334Speter	    }
371318334Speter	}
371418334Speter
371518334Speter      /* Comparison operations are shortened too but differently.
371618334Speter	 They identify themselves by setting short_compare = 1.  */
371718334Speter
371818334Speter      if (short_compare)
371918334Speter	{
372018334Speter	  /* Don't write &op0, etc., because that would prevent op0
372118334Speter	     from being kept in a register.
372218334Speter	     Instead, make copies of the our local variables and
372318334Speter	     pass the copies by reference, then copy them back afterward.  */
372418334Speter	  tree xop0 = op0, xop1 = op1, xresult_type = result_type;
372518334Speter	  enum tree_code xresultcode = resultcode;
3726169689Skan	  tree val
372718334Speter	    = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
372818334Speter	  if (val != 0)
372950397Sobrien	    return cp_convert (boolean_type_node, val);
373018334Speter	  op0 = xop0, op1 = xop1;
373118334Speter	  converted = 1;
373218334Speter	  resultcode = xresultcode;
373318334Speter	}
373418334Speter
373590075Sobrien      if ((short_compare || code == MIN_EXPR || code == MAX_EXPR)
3736132718Skan	  && warn_sign_compare
3737132718Skan	  /* Do not warn until the template is instantiated; we cannot
3738132718Skan	     bound the ranges of the arguments until that point.  */
3739132718Skan	  && !processing_template_decl)
374018334Speter	{
3741169689Skan	  int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
3742169689Skan	  int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
374318334Speter
374418334Speter	  int unsignedp0, unsignedp1;
374518334Speter	  tree primop0 = get_narrower (op0, &unsignedp0);
374618334Speter	  tree primop1 = get_narrower (op1, &unsignedp1);
374718334Speter
374850397Sobrien	  /* Check for comparison of different enum types.  */
3749169689Skan	  if (TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
3750169689Skan	      && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
375150397Sobrien	      && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
3752169689Skan		 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
375350397Sobrien	    {
3754169689Skan	      warning (0, "comparison between types %q#T and %q#T",
3755169689Skan		       TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
375650397Sobrien	    }
375750397Sobrien
375818334Speter	  /* Give warnings for comparisons between signed and unsigned
375918334Speter	     quantities that may fail.  */
376018334Speter	  /* Do the checking based on the original operand trees, so that
376118334Speter	     casts will be considered, but default promotions won't be.  */
376218334Speter
376318334Speter	  /* Do not warn if the comparison is being done in a signed type,
376418334Speter	     since the signed type will only be chosen if it can represent
376518334Speter	     all the values of the unsigned type.  */
3766169689Skan	  if (!TYPE_UNSIGNED (result_type))
376718334Speter	    /* OK */;
376818334Speter	  /* Do not warn if both operands are unsigned.  */
376918334Speter	  else if (op0_signed == op1_signed)
377018334Speter	    /* OK */;
377118334Speter	  /* Do not warn if the signed quantity is an unsuffixed
377218334Speter	     integer literal (or some static constant expression
377390075Sobrien	     involving such literals or a conditional expression
377418334Speter	     involving such literals) and it is non-negative.  */
377590075Sobrien	  else if ((op0_signed && tree_expr_nonnegative_p (orig_op0))
377690075Sobrien		   || (op1_signed && tree_expr_nonnegative_p (orig_op1)))
377718334Speter	    /* OK */;
377818334Speter	  /* Do not warn if the comparison is an equality operation,
377918334Speter	     the unsigned quantity is an integral constant and it does
378018334Speter	     not use the most significant bit of result_type.  */
378118334Speter	  else if ((resultcode == EQ_EXPR || resultcode == NE_EXPR)
378218334Speter		   && ((op0_signed && TREE_CODE (orig_op1) == INTEGER_CST
3783117395Skan			&& int_fits_type_p (orig_op1, c_common_signed_type
3784117395Skan					    (result_type)))
378518334Speter			|| (op1_signed && TREE_CODE (orig_op0) == INTEGER_CST
3786117395Skan			    && int_fits_type_p (orig_op0, c_common_signed_type
3787117395Skan						(result_type)))))
378818334Speter	    /* OK */;
378918334Speter	  else
3790169689Skan	    warning (0, "comparison between signed and unsigned integer expressions");
379118334Speter
379218334Speter	  /* Warn if two unsigned values are being compared in a size
379318334Speter	     larger than their original size, and one (and only one) is the
379418334Speter	     result of a `~' operator.  This comparison will always fail.
379518334Speter
379618334Speter	     Also warn if one operand is a constant, and the constant does not
379718334Speter	     have all bits set that are set in the ~ operand when it is
379818334Speter	     extended.  */
379918334Speter
380050397Sobrien	  if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
380150397Sobrien	      ^ (TREE_CODE (primop1) == BIT_NOT_EXPR))
380218334Speter	    {
380318334Speter	      if (TREE_CODE (primop0) == BIT_NOT_EXPR)
380418334Speter		primop0 = get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
380518334Speter	      if (TREE_CODE (primop1) == BIT_NOT_EXPR)
380618334Speter		primop1 = get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
3807169689Skan
380890075Sobrien	      if (host_integerp (primop0, 0) || host_integerp (primop1, 0))
380918334Speter		{
381018334Speter		  tree primop;
381118334Speter		  HOST_WIDE_INT constant, mask;
381218334Speter		  int unsignedp;
381390075Sobrien		  unsigned int bits;
381418334Speter
381590075Sobrien		  if (host_integerp (primop0, 0))
381618334Speter		    {
381718334Speter		      primop = primop1;
381818334Speter		      unsignedp = unsignedp1;
381990075Sobrien		      constant = tree_low_cst (primop0, 0);
382018334Speter		    }
382118334Speter		  else
382218334Speter		    {
382318334Speter		      primop = primop0;
382418334Speter		      unsignedp = unsignedp0;
382590075Sobrien		      constant = tree_low_cst (primop1, 0);
382618334Speter		    }
382718334Speter
382818334Speter		  bits = TYPE_PRECISION (TREE_TYPE (primop));
382918334Speter		  if (bits < TYPE_PRECISION (result_type)
383018334Speter		      && bits < HOST_BITS_PER_LONG && unsignedp)
383118334Speter		    {
383218334Speter		      mask = (~ (HOST_WIDE_INT) 0) << bits;
383318334Speter		      if ((mask & constant) != mask)
3834169689Skan			warning (0, "comparison of promoted ~unsigned with constant");
383518334Speter		    }
383618334Speter		}
383718334Speter	      else if (unsignedp0 && unsignedp1
383818334Speter		       && (TYPE_PRECISION (TREE_TYPE (primop0))
383918334Speter			   < TYPE_PRECISION (result_type))
384018334Speter		       && (TYPE_PRECISION (TREE_TYPE (primop1))
384118334Speter			   < TYPE_PRECISION (result_type)))
3842169689Skan		warning (0, "comparison of promoted ~unsigned with unsigned");
384318334Speter	    }
384418334Speter	}
384518334Speter    }
384618334Speter
3847169689Skan  /* If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
384818334Speter     Then the expression will be built.
384918334Speter     It will be given type FINAL_TYPE if that is nonzero;
385018334Speter     otherwise, it will be given type RESULT_TYPE.  */
385118334Speter
3852117395Skan  /* Issue warnings about peculiar, but valid, uses of NULL.  */
385352284Sobrien  if (/* It's reasonable to use pointer values as operands of &&
385452284Sobrien	 and ||, so NULL is no exception.  */
385552284Sobrien      !(code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
385652284Sobrien      && (/* If OP0 is NULL and OP1 is not a pointer, or vice versa.  */
385752284Sobrien	  (orig_op0 == null_node
385852284Sobrien	   && TREE_CODE (TREE_TYPE (op1)) != POINTER_TYPE)
385952284Sobrien	  /* Or vice versa.  */
386052284Sobrien	  || (orig_op1 == null_node
386152284Sobrien	      && TREE_CODE (TREE_TYPE (op0)) != POINTER_TYPE)
386252284Sobrien	  /* Or, both are NULL and the operation was not a comparison.  */
3863169689Skan	  || (orig_op0 == null_node && orig_op1 == null_node
386452284Sobrien	      && code != EQ_EXPR && code != NE_EXPR)))
386552284Sobrien    /* Some sort of arithmetic operation involving NULL was
386652284Sobrien       performed.  Note that pointer-difference and pointer-addition
386752284Sobrien       have already been handled above, and so we don't end up here in
386852284Sobrien       that case.  */
3869169689Skan    warning (0, "NULL used in arithmetic");
387052284Sobrien
387118334Speter  if (! converted)
387218334Speter    {
387318334Speter      if (TREE_TYPE (op0) != result_type)
3874169689Skan	op0 = cp_convert (result_type, op0);
387518334Speter      if (TREE_TYPE (op1) != result_type)
3876169689Skan	op1 = cp_convert (result_type, op1);
387752284Sobrien
387852284Sobrien      if (op0 == error_mark_node || op1 == error_mark_node)
387952284Sobrien	return error_mark_node;
388018334Speter    }
388118334Speter
388218334Speter  if (build_type == NULL_TREE)
388318334Speter    build_type = result_type;
388418334Speter
3885169689Skan  result = build2 (resultcode, build_type, op0, op1);
3886169689Skan  result = fold_if_not_in_template (result);
3887169689Skan  if (final_type != 0)
3888169689Skan    result = cp_convert (final_type, result);
3889258157Spfg
3890258157Spfg  if (TREE_OVERFLOW_P (result)
3891258157Spfg      && !TREE_OVERFLOW_P (op0)
3892258157Spfg      && !TREE_OVERFLOW_P (op1))
3893258157Spfg    overflow_warning (result);
3894258157Spfg
3895169689Skan  return result;
389618334Speter}
389718334Speter
389818334Speter/* Return a tree for the sum or difference (RESULTCODE says which)
389918334Speter   of pointer PTROP and integer INTOP.  */
390018334Speter
390118334Speterstatic tree
3902132718Skancp_pointer_int_sum (enum tree_code resultcode, tree ptrop, tree intop)
390318334Speter{
390496263Sobrien  tree res_type = TREE_TYPE (ptrop);
390518334Speter
390696263Sobrien  /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
390796263Sobrien     in certain circumstance (when it's valid to do so).  So we need
390896263Sobrien     to make sure it's complete.  We don't need to check here, if we
390996263Sobrien     can actually complete it at all, as those checks will be done in
391096263Sobrien     pointer_int_sum() anyway.  */
391196263Sobrien  complete_type (TREE_TYPE (res_type));
391218334Speter
3913169689Skan  return pointer_int_sum (resultcode, ptrop,
3914169689Skan			  fold_if_not_in_template (intop));
391518334Speter}
391618334Speter
391718334Speter/* Return a tree for the difference of pointers OP0 and OP1.
391818334Speter   The resulting tree has type int.  */
391918334Speter
392018334Speterstatic tree
3921132718Skanpointer_diff (tree op0, tree op1, tree ptrtype)
392218334Speter{
3923169689Skan  tree result;
392418334Speter  tree restype = ptrdiff_type_node;
392550397Sobrien  tree target_type = TREE_TYPE (ptrtype);
392618334Speter
392752284Sobrien  if (!complete_type_or_else (target_type, NULL_TREE))
392850397Sobrien    return error_mark_node;
392950397Sobrien
393050397Sobrien  if (pedantic || warn_pointer_arith)
393118334Speter    {
393218334Speter      if (TREE_CODE (target_type) == VOID_TYPE)
3933169689Skan	pedwarn ("ISO C++ forbids using pointer of type %<void *%> in subtraction");
393418334Speter      if (TREE_CODE (target_type) == FUNCTION_TYPE)
393590075Sobrien	pedwarn ("ISO C++ forbids using pointer to a function in subtraction");
393618334Speter      if (TREE_CODE (target_type) == METHOD_TYPE)
393790075Sobrien	pedwarn ("ISO C++ forbids using pointer to a method in subtraction");
393818334Speter    }
393918334Speter
394018334Speter  /* First do the subtraction as integers;
394118334Speter     then drop through to build the divide operator.  */
394218334Speter
3943169689Skan  op0 = cp_build_binary_op (MINUS_EXPR,
394490075Sobrien			    cp_convert (restype, op0),
394590075Sobrien			    cp_convert (restype, op1));
394618334Speter
394718334Speter  /* This generates an error if op1 is a pointer to an incomplete type.  */
394890075Sobrien  if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1))))
394990075Sobrien    error ("invalid use of a pointer to an incomplete type in pointer arithmetic");
395018334Speter
3951169689Skan  op1 = (TYPE_PTROB_P (ptrtype)
3952132718Skan	 ? size_in_bytes (target_type)
3953132718Skan	 : integer_one_node);
395418334Speter
395518334Speter  /* Do the division.  */
395618334Speter
3957169689Skan  result = build2 (EXACT_DIV_EXPR, restype, op0, cp_convert (restype, op1));
3958169689Skan  return fold_if_not_in_template (result);
395918334Speter}
396018334Speter
396118334Speter/* Construct and perhaps optimize a tree representation
396218334Speter   for a unary operation.  CODE, a tree_code, specifies the operation
396318334Speter   and XARG is the operand.  */
396418334Speter
396518334Spetertree
3966132718Skanbuild_x_unary_op (enum tree_code code, tree xarg)
396718334Speter{
3968132718Skan  tree orig_expr = xarg;
396990075Sobrien  tree exp;
397090075Sobrien  int ptrmem = 0;
3971169689Skan
397250397Sobrien  if (processing_template_decl)
3973132718Skan    {
3974132718Skan      if (type_dependent_expression_p (xarg))
3975132718Skan	return build_min_nt (code, xarg, NULL_TREE);
3976146895Skan
3977132718Skan      xarg = build_non_dependent_expr (xarg);
3978132718Skan    }
397950397Sobrien
3980132718Skan  exp = NULL_TREE;
3981132718Skan
3982132718Skan  /* [expr.unary.op] says:
3983132718Skan
3984132718Skan       The address of an object of incomplete type can be taken.
3985132718Skan
3986132718Skan     (And is just the ordinary address operator, not an overloaded
3987132718Skan     "operator &".)  However, if the type is a template
3988132718Skan     specialization, we must complete the type at this point so that
3989132718Skan     an overloaded "operator &" will be available if required.  */
399018334Speter  if (code == ADDR_EXPR
399150397Sobrien      && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
3992132718Skan      && ((CLASS_TYPE_P (TREE_TYPE (xarg))
3993132718Skan	   && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (xarg))))
399418334Speter	  || (TREE_CODE (xarg) == OFFSET_REF)))
3995132718Skan    /* Don't look for a function.  */;
399618334Speter  else
3997132718Skan    exp = build_new_op (code, LOOKUP_NORMAL, xarg, NULL_TREE, NULL_TREE,
3998132718Skan			/*overloaded_p=*/NULL);
3999132718Skan  if (!exp && code == ADDR_EXPR)
400018334Speter    {
4001107590Sobrien      /*  A pointer to member-function can be formed only by saying
4002107590Sobrien	  &X::mf.  */
4003107590Sobrien      if (!flag_ms_extensions && TREE_CODE (TREE_TYPE (xarg)) == METHOD_TYPE
4004107590Sobrien	  && (TREE_CODE (xarg) != OFFSET_REF || !PTRMEM_OK_P (xarg)))
4005107590Sobrien	{
4006169689Skan	  if (TREE_CODE (xarg) != OFFSET_REF
4007169689Skan	      || !TYPE_P (TREE_OPERAND (xarg, 0)))
4008107590Sobrien	    {
4009169689Skan	      error ("invalid use of %qE to form a pointer-to-member-function",
4010107590Sobrien		     xarg);
4011169689Skan	      if (TREE_CODE (xarg) != OFFSET_REF)
4012169689Skan		inform ("  a qualified-id is required");
4013107590Sobrien	      return error_mark_node;
4014107590Sobrien	    }
4015107590Sobrien	  else
4016107590Sobrien	    {
4017169689Skan	      error ("parentheses around %qE cannot be used to form a"
4018169689Skan		     " pointer-to-member-function",
4019107590Sobrien		     xarg);
4020107590Sobrien	      PTRMEM_OK_P (xarg) = 1;
4021107590Sobrien	    }
4022107590Sobrien	}
4023169689Skan
402490075Sobrien      if (TREE_CODE (xarg) == OFFSET_REF)
4025169689Skan	{
4026169689Skan	  ptrmem = PTRMEM_OK_P (xarg);
4027169689Skan
4028169689Skan	  if (!ptrmem && !flag_ms_extensions
4029169689Skan	      && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg, 1))) == METHOD_TYPE)
403090075Sobrien	    {
403190075Sobrien	      /* A single non-static member, make sure we don't allow a
4032169689Skan		 pointer-to-member.  */
4033169689Skan	      xarg = build2 (OFFSET_REF, TREE_TYPE (xarg),
4034169689Skan			     TREE_OPERAND (xarg, 0),
4035169689Skan			     ovl_cons (TREE_OPERAND (xarg, 1), NULL_TREE));
403690075Sobrien	      PTRMEM_OK_P (xarg) = ptrmem;
4037169689Skan	    }
4038169689Skan	}
403990075Sobrien      else if (TREE_CODE (xarg) == TARGET_EXPR)
4040169689Skan	warning (0, "taking address of temporary");
4041132718Skan      exp = build_unary_op (ADDR_EXPR, xarg, 0);
404250397Sobrien    }
404350397Sobrien
4044132718Skan  if (processing_template_decl && exp != error_mark_node)
4045146895Skan    exp = build_min_non_dep (code, exp, orig_expr,
4046146895Skan			     /*For {PRE,POST}{INC,DEC}REMENT_EXPR*/NULL_TREE);
4047146895Skan  if (TREE_CODE (exp) == ADDR_EXPR)
4048146895Skan    PTRMEM_OK_P (exp) = ptrmem;
404990075Sobrien  return exp;
405018334Speter}
405118334Speter
4052117395Skan/* Like c_common_truthvalue_conversion, but handle pointer-to-member
4053117395Skan   constants, where a null value is represented by an INTEGER_CST of
4054117395Skan   -1.  */
405590075Sobrien
405690075Sobrientree
4057132718Skancp_truthvalue_conversion (tree expr)
405890075Sobrien{
405990075Sobrien  tree type = TREE_TYPE (expr);
406090075Sobrien  if (TYPE_PTRMEM_P (type))
406190075Sobrien    return build_binary_op (NE_EXPR, expr, integer_zero_node, 1);
406290075Sobrien  else
4063117395Skan    return c_common_truthvalue_conversion (expr);
406490075Sobrien}
406590075Sobrien
406690075Sobrien/* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR.  */
4067169689Skan
406818334Spetertree
4069132718Skancondition_conversion (tree expr)
407018334Speter{
407150397Sobrien  tree t;
407250397Sobrien  if (processing_template_decl)
407350397Sobrien    return expr;
407490075Sobrien  t = perform_implicit_conversion (boolean_type_node, expr);
4075169689Skan  t = fold_build_cleanup_point_expr (boolean_type_node, t);
407618334Speter  return t;
407718334Speter}
4078169689Skan
4079117395Skan/* Return an ADDR_EXPR giving the address of T.  This function
4080117395Skan   attempts no optimizations or simplifications; it is a low-level
4081117395Skan   primitive.  */
4082117395Skan
4083117395Skantree
4084117395Skanbuild_address (tree t)
4085117395Skan{
4086117395Skan  tree addr;
4087117395Skan
4088117395Skan  if (error_operand_p (t) || !cxx_mark_addressable (t))
4089117395Skan    return error_mark_node;
4090117395Skan
4091132718Skan  addr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (t)), t);
4092117395Skan
4093117395Skan  return addr;
4094117395Skan}
4095117395Skan
4096117395Skan/* Return a NOP_EXPR converting EXPR to TYPE.  */
4097117395Skan
4098117395Skantree
4099117395Skanbuild_nop (tree type, tree expr)
4100117395Skan{
4101117395Skan  if (type == error_mark_node || error_operand_p (expr))
4102117395Skan    return expr;
4103169689Skan  return build1 (NOP_EXPR, type, expr);
4104117395Skan}
4105117395Skan
410618334Speter/* C++: Must handle pointers to members.
410718334Speter
410818334Speter   Perhaps type instantiation should be extended to handle conversion
410918334Speter   from aggregates to types we don't yet know we want?  (Or are those
411018334Speter   cases typically errors which should be reported?)
411118334Speter
411218334Speter   NOCONVERT nonzero suppresses the default promotions
411318334Speter   (such as from short to int).  */
411450397Sobrien
411518334Spetertree
4116132718Skanbuild_unary_op (enum tree_code code, tree xarg, int noconvert)
411718334Speter{
411818334Speter  /* No default_conversion here.  It causes trouble for ADDR_EXPR.  */
4119132718Skan  tree arg = xarg;
4120132718Skan  tree argtype = 0;
412152284Sobrien  const char *errstring = NULL;
412218334Speter  tree val;
4123169689Skan  const char *invalid_op_diag;
412418334Speter
412518334Speter  if (arg == error_mark_node)
412618334Speter    return error_mark_node;
412718334Speter
4128169689Skan  if ((invalid_op_diag
4129169689Skan       = targetm.invalid_unary_op ((code == UNARY_PLUS_EXPR
4130169689Skan				    ? CONVERT_EXPR
4131169689Skan				    : code),
4132169689Skan				   TREE_TYPE (xarg))))
4133169689Skan    {
4134259666Spfg      error (invalid_op_diag, "");
4135169689Skan      return error_mark_node;
4136169689Skan    }
4137169689Skan
413818334Speter  switch (code)
413918334Speter    {
4140169689Skan    case UNARY_PLUS_EXPR:
4141169689Skan    case NEGATE_EXPR:
4142169689Skan      {
4143169689Skan	int flags = WANT_ARITH | WANT_ENUM;
4144169689Skan	/* Unary plus (but not unary minus) is allowed on pointers.  */
4145169689Skan	if (code == UNARY_PLUS_EXPR)
4146169689Skan	  flags |= WANT_POINTER;
4147169689Skan	arg = build_expr_type_conversion (flags, arg, true);
4148169689Skan	if (!arg)
4149169689Skan	  errstring = (code == NEGATE_EXPR
4150169689Skan		       ? "wrong type argument to unary minus"
4151169689Skan		       : "wrong type argument to unary plus");
4152169689Skan	else
4153169689Skan	  {
4154169689Skan	    if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
4155169689Skan	      arg = perform_integral_promotions (arg);
415618334Speter
4157169689Skan	    /* Make sure the result is not an lvalue: a unary plus or minus
4158169689Skan	       expression is always a rvalue.  */
4159169689Skan	    arg = rvalue (arg);
4160169689Skan	  }
4161169689Skan      }
416218334Speter      break;
416318334Speter
416418334Speter    case BIT_NOT_EXPR:
416550397Sobrien      if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
416650397Sobrien	{
416750397Sobrien	  code = CONJ_EXPR;
416850397Sobrien	  if (!noconvert)
416950397Sobrien	    arg = default_conversion (arg);
417050397Sobrien	}
4171169689Skan      else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM
4172169689Skan						   | WANT_VECTOR,
4173132718Skan						   arg, true)))
417418334Speter	errstring = "wrong type argument to bit-complement";
4175169689Skan      else if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
4176132718Skan	arg = perform_integral_promotions (arg);
417718334Speter      break;
417818334Speter
417918334Speter    case ABS_EXPR:
4180132718Skan      if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
418118334Speter	errstring = "wrong type argument to abs";
418218334Speter      else if (!noconvert)
418318334Speter	arg = default_conversion (arg);
418418334Speter      break;
418518334Speter
418650397Sobrien    case CONJ_EXPR:
418750397Sobrien      /* Conjugating a real value is a no-op, but allow it anyway.  */
4188132718Skan      if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
418950397Sobrien	errstring = "wrong type argument to conjugation";
419050397Sobrien      else if (!noconvert)
419150397Sobrien	arg = default_conversion (arg);
419250397Sobrien      break;
419350397Sobrien
419418334Speter    case TRUTH_NOT_EXPR:
4195132718Skan      arg = perform_implicit_conversion (boolean_type_node, arg);
419618334Speter      val = invert_truthvalue (arg);
419718334Speter      if (arg != error_mark_node)
419818334Speter	return val;
419918334Speter      errstring = "in argument to unary !";
420018334Speter      break;
420118334Speter
420218334Speter    case NOP_EXPR:
420318334Speter      break;
4204169689Skan
420550397Sobrien    case REALPART_EXPR:
420650397Sobrien      if (TREE_CODE (arg) == COMPLEX_CST)
420750397Sobrien	return TREE_REALPART (arg);
420850397Sobrien      else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4209169689Skan	{
4210169689Skan	  arg = build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg);
4211169689Skan	  return fold_if_not_in_template (arg);
4212169689Skan	}
421350397Sobrien      else
421450397Sobrien	return arg;
421550397Sobrien
421650397Sobrien    case IMAGPART_EXPR:
421750397Sobrien      if (TREE_CODE (arg) == COMPLEX_CST)
421850397Sobrien	return TREE_IMAGPART (arg);
421950397Sobrien      else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4220169689Skan	{
4221169689Skan	  arg = build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg);
4222169689Skan	  return fold_if_not_in_template (arg);
4223169689Skan	}
422450397Sobrien      else
422550397Sobrien	return cp_convert (TREE_TYPE (arg), integer_zero_node);
4226169689Skan
422718334Speter    case PREINCREMENT_EXPR:
422818334Speter    case POSTINCREMENT_EXPR:
422918334Speter    case PREDECREMENT_EXPR:
423018334Speter    case POSTDECREMENT_EXPR:
423118334Speter      /* Handle complex lvalues (when permitted)
423218334Speter	 by reduction to simpler cases.  */
423318334Speter
423418334Speter      val = unary_complex_lvalue (code, arg);
423518334Speter      if (val != 0)
423618334Speter	return val;
423718334Speter
423850397Sobrien      /* Increment or decrement the real part of the value,
423950397Sobrien	 and don't change the imaginary part.  */
424050397Sobrien      if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
424150397Sobrien	{
424250397Sobrien	  tree real, imag;
424350397Sobrien
424450397Sobrien	  arg = stabilize_reference (arg);
424550397Sobrien	  real = build_unary_op (REALPART_EXPR, arg, 1);
424650397Sobrien	  imag = build_unary_op (IMAGPART_EXPR, arg, 1);
4247169689Skan	  return build2 (COMPLEX_EXPR, TREE_TYPE (arg),
4248169689Skan			 build_unary_op (code, real, 1), imag);
424950397Sobrien	}
425050397Sobrien
425118334Speter      /* Report invalid types.  */
425218334Speter
425318334Speter      if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
4254132718Skan					      arg, true)))
425518334Speter	{
425618334Speter	  if (code == PREINCREMENT_EXPR)
425718334Speter	    errstring ="no pre-increment operator for type";
425818334Speter	  else if (code == POSTINCREMENT_EXPR)
425918334Speter	    errstring ="no post-increment operator for type";
426018334Speter	  else if (code == PREDECREMENT_EXPR)
426118334Speter	    errstring ="no pre-decrement operator for type";
426218334Speter	  else
426318334Speter	    errstring ="no post-decrement operator for type";
426418334Speter	  break;
426518334Speter	}
426618334Speter
426718334Speter      /* Report something read-only.  */
426818334Speter
426952284Sobrien      if (CP_TYPE_CONST_P (TREE_TYPE (arg))
427018334Speter	  || TREE_READONLY (arg))
427118334Speter	readonly_error (arg, ((code == PREINCREMENT_EXPR
427218334Speter			       || code == POSTINCREMENT_EXPR)
427318334Speter			      ? "increment" : "decrement"),
427418334Speter			0);
427518334Speter
427618334Speter      {
4277132718Skan	tree inc;
4278169689Skan	tree declared_type;
427918334Speter	tree result_type = TREE_TYPE (arg);
428018334Speter
4281169689Skan	declared_type = unlowered_expr_type (arg);
4282169689Skan
428318334Speter	arg = get_unwidened (arg, 0);
428418334Speter	argtype = TREE_TYPE (arg);
428518334Speter
428618334Speter	/* ARM $5.2.5 last annotation says this should be forbidden.  */
428718334Speter	if (TREE_CODE (argtype) == ENUMERAL_TYPE)
428890075Sobrien	  pedwarn ("ISO C++ forbids %sing an enum",
428918334Speter		   (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
429018334Speter		   ? "increment" : "decrement");
4291169689Skan
429218334Speter	/* Compute the increment.  */
429318334Speter
429418334Speter	if (TREE_CODE (argtype) == POINTER_TYPE)
429518334Speter	  {
429690075Sobrien	    tree type = complete_type (TREE_TYPE (argtype));
4297169689Skan
429890075Sobrien	    if (!COMPLETE_OR_VOID_TYPE_P (type))
4299169689Skan	      error ("cannot %s a pointer to incomplete type %qT",
4300169689Skan		     ((code == PREINCREMENT_EXPR
4301169689Skan		       || code == POSTINCREMENT_EXPR)
4302169689Skan		      ? "increment" : "decrement"), TREE_TYPE (argtype));
430350397Sobrien	    else if ((pedantic || warn_pointer_arith)
4304132718Skan		     && !TYPE_PTROB_P (argtype))
4305169689Skan	      pedwarn ("ISO C++ forbids %sing a pointer of type %qT",
4306169689Skan		       ((code == PREINCREMENT_EXPR
4307169689Skan			 || code == POSTINCREMENT_EXPR)
4308169689Skan			? "increment" : "decrement"), argtype);
4309117395Skan	    inc = cxx_sizeof_nowarn (TREE_TYPE (argtype));
431018334Speter	  }
431118334Speter	else
431218334Speter	  inc = integer_one_node;
431318334Speter
431450397Sobrien	inc = cp_convert (argtype, inc);
431518334Speter
431618334Speter	/* Handle incrementing a cast-expression.  */
431718334Speter
431818334Speter	switch (TREE_CODE (arg))
431918334Speter	  {
432018334Speter	  case NOP_EXPR:
432118334Speter	  case CONVERT_EXPR:
432218334Speter	  case FLOAT_EXPR:
432318334Speter	  case FIX_TRUNC_EXPR:
432418334Speter	  case FIX_FLOOR_EXPR:
432518334Speter	  case FIX_ROUND_EXPR:
432618334Speter	  case FIX_CEIL_EXPR:
432718334Speter	    {
432850397Sobrien	      tree incremented, modify, value, compound;
432918334Speter	      if (! lvalue_p (arg) && pedantic)
433018334Speter		pedwarn ("cast to non-reference type used as lvalue");
433118334Speter	      arg = stabilize_reference (arg);
433218334Speter	      if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
433318334Speter		value = arg;
433418334Speter	      else
433518334Speter		value = save_expr (arg);
4336169689Skan	      incremented = build2 (((code == PREINCREMENT_EXPR
4337169689Skan				      || code == POSTINCREMENT_EXPR)
4338169689Skan				     ? PLUS_EXPR : MINUS_EXPR),
4339169689Skan				    argtype, value, inc);
434050397Sobrien
434118334Speter	      modify = build_modify_expr (arg, NOP_EXPR, incremented);
4342169689Skan	      compound = build2 (COMPOUND_EXPR, TREE_TYPE (arg),
4343169689Skan				 modify, value);
434450397Sobrien
434550397Sobrien	      /* Eliminate warning about unused result of + or -.  */
4346169689Skan	      TREE_NO_WARNING (compound) = 1;
434750397Sobrien	      return compound;
434818334Speter	    }
434950397Sobrien
435050397Sobrien	  default:
435150397Sobrien	    break;
435218334Speter	  }
435318334Speter
435418334Speter	/* Complain about anything else that is not a true lvalue.  */
435518334Speter	if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
435618334Speter				    || code == POSTINCREMENT_EXPR)
4357169689Skan				   ? lv_increment : lv_decrement)))
435818334Speter	  return error_mark_node;
435918334Speter
436018334Speter	/* Forbid using -- on `bool'.  */
4361169689Skan	if (same_type_p (declared_type, boolean_type_node))
436218334Speter	  {
436318334Speter	    if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
436418334Speter	      {
4365169689Skan		error ("invalid use of %<--%> on bool variable %qD", arg);
436618334Speter		return error_mark_node;
436718334Speter	      }
436890075Sobrien	    val = boolean_increment (code, arg);
436918334Speter	  }
437018334Speter	else
4371169689Skan	  val = build2 (code, TREE_TYPE (arg), arg, inc);
437218334Speter
437318334Speter	TREE_SIDE_EFFECTS (val) = 1;
437450397Sobrien	return cp_convert (result_type, val);
437518334Speter      }
437618334Speter
437718334Speter    case ADDR_EXPR:
437818334Speter      /* Note that this operation never does default_conversion
437918334Speter	 regardless of NOCONVERT.  */
438018334Speter
438152284Sobrien      argtype = lvalue_type (arg);
4382132718Skan
4383132718Skan      if (TREE_CODE (arg) == OFFSET_REF)
4384132718Skan	goto offset_ref;
4385132718Skan
438618334Speter      if (TREE_CODE (argtype) == REFERENCE_TYPE)
438718334Speter	{
4388169689Skan	  tree type = build_pointer_type (TREE_TYPE (argtype));
4389169689Skan	  arg = build1 (CONVERT_EXPR, type, arg);
439018334Speter	  return arg;
439118334Speter	}
439250397Sobrien      else if (pedantic && DECL_MAIN_P (arg))
439318334Speter	/* ARM $3.4 */
4394169689Skan	pedwarn ("ISO C++ forbids taking address of function %<::main%>");
439518334Speter
439618334Speter      /* Let &* cancel out to simplify resulting code.  */
439718334Speter      if (TREE_CODE (arg) == INDIRECT_REF)
439818334Speter	{
439950397Sobrien	  /* We don't need to have `current_class_ptr' wrapped in a
440018334Speter	     NON_LVALUE_EXPR node.  */
440150397Sobrien	  if (arg == current_class_ref)
440250397Sobrien	    return current_class_ptr;
440318334Speter
440418334Speter	  arg = TREE_OPERAND (arg, 0);
440518334Speter	  if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE)
440618334Speter	    {
4407169689Skan	      tree type = build_pointer_type (TREE_TYPE (TREE_TYPE (arg)));
4408169689Skan	      arg = build1 (CONVERT_EXPR, type, arg);
440918334Speter	    }
4410169689Skan	  else
441118334Speter	    /* Don't let this be an lvalue.  */
4412169689Skan	    arg = rvalue (arg);
441318334Speter	  return arg;
441418334Speter	}
441518334Speter
441618334Speter      /* Uninstantiated types are all functions.  Taking the
441718334Speter	 address of a function is a no-op, so just return the
441818334Speter	 argument.  */
441918334Speter
4420169689Skan      gcc_assert (TREE_CODE (arg) != IDENTIFIER_NODE
4421169689Skan		  || !IDENTIFIER_OPNAME_P (arg));
442218334Speter
442352284Sobrien      if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg)
4424117395Skan	  && !really_overloaded_fn (TREE_OPERAND (arg, 1)))
4425169689Skan	{
442652284Sobrien	  /* They're trying to take the address of a unique non-static
442790075Sobrien	     member function.  This is ill-formed (except in MS-land),
442890075Sobrien	     but let's try to DTRT.
442990075Sobrien	     Note: We only handle unique functions here because we don't
443090075Sobrien	     want to complain if there's a static overload; non-unique
443190075Sobrien	     cases will be handled by instantiate_type.  But we need to
443290075Sobrien	     handle this case here to allow casts on the resulting PMF.
443390075Sobrien	     We could defer this in non-MS mode, but it's easier to give
443490075Sobrien	     a useful error here.  */
443518334Speter
4436132718Skan	  /* Inside constant member functions, the `this' pointer
4437132718Skan	     contains an extra const qualifier.  TYPE_MAIN_VARIANT
4438132718Skan	     is used here to remove this const from the diagnostics
4439132718Skan	     and the created OFFSET_REF.  */
4440132718Skan	  tree base = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg, 0)));
4441169689Skan	  tree fn = get_first_fn (TREE_OPERAND (arg, 1));
4442169689Skan	  mark_used (fn);
444350397Sobrien
444490075Sobrien	  if (! flag_ms_extensions)
444590075Sobrien	    {
4446169689Skan	      tree name = DECL_NAME (fn);
444790075Sobrien	      if (current_class_type
444890075Sobrien		  && TREE_OPERAND (arg, 0) == current_class_ref)
444990075Sobrien		/* An expression like &memfn.  */
4450132718Skan		pedwarn ("ISO C++ forbids taking the address of an unqualified"
4451132718Skan			 " or parenthesized non-static member function to form"
4452169689Skan			 " a pointer to member function.  Say %<&%T::%D%>",
4453132718Skan			 base, name);
445490075Sobrien	      else
4455132718Skan		pedwarn ("ISO C++ forbids taking the address of a bound member"
4456132718Skan			 " function to form a pointer to member function."
4457169689Skan			 "  Say %<&%T::%D%>",
4458132718Skan			 base, name);
445990075Sobrien	    }
4460169689Skan	  arg = build_offset_ref (base, fn, /*address_p=*/true);
4461169689Skan	}
4462132718Skan
4463169689Skan    offset_ref:
446452284Sobrien      if (type_unknown_p (arg))
446552284Sobrien	return build1 (ADDR_EXPR, unknown_type_node, arg);
4466169689Skan
446718334Speter      /* Handle complex lvalues (when permitted)
446818334Speter	 by reduction to simpler cases.  */
446918334Speter      val = unary_complex_lvalue (code, arg);
447018334Speter      if (val != 0)
447118334Speter	return val;
447218334Speter
447318334Speter      switch (TREE_CODE (arg))
447418334Speter	{
447518334Speter	case NOP_EXPR:
447618334Speter	case CONVERT_EXPR:
447718334Speter	case FLOAT_EXPR:
447818334Speter	case FIX_TRUNC_EXPR:
447918334Speter	case FIX_FLOOR_EXPR:
448018334Speter	case FIX_ROUND_EXPR:
448118334Speter	case FIX_CEIL_EXPR:
448218334Speter	  if (! lvalue_p (arg) && pedantic)
448390075Sobrien	    pedwarn ("ISO C++ forbids taking the address of a cast to a non-lvalue expression");
448450397Sobrien	  break;
4485132718Skan
4486169689Skan	case BASELINK:
4487169689Skan	  arg = BASELINK_FUNCTIONS (arg);
4488169689Skan	  /* Fall through.  */
4489169689Skan
4490132718Skan	case OVERLOAD:
4491132718Skan	  arg = OVL_CURRENT (arg);
4492132718Skan	  break;
4493132718Skan
4494169689Skan	case OFFSET_REF:
4495169689Skan	  /* Turn a reference to a non-static data member into a
4496169689Skan	     pointer-to-member.  */
4497169689Skan	  {
4498169689Skan	    tree type;
4499169689Skan	    tree t;
4500169689Skan
4501169689Skan	    if (!PTRMEM_OK_P (arg))
4502169689Skan	      return build_unary_op (code, arg, 0);
4503169689Skan
4504169689Skan	    t = TREE_OPERAND (arg, 1);
4505169689Skan	    if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
4506169689Skan	      {
4507169689Skan		error ("cannot create pointer to reference member %qD", t);
4508169689Skan		return error_mark_node;
4509169689Skan	      }
4510169689Skan
4511169689Skan	    type = build_ptrmem_type (context_for_name_lookup (t),
4512169689Skan				      TREE_TYPE (t));
4513169689Skan	    t = make_ptrmem_cst (type, TREE_OPERAND (arg, 1));
4514169689Skan	    return t;
4515169689Skan	  }
4516169689Skan
451750397Sobrien	default:
451850397Sobrien	  break;
451918334Speter	}
452018334Speter
452118334Speter      /* Anything not already handled and not a true memory reference
452218334Speter	 is an error.  */
4523169689Skan      if (TREE_CODE (argtype) != FUNCTION_TYPE
4524169689Skan	  && TREE_CODE (argtype) != METHOD_TYPE
4525169689Skan	  && TREE_CODE (arg) != OFFSET_REF
4526169689Skan	  && !lvalue_or_else (arg, lv_addressof))
452718334Speter	return error_mark_node;
452818334Speter
452952284Sobrien      if (argtype != error_mark_node)
453052284Sobrien	argtype = build_pointer_type (argtype);
453118334Speter
4532169689Skan      /* In a template, we are processing a non-dependent expression
4533169689Skan	 so we can just form an ADDR_EXPR with the correct type.  */
4534169689Skan      if (processing_template_decl)
4535169689Skan	{
4536169689Skan	  val = build_address (arg);
4537169689Skan	  if (TREE_CODE (arg) == OFFSET_REF)
4538169689Skan	    PTRMEM_OK_P (val) = PTRMEM_OK_P (arg);
4539169689Skan	  return val;
4540169689Skan	}
454118334Speter
4542169689Skan      if (TREE_CODE (arg) != COMPONENT_REF)
4543169689Skan	{
4544169689Skan	  val = build_address (arg);
4545169689Skan	  if (TREE_CODE (arg) == OFFSET_REF)
4546169689Skan	    PTRMEM_OK_P (val) = PTRMEM_OK_P (arg);
4547169689Skan	}
4548169689Skan      else if (TREE_CODE (TREE_OPERAND (arg, 1)) == BASELINK)
4549169689Skan	{
4550169689Skan	  tree fn = BASELINK_FUNCTIONS (TREE_OPERAND (arg, 1));
4551132718Skan
4552169689Skan	  /* We can only get here with a single static member
4553169689Skan	     function.  */
4554169689Skan	  gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
4555169689Skan		      && DECL_STATIC_FUNCTION_P (fn));
4556169689Skan	  mark_used (fn);
4557169689Skan	  val = build_address (fn);
4558169689Skan	  if (TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
4559169689Skan	    /* Do not lose object's side effects.  */
4560169689Skan	    val = build2 (COMPOUND_EXPR, TREE_TYPE (val),
4561169689Skan			  TREE_OPERAND (arg, 0), val);
4562169689Skan	}
4563169689Skan      else if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
4564169689Skan	{
4565169689Skan	  error ("attempt to take address of bit-field structure member %qD",
4566169689Skan		 TREE_OPERAND (arg, 1));
4567169689Skan	  return error_mark_node;
4568169689Skan	}
4569169689Skan      else
4570169689Skan	{
4571169689Skan	  tree object = TREE_OPERAND (arg, 0);
4572169689Skan	  tree field = TREE_OPERAND (arg, 1);
4573169689Skan	  gcc_assert (same_type_ignoring_top_level_qualifiers_p
4574169689Skan		      (TREE_TYPE (object), decl_type_context (field)));
4575169689Skan	  val = build_address (arg);
4576169689Skan	}
457718334Speter
4578169689Skan      if (TREE_CODE (argtype) == POINTER_TYPE
4579169689Skan	  && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
4580169689Skan	{
4581169689Skan	  build_ptrmemfunc_type (argtype);
4582169689Skan	  val = build_ptrmemfunc (argtype, val, 0,
4583169689Skan				  /*c_cast_p=*/false);
4584169689Skan	}
458550397Sobrien
4586169689Skan      return val;
458750397Sobrien
458850397Sobrien    default:
458950397Sobrien      break;
459018334Speter    }
459118334Speter
459218334Speter  if (!errstring)
459318334Speter    {
459418334Speter      if (argtype == 0)
459518334Speter	argtype = TREE_TYPE (arg);
4596169689Skan      return fold_if_not_in_template (build1 (code, argtype, arg));
459718334Speter    }
459818334Speter
459990075Sobrien  error ("%s", errstring);
460018334Speter  return error_mark_node;
460118334Speter}
460218334Speter
460318334Speter/* Apply unary lvalue-demanding operator CODE to the expression ARG
460418334Speter   for certain kinds of expressions which are not really lvalues
460518334Speter   but which we can accept as lvalues.
460618334Speter
4607146895Skan   If ARG is not a kind of expression we can handle, return
4608146895Skan   NULL_TREE.  */
4609169689Skan
461018334Spetertree
4611132718Skanunary_complex_lvalue (enum tree_code code, tree arg)
461218334Speter{
4613169689Skan  /* Inside a template, making these kinds of adjustments is
4614169689Skan     pointless; we are only concerned with the type of the
4615169689Skan     expression.  */
4616169689Skan  if (processing_template_decl)
4617169689Skan    return NULL_TREE;
4618169689Skan
461918334Speter  /* Handle (a, b) used as an "lvalue".  */
462018334Speter  if (TREE_CODE (arg) == COMPOUND_EXPR)
462118334Speter    {
462218334Speter      tree real_result = build_unary_op (code, TREE_OPERAND (arg, 1), 0);
4623169689Skan      return build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
4624169689Skan		     TREE_OPERAND (arg, 0), real_result);
462518334Speter    }
462618334Speter
462718334Speter  /* Handle (a ? b : c) used as an "lvalue".  */
462850397Sobrien  if (TREE_CODE (arg) == COND_EXPR
462950397Sobrien      || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
463018334Speter    return rationalize_conditional_expr (code, arg);
463118334Speter
463290075Sobrien  /* Handle (a = b), (++a), and (--a) used as an "lvalue".  */
463318334Speter  if (TREE_CODE (arg) == MODIFY_EXPR
463418334Speter      || TREE_CODE (arg) == PREINCREMENT_EXPR
463518334Speter      || TREE_CODE (arg) == PREDECREMENT_EXPR)
463690075Sobrien    {
463790075Sobrien      tree lvalue = TREE_OPERAND (arg, 0);
463890075Sobrien      if (TREE_SIDE_EFFECTS (lvalue))
463990075Sobrien	{
464090075Sobrien	  lvalue = stabilize_reference (lvalue);
4641169689Skan	  arg = build2 (TREE_CODE (arg), TREE_TYPE (arg),
4642169689Skan			lvalue, TREE_OPERAND (arg, 1));
464390075Sobrien	}
464490075Sobrien      return unary_complex_lvalue
4645169689Skan	(code, build2 (COMPOUND_EXPR, TREE_TYPE (lvalue), arg, lvalue));
464690075Sobrien    }
464718334Speter
464818334Speter  if (code != ADDR_EXPR)
4649169689Skan    return NULL_TREE;
465018334Speter
465118334Speter  /* Handle (a = b) used as an "lvalue" for `&'.  */
465218334Speter  if (TREE_CODE (arg) == MODIFY_EXPR
465318334Speter      || TREE_CODE (arg) == INIT_EXPR)
465418334Speter    {
465518334Speter      tree real_result = build_unary_op (code, TREE_OPERAND (arg, 0), 0);
4656169689Skan      arg = build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
4657169689Skan		    arg, real_result);
4658169689Skan      TREE_NO_WARNING (arg) = 1;
465950397Sobrien      return arg;
466018334Speter    }
466118334Speter
466218334Speter  if (TREE_CODE (TREE_TYPE (arg)) == FUNCTION_TYPE
466318334Speter      || TREE_CODE (TREE_TYPE (arg)) == METHOD_TYPE
4664132718Skan      || TREE_CODE (arg) == OFFSET_REF)
4665169689Skan    return NULL_TREE;
466618334Speter
466718334Speter  /* We permit compiler to make function calls returning
466818334Speter     objects of aggregate type look like lvalues.  */
466918334Speter  {
467018334Speter    tree targ = arg;
467118334Speter
467218334Speter    if (TREE_CODE (targ) == SAVE_EXPR)
467318334Speter      targ = TREE_OPERAND (targ, 0);
467418334Speter
467518334Speter    if (TREE_CODE (targ) == CALL_EXPR && IS_AGGR_TYPE (TREE_TYPE (targ)))
467618334Speter      {
467718334Speter	if (TREE_CODE (arg) == SAVE_EXPR)
467818334Speter	  targ = arg;
467918334Speter	else
468050397Sobrien	  targ = build_cplus_new (TREE_TYPE (arg), arg);
468118334Speter	return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
468218334Speter      }
468318334Speter
468418334Speter    if (TREE_CODE (arg) == SAVE_EXPR && TREE_CODE (targ) == INDIRECT_REF)
4685169689Skan      return build3 (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
468618334Speter		     TREE_OPERAND (targ, 0), current_function_decl, NULL);
468718334Speter  }
468818334Speter
468918334Speter  /* Don't let anything else be handled specially.  */
4690169689Skan  return NULL_TREE;
469118334Speter}
469218334Speter
469318334Speter/* Mark EXP saying that we need to be able to take the
469418334Speter   address of it; it should not be allocated in a register.
4695117395Skan   Value is true if successful.
469618334Speter
469750397Sobrien   C++: we do not allow `current_class_ptr' to be addressable.  */
469818334Speter
4699117395Skanbool
4700132718Skancxx_mark_addressable (tree exp)
470118334Speter{
4702132718Skan  tree x = exp;
470318334Speter
470418334Speter  while (1)
470518334Speter    switch (TREE_CODE (x))
470618334Speter      {
470718334Speter      case ADDR_EXPR:
470818334Speter      case COMPONENT_REF:
470918334Speter      case ARRAY_REF:
471050397Sobrien      case REALPART_EXPR:
471150397Sobrien      case IMAGPART_EXPR:
471218334Speter	x = TREE_OPERAND (x, 0);
471318334Speter	break;
471418334Speter
471518334Speter      case PARM_DECL:
471650397Sobrien	if (x == current_class_ptr)
471718334Speter	  {
4718169689Skan	    error ("cannot take the address of %<this%>, which is an rvalue expression");
4719132718Skan	    TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later.  */
4720117395Skan	    return true;
472118334Speter	  }
4722132718Skan	/* Fall through.  */
4723102780Skan
472418334Speter      case VAR_DECL:
472518334Speter	/* Caller should not be trying to mark initialized
472618334Speter	   constant fields addressable.  */
4727169689Skan	gcc_assert (DECL_LANG_SPECIFIC (x) == 0
4728169689Skan		    || DECL_IN_AGGR_P (x) == 0
4729169689Skan		    || TREE_STATIC (x)
4730169689Skan		    || DECL_EXTERNAL (x));
4731132718Skan	/* Fall through.  */
473218334Speter
473318334Speter      case CONST_DECL:
473418334Speter      case RESULT_DECL:
473550397Sobrien	if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
4736169689Skan	    && !DECL_ARTIFICIAL (x))
4737169689Skan	  {
4738169689Skan	    if (TREE_CODE (x) == VAR_DECL && DECL_HARD_REGISTER (x))
4739169689Skan	      {
4740169689Skan		error
4741169689Skan		  ("address of explicit register variable %qD requested", x);
4742169689Skan		return false;
4743169689Skan	      }
4744169689Skan	    else if (extra_warnings)
4745169689Skan	      warning
4746169689Skan		(OPT_Wextra, "address requested for %qD, which is declared %<register%>", x);
4747169689Skan	  }
474818334Speter	TREE_ADDRESSABLE (x) = 1;
4749117395Skan	return true;
475018334Speter
475118334Speter      case FUNCTION_DECL:
475218334Speter	TREE_ADDRESSABLE (x) = 1;
4753117395Skan	return true;
475418334Speter
475550397Sobrien      case CONSTRUCTOR:
475650397Sobrien	TREE_ADDRESSABLE (x) = 1;
4757117395Skan	return true;
475850397Sobrien
475950397Sobrien      case TARGET_EXPR:
476050397Sobrien	TREE_ADDRESSABLE (x) = 1;
4761117395Skan	cxx_mark_addressable (TREE_OPERAND (x, 0));
4762117395Skan	return true;
476350397Sobrien
476418334Speter      default:
4765117395Skan	return true;
476618334Speter    }
476718334Speter}
476818334Speter
476918334Speter/* Build and return a conditional expression IFEXP ? OP1 : OP2.  */
477018334Speter
477118334Spetertree
4772132718Skanbuild_x_conditional_expr (tree ifexp, tree op1, tree op2)
477318334Speter{
4774132718Skan  tree orig_ifexp = ifexp;
4775132718Skan  tree orig_op1 = op1;
4776132718Skan  tree orig_op2 = op2;
4777132718Skan  tree expr;
4778132718Skan
477950397Sobrien  if (processing_template_decl)
4780132718Skan    {
4781132718Skan      /* The standard says that the expression is type-dependent if
4782132718Skan	 IFEXP is type-dependent, even though the eventual type of the
4783132718Skan	 expression doesn't dependent on IFEXP.  */
4784132718Skan      if (type_dependent_expression_p (ifexp)
4785132718Skan	  /* As a GNU extension, the middle operand may be omitted.  */
4786132718Skan	  || (op1 && type_dependent_expression_p (op1))
4787132718Skan	  || type_dependent_expression_p (op2))
4788132718Skan	return build_min_nt (COND_EXPR, ifexp, op1, op2);
4789132718Skan      ifexp = build_non_dependent_expr (ifexp);
4790132718Skan      if (op1)
4791132718Skan	op1 = build_non_dependent_expr (op1);
4792132718Skan      op2 = build_non_dependent_expr (op2);
4793132718Skan    }
479418334Speter
4795132718Skan  expr = build_conditional_expr (ifexp, op1, op2);
4796132718Skan  if (processing_template_decl && expr != error_mark_node)
4797169689Skan    return build_min_non_dep (COND_EXPR, expr,
4798132718Skan			      orig_ifexp, orig_op1, orig_op2);
4799132718Skan  return expr;
480018334Speter}
480118334Speter
4802132718Skan/* Given a list of expressions, return a compound expression
4803132718Skan   that performs them all and returns the value of the last of them.  */
480450397Sobrien
4805132718Skantree build_x_compound_expr_from_list (tree list, const char *msg)
4806132718Skan{
4807132718Skan  tree expr = TREE_VALUE (list);
4808169689Skan
4809132718Skan  if (TREE_CHAIN (list))
4810132718Skan    {
4811132718Skan      if (msg)
4812132718Skan	pedwarn ("%s expression list treated as compound expression", msg);
4813132718Skan
4814132718Skan      for (list = TREE_CHAIN (list); list; list = TREE_CHAIN (list))
4815132718Skan	expr = build_x_compound_expr (expr, TREE_VALUE (list));
4816132718Skan    }
4817169689Skan
4818132718Skan  return expr;
4819132718Skan}
4820132718Skan
4821132718Skan/* Handle overloading of the ',' operator when needed.  */
4822132718Skan
482318334Spetertree
4824132718Skanbuild_x_compound_expr (tree op1, tree op2)
482518334Speter{
482618334Speter  tree result;
4827132718Skan  tree orig_op1 = op1;
4828132718Skan  tree orig_op2 = op2;
482918334Speter
483050397Sobrien  if (processing_template_decl)
483118334Speter    {
4832132718Skan      if (type_dependent_expression_p (op1)
4833132718Skan	  || type_dependent_expression_p (op2))
4834132718Skan	return build_min_nt (COMPOUND_EXPR, op1, op2);
4835132718Skan      op1 = build_non_dependent_expr (op1);
4836132718Skan      op2 = build_non_dependent_expr (op2);
483718334Speter    }
483818334Speter
4839132718Skan  result = build_new_op (COMPOUND_EXPR, LOOKUP_NORMAL, op1, op2, NULL_TREE,
4840132718Skan			 /*overloaded_p=*/NULL);
4841132718Skan  if (!result)
4842132718Skan    result = build_compound_expr (op1, op2);
4843132718Skan
4844132718Skan  if (processing_template_decl && result != error_mark_node)
4845132718Skan    return build_min_non_dep (COMPOUND_EXPR, result, orig_op1, orig_op2);
4846169689Skan
4847132718Skan  return result;
484818334Speter}
484918334Speter
4850132718Skan/* Build a compound expression.  */
485118334Speter
485218334Spetertree
4853132718Skanbuild_compound_expr (tree lhs, tree rhs)
485418334Speter{
4855132718Skan  lhs = convert_to_void (lhs, "left-hand operand of comma");
4856169689Skan
4857132718Skan  if (lhs == error_mark_node || rhs == error_mark_node)
4858132718Skan    return error_mark_node;
4859169689Skan
4860132718Skan  if (TREE_CODE (rhs) == TARGET_EXPR)
486118334Speter    {
4862132718Skan      /* If the rhs is a TARGET_EXPR, then build the compound
4863169689Skan	 expression inside the target_expr's initializer. This
4864132718Skan	 helps the compiler to eliminate unnecessary temporaries.  */
4865132718Skan      tree init = TREE_OPERAND (rhs, 1);
4866169689Skan
4867169689Skan      init = build2 (COMPOUND_EXPR, TREE_TYPE (init), lhs, init);
4868132718Skan      TREE_OPERAND (rhs, 1) = init;
4869169689Skan
4870132718Skan      return rhs;
487118334Speter    }
4872169689Skan
4873169689Skan  return build2 (COMPOUND_EXPR, TREE_TYPE (rhs), lhs, rhs);
487418334Speter}
487518334Speter
4876169689Skan/* Issue a diagnostic message if casting from SRC_TYPE to DEST_TYPE
4877169689Skan   casts away constness.  DIAG_FN gives the function to call if we
4878169689Skan   need to issue a diagnostic; if it is NULL, no diagnostic will be
4879169689Skan   issued.  DESCRIPTION explains what operation is taking place.  */
4880117395Skan
4881117395Skanstatic void
4882132718Skancheck_for_casting_away_constness (tree src_type, tree dest_type,
4883169689Skan				  void (*diag_fn)(const char *, ...) ATTRIBUTE_GCC_CXXDIAG(1,2),
4884132718Skan				  const char *description)
4885117395Skan{
4886169689Skan  if (diag_fn && casts_away_constness (src_type, dest_type))
4887169689Skan    diag_fn ("%s from type %qT to type %qT casts away constness",
4888169689Skan	     description, src_type, dest_type);
4889117395Skan}
4890117395Skan
4891169689Skan/* Convert EXPR (an expression with pointer-to-member type) to TYPE
4892169689Skan   (another pointer-to-member type in the same hierarchy) and return
4893169689Skan   the converted expression.  If ALLOW_INVERSE_P is permitted, a
4894169689Skan   pointer-to-derived may be converted to pointer-to-base; otherwise,
4895169689Skan   only the other direction is permitted.  If C_CAST_P is true, this
4896169689Skan   conversion is taking place as part of a C-style cast.  */
4897117395Skan
489850397Sobrientree
4899169689Skanconvert_ptrmem (tree type, tree expr, bool allow_inverse_p,
4900169689Skan		bool c_cast_p)
490118334Speter{
4902169689Skan  if (TYPE_PTRMEM_P (type))
4903169689Skan    {
4904169689Skan      tree delta;
490518334Speter
4906169689Skan      if (TREE_CODE (expr) == PTRMEM_CST)
4907169689Skan	expr = cplus_expand_constant (expr);
4908169689Skan      delta = get_delta_difference (TYPE_PTRMEM_CLASS_TYPE (TREE_TYPE (expr)),
4909169689Skan				    TYPE_PTRMEM_CLASS_TYPE (type),
4910169689Skan				    allow_inverse_p,
4911169689Skan				    c_cast_p);
4912169689Skan      if (!integer_zerop (delta))
4913169689Skan	expr = cp_build_binary_op (PLUS_EXPR,
4914169689Skan				   build_nop (ptrdiff_type_node, expr),
4915169689Skan				   delta);
4916169689Skan      return build_nop (type, expr);
4917169689Skan    }
4918169689Skan  else
4919169689Skan    return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr,
4920169689Skan			     allow_inverse_p, c_cast_p);
4921169689Skan}
492218334Speter
4923169689Skan/* If EXPR is an INTEGER_CST and ORIG is an arithmetic constant, return
4924169689Skan   a version of EXPR that has TREE_OVERFLOW and/or TREE_CONSTANT_OVERFLOW
4925169689Skan   set iff they are set in ORIG.  Otherwise, return EXPR unchanged.  */
4926169689Skan
4927169689Skanstatic tree
4928169689Skanignore_overflows (tree expr, tree orig)
4929169689Skan{
4930169689Skan  if (TREE_CODE (expr) == INTEGER_CST
4931169689Skan      && CONSTANT_CLASS_P (orig)
4932169689Skan      && TREE_CODE (orig) != STRING_CST
4933169689Skan      && (TREE_OVERFLOW (expr) != TREE_OVERFLOW (orig)
4934169689Skan	  || TREE_CONSTANT_OVERFLOW (expr)
4935169689Skan	     != TREE_CONSTANT_OVERFLOW (orig)))
493618334Speter    {
4937169689Skan      if (!TREE_OVERFLOW (orig) && !TREE_CONSTANT_OVERFLOW (orig))
4938169689Skan	/* Ensure constant sharing.  */
4939169689Skan	expr = build_int_cst_wide (TREE_TYPE (expr),
4940169689Skan				   TREE_INT_CST_LOW (expr),
4941169689Skan				   TREE_INT_CST_HIGH (expr));
4942169689Skan      else
4943169689Skan	{
4944169689Skan	  /* Avoid clobbering a shared constant.  */
4945169689Skan	  expr = copy_node (expr);
4946169689Skan	  TREE_OVERFLOW (expr) = TREE_OVERFLOW (orig);
4947169689Skan	  TREE_CONSTANT_OVERFLOW (expr)
4948169689Skan	    = TREE_CONSTANT_OVERFLOW (orig);
4949169689Skan	}
495018334Speter    }
4951169689Skan  return expr;
4952169689Skan}
495350397Sobrien
4954169689Skan/* Perform a static_cast from EXPR to TYPE.  When C_CAST_P is true,
4955169689Skan   this static_cast is being attempted as one of the possible casts
4956169689Skan   allowed by a C-style cast.  (In that case, accessibility of base
4957169689Skan   classes is not considered, and it is OK to cast away
4958169689Skan   constness.)  Return the result of the cast.  *VALID_P is set to
4959169689Skan   indicate whether or not the cast was valid.  */
496050397Sobrien
4961169689Skanstatic tree
4962169689Skanbuild_static_cast_1 (tree type, tree expr, bool c_cast_p,
4963169689Skan		     bool *valid_p)
4964169689Skan{
4965169689Skan  tree intype;
4966169689Skan  tree result;
4967169689Skan  tree orig;
4968169689Skan  void (*diag_fn)(const char*, ...) ATTRIBUTE_GCC_CXXDIAG(1,2);
4969169689Skan  const char *desc;
4970169689Skan
4971169689Skan  /* Assume the cast is valid.  */
4972169689Skan  *valid_p = true;
4973169689Skan
4974117395Skan  intype = TREE_TYPE (expr);
4975117395Skan
4976169689Skan  /* Save casted types in the function's used types hash table.  */
4977169689Skan  used_types_insert (type);
4978169689Skan
4979169689Skan  /* Determine what to do when casting away constness.  */
4980169689Skan  if (c_cast_p)
4981169689Skan    {
4982169689Skan      /* C-style casts are allowed to cast away constness.  With
4983169689Skan	 WARN_CAST_QUAL, we still want to issue a warning.  */
4984169689Skan      diag_fn = warn_cast_qual ? warning0 : NULL;
4985169689Skan      desc = "cast";
4986169689Skan    }
4987169689Skan  else
4988169689Skan    {
4989169689Skan      /* A static_cast may not cast away constness.  */
4990169689Skan      diag_fn = error;
4991169689Skan      desc = "static_cast";
4992169689Skan    }
4993169689Skan
4994117395Skan  /* [expr.static.cast]
4995117395Skan
4996117395Skan     An lvalue of type "cv1 B", where B is a class type, can be cast
4997117395Skan     to type "reference to cv2 D", where D is a class derived (clause
4998117395Skan     _class.derived_) from B, if a valid standard conversion from
4999117395Skan     "pointer to D" to "pointer to B" exists (_conv.ptr_), cv2 is the
5000117395Skan     same cv-qualification as, or greater cv-qualification than, cv1,
5001117395Skan     and B is not a virtual base class of D.  */
5002117395Skan  /* We check this case before checking the validity of "TYPE t =
5003117395Skan     EXPR;" below because for this case:
5004117395Skan
5005117395Skan       struct B {};
5006117395Skan       struct D : public B { D(const B&); };
5007117395Skan       extern B& b;
5008117395Skan       void f() { static_cast<const D&>(b); }
5009117395Skan
5010117395Skan     we want to avoid constructing a new D.  The standard is not
5011117395Skan     completely clear about this issue, but our interpretation is
5012117395Skan     consistent with other compilers.  */
5013117395Skan  if (TREE_CODE (type) == REFERENCE_TYPE
5014117395Skan      && CLASS_TYPE_P (TREE_TYPE (type))
5015117395Skan      && CLASS_TYPE_P (intype)
5016132718Skan      && real_lvalue_p (expr)
5017117395Skan      && DERIVED_FROM_P (intype, TREE_TYPE (type))
5018117395Skan      && can_convert (build_pointer_type (TYPE_MAIN_VARIANT (intype)),
5019169689Skan		      build_pointer_type (TYPE_MAIN_VARIANT
5020117395Skan					  (TREE_TYPE (type))))
5021169689Skan      && (c_cast_p
5022169689Skan	  || at_least_as_qualified_p (TREE_TYPE (type), intype)))
502390075Sobrien    {
5024169689Skan      tree base;
5025169689Skan
5026122180Skan      /* There is a standard conversion from "D*" to "B*" even if "B"
5027169689Skan	 is ambiguous or inaccessible.  If this is really a
5028169689Skan	 static_cast, then we check both for inaccessibility and
5029169689Skan	 ambiguity.  However, if this is a static_cast being performed
5030169689Skan	 because the user wrote a C-style cast, then accessibility is
5031169689Skan	 not considered.  */
5032169689Skan      base = lookup_base (TREE_TYPE (type), intype,
5033169689Skan			  c_cast_p ? ba_unique : ba_check,
5034169689Skan			  NULL);
5035117395Skan
5036122180Skan      /* Convert from "B*" to "D*".  This function will check that "B"
5037122180Skan	 is not a virtual base of "D".  */
5038169689Skan      expr = build_base_path (MINUS_EXPR, build_address (expr),
5039117395Skan			      base, /*nonnull=*/false);
5040117395Skan      /* Convert the pointer to a reference -- but then remember that
5041117395Skan	 there are no expressions with reference type in C++.  */
5042117395Skan      return convert_from_reference (build_nop (type, expr));
504390075Sobrien    }
504450397Sobrien
5045169689Skan  orig = expr;
5046169689Skan
5047117395Skan  /* [expr.static.cast]
504850397Sobrien
5049117395Skan     An expression e can be explicitly converted to a type T using a
5050117395Skan     static_cast of the form static_cast<T>(e) if the declaration T
5051117395Skan     t(e);" is well-formed, for some invented temporary variable
5052117395Skan     t.  */
5053169689Skan  result = perform_direct_initialization_if_possible (type, expr,
5054169689Skan						      c_cast_p);
5055117395Skan  if (result)
5056132718Skan    {
5057132718Skan      result = convert_from_reference (result);
5058169689Skan
5059169689Skan      /* Ignore any integer overflow caused by the cast.  */
5060169689Skan      result = ignore_overflows (result, orig);
5061169689Skan
5062132718Skan      /* [expr.static.cast]
5063132718Skan
5064169689Skan	 If T is a reference type, the result is an lvalue; otherwise,
5065132718Skan	 the result is an rvalue.  */
5066169689Skan      if (TREE_CODE (type) != REFERENCE_TYPE)
5067169689Skan	result = rvalue (result);
5068132718Skan      return result;
5069132718Skan    }
5070169689Skan
5071117395Skan  /* [expr.static.cast]
507250397Sobrien
5073117395Skan     Any expression can be explicitly converted to type cv void.  */
5074117395Skan  if (TREE_CODE (type) == VOID_TYPE)
5075117395Skan    return convert_to_void (expr, /*implicit=*/NULL);
507650397Sobrien
5077117395Skan  /* [expr.static.cast]
5078117395Skan
5079117395Skan     The inverse of any standard conversion sequence (clause _conv_),
5080117395Skan     other than the lvalue-to-rvalue (_conv.lval_), array-to-pointer
5081117395Skan     (_conv.array_), function-to-pointer (_conv.func_), and boolean
5082117395Skan     (_conv.bool_) conversions, can be performed explicitly using
5083117395Skan     static_cast subject to the restriction that the explicit
5084117395Skan     conversion does not cast away constness (_expr.const.cast_), and
5085117395Skan     the following additional rules for specific cases:  */
5086117395Skan  /* For reference, the conversions not excluded are: integral
5087117395Skan     promotions, floating point promotion, integral conversions,
5088117395Skan     floating point conversions, floating-integral conversions,
5089117395Skan     pointer conversions, and pointer to member conversions.  */
5090169689Skan  /* DR 128
5091117395Skan
5092169689Skan     A value of integral _or enumeration_ type can be explicitly
5093169689Skan     converted to an enumeration type.  */
5094169689Skan  /* The effect of all that is that any conversion between any two
5095169689Skan     types which are integral, floating, or enumeration types can be
5096169689Skan     performed.  */
5097169689Skan  if ((INTEGRAL_TYPE_P (type) || SCALAR_FLOAT_TYPE_P (type))
5098169689Skan      && (INTEGRAL_TYPE_P (intype) || SCALAR_FLOAT_TYPE_P (intype)))
5099169689Skan    {
5100169689Skan      expr = ocp_convert (type, expr, CONV_C_CAST, LOOKUP_NORMAL);
5101169689Skan
5102169689Skan      /* Ignore any integer overflow caused by the cast.  */
5103169689Skan      expr = ignore_overflows (expr, orig);
5104169689Skan      return expr;
5105169689Skan    }
5106169689Skan
5107117395Skan  if (TYPE_PTR_P (type) && TYPE_PTR_P (intype)
5108117395Skan      && CLASS_TYPE_P (TREE_TYPE (type))
5109117395Skan      && CLASS_TYPE_P (TREE_TYPE (intype))
5110169689Skan      && can_convert (build_pointer_type (TYPE_MAIN_VARIANT
5111169689Skan					  (TREE_TYPE (intype))),
5112169689Skan		      build_pointer_type (TYPE_MAIN_VARIANT
5113117395Skan					  (TREE_TYPE (type)))))
511418334Speter    {
5115117395Skan      tree base;
5116117395Skan
5117169689Skan      if (!c_cast_p)
5118169689Skan	check_for_casting_away_constness (intype, type, diag_fn, desc);
5119169689Skan      base = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
5120169689Skan			  c_cast_p ? ba_unique : ba_check,
5121132718Skan			  NULL);
5122117395Skan      return build_base_path (MINUS_EXPR, expr, base, /*nonnull=*/false);
512318334Speter    }
5124169689Skan
5125117395Skan  if ((TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype))
5126117395Skan      || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
512796263Sobrien    {
5128117395Skan      tree c1;
5129117395Skan      tree c2;
5130117395Skan      tree t1;
5131117395Skan      tree t2;
5132117395Skan
5133117395Skan      c1 = TYPE_PTRMEM_CLASS_TYPE (intype);
5134117395Skan      c2 = TYPE_PTRMEM_CLASS_TYPE (type);
5135117395Skan
5136117395Skan      if (TYPE_PTRMEM_P (type))
5137117395Skan	{
5138169689Skan	  t1 = (build_ptrmem_type
5139117395Skan		(c1,
5140117395Skan		 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (intype))));
5141169689Skan	  t2 = (build_ptrmem_type
5142117395Skan		(c2,
5143117395Skan		 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
5144117395Skan	}
5145117395Skan      else
5146117395Skan	{
5147117395Skan	  t1 = intype;
5148117395Skan	  t2 = type;
5149117395Skan	}
5150117395Skan      if (can_convert (t1, t2))
5151117395Skan	{
5152169689Skan	  if (!c_cast_p)
5153169689Skan	    check_for_casting_away_constness (intype, type, diag_fn,
5154169689Skan					      desc);
5155169689Skan	  return convert_ptrmem (type, expr, /*allow_inverse_p=*/1,
5156169689Skan				 c_cast_p);
5157117395Skan	}
515896263Sobrien    }
5159169689Skan
516090075Sobrien  /* [expr.static.cast]
516190075Sobrien
5162117395Skan     An rvalue of type "pointer to cv void" can be explicitly
5163117395Skan     converted to a pointer to object type.  A value of type pointer
5164117395Skan     to object converted to "pointer to cv void" and back to the
5165117395Skan     original pointer type will have its original value.  */
5166169689Skan  if (TREE_CODE (intype) == POINTER_TYPE
5167117395Skan      && VOID_TYPE_P (TREE_TYPE (intype))
5168117395Skan      && TYPE_PTROB_P (type))
516990075Sobrien    {
5170169689Skan      if (!c_cast_p)
5171169689Skan	check_for_casting_away_constness (intype, type, diag_fn, desc);
5172117395Skan      return build_nop (type, expr);
517390075Sobrien    }
517490075Sobrien
5175169689Skan  *valid_p = false;
517650397Sobrien  return error_mark_node;
517718334Speter}
517818334Speter
5179169689Skan/* Return an expression representing static_cast<TYPE>(EXPR).  */
5180169689Skan
518150397Sobrientree
5182169689Skanbuild_static_cast (tree type, tree expr)
518318334Speter{
5184169689Skan  tree result;
5185169689Skan  bool valid_p;
518618334Speter
518718334Speter  if (type == error_mark_node || expr == error_mark_node)
518818334Speter    return error_mark_node;
518918334Speter
519050397Sobrien  if (processing_template_decl)
519118334Speter    {
5192169689Skan      expr = build_min (STATIC_CAST_EXPR, type, expr);
5193169689Skan      /* We don't know if it will or will not have side effects.  */
5194169689Skan      TREE_SIDE_EFFECTS (expr) = 1;
5195169689Skan      return convert_from_reference (expr);
519618334Speter    }
519750397Sobrien
5198169689Skan  /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5199169689Skan     Strip such NOP_EXPRs if VALUE is being used in non-lvalue context.  */
5200169689Skan  if (TREE_CODE (type) != REFERENCE_TYPE
5201169689Skan      && TREE_CODE (expr) == NOP_EXPR
5202169689Skan      && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
5203169689Skan    expr = TREE_OPERAND (expr, 0);
5204169689Skan
5205169689Skan  result = build_static_cast_1 (type, expr, /*c_cast_p=*/false, &valid_p);
5206169689Skan  if (valid_p)
5207169689Skan    return result;
5208169689Skan
5209169689Skan  error ("invalid static_cast from type %qT to type %qT",
5210169689Skan	 TREE_TYPE (expr), type);
5211169689Skan  return error_mark_node;
5212169689Skan}
5213169689Skan
5214169689Skan/* EXPR is an expression with member function or pointer-to-member
5215169689Skan   function type.  TYPE is a pointer type.  Converting EXPR to TYPE is
5216169689Skan   not permitted by ISO C++, but we accept it in some modes.  If we
5217169689Skan   are not in one of those modes, issue a diagnostic.  Return the
5218169689Skan   converted expression.  */
5219169689Skan
5220169689Skantree
5221169689Skanconvert_member_func_to_ptr (tree type, tree expr)
5222169689Skan{
5223169689Skan  tree intype;
5224169689Skan  tree decl;
5225169689Skan
5226169689Skan  intype = TREE_TYPE (expr);
5227169689Skan  gcc_assert (TYPE_PTRMEMFUNC_P (intype)
5228169689Skan	      || TREE_CODE (intype) == METHOD_TYPE);
5229169689Skan
5230169689Skan  if (pedantic || warn_pmf2ptr)
5231169689Skan    pedwarn ("converting from %qT to %qT", intype, type);
5232169689Skan
5233169689Skan  if (TREE_CODE (intype) == METHOD_TYPE)
5234169689Skan    expr = build_addr_func (expr);
5235169689Skan  else if (TREE_CODE (expr) == PTRMEM_CST)
5236169689Skan    expr = build_address (PTRMEM_CST_MEMBER (expr));
5237169689Skan  else
523818334Speter    {
5239169689Skan      decl = maybe_dummy_object (TYPE_PTRMEM_CLASS_TYPE (intype), 0);
5240169689Skan      decl = build_address (decl);
5241169689Skan      expr = get_member_function_from_ptrfunc (&decl, expr);
524218334Speter    }
524350397Sobrien
5244169689Skan  return build_nop (type, expr);
5245169689Skan}
5246169689Skan
5247169689Skan/* Return a representation for a reinterpret_cast from EXPR to TYPE.
5248169689Skan   If C_CAST_P is true, this reinterpret cast is being done as part of
5249169689Skan   a C-style cast.  If VALID_P is non-NULL, *VALID_P is set to
5250169689Skan   indicate whether or not reinterpret_cast was valid.  */
5251169689Skan
5252169689Skanstatic tree
5253169689Skanbuild_reinterpret_cast_1 (tree type, tree expr, bool c_cast_p,
5254169689Skan			  bool *valid_p)
5255169689Skan{
5256169689Skan  tree intype;
5257169689Skan
5258169689Skan  /* Assume the cast is invalid.  */
5259169689Skan  if (valid_p)
5260169689Skan    *valid_p = true;
5261169689Skan
5262169689Skan  if (type == error_mark_node || error_operand_p (expr))
5263169689Skan    return error_mark_node;
5264169689Skan
526550397Sobrien  intype = TREE_TYPE (expr);
526650397Sobrien
5267169689Skan  /* Save casted types in the function's used types hash table.  */
5268169689Skan  used_types_insert (type);
5269169689Skan
5270169689Skan  /* [expr.reinterpret.cast]
5271169689Skan     An lvalue expression of type T1 can be cast to the type
5272169689Skan     "reference to T2" if an expression of type "pointer to T1" can be
5273169689Skan     explicitly converted to the type "pointer to T2" using a
5274169689Skan     reinterpret_cast.  */
527518334Speter  if (TREE_CODE (type) == REFERENCE_TYPE)
527618334Speter    {
527750397Sobrien      if (! real_lvalue_p (expr))
527850397Sobrien	{
5279169689Skan	  error ("invalid cast of an rvalue expression of type "
5280169689Skan		 "%qT to type %qT",
5281169689Skan		 intype, type);
528250397Sobrien	  return error_mark_node;
528350397Sobrien	}
5284169689Skan
5285169689Skan      /* Warn about a reinterpret_cast from "A*" to "B&" if "A" and
5286169689Skan	 "B" are related class types; the reinterpret_cast does not
5287169689Skan	 adjust the pointer.  */
5288169689Skan      if (TYPE_PTR_P (intype)
5289169689Skan	  && (comptypes (TREE_TYPE (intype), TREE_TYPE (type),
5290169689Skan			 COMPARE_BASE | COMPARE_DERIVED)))
5291169689Skan	warning (0, "casting %qT to %qT does not dereference pointer",
5292169689Skan		 intype, type);
5293169689Skan
529450397Sobrien      expr = build_unary_op (ADDR_EXPR, expr, 0);
529550397Sobrien      if (expr != error_mark_node)
5296169689Skan	expr = build_reinterpret_cast_1
5297169689Skan	  (build_pointer_type (TREE_TYPE (type)), expr, c_cast_p,
5298169689Skan	   valid_p);
529950397Sobrien      if (expr != error_mark_node)
530050397Sobrien	expr = build_indirect_ref (expr, 0);
530150397Sobrien      return expr;
530218334Speter    }
530350397Sobrien
5304169689Skan  /* As a G++ extension, we consider conversions from member
5305169689Skan     functions, and pointers to member functions to
5306169689Skan     pointer-to-function and pointer-to-void types.  If
5307169689Skan     -Wno-pmf-conversions has not been specified,
5308169689Skan     convert_member_func_to_ptr will issue an error message.  */
5309169689Skan  if ((TYPE_PTRMEMFUNC_P (intype)
5310169689Skan       || TREE_CODE (intype) == METHOD_TYPE)
5311169689Skan      && TYPE_PTR_P (type)
5312169689Skan      && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5313169689Skan	  || VOID_TYPE_P (TREE_TYPE (type))))
5314169689Skan    return convert_member_func_to_ptr (type, expr);
5315169689Skan
5316169689Skan  /* If the cast is not to a reference type, the lvalue-to-rvalue,
5317169689Skan     array-to-pointer, and function-to-pointer conversions are
5318169689Skan     performed.  */
5319169689Skan  expr = decay_conversion (expr);
5320169689Skan
5321169689Skan  /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5322169689Skan     Strip such NOP_EXPRs if VALUE is being used in non-lvalue context.  */
5323169689Skan  if (TREE_CODE (expr) == NOP_EXPR
5324169689Skan      && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
5325169689Skan    expr = TREE_OPERAND (expr, 0);
5326169689Skan
5327169689Skan  if (error_operand_p (expr))
5328169689Skan    return error_mark_node;
5329169689Skan
5330169689Skan  intype = TREE_TYPE (expr);
5331169689Skan
5332169689Skan  /* [expr.reinterpret.cast]
5333169689Skan     A pointer can be converted to any integral type large enough to
5334169689Skan     hold it.  */
5335169689Skan  if (CP_INTEGRAL_TYPE_P (type) && TYPE_PTR_P (intype))
533650397Sobrien    {
533750397Sobrien      if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
5338169689Skan	pedwarn ("cast from %qT to %qT loses precision",
5339169689Skan		 intype, type);
534050397Sobrien    }
5341169689Skan  /* [expr.reinterpret.cast]
5342169689Skan     A value of integral or enumeration type can be explicitly
5343169689Skan     converted to a pointer.  */
5344169689Skan  else if (TYPE_PTR_P (type) && INTEGRAL_OR_ENUMERATION_TYPE_P (intype))
5345169689Skan    /* OK */
5346169689Skan    ;
5347260311Spfg  /* APPLE LOCAL begin blocks 6040305 (ck) */
5348260311Spfg  else if (TREE_CODE (type) == INTEGER_TYPE && TREE_CODE (intype) == BLOCK_POINTER_TYPE)
5349260311Spfg    {
5350260311Spfg      if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
5351260311Spfg	pedwarn ("cast from %qT to %qT loses precision",
5352260311Spfg		 intype, type);
5353260311Spfg    }
5354260311Spfg  else if (TREE_CODE (type) == BLOCK_POINTER_TYPE && TREE_CODE (intype) == INTEGER_TYPE)
5355260311Spfg    /* OK */
5356260311Spfg    ;
5357260311Spfg  else if (TREE_CODE (type) == BLOCK_POINTER_TYPE && TREE_CODE (intype) == BLOCK_POINTER_TYPE)
5358260311Spfg    /* OK */
5359260311Spfg    ;
5360260311Spfg  else if (TREE_CODE (intype) == BLOCK_POINTER_TYPE
5361260311Spfg	   && (objc_is_id (type)
5362260311Spfg	       || (TREE_CODE (type) == POINTER_TYPE && VOID_TYPE_P (TREE_TYPE (type)))))
5363260311Spfg    /* OK */
5364260311Spfg    ;
5365260311Spfg  else if (TREE_CODE (type) == BLOCK_POINTER_TYPE
5366260311Spfg	   && TREE_CODE (intype) == POINTER_TYPE
5367260311Spfg	   && (objc_is_id (intype) || VOID_TYPE_P (TREE_TYPE (intype))))
5368260311Spfg    /* OK */
5369260311Spfg    ;
5370260311Spfg  /* APPLE LOCAL end blocks 6040305 (ck) */
537150397Sobrien  else if ((TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
537250397Sobrien	   || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
5373169689Skan    return fold_if_not_in_template (build_nop (type, expr));
537450397Sobrien  else if ((TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype))
537550397Sobrien	   || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
537650397Sobrien    {
5377169689Skan      tree sexpr = expr;
5378169689Skan
5379169689Skan      if (!c_cast_p)
5380169689Skan	check_for_casting_away_constness (intype, type, error,
5381169689Skan					  "reinterpret_cast");
5382169689Skan      /* Warn about possible alignment problems.  */
5383169689Skan      if (STRICT_ALIGNMENT && warn_cast_align
5384169689Skan	  && !VOID_TYPE_P (type)
5385169689Skan	  && TREE_CODE (TREE_TYPE (intype)) != FUNCTION_TYPE
5386169689Skan	  && COMPLETE_TYPE_P (TREE_TYPE (type))
5387169689Skan	  && COMPLETE_TYPE_P (TREE_TYPE (intype))
5388169689Skan	  && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (intype)))
5389169689Skan	warning (0, "cast from %qT to %qT increases required alignment of "
5390169689Skan		 "target type",
5391169689Skan		 intype, type);
5392169689Skan
5393169689Skan      /* We need to strip nops here, because the frontend likes to
5394169689Skan	 create (int *)&a for array-to-pointer decay, instead of &a[0].  */
5395169689Skan      STRIP_NOPS (sexpr);
5396258501Spfg      if (warn_strict_aliasing <= 2)
5397258501Spfg	strict_aliasing_warning (intype, type, sexpr);
5398169689Skan
5399169689Skan      return fold_if_not_in_template (build_nop (type, expr));
540050397Sobrien    }
540150397Sobrien  else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
540252284Sobrien	   || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
540350397Sobrien    {
5404169689Skan      if (pedantic)
5405169689Skan	/* Only issue a warning, as we have always supported this
5406169689Skan	   where possible, and it is necessary in some cases.  DR 195
5407169689Skan	   addresses this issue, but as of 2004/10/26 is still in
5408169689Skan	   drafting.  */
5409169689Skan	warning (0, "ISO C++ forbids casting between pointer-to-function and pointer-to-object");
5410169689Skan      return fold_if_not_in_template (build_nop (type, expr));
541150397Sobrien    }
5412169689Skan  else if (TREE_CODE (type) == VECTOR_TYPE)
5413169689Skan    return fold_if_not_in_template (convert_to_vector (type, expr));
5414169689Skan  else if (TREE_CODE (intype) == VECTOR_TYPE && INTEGRAL_TYPE_P (type))
5415169689Skan    return fold_if_not_in_template (convert_to_integer (type, expr));
541618334Speter  else
541718334Speter    {
5418169689Skan      if (valid_p)
5419169689Skan	*valid_p = false;
5420169689Skan      error ("invalid cast from type %qT to type %qT", intype, type);
542150397Sobrien      return error_mark_node;
542250397Sobrien    }
5423169689Skan
5424260311Spfg  /* APPLE LOCAL begin don't sign-extend pointers cast to integers */
5425260311Spfg  if (TREE_CODE (type) == INTEGER_TYPE
5426260311Spfg      && TREE_CODE (intype) == POINTER_TYPE
5427260311Spfg      && TYPE_PRECISION (type) > TYPE_PRECISION (intype)
5428260311Spfg      && TYPE_UNSIGNED (type))
5429260311Spfg    expr = cp_convert (c_common_type_for_size (POINTER_SIZE, 1), expr);
5430260311Spfg  /* APPLE LOCAL end don't sign-extend pointers cast to integers */
5431260311Spfg
543250397Sobrien  return cp_convert (type, expr);
543350397Sobrien}
543418334Speter
543550397Sobrientree
5436169689Skanbuild_reinterpret_cast (tree type, tree expr)
543750397Sobrien{
5438169689Skan  if (type == error_mark_node || expr == error_mark_node)
543950397Sobrien    return error_mark_node;
544050397Sobrien
544150397Sobrien  if (processing_template_decl)
544250397Sobrien    {
5443169689Skan      tree t = build_min (REINTERPRET_CAST_EXPR, type, expr);
5444169689Skan
5445132718Skan      if (!TREE_SIDE_EFFECTS (t)
5446132718Skan	  && type_dependent_expression_p (expr))
5447132718Skan	/* There might turn out to be side effects inside expr.  */
5448132718Skan	TREE_SIDE_EFFECTS (t) = 1;
5449169689Skan      return convert_from_reference (t);
545018334Speter    }
545118334Speter
5452169689Skan  return build_reinterpret_cast_1 (type, expr, /*c_cast_p=*/false,
5453169689Skan				   /*valid_p=*/NULL);
5454169689Skan}
5455169689Skan
5456169689Skan/* Perform a const_cast from EXPR to TYPE.  If the cast is valid,
5457169689Skan   return an appropriate expression.  Otherwise, return
5458169689Skan   error_mark_node.  If the cast is not valid, and COMPLAIN is true,
5459169689Skan   then a diagnostic will be issued.  If VALID_P is non-NULL, we are
5460169689Skan   performing a C-style cast, its value upon return will indicate
5461169689Skan   whether or not the conversion succeeded.  */
5462169689Skan
5463169689Skanstatic tree
5464169689Skanbuild_const_cast_1 (tree dst_type, tree expr, bool complain,
5465169689Skan		    bool *valid_p)
5466169689Skan{
5467169689Skan  tree src_type;
5468169689Skan  tree reference_type;
5469169689Skan
5470169689Skan  /* Callers are responsible for handling error_mark_node as a
5471169689Skan     destination type.  */
5472169689Skan  gcc_assert (dst_type != error_mark_node);
5473169689Skan  /* In a template, callers should be building syntactic
5474169689Skan     representations of casts, not using this machinery.  */
5475169689Skan  gcc_assert (!processing_template_decl);
5476169689Skan
5477169689Skan  /* Assume the conversion is invalid.  */
5478169689Skan  if (valid_p)
5479169689Skan    *valid_p = false;
5480169689Skan
5481169689Skan  if (!POINTER_TYPE_P (dst_type) && !TYPE_PTRMEM_P (dst_type))
548252284Sobrien    {
5483169689Skan      if (complain)
5484169689Skan	error ("invalid use of const_cast with type %qT, "
5485169689Skan	       "which is not a pointer, "
5486169689Skan	       "reference, nor a pointer-to-data-member type", dst_type);
548752284Sobrien      return error_mark_node;
548852284Sobrien    }
548952284Sobrien
5490169689Skan  if (TREE_CODE (TREE_TYPE (dst_type)) == FUNCTION_TYPE)
549118334Speter    {
5492169689Skan      if (complain)
5493169689Skan	error ("invalid use of const_cast with type %qT, which is a pointer "
5494169689Skan	       "or reference to a function type", dst_type);
5495169689Skan      return error_mark_node;
549650397Sobrien    }
549750397Sobrien
5498169689Skan  /* Save casted types in the function's used types hash table.  */
5499169689Skan  used_types_insert (dst_type);
5500169689Skan
5501169689Skan  src_type = TREE_TYPE (expr);
5502169689Skan  /* Expressions do not really have reference types.  */
5503169689Skan  if (TREE_CODE (src_type) == REFERENCE_TYPE)
5504169689Skan    src_type = TREE_TYPE (src_type);
5505169689Skan
5506169689Skan  /* [expr.const.cast]
5507169689Skan
5508169689Skan     An lvalue of type T1 can be explicitly converted to an lvalue of
5509169689Skan     type T2 using the cast const_cast<T2&> (where T1 and T2 are object
5510169689Skan     types) if a pointer to T1 can be explicitly converted to the type
5511169689Skan     pointer to T2 using a const_cast.  */
5512169689Skan  if (TREE_CODE (dst_type) == REFERENCE_TYPE)
551350397Sobrien    {
5514169689Skan      reference_type = dst_type;
551550397Sobrien      if (! real_lvalue_p (expr))
551618334Speter	{
5517169689Skan	  if (complain)
5518169689Skan	    error ("invalid const_cast of an rvalue of type %qT to type %qT",
5519169689Skan		   src_type, dst_type);
552018334Speter	  return error_mark_node;
552118334Speter	}
5522169689Skan      dst_type = build_pointer_type (TREE_TYPE (dst_type));
5523169689Skan      src_type = build_pointer_type (src_type);
5524169689Skan    }
5525169689Skan  else
5526169689Skan    {
5527169689Skan      reference_type = NULL_TREE;
5528169689Skan      /* If the destination type is not a reference type, the
5529169689Skan	 lvalue-to-rvalue, array-to-pointer, and function-to-pointer
5530169689Skan	 conversions are performed.  */
5531169689Skan      src_type = type_decays_to (src_type);
5532169689Skan      if (src_type == error_mark_node)
5533169689Skan	return error_mark_node;
5534169689Skan    }
553518334Speter
5536169689Skan  if ((TYPE_PTR_P (src_type) || TYPE_PTRMEM_P (src_type))
5537169689Skan      && comp_ptr_ttypes_const (dst_type, src_type))
5538169689Skan    {
5539169689Skan      if (valid_p)
554050397Sobrien	{
5541169689Skan	  *valid_p = true;
5542169689Skan	  /* This cast is actually a C-style cast.  Issue a warning if
5543169689Skan	     the user is making a potentially unsafe cast.  */
5544169689Skan	  if (warn_cast_qual)
5545169689Skan	    check_for_casting_away_constness (src_type, dst_type,
5546169689Skan					      warning0,
5547169689Skan					      "cast");
5548169689Skan	}
5549169689Skan      if (reference_type)
5550169689Skan	{
555150397Sobrien	  expr = build_unary_op (ADDR_EXPR, expr, 0);
5552169689Skan	  expr = build_nop (reference_type, expr);
555350397Sobrien	  return convert_from_reference (expr);
555450397Sobrien	}
5555169689Skan      else
5556169689Skan	{
5557169689Skan	  expr = decay_conversion (expr);
5558169689Skan	  /* build_c_cast puts on a NOP_EXPR to make the result not an
5559169689Skan	     lvalue.  Strip such NOP_EXPRs if VALUE is being used in
5560169689Skan	     non-lvalue context.  */
5561169689Skan	  if (TREE_CODE (expr) == NOP_EXPR
5562169689Skan	      && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
5563169689Skan	    expr = TREE_OPERAND (expr, 0);
5564169689Skan	  return build_nop (dst_type, expr);
5565169689Skan	}
556618334Speter    }
556718334Speter
5568169689Skan  if (complain)
5569169689Skan    error ("invalid const_cast from type %qT to type %qT",
5570169689Skan	   src_type, dst_type);
557150397Sobrien  return error_mark_node;
557218334Speter}
557318334Speter
5574169689Skantree
5575169689Skanbuild_const_cast (tree type, tree expr)
5576169689Skan{
5577169689Skan  if (type == error_mark_node || error_operand_p (expr))
5578169689Skan    return error_mark_node;
557918334Speter
5580169689Skan  if (processing_template_decl)
5581169689Skan    {
5582169689Skan      tree t = build_min (CONST_CAST_EXPR, type, expr);
558318334Speter
5584169689Skan      if (!TREE_SIDE_EFFECTS (t)
5585169689Skan	  && type_dependent_expression_p (expr))
5586169689Skan	/* There might turn out to be side effects inside expr.  */
5587169689Skan	TREE_SIDE_EFFECTS (t) = 1;
5588169689Skan      return convert_from_reference (t);
5589169689Skan    }
5590169689Skan
5591169689Skan  return build_const_cast_1 (type, expr, /*complain=*/true,
5592169689Skan			     /*valid_p=*/NULL);
5593169689Skan}
5594169689Skan
5595169689Skan/* Build an expression representing an explicit C-style cast to type
5596169689Skan   TYPE of expression EXPR.  */
5597169689Skan
559818334Spetertree
5599132718Skanbuild_c_cast (tree type, tree expr)
560018334Speter{
5601132718Skan  tree value = expr;
5602169689Skan  tree result;
5603169689Skan  bool valid_p;
560418334Speter
5605169689Skan  if (type == error_mark_node || error_operand_p (expr))
560618334Speter    return error_mark_node;
560718334Speter
560890075Sobrien  if (processing_template_decl)
560990075Sobrien    {
561090075Sobrien      tree t = build_min (CAST_EXPR, type,
561190075Sobrien			  tree_cons (NULL_TREE, value, NULL_TREE));
5612132718Skan      /* We don't know if it will or will not have side effects.  */
5613132718Skan      TREE_SIDE_EFFECTS (t) = 1;
5614169689Skan      return convert_from_reference (t);
561590075Sobrien    }
561690075Sobrien
5617169689Skan  /* Casts to a (pointer to a) specific ObjC class (or 'id' or
5618169689Skan     'Class') should always be retained, because this information aids
5619169689Skan     in method lookup.  */
5620169689Skan  if (objc_is_object_ptr (type)
5621169689Skan      && objc_is_object_ptr (TREE_TYPE (expr)))
5622169689Skan    return build_nop (type, expr);
5623169689Skan
562418334Speter  /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
562518334Speter     Strip such NOP_EXPRs if VALUE is being used in non-lvalue context.  */
562618334Speter  if (TREE_CODE (type) != REFERENCE_TYPE
562718334Speter      && TREE_CODE (value) == NOP_EXPR
562818334Speter      && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
562918334Speter    value = TREE_OPERAND (value, 0);
563018334Speter
563118334Speter  if (TREE_CODE (type) == ARRAY_TYPE)
563218334Speter    {
563318334Speter      /* Allow casting from T1* to T2[] because Cfront allows it.
563490075Sobrien	 NIHCL uses it. It is not valid ISO C++ however.  */
563518334Speter      if (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE)
563618334Speter	{
5637169689Skan	  pedwarn ("ISO C++ forbids casting to an array type %qT", type);
563818334Speter	  type = build_pointer_type (TREE_TYPE (type));
563918334Speter	}
564018334Speter      else
564118334Speter	{
5642169689Skan	  error ("ISO C++ forbids casting to an array type %qT", type);
564318334Speter	  return error_mark_node;
564418334Speter	}
564518334Speter    }
564618334Speter
564718334Speter  if (TREE_CODE (type) == FUNCTION_TYPE
564818334Speter      || TREE_CODE (type) == METHOD_TYPE)
564918334Speter    {
5650169689Skan      error ("invalid cast to function type %qT", type);
565118334Speter      return error_mark_node;
565218334Speter    }
565318334Speter
5654169689Skan  /* A C-style cast can be a const_cast.  */
5655169689Skan  result = build_const_cast_1 (type, value, /*complain=*/false,
5656169689Skan			       &valid_p);
5657169689Skan  if (valid_p)
5658169689Skan    return result;
5659117395Skan
5660169689Skan  /* Or a static cast.  */
5661169689Skan  result = build_static_cast_1 (type, value, /*c_cast_p=*/true,
5662169689Skan				&valid_p);
5663169689Skan  /* Or a reinterpret_cast.  */
5664169689Skan  if (!valid_p)
5665169689Skan    result = build_reinterpret_cast_1 (type, value, /*c_cast_p=*/true,
5666169689Skan				       &valid_p);
5667169689Skan  /* The static_cast or reinterpret_cast may be followed by a
5668169689Skan     const_cast.  */
5669169689Skan  if (valid_p
5670169689Skan      /* A valid cast may result in errors if, for example, a
5671169689Skan	 conversion to am ambiguous base class is required.  */
5672169689Skan      && !error_operand_p (result))
567318334Speter    {
5674169689Skan      tree result_type;
567518334Speter
5676169689Skan      /* Non-class rvalues always have cv-unqualified type.  */
5677169689Skan      if (!CLASS_TYPE_P (type))
5678169689Skan	type = TYPE_MAIN_VARIANT (type);
5679169689Skan      result_type = TREE_TYPE (result);
5680169689Skan      if (!CLASS_TYPE_P (result_type))
5681169689Skan	result_type = TYPE_MAIN_VARIANT (result_type);
5682169689Skan      /* If the type of RESULT does not match TYPE, perform a
5683169689Skan	 const_cast to make it match.  If the static_cast or
5684169689Skan	 reinterpret_cast succeeded, we will differ by at most
5685169689Skan	 cv-qualification, so the follow-on const_cast is guaranteed
5686169689Skan	 to succeed.  */
5687169689Skan      if (!same_type_p (non_reference (type), non_reference (result_type)))
568852284Sobrien	{
5689169689Skan	  result = build_const_cast_1 (type, result, false, &valid_p);
5690169689Skan	  gcc_assert (valid_p);
569118334Speter	}
5692169689Skan      return result;
569318334Speter    }
569418334Speter
5695169689Skan  return error_mark_node;
569618334Speter}
569718334Speter
569818334Speter/* Build an assignment expression of lvalue LHS from value RHS.
569918334Speter   MODIFYCODE is the code for a binary operator that we use
570018334Speter   to combine the old value of LHS with RHS to get the new value.
570118334Speter   Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
570218334Speter
570350397Sobrien   C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed.  */
570418334Speter
570518334Spetertree
5706132718Skanbuild_modify_expr (tree lhs, enum tree_code modifycode, tree rhs)
570718334Speter{
5708132718Skan  tree result;
570918334Speter  tree newrhs = rhs;
571018334Speter  tree lhstype = TREE_TYPE (lhs);
571118334Speter  tree olhstype = lhstype;
5712132718Skan  tree olhs = NULL_TREE;
5713169689Skan  bool plain_assign = (modifycode == NOP_EXPR);
571418334Speter
571518334Speter  /* Avoid duplicate error messages from operands that had errors.  */
5716169689Skan  if (error_operand_p (lhs) || error_operand_p (rhs))
571718334Speter    return error_mark_node;
571818334Speter
571918334Speter  /* Handle control structure constructs used as "lvalues".  */
572018334Speter  switch (TREE_CODE (lhs))
572118334Speter    {
5722132718Skan      /* Handle --foo = 5; as these are valid constructs in C++.  */
572318334Speter    case PREDECREMENT_EXPR:
572418334Speter    case PREINCREMENT_EXPR:
572518334Speter      if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
5726169689Skan	lhs = build2 (TREE_CODE (lhs), TREE_TYPE (lhs),
5727169689Skan		      stabilize_reference (TREE_OPERAND (lhs, 0)),
5728169689Skan		      TREE_OPERAND (lhs, 1));
5729169689Skan      return build2 (COMPOUND_EXPR, lhstype,
5730169689Skan		     lhs,
5731169689Skan		     build_modify_expr (TREE_OPERAND (lhs, 0),
5732169689Skan					modifycode, rhs));
573318334Speter
573418334Speter      /* Handle (a, b) used as an "lvalue".  */
573518334Speter    case COMPOUND_EXPR:
573618334Speter      newrhs = build_modify_expr (TREE_OPERAND (lhs, 1),
573718334Speter				  modifycode, rhs);
573850397Sobrien      if (newrhs == error_mark_node)
573918334Speter	return error_mark_node;
5740169689Skan      return build2 (COMPOUND_EXPR, lhstype,
5741169689Skan		     TREE_OPERAND (lhs, 0), newrhs);
574218334Speter
574318334Speter    case MODIFY_EXPR:
5744122180Skan      if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
5745169689Skan	lhs = build2 (TREE_CODE (lhs), TREE_TYPE (lhs),
5746169689Skan		      stabilize_reference (TREE_OPERAND (lhs, 0)),
5747169689Skan		      TREE_OPERAND (lhs, 1));
574818334Speter      newrhs = build_modify_expr (TREE_OPERAND (lhs, 0), modifycode, rhs);
574950397Sobrien      if (newrhs == error_mark_node)
575018334Speter	return error_mark_node;
5751169689Skan      return build2 (COMPOUND_EXPR, lhstype, lhs, newrhs);
575218334Speter
5753169689Skan    case MIN_EXPR:
5754169689Skan    case MAX_EXPR:
5755169689Skan      /* MIN_EXPR and MAX_EXPR are currently only permitted as lvalues,
5756169689Skan	 when neither operand has side-effects.  */
5757169689Skan      if (!lvalue_or_else (lhs, lv_assign))
5758169689Skan	return error_mark_node;
5759169689Skan
5760169689Skan      gcc_assert (!TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0))
5761169689Skan		  && !TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 1)));
5762169689Skan
5763169689Skan      lhs = build3 (COND_EXPR, TREE_TYPE (lhs),
5764169689Skan		    build2 (TREE_CODE (lhs) == MIN_EXPR ? LE_EXPR : GE_EXPR,
5765169689Skan			    boolean_type_node,
5766169689Skan			    TREE_OPERAND (lhs, 0),
5767169689Skan			    TREE_OPERAND (lhs, 1)),
5768169689Skan		    TREE_OPERAND (lhs, 0),
5769169689Skan		    TREE_OPERAND (lhs, 1));
5770169689Skan      /* Fall through.  */
5771169689Skan
577218334Speter      /* Handle (a ? b : c) used as an "lvalue".  */
577318334Speter    case COND_EXPR:
577418334Speter      {
577518334Speter	/* Produce (a ? (b = rhs) : (c = rhs))
577618334Speter	   except that the RHS goes through a save-expr
577718334Speter	   so the code to compute it is only emitted once.  */
577890075Sobrien	tree cond;
5779117395Skan	tree preeval = NULL_TREE;
578090075Sobrien
5781169689Skan	if (VOID_TYPE_P (TREE_TYPE (rhs)))
5782169689Skan	  {
5783169689Skan	    error ("void value not ignored as it ought to be");
5784169689Skan	    return error_mark_node;
5785169689Skan	  }
5786169689Skan
5787117395Skan	rhs = stabilize_expr (rhs, &preeval);
5788169689Skan
578990075Sobrien	/* Check this here to avoid odd errors when trying to convert
579090075Sobrien	   a throw to the type of the COND_EXPR.  */
5791169689Skan	if (!lvalue_or_else (lhs, lv_assign))
579290075Sobrien	  return error_mark_node;
579390075Sobrien
579490075Sobrien	cond = build_conditional_expr
579590075Sobrien	  (TREE_OPERAND (lhs, 0),
5796169689Skan	   build_modify_expr (TREE_OPERAND (lhs, 1),
579790075Sobrien			      modifycode, rhs),
5798169689Skan	   build_modify_expr (TREE_OPERAND (lhs, 2),
579990075Sobrien			      modifycode, rhs));
580090075Sobrien
580150397Sobrien	if (cond == error_mark_node)
580218334Speter	  return cond;
580318334Speter	/* Make sure the code to compute the rhs comes out
580418334Speter	   before the split.  */
5805132718Skan	if (preeval)
5806169689Skan	  cond = build2 (COMPOUND_EXPR, TREE_TYPE (lhs), preeval, cond);
5807132718Skan	return cond;
580818334Speter      }
5809169689Skan
581050397Sobrien    default:
581150397Sobrien      break;
581218334Speter    }
581318334Speter
581418334Speter  if (modifycode == INIT_EXPR)
581518334Speter    {
581690075Sobrien      if (TREE_CODE (rhs) == CONSTRUCTOR)
581718334Speter	{
5818122180Skan	  if (! same_type_p (TREE_TYPE (rhs), lhstype))
5819122180Skan	    /* Call convert to generate an error; see PR 11063.  */
5820122180Skan	    rhs = convert (lhstype, rhs);
5821169689Skan	  result = build2 (INIT_EXPR, lhstype, lhs, rhs);
582290075Sobrien	  TREE_SIDE_EFFECTS (result) = 1;
582318334Speter	  return result;
582418334Speter	}
582590075Sobrien      else if (! IS_AGGR_TYPE (lhstype))
5826132718Skan	/* Do the default thing.  */;
582718334Speter      else
582818334Speter	{
5829117395Skan	  result = build_special_member_call (lhs, complete_ctor_identifier,
5830117395Skan					      build_tree_list (NULL_TREE, rhs),
5831169689Skan					      lhstype, LOOKUP_NORMAL);
583218334Speter	  if (result == NULL_TREE)
583318334Speter	    return error_mark_node;
583418334Speter	  return result;
583518334Speter	}
583618334Speter    }
583718334Speter  else
583818334Speter    {
583990075Sobrien      lhs = require_complete_type (lhs);
584090075Sobrien      if (lhs == error_mark_node)
584190075Sobrien	return error_mark_node;
584290075Sobrien
584390075Sobrien      if (modifycode == NOP_EXPR)
584490075Sobrien	{
584590075Sobrien	  /* `operator=' is not an inheritable operator.  */
584690075Sobrien	  if (! IS_AGGR_TYPE (lhstype))
5847132718Skan	    /* Do the default thing.  */;
584890075Sobrien	  else
584990075Sobrien	    {
5850132718Skan	      result = build_new_op (MODIFY_EXPR, LOOKUP_NORMAL,
5851132718Skan				     lhs, rhs, make_node (NOP_EXPR),
5852132718Skan				     /*overloaded_p=*/NULL);
585390075Sobrien	      if (result == NULL_TREE)
585490075Sobrien		return error_mark_node;
585590075Sobrien	      return result;
585690075Sobrien	    }
5857260311Spfg	  /* APPLE LOCAL end C* property (Radar 4436866) */
5858260311Spfg	  /* `operator=' is not an inheritable operator.  */
5859260311Spfg	  if (! IS_AGGR_TYPE (lhstype))
5860260311Spfg	    /* Do the default thing.  */;
5861260311Spfg	  else
5862260311Spfg	    {
5863260311Spfg	      result = build_new_op (MODIFY_EXPR, LOOKUP_NORMAL,
5864260311Spfg				     lhs, rhs, make_node (NOP_EXPR),
5865260311Spfg				     /*overloaded_p=*/NULL);
5866260311Spfg	      if (result == NULL_TREE)
5867260311Spfg		return error_mark_node;
5868260311Spfg	      return result;
5869260311Spfg	    }
587090075Sobrien	  lhstype = olhstype;
587190075Sobrien	}
587290075Sobrien      else
587390075Sobrien	{
587490075Sobrien	  /* A binary op has been requested.  Combine the old LHS
5875169689Skan	     value with the RHS producing the value we should actually
5876169689Skan	     store into the LHS.  */
587790075Sobrien
5878169689Skan	  gcc_assert (!PROMOTES_TO_AGGR_TYPE (lhstype, REFERENCE_TYPE));
587990075Sobrien	  lhs = stabilize_reference (lhs);
588090075Sobrien	  newrhs = cp_build_binary_op (modifycode, lhs, rhs);
588190075Sobrien	  if (newrhs == error_mark_node)
588290075Sobrien	    {
5883169689Skan	      error ("  in evaluation of %<%Q(%#T, %#T)%>", modifycode,
588490075Sobrien		     TREE_TYPE (lhs), TREE_TYPE (rhs));
588590075Sobrien	      return error_mark_node;
588690075Sobrien	    }
5887169689Skan
588890075Sobrien	  /* Now it looks like a plain assignment.  */
588990075Sobrien	  modifycode = NOP_EXPR;
5890260311Spfg	  lhstype = olhstype;
589190075Sobrien	}
5892169689Skan      gcc_assert (TREE_CODE (lhstype) != REFERENCE_TYPE);
5893169689Skan      gcc_assert (TREE_CODE (TREE_TYPE (newrhs)) != REFERENCE_TYPE);
589418334Speter    }
589518334Speter
5896169689Skan  /* The left-hand side must be an lvalue.  */
5897169689Skan  if (!lvalue_or_else (lhs, lv_assign))
589818334Speter    return error_mark_node;
589918334Speter
590090075Sobrien  /* Warn about modifying something that is `const'.  Don't warn if
590190075Sobrien     this is initialization.  */
590218334Speter  if (modifycode != INIT_EXPR
590352284Sobrien      && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype)
590452284Sobrien	  /* Functions are not modifiable, even though they are
590552284Sobrien	     lvalues.  */
590652284Sobrien	  || TREE_CODE (TREE_TYPE (lhs)) == FUNCTION_TYPE
590790075Sobrien	  || TREE_CODE (TREE_TYPE (lhs)) == METHOD_TYPE
590890075Sobrien	  /* If it's an aggregate and any field is const, then it is
590990075Sobrien	     effectively const.  */
5910132718Skan	  || (CLASS_TYPE_P (lhstype)
591190075Sobrien	      && C_TYPE_FIELDS_READONLY (lhstype))))
591218334Speter    readonly_error (lhs, "assignment", 0);
591318334Speter
591490075Sobrien  /* If storing into a structure or union member, it has probably been
591590075Sobrien     given type `int'.  Compute the type that would go with the actual
591690075Sobrien     amount of storage the member occupies.  */
591718334Speter
591818334Speter  if (TREE_CODE (lhs) == COMPONENT_REF
591918334Speter      && (TREE_CODE (lhstype) == INTEGER_TYPE
592018334Speter	  || TREE_CODE (lhstype) == REAL_TYPE
592118334Speter	  || TREE_CODE (lhstype) == ENUMERAL_TYPE))
592218334Speter    {
592318334Speter      lhstype = TREE_TYPE (get_unwidened (lhs, 0));
592418334Speter
592518334Speter      /* If storing in a field that is in actuality a short or narrower
592618334Speter	 than one, we must store in the field in its actual type.  */
592718334Speter
592818334Speter      if (lhstype != TREE_TYPE (lhs))
592918334Speter	{
5930132718Skan	  /* Avoid warnings converting integral types back into enums for
5931132718Skan	     enum bit fields.  */
5932132718Skan	  if (TREE_CODE (lhstype) == INTEGER_TYPE
5933132718Skan	      && TREE_CODE (olhstype) == ENUMERAL_TYPE)
5934132718Skan	    {
5935132718Skan	      if (TREE_SIDE_EFFECTS (lhs))
5936132718Skan		lhs = stabilize_reference (lhs);
5937132718Skan	      olhs = lhs;
5938132718Skan	    }
593918334Speter	  lhs = copy_node (lhs);
594018334Speter	  TREE_TYPE (lhs) = lhstype;
594118334Speter	}
594218334Speter    }
594318334Speter
594418334Speter  /* Convert new value to destination type.  */
594518334Speter
594618334Speter  if (TREE_CODE (lhstype) == ARRAY_TYPE)
594718334Speter    {
594818334Speter      int from_array;
5949169689Skan
595096263Sobrien      if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype),
595196263Sobrien				TYPE_MAIN_VARIANT (TREE_TYPE (rhs))))
595218334Speter	{
5953169689Skan	  error ("incompatible types in assignment of %qT to %qT",
595496263Sobrien		 TREE_TYPE (rhs), lhstype);
595518334Speter	  return error_mark_node;
595618334Speter	}
595718334Speter
595818334Speter      /* Allow array assignment in compiler-generated code.  */
595996263Sobrien      if (! DECL_ARTIFICIAL (current_function_decl))
5960169689Skan	{
5961169689Skan          /* This routine is used for both initialization and assignment.
5962169689Skan             Make sure the diagnostic message differentiates the context.  */
5963169689Skan          if (modifycode == INIT_EXPR)
5964169689Skan            error ("array used as initializer");
5965169689Skan          else
5966169689Skan            error ("invalid array assignment");
5967169689Skan	  return error_mark_node;
5968169689Skan	}
596918334Speter
597018334Speter      from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
5971169689Skan		   ? 1 + (modifycode != INIT_EXPR): 0;
5972169689Skan      return build_vec_init (lhs, NULL_TREE, newrhs,
5973169689Skan			     /*explicit_default_init_p=*/false,
5974169689Skan			     from_array);
597518334Speter    }
597618334Speter
597718334Speter  if (modifycode == INIT_EXPR)
597890075Sobrien    newrhs = convert_for_initialization (lhs, lhstype, newrhs, LOOKUP_NORMAL,
597990075Sobrien					 "initialization", NULL_TREE, 0);
598018334Speter  else
598118334Speter    {
598250397Sobrien      /* Avoid warnings on enum bit fields.  */
598318334Speter      if (TREE_CODE (olhstype) == ENUMERAL_TYPE
598418334Speter	  && TREE_CODE (lhstype) == INTEGER_TYPE)
598518334Speter	{
598618334Speter	  newrhs = convert_for_assignment (olhstype, newrhs, "assignment",
598718334Speter					   NULL_TREE, 0);
598818334Speter	  newrhs = convert_force (lhstype, newrhs, 0);
598918334Speter	}
599018334Speter      else
599118334Speter	newrhs = convert_for_assignment (lhstype, newrhs, "assignment",
599218334Speter					 NULL_TREE, 0);
599318334Speter      if (TREE_CODE (newrhs) == CALL_EXPR
599418334Speter	  && TYPE_NEEDS_CONSTRUCTING (lhstype))
599550397Sobrien	newrhs = build_cplus_new (lhstype, newrhs);
599618334Speter
599718334Speter      /* Can't initialize directly from a TARGET_EXPR, since that would
599818334Speter	 cause the lhs to be constructed twice, and possibly result in
599918334Speter	 accidental self-initialization.  So we force the TARGET_EXPR to be
600050397Sobrien	 expanded without a target.  */
600118334Speter      if (TREE_CODE (newrhs) == TARGET_EXPR)
6002169689Skan	newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
6003169689Skan			 TREE_OPERAND (newrhs, 0));
600418334Speter    }
600518334Speter
600650397Sobrien  if (newrhs == error_mark_node)
600718334Speter    return error_mark_node;
600818334Speter
6009169689Skan  if (c_dialect_objc () && flag_objc_gc)
6010169689Skan    {
6011169689Skan      result = objc_generate_write_barrier (lhs, modifycode, newrhs);
601218334Speter
6013169689Skan      if (result)
6014169689Skan	return result;
6015169689Skan    }
6016169689Skan
6017169689Skan  result = build2 (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
6018169689Skan		   lhstype, lhs, newrhs);
6019169689Skan
602018334Speter  TREE_SIDE_EFFECTS (result) = 1;
6021169689Skan  if (!plain_assign)
6022169689Skan    TREE_NO_WARNING (result) = 1;
602318334Speter
602418334Speter  /* If we got the LHS in a different type for storing in,
602518334Speter     convert the result back to the nominal type of LHS
602618334Speter     so that the value we return always has the same type
602718334Speter     as the LHS argument.  */
602818334Speter
602918334Speter  if (olhstype == TREE_TYPE (result))
603018334Speter    return result;
6031132718Skan  if (olhs)
603218334Speter    {
6033169689Skan      result = build2 (COMPOUND_EXPR, olhstype, result, olhs);
6034169689Skan      TREE_NO_WARNING (result) = 1;
603518334Speter      return result;
603618334Speter    }
603718334Speter  return convert_for_assignment (olhstype, result, "assignment",
603818334Speter				 NULL_TREE, 0);
603918334Speter}
604018334Speter
604150397Sobrientree
6042132718Skanbuild_x_modify_expr (tree lhs, enum tree_code modifycode, tree rhs)
604350397Sobrien{
6044260311Spfg  /* APPLE LOCAL __block assign sequence point 6639533 */
6045260311Spfg  bool insert_sequence_point = false;
6046260311Spfg
604750397Sobrien  if (processing_template_decl)
604850397Sobrien    return build_min_nt (MODOP_EXPR, lhs,
604950397Sobrien			 build_min_nt (modifycode, NULL_TREE, NULL_TREE), rhs);
605018334Speter
6051260311Spfg  /* APPLE LOCAL begin __block assign sequence point 6639533 */
6052260311Spfg  /* For byref = x;, we have to transform this into ({ typeof(x) x' =
6053260311Spfg     x; byref = x`; )} to ensure there is a sequence point before the
6054260311Spfg     evaluation of the byref, inorder to ensure that the access
6055260311Spfg     expression for byref doesn't start running before x is evaluated,
6056260311Spfg     as it will access the __forwarding pointer and that must be done
6057260311Spfg     after x is evaluated.  */
6058260311Spfg  /* First we check to see if lhs is a byref...  byrefs look like:
6059260311Spfg	__Block_byref_X.__forwarding->x  */
6060260311Spfg  if (TREE_CODE (lhs) == COMPONENT_REF)
6061260311Spfg    {
6062260311Spfg      tree inner = TREE_OPERAND (lhs, 0);
6063260311Spfg      /* now check for -> */
6064260311Spfg      if (TREE_CODE (inner) == INDIRECT_REF)
6065260311Spfg	{
6066260311Spfg	  inner = TREE_OPERAND (inner, 0);
6067260311Spfg	  if (TREE_CODE (inner) == COMPONENT_REF)
6068260311Spfg	    {
6069260311Spfg	      inner = TREE_OPERAND (inner, 0);
6070260311Spfg	      if (TREE_CODE (inner) == VAR_DECL
6071260311Spfg		  && COPYABLE_BYREF_LOCAL_VAR (inner))
6072260311Spfg		{
6073260311Spfg		  tree old_rhs = rhs;
6074260311Spfg		  /* then we save the rhs.  */
6075260311Spfg		  rhs = save_expr (rhs);
6076260311Spfg		  if (rhs != old_rhs)
6077260311Spfg		    /* And arrange for the sequence point to be inserted.  */
6078260311Spfg		    insert_sequence_point = true;
6079260311Spfg		}
6080260311Spfg	    }
6081260311Spfg	}
6082260311Spfg    }
6083260311Spfg  /* APPLE LOCAL end __block assign sequence point 6639533 */
6084260311Spfg
608550397Sobrien  if (modifycode != NOP_EXPR)
608650397Sobrien    {
6087132718Skan      tree rval = build_new_op (MODIFY_EXPR, LOOKUP_NORMAL, lhs, rhs,
6088132718Skan				make_node (modifycode),
6089132718Skan				/*overloaded_p=*/NULL);
609050397Sobrien      if (rval)
6091169689Skan	{
6092260311Spfg	  /* APPLE LOCAL begin __block assign sequence point 6639533 */
6093260311Spfg	  if (insert_sequence_point)
6094260311Spfg	    rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), rhs, rval);
6095260311Spfg	  /* APPLE LOCAL end __block assign sequence point 6639533 */
6096169689Skan	  TREE_NO_WARNING (rval) = 1;
6097169689Skan	  return rval;
6098169689Skan	}
609950397Sobrien    }
6100260311Spfg  lhs = build_modify_expr (lhs, modifycode, rhs);
6101260311Spfg  /* APPLE LOCAL begin __block assign sequence point 6639533 */
6102260311Spfg  if (insert_sequence_point)
6103260311Spfg    lhs = build2 (COMPOUND_EXPR, TREE_TYPE (lhs), rhs, lhs);
6104260311Spfg  /* APPLE LOCAL end __block assign sequence point 6639533 */
6105260311Spfg  return lhs;
610650397Sobrien}
610750397Sobrien
610818334Speter
610918334Speter/* Get difference in deltas for different pointer to member function
6110132718Skan   types.  Returns an integer constant of type PTRDIFF_TYPE_NODE.  If
6111169689Skan   the conversion is invalid, the constant is zero.  If
6112169689Skan   ALLOW_INVERSE_P is true, then allow reverse conversions as well.
6113169689Skan   If C_CAST_P is true this conversion is taking place as part of a
6114169689Skan   C-style cast.
611550397Sobrien
611652284Sobrien   Note that the naming of FROM and TO is kind of backwards; the return
611752284Sobrien   value is what we add to a TO in order to get a FROM.  They are named
611852284Sobrien   this way because we call this function to find out how to convert from
611952284Sobrien   a pointer to member of FROM to a pointer to member of TO.  */
612052284Sobrien
612118334Speterstatic tree
6122169689Skanget_delta_difference (tree from, tree to,
6123169689Skan		      bool allow_inverse_p,
6124169689Skan		      bool c_cast_p)
612518334Speter{
612618334Speter  tree binfo;
612790075Sobrien  base_kind kind;
6128169689Skan  tree result;
6129169689Skan
6130169689Skan  /* Assume no conversion is required.  */
6131169689Skan  result = integer_zero_node;
6132169689Skan  binfo = lookup_base (to, from, c_cast_p ? ba_unique : ba_check, &kind);
613390075Sobrien  if (kind == bk_inaccessible || kind == bk_ambig)
6134169689Skan    error ("   in pointer to member function conversion");
6135169689Skan  else if (binfo)
613618334Speter    {
6137169689Skan      if (kind != bk_via_virtual)
6138169689Skan	result = BINFO_OFFSET (binfo);
6139169689Skan      else
6140169689Skan	{
6141169689Skan	  tree virt_binfo = binfo_from_vbase (binfo);
6142169689Skan
6143169689Skan	  /* This is a reinterpret cast, we choose to do nothing.  */
6144169689Skan	  if (allow_inverse_p)
6145169689Skan	    warning (0, "pointer to member cast via virtual base %qT",
6146169689Skan		     BINFO_TYPE (virt_binfo));
6147169689Skan	  else
6148169689Skan	    error ("pointer to member conversion via virtual base %qT",
6149169689Skan		   BINFO_TYPE (virt_binfo));
6150169689Skan	}
615118334Speter    }
6152169689Skan  else if (same_type_ignoring_top_level_qualifiers_p (from, to))
6153169689Skan    /* Pointer to member of incomplete class is permitted*/;
6154169689Skan  else if (!allow_inverse_p)
615518334Speter    {
6156169689Skan      error_not_base_type (from, to);
6157169689Skan      error ("   in pointer to member conversion");
6158169689Skan    }
6159169689Skan  else
6160169689Skan    {
6161169689Skan      binfo = lookup_base (from, to, c_cast_p ? ba_unique : ba_check, &kind);
6162169689Skan      if (binfo)
616318334Speter	{
6164169689Skan	  if (kind != bk_via_virtual)
6165169689Skan	    result = size_diffop (size_zero_node, BINFO_OFFSET (binfo));
6166169689Skan	  else
6167169689Skan	    {
6168169689Skan	      /* This is a reinterpret cast, we choose to do nothing.  */
6169169689Skan	      tree virt_binfo = binfo_from_vbase (binfo);
6170169689Skan
6171169689Skan	      warning (0, "pointer to member cast via virtual base %qT",
6172169689Skan		       BINFO_TYPE (virt_binfo));
6173169689Skan	    }
617418334Speter	}
617518334Speter    }
617650397Sobrien
6177169689Skan  return fold_if_not_in_template (convert_to_integer (ptrdiff_type_node,
6178169689Skan						      result));
617918334Speter}
618018334Speter
618190075Sobrien/* Return a constructor for the pointer-to-member-function TYPE using
618290075Sobrien   the other components as specified.  */
618390075Sobrien
618452284Sobrientree
6185132718Skanbuild_ptrmemfunc1 (tree type, tree delta, tree pfn)
618650397Sobrien{
618790075Sobrien  tree u = NULL_TREE;
618890075Sobrien  tree delta_field;
618990075Sobrien  tree pfn_field;
6190169689Skan  VEC(constructor_elt, gc) *v;
619150397Sobrien
619290075Sobrien  /* Pull the FIELD_DECLs out of the type.  */
619390075Sobrien  pfn_field = TYPE_FIELDS (type);
619490075Sobrien  delta_field = TREE_CHAIN (pfn_field);
619550397Sobrien
619690075Sobrien  /* Make sure DELTA has the type we want.  */
619750397Sobrien  delta = convert_and_check (delta_type_node, delta);
619850397Sobrien
619990075Sobrien  /* Finish creating the initializer.  */
6200169689Skan  v = VEC_alloc(constructor_elt, gc, 2);
6201169689Skan  CONSTRUCTOR_APPEND_ELT(v, pfn_field, pfn);
6202169689Skan  CONSTRUCTOR_APPEND_ELT(v, delta_field, delta);
6203169689Skan  u = build_constructor (type, v);
6204169689Skan  TREE_CONSTANT (u) = TREE_CONSTANT (pfn) & TREE_CONSTANT (delta);
6205169689Skan  TREE_INVARIANT (u) = TREE_INVARIANT (pfn) & TREE_INVARIANT (delta);
620690075Sobrien  TREE_STATIC (u) = (TREE_CONSTANT (u)
620790075Sobrien		     && (initializer_constant_valid_p (pfn, TREE_TYPE (pfn))
620890075Sobrien			 != NULL_TREE)
6209169689Skan		     && (initializer_constant_valid_p (delta, TREE_TYPE (delta))
621090075Sobrien			 != NULL_TREE));
621150397Sobrien  return u;
621250397Sobrien}
621350397Sobrien
621418334Speter/* Build a constructor for a pointer to member function.  It can be
621518334Speter   used to initialize global variables, local variable, or used
621618334Speter   as a value in expressions.  TYPE is the POINTER to METHOD_TYPE we
621718334Speter   want to be.
621818334Speter
6219117395Skan   If FORCE is nonzero, then force this conversion, even if
622018334Speter   we would rather not do it.  Usually set when using an explicit
6221169689Skan   cast.  A C-style cast is being processed iff C_CAST_P is true.
622218334Speter
622318334Speter   Return error_mark_node, if something goes wrong.  */
622418334Speter
622518334Spetertree
6226169689Skanbuild_ptrmemfunc (tree type, tree pfn, int force, bool c_cast_p)
622718334Speter{
622852284Sobrien  tree fn;
6229117395Skan  tree pfn_type;
6230117395Skan  tree to_type;
623190075Sobrien
6232117395Skan  if (error_operand_p (pfn))
6233117395Skan    return error_mark_node;
6234117395Skan
6235117395Skan  pfn_type = TREE_TYPE (pfn);
6236117395Skan  to_type = build_ptrmemfunc_type (type);
6237117395Skan
623850397Sobrien  /* Handle multiple conversions of pointer to member functions.  */
6239117395Skan  if (TYPE_PTRMEMFUNC_P (pfn_type))
624018334Speter    {
624190075Sobrien      tree delta = NULL_TREE;
624252284Sobrien      tree npfn = NULL_TREE;
624390075Sobrien      tree n;
624450397Sobrien
6245169689Skan      if (!force
6246169689Skan	  && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn, LOOKUP_NORMAL))
6247169689Skan	error ("invalid conversion to type %qT from type %qT",
6248169689Skan	       to_type, pfn_type);
624918334Speter
625090075Sobrien      n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type),
625190075Sobrien				TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type),
6252169689Skan				force,
6253169689Skan				c_cast_p);
625418334Speter
625590075Sobrien      /* We don't have to do any conversion to convert a
625690075Sobrien	 pointer-to-member to its own type.  But, we don't want to
625790075Sobrien	 just return a PTRMEM_CST if there's an explicit cast; that
625890075Sobrien	 cast should make the expression an invalid template argument.  */
625990075Sobrien      if (TREE_CODE (pfn) != PTRMEM_CST)
626052284Sobrien	{
626190075Sobrien	  if (same_type_p (to_type, pfn_type))
626290075Sobrien	    return pfn;
626390075Sobrien	  else if (integer_zerop (n))
626490075Sobrien	    return build_reinterpret_cast (to_type, pfn);
626590075Sobrien	}
626618334Speter
626790075Sobrien      if (TREE_SIDE_EFFECTS (pfn))
626890075Sobrien	pfn = save_expr (pfn);
626990075Sobrien
627090075Sobrien      /* Obtain the function pointer and the current DELTA.  */
627190075Sobrien      if (TREE_CODE (pfn) == PTRMEM_CST)
627290075Sobrien	expand_ptrmemfunc_cst (pfn, &delta, &npfn);
627352284Sobrien      else
627452284Sobrien	{
6275117395Skan	  npfn = build_ptrmemfunc_access_expr (pfn, pfn_identifier);
6276117395Skan	  delta = build_ptrmemfunc_access_expr (pfn, delta_identifier);
627752284Sobrien	}
627852284Sobrien
627990075Sobrien      /* Just adjust the DELTA field.  */
6280169689Skan      gcc_assert  (same_type_ignoring_top_level_qualifiers_p
6281169689Skan		   (TREE_TYPE (delta), ptrdiff_type_node));
628290075Sobrien      if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_delta)
628390075Sobrien	n = cp_build_binary_op (LSHIFT_EXPR, n, integer_one_node);
628490075Sobrien      delta = cp_build_binary_op (PLUS_EXPR, delta, n);
628590075Sobrien      return build_ptrmemfunc1 (to_type, delta, npfn);
628618334Speter    }
628718334Speter
628850397Sobrien  /* Handle null pointer to member function conversions.  */
628918334Speter  if (integer_zerop (pfn))
629018334Speter    {
629150397Sobrien      pfn = build_c_cast (type, integer_zero_node);
629290075Sobrien      return build_ptrmemfunc1 (to_type,
6293169689Skan				integer_zero_node,
629490075Sobrien				pfn);
629518334Speter    }
629618334Speter
629752284Sobrien  if (type_unknown_p (pfn))
6298169689Skan    return instantiate_type (type, pfn, tf_warning_or_error);
629918334Speter
630052284Sobrien  fn = TREE_OPERAND (pfn, 0);
6301169689Skan  gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
6302169689Skan	      /* In a template, we will have preserved the
6303169689Skan		 OFFSET_REF.  */
6304169689Skan	      || (processing_template_decl && TREE_CODE (fn) == OFFSET_REF));
630590075Sobrien  return make_ptrmem_cst (to_type, fn);
630652284Sobrien}
630750397Sobrien
630852284Sobrien/* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
630990075Sobrien   given by CST.
631018334Speter
631190075Sobrien   ??? There is no consistency as to the types returned for the above
6312169689Skan   values.  Some code acts as if it were a sizetype and some as if it were
631390075Sobrien   integer_type_node.  */
631490075Sobrien
631552284Sobrienvoid
6316132718Skanexpand_ptrmemfunc_cst (tree cst, tree *delta, tree *pfn)
631752284Sobrien{
631852284Sobrien  tree type = TREE_TYPE (cst);
631952284Sobrien  tree fn = PTRMEM_CST_MEMBER (cst);
632052284Sobrien  tree ptr_class, fn_class;
632118334Speter
6322169689Skan  gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
632352284Sobrien
632452284Sobrien  /* The class that the function belongs to.  */
632590075Sobrien  fn_class = DECL_CONTEXT (fn);
632652284Sobrien
632752284Sobrien  /* The class that we're creating a pointer to member of.  */
632852284Sobrien  ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type);
632952284Sobrien
633052284Sobrien  /* First, calculate the adjustment to the function's class.  */
6331169689Skan  *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0,
6332169689Skan				 /*c_cast_p=*/0);
633352284Sobrien
633452284Sobrien  if (!DECL_VIRTUAL_P (fn))
633590075Sobrien    *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type), build_addr_func (fn));
633618334Speter  else
633718334Speter    {
633852284Sobrien      /* If we're dealing with a virtual function, we have to adjust 'this'
6339169689Skan	 again, to point to the base which provides the vtable entry for
6340169689Skan	 fn; the call will do the opposite adjustment.  */
6341117395Skan      tree orig_class = DECL_CONTEXT (fn);
634252284Sobrien      tree binfo = binfo_or_else (orig_class, fn_class);
6343169689Skan      *delta = build2 (PLUS_EXPR, TREE_TYPE (*delta),
6344169689Skan		       *delta, BINFO_OFFSET (binfo));
6345169689Skan      *delta = fold_if_not_in_template (*delta);
634618334Speter
634790075Sobrien      /* We set PFN to the vtable offset at which the function can be
634890075Sobrien	 found, plus one (unless ptrmemfunc_vbit_in_delta, in which
634990075Sobrien	 case delta is shifted left, and then incremented).  */
635090075Sobrien      *pfn = DECL_VINDEX (fn);
6351169689Skan      *pfn = build2 (MULT_EXPR, integer_type_node, *pfn,
6352169689Skan		     TYPE_SIZE_UNIT (vtable_entry_type));
6353169689Skan      *pfn = fold_if_not_in_template (*pfn);
635452284Sobrien
635590075Sobrien      switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
635690075Sobrien	{
635790075Sobrien	case ptrmemfunc_vbit_in_pfn:
6358169689Skan	  *pfn = build2 (PLUS_EXPR, integer_type_node, *pfn,
6359169689Skan			 integer_one_node);
6360169689Skan	  *pfn = fold_if_not_in_template (*pfn);
636190075Sobrien	  break;
636290075Sobrien
636390075Sobrien	case ptrmemfunc_vbit_in_delta:
6364169689Skan	  *delta = build2 (LSHIFT_EXPR, TREE_TYPE (*delta),
6365169689Skan			   *delta, integer_one_node);
6366169689Skan	  *delta = fold_if_not_in_template (*delta);
6367169689Skan	  *delta = build2 (PLUS_EXPR, TREE_TYPE (*delta),
6368169689Skan			   *delta, integer_one_node);
6369169689Skan	  *delta = fold_if_not_in_template (*delta);
637090075Sobrien	  break;
637190075Sobrien
637290075Sobrien	default:
6373169689Skan	  gcc_unreachable ();
637490075Sobrien	}
637590075Sobrien
6376169689Skan      *pfn = build_nop (TYPE_PTRMEMFUNC_FN_TYPE (type), *pfn);
6377169689Skan      *pfn = fold_if_not_in_template (*pfn);
637818334Speter    }
637952284Sobrien}
638018334Speter
638190075Sobrien/* Return an expression for PFN from the pointer-to-member function
638252284Sobrien   given by T.  */
638352284Sobrien
6384169689Skanstatic tree
6385132718Skanpfn_from_ptrmemfunc (tree t)
638652284Sobrien{
638752284Sobrien  if (TREE_CODE (t) == PTRMEM_CST)
638852284Sobrien    {
638952284Sobrien      tree delta;
639052284Sobrien      tree pfn;
6391169689Skan
639290075Sobrien      expand_ptrmemfunc_cst (t, &delta, &pfn);
639390075Sobrien      if (pfn)
639490075Sobrien	return pfn;
639552284Sobrien    }
639652284Sobrien
6397117395Skan  return build_ptrmemfunc_access_expr (t, pfn_identifier);
639818334Speter}
639918334Speter
640090075Sobrien/* Convert value RHS to type TYPE as preparation for an assignment to
640190075Sobrien   an lvalue of type TYPE.  ERRTYPE is a string to use in error
640290075Sobrien   messages: "assignment", "return", etc.  If FNDECL is non-NULL, we
640390075Sobrien   are doing the conversion in order to pass the PARMNUMth argument of
640490075Sobrien   FNDECL.  */
640518334Speter
640618334Speterstatic tree
6407132718Skanconvert_for_assignment (tree type, tree rhs,
6408132718Skan			const char *errtype, tree fndecl, int parmnum)
640918334Speter{
6410132718Skan  tree rhstype;
6411132718Skan  enum tree_code coder;
6412260311Spfg  /* APPLE LOCAL radar 4874632 */
6413260311Spfg  tree new_rhs = NULL_TREE;
641418334Speter
641518334Speter  /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue.  */
641618334Speter  if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
641718334Speter    rhs = TREE_OPERAND (rhs, 0);
641818334Speter
641990075Sobrien  rhstype = TREE_TYPE (rhs);
642090075Sobrien  coder = TREE_CODE (rhstype);
642190075Sobrien
6422132718Skan  if (TREE_CODE (type) == VECTOR_TYPE && coder == VECTOR_TYPE
6423259005Spfg      && vector_types_convertible_p (type, rhstype, true))
6424132718Skan    return convert (type, rhs);
6425132718Skan
642690075Sobrien  if (rhs == error_mark_node || rhstype == error_mark_node)
642718334Speter    return error_mark_node;
642850397Sobrien  if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)
642918334Speter    return error_mark_node;
643018334Speter
643190075Sobrien  /* The RHS of an assignment cannot have void type.  */
643218334Speter  if (coder == VOID_TYPE)
643318334Speter    {
643418334Speter      error ("void value not ignored as it ought to be");
643518334Speter      return error_mark_node;
643618334Speter    }
643718334Speter
643890075Sobrien  /* Simplify the RHS if possible.  */
643990075Sobrien  if (TREE_CODE (rhs) == CONST_DECL)
644090075Sobrien    rhs = DECL_INITIAL (rhs);
644118334Speter
6442169689Skan  if (c_dialect_objc ())
6443169689Skan    {
6444169689Skan      int parmno;
6445169689Skan      tree rname = fndecl;
6446110611Skan
6447169689Skan      if (!strcmp (errtype, "assignment"))
6448169689Skan	parmno = -1;
6449169689Skan      else if (!strcmp (errtype, "initialization"))
6450169689Skan	parmno = -2;
6451169689Skan      else
6452169689Skan	{
6453169689Skan	  tree selector = objc_message_selector ();
6454169689Skan
6455169689Skan	  parmno = parmnum;
6456169689Skan
6457169689Skan	  if (selector && parmno > 1)
6458169689Skan	    {
6459169689Skan	      rname = selector;
6460169689Skan	      parmno -= 1;
6461169689Skan	    }
6462169689Skan	}
6463169689Skan
6464260311Spfg      /* APPLE LOCAL file radar 6231433 */
6465260311Spfg      if (objc_compare_types (type, rhstype, parmno, rname, "comparison"))
6466260311Spfg	/* APPLE LOCAL radar 4874632 */
6467260311Spfg	new_rhs = convert (type, rhs);
6468169689Skan    }
6469169689Skan
647090075Sobrien  /* [expr.ass]
647118334Speter
647290075Sobrien     The expression is implicitly converted (clause _conv_) to the
647390075Sobrien     cv-unqualified type of the left operand.
647418334Speter
647590075Sobrien     We allow bad conversions here because by the time we get to this point
647690075Sobrien     we are committed to doing the conversion.  If we end up doing a bad
647790075Sobrien     conversion, convert_like will complain.  */
647890075Sobrien  if (!can_convert_arg_bad (type, rhstype, rhs))
647990075Sobrien    {
648090075Sobrien      /* When -Wno-pmf-conversions is use, we just silently allow
648190075Sobrien	 conversions from pointers-to-members to plain pointers.  If
648290075Sobrien	 the conversion doesn't work, cp_convert will complain.  */
6483169689Skan      if (!warn_pmf2ptr
6484169689Skan	  && TYPE_PTR_P (type)
648590075Sobrien	  && TYPE_PTRMEMFUNC_P (rhstype))
648690075Sobrien	rhs = cp_convert (strip_top_quals (type), rhs);
648718334Speter      else
648818334Speter	{
648990075Sobrien	  /* If the right-hand side has unknown type, then it is an
649090075Sobrien	     overloaded function.  Call instantiate_type to get error
649190075Sobrien	     messages.  */
649290075Sobrien	  if (rhstype == unknown_type_node)
6493169689Skan	    instantiate_type (type, rhs, tf_warning_or_error);
649490075Sobrien	  else if (fndecl)
6495169689Skan	    error ("cannot convert %qT to %qT for argument %qP to %qD",
6496169689Skan		   rhstype, type, parmnum, fndecl);
649718334Speter	  else
6498169689Skan	    error ("cannot convert %qT to %qT in %s", rhstype, type, errtype);
649990075Sobrien	  return error_mark_node;
650018334Speter	}
650118334Speter    }
6502169689Skan  if (warn_missing_format_attribute)
6503169689Skan    {
6504169689Skan      const enum tree_code codel = TREE_CODE (type);
6505169689Skan      if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
6506169689Skan	  && coder == codel
6507169689Skan	  && check_missing_format_attribute (type, rhstype))
6508169689Skan	warning (OPT_Wmissing_format_attribute,
6509169689Skan		 "%s might be a candidate for a format attribute",
6510169689Skan		 errtype);
6511169689Skan    }
6512169689Skan
6513258081Spfg  /* If -Wparentheses, warn about a = b = c when a has type bool and b
6514258081Spfg     does not.  */
6515258081Spfg  if (warn_parentheses
6516258081Spfg      && type == boolean_type_node
6517258081Spfg      && TREE_CODE (rhs) == MODIFY_EXPR
6518258081Spfg      && !TREE_NO_WARNING (rhs)
6519258081Spfg      && TREE_TYPE (rhs) != boolean_type_node)
6520258081Spfg    {
6521258081Spfg      warning (OPT_Wparentheses,
6522258081Spfg	       "suggest parentheses around assignment used as truth value");
6523258081Spfg      TREE_NO_WARNING (rhs) = 1;
6524258081Spfg    }
6525258081Spfg
652690075Sobrien  return perform_implicit_conversion (strip_top_quals (type), rhs);
652718334Speter}
652818334Speter
652952284Sobrien/* Convert RHS to be of type TYPE.
6530117395Skan   If EXP is nonzero, it is the target of the initialization.
653118334Speter   ERRTYPE is a string to use in error messages.
653218334Speter
653318334Speter   Two major differences between the behavior of
653418334Speter   `convert_for_assignment' and `convert_for_initialization'
653518334Speter   are that references are bashed in the former, while
653618334Speter   copied in the latter, and aggregates are assigned in
653718334Speter   the former (operator=) while initialized in the
653818334Speter   latter (X(X&)).
653918334Speter
654018334Speter   If using constructor make sure no conversion operator exists, if one does
654118334Speter   exist, an ambiguity exists.
654218334Speter
654318334Speter   If flags doesn't include LOOKUP_COMPLAIN, don't complain about anything.  */
654450397Sobrien
654518334Spetertree
6546132718Skanconvert_for_initialization (tree exp, tree type, tree rhs, int flags,
6547132718Skan			    const char *errtype, tree fndecl, int parmnum)
654818334Speter{
6549132718Skan  enum tree_code codel = TREE_CODE (type);
6550132718Skan  tree rhstype;
6551132718Skan  enum tree_code coder;
655218334Speter
655318334Speter  /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
655418334Speter     Strip such NOP_EXPRs, since RHS is used in non-lvalue context.  */
655518334Speter  if (TREE_CODE (rhs) == NOP_EXPR
655618334Speter      && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
655718334Speter      && codel != REFERENCE_TYPE)
655818334Speter    rhs = TREE_OPERAND (rhs, 0);
655918334Speter
6560169689Skan  if (type == error_mark_node
6561169689Skan      || rhs == error_mark_node
656218334Speter      || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
656318334Speter    return error_mark_node;
656418334Speter
656518334Speter  if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
656618334Speter       && TREE_CODE (type) != ARRAY_TYPE
656718334Speter       && (TREE_CODE (type) != REFERENCE_TYPE
656818334Speter	   || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
656952284Sobrien      || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
657052284Sobrien	  && (TREE_CODE (type) != REFERENCE_TYPE
657152284Sobrien	      || TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE))
657218334Speter      || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
6573132718Skan    rhs = decay_conversion (rhs);
657418334Speter
657518334Speter  rhstype = TREE_TYPE (rhs);
657618334Speter  coder = TREE_CODE (rhstype);
657718334Speter
657818334Speter  if (coder == ERROR_MARK)
657918334Speter    return error_mark_node;
658018334Speter
658118334Speter  /* We accept references to incomplete types, so we can
658218334Speter     return here before checking if RHS is of complete type.  */
6583169689Skan
658418334Speter  if (codel == REFERENCE_TYPE)
658518334Speter    {
658618334Speter      /* This should eventually happen in convert_arguments.  */
658750397Sobrien      int savew = 0, savee = 0;
658818334Speter
658918334Speter      if (fndecl)
659018334Speter	savew = warningcount, savee = errorcount;
6591122180Skan      rhs = initialize_reference (type, rhs, /*decl=*/NULL_TREE,
6592122180Skan				  /*cleanup=*/NULL);
659318334Speter      if (fndecl)
659418334Speter	{
659518334Speter	  if (warningcount > savew)
6596169689Skan	    warning (0, "in passing argument %P of %q+D", parmnum, fndecl);
659718334Speter	  else if (errorcount > savee)
6598169689Skan	    error ("in passing argument %P of %q+D", parmnum, fndecl);
659918334Speter	}
660018334Speter      return rhs;
6601169689Skan    }
660218334Speter
660352284Sobrien  if (exp != 0)
660452284Sobrien    exp = require_complete_type (exp);
660518334Speter  if (exp == error_mark_node)
660618334Speter    return error_mark_node;
660718334Speter
6608132718Skan  rhstype = non_reference (rhstype);
660918334Speter
661050397Sobrien  type = complete_type (type);
661150397Sobrien
661250397Sobrien  if (IS_AGGR_TYPE (type))
661350397Sobrien    return ocp_convert (type, rhs, CONV_IMPLICIT|CONV_FORCE_TEMP, flags);
661418334Speter
661518334Speter  return convert_for_assignment (type, rhs, errtype, fndecl, parmnum);
661618334Speter}
661718334Speter
661890075Sobrien/* If RETVAL is the address of, or a reference to, a local variable or
6619132718Skan   temporary give an appropriate warning.  */
662018334Speter
662190075Sobrienstatic void
6622132718Skanmaybe_warn_about_returning_address_of_local (tree retval)
662318334Speter{
662490075Sobrien  tree valtype = TREE_TYPE (DECL_RESULT (current_function_decl));
662590075Sobrien  tree whats_returned = retval;
662618334Speter
662790075Sobrien  for (;;)
662818334Speter    {
662990075Sobrien      if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
663090075Sobrien	whats_returned = TREE_OPERAND (whats_returned, 1);
663190075Sobrien      else if (TREE_CODE (whats_returned) == CONVERT_EXPR
663290075Sobrien	       || TREE_CODE (whats_returned) == NON_LVALUE_EXPR
663390075Sobrien	       || TREE_CODE (whats_returned) == NOP_EXPR)
663490075Sobrien	whats_returned = TREE_OPERAND (whats_returned, 0);
663590075Sobrien      else
663690075Sobrien	break;
663718334Speter    }
663818334Speter
663990075Sobrien  if (TREE_CODE (whats_returned) != ADDR_EXPR)
664090075Sobrien    return;
6641169689Skan  whats_returned = TREE_OPERAND (whats_returned, 0);
664290075Sobrien
664390075Sobrien  if (TREE_CODE (valtype) == REFERENCE_TYPE)
664450397Sobrien    {
664590075Sobrien      if (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
664690075Sobrien	  || TREE_CODE (whats_returned) == TARGET_EXPR)
664790075Sobrien	{
6648169689Skan	  warning (0, "returning reference to temporary");
664990075Sobrien	  return;
665090075Sobrien	}
6651169689Skan      if (TREE_CODE (whats_returned) == VAR_DECL
665290075Sobrien	  && DECL_NAME (whats_returned)
665390075Sobrien	  && TEMP_NAME_P (DECL_NAME (whats_returned)))
665490075Sobrien	{
6655169689Skan	  warning (0, "reference to non-lvalue returned");
665690075Sobrien	  return;
665790075Sobrien	}
665850397Sobrien    }
665950397Sobrien
6660169689Skan  while (TREE_CODE (whats_returned) == COMPONENT_REF
6661169689Skan	 || TREE_CODE (whats_returned) == ARRAY_REF)
6662169689Skan    whats_returned = TREE_OPERAND (whats_returned, 0);
6663169689Skan
6664169689Skan  if (DECL_P (whats_returned)
666590075Sobrien      && DECL_NAME (whats_returned)
666690075Sobrien      && DECL_FUNCTION_SCOPE_P (whats_returned)
666790075Sobrien      && !(TREE_STATIC (whats_returned)
666890075Sobrien	   || TREE_PUBLIC (whats_returned)))
666950397Sobrien    {
667090075Sobrien      if (TREE_CODE (valtype) == REFERENCE_TYPE)
6671169689Skan	warning (0, "reference to local variable %q+D returned",
6672169689Skan		 whats_returned);
6673260311Spfg      /* APPLE LOCAL begin blocks 6040305 (cn) */
6674260311Spfg      else if (TREE_CODE (valtype) == BLOCK_POINTER_TYPE)
6675260311Spfg	error ("returning block that lives on the local stack");
6676260311Spfg      /* APPLE LOCAL end blocks 6040305 (cn) */
667790075Sobrien      else
6678169689Skan	warning (0, "address of local variable %q+D returned",
6679169689Skan		 whats_returned);
668050397Sobrien      return;
668150397Sobrien    }
668290075Sobrien}
668350397Sobrien
6684260311Spfg/* APPLE LOCAL begin blocks 6040305 (cm) */
6685260311Spfgstatic bool
6686260311Spfgtypes_are_block_compatible (tree t1, tree t2)
6687260311Spfg{
6688260311Spfg  return comptypes (t1, t2, COMPARE_STRICT);
6689260311Spfg}
6690260311Spfg/* APPLE LOCAL end blocks 6040305 (cm) */
6691260311Spfg
6692117395Skan/* Check that returning RETVAL from the current function is valid.
669390075Sobrien   Return an expression explicitly showing all conversions required to
669490075Sobrien   change RETVAL into the function return type, and to assign it to
6695169689Skan   the DECL_RESULT for the function.  Set *NO_WARNING to true if
6696169689Skan   code reaches end of non-void function warning shouldn't be issued
6697169689Skan   on this RETURN_EXPR.  */
669818334Speter
669990075Sobrientree
6700169689Skancheck_return_expr (tree retval, bool *no_warning)
670190075Sobrien{
670290075Sobrien  tree result;
670390075Sobrien  /* The type actually returned by the function, after any
670490075Sobrien     promotions.  */
670590075Sobrien  tree valtype;
670690075Sobrien  int fn_returns_value_p;
670718334Speter
6708169689Skan  *no_warning = false;
6709169689Skan
671090075Sobrien  /* A `volatile' function is one that isn't supposed to return, ever.
671190075Sobrien     (This is a G++ extension, used to get better code for functions
671290075Sobrien     that call the `volatile' function.)  */
671390075Sobrien  if (TREE_THIS_VOLATILE (current_function_decl))
6714169689Skan    warning (0, "function declared %<noreturn%> has a %<return%> statement");
671518334Speter
671690075Sobrien  /* Check for various simple errors.  */
671790075Sobrien  if (DECL_DESTRUCTOR_P (current_function_decl))
671890075Sobrien    {
671990075Sobrien      if (retval)
672090075Sobrien	error ("returning a value from a destructor");
672190075Sobrien      return NULL_TREE;
672218334Speter    }
672350397Sobrien  else if (DECL_CONSTRUCTOR_P (current_function_decl))
672418334Speter    {
672590075Sobrien      if (in_function_try_handler)
672690075Sobrien	/* If a return statement appears in a handler of the
6727117395Skan	   function-try-block of a constructor, the program is ill-formed.  */
672890075Sobrien	error ("cannot return from a handler of a function-try-block of a constructor");
672990075Sobrien      else if (retval)
673090075Sobrien	/* You can't return a value from a constructor.  */
673150397Sobrien	error ("returning a value from a constructor");
673290075Sobrien      return NULL_TREE;
673318334Speter    }
673418334Speter
6735260311Spfg  /* APPLE LOCAL begin blocks 6040305 (cm) */
6736260311Spfg  /* APPLE LOCAL radar 6185344 */
6737260311Spfg  if (cur_block && !cur_block->block_has_return_type)
6738260311Spfg    {
6739260311Spfg      /* If this is the first return we've seen in the block, infer the type of
6740260311Spfg   the block from it. */
6741260311Spfg      if (cur_block->return_type == NULL_TREE)
6742260311Spfg  {
6743260311Spfg    if (retval)
6744260311Spfg      {
6745260311Spfg	 tree restype;
6746260311Spfg	 retval = decay_conversion (retval);
6747260311Spfg	 restype = TYPE_MAIN_VARIANT (TREE_TYPE (retval));
6748260311Spfg	 TREE_TYPE (current_function_decl)
6749260311Spfg      = build_function_type (restype,
6750260311Spfg		       TYPE_ARG_TYPES (TREE_TYPE (current_function_decl)));
6751260311Spfg	 TREE_TYPE (DECL_RESULT (current_function_decl)) = restype;
6752260311Spfg	 relayout_decl (DECL_RESULT (current_function_decl));
6753260311Spfg	 cur_block->return_type = restype;
6754260311Spfg      }
6755260311Spfg    else
6756260311Spfg      cur_block->return_type = void_type_node;
6757260311Spfg  }
6758260311Spfg
6759260311Spfg      /* Verify that this result type matches the previous one.  We
6760260311Spfg   are pickier with blocks than for normal functions because
6761260311Spfg   this is a new feature and we set the rules. */
6762260311Spfg      if (TREE_CODE (cur_block->return_type) == VOID_TYPE)
6763260311Spfg  {
6764260311Spfg    if (retval)
6765260311Spfg      {
6766260311Spfg	 error ("void block should not return a value");
6767260311Spfg	 return error_mark_node;
6768260311Spfg      }
6769260311Spfg  }
6770260311Spfg      else if (!retval)
6771260311Spfg  {
6772260311Spfg    error ("non-void block should return a value");
6773260311Spfg    return error_mark_node;
6774260311Spfg  }
6775260311Spfg
6776260311Spfg      if (retval)
6777260311Spfg  {
6778260311Spfg    /* We have a non-void block with an expression, continue checking.  */
6779260311Spfg    valtype = TREE_TYPE (retval);
6780260311Spfg
6781260311Spfg    /* For now, restrict multiple return statements in a block to have
6782260311Spfg	strict compatible types only. */
6783260311Spfg    if (!types_are_block_compatible (cur_block->return_type, valtype))
6784260311Spfg      {
6785260311Spfg	 error ("incompatible type returning %qT, expected %qT",
6786260311Spfg	    valtype, cur_block->return_type);
6787260311Spfg	 return error_mark_node;
6788260311Spfg      }
6789260311Spfg  }
6790260311Spfg    }
6791260311Spfg  /* APPLE LOCAL end blocks 6040305 (cm) */
6792260311Spfg
6793132718Skan  if (processing_template_decl)
6794132718Skan    {
6795132718Skan      current_function_returns_value = 1;
6796132718Skan      return retval;
6797132718Skan    }
6798169689Skan
679990075Sobrien  /* When no explicit return-value is given in a function with a named
680090075Sobrien     return value, the named return value is used.  */
680190075Sobrien  result = DECL_RESULT (current_function_decl);
680290075Sobrien  valtype = TREE_TYPE (result);
6803169689Skan  gcc_assert (valtype != NULL_TREE);
680490075Sobrien  fn_returns_value_p = !VOID_TYPE_P (valtype);
680590075Sobrien  if (!retval && DECL_NAME (result) && fn_returns_value_p)
680690075Sobrien    retval = result;
680790075Sobrien
680890075Sobrien  /* Check for a return statement with no return value in a function
680990075Sobrien     that's supposed to return a value.  */
681090075Sobrien  if (!retval && fn_returns_value_p)
681190075Sobrien    {
6812169689Skan      pedwarn ("return-statement with no value, in function returning %qT",
6813132718Skan	       valtype);
681490075Sobrien      /* Clear this, so finish_function won't say that we reach the
681590075Sobrien	 end of a non-void function (which we don't, we gave a
681690075Sobrien	 return!).  */
681790075Sobrien      current_function_returns_null = 0;
6818169689Skan      /* And signal caller that TREE_NO_WARNING should be set on the
6819169689Skan	 RETURN_EXPR to avoid control reaches end of non-void function
6820169689Skan	 warnings in tree-cfg.c.  */
6821169689Skan      *no_warning = true;
682290075Sobrien    }
682390075Sobrien  /* Check for a return statement with a value in a function that
682490075Sobrien     isn't supposed to return a value.  */
682590075Sobrien  else if (retval && !fn_returns_value_p)
6826169689Skan    {
682790075Sobrien      if (VOID_TYPE_P (TREE_TYPE (retval)))
682890075Sobrien	/* You can return a `void' value from a function of `void'
682990075Sobrien	   type.  In that case, we have to evaluate the expression for
683090075Sobrien	   its side-effects.  */
683190075Sobrien	  finish_expr_stmt (retval);
683290075Sobrien      else
6833132718Skan	pedwarn ("return-statement with a value, in function "
6834169689Skan		 "returning 'void'");
683590075Sobrien
683690075Sobrien      current_function_returns_null = 1;
683790075Sobrien
683890075Sobrien      /* There's really no value to return, after all.  */
683990075Sobrien      return NULL_TREE;
684090075Sobrien    }
684190075Sobrien  else if (!retval)
684290075Sobrien    /* Remember that this function can sometimes return without a
684390075Sobrien       value.  */
684490075Sobrien    current_function_returns_null = 1;
684590075Sobrien  else
684690075Sobrien    /* Remember that this function did return a value.  */
684790075Sobrien    current_function_returns_value = 1;
684890075Sobrien
6849161651Skan  /* Check for erroneous operands -- but after giving ourselves a
6850161651Skan     chance to provide an error about returning a value from a void
6851161651Skan     function.  */
6852161651Skan  if (error_operand_p (retval))
6853161651Skan    {
6854161651Skan      current_function_return_value = error_mark_node;
6855161651Skan      return error_mark_node;
6856161651Skan    }
6857161651Skan
685890075Sobrien  /* Only operator new(...) throw(), can return NULL [expr.new/13].  */
685990075Sobrien  if ((DECL_OVERLOADED_OPERATOR_P (current_function_decl) == NEW_EXPR
686090075Sobrien       || DECL_OVERLOADED_OPERATOR_P (current_function_decl) == VEC_NEW_EXPR)
686190075Sobrien      && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl))
686290075Sobrien      && ! flag_check_new
686390075Sobrien      && null_ptr_cst_p (retval))
6864169689Skan    warning (0, "%<operator new%> must not return NULL unless it is "
6865169689Skan	     "declared %<throw()%> (or -fcheck-new is in effect)");
686690075Sobrien
686750397Sobrien  /* Effective C++ rule 15.  See also start_function.  */
686850397Sobrien  if (warn_ecpp
6869169689Skan      && DECL_NAME (current_function_decl) == ansi_assopname(NOP_EXPR))
6870169689Skan    {
6871169689Skan      bool warn = true;
687250397Sobrien
6873169689Skan      /* The function return type must be a reference to the current
6874169689Skan	class.  */
6875169689Skan      if (TREE_CODE (valtype) == REFERENCE_TYPE
6876169689Skan	  && same_type_ignoring_top_level_qualifiers_p
6877169689Skan	      (TREE_TYPE (valtype), TREE_TYPE (current_class_ref)))
6878169689Skan	{
6879169689Skan	  /* Returning '*this' is obviously OK.  */
6880169689Skan	  if (retval == current_class_ref)
6881169689Skan	    warn = false;
6882169689Skan	  /* If we are calling a function whose return type is the same of
6883169689Skan	     the current class reference, it is ok.  */
6884169689Skan	  else if (TREE_CODE (retval) == INDIRECT_REF
6885169689Skan		   && TREE_CODE (TREE_OPERAND (retval, 0)) == CALL_EXPR)
6886169689Skan	    warn = false;
6887169689Skan	}
6888169689Skan
6889169689Skan      if (warn)
6890169689Skan	warning (OPT_Weffc__, "%<operator=%> should return a reference to %<*this%>");
6891169689Skan    }
6892169689Skan
689390075Sobrien  /* The fabled Named Return Value optimization, as per [class.copy]/15:
689490075Sobrien
689590075Sobrien     [...]      For  a function with a class return type, if the expression
689690075Sobrien     in the return statement is the name of a local  object,  and  the  cv-
689790075Sobrien     unqualified  type  of  the  local  object  is the same as the function
689890075Sobrien     return type, an implementation is permitted to omit creating the  tem-
689990075Sobrien     porary  object  to  hold  the function return value [...]
690090075Sobrien
690190075Sobrien     So, if this is a value-returning function that always returns the same
690290075Sobrien     local variable, remember it.
690390075Sobrien
690490075Sobrien     It might be nice to be more flexible, and choose the first suitable
690590075Sobrien     variable even if the function sometimes returns something else, but
690690075Sobrien     then we run the risk of clobbering the variable we chose if the other
690790075Sobrien     returned expression uses the chosen variable somehow.  And people expect
690890075Sobrien     this restriction, anyway.  (jason 2000-11-19)
690990075Sobrien
6910169689Skan     See finish_function and finalize_nrv for the rest of this optimization.  */
691190075Sobrien
691290075Sobrien  if (fn_returns_value_p && flag_elide_constructors)
691318334Speter    {
6914259660Spfg      if (retval != NULL_TREE
6915259660Spfg	  && (current_function_return_value == NULL_TREE
6916259660Spfg	      || current_function_return_value == retval)
6917259660Spfg	  && TREE_CODE (retval) == VAR_DECL
6918259660Spfg	  && DECL_CONTEXT (retval) == current_function_decl
6919259660Spfg	  && ! TREE_STATIC (retval)
6920259660Spfg	  && ! DECL_ANON_UNION_VAR_P (retval)
6921259660Spfg	  && (DECL_ALIGN (retval)
6922259660Spfg	      >= DECL_ALIGN (DECL_RESULT (current_function_decl)))
6923259660Spfg	  && same_type_p ((TYPE_MAIN_VARIANT
6924259660Spfg			   (TREE_TYPE (retval))),
6925259660Spfg			  (TYPE_MAIN_VARIANT
6926259660Spfg			   (TREE_TYPE (TREE_TYPE (current_function_decl))))))
692790075Sobrien	current_function_return_value = retval;
692890075Sobrien      else
692990075Sobrien	current_function_return_value = error_mark_node;
693018334Speter    }
693118334Speter
693290075Sobrien  /* We don't need to do any conversions when there's nothing being
693390075Sobrien     returned.  */
6934161651Skan  if (!retval)
6935161651Skan    return NULL_TREE;
693690075Sobrien
693790075Sobrien  /* Do any required conversions.  */
693890075Sobrien  if (retval == result || DECL_CONSTRUCTOR_P (current_function_decl))
693990075Sobrien    /* No conversions are required.  */
694090075Sobrien    ;
694118334Speter  else
694218334Speter    {
694390075Sobrien      /* The type the function is declared to return.  */
694450397Sobrien      tree functype = TREE_TYPE (TREE_TYPE (current_function_decl));
694550397Sobrien
6946169689Skan      /* The functype's return type will have been set to void, if it
6947169689Skan	 was an incomplete type.  Just treat this as 'return;' */
6948169689Skan      if (VOID_TYPE_P (functype))
6949169689Skan	return error_mark_node;
6950169689Skan
695150397Sobrien      /* First convert the value to the function's return type, then
695250397Sobrien	 to the type of return value's location to handle the
6953169689Skan	 case that functype is smaller than the valtype.  */
695450397Sobrien      retval = convert_for_initialization
6955259660Spfg	(NULL_TREE, functype, retval, LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING,
6956259660Spfg	 "return", NULL_TREE, 0);
695750397Sobrien      retval = convert (valtype, retval);
695850397Sobrien
695990075Sobrien      /* If the conversion failed, treat this just like `return;'.  */
696050397Sobrien      if (retval == error_mark_node)
696190075Sobrien	return retval;
696250397Sobrien      /* We can't initialize a register from a AGGR_INIT_EXPR.  */
696350397Sobrien      else if (! current_function_returns_struct
696450397Sobrien	       && TREE_CODE (retval) == TARGET_EXPR
696550397Sobrien	       && TREE_CODE (TREE_OPERAND (retval, 1)) == AGGR_INIT_EXPR)
6966169689Skan	retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
6967169689Skan			 TREE_OPERAND (retval, 0));
696890075Sobrien      else
696990075Sobrien	maybe_warn_about_returning_address_of_local (retval);
697018334Speter    }
6971169689Skan
697290075Sobrien  /* Actually copy the value returned into the appropriate location.  */
697350397Sobrien  if (retval && retval != result)
6974169689Skan    retval = build2 (INIT_EXPR, TREE_TYPE (result), result, retval);
697518334Speter
697690075Sobrien  return retval;
697790075Sobrien}
697818334Speter
697918334Speter
6980117395Skan/* Returns nonzero if the pointer-type FROM can be converted to the
698150397Sobrien   pointer-type TO via a qualification conversion.  If CONSTP is -1,
6982117395Skan   then we return nonzero if the pointers are similar, and the
698350397Sobrien   cv-qualification signature of FROM is a proper subset of that of TO.
698450397Sobrien
698550397Sobrien   If CONSTP is positive, then all outer pointers have been
698650397Sobrien   const-qualified.  */
698750397Sobrien
698850397Sobrienstatic int
6989132718Skancomp_ptr_ttypes_real (tree to, tree from, int constp)
699018334Speter{
6991132718Skan  bool to_more_cv_qualified = false;
699250397Sobrien
699318334Speter  for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
699418334Speter    {
699518334Speter      if (TREE_CODE (to) != TREE_CODE (from))
699618334Speter	return 0;
699718334Speter
699850397Sobrien      if (TREE_CODE (from) == OFFSET_TYPE
6999132718Skan	  && !same_type_p (TYPE_OFFSET_BASETYPE (from),
7000132718Skan			   TYPE_OFFSET_BASETYPE (to)))
7001132718Skan	return 0;
700250397Sobrien
700318334Speter      /* Const and volatile mean something different for function types,
700418334Speter	 so the usual checks are not appropriate.  */
700518334Speter      if (TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
700618334Speter	{
7007169689Skan	  /* In Objective-C++, some types may have been 'volatilized' by
7008169689Skan	     the compiler for EH; when comparing them here, the volatile
7009169689Skan	     qualification must be ignored.  */
7010169689Skan	  bool objc_quals_match = objc_type_quals_match (to, from);
7011169689Skan
7012169689Skan	  if (!at_least_as_qualified_p (to, from) && !objc_quals_match)
701318334Speter	    return 0;
701418334Speter
7015169689Skan	  if (!at_least_as_qualified_p (from, to) && !objc_quals_match)
701650397Sobrien	    {
701750397Sobrien	      if (constp == 0)
701850397Sobrien		return 0;
7019132718Skan	      to_more_cv_qualified = true;
702050397Sobrien	    }
702150397Sobrien
702250397Sobrien	  if (constp > 0)
702350397Sobrien	    constp &= TYPE_READONLY (to);
702418334Speter	}
702518334Speter
7026132718Skan      if (TREE_CODE (to) != POINTER_TYPE && !TYPE_PTRMEM_P (to))
7027132718Skan	return ((constp >= 0 || to_more_cv_qualified)
7028132718Skan		&& same_type_ignoring_top_level_qualifiers_p (to, from));
702918334Speter    }
703018334Speter}
703118334Speter
7032132718Skan/* When comparing, say, char ** to char const **, this function takes
7033132718Skan   the 'char *' and 'char const *'.  Do not pass non-pointer/reference
7034132718Skan   types to this function.  */
703550397Sobrien
703618334Speterint
7037132718Skancomp_ptr_ttypes (tree to, tree from)
703818334Speter{
703918334Speter  return comp_ptr_ttypes_real (to, from, 1);
704018334Speter}
704150397Sobrien
704250397Sobrien/* Returns 1 if to and from are (possibly multi-level) pointers to the same
704350397Sobrien   type or inheritance-related types, regardless of cv-quals.  */
704450397Sobrien
704550397Sobrienint
7046132718Skanptr_reasonably_similar (tree to, tree from)
704750397Sobrien{
704850397Sobrien  for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
704950397Sobrien    {
705090075Sobrien      /* Any target type is similar enough to void.  */
705190075Sobrien      if (TREE_CODE (to) == VOID_TYPE
705290075Sobrien	  || TREE_CODE (from) == VOID_TYPE)
705390075Sobrien	return 1;
705490075Sobrien
705550397Sobrien      if (TREE_CODE (to) != TREE_CODE (from))
705650397Sobrien	return 0;
705750397Sobrien
705850397Sobrien      if (TREE_CODE (from) == OFFSET_TYPE
705950397Sobrien	  && comptypes (TYPE_OFFSET_BASETYPE (to),
7060169689Skan			TYPE_OFFSET_BASETYPE (from),
7061132718Skan			COMPARE_BASE | COMPARE_DERIVED))
706250397Sobrien	continue;
706350397Sobrien
7064169689Skan      if (TREE_CODE (to) == VECTOR_TYPE
7065259005Spfg	  && vector_types_convertible_p (to, from, false))
7066169689Skan	return 1;
7067169689Skan
706890075Sobrien      if (TREE_CODE (to) == INTEGER_TYPE
706990075Sobrien	  && TYPE_PRECISION (to) == TYPE_PRECISION (from))
707090075Sobrien	return 1;
707190075Sobrien
707290075Sobrien      if (TREE_CODE (to) == FUNCTION_TYPE)
707390075Sobrien	return 1;
707490075Sobrien
707550397Sobrien      if (TREE_CODE (to) != POINTER_TYPE)
707650397Sobrien	return comptypes
7077169689Skan	  (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from),
7078132718Skan	   COMPARE_BASE | COMPARE_DERIVED);
707950397Sobrien    }
708050397Sobrien}
708150397Sobrien
7082169689Skan/* Return true if TO and FROM (both of which are POINTER_TYPEs or
7083169689Skan   pointer-to-member types) are the same, ignoring cv-qualification at
7084169689Skan   all levels.  */
708550397Sobrien
7086169689Skanbool
7087132718Skancomp_ptr_ttypes_const (tree to, tree from)
708850397Sobrien{
708950397Sobrien  for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
709050397Sobrien    {
709150397Sobrien      if (TREE_CODE (to) != TREE_CODE (from))
7092169689Skan	return false;
709350397Sobrien
709450397Sobrien      if (TREE_CODE (from) == OFFSET_TYPE
709552284Sobrien	  && same_type_p (TYPE_OFFSET_BASETYPE (from),
709652284Sobrien			  TYPE_OFFSET_BASETYPE (to)))
709750397Sobrien	  continue;
709850397Sobrien
709950397Sobrien      if (TREE_CODE (to) != POINTER_TYPE)
710090075Sobrien	return same_type_ignoring_top_level_qualifiers_p (to, from);
710150397Sobrien    }
710250397Sobrien}
710350397Sobrien
710490075Sobrien/* Returns the type qualifiers for this type, including the qualifiers on the
710590075Sobrien   elements for an array type.  */
710652284Sobrien
710752284Sobrienint
7108132718Skancp_type_quals (tree type)
710952284Sobrien{
711090075Sobrien  type = strip_array_types (type);
7111117395Skan  if (type == error_mark_node)
7112117395Skan    return TYPE_UNQUALIFIED;
711352284Sobrien  return TYPE_QUALS (type);
711452284Sobrien}
711552284Sobrien
7116169689Skan/* Returns nonzero if the TYPE is const from a C++ perspective: look inside
7117169689Skan   arrays.  */
7118169689Skan
7119169689Skanbool
7120169689Skancp_type_readonly (tree type)
7121169689Skan{
7122169689Skan  type = strip_array_types (type);
7123169689Skan  return TYPE_READONLY (type);
7124169689Skan}
7125169689Skan
7126132718Skan/* Returns nonzero if the TYPE contains a mutable member.  */
712752284Sobrien
7128132718Skanbool
7129132718Skancp_has_mutable_p (tree type)
713052284Sobrien{
713190075Sobrien  type = strip_array_types (type);
713252284Sobrien
713352284Sobrien  return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type);
713452284Sobrien}
713590075Sobrien
7136169689Skan/* Apply the TYPE_QUALS to the new DECL.  */
7137169689Skanvoid
7138169689Skancp_apply_type_quals_to_decl (int type_quals, tree decl)
7139169689Skan{
7140169689Skan  tree type = TREE_TYPE (decl);
7141169689Skan
7142169689Skan  if (type == error_mark_node)
7143169689Skan    return;
7144169689Skan
7145169689Skan  if (TREE_CODE (type) == FUNCTION_TYPE
7146169689Skan      && type_quals != TYPE_UNQUALIFIED)
7147169689Skan    {
7148169689Skan      /* This was an error in C++98 (cv-qualifiers cannot be added to
7149169689Skan	 a function type), but DR 295 makes the code well-formed by
7150169689Skan	 dropping the extra qualifiers. */
7151169689Skan      if (pedantic)
7152169689Skan	{
7153169689Skan	  tree bad_type = build_qualified_type (type, type_quals);
7154169689Skan	  pedwarn ("ignoring %qV qualifiers added to function type %qT",
7155169689Skan		   bad_type, type);
7156169689Skan	}
7157169689Skan
7158169689Skan      TREE_TYPE (decl) = TYPE_MAIN_VARIANT (type);
7159169689Skan      return;
7160169689Skan    }
7161169689Skan
7162169689Skan  /* Avoid setting TREE_READONLY incorrectly.  */
7163169689Skan  if (/* If the object has a constructor, the constructor may modify
7164169689Skan	 the object.  */
7165169689Skan      TYPE_NEEDS_CONSTRUCTING (type)
7166169689Skan      /* If the type isn't complete, we don't know yet if it will need
7167169689Skan	 constructing.  */
7168169689Skan      || !COMPLETE_TYPE_P (type)
7169169689Skan      /* If the type has a mutable component, that component might be
7170169689Skan	 modified.  */
7171169689Skan      || TYPE_HAS_MUTABLE_P (type))
7172169689Skan    type_quals &= ~TYPE_QUAL_CONST;
7173169689Skan
7174169689Skan  c_apply_type_quals_to_decl (type_quals, decl);
7175169689Skan}
7176169689Skan
717790075Sobrien/* Subroutine of casts_away_constness.  Make T1 and T2 point at
7178169689Skan   exemplar types such that casting T1 to T2 is casting away constness
717990075Sobrien   if and only if there is no implicit conversion from T1 to T2.  */
718090075Sobrien
718190075Sobrienstatic void
7182132718Skancasts_away_constness_r (tree *t1, tree *t2)
718390075Sobrien{
718490075Sobrien  int quals1;
718590075Sobrien  int quals2;
718690075Sobrien
718790075Sobrien  /* [expr.const.cast]
718890075Sobrien
718990075Sobrien     For multi-level pointer to members and multi-level mixed pointers
719090075Sobrien     and pointers to members (conv.qual), the "member" aspect of a
719190075Sobrien     pointer to member level is ignored when determining if a const
719290075Sobrien     cv-qualifier has been cast away.  */
719390075Sobrien  /* [expr.const.cast]
719490075Sobrien
719590075Sobrien     For  two  pointer types:
719690075Sobrien
7197169689Skan	    X1 is T1cv1,1 * ... cv1,N *   where T1 is not a pointer type
7198169689Skan	    X2 is T2cv2,1 * ... cv2,M *   where T2 is not a pointer type
7199169689Skan	    K is min(N,M)
720090075Sobrien
720190075Sobrien     casting from X1 to X2 casts away constness if, for a non-pointer
720290075Sobrien     type T there does not exist an implicit conversion (clause
720390075Sobrien     _conv_) from:
720490075Sobrien
7205169689Skan	    Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
7206169689Skan
720790075Sobrien     to
720890075Sobrien
7209169689Skan	    Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *.  */
7210161651Skan  if ((!TYPE_PTR_P (*t1) && !TYPE_PTRMEM_P (*t1))
7211161651Skan      || (!TYPE_PTR_P (*t2) && !TYPE_PTRMEM_P (*t2)))
721290075Sobrien    {
721390075Sobrien      *t1 = cp_build_qualified_type (void_type_node,
721490075Sobrien				     cp_type_quals (*t1));
721590075Sobrien      *t2 = cp_build_qualified_type (void_type_node,
721690075Sobrien				     cp_type_quals (*t2));
721790075Sobrien      return;
721890075Sobrien    }
7219169689Skan
722090075Sobrien  quals1 = cp_type_quals (*t1);
722190075Sobrien  quals2 = cp_type_quals (*t2);
7222161651Skan
7223161651Skan  if (TYPE_PTRMEM_P (*t1))
7224161651Skan    *t1 = TYPE_PTRMEM_POINTED_TO_TYPE (*t1);
7225161651Skan  else
7226161651Skan    *t1 = TREE_TYPE (*t1);
7227161651Skan  if (TYPE_PTRMEM_P (*t2))
7228161651Skan    *t2 = TYPE_PTRMEM_POINTED_TO_TYPE (*t2);
7229161651Skan  else
7230161651Skan    *t2 = TREE_TYPE (*t2);
7231161651Skan
723290075Sobrien  casts_away_constness_r (t1, t2);
723390075Sobrien  *t1 = build_pointer_type (*t1);
723490075Sobrien  *t2 = build_pointer_type (*t2);
723590075Sobrien  *t1 = cp_build_qualified_type (*t1, quals1);
723690075Sobrien  *t2 = cp_build_qualified_type (*t2, quals2);
723790075Sobrien}
723890075Sobrien
7239117395Skan/* Returns nonzero if casting from TYPE1 to TYPE2 casts away
724090075Sobrien   constness.  */
724190075Sobrien
7242132718Skanstatic bool
7243132718Skancasts_away_constness (tree t1, tree t2)
724490075Sobrien{
724590075Sobrien  if (TREE_CODE (t2) == REFERENCE_TYPE)
724690075Sobrien    {
724790075Sobrien      /* [expr.const.cast]
7248169689Skan
724990075Sobrien	 Casting from an lvalue of type T1 to an lvalue of type T2
725090075Sobrien	 using a reference cast casts away constness if a cast from an
725190075Sobrien	 rvalue of type "pointer to T1" to the type "pointer to T2"
725290075Sobrien	 casts away constness.  */
7253132718Skan      t1 = (TREE_CODE (t1) == REFERENCE_TYPE ? TREE_TYPE (t1) : t1);
725490075Sobrien      return casts_away_constness (build_pointer_type (t1),
725590075Sobrien				   build_pointer_type (TREE_TYPE (t2)));
725690075Sobrien    }
725790075Sobrien
725890075Sobrien  if (TYPE_PTRMEM_P (t1) && TYPE_PTRMEM_P (t2))
725990075Sobrien    /* [expr.const.cast]
7260169689Skan
726190075Sobrien       Casting from an rvalue of type "pointer to data member of X
726290075Sobrien       of type T1" to the type "pointer to data member of Y of type
726390075Sobrien       T2" casts away constness if a cast from an rvalue of type
726490075Sobrien       "pointer to T1" to the type "pointer to T2" casts away
726590075Sobrien       constness.  */
726690075Sobrien    return casts_away_constness
7267132718Skan      (build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t1)),
7268132718Skan       build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t2)));
726990075Sobrien
727090075Sobrien  /* Casting away constness is only something that makes sense for
727190075Sobrien     pointer or reference types.  */
7272169689Skan  if (TREE_CODE (t1) != POINTER_TYPE
727390075Sobrien      || TREE_CODE (t2) != POINTER_TYPE)
7274132718Skan    return false;
727590075Sobrien
727690075Sobrien  /* Top-level qualifiers don't matter.  */
727790075Sobrien  t1 = TYPE_MAIN_VARIANT (t1);
727890075Sobrien  t2 = TYPE_MAIN_VARIANT (t2);
727990075Sobrien  casts_away_constness_r (&t1, &t2);
728090075Sobrien  if (!can_convert (t2, t1))
7281132718Skan    return true;
728290075Sobrien
7283132718Skan  return false;
728490075Sobrien}
728590075Sobrien
7286132718Skan/* If T is a REFERENCE_TYPE return the type to which T refers.
7287132718Skan   Otherwise, return T itself.  */
728890075Sobrien
7289132718Skantree
7290132718Skannon_reference (tree t)
729190075Sobrien{
7292132718Skan  if (TREE_CODE (t) == REFERENCE_TYPE)
7293132718Skan    t = TREE_TYPE (t);
7294132718Skan  return t;
729590075Sobrien}
7296169689Skan
7297169689Skan
7298169689Skan/* Return nonzero if REF is an lvalue valid for this language;
7299169689Skan   otherwise, print an error message and return zero.  USE says
7300169689Skan   how the lvalue is being used and so selects the error message.  */
7301169689Skan
7302169689Skanint
7303169689Skanlvalue_or_else (tree ref, enum lvalue_use use)
7304169689Skan{
7305169689Skan  int win = lvalue_p (ref);
7306169689Skan
7307169689Skan  if (!win)
7308169689Skan    lvalue_error (use);
7309169689Skan
7310169689Skan  return win;
7311169689Skan}
7312