]> oss.titaniummirror.com Git - msp430-gcc.git/blobdiff - gcc/cp/typeck.c
Imported gcc-4.4.3
[msp430-gcc.git] / gcc / cp / typeck.c
index 82867eba0b3bce4740a2463052b39888cb8624de..1c0689eb36fd4571377fcc88bb0db58f28772c0c 100644 (file)
@@ -1,37 +1,35 @@
 /* Build expressions with type checking for C++ compiler.
    Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
-   1999, 2000, 2001, 2002 Free Software Foundation, Inc.
+   1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
+   Free Software Foundation, Inc.
    Hacked by Michael Tiemann (tiemann@cygnus.com)
 
-This file is part of GNU CC.
+This file is part of GCC.
 
-GNU CC is free software; you can redistribute it and/or modify
+GCC is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
-the Free Software Foundation; either version 2, or (at your option)
+the Free Software Foundation; either version 3, or (at your option)
 any later version.
 
-GNU CC is distributed in the hope that it will be useful,
+GCC is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
-along with GNU CC; see the file COPYING.  If not, write to
-the Free Software Foundation, 59 Temple Place - Suite 330,
-Boston, MA 02111-1307, USA.  */
+along with GCC; see the file COPYING3.  If not see
+<http://www.gnu.org/licenses/>.  */
 
 
 /* This file is part of the C++ front end.
    It contains routines to build C++ expressions given their operands,
    including computing the types of the result, C and C++ specific error
-   checks, and some optimization.
-
-   There are also routines to build RETURN_STMT nodes and CASE_STMT nodes,
-   and to process initializations in declarations (since they work
-   like a strange sort of assignment).  */
+   checks, and some optimization.  */
 
 #include "config.h"
 #include "system.h"
+#include "coretypes.h"
+#include "tm.h"
 #include "tree.h"
 #include "rtl.h"
 #include "expr.h"
@@ -41,48 +39,30 @@ Boston, MA 02111-1307, USA.  */
 #include "output.h"
 #include "toplev.h"
 #include "diagnostic.h"
+#include "intl.h"
 #include "target.h"
-
-static tree convert_for_assignment PARAMS ((tree, tree, const char *, tree,
-                                         int));
-static tree cp_pointer_int_sum PARAMS ((enum tree_code, tree, tree));
-static tree rationalize_conditional_expr PARAMS ((enum tree_code, tree));
-static int comp_target_parms PARAMS ((tree, tree));
-static int comp_ptr_ttypes_real PARAMS ((tree, tree, int));
-static int comp_ptr_ttypes_const PARAMS ((tree, tree));
-static int comp_ptr_ttypes_reinterpret PARAMS ((tree, tree));
-static int comp_except_types PARAMS ((tree, tree, int));
-static int comp_array_types PARAMS ((int (*) (tree, tree, int), tree,
-                                  tree, int));
-static tree common_base_type PARAMS ((tree, tree));
-static tree lookup_anon_field PARAMS ((tree, tree));
-static tree pointer_diff PARAMS ((tree, tree, tree));
-static tree build_component_addr PARAMS ((tree, tree));
-static tree qualify_type_recursive PARAMS ((tree, tree));
-static tree get_delta_difference PARAMS ((tree, tree, int));
-static int comp_cv_target_types PARAMS ((tree, tree, int));
-static void casts_away_constness_r PARAMS ((tree *, tree *));
-static int casts_away_constness PARAMS ((tree, tree));
-static void maybe_warn_about_returning_address_of_local PARAMS ((tree));
-static tree strip_all_pointer_quals PARAMS ((tree));
-
-/* Return the target type of TYPE, which means return T for:
-   T*, T&, T[], T (...), and otherwise, just T.  */
-
-tree
-target_type (type)
-     tree type;
-{
-  if (TREE_CODE (type) == REFERENCE_TYPE)
-    type = TREE_TYPE (type);
-  while (TREE_CODE (type) == POINTER_TYPE
-        || TREE_CODE (type) == ARRAY_TYPE
-        || TREE_CODE (type) == FUNCTION_TYPE
-        || TREE_CODE (type) == METHOD_TYPE
-        || TREE_CODE (type) == OFFSET_TYPE)
-    type = TREE_TYPE (type);
-  return type;
-}
+#include "convert.h"
+#include "c-common.h"
+#include "params.h"
+
+static tree pfn_from_ptrmemfunc (tree);
+static tree delta_from_ptrmemfunc (tree);
+static tree convert_for_assignment (tree, tree, const char *, tree, int,
+                                   tsubst_flags_t);
+static tree cp_pointer_int_sum (enum tree_code, tree, tree);
+static tree rationalize_conditional_expr (enum tree_code, tree, 
+                                         tsubst_flags_t);
+static int comp_ptr_ttypes_real (tree, tree, int);
+static bool comp_except_types (tree, tree, bool);
+static bool comp_array_types (const_tree, const_tree, bool);
+static tree pointer_diff (tree, tree, tree);
+static tree get_delta_difference (tree, tree, bool, bool);
+static void casts_away_constness_r (tree *, tree *);
+static bool casts_away_constness (tree, tree);
+static void maybe_warn_about_returning_address_of_local (tree);
+static tree lookup_destructor (tree, tree, tree);
+static int convert_arguments (int, tree *, tree, tree, tree, int,
+                              tsubst_flags_t);
 
 /* Do `exp = require_complete_type (exp);' to make sure exp
    does not have an incomplete type.  (That includes void types.)
@@ -90,8 +70,7 @@ target_type (type)
    complete type when this function returns.  */
 
 tree
-require_complete_type (value)
-     tree value;
+require_complete_type (tree value)
 {
   tree type;
 
@@ -103,21 +82,13 @@ require_complete_type (value)
   else
     type = TREE_TYPE (value);
 
+  if (type == error_mark_node)
+    return error_mark_node;
+
   /* First, detect a valid value with a complete type.  */
   if (COMPLETE_TYPE_P (type))
     return value;
 
-  /* If we see X::Y, we build an OFFSET_TYPE which has
-     not been laid out.  Try to avoid an error by interpreting
-     it as this->X::Y, if reasonable.  */
-  if (TREE_CODE (value) == OFFSET_REF
-      && current_class_ref != 0
-      && TREE_OPERAND (value, 0) == current_class_ref)
-    {
-      value = resolve_offset_ref (value);
-      return require_complete_type (value);
-    }
-
   if (complete_type_or_else (type, value))
     return value;
   else
@@ -130,8 +101,7 @@ require_complete_type (value)
    horribly wrong, in which case the error_mark_node is returned.  */
 
 tree
-complete_type (type)
-     tree type;
+complete_type (tree type)
 {
   if (type == NULL_TREE)
     /* Rather than crash, we return something sure to cause an error
@@ -143,12 +113,18 @@ complete_type (type)
   else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
     {
       tree t = complete_type (TREE_TYPE (type));
-      if (COMPLETE_TYPE_P (t) && ! processing_template_decl)
+      unsigned int needs_constructing, has_nontrivial_dtor;
+      if (COMPLETE_TYPE_P (t) && !dependent_type_p (type))
        layout_type (type);
-      TYPE_NEEDS_CONSTRUCTING (type)
+      needs_constructing
        = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t));
-      TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
+      has_nontrivial_dtor
        = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t));
+      for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
+       {
+         TYPE_NEEDS_CONSTRUCTING (t) = needs_constructing;
+         TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = has_nontrivial_dtor;
+       }
     }
   else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INSTANTIATION (type))
     instantiate_class_template (TYPE_MAIN_VARIANT (type));
@@ -156,14 +132,12 @@ complete_type (type)
   return type;
 }
 
-/* Like complete_type, but issue an error if the TYPE cannot be
-   completed.  VALUE is used for informative diagnostics.
+/* Like complete_type, but issue an error if the TYPE cannot be completed.
+   VALUE is used for informative diagnostics.
    Returns NULL_TREE if the type cannot be made complete.  */
 
 tree
-complete_type_or_else (type, value)
-     tree type;
-     tree value;
+complete_type_or_else (tree type, tree value)
 {
   type = complete_type (type);
   if (type == error_mark_node)
@@ -171,7 +145,7 @@ complete_type_or_else (type, value)
     return NULL_TREE;
   else if (!COMPLETE_TYPE_P (type))
     {
-      incomplete_type_error (value, type);
+      cxx_incomplete_type_diagnostic (value, type, DK_ERROR);
       return NULL_TREE;
     }
   else
@@ -181,53 +155,12 @@ complete_type_or_else (type, value)
 /* Return truthvalue of whether type of EXP is instantiated.  */
 
 int
-type_unknown_p (exp)
-     tree exp;
+type_unknown_p (const_tree exp)
 {
-  return (TREE_CODE (exp) == OVERLOAD
-          || TREE_CODE (exp) == TREE_LIST
-         || TREE_TYPE (exp) == unknown_type_node
-         || (TREE_CODE (TREE_TYPE (exp)) == OFFSET_TYPE
-             && TREE_TYPE (TREE_TYPE (exp)) == unknown_type_node));
+  return (TREE_CODE (exp) == TREE_LIST
+         || TREE_TYPE (exp) == unknown_type_node);
 }
 
-/* Return a pointer or pointer to member type similar to T1, with a
-   cv-qualification signature that is the union of the cv-qualification
-   signatures of T1 and T2: [expr.rel], [expr.eq].  */
-
-static tree
-qualify_type_recursive (t1, t2)
-     tree t1, t2;
-{
-  if ((TYPE_PTR_P (t1) && TYPE_PTR_P (t2))
-      || (TYPE_PTRMEM_P (t1) && TYPE_PTRMEM_P (t2)))
-    {
-      tree tt1 = TREE_TYPE (t1);
-      tree tt2 = TREE_TYPE (t2);
-      tree b1;
-      int type_quals;
-      tree tgt;
-      tree attributes = (*targetm.merge_type_attributes) (t1, t2);
-
-      if (TREE_CODE (tt1) == OFFSET_TYPE)
-       {
-         b1 = TYPE_OFFSET_BASETYPE (tt1);
-         tt1 = TREE_TYPE (tt1);
-         tt2 = TREE_TYPE (tt2);
-       }
-      else
-       b1 = NULL_TREE;
-
-      type_quals = (cp_type_quals (tt1) | cp_type_quals (tt2));
-      tgt = qualify_type_recursive (tt1, tt2);
-      tgt = cp_build_qualified_type (tgt, type_quals);
-      if (b1)
-       tgt = build_offset_type (b1, tgt);
-      t1 = build_pointer_type (tgt);
-      t1 = build_type_attribute_variant (t1, attributes);
-    }
-  return t1;
-}
 \f
 /* Return the common type of two parameter lists.
    We assume that comptypes has already been done and returned 1;
@@ -236,9 +169,8 @@ qualify_type_recursive (t1, t2)
    As an optimization, free the space we allocate if the parameter
    lists are already common.  */
 
-tree
-commonparms (p1, p2)
-     tree p1, p2;
+static tree
+commonparms (tree p1, tree p2)
 {
   tree oldargs = p1, newargs, n;
   int i, len;
@@ -298,11 +230,12 @@ commonparms (p1, p2)
 
 /* Given a type, perhaps copied for a typedef,
    find the "original" version of it.  */
-tree
-original_type (t)
-     tree t;
+static tree
+original_type (tree t)
 {
-  while (TYPE_NAME (t) != NULL_TREE)
+  int quals = cp_type_quals (t);
+  while (t != error_mark_node
+        && TYPE_NAME (t) != NULL_TREE)
     {
       tree x = TYPE_NAME (t);
       if (TREE_CODE (x) != TYPE_DECL)
@@ -312,37 +245,42 @@ original_type (t)
        break;
       t = x;
     }
-  return t;
+  return cp_build_qualified_type (t, quals);
 }
 
-/* T1 and T2 are arithmetic or enumeration types.  Return the type
-   that will result from the "usual arithmetic conversions" on T1 and
-   T2 as described in [expr].  */
+/* Return the common type for two arithmetic types T1 and T2 under the
+   usual arithmetic conversions.  The default conversions have already
+   been applied, and enumerated types converted to their compatible
+   integer types.  */
 
-tree
-type_after_usual_arithmetic_conversions (t1, t2)
-     tree t1;
-     tree t2;
+static tree
+cp_common_type (tree t1, tree t2)
 {
   enum tree_code code1 = TREE_CODE (t1);
   enum tree_code code2 = TREE_CODE (t2);
   tree attributes;
 
-  /* FIXME: Attributes.  */
-  my_friendly_assert (ARITHMETIC_TYPE_P (t1) 
-                     || TREE_CODE (t1) == COMPLEX_TYPE
-                     || TREE_CODE (t1) == ENUMERAL_TYPE,
-                     19990725);
-  my_friendly_assert (ARITHMETIC_TYPE_P (t2) 
-                     || TREE_CODE (t2) == COMPLEX_TYPE
-                     || TREE_CODE (t2) == ENUMERAL_TYPE,
-                     19990725);
-
   /* In what follows, we slightly generalize the rules given in [expr] so
      as to deal with `long long' and `complex'.  First, merge the
      attributes.  */
   attributes = (*targetm.merge_type_attributes) (t1, t2);
 
+  if (SCOPED_ENUM_P (t1) || SCOPED_ENUM_P (t2))
+    {
+      if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
+       return build_type_attribute_variant (t1, attributes);
+      else
+       return NULL_TREE;
+    }
+
+  /* FIXME: Attributes.  */
+  gcc_assert (ARITHMETIC_TYPE_P (t1)
+             || TREE_CODE (t1) == VECTOR_TYPE
+             || UNSCOPED_ENUM_P (t1));
+  gcc_assert (ARITHMETIC_TYPE_P (t2)
+             || TREE_CODE (t2) == VECTOR_TYPE
+             || UNSCOPED_ENUM_P (t2));
+
   /* If one type is complex, form the common type of the non-complex
      components, then make that complex.  Use T1 or T2 if it is the
      required type.  */
@@ -362,19 +300,22 @@ type_after_usual_arithmetic_conversions (t1, t2)
                                             attributes);
     }
 
+  if (code1 == VECTOR_TYPE)
+    {
+      /* When we get here we should have two vectors of the same size.
+        Just prefer the unsigned one if present.  */
+      if (TYPE_UNSIGNED (t1))
+       return build_type_attribute_variant (t1, attributes);
+      else
+       return build_type_attribute_variant (t2, attributes);
+    }
+
   /* If only one is real, use it as the result.  */
   if (code1 == REAL_TYPE && code2 != REAL_TYPE)
     return build_type_attribute_variant (t1, attributes);
   if (code2 == REAL_TYPE && code1 != REAL_TYPE)
     return build_type_attribute_variant (t2, attributes);
 
-  /* Perform the integral promotions.  */
-  if (code1 != REAL_TYPE)
-    {
-      t1 = type_promotes_to (t1);
-      t2 = type_promotes_to (t2);
-    }
-
   /* Both real or both integers; use the one with greater precision.  */
   if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
     return build_type_attribute_variant (t1, attributes);
@@ -387,12 +328,6 @@ type_after_usual_arithmetic_conversions (t1, t2)
 
   if (code1 != REAL_TYPE)
     {
-      /* If one is a sizetype, use it so size_binop doesn't blow up.  */
-      if (TYPE_IS_SIZETYPE (t1) > TYPE_IS_SIZETYPE (t2))
-       return build_type_attribute_variant (t1, attributes);
-      if (TYPE_IS_SIZETYPE (t2) > TYPE_IS_SIZETYPE (t1))
-       return build_type_attribute_variant (t2, attributes);
-
       /* If one is unsigned long long, then convert the other to unsigned
         long long.  */
       if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_unsigned_type_node)
@@ -404,7 +339,7 @@ type_after_usual_arithmetic_conversions (t1, t2)
         convert to a long long.  Otherwise, convert to an unsigned long
         long.  Otherwise, if either operand is long long, convert the
         other to long long.
-        
+
         Since we're here, we know the TYPE_PRECISION is the same;
         therefore converting to long long cannot represent all the values
         of an unsigned long, so we choose unsigned long long in that
@@ -412,12 +347,12 @@ type_after_usual_arithmetic_conversions (t1, t2)
       if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_integer_type_node)
          || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_integer_type_node))
        {
-         tree t = ((TREE_UNSIGNED (t1) || TREE_UNSIGNED (t2))
-                   ? long_long_unsigned_type_node 
+         tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
+                   ? long_long_unsigned_type_node
                    : long_long_integer_type_node);
          return build_type_attribute_variant (t, attributes);
        }
-      
+
       /* Go through the same procedure, but for longs.  */
       if (same_type_p (TYPE_MAIN_VARIANT (t1), long_unsigned_type_node)
          || same_type_p (TYPE_MAIN_VARIANT (t2), long_unsigned_type_node))
@@ -426,12 +361,12 @@ type_after_usual_arithmetic_conversions (t1, t2)
       if (same_type_p (TYPE_MAIN_VARIANT (t1), long_integer_type_node)
          || same_type_p (TYPE_MAIN_VARIANT (t2), long_integer_type_node))
        {
-         tree t = ((TREE_UNSIGNED (t1) || TREE_UNSIGNED (t2))
+         tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
                    ? long_unsigned_type_node : long_integer_type_node);
          return build_type_attribute_variant (t, attributes);
        }
       /* Otherwise prefer the unsigned one.  */
-      if (TREE_UNSIGNED (t1))
+      if (TYPE_UNSIGNED (t1))
        return build_type_attribute_variant (t1, attributes);
       else
        return build_type_attribute_variant (t2, attributes);
@@ -450,31 +385,126 @@ type_after_usual_arithmetic_conversions (t1, t2)
          || same_type_p (TYPE_MAIN_VARIANT (t2), float_type_node))
        return build_type_attribute_variant (float_type_node,
                                             attributes);
-      
+
       /* Two floating-point types whose TYPE_MAIN_VARIANTs are none of
-         the standard C++ floating-point types.  Logic earlier in this
-         function has already eliminated the possibility that
-         TYPE_PRECISION (t2) != TYPE_PRECISION (t1), so there's no
-         compelling reason to choose one or the other.  */
+        the standard C++ floating-point types.  Logic earlier in this
+        function has already eliminated the possibility that
+        TYPE_PRECISION (t2) != TYPE_PRECISION (t1), so there's no
+        compelling reason to choose one or the other.  */
       return build_type_attribute_variant (t1, attributes);
     }
 }
 
-/* Return the composite pointer type (see [expr.rel]) for T1 and T2.
-   ARG1 and ARG2 are the values with those types.  The LOCATION is a
-   string describing the current location, in case an error occurs.  */
+/* T1 and T2 are arithmetic or enumeration types.  Return the type
+   that will result from the "usual arithmetic conversions" on T1 and
+   T2 as described in [expr].  */
 
-tree 
-composite_pointer_type (t1, t2, arg1, arg2, location)
-     tree t1;
-     tree t2;
-     tree arg1;
-     tree arg2;
-     const char* location;
+tree
+type_after_usual_arithmetic_conversions (tree t1, tree t2)
+{
+  gcc_assert (ARITHMETIC_TYPE_P (t1)
+             || TREE_CODE (t1) == VECTOR_TYPE
+             || UNSCOPED_ENUM_P (t1));
+  gcc_assert (ARITHMETIC_TYPE_P (t2)
+             || TREE_CODE (t2) == VECTOR_TYPE
+             || UNSCOPED_ENUM_P (t2));
+
+  /* Perform the integral promotions.  We do not promote real types here.  */
+  if (INTEGRAL_OR_ENUMERATION_TYPE_P (t1)
+      && INTEGRAL_OR_ENUMERATION_TYPE_P (t2))
+    {
+      t1 = type_promotes_to (t1);
+      t2 = type_promotes_to (t2);
+    }
+
+  return cp_common_type (t1, t2);
+}
+
+/* Subroutine of composite_pointer_type to implement the recursive
+   case.  See that function for documentation fo the parameters.  */
+
+static tree
+composite_pointer_type_r (tree t1, tree t2, const char* location,
+                         tsubst_flags_t complain)
 {
+  tree pointee1;
+  tree pointee2;
   tree result_type;
   tree attributes;
 
+  /* Determine the types pointed to by T1 and T2.  */
+  if (TREE_CODE (t1) == POINTER_TYPE)
+    {
+      pointee1 = TREE_TYPE (t1);
+      pointee2 = TREE_TYPE (t2);
+    }
+  else
+    {
+      pointee1 = TYPE_PTRMEM_POINTED_TO_TYPE (t1);
+      pointee2 = TYPE_PTRMEM_POINTED_TO_TYPE (t2);
+    }
+
+  /* [expr.rel]
+
+     Otherwise, the composite pointer type is a pointer type
+     similar (_conv.qual_) to the type of one of the operands,
+     with a cv-qualification signature (_conv.qual_) that is the
+     union of the cv-qualification signatures of the operand
+     types.  */
+  if (same_type_ignoring_top_level_qualifiers_p (pointee1, pointee2))
+    result_type = pointee1;
+  else if ((TREE_CODE (pointee1) == POINTER_TYPE
+           && TREE_CODE (pointee2) == POINTER_TYPE)
+          || (TYPE_PTR_TO_MEMBER_P (pointee1)
+              && TYPE_PTR_TO_MEMBER_P (pointee2)))
+    result_type = composite_pointer_type_r (pointee1, pointee2, location,
+                                           complain);
+  else
+    {
+      if (complain & tf_error)
+       permerror (input_location, "%s between distinct pointer types %qT and %qT "
+                  "lacks a cast",
+                  location, t1, t2);
+      result_type = void_type_node;
+    }
+  result_type = cp_build_qualified_type (result_type,
+                                        (cp_type_quals (pointee1)
+                                         | cp_type_quals (pointee2)));
+  /* If the original types were pointers to members, so is the
+     result.  */
+  if (TYPE_PTR_TO_MEMBER_P (t1))
+    {
+      if (!same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
+                       TYPE_PTRMEM_CLASS_TYPE (t2))
+         && (complain & tf_error))
+       permerror (input_location, "%s between distinct pointer types %qT and %qT "
+                  "lacks a cast",
+                  location, t1, t2);
+      result_type = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
+                                      result_type);
+    }
+  else
+    result_type = build_pointer_type (result_type);
+
+  /* Merge the attributes.  */
+  attributes = (*targetm.merge_type_attributes) (t1, t2);
+  return build_type_attribute_variant (result_type, attributes);
+}
+
+/* Return the composite pointer type (see [expr.rel]) for T1 and T2.
+   ARG1 and ARG2 are the values with those types.  The LOCATION is a
+   string describing the current location, in case an error occurs.
+
+   This routine also implements the computation of a common type for
+   pointers-to-members as per [expr.eq].  */
+
+tree
+composite_pointer_type (tree t1, tree t2, tree arg1, tree arg2,
+                       const char* location, tsubst_flags_t complain)
+{
+  tree class1;
+  tree class2;
+
   /* [expr.rel]
 
      If one operand is a null pointer constant, the composite pointer
@@ -483,16 +513,6 @@ composite_pointer_type (t1, t2, arg1, arg2, location)
     return t2;
   if (null_ptr_cst_p (arg2))
     return t1;
-  /* Deal with pointer-to-member functions in the same way as we deal
-     with pointers to functions. */
-  if (TYPE_PTRMEMFUNC_P (t1))
-    t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
-  if (TYPE_PTRMEMFUNC_P (t2))
-    t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
-  
-  /* Merge the attributes.  */
-  attributes = (*targetm.merge_type_attributes) (t1, t2);
 
   /* We have:
 
@@ -504,45 +524,89 @@ composite_pointer_type (t1, t2, arg1, arg2, location)
        and cv2.
 
     If either type is a pointer to void, make sure it is T1.  */
-  if (VOID_TYPE_P (TREE_TYPE (t2)))
+  if (TREE_CODE (t2) == POINTER_TYPE && VOID_TYPE_P (TREE_TYPE (t2)))
     {
       tree t;
       t = t1;
       t1 = t2;
       t2 = t;
     }
+
   /* Now, if T1 is a pointer to void, merge the qualifiers.  */
-  if (VOID_TYPE_P (TREE_TYPE (t1)))
-    {
-      if (pedantic && TYPE_PTRFN_P (t2))
-       pedwarn ("ISO C++ forbids %s between pointer of type `void *' and pointer-to-function", location);
-      t1 = TREE_TYPE (t1);
-      t2 = TREE_TYPE (t2);
-      result_type = cp_build_qualified_type (void_type_node,
-                                            (cp_type_quals (t1)
-                                             | cp_type_quals (t2)));
+  if (TREE_CODE (t1) == POINTER_TYPE && VOID_TYPE_P (TREE_TYPE (t1)))
+    {
+      tree attributes;
+      tree result_type;
+
+      if (TYPE_PTRFN_P (t2) && (complain & tf_error))
+       pedwarn (input_location, OPT_pedantic, "ISO C++ forbids %s "
+                "between pointer of type %<void *%> and pointer-to-function",
+                location);
+      result_type
+       = cp_build_qualified_type (void_type_node,
+                                  (cp_type_quals (TREE_TYPE (t1))
+                                   | cp_type_quals (TREE_TYPE (t2))));
       result_type = build_pointer_type (result_type);
+      /* Merge the attributes.  */
+      attributes = (*targetm.merge_type_attributes) (t1, t2);
+      return build_type_attribute_variant (result_type, attributes);
     }
-  else
+
+  if (c_dialect_objc () && TREE_CODE (t1) == POINTER_TYPE
+      && TREE_CODE (t2) == POINTER_TYPE)
+    {
+      if (objc_compare_types (t1, t2, -3, NULL_TREE))
+       return t1;
+    }
+
+  /* [expr.eq] permits the application of a pointer conversion to
+     bring the pointers to a common type.  */
+  if (TREE_CODE (t1) == POINTER_TYPE && TREE_CODE (t2) == POINTER_TYPE
+      && CLASS_TYPE_P (TREE_TYPE (t1))
+      && CLASS_TYPE_P (TREE_TYPE (t2))
+      && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (t1),
+                                                    TREE_TYPE (t2)))
     {
-      tree full1 = qualify_type_recursive (t1, t2);
-      tree full2 = qualify_type_recursive (t2, t1);
+      class1 = TREE_TYPE (t1);
+      class2 = TREE_TYPE (t2);
 
-      int val = comp_target_types (full1, full2, 1);
+      if (DERIVED_FROM_P (class1, class2))
+       t2 = (build_pointer_type
+             (cp_build_qualified_type (class1, TYPE_QUALS (class2))));
+      else if (DERIVED_FROM_P (class2, class1))
+       t1 = (build_pointer_type
+             (cp_build_qualified_type (class2, TYPE_QUALS (class1))));
+      else
+       {
+         if (complain & tf_error)
+           error ("%s between distinct pointer types %qT and %qT "
+                  "lacks a cast", location, t1, t2);
+         return error_mark_node;
+       }
+    }
+  /* [expr.eq] permits the application of a pointer-to-member
+     conversion to change the class type of one of the types.  */
+  else if (TYPE_PTR_TO_MEMBER_P (t1)
+          && !same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
+                           TYPE_PTRMEM_CLASS_TYPE (t2)))
+    {
+      class1 = TYPE_PTRMEM_CLASS_TYPE (t1);
+      class2 = TYPE_PTRMEM_CLASS_TYPE (t2);
 
-      if (val > 0)
-       result_type = full1;
-      else if (val < 0)
-       result_type = full2;
+      if (DERIVED_FROM_P (class1, class2))
+       t1 = build_ptrmem_type (class2, TYPE_PTRMEM_POINTED_TO_TYPE (t1));
+      else if (DERIVED_FROM_P (class2, class1))
+       t2 = build_ptrmem_type (class1, TYPE_PTRMEM_POINTED_TO_TYPE (t2));
       else
        {
-         pedwarn ("%s between distinct pointer types `%T' and `%T' lacks a cast",
-                     location, t1, t2);
-         result_type = ptr_type_node;
+         if (complain & tf_error)
+           error ("%s between distinct pointer-to-member types %qT and %qT "
+                  "lacks a cast", location, t1, t2);
+         return error_mark_node;
        }
     }
 
-  return build_type_attribute_variant (result_type, attributes);
+  return composite_pointer_type_r (t1, t2, location, complain);
 }
 
 /* Return the merged type of two types.
@@ -553,11 +617,10 @@ composite_pointer_type (t1, t2, arg1, arg2, location)
    differences would cause the two types to compare unalike.  */
 
 tree
-merge_types (t1, t2)
-     tree t1, t2;
+merge_types (tree t1, tree t2)
 {
-  register enum tree_code code1;
-  register enum tree_code code2;
+  enum tree_code code1;
+  enum tree_code code2;
   tree attributes;
 
   /* Save time if the two types are the same.  */
@@ -575,8 +638,6 @@ merge_types (t1, t2)
   /* Merge the attributes.  */
   attributes = (*targetm.merge_type_attributes) (t1, t2);
 
-  /* Treat an enum type as the unsigned integer type of the same width.  */
-
   if (TYPE_PTRMEMFUNC_P (t1))
     t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
   if (TYPE_PTRMEMFUNC_P (t2))
@@ -584,6 +645,20 @@ merge_types (t1, t2)
 
   code1 = TREE_CODE (t1);
   code2 = TREE_CODE (t2);
+  if (code1 != code2)
+    {
+      gcc_assert (code1 == TYPENAME_TYPE || code2 == TYPENAME_TYPE);
+      if (code1 == TYPENAME_TYPE)
+       {
+          t1 = resolve_typename_type (t1, /*only_current_p=*/true);
+         code1 = TREE_CODE (t1);
+       }
+      else
+       {
+          t2 = resolve_typename_type (t2, /*only_current_p=*/true);
+         code2 = TREE_CODE (t2);
+       }
+    }
 
   switch (code1)
     {
@@ -597,7 +672,7 @@ merge_types (t1, t2)
        if (code1 == POINTER_TYPE)
          t1 = build_pointer_type (target);
        else
-         t1 = build_reference_type (target);
+         t1 = cp_build_reference_type (target, TYPE_REF_IS_RVALUE (t1));
        t1 = build_type_attribute_variant (t1, attributes);
        t1 = cp_build_qualified_type (t1, quals);
 
@@ -609,9 +684,14 @@ merge_types (t1, t2)
 
     case OFFSET_TYPE:
       {
-       tree base = TYPE_OFFSET_BASETYPE (t1);
-       tree target = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
-       t1 = build_offset_type (base, target);
+       int quals;
+       tree pointee;
+       quals = cp_type_quals (t1);
+       pointee = merge_types (TYPE_PTRMEM_POINTED_TO_TYPE (t1),
+                              TYPE_PTRMEM_POINTED_TO_TYPE (t2));
+       t1 = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
+                               pointee);
+       t1 = cp_build_qualified_type (t1, quals);
        break;
       }
 
@@ -640,9 +720,9 @@ merge_types (t1, t2)
 
        /* Save space: see if the result is identical to one of the args.  */
        if (valtype == TREE_TYPE (t1) && ! p2)
-         return build_type_attribute_variant (t1, attributes);
+         return cp_build_type_attribute_variant (t1, attributes);
        if (valtype == TREE_TYPE (t2) && ! p1)
-         return build_type_attribute_variant (t2, attributes);
+         return cp_build_type_attribute_variant (t2, attributes);
 
        /* Simple way if one arg fails to specify argument types.  */
        if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node)
@@ -650,7 +730,7 @@ merge_types (t1, t2)
            rval = build_function_type (valtype, p2);
            if ((raises = TYPE_RAISES_EXCEPTIONS (t2)))
              rval = build_exception_variant (rval, raises);
-           return build_type_attribute_variant (rval, attributes);
+           return cp_build_type_attribute_variant (rval, attributes);
          }
        raises = TYPE_RAISES_EXCEPTIONS (t1);
        if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node)
@@ -658,7 +738,7 @@ merge_types (t1, t2)
            rval = build_function_type (valtype, p1);
            if (raises)
              rval = build_exception_variant (rval, raises);
-           return build_type_attribute_variant (rval, attributes);
+           return cp_build_type_attribute_variant (rval, attributes);
          }
 
        rval = build_function_type (valtype, commonparms (p1, p2));
@@ -682,127 +762,137 @@ merge_types (t1, t2)
        t2 = build_function_type (TREE_TYPE (t2),
                                  TREE_CHAIN (TYPE_ARG_TYPES (t2)));
        t3 = merge_types (t1, t2);
-       t3 = build_cplus_method_type (basetype, TREE_TYPE (t3),
-                                     TYPE_ARG_TYPES (t3));
+       t3 = build_method_type_directly (basetype, TREE_TYPE (t3),
+                                        TYPE_ARG_TYPES (t3));
        t1 = build_exception_variant (t3, raises);
        break;
       }
 
+    case TYPENAME_TYPE:
+      /* There is no need to merge attributes into a TYPENAME_TYPE.
+        When the type is instantiated it will have whatever
+        attributes result from the instantiation.  */
+      return t1;
+
     default:;
     }
-  return build_type_attribute_variant (t1, attributes);
+
+  if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
+    return t1;
+  else if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
+    return t2;
+  else
+    return cp_build_type_attribute_variant (t1, attributes);
 }
 
-/* Return the common type of two types.
-   We assume that comptypes has already been done and returned 1;
-   if that isn't so, this may crash.
+/* Wrapper around cp_common_type that is used by c-common.c and other
+   front end optimizations that remove promotions.  
 
-   This is the type for the result of most arithmetic operations
-   if the operands have the given two types.  */
+   Return the common type for two arithmetic types T1 and T2 under the
+   usual arithmetic conversions.  The default conversions have already
+   been applied, and enumerated types converted to their compatible
+   integer types.  */
 
 tree
-common_type (t1, t2)
-     tree t1, t2;
+common_type (tree t1, tree t2)
 {
-  enum tree_code code1;
-  enum tree_code code2;
-
-  /* If one type is nonsense, bail.  */
-  if (t1 == error_mark_node || t2 == error_mark_node)
-    return error_mark_node;
+  /* If one type is nonsense, use the other  */
+  if (t1 == error_mark_node)
+    return t2;
+  if (t2 == error_mark_node)
+    return t1;
 
-  code1 = TREE_CODE (t1);
-  code2 = TREE_CODE (t2);
+  return cp_common_type (t1, t2);
+}
 
-  if ((ARITHMETIC_TYPE_P (t1) || code1 == ENUMERAL_TYPE
-       || code1 == COMPLEX_TYPE)
-      && (ARITHMETIC_TYPE_P (t2) || code2 == ENUMERAL_TYPE
-         || code2 == COMPLEX_TYPE))
-    return type_after_usual_arithmetic_conversions (t1, t2);
+/* Return the common type of two pointer types T1 and T2.  This is the
+   type for the result of most arithmetic operations if the operands
+   have the given two types.
+   We assume that comp_target_types has already been done and returned
+   nonzero; if that isn't so, this may crash.  */
 
-  else if ((TYPE_PTR_P (t1) && TYPE_PTR_P (t2))
-          || (TYPE_PTRMEM_P (t1) && TYPE_PTRMEM_P (t2))
-          || (TYPE_PTRMEMFUNC_P (t1) && TYPE_PTRMEMFUNC_P (t2)))
-    return composite_pointer_type (t1, t2, error_mark_node, error_mark_node,
-                                  "conversion");
+tree
+common_pointer_type (tree t1, tree t2)
+{
+  gcc_assert ((TYPE_PTR_P (t1) && TYPE_PTR_P (t2))
+              || (TYPE_PTRMEM_P (t1) && TYPE_PTRMEM_P (t2))
+              || (TYPE_PTRMEMFUNC_P (t1) && TYPE_PTRMEMFUNC_P (t2)));
 
-  else
-    abort ();
+  return composite_pointer_type (t1, t2, error_mark_node, error_mark_node,
+                                 "conversion", tf_warning_or_error);
 }
 \f
 /* Compare two exception specifier types for exactness or subsetness, if
-   allowed. Returns 0 for mismatch, 1 for same, 2 if B is allowed by A.
+   allowed. Returns false for mismatch, true for match (same, or
+   derived and !exact).
+
    [except.spec] "If a class X ... objects of class X or any class publicly
-   and unambigously derrived from X. Similarly, if a pointer type Y * ...
+   and unambiguously derived from X. Similarly, if a pointer type Y * ...
    exceptions of type Y * or that are pointers to any type publicly and
-   unambigously derrived from Y. Otherwise a function only allows exceptions
+   unambiguously derived from Y. Otherwise a function only allows exceptions
    that have the same type ..."
    This does not mention cv qualifiers and is different to what throw
    [except.throw] and catch [except.catch] will do. They will ignore the
    top level cv qualifiers, and allow qualifiers in the pointer to class
    example.
-   
+
    We implement the letter of the standard.  */
 
-static int
-comp_except_types (a, b, exact)
-     tree a, b;
-     int exact;
+static bool
+comp_except_types (tree a, tree b, bool exact)
 {
   if (same_type_p (a, b))
-    return 1;
+    return true;
   else if (!exact)
     {
       if (cp_type_quals (a) || cp_type_quals (b))
-        return 0;
-      
+       return false;
+
       if (TREE_CODE (a) == POINTER_TYPE
-          && TREE_CODE (b) == POINTER_TYPE)
-        {
-          a = TREE_TYPE (a);
-          b = TREE_TYPE (b);
-          if (cp_type_quals (a) || cp_type_quals (b))
-            return 0;
-        }
-      
+         && TREE_CODE (b) == POINTER_TYPE)
+       {
+         a = TREE_TYPE (a);
+         b = TREE_TYPE (b);
+         if (cp_type_quals (a) || cp_type_quals (b))
+           return false;
+       }
+
       if (TREE_CODE (a) != RECORD_TYPE
-          || TREE_CODE (b) != RECORD_TYPE)
-        return 0;
-      
-      if (ACCESSIBLY_UNIQUELY_DERIVED_P (a, b))
-        return 2;
+         || TREE_CODE (b) != RECORD_TYPE)
+       return false;
+
+      if (PUBLICLY_UNIQUELY_DERIVED_P (a, b))
+       return true;
     }
-  return 0;
+  return false;
 }
 
-/* Return 1 if TYPE1 and TYPE2 are equivalent exception specifiers.
-   If EXACT is 0, T2 can be a subset of T1 (according to 15.4/7),
+/* Return true if TYPE1 and TYPE2 are equivalent exception specifiers.
+   If EXACT is false, T2 can be stricter than T1 (according to 15.4/7),
    otherwise it must be exact. Exception lists are unordered, but
    we've already filtered out duplicates. Most lists will be in order,
    we should try to make use of that.  */
 
-int
-comp_except_specs (t1, t2, exact)
-     tree t1, t2;
-     int exact;
+bool
+comp_except_specs (const_tree t1, const_tree t2, bool exact)
 {
-  tree probe;
-  tree base;
+  const_tree probe;
+  const_tree base;
   int  length = 0;
 
   if (t1 == t2)
-    return 1;
-  
-  if (t1 == NULL_TREE)              /* T1 is ... */
+    return true;
+
+  if (t1 == NULL_TREE)                    /* T1 is ...  */
     return t2 == NULL_TREE || !exact;
-  if (!TREE_VALUE (t1)) /* t1 is EMPTY */
+  if (!TREE_VALUE (t1))                           /* t1 is EMPTY */
     return t2 != NULL_TREE && !TREE_VALUE (t2);
-  if (t2 == NULL_TREE)              /* T2 is ... */
-    return 0;
-  if (TREE_VALUE(t1) && !TREE_VALUE (t2)) /* T2 is EMPTY, T1 is not */
+  if (t2 == NULL_TREE)                    /* T2 is ...  */
+    return false;
+  if (TREE_VALUE (t1) && !TREE_VALUE (t2)) /* T2 is EMPTY, T1 is not */
     return !exact;
-  
+
   /* Neither set is ... or EMPTY, make sure each part of T2 is in T1.
      Count how many we find, to determine exactness. For exact matching and
      ordered T1, T2, this is an O(n) operation, otherwise its worst case is
@@ -810,127 +900,107 @@ comp_except_specs (t1, t2, exact)
   for (base = t1; t2 != NULL_TREE; t2 = TREE_CHAIN (t2))
     {
       for (probe = base; probe != NULL_TREE; probe = TREE_CHAIN (probe))
-        {
-          tree a = TREE_VALUE (probe);
-          tree b = TREE_VALUE (t2);
-          
-          if (comp_except_types (a, b, exact))
-            {
-              if (probe == base && exact)
-                base = TREE_CHAIN (probe);
-              length++;
-              break;
-            }
-        }
+       {
+         tree a = TREE_VALUE (probe);
+         tree b = TREE_VALUE (t2);
+
+         if (comp_except_types (a, b, exact))
+           {
+             if (probe == base && exact)
+               base = TREE_CHAIN (probe);
+             length++;
+             break;
+           }
+       }
       if (probe == NULL_TREE)
-        return 0;
+       return false;
     }
   return !exact || base == NULL_TREE || length == list_length (t1);
 }
 
-/* Compare the array types T1 and T2, using CMP as the type comparison
-   function for the element types.  STRICT is as for comptypes.  */
+/* Compare the array types T1 and T2.  ALLOW_REDECLARATION is true if
+   [] can match [size].  */
 
-static int
-comp_array_types (cmp, t1, t2, strict)
-     register int (*cmp) PARAMS ((tree, tree, int));
-     tree t1, t2;
-     int strict;
+static bool
+comp_array_types (const_tree t1, const_tree t2, bool allow_redeclaration)
 {
   tree d1;
   tree d2;
+  tree max1, max2;
 
   if (t1 == t2)
-    return 1;
+    return true;
 
   /* The type of the array elements must be the same.  */
-  if (!(TREE_TYPE (t1) == TREE_TYPE (t2)
-       || (*cmp) (TREE_TYPE (t1), TREE_TYPE (t2), 
-                  strict & ~COMPARE_REDECLARATION)))
-    return 0;
+  if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
+    return false;
 
   d1 = TYPE_DOMAIN (t1);
   d2 = TYPE_DOMAIN (t2);
 
   if (d1 == d2)
-    return 1;
+    return true;
 
   /* If one of the arrays is dimensionless, and the other has a
-     dimension, they are of different types.  However, it is legal to
+     dimension, they are of different types.  However, it is valid to
      write:
 
        extern int a[];
        int a[3];
 
-     by [basic.link]: 
+     by [basic.link]:
 
        declarations for an array object can specify
        array types that differ by the presence or absence of a major
        array bound (_dcl.array_).  */
   if (!d1 || !d2)
-    return strict & COMPARE_REDECLARATION;
+    return allow_redeclaration;
 
   /* Check that the dimensions are the same.  */
-  return (cp_tree_equal (TYPE_MIN_VALUE (d1),
-                        TYPE_MIN_VALUE (d2))
-         && cp_tree_equal (TYPE_MAX_VALUE (d1),
-                           TYPE_MAX_VALUE (d2)));
-}
-
-/* Return 1 if T1 and T2 are compatible types for assignment or
-   various other operations.  STRICT is a bitwise-or of the COMPARE_*
-   flags.  */
 
-int
-comptypes (t1, t2, strict)
-     tree t1;
-     tree t2;
-     int strict;
-{
-  int attrval, val;
-  int orig_strict = strict;
+  if (!cp_tree_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2)))
+    return false;
+  max1 = TYPE_MAX_VALUE (d1);
+  max2 = TYPE_MAX_VALUE (d2);
+  if (processing_template_decl && !abi_version_at_least (2)
+      && !value_dependent_expression_p (max1)
+      && !value_dependent_expression_p (max2))
+    {
+      /* With abi-1 we do not fold non-dependent array bounds, (and
+        consequently mangle them incorrectly).  We must therefore
+        fold them here, to verify the domains have the same
+        value.  */
+      max1 = fold (max1);
+      max2 = fold (max2);
+    }
 
-  /* The special exemption for redeclaring array types without an
-     array bound only applies at the top level:
+  if (!cp_tree_equal (max1, max2))
+    return false;
 
-       extern int (*i)[];
-       int (*i)[8];
+  return true;
+}
 
-     is not legal, for example.  */
-  strict &= ~COMPARE_REDECLARATION;
+/* Subroutine in comptypes.  */
 
-  /* Suppress errors caused by previously reported errors */
+static bool
+structural_comptypes (tree t1, tree t2, int strict)
+{
   if (t1 == t2)
-    return 1;
-
-  /* This should never happen.  */
-  my_friendly_assert (t1 != error_mark_node, 307);
-
-  if (t2 == error_mark_node)
-    return 0;
-
-  /* If either type is the internal version of sizetype, return the
-     language version.  */
-  if (TREE_CODE (t1) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t1)
-      && TYPE_DOMAIN (t1) != 0)
-    t1 = TYPE_DOMAIN (t1);
+    return true;
 
-  if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2)
-      && TYPE_DOMAIN (t2) != 0)
-    t2 = TYPE_DOMAIN (t2);
+  /* Suppress errors caused by previously reported errors.  */
+  if (t1 == error_mark_node || t2 == error_mark_node)
+    return false;
 
-  if (strict & COMPARE_RELAXED)
-    {
-      /* Treat an enum type as the unsigned integer type of the same width.  */
+  gcc_assert (TYPE_P (t1) && TYPE_P (t2));
 
-      if (TREE_CODE (t1) == ENUMERAL_TYPE)
-       t1 = type_for_size (TYPE_PRECISION (t1), 1);
-      if (TREE_CODE (t2) == ENUMERAL_TYPE)
-       t2 = type_for_size (TYPE_PRECISION (t2), 1);
+  /* TYPENAME_TYPEs should be resolved if the qualifying scope is the
+     current instantiation.  */
+  if (TREE_CODE (t1) == TYPENAME_TYPE)
+    t1 = resolve_typename_type (t1, /*only_current_p=*/true);
 
-      if (t1 == t2)
-       return 1;
-    }
+  if (TREE_CODE (t2) == TYPENAME_TYPE)
+    t2 = resolve_typename_type (t2, /*only_current_p=*/true);
 
   if (TYPE_PTRMEMFUNC_P (t1))
     t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
@@ -939,366 +1009,254 @@ comptypes (t1, t2, strict)
 
   /* Different classes of types can't be compatible.  */
   if (TREE_CODE (t1) != TREE_CODE (t2))
-    return 0;
+    return false;
 
-  /* Qualifiers must match.  */
-  if (cp_type_quals (t1) != cp_type_quals (t2))
-    return 0;
-  if (strict == COMPARE_STRICT 
-      && TYPE_FOR_JAVA (t1) != TYPE_FOR_JAVA (t2))
-    return 0;
+  /* Qualifiers must match.  For array types, we will check when we
+     recur on the array element types.  */
+  if (TREE_CODE (t1) != ARRAY_TYPE
+      && TYPE_QUALS (t1) != TYPE_QUALS (t2))
+    return false;
+  if (TYPE_FOR_JAVA (t1) != TYPE_FOR_JAVA (t2))
+    return false;
 
   /* Allow for two different type nodes which have essentially the same
      definition.  Note that we already checked for equality of the type
      qualifiers (just above).  */
 
-  if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
-    return 1;
-
-  if (strict & COMPARE_NO_ATTRIBUTES)
-    attrval = 1;
-  /* 1 if no need for warning yet, 2 if warning cause has been seen.  */
-  else if (! (attrval = (*targetm.comp_type_attributes) (t1, t2)))
-     return 0;
-
-  /* 1 if no need for warning yet, 2 if warning cause has been seen.  */
-  val = 0;
+  if (TREE_CODE (t1) != ARRAY_TYPE
+      && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
+    return true;
 
+  /* Compare the types.  Break out if they could be the same.  */
   switch (TREE_CODE (t1))
     {
+    case VOID_TYPE:
+    case BOOLEAN_TYPE:
+      /* All void and bool types are the same.  */
+      break;
+
+    case INTEGER_TYPE:
+    case FIXED_POINT_TYPE:
+    case REAL_TYPE:
+      /* With these nodes, we can't determine type equivalence by
+        looking at what is stored in the nodes themselves, because
+        two nodes might have different TYPE_MAIN_VARIANTs but still
+        represent the same type.  For example, wchar_t and int could
+        have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
+        TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
+        and are distinct types. On the other hand, int and the
+        following typedef
+
+           typedef int INT __attribute((may_alias));
+
+        have identical properties, different TYPE_MAIN_VARIANTs, but
+        represent the same type.  The canonical type system keeps
+        track of equivalence in this case, so we fall back on it.  */
+      return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
+
     case TEMPLATE_TEMPLATE_PARM:
     case BOUND_TEMPLATE_TEMPLATE_PARM:
       if (TEMPLATE_TYPE_IDX (t1) != TEMPLATE_TYPE_IDX (t2)
-         || TEMPLATE_TYPE_LEVEL (t1) != TEMPLATE_TYPE_LEVEL (t2))
-       return 0;
-      if (! comp_template_parms
-             (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1)),
-              DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2))))
-       return 0;
+         || TEMPLATE_TYPE_LEVEL (t1) != TEMPLATE_TYPE_LEVEL (t2)
+          || (TEMPLATE_TYPE_PARAMETER_PACK (t1) 
+              != TEMPLATE_TYPE_PARAMETER_PACK (t2)))
+       return false;
+      if (!comp_template_parms
+         (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1)),
+          DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2))))
+       return false;
       if (TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM)
-       return 1;
+       break;
       /* Don't check inheritance.  */
       strict = COMPARE_STRICT;
-      /* fall through */
+      /* Fall through.  */
 
     case RECORD_TYPE:
     case UNION_TYPE:
       if (TYPE_TEMPLATE_INFO (t1) && TYPE_TEMPLATE_INFO (t2)
          && (TYPE_TI_TEMPLATE (t1) == TYPE_TI_TEMPLATE (t2)
-             || TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM))
-       val = comp_template_args (TYPE_TI_ARGS (t1),
-                                 TYPE_TI_ARGS (t2));
-    look_hard:
+             || TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM)
+         && comp_template_args (TYPE_TI_ARGS (t1), TYPE_TI_ARGS (t2)))
+       break;
+
       if ((strict & COMPARE_BASE) && DERIVED_FROM_P (t1, t2))
-       val = 1;
-      else if ((strict & COMPARE_RELAXED) && DERIVED_FROM_P (t2, t1))
-       val = 1;
-      break;
+       break;
+      else if ((strict & COMPARE_DERIVED) && DERIVED_FROM_P (t2, t1))
+       break;
+
+      return false;
 
     case OFFSET_TYPE:
-      val = (comptypes (build_pointer_type (TYPE_OFFSET_BASETYPE (t1)),
-                       build_pointer_type (TYPE_OFFSET_BASETYPE (t2)), strict)
-            && comptypes (TREE_TYPE (t1), TREE_TYPE (t2), strict));
+      if (!comptypes (TYPE_OFFSET_BASETYPE (t1), TYPE_OFFSET_BASETYPE (t2),
+                     strict & ~COMPARE_REDECLARATION))
+       return false;
+      if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
+       return false;
       break;
 
-    case POINTER_TYPE:
     case REFERENCE_TYPE:
-      t1 = TREE_TYPE (t1);
-      t2 = TREE_TYPE (t2);
-      /* first, check whether the referred types match with the
-         required level of strictness */
-      val = comptypes (t1, t2, strict);
-      if (val)
-       break;
-      if (TREE_CODE (t1) == RECORD_TYPE 
-         && TREE_CODE (t2) == RECORD_TYPE)
-       goto look_hard;
+      if (TYPE_REF_IS_RVALUE (t1) != TYPE_REF_IS_RVALUE (t2))
+       return false;
+      /* fall through to checks for pointer types */
+
+    case POINTER_TYPE:
+      if (TYPE_MODE (t1) != TYPE_MODE (t2)
+         || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2)
+         || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
+       return false;
       break;
 
     case METHOD_TYPE:
     case FUNCTION_TYPE:
-      val = ((TREE_TYPE (t1) == TREE_TYPE (t2)
-             || comptypes (TREE_TYPE (t1), TREE_TYPE (t2), strict))
-            && compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)));
+      if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
+       return false;
+      if (!compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)))
+       return false;
       break;
 
     case ARRAY_TYPE:
-      /* Target types must match incl. qualifiers.  We use ORIG_STRICT
-        here since this is the one place where
-        COMPARE_REDECLARATION should be used.  */
-      val = comp_array_types (comptypes, t1, t2, orig_strict);
+      /* Target types must match incl. qualifiers.  */
+      if (!comp_array_types (t1, t2, !!(strict & COMPARE_REDECLARATION)))
+       return false;
       break;
 
     case TEMPLATE_TYPE_PARM:
-      return TEMPLATE_TYPE_IDX (t1) == TEMPLATE_TYPE_IDX (t2)
-       && TEMPLATE_TYPE_LEVEL (t1) == TEMPLATE_TYPE_LEVEL (t2);
+      if (TEMPLATE_TYPE_IDX (t1) != TEMPLATE_TYPE_IDX (t2)
+         || TEMPLATE_TYPE_LEVEL (t1) != TEMPLATE_TYPE_LEVEL (t2)
+          || (TEMPLATE_TYPE_PARAMETER_PACK (t1) 
+              != TEMPLATE_TYPE_PARAMETER_PACK (t2)))
+       return false;
+      break;
 
     case TYPENAME_TYPE:
-      if (cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1),
-                         TYPENAME_TYPE_FULLNAME (t2)) < 1)
-        return 0;
-      return same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2));
+      if (!cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1),
+                         TYPENAME_TYPE_FULLNAME (t2)))
+       return false;
+      if (!same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2)))
+       return false;
+      break;
 
     case UNBOUND_CLASS_TEMPLATE:
-      if (cp_tree_equal (TYPE_IDENTIFIER (t1),
-                         TYPE_IDENTIFIER (t2)) < 1)
-        return 0;
-      return same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2));
+      if (!cp_tree_equal (TYPE_IDENTIFIER (t1), TYPE_IDENTIFIER (t2)))
+       return false;
+      if (!same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2)))
+       return false;
+      break;
 
     case COMPLEX_TYPE:
-      return same_type_p (TREE_TYPE (t1), TREE_TYPE (t2));
-
-    default:
+      if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
+       return false;
       break;
-    }
-  return attrval == 2 && val == 1 ? 2 : val;
-}
-
-/* Subroutine of comp_target-types.  Make sure that the cv-quals change
-   only in the same direction as the target type.  */
 
-static int
-comp_cv_target_types (ttl, ttr, nptrs)
-     tree ttl, ttr;
-     int nptrs;
-{
-  int t;
+    case VECTOR_TYPE:
+      if (TYPE_VECTOR_SUBPARTS (t1) != TYPE_VECTOR_SUBPARTS (t2)
+         || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
+       return false;
+      break;
 
-  if (!at_least_as_qualified_p (ttl, ttr)
-      && !at_least_as_qualified_p (ttr, ttl))
-    /* The qualifications are incomparable.  */
-    return 0;
+    case TYPE_PACK_EXPANSION:
+      return same_type_p (PACK_EXPANSION_PATTERN (t1), 
+                          PACK_EXPANSION_PATTERN (t2));
 
-  if (TYPE_MAIN_VARIANT (ttl) == TYPE_MAIN_VARIANT (ttr))
-    return more_qualified_p (ttr, ttl) ? -1 : 1;
+    case DECLTYPE_TYPE:
+      if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t1)
+          != DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t2)
+          || !cp_tree_equal (DECLTYPE_TYPE_EXPR (t1), 
+                             DECLTYPE_TYPE_EXPR (t2)))
+        return false;
+      break;
 
-  t = comp_target_types (ttl, ttr, nptrs);
-  if ((t == 1 && at_least_as_qualified_p (ttl, ttr)) 
-      || (t == -1 && at_least_as_qualified_p (ttr, ttl)))
-    return t;
+    default:
+      return false;
+    }
 
-  return 0;
+  /* If we get here, we know that from a target independent POV the
+     types are the same.  Make sure the target attributes are also
+     the same.  */
+  return targetm.comp_type_attributes (t1, t2);
 }
 
-/* Return 1 or -1 if TTL and TTR are pointers to types that are equivalent,
-   ignoring their qualifiers, 0 if not. Return 1 means that TTR can be
-   converted to TTL. Return -1 means that TTL can be converted to TTR but
-   not vice versa.
-
-   NPTRS is the number of pointers we can strip off and keep cool.
-   This is used to permit (for aggr A, aggr B) A, B* to convert to A*,
-   but to not permit B** to convert to A**.
+/* Return true if T1 and T2 are related as allowed by STRICT.  STRICT
+   is a bitwise-or of the COMPARE_* flags.  */
 
-   This should go away.  Callers should use can_convert or something
-   similar instead.  (jason 17 Apr 1997)  */
-
-int
-comp_target_types (ttl, ttr, nptrs)
-     tree ttl, ttr;
-     int nptrs;
+bool
+comptypes (tree t1, tree t2, int strict)
 {
-  ttl = TYPE_MAIN_VARIANT (ttl);
-  ttr = TYPE_MAIN_VARIANT (ttr);
-  if (same_type_p (ttl, ttr))
-    return 1;
-
-  if (TREE_CODE (ttr) != TREE_CODE (ttl))
-    return 0;
-
-  if ((TREE_CODE (ttr) == POINTER_TYPE
-       || TREE_CODE (ttr) == REFERENCE_TYPE)
-      /* If we get a pointer with nptrs == 0, we don't allow any tweaking
-        of the type pointed to.  This is necessary for reference init
-        semantics.  We won't get here from a previous call with nptrs == 1;
-        for multi-level pointers we end up in comp_ptr_ttypes.  */
-      && nptrs > 0)
+  if (strict == COMPARE_STRICT)
     {
-      int is_ptr = TREE_CODE (ttr) == POINTER_TYPE;
-
-      ttl = TREE_TYPE (ttl);
-      ttr = TREE_TYPE (ttr);
-
-      if (is_ptr)
-       {
-         if (TREE_CODE (ttl) == UNKNOWN_TYPE
-             || TREE_CODE (ttr) == UNKNOWN_TYPE)
-           return 1;
-         else if (TREE_CODE (ttl) == VOID_TYPE
-                  && TREE_CODE (ttr) != FUNCTION_TYPE
-                  && TREE_CODE (ttr) != METHOD_TYPE
-                  && TREE_CODE (ttr) != OFFSET_TYPE)
-           return 1;
-         else if (TREE_CODE (ttr) == VOID_TYPE
-                  && TREE_CODE (ttl) != FUNCTION_TYPE
-                  && TREE_CODE (ttl) != METHOD_TYPE
-                  && TREE_CODE (ttl) != OFFSET_TYPE)
-           return -1;
-         else if (TREE_CODE (ttl) == POINTER_TYPE
-                  || TREE_CODE (ttl) == ARRAY_TYPE)
-           {
-             if (comp_ptr_ttypes (ttl, ttr))
-               return 1;
-             else if (comp_ptr_ttypes (ttr, ttl))
-               return -1;
-             return 0;
-           }
-       }
-
-      /* Const and volatile mean something different for function types,
-        so the usual checks are not appropriate.  */
-      if (TREE_CODE (ttl) == FUNCTION_TYPE || TREE_CODE (ttl) == METHOD_TYPE)
-       return comp_target_types (ttl, ttr, nptrs - 1);
-
-      return comp_cv_target_types (ttl, ttr, nptrs - 1);
-    }
-
-  if (TREE_CODE (ttr) == ARRAY_TYPE)
-    return comp_array_types (comp_target_types, ttl, ttr, COMPARE_STRICT);
-  else if (TREE_CODE (ttr) == FUNCTION_TYPE || TREE_CODE (ttr) == METHOD_TYPE)
-    {
-      tree argsl, argsr;
-      int saw_contra = 0;
-
-      if (pedantic)
-       {
-         if (!same_type_p (TREE_TYPE (ttl), TREE_TYPE (ttr)))
-           return 0;
-       }
-      else
-       {
-         switch (comp_target_types (TREE_TYPE (ttl), TREE_TYPE (ttr), -1))
-           {
-           case 0:
-             return 0;
-           case -1:
-             saw_contra = 1;
-           }
-       }
-
-      argsl = TYPE_ARG_TYPES (ttl);
-      argsr = TYPE_ARG_TYPES (ttr);
-
-      /* Compare 'this' here, not in comp_target_parms.  */
-      if (TREE_CODE (ttr) == METHOD_TYPE)
-       {
-         tree tl = TYPE_METHOD_BASETYPE (ttl);
-         tree tr = TYPE_METHOD_BASETYPE (ttr);
-
-         if (!same_or_base_type_p (tr, tl))
-           {
-             if (same_or_base_type_p (tl, tr))
-               saw_contra = 1;
-             else
-               return 0;
-           }
-
-         argsl = TREE_CHAIN (argsl);
-         argsr = TREE_CHAIN (argsr);
-       }
-
-       switch (comp_target_parms (argsl, argsr))
-         {
-         case 0:
-           return 0;
-         case -1:
-           saw_contra = 1;
-         }
+      if (t1 == t2)
+       return true;
 
-       return saw_contra ? -1 : 1;
-    }
-  /* for C++ */
-  else if (TREE_CODE (ttr) == OFFSET_TYPE)
-    {
-      int base;
+      if (t1 == error_mark_node || t2 == error_mark_node)
+       return false;
 
-      /* Contravariance: we can assign a pointer to base member to a pointer
-        to derived member.  Note difference from simple pointer case, where
-        we can pass a pointer to derived to a pointer to base.  */
-      if (same_or_base_type_p (TYPE_OFFSET_BASETYPE (ttr),
-                              TYPE_OFFSET_BASETYPE (ttl)))
-       base = 1;
-      else if (same_or_base_type_p (TYPE_OFFSET_BASETYPE (ttl),
-                                   TYPE_OFFSET_BASETYPE (ttr)))
-       {
-         tree tmp = ttl;
-         ttl = ttr;
-         ttr = tmp;
-         base = -1;
-       }
-      else
-       return 0;
-
-      ttl = TREE_TYPE (ttl);
-      ttr = TREE_TYPE (ttr);
+      if (TYPE_STRUCTURAL_EQUALITY_P (t1) || TYPE_STRUCTURAL_EQUALITY_P (t2))
+       /* At least one of the types requires structural equality, so
+          perform a deep check. */
+       return structural_comptypes (t1, t2, strict);
 
-      if (TREE_CODE (ttl) == POINTER_TYPE
-         || TREE_CODE (ttl) == ARRAY_TYPE)
+#ifdef ENABLE_CHECKING
+      if (USE_CANONICAL_TYPES)
        {
-         if (comp_ptr_ttypes (ttl, ttr))
-           return base;
-         return 0;
+         bool result = structural_comptypes (t1, t2, strict);
+         
+         if (result && TYPE_CANONICAL (t1) != TYPE_CANONICAL (t2))
+           /* The two types are structurally equivalent, but their
+              canonical types were different. This is a failure of the
+              canonical type propagation code.*/
+           internal_error 
+             ("canonical types differ for identical types %T and %T", 
+              t1, t2);
+         else if (!result && TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2))
+           /* Two types are structurally different, but the canonical
+              types are the same. This means we were over-eager in
+              assigning canonical types. */
+           internal_error 
+             ("same canonical type node for different types %T and %T",
+              t1, t2);
+         
+         return result;
        }
+#else
+      if (USE_CANONICAL_TYPES)
+       return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
+#endif
       else
-       {
-         if (comp_cv_target_types (ttl, ttr, nptrs) == 1)
-           return base;
-         return 0;
-       }
-    }
-  else if (IS_AGGR_TYPE (ttl))
-    {
-      if (nptrs < 0)
-       return 0;
-      if (same_or_base_type_p (build_pointer_type (ttl), 
-                              build_pointer_type (ttr)))
-       return 1;
-      if (same_or_base_type_p (build_pointer_type (ttr), 
-                              build_pointer_type (ttl)))
-       return -1;
-      return 0;
+       return structural_comptypes (t1, t2, strict);
     }
-
-  return 0;
+  else if (strict == COMPARE_STRUCTURAL)
+    return structural_comptypes (t1, t2, COMPARE_STRICT);
+  else
+    return structural_comptypes (t1, t2, strict);
 }
 
 /* Returns 1 if TYPE1 is at least as qualified as TYPE2.  */
 
-int
-at_least_as_qualified_p (type1, type2)
-     tree type1;
-     tree type2;
+bool
+at_least_as_qualified_p (const_tree type1, const_tree type2)
 {
-  /* All qualifiers for TYPE2 must also appear in TYPE1.  */
-  return ((cp_type_quals (type1) & cp_type_quals (type2))
-         == cp_type_quals (type2));
-}
-
-/* Returns 1 if TYPE1 is more qualified than TYPE2.  */
+  int q1 = cp_type_quals (type1);
+  int q2 = cp_type_quals (type2);
 
-int
-more_qualified_p (type1, type2)
-     tree type1;
-     tree type2;
-{
-  return (cp_type_quals (type1) != cp_type_quals (type2)
-         && at_least_as_qualified_p (type1, type2));
+  /* All qualifiers for TYPE2 must also appear in TYPE1.  */
+  return (q1 & q2) == q2;
 }
 
 /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
    more cv-qualified that TYPE1, and 0 otherwise.  */
 
 int
-comp_cv_qualification (type1, type2)
-     tree type1;
-     tree type2;
+comp_cv_qualification (const_tree type1, const_tree type2)
 {
-  if (cp_type_quals (type1) == cp_type_quals (type2))
+  int q1 = cp_type_quals (type1);
+  int q2 = cp_type_quals (type2);
+
+  if (q1 == q2)
     return 0;
 
-  if (at_least_as_qualified_p (type1, type2))
+  if ((q1 & q2) == q2)
     return 1;
-
-  else if (at_least_as_qualified_p (type2, type1))
+  else if ((q1 & q2) == q1)
     return -1;
 
   return 0;
@@ -1309,9 +1267,7 @@ comp_cv_qualification (type1, type2)
    are similar.  Returns -1 if the other way 'round, and 0 otherwise.  */
 
 int
-comp_cv_qual_signature (type1, type2)
-     tree type1;
-     tree type2;
+comp_cv_qual_signature (tree type1, tree type2)
 {
   if (comp_ptr_ttypes_real (type2, type1, -1))
     return 1;
@@ -1320,391 +1276,375 @@ comp_cv_qual_signature (type1, type2)
   else
     return 0;
 }
-
-/* If two types share a common base type, return that basetype.
-   If there is not a unique most-derived base type, this function
-   returns ERROR_MARK_NODE.  */
-
-static tree
-common_base_type (tt1, tt2)
-     tree tt1, tt2;
-{
-  tree best = NULL_TREE;
-  int i;
-
-  /* If one is a baseclass of another, that's good enough.  */
-  if (UNIQUELY_DERIVED_FROM_P (tt1, tt2))
-    return tt1;
-  if (UNIQUELY_DERIVED_FROM_P (tt2, tt1))
-    return tt2;
-
-  /* Otherwise, try to find a unique baseclass of TT1
-     that is shared by TT2, and follow that down.  */
-  for (i = CLASSTYPE_N_BASECLASSES (tt1)-1; i >= 0; i--)
-    {
-      tree basetype = TYPE_BINFO_BASETYPE (tt1, i);
-      tree trial = common_base_type (basetype, tt2);
-      if (trial)
-       {
-         if (trial == error_mark_node)
-           return trial;
-         if (best == NULL_TREE)
-           best = trial;
-         else if (best != trial)
-           return error_mark_node;
-       }
-    }
-
-  /* Same for TT2.  */
-  for (i = CLASSTYPE_N_BASECLASSES (tt2)-1; i >= 0; i--)
-    {
-      tree basetype = TYPE_BINFO_BASETYPE (tt2, i);
-      tree trial = common_base_type (tt1, basetype);
-      if (trial)
-       {
-         if (trial == error_mark_node)
-           return trial;
-         if (best == NULL_TREE)
-           best = trial;
-         else if (best != trial)
-           return error_mark_node;
-       }
-    }
-  return best;
-}
 \f
 /* Subroutines of `comptypes'.  */
 
-/* Return 1 if two parameter type lists PARMS1 and PARMS2 are
+/* Return true if two parameter type lists PARMS1 and PARMS2 are
    equivalent in the sense that functions with those parameter types
    can have equivalent types.  The two lists must be equivalent,
-   element by element.
-
-   C++: See comment above about TYPE1, TYPE2.  */
+   element by element.  */
 
-int
-compparms (parms1, parms2)
-     tree parms1, parms2;
+bool
+compparms (const_tree parms1, const_tree parms2)
 {
-  register tree t1 = parms1, t2 = parms2;
+  const_tree t1, t2;
 
   /* An unspecified parmlist matches any specified parmlist
      whose argument types don't need default promotions.  */
 
-  while (1)
+  for (t1 = parms1, t2 = parms2;
+       t1 || t2;
+       t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
     {
-      if (t1 == 0 && t2 == 0)
-       return 1;
       /* If one parmlist is shorter than the other,
         they fail to match.  */
-      if (t1 == 0 || t2 == 0)
-       return 0;
-      if (!same_type_p (TREE_VALUE (t2), TREE_VALUE (t1)))
-       return 0;
-
-      t1 = TREE_CHAIN (t1);
-      t2 = TREE_CHAIN (t2);
+      if (!t1 || !t2)
+       return false;
+      if (!same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
+       return false;
     }
+  return true;
 }
 
-/* This really wants return whether or not parameter type lists
-   would make their owning functions assignment compatible or not.
-
-   The return value is like for comp_target_types.
-
-   This should go away, possibly with the exception of the empty parmlist
-   conversion; there are no conversions between function types in C++.
-   (jason 17 Apr 1997)  */
+\f
+/* Process a sizeof or alignof expression where the operand is a
+   type.  */
 
-static int
-comp_target_parms (parms1, parms2)
-     tree parms1, parms2;
+tree
+cxx_sizeof_or_alignof_type (tree type, enum tree_code op, bool complain)
 {
-  register tree t1 = parms1, t2 = parms2;
-  int warn_contravariance = 0;
+  tree value;
+  bool dependent_p;
 
-  /* In C, an unspecified parmlist matches any specified parmlist
-     whose argument types don't need default promotions.  This is not
-     true for C++, but let's do it anyway for unfixed headers.  */
+  gcc_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR);
+  if (type == error_mark_node)
+    return error_mark_node;
 
-  if (t1 == 0 && t2 != 0)
-    {
-      pedwarn ("ISO C++ prohibits conversion from `%#T' to `(...)'",
-                 parms2);
-      return self_promoting_args_p (t2);
+  type = non_reference (type);
+  if (TREE_CODE (type) == METHOD_TYPE)
+    {
+      if (complain)
+       pedwarn (input_location, pedantic ? OPT_pedantic : OPT_Wpointer_arith, 
+                "invalid application of %qs to a member function", 
+                operator_name_info[(int) op].name);
+      value = size_one_node;
+    }
+
+  dependent_p = dependent_type_p (type);
+  if (!dependent_p)
+    complete_type (type);
+  if (dependent_p
+      /* VLA types will have a non-constant size.  In the body of an
+        uninstantiated template, we don't need to try to compute the
+        value, because the sizeof expression is not an integral
+        constant expression in that case.  And, if we do try to
+        compute the value, we'll likely end up with SAVE_EXPRs, which
+        the template substitution machinery does not expect to see.  */
+      || (processing_template_decl 
+         && COMPLETE_TYPE_P (type)
+         && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST))
+    {
+      value = build_min (op, size_type_node, type);
+      TREE_READONLY (value) = 1;
+      return value;
     }
-  if (t2 == 0)
-    return self_promoting_args_p (t1);
-
-  for (; t1 || t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
-    {
-      tree p1, p2;
 
-      /* If one parmlist is shorter than the other,
-        they fail to match, unless STRICT is <= 0.  */
-      if (t1 == 0 || t2 == 0)
-       return 0;
-      p1 = TREE_VALUE (t1);
-      p2 = TREE_VALUE (t2);
-      if (same_type_p (p1, p2))
-       continue;
-
-      if (pedantic)
-       return 0;
+  return c_sizeof_or_alignof_type (complete_type (type),
+                                  op == SIZEOF_EXPR,
+                                  complain);
+}
 
-      if ((TREE_CODE (p1) == POINTER_TYPE && TREE_CODE (p2) == POINTER_TYPE)
-         || (TREE_CODE (p1) == REFERENCE_TYPE
-             && TREE_CODE (p2) == REFERENCE_TYPE))
-       {
-         /* The following is wrong for contravariance,
-            but many programs depend on it.  */
-         if (TREE_TYPE (p1) == void_type_node)
-           continue;
-         if (TREE_TYPE (p2) == void_type_node)
-           {
-             warn_contravariance = 1;
-             continue;
-           }
-         if (IS_AGGR_TYPE (TREE_TYPE (p1))
-             && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (p1),
-                                                            TREE_TYPE (p2)))
-           return 0;
-       }
-      /* Note backwards order due to contravariance.  */
-      if (comp_target_types (p2, p1, 1) <= 0)
-       {
-         if (comp_target_types (p1, p2, 1) > 0)
-           {
-             warn_contravariance = 1;
-             continue;
-           }
-         return 0;
-       }
-    }
-  return warn_contravariance ? -1 : 1;
+/* Return the size of the type, without producing any warnings for
+   types whose size cannot be taken.  This routine should be used only
+   in some other routine that has already produced a diagnostic about
+   using the size of such a type.  */
+tree 
+cxx_sizeof_nowarn (tree type)
+{
+  if (TREE_CODE (type) == FUNCTION_TYPE
+      || TREE_CODE (type) == VOID_TYPE
+      || TREE_CODE (type) == ERROR_MARK)
+    return size_one_node;
+  else if (!COMPLETE_TYPE_P (type))
+    return size_zero_node;
+  else
+    return cxx_sizeof_or_alignof_type (type, SIZEOF_EXPR, false);
 }
-\f
-/* Compute the value of the `sizeof' operator.  */
 
-tree
-c_sizeof (type)
-     tree type;
+/* Process a sizeof expression where the operand is an expression.  */
+
+static tree
+cxx_sizeof_expr (tree e, tsubst_flags_t complain)
 {
-  enum tree_code code = TREE_CODE (type);
-  tree size;
+  if (e == error_mark_node)
+    return error_mark_node;
 
   if (processing_template_decl)
-    return build_min_nt (SIZEOF_EXPR, type);
+    {
+      e = build_min (SIZEOF_EXPR, size_type_node, e);
+      TREE_SIDE_EFFECTS (e) = 0;
+      TREE_READONLY (e) = 1;
+
+      return e;
+    }
 
-  if (code == FUNCTION_TYPE)
+  if (TREE_CODE (e) == COMPONENT_REF
+      && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL
+      && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1)))
     {
-      if (pedantic || warn_pointer_arith)
-       pedwarn ("ISO C++ forbids applying `sizeof' to a function type");
-      size = size_one_node;
+      if (complain & tf_error)
+        error ("invalid application of %<sizeof%> to a bit-field");
+      else
+        return error_mark_node;
+      e = char_type_node;
     }
-  else if (code == METHOD_TYPE)
+  else if (is_overloaded_fn (e))
     {
-      if (pedantic || warn_pointer_arith)
-       pedwarn ("ISO C++ forbids applying `sizeof' to a member function");
-      size = size_one_node;
+      if (complain & tf_error)
+        permerror (input_location, "ISO C++ forbids applying %<sizeof%> to an expression of "
+                   "function type");
+      else
+        return error_mark_node;
+      e = char_type_node;
     }
-  else if (code == VOID_TYPE)
+  else if (type_unknown_p (e))
     {
-      if (pedantic || warn_pointer_arith)
-       pedwarn ("ISO C++ forbids applying `sizeof' to type `void' which is an incomplete type");
-      size = size_one_node;
+      if (complain & tf_error)
+        cxx_incomplete_type_error (e, TREE_TYPE (e));
+      else
+        return error_mark_node;
+      e = char_type_node;
     }
-  else if (code == ERROR_MARK)
-    size = size_one_node;
   else
-    {
-      /* ARM $5.3.2: ``When applied to a reference, the result is the
-        size of the referenced object.'' */
-      if (code == REFERENCE_TYPE)
-       type = TREE_TYPE (type);
+    e = TREE_TYPE (e);
 
-      if (code == OFFSET_TYPE)
-       {
-         error ("`sizeof' applied to non-static member");
-         size = size_zero_node;
-       }
-      else if (!COMPLETE_TYPE_P (complete_type (type)))
-       {
-         error ("`sizeof' applied to incomplete type `%T'", type);
-         size = size_zero_node;
-       }
-      else
-       /* Convert in case a char is more than one unit.  */
-       size = size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
-                          size_int (TYPE_PRECISION (char_type_node)
-                                    / BITS_PER_UNIT));
-    }
-
-  /* SIZE will have an integer type with TYPE_IS_SIZETYPE set.
-     TYPE_IS_SIZETYPE means that certain things (like overflow) will
-     never happen.  However, this node should really have type
-     `size_t', which is just a typedef for an ordinary integer type.  */
-  size = fold (build1 (NOP_EXPR, size_type_node, size));
-  my_friendly_assert (!TYPE_IS_SIZETYPE (TREE_TYPE (size)), 
-                     20001021);
-  return size;
+  return cxx_sizeof_or_alignof_type (e, SIZEOF_EXPR, complain & tf_error);
 }
 
+/* Implement the __alignof keyword: Return the minimum required
+   alignment of E, measured in bytes.  For VAR_DECL's and
+   FIELD_DECL's return DECL_ALIGN (which can be set from an
+   "aligned" __attribute__ specification).  */
 
-tree
-expr_sizeof (e)
-     tree e;
+static tree
+cxx_alignof_expr (tree e, tsubst_flags_t complain)
 {
+  tree t;
+
+  if (e == error_mark_node)
+    return error_mark_node;
+
   if (processing_template_decl)
-    return build_min_nt (SIZEOF_EXPR, e);
+    {
+      e = build_min (ALIGNOF_EXPR, size_type_node, e);
+      TREE_SIDE_EFFECTS (e) = 0;
+      TREE_READONLY (e) = 1;
 
-  if (TREE_CODE (e) == COMPONENT_REF
-      && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1)))
-    error ("sizeof applied to a bit-field");
-  if (is_overloaded_fn (e))
+      return e;
+    }
+
+  if (TREE_CODE (e) == VAR_DECL)
+    t = size_int (DECL_ALIGN_UNIT (e));
+  else if (TREE_CODE (e) == COMPONENT_REF
+          && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL
+          && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1)))
     {
-      pedwarn ("ISO C++ forbids applying `sizeof' to an expression of function type");
-      return c_sizeof (char_type_node);
+      if (complain & tf_error)
+        error ("invalid application of %<__alignof%> to a bit-field");
+      else
+        return error_mark_node;
+      t = size_one_node;
+    }
+  else if (TREE_CODE (e) == COMPONENT_REF
+          && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL)
+    t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (e, 1)));
+  else if (is_overloaded_fn (e))
+    {
+      if (complain & tf_error)
+        permerror (input_location, "ISO C++ forbids applying %<__alignof%> to an expression of "
+                   "function type");
+      else
+        return error_mark_node;
+      if (TREE_CODE (e) == FUNCTION_DECL)
+       t = size_int (DECL_ALIGN_UNIT (e));
+      else
+       t = size_one_node;
     }
   else if (type_unknown_p (e))
     {
-      incomplete_type_error (e, TREE_TYPE (e));
-      return c_sizeof (char_type_node);
+      if (complain & tf_error)
+        cxx_incomplete_type_error (e, TREE_TYPE (e));
+      else
+        return error_mark_node;
+      t = size_one_node;
     }
-  /* It's illegal to say `sizeof (X::i)' for `i' a non-static data
-     member unless you're in a non-static member of X.  So hand off to
-     resolve_offset_ref.  [expr.prim]  */
-  else if (TREE_CODE (e) == OFFSET_REF)
-    e = resolve_offset_ref (e);
-
-  if (e == error_mark_node)
-    return e;
+  else
+    return cxx_sizeof_or_alignof_type (TREE_TYPE (e), ALIGNOF_EXPR, 
+                                       complain & tf_error);
 
-  return c_sizeof (TREE_TYPE (e));
+  return fold_convert (size_type_node, t);
 }
-  
+
+/* Process a sizeof or alignof expression E with code OP where the operand
+   is an expression.  */
+
 tree
-c_sizeof_nowarn (type)
-     tree type;
+cxx_sizeof_or_alignof_expr (tree e, enum tree_code op, bool complain)
 {
-  enum tree_code code = TREE_CODE (type);
-  tree size;
-
-  if (code == FUNCTION_TYPE
-      || code == METHOD_TYPE
-      || code == VOID_TYPE
-      || code == ERROR_MARK)
-    size = size_one_node;
+  if (op == SIZEOF_EXPR)
+    return cxx_sizeof_expr (e, complain? tf_warning_or_error : tf_none);
   else
-    {
-      if (code == REFERENCE_TYPE)
-       type = TREE_TYPE (type);
-
-      if (!COMPLETE_TYPE_P (type))
-       size = size_zero_node;
-      else
-       /* Convert in case a char is more than one unit.  */
-       size = size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
-                          size_int (TYPE_PRECISION (char_type_node)
-                                    / BITS_PER_UNIT));
-    }
-
-  /* SIZE will have an integer type with TYPE_IS_SIZETYPE set.
-     TYPE_IS_SIZETYPE means that certain things (like overflow) will
-     never happen.  However, this node should really have type
-     `size_t', which is just a typedef for an ordinary integer type.  */
-  size = fold (build1 (NOP_EXPR, size_type_node, size));
-  my_friendly_assert (!TYPE_IS_SIZETYPE (TREE_TYPE (size)), 
-                     20001021);
-  return size;
+    return cxx_alignof_expr (e, complain? tf_warning_or_error : tf_none);
 }
 \f
-/* Perform the array-to-pointer and function-to-pointer conversions
-   for EXP.  
+/* EXPR is being used in a context that is not a function call.
+   Enforce:
+
+     [expr.ref]
 
-   In addition, references are converted to lvalues and manifest
-   constants are replaced by their values.  */
+     The expression can be used only as the left-hand operand of a
+     member function call.
+
+     [expr.mptr.operator]
+
+     If the result of .* or ->* is a function, then that result can be
+     used only as the operand for the function call operator ().
+
+   by issuing an error message if appropriate.  Returns true iff EXPR
+   violates these rules.  */
+
+bool
+invalid_nonstatic_memfn_p (const_tree expr, tsubst_flags_t complain)
+{
+  if (expr && DECL_NONSTATIC_MEMBER_FUNCTION_P (expr))
+    {
+      if (complain & tf_error)
+        error ("invalid use of non-static member function");
+      return true;
+    }
+  return false;
+}
+
+/* If EXP is a reference to a bitfield, and the type of EXP does not
+   match the declared type of the bitfield, return the declared type
+   of the bitfield.  Otherwise, return NULL_TREE.  */
 
 tree
-decay_conversion (exp)
-     tree exp;
+is_bitfield_expr_with_lowered_type (const_tree exp)
 {
-  register tree type;
-  register enum tree_code code;
+  switch (TREE_CODE (exp))
+    {
+    case COND_EXPR:
+      if (!is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1)
+                                              ? TREE_OPERAND (exp, 1)
+                                              : TREE_OPERAND (exp, 0)))
+       return NULL_TREE;
+      return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 2));
 
-  if (TREE_CODE (exp) == OFFSET_REF)
-    exp = resolve_offset_ref (exp);
+    case COMPOUND_EXPR:
+      return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1));
 
-  type = TREE_TYPE (exp);
-  code = TREE_CODE (type);
+    case MODIFY_EXPR:
+    case SAVE_EXPR:
+      return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
 
-  if (code == REFERENCE_TYPE)
-    {
-      exp = convert_from_reference (exp);
-      type = TREE_TYPE (exp);
-      code = TREE_CODE (type);
+    case COMPONENT_REF:
+      {
+       tree field;
+       
+       field = TREE_OPERAND (exp, 1);
+       if (TREE_CODE (field) != FIELD_DECL || !DECL_BIT_FIELD_TYPE (field))
+         return NULL_TREE;
+       if (same_type_ignoring_top_level_qualifiers_p
+           (TREE_TYPE (exp), DECL_BIT_FIELD_TYPE (field)))
+         return NULL_TREE;
+       return DECL_BIT_FIELD_TYPE (field);
+      }
+
+    CASE_CONVERT:
+      if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (exp, 0)))
+         == TYPE_MAIN_VARIANT (TREE_TYPE (exp)))
+       return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
+      /* Fallthrough.  */
+
+    default:
+      return NULL_TREE;
     }
+}
+
+/* Like is_bitfield_with_lowered_type, except that if EXP is not a
+   bitfield with a lowered type, the type of EXP is returned, rather
+   than NULL_TREE.  */
+
+tree
+unlowered_expr_type (const_tree exp)
+{
+  tree type;
+
+  type = is_bitfield_expr_with_lowered_type (exp);
+  if (!type)
+    type = TREE_TYPE (exp);
+
+  return type;
+}
 
+/* Perform the conversions in [expr] that apply when an lvalue appears
+   in an rvalue context: the lvalue-to-rvalue, array-to-pointer, and
+   function-to-pointer conversions.  In addition, manifest constants
+   are replaced by their values, and bitfield references are converted
+   to their declared types.
+
+   Although the returned value is being used as an rvalue, this
+   function does not wrap the returned expression in a
+   NON_LVALUE_EXPR; the caller is expected to be mindful of the fact
+   that the return value is no longer an lvalue.  */
+
+tree
+decay_conversion (tree exp)
+{
+  tree type;
+  enum tree_code code;
+
+  type = TREE_TYPE (exp);
   if (type == error_mark_node)
     return error_mark_node;
 
   if (type_unknown_p (exp))
     {
-      incomplete_type_error (exp, TREE_TYPE (exp));
+      cxx_incomplete_type_error (exp, TREE_TYPE (exp));
       return error_mark_node;
     }
-  
-  /* Constants can be used directly unless they're not loadable.  */
-  if (TREE_CODE (exp) == CONST_DECL)
-    exp = DECL_INITIAL (exp);
-  /* Replace a nonvolatile const static variable with its value.  We
-     don't do this for arrays, though; we want the address of the
-     first element of the array, not the address of the first element
-     of its initializing constant.  */
-  else if (code != ARRAY_TYPE)
-    {
-      exp = decl_constant_value (exp);
-      type = TREE_TYPE (exp);
-    }
+
+  exp = decl_constant_value (exp);
+  if (error_operand_p (exp))
+    return error_mark_node;
 
   /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
      Leave such NOP_EXPRs, since RHS is being used in non-lvalue context.  */
-
+  code = TREE_CODE (type);
   if (code == VOID_TYPE)
     {
       error ("void value not ignored as it ought to be");
       return error_mark_node;
     }
-  if (code == METHOD_TYPE)
-    abort ();
+  if (invalid_nonstatic_memfn_p (exp, tf_warning_or_error))
+    return error_mark_node;
   if (code == FUNCTION_TYPE || is_overloaded_fn (exp))
-    return build_unary_op (ADDR_EXPR, exp, 0);
+    return cp_build_unary_op (ADDR_EXPR, exp, 0, tf_warning_or_error);
   if (code == ARRAY_TYPE)
     {
-      register tree adr;
+      tree adr;
       tree ptrtype;
 
       if (TREE_CODE (exp) == INDIRECT_REF)
-       {
-         /* Stripping away the INDIRECT_REF is not the right
-            thing to do for references...  */
-         tree inner = TREE_OPERAND (exp, 0);
-         if (TREE_CODE (TREE_TYPE (inner)) == REFERENCE_TYPE)
-           {
-             inner = build1 (CONVERT_EXPR,
-                             build_pointer_type (TREE_TYPE
-                                                 (TREE_TYPE (inner))),
-                             inner);
-             TREE_CONSTANT (inner) = TREE_CONSTANT (TREE_OPERAND (inner, 0));
-           }
-         return cp_convert (build_pointer_type (TREE_TYPE (type)), inner);
-       }
+       return build_nop (build_pointer_type (TREE_TYPE (type)),
+                         TREE_OPERAND (exp, 0));
 
       if (TREE_CODE (exp) == COMPOUND_EXPR)
        {
          tree op1 = decay_conversion (TREE_OPERAND (exp, 1));
-         return build (COMPOUND_EXPR, TREE_TYPE (op1),
-                       TREE_OPERAND (exp, 0), op1);
+         return build2 (COMPOUND_EXPR, TREE_TYPE (op1),
+                        TREE_OPERAND (exp, 0), op1);
        }
 
       if (!lvalue_p (exp)
@@ -1718,87 +1658,111 @@ decay_conversion (exp)
 
       if (TREE_CODE (exp) == VAR_DECL)
        {
-         /* ??? This is not really quite correct
-            in that the type of the operand of ADDR_EXPR
-            is not the target type of the type of the ADDR_EXPR itself.
-            Question is, can this lossage be avoided?  */
-         adr = build1 (ADDR_EXPR, ptrtype, exp);
-         if (mark_addressable (exp) == 0)
+         if (!cxx_mark_addressable (exp))
            return error_mark_node;
-         TREE_CONSTANT (adr) = staticp (exp);
-         TREE_SIDE_EFFECTS (adr) = 0;   /* Default would be, same as EXP.  */
+         adr = build_nop (ptrtype, build_address (exp));
          return adr;
        }
       /* This way is better for a COMPONENT_REF since it can
         simplify the offset for a component.  */
-      adr = build_unary_op (ADDR_EXPR, exp, 1);
+      adr = cp_build_unary_op (ADDR_EXPR, exp, 1, tf_warning_or_error);
       return cp_convert (ptrtype, adr);
     }
 
-  /* [basic.lval]: Class rvalues can have cv-qualified types; non-class
-     rvalues always have cv-unqualified types.  */
-  if (! CLASS_TYPE_P (type))
-    exp = cp_convert (TYPE_MAIN_VARIANT (type), exp);
+  /* If a bitfield is used in a context where integral promotion
+     applies, then the caller is expected to have used
+     default_conversion.  That function promotes bitfields correctly
+     before calling this function.  At this point, if we have a
+     bitfield referenced, we may assume that is not subject to
+     promotion, and that, therefore, the type of the resulting rvalue
+     is the declared type of the bitfield.  */
+  exp = convert_bitfield_to_declared_type (exp);
+
+  /* We do not call rvalue() here because we do not want to wrap EXP
+     in a NON_LVALUE_EXPR.  */
+
+  /* [basic.lval]
+
+     Non-class rvalues always have cv-unqualified types.  */
+  type = TREE_TYPE (exp);
+  if (!CLASS_TYPE_P (type) && cp_type_quals (type))
+    exp = build_nop (TYPE_MAIN_VARIANT (type), exp);
 
   return exp;
 }
 
-tree
-default_conversion (exp)
-     tree exp;
-{
-  tree type;
-  enum tree_code code;
+/* Perform preparatory conversions, as part of the "usual arithmetic
+   conversions".  In particular, as per [expr]:
 
-  exp = decay_conversion (exp);
+     Whenever an lvalue expression appears as an operand of an
+     operator that expects the rvalue for that operand, the
+     lvalue-to-rvalue, array-to-pointer, or function-to-pointer
+     standard conversions are applied to convert the expression to an
+     rvalue.
 
-  type = TREE_TYPE (exp);
-  code = TREE_CODE (type);
+   In addition, we perform integral promotions here, as those are
+   applied to both operands to a binary operator before determining
+   what additional conversions should apply.  */
 
-  if (INTEGRAL_CODE_P (code))
-    {
-      tree t = type_promotes_to (type);
-      if (t != type)
-       return cp_convert (t, exp);
-    }
+tree
+default_conversion (tree exp)
+{
+  /* Perform the integral promotions first so that bitfield
+     expressions (which may promote to "int", even if the bitfield is
+     declared "unsigned") are promoted correctly.  */
+  if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (exp)))
+    exp = perform_integral_promotions (exp);
+  /* Perform the other conversions.  */
+  exp = decay_conversion (exp);
 
   return exp;
 }
 
-/* Take the address of an inline function without setting TREE_ADDRESSABLE
-   or TREE_USED.  */
+/* EXPR is an expression with an integral or enumeration type.
+   Perform the integral promotions in [conv.prom], and return the
+   converted value.  */
 
 tree
-inline_conversion (exp)
-     tree exp;
+perform_integral_promotions (tree expr)
 {
-  if (TREE_CODE (exp) == FUNCTION_DECL)
-    exp = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (exp)), exp);
-
-  return exp;
+  tree type;
+  tree promoted_type;
+
+  /* [conv.prom]
+
+     If the bitfield has an enumerated type, it is treated as any
+     other value of that type for promotion purposes.  */
+  type = is_bitfield_expr_with_lowered_type (expr);
+  if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
+    type = TREE_TYPE (expr);
+  gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
+  promoted_type = type_promotes_to (type);
+  if (type != promoted_type)
+    expr = cp_convert (promoted_type, expr);
+  return expr;
 }
 
 /* Returns nonzero iff exp is a STRING_CST or the result of applying
    decay_conversion to one.  */
 
 int
-string_conv_p (totype, exp, warn)
-     tree totype, exp;
-     int warn;
+string_conv_p (const_tree totype, const_tree exp, int warn)
 {
   tree t;
 
-  if (! flag_const_strings || TREE_CODE (totype) != POINTER_TYPE)
+  if (TREE_CODE (totype) != POINTER_TYPE)
     return 0;
 
   t = TREE_TYPE (totype);
   if (!same_type_p (t, char_type_node)
+      && !same_type_p (t, char16_type_node)
+      && !same_type_p (t, char32_type_node)
       && !same_type_p (t, wchar_type_node))
     return 0;
 
   if (TREE_CODE (exp) == STRING_CST)
     {
-      /* Make sure that we don't try to convert between char and wchar_t.  */
+      /* Make sure that we don't try to convert between char and wide chars.  */
       if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t))
        return 0;
     }
@@ -1815,51 +1779,13 @@ string_conv_p (totype, exp, warn)
     }
 
   /* This warning is not very useful, as it complains about printf.  */
-  if (warn && warn_write_strings)
-    warning ("deprecated conversion from string constant to `%T'", totype);
+  if (warn)
+    warning (OPT_Wwrite_strings,
+            "deprecated conversion from string constant to %qT",
+            totype);
 
   return 1;
 }
-\f
-tree
-build_object_ref (datum, basetype, field)
-     tree datum, basetype, field;
-{
-  tree dtype;
-  if (datum == error_mark_node)
-    return error_mark_node;
-
-  dtype = TREE_TYPE (datum);
-  if (TREE_CODE (dtype) == REFERENCE_TYPE)
-    dtype = TREE_TYPE (dtype);
-  if (! IS_AGGR_TYPE_CODE (TREE_CODE (dtype)))
-    {
-      error ("request for member `%T::%D' in expression of non-aggregate type `%T'",
-               basetype, field, dtype);
-      return error_mark_node;
-    }
-  else if (is_aggr_type (basetype, 1))
-    {
-      tree binfo = binfo_or_else (basetype, dtype);
-      if (binfo)
-       return build_x_component_ref (build_scoped_ref (datum, basetype),
-                                     field, binfo, 1);
-    }
-  return error_mark_node;
-}
-
-/* Like `build_component_ref, but uses an already found field, and converts
-   from a reference.  Must compute access for current_class_ref.
-   Otherwise, ok.  */
-
-tree
-build_component_ref_1 (datum, field, protect)
-     tree datum, field;
-     int protect;
-{
-  return convert_from_reference
-    (build_component_ref (datum, field, NULL_TREE, protect));
-}
 
 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
    can, for example, use as an lvalue.  This code used to be in
@@ -1870,28 +1796,39 @@ build_component_ref_1 (datum, field, protect)
    get it there.  */
 
 static tree
-rationalize_conditional_expr (code, t)
-     enum tree_code code;
-     tree t;
+rationalize_conditional_expr (enum tree_code code, tree t,
+                              tsubst_flags_t complain)
 {
   /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
      the first operand is always the one to be used if both operands
      are equal, so we know what conditional expression this used to be.  */
   if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR)
     {
+      tree op0 = TREE_OPERAND (t, 0);
+      tree op1 = TREE_OPERAND (t, 1);
+
+      /* The following code is incorrect if either operand side-effects.  */
+      gcc_assert (!TREE_SIDE_EFFECTS (op0)
+                 && !TREE_SIDE_EFFECTS (op1));
       return
        build_conditional_expr (build_x_binary_op ((TREE_CODE (t) == MIN_EXPR
                                                    ? LE_EXPR : GE_EXPR),
-                                                  TREE_OPERAND (t, 0),
-                                                  TREE_OPERAND (t, 1)),
-                           build_unary_op (code, TREE_OPERAND (t, 0), 0),
-                           build_unary_op (code, TREE_OPERAND (t, 1), 0));
+                                                  op0, TREE_CODE (op0),
+                                                  op1, TREE_CODE (op1),
+                                                  /*overloaded_p=*/NULL,
+                                                  complain),
+                                cp_build_unary_op (code, op0, 0, complain),
+                                cp_build_unary_op (code, op1, 0, complain),
+                                complain);
     }
 
   return
     build_conditional_expr (TREE_OPERAND (t, 0),
-                           build_unary_op (code, TREE_OPERAND (t, 1), 0),
-                           build_unary_op (code, TREE_OPERAND (t, 2), 0));
+                           cp_build_unary_op (code, TREE_OPERAND (t, 1), 0,
+                                               complain),
+                           cp_build_unary_op (code, TREE_OPERAND (t, 2), 0,
+                                               complain),
+                            complain);
 }
 
 /* Given the TYPE of an anonymous union field inside T, return the
@@ -1899,9 +1836,8 @@ rationalize_conditional_expr (code, t)
    anonymous unions can nest, we must also search all anonymous unions
    that are directly reachable.  */
 
-static tree
-lookup_anon_field (t, type)
-     tree t, type;
+tree
+lookup_anon_field (tree t, tree type)
 {
   tree field;
 
@@ -1909,7 +1845,7 @@ lookup_anon_field (t, type)
     {
       if (TREE_STATIC (field))
        continue;
-      if (TREE_CODE (field) != FIELD_DECL)
+      if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
        continue;
 
       /* If we find it directly, return the field.  */
@@ -1931,333 +1867,568 @@ lookup_anon_field (t, type)
   return NULL_TREE;
 }
 
-/* Build a COMPONENT_REF for a given DATUM, and it's member COMPONENT.
-   COMPONENT can be an IDENTIFIER_NODE that is the name of the member
-   that we are interested in, or it can be a FIELD_DECL.  */
+/* Build an expression representing OBJECT.MEMBER.  OBJECT is an
+   expression; MEMBER is a DECL or baselink.  If ACCESS_PATH is
+   non-NULL, it indicates the path to the base used to name MEMBER.
+   If PRESERVE_REFERENCE is true, the expression returned will have
+   REFERENCE_TYPE if the MEMBER does.  Otherwise, the expression
+   returned will have the type referred to by the reference.
+
+   This function does not perform access control; that is either done
+   earlier by the parser when the name of MEMBER is resolved to MEMBER
+   itself, or later when overload resolution selects one of the
+   functions indicated by MEMBER.  */
 
 tree
-build_component_ref (datum, component, basetype_path, protect)
-     tree datum, component, basetype_path;
-     int protect;
+build_class_member_access_expr (tree object, tree member,
+                               tree access_path, bool preserve_reference,
+                               tsubst_flags_t complain)
 {
-  register tree basetype;
-  register enum tree_code code;
-  register tree field = NULL;
-  register tree ref;
-  tree field_type;
-  int type_quals;
-  tree old_datum;
-  tree old_basetype;
+  tree object_type;
+  tree member_scope;
+  tree result = NULL_TREE;
 
-  if (processing_template_decl)
-    return build_min_nt (COMPONENT_REF, datum, component);
-  
-  if (datum == error_mark_node 
-      || TREE_TYPE (datum) == error_mark_node)
+  if (error_operand_p (object) || error_operand_p (member))
     return error_mark_node;
 
-  /* BASETYPE holds the type of the class containing the COMPONENT.  */
-  basetype = TYPE_MAIN_VARIANT (TREE_TYPE (datum));
-    
-  /* If DATUM is a COMPOUND_EXPR or COND_EXPR, move our reference
-     inside it.  */
-  switch (TREE_CODE (datum))
+  gcc_assert (DECL_P (member) || BASELINK_P (member));
+
+  /* [expr.ref]
+
+     The type of the first expression shall be "class object" (of a
+     complete type).  */
+  object_type = TREE_TYPE (object);
+  if (!currently_open_class (object_type)
+      && !complete_type_or_else (object_type, object))
+    return error_mark_node;
+  if (!CLASS_TYPE_P (object_type))
     {
-    case COMPOUND_EXPR:
-      {
-       tree value = build_component_ref (TREE_OPERAND (datum, 1), component,
-                                         basetype_path, protect);
-       return build (COMPOUND_EXPR, TREE_TYPE (value),
-                     TREE_OPERAND (datum, 0), value);
-      }
-    case COND_EXPR:
-      return build_conditional_expr
-       (TREE_OPERAND (datum, 0),
-        build_component_ref (TREE_OPERAND (datum, 1), component,
-                             basetype_path, protect),
-        build_component_ref (TREE_OPERAND (datum, 2), component,
-                             basetype_path, protect));
-
-    case TEMPLATE_DECL:
-      error ("invalid use of `%D'", datum);
-      datum = error_mark_node;
-      break;
+      if (complain & tf_error)
+       error ("request for member %qD in %qE, which is of non-class type %qT",
+              member, object, object_type);
+      return error_mark_node;
+    }
 
-    default:
-      break;
+  /* The standard does not seem to actually say that MEMBER must be a
+     member of OBJECT_TYPE.  However, that is clearly what is
+     intended.  */
+  if (DECL_P (member))
+    {
+      member_scope = DECL_CLASS_CONTEXT (member);
+      mark_used (member);
+      if (TREE_DEPRECATED (member))
+       warn_deprecated_use (member);
+    }
+  else
+    member_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (member));
+  /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will
+     presently be the anonymous union.  Go outwards until we find a
+     type related to OBJECT_TYPE.  */
+  while (ANON_AGGR_TYPE_P (member_scope)
+        && !same_type_ignoring_top_level_qualifiers_p (member_scope,
+                                                       object_type))
+    member_scope = TYPE_CONTEXT (member_scope);
+  if (!member_scope || !DERIVED_FROM_P (member_scope, object_type))
+    {
+      if (complain & tf_error)
+       {
+         if (TREE_CODE (member) == FIELD_DECL)
+           error ("invalid use of nonstatic data member %qE", member);
+         else
+           error ("%qD is not a member of %qT", member, object_type);
+       }
+      return error_mark_node;
+    }
+
+  /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into
+     `(*(a ?  &b : &c)).x', and so on.  A COND_EXPR is only an lvalue
+     in the front end; only _DECLs and _REFs are lvalues in the back end.  */
+  {
+    tree temp = unary_complex_lvalue (ADDR_EXPR, object);
+    if (temp)
+      object = cp_build_indirect_ref (temp, NULL, complain);
+  }
+
+  /* In [expr.ref], there is an explicit list of the valid choices for
+     MEMBER.  We check for each of those cases here.  */
+  if (TREE_CODE (member) == VAR_DECL)
+    {
+      /* A static data member.  */
+      result = member;
+      /* If OBJECT has side-effects, they are supposed to occur.  */
+      if (TREE_SIDE_EFFECTS (object))
+       result = build2 (COMPOUND_EXPR, TREE_TYPE (result), object, result);
+    }
+  else if (TREE_CODE (member) == FIELD_DECL)
+    {
+      /* A non-static data member.  */
+      bool null_object_p;
+      int type_quals;
+      tree member_type;
+
+      null_object_p = (TREE_CODE (object) == INDIRECT_REF
+                      && integer_zerop (TREE_OPERAND (object, 0)));
+
+      /* Convert OBJECT to the type of MEMBER.  */
+      if (!same_type_p (TYPE_MAIN_VARIANT (object_type),
+                       TYPE_MAIN_VARIANT (member_scope)))
+       {
+         tree binfo;
+         base_kind kind;
+
+         binfo = lookup_base (access_path ? access_path : object_type,
+                              member_scope, ba_unique,  &kind);
+         if (binfo == error_mark_node)
+           return error_mark_node;
+
+         /* It is invalid to try to get to a virtual base of a
+            NULL object.  The most common cause is invalid use of
+            offsetof macro.  */
+         if (null_object_p && kind == bk_via_virtual)
+           {
+             if (complain & tf_error)
+               {
+                 error ("invalid access to non-static data member %qD of "
+                        "NULL object",
+                        member);
+                 error ("(perhaps the %<offsetof%> macro was used incorrectly)");
+               }
+             return error_mark_node;
+           }
+
+         /* Convert to the base.  */
+         object = build_base_path (PLUS_EXPR, object, binfo,
+                                   /*nonnull=*/1);
+         /* If we found the base successfully then we should be able
+            to convert to it successfully.  */
+         gcc_assert (object != error_mark_node);
+       }
+
+      /* Complain about other invalid uses of offsetof, even though they will
+        give the right answer.  Note that we complain whether or not they
+        actually used the offsetof macro, since there's no way to know at this
+        point.  So we just give a warning, instead of a pedwarn.  */
+      /* Do not produce this warning for base class field references, because
+        we know for a fact that didn't come from offsetof.  This does occur
+        in various testsuite cases where a null object is passed where a
+        vtable access is required.  */
+      if (null_object_p && warn_invalid_offsetof
+         && CLASSTYPE_NON_POD_P (object_type)
+         && !DECL_FIELD_IS_BASE (member)
+         && !skip_evaluation
+         && (complain & tf_warning))
+       {
+         warning (OPT_Winvalid_offsetof, 
+                   "invalid access to non-static data member %qD "
+                   " of NULL object", member);
+         warning (OPT_Winvalid_offsetof, 
+                   "(perhaps the %<offsetof%> macro was used incorrectly)");
+       }
+
+      /* If MEMBER is from an anonymous aggregate, we have converted
+        OBJECT so that it refers to the class containing the
+        anonymous union.  Generate a reference to the anonymous union
+        itself, and recur to find MEMBER.  */
+      if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member))
+         /* When this code is called from build_field_call, the
+            object already has the type of the anonymous union.
+            That is because the COMPONENT_REF was already
+            constructed, and was then disassembled before calling
+            build_field_call.  After the function-call code is
+            cleaned up, this waste can be eliminated.  */
+         && (!same_type_ignoring_top_level_qualifiers_p
+             (TREE_TYPE (object), DECL_CONTEXT (member))))
+       {
+         tree anonymous_union;
+
+         anonymous_union = lookup_anon_field (TREE_TYPE (object),
+                                              DECL_CONTEXT (member));
+         object = build_class_member_access_expr (object,
+                                                  anonymous_union,
+                                                  /*access_path=*/NULL_TREE,
+                                                  preserve_reference,
+                                                  complain);
+       }
+
+      /* Compute the type of the field, as described in [expr.ref].  */
+      type_quals = TYPE_UNQUALIFIED;
+      member_type = TREE_TYPE (member);
+      if (TREE_CODE (member_type) != REFERENCE_TYPE)
+       {
+         type_quals = (cp_type_quals (member_type)
+                       | cp_type_quals (object_type));
+
+         /* A field is const (volatile) if the enclosing object, or the
+            field itself, is const (volatile).  But, a mutable field is
+            not const, even within a const object.  */
+         if (DECL_MUTABLE_P (member))
+           type_quals &= ~TYPE_QUAL_CONST;
+         member_type = cp_build_qualified_type (member_type, type_quals);
+       }
+
+      result = build3 (COMPONENT_REF, member_type, object, member,
+                      NULL_TREE);
+      result = fold_if_not_in_template (result);
+
+      /* Mark the expression const or volatile, as appropriate.  Even
+        though we've dealt with the type above, we still have to mark the
+        expression itself.  */
+      if (type_quals & TYPE_QUAL_CONST)
+       TREE_READONLY (result) = 1;
+      if (type_quals & TYPE_QUAL_VOLATILE)
+       TREE_THIS_VOLATILE (result) = 1;
+    }
+  else if (BASELINK_P (member))
+    {
+      /* The member is a (possibly overloaded) member function.  */
+      tree functions;
+      tree type;
+
+      /* If the MEMBER is exactly one static member function, then we
+        know the type of the expression.  Otherwise, we must wait
+        until overload resolution has been performed.  */
+      functions = BASELINK_FUNCTIONS (member);
+      if (TREE_CODE (functions) == FUNCTION_DECL
+         && DECL_STATIC_FUNCTION_P (functions))
+       type = TREE_TYPE (functions);
+      else
+       type = unknown_type_node;
+      /* Note that we do not convert OBJECT to the BASELINK_BINFO
+        base.  That will happen when the function is called.  */
+      result = build3 (COMPONENT_REF, type, object, member, NULL_TREE);
+    }
+  else if (TREE_CODE (member) == CONST_DECL)
+    {
+      /* The member is an enumerator.  */
+      result = member;
+      /* If OBJECT has side-effects, they are supposed to occur.  */
+      if (TREE_SIDE_EFFECTS (object))
+       result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
+                        object, result);
+    }
+  else
+    {
+      if (complain & tf_error)
+       error ("invalid use of %qD", member);
+      return error_mark_node;
     }
 
-  code = TREE_CODE (basetype);
+  if (!preserve_reference)
+    /* [expr.ref]
+
+       If E2 is declared to have type "reference to T", then ... the
+       type of E1.E2 is T.  */
+    result = convert_from_reference (result);
+
+  return result;
+}
+
+/* Return the destructor denoted by OBJECT.SCOPE::DTOR_NAME, or, if
+   SCOPE is NULL, by OBJECT.DTOR_NAME, where DTOR_NAME is ~type.  */
 
-  if (code == REFERENCE_TYPE)
+static tree
+lookup_destructor (tree object, tree scope, tree dtor_name)
+{
+  tree object_type = TREE_TYPE (object);
+  tree dtor_type = TREE_OPERAND (dtor_name, 0);
+  tree expr;
+
+  if (scope && !check_dtor_name (scope, dtor_type))
+    {
+      error ("qualified type %qT does not match destructor name ~%qT",
+            scope, dtor_type);
+      return error_mark_node;
+    }
+  if (TREE_CODE (dtor_type) == IDENTIFIER_NODE)
     {
-      datum = convert_from_reference (datum);
-      basetype = TYPE_MAIN_VARIANT (TREE_TYPE (datum));
-      code = TREE_CODE (basetype);
+      /* In a template, names we can't find a match for are still accepted
+        destructor names, and we check them here.  */
+      if (check_dtor_name (object_type, dtor_type))
+       dtor_type = object_type;
+      else
+       {
+         error ("object type %qT does not match destructor name ~%qT",
+                object_type, dtor_type);
+         return error_mark_node;
+       }
+      
     }
-  if (TREE_CODE (datum) == OFFSET_REF)
+  else if (!DERIVED_FROM_P (dtor_type, TYPE_MAIN_VARIANT (object_type)))
     {
-      datum = resolve_offset_ref (datum);
-      basetype = TYPE_MAIN_VARIANT (TREE_TYPE (datum));
-      code = TREE_CODE (basetype);
+      error ("the type being destroyed is %qT, but the destructor refers to %qT",
+            TYPE_MAIN_VARIANT (object_type), dtor_type);
+      return error_mark_node;
     }
+  expr = lookup_member (dtor_type, complete_dtor_identifier,
+                       /*protect=*/1, /*want_type=*/false);
+  expr = (adjust_result_of_qualified_name_lookup
+         (expr, dtor_type, object_type));
+  return expr;
+}
 
-  /* First, see if there is a field or component with name COMPONENT.  */
-  if (TREE_CODE (component) == TREE_LIST)
-    {
-      /* I could not trigger this code. MvL */
-      abort ();
-#ifdef DEAD
-      my_friendly_assert (!(TREE_CHAIN (component) == NULL_TREE
-               && DECL_CHAIN (TREE_VALUE (component)) == NULL_TREE), 309);
-#endif
-      return build (COMPONENT_REF, TREE_TYPE (component), datum, component);
-    }
+/* An expression of the form "A::template B" has been resolved to
+   DECL.  Issue a diagnostic if B is not a template or template
+   specialization.  */
 
-  if (! IS_AGGR_TYPE_CODE (code))
-    {
-      if (code != ERROR_MARK)
-       error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
-                 component, datum, basetype);
-      return error_mark_node;
-    }
+void
+check_template_keyword (tree decl)
+{
+  /* The standard says:
 
-  if (!complete_type_or_else (basetype, datum))
-    return error_mark_node;
+      [temp.names]
 
-  if (TREE_CODE (component) == BIT_NOT_EXPR)
+      If a name prefixed by the keyword template is not a member
+      template, the program is ill-formed.
+
+     DR 228 removed the restriction that the template be a member
+     template.
+
+     DR 96, if accepted would add the further restriction that explicit
+     template arguments must be provided if the template keyword is
+     used, but, as of 2005-10-16, that DR is still in "drafting".  If
+     this DR is accepted, then the semantic checks here can be
+     simplified, as the entity named must in fact be a template
+     specialization, rather than, as at present, a set of overloaded
+     functions containing at least one template function.  */
+  if (TREE_CODE (decl) != TEMPLATE_DECL
+      && TREE_CODE (decl) != TEMPLATE_ID_EXPR)
     {
-      if (TYPE_IDENTIFIER (basetype) != TREE_OPERAND (component, 0))
-       {
-         error ("destructor specifier `%T::~%T' must have matching names",
-                   basetype, TREE_OPERAND (component, 0));
-         return error_mark_node;
-       }
-      if (! TYPE_HAS_DESTRUCTOR (basetype))
+      if (!is_overloaded_fn (decl))
+       permerror (input_location, "%qD is not a template", decl);
+      else
        {
-         error ("type `%T' has no destructor", basetype);
-         return error_mark_node;
+         tree fns;
+         fns = decl;
+         if (BASELINK_P (fns))
+           fns = BASELINK_FUNCTIONS (fns);
+         while (fns)
+           {
+             tree fn = OVL_CURRENT (fns);
+             if (TREE_CODE (fn) == TEMPLATE_DECL
+                 || TREE_CODE (fn) == TEMPLATE_ID_EXPR)
+               break;
+             if (TREE_CODE (fn) == FUNCTION_DECL
+                 && DECL_USE_TEMPLATE (fn)
+                 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (fn)))
+               break;
+             fns = OVL_NEXT (fns);
+           }
+         if (!fns)
+           permerror (input_location, "%qD is not a template", decl);
        }
-      return TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype), 1);
     }
+}
+
+/* This function is called by the parser to process a class member
+   access expression of the form OBJECT.NAME.  NAME is a node used by
+   the parser to represent a name; it is not yet a DECL.  It may,
+   however, be a BASELINK where the BASELINK_FUNCTIONS is a
+   TEMPLATE_ID_EXPR.  Templates must be looked up by the parser, and
+   there is no reason to do the lookup twice, so the parser keeps the
+   BASELINK.  TEMPLATE_P is true iff NAME was explicitly declared to
+   be a template via the use of the "A::template B" syntax.  */
+
+tree
+finish_class_member_access_expr (tree object, tree name, bool template_p,
+                                tsubst_flags_t complain)
+{
+  tree expr;
+  tree object_type;
+  tree member;
+  tree access_path = NULL_TREE;
+  tree orig_object = object;
+  tree orig_name = name;
+
+  if (object == error_mark_node || name == error_mark_node)
+    return error_mark_node;
+
+  /* If OBJECT is an ObjC class instance, we must obey ObjC access rules.  */
+  if (!objc_is_public (object, name))
+    return error_mark_node;
+
+  object_type = TREE_TYPE (object);
 
-  /* Look up component name in the structure type definition.  */
-  if (TYPE_VFIELD (basetype)
-      && DECL_NAME (TYPE_VFIELD (basetype)) == component)
-    /* Special-case this because if we use normal lookups in an ambiguous
-       hierarchy, the compiler will abort (because vptr lookups are
-       not supposed to be ambiguous.  */
-    field = TYPE_VFIELD (basetype);
-  else if (TREE_CODE (component) == FIELD_DECL)
-    field = component;
-  else if (TREE_CODE (component) == TYPE_DECL)
+  if (processing_template_decl)
     {
-      error ("invalid use of type decl `%#D' as expression", component);
-      return error_mark_node;
-    }
-  else if (TREE_CODE (component) == TEMPLATE_DECL)
+      if (/* If OBJECT_TYPE is dependent, so is OBJECT.NAME.  */
+         dependent_type_p (object_type)
+         /* If NAME is just an IDENTIFIER_NODE, then the expression
+            is dependent.  */
+         || TREE_CODE (object) == IDENTIFIER_NODE
+         /* If NAME is "f<args>", where either 'f' or 'args' is
+            dependent, then the expression is dependent.  */
+         || (TREE_CODE (name) == TEMPLATE_ID_EXPR
+             && dependent_template_id_p (TREE_OPERAND (name, 0),
+                                         TREE_OPERAND (name, 1)))
+         /* If NAME is "T::X" where "T" is dependent, then the
+            expression is dependent.  */
+         || (TREE_CODE (name) == SCOPE_REF
+             && TYPE_P (TREE_OPERAND (name, 0))
+             && dependent_type_p (TREE_OPERAND (name, 0))))
+       return build_min_nt (COMPONENT_REF, object, name, NULL_TREE);
+      object = build_non_dependent_expr (object);
+    }
+
+  /* [expr.ref]
+
+     The type of the first expression shall be "class object" (of a
+     complete type).  */
+  if (!currently_open_class (object_type)
+      && !complete_type_or_else (object_type, object))
+    return error_mark_node;
+  if (!CLASS_TYPE_P (object_type))
     {
-      error ("invalid use of template `%#D' as expression", component);
+      if (complain & tf_error)
+       error ("request for member %qD in %qE, which is of non-class type %qT",
+              name, object, object_type);
       return error_mark_node;
     }
+
+  if (BASELINK_P (name))
+    /* A member function that has already been looked up.  */
+    member = name;
   else
     {
-      tree name = component;
-      
-      if (TREE_CODE (component) == TEMPLATE_ID_EXPR)
-       name = TREE_OPERAND (component, 0);
-      else if (TREE_CODE (component) == VAR_DECL)
-       name = DECL_NAME (component);
-      if (TREE_CODE (component) == NAMESPACE_DECL)
-        /* Source is in error, but produce a sensible diagnostic.  */
-        name = DECL_NAME (component);
-      if (basetype_path == NULL_TREE)
-       basetype_path = TYPE_BINFO (basetype);
-      field = lookup_field (basetype_path, name,
-                           protect && !VFIELD_NAME_P (name), 0);
-      if (field == error_mark_node)
-       return error_mark_node;
+      bool is_template_id = false;
+      tree template_args = NULL_TREE;
+      tree scope;
 
-      if (field == NULL_TREE)
+      if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
        {
-         /* Not found as a data field, look for it as a method.  If found,
-            then if this is the only possible one, return it, else
-            report ambiguity error.  */
-         tree fndecls = lookup_fnfields (basetype_path, name, 1);
-         if (fndecls == error_mark_node)
-           return error_mark_node;
-         if (fndecls)
+         is_template_id = true;
+         template_args = TREE_OPERAND (name, 1);
+         name = TREE_OPERAND (name, 0);
+
+         if (TREE_CODE (name) == OVERLOAD)
+           name = DECL_NAME (get_first_fn (name));
+         else if (DECL_P (name))
+           name = DECL_NAME (name);
+       }
+
+      if (TREE_CODE (name) == SCOPE_REF)
+       {
+         /* A qualified name.  The qualifying class or namespace `S'
+            has already been looked up; it is either a TYPE or a
+            NAMESPACE_DECL.  */
+         scope = TREE_OPERAND (name, 0);
+         name = TREE_OPERAND (name, 1);
+
+         /* If SCOPE is a namespace, then the qualified name does not
+            name a member of OBJECT_TYPE.  */
+         if (TREE_CODE (scope) == NAMESPACE_DECL)
            {
-             /* If the function is unique and static, we can resolve it
-                now.  Otherwise, we have to wait and see what context it is
-                used in; a component_ref involving a non-static member
-                function can only be used in a call (expr.ref).  */
+             if (complain & tf_error)
+               error ("%<%D::%D%> is not a member of %qT",
+                      scope, name, object_type);
+             return error_mark_node;
+           }
 
-             if (TREE_CHAIN (fndecls) == NULL_TREE
-                 && TREE_CODE (TREE_VALUE (fndecls)) == FUNCTION_DECL)
-               {
-                 if (DECL_STATIC_FUNCTION_P (TREE_VALUE (fndecls)))
-                   {
-                     tree fndecl = TREE_VALUE (fndecls);
-                     enforce_access (basetype_path, fndecl);
-                     mark_used (fndecl);
-                     return fndecl;
-                   }
-                 else
-                   {
-                     /* A unique non-static member function.  Other parts
-                        of the compiler expect something with
-                        unknown_type_node to be really overloaded, so
-                        let's oblige.  */
-                     TREE_VALUE (fndecls)
-                       = ovl_cons (TREE_VALUE (fndecls), NULL_TREE);
-                   }
-               }
+         gcc_assert (CLASS_TYPE_P (scope));
+         gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE
+                     || TREE_CODE (name) == BIT_NOT_EXPR);
 
-             fndecls = TREE_VALUE (fndecls);
-             
-             if (TREE_CODE (component) == TEMPLATE_ID_EXPR)
-               fndecls = build_nt (TEMPLATE_ID_EXPR,
-                                   fndecls, TREE_OPERAND (component, 1));
-             
-             ref = build (COMPONENT_REF, unknown_type_node,
-                          datum, fndecls);
-             return ref;
+         /* Find the base of OBJECT_TYPE corresponding to SCOPE.  */
+         access_path = lookup_base (object_type, scope, ba_check, NULL);
+         if (access_path == error_mark_node)
+           return error_mark_node;
+         if (!access_path)
+           {
+             if (complain & tf_error)
+               error ("%qT is not a base of %qT", scope, object_type);
+             return error_mark_node;
            }
-
-         error ("`%#T' has no member named `%D'", basetype, name);
-         return error_mark_node;
        }
-      else if (TREE_TYPE (field) == error_mark_node)
-       return error_mark_node;
-
-      if (TREE_CODE (field) != FIELD_DECL)
+      else
        {
-         if (TREE_CODE (field) == TYPE_DECL)
-           pedwarn ("invalid use of type decl `%#D' as expression", field);
-         else if (DECL_RTL (field) != 0)
-           mark_used (field);
-         else
-           TREE_USED (field) = 1;
-
-         /* Do evaluate the object when accessing a static member.  */
-         if (TREE_SIDE_EFFECTS (datum))
-           field = build (COMPOUND_EXPR, TREE_TYPE (field), datum, field);
-
-         return field;
+         scope = NULL_TREE;
+         access_path = object_type;
        }
-    }
-
-  if (TREE_DEPRECATED (field))
-    warn_deprecated_use (field);
-
-  old_datum = datum;
-  old_basetype = basetype;
-
-  /* See if we have to do any conversions so that we pick up the field from the
-     right context.  */
-  if (DECL_FIELD_CONTEXT (field) != basetype)
-    {
-      tree context = DECL_FIELD_CONTEXT (field);
-      tree base = context;
-      while (!same_type_p (base, basetype) && TYPE_NAME (base)
-            && ANON_AGGR_TYPE_P (base))
-       base = TYPE_CONTEXT (base);
 
-      /* Handle base classes here...  */
-      if (base != basetype && TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (basetype))
+      if (TREE_CODE (name) == BIT_NOT_EXPR)
+       member = lookup_destructor (object, scope, name);
+      else
        {
-         base_kind kind;
-         tree binfo = lookup_base (TREE_TYPE (datum), base, ba_check, &kind);
-
-         /* Complain about use of offsetof which will break.  */
-         if (TREE_CODE (datum) == INDIRECT_REF
-             && integer_zerop (TREE_OPERAND (datum, 0))
-             && kind == bk_via_virtual)
+         /* Look up the member.  */
+         member = lookup_member (access_path, name, /*protect=*/1,
+                                 /*want_type=*/false);
+         if (member == NULL_TREE)
            {
-             error ("\
-invalid offsetof from non-POD type `%#T'; use pointer to member instead",
-                    basetype);
+             if (complain & tf_error)
+               error ("%qD has no member named %qE", object_type, name);
              return error_mark_node;
            }
-         datum = build_base_path (PLUS_EXPR, datum, binfo, 1);
-         if (datum == error_mark_node)
+         if (member == error_mark_node)
            return error_mark_node;
        }
-      basetype = base;
-      /* Handle things from anon unions here...  */
-      if (TYPE_NAME (context) && ANON_AGGR_TYPE_P (context))
+
+      if (is_template_id)
        {
-         tree subfield = lookup_anon_field (basetype, context);
-         tree subdatum = build_component_ref (datum, subfield,
-                                              basetype_path, protect);
-         return build_component_ref (subdatum, field, basetype_path, protect);
+         tree templ = member;
+
+         if (BASELINK_P (templ))
+           templ = lookup_template_function (templ, template_args);
+         else
+           {
+             if (complain & tf_error)
+               error ("%qD is not a member template function", name);
+             return error_mark_node;
+           }
        }
     }
 
-  /* Complain about other invalid uses of offsetof, even though they will
-     give the right answer.  Note that we complain whether or not they
-     actually used the offsetof macro, since there's no way to know at this
-     point.  So we just give a warning, instead of a pedwarn.  */
-  if (protect
-      && CLASSTYPE_NON_POD_P (old_basetype)
-      && TREE_CODE (old_datum) == INDIRECT_REF
-      && integer_zerop (TREE_OPERAND (old_datum, 0)))
-    warning ("\
-invalid offsetof from non-POD type `%#T'; use pointer to member instead",
-            basetype);
-
-  /* Compute the type of the field, as described in [expr.ref].  */
-  type_quals = TYPE_UNQUALIFIED;
-  field_type = TREE_TYPE (field);
-  if (TREE_CODE (field_type) == REFERENCE_TYPE)
-    /* The standard says that the type of the result should be the
-       type referred to by the reference.  But for now, at least, we
-       do the conversion from reference type later.  */
-    ;
-  else
-    {
-      type_quals = (cp_type_quals (field_type)  
-                   | cp_type_quals (TREE_TYPE (datum)));
-
-      /* A field is const (volatile) if the enclosing object, or the
-        field itself, is const (volatile).  But, a mutable field is
-        not const, even within a const object.  */
-      if (DECL_MUTABLE_P (field))
-       type_quals &= ~TYPE_QUAL_CONST;
-      field_type = cp_build_qualified_type (field_type, type_quals);
-    }
+  if (TREE_DEPRECATED (member))
+    warn_deprecated_use (member);
 
-  ref = fold (build (COMPONENT_REF, field_type, datum, field));
+  if (template_p)
+    check_template_keyword (member);
 
-  /* Mark the expression const or volatile, as appropriate.  Even
-     though we've dealt with the type above, we still have to mark the
-     expression itself.  */
-  if (type_quals & TYPE_QUAL_CONST)
-    TREE_READONLY (ref) = 1;
-  else if (type_quals & TYPE_QUAL_VOLATILE)
-    TREE_THIS_VOLATILE (ref) = 1;
+  expr = build_class_member_access_expr (object, member, access_path,
+                                        /*preserve_reference=*/false,
+                                        complain);
+  if (processing_template_decl && expr != error_mark_node)
+    {
+      if (BASELINK_P (member))
+       {
+         if (TREE_CODE (orig_name) == SCOPE_REF)
+           BASELINK_QUALIFIED_P (member) = 1;
+         orig_name = member;
+       }
+      return build_min_non_dep (COMPONENT_REF, expr,
+                               orig_object, orig_name,
+                               NULL_TREE);
+    }
 
-  return ref;
+  return expr;
 }
 
-/* Variant of build_component_ref for use in expressions, which should
-   never have REFERENCE_TYPE.  */
+/* Return an expression for the MEMBER_NAME field in the internal
+   representation of PTRMEM, a pointer-to-member function.  (Each
+   pointer-to-member function type gets its own RECORD_TYPE so it is
+   more convenient to access the fields by name than by FIELD_DECL.)
+   This routine converts the NAME to a FIELD_DECL and then creates the
+   node for the complete expression.  */
 
 tree
-build_x_component_ref (datum, component, basetype_path, protect)
-     tree datum, component, basetype_path;
-     int protect;
+build_ptrmemfunc_access_expr (tree ptrmem, tree member_name)
 {
-  tree t = build_component_ref (datum, component, basetype_path, protect);
-
-  if (! processing_template_decl)
-    t = convert_from_reference (t);
-
-  return t;
+  tree ptrmem_type;
+  tree member;
+  tree member_type;
+
+  /* This code is a stripped down version of
+     build_class_member_access_expr.  It does not work to use that
+     routine directly because it expects the object to be of class
+     type.  */
+  ptrmem_type = TREE_TYPE (ptrmem);
+  gcc_assert (TYPE_PTRMEMFUNC_P (ptrmem_type));
+  member = lookup_member (ptrmem_type, member_name, /*protect=*/0,
+                         /*want_type=*/false);
+  member_type = cp_build_qualified_type (TREE_TYPE (member),
+                                        cp_type_quals (ptrmem_type));
+  return fold_build3 (COMPONENT_REF, member_type,
+                     ptrmem, member, NULL_TREE);
 }
-\f
+
 /* Given an expression PTR for a pointer, return an expression
    for the value pointed to.
    ERRORSTRING is the name of the operator to appear in error messages.
@@ -2266,28 +2437,47 @@ build_x_component_ref (datum, component, basetype_path, protect)
    Must also handle REFERENCE_TYPEs for C++.  */
 
 tree
-build_x_indirect_ref (ptr, errorstring)
-     tree ptr;
-     const char *errorstring;
+build_x_indirect_ref (tree expr, const char *errorstring, 
+                      tsubst_flags_t complain)
 {
+  tree orig_expr = expr;
   tree rval;
 
   if (processing_template_decl)
-    return build_min_nt (INDIRECT_REF, ptr);
+    {
+      /* Retain the type if we know the operand is a pointer so that
+        describable_type doesn't make auto deduction break.  */
+      if (TREE_TYPE (expr) && POINTER_TYPE_P (TREE_TYPE (expr)))
+       return build_min (INDIRECT_REF, TREE_TYPE (TREE_TYPE (expr)), expr);
+      if (type_dependent_expression_p (expr))
+       return build_min_nt (INDIRECT_REF, expr);
+      expr = build_non_dependent_expr (expr);
+    }
+
+  rval = build_new_op (INDIRECT_REF, LOOKUP_NORMAL, expr, NULL_TREE,
+                      NULL_TREE, /*overloaded_p=*/NULL, complain);
+  if (!rval)
+    rval = cp_build_indirect_ref (expr, errorstring, complain);
 
-  rval = build_opfncall (INDIRECT_REF, LOOKUP_NORMAL, ptr, NULL_TREE,
-                        NULL_TREE);
-  if (rval)
+  if (processing_template_decl && rval != error_mark_node)
+    return build_min_non_dep (INDIRECT_REF, rval, orig_expr);
+  else
     return rval;
-  return build_indirect_ref (ptr, errorstring);
+}
+
+/* Helper function called from c-common.  */
+tree
+build_indirect_ref (location_t loc __attribute__ ((__unused__)),
+                   tree ptr, const char *errorstring)
+{
+  return cp_build_indirect_ref (ptr, errorstring, tf_warning_or_error);
 }
 
 tree
-build_indirect_ref (ptr, errorstring)
-     tree ptr;
-     const char *errorstring;
+cp_build_indirect_ref (tree ptr, const char *errorstring, 
+                       tsubst_flags_t complain)
 {
-  register tree pointer, type;
+  tree pointer, type;
 
   if (ptr == error_mark_node)
     return error_mark_node;
@@ -2296,31 +2486,43 @@ build_indirect_ref (ptr, errorstring)
     return current_class_ref;
 
   pointer = (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
-            ? ptr : default_conversion (ptr));
+            ? ptr : decay_conversion (ptr));
   type = TREE_TYPE (pointer);
 
-  if (TYPE_PTR_P (type) || TREE_CODE (type) == REFERENCE_TYPE)
+  if (POINTER_TYPE_P (type))
     {
       /* [expr.unary.op]
-        
+
         If the type of the expression is "pointer to T," the type
-        of  the  result  is  "T."   
+        of  the  result  is  "T."
 
-         We must use the canonical variant because certain parts of
+        We must use the canonical variant because certain parts of
         the back end, like fold, do pointer comparisons between
         types.  */
       tree t = canonical_type_variant (TREE_TYPE (type));
 
+      if (CONVERT_EXPR_P (ptr)
+          || TREE_CODE (ptr) == VIEW_CONVERT_EXPR)
+       {
+         /* If a warning is issued, mark it to avoid duplicates from
+            the backend.  This only needs to be done at
+            warn_strict_aliasing > 2.  */
+         if (warn_strict_aliasing > 2)
+           if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (ptr, 0)),
+                                        type, TREE_OPERAND (ptr, 0)))
+             TREE_NO_WARNING (ptr) = 1;
+       }
+
       if (VOID_TYPE_P (t))
-        {
-          /* A pointer to incomplete type (other than cv void) can be
-             dereferenced [expr.unary.op]/1  */
-          error ("`%T' is not a pointer-to-object type", type);
-          return error_mark_node;
-        }
+       {
+         /* A pointer to incomplete type (other than cv void) can be
+            dereferenced [expr.unary.op]/1  */
+          if (complain & tf_error)
+            error ("%qT is not a pointer-to-object type", type);
+         return error_mark_node;
+       }
       else if (TREE_CODE (pointer) == ADDR_EXPR
-         && !flag_volatile
-         && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
+              && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
        /* The POINTER was something like `&x'.  We simplify `*&x' to
           `x'.  */
        return TREE_OPERAND (pointer, 0);
@@ -2334,19 +2536,21 @@ build_indirect_ref (ptr, errorstring)
          TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
          TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
          TREE_SIDE_EFFECTS (ref)
-           = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer)
-              || flag_volatile);
+           = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer));
          return ref;
        }
     }
+  else if (!(complain & tf_error))
+    /* Don't emit any errors; we'll just return ERROR_MARK_NODE later.  */
+    ;
   /* `pointer' won't be an error_mark_node if we were given a
      pointer to member, so it's cool to check for this here.  */
-  else if (TYPE_PTRMEM_P (type) || TYPE_PTRMEMFUNC_P (type))
-    error ("invalid use of `%s' on pointer to member", errorstring);
+  else if (TYPE_PTR_TO_MEMBER_P (type))
+    error ("invalid use of %qs on pointer to member", errorstring);
   else if (pointer != error_mark_node)
     {
       if (errorstring)
-       error ("invalid type argument of `%s'", errorstring);
+       error ("invalid type argument of %qs", errorstring);
       else
        error ("invalid type argument");
     }
@@ -2364,15 +2568,18 @@ build_indirect_ref (ptr, errorstring)
 
    If INDEX is of some user-defined type, it must be converted to
    integer type.  Otherwise, to make a compatible PLUS_EXPR, it
-   will inherit the type of the array, which will be some pointer type.  */
+   will inherit the type of the array, which will be some pointer type.
+   
+   LOC is the location to use in building the array reference.  */
 
 tree
-build_array_ref (array, idx)
-     tree array, idx;
+build_array_ref (tree array, tree idx, location_t loc)
 {
+  tree ret;
+
   if (idx == 0)
     {
-      error ("subscript missing in array reference");
+      error_at (loc, "subscript missing in array reference");
       return error_mark_node;
     }
 
@@ -2386,45 +2593,45 @@ build_array_ref (array, idx)
     {
     case COMPOUND_EXPR:
       {
-       tree value = build_array_ref (TREE_OPERAND (array, 1), idx);
-       return build (COMPOUND_EXPR, TREE_TYPE (value),
+       tree value = build_array_ref (TREE_OPERAND (array, 1), idx, loc);
+       ret = build2 (COMPOUND_EXPR, TREE_TYPE (value),
                      TREE_OPERAND (array, 0), value);
+       SET_EXPR_LOCATION (ret, loc);
+       return ret;
       }
 
     case COND_EXPR:
-      return build_conditional_expr
-       (TREE_OPERAND (array, 0),
-        build_array_ref (TREE_OPERAND (array, 1), idx),
-        build_array_ref (TREE_OPERAND (array, 2), idx));
+      ret = build_conditional_expr
+             (TREE_OPERAND (array, 0),
+             build_array_ref (TREE_OPERAND (array, 1), idx, loc),
+             build_array_ref (TREE_OPERAND (array, 2), idx, loc),
+             tf_warning_or_error);
+      protected_set_expr_location (ret, loc);
+      return ret;
 
     default:
       break;
     }
 
-  if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE
-      && TREE_CODE (array) != INDIRECT_REF)
+  if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
     {
       tree rval, type;
 
-      /* Subscripting with type char is likely to lose
-        on a machine where chars are signed.
-        So warn on any machine, but optionally.
-        Don't warn for unsigned char since that type is safe.
-        Don't warn for signed char because anyone who uses that
-        must have done so deliberately.  */
-      if (warn_char_subscripts
-         && TYPE_MAIN_VARIANT (TREE_TYPE (idx)) == char_type_node)
-       warning ("array subscript has type `char'");
-
-      /* Apply default promotions *after* noticing character types.  */
-      idx = default_conversion (idx);
+      warn_array_subscript_with_type_char (idx);
 
-      if (TREE_CODE (TREE_TYPE (idx)) != INTEGER_TYPE)
+      if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (idx)))
        {
-         error ("array subscript is not an integer");
+         error_at (loc, "array subscript is not an integer");
          return error_mark_node;
        }
 
+      /* Apply integral promotions *after* noticing character types.
+        (It is unclear why we do these promotions -- the standard
+        does not say that we should.  In fact, the natural thing would
+        seem to be to convert IDX to ptrdiff_t; we're performing
+        pointer arithmetic.)  */
+      idx = perform_integral_promotions (idx);
+
       /* An array that is indexed by a non-constant
         cannot be stored in a register; we must be able to do
         address arithmetic on its address.
@@ -2434,7 +2641,7 @@ build_array_ref (array, idx)
              && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
                  != INTEGER_CST)))
        {
-         if (mark_addressable (array) == 0)
+         if (!cxx_mark_addressable (array))
            return error_mark_node;
        }
 
@@ -2443,15 +2650,16 @@ build_array_ref (array, idx)
         would get a crash in store_bit_field/extract_bit_field when trying
         to access a non-existent part of the register.  */
       if (TREE_CODE (idx) == INTEGER_CST
-         && TYPE_VALUES (TREE_TYPE (array))
-         && ! int_fits_type_p (idx, TYPE_VALUES (TREE_TYPE (array))))
+         && TYPE_DOMAIN (TREE_TYPE (array))
+         && ! int_fits_type_p (idx, TYPE_DOMAIN (TREE_TYPE (array))))
        {
-         if (mark_addressable (array) == 0)
+         if (!cxx_mark_addressable (array))
            return error_mark_node;
        }
 
-      if (pedantic && !lvalue_p (array))
-       pedwarn ("ISO C++ forbids subscripting non-lvalue array");
+      if (!lvalue_p (array))
+       pedwarn (loc, OPT_pedantic, 
+                "ISO C++ forbids subscripting non-lvalue array");
 
       /* Note in C++ it is valid to subscript a `register' array, since
         it is valid to take the address of something with that
@@ -2462,11 +2670,12 @@ build_array_ref (array, idx)
          while (TREE_CODE (foo) == COMPONENT_REF)
            foo = TREE_OPERAND (foo, 0);
          if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo))
-           warning ("subscripting array declared `register'");
+           warning_at (loc, OPT_Wextra,
+                       "subscripting array declared %<register%>");
        }
 
       type = TREE_TYPE (TREE_TYPE (array));
-      rval = build (ARRAY_REF, type, array, idx);
+      rval = build4 (ARRAY_REF, type, array, idx, NULL_TREE, NULL_TREE);
       /* Array ref is const/volatile if the array elements are
         or if the array is..  */
       TREE_READONLY (rval)
@@ -2475,7 +2684,9 @@ build_array_ref (array, idx)
        |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array));
       TREE_THIS_VOLATILE (rval)
        |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array));
-      return require_complete_type (fold (rval));
+      ret = require_complete_type (fold_if_not_in_template (rval));
+      protected_set_expr_location (ret, loc);
+      return ret;
     }
 
   {
@@ -2495,458 +2706,183 @@ build_array_ref (array, idx)
 
     if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE)
       {
-       error ("subscripted value is neither array nor pointer");
+       error_at (loc, "subscripted value is neither array nor pointer");
        return error_mark_node;
       }
     if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
       {
-       error ("array subscript is not an integer");
+       error_at (loc, "array subscript is not an integer");
        return error_mark_node;
       }
 
-    return build_indirect_ref (cp_build_binary_op (PLUS_EXPR, ar, ind),
-                              "array indexing");
+    warn_array_subscript_with_type_char (idx);
+
+    ret = cp_build_indirect_ref (cp_build_binary_op (input_location,
+                                                    PLUS_EXPR, ar, ind,
+                                                    tf_warning_or_error),
+                                 "array indexing",
+                                 tf_warning_or_error);
+    protected_set_expr_location (ret, loc);
+    return ret;
   }
 }
 \f
-/* Build a function call to function FUNCTION with parameters PARAMS.
-   PARAMS is a list--a chain of TREE_LIST nodes--in which the
-   TREE_VALUE of each node is a parameter-expression.  The PARAMS do
-   not include any object pointer that may be required.  FUNCTION's
-   data type may be a function type or a pointer-to-function.
-
-   For C++: If FUNCTION's data type is a TREE_LIST, then the tree list
-   is the list of possible methods that FUNCTION could conceivably
-   be.  If the list of methods comes from a class, then it will be
-   a list of lists (where each element is associated with the class
-   that produced it), otherwise it will be a simple list (for
-   functions overloaded in global scope).
-
-   In the first case, TREE_VALUE (function) is the head of one of those
-   lists, and TREE_PURPOSE is the name of the function.
-
-   In the second case, TREE_PURPOSE (function) is the function's
-   name directly.
-
-   DECL is the class instance variable, usually CURRENT_CLASS_REF.
-
-   When calling a TEMPLATE_DECL, we don't require a complete return
-   type.  */
-
-tree
-build_x_function_call (function, params, decl)
-     tree function, params, decl;
-{
-  tree type;
-  tree template_id = NULL_TREE;
-  int is_method;
-
-  if (function == error_mark_node)
-    return error_mark_node;
-
-  if (processing_template_decl)
-    return build_min_nt (CALL_EXPR, function, params, NULL_TREE);
-
-  /* Save explicit template arguments if found */
-  if (TREE_CODE (function) == TEMPLATE_ID_EXPR)
-    {
-      template_id = function;
-      function = TREE_OPERAND (function, 0);
-    }
-
-  type = TREE_TYPE (function);
-
-  if (TREE_CODE (type) == OFFSET_TYPE
-      && TREE_TYPE (type) == unknown_type_node
-      && TREE_CODE (function) == TREE_LIST
-      && TREE_CHAIN (function) == NULL_TREE)
-    {
-      /* Undo (Foo:bar)()...  */
-      type = TYPE_OFFSET_BASETYPE (type);
-      function = TREE_VALUE (function);
-      my_friendly_assert (TREE_CODE (function) == TREE_LIST, 999);
-      my_friendly_assert (TREE_CHAIN (function) == NULL_TREE, 999);
-      function = TREE_VALUE (function);
-      if (TREE_CODE (function) == OVERLOAD)
-       function = OVL_FUNCTION (function);
-      my_friendly_assert (TREE_CODE (function) == FUNCTION_DECL, 999);
-      function = DECL_NAME (function);
-      return build_method_call (decl, function, params,
-                               TYPE_BINFO (type), LOOKUP_NORMAL);
-    }
-    
-  if (TREE_CODE (function) == OFFSET_REF
-      && TREE_CODE (type) != METHOD_TYPE)
-    function = resolve_offset_ref (function);
-
-  if ((TREE_CODE (function) == FUNCTION_DECL
-       && DECL_STATIC_FUNCTION_P (function))
-      || (DECL_FUNCTION_TEMPLATE_P (function)
-         && DECL_STATIC_FUNCTION_P (DECL_TEMPLATE_RESULT (function))))
-      return build_member_call (DECL_CONTEXT (function), 
-                               template_id 
-                               ? template_id : DECL_NAME (function), 
-                               params);
-
-  is_method = ((TREE_CODE (function) == TREE_LIST
-               && current_class_type != NULL_TREE
-               && (IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (function))
-                   == function))
-              || (TREE_CODE (function) == OVERLOAD
-                  && DECL_FUNCTION_MEMBER_P (OVL_CURRENT (function)))
-              || TREE_CODE (function) == IDENTIFIER_NODE
-              || TREE_CODE (type) == METHOD_TYPE
-              || TYPE_PTRMEMFUNC_P (type));
-
-  /* A friend template.  Make it look like a toplevel declaration.  */
-  if (! is_method && TREE_CODE (function) == TEMPLATE_DECL)
-    function = ovl_cons (function, NULL_TREE);
-
-  /* Handle methods, friends, and overloaded functions, respectively.  */
-  if (is_method)
-    {
-      tree basetype = NULL_TREE;
-
-      if (TREE_CODE (function) == OVERLOAD)
-       function = OVL_CURRENT (function);
-
-      if (TREE_CODE (function) == FUNCTION_DECL
-         || DECL_FUNCTION_TEMPLATE_P (function))
-       {
-         basetype = DECL_CONTEXT (function);
-
-         if (DECL_NAME (function))
-           function = DECL_NAME (function);
-         else
-           function = TYPE_IDENTIFIER (DECL_CONTEXT (function));
-       }
-      else if (TREE_CODE (function) == TREE_LIST)
-       {
-         my_friendly_assert (TREE_CODE (TREE_VALUE (function))
-                             == FUNCTION_DECL, 312);
-         basetype = DECL_CONTEXT (TREE_VALUE (function));
-         function = TREE_PURPOSE (function);
-       }
-      else if (TREE_CODE (function) != IDENTIFIER_NODE)
-       {
-         if (TREE_CODE (function) == OFFSET_REF)
-           {
-             if (TREE_OPERAND (function, 0))
-               decl = TREE_OPERAND (function, 0);
-           }
-         /* Call via a pointer to member function.  */
-         if (decl == NULL_TREE)
-           {
-             error ("pointer to member function called, but not in class scope");
-             return error_mark_node;
-           }
-         /* What other type of POINTER_TYPE could this be? */
-         if (TREE_CODE (TREE_TYPE (function)) != POINTER_TYPE
-             && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (function))
-             && TREE_CODE (function) != OFFSET_REF)
-           function = build (OFFSET_REF, TREE_TYPE (type), NULL_TREE,
-                             function);
-         goto do_x_function;
-       }
-
-      /* this is an abbreviated method call.
-         must go through here in case it is a virtual function.
-        @@ Perhaps this could be optimized.  */
-
-      if (basetype && (! current_class_type
-                      || ! DERIVED_FROM_P (basetype, current_class_type)))
-       return build_member_call (basetype, function, params);
-
-      if (decl == NULL_TREE)
-       {
-         if (current_class_type == NULL_TREE)
-           {
-             error ("object missing in call to method `%D'", function);
-             return error_mark_node;
-           }
-         /* Yow: call from a static member function.  */
-         decl = build_dummy_object (current_class_type);
-       }
-
-      /* Put back explicit template arguments, if any.  */
-      if (template_id)
-        function = template_id;
-      return build_method_call (decl, function, params,
-                               NULL_TREE, LOOKUP_NORMAL);
-    }
-  else if (TREE_CODE (function) == COMPONENT_REF
-          && type == unknown_type_node)
-    {
-      /* Undo what we did in build_component_ref.  */
-      decl = TREE_OPERAND (function, 0);
-      function = TREE_OPERAND (function, 1);
-
-      if (TREE_CODE (function) == TEMPLATE_ID_EXPR)
-       {
-         my_friendly_assert (!template_id, 20011228);
-
-         template_id = function;
-       }
-      else
-       {
-         function = DECL_NAME (OVL_CURRENT (function));
-
-         if (template_id)
-           {
-             TREE_OPERAND (template_id, 0) = function;
-             function = template_id;
-           }
-       }
-
-      return build_method_call (decl, function, params,
-                               NULL_TREE, LOOKUP_NORMAL);
-    }
-  else if (really_overloaded_fn (function))
-    {
-      if (OVL_FUNCTION (function) == NULL_TREE)
-       {
-         error ("function `%D' declared overloaded, but no definitions appear with which to resolve it?!?",
-                   TREE_PURPOSE (function));
-         return error_mark_node;
-       }
-      else
-       {
-         /* Put back explicit template arguments, if any.  */
-         if (template_id)
-           function = template_id;
-         return build_new_function_call (function, params);
-       }
-    }
-  else
-    /* Remove a potential OVERLOAD around it */
-    function = OVL_CURRENT (function);
-
- do_x_function:
-  if (TREE_CODE (function) == OFFSET_REF)
-    {
-      /* If the component is a data element (or a virtual function), we play
-        games here to make things work.  */
-      tree decl_addr;
-
-      if (TREE_OPERAND (function, 0))
-       decl = TREE_OPERAND (function, 0);
-      else
-       decl = current_class_ref;
-
-      decl_addr = build_unary_op (ADDR_EXPR, decl, 0);
-
-      /* Sigh.  OFFSET_REFs are being used for too many things.
-        They're being used both for -> and ->*, and we want to resolve
-        the -> cases here, but leave the ->*.  We could use
-        resolve_offset_ref for those, too, but it would call
-         get_member_function_from_ptrfunc and decl_addr wouldn't get
-         updated properly.  Nasty.  */
-      if (TREE_CODE (TREE_OPERAND (function, 1)) == FIELD_DECL)
-       function = resolve_offset_ref (function);
-      else
-       function = TREE_OPERAND (function, 1);
-
-      function = get_member_function_from_ptrfunc (&decl_addr, function);
-      params = tree_cons (NULL_TREE, decl_addr, params);
-      return build_function_call (function, params);
-    }
-
-  type = TREE_TYPE (function);
-  if (type != error_mark_node)
-    {
-      if (TREE_CODE (type) == REFERENCE_TYPE)
-       type = TREE_TYPE (type);
-
-      if (IS_AGGR_TYPE (type))
-       return build_opfncall (CALL_EXPR, LOOKUP_NORMAL, function, params, NULL_TREE);
-    }
-
-  if (is_method)
-    {
-      tree fntype = TREE_TYPE (function);
-      tree ctypeptr = NULL_TREE;
-
-      /* Explicitly named method?  */
-      if (TREE_CODE (function) == FUNCTION_DECL)
-       ctypeptr = build_pointer_type (DECL_CLASS_CONTEXT (function));
-      /* Expression with ptr-to-method type?  It could either be a plain
-        usage, or it might be a case where the ptr-to-method is being
-        passed in as an argument.  */
-      else if (TYPE_PTRMEMFUNC_P (fntype))
-       {
-         tree rec = TYPE_METHOD_BASETYPE (TREE_TYPE
-                                          (TYPE_PTRMEMFUNC_FN_TYPE (fntype)));
-         ctypeptr = build_pointer_type (rec);
-       }
-      /* Unexpected node type?  */
-      else
-       abort ();
-      if (decl == NULL_TREE)
-       {
-         if (current_function_decl
-             && DECL_STATIC_FUNCTION_P (current_function_decl))
-           error ("invalid call to member function needing `this' in static member function scope");
-         else
-           error ("pointer to member function called, but not in class scope");
-         return error_mark_node;
-       }
-      if (TREE_CODE (TREE_TYPE (decl)) != POINTER_TYPE
-         && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (decl)))
-       {
-         tree binfo = lookup_base (TREE_TYPE (decl), TREE_TYPE (ctypeptr),
-                                   ba_check, NULL);
-         
-         decl = build_unary_op (ADDR_EXPR, decl, 0);
-         decl = build_base_path (PLUS_EXPR, decl, binfo, 1);
-       }
-      else
-       decl = build_c_cast (ctypeptr, decl);
-      params = tree_cons (NULL_TREE, decl, params);
-    }
-
-  return build_function_call (function, params);
-}
-
 /* Resolve a pointer to member function.  INSTANCE is the object
-   instance to use, if the member points to a virtual member.  */
+   instance to use, if the member points to a virtual member.
+
+   This used to avoid checking for virtual functions if basetype
+   has no virtual functions, according to an earlier ANSI draft.
+   With the final ISO C++ rules, such an optimization is
+   incorrect: A pointer to a derived member can be static_cast
+   to pointer-to-base-member, as long as the dynamic object
+   later has the right member.  */
 
 tree
-get_member_function_from_ptrfunc (instance_ptrptr, function)
-     tree *instance_ptrptr;
-     tree function;
+get_member_function_from_ptrfunc (tree *instance_ptrptr, tree function)
 {
   if (TREE_CODE (function) == OFFSET_REF)
     function = TREE_OPERAND (function, 1);
 
   if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
     {
-      tree fntype, idx, e1, delta, delta2, e2, e3, vtbl;
-      tree instance, basetype;
+      tree idx, delta, e1, e2, e3, vtbl, basetype;
+      tree fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
 
       tree instance_ptr = *instance_ptrptr;
-
-      if (instance_ptr == error_mark_node
-         && TREE_CODE (function) == PTRMEM_CST)
+      tree instance_save_expr = 0;
+      if (instance_ptr == error_mark_node)
        {
-         /* Extracting the function address from a pmf is only
-            allowed with -Wno-pmf-conversions. It only works for
-            pmf constants. */
-         e1 = build_addr_func (PTRMEM_CST_MEMBER (function));
-         e1 = convert (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function)), e1);
-         return e1;
+         if (TREE_CODE (function) == PTRMEM_CST)
+           {
+             /* Extracting the function address from a pmf is only
+                allowed with -Wno-pmf-conversions. It only works for
+                pmf constants.  */
+             e1 = build_addr_func (PTRMEM_CST_MEMBER (function));
+             e1 = convert (fntype, e1);
+             return e1;
+           }
+         else
+           {
+             error ("object missing in use of %qE", function);
+             return error_mark_node;
+           }
        }
 
       if (TREE_SIDE_EFFECTS (instance_ptr))
-       instance_ptr = save_expr (instance_ptr);
+       instance_ptr = instance_save_expr = save_expr (instance_ptr);
 
       if (TREE_SIDE_EFFECTS (function))
        function = save_expr (function);
 
-      fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
-      basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
-
-      /* Convert down to the right base, before using the instance. */
-      instance = lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr)), basetype,
-                             ba_check, NULL);
-      instance = build_base_path (PLUS_EXPR, instance_ptr, instance, 1);
-      if (instance == error_mark_node && instance_ptr != error_mark_node)
-       return instance;
-
-      e3 = PFN_FROM_PTRMEMFUNC (function);
-      
-      vtbl = build1 (NOP_EXPR, build_pointer_type (ptr_type_node), instance);
-      TREE_CONSTANT (vtbl) = TREE_CONSTANT (instance);
-      
-      delta = cp_convert (ptrdiff_type_node,
-                         build_component_ref (function, delta_identifier,
-                                              NULL_TREE, 0));
-
-      /* This used to avoid checking for virtual functions if basetype
-        has no virtual functions, according to an earlier ANSI draft.
-        With the final ISO C++ rules, such an optimization is
-        incorrect: A pointer to a derived member can be static_cast
-        to pointer-to-base-member, as long as the dynamic object
-        later has the right member. */
-
-      /* Promoting idx before saving it improves performance on RISC
-        targets.  Without promoting, the first compare used
-        load-with-sign-extend, while the second used normal load then
-        shift to sign-extend.  An optimizer flaw, perhaps, but it's
-        easier to make this change.  */
-      idx = cp_build_binary_op (TRUNC_DIV_EXPR, 
-                               build1 (NOP_EXPR, vtable_index_type, e3),
-                               TYPE_SIZE_UNIT (vtable_entry_type));
+      /* Start by extracting all the information from the PMF itself.  */
+      e3 = pfn_from_ptrmemfunc (function);
+      delta = delta_from_ptrmemfunc (function);
+      idx = build1 (NOP_EXPR, vtable_index_type, e3);
       switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
        {
        case ptrmemfunc_vbit_in_pfn:
-         e1 = cp_build_binary_op (BIT_AND_EXPR,
-                                  build1 (NOP_EXPR, vtable_index_type, e3),
-                                  integer_one_node);
+         e1 = cp_build_binary_op (input_location,
+                                  BIT_AND_EXPR, idx, integer_one_node,
+                                  tf_warning_or_error);
+         idx = cp_build_binary_op (input_location,
+                                   MINUS_EXPR, idx, integer_one_node,
+                                   tf_warning_or_error);
          break;
 
        case ptrmemfunc_vbit_in_delta:
-         e1 = cp_build_binary_op (BIT_AND_EXPR,
-                                  delta, integer_one_node);
-         delta = cp_build_binary_op (RSHIFT_EXPR,
-                                     build1 (NOP_EXPR, vtable_index_type,
-                                             delta),
-                                     integer_one_node);
+         e1 = cp_build_binary_op (input_location,
+                                  BIT_AND_EXPR, delta, integer_one_node,
+                                  tf_warning_or_error);
+         delta = cp_build_binary_op (input_location,
+                                     RSHIFT_EXPR, delta, integer_one_node,
+                                     tf_warning_or_error);
          break;
 
        default:
-         abort ();
+         gcc_unreachable ();
        }
 
-      /* DELTA2 is the amount by which to adjust the `this' pointer
-        to find the vtbl.  */
-      delta2 = delta;
-      vtbl = build
-       (PLUS_EXPR,
-        build_pointer_type (build_pointer_type (vtable_entry_type)),
-        vtbl, cp_convert (ptrdiff_type_node, delta2));
-      vtbl = build_indirect_ref (vtbl, NULL);
-      e2 = build_array_ref (vtbl, idx);
+      /* Convert down to the right base before using the instance.  A
+        special case is that in a pointer to member of class C, C may
+        be incomplete.  In that case, the function will of course be
+        a member of C, and no conversion is required.  In fact,
+        lookup_base will fail in that case, because incomplete
+        classes do not have BINFOs.  */
+      basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
+      if (!same_type_ignoring_top_level_qualifiers_p
+         (basetype, TREE_TYPE (TREE_TYPE (instance_ptr))))
+       {
+         basetype = lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr)),
+                                 basetype, ba_check, NULL);
+         instance_ptr = build_base_path (PLUS_EXPR, instance_ptr, basetype,
+                                         1);
+         if (instance_ptr == error_mark_node)
+           return error_mark_node;
+       }
+      /* ...and then the delta in the PMF.  */
+      instance_ptr = build2 (POINTER_PLUS_EXPR, TREE_TYPE (instance_ptr),
+                            instance_ptr, fold_convert (sizetype, delta));
+
+      /* Hand back the adjusted 'this' argument to our caller.  */
+      *instance_ptrptr = instance_ptr;
+
+      /* Next extract the vtable pointer from the object.  */
+      vtbl = build1 (NOP_EXPR, build_pointer_type (vtbl_ptr_type_node),
+                    instance_ptr);
+      vtbl = cp_build_indirect_ref (vtbl, NULL, tf_warning_or_error);
+      /* If the object is not dynamic the access invokes undefined
+        behavior.  As it is not executed in this case silence the
+        spurious warnings it may provoke.  */
+      TREE_NO_WARNING (vtbl) = 1;
+
+      /* Finally, extract the function pointer from the vtable.  */
+      e2 = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (vtbl), vtbl,
+                       fold_convert (sizetype, idx));
+      e2 = cp_build_indirect_ref (e2, NULL, tf_warning_or_error);
+      TREE_CONSTANT (e2) = 1;
 
       /* When using function descriptors, the address of the
         vtable entry is treated as a function pointer.  */
       if (TARGET_VTABLE_USES_DESCRIPTORS)
        e2 = build1 (NOP_EXPR, TREE_TYPE (e2),
-                    build_unary_op (ADDR_EXPR, e2, /*noconvert=*/1));
+                    cp_build_unary_op (ADDR_EXPR, e2, /*noconvert=*/1,
+                                     tf_warning_or_error));
+
+      e2 = fold_convert (TREE_TYPE (e3), e2);
+      e1 = build_conditional_expr (e1, e2, e3, tf_warning_or_error);
 
-      TREE_TYPE (e2) = TREE_TYPE (e3);
-      e1 = build_conditional_expr (e1, e2, e3);
-      
       /* Make sure this doesn't get evaluated first inside one of the
         branches of the COND_EXPR.  */
-      if (TREE_CODE (instance_ptr) == SAVE_EXPR)
-       e1 = build (COMPOUND_EXPR, TREE_TYPE (e1),
-                   instance_ptr, e1);
-
-      *instance_ptrptr = build (PLUS_EXPR, TREE_TYPE (instance_ptr),
-                               instance_ptr, delta);
-
-      if (instance_ptr == error_mark_node
-         && TREE_CODE (e1) != ADDR_EXPR
-         && TREE_CODE (TREE_OPERAND (e1, 0)) != FUNCTION_DECL)
-       error ("object missing in `%E'", function);
+      if (instance_save_expr)
+       e1 = build2 (COMPOUND_EXPR, TREE_TYPE (e1),
+                    instance_save_expr, e1);
 
       function = e1;
     }
   return function;
 }
 
+/* Used by the C-common bits.  */
 tree
-build_function_call_real (function, params, require_complete, flags)
-     tree function, params;
-     int require_complete, flags;
+build_function_call (tree function, tree params)
 {
-  register tree fntype, fndecl;
-  register tree value_type;
-  register tree coerced_params;
-  tree result;
-  tree name = NULL_TREE, assembler_name = NULL_TREE;
+  return cp_build_function_call (function, params, tf_warning_or_error);
+}
+
+tree
+cp_build_function_call (tree function, tree params, tsubst_flags_t complain)
+{
+  tree fntype, fndecl;
+  tree name = NULL_TREE;
   int is_method;
   tree original = function;
+  int nargs, parm_types_len;
+  tree *argarray;
+  tree parm_types;
+
+  /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
+     expressions, like those used for ObjC messenger dispatches.  */
+  function = objc_rewrite_function_call (function, params);
 
   /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
      Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context.  */
@@ -2957,23 +2893,16 @@ build_function_call_real (function, params, require_complete, flags)
   if (TREE_CODE (function) == FUNCTION_DECL)
     {
       name = DECL_NAME (function);
-      assembler_name = DECL_ASSEMBLER_NAME (function);
 
       mark_used (function);
       fndecl = function;
 
       /* Convert anything with function type to a pointer-to-function.  */
-      if (pedantic && DECL_MAIN_P (function))
-       pedwarn ("ISO C++ forbids calling `::main' from within program");
+      if (DECL_MAIN_P (function) && (complain & tf_error))
+       pedwarn (input_location, OPT_pedantic, 
+                "ISO C++ forbids calling %<::main%> from within program");
 
-      /* Differs from default_conversion by not setting TREE_ADDRESSABLE
-        (because calling an inline function does not mean the function
-        needs to be separately compiled).  */
-      
-      if (DECL_INLINE (function))
-       function = inline_conversion (function);
-      else
-       function = build_addr_func (function);
+      function = build_addr_func (function);
     }
   else
     {
@@ -2989,8 +2918,10 @@ build_function_call_real (function, params, require_complete, flags)
 
   if (TYPE_PTRMEMFUNC_P (fntype))
     {
-      error ("must use .* or ->* to call pointer-to-member function in `%E (...)'",
-               original);
+      if (complain & tf_error)
+       error ("must use %<.*%> or %<->*%> to call pointer-to-member "
+              "function in %<%E (...)%>, e.g. %<(... ->* %E) (...)%>",
+              original, original);
       return error_mark_node;
     }
 
@@ -3002,71 +2933,36 @@ build_function_call_real (function, params, require_complete, flags)
        || is_method
        || TREE_CODE (function) == TEMPLATE_ID_EXPR))
     {
-      error ("`%E' cannot be used as a function", original);
+      if (complain & tf_error)
+       error ("%qE cannot be used as a function", original);
       return error_mark_node;
     }
 
   /* fntype now gets the type of function pointed to.  */
   fntype = TREE_TYPE (fntype);
+  parm_types = TYPE_ARG_TYPES (fntype);
+
+  /* Allocate storage for converted arguments.  */
+  parm_types_len = list_length (parm_types);
+  nargs = list_length (params);
+  if (parm_types_len > nargs)
+    nargs = parm_types_len;
+  argarray = (tree *) alloca (nargs * sizeof (tree));
 
   /* Convert the parameters to the types declared in the
      function prototype, or apply default promotions.  */
+  nargs = convert_arguments (nargs, argarray, parm_types,
+                            params, fndecl, LOOKUP_NORMAL,
+                             complain);
+  if (nargs < 0)
+    return error_mark_node;
 
-  if (flags & LOOKUP_COMPLAIN)
-    coerced_params = convert_arguments (TYPE_ARG_TYPES (fntype),
-                                       params, fndecl, LOOKUP_NORMAL);
-  else
-    coerced_params = convert_arguments (TYPE_ARG_TYPES (fntype),
-                                       params, fndecl, 0);
-
-  if (coerced_params == error_mark_node)
-    {
-      if (flags & LOOKUP_SPECULATIVELY)
-       return NULL_TREE;
-      else
-       return error_mark_node;
-    }
-
-  /* Check for errors in format strings.  */
-
-  if (warn_format)
-    check_function_format (NULL, TYPE_ATTRIBUTES (fntype), coerced_params);
-
-  /* Recognize certain built-in functions so we can make tree-codes
-     other than CALL_EXPR.  We do this when it enables fold-const.c
-     to do something useful.  */
-
-  if (TREE_CODE (function) == ADDR_EXPR
-      && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
-      && DECL_BUILT_IN (TREE_OPERAND (function, 0)))
-    {
-      result = expand_tree_builtin (TREE_OPERAND (function, 0),
-                                   params, coerced_params);
-      if (result)
-       return result;
-    }
-
-  /* Some built-in function calls will be evaluated at
-     compile-time in fold ().  */
-  result = fold (build_call (function, coerced_params));
-  value_type = TREE_TYPE (result);
-
-  if (require_complete)
-    {
-      if (TREE_CODE (value_type) == VOID_TYPE)
-       return result;
-      result = require_complete_type (result);
-    }
-  if (IS_AGGR_TYPE (value_type))
-    result = build_cplus_new (value_type, result);
-  return convert_from_reference (result);
-}
-
-tree
-build_function_call (function, params)
-     tree function, params;
-{
-  return build_function_call_real (function, params, 1, LOOKUP_NORMAL);
+  /* Check for errors in format strings and inappropriately
+     null parameters.  */
+  check_function_arguments (TYPE_ATTRIBUTES (fntype), nargs, argarray,
+                           parm_types);
+
+  return build_cxx_call (function, nargs, argarray);
 }
 \f
 /* Convert the actual parameter expressions in the list VALUES
@@ -3074,25 +2970,27 @@ build_function_call (function, params)
    If parmdecls is exhausted, or when an element has NULL as its type,
    perform the default conversions.
 
+   Store the converted arguments in ARGARRAY.  NARGS is the size of this array.
+
    NAME is an IDENTIFIER_NODE or 0.  It is used only for error messages.
 
    This is also where warnings about wrong number of args are generated.
-   
-   Return a list of expressions for the parameters as converted.
 
-   Both VALUES and the returned value are chains of TREE_LIST nodes
-   with the elements of the list in the TREE_VALUE slots of those nodes.
+   Returns the actual number of arguments processed (which might be less
+   than NARGS), or -1 on error.
+
+   VALUES is a chain of TREE_LIST nodes with the elements of the list
+   in the TREE_VALUE slots of those nodes.
 
    In C++, unspecified trailing parameters can be filled in with their
    default arguments, if such were specified.  Do so here.  */
 
-tree
-convert_arguments (typelist, values, fndecl, flags)
-     tree typelist, values, fndecl;
-     int flags;
+static int
+convert_arguments (int nargs, tree *argarray,
+                  tree typelist, tree values, tree fndecl, int flags,
+                   tsubst_flags_t complain)
 {
-  register tree typetail, valtail;
-  register tree result = NULL_TREE;
+  tree typetail, valtail;
   const char *called_thing = 0;
   int i = 0;
 
@@ -3117,32 +3015,30 @@ convert_arguments (typelist, values, fndecl, flags)
        valtail;
        valtail = TREE_CHAIN (valtail), i++)
     {
-      register tree type = typetail ? TREE_VALUE (typetail) : 0;
-      register tree val = TREE_VALUE (valtail);
+      tree type = typetail ? TREE_VALUE (typetail) : 0;
+      tree val = TREE_VALUE (valtail);
 
-      if (val == error_mark_node)
-       return error_mark_node;
+      if (val == error_mark_node || type == error_mark_node)
+       return -1;
 
       if (type == void_type_node)
        {
-         if (fndecl)
-           {
-             cp_error_at ("too many arguments to %s `%+#D'", called_thing,
-                          fndecl);
-             error ("at this point in file");
-           }
-         else
-           error ("too many arguments to function");
-         /* In case anybody wants to know if this argument
-            list is valid.  */
-         if (result)
-           TREE_TYPE (tree_last (result)) = error_mark_node;
-         break;
+          if (complain & tf_error)
+            {
+              if (fndecl)
+                {
+                  error ("too many arguments to %s %q+#D", 
+                         called_thing, fndecl);
+                  error ("at this point in file");
+                }
+              else
+                error ("too many arguments to function");
+              return i;
+            }
+          else
+            return -1;
        }
 
-      if (TREE_CODE (val) == OFFSET_REF)
-       val = resolve_offset_ref (val);
-
       /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
         Strip such NOP_EXPRs, since VAL is used in non-lvalue context.  */
       if (TREE_CODE (val) == NOP_EXPR
@@ -3155,11 +3051,11 @@ convert_arguments (typelist, values, fndecl, flags)
          if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
              || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE
              || TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
-           val = default_conversion (val);
+           val = decay_conversion (val);
        }
 
       if (val == error_mark_node)
-       return error_mark_node;
+       return -1;
 
       if (type != 0)
        {
@@ -3168,31 +3064,31 @@ convert_arguments (typelist, values, fndecl, flags)
 
          if (!COMPLETE_TYPE_P (complete_type (type)))
            {
-             error ("parameter type of called function is incomplete");
-             parmval = val;
+              if (complain & tf_error)
+                {
+                  if (fndecl)
+                    error ("parameter %P of %qD has incomplete type %qT",
+                           i, fndecl, type);
+                  else
+                    error ("parameter %P has incomplete type %qT", i, type);
+                }
+             parmval = error_mark_node;
            }
          else
            {
              parmval = convert_for_initialization
                (NULL_TREE, type, val, flags,
-                "argument passing", fndecl, i);
-             if (PROMOTE_PROTOTYPES
-                 && INTEGRAL_TYPE_P (type)
-                 && (TYPE_PRECISION (type)
-                     < TYPE_PRECISION (integer_type_node)))
-               parmval = default_conversion (parmval);
+                "argument passing", fndecl, i, complain);
+             parmval = convert_for_arg_passing (type, parmval);
            }
 
          if (parmval == error_mark_node)
-           return error_mark_node;
+           return -1;
 
-         result = tree_cons (NULL_TREE, parmval, result);
+         argarray[i] = parmval;
        }
       else
        {
-         if (TREE_CODE (TREE_TYPE (val)) == REFERENCE_TYPE)
-           val = convert_from_reference (val);
-
          if (fndecl && DECL_BUILT_IN (fndecl)
              && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CONSTANT_P)
            /* Don't do ellipsis conversion for __built_in_constant_p
@@ -3202,7 +3098,7 @@ convert_arguments (typelist, values, fndecl, flags)
          else
            val = convert_arg_to_ellipsis (val);
 
-         result = tree_cons (NULL_TREE, val, result);
+         argarray[i] = val;
        }
 
       if (typetail)
@@ -3211,20 +3107,27 @@ convert_arguments (typelist, values, fndecl, flags)
 
   if (typetail != 0 && typetail != void_list_node)
     {
-      /* See if there are default arguments that can be used */
-      if (TREE_PURPOSE (typetail))
+      /* See if there are default arguments that can be used.  Because
+        we hold default arguments in the FUNCTION_TYPE (which is so
+        wrong), we can see default parameters here from deduced
+        contexts (and via typeof) for indirect function calls.
+        Fortunately we know whether we have a function decl to
+        provide default arguments in a language conformant
+        manner.  */
+      if (fndecl && TREE_PURPOSE (typetail)
+         && TREE_CODE (TREE_PURPOSE (typetail)) != DEFAULT_ARG)
        {
          for (; typetail != void_list_node; ++i)
            {
-             tree parmval 
-               = convert_default_arg (TREE_VALUE (typetail), 
-                                      TREE_PURPOSE (typetail), 
+             tree parmval
+               = convert_default_arg (TREE_VALUE (typetail),
+                                      TREE_PURPOSE (typetail),
                                       fndecl, i);
 
              if (parmval == error_mark_node)
-               return error_mark_node;
+               return -1;
 
-             result = tree_cons (0, parmval, result);
+             argarray[i] = parmval;
              typetail = TREE_CHAIN (typetail);
              /* ends with `...'.  */
              if (typetail == NULL_TREE)
@@ -3233,37 +3136,120 @@ convert_arguments (typelist, values, fndecl, flags)
        }
       else
        {
-         if (fndecl)
-           {
-             cp_error_at ("too few arguments to %s `%+#D'",
-                          called_thing, fndecl);
-             error ("at this point in file");
-           }
-         else
-           error ("too few arguments to function");
-         return error_mark_list;
+          if (complain & tf_error)
+            {
+              if (fndecl)
+                {
+                  error ("too few arguments to %s %q+#D", 
+                         called_thing, fndecl);
+                  error ("at this point in file");
+                }
+              else
+                error ("too few arguments to function");
+            }
+         return -1;
        }
     }
 
-  return nreverse (result);
+  gcc_assert (i <= nargs);
+  return i;
 }
 \f
 /* Build a binary-operation expression, after performing default
-   conversions on the operands.  CODE is the kind of expression to build.  */
+   conversions on the operands.  CODE is the kind of expression to
+   build.  ARG1 and ARG2 are the arguments.  ARG1_CODE and ARG2_CODE
+   are the tree codes which correspond to ARG1 and ARG2 when issuing
+   warnings about possibly misplaced parentheses.  They may differ
+   from the TREE_CODE of ARG1 and ARG2 if the parser has done constant
+   folding (e.g., if the parser sees "a | 1 + 1", it may call this
+   routine with ARG2 being an INTEGER_CST and ARG2_CODE == PLUS_EXPR).
+   To avoid issuing any parentheses warnings, pass ARG1_CODE and/or
+   ARG2_CODE as ERROR_MARK.  */
+
+tree
+build_x_binary_op (enum tree_code code, tree arg1, enum tree_code arg1_code,
+                  tree arg2, enum tree_code arg2_code, bool *overloaded_p,
+                  tsubst_flags_t complain)
+{
+  tree orig_arg1;
+  tree orig_arg2;
+  tree expr;
+
+  orig_arg1 = arg1;
+  orig_arg2 = arg2;
+
+  if (processing_template_decl)
+    {
+      if (type_dependent_expression_p (arg1)
+         || type_dependent_expression_p (arg2))
+       return build_min_nt (code, arg1, arg2);
+      arg1 = build_non_dependent_expr (arg1);
+      arg2 = build_non_dependent_expr (arg2);
+    }
+
+  if (code == DOTSTAR_EXPR)
+    expr = build_m_component_ref (arg1, arg2);
+  else
+    expr = build_new_op (code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE,
+                        overloaded_p, complain);
+
+  /* Check for cases such as x+y<<z which users are likely to
+     misinterpret.  But don't warn about obj << x + y, since that is a
+     common idiom for I/O.  */
+  if (warn_parentheses
+      && !processing_template_decl
+      && !error_operand_p (arg1)
+      && !error_operand_p (arg2)
+      && (code != LSHIFT_EXPR
+         || !CLASS_TYPE_P (TREE_TYPE (arg1))))
+    warn_about_parentheses (code, arg1_code, orig_arg1, arg2_code, orig_arg2);
+
+  if (processing_template_decl && expr != error_mark_node)
+    return build_min_non_dep (code, expr, orig_arg1, orig_arg2);
+
+  return expr;
+}
+
+/* Build and return an ARRAY_REF expression.  */
 
 tree
-build_x_binary_op (code, arg1, arg2)
-     enum tree_code code;
-     tree arg1, arg2;
+build_x_array_ref (tree arg1, tree arg2, tsubst_flags_t complain)
 {
+  tree orig_arg1 = arg1;
+  tree orig_arg2 = arg2;
+  tree expr;
+
   if (processing_template_decl)
-    return build_min_nt (code, arg1, arg2);
+    {
+      if (type_dependent_expression_p (arg1)
+         || type_dependent_expression_p (arg2))
+       return build_min_nt (ARRAY_REF, arg1, arg2,
+                            NULL_TREE, NULL_TREE);
+      arg1 = build_non_dependent_expr (arg1);
+      arg2 = build_non_dependent_expr (arg2);
+    }
 
-  return build_new_op (code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE);
+  expr = build_new_op (ARRAY_REF, LOOKUP_NORMAL, arg1, arg2, NULL_TREE,
+                      /*overloaded_p=*/NULL, complain);
+
+  if (processing_template_decl && expr != error_mark_node)
+    return build_min_non_dep (ARRAY_REF, expr, orig_arg1, orig_arg2,
+                             NULL_TREE, NULL_TREE);
+  return expr;
+}
+
+/* For the c-common bits.  */
+tree
+build_binary_op (location_t location, enum tree_code code, tree op0, tree op1,
+                int convert_p ATTRIBUTE_UNUSED)
+{
+  return cp_build_binary_op (location, code, op0, op1, tf_warning_or_error);
 }
 
+
 /* Build a binary-operation expression without default conversions.
    CODE is the kind of expression to build.
+   LOCATION is the location_t of the operator in the source code.
    This function differs from `build' in several ways:
    the data type of the result is computed and recorded in it,
    warnings are generated if arg data types are invalid,
@@ -3281,23 +3267,23 @@ build_x_binary_op (code, arg1, arg2)
    multiple inheritance, and deal with pointer to member functions.  */
 
 tree
-build_binary_op (code, orig_op0, orig_op1, convert_p)
-     enum tree_code code;
-     tree orig_op0, orig_op1;
-     int convert_p ATTRIBUTE_UNUSED;
+cp_build_binary_op (location_t location,
+                   enum tree_code code, tree orig_op0, tree orig_op1,
+                   tsubst_flags_t complain)
 {
   tree op0, op1;
-  register enum tree_code code0, code1;
+  enum tree_code code0, code1;
   tree type0, type1;
+  const char *invalid_op_diag;
 
   /* Expression code to give to the expression when it is built.
      Normally this is CODE, which is what the caller asked for,
      but in some special cases we change it.  */
-  register enum tree_code resultcode = code;
+  enum tree_code resultcode = code;
 
   /* Data type in which the computation is to be performed.
      In the simplest cases this is the common type of the arguments.  */
-  register tree result_type = NULL;
+  tree result_type = NULL;
 
   /* Nonzero means operands have already been type-converted
      in whatever way is necessary.
@@ -3312,6 +3298,8 @@ build_binary_op (code, orig_op0, orig_op1, convert_p)
      convert it to this type.  */
   tree final_type = 0;
 
+  tree result;
+
   /* Nonzero if this is an operation like MIN or MAX which can
      safely be computed in short if both args are promoted shorts.
      Also implies COMMON.
@@ -3325,17 +3313,16 @@ build_binary_op (code, orig_op0, orig_op1, convert_p)
      Also implies COMMON.  */
   int short_compare = 0;
 
-  /* Nonzero if this is a right-shift operation, which can be computed on the
-     original short and then promoted if the operand is a promoted short.  */
-  int short_shift = 0;
-
   /* Nonzero means set RESULT_TYPE to the common type of the args.  */
   int common = 0;
 
+  /* True if both operands have arithmetic type.  */
+  bool arithmetic_types_p;
+
   /* Apply default conversions.  */
   op0 = orig_op0;
   op1 = orig_op1;
-  
+
   if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
       || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
       || code == TRUTH_XOR_EXPR)
@@ -3363,8 +3350,9 @@ build_binary_op (code, orig_op0, orig_op1, convert_p)
       tree t = instantiate_type (TREE_TYPE (op1), op0, tf_none);
       if (t != error_mark_node)
        {
-         pedwarn ("assuming cast to type `%T' from overloaded function",
-                     TREE_TYPE (t));
+         if (complain & tf_error)
+           permerror (input_location, "assuming cast to type %qT from overloaded function",
+                      TREE_TYPE (t));
          op0 = t;
        }
     }
@@ -3373,8 +3361,9 @@ build_binary_op (code, orig_op0, orig_op1, convert_p)
       tree t = instantiate_type (TREE_TYPE (op0), op1, tf_none);
       if (t != error_mark_node)
        {
-         pedwarn ("assuming cast to type `%T' from overloaded function",
-                     TREE_TYPE (t));
+         if (complain & tf_error)
+           permerror (input_location, "assuming cast to type %qT from overloaded function",
+                      TREE_TYPE (t));
          op1 = t;
        }
     }
@@ -3393,29 +3382,49 @@ build_binary_op (code, orig_op0, orig_op1, convert_p)
   if (code0 == ERROR_MARK || code1 == ERROR_MARK)
     return error_mark_node;
 
-  switch (code)
+  if ((invalid_op_diag
+       = targetm.invalid_binary_op (code, type0, type1)))
     {
-    case PLUS_EXPR:
-      /* Handle the pointer + int case.  */
-      if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
-       return cp_pointer_int_sum (PLUS_EXPR, op0, op1);
-      else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
-       return cp_pointer_int_sum (PLUS_EXPR, op1, op0);
-      else
-       common = 1;
-      break;
+      error (invalid_op_diag);
+      return error_mark_node;
+    }
 
+  switch (code)
+    {
     case MINUS_EXPR:
       /* Subtraction of two similar pointers.
         We must subtract them as integers, then divide by object size.  */
       if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
-         && comp_target_types (type0, type1, 1))
-       return pointer_diff (op0, op1, common_type (type0, type1));
-      /* Handle pointer minus int.  Just like pointer plus int.  */
-      else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
-       return cp_pointer_int_sum (MINUS_EXPR, op0, op1);
-      else
-       common = 1;
+         && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
+                                                       TREE_TYPE (type1)))
+       return pointer_diff (op0, op1, common_pointer_type (type0, type1));
+      /* In all other cases except pointer - int, the usual arithmetic
+        rules apply.  */
+      else if (!(code0 == POINTER_TYPE && code1 == INTEGER_TYPE))
+       {
+         common = 1;
+         break;
+       }
+      /* The pointer - int case is just like pointer + int; fall
+        through.  */
+    case PLUS_EXPR:
+      if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
+         && (code0 == INTEGER_TYPE || code1 == INTEGER_TYPE))
+       {
+         tree ptr_operand;
+         tree int_operand;
+         ptr_operand = ((code0 == POINTER_TYPE) ? op0 : op1);
+         int_operand = ((code0 == INTEGER_TYPE) ? op0 : op1);
+         if (processing_template_decl)
+           {
+             result_type = TREE_TYPE (ptr_operand);
+             break;
+           }
+         return cp_pointer_int_sum (code,
+                                    ptr_operand, 
+                                    int_operand);
+       }
+      common = 1;
       break;
 
     case MULT_EXPR:
@@ -3428,16 +3437,20 @@ build_binary_op (code, orig_op0, orig_op1, convert_p)
     case ROUND_DIV_EXPR:
     case EXACT_DIV_EXPR:
       if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
-          || code0 == COMPLEX_TYPE)
+          || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
          && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
-             || code1 == COMPLEX_TYPE))
+             || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
        {
-         if (TREE_CODE (op1) == INTEGER_CST && integer_zerop (op1))
-           warning ("division by zero in `%E / 0'", op0);
-         else if (TREE_CODE (op1) == REAL_CST && real_zerop (op1))
-           warning ("division by zero in `%E / 0.'", op0);
-             
-         if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
+         enum tree_code tcode0 = code0, tcode1 = code1;
+
+         warn_for_div_by_zero (location, op1);
+
+         if (tcode0 == COMPLEX_TYPE || tcode0 == VECTOR_TYPE)
+           tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
+         if (tcode1 == COMPLEX_TYPE || tcode1 == VECTOR_TYPE)
+           tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
+
+         if (!(tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE))
            resultcode = RDIV_EXPR;
          else
            /* When dividing two signed integers, we have to promote to int.
@@ -3446,7 +3459,7 @@ build_binary_op (code, orig_op0, orig_op1, convert_p)
               point, so we have to dig out the original type to find out if
               it was unsigned.  */
            shorten = ((TREE_CODE (op0) == NOP_EXPR
-                       && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
+                       && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
                       || (TREE_CODE (op1) == INTEGER_CST
                           && ! integer_all_onesp (op1)));
 
@@ -3455,20 +3468,19 @@ build_binary_op (code, orig_op0, orig_op1, convert_p)
       break;
 
     case BIT_AND_EXPR:
-    case BIT_ANDTC_EXPR:
     case BIT_IOR_EXPR:
     case BIT_XOR_EXPR:
-      if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
+      if ((code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
+         || (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
+             && !VECTOR_FLOAT_TYPE_P (type0)
+             && !VECTOR_FLOAT_TYPE_P (type1)))
        shorten = -1;
       break;
 
     case TRUNC_MOD_EXPR:
     case FLOOR_MOD_EXPR:
-      if (code1 == INTEGER_TYPE && integer_zerop (op1))
-       warning ("division by zero in `%E %% 0'", op0);
-      else if (code1 == REAL_TYPE && real_zerop (op1))
-       warning ("division by zero in `%E %% 0.'", op0);
-      
+      warn_for_div_by_zero (location, op1);
+
       if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
        {
          /* Although it would be tempting to shorten always here, that loses
@@ -3476,7 +3488,7 @@ build_binary_op (code, orig_op0, orig_op1, convert_p)
             quotient can't be represented in the computation mode.  We shorten
             only if unsigned or if dividing by something we know != -1.  */
          shorten = ((TREE_CODE (op0) == NOP_EXPR
-                     && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
+                     && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
                     || (TREE_CODE (op1) == INTEGER_CST
                         && ! integer_all_onesp (op1)));
          common = 1;
@@ -3501,13 +3513,15 @@ build_binary_op (code, orig_op0, orig_op1, convert_p)
          if (TREE_CODE (op1) == INTEGER_CST)
            {
              if (tree_int_cst_lt (op1, integer_zero_node))
-               warning ("right shift count is negative");
+               {
+                 if (complain & tf_warning)
+                   warning (0, "right shift count is negative");
+               }
              else
                {
-                 if (! integer_zerop (op1))
-                   short_shift = 1;
-                 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
-                   warning ("right shift count >= width of type");
+                 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0
+                     && (complain & tf_warning))
+                   warning (0, "right shift count >= width of type");
                }
            }
          /* Convert the shift-count to an integer, regardless of
@@ -3526,9 +3540,15 @@ build_binary_op (code, orig_op0, orig_op1, convert_p)
          if (TREE_CODE (op1) == INTEGER_CST)
            {
              if (tree_int_cst_lt (op1, integer_zero_node))
-               warning ("left shift count is negative");
+               {
+                 if (complain & tf_warning)
+                   warning (0, "left shift count is negative");
+               }
              else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
-               warning ("left shift count >= width of type");
+               {
+                 if (complain & tf_warning)
+                   warning (0, "left shift count >= width of type");
+               }
            }
          /* Convert the shift-count to an integer, regardless of
             size of value being shifted.  */
@@ -3547,11 +3567,19 @@ build_binary_op (code, orig_op0, orig_op1, convert_p)
          if (TREE_CODE (op1) == INTEGER_CST)
            {
              if (tree_int_cst_lt (op1, integer_zero_node))
-               warning ("%s rotate count is negative",
-                        (code == LROTATE_EXPR) ? "left" : "right");
+               {
+                 if (complain & tf_warning)
+                   warning (0, (code == LROTATE_EXPR)
+                                 ? G_("left rotate count is negative")
+                                 : G_("right rotate count is negative"));
+               }
              else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
-               warning ("%s rotate count >= width of type",
-                        (code == LROTATE_EXPR) ? "left" : "right");
+               {
+                 if (complain & tf_warning)
+                   warning (0, (code == LROTATE_EXPR) 
+                                  ? G_("left rotate count >= width of type")
+                                  : G_("right rotate count >= width of type"));
+               }
            }
          /* Convert the shift-count to an integer, regardless of
             size of value being shifted.  */
@@ -3562,43 +3590,103 @@ build_binary_op (code, orig_op0, orig_op1, convert_p)
 
     case EQ_EXPR:
     case NE_EXPR:
-      if (warn_float_equal && (code0 == REAL_TYPE || code1 == REAL_TYPE))
-       warning ("comparing floating point with == or != is unsafe");
+      if ((complain & tf_warning)
+         && (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1)))
+       warning (OPT_Wfloat_equal,
+                "comparing floating point with == or != is unsafe");
+      if ((complain & tf_warning)
+         && ((TREE_CODE (orig_op0) == STRING_CST && !integer_zerop (op1))
+             || (TREE_CODE (orig_op1) == STRING_CST && !integer_zerop (op0))))
+       warning (OPT_Waddress, "comparison with string literal results in unspecified behaviour");
 
-      build_type = boolean_type_node; 
+      build_type = boolean_type_node;
       if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
-          || code0 == COMPLEX_TYPE)
+          || code0 == COMPLEX_TYPE || code0 == ENUMERAL_TYPE)
          && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
-             || code1 == COMPLEX_TYPE))
+             || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE))
        short_compare = 1;
-      else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
+      else if ((code0 == POINTER_TYPE && code1 == POINTER_TYPE)
+              || (TYPE_PTRMEM_P (type0) && TYPE_PTRMEM_P (type1)))
        result_type = composite_pointer_type (type0, type1, op0, op1,
-                                             "comparison");
-      else if (code0 == POINTER_TYPE && null_ptr_cst_p (op1))
-       result_type = type0;
-      else if (code1 == POINTER_TYPE && null_ptr_cst_p (op0))
-       result_type = type1;
+                                             "comparison", complain);
+      else if ((code0 == POINTER_TYPE || TYPE_PTRMEM_P (type0))
+              && null_ptr_cst_p (op1))
+       {
+         if (TREE_CODE (op0) == ADDR_EXPR
+             && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
+           {
+             if (complain & tf_warning)
+               warning (OPT_Waddress, "the address of %qD will never be NULL",
+                        TREE_OPERAND (op0, 0));
+           }
+         result_type = type0;
+       }
+      else if ((code1 == POINTER_TYPE || TYPE_PTRMEM_P (type1))
+              && null_ptr_cst_p (op0))
+       {
+         if (TREE_CODE (op1) == ADDR_EXPR 
+             && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
+           {
+             if (complain & tf_warning)
+               warning (OPT_Waddress, "the address of %qD will never be NULL",
+                        TREE_OPERAND (op1, 0));
+           }
+         result_type = type1;
+       }
       else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
        {
          result_type = type0;
-         error ("ISO C++ forbids comparison between pointer and integer");
+         if (complain & tf_error) 
+            permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
+          else
+            return error_mark_node;
        }
       else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
        {
          result_type = type1;
-         error ("ISO C++ forbids comparison between pointer and integer");
+         if (complain & tf_error)
+           permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
+          else
+            return error_mark_node;
        }
       else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (op1))
        {
-         op0 = build_component_ref (op0, pfn_identifier, NULL_TREE, 0);
-         op1 = cp_convert (TREE_TYPE (op0), integer_zero_node);
+         if (TARGET_PTRMEMFUNC_VBIT_LOCATION
+             == ptrmemfunc_vbit_in_delta)
+           {
+             tree pfn0 = pfn_from_ptrmemfunc (op0);
+             tree delta0 = delta_from_ptrmemfunc (op0);
+             tree e1 = cp_build_binary_op (location,
+                                           EQ_EXPR,
+                                           pfn0,       
+                                           fold_convert (TREE_TYPE (pfn0),
+                                                         integer_zero_node),
+                                           complain);
+             tree e2 = cp_build_binary_op (location,
+                                           BIT_AND_EXPR, 
+                                           delta0,
+                                           integer_one_node,
+                                           complain);
+             e2 = cp_build_binary_op (location,
+                                      EQ_EXPR, e2, integer_zero_node,
+                                      complain);
+             op0 = cp_build_binary_op (location,
+                                       TRUTH_ANDIF_EXPR, e1, e2,
+                                       complain);
+             op1 = cp_convert (TREE_TYPE (op0), integer_one_node); 
+           }
+         else 
+           {
+             op0 = build_ptrmemfunc_access_expr (op0, pfn_identifier);
+             op1 = cp_convert (TREE_TYPE (op0), integer_zero_node); 
+           }
          result_type = TREE_TYPE (op0);
        }
       else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (op0))
-       return cp_build_binary_op (code, op1, op0);
-      else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1)
-              && same_type_p (type0, type1))
+       return cp_build_binary_op (location, code, op1, op0, complain);
+      else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1))
        {
+         tree type;
          /* E will be the final comparison.  */
          tree e;
          /* E1 and E2 are for scratch.  */
@@ -3609,42 +3697,109 @@ build_binary_op (code, orig_op0, orig_op1, convert_p)
          tree delta0;
          tree delta1;
 
+         type = composite_pointer_type (type0, type1, op0, op1, "comparison",
+                                        complain);
+
+         if (!same_type_p (TREE_TYPE (op0), type))
+           op0 = cp_convert_and_check (type, op0);
+         if (!same_type_p (TREE_TYPE (op1), type))
+           op1 = cp_convert_and_check (type, op1);
+
+         if (op0 == error_mark_node || op1 == error_mark_node)
+           return error_mark_node;
+
          if (TREE_SIDE_EFFECTS (op0))
            op0 = save_expr (op0);
          if (TREE_SIDE_EFFECTS (op1))
            op1 = save_expr (op1);
 
-         /* We generate:
-
-            (op0.pfn == op1.pfn 
-             && (!op0.pfn || op0.delta == op1.delta))
-            
-            The reason for the `!op0.pfn' bit is that a NULL
-            pointer-to-member is any member with a zero PFN; the
-            DELTA field is unspecified.  */
          pfn0 = pfn_from_ptrmemfunc (op0);
          pfn1 = pfn_from_ptrmemfunc (op1);
-         delta0 = build_component_ref (op0, delta_identifier,
-                                       NULL_TREE, 0);
-         delta1 = build_component_ref (op1, delta_identifier,
-                                       NULL_TREE, 0);
-         e1 = cp_build_binary_op (EQ_EXPR, delta0, delta1);
-         e2 = cp_build_binary_op (EQ_EXPR, 
-                                  pfn0,
-                                  cp_convert (TREE_TYPE (pfn0),
-                                              integer_zero_node));
-         e1 = cp_build_binary_op (TRUTH_ORIF_EXPR, e1, e2);
-         e2 = build (EQ_EXPR, boolean_type_node, pfn0, pfn1);
-         e = cp_build_binary_op (TRUTH_ANDIF_EXPR, e2, e1);
+         delta0 = delta_from_ptrmemfunc (op0);
+         delta1 = delta_from_ptrmemfunc (op1);
+         if (TARGET_PTRMEMFUNC_VBIT_LOCATION
+             == ptrmemfunc_vbit_in_delta)
+           {
+             /* We generate:
+
+                (op0.pfn == op1.pfn
+                 && ((op0.delta == op1.delta)
+                      || (!op0.pfn && op0.delta & 1 == 0 
+                          && op1.delta & 1 == 0))
+
+                The reason for the `!op0.pfn' bit is that a NULL
+                pointer-to-member is any member with a zero PFN and
+                LSB of the DELTA field is 0.  */
+
+             e1 = cp_build_binary_op (location, BIT_AND_EXPR,
+                                      delta0, 
+                                      integer_one_node,
+                                      complain);
+             e1 = cp_build_binary_op (location,
+                                      EQ_EXPR, e1, integer_zero_node,
+                                      complain);
+             e2 = cp_build_binary_op (location, BIT_AND_EXPR,
+                                      delta1,
+                                      integer_one_node,
+                                      complain);
+             e2 = cp_build_binary_op (location,
+                                      EQ_EXPR, e2, integer_zero_node,
+                                      complain);
+             e1 = cp_build_binary_op (location,
+                                      TRUTH_ANDIF_EXPR, e2, e1,
+                                      complain);
+             e2 = cp_build_binary_op (location, EQ_EXPR,
+                                      pfn0,
+                                      fold_convert (TREE_TYPE (pfn0),
+                                                    integer_zero_node),
+                                      complain);
+             e2 = cp_build_binary_op (location,
+                                      TRUTH_ANDIF_EXPR, e2, e1, complain);
+             e1 = cp_build_binary_op (location,
+                                      EQ_EXPR, delta0, delta1, complain);
+             e1 = cp_build_binary_op (location,
+                                      TRUTH_ORIF_EXPR, e1, e2, complain);
+           }
+         else
+           {
+             /* We generate:
+
+                (op0.pfn == op1.pfn
+                && (!op0.pfn || op0.delta == op1.delta))
+
+                The reason for the `!op0.pfn' bit is that a NULL
+                pointer-to-member is any member with a zero PFN; the
+                DELTA field is unspecified.  */
+             e1 = cp_build_binary_op (location,
+                                      EQ_EXPR, delta0, delta1, complain);
+             e2 = cp_build_binary_op (location,
+                                      EQ_EXPR,
+                                      pfn0,
+                                      fold_convert (TREE_TYPE (pfn0),
+                                                    integer_zero_node),
+                                      complain);
+             e1 = cp_build_binary_op (location,
+                                      TRUTH_ORIF_EXPR, e1, e2, complain);
+           }
+         e2 = build2 (EQ_EXPR, boolean_type_node, pfn0, pfn1);
+         e = cp_build_binary_op (location,
+                                 TRUTH_ANDIF_EXPR, e2, e1, complain);
          if (code == EQ_EXPR)
            return e;
-         return cp_build_binary_op (EQ_EXPR, e, integer_zero_node);
+         return cp_build_binary_op (location,
+                                    EQ_EXPR, e, integer_zero_node, complain);
+       }
+      else
+       {
+         gcc_assert (!TYPE_PTRMEMFUNC_P (type0)
+                     || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0),
+                                      type1));
+         gcc_assert (!TYPE_PTRMEMFUNC_P (type1)
+                     || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1),
+                                      type0));
        }
-      else if ((TYPE_PTRMEMFUNC_P (type0)
-               && same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0), type1))
-              || (TYPE_PTRMEMFUNC_P (type1)
-                  && same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1), type0)))
-       abort ();
+
       break;
 
     case MAX_EXPR:
@@ -3654,20 +3809,27 @@ build_binary_op (code, orig_op0, orig_op1, convert_p)
        shorten = 1;
       else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
        result_type = composite_pointer_type (type0, type1, op0, op1,
-                                             "comparison");
+                                             "comparison", complain);
       break;
 
     case LE_EXPR:
     case GE_EXPR:
     case LT_EXPR:
     case GT_EXPR:
+      if (TREE_CODE (orig_op0) == STRING_CST
+         || TREE_CODE (orig_op1) == STRING_CST)
+       {
+         if (complain & tf_warning)
+           warning (OPT_Waddress, "comparison with string literal results in unspecified behaviour");
+       }
+
       build_type = boolean_type_node;
       if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
           && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
        short_compare = 1;
       else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
        result_type = composite_pointer_type (type0, type1, op0, op1,
-                                             "comparison");
+                                             "comparison", complain);
       else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
               && integer_zerop (op1))
        result_type = type0;
@@ -3677,12 +3839,18 @@ build_binary_op (code, orig_op0, orig_op1, convert_p)
       else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
        {
          result_type = type0;
-         pedwarn ("ISO C++ forbids comparison between pointer and integer");
+         if (complain & tf_error)
+           permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
+          else
+            return error_mark_node;
        }
       else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
        {
          result_type = type1;
-         pedwarn ("ISO C++ forbids comparison between pointer and integer");
+         if (complain & tf_error)
+           permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
+          else
+            return error_mark_node;
        }
       break;
 
@@ -3696,7 +3864,8 @@ build_binary_op (code, orig_op0, orig_op1, convert_p)
       build_type = integer_type_node;
       if (code0 != REAL_TYPE || code1 != REAL_TYPE)
        {
-         error ("unordered comparison on non-floating point argument");
+         if (complain & tf_error)
+           error ("unordered comparison on non-floating point argument");
          return error_mark_node;
        }
       common = 1;
@@ -3706,14 +3875,59 @@ build_binary_op (code, orig_op0, orig_op1, convert_p)
       break;
     }
 
-  if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
-      &&
-      (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
+  if (((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
+       || code0 == ENUMERAL_TYPE)
+       && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
+          || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE)))
+    arithmetic_types_p = 1;
+  else
     {
-      int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
+      arithmetic_types_p = 0;
+      /* Vector arithmetic is only allowed when both sides are vectors.  */
+      if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
+       {
+         if (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
+             || !same_scalar_type_ignoring_signedness (TREE_TYPE (type0),
+                                                       TREE_TYPE (type1)))
+           {
+             binary_op_error (location, code, type0, type1);
+             return error_mark_node;
+           }
+         arithmetic_types_p = 1;
+       }
+    }
+  /* Determine the RESULT_TYPE, if it is not already known.  */
+  if (!result_type
+      && arithmetic_types_p
+      && (shorten || common || short_compare))
+    result_type = cp_common_type (type0, type1);
 
-      if (shorten || common || short_compare)
-       result_type = common_type (type0, type1);
+  if (!result_type)
+    {
+      if (complain & tf_error)
+       error ("invalid operands of types %qT and %qT to binary %qO",
+              TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), code);
+      return error_mark_node;
+    }
+
+  /* If we're in a template, the only thing we need to know is the
+     RESULT_TYPE.  */
+  if (processing_template_decl)
+    {
+      /* Since the middle-end checks the type when doing a build2, we
+        need to build the tree in pieces.  This built tree will never
+        get out of the front-end as we replace it when instantiating
+        the template.  */
+      tree tmp = build2 (resultcode,
+                        build_type ? build_type : result_type,
+                        NULL_TREE, op1);
+      TREE_OPERAND (tmp, 0) = op0;
+      return tmp;
+    }
+
+  if (arithmetic_types_p)
+    {
+      int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
 
       /* For certain operations (which identify themselves by shorten != 0)
         if both args were extended from the same smaller type,
@@ -3723,107 +3937,14 @@ build_binary_op (code, orig_op0, orig_op1, convert_p)
         For them, this optimization is safe only if
         both args are zero-extended or both are sign-extended.
         Otherwise, we might change the result.
-        Eg, (short)-1 | (unsigned short)-1 is (int)-1
+        E.g., (short)-1 | (unsigned short)-1 is (int)-1
         but calculated in (unsigned short) it would be (unsigned short)-1.  */
 
       if (shorten && none_complex)
        {
-         int unsigned0, unsigned1;
-         tree arg0 = get_narrower (op0, &unsigned0);
-         tree arg1 = get_narrower (op1, &unsigned1);
-         /* UNS is 1 if the operation to be done is an unsigned one.  */
-         int uns = TREE_UNSIGNED (result_type);
-         tree type;
-
-         final_type = result_type;
-
-         /* Handle the case that OP0 does not *contain* a conversion
-            but it *requires* conversion to FINAL_TYPE.  */
-
-         if (op0 == arg0 && TREE_TYPE (op0) != final_type)
-           unsigned0 = TREE_UNSIGNED (TREE_TYPE (op0));
-         if (op1 == arg1 && TREE_TYPE (op1) != final_type)
-           unsigned1 = TREE_UNSIGNED (TREE_TYPE (op1));
-
-         /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE.  */
-
-         /* For bitwise operations, signedness of nominal type
-            does not matter.  Consider only how operands were extended.  */
-         if (shorten == -1)
-           uns = unsigned0;
-
-         /* Note that in all three cases below we refrain from optimizing
-            an unsigned operation on sign-extended args.
-            That would not be valid.  */
-
-         /* Both args variable: if both extended in same way
-            from same width, do it in that width.
-            Do it unsigned if args were zero-extended.  */
-         if ((TYPE_PRECISION (TREE_TYPE (arg0))
-              < TYPE_PRECISION (result_type))
-             && (TYPE_PRECISION (TREE_TYPE (arg1))
-                 == TYPE_PRECISION (TREE_TYPE (arg0)))
-             && unsigned0 == unsigned1
-             && (unsigned0 || !uns))
-           result_type
-             = signed_or_unsigned_type (unsigned0,
-                                        common_type (TREE_TYPE (arg0),
-                                                     TREE_TYPE (arg1)));
-         else if (TREE_CODE (arg0) == INTEGER_CST
-                  && (unsigned1 || !uns)
-                  && (TYPE_PRECISION (TREE_TYPE (arg1))
-                      < TYPE_PRECISION (result_type))
-                  && (type = signed_or_unsigned_type (unsigned1,
-                                                      TREE_TYPE (arg1)),
-                      int_fits_type_p (arg0, type)))
-           result_type = type;
-         else if (TREE_CODE (arg1) == INTEGER_CST
-                  && (unsigned0 || !uns)
-                  && (TYPE_PRECISION (TREE_TYPE (arg0))
-                      < TYPE_PRECISION (result_type))
-                  && (type = signed_or_unsigned_type (unsigned0,
-                                                      TREE_TYPE (arg0)),
-                      int_fits_type_p (arg1, type)))
-           result_type = type;
-       }
-
-      /* Shifts can be shortened if shifting right.  */
-
-      if (short_shift)
-       {
-         int unsigned_arg;
-         tree arg0 = get_narrower (op0, &unsigned_arg);
-
          final_type = result_type;
-
-         if (arg0 == op0 && final_type == TREE_TYPE (op0))
-           unsigned_arg = TREE_UNSIGNED (TREE_TYPE (op0));
-
-         if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
-             /* We can shorten only if the shift count is less than the
-                number of bits in the smaller type size.  */
-             && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
-             /* If arg is sign-extended and then unsigned-shifted,
-                we can simulate this with a signed shift in arg's type
-                only if the extended result is at least twice as wide
-                as the arg.  Otherwise, the shift could use up all the
-                ones made by sign-extension and bring in zeros.
-                We can't optimize that case at all, but in most machines
-                it never happens because available widths are 2**N.  */
-             && (!TREE_UNSIGNED (final_type)
-                 || unsigned_arg
-                 || (((unsigned) 2 * TYPE_PRECISION (TREE_TYPE (arg0)))
-                     <= TYPE_PRECISION (result_type))))
-           {
-             /* Do an unsigned shift if the operand was zero-extended.  */
-             result_type
-               = signed_or_unsigned_type (unsigned_arg,
-                                          TREE_TYPE (arg0));
-             /* Convert value-to-be-shifted to that type.  */
-             if (TREE_TYPE (op0) != result_type)
-               op0 = cp_convert (result_type, op0);
-             converted = 1;
-           }
+         result_type = shorten_binary_op (result_type, op0, op1, 
+                                          shorten == -1);
        }
 
       /* Comparison operations are shortened too but differently.
@@ -3837,7 +3958,7 @@ build_binary_op (code, orig_op0, orig_op1, convert_p)
             pass the copies by reference, then copy them back afterward.  */
          tree xop0 = op0, xop1 = op1, xresult_type = result_type;
          enum tree_code xresultcode = resultcode;
-         tree val 
+         tree val
            = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
          if (val != 0)
            return cp_convert (boolean_type_node, val);
@@ -3847,152 +3968,46 @@ build_binary_op (code, orig_op0, orig_op1, convert_p)
        }
 
       if ((short_compare || code == MIN_EXPR || code == MAX_EXPR)
-         && warn_sign_compare)
+         && warn_sign_compare
+         /* Do not warn until the template is instantiated; we cannot
+            bound the ranges of the arguments until that point.  */
+         && !processing_template_decl
+          && (complain & tf_warning))
        {
-         int op0_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op0));
-         int op1_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op1));
-
-         int unsignedp0, unsignedp1;
-         tree primop0 = get_narrower (op0, &unsignedp0);
-         tree primop1 = get_narrower (op1, &unsignedp1);
-
-         /* Check for comparison of different enum types.  */
-         if (TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE 
-             && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE 
-             && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
-                != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
-           {
-             warning ("comparison between types `%#T' and `%#T'", 
-                         TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
-           }
-
-         /* Give warnings for comparisons between signed and unsigned
-            quantities that may fail.  */
-         /* Do the checking based on the original operand trees, so that
-            casts will be considered, but default promotions won't be.  */
-
-         /* Do not warn if the comparison is being done in a signed type,
-            since the signed type will only be chosen if it can represent
-            all the values of the unsigned type.  */
-         if (! TREE_UNSIGNED (result_type))
-           /* OK */;
-         /* Do not warn if both operands are unsigned.  */
-         else if (op0_signed == op1_signed)
-           /* OK */;
-         /* Do not warn if the signed quantity is an unsuffixed
-            integer literal (or some static constant expression
-            involving such literals or a conditional expression
-            involving such literals) and it is non-negative.  */
-         else if ((op0_signed && tree_expr_nonnegative_p (orig_op0))
-                  || (op1_signed && tree_expr_nonnegative_p (orig_op1)))
-           /* OK */;
-         /* Do not warn if the comparison is an equality operation,
-            the unsigned quantity is an integral constant and it does
-            not use the most significant bit of result_type.  */
-         else if ((resultcode == EQ_EXPR || resultcode == NE_EXPR)
-                  && ((op0_signed && TREE_CODE (orig_op1) == INTEGER_CST
-                       && int_fits_type_p (orig_op1,
-                                           signed_type (result_type)))
-                       || (op1_signed && TREE_CODE (orig_op0) == INTEGER_CST
-                           && int_fits_type_p (orig_op0,
-                                               signed_type (result_type)))))
-           /* OK */;
-         else
-           warning ("comparison between signed and unsigned integer expressions");
-
-         /* Warn if two unsigned values are being compared in a size
-            larger than their original size, and one (and only one) is the
-            result of a `~' operator.  This comparison will always fail.
-
-            Also warn if one operand is a constant, and the constant does not
-            have all bits set that are set in the ~ operand when it is
-            extended.  */
-
-         if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
-             ^ (TREE_CODE (primop1) == BIT_NOT_EXPR))
-           {
-             if (TREE_CODE (primop0) == BIT_NOT_EXPR)
-               primop0 = get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
-             if (TREE_CODE (primop1) == BIT_NOT_EXPR)
-               primop1 = get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
-             
-             if (host_integerp (primop0, 0) || host_integerp (primop1, 0))
-               {
-                 tree primop;
-                 HOST_WIDE_INT constant, mask;
-                 int unsignedp;
-                 unsigned int bits;
-
-                 if (host_integerp (primop0, 0))
-                   {
-                     primop = primop1;
-                     unsignedp = unsignedp1;
-                     constant = tree_low_cst (primop0, 0);
-                   }
-                 else
-                   {
-                     primop = primop0;
-                     unsignedp = unsignedp0;
-                     constant = tree_low_cst (primop1, 0);
-                   }
-
-                 bits = TYPE_PRECISION (TREE_TYPE (primop));
-                 if (bits < TYPE_PRECISION (result_type)
-                     && bits < HOST_BITS_PER_LONG && unsignedp)
-                   {
-                     mask = (~ (HOST_WIDE_INT) 0) << bits;
-                     if ((mask & constant) != mask)
-                       warning ("comparison of promoted ~unsigned with constant");
-                   }
-               }
-             else if (unsignedp0 && unsignedp1
-                      && (TYPE_PRECISION (TREE_TYPE (primop0))
-                          < TYPE_PRECISION (result_type))
-                      && (TYPE_PRECISION (TREE_TYPE (primop1))
-                          < TYPE_PRECISION (result_type)))
-               warning ("comparison of promoted ~unsigned with unsigned");
-           }
+         warn_for_sign_compare (location, orig_op0, orig_op1, op0, op1, 
+                                result_type, resultcode);
        }
     }
 
-  /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
-     If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
-     Then the expression will be built.
-     It will be given type FINAL_TYPE if that is nonzero;
-     otherwise, it will be given type RESULT_TYPE.  */
-
-  if (!result_type)
-    {
-      error ("invalid operands of types `%T' and `%T' to binary `%O'",
-               TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), code);
-      return error_mark_node;
-    }
-
-  /* Issue warnings about peculiar, but legal, uses of NULL.  */
-  if (/* It's reasonable to use pointer values as operands of &&
+  /* Issue warnings about peculiar, but valid, uses of NULL.  */
+  if ((orig_op0 == null_node || orig_op1 == null_node)
+      /* It's reasonable to use pointer values as operands of &&
         and ||, so NULL is no exception.  */
-      !(code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
-      && (/* If OP0 is NULL and OP1 is not a pointer, or vice versa.  */
-         (orig_op0 == null_node
-          && TREE_CODE (TREE_TYPE (op1)) != POINTER_TYPE)
-         /* Or vice versa.  */
-         || (orig_op1 == null_node
-             && TREE_CODE (TREE_TYPE (op0)) != POINTER_TYPE)
-         /* Or, both are NULL and the operation was not a comparison.  */
-         || (orig_op0 == null_node && orig_op1 == null_node 
-             && code != EQ_EXPR && code != NE_EXPR)))
+      && code != TRUTH_ANDIF_EXPR && code != TRUTH_ORIF_EXPR 
+      && ( /* Both are NULL (or 0) and the operation was not a comparison.  */
+         (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1) 
+          && code != EQ_EXPR && code != NE_EXPR) 
+         /* Or if one of OP0 or OP1 is neither a pointer nor NULL.  */
+         || (!null_ptr_cst_p (orig_op0) && TREE_CODE (TREE_TYPE (op0)) != POINTER_TYPE)
+         || (!null_ptr_cst_p (orig_op1) && TREE_CODE (TREE_TYPE (op1)) != POINTER_TYPE))
+      && (complain & tf_warning))
     /* Some sort of arithmetic operation involving NULL was
        performed.  Note that pointer-difference and pointer-addition
        have already been handled above, and so we don't end up here in
        that case.  */
-    warning ("NULL used in arithmetic");
+    warning (OPT_Wpointer_arith, "NULL used in arithmetic");
+  
 
+  /* If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
+     Then the expression will be built.
+     It will be given type FINAL_TYPE if that is nonzero;
+     otherwise, it will be given type RESULT_TYPE.  */
   if (! converted)
     {
       if (TREE_TYPE (op0) != result_type)
-       op0 = cp_convert (result_type, op0); 
+       op0 = cp_convert_and_check (result_type, op0);
       if (TREE_TYPE (op1) != result_type)
-       op1 = cp_convert (result_type, op1); 
+       op1 = cp_convert_and_check (result_type, op1);
 
       if (op0 == error_mark_node || op1 == error_mark_node)
        return error_mark_node;
@@ -4001,26 +4016,24 @@ build_binary_op (code, orig_op0, orig_op1, convert_p)
   if (build_type == NULL_TREE)
     build_type = result_type;
 
-  {
-    register tree result = build (resultcode, build_type, op0, op1);
-    register tree folded;
-
-    folded = fold (result);
-    if (folded == result)
-      TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
-    if (final_type != 0)
-      return cp_convert (final_type, folded);
-    return folded;
-  }
+  result = build2 (resultcode, build_type, op0, op1);
+  result = fold_if_not_in_template (result);
+  if (final_type != 0)
+    result = cp_convert (final_type, result);
+
+  if (TREE_OVERFLOW_P (result) 
+      && !TREE_OVERFLOW_P (op0) 
+      && !TREE_OVERFLOW_P (op1))
+    overflow_warning (result);
+
+  return result;
 }
 \f
 /* Return a tree for the sum or difference (RESULTCODE says which)
    of pointer PTROP and integer INTOP.  */
 
 static tree
-cp_pointer_int_sum (resultcode, ptrop, intop)
-     enum tree_code resultcode;
-     register tree ptrop, intop;
+cp_pointer_int_sum (enum tree_code resultcode, tree ptrop, tree intop)
 {
   tree res_type = TREE_TYPE (ptrop);
 
@@ -4031,217 +4044,200 @@ cp_pointer_int_sum (resultcode, ptrop, intop)
      pointer_int_sum() anyway.  */
   complete_type (TREE_TYPE (res_type));
 
-  return pointer_int_sum (resultcode, ptrop, fold (intop));
+  return pointer_int_sum (resultcode, ptrop,
+                         fold_if_not_in_template (intop));
 }
 
 /* Return a tree for the difference of pointers OP0 and OP1.
    The resulting tree has type int.  */
 
 static tree
-pointer_diff (op0, op1, ptrtype)
-     register tree op0, op1;
-     register tree ptrtype;
+pointer_diff (tree op0, tree op1, tree ptrtype)
 {
-  register tree result, folded;
+  tree result;
   tree restype = ptrdiff_type_node;
   tree target_type = TREE_TYPE (ptrtype);
 
   if (!complete_type_or_else (target_type, NULL_TREE))
     return error_mark_node;
 
-  if (pedantic || warn_pointer_arith)
-    {
-      if (TREE_CODE (target_type) == VOID_TYPE)
-       pedwarn ("ISO C++ forbids using pointer of type `void *' in subtraction");
-      if (TREE_CODE (target_type) == FUNCTION_TYPE)
-       pedwarn ("ISO C++ forbids using pointer to a function in subtraction");
-      if (TREE_CODE (target_type) == METHOD_TYPE)
-       pedwarn ("ISO C++ forbids using pointer to a method in subtraction");
-      if (TREE_CODE (target_type) == OFFSET_TYPE)
-       pedwarn ("ISO C++ forbids using pointer to a member in subtraction");
-    }
+  if (TREE_CODE (target_type) == VOID_TYPE)
+    permerror (input_location, "ISO C++ forbids using pointer of type %<void *%> in subtraction");
+  if (TREE_CODE (target_type) == FUNCTION_TYPE)
+    permerror (input_location, "ISO C++ forbids using pointer to a function in subtraction");
+  if (TREE_CODE (target_type) == METHOD_TYPE)
+    permerror (input_location, "ISO C++ forbids using pointer to a method in subtraction");
 
   /* First do the subtraction as integers;
      then drop through to build the divide operator.  */
 
-  op0 = cp_build_binary_op (MINUS_EXPR, 
+  op0 = cp_build_binary_op (input_location,
+                           MINUS_EXPR,
                            cp_convert (restype, op0),
-                           cp_convert (restype, op1));
+                           cp_convert (restype, op1),
+                           tf_warning_or_error);
 
   /* This generates an error if op1 is a pointer to an incomplete type.  */
   if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1))))
     error ("invalid use of a pointer to an incomplete type in pointer arithmetic");
 
-  op1 = ((TREE_CODE (target_type) == VOID_TYPE
-         || TREE_CODE (target_type) == FUNCTION_TYPE
-         || TREE_CODE (target_type) == METHOD_TYPE
-         || TREE_CODE (target_type) == OFFSET_TYPE)
-        ? integer_one_node
-        : size_in_bytes (target_type));
+  op1 = (TYPE_PTROB_P (ptrtype)
+        ? size_in_bytes (target_type)
+        : integer_one_node);
 
   /* Do the division.  */
 
-  result = build (EXACT_DIV_EXPR, restype, op0, cp_convert (restype, op1));
-
-  folded = fold (result);
-  if (folded == result)
-    TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
-  return folded;
+  result = build2 (EXACT_DIV_EXPR, restype, op0, cp_convert (restype, op1));
+  return fold_if_not_in_template (result);
 }
 \f
-/* Handle the case of taking the address of a COMPONENT_REF.
-   Called by `build_unary_op'.
-
-   ARG is the COMPONENT_REF whose address we want.
-   ARGTYPE is the pointer type that this address should have. */
-
-static tree
-build_component_addr (arg, argtype)
-     tree arg, argtype;
-{
-  tree field = TREE_OPERAND (arg, 1);
-  tree basetype = decl_type_context (field);
-  tree rval = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), 0);
-
-  my_friendly_assert (TREE_CODE (field) == FIELD_DECL, 981018);
-
-  if (DECL_C_BIT_FIELD (field))
-    {
-      error ("attempt to take address of bit-field structure member `%D'",
-                field);
-      return error_mark_node;
-    }
-
-  if (TREE_CODE (field) == FIELD_DECL
-      && TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (basetype))
-    {
-      /* Can't convert directly to ARGTYPE, since that
-        may have the same pointer type as one of our
-        baseclasses.  */
-      tree binfo = lookup_base (TREE_TYPE (TREE_TYPE (rval)), basetype,
-                               ba_check, NULL);
-
-      rval = build_base_path (PLUS_EXPR, rval, binfo, 1);
-      rval = build1 (NOP_EXPR, argtype, rval);
-      TREE_CONSTANT (rval) = TREE_CONSTANT (TREE_OPERAND (rval, 0));
-    }
-  else
-    /* This conversion is harmless.  */
-    rval = convert_force (argtype, rval, 0);
-
-  return fold (build (PLUS_EXPR, argtype, rval,
-                     cp_convert (argtype, byte_position (field))));
-}
-   
 /* Construct and perhaps optimize a tree representation
    for a unary operation.  CODE, a tree_code, specifies the operation
    and XARG is the operand.  */
 
 tree
-build_x_unary_op (code, xarg)
-     enum tree_code code;
-     tree xarg;
+build_x_unary_op (enum tree_code code, tree xarg, tsubst_flags_t complain)
 {
+  tree orig_expr = xarg;
   tree exp;
   int ptrmem = 0;
-  
+
   if (processing_template_decl)
-    return build_min_nt (code, xarg, NULL_TREE);
+    {
+      if (type_dependent_expression_p (xarg))
+       return build_min_nt (code, xarg, NULL_TREE);
+
+      xarg = build_non_dependent_expr (xarg);
+    }
+
+  exp = NULL_TREE;
+
+  /* [expr.unary.op] says:
 
-  /* & rec, on incomplete RECORD_TYPEs is the simple opr &, not an
-     error message.  */
+       The address of an object of incomplete type can be taken.
+
+     (And is just the ordinary address operator, not an overloaded
+     "operator &".)  However, if the type is a template
+     specialization, we must complete the type at this point so that
+     an overloaded "operator &" will be available if required.  */
   if (code == ADDR_EXPR
       && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
-      && ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (xarg)))
-          && !COMPLETE_TYPE_P (TREE_TYPE (xarg)))
+      && ((CLASS_TYPE_P (TREE_TYPE (xarg))
+          && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (xarg))))
          || (TREE_CODE (xarg) == OFFSET_REF)))
-    /* don't look for a function */;
+    /* Don't look for a function.  */;
   else
-    {
-      tree rval;
-
-      rval = build_new_op (code, LOOKUP_NORMAL, xarg,
-                          NULL_TREE, NULL_TREE);
-      if (rval || code != ADDR_EXPR)
-       return rval;
-    }
-  if (code == ADDR_EXPR)
+    exp = build_new_op (code, LOOKUP_NORMAL, xarg, NULL_TREE, NULL_TREE,
+                       /*overloaded_p=*/NULL, complain);
+  if (!exp && code == ADDR_EXPR)
     {
       /*  A pointer to member-function can be formed only by saying
          &X::mf.  */
       if (!flag_ms_extensions && TREE_CODE (TREE_TYPE (xarg)) == METHOD_TYPE
          && (TREE_CODE (xarg) != OFFSET_REF || !PTRMEM_OK_P (xarg)))
        {
-         if (TREE_CODE (xarg) != OFFSET_REF)
+         if (TREE_CODE (xarg) != OFFSET_REF
+             || !TYPE_P (TREE_OPERAND (xarg, 0)))
            {
-             error ("invalid use of '%E' to form a pointer-to-member-function.  Use a qualified-id.",
-                    xarg);
+              error ("invalid use of %qE to form a pointer-to-member-function",
+                     xarg);
+              if (TREE_CODE (xarg) != OFFSET_REF)
+                inform (input_location, "  a qualified-id is required");
              return error_mark_node;
            }
          else
            {
-             error ("parenthesis around '%E' cannot be used to form a pointer-to-member-function",
+             error ("parentheses around %qE cannot be used to form a"
+                    " pointer-to-member-function",
                     xarg);
              PTRMEM_OK_P (xarg) = 1;
            }
        }
-      
+
       if (TREE_CODE (xarg) == OFFSET_REF)
-        {
-          ptrmem = PTRMEM_OK_P (xarg);
-          
-          if (!ptrmem && !flag_ms_extensions
-              && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg, 1))) == METHOD_TYPE)
+       {
+         ptrmem = PTRMEM_OK_P (xarg);
+
+         if (!ptrmem && !flag_ms_extensions
+             && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg, 1))) == METHOD_TYPE)
            {
              /* A single non-static member, make sure we don't allow a
-                 pointer-to-member.  */
-             xarg = build (OFFSET_REF, TREE_TYPE (xarg),
-                           TREE_OPERAND (xarg, 0),
-                           ovl_cons (TREE_OPERAND (xarg, 1), NULL_TREE));
+                pointer-to-member.  */
+             xarg = build2 (OFFSET_REF, TREE_TYPE (xarg),
+                            TREE_OPERAND (xarg, 0),
+                            ovl_cons (TREE_OPERAND (xarg, 1), NULL_TREE));
              PTRMEM_OK_P (xarg) = ptrmem;
            }
-             
-        }
-      else if (TREE_CODE (xarg) == TARGET_EXPR)
-       warning ("taking address of temporary");
+       }
+      else if (TREE_CODE (xarg) == TARGET_EXPR && (complain & tf_warning))
+       warning (0, "taking address of temporary");
+      exp = cp_build_unary_op (ADDR_EXPR, xarg, 0, complain);
     }
-  exp = build_unary_op (code, xarg, 0);
+
+  if (processing_template_decl && exp != error_mark_node)
+    exp = build_min_non_dep (code, exp, orig_expr,
+                            /*For {PRE,POST}{INC,DEC}REMENT_EXPR*/NULL_TREE);
   if (TREE_CODE (exp) == ADDR_EXPR)
     PTRMEM_OK_P (exp) = ptrmem;
-
   return exp;
 }
 
-/* Like truthvalue_conversion, but handle pointer-to-member constants, where
-   a null value is represented by an INTEGER_CST of -1.  */
+/* Like c_common_truthvalue_conversion, but handle pointer-to-member
+   constants, where a null value is represented by an INTEGER_CST of
+   -1.  */
 
 tree
-cp_truthvalue_conversion (expr)
-     tree expr;
+cp_truthvalue_conversion (tree expr)
 {
   tree type = TREE_TYPE (expr);
   if (TYPE_PTRMEM_P (type))
-    return build_binary_op (NE_EXPR, expr, integer_zero_node, 1);
+    return build_binary_op (EXPR_LOCATION (expr),
+                           NE_EXPR, expr, integer_zero_node, 1);
   else
-    return truthvalue_conversion (expr);
+    return c_common_truthvalue_conversion (input_location, expr);
 }
 
 /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR.  */
-   
+
 tree
-condition_conversion (expr)
-     tree expr;
+condition_conversion (tree expr)
 {
   tree t;
   if (processing_template_decl)
     return expr;
-  if (TREE_CODE (expr) == OFFSET_REF)
-    expr = resolve_offset_ref (expr);
-  t = perform_implicit_conversion (boolean_type_node, expr);
-  t = fold (build1 (CLEANUP_POINT_EXPR, boolean_type_node, t));
+  t = perform_implicit_conversion (boolean_type_node, expr, 
+                                  tf_warning_or_error);
+  t = fold_build_cleanup_point_expr (boolean_type_node, t);
   return t;
 }
-                              
+
+/* Return an ADDR_EXPR giving the address of T.  This function
+   attempts no optimizations or simplifications; it is a low-level
+   primitive.  */
+
+tree
+build_address (tree t)
+{
+  tree addr;
+
+  if (error_operand_p (t) || !cxx_mark_addressable (t))
+    return error_mark_node;
+
+  addr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (t)), t);
+
+  return addr;
+}
+
+/* Return a NOP_EXPR converting EXPR to TYPE.  */
+
+tree
+build_nop (tree type, tree expr)
+{
+  if (type == error_mark_node || error_operand_p (expr))
+    return expr;
+  return build1 (NOP_EXPR, type, expr);
+}
+
 /* C++: Must handle pointers to members.
 
    Perhaps type instantiation should be extended to handle conversion
@@ -4252,43 +4248,53 @@ condition_conversion (expr)
    (such as from short to int).  */
 
 tree
-build_unary_op (code, xarg, noconvert)
-     enum tree_code code;
-     tree xarg;
-     int noconvert;
+cp_build_unary_op (enum tree_code code, tree xarg, int noconvert, 
+                   tsubst_flags_t complain)
 {
   /* No default_conversion here.  It causes trouble for ADDR_EXPR.  */
-  register tree arg = xarg;
-  register tree argtype = 0;
+  tree arg = xarg;
+  tree argtype = 0;
   const char *errstring = NULL;
   tree val;
+  const char *invalid_op_diag;
 
-  if (arg == error_mark_node)
+  if (error_operand_p (arg))
     return error_mark_node;
 
-  switch (code)
+  if ((invalid_op_diag
+       = targetm.invalid_unary_op ((code == UNARY_PLUS_EXPR
+                                   ? CONVERT_EXPR
+                                   : code),
+                                  TREE_TYPE (xarg))))
     {
-    case CONVERT_EXPR:
-      /* This is used for unary plus, because a CONVERT_EXPR
-        is enough to prevent anybody from looking inside for
-        associativity, but won't generate any code.  */
-      if (!(arg = build_expr_type_conversion
-           (WANT_ARITH | WANT_ENUM | WANT_POINTER, arg, 1)))
-       errstring = "wrong type argument to unary plus";
-      else
-       {
-         if (!noconvert)
-          arg = default_conversion (arg);
-         arg = build1 (NON_LVALUE_EXPR, TREE_TYPE (arg), arg);
-         TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
-       }
-      break;
+      error (invalid_op_diag);
+      return error_mark_node;
+    }
 
+  switch (code)
+    {
+    case UNARY_PLUS_EXPR:
     case NEGATE_EXPR:
-      if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, 1)))
-       errstring = "wrong type argument to unary minus";
-      else if (!noconvert)
-       arg = default_conversion (arg);
+      {
+       int flags = WANT_ARITH | WANT_ENUM;
+       /* Unary plus (but not unary minus) is allowed on pointers.  */
+       if (code == UNARY_PLUS_EXPR)
+         flags |= WANT_POINTER;
+       arg = build_expr_type_conversion (flags, arg, true);
+       if (!arg)
+         errstring = (code == NEGATE_EXPR
+                      ? "wrong type argument to unary minus"
+                      : "wrong type argument to unary plus");
+       else
+         {
+           if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
+             arg = perform_integral_promotions (arg);
+
+           /* Make sure the result is not an lvalue: a unary plus or minus
+              expression is always a rvalue.  */
+           arg = rvalue (arg);
+         }
+      }
       break;
 
     case BIT_NOT_EXPR:
@@ -4298,15 +4304,16 @@ build_unary_op (code, xarg, noconvert)
          if (!noconvert)
            arg = default_conversion (arg);
        }
-      else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM,
-                                                  arg, 1)))
+      else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM
+                                                  | WANT_VECTOR,
+                                                  arg, true)))
        errstring = "wrong type argument to bit-complement";
-      else if (!noconvert)
-       arg = default_conversion (arg);
+      else if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
+       arg = perform_integral_promotions (arg);
       break;
 
     case ABS_EXPR:
-      if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, 1)))
+      if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
        errstring = "wrong type argument to abs";
       else if (!noconvert)
        arg = default_conversion (arg);
@@ -4314,14 +4321,15 @@ build_unary_op (code, xarg, noconvert)
 
     case CONJ_EXPR:
       /* Conjugating a real value is a no-op, but allow it anyway.  */
-      if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, 1)))
+      if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
        errstring = "wrong type argument to conjugation";
       else if (!noconvert)
        arg = default_conversion (arg);
       break;
 
     case TRUTH_NOT_EXPR:
-      arg = cp_convert (boolean_type_node, arg);
+      arg = perform_implicit_conversion (boolean_type_node, arg,
+                                        complain);
       val = invert_truthvalue (arg);
       if (arg != error_mark_node)
        return val;
@@ -4330,12 +4338,15 @@ build_unary_op (code, xarg, noconvert)
 
     case NOP_EXPR:
       break;
-      
+
     case REALPART_EXPR:
       if (TREE_CODE (arg) == COMPLEX_CST)
        return TREE_REALPART (arg);
       else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
-       return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
+       {
+         arg = build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg);
+         return fold_if_not_in_template (arg);
+       }
       else
        return arg;
 
@@ -4343,10 +4354,13 @@ build_unary_op (code, xarg, noconvert)
       if (TREE_CODE (arg) == COMPLEX_CST)
        return TREE_IMAGPART (arg);
       else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
-       return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
+       {
+         arg = build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg);
+         return fold_if_not_in_template (arg);
+       }
       else
        return cp_convert (TREE_TYPE (arg), integer_zero_node);
-      
+
     case PREINCREMENT_EXPR:
     case POSTINCREMENT_EXPR:
     case PREDECREMENT_EXPR:
@@ -4365,16 +4379,19 @@ build_unary_op (code, xarg, noconvert)
          tree real, imag;
 
          arg = stabilize_reference (arg);
-         real = build_unary_op (REALPART_EXPR, arg, 1);
-         imag = build_unary_op (IMAGPART_EXPR, arg, 1);
-         return build (COMPLEX_EXPR, TREE_TYPE (arg),
-                       build_unary_op (code, real, 1), imag);
+         real = cp_build_unary_op (REALPART_EXPR, arg, 1, complain);
+         imag = cp_build_unary_op (IMAGPART_EXPR, arg, 1, complain);
+         real = cp_build_unary_op (code, real, 1, complain);
+         if (real == error_mark_node || imag == error_mark_node)
+           return error_mark_node;
+         return build2 (COMPLEX_EXPR, TREE_TYPE (arg),
+                        real, imag);
        }
 
       /* Report invalid types.  */
 
       if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
-                                             arg, 1)))
+                                             arg, true)))
        {
          if (code == PREINCREMENT_EXPR)
            errstring ="no pre-increment operator for type";
@@ -4386,119 +4403,100 @@ build_unary_op (code, xarg, noconvert)
            errstring ="no post-decrement operator for type";
          break;
        }
+      else if (arg == error_mark_node)
+       return error_mark_node;
 
       /* Report something read-only.  */
 
       if (CP_TYPE_CONST_P (TREE_TYPE (arg))
-         || TREE_READONLY (arg))
-       readonly_error (arg, ((code == PREINCREMENT_EXPR
-                              || code == POSTINCREMENT_EXPR)
-                             ? "increment" : "decrement"),
-                       0);
+         || TREE_READONLY (arg)) 
+        {
+          if (complain & tf_error)
+            readonly_error (arg, ((code == PREINCREMENT_EXPR
+                                   || code == POSTINCREMENT_EXPR)
+                                  ? "increment" : "decrement"));
+          else
+            return error_mark_node;
+        }
 
       {
-       register tree inc;
-       tree result_type = TREE_TYPE (arg);
+       tree inc;
+       tree declared_type = unlowered_expr_type (arg);
 
-       arg = get_unwidened (arg, 0);
        argtype = TREE_TYPE (arg);
 
        /* ARM $5.2.5 last annotation says this should be forbidden.  */
        if (TREE_CODE (argtype) == ENUMERAL_TYPE)
-         pedwarn ("ISO C++ forbids %sing an enum",
-                  (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
-                  ? "increment" : "decrement");
-           
+          {
+            if (complain & tf_error)
+              permerror (input_location, (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
+                         ? G_("ISO C++ forbids incrementing an enum")
+                         : G_("ISO C++ forbids decrementing an enum"));
+            else
+              return error_mark_node;
+          }
+
        /* Compute the increment.  */
 
        if (TREE_CODE (argtype) == POINTER_TYPE)
          {
-           enum tree_code tmp = TREE_CODE (TREE_TYPE (argtype));
            tree type = complete_type (TREE_TYPE (argtype));
-           
+
            if (!COMPLETE_OR_VOID_TYPE_P (type))
-             error ("cannot %s a pointer to incomplete type `%T'",
-                       ((code == PREINCREMENT_EXPR
-                         || code == POSTINCREMENT_EXPR)
-                        ? "increment" : "decrement"), TREE_TYPE (argtype));
+              {
+                if (complain & tf_error)
+                  error (((code == PREINCREMENT_EXPR
+                           || code == POSTINCREMENT_EXPR))
+                         ? G_("cannot increment a pointer to incomplete type %qT")
+                         : G_("cannot decrement a pointer to incomplete type %qT"),
+                         TREE_TYPE (argtype));
+                else
+                  return error_mark_node;
+              }
            else if ((pedantic || warn_pointer_arith)
-                    && (tmp == FUNCTION_TYPE || tmp == METHOD_TYPE
-                        || tmp == VOID_TYPE || tmp == OFFSET_TYPE))
-             pedwarn ("ISO C++ forbids %sing a pointer of type `%T'",
-                         ((code == PREINCREMENT_EXPR
-                           || code == POSTINCREMENT_EXPR)
-                          ? "increment" : "decrement"), argtype);
-           inc = c_sizeof_nowarn (TREE_TYPE (argtype));
+                    && !TYPE_PTROB_P (argtype)) 
+              {
+                if (complain & tf_error)
+                  permerror (input_location, (code == PREINCREMENT_EXPR
+                              || code == POSTINCREMENT_EXPR)
+                             ? G_("ISO C++ forbids incrementing a pointer of type %qT")
+                             : G_("ISO C++ forbids decrementing a pointer of type %qT"),
+                             argtype);
+                else
+                  return error_mark_node;
+              }
+
+           inc = cxx_sizeof_nowarn (TREE_TYPE (argtype));
          }
        else
          inc = integer_one_node;
 
        inc = cp_convert (argtype, inc);
 
-       /* Handle incrementing a cast-expression.  */
-
-       switch (TREE_CODE (arg))
-         {
-         case NOP_EXPR:
-         case CONVERT_EXPR:
-         case FLOAT_EXPR:
-         case FIX_TRUNC_EXPR:
-         case FIX_FLOOR_EXPR:
-         case FIX_ROUND_EXPR:
-         case FIX_CEIL_EXPR:
-           {
-             tree incremented, modify, value, compound;
-             if (! lvalue_p (arg) && pedantic)
-               pedwarn ("cast to non-reference type used as lvalue");
-             arg = stabilize_reference (arg);
-             if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
-               value = arg;
-             else
-               value = save_expr (arg);
-             incremented = build (((code == PREINCREMENT_EXPR
-                                    || code == POSTINCREMENT_EXPR)
-                                   ? PLUS_EXPR : MINUS_EXPR),
-                                  argtype, value, inc);
-
-             modify = build_modify_expr (arg, NOP_EXPR, incremented);
-             compound = build (COMPOUND_EXPR, TREE_TYPE (arg), modify, value);
-
-             /* Eliminate warning about unused result of + or -.  */
-             TREE_NO_UNUSED_WARNING (compound) = 1;
-             return compound;
-           }
-
-         default:
-           break;
-         }
-
        /* Complain about anything else that is not a true lvalue.  */
        if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
                                    || code == POSTINCREMENT_EXPR)
-                                  ? "increment" : "decrement")))
+                                  ? lv_increment : lv_decrement),
+                             complain))
          return error_mark_node;
 
        /* Forbid using -- on `bool'.  */
-       if (TREE_TYPE (arg) == boolean_type_node)
+       if (same_type_p (declared_type, boolean_type_node))
          {
            if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
              {
-               error ("invalid use of `--' on bool variable `%D'", arg);
+                if (complain & tf_error)
+                  error ("invalid use of Boolean expression as operand "
+                         "to %<operator--%>");
                return error_mark_node;
              }
-#if 0
-           /* This will only work if someone can convince Kenner to accept
-              my patch to expand_increment. (jason)  */
-           val = build (code, TREE_TYPE (arg), arg, inc);
-#else
            val = boolean_increment (code, arg);
-#endif
          }
        else
-         val = build (code, TREE_TYPE (arg), arg, inc);
+         val = build2 (code, TREE_TYPE (arg), arg, inc);
 
        TREE_SIDE_EFFECTS (val) = 1;
-       return cp_convert (result_type, val);
+       return val;
       }
 
     case ADDR_EXPR:
@@ -4506,17 +4504,27 @@ build_unary_op (code, xarg, noconvert)
         regardless of NOCONVERT.  */
 
       argtype = lvalue_type (arg);
+
+      if (TREE_CODE (arg) == OFFSET_REF)
+       goto offset_ref;
+
       if (TREE_CODE (argtype) == REFERENCE_TYPE)
        {
-         arg = build1
-           (CONVERT_EXPR,
-            build_pointer_type (TREE_TYPE (argtype)), arg);
-         TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
+         tree type = build_pointer_type (TREE_TYPE (argtype));
+         arg = build1 (CONVERT_EXPR, type, arg);
          return arg;
        }
       else if (pedantic && DECL_MAIN_P (arg))
-       /* ARM $3.4 */
-       pedwarn ("ISO C++ forbids taking address of function `::main'");
+        {
+          /* ARM $3.4 */
+         /* Apparently a lot of autoconf scripts for C++ packages do this,
+            so only complain if -pedantic.  */
+          if (complain & (flag_pedantic_errors ? tf_error : tf_warning))
+            pedwarn (input_location, OPT_pedantic,
+                    "ISO C++ forbids taking address of function %<::main%>");
+          else if (flag_pedantic_errors)
+            return error_mark_node;
+        }
 
       /* Let &* cancel out to simplify resulting code.  */
       if (TREE_CODE (arg) == INDIRECT_REF)
@@ -4529,42 +4537,25 @@ build_unary_op (code, xarg, noconvert)
          arg = TREE_OPERAND (arg, 0);
          if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE)
            {
-             arg = build1
-               (CONVERT_EXPR,
-                build_pointer_type (TREE_TYPE (TREE_TYPE (arg))), arg);
-             TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
+             tree type = build_pointer_type (TREE_TYPE (TREE_TYPE (arg)));
+             arg = build1 (CONVERT_EXPR, type, arg);
            }
-         else if (lvalue_p (arg))
+         else
            /* Don't let this be an lvalue.  */
-           return non_lvalue (arg);
+           arg = rvalue (arg);
          return arg;
        }
 
-      /* For &x[y], return x+y */
-      if (TREE_CODE (arg) == ARRAY_REF)
-       {
-         if (mark_addressable (TREE_OPERAND (arg, 0)) == 0)
-           return error_mark_node;
-         return cp_build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
-                                    TREE_OPERAND (arg, 1));
-       }
-
       /* Uninstantiated types are all functions.  Taking the
         address of a function is a no-op, so just return the
         argument.  */
 
-      if (TREE_CODE (arg) == IDENTIFIER_NODE
-         && IDENTIFIER_OPNAME_P (arg))
-       {
-         abort ();
-         /* We don't know the type yet, so just work around the problem.
-            We know that this will resolve to an lvalue.  */
-         return build1 (ADDR_EXPR, unknown_type_node, arg);
-       }
+      gcc_assert (TREE_CODE (arg) != IDENTIFIER_NODE
+                 || !IDENTIFIER_OPNAME_P (arg));
 
       if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg)
-          && OVL_NEXT (TREE_OPERAND (arg, 1)) == NULL_TREE)
-        {
+         && !really_overloaded_fn (TREE_OPERAND (arg, 1)))
+       {
          /* They're trying to take the address of a unique non-static
             member function.  This is ill-formed (except in MS-land),
             but let's try to DTRT.
@@ -4575,24 +4566,39 @@ build_unary_op (code, xarg, noconvert)
             We could defer this in non-MS mode, but it's easier to give
             a useful error here.  */
 
-         tree base = TREE_TYPE (TREE_OPERAND (arg, 0));
-         tree name = DECL_NAME (OVL_CURRENT (TREE_OPERAND (arg, 1)));
+         /* Inside constant member functions, the `this' pointer
+            contains an extra const qualifier.  TYPE_MAIN_VARIANT
+            is used here to remove this const from the diagnostics
+            and the created OFFSET_REF.  */
+         tree base = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg, 0)));
+         tree fn = get_first_fn (TREE_OPERAND (arg, 1));
+         mark_used (fn);
 
          if (! flag_ms_extensions)
            {
-             if (current_class_type
-                 && TREE_OPERAND (arg, 0) == current_class_ref)
-               /* An expression like &memfn.  */
-               pedwarn ("ISO C++ forbids taking the address of an unqualified non-static member function to form a pointer to member function.  Say `&%T::%D'", base, name);
+             tree name = DECL_NAME (fn);
+              if (!(complain & tf_error))
+                return error_mark_node;
+             else if (current_class_type
+                       && TREE_OPERAND (arg, 0) == current_class_ref)
+                  /* An expression like &memfn.  */
+                permerror (input_location, "ISO C++ forbids taking the address of an unqualified"
+                           " or parenthesized non-static member function to form"
+                           " a pointer to member function.  Say %<&%T::%D%>",
+                           base, name);
              else
-               pedwarn ("ISO C++ forbids taking the address of a bound member function to form a pointer to member function.  Say `&%T::%D'", base, name);
+               permerror (input_location, "ISO C++ forbids taking the address of a bound member"
+                          " function to form a pointer to member function."
+                          "  Say %<&%T::%D%>",
+                          base, name);
            }
-         arg = build_offset_ref (base, name);
-        }
-        
+         arg = build_offset_ref (base, fn, /*address_p=*/true);
+       }
+
+    offset_ref:
       if (type_unknown_p (arg))
        return build1 (ADDR_EXPR, unknown_type_node, arg);
-       
+
       /* Handle complex lvalues (when permitted)
         by reduction to simpler cases.  */
       val = unary_complex_lvalue (code, arg);
@@ -4601,61 +4607,116 @@ build_unary_op (code, xarg, noconvert)
 
       switch (TREE_CODE (arg))
        {
-       case NOP_EXPR:
-       case CONVERT_EXPR:
+       CASE_CONVERT:
        case FLOAT_EXPR:
        case FIX_TRUNC_EXPR:
-       case FIX_FLOOR_EXPR:
-       case FIX_ROUND_EXPR:
-       case FIX_CEIL_EXPR:
-         if (! lvalue_p (arg) && pedantic)
-           pedwarn ("ISO C++ forbids taking the address of a cast to a non-lvalue expression");
+          /* Even if we're not being pedantic, we cannot allow this
+             extension when we're instantiating in a SFINAE
+             context.  */
+         if (! lvalue_p (arg) && complain == tf_none)
+            {
+              if (complain & tf_error)
+                permerror (input_location, "ISO C++ forbids taking the address of a cast to a non-lvalue expression");
+              else
+                return error_mark_node;
+            }
          break;
-         
+
+       case BASELINK:
+         arg = BASELINK_FUNCTIONS (arg);
+         /* Fall through.  */
+
+       case OVERLOAD:
+         arg = OVL_CURRENT (arg);
+         break;
+
+       case OFFSET_REF:
+         /* Turn a reference to a non-static data member into a
+            pointer-to-member.  */
+         {
+           tree type;
+           tree t;
+
+           if (!PTRMEM_OK_P (arg))
+             return cp_build_unary_op (code, arg, 0, complain);
+
+           t = TREE_OPERAND (arg, 1);
+           if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
+             {
+                if (complain & tf_error)
+                  error ("cannot create pointer to reference member %qD", t);
+               return error_mark_node;
+             }
+
+           type = build_ptrmem_type (context_for_name_lookup (t),
+                                     TREE_TYPE (t));
+           t = make_ptrmem_cst (type, TREE_OPERAND (arg, 1));
+           return t;
+         }
+
        default:
          break;
        }
 
-      /* Allow the address of a constructor if all the elements
-        are constant.  */
-      if (TREE_CODE (arg) == CONSTRUCTOR && TREE_HAS_CONSTRUCTOR (arg)
-         && TREE_CONSTANT (arg))
-       ;
       /* Anything not already handled and not a true memory reference
         is an error.  */
-      else if (TREE_CODE (argtype) != FUNCTION_TYPE
-              && TREE_CODE (argtype) != METHOD_TYPE
-              && !lvalue_or_else (arg, "unary `&'"))
+      if (TREE_CODE (argtype) != FUNCTION_TYPE
+         && TREE_CODE (argtype) != METHOD_TYPE
+         && TREE_CODE (arg) != OFFSET_REF
+         && !lvalue_or_else (arg, lv_addressof, complain))
        return error_mark_node;
 
       if (argtype != error_mark_node)
        argtype = build_pointer_type (argtype);
 
-      if (mark_addressable (arg) == 0)
-       return error_mark_node;
-
-      {
-       tree addr;
-
-       if (TREE_CODE (arg) == COMPONENT_REF)
-         addr = build_component_addr (arg, argtype);
-       else
-         addr = build1 (ADDR_EXPR, argtype, arg);
-
-       /* Address of a static or external variable or
-          function counts as a constant */
-       if (staticp (arg))
-         TREE_CONSTANT (addr) = 1;
+      /* In a template, we are processing a non-dependent expression
+        so we can just form an ADDR_EXPR with the correct type.  */
+      if (processing_template_decl || TREE_CODE (arg) != COMPONENT_REF)
+       {
+         val = build_address (arg);
+         if (TREE_CODE (arg) == OFFSET_REF)
+           PTRMEM_OK_P (val) = PTRMEM_OK_P (arg);
+       }
+      else if (TREE_CODE (TREE_OPERAND (arg, 1)) == BASELINK)
+       {
+         tree fn = BASELINK_FUNCTIONS (TREE_OPERAND (arg, 1));
+
+         /* We can only get here with a single static member
+            function.  */
+         gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
+                     && DECL_STATIC_FUNCTION_P (fn));
+         mark_used (fn);
+         val = build_address (fn);
+         if (TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
+           /* Do not lose object's side effects.  */
+           val = build2 (COMPOUND_EXPR, TREE_TYPE (val),
+                         TREE_OPERAND (arg, 0), val);
+       }
+      else if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
+       {
+          if (complain & tf_error)
+            error ("attempt to take address of bit-field structure member %qD",
+                   TREE_OPERAND (arg, 1));
+         return error_mark_node;
+       }
+      else
+       {
+         tree object = TREE_OPERAND (arg, 0);
+         tree field = TREE_OPERAND (arg, 1);
+         gcc_assert (same_type_ignoring_top_level_qualifiers_p
+                     (TREE_TYPE (object), decl_type_context (field)));
+         val = build_address (arg);
+       }
 
-       if (TREE_CODE (argtype) == POINTER_TYPE
-           && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
-         {
-           build_ptrmemfunc_type (argtype);
-           addr = build_ptrmemfunc (argtype, addr, 0);
-         }
+      if (TREE_CODE (argtype) == POINTER_TYPE
+         && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
+       {
+         build_ptrmemfunc_type (argtype);
+         val = build_ptrmemfunc (argtype, val, 0,
+                                 /*c_cast_p=*/false);
+       }
 
-       return addr;
-      }
+      return val;
 
     default:
       break;
@@ -4665,36 +4726,51 @@ build_unary_op (code, xarg, noconvert)
     {
       if (argtype == 0)
        argtype = TREE_TYPE (arg);
-      return fold (build1 (code, argtype, arg));
+      return fold_if_not_in_template (build1 (code, argtype, arg));
     }
 
-  error ("%s", errstring);
+  if (complain & tf_error)
+    error ("%s", errstring);
   return error_mark_node;
 }
 
+/* Hook for the c-common bits that build a unary op.  */
+tree
+build_unary_op (location_t location ATTRIBUTE_UNUSED,
+               enum tree_code code, tree xarg, int noconvert)
+{
+  return cp_build_unary_op (code, xarg, noconvert, tf_warning_or_error);
+}
+
 /* Apply unary lvalue-demanding operator CODE to the expression ARG
    for certain kinds of expressions which are not really lvalues
    but which we can accept as lvalues.
 
-   If ARG is not a kind of expression we can handle, return zero.  */
-   
+   If ARG is not a kind of expression we can handle, return
+   NULL_TREE.  */
+
 tree
-unary_complex_lvalue (code, arg)
-     enum tree_code code;
-     tree arg;
+unary_complex_lvalue (enum tree_code code, tree arg)
 {
+  /* Inside a template, making these kinds of adjustments is
+     pointless; we are only concerned with the type of the
+     expression.  */
+  if (processing_template_decl)
+    return NULL_TREE;
+
   /* Handle (a, b) used as an "lvalue".  */
   if (TREE_CODE (arg) == COMPOUND_EXPR)
     {
-      tree real_result = build_unary_op (code, TREE_OPERAND (arg, 1), 0);
-      return build (COMPOUND_EXPR, TREE_TYPE (real_result),
-                   TREE_OPERAND (arg, 0), real_result);
+      tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 1), 0,
+                                            tf_warning_or_error);
+      return build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
+                    TREE_OPERAND (arg, 0), real_result);
     }
 
   /* Handle (a ? b : c) used as an "lvalue".  */
   if (TREE_CODE (arg) == COND_EXPR
       || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
-    return rationalize_conditional_expr (code, arg);
+    return rationalize_conditional_expr (code, arg, tf_warning_or_error);
 
   /* Handle (a = b), (++a), and (--a) used as an "lvalue".  */
   if (TREE_CODE (arg) == MODIFY_EXPR
@@ -4705,88 +4781,33 @@ unary_complex_lvalue (code, arg)
       if (TREE_SIDE_EFFECTS (lvalue))
        {
          lvalue = stabilize_reference (lvalue);
-         arg = build (TREE_CODE (arg), TREE_TYPE (arg),
-                      lvalue, TREE_OPERAND (arg, 1));
+         arg = build2 (TREE_CODE (arg), TREE_TYPE (arg),
+                       lvalue, TREE_OPERAND (arg, 1));
        }
       return unary_complex_lvalue
-       (code, build (COMPOUND_EXPR, TREE_TYPE (lvalue), arg, lvalue));
+       (code, build2 (COMPOUND_EXPR, TREE_TYPE (lvalue), arg, lvalue));
     }
 
   if (code != ADDR_EXPR)
-    return 0;
+    return NULL_TREE;
 
   /* Handle (a = b) used as an "lvalue" for `&'.  */
   if (TREE_CODE (arg) == MODIFY_EXPR
       || TREE_CODE (arg) == INIT_EXPR)
     {
-      tree real_result = build_unary_op (code, TREE_OPERAND (arg, 0), 0);
-      arg = build (COMPOUND_EXPR, TREE_TYPE (real_result), arg, real_result);
-      TREE_NO_UNUSED_WARNING (arg) = 1;
+      tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 0), 0,
+                                            tf_warning_or_error);
+      arg = build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
+                   arg, real_result);
+      TREE_NO_WARNING (arg) = 1;
       return arg;
     }
 
   if (TREE_CODE (TREE_TYPE (arg)) == FUNCTION_TYPE
       || TREE_CODE (TREE_TYPE (arg)) == METHOD_TYPE
-      || TREE_CODE (TREE_TYPE (arg)) == OFFSET_TYPE)
-    {
-      /* The representation of something of type OFFSET_TYPE
-        is really the representation of a pointer to it.
-        Here give the representation its true type.  */
-      tree t;
-
-      my_friendly_assert (TREE_CODE (arg) != SCOPE_REF, 313);
-
-      if (TREE_CODE (arg) != OFFSET_REF)
-       return 0;
-
-      t = TREE_OPERAND (arg, 1);
-
-      /* Check all this code for right semantics.  */  
-      if (TREE_CODE (t) == FUNCTION_DECL)
-       {
-         if (DECL_DESTRUCTOR_P (t))
-           error ("taking address of destructor");
-         return build_unary_op (ADDR_EXPR, t, 0);
-       }
-      if (TREE_CODE (t) == VAR_DECL)
-       return build_unary_op (ADDR_EXPR, t, 0);
-      else
-       {
-         tree type;
-
-         if (TREE_OPERAND (arg, 0)
-             && ! is_dummy_object (TREE_OPERAND (arg, 0))
-             && TREE_CODE (t) != FIELD_DECL)
-           {
-             error ("taking address of bound pointer-to-member expression");
-             return error_mark_node;
-           }
-         if (!PTRMEM_OK_P (arg))
-           {
-             /* This cannot form a pointer to method, so we must
-                resolve the offset ref, and take the address of the
-                result.  For instance,
-                       &(C::m)       */
-             arg = resolve_offset_ref (arg);
-
-             return build_unary_op (code, arg, 0);
-           }
-         
-         if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
-           {
-             error ("cannot create pointer to reference member `%D'", t);
-             return error_mark_node;
-           }
-
-         type = build_offset_type (DECL_FIELD_CONTEXT (t), TREE_TYPE (t));
-         type = build_pointer_type (type);
-
-         t = make_ptrmem_cst (type, TREE_OPERAND (arg, 1));
-         return t;
-       }
-    }
+      || TREE_CODE (arg) == OFFSET_REF)
+    return NULL_TREE;
 
-  
   /* We permit compiler to make function calls returning
      objects of aggregate type look like lvalues.  */
   {
@@ -4795,7 +4816,7 @@ unary_complex_lvalue (code, arg)
     if (TREE_CODE (targ) == SAVE_EXPR)
       targ = TREE_OPERAND (targ, 0);
 
-    if (TREE_CODE (targ) == CALL_EXPR && IS_AGGR_TYPE (TREE_TYPE (targ)))
+    if (TREE_CODE (targ) == CALL_EXPR && MAYBE_CLASS_TYPE_P (TREE_TYPE (targ)))
       {
        if (TREE_CODE (arg) == SAVE_EXPR)
          targ = arg;
@@ -4805,25 +4826,24 @@ unary_complex_lvalue (code, arg)
       }
 
     if (TREE_CODE (arg) == SAVE_EXPR && TREE_CODE (targ) == INDIRECT_REF)
-      return build (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
+      return build3 (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
                     TREE_OPERAND (targ, 0), current_function_decl, NULL);
   }
 
   /* Don't let anything else be handled specially.  */
-  return 0;
+  return NULL_TREE;
 }
 \f
 /* Mark EXP saying that we need to be able to take the
    address of it; it should not be allocated in a register.
-   Value is 1 if successful.
+   Value is true if successful.
 
    C++: we do not allow `current_class_ptr' to be addressable.  */
 
-int
-mark_addressable (exp)
-     tree exp;
+bool
+cxx_mark_addressable (tree exp)
 {
-  register tree x = exp;
+  tree x = exp;
 
   while (1)
     switch (TREE_CODE (x))
@@ -4839,162 +4859,506 @@ mark_addressable (exp)
       case PARM_DECL:
        if (x == current_class_ptr)
          {
-            error ("cannot take the address of `this', which is an rvalue expression");
-           TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later */
-           return 1;
+           error ("cannot take the address of %<this%>, which is an rvalue expression");
+           TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later */
+           return true;
          }
-       /* FALLTHRU */
+       /* Fall through.  */
 
       case VAR_DECL:
        /* Caller should not be trying to mark initialized
           constant fields addressable.  */
-       my_friendly_assert (DECL_LANG_SPECIFIC (x) == 0
-                           || DECL_IN_AGGR_P (x) == 0
-                           || TREE_STATIC (x)
-                           || DECL_EXTERNAL (x), 314);
-       /* FALLTHRU */
+       gcc_assert (DECL_LANG_SPECIFIC (x) == 0
+                   || DECL_IN_AGGR_P (x) == 0
+                   || TREE_STATIC (x)
+                   || DECL_EXTERNAL (x));
+       /* Fall through.  */
 
       case CONST_DECL:
       case RESULT_DECL:
        if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
-           && !DECL_ARTIFICIAL (x) && extra_warnings)
-         warning ("address requested for `%D', which is declared `register'",
-                     x);
+           && !DECL_ARTIFICIAL (x))
+         {
+           if (TREE_CODE (x) == VAR_DECL && DECL_HARD_REGISTER (x))
+             {
+               error
+                 ("address of explicit register variable %qD requested", x);
+               return false;
+             }
+           else if (extra_warnings)
+             warning
+               (OPT_Wextra, "address requested for %qD, which is declared %<register%>", x);
+         }
        TREE_ADDRESSABLE (x) = 1;
-       put_var_into_stack (x);
-       return 1;
+       return true;
 
       case FUNCTION_DECL:
        TREE_ADDRESSABLE (x) = 1;
-       TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (x)) = 1;
-       return 1;
+       return true;
 
       case CONSTRUCTOR:
        TREE_ADDRESSABLE (x) = 1;
-       return 1;
+       return true;
 
       case TARGET_EXPR:
        TREE_ADDRESSABLE (x) = 1;
-       mark_addressable (TREE_OPERAND (x, 0));
-       return 1;
+       cxx_mark_addressable (TREE_OPERAND (x, 0));
+       return true;
 
       default:
-       return 1;
+       return true;
     }
 }
 \f
 /* Build and return a conditional expression IFEXP ? OP1 : OP2.  */
 
 tree
-build_x_conditional_expr (ifexp, op1, op2)
-     tree ifexp, op1, op2;
+build_x_conditional_expr (tree ifexp, tree op1, tree op2, 
+                          tsubst_flags_t complain)
 {
-  if (processing_template_decl)
-    return build_min_nt (COND_EXPR, ifexp, op1, op2);
+  tree orig_ifexp = ifexp;
+  tree orig_op1 = op1;
+  tree orig_op2 = op2;
+  tree expr;
 
-  return build_conditional_expr (ifexp, op1, op2);
+  if (processing_template_decl)
+    {
+      /* The standard says that the expression is type-dependent if
+        IFEXP is type-dependent, even though the eventual type of the
+        expression doesn't dependent on IFEXP.  */
+      if (type_dependent_expression_p (ifexp)
+         /* As a GNU extension, the middle operand may be omitted.  */
+         || (op1 && type_dependent_expression_p (op1))
+         || type_dependent_expression_p (op2))
+       return build_min_nt (COND_EXPR, ifexp, op1, op2);
+      ifexp = build_non_dependent_expr (ifexp);
+      if (op1)
+       op1 = build_non_dependent_expr (op1);
+      op2 = build_non_dependent_expr (op2);
+    }
+
+  expr = build_conditional_expr (ifexp, op1, op2, complain);
+  if (processing_template_decl && expr != error_mark_node)
+    return build_min_non_dep (COND_EXPR, expr,
+                             orig_ifexp, orig_op1, orig_op2);
+  return expr;
 }
 \f
-/* Handle overloading of the ',' operator when needed.  Otherwise,
-   this function just builds an expression list.  */
+/* Given a list of expressions, return a compound expression
+   that performs them all and returns the value of the last of them.  */
+
+tree build_x_compound_expr_from_list (tree list, const char *msg)
+{
+  tree expr = TREE_VALUE (list);
+
+  if (TREE_CHAIN (list))
+    {
+      if (msg)
+       permerror (input_location, "%s expression list treated as compound expression", msg);
+
+      for (list = TREE_CHAIN (list); list; list = TREE_CHAIN (list))
+       expr = build_x_compound_expr (expr, TREE_VALUE (list), 
+                                      tf_warning_or_error);
+    }
+
+  return expr;
+}
+
+/* Handle overloading of the ',' operator when needed.  */
 
 tree
-build_x_compound_expr (list)
-     tree list;
+build_x_compound_expr (tree op1, tree op2, tsubst_flags_t complain)
 {
-  tree rest = TREE_CHAIN (list);
   tree result;
+  tree orig_op1 = op1;
+  tree orig_op2 = op2;
 
   if (processing_template_decl)
-    return build_min_nt (COMPOUND_EXPR, list, NULL_TREE);
+    {
+      if (type_dependent_expression_p (op1)
+         || type_dependent_expression_p (op2))
+       return build_min_nt (COMPOUND_EXPR, op1, op2);
+      op1 = build_non_dependent_expr (op1);
+      op2 = build_non_dependent_expr (op2);
+    }
+
+  result = build_new_op (COMPOUND_EXPR, LOOKUP_NORMAL, op1, op2, NULL_TREE,
+                        /*overloaded_p=*/NULL, complain);
+  if (!result)
+    result = cp_build_compound_expr (op1, op2, complain);
+
+  if (processing_template_decl && result != error_mark_node)
+    return build_min_non_dep (COMPOUND_EXPR, result, orig_op1, orig_op2);
+
+  return result;
+}
+
+/* Like cp_build_compound_expr, but for the c-common bits.  */
+
+tree
+build_compound_expr (tree lhs, tree rhs)
+{
+  return cp_build_compound_expr (lhs, rhs, tf_warning_or_error);
+}
+
+/* Build a compound expression.  */
+
+tree
+cp_build_compound_expr (tree lhs, tree rhs, tsubst_flags_t complain)
+{
+  lhs = convert_to_void (lhs, "left-hand operand of comma", complain);
+
+  if (lhs == error_mark_node || rhs == error_mark_node)
+    return error_mark_node;
+
+  if (TREE_CODE (rhs) == TARGET_EXPR)
+    {
+      /* If the rhs is a TARGET_EXPR, then build the compound
+        expression inside the target_expr's initializer. This
+        helps the compiler to eliminate unnecessary temporaries.  */
+      tree init = TREE_OPERAND (rhs, 1);
+
+      init = build2 (COMPOUND_EXPR, TREE_TYPE (init), lhs, init);
+      TREE_OPERAND (rhs, 1) = init;
+
+      return rhs;
+    }
+
+  return build2 (COMPOUND_EXPR, TREE_TYPE (rhs), lhs, rhs);
+}
+
+/* Issue a diagnostic message if casting from SRC_TYPE to DEST_TYPE
+   casts away constness.  CAST gives the type of cast.  */
+
+static void
+check_for_casting_away_constness (tree src_type, tree dest_type,
+                                 enum tree_code cast)
+{
+  /* C-style casts are allowed to cast away constness.  With
+     WARN_CAST_QUAL, we still want to issue a warning.  */
+  if (cast == CAST_EXPR && !warn_cast_qual)
+      return;
+  
+  if (casts_away_constness (src_type, dest_type))
+    switch (cast)
+      {
+      case CAST_EXPR:
+       warning (OPT_Wcast_qual, 
+                 "cast from type %qT to type %qT casts away constness",
+                src_type, dest_type);
+       return;
+       
+      case STATIC_CAST_EXPR:
+       error ("static_cast from type %qT to type %qT casts away constness",
+              src_type, dest_type);
+       return;
+       
+      case REINTERPRET_CAST_EXPR:
+       error ("reinterpret_cast from type %qT to type %qT casts away constness",
+              src_type, dest_type);
+       return;
+      default:
+       gcc_unreachable();
+      }
+}
+
+/* Convert EXPR (an expression with pointer-to-member type) to TYPE
+   (another pointer-to-member type in the same hierarchy) and return
+   the converted expression.  If ALLOW_INVERSE_P is permitted, a
+   pointer-to-derived may be converted to pointer-to-base; otherwise,
+   only the other direction is permitted.  If C_CAST_P is true, this
+   conversion is taking place as part of a C-style cast.  */
+
+tree
+convert_ptrmem (tree type, tree expr, bool allow_inverse_p,
+               bool c_cast_p)
+{
+  if (TYPE_PTRMEM_P (type))
+    {
+      tree delta;
+
+      if (TREE_CODE (expr) == PTRMEM_CST)
+       expr = cplus_expand_constant (expr);
+      delta = get_delta_difference (TYPE_PTRMEM_CLASS_TYPE (TREE_TYPE (expr)),
+                                   TYPE_PTRMEM_CLASS_TYPE (type),
+                                   allow_inverse_p,
+                                   c_cast_p);
+      if (!integer_zerop (delta))
+       {
+         tree cond, op1, op2;
+
+         cond = cp_build_binary_op (input_location,
+                                    EQ_EXPR,
+                                    expr,
+                                    build_int_cst (TREE_TYPE (expr), -1),
+                                    tf_warning_or_error);
+         op1 = build_nop (ptrdiff_type_node, expr);
+         op2 = cp_build_binary_op (input_location,
+                                   PLUS_EXPR, op1, delta,
+                                   tf_warning_or_error);
+
+         expr = fold_build3 (COND_EXPR, ptrdiff_type_node, cond, op1, op2);
+                        
+       }
+
+      return build_nop (type, expr);
+    }
+  else
+    return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr,
+                            allow_inverse_p, c_cast_p);
+}
+
+/* If EXPR is an INTEGER_CST and ORIG is an arithmetic constant, return
+   a version of EXPR that has TREE_OVERFLOW set if it is set in ORIG.
+   Otherwise, return EXPR unchanged.  */
+
+static tree
+ignore_overflows (tree expr, tree orig)
+{
+  if (TREE_CODE (expr) == INTEGER_CST
+      && CONSTANT_CLASS_P (orig)
+      && TREE_CODE (orig) != STRING_CST
+      && TREE_OVERFLOW (expr) != TREE_OVERFLOW (orig))
+    {
+      if (!TREE_OVERFLOW (orig))
+       /* Ensure constant sharing.  */
+       expr = build_int_cst_wide (TREE_TYPE (expr),
+                                  TREE_INT_CST_LOW (expr),
+                                  TREE_INT_CST_HIGH (expr));
+      else
+       {
+         /* Avoid clobbering a shared constant.  */
+         expr = copy_node (expr);
+         TREE_OVERFLOW (expr) = TREE_OVERFLOW (orig);
+       }
+    }
+  return expr;
+}
+
+/* Perform a static_cast from EXPR to TYPE.  When C_CAST_P is true,
+   this static_cast is being attempted as one of the possible casts
+   allowed by a C-style cast.  (In that case, accessibility of base
+   classes is not considered, and it is OK to cast away
+   constness.)  Return the result of the cast.  *VALID_P is set to
+   indicate whether or not the cast was valid.  */
+
+static tree
+build_static_cast_1 (tree type, tree expr, bool c_cast_p,
+                    bool *valid_p, tsubst_flags_t complain)
+{
+  tree intype;
+  tree result;
+  tree orig;
+
+  /* Assume the cast is valid.  */
+  *valid_p = true;
+
+  intype = TREE_TYPE (expr);
+
+  /* Save casted types in the function's used types hash table.  */
+  used_types_insert (type);
+
+  /* [expr.static.cast]
+
+     An lvalue of type "cv1 B", where B is a class type, can be cast
+     to type "reference to cv2 D", where D is a class derived (clause
+     _class.derived_) from B, if a valid standard conversion from
+     "pointer to D" to "pointer to B" exists (_conv.ptr_), cv2 is the
+     same cv-qualification as, or greater cv-qualification than, cv1,
+     and B is not a virtual base class of D.  */
+  /* We check this case before checking the validity of "TYPE t =
+     EXPR;" below because for this case:
+
+       struct B {};
+       struct D : public B { D(const B&); };
+       extern B& b;
+       void f() { static_cast<const D&>(b); }
+
+     we want to avoid constructing a new D.  The standard is not
+     completely clear about this issue, but our interpretation is
+     consistent with other compilers.  */
+  if (TREE_CODE (type) == REFERENCE_TYPE
+      && CLASS_TYPE_P (TREE_TYPE (type))
+      && CLASS_TYPE_P (intype)
+      && real_lvalue_p (expr)
+      && DERIVED_FROM_P (intype, TREE_TYPE (type))
+      && can_convert (build_pointer_type (TYPE_MAIN_VARIANT (intype)),
+                     build_pointer_type (TYPE_MAIN_VARIANT
+                                         (TREE_TYPE (type))))
+      && (c_cast_p
+         || at_least_as_qualified_p (TREE_TYPE (type), intype)))
+    {
+      tree base;
+
+      /* There is a standard conversion from "D*" to "B*" even if "B"
+        is ambiguous or inaccessible.  If this is really a
+        static_cast, then we check both for inaccessibility and
+        ambiguity.  However, if this is a static_cast being performed
+        because the user wrote a C-style cast, then accessibility is
+        not considered.  */
+      base = lookup_base (TREE_TYPE (type), intype,
+                         c_cast_p ? ba_unique : ba_check,
+                         NULL);
+
+      /* Convert from "B*" to "D*".  This function will check that "B"
+        is not a virtual base of "D".  */
+      expr = build_base_path (MINUS_EXPR, build_address (expr),
+                             base, /*nonnull=*/false);
+      /* Convert the pointer to a reference -- but then remember that
+        there are no expressions with reference type in C++.  */
+      return convert_from_reference (build_nop (type, expr));
+    }
+
+  orig = expr;
+
+  /* [expr.static.cast]
+
+     An expression e can be explicitly converted to a type T using a
+     static_cast of the form static_cast<T>(e) if the declaration T
+     t(e);" is well-formed, for some invented temporary variable
+     t.  */
+  result = perform_direct_initialization_if_possible (type, expr,
+                                                     c_cast_p, complain);
+  if (result)
+    {
+      result = convert_from_reference (result);
+
+      /* Ignore any integer overflow caused by the cast.  */
+      result = ignore_overflows (result, orig);
+
+      /* [expr.static.cast]
+
+        If T is a reference type, the result is an lvalue; otherwise,
+        the result is an rvalue.  */
+      if (TREE_CODE (type) != REFERENCE_TYPE)
+       result = rvalue (result);
+      return result;
+    }
+
+  /* [expr.static.cast]
 
-  if (rest == NULL_TREE)
-    return build_compound_expr (list);
+     Any expression can be explicitly converted to type cv void.  */
+  if (TREE_CODE (type) == VOID_TYPE)
+    return convert_to_void (expr, /*implicit=*/NULL, complain);
 
-  result = build_opfncall (COMPOUND_EXPR, LOOKUP_NORMAL,
-                          TREE_VALUE (list), TREE_VALUE (rest), NULL_TREE);
-  if (result)
-    return build_x_compound_expr (tree_cons (NULL_TREE, result,
-                                                 TREE_CHAIN (rest)));
-
-  if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)))
-    {
-      /* FIXME: This test should be in the implicit cast to void of the LHS. */
-      /* the left-hand operand of a comma expression is like an expression
-         statement: we should warn if it doesn't have any side-effects,
-         unless it was explicitly cast to (void).  */
-      if ((extra_warnings || warn_unused_value)
-           && !(TREE_CODE (TREE_VALUE(list)) == CONVERT_EXPR
-                && VOID_TYPE_P (TREE_TYPE (TREE_VALUE(list)))))
-        warning("left-hand operand of comma expression has no effect");
-    }
-#if 0 /* this requires a gcc backend patch to export warn_if_unused_value */
-  else if (warn_unused_value)
-    warn_if_unused_value (TREE_VALUE(list));
-#endif
+  /* [expr.static.cast]
 
-  return build_compound_expr
-    (tree_cons (NULL_TREE, TREE_VALUE (list),
-                    build_tree_list (NULL_TREE,
-                                     build_x_compound_expr (rest))));
-}
+     The inverse of any standard conversion sequence (clause _conv_),
+     other than the lvalue-to-rvalue (_conv.lval_), array-to-pointer
+     (_conv.array_), function-to-pointer (_conv.func_), and boolean
+     (_conv.bool_) conversions, can be performed explicitly using
+     static_cast subject to the restriction that the explicit
+     conversion does not cast away constness (_expr.const.cast_), and
+     the following additional rules for specific cases:  */
+  /* For reference, the conversions not excluded are: integral
+     promotions, floating point promotion, integral conversions,
+     floating point conversions, floating-integral conversions,
+     pointer conversions, and pointer to member conversions.  */
+  /* DR 128
+
+     A value of integral _or enumeration_ type can be explicitly
+     converted to an enumeration type.  */
+  /* The effect of all that is that any conversion between any two
+     types which are integral, floating, or enumeration types can be
+     performed.  */
+  if ((INTEGRAL_TYPE_P (type) || SCALAR_FLOAT_TYPE_P (type))
+      && (INTEGRAL_TYPE_P (intype) || SCALAR_FLOAT_TYPE_P (intype)))
+    {
+      expr = ocp_convert (type, expr, CONV_C_CAST, LOOKUP_NORMAL);
 
-/* Given a list of expressions, return a compound expression
-   that performs them all and returns the value of the last of them.  */
+      /* Ignore any integer overflow caused by the cast.  */
+      expr = ignore_overflows (expr, orig);
+      return expr;
+    }
 
-tree
-build_compound_expr (list)
-     tree list;
-{
-  register tree rest;
-  tree first;
+  if (TYPE_PTR_P (type) && TYPE_PTR_P (intype)
+      && CLASS_TYPE_P (TREE_TYPE (type))
+      && CLASS_TYPE_P (TREE_TYPE (intype))
+      && can_convert (build_pointer_type (TYPE_MAIN_VARIANT
+                                         (TREE_TYPE (intype))),
+                     build_pointer_type (TYPE_MAIN_VARIANT
+                                         (TREE_TYPE (type)))))
+    {
+      tree base;
 
-  TREE_VALUE (list) = decl_constant_value (TREE_VALUE (list));
+      if (!c_cast_p)
+       check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR);
+      base = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
+                         c_cast_p ? ba_unique : ba_check,
+                         NULL);
+      return build_base_path (MINUS_EXPR, expr, base, /*nonnull=*/false);
+    }
 
-  if (TREE_CHAIN (list) == 0)
+  if ((TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype))
+      || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
     {
-      /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
-        Strip such NOP_EXPRs, since LIST is used in non-lvalue context.  */
-      if (TREE_CODE (list) == NOP_EXPR
-         && TREE_TYPE (list) == TREE_TYPE (TREE_OPERAND (list, 0)))
-       list = TREE_OPERAND (list, 0);
-       
-      return TREE_VALUE (list);
+      tree c1;
+      tree c2;
+      tree t1;
+      tree t2;
+
+      c1 = TYPE_PTRMEM_CLASS_TYPE (intype);
+      c2 = TYPE_PTRMEM_CLASS_TYPE (type);
+
+      if (TYPE_PTRMEM_P (type))
+       {
+         t1 = (build_ptrmem_type
+               (c1,
+                TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (intype))));
+         t2 = (build_ptrmem_type
+               (c2,
+                TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
+       }
+      else
+       {
+         t1 = intype;
+         t2 = type;
+       }
+      if (can_convert (t1, t2) || can_convert (t2, t1))
+       {
+         if (!c_cast_p)
+           check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR);
+         return convert_ptrmem (type, expr, /*allow_inverse_p=*/1,
+                                c_cast_p);
+       }
     }
 
-  first = TREE_VALUE (list);
-  first = convert_to_void (first, "left-hand operand of comma");
-  if (first == error_mark_node)
-    return error_mark_node;
-  
-  rest = build_compound_expr (TREE_CHAIN (list));
-  if (rest == error_mark_node)
-    return error_mark_node;
+  /* [expr.static.cast]
 
-  /* When pedantic, a compound expression cannot be a constant expression.  */
-  if (! TREE_SIDE_EFFECTS (first) && ! pedantic)
-    return rest;
+     An rvalue of type "pointer to cv void" can be explicitly
+     converted to a pointer to object type.  A value of type pointer
+     to object converted to "pointer to cv void" and back to the
+     original pointer type will have its original value.  */
+  if (TREE_CODE (intype) == POINTER_TYPE
+      && VOID_TYPE_P (TREE_TYPE (intype))
+      && TYPE_PTROB_P (type))
+    {
+      if (!c_cast_p)
+       check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR);
+      return build_nop (type, expr);
+    }
 
-  return build (COMPOUND_EXPR, TREE_TYPE (rest), first, rest);
+  *valid_p = false;
+  return error_mark_node;
 }
 
+/* Return an expression representing static_cast<TYPE>(EXPR).  */
+
 tree
-build_static_cast (type, expr)
-   tree type, expr;
+build_static_cast (tree type, tree expr, tsubst_flags_t complain)
 {
-  tree intype;
-  int ok;
+  tree result;
+  bool valid_p;
 
   if (type == error_mark_node || expr == error_mark_node)
     return error_mark_node;
 
-  if (TREE_CODE (expr) == OFFSET_REF)
-    expr = resolve_offset_ref (expr);
-
   if (processing_template_decl)
     {
-      tree t = build_min (STATIC_CAST_EXPR, type, expr); 
-      return t;
+      expr = build_min (STATIC_CAST_EXPR, type, expr);
+      /* We don't know if it will or will not have side effects.  */
+      TREE_SIDE_EFFECTS (expr) = 1;
+      return convert_from_reference (expr);
     }
 
   /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
@@ -5004,271 +5368,414 @@ build_static_cast (type, expr)
       && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
     expr = TREE_OPERAND (expr, 0);
 
-  if (TREE_CODE (type) == VOID_TYPE)
-    {
-      expr = convert_to_void (expr, /*implicit=*/NULL);
-      return expr;
-    }
-
-  if (TREE_CODE (type) == REFERENCE_TYPE)
-    return (convert_from_reference
-           (convert_to_reference (type, expr, CONV_STATIC|CONV_IMPLICIT,
-                                  LOOKUP_COMPLAIN, NULL_TREE)));
-
-  if (IS_AGGR_TYPE (type))
-    return build_cplus_new (type, (build_method_call
-                                  (NULL_TREE, complete_ctor_identifier, 
-                                   build_tree_list (NULL_TREE, expr),
-                                   TYPE_BINFO (type), LOOKUP_NORMAL)));
-  
-  intype = TREE_TYPE (expr);
-
-  /* FIXME handle casting to array type.  */
-
-  ok = 0;
-  if (IS_AGGR_TYPE (intype)
-      ? can_convert_arg (type, intype, expr)
-      : can_convert_arg (strip_all_pointer_quals (type),
-                         strip_all_pointer_quals (intype), expr))
-    /* This is a standard conversion. */
-    ok = 1;
-  else if (TYPE_PTROB_P (type) && TYPE_PTROB_P (intype))
-    {
-      /* They're pointers to objects. They must be aggregates that
-         are related non-virtually. */
-      base_kind kind;
-      
-      if (IS_AGGR_TYPE (TREE_TYPE (type)) && IS_AGGR_TYPE (TREE_TYPE (intype))
-         && lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
-                         ba_ignore | ba_quiet, &kind)
-         && kind != bk_via_virtual)
-       ok = 1;
-    }
-  else if (TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype))
-    {
-      /* They're pointers to members. The pointed to objects must be
-        the same (ignoring CV qualifiers), and the containing classes
-        must be related non-virtually. */
-      base_kind kind;
-      
-      if (same_type_p
-         (strip_all_pointer_quals (TREE_TYPE (TREE_TYPE (type))),
-          strip_all_pointer_quals (TREE_TYPE (TREE_TYPE (intype))))
-         && (lookup_base (TYPE_OFFSET_BASETYPE (TREE_TYPE (intype)),
-                          TYPE_OFFSET_BASETYPE (TREE_TYPE (type)),
-                          ba_ignore | ba_quiet, &kind))
-         && kind != bk_via_virtual)
-       ok = 1;
-    }
-  else if (TREE_CODE (intype) != BOOLEAN_TYPE
-          && TREE_CODE (type) != ARRAY_TYPE
-          && TREE_CODE (type) != FUNCTION_TYPE
-          && can_convert (intype, strip_all_pointer_quals (type)))
-    ok = 1;
-  else if (TREE_CODE (intype) == ENUMERAL_TYPE
-           && TREE_CODE (type) == ENUMERAL_TYPE)
-    /* DR 128: "A value of integral _or enumeration_ type can be explicitly
-       converted to an enumeration type."
-       The integral to enumeration will be accepted by the previous clause.
-       We need to explicitly check for enumeration to enumeration.  */
-    ok = 1;
-
-  /* [expr.static.cast]
-
-     The static_cast operator shall not be used to cast away
-     constness.  */
-  if (ok && casts_away_constness (intype, type))
-    {
-      error ("static_cast from type `%T' to type `%T' casts away constness",
-               intype, type);
-      return error_mark_node;
-    }
-
-  if (ok)
-    return build_c_cast (type, expr);
+  result = build_static_cast_1 (type, expr, /*c_cast_p=*/false, &valid_p,
+                                complain);
+  if (valid_p)
+    return result;
 
-  error ("invalid static_cast from type `%T' to type `%T'", intype, type);
+  if (complain & tf_error)
+    error ("invalid static_cast from type %qT to type %qT",
+           TREE_TYPE (expr), type);
   return error_mark_node;
 }
 
+/* EXPR is an expression with member function or pointer-to-member
+   function type.  TYPE is a pointer type.  Converting EXPR to TYPE is
+   not permitted by ISO C++, but we accept it in some modes.  If we
+   are not in one of those modes, issue a diagnostic.  Return the
+   converted expression.  */
+
 tree
-build_reinterpret_cast (type, expr)
-   tree type, expr;
+convert_member_func_to_ptr (tree type, tree expr)
 {
   tree intype;
+  tree decl;
 
-  if (type == error_mark_node || expr == error_mark_node)
-    return error_mark_node;
+  intype = TREE_TYPE (expr);
+  gcc_assert (TYPE_PTRMEMFUNC_P (intype)
+             || TREE_CODE (intype) == METHOD_TYPE);
 
-  if (TREE_CODE (expr) == OFFSET_REF)
-    expr = resolve_offset_ref (expr);
+  if (pedantic || warn_pmf2ptr)
+    pedwarn (input_location, pedantic ? OPT_pedantic : OPT_Wpmf_conversions,
+            "converting from %qT to %qT", intype, type);
 
-  if (processing_template_decl)
+  if (TREE_CODE (intype) == METHOD_TYPE)
+    expr = build_addr_func (expr);
+  else if (TREE_CODE (expr) == PTRMEM_CST)
+    expr = build_address (PTRMEM_CST_MEMBER (expr));
+  else
     {
-      tree t = build_min (REINTERPRET_CAST_EXPR, type, expr);
-      return t;
+      decl = maybe_dummy_object (TYPE_PTRMEM_CLASS_TYPE (intype), 0);
+      decl = build_address (decl);
+      expr = get_member_function_from_ptrfunc (&decl, expr);
     }
 
-  if (TREE_CODE (type) != REFERENCE_TYPE)
-    {
-      expr = decay_conversion (expr);
+  return build_nop (type, expr);
+}
 
-      /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
-        Strip such NOP_EXPRs if VALUE is being used in non-lvalue context.  */
-      if (TREE_CODE (expr) == NOP_EXPR
-         && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
-       expr = TREE_OPERAND (expr, 0);
-    }
+/* Return a representation for a reinterpret_cast from EXPR to TYPE.
+   If C_CAST_P is true, this reinterpret cast is being done as part of
+   a C-style cast.  If VALID_P is non-NULL, *VALID_P is set to
+   indicate whether or not reinterpret_cast was valid.  */
+
+static tree
+build_reinterpret_cast_1 (tree type, tree expr, bool c_cast_p,
+                         bool *valid_p, tsubst_flags_t complain)
+{
+  tree intype;
+
+  /* Assume the cast is invalid.  */
+  if (valid_p)
+    *valid_p = true;
+
+  if (type == error_mark_node || error_operand_p (expr))
+    return error_mark_node;
 
   intype = TREE_TYPE (expr);
 
+  /* Save casted types in the function's used types hash table.  */
+  used_types_insert (type);
+
+  /* [expr.reinterpret.cast]
+     An lvalue expression of type T1 can be cast to the type
+     "reference to T2" if an expression of type "pointer to T1" can be
+     explicitly converted to the type "pointer to T2" using a
+     reinterpret_cast.  */
   if (TREE_CODE (type) == REFERENCE_TYPE)
     {
       if (! real_lvalue_p (expr))
        {
-         error ("invalid reinterpret_cast of an rvalue expression of type `%T' to type `%T'", intype, type);
+          if (complain & tf_error)
+            error ("invalid cast of an rvalue expression of type "
+                   "%qT to type %qT",
+                   intype, type);
          return error_mark_node;
        }
-      expr = build_unary_op (ADDR_EXPR, expr, 0);
+
+      /* Warn about a reinterpret_cast from "A*" to "B&" if "A" and
+        "B" are related class types; the reinterpret_cast does not
+        adjust the pointer.  */
+      if (TYPE_PTR_P (intype)
+          && (complain & tf_warning)
+         && (comptypes (TREE_TYPE (intype), TREE_TYPE (type),
+                        COMPARE_BASE | COMPARE_DERIVED)))
+       warning (0, "casting %qT to %qT does not dereference pointer",
+                intype, type);
+
+      expr = cp_build_unary_op (ADDR_EXPR, expr, 0, complain);
+
+      if (warn_strict_aliasing > 2)
+       strict_aliasing_warning (TREE_TYPE (expr), type, expr);
+
       if (expr != error_mark_node)
-       expr = build_reinterpret_cast
-         (build_pointer_type (TREE_TYPE (type)), expr);
+       expr = build_reinterpret_cast_1
+         (build_pointer_type (TREE_TYPE (type)), expr, c_cast_p,
+          valid_p, complain);
       if (expr != error_mark_node)
-       expr = build_indirect_ref (expr, 0);
+       /* cp_build_indirect_ref isn't right for rvalue refs.  */
+       expr = convert_from_reference (fold_convert (type, expr));
       return expr;
     }
-  else if (same_type_ignoring_top_level_qualifiers_p (intype, type))
-    return build_static_cast (type, expr);
 
-  if (TYPE_PTR_P (type) && (TREE_CODE (intype) == INTEGER_TYPE
-                           || TREE_CODE (intype) == ENUMERAL_TYPE))
-    /* OK */;
-  else if (TREE_CODE (type) == INTEGER_TYPE && TYPE_PTR_P (intype))
+  /* As a G++ extension, we consider conversions from member
+     functions, and pointers to member functions to
+     pointer-to-function and pointer-to-void types.  If
+     -Wno-pmf-conversions has not been specified,
+     convert_member_func_to_ptr will issue an error message.  */
+  if ((TYPE_PTRMEMFUNC_P (intype)
+       || TREE_CODE (intype) == METHOD_TYPE)
+      && TYPE_PTR_P (type)
+      && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
+         || VOID_TYPE_P (TREE_TYPE (type))))
+    return convert_member_func_to_ptr (type, expr);
+
+  /* If the cast is not to a reference type, the lvalue-to-rvalue,
+     array-to-pointer, and function-to-pointer conversions are
+     performed.  */
+  expr = decay_conversion (expr);
+
+  /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
+     Strip such NOP_EXPRs if VALUE is being used in non-lvalue context.  */
+  if (TREE_CODE (expr) == NOP_EXPR
+      && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
+    expr = TREE_OPERAND (expr, 0);
+
+  if (error_operand_p (expr))
+    return error_mark_node;
+
+  intype = TREE_TYPE (expr);
+
+  /* [expr.reinterpret.cast]
+     A pointer can be converted to any integral type large enough to
+     hold it.  */
+  if (CP_INTEGRAL_TYPE_P (type) && TYPE_PTR_P (intype))
     {
       if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
-       pedwarn ("reinterpret_cast from `%T' to `%T' loses precision",
-                   intype, type);
+        {
+          if (complain & tf_error)
+            permerror (input_location, "cast from %qT to %qT loses precision",
+                       intype, type);
+          else
+            return error_mark_node;
+        }
     }
+  /* [expr.reinterpret.cast]
+     A value of integral or enumeration type can be explicitly
+     converted to a pointer.  */
+  else if (TYPE_PTR_P (type) && INTEGRAL_OR_ENUMERATION_TYPE_P (intype))
+    /* OK */
+    ;
   else if ((TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
           || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
-    {
-      expr = decl_constant_value (expr);
-      return fold (build1 (NOP_EXPR, type, expr));
-    }
+    return fold_if_not_in_template (build_nop (type, expr));
   else if ((TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype))
           || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
     {
-      if (! comp_ptr_ttypes_reinterpret (TREE_TYPE (type), TREE_TYPE (intype)))
-       pedwarn ("reinterpret_cast from `%T' to `%T' casts away const (or volatile)",
-                   intype, type);
-
-      expr = decl_constant_value (expr);
-      return fold (build1 (NOP_EXPR, type, expr));
+      tree sexpr = expr;
+
+      if (!c_cast_p)
+       check_for_casting_away_constness (intype, type, REINTERPRET_CAST_EXPR);
+      /* Warn about possible alignment problems.  */
+      if (STRICT_ALIGNMENT && warn_cast_align
+          && (complain & tf_warning)
+         && !VOID_TYPE_P (type)
+         && TREE_CODE (TREE_TYPE (intype)) != FUNCTION_TYPE
+         && COMPLETE_TYPE_P (TREE_TYPE (type))
+         && COMPLETE_TYPE_P (TREE_TYPE (intype))
+         && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (intype)))
+       warning (OPT_Wcast_align, "cast from %qT to %qT "
+                 "increases required alignment of target type", intype, type);
+
+      /* We need to strip nops here, because the front end likes to
+        create (int *)&a for array-to-pointer decay, instead of &a[0].  */
+      STRIP_NOPS (sexpr);
+      if (warn_strict_aliasing <= 2)
+       strict_aliasing_warning (intype, type, sexpr);
+
+      return fold_if_not_in_template (build_nop (type, expr));
     }
   else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
           || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
     {
-      pedwarn ("ISO C++ forbids casting between pointer-to-function and pointer-to-object");
-      expr = decl_constant_value (expr);
-      return fold (build1 (NOP_EXPR, type, expr));
-    }
+      if (pedantic && (complain & tf_warning))
+       /* Only issue a warning, as we have always supported this
+          where possible, and it is necessary in some cases.  DR 195
+          addresses this issue, but as of 2004/10/26 is still in
+          drafting.  */
+       warning (0, "ISO C++ forbids casting between pointer-to-function and pointer-to-object");
+      return fold_if_not_in_template (build_nop (type, expr));
+    }
+  else if (TREE_CODE (type) == VECTOR_TYPE)
+    return fold_if_not_in_template (convert_to_vector (type, expr));
+  else if (TREE_CODE (intype) == VECTOR_TYPE && INTEGRAL_TYPE_P (type))
+    return fold_if_not_in_template (convert_to_integer (type, expr));
   else
     {
-      error ("invalid reinterpret_cast from type `%T' to type `%T'",
-                intype, type);
+      if (valid_p)
+       *valid_p = false;
+      if (complain & tf_error)
+        error ("invalid cast from type %qT to type %qT", intype, type);
       return error_mark_node;
     }
-      
+
   return cp_convert (type, expr);
 }
 
 tree
-build_const_cast (type, expr)
-   tree type, expr;
+build_reinterpret_cast (tree type, tree expr, tsubst_flags_t complain)
 {
-  tree intype;
-
   if (type == error_mark_node || expr == error_mark_node)
     return error_mark_node;
 
-  if (TREE_CODE (expr) == OFFSET_REF)
-    expr = resolve_offset_ref (expr);
-
   if (processing_template_decl)
     {
-      tree t = build_min (CONST_CAST_EXPR, type, expr);
-      return t;
+      tree t = build_min (REINTERPRET_CAST_EXPR, type, expr);
+
+      if (!TREE_SIDE_EFFECTS (t)
+         && type_dependent_expression_p (expr))
+       /* There might turn out to be side effects inside expr.  */
+       TREE_SIDE_EFFECTS (t) = 1;
+      return convert_from_reference (t);
     }
 
-  if (!POINTER_TYPE_P (type))
-    error ("invalid use of const_cast with type `%T', which is not a pointer, reference, nor a pointer-to-data-member type", type);
-  else if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
-    {
-      error ("invalid use of const_cast with type `%T', which is a pointer or reference to a function type", type);
+  return build_reinterpret_cast_1 (type, expr, /*c_cast_p=*/false,
+                                  /*valid_p=*/NULL, complain);
+}
+
+/* Perform a const_cast from EXPR to TYPE.  If the cast is valid,
+   return an appropriate expression.  Otherwise, return
+   error_mark_node.  If the cast is not valid, and COMPLAIN is true,
+   then a diagnostic will be issued.  If VALID_P is non-NULL, we are
+   performing a C-style cast, its value upon return will indicate
+   whether or not the conversion succeeded.  */
+
+static tree
+build_const_cast_1 (tree dst_type, tree expr, bool complain,
+                   bool *valid_p)
+{
+  tree src_type;
+  tree reference_type;
+
+  /* Callers are responsible for handling error_mark_node as a
+     destination type.  */
+  gcc_assert (dst_type != error_mark_node);
+  /* In a template, callers should be building syntactic
+     representations of casts, not using this machinery.  */
+  gcc_assert (!processing_template_decl);
+
+  /* Assume the conversion is invalid.  */
+  if (valid_p)
+    *valid_p = false;
+
+  if (!POINTER_TYPE_P (dst_type) && !TYPE_PTRMEM_P (dst_type))
+    {
+      if (complain)
+       error ("invalid use of const_cast with type %qT, "
+              "which is not a pointer, "
+              "reference, nor a pointer-to-data-member type", dst_type);
       return error_mark_node;
     }
 
-  if (TREE_CODE (type) != REFERENCE_TYPE)
+  if (TREE_CODE (TREE_TYPE (dst_type)) == FUNCTION_TYPE)
     {
-      expr = decay_conversion (expr);
-
-      /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
-        Strip such NOP_EXPRs if VALUE is being used in non-lvalue context.  */
-      if (TREE_CODE (expr) == NOP_EXPR
-         && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
-       expr = TREE_OPERAND (expr, 0);
+      if (complain)
+       error ("invalid use of const_cast with type %qT, which is a pointer "
+              "or reference to a function type", dst_type);
+      return error_mark_node;
     }
 
-  intype = TREE_TYPE (expr);
-  
-  if (same_type_ignoring_top_level_qualifiers_p (intype, type))
-    return build_static_cast (type, expr);
-  else if (TREE_CODE (type) == REFERENCE_TYPE)
+  /* Save casted types in the function's used types hash table.  */
+  used_types_insert (dst_type);
+
+  src_type = TREE_TYPE (expr);
+  /* Expressions do not really have reference types.  */
+  if (TREE_CODE (src_type) == REFERENCE_TYPE)
+    src_type = TREE_TYPE (src_type);
+
+  /* [expr.const.cast]
+
+     An lvalue of type T1 can be explicitly converted to an lvalue of
+     type T2 using the cast const_cast<T2&> (where T1 and T2 are object
+     types) if a pointer to T1 can be explicitly converted to the type
+     pointer to T2 using a const_cast.  */
+  if (TREE_CODE (dst_type) == REFERENCE_TYPE)
     {
+      reference_type = dst_type;
       if (! real_lvalue_p (expr))
        {
-         error ("invalid const_cast of an rvalue of type `%T' to type `%T'", intype, type);
+         if (complain)
+           error ("invalid const_cast of an rvalue of type %qT to type %qT",
+                  src_type, dst_type);
          return error_mark_node;
        }
+      dst_type = build_pointer_type (TREE_TYPE (dst_type));
+      src_type = build_pointer_type (src_type);
+    }
+  else
+    {
+      reference_type = NULL_TREE;
+      /* If the destination type is not a reference type, the
+        lvalue-to-rvalue, array-to-pointer, and function-to-pointer
+        conversions are performed.  */
+      src_type = type_decays_to (src_type);
+      if (src_type == error_mark_node)
+       return error_mark_node;
+    }
 
-      if (comp_ptr_ttypes_const (TREE_TYPE (type), intype))
+  if ((TYPE_PTR_P (src_type) || TYPE_PTRMEM_P (src_type))
+      && comp_ptr_ttypes_const (dst_type, src_type))
+    {
+      if (valid_p)
+       {
+         *valid_p = true;
+         /* This cast is actually a C-style cast.  Issue a warning if
+            the user is making a potentially unsafe cast.  */
+         check_for_casting_away_constness (src_type, dst_type, CAST_EXPR);
+       }
+      if (reference_type)
        {
-         expr = build_unary_op (ADDR_EXPR, expr, 0);
-         expr = build1 (NOP_EXPR, type, expr);
+         expr = cp_build_unary_op (ADDR_EXPR, expr, 0, 
+                                    complain? tf_warning_or_error : tf_none);
+         expr = build_nop (reference_type, expr);
          return convert_from_reference (expr);
        }
+      else
+       {
+         expr = decay_conversion (expr);
+         /* build_c_cast puts on a NOP_EXPR to make the result not an
+            lvalue.  Strip such NOP_EXPRs if VALUE is being used in
+            non-lvalue context.  */
+         if (TREE_CODE (expr) == NOP_EXPR
+             && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
+           expr = TREE_OPERAND (expr, 0);
+         return build_nop (dst_type, expr);
+       }
     }
-  else if (TREE_CODE (type) == POINTER_TYPE
-          && TREE_CODE (intype) == POINTER_TYPE
-          && comp_ptr_ttypes_const (TREE_TYPE (type), TREE_TYPE (intype)))
-    return cp_convert (type, expr);
 
-  error ("invalid const_cast from type `%T' to type `%T'", intype, type);
+  if (complain)
+    error ("invalid const_cast from type %qT to type %qT",
+          src_type, dst_type);
   return error_mark_node;
 }
 
-/* Build an expression representing a cast to type TYPE of expression EXPR.
+tree
+build_const_cast (tree type, tree expr, tsubst_flags_t complain)
+{
+  if (type == error_mark_node || error_operand_p (expr))
+    return error_mark_node;
+
+  if (processing_template_decl)
+    {
+      tree t = build_min (CONST_CAST_EXPR, type, expr);
+
+      if (!TREE_SIDE_EFFECTS (t)
+         && type_dependent_expression_p (expr))
+       /* There might turn out to be side effects inside expr.  */
+       TREE_SIDE_EFFECTS (t) = 1;
+      return convert_from_reference (t);
+    }
+
+  return build_const_cast_1 (type, expr, complain & tf_error,
+                            /*valid_p=*/NULL);
+}
+
+/* Like cp_build_c_cast, but for the c-common bits.  */
 
-   ALLOW_NONCONVERTING is true if we should allow non-converting constructors
-   when doing the cast.  */
+tree
+build_c_cast (tree type, tree expr)
+{
+  return cp_build_c_cast (type, expr, tf_warning_or_error);
+}
+
+/* Build an expression representing an explicit C-style cast to type
+   TYPE of expression EXPR.  */
 
 tree
-build_c_cast (type, expr)
-     tree type, expr;
+cp_build_c_cast (tree type, tree expr, tsubst_flags_t complain)
 {
-  register tree value = expr;
-  tree otype;
+  tree value = expr;
+  tree result;
+  bool valid_p;
 
-  if (type == error_mark_node || expr == error_mark_node)
+  if (type == error_mark_node || error_operand_p (expr))
     return error_mark_node;
 
   if (processing_template_decl)
     {
       tree t = build_min (CAST_EXPR, type,
                          tree_cons (NULL_TREE, value, NULL_TREE));
-      return t;
+      /* We don't know if it will or will not have side effects.  */
+      TREE_SIDE_EFFECTS (t) = 1;
+      return convert_from_reference (t);
     }
 
+  /* Casts to a (pointer to a) specific ObjC class (or 'id' or
+     'Class') should always be retained, because this information aids
+     in method lookup.  */
+  if (objc_is_object_ptr (type)
+      && objc_is_object_ptr (TREE_TYPE (expr)))
+    return build_nop (type, expr);
+
   /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
      Strip such NOP_EXPRs if VALUE is being used in non-lvalue context.  */
   if (TREE_CODE (type) != REFERENCE_TYPE
@@ -5276,21 +5783,22 @@ build_c_cast (type, expr)
       && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
     value = TREE_OPERAND (value, 0);
 
-  if (TREE_CODE (value) == OFFSET_REF)
-    value = resolve_offset_ref (value);
-
   if (TREE_CODE (type) == ARRAY_TYPE)
     {
       /* Allow casting from T1* to T2[] because Cfront allows it.
         NIHCL uses it. It is not valid ISO C++ however.  */
       if (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE)
        {
-         pedwarn ("ISO C++ forbids casting to an array type `%T'", type);
+          if (complain & tf_error)
+            permerror (input_location, "ISO C++ forbids casting to an array type %qT", type);
+          else
+            return error_mark_node;
          type = build_pointer_type (TREE_TYPE (type));
        }
       else
        {
-         error ("ISO C++ forbids casting to an array type `%T'", type);
+          if (complain & tf_error)
+            error ("ISO C++ forbids casting to an array type %qT", type);
          return error_mark_node;
        }
     }
@@ -5298,116 +5806,63 @@ build_c_cast (type, expr)
   if (TREE_CODE (type) == FUNCTION_TYPE
       || TREE_CODE (type) == METHOD_TYPE)
     {
-      error ("invalid cast to function type `%T'", type);
+      if (complain & tf_error)
+        error ("invalid cast to function type %qT", type);
       return error_mark_node;
     }
 
-  if (TREE_CODE (type) == VOID_TYPE)
-    {
-      /* Conversion to void does not cause any of the normal function to
-       * pointer, array to pointer and lvalue to rvalue decays.  */
-      
-      value = convert_to_void (value, /*implicit=*/NULL);
-      return value;
-    }
-  /* Convert functions and arrays to pointers and
-     convert references to their expanded types,
-     but don't convert any other types.  If, however, we are
-     casting to a class type, there's no reason to do this: the
-     cast will only succeed if there is a converting constructor,
-     and the default conversions will be done at that point.  In
-     fact, doing the default conversion here is actually harmful
-     in cases like this:
-
-     typedef int A[2];
-     struct S { S(const A&); };
-
-     since we don't want the array-to-pointer conversion done.  */
-  if (!IS_AGGR_TYPE (type))
-    {
-      if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
-         || (TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE
-             /* Don't do the default conversion on a ->* expression.  */
-             && ! (TREE_CODE (type) == POINTER_TYPE
-                   && bound_pmf_p (value)))
-         || TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
-         || TREE_CODE (TREE_TYPE (value)) == REFERENCE_TYPE)
-       value = default_conversion (value);
-    }
-  else if (TREE_CODE (TREE_TYPE (value)) == REFERENCE_TYPE)
-    /* However, even for class types, we still need to strip away
-       the reference type, since the call to convert_force below
-       does not expect the input expression to be of reference
-       type.  */
-    value = convert_from_reference (value);
-       
-  otype = TREE_TYPE (value);
-
-  /* Optionally warn about potentially worrisome casts.  */
-
-  if (warn_cast_qual
-      && TREE_CODE (type) == POINTER_TYPE
-      && TREE_CODE (otype) == POINTER_TYPE
-      && !at_least_as_qualified_p (TREE_TYPE (type),
-                                  TREE_TYPE (otype)))
-    warning ("cast from `%T' to `%T' discards qualifiers from pointer target type",
-                otype, type);
-
-  if (TREE_CODE (type) == INTEGER_TYPE
-      && TREE_CODE (otype) == POINTER_TYPE
-      && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
-    warning ("cast from pointer to integer of different size");
-
-  if (TREE_CODE (type) == POINTER_TYPE
-      && TREE_CODE (otype) == INTEGER_TYPE
-      && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
-      /* Don't warn about converting any constant.  */
-      && !TREE_CONSTANT (value))
-    warning ("cast to pointer from integer of different size");
-
-  if (TREE_CODE (type) == REFERENCE_TYPE)
-    value = (convert_from_reference
-            (convert_to_reference (type, value, CONV_C_CAST,
-                                   LOOKUP_COMPLAIN, NULL_TREE)));
-  else
-    {
-      tree ovalue;
-
-      value = decl_constant_value (value);
-
-      ovalue = value;
-      value = convert_force (type, value, CONV_C_CAST);
+  /* A C-style cast can be a const_cast.  */
+  result = build_const_cast_1 (type, value, /*complain=*/false,
+                              &valid_p);
+  if (valid_p)
+    return result;
 
-      /* Ignore any integer overflow caused by the cast.  */
-      if (TREE_CODE (value) == INTEGER_CST)
+  /* Or a static cast.  */
+  result = build_static_cast_1 (type, value, /*c_cast_p=*/true,
+                               &valid_p, complain);
+  /* Or a reinterpret_cast.  */
+  if (!valid_p)
+    result = build_reinterpret_cast_1 (type, value, /*c_cast_p=*/true,
+                                      &valid_p, complain);
+  /* The static_cast or reinterpret_cast may be followed by a
+     const_cast.  */
+  if (valid_p
+      /* A valid cast may result in errors if, for example, a
+        conversion to am ambiguous base class is required.  */
+      && !error_operand_p (result))
+    {
+      tree result_type;
+
+      /* Non-class rvalues always have cv-unqualified type.  */
+      if (!CLASS_TYPE_P (type))
+       type = TYPE_MAIN_VARIANT (type);
+      result_type = TREE_TYPE (result);
+      if (!CLASS_TYPE_P (result_type))
+       result_type = TYPE_MAIN_VARIANT (result_type);
+      /* If the type of RESULT does not match TYPE, perform a
+        const_cast to make it match.  If the static_cast or
+        reinterpret_cast succeeded, we will differ by at most
+        cv-qualification, so the follow-on const_cast is guaranteed
+        to succeed.  */
+      if (!same_type_p (non_reference (type), non_reference (result_type)))
        {
-         TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
-         TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
+         result = build_const_cast_1 (type, result, false, &valid_p);
+         gcc_assert (valid_p);
        }
+      return result;
     }
 
-  /* Warn about possible alignment problems.  Do this here when we will have
-     instantiated any necessary template types.  */
-  if (STRICT_ALIGNMENT && warn_cast_align
-      && TREE_CODE (type) == POINTER_TYPE
-      && TREE_CODE (otype) == POINTER_TYPE
-      && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
-      && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
-      && COMPLETE_TYPE_P (TREE_TYPE (otype))
-      && COMPLETE_TYPE_P (TREE_TYPE (type))
-      && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
-    warning ("cast from `%T' to `%T' increases required alignment of target type",
-                otype, type);
-
-    /* Always produce some operator for an explicit cast,
-       so we can tell (for -pedantic) that the cast is no lvalue.  */
-  if (TREE_CODE (type) != REFERENCE_TYPE && value == expr
-      && real_lvalue_p (value))
-    value = non_lvalue (value);
-
-  return value;
+  return error_mark_node;
 }
 \f
+/* For use from the C common bits.  */
+tree
+build_modify_expr (location_t location ATTRIBUTE_UNUSED,
+                  tree lhs, enum tree_code modifycode, tree rhs)
+{
+  return cp_build_modify_expr (lhs, modifycode, rhs, tf_warning_or_error);
+}
+
 /* Build an assignment expression of lvalue LHS from value RHS.
    MODIFYCODE is the code for a binary operator that we use
    to combine the old value of LHS with RHS to get the new value.
@@ -5416,50 +5871,73 @@ build_c_cast (type, expr)
    C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed.  */
 
 tree
-build_modify_expr (lhs, modifycode, rhs)
-     tree lhs;
-     enum tree_code modifycode;
-     tree rhs;
+cp_build_modify_expr (tree lhs, enum tree_code modifycode, tree rhs,
+                     tsubst_flags_t complain)
 {
-  register tree result;
+  tree result;
   tree newrhs = rhs;
   tree lhstype = TREE_TYPE (lhs);
   tree olhstype = lhstype;
-  tree olhs = lhs;
+  bool plain_assign = (modifycode == NOP_EXPR);
 
   /* Avoid duplicate error messages from operands that had errors.  */
-  if (lhs == error_mark_node || rhs == error_mark_node)
+  if (error_operand_p (lhs) || error_operand_p (rhs))
     return error_mark_node;
 
   /* Handle control structure constructs used as "lvalues".  */
   switch (TREE_CODE (lhs))
     {
-      /* Handle --foo = 5; as these are valid constructs in C++ */
+      /* Handle --foo = 5; as these are valid constructs in C++ */
     case PREDECREMENT_EXPR:
     case PREINCREMENT_EXPR:
       if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
-       lhs = build (TREE_CODE (lhs), TREE_TYPE (lhs),
-                    stabilize_reference (TREE_OPERAND (lhs, 0)),
-                    TREE_OPERAND (lhs, 1));
-      return build (COMPOUND_EXPR, lhstype,
-                   lhs,
-                   build_modify_expr (TREE_OPERAND (lhs, 0),
-                                      modifycode, rhs));
+       lhs = build2 (TREE_CODE (lhs), TREE_TYPE (lhs),
+                     stabilize_reference (TREE_OPERAND (lhs, 0)),
+                     TREE_OPERAND (lhs, 1));
+      newrhs = cp_build_modify_expr (TREE_OPERAND (lhs, 0),
+                                    modifycode, rhs, complain);
+      if (newrhs == error_mark_node)
+       return error_mark_node;
+      return build2 (COMPOUND_EXPR, lhstype, lhs, newrhs);
 
       /* Handle (a, b) used as an "lvalue".  */
     case COMPOUND_EXPR:
-      newrhs = build_modify_expr (TREE_OPERAND (lhs, 1),
-                                 modifycode, rhs);
+      newrhs = cp_build_modify_expr (TREE_OPERAND (lhs, 1),
+                                    modifycode, rhs, complain);
       if (newrhs == error_mark_node)
        return error_mark_node;
-      return build (COMPOUND_EXPR, lhstype,
-                   TREE_OPERAND (lhs, 0), newrhs);
+      return build2 (COMPOUND_EXPR, lhstype,
+                    TREE_OPERAND (lhs, 0), newrhs);
 
     case MODIFY_EXPR:
-      newrhs = build_modify_expr (TREE_OPERAND (lhs, 0), modifycode, rhs);
+      if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
+       lhs = build2 (TREE_CODE (lhs), TREE_TYPE (lhs),
+                     stabilize_reference (TREE_OPERAND (lhs, 0)),
+                     TREE_OPERAND (lhs, 1));
+      newrhs = cp_build_modify_expr (TREE_OPERAND (lhs, 0), modifycode, rhs,
+                                    complain);
       if (newrhs == error_mark_node)
        return error_mark_node;
-      return build (COMPOUND_EXPR, lhstype, lhs, newrhs);
+      return build2 (COMPOUND_EXPR, lhstype, lhs, newrhs);
+
+    case MIN_EXPR:
+    case MAX_EXPR:
+      /* MIN_EXPR and MAX_EXPR are currently only permitted as lvalues,
+        when neither operand has side-effects.  */
+      if (!lvalue_or_else (lhs, lv_assign, complain))
+       return error_mark_node;
+
+      gcc_assert (!TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0))
+                 && !TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 1)));
+
+      lhs = build3 (COND_EXPR, TREE_TYPE (lhs),
+                   build2 (TREE_CODE (lhs) == MIN_EXPR ? LE_EXPR : GE_EXPR,
+                           boolean_type_node,
+                           TREE_OPERAND (lhs, 0),
+                           TREE_OPERAND (lhs, 1)),
+                   TREE_OPERAND (lhs, 0),
+                   TREE_OPERAND (lhs, 1));
+      /* Fall through.  */
 
       /* Handle (a ? b : c) used as an "lvalue".  */
     case COND_EXPR:
@@ -5468,63 +5946,65 @@ build_modify_expr (lhs, modifycode, rhs)
           except that the RHS goes through a save-expr
           so the code to compute it is only emitted once.  */
        tree cond;
+       tree preeval = NULL_TREE;
+
+       if (VOID_TYPE_P (TREE_TYPE (rhs)))
+         {
+           if (complain & tf_error)
+             error ("void value not ignored as it ought to be");
+           return error_mark_node;
+         }
+
+       rhs = stabilize_expr (rhs, &preeval);
 
-       if (lvalue_p (rhs))
-         rhs = stabilize_reference (rhs);
-       else
-         rhs = save_expr (rhs);
-       
        /* Check this here to avoid odd errors when trying to convert
           a throw to the type of the COND_EXPR.  */
-       if (!lvalue_or_else (lhs, "assignment"))
+       if (!lvalue_or_else (lhs, lv_assign, complain))
          return error_mark_node;
 
        cond = build_conditional_expr
          (TREE_OPERAND (lhs, 0),
-          build_modify_expr (cp_convert (TREE_TYPE (lhs),
-                                         TREE_OPERAND (lhs, 1)),
-                             modifycode, rhs),
-          build_modify_expr (cp_convert (TREE_TYPE (lhs),
-                                         TREE_OPERAND (lhs, 2)),
-                             modifycode, rhs));
+          cp_build_modify_expr (TREE_OPERAND (lhs, 1),
+                                modifycode, rhs, complain),
+          cp_build_modify_expr (TREE_OPERAND (lhs, 2),
+                                modifycode, rhs, complain),
+           complain);
 
        if (cond == error_mark_node)
          return cond;
        /* Make sure the code to compute the rhs comes out
           before the split.  */
-       return build (COMPOUND_EXPR, TREE_TYPE (lhs),
-                     /* Cast to void to suppress warning
-                        from warn_if_unused_value.  */
-                     cp_convert (void_type_node, rhs), cond);
+       if (preeval)
+         cond = build2 (COMPOUND_EXPR, TREE_TYPE (lhs), preeval, cond);
+       return cond;
       }
-      
-    case OFFSET_REF:
-      lhs = resolve_offset_ref (lhs);
-      if (lhs == error_mark_node)
-       return error_mark_node;
-      olhstype = lhstype = TREE_TYPE (lhs);
-    
+
     default:
       break;
     }
 
   if (modifycode == INIT_EXPR)
     {
-      if (TREE_CODE (rhs) == CONSTRUCTOR)
+      if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
+       /* Do the default thing.  */;
+      else if (TREE_CODE (rhs) == CONSTRUCTOR)
        {
-         my_friendly_assert (same_type_p (TREE_TYPE (rhs), lhstype),
-                             20011220);
-         result = build (INIT_EXPR, lhstype, lhs, rhs);
+         /* Compound literal.  */
+         if (! same_type_p (TREE_TYPE (rhs), lhstype))
+           /* Call convert to generate an error; see PR 11063.  */
+           rhs = convert (lhstype, rhs);
+         result = build2 (INIT_EXPR, lhstype, lhs, rhs);
          TREE_SIDE_EFFECTS (result) = 1;
          return result;
        }
-      else if (! IS_AGGR_TYPE (lhstype))
-       /* Do the default thing */;
+      else if (! MAYBE_CLASS_TYPE_P (lhstype))
+       /* Do the default thing */;
       else
        {
-         result = build_method_call (lhs, complete_ctor_identifier,
-                                     build_tree_list (NULL_TREE, rhs),
-                                     TYPE_BINFO (lhstype), LOOKUP_NORMAL);
+         result = build_special_member_call (lhs, complete_ctor_identifier,
+                                             build_tree_list (NULL_TREE, rhs),
+                                             lhstype, LOOKUP_NORMAL,
+                                              complain);
          if (result == NULL_TREE)
            return error_mark_node;
          return result;
@@ -5532,11 +6012,6 @@ build_modify_expr (lhs, modifycode, rhs)
     }
   else
     {
-      if (TREE_CODE (lhstype) == REFERENCE_TYPE)
-       {
-         lhs = convert_from_reference (lhs);
-         olhstype = lhstype = TREE_TYPE (lhs);
-       }
       lhs = require_complete_type (lhs);
       if (lhs == error_mark_node)
        return error_mark_node;
@@ -5544,12 +6019,14 @@ build_modify_expr (lhs, modifycode, rhs)
       if (modifycode == NOP_EXPR)
        {
          /* `operator=' is not an inheritable operator.  */
-         if (! IS_AGGR_TYPE (lhstype))
-           /* Do the default thing */;
+         if (! MAYBE_CLASS_TYPE_P (lhstype))
+           /* Do the default thing */;
          else
            {
-             result = build_opfncall (MODIFY_EXPR, LOOKUP_NORMAL,
-                                      lhs, rhs, make_node (NOP_EXPR));
+             result = build_new_op (MODIFY_EXPR, LOOKUP_NORMAL,
+                                    lhs, rhs, make_node (NOP_EXPR),
+                                    /*overloaded_p=*/NULL, 
+                                    complain);
              if (result == NULL_TREE)
                return error_mark_node;
              return result;
@@ -5559,75 +6036,33 @@ build_modify_expr (lhs, modifycode, rhs)
       else
        {
          /* A binary op has been requested.  Combine the old LHS
-            value with the RHS producing the value we should actually
-            store into the LHS.  */
+            value with the RHS producing the value we should actually
+            store into the LHS.  */
+         gcc_assert (!((TREE_CODE (lhstype) == REFERENCE_TYPE
+                        && MAYBE_CLASS_TYPE_P (TREE_TYPE (lhstype)))
+                       || MAYBE_CLASS_TYPE_P (lhstype)));
 
-         my_friendly_assert (!PROMOTES_TO_AGGR_TYPE (lhstype, REFERENCE_TYPE),
-                             978652);
          lhs = stabilize_reference (lhs);
-         newrhs = cp_build_binary_op (modifycode, lhs, rhs);
+         newrhs = cp_build_binary_op (input_location,
+                                      modifycode, lhs, rhs,
+                                      complain);
          if (newrhs == error_mark_node)
            {
-             error ("  in evaluation of `%Q(%#T, %#T)'", modifycode,
-                    TREE_TYPE (lhs), TREE_TYPE (rhs));
+             if (complain & tf_error)
+               error ("  in evaluation of %<%Q(%#T, %#T)%>", modifycode,
+                      TREE_TYPE (lhs), TREE_TYPE (rhs));
              return error_mark_node;
            }
-         
+
          /* Now it looks like a plain assignment.  */
          modifycode = NOP_EXPR;
        }
-      my_friendly_assert (TREE_CODE (lhstype) != REFERENCE_TYPE, 20011220);
-      my_friendly_assert (TREE_CODE (TREE_TYPE (newrhs)) != REFERENCE_TYPE,
-                         20011220);
-    }
-
-  /* Handle a cast used as an "lvalue".
-     We have already performed any binary operator using the value as cast.
-     Now convert the result to the cast type of the lhs,
-     and then true type of the lhs and store it there;
-     then convert result back to the cast type to be the value
-     of the assignment.  */
-
-  switch (TREE_CODE (lhs))
-    {
-    case NOP_EXPR:
-    case CONVERT_EXPR:
-    case FLOAT_EXPR:
-    case FIX_TRUNC_EXPR:
-    case FIX_FLOOR_EXPR:
-    case FIX_ROUND_EXPR:
-    case FIX_CEIL_EXPR:
-      {
-       tree inner_lhs = TREE_OPERAND (lhs, 0);
-       tree result;
-
-       if (TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
-           || TREE_CODE (TREE_TYPE (newrhs)) == FUNCTION_TYPE
-           || TREE_CODE (TREE_TYPE (newrhs)) == METHOD_TYPE
-           || TREE_CODE (TREE_TYPE (newrhs)) == OFFSET_TYPE)
-         newrhs = default_conversion (newrhs);
-       
-       /* ISO C++ 5.4/1: The result is an lvalue if T is a reference
-          type, otherwise the result is an rvalue.  */
-       if (! lvalue_p (lhs))
-         pedwarn ("ISO C++ forbids cast to non-reference type used as lvalue");
-
-       result = build_modify_expr (inner_lhs, NOP_EXPR,
-                                   cp_convert (TREE_TYPE (inner_lhs),
-                                               cp_convert (lhstype, newrhs)));
-       if (result == error_mark_node)
-         return result;
-       return cp_convert (TREE_TYPE (lhs), result);
-      }
-
-    default:
-      break;
+      gcc_assert (TREE_CODE (lhstype) != REFERENCE_TYPE);
+      gcc_assert (TREE_CODE (TREE_TYPE (newrhs)) != REFERENCE_TYPE);
     }
 
-  /* Now we have handled acceptable kinds of LHS that are not truly lvalues.
-     Reject anything strange now.  */
-
-  if (!lvalue_or_else (lhs, "assignment"))
+  /* The left-hand side must be an lvalue.  */
+  if (!lvalue_or_else (lhs, lv_assign, complain))
     return error_mark_node;
 
   /* Warn about modifying something that is `const'.  Don't warn if
@@ -5640,78 +6075,79 @@ build_modify_expr (lhs, modifycode, rhs)
          || TREE_CODE (TREE_TYPE (lhs)) == METHOD_TYPE
          /* If it's an aggregate and any field is const, then it is
             effectively const.  */
-         || (IS_AGGR_TYPE_CODE (TREE_CODE (lhstype))
+         || (CLASS_TYPE_P (lhstype)
              && C_TYPE_FIELDS_READONLY (lhstype))))
-    readonly_error (lhs, "assignment", 0);
-
-  /* If storing into a structure or union member, it has probably been
-     given type `int'.  Compute the type that would go with the actual
-     amount of storage the member occupies.  */
-
-  if (TREE_CODE (lhs) == COMPONENT_REF
-      && (TREE_CODE (lhstype) == INTEGER_TYPE
-         || TREE_CODE (lhstype) == REAL_TYPE
-         || TREE_CODE (lhstype) == ENUMERAL_TYPE))
     {
-      lhstype = TREE_TYPE (get_unwidened (lhs, 0));
-
-      /* If storing in a field that is in actuality a short or narrower
-        than one, we must store in the field in its actual type.  */
-
-      if (lhstype != TREE_TYPE (lhs))
-       {
-         lhs = copy_node (lhs);
-         TREE_TYPE (lhs) = lhstype;
-       }
+      if (complain & tf_error)
+       readonly_error (lhs, "assignment");
+      else
+       return error_mark_node;
     }
 
-  if (TREE_CODE (lhstype) != REFERENCE_TYPE)
-    {
-      if (TREE_SIDE_EFFECTS (lhs))
-       lhs = stabilize_reference (lhs);
-      if (TREE_SIDE_EFFECTS (newrhs))
-       newrhs = stabilize_reference (newrhs);
-    }
+  /* If storing into a structure or union member, it may have been given a
+     lowered bitfield type.  We need to convert to the declared type first,
+     so retrieve it now.  */
+
+  olhstype = unlowered_expr_type (lhs);
 
   /* Convert new value to destination type.  */
 
   if (TREE_CODE (lhstype) == ARRAY_TYPE)
     {
       int from_array;
-      
-      if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype),
-                               TYPE_MAIN_VARIANT (TREE_TYPE (rhs))))
+
+      if (BRACE_ENCLOSED_INITIALIZER_P (newrhs))
+       {
+         if (check_array_initializer (lhs, lhstype, newrhs))
+           return error_mark_node;
+         newrhs = digest_init (lhstype, newrhs);
+       }
+
+      else if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype),
+                                    TYPE_MAIN_VARIANT (TREE_TYPE (newrhs))))
        {
-         error ("incompatible types in assignment of `%T' to `%T'",
-                TREE_TYPE (rhs), lhstype);
+         if (complain & tf_error)
+           error ("incompatible types in assignment of %qT to %qT",
+                  TREE_TYPE (rhs), lhstype);
          return error_mark_node;
        }
 
       /* Allow array assignment in compiler-generated code.  */
-      if (! DECL_ARTIFICIAL (current_function_decl))
-       pedwarn ("ISO C++ forbids assignment of arrays");
+      else if (!current_function_decl
+              || !DECL_ARTIFICIAL (current_function_decl))
+       {
+          /* This routine is used for both initialization and assignment.
+             Make sure the diagnostic message differentiates the context.  */
+         if (complain & tf_error)
+           {
+             if (modifycode == INIT_EXPR)
+               error ("array used as initializer");
+             else
+               error ("invalid array assignment");
+           }
+         return error_mark_node;
+       }
 
       from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
-                  ? 1 + (modifycode != INIT_EXPR): 0;
-      return build_vec_init (lhs, newrhs, from_array);
+                  ? 1 + (modifycode != INIT_EXPR): 0;
+      return build_vec_init (lhs, NULL_TREE, newrhs,
+                            /*explicit_value_init_p=*/false,
+                            from_array, complain);
     }
 
   if (modifycode == INIT_EXPR)
-    newrhs = convert_for_initialization (lhs, lhstype, newrhs, LOOKUP_NORMAL,
-                                        "initialization", NULL_TREE, 0);
+    newrhs = convert_for_initialization (lhs, olhstype, newrhs, LOOKUP_NORMAL,
+                                        "initialization", NULL_TREE, 0,
+                                         complain);
   else
+    newrhs = convert_for_assignment (olhstype, newrhs, "assignment",
+                                    NULL_TREE, 0, complain);
+
+  if (!same_type_p (lhstype, olhstype))
+    newrhs = cp_convert_and_check (lhstype, newrhs);
+
+  if (modifycode != INIT_EXPR)
     {
-      /* Avoid warnings on enum bit fields.  */
-      if (TREE_CODE (olhstype) == ENUMERAL_TYPE
-         && TREE_CODE (lhstype) == INTEGER_TYPE)
-       {
-         newrhs = convert_for_assignment (olhstype, newrhs, "assignment",
-                                          NULL_TREE, 0);
-         newrhs = convert_force (lhstype, newrhs, 0);
-       }
-      else
-       newrhs = convert_for_assignment (lhstype, newrhs, "assignment",
-                                        NULL_TREE, 0);
       if (TREE_CODE (newrhs) == CALL_EXPR
          && TYPE_NEEDS_CONSTRUCTING (lhstype))
        newrhs = build_cplus_new (lhstype, newrhs);
@@ -5721,79 +6157,34 @@ build_modify_expr (lhs, modifycode, rhs)
         accidental self-initialization.  So we force the TARGET_EXPR to be
         expanded without a target.  */
       if (TREE_CODE (newrhs) == TARGET_EXPR)
-       newrhs = build (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
-                       TREE_OPERAND (newrhs, 0));
+       newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
+                        TREE_OPERAND (newrhs, 0));
     }
 
   if (newrhs == error_mark_node)
     return error_mark_node;
 
-  if (TREE_CODE (newrhs) == COND_EXPR)
+  if (c_dialect_objc () && flag_objc_gc)
     {
-      tree lhs1;
-      tree cond = TREE_OPERAND (newrhs, 0);
+      result = objc_generate_write_barrier (lhs, modifycode, newrhs);
 
-      if (TREE_SIDE_EFFECTS (lhs))
-       cond = build_compound_expr (tree_cons
-                                   (NULL_TREE, lhs,
-                                    build_tree_list (NULL_TREE, cond)));
-
-      /* Cannot have two identical lhs on this one tree (result) as preexpand
-        calls will rip them out and fill in RTL for them, but when the
-        rtl is generated, the calls will only be in the first side of the
-        condition, not on both, or before the conditional jump! (mrs) */
-      lhs1 = break_out_calls (lhs);
-
-      if (lhs == lhs1)
-       /* If there's no change, the COND_EXPR behaves like any other rhs.  */
-       result = build (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
-                       lhstype, lhs, newrhs);
-      else
-       {
-         tree result_type = TREE_TYPE (newrhs);
-         /* We have to convert each arm to the proper type because the
-            types may have been munged by constant folding.  */
-         result
-           = build (COND_EXPR, result_type, cond,
-                    build_modify_expr (lhs, modifycode,
-                                       cp_convert (result_type,
-                                                   TREE_OPERAND (newrhs, 1))),
-                    build_modify_expr (lhs1, modifycode,
-                                       cp_convert (result_type,
-                                                   TREE_OPERAND (newrhs, 2))));
-       }
-    }
-  else
-    result = build (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
-                   lhstype, lhs, newrhs);
-
-  TREE_SIDE_EFFECTS (result) = 1;
-
-  /* If we got the LHS in a different type for storing in,
-     convert the result back to the nominal type of LHS
-     so that the value we return always has the same type
-     as the LHS argument.  */
-
-  if (olhstype == TREE_TYPE (result))
-    return result;
-  /* Avoid warnings converting integral types back into enums
-     for enum bit fields.  */
-  if (TREE_CODE (TREE_TYPE (result)) == INTEGER_TYPE
-      && TREE_CODE (olhstype) == ENUMERAL_TYPE)
-    {
-      result = build (COMPOUND_EXPR, olhstype, result, olhs);
-      TREE_NO_UNUSED_WARNING (result) = 1;
-      return result;
+      if (result)
+       return result;
     }
-  return convert_for_assignment (olhstype, result, "assignment",
-                                NULL_TREE, 0);
+
+  result = build2 (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
+                  lhstype, lhs, newrhs);
+
+  TREE_SIDE_EFFECTS (result) = 1;
+  if (!plain_assign)
+    TREE_NO_WARNING (result) = 1;
+
+  return result;
 }
 
 tree
-build_x_modify_expr (lhs, modifycode, rhs)
-     tree lhs;
-     enum tree_code modifycode;
-     tree rhs;
+build_x_modify_expr (tree lhs, enum tree_code modifycode, tree rhs,
+                    tsubst_flags_t complain)
 {
   if (processing_template_decl)
     return build_min_nt (MODOP_EXPR, lhs,
@@ -5801,99 +6192,118 @@ build_x_modify_expr (lhs, modifycode, rhs)
 
   if (modifycode != NOP_EXPR)
     {
-      tree rval = build_opfncall (MODIFY_EXPR, LOOKUP_NORMAL, lhs, rhs,
-                                 make_node (modifycode));
+      tree rval = build_new_op (MODIFY_EXPR, LOOKUP_NORMAL, lhs, rhs,
+                               make_node (modifycode),
+                               /*overloaded_p=*/NULL,
+                               complain);
       if (rval)
-       return rval;
+       {
+         TREE_NO_WARNING (rval) = 1;
+         return rval;
+       }
     }
-  return build_modify_expr (lhs, modifycode, rhs);
+  return cp_build_modify_expr (lhs, modifycode, rhs, complain);
 }
 
-\f
-/* Get difference in deltas for different pointer to member function
-   types.  Return integer_zero_node, if FROM cannot be converted to a
-   TO type.  If FORCE is true, then allow reverse conversions as well.
-
-   Note that the naming of FROM and TO is kind of backwards; the return
-   value is what we add to a TO in order to get a FROM.  They are named
-   this way because we call this function to find out how to convert from
-   a pointer to member of FROM to a pointer to member of TO.  */
+/* Helper function for get_delta_difference which assumes FROM is a base
+   class of TO.  Returns a delta for the conversion of pointer-to-member
+   of FROM to pointer-to-member of TO.  If the conversion is invalid,
+   returns zero.  If FROM is not a base class of TO, returns NULL_TREE.
+   If C_CAST_P is true, this conversion is taking place as part of a C-style
+   cast.  */
 
 static tree
-get_delta_difference (from, to, force)
-     tree from, to;
-     int force;
+get_delta_difference_1 (tree from, tree to, bool c_cast_p)
 {
-  tree delta = integer_zero_node;
   tree binfo;
-  tree virt_binfo;
   base_kind kind;
-  
-  binfo = lookup_base (to, from, ba_check, &kind);
+
+  binfo = lookup_base (to, from, c_cast_p ? ba_unique : ba_check, &kind);
   if (kind == bk_inaccessible || kind == bk_ambig)
     {
       error ("   in pointer to member function conversion");
-      return delta;
+      return size_zero_node;
     }
-  if (!binfo)
+  else if (binfo)
     {
-      if (!force)
+      if (kind != bk_via_virtual)
+       return BINFO_OFFSET (binfo);
+      else
+       /* FROM is a virtual base class of TO.  Issue an error or warning
+          depending on whether or not this is a reinterpret cast.  */
        {
-         error_not_base_type (from, to);
-         error ("   in pointer to member conversion");
-         return delta;
+         error ("pointer to member conversion via virtual base %qT",
+                BINFO_TYPE (binfo_from_vbase (binfo)));
+
+         return size_zero_node;
        }
-      binfo = lookup_base (from, to, ba_check, &kind);
-      if (binfo == 0)
-       return delta;
-      virt_binfo = binfo_from_vbase (binfo);
-      
-      if (virt_binfo)
-        {
-          /* This is a reinterpret cast, we choose to do nothing. */
-          warning ("pointer to member cast via virtual base `%T' of `%T'",
-                     BINFO_TYPE (virt_binfo),
-                     BINFO_TYPE (BINFO_INHERITANCE_CHAIN (virt_binfo)));
-          return delta;
-        }
-      delta = BINFO_OFFSET (binfo);
-      delta = cp_convert (ptrdiff_type_node, delta);
-      delta = cp_build_binary_op (MINUS_EXPR,
-                                integer_zero_node,
-                                delta);
+      }
+    else
+      return NULL_TREE;
+}
 
-      return delta;
-    }
+/* Get difference in deltas for different pointer to member function
+   types.  Returns an integer constant of type PTRDIFF_TYPE_NODE.  If
+   the conversion is invalid, the constant is zero.  If
+   ALLOW_INVERSE_P is true, then allow reverse conversions as well.
+   If C_CAST_P is true this conversion is taking place as part of a
+   C-style cast.
 
-  virt_binfo = binfo_from_vbase (binfo);
-  if (virt_binfo)
-    {
-      /* This is a reinterpret cast, we choose to do nothing. */
-      if (force)
-        warning ("pointer to member cast via virtual base `%T' of `%T'",
-                    BINFO_TYPE (virt_binfo),
-                    BINFO_TYPE (BINFO_INHERITANCE_CHAIN (virt_binfo)));
-      else
-       error ("pointer to member conversion via virtual base `%T' of `%T'",
-                 BINFO_TYPE (virt_binfo),
-                  BINFO_TYPE (BINFO_INHERITANCE_CHAIN (virt_binfo)));
-      return delta;
-    }
-  delta = BINFO_OFFSET (binfo);
+   Note that the naming of FROM and TO is kind of backwards; the return
+   value is what we add to a TO in order to get a FROM.  They are named
+   this way because we call this function to find out how to convert from
+   a pointer to member of FROM to a pointer to member of TO.  */
+
+static tree
+get_delta_difference (tree from, tree to,
+                     bool allow_inverse_p,
+                     bool c_cast_p)
+{
+  tree result;
+
+  if (same_type_ignoring_top_level_qualifiers_p (from, to))
+    /* Pointer to member of incomplete class is permitted*/
+    result = size_zero_node;
+  else
+    result = get_delta_difference_1 (from, to, c_cast_p);
+
+  if (!result)
+  {
+    if (!allow_inverse_p)
+      {
+       error_not_base_type (from, to);
+       error ("   in pointer to member conversion");
+       result = size_zero_node;
+      }
+    else
+      {
+       result = get_delta_difference_1 (to, from, c_cast_p);
+
+       if (result)
+         result = size_diffop (size_zero_node, result);
+       else
+         {
+           error_not_base_type (from, to);
+           error ("   in pointer to member conversion");
+           result = size_zero_node;
+         }
+      }
+  }
 
-  return cp_convert (ptrdiff_type_node, delta);
+  return fold_if_not_in_template (convert_to_integer (ptrdiff_type_node,
+                                                     result));
 }
 
 /* Return a constructor for the pointer-to-member-function TYPE using
    the other components as specified.  */
 
 tree
-build_ptrmemfunc1 (type, delta, pfn)
-     tree type, delta, pfn;
+build_ptrmemfunc1 (tree type, tree delta, tree pfn)
 {
   tree u = NULL_TREE;
   tree delta_field;
   tree pfn_field;
+  VEC(constructor_elt, gc) *v;
 
   /* Pull the FIELD_DECLs out of the type.  */
   pfn_field = TYPE_FIELDS (type);
@@ -5902,15 +6312,19 @@ build_ptrmemfunc1 (type, delta, pfn)
   /* Make sure DELTA has the type we want.  */
   delta = convert_and_check (delta_type_node, delta);
 
+  /* Convert to the correct target type if necessary.  */
+  pfn = fold_convert (TREE_TYPE (pfn_field), pfn);
+
   /* Finish creating the initializer.  */
-  u = tree_cons (pfn_field, pfn,
-                build_tree_list (delta_field, delta));
-  u = build (CONSTRUCTOR, type, NULL_TREE, u);
-  TREE_CONSTANT (u) = TREE_CONSTANT (pfn) && TREE_CONSTANT (delta);
+  v = VEC_alloc(constructor_elt, gc, 2);
+  CONSTRUCTOR_APPEND_ELT(v, pfn_field, pfn);
+  CONSTRUCTOR_APPEND_ELT(v, delta_field, delta);
+  u = build_constructor (type, v);
+  TREE_CONSTANT (u) = TREE_CONSTANT (pfn) & TREE_CONSTANT (delta);
   TREE_STATIC (u) = (TREE_CONSTANT (u)
                     && (initializer_constant_valid_p (pfn, TREE_TYPE (pfn))
                         != NULL_TREE)
-                    && (initializer_constant_valid_p (delta, TREE_TYPE (delta)) 
+                    && (initializer_constant_valid_p (delta, TREE_TYPE (delta))
                         != NULL_TREE));
   return u;
 }
@@ -5920,36 +6334,41 @@ build_ptrmemfunc1 (type, delta, pfn)
    as a value in expressions.  TYPE is the POINTER to METHOD_TYPE we
    want to be.
 
-   If FORCE is non-zero, then force this conversion, even if
+   If FORCE is nonzero, then force this conversion, even if
    we would rather not do it.  Usually set when using an explicit
-   cast.
+   cast.  A C-style cast is being processed iff C_CAST_P is true.
 
    Return error_mark_node, if something goes wrong.  */
 
 tree
-build_ptrmemfunc (type, pfn, force)
-     tree type, pfn;
-     int force;
+build_ptrmemfunc (tree type, tree pfn, int force, bool c_cast_p)
 {
   tree fn;
-  tree pfn_type = TREE_TYPE (pfn);
-  tree to_type = build_ptrmemfunc_type (type);
+  tree pfn_type;
+  tree to_type;
+
+  if (error_operand_p (pfn))
+    return error_mark_node;
+
+  pfn_type = TREE_TYPE (pfn);
+  to_type = build_ptrmemfunc_type (type);
 
   /* Handle multiple conversions of pointer to member functions.  */
-  if (TYPE_PTRMEMFUNC_P (TREE_TYPE (pfn)))
+  if (TYPE_PTRMEMFUNC_P (pfn_type))
     {
       tree delta = NULL_TREE;
       tree npfn = NULL_TREE;
       tree n;
 
-      if (!force 
-         && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn))
-       error ("invalid conversion to type `%T' from type `%T'", 
-                 to_type, pfn_type);
+      if (!force
+         && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn, LOOKUP_NORMAL))
+       error ("invalid conversion to type %qT from type %qT",
+              to_type, pfn_type);
 
       n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type),
                                TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type),
-                               force);
+                               force,
+                               c_cast_p);
 
       /* We don't have to do any conversion to convert a
         pointer-to-member to its own type.  But, we don't want to
@@ -5960,7 +6379,8 @@ build_ptrmemfunc (type, pfn, force)
          if (same_type_p (to_type, pfn_type))
            return pfn;
          else if (integer_zerop (n))
-           return build_reinterpret_cast (to_type, pfn);
+           return build_reinterpret_cast (to_type, pfn, 
+                                           tf_warning_or_error);
        }
 
       if (TREE_SIDE_EFFECTS (pfn))
@@ -5971,15 +6391,19 @@ build_ptrmemfunc (type, pfn, force)
        expand_ptrmemfunc_cst (pfn, &delta, &npfn);
       else
        {
-         npfn = build_component_ref (pfn, pfn_identifier, NULL_TREE, 0);
-         delta = build_component_ref (pfn, delta_identifier, NULL_TREE, 0);
+         npfn = build_ptrmemfunc_access_expr (pfn, pfn_identifier);
+         delta = build_ptrmemfunc_access_expr (pfn, delta_identifier);
        }
 
       /* Just adjust the DELTA field.  */
-      delta = cp_convert (ptrdiff_type_node, delta);
+      gcc_assert  (same_type_ignoring_top_level_qualifiers_p
+                  (TREE_TYPE (delta), ptrdiff_type_node));
       if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_delta)
-       n = cp_build_binary_op (LSHIFT_EXPR, n, integer_one_node);
-      delta = cp_build_binary_op (PLUS_EXPR, delta, n);
+       n = cp_build_binary_op (input_location,
+                               LSHIFT_EXPR, n, integer_one_node,
+                               tf_warning_or_error);
+      delta = cp_build_binary_op (input_location,
+                                 PLUS_EXPR, delta, n, tf_warning_or_error);
       return build_ptrmemfunc1 (to_type, delta, npfn);
     }
 
@@ -5988,15 +6412,18 @@ build_ptrmemfunc (type, pfn, force)
     {
       pfn = build_c_cast (type, integer_zero_node);
       return build_ptrmemfunc1 (to_type,
-                               integer_zero_node, 
+                               integer_zero_node,
                                pfn);
     }
 
   if (type_unknown_p (pfn))
-    return instantiate_type (type, pfn, tf_error | tf_warning);
+    return instantiate_type (type, pfn, tf_warning_or_error);
 
   fn = TREE_OPERAND (pfn, 0);
-  my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL, 0);
+  gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
+             /* In a template, we will have preserved the
+                OFFSET_REF.  */
+             || (processing_template_decl && TREE_CODE (fn) == OFFSET_REF));
   return make_ptrmem_cst (to_type, fn);
 }
 
@@ -6004,20 +6431,17 @@ build_ptrmemfunc (type, pfn, force)
    given by CST.
 
    ??? There is no consistency as to the types returned for the above
-   values.  Some code acts as if its a sizetype and some as if its
+   values.  Some code acts as if it were a sizetype and some as if it were
    integer_type_node.  */
 
 void
-expand_ptrmemfunc_cst (cst, delta, pfn)
-     tree cst;
-     tree *delta;
-     tree *pfn;
+expand_ptrmemfunc_cst (tree cst, tree *delta, tree *pfn)
 {
   tree type = TREE_TYPE (cst);
   tree fn = PTRMEM_CST_MEMBER (cst);
   tree ptr_class, fn_class;
 
-  my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL, 0);
+  gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
 
   /* The class that the function belongs to.  */
   fn_class = DECL_CONTEXT (fn);
@@ -6026,126 +6450,92 @@ expand_ptrmemfunc_cst (cst, delta, pfn)
   ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type);
 
   /* First, calculate the adjustment to the function's class.  */
-  *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0);
+  *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0,
+                                /*c_cast_p=*/0);
 
   if (!DECL_VIRTUAL_P (fn))
     *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type), build_addr_func (fn));
   else
     {
       /* If we're dealing with a virtual function, we have to adjust 'this'
-         again, to point to the base which provides the vtable entry for
-         fn; the call will do the opposite adjustment.  */
-      tree orig_class = DECL_VIRTUAL_CONTEXT (fn);
+        again, to point to the base which provides the vtable entry for
+        fn; the call will do the opposite adjustment.  */
+      tree orig_class = DECL_CONTEXT (fn);
       tree binfo = binfo_or_else (orig_class, fn_class);
-      *delta = fold (build (PLUS_EXPR, TREE_TYPE (*delta),
-                           *delta, BINFO_OFFSET (binfo)));
+      *delta = build2 (PLUS_EXPR, TREE_TYPE (*delta),
+                      *delta, BINFO_OFFSET (binfo));
+      *delta = fold_if_not_in_template (*delta);
 
       /* We set PFN to the vtable offset at which the function can be
         found, plus one (unless ptrmemfunc_vbit_in_delta, in which
         case delta is shifted left, and then incremented).  */
       *pfn = DECL_VINDEX (fn);
-      *pfn = fold (build (MULT_EXPR, integer_type_node, *pfn,
-                         TYPE_SIZE_UNIT (vtable_entry_type)));
+      *pfn = build2 (MULT_EXPR, integer_type_node, *pfn,
+                    TYPE_SIZE_UNIT (vtable_entry_type));
+      *pfn = fold_if_not_in_template (*pfn);
 
       switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
        {
        case ptrmemfunc_vbit_in_pfn:
-         *pfn = fold (build (PLUS_EXPR, integer_type_node, *pfn,
-                             integer_one_node));
+         *pfn = build2 (PLUS_EXPR, integer_type_node, *pfn,
+                        integer_one_node);
+         *pfn = fold_if_not_in_template (*pfn);
          break;
 
        case ptrmemfunc_vbit_in_delta:
-         *delta = fold (build (LSHIFT_EXPR, TREE_TYPE (*delta),
-                               *delta, integer_one_node));
-         *delta = fold (build (PLUS_EXPR, TREE_TYPE (*delta),
-                               *delta, integer_one_node));
+         *delta = build2 (LSHIFT_EXPR, TREE_TYPE (*delta),
+                          *delta, integer_one_node);
+         *delta = fold_if_not_in_template (*delta);
+         *delta = build2 (PLUS_EXPR, TREE_TYPE (*delta),
+                          *delta, integer_one_node);
+         *delta = fold_if_not_in_template (*delta);
          break;
 
        default:
-         abort ();
+         gcc_unreachable ();
        }
 
-      *pfn = fold (build1 (NOP_EXPR, TYPE_PTRMEMFUNC_FN_TYPE (type),
-                          *pfn));
+      *pfn = build_nop (TYPE_PTRMEMFUNC_FN_TYPE (type), *pfn);
+      *pfn = fold_if_not_in_template (*pfn);
     }
 }
 
 /* Return an expression for PFN from the pointer-to-member function
    given by T.  */
 
-tree
-pfn_from_ptrmemfunc (t)
-     tree t;
+static tree
+pfn_from_ptrmemfunc (tree t)
 {
   if (TREE_CODE (t) == PTRMEM_CST)
     {
       tree delta;
       tree pfn;
-      
+
       expand_ptrmemfunc_cst (t, &delta, &pfn);
       if (pfn)
        return pfn;
     }
 
-  return build_component_ref (t, pfn_identifier, NULL_TREE, 0);
+  return build_ptrmemfunc_access_expr (t, pfn_identifier);
 }
 
-/* Expression EXPR is about to be implicitly converted to TYPE.  Warn
-   if this is a potentially dangerous thing to do.  Returns a possibly
-   marked EXPR.  */
+/* Return an expression for DELTA from the pointer-to-member function
+   given by T.  */
 
-tree
-dubious_conversion_warnings (type, expr, errtype, fndecl, parmnum)
-     tree type;
-     tree expr;
-     const char *errtype;
-     tree fndecl;
-     int parmnum;
+static tree
+delta_from_ptrmemfunc (tree t)
 {
-  if (TREE_CODE (type) == REFERENCE_TYPE)
-    type = TREE_TYPE (type);
-  
-  /* Issue warnings about peculiar, but legal, uses of NULL.  */
-  if (ARITHMETIC_TYPE_P (type) && expr == null_node)
-    {
-      if (fndecl)
-        warning ("passing NULL used for non-pointer %s %P of `%D'",
-                    errtype, parmnum, fndecl);
-      else
-        warning ("%s to non-pointer type `%T' from NULL", errtype, type);
-    }
-  
-  /* Warn about assigning a floating-point type to an integer type.  */
-  if (TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE
-      && TREE_CODE (type) == INTEGER_TYPE)
-    {
-      if (fndecl)
-       warning ("passing `%T' for %s %P of `%D'",
-                   TREE_TYPE (expr), errtype, parmnum, fndecl);
-      else
-       warning ("%s to `%T' from `%T'", errtype, type, TREE_TYPE (expr));
-    }
-  /* And warn about assigning a negative value to an unsigned
-     variable.  */
-  else if (TREE_UNSIGNED (type) && TREE_CODE (type) != BOOLEAN_TYPE)
+  if (TREE_CODE (t) == PTRMEM_CST)
     {
-      if (TREE_CODE (expr) == INTEGER_CST
-         && TREE_NEGATED_INT (expr))
-       {
-         if (fndecl)
-           warning ("passing negative value `%E' for %s %P of `%D'",
-                       expr, errtype, parmnum, fndecl);
-         else
-           warning ("%s of negative value `%E' to `%T'",
-                       errtype, expr, type);
-       }
-
-      overflow_warning (expr);
+      tree delta;
+      tree pfn;
 
-      if (TREE_CONSTANT (expr))
-       expr = fold (expr);
+      expand_ptrmemfunc_cst (t, &delta, &pfn);
+      if (delta)
+       return delta;
     }
-  return expr;
+
+  return build_ptrmemfunc_access_expr (t, delta_identifier);
 }
 
 /* Convert value RHS to type TYPE as preparation for an assignment to
@@ -6155,21 +6545,12 @@ dubious_conversion_warnings (type, expr, errtype, fndecl, parmnum)
    FNDECL.  */
 
 static tree
-convert_for_assignment (type, rhs, errtype, fndecl, parmnum)
-     tree type, rhs;
-     const char *errtype;
-     tree fndecl;
-     int parmnum;
+convert_for_assignment (tree type, tree rhs,
+                       const char *errtype, tree fndecl, int parmnum,
+                       tsubst_flags_t complain)
 {
-  register enum tree_code codel = TREE_CODE (type);
-  register tree rhstype;
-  register enum tree_code coder;
-
-  if (codel == OFFSET_TYPE)
-    abort ();
-
-  if (TREE_CODE (rhs) == OFFSET_REF)
-    rhs = resolve_offset_ref (rhs);
+  tree rhstype;
+  enum tree_code coder;
 
   /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue.  */
   if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
@@ -6178,33 +6559,52 @@ convert_for_assignment (type, rhs, errtype, fndecl, parmnum)
   rhstype = TREE_TYPE (rhs);
   coder = TREE_CODE (rhstype);
 
+  if (TREE_CODE (type) == VECTOR_TYPE && coder == VECTOR_TYPE
+      && vector_types_convertible_p (type, rhstype, true))
+    return convert (type, rhs);
+
   if (rhs == error_mark_node || rhstype == error_mark_node)
     return error_mark_node;
   if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)
     return error_mark_node;
 
-  rhs = dubious_conversion_warnings (type, rhs, errtype, fndecl, parmnum);
-
   /* The RHS of an assignment cannot have void type.  */
   if (coder == VOID_TYPE)
     {
-      error ("void value not ignored as it ought to be");
+      if (complain & tf_error)
+       error ("void value not ignored as it ought to be");
       return error_mark_node;
     }
 
   /* Simplify the RHS if possible.  */
   if (TREE_CODE (rhs) == CONST_DECL)
     rhs = DECL_INITIAL (rhs);
-  
-  /* We do not use decl_constant_value here because of this case:
 
-       const char* const s = "s";
-     The conversion rules for a string literal are more lax than for a
-     variable; in particular, a string literal can be converted to a
-     "char *" but the variable "s" cannot be converted in the same
-     way.  If the conversion is allowed, the optimization should be
-     performed while creating the converted expression.  */
+  if (c_dialect_objc ())
+    {
+      int parmno;
+      tree rname = fndecl;
+
+      if (!strcmp (errtype, "assignment"))
+       parmno = -1;
+      else if (!strcmp (errtype, "initialization"))
+       parmno = -2;
+      else
+       {
+         tree selector = objc_message_selector ();
+
+         parmno = parmnum;
+
+         if (selector && parmno > 1)
+           {
+             rname = selector;
+             parmno -= 1;
+           }
+       }
+
+      if (objc_compare_types (type, rhstype, parmno, rname))
+       return convert (type, rhs);
+    }
 
   /* [expr.ass]
 
@@ -6219,31 +6619,60 @@ convert_for_assignment (type, rhs, errtype, fndecl, parmnum)
       /* When -Wno-pmf-conversions is use, we just silently allow
         conversions from pointers-to-members to plain pointers.  If
         the conversion doesn't work, cp_convert will complain.  */
-      if (!warn_pmf2ptr 
-         && TYPE_PTR_P (type) 
+      if (!warn_pmf2ptr
+         && TYPE_PTR_P (type)
          && TYPE_PTRMEMFUNC_P (rhstype))
        rhs = cp_convert (strip_top_quals (type), rhs);
       else
        {
-         /* If the right-hand side has unknown type, then it is an
-            overloaded function.  Call instantiate_type to get error
-            messages.  */
-         if (rhstype == unknown_type_node)
-           instantiate_type (type, rhs, tf_error | tf_warning);
-         else if (fndecl)
-           error ("cannot convert `%T' to `%T' for argument `%P' to `%D'",
-                     rhstype, type, parmnum, fndecl);
-         else
-           error ("cannot convert `%T' to `%T' in %s", rhstype, type, 
-                     errtype);
+         if (complain & tf_error)
+           {
+             /* If the right-hand side has unknown type, then it is an
+                overloaded function.  Call instantiate_type to get error
+                messages.  */
+             if (rhstype == unknown_type_node)
+               instantiate_type (type, rhs, tf_warning_or_error);
+             else if (fndecl)
+               error ("cannot convert %qT to %qT for argument %qP to %qD",
+                      rhstype, type, parmnum, fndecl);
+             else
+               error ("cannot convert %qT to %qT in %s", rhstype, type,
+                      errtype);
+           }
          return error_mark_node;
        }
     }
-  return perform_implicit_conversion (strip_top_quals (type), rhs);
+  if (warn_missing_format_attribute)
+    {
+      const enum tree_code codel = TREE_CODE (type);
+      if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
+         && coder == codel
+         && check_missing_format_attribute (type, rhstype)
+         && (complain & tf_warning))
+       warning (OPT_Wmissing_format_attribute,
+                "%s might be a candidate for a format attribute",
+                errtype);
+    }
+
+  /* If -Wparentheses, warn about a = b = c when a has type bool and b
+     does not.  */
+  if (warn_parentheses
+      && type == boolean_type_node
+      && TREE_CODE (rhs) == MODIFY_EXPR
+      && !TREE_NO_WARNING (rhs)
+      && TREE_TYPE (rhs) != boolean_type_node
+      && (complain & tf_warning))
+    {
+      warning (OPT_Wparentheses,
+              "suggest parentheses around assignment used as truth value");
+      TREE_NO_WARNING (rhs) = 1;
+    }
+
+  return perform_implicit_conversion (strip_top_quals (type), rhs, complain);
 }
 
 /* Convert RHS to be of type TYPE.
-   If EXP is non-zero, it is the target of the initialization.
+   If EXP is nonzero, it is the target of the initialization.
    ERRTYPE is a string to use in error messages.
 
    Two major differences between the behavior of
@@ -6259,16 +6688,13 @@ convert_for_assignment (type, rhs, errtype, fndecl, parmnum)
    If flags doesn't include LOOKUP_COMPLAIN, don't complain about anything.  */
 
 tree
-convert_for_initialization (exp, type, rhs, flags, errtype, fndecl, parmnum)
-     tree exp, type, rhs;
-     int flags;
-     const char *errtype;
-     tree fndecl;
-     int parmnum;
+convert_for_initialization (tree exp, tree type, tree rhs, int flags,
+                           const char *errtype, tree fndecl, int parmnum,
+                            tsubst_flags_t complain)
 {
-  register enum tree_code codel = TREE_CODE (type);
-  register tree rhstype;
-  register enum tree_code coder;
+  enum tree_code codel = TREE_CODE (type);
+  tree rhstype;
+  enum tree_code coder;
 
   /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
      Strip such NOP_EXPRs, since RHS is used in non-lvalue context.  */
@@ -6277,20 +6703,11 @@ convert_for_initialization (exp, type, rhs, flags, errtype, fndecl, parmnum)
       && codel != REFERENCE_TYPE)
     rhs = TREE_OPERAND (rhs, 0);
 
-  if (rhs == error_mark_node
+  if (type == error_mark_node
+      || rhs == error_mark_node
       || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
     return error_mark_node;
 
-  if (TREE_CODE (rhs) == OFFSET_REF)
-    {
-      rhs = resolve_offset_ref (rhs);
-      if (rhs == error_mark_node)
-       return error_mark_node;
-    }
-
-  if (TREE_CODE (TREE_TYPE (rhs)) == REFERENCE_TYPE)
-    rhs = convert_from_reference (rhs);
-
   if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
        && TREE_CODE (type) != ARRAY_TYPE
        && (TREE_CODE (type) != REFERENCE_TYPE
@@ -6299,7 +6716,7 @@ convert_for_initialization (exp, type, rhs, flags, errtype, fndecl, parmnum)
          && (TREE_CODE (type) != REFERENCE_TYPE
              || TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE))
       || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
-    rhs = default_conversion (rhs);
+    rhs = decay_conversion (rhs);
 
   rhstype = TREE_TYPE (rhs);
   coder = TREE_CODE (rhstype);
@@ -6309,7 +6726,7 @@ convert_for_initialization (exp, type, rhs, flags, errtype, fndecl, parmnum)
 
   /* We accept references to incomplete types, so we can
      return here before checking if RHS is of complete type.  */
-     
+
   if (codel == REFERENCE_TYPE)
     {
       /* This should eventually happen in convert_arguments.  */
@@ -6317,99 +6734,39 @@ convert_for_initialization (exp, type, rhs, flags, errtype, fndecl, parmnum)
 
       if (fndecl)
        savew = warningcount, savee = errorcount;
-      rhs = initialize_reference (type, rhs);
+      rhs = initialize_reference (type, rhs, /*decl=*/NULL_TREE,
+                                 /*cleanup=*/NULL);
       if (fndecl)
        {
          if (warningcount > savew)
-           cp_warning_at ("in passing argument %P of `%+D'", parmnum, fndecl);
+           warning (0, "in passing argument %P of %q+D", parmnum, fndecl);
          else if (errorcount > savee)
-           cp_error_at ("in passing argument %P of `%+D'", parmnum, fndecl);
+           error ("in passing argument %P of %q+D", parmnum, fndecl);
        }
       return rhs;
-    }      
+    }
 
   if (exp != 0)
     exp = require_complete_type (exp);
   if (exp == error_mark_node)
     return error_mark_node;
 
-  if (TREE_CODE (rhstype) == REFERENCE_TYPE)
-    rhstype = TREE_TYPE (rhstype);
+  rhstype = non_reference (rhstype);
 
   type = complete_type (type);
 
-  if (IS_AGGR_TYPE (type))
+  if (MAYBE_CLASS_TYPE_P (type))
     return ocp_convert (type, rhs, CONV_IMPLICIT|CONV_FORCE_TEMP, flags);
 
-  return convert_for_assignment (type, rhs, errtype, fndecl, parmnum);
-}
-\f
-/* Expand an ASM statement with operands, handling output operands
-   that are not variables or INDIRECT_REFS by transforming such
-   cases into cases that expand_asm_operands can handle.
-
-   Arguments are same as for expand_asm_operands.
-
-   We don't do default conversions on all inputs, because it can screw
-   up operands that are expected to be in memory.  */
-
-void
-c_expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line)
-     tree string, outputs, inputs, clobbers;
-     int vol;
-     const char *filename;
-     int line;
-{
-  int noutputs = list_length (outputs);
-  register int i;
-  /* o[I] is the place that output number I should be written.  */
-  register tree *o = (tree *) alloca (noutputs * sizeof (tree));
-  register tree tail;
-
-  /* Record the contents of OUTPUTS before it is modified.  */
-  for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
-    o[i] = TREE_VALUE (tail);
-
-  /* Generate the ASM_OPERANDS insn;
-     store into the TREE_VALUEs of OUTPUTS some trees for
-     where the values were actually stored.  */
-  expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line);
-
-  /* Copy all the intermediate outputs into the specified outputs.  */
-  for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
-    {
-      if (o[i] != TREE_VALUE (tail))
-       {
-         expand_expr (build_modify_expr (o[i], NOP_EXPR, TREE_VALUE (tail)),
-                      const0_rtx, VOIDmode, EXPAND_NORMAL);
-         free_temp_slots ();
-
-         /* Restore the original value so that it's correct the next
-            time we expand this function.  */
-         TREE_VALUE (tail) = o[i];
-       }
-      /* Detect modification of read-only values.
-        (Otherwise done by build_modify_expr.)  */
-      else
-       {
-         tree type = TREE_TYPE (o[i]);
-         if (CP_TYPE_CONST_P (type)
-             || (IS_AGGR_TYPE_CODE (TREE_CODE (type))
-                 && C_TYPE_FIELDS_READONLY (type)))
-           readonly_error (o[i], "modification by `asm'", 1);
-       }
-    }
-
-  /* Those MODIFY_EXPRs could do autoincrements.  */
-  emit_queue ();
+  return convert_for_assignment (type, rhs, errtype, fndecl, parmnum,
+                                complain);
 }
 \f
 /* If RETVAL is the address of, or a reference to, a local variable or
-   temporary give an appropraite warning.  */
+   temporary give an appropriate warning.  */
 
 static void
-maybe_warn_about_returning_address_of_local (retval)
-     tree retval;
+maybe_warn_about_returning_address_of_local (tree retval)
 {
   tree valtype = TREE_TYPE (DECL_RESULT (current_function_decl));
   tree whats_returned = retval;
@@ -6418,9 +6775,8 @@ maybe_warn_about_returning_address_of_local (retval)
     {
       if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
        whats_returned = TREE_OPERAND (whats_returned, 1);
-      else if (TREE_CODE (whats_returned) == CONVERT_EXPR
-              || TREE_CODE (whats_returned) == NON_LVALUE_EXPR
-              || TREE_CODE (whats_returned) == NOP_EXPR)
+      else if (CONVERT_EXPR_P (whats_returned)
+              || TREE_CODE (whats_returned) == NON_LVALUE_EXPR)
        whats_returned = TREE_OPERAND (whats_returned, 0);
       else
        break;
@@ -6428,63 +6784,69 @@ maybe_warn_about_returning_address_of_local (retval)
 
   if (TREE_CODE (whats_returned) != ADDR_EXPR)
     return;
-  whats_returned = TREE_OPERAND (whats_returned, 0);      
+  whats_returned = TREE_OPERAND (whats_returned, 0);
 
   if (TREE_CODE (valtype) == REFERENCE_TYPE)
     {
       if (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
          || TREE_CODE (whats_returned) == TARGET_EXPR)
        {
-         /* Get the target.  */
-         whats_returned = TREE_OPERAND (whats_returned, 0);
-         warning ("returning reference to temporary");
+         warning (0, "returning reference to temporary");
          return;
        }
-      if (TREE_CODE (whats_returned) == VAR_DECL 
+      if (TREE_CODE (whats_returned) == VAR_DECL
          && DECL_NAME (whats_returned)
          && TEMP_NAME_P (DECL_NAME (whats_returned)))
        {
-         warning ("reference to non-lvalue returned");
+         warning (0, "reference to non-lvalue returned");
          return;
        }
     }
 
-  if (TREE_CODE (whats_returned) == VAR_DECL
+  while (TREE_CODE (whats_returned) == COMPONENT_REF
+        || TREE_CODE (whats_returned) == ARRAY_REF)
+    whats_returned = TREE_OPERAND (whats_returned, 0);
+
+  if (DECL_P (whats_returned)
       && DECL_NAME (whats_returned)
       && DECL_FUNCTION_SCOPE_P (whats_returned)
       && !(TREE_STATIC (whats_returned)
           || TREE_PUBLIC (whats_returned)))
     {
       if (TREE_CODE (valtype) == REFERENCE_TYPE)
-       cp_warning_at ("reference to local variable `%D' returned", 
-                      whats_returned);
+       warning (0, "reference to local variable %q+D returned",
+                whats_returned);
       else
-       cp_warning_at ("address of local variable `%D' returned", 
-                      whats_returned);
+       warning (0, "address of local variable %q+D returned",
+                whats_returned);
       return;
     }
 }
 
-/* Check that returning RETVAL from the current function is legal.
+/* Check that returning RETVAL from the current function is valid.
    Return an expression explicitly showing all conversions required to
    change RETVAL into the function return type, and to assign it to
-   the DECL_RESULT for the function.  */
+   the DECL_RESULT for the function.  Set *NO_WARNING to true if
+   code reaches end of non-void function warning shouldn't be issued
+   on this RETURN_EXPR.  */
 
 tree
-check_return_expr (retval)
-     tree retval;
+check_return_expr (tree retval, bool *no_warning)
 {
   tree result;
   /* The type actually returned by the function, after any
      promotions.  */
   tree valtype;
   int fn_returns_value_p;
+  bool named_return_value_okay_p;
+
+  *no_warning = false;
 
   /* A `volatile' function is one that isn't supposed to return, ever.
      (This is a G++ extension, used to get better code for functions
      that call the `volatile' function.)  */
   if (TREE_THIS_VOLATILE (current_function_decl))
-    warning ("function declared `noreturn' has a `return' statement");
+    warning (0, "function declared %<noreturn%> has a %<return%> statement");
 
   /* Check for various simple errors.  */
   if (DECL_DESTRUCTOR_P (current_function_decl))
@@ -6497,7 +6859,7 @@ check_return_expr (retval)
     {
       if (in_function_try_handler)
        /* If a return statement appears in a handler of the
-          function-try-block of a constructor, the program is ill-formed. */
+          function-try-block of a constructor, the program is ill-formed.  */
        error ("cannot return from a handler of a function-try-block of a constructor");
       else if (retval)
        /* You can't return a value from a constructor.  */
@@ -6505,11 +6867,19 @@ check_return_expr (retval)
       return NULL_TREE;
     }
 
+  if (processing_template_decl)
+    {
+      current_function_returns_value = 1;
+      if (check_for_bare_parameter_packs (retval))
+        retval = error_mark_node;
+      return retval;
+    }
+
   /* When no explicit return-value is given in a function with a named
      return value, the named return value is used.  */
   result = DECL_RESULT (current_function_decl);
   valtype = TREE_TYPE (result);
-  my_friendly_assert (valtype != NULL_TREE, 19990924);
+  gcc_assert (valtype != NULL_TREE);
   fn_returns_value_p = !VOID_TYPE_P (valtype);
   if (!retval && DECL_NAME (result) && fn_returns_value_p)
     retval = result;
@@ -6518,24 +6888,29 @@ check_return_expr (retval)
      that's supposed to return a value.  */
   if (!retval && fn_returns_value_p)
     {
-      pedwarn ("return-statement with no value, in function declared with a non-void return type");
+      permerror (input_location, "return-statement with no value, in function returning %qT",
+                valtype);
       /* Clear this, so finish_function won't say that we reach the
         end of a non-void function (which we don't, we gave a
         return!).  */
       current_function_returns_null = 0;
+      /* And signal caller that TREE_NO_WARNING should be set on the
+        RETURN_EXPR to avoid control reaches end of non-void function
+        warnings in tree-cfg.c.  */
+      *no_warning = true;
     }
   /* Check for a return statement with a value in a function that
      isn't supposed to return a value.  */
   else if (retval && !fn_returns_value_p)
-    {     
+    {
       if (VOID_TYPE_P (TREE_TYPE (retval)))
        /* You can return a `void' value from a function of `void'
           type.  In that case, we have to evaluate the expression for
           its side-effects.  */
          finish_expr_stmt (retval);
       else
-       pedwarn ("return-statement with a value, in function declared with a void return type");
-
+       permerror (input_location, "return-statement with a value, in function "
+                  "returning 'void'");
       current_function_returns_null = 1;
 
       /* There's really no value to return, after all.  */
@@ -6549,19 +6924,49 @@ check_return_expr (retval)
     /* Remember that this function did return a value.  */
     current_function_returns_value = 1;
 
+  /* Check for erroneous operands -- but after giving ourselves a
+     chance to provide an error about returning a value from a void
+     function.  */
+  if (error_operand_p (retval))
+    {
+      current_function_return_value = error_mark_node;
+      return error_mark_node;
+    }
+
   /* Only operator new(...) throw(), can return NULL [expr.new/13].  */
   if ((DECL_OVERLOADED_OPERATOR_P (current_function_decl) == NEW_EXPR
        || DECL_OVERLOADED_OPERATOR_P (current_function_decl) == VEC_NEW_EXPR)
       && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl))
       && ! flag_check_new
-      && null_ptr_cst_p (retval))
-    warning ("`operator new' must not return NULL unless it is declared `throw()' (or -fcheck-new is in effect)");
+      && retval && null_ptr_cst_p (retval))
+    warning (0, "%<operator new%> must not return NULL unless it is "
+            "declared %<throw()%> (or -fcheck-new is in effect)");
 
   /* Effective C++ rule 15.  See also start_function.  */
   if (warn_ecpp
-      && DECL_NAME (current_function_decl) == ansi_assopname(NOP_EXPR)
-      && retval != current_class_ref)
-    warning ("`operator=' should return a reference to `*this'");
+      && DECL_NAME (current_function_decl) == ansi_assopname(NOP_EXPR))
+    {
+      bool warn = true;
+
+      /* The function return type must be a reference to the current
+       class.  */
+      if (TREE_CODE (valtype) == REFERENCE_TYPE
+         && same_type_ignoring_top_level_qualifiers_p
+             (TREE_TYPE (valtype), TREE_TYPE (current_class_ref)))
+       {
+         /* Returning '*this' is obviously OK.  */
+         if (retval == current_class_ref)
+           warn = false;
+         /* If we are calling a function whose return type is the same of
+            the current class reference, it is ok.  */
+         else if (TREE_CODE (retval) == INDIRECT_REF
+                  && TREE_CODE (TREE_OPERAND (retval, 0)) == CALL_EXPR)
+           warn = false;
+       }
+
+      if (warn)
+       warning (OPT_Weffc__, "%<operator=%> should return a reference to %<*this%>");
+    }
 
   /* The fabled Named Return Value optimization, as per [class.copy]/15:
 
@@ -6580,23 +6985,31 @@ check_return_expr (retval)
      returned expression uses the chosen variable somehow.  And people expect
      this restriction, anyway.  (jason 2000-11-19)
 
-     See finish_function, genrtl_start_function, and declare_return_variable
-     for other pieces of this optimization.  */
-
+     See finish_function and finalize_nrv for the rest of this optimization.  */
+
+  named_return_value_okay_p = 
+    (retval != NULL_TREE
+     /* Must be a local, automatic variable.  */
+     && TREE_CODE (retval) == VAR_DECL
+     && DECL_CONTEXT (retval) == current_function_decl
+     && ! TREE_STATIC (retval)
+     && ! DECL_ANON_UNION_VAR_P (retval)
+     && (DECL_ALIGN (retval)
+         >= DECL_ALIGN (DECL_RESULT (current_function_decl)))
+     /* The cv-unqualified type of the returned value must be the
+        same as the cv-unqualified return type of the
+        function.  */
+     && same_type_p ((TYPE_MAIN_VARIANT (TREE_TYPE (retval))),
+                     (TYPE_MAIN_VARIANT
+                      (TREE_TYPE (TREE_TYPE (current_function_decl)))))
+     /* And the returned value must be non-volatile.  */
+     && ! TYPE_VOLATILE (TREE_TYPE (retval)));
+     
   if (fn_returns_value_p && flag_elide_constructors)
     {
-      if (retval != NULL_TREE
-         && (current_function_return_value == NULL_TREE
-             || current_function_return_value == retval)
-         && TREE_CODE (retval) == VAR_DECL
-         && DECL_CONTEXT (retval) == current_function_decl
-         && ! TREE_STATIC (retval)
-         && (DECL_ALIGN (retval)
-             >= DECL_ALIGN (DECL_RESULT (current_function_decl)))
-         && same_type_p ((TYPE_MAIN_VARIANT
-                          (TREE_TYPE (retval))),
-                         (TYPE_MAIN_VARIANT
-                          (TREE_TYPE (TREE_TYPE (current_function_decl))))))
+      if (named_return_value_okay_p
+          && (current_function_return_value == NULL_TREE
+              || current_function_return_value == retval))
        current_function_return_value = retval;
       else
        current_function_return_value = error_mark_node;
@@ -6604,8 +7017,8 @@ check_return_expr (retval)
 
   /* We don't need to do any conversions when there's nothing being
      returned.  */
-  if (!retval || retval == error_mark_node)
-    return retval;
+  if (!retval)
+    return NULL_TREE;
 
   /* Do any required conversions.  */
   if (retval == result || DECL_CONSTRUCTOR_P (current_function_decl))
@@ -6615,50 +7028,66 @@ check_return_expr (retval)
     {
       /* The type the function is declared to return.  */
       tree functype = TREE_TYPE (TREE_TYPE (current_function_decl));
+      int flags = LOOKUP_NORMAL | LOOKUP_ONLYCONVERTING;
+
+      /* The functype's return type will have been set to void, if it
+        was an incomplete type.  Just treat this as 'return;' */
+      if (VOID_TYPE_P (functype))
+       return error_mark_node;
+
+      /* Under C++0x [12.8/16 class.copy], a returned lvalue is sometimes
+        treated as an rvalue for the purposes of overload resolution to
+        favor move constructors over copy constructors.  */
+      if ((cxx_dialect != cxx98) 
+          && named_return_value_okay_p
+          /* The variable must not have the `volatile' qualifier.  */
+         && !(cp_type_quals (TREE_TYPE (retval)) & TYPE_QUAL_VOLATILE)
+         /* The return type must be a class type.  */
+         && CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
+       flags = flags | LOOKUP_PREFER_RVALUE;
 
       /* First convert the value to the function's return type, then
         to the type of return value's location to handle the
-         case that functype is smaller than the valtype. */
+        case that functype is smaller than the valtype.  */
       retval = convert_for_initialization
-       (NULL_TREE, functype, retval, LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING,
-        "return", NULL_TREE, 0);
+       (NULL_TREE, functype, retval, flags, "return", NULL_TREE, 0,
+         tf_warning_or_error);
       retval = convert (valtype, retval);
 
       /* If the conversion failed, treat this just like `return;'.  */
       if (retval == error_mark_node)
        return retval;
       /* We can't initialize a register from a AGGR_INIT_EXPR.  */
-      else if (! current_function_returns_struct
+      else if (! cfun->returns_struct
               && TREE_CODE (retval) == TARGET_EXPR
               && TREE_CODE (TREE_OPERAND (retval, 1)) == AGGR_INIT_EXPR)
-       retval = build (COMPOUND_EXPR, TREE_TYPE (retval), retval,
-                       TREE_OPERAND (retval, 0));
+       retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
+                        TREE_OPERAND (retval, 0));
       else
        maybe_warn_about_returning_address_of_local (retval);
     }
-  
+
   /* Actually copy the value returned into the appropriate location.  */
   if (retval && retval != result)
-    retval = build (INIT_EXPR, TREE_TYPE (result), result, retval);
+    retval = build2 (INIT_EXPR, TREE_TYPE (result), result, retval);
 
   return retval;
 }
 
 \f
-/* Returns non-zero if the pointer-type FROM can be converted to the
+/* Returns nonzero if the pointer-type FROM can be converted to the
    pointer-type TO via a qualification conversion.  If CONSTP is -1,
-   then we return non-zero if the pointers are similar, and the
+   then we return nonzero if the pointers are similar, and the
    cv-qualification signature of FROM is a proper subset of that of TO.
 
    If CONSTP is positive, then all outer pointers have been
    const-qualified.  */
 
 static int
-comp_ptr_ttypes_real (to, from, constp)
-     tree to, from;
-     int constp;
+comp_ptr_ttypes_real (tree to, tree from, int constp)
 {
-  int to_more_cv_qualified = 0;
+  bool to_more_cv_qualified = false;
+  bool is_opaque_pointer = false;
 
   for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
     {
@@ -6666,43 +7095,49 @@ comp_ptr_ttypes_real (to, from, constp)
        return 0;
 
       if (TREE_CODE (from) == OFFSET_TYPE
-         && same_type_p (TYPE_OFFSET_BASETYPE (from),
-                         TYPE_OFFSET_BASETYPE (to)))
-         continue;
+         && !same_type_p (TYPE_OFFSET_BASETYPE (from),
+                          TYPE_OFFSET_BASETYPE (to)))
+       return 0;
 
       /* Const and volatile mean something different for function types,
         so the usual checks are not appropriate.  */
       if (TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
        {
-         if (!at_least_as_qualified_p (to, from))
+         /* In Objective-C++, some types may have been 'volatilized' by
+            the compiler for EH; when comparing them here, the volatile
+            qualification must be ignored.  */
+         bool objc_quals_match = objc_type_quals_match (to, from);
+
+         if (!at_least_as_qualified_p (to, from) && !objc_quals_match)
            return 0;
 
-         if (!at_least_as_qualified_p (from, to))
+         if (!at_least_as_qualified_p (from, to) && !objc_quals_match)
            {
              if (constp == 0)
                return 0;
-             else
-               ++to_more_cv_qualified;
+             to_more_cv_qualified = true;
            }
 
          if (constp > 0)
            constp &= TYPE_READONLY (to);
        }
 
-      if (TREE_CODE (to) != POINTER_TYPE)
-       return 
-         same_type_ignoring_top_level_qualifiers_p (to, from)
-         && (constp >= 0 || to_more_cv_qualified);
+      if (TREE_CODE (to) == VECTOR_TYPE)
+       is_opaque_pointer = vector_targets_convertible_p (to, from);
+
+      if (TREE_CODE (to) != POINTER_TYPE && !TYPE_PTRMEM_P (to))
+       return ((constp >= 0 || to_more_cv_qualified)
+               && (is_opaque_pointer
+                   || same_type_ignoring_top_level_qualifiers_p (to, from)));
     }
 }
 
-/* When comparing, say, char ** to char const **, this function takes the
-   'char *' and 'char const *'.  Do not pass non-pointer types to this
-   function.  */
+/* When comparing, say, char ** to char const **, this function takes
+   the 'char *' and 'char const *'.  Do not pass non-pointer/reference
+   types to this function.  */
 
 int
-comp_ptr_ttypes (to, from)
-     tree to, from;
+comp_ptr_ttypes (tree to, tree from)
 {
   return comp_ptr_ttypes_real (to, from, 1);
 }
@@ -6711,8 +7146,7 @@ comp_ptr_ttypes (to, from)
    type or inheritance-related types, regardless of cv-quals.  */
 
 int
-ptr_reasonably_similar (to, from)
-     tree to, from;
+ptr_reasonably_similar (const_tree to, const_tree from)
 {
   for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
     {
@@ -6726,10 +7160,14 @@ ptr_reasonably_similar (to, from)
 
       if (TREE_CODE (from) == OFFSET_TYPE
          && comptypes (TYPE_OFFSET_BASETYPE (to),
-                       TYPE_OFFSET_BASETYPE (from), 
-                       COMPARE_BASE | COMPARE_RELAXED))
+                       TYPE_OFFSET_BASETYPE (from),
+                       COMPARE_BASE | COMPARE_DERIVED))
        continue;
 
+      if (TREE_CODE (to) == VECTOR_TYPE
+         && vector_types_convertible_p (to, from, false))
+       return 1;
+
       if (TREE_CODE (to) == INTEGER_TYPE
          && TYPE_PRECISION (to) == TYPE_PRECISION (from))
        return 1;
@@ -6739,97 +7177,139 @@ ptr_reasonably_similar (to, from)
 
       if (TREE_CODE (to) != POINTER_TYPE)
        return comptypes
-         (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from), 
-          COMPARE_BASE | COMPARE_RELAXED);
+         (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from),
+          COMPARE_BASE | COMPARE_DERIVED);
     }
 }
 
-/* Like comp_ptr_ttypes, for const_cast.  */
+/* Return true if TO and FROM (both of which are POINTER_TYPEs or
+   pointer-to-member types) are the same, ignoring cv-qualification at
+   all levels.  */
 
-static int
-comp_ptr_ttypes_const (to, from)
-     tree to, from;
+bool
+comp_ptr_ttypes_const (tree to, tree from)
 {
+  bool is_opaque_pointer = false;
+
   for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
     {
       if (TREE_CODE (to) != TREE_CODE (from))
-       return 0;
+       return false;
 
       if (TREE_CODE (from) == OFFSET_TYPE
          && same_type_p (TYPE_OFFSET_BASETYPE (from),
                          TYPE_OFFSET_BASETYPE (to)))
          continue;
 
+      if (TREE_CODE (to) == VECTOR_TYPE)
+       is_opaque_pointer = vector_targets_convertible_p (to, from);
+
       if (TREE_CODE (to) != POINTER_TYPE)
-       return same_type_ignoring_top_level_qualifiers_p (to, from);
+       return (is_opaque_pointer
+               || same_type_ignoring_top_level_qualifiers_p (to, from));
     }
 }
 
-/* Like comp_ptr_ttypes, for reinterpret_cast.  */
+/* Returns the type qualifiers for this type, including the qualifiers on the
+   elements for an array type.  */
 
-static int
-comp_ptr_ttypes_reinterpret (to, from)
-     tree to, from;
+int
+cp_type_quals (const_tree type)
 {
-  int constp = 1;
-
-  for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
-    {
-      if (TREE_CODE (from) == OFFSET_TYPE)
-       from = TREE_TYPE (from);
-      if (TREE_CODE (to) == OFFSET_TYPE)
-       to = TREE_TYPE (to);
-
-      /* Const and volatile mean something different for function types,
-        so the usual checks are not appropriate.  */
-      if (TREE_CODE (from) != FUNCTION_TYPE && TREE_CODE (from) != METHOD_TYPE
-         && TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
-       {
-         if (!at_least_as_qualified_p (to, from))
-           return 0;
+  /* This CONST_CAST is okay because strip_array_types returns its
+     argument unmodified and we assign it to a const_tree.  */
+  type = strip_array_types (CONST_CAST_TREE(type));
+  if (type == error_mark_node)
+    return TYPE_UNQUALIFIED;
+  return TYPE_QUALS (type);
+}
 
-         if (! constp
-             && !at_least_as_qualified_p (from, to))
-           return 0;
-         constp &= TYPE_READONLY (to);
-       }
+/* Returns nonzero if the TYPE is const from a C++ perspective: look inside
+   arrays.  */
 
-      if (TREE_CODE (from) != POINTER_TYPE
-         || TREE_CODE (to) != POINTER_TYPE)
-       return 1;
-    }
+bool
+cp_type_readonly (const_tree type)
+{
+  /* This CONST_CAST is okay because strip_array_types returns its
+     argument unmodified and we assign it to a const_tree.  */
+  type = strip_array_types (CONST_CAST_TREE(type));
+  return TYPE_READONLY (type);
 }
 
-/* Returns the type qualifiers for this type, including the qualifiers on the
-   elements for an array type.  */
+/* Returns nonzero if the TYPE contains a mutable member.  */
 
-int
-cp_type_quals (type)
-     tree type;
+bool
+cp_has_mutable_p (const_tree type)
 {
-  type = strip_array_types (type);
-  return TYPE_QUALS (type);
+  /* This CONST_CAST is okay because strip_array_types returns its
+     argument unmodified and we assign it to a const_tree.  */
+  type = strip_array_types (CONST_CAST_TREE(type));
+
+  return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type);
 }
 
-/* Returns non-zero if the TYPE contains a mutable member */
+/* Set TREE_READONLY and TREE_VOLATILE on DECL as indicated by the
+   TYPE_QUALS.  For a VAR_DECL, this may be an optimistic
+   approximation.  In particular, consider:
 
-int
-cp_has_mutable_p (type)
-     tree type;
+     int f();
+     struct S { int i; };
+     const S s = { f(); }
+
+   Here, we will make "s" as TREE_READONLY (because it is declared
+   "const") -- only to reverse ourselves upon seeing that the
+   initializer is non-constant.  */
+
+void
+cp_apply_type_quals_to_decl (int type_quals, tree decl)
 {
-  type = strip_array_types (type);
+  tree type = TREE_TYPE (decl);
 
-  return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type);
+  if (type == error_mark_node)
+    return;
+
+  if (TREE_CODE (decl) == TYPE_DECL)
+    return;
+
+  if (TREE_CODE (type) == FUNCTION_TYPE
+      && type_quals != TYPE_UNQUALIFIED)
+    {
+      /* This was an error in C++98 (cv-qualifiers cannot be added to
+        a function type), but DR 295 makes the code well-formed by
+        dropping the extra qualifiers. */
+      if (pedantic)
+       {
+         tree bad_type = build_qualified_type (type, type_quals);
+         pedwarn (input_location, OPT_pedantic, 
+                  "ignoring %qV qualifiers added to function type %qT",
+                  bad_type, type);
+       }
+
+      TREE_TYPE (decl) = TYPE_MAIN_VARIANT (type);
+      return;
+    }
+
+  /* Avoid setting TREE_READONLY incorrectly.  */
+  if (/* If the object has a constructor, the constructor may modify
+        the object.  */
+      TYPE_NEEDS_CONSTRUCTING (type)
+      /* If the type isn't complete, we don't know yet if it will need
+        constructing.  */
+      || !COMPLETE_TYPE_P (type)
+      /* If the type has a mutable component, that component might be
+        modified.  */
+      || TYPE_HAS_MUTABLE_P (type))
+    type_quals &= ~TYPE_QUAL_CONST;
+
+  c_apply_type_quals_to_decl (type_quals, decl);
 }
 
 /* Subroutine of casts_away_constness.  Make T1 and T2 point at
-   exemplar types such that casting T1 to T2 is casting away castness
+   exemplar types such that casting T1 to T2 is casting away constness
    if and only if there is no implicit conversion from T1 to T2.  */
 
 static void
-casts_away_constness_r (t1, t2)
-     tree *t1;
-     tree *t2;
+casts_away_constness_r (tree *t1, tree *t2)
 {
   int quals1;
   int quals2;
@@ -6840,31 +7320,25 @@ casts_away_constness_r (t1, t2)
      and pointers to members (conv.qual), the "member" aspect of a
      pointer to member level is ignored when determining if a const
      cv-qualifier has been cast away.  */
-  if (TYPE_PTRMEM_P (*t1))
-    *t1 = build_pointer_type (TREE_TYPE (TREE_TYPE (*t1)));
-  if (TYPE_PTRMEM_P (*t2))
-    *t2 = build_pointer_type (TREE_TYPE (TREE_TYPE (*t2)));
-
   /* [expr.const.cast]
 
      For  two  pointer types:
 
-            X1 is T1cv1,1 * ... cv1,N *   where T1 is not a pointer type
-            X2 is T2cv2,1 * ... cv2,M *   where T2 is not a pointer type
-            K is min(N,M)
+           X1 is T1cv1,1 * ... cv1,N *   where T1 is not a pointer type
+           X2 is T2cv2,1 * ... cv2,M *   where T2 is not a pointer type
+           K is min(N,M)
 
      casting from X1 to X2 casts away constness if, for a non-pointer
      type T there does not exist an implicit conversion (clause
      _conv_) from:
 
-            Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
-      
-     to
+           Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
 
-            Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *.  */
+     to
 
-  if (TREE_CODE (*t1) != POINTER_TYPE
-      || TREE_CODE (*t2) != POINTER_TYPE)
+           Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *.  */
+  if ((!TYPE_PTR_P (*t1) && !TYPE_PTRMEM_P (*t1))
+      || (!TYPE_PTR_P (*t2) && !TYPE_PTRMEM_P (*t2)))
     {
       *t1 = cp_build_qualified_type (void_type_node,
                                     cp_type_quals (*t1));
@@ -6872,11 +7346,19 @@ casts_away_constness_r (t1, t2)
                                     cp_type_quals (*t2));
       return;
     }
-  
+
   quals1 = cp_type_quals (*t1);
   quals2 = cp_type_quals (*t2);
-  *t1 = TREE_TYPE (*t1);
-  *t2 = TREE_TYPE (*t2);
+
+  if (TYPE_PTRMEM_P (*t1))
+    *t1 = TYPE_PTRMEM_POINTED_TO_TYPE (*t1);
+  else
+    *t1 = TREE_TYPE (*t1);
+  if (TYPE_PTRMEM_P (*t2))
+    *t2 = TYPE_PTRMEM_POINTED_TO_TYPE (*t2);
+  else
+    *t2 = TREE_TYPE (*t2);
+
   casts_away_constness_r (t1, t2);
   *t1 = build_pointer_type (*t1);
   *t2 = build_pointer_type (*t2);
@@ -6884,70 +7366,76 @@ casts_away_constness_r (t1, t2)
   *t2 = cp_build_qualified_type (*t2, quals2);
 }
 
-/* Returns non-zero if casting from TYPE1 to TYPE2 casts away
+/* Returns nonzero if casting from TYPE1 to TYPE2 casts away
    constness.  */
 
-static int
-casts_away_constness (t1, t2)
-     tree t1;
-     tree t2;
+static bool
+casts_away_constness (tree t1, tree t2)
 {
   if (TREE_CODE (t2) == REFERENCE_TYPE)
     {
       /* [expr.const.cast]
-        
+
         Casting from an lvalue of type T1 to an lvalue of type T2
         using a reference cast casts away constness if a cast from an
         rvalue of type "pointer to T1" to the type "pointer to T2"
         casts away constness.  */
-      t1 = (TREE_CODE (t1) == REFERENCE_TYPE
-           ? TREE_TYPE (t1) : t1);
+      t1 = (TREE_CODE (t1) == REFERENCE_TYPE ? TREE_TYPE (t1) : t1);
       return casts_away_constness (build_pointer_type (t1),
                                   build_pointer_type (TREE_TYPE (t2)));
     }
 
   if (TYPE_PTRMEM_P (t1) && TYPE_PTRMEM_P (t2))
     /* [expr.const.cast]
-       
+
        Casting from an rvalue of type "pointer to data member of X
        of type T1" to the type "pointer to data member of Y of type
        T2" casts away constness if a cast from an rvalue of type
        "pointer to T1" to the type "pointer to T2" casts away
        constness.  */
     return casts_away_constness
-      (build_pointer_type (TREE_TYPE (TREE_TYPE (t1))),
-       build_pointer_type (TREE_TYPE (TREE_TYPE (t2))));
+      (build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t1)),
+       build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t2)));
 
   /* Casting away constness is only something that makes sense for
      pointer or reference types.  */
-  if (TREE_CODE (t1) != POINTER_TYPE 
+  if (TREE_CODE (t1) != POINTER_TYPE
       || TREE_CODE (t2) != POINTER_TYPE)
-    return 0;
+    return false;
 
   /* Top-level qualifiers don't matter.  */
   t1 = TYPE_MAIN_VARIANT (t1);
   t2 = TYPE_MAIN_VARIANT (t2);
   casts_away_constness_r (&t1, &t2);
   if (!can_convert (t2, t1))
-    return 1;
+    return true;
 
-  return 0;
+  return false;
 }
 
-/* Returns TYPE with its cv qualifiers removed
-   TYPE is T cv* .. *cv where T is not a pointer type,
-   returns T * .. *. (If T is an array type, then the cv qualifiers
-   above are those of the array members.)  */
+/* If T is a REFERENCE_TYPE return the type to which T refers.
+   Otherwise, return T itself.  */
 
-static tree
-strip_all_pointer_quals (type)
-     tree type;
+tree
+non_reference (tree t)
 {
-  if (TREE_CODE (type) == POINTER_TYPE)
-    return build_pointer_type (strip_all_pointer_quals (TREE_TYPE (type)));
-  else if (TREE_CODE (type) == OFFSET_TYPE)
-    return build_offset_type (TYPE_OFFSET_BASETYPE (type),
-                             strip_all_pointer_quals (TREE_TYPE (type)));
-  else
-    return TYPE_MAIN_VARIANT (type);
+  if (TREE_CODE (t) == REFERENCE_TYPE)
+    t = TREE_TYPE (t);
+  return t;
+}
+
+
+/* Return nonzero if REF is an lvalue valid for this language;
+   otherwise, print an error message and return zero.  USE says
+   how the lvalue is being used and so selects the error message.  */
+
+int
+lvalue_or_else (tree ref, enum lvalue_use use, tsubst_flags_t complain)
+{
+  int win = lvalue_p (ref);
+
+  if (!win && (complain & tf_error))
+    lvalue_error (use);
+
+  return win;
 }