OSDN Git Service

i386: Rewrite ix86_expand_vshuffle.
[pf3gnuchains/gcc-fork.git] / gcc / cp / call.c
index f75c248..a52ec29 100644 (file)
@@ -1,6 +1,7 @@
 /* Functions related to invoking methods and overloaded functions.
    Copyright (C) 1987, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
-   1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
+   1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
+   2010, 2011
    Free Software Foundation, Inc.
    Contributed by Michael Tiemann (tiemann@cygnus.com) and
    modified by Brendan Kehoe (brendan@cygnus.com).
@@ -39,6 +40,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "convert.h"
 #include "langhooks.h"
 #include "c-family/c-objc.h"
+#include "timevar.h"
 
 /* The various kinds of conversion.  */
 
@@ -98,7 +100,9 @@ struct conversion {
   BOOL_BITFIELD base_p : 1;
   /* If KIND is ck_ref_bind, true when either an lvalue reference is
      being bound to an lvalue expression or an rvalue reference is
-     being bound to an rvalue expression. */
+     being bound to an rvalue expression.  If KIND is ck_rvalue,
+     true when we should treat an lvalue as an rvalue (12.8p33).  If
+     KIND is ck_base, always false.  */
   BOOL_BITFIELD rvaluedness_matches_p: 1;
   BOOL_BITFIELD check_narrowing: 1;
   /* The type of the expression resulting from the conversion.  */
@@ -153,7 +157,6 @@ static tree convert_like_real (conversion *, tree, tree, int, int, bool,
                               bool, tsubst_flags_t);
 static void op_error (enum tree_code, enum tree_code, tree, tree,
                      tree, bool);
-static VEC(tree,gc) *resolve_args (VEC(tree,gc) *);
 static struct z_candidate *build_user_type_conversion_1 (tree, tree, int);
 static void print_z_candidate (const char *, struct z_candidate *);
 static void print_z_candidates (location_t, struct z_candidate *);
@@ -199,7 +202,6 @@ static struct z_candidate *add_candidate
 static tree source_type (conversion *);
 static void add_warning (struct z_candidate *, struct z_candidate *);
 static bool reference_compatible_p (tree, tree);
-static conversion *convert_class_to_reference (tree, tree, tree, int);
 static conversion *direct_reference_binding (tree, conversion *);
 static bool promoted_arithmetic_type_p (tree);
 static conversion *conditional_conversion (tree, tree);
@@ -304,11 +306,32 @@ build_call_n (tree function, int n, ...)
     }
 }
 
+/* Update various flags in cfun and the call itself based on what is being
+   called.  Split out of build_call_a so that bot_manip can use it too.  */
+
+void
+set_flags_from_callee (tree call)
+{
+  int nothrow;
+  tree decl = get_callee_fndecl (call);
+
+  /* We check both the decl and the type; a function may be known not to
+     throw without being declared throw().  */
+  nothrow = ((decl && TREE_NOTHROW (decl))
+            || TYPE_NOTHROW_P (TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (call)))));
+
+  if (!nothrow && at_function_scope_p () && cfun && cp_function_chain)
+    cp_function_chain->can_throw = 1;
+
+  if (decl && TREE_THIS_VOLATILE (decl) && cfun && cp_function_chain)
+    current_function_returns_abnormally = 1;
+
+  TREE_NOTHROW (call) = nothrow;
+}
+
 tree
 build_call_a (tree function, int n, tree *argarray)
 {
-  int is_constructor = 0;
-  int nothrow;
   tree decl;
   tree result_type;
   tree fntype;
@@ -325,57 +348,45 @@ build_call_a (tree function, int n, tree *argarray)
   if (SCALAR_TYPE_P (result_type) || VOID_TYPE_P (result_type))
     result_type = cv_unqualified (result_type);
 
-  if (TREE_CODE (function) == ADDR_EXPR
-      && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
-    {
-      decl = TREE_OPERAND (function, 0);
-      if (!TREE_USED (decl))
-       {
-         /* We invoke build_call directly for several library
-            functions.  These may have been declared normally if
-            we're building libgcc, so we can't just check
-            DECL_ARTIFICIAL.  */
-         gcc_assert (DECL_ARTIFICIAL (decl)
-                     || !strncmp (IDENTIFIER_POINTER (DECL_NAME (decl)),
-                                  "__", 2));
-         mark_used (decl);
-       }
-    }
-  else
-    decl = NULL_TREE;
+  function = build_call_array_loc (input_location,
+                                  result_type, function, n, argarray);
+  set_flags_from_callee (function);
 
-  /* We check both the decl and the type; a function may be known not to
-     throw without being declared throw().  */
-  nothrow = ((decl && TREE_NOTHROW (decl))
-            || TYPE_NOTHROW_P (TREE_TYPE (TREE_TYPE (function))));
+  decl = get_callee_fndecl (function);
 
-  if (decl && TREE_THIS_VOLATILE (decl) && cfun && cp_function_chain)
-    current_function_returns_abnormally = 1;
+  if (decl && !TREE_USED (decl))
+    {
+      /* We invoke build_call directly for several library
+        functions.  These may have been declared normally if
+        we're building libgcc, so we can't just check
+        DECL_ARTIFICIAL.  */
+      gcc_assert (DECL_ARTIFICIAL (decl)
+                 || !strncmp (IDENTIFIER_POINTER (DECL_NAME (decl)),
+                              "__", 2));
+      mark_used (decl);
+    }
 
   if (decl && TREE_DEPRECATED (decl))
     warn_deprecated_use (decl, NULL_TREE);
   require_complete_eh_spec_types (fntype, decl);
 
-  if (decl && DECL_CONSTRUCTOR_P (decl))
-    is_constructor = 1;
+  TREE_HAS_CONSTRUCTOR (function) = (decl && DECL_CONSTRUCTOR_P (decl));
 
   /* Don't pass empty class objects by value.  This is useful
      for tags in STL, which are used to control overload resolution.
      We don't need to handle other cases of copying empty classes.  */
   if (! decl || ! DECL_BUILT_IN (decl))
     for (i = 0; i < n; i++)
-      if (is_empty_class (TREE_TYPE (argarray[i]))
-         && ! TREE_ADDRESSABLE (TREE_TYPE (argarray[i])))
-       {
-         tree t = build0 (EMPTY_CLASS_EXPR, TREE_TYPE (argarray[i]));
-         argarray[i] = build2 (COMPOUND_EXPR, TREE_TYPE (t),
-                               argarray[i], t);
-       }
-
-  function = build_call_array_loc (input_location,
-                                  result_type, function, n, argarray);
-  TREE_HAS_CONSTRUCTOR (function) = is_constructor;
-  TREE_NOTHROW (function) = nothrow;
+      {
+       tree arg = CALL_EXPR_ARG (function, i);
+       if (is_empty_class (TREE_TYPE (arg))
+           && ! TREE_ADDRESSABLE (TREE_TYPE (arg)))
+         {
+           tree t = build0 (EMPTY_CLASS_EXPR, TREE_TYPE (arg));
+           arg = build2 (COMPOUND_EXPR, TREE_TYPE (t), arg, t);
+           CALL_EXPR_ARG (function, i) = arg;
+         }
+      }
 
   return function;
 }
@@ -426,8 +437,13 @@ struct candidate_warning {
 enum rejection_reason_code {
   rr_none,
   rr_arity,
+  rr_explicit_conversion,
+  rr_template_conversion,
   rr_arg_conversion,
-  rr_bad_arg_conversion
+  rr_bad_arg_conversion,
+  rr_template_unification,
+  rr_template_instantiation,
+  rr_invalid_copy
 };
 
 struct conversion_info {
@@ -455,6 +471,24 @@ struct rejection_reason {
     struct conversion_info conversion;
     /* Same, but for bad argument conversions.  */
     struct conversion_info bad_conversion;
+    /* Information about template unification failures.  These are the
+       parameters passed to fn_type_unification.  */
+    struct {
+      tree tmpl;
+      tree explicit_targs;
+      tree targs;
+      const tree *args;
+      unsigned int nargs;
+      tree return_type;
+      unification_kind_t strict;
+      int flags;
+    } template_unification;
+    /* Information about template instantiation failures.  These are the
+       parameters passed to instantiate_template.  */
+    struct {
+      tree tmpl;
+      tree targs;
+    } template_instantiation;
   } u;
 };
 
@@ -513,33 +547,45 @@ null_ptr_cst_p (tree t)
     return true;
   if (CP_INTEGRAL_TYPE_P (TREE_TYPE (t)))
     {
-      if (cxx_dialect >= cxx0x)
-       {
-         t = fold_non_dependent_expr (t);
-         t = maybe_constant_value (t);
-         if (TREE_CONSTANT (t) && integer_zerop (t))
-           return true;
-       }
-      else
+      /* Core issue 903 says only literal 0 is a null pointer constant.  */
+      if (cxx_dialect < cxx0x)
        {
          t = integral_constant_value (t);
          STRIP_NOPS (t);
-         if (integer_zerop (t) && !TREE_OVERFLOW (t))
-           return true;
        }
+      if (integer_zerop (t) && !TREE_OVERFLOW (t))
+       return true;
     }
   return false;
 }
 
-/* Returns nonzero if PARMLIST consists of only default parms and/or
-   ellipsis.  */
+/* Returns true iff T is a null member pointer value (4.11).  */
+
+bool
+null_member_pointer_value_p (tree t)
+{
+  tree type = TREE_TYPE (t);
+  if (!type)
+    return false;
+  else if (TYPE_PTRMEMFUNC_P (type))
+    return (TREE_CODE (t) == CONSTRUCTOR
+           && integer_zerop (CONSTRUCTOR_ELT (t, 0)->value));
+  else if (TYPE_PTRMEM_P (type))
+    return integer_all_onesp (t);
+  else
+    return false;
+}
+
+/* Returns nonzero if PARMLIST consists of only default parms,
+   ellipsis, and/or undeduced parameter packs.  */
 
 bool
 sufficient_parms_p (const_tree parmlist)
 {
   for (; parmlist && parmlist != void_list_node;
        parmlist = TREE_CHAIN (parmlist))
-    if (!TREE_PURPOSE (parmlist))
+    if (!TREE_PURPOSE (parmlist)
+       && !PACK_EXPANSION_P (TREE_VALUE (parmlist)))
       return false;
   return true;
 }
@@ -604,6 +650,70 @@ bad_arg_conversion_rejection (tree first_arg, int n_arg, tree from, tree to)
   return r;
 }
 
+static struct rejection_reason *
+explicit_conversion_rejection (tree from, tree to)
+{
+  struct rejection_reason *r = alloc_rejection (rr_explicit_conversion);
+  r->u.conversion.n_arg = 0;
+  r->u.conversion.from_type = from;
+  r->u.conversion.to_type = to;
+  return r;
+}
+
+static struct rejection_reason *
+template_conversion_rejection (tree from, tree to)
+{
+  struct rejection_reason *r = alloc_rejection (rr_template_conversion);
+  r->u.conversion.n_arg = 0;
+  r->u.conversion.from_type = from;
+  r->u.conversion.to_type = to;
+  return r;
+}
+
+static struct rejection_reason *
+template_unification_rejection (tree tmpl, tree explicit_targs, tree targs,
+                               const tree *args, unsigned int nargs,
+                               tree return_type, unification_kind_t strict,
+                               int flags)
+{
+  size_t args_n_bytes = sizeof (*args) * nargs;
+  tree *args1 = (tree *) conversion_obstack_alloc (args_n_bytes);
+  struct rejection_reason *r = alloc_rejection (rr_template_unification);
+  r->u.template_unification.tmpl = tmpl;
+  r->u.template_unification.explicit_targs = explicit_targs;
+  r->u.template_unification.targs = targs;
+  /* Copy args to our own storage.  */
+  memcpy (args1, args, args_n_bytes);
+  r->u.template_unification.args = args1;
+  r->u.template_unification.nargs = nargs;
+  r->u.template_unification.return_type = return_type;
+  r->u.template_unification.strict = strict;
+  r->u.template_unification.flags = flags;
+  return r;
+}
+
+static struct rejection_reason *
+template_unification_error_rejection (void)
+{
+  return alloc_rejection (rr_template_unification);
+}
+
+static struct rejection_reason *
+template_instantiation_rejection (tree tmpl, tree targs)
+{
+  struct rejection_reason *r = alloc_rejection (rr_template_instantiation);
+  r->u.template_instantiation.tmpl = tmpl;
+  r->u.template_instantiation.targs = targs;
+  return r;
+}
+
+static struct rejection_reason *
+invalid_copy_with_fn_template_rejection (void)
+{
+  struct rejection_reason *r = alloc_rejection (rr_invalid_copy);
+  return r;
+}
+
 /* Dynamically allocate a conversion.  */
 
 static conversion *
@@ -799,6 +909,96 @@ build_aggr_conv (tree type, tree ctor, int flags)
   return c;
 }
 
+/* Represent a conversion from CTOR, a braced-init-list, to TYPE, an
+   array type, if such a conversion is possible.  */
+
+static conversion *
+build_array_conv (tree type, tree ctor, int flags)
+{
+  conversion *c;
+  unsigned HOST_WIDE_INT len = CONSTRUCTOR_NELTS (ctor);
+  tree elttype = TREE_TYPE (type);
+  unsigned i;
+  tree val;
+  bool bad = false;
+  bool user = false;
+  enum conversion_rank rank = cr_exact;
+
+  if (TYPE_DOMAIN (type))
+    {
+      unsigned HOST_WIDE_INT alen = tree_low_cst (array_type_nelts_top (type), 1);
+      if (alen < len)
+       return NULL;
+    }
+
+  FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), i, val)
+    {
+      conversion *sub
+       = implicit_conversion (elttype, TREE_TYPE (val), val,
+                              false, flags);
+      if (sub == NULL)
+       return NULL;
+
+      if (sub->rank > rank)
+       rank = sub->rank;
+      if (sub->user_conv_p)
+       user = true;
+      if (sub->bad_p)
+       bad = true;
+    }
+
+  c = alloc_conversion (ck_aggr);
+  c->type = type;
+  c->rank = rank;
+  c->user_conv_p = user;
+  c->bad_p = bad;
+  c->u.next = NULL;
+  return c;
+}
+
+/* Represent a conversion from CTOR, a braced-init-list, to TYPE, a
+   complex type, if such a conversion is possible.  */
+
+static conversion *
+build_complex_conv (tree type, tree ctor, int flags)
+{
+  conversion *c;
+  unsigned HOST_WIDE_INT len = CONSTRUCTOR_NELTS (ctor);
+  tree elttype = TREE_TYPE (type);
+  unsigned i;
+  tree val;
+  bool bad = false;
+  bool user = false;
+  enum conversion_rank rank = cr_exact;
+
+  if (len != 2)
+    return NULL;
+
+  FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), i, val)
+    {
+      conversion *sub
+       = implicit_conversion (elttype, TREE_TYPE (val), val,
+                              false, flags);
+      if (sub == NULL)
+       return NULL;
+
+      if (sub->rank > rank)
+       rank = sub->rank;
+      if (sub->user_conv_p)
+       user = true;
+      if (sub->bad_p)
+       bad = true;
+    }
+
+  c = alloc_conversion (ck_aggr);
+  c->type = type;
+  c->rank = rank;
+  c->user_conv_p = user;
+  c->bad_p = bad;
+  c->u.next = NULL;
+  return c;
+}
+
 /* Build a representation of the identity conversion from EXPR to
    itself.  The TYPE should match the type of EXPR, if EXPR is non-NULL.  */
 
@@ -897,6 +1097,8 @@ standard_conversion (tree to, tree from, tree expr, bool c_cast_p,
            }
        }
       conv = build_conv (ck_rvalue, from, conv);
+      if (flags & LOOKUP_PREFER_RVALUE)
+       conv->rvaluedness_matches_p = true;
     }
 
    /* Allow conversion between `__complex__' data types.  */
@@ -1051,8 +1253,8 @@ standard_conversion (tree to, tree from, tree expr, bool c_cast_p,
     {
       tree fromfn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from));
       tree tofn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to));
-      tree fbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fromfn)));
-      tree tbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (tofn)));
+      tree fbase = class_of_this_parm (fromfn);
+      tree tbase = class_of_this_parm (tofn);
 
       if (!DERIVED_FROM_P (fbase, tbase)
          || !same_type_p (TREE_TYPE (fromfn), TREE_TYPE (tofn))
@@ -1166,146 +1368,6 @@ reference_compatible_p (tree t1, tree t2)
          && at_least_as_qualified_p (t1, t2));
 }
 
-/* Determine whether or not the EXPR (of class type S) can be
-   converted to T as in [over.match.ref].  */
-
-static conversion *
-convert_class_to_reference (tree reference_type, tree s, tree expr, int flags)
-{
-  tree conversions;
-  tree first_arg;
-  conversion *conv;
-  tree t;
-  struct z_candidate *candidates;
-  struct z_candidate *cand;
-  bool any_viable_p;
-
-  if (!expr)
-    return NULL;
-
-  conversions = lookup_conversions (s);
-  if (!conversions)
-    return NULL;
-
-  /* [over.match.ref]
-
-     Assuming that "cv1 T" is the underlying type of the reference
-     being initialized, and "cv S" is the type of the initializer
-     expression, with S a class type, the candidate functions are
-     selected as follows:
-
-     --The conversion functions of S and its base classes are
-       considered.  Those that are not hidden within S and yield type
-       "reference to cv2 T2", where "cv1 T" is reference-compatible
-       (_dcl.init.ref_) with "cv2 T2", are candidate functions.
-
-     The argument list has one argument, which is the initializer
-     expression.  */
-
-  candidates = 0;
-
-  /* Conceptually, we should take the address of EXPR and put it in
-     the argument list.  Unfortunately, however, that can result in
-     error messages, which we should not issue now because we are just
-     trying to find a conversion operator.  Therefore, we use NULL,
-     cast to the appropriate type.  */
-  first_arg = build_int_cst (build_pointer_type (s), 0);
-
-  t = TREE_TYPE (reference_type);
-
-  /* We're performing a user-defined conversion to a desired type, so set
-     this for the benefit of add_candidates.  */
-  flags |= LOOKUP_NO_CONVERSION;
-
-  for (; conversions; conversions = TREE_CHAIN (conversions))
-    {
-      tree fns = TREE_VALUE (conversions);
-      tree binfo = TREE_PURPOSE (conversions);
-      struct z_candidate *old_candidates = candidates;;
-
-      add_candidates (fns, first_arg, NULL, reference_type,
-                     NULL_TREE, false,
-                     binfo, TYPE_BINFO (s),
-                     flags, &candidates);
-
-      for (cand = candidates; cand != old_candidates; cand = cand->next)
-       {
-         /* Now, see if the conversion function really returns
-            an lvalue of the appropriate type.  From the
-            point of view of unification, simply returning an
-            rvalue of the right type is good enough.  */
-         tree f = cand->fn;
-         tree t2 = TREE_TYPE (TREE_TYPE (f));
-         if (cand->viable == 0)
-           /* Don't bother looking more closely.  */;
-         else if (TREE_CODE (t2) != REFERENCE_TYPE
-                  || !reference_compatible_p (t, TREE_TYPE (t2)))
-           {
-             /* No need to set cand->reason here; this is most likely
-                an ambiguous match.  If it's not, either this candidate
-                will win, or we will have identified a reason for it
-                losing already.  */
-             cand->viable = 0;
-           }
-         else
-           {
-             conversion *identity_conv;
-             /* Build a standard conversion sequence indicating the
-                binding from the reference type returned by the
-                function to the desired REFERENCE_TYPE.  */
-             identity_conv
-               = build_identity_conv (TREE_TYPE (TREE_TYPE
-                                                 (TREE_TYPE (cand->fn))),
-                                      NULL_TREE);
-             cand->second_conv
-               = (direct_reference_binding
-                  (reference_type, identity_conv));
-             cand->second_conv->rvaluedness_matches_p
-               = TYPE_REF_IS_RVALUE (TREE_TYPE (TREE_TYPE (cand->fn)))
-                 == TYPE_REF_IS_RVALUE (reference_type);
-             cand->second_conv->bad_p |= cand->convs[0]->bad_p;
-
-              /* Don't allow binding of lvalues to rvalue references.  */
-              if (TYPE_REF_IS_RVALUE (reference_type)
-                  && !TYPE_REF_IS_RVALUE (TREE_TYPE (TREE_TYPE (cand->fn))))
-                cand->second_conv->bad_p = true;
-           }
-       }
-    }
-
-  candidates = splice_viable (candidates, pedantic, &any_viable_p);
-  /* If none of the conversion functions worked out, let our caller
-     know.  */
-  if (!any_viable_p)
-    return NULL;
-
-  cand = tourney (candidates);
-  if (!cand)
-    return NULL;
-
-  /* Now that we know that this is the function we're going to use fix
-     the dummy first argument.  */
-  gcc_assert (cand->first_arg == NULL_TREE
-             || integer_zerop (cand->first_arg));
-  cand->first_arg = build_this (expr);
-
-  /* Build a user-defined conversion sequence representing the
-     conversion.  */
-  conv = build_conv (ck_user,
-                    TREE_TYPE (TREE_TYPE (cand->fn)),
-                    build_identity_conv (TREE_TYPE (expr), expr));
-  conv->cand = cand;
-
-  if (cand->viable == -1)
-    conv->bad_p = true;
-
-  /* Merge it with the standard conversion sequence from the
-     conversion function's return type to the desired type.  */
-  cand->second_conv = merge_conversion_sequences (conv, cand->second_conv);
-
-  return cand->second_conv;
-}
-
 /* A reference of the indicated TYPE is being bound directly to the
    expression represented by the implicit conversion sequence CONV.
    Return a conversion sequence for this binding.  */
@@ -1364,7 +1426,8 @@ reference_binding (tree rto, tree rfrom, tree expr, bool c_cast_p, int flags)
   tree tfrom;
   bool related_p;
   bool compatible_p;
-  cp_lvalue_kind is_lvalue = clk_none;
+  cp_lvalue_kind gl_kind;
+  bool is_lvalue;
 
   if (TREE_CODE (to) == FUNCTION_TYPE && expr && type_unknown_p (expr))
     {
@@ -1374,13 +1437,6 @@ reference_binding (tree rto, tree rfrom, tree expr, bool c_cast_p, int flags)
       from = TREE_TYPE (expr);
     }
 
-  if (TREE_CODE (from) == REFERENCE_TYPE)
-    {
-      /* Anything with reference type is an lvalue.  */
-      is_lvalue = clk_ordinary;
-      from = TREE_TYPE (from);
-    }
-
   if (expr && BRACE_ENCLOSED_INITIALIZER_P (expr))
     {
       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
@@ -1396,11 +1452,28 @@ reference_binding (tree rto, tree rfrom, tree expr, bool c_cast_p, int flags)
        }
     }
 
-  if (is_lvalue == clk_none && expr)
-    is_lvalue = real_lvalue_p (expr);
+  if (TREE_CODE (from) == REFERENCE_TYPE)
+    {
+      from = TREE_TYPE (from);
+      if (!TYPE_REF_IS_RVALUE (rfrom)
+         || TREE_CODE (from) == FUNCTION_TYPE)
+       gl_kind = clk_ordinary;
+      else
+       gl_kind = clk_rvalueref;
+    }
+  else if (expr)
+    {
+      gl_kind = lvalue_kind (expr);
+      if (gl_kind & clk_class)
+       /* A class prvalue is not a glvalue.  */
+       gl_kind = clk_none;
+    }
+  else
+    gl_kind = clk_none;
+  is_lvalue = gl_kind && !(gl_kind & clk_rvalueref);
 
   tfrom = from;
-  if ((is_lvalue & clk_bitfield) != 0)
+  if ((gl_kind & clk_bitfield) != 0)
     tfrom = unlowered_expr_type (expr);
 
   /* Figure out whether or not the types are reference-related and
@@ -1418,14 +1491,16 @@ reference_binding (tree rto, tree rfrom, tree expr, bool c_cast_p, int flags)
      the reference and expression is an lvalue. In DR391, the wording in
      [8.5.3/5 dcl.init.ref] is changed to also require direct bindings for
      const and rvalue references to rvalues of compatible class type.
-     We should also do direct bindings for non-class "rvalues" derived from
-     rvalue references.  */
+     We should also do direct bindings for non-class xvalues.  */
   if (compatible_p
       && (is_lvalue
          || (((CP_TYPE_CONST_NON_VOLATILE_P (to)
-               && !(flags & LOOKUP_NO_TEMP_BIND))
+               && !(flags & LOOKUP_NO_RVAL_BIND))
               || TYPE_REF_IS_RVALUE (rto))
-             && (CLASS_TYPE_P (from) || (expr && lvalue_p (expr))))))
+             && (gl_kind
+                 || (!(flags & LOOKUP_NO_TEMP_BIND)
+                     && (CLASS_TYPE_P (from)
+                         || TREE_CODE (from) == ARRAY_TYPE))))))
     {
       /* [dcl.init.ref]
 
@@ -1450,12 +1525,16 @@ reference_binding (tree rto, tree rfrom, tree expr, bool c_cast_p, int flags)
        /* The top-level caller requested that we pretend that the lvalue
           be treated as an rvalue.  */
        conv->rvaluedness_matches_p = TYPE_REF_IS_RVALUE (rto);
+      else if (TREE_CODE (rfrom) == REFERENCE_TYPE)
+       /* Handle rvalue reference to function properly.  */
+       conv->rvaluedness_matches_p
+         = (TYPE_REF_IS_RVALUE (rto) == TYPE_REF_IS_RVALUE (rfrom));
       else
        conv->rvaluedness_matches_p 
           = (TYPE_REF_IS_RVALUE (rto) == !is_lvalue);
 
-      if ((is_lvalue & clk_bitfield) != 0
-         || ((is_lvalue & clk_packed) != 0 && !TYPE_PACKED (to)))
+      if ((gl_kind & clk_bitfield) != 0
+         || ((gl_kind & clk_packed) != 0 && !TYPE_PACKED (to)))
        /* For the purposes of overload resolution, we ignore the fact
           this expression is a bitfield or packed field. (In particular,
           [over.ics.ref] says specifically that a function with a
@@ -1469,8 +1548,10 @@ reference_binding (tree rto, tree rfrom, tree expr, bool c_cast_p, int flags)
           actually occurs.  */
        conv->need_temporary_p = true;
 
-      /* Don't allow binding of lvalues to rvalue references.  */
+      /* Don't allow binding of lvalues (other than function lvalues) to
+        rvalue references.  */
       if (is_lvalue && TYPE_REF_IS_RVALUE (rto)
+         && TREE_CODE (to) != FUNCTION_TYPE
           && !(flags & LOOKUP_PREFER_RVALUE))
        conv->bad_p = true;
 
@@ -1496,9 +1577,9 @@ reference_binding (tree rto, tree rfrom, tree expr, bool c_cast_p, int flags)
 
        the reference is bound to the lvalue result of the conversion
        in the second case.  */
-      conv = convert_class_to_reference (rto, from, expr, flags);
-      if (conv)
-       return conv;
+      z_candidate *cand = build_user_type_conversion_1 (rto, expr, flags);
+      if (cand)
+       return cand->second_conv;
     }
 
   /* From this point on, we conceptually need temporaries, even if we
@@ -1579,6 +1660,12 @@ implicit_conversion (tree to, tree from, tree expr, bool c_cast_p,
       || expr == error_mark_node)
     return NULL;
 
+  /* Other flags only apply to the primary function in overload
+     resolution, or after we've chosen one.  */
+  flags &= (LOOKUP_ONLYCONVERTING|LOOKUP_NO_CONVERSION|LOOKUP_COPY_PARM
+           |LOOKUP_NO_TEMP_BIND|LOOKUP_NO_RVAL_BIND|LOOKUP_PREFER_RVALUE
+           |LOOKUP_NO_NARROWING|LOOKUP_PROTECT);
+
   if (TREE_CODE (to) == REFERENCE_TYPE)
     conv = reference_binding (to, from, expr, c_cast_p, flags);
   else
@@ -1592,6 +1679,14 @@ implicit_conversion (tree to, tree from, tree expr, bool c_cast_p,
       if (is_std_init_list (to))
        return build_list_conv (to, expr, flags);
 
+      /* As an extension, allow list-initialization of _Complex.  */
+      if (TREE_CODE (to) == COMPLEX_TYPE)
+       {
+         conv = build_complex_conv (to, expr, flags);
+         if (conv)
+           return conv;
+       }
+
       /* Allow conversion from an initializer-list with one element to a
         scalar type.  */
       if (SCALAR_TYPE_P (to))
@@ -1617,6 +1712,8 @@ implicit_conversion (tree to, tree from, tree expr, bool c_cast_p,
              return conv;
            }
        }
+      else if (TREE_CODE (to) == ARRAY_TYPE)
+       return build_array_conv (to, expr, flags);
     }
 
   if (expr != NULL_TREE
@@ -1625,15 +1722,13 @@ implicit_conversion (tree to, tree from, tree expr, bool c_cast_p,
       && (flags & LOOKUP_NO_CONVERSION) == 0)
     {
       struct z_candidate *cand;
-      int convflags = (flags & (LOOKUP_NO_TEMP_BIND|LOOKUP_ONLYCONVERTING
-                               |LOOKUP_NO_NARROWING));
 
       if (CLASS_TYPE_P (to)
-         && !CLASSTYPE_NON_AGGREGATE (complete_type (to))
-         && BRACE_ENCLOSED_INITIALIZER_P (expr))
+         && BRACE_ENCLOSED_INITIALIZER_P (expr)
+         && !CLASSTYPE_NON_AGGREGATE (complete_type (to)))
        return build_aggr_conv (to, expr, flags);
 
-      cand = build_user_type_conversion_1 (to, expr, convflags);
+      cand = build_user_type_conversion_1 (to, expr, flags);
       if (cand)
        conv = cand->second_conv;
 
@@ -2466,6 +2561,12 @@ add_builtin_candidate (struct z_candidate **candidates, enum tree_code code,
         types are TYPE2.  */
       break;
 
+    case REALPART_EXPR:
+    case IMAGPART_EXPR:
+      if (ARITHMETIC_TYPE_P (type1))
+       break;
+      return;
     default:
       gcc_unreachable ();
     }
@@ -2499,8 +2600,6 @@ type_decays_to (tree type)
     return build_pointer_type (TREE_TYPE (type));
   if (TREE_CODE (type) == FUNCTION_TYPE)
     return build_pointer_type (type);
-  if (!MAYBE_CLASS_TYPE_P (type))
-    type = cv_unqualified (type);
   return type;
 }
 
@@ -2634,7 +2733,7 @@ add_builtin_candidates (struct z_candidate **candidates, enum tree_code code,
              type = non_reference (type);
              if (i != 0 || ! ref1)
                {
-                 type = TYPE_MAIN_VARIANT (type_decays_to (type));
+                 type = cv_unqualified (type_decays_to (type));
                  if (enum_p && TREE_CODE (type) == ENUMERAL_TYPE)
                    VEC_safe_push (tree, gc, types[i], type);
                  if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
@@ -2653,7 +2752,7 @@ add_builtin_candidates (struct z_candidate **candidates, enum tree_code code,
          type = non_reference (argtypes[i]);
          if (i != 0 || ! ref1)
            {
-             type = TYPE_MAIN_VARIANT (type_decays_to (type));
+             type = cv_unqualified (type_decays_to (type));
              if (enum_p && UNSCOPED_ENUM_P (type))
                VEC_safe_push (tree, gc, types[i], type);
              if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
@@ -2717,6 +2816,7 @@ add_template_candidate_real (struct z_candidate **candidates, tree tmpl,
   int i;
   tree fn;
   struct rejection_reason *reason = NULL;
+  int errs;
 
   /* We don't do deduction on the in-charge parameter, the VTT
      parameter or 'this'.  */
@@ -2759,17 +2859,31 @@ add_template_candidate_real (struct z_candidate **candidates, tree tmpl,
     }
   gcc_assert (ia == nargs_without_in_chrg);
 
+  errs = errorcount+sorrycount;
   i = fn_type_unification (tmpl, explicit_targs, targs,
                           args_without_in_chrg,
                           nargs_without_in_chrg,
-                          return_type, strict, flags);
+                          return_type, strict, flags, false);
 
   if (i != 0)
-    goto fail;
+    {
+      /* Don't repeat unification later if it already resulted in errors.  */
+      if (errorcount+sorrycount == errs)
+       reason = template_unification_rejection (tmpl, explicit_targs,
+                                                targs, args_without_in_chrg,
+                                                nargs_without_in_chrg,
+                                                return_type, strict, flags);
+      else
+       reason = template_unification_error_rejection ();
+      goto fail;
+    }
 
   fn = instantiate_template (tmpl, targs, tf_none);
   if (fn == error_mark_node)
-    goto fail;
+    {
+      reason = template_instantiation_rejection (tmpl, targs);
+      goto fail;
+    }
 
   /* In [class.copy]:
 
@@ -2798,7 +2912,10 @@ add_template_candidate_real (struct z_candidate **candidates, tree tmpl,
       tree arg_types = FUNCTION_FIRST_USER_PARMTYPE (fn);
       if (arg_types && same_type_p (TYPE_MAIN_VARIANT (TREE_VALUE (arg_types)),
                                    ctype))
-       goto fail;
+       {
+         reason = invalid_copy_with_fn_template_rejection ();
+         goto fail;
+       }
     }
 
   if (obj != NULL_TREE)
@@ -2882,6 +2999,11 @@ splice_viable (struct z_candidate *cands,
   struct z_candidate **last_viable;
   struct z_candidate **cand;
 
+  /* Be strict inside templates, since build_over_call won't actually
+     do the conversions to get pedwarns.  */
+  if (processing_template_decl)
+    strict_p = true;
+
   viable = NULL;
   last_viable = &viable;
   *any_viable_p = false;
@@ -2961,6 +3083,18 @@ print_conversion_rejection (location_t loc, struct conversion_info *info)
            info->n_arg+1, info->from_type, info->to_type);
 }
 
+/* Print information about a candidate with WANT parameters and we found
+   HAVE.  */
+
+static void
+print_arity_information (location_t loc, unsigned int have, unsigned int want)
+{
+  inform_n (loc, want,
+           "  candidate expects %d argument, %d provided",
+           "  candidate expects %d arguments, %d provided",
+           want, have);
+}
+
 /* Print information about one overload candidate CANDIDATE.  MSGSTR
    is the text to print before the candidate itself.
 
@@ -3007,10 +3141,8 @@ print_z_candidate (const char *msgstr, struct z_candidate *candidate)
       switch (r->code)
        {
        case rr_arity:
-         inform_n (loc, r->u.arity.expected,
-                   "  candidate expects %d argument, %d provided",
-                   "  candidate expects %d arguments, %d provided",
-                   r->u.arity.expected, r->u.arity.actual);
+         print_arity_information (loc, r->u.arity.actual,
+                                  r->u.arity.expected);
          break;
        case rr_arg_conversion:
          print_conversion_rejection (loc, &r->u.conversion);
@@ -3018,6 +3150,51 @@ print_z_candidate (const char *msgstr, struct z_candidate *candidate)
        case rr_bad_arg_conversion:
          print_conversion_rejection (loc, &r->u.bad_conversion);
          break;
+       case rr_explicit_conversion:
+         inform (loc, "  return type %qT of explicit conversion function "
+                 "cannot be converted to %qT with a qualification "
+                 "conversion", r->u.conversion.from_type,
+                 r->u.conversion.to_type);
+         break;
+       case rr_template_conversion:
+         inform (loc, "  conversion from return type %qT of template "
+                 "conversion function specialization to %qT is not an "
+                 "exact match", r->u.conversion.from_type,
+                 r->u.conversion.to_type);
+         break;
+       case rr_template_unification:
+         /* We use template_unification_error_rejection if unification caused
+            actual non-SFINAE errors, in which case we don't need to repeat
+            them here.  */
+         if (r->u.template_unification.tmpl == NULL_TREE)
+           {
+             inform (loc, "  substitution of deduced template arguments "
+                     "resulted in errors seen above");
+             break;
+           }
+         /* Re-run template unification with diagnostics.  */
+         inform (loc, "  template argument deduction/substitution failed:");
+         fn_type_unification (r->u.template_unification.tmpl,
+                              r->u.template_unification.explicit_targs,
+                              r->u.template_unification.targs,
+                              r->u.template_unification.args,
+                              r->u.template_unification.nargs,
+                              r->u.template_unification.return_type,
+                              r->u.template_unification.strict,
+                              r->u.template_unification.flags,
+                              true);
+         break;
+       case rr_template_instantiation:
+         /* Re-run template instantiation with diagnostics.  */
+         instantiate_template (r->u.template_instantiation.tmpl,
+                               r->u.template_instantiation.targs,
+                               tf_warning_or_error);
+         break;
+       case rr_invalid_copy:
+         inform (loc,
+                 "  a constructor taking a single argument of its own "
+                 "class type is invalid");
+         break;
        case rr_none:
        default:
          /* This candidate didn't have any issues or we failed to
@@ -3092,21 +3269,23 @@ static conversion *
 merge_conversion_sequences (conversion *user_seq, conversion *std_seq)
 {
   conversion **t;
+  bool bad = user_seq->bad_p;
 
   gcc_assert (user_seq->kind == ck_user);
 
   /* Find the end of the second conversion sequence.  */
-  t = &(std_seq);
-  while ((*t)->kind != ck_identity)
-    t = &((*t)->u.next);
+  for (t = &std_seq; (*t)->kind != ck_identity; t = &((*t)->u.next))
+    {
+      /* The entire sequence is a user-conversion sequence.  */
+      (*t)->user_conv_p = true;
+      if (bad)
+       (*t)->bad_p = true;
+    }
 
   /* Replace the identity conversion with the user conversion
      sequence.  */
   *t = user_seq;
 
-  /* The entire sequence is a user-conversion sequence.  */
-  std_seq->user_conv_p = true;
-
   return std_seq;
 }
 
@@ -3172,7 +3351,7 @@ add_list_candidates (tree fns, tree first_arg,
 /* Returns the best overload candidate to perform the requested
    conversion.  This function is used for three the overloading situations
    described in [over.match.copy], [over.match.conv], and [over.match.ref].
-   If TOTYPE is a REFERENCE_TYPE, we're trying to find an lvalue binding as
+   If TOTYPE is a REFERENCE_TYPE, we're trying to find a direct binding as
    per [dcl.init.ref], so we ignore temporary bindings.  */
 
 static struct z_candidate *
@@ -3195,7 +3374,9 @@ build_user_type_conversion_1 (tree totype, tree expr, int flags)
              || !DERIVED_FROM_P (totype, fromtype));
 
   if (MAYBE_CLASS_TYPE_P (totype))
-    ctors = lookup_fnfields (totype, complete_ctor_identifier, 0);
+    /* Use lookup_fnfields_slot instead of lookup_fnfields to avoid
+       creating a garbage BASELINK; constructors can't be inherited.  */
+    ctors = lookup_fnfields_slot (totype, complete_ctor_identifier);
 
   if (MAYBE_CLASS_TYPE_P (fromtype))
     {
@@ -3227,7 +3408,6 @@ build_user_type_conversion_1 (tree totype, tree expr, int flags)
   if (ctors)
     {
       int ctorflags = flags;
-      ctors = BASELINK_FUNCTIONS (ctors);
 
       first_arg = build_int_cst (build_pointer_type (totype), 0);
 
@@ -3279,8 +3459,8 @@ build_user_type_conversion_1 (tree totype, tree expr, int flags)
       struct z_candidate *old_candidates;
 
       /* If we are called to convert to a reference type, we are trying to
-        find an lvalue binding, so don't even consider temporaries.  If
-        we don't find an lvalue binding, the caller will try again to
+        find a direct binding, so don't even consider temporaries.  If
+        we don't find a direct binding, the caller will try again to
         look for a temporary binding.  */
       if (TREE_CODE (totype) == REFERENCE_TYPE)
        convflags |= LOOKUP_NO_TEMP_BIND;
@@ -3293,9 +3473,10 @@ build_user_type_conversion_1 (tree totype, tree expr, int flags)
 
       for (cand = candidates; cand != old_candidates; cand = cand->next)
        {
+         tree rettype = TREE_TYPE (TREE_TYPE (cand->fn));
          conversion *ics
            = implicit_conversion (totype,
-                                  TREE_TYPE (TREE_TYPE (cand->fn)),
+                                  rettype,
                                   0,
                                   /*c_cast_p=*/false, convflags);
 
@@ -3317,25 +3498,50 @@ build_user_type_conversion_1 (tree totype, tree expr, int flags)
 
          if (!ics)
            {
-             tree rettype = TREE_TYPE (TREE_TYPE (cand->fn));
              cand->viable = 0;
              cand->reason = arg_conversion_rejection (NULL_TREE, -1,
                                                       rettype, totype);
            }
+         else if (DECL_NONCONVERTING_P (cand->fn)
+                  && ics->rank > cr_exact)
+           {
+             /* 13.3.1.5: For direct-initialization, those explicit
+                conversion functions that are not hidden within S and
+                yield type T or a type that can be converted to type T
+                with a qualification conversion (4.4) are also candidate
+                functions.  */
+             /* 13.3.1.6 doesn't have a parallel restriction, but it should;
+                I've raised this issue with the committee. --jason 9/2011 */
+             cand->viable = -1;
+             cand->reason = explicit_conversion_rejection (rettype, totype);
+           }
          else if (cand->viable == 1 && ics->bad_p)
            {
-             tree rettype = TREE_TYPE (TREE_TYPE (cand->fn));
              cand->viable = -1;
              cand->reason
                = bad_arg_conversion_rejection (NULL_TREE, -1,
                                                rettype, totype);
            }
+         else if (primary_template_instantiation_p (cand->fn)
+                  && ics->rank > cr_exact)
+           {
+             /* 13.3.3.1.2: If the user-defined conversion is specified by
+                a specialization of a conversion function template, the
+                second standard conversion sequence shall have exact match
+                rank.  */
+             cand->viable = -1;
+             cand->reason = template_conversion_rejection (rettype, totype);
+           }
        }
     }
 
   candidates = splice_viable (candidates, pedantic, &any_viable_p);
   if (!any_viable_p)
-    return NULL;
+    {
+      if (args)
+       release_tree_vector (args);
+      return NULL;
+    }
 
   cand = tourney (candidates);
   if (cand == 0)
@@ -3366,6 +3572,8 @@ build_user_type_conversion_1 (tree totype, tree expr, int flags)
       ? totype : non_reference (TREE_TYPE (TREE_TYPE (cand->fn)))),
      build_identity_conv (TREE_TYPE (expr), expr));
   conv->cand = cand;
+  if (cand->viable == -1)
+    conv->bad_p = true;
 
   /* Remember that this was a list-initialization.  */
   if (flags & LOOKUP_NO_NARROWING)
@@ -3375,26 +3583,35 @@ build_user_type_conversion_1 (tree totype, tree expr, int flags)
   cand->second_conv = merge_conversion_sequences (conv,
                                                  cand->second_conv);
 
-  if (cand->viable == -1)
-    cand->second_conv->bad_p = true;
-
   return cand;
 }
 
+/* Wrapper for above. */
+
 tree
 build_user_type_conversion (tree totype, tree expr, int flags)
 {
-  struct z_candidate *cand
-    = build_user_type_conversion_1 (totype, expr, flags);
+  struct z_candidate *cand;
+  tree ret;
+
+  bool subtime = timevar_cond_start (TV_OVERLOAD);
+  cand = build_user_type_conversion_1 (totype, expr, flags);
 
   if (cand)
     {
       if (cand->second_conv->kind == ck_ambig)
-       return error_mark_node;
-      expr = convert_like (cand->second_conv, expr, tf_warning_or_error);
-      return convert_from_reference (expr);
+       ret = error_mark_node;
+      else
+        {
+          expr = convert_like (cand->second_conv, expr, tf_warning_or_error);
+          ret = convert_from_reference (expr);
+        }
     }
-  return NULL_TREE;
+  else
+    ret = NULL_TREE;
+
+  timevar_cond_stop (TV_OVERLOAD, subtime);
+  return ret;
 }
 
 /* Subroutine of convert_nontype_argument.
@@ -3470,7 +3687,7 @@ build_integral_nontype_arg_conv (tree type, tree expr, tsubst_flags_t complain)
 /* Do any initial processing on the arguments to a function call.  */
 
 static VEC(tree,gc) *
-resolve_args (VEC(tree,gc) *args)
+resolve_args (VEC(tree,gc) *args, tsubst_flags_t complain)
 {
   unsigned int ix;
   tree arg;
@@ -3481,7 +3698,8 @@ resolve_args (VEC(tree,gc) *args)
        return NULL;
       else if (VOID_TYPE_P (TREE_TYPE (arg)))
        {
-         error ("invalid use of void expression");
+         if (complain & tf_error)
+           error ("invalid use of void expression");
          return NULL;
        }
       else if (invalid_nonstatic_memfn_p (arg, tf_warning_or_error))
@@ -3509,8 +3727,13 @@ perform_overload_resolution (tree fn,
                             bool *any_viable_p)
 {
   struct z_candidate *cand;
-  tree explicit_targs = NULL_TREE;
-  int template_only = 0;
+  tree explicit_targs;
+  int template_only;
+
+  bool subtime = timevar_cond_start (TV_OVERLOAD);
+
+  explicit_targs = NULL_TREE;
+  template_only = 0;
 
   *candidates = NULL;
   *any_viable_p = true;
@@ -3537,10 +3760,12 @@ perform_overload_resolution (tree fn,
                  candidates);
 
   *candidates = splice_viable (*candidates, pedantic, any_viable_p);
-  if (!*any_viable_p)
-    return NULL;
+  if (*any_viable_p)
+    cand = tourney (*candidates);
+  else
+    cand = NULL;
 
-  cand = tourney (*candidates);
+  timevar_cond_stop (TV_OVERLOAD, subtime);
   return cand;
 }
 
@@ -3581,7 +3806,7 @@ build_new_function_call (tree fn, VEC(tree,gc) **args, bool koenig_p,
 
   if (args != NULL && *args != NULL)
     {
-      *args = resolve_args (*args);
+      *args = resolve_args (*args, complain);
       if (*args == NULL)
        return error_mark_node;
     }
@@ -3621,7 +3846,16 @@ build_new_function_call (tree fn, VEC(tree,gc) **args, bool koenig_p,
       result = error_mark_node;
     }
   else
-    result = build_over_call (cand, LOOKUP_NORMAL, complain);
+    {
+      int flags = LOOKUP_NORMAL;
+      /* If fn is template_id_expr, the call has explicit template arguments
+         (e.g. func<int>(5)), communicate this info to build_over_call
+         through flags so that later we can use it to decide whether to warn
+         about peculiar null pointer conversion.  */
+      if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
+        flags |= LOOKUP_EXPLICIT_TMPL_ARGS;
+      result = build_over_call (cand, flags, complain);
+    }
 
   /* Free all the conversions we allocated.  */
   obstack_free (&conversion_obstack, p);
@@ -3652,7 +3886,7 @@ build_operator_new_call (tree fnname, VEC(tree,gc) **args,
   if (fn)
     *fn = NULL_TREE;
   VEC_safe_insert (tree, gc, *args, 0, *size);
-  *args = resolve_args (*args);
+  *args = resolve_args (*args, tf_warning_or_error);
   if (*args == NULL)
     return error_mark_node;
 
@@ -3730,8 +3964,8 @@ build_operator_new_call (tree fnname, VEC(tree,gc) **args,
 
 /* Build a new call to operator().  This may change ARGS.  */
 
-tree
-build_op_call (tree obj, VEC(tree,gc) **args, tsubst_flags_t complain)
+static tree
+build_op_call_1 (tree obj, VEC(tree,gc) **args, tsubst_flags_t complain)
 {
   struct z_candidate *candidates = 0, *cand;
   tree fns, convs, first_mem_arg = NULL_TREE;
@@ -3765,7 +3999,7 @@ build_op_call (tree obj, VEC(tree,gc) **args, tsubst_flags_t complain)
 
   if (args != NULL && *args != NULL)
     {
-      *args = resolve_args (*args);
+      *args = resolve_args (*args, complain);
       if (*args == NULL)
        return error_mark_node;
     }
@@ -3862,6 +4096,18 @@ build_op_call (tree obj, VEC(tree,gc) **args, tsubst_flags_t complain)
   return result;
 }
 
+/* Wrapper for above.  */
+
+tree
+build_op_call (tree obj, VEC(tree,gc) **args, tsubst_flags_t complain)
+{
+  tree ret;
+  bool subtime = timevar_cond_start (TV_OVERLOAD);
+  ret = build_op_call_1 (obj, args, complain);
+  timevar_cond_stop (TV_OVERLOAD, subtime);
+  return ret;
+}
+
 static void
 op_error (enum tree_code code, enum tree_code code2,
          tree arg1, tree arg2, tree arg3, bool match)
@@ -3946,16 +4192,17 @@ conditional_conversion (tree e1, tree e2)
   /* [expr.cond]
 
      If E2 is an lvalue: E1 can be converted to match E2 if E1 can be
-     implicitly converted (clause _conv_) to the type "reference to
+     implicitly converted (clause _conv_) to the type "lvalue reference to
      T2", subject to the constraint that in the conversion the
-     reference must bind directly (_dcl.init.ref_) to E1.  */
+     reference must bind directly (_dcl.init.ref_) to an lvalue.  */
   if (real_lvalue_p (e2))
     {
       conv = implicit_conversion (build_reference_type (t2),
                                  t1,
                                  e1,
                                  /*c_cast_p=*/false,
-                                 LOOKUP_NO_TEMP_BIND|LOOKUP_ONLYCONVERTING);
+                                 LOOKUP_NO_TEMP_BIND|LOOKUP_NO_RVAL_BIND
+                                 |LOOKUP_ONLYCONVERTING);
       if (conv)
        return conv;
     }
@@ -3999,9 +4246,9 @@ conditional_conversion (tree e1, tree e2)
 /* Implement [expr.cond].  ARG1, ARG2, and ARG3 are the three
    arguments to the conditional expression.  */
 
-tree
-build_conditional_expr (tree arg1, tree arg2, tree arg3,
-                        tsubst_flags_t complain)
+static tree
+build_conditional_expr_1 (tree arg1, tree arg2, tree arg3,
+                          tsubst_flags_t complain)
 {
   tree arg2_type;
   tree arg3_type;
@@ -4082,7 +4329,11 @@ build_conditional_expr (tree arg1, tree arg2, tree arg3,
          && TREE_CODE (arg3) != THROW_EXPR)
        {
          if (!VOID_TYPE_P (arg3_type))
-           arg3 = force_rvalue (arg3);
+           {
+             arg3 = force_rvalue (arg3, complain);
+             if (arg3 == error_mark_node)
+               return error_mark_node;
+           }
          arg3_type = TREE_TYPE (arg3);
          result_type = arg3_type;
        }
@@ -4090,7 +4341,11 @@ build_conditional_expr (tree arg1, tree arg2, tree arg3,
               && TREE_CODE (arg3) == THROW_EXPR)
        {
          if (!VOID_TYPE_P (arg2_type))
-           arg2 = force_rvalue (arg2);
+           {
+             arg2 = force_rvalue (arg2, complain);
+             if (arg2 == error_mark_node)
+               return error_mark_node;
+           }
          arg2_type = TREE_TYPE (arg2);
          result_type = arg2_type;
        }
@@ -4297,11 +4552,11 @@ build_conditional_expr (tree arg1, tree arg2, tree arg3,
      that isn't wrapped with a TARGET_EXPR plays havoc with exception
      regions.  */
 
-  arg2 = force_rvalue (arg2);
+  arg2 = force_rvalue (arg2, complain);
   if (!CLASS_TYPE_P (arg2_type))
     arg2_type = TREE_TYPE (arg2);
 
-  arg3 = force_rvalue (arg3);
+  arg3 = force_rvalue (arg3, complain);
   if (!CLASS_TYPE_P (arg3_type))
     arg3_type = TREE_TYPE (arg3);
 
@@ -4422,6 +4677,19 @@ build_conditional_expr (tree arg1, tree arg2, tree arg3,
   return result;
 }
 
+/* Wrapper for above.  */
+
+tree
+build_conditional_expr (tree arg1, tree arg2, tree arg3,
+                        tsubst_flags_t complain)
+{
+  tree ret;
+  bool subtime = timevar_cond_start (TV_OVERLOAD);
+  ret = build_conditional_expr_1 (arg1, arg2, arg3, complain);
+  timevar_cond_stop (TV_OVERLOAD, subtime);
+  return ret;
+}
+
 /* OPERAND is an operand to an expression.  Perform necessary steps
    required before using it.  If OPERAND is NULL_TREE, NULL_TREE is
    returned.  */
@@ -4590,9 +4858,9 @@ avoid_sign_compare_warnings (tree orig_arg, tree arg)
     TREE_NO_WARNING (arg) = 1;
 }
 
-tree
-build_new_op (enum tree_code code, int flags, tree arg1, tree arg2, tree arg3,
-             bool *overloaded_p, tsubst_flags_t complain)
+static tree
+build_new_op_1 (enum tree_code code, int flags, tree arg1, tree arg2, tree arg3,
+               tree *overload, tsubst_flags_t complain)
 {
   tree orig_arg1 = arg1;
   tree orig_arg2 = arg2;
@@ -4758,8 +5026,8 @@ build_new_op (enum tree_code code, int flags, tree arg1, tree arg2, tree arg3,
            code = PREINCREMENT_EXPR;
          else
            code = PREDECREMENT_EXPR;
-         result = build_new_op (code, flags, arg1, NULL_TREE, NULL_TREE,
-                                overloaded_p, complain);
+         result = build_new_op_1 (code, flags, arg1, NULL_TREE, NULL_TREE,
+                                  overload, complain);
          break;
 
          /* The caller will deal with these.  */
@@ -4806,10 +5074,10 @@ build_new_op (enum tree_code code, int flags, tree arg1, tree arg2, tree arg3,
        }
       else if (TREE_CODE (cand->fn) == FUNCTION_DECL)
        {
-         if (overloaded_p)
-           *overloaded_p = true;
+         if (overload)
+           *overload = cand->fn;
 
-         if (resolve_args (arglist) == NULL)
+         if (resolve_args (arglist, complain) == NULL)
            result = error_mark_node;
          else
            result = build_over_call (cand, LOOKUP_NORMAL, complain);
@@ -4962,6 +5230,19 @@ build_new_op (enum tree_code code, int flags, tree arg1, tree arg2, tree arg3,
   return NULL_TREE;
 }
 
+/* Wrapper for above.  */
+
+tree
+build_new_op (enum tree_code code, int flags, tree arg1, tree arg2, tree arg3,
+             tree *overload, tsubst_flags_t complain)
+{
+  tree ret;
+  bool subtime = timevar_cond_start (TV_OVERLOAD);
+  ret = build_new_op_1 (code, flags, arg1, arg2, arg3, overload, complain);
+  timevar_cond_stop (TV_OVERLOAD, subtime);
+  return ret;
+}
+
 /* Returns true iff T, an element of an OVERLOAD chain, is a usual
    deallocation function (3.7.4.2 [basic.stc.dynamic.deallocation]).  */
 
@@ -5248,10 +5529,16 @@ conversion_null_warnings (tree totype, tree expr, tree fn, int argnum)
     }
 
   /* Issue warnings if "false" is converted to a NULL pointer */
-  else if (expr == boolean_false_node && fn && POINTER_TYPE_P (t))
-    warning_at (input_location, OPT_Wconversion_null,
-               "converting %<false%> to pointer type for argument %P of %qD",
-               argnum, fn);
+  else if (expr == boolean_false_node && POINTER_TYPE_P (t))
+    {
+      if (fn)
+       warning_at (input_location, OPT_Wconversion_null,
+                   "converting %<false%> to pointer type for argument %P "
+                   "of %qD", argnum, fn);
+      else
+       warning_at (input_location, OPT_Wconversion_null,
+                   "converting %<false%> to pointer type %qT", t);
+    }
 }
 
 /* Perform the conversions in CONVS on the expression EXPR.  FN and
@@ -5273,11 +5560,15 @@ convert_like_real (conversion *convs, tree expr, tree fn, int argnum,
   diagnostic_t diag_kind;
   int flags;
 
+  if (convs->bad_p && !(complain & tf_error))
+    return error_mark_node;
+
   if (convs->bad_p
       && convs->kind != ck_user
       && convs->kind != ck_list
       && convs->kind != ck_ambig
-      && convs->kind != ck_ref_bind
+      && (convs->kind != ck_ref_bind
+         || convs->user_conv_p)
       && convs->kind != ck_rvalue
       && convs->kind != ck_base)
     {
@@ -5290,9 +5581,24 @@ convert_like_real (conversion *convs, tree expr, tree fn, int argnum,
          && BRACE_ENCLOSED_INITIALIZER_P (CONSTRUCTOR_ELT (expr, 0)->value))
        permerror (input_location, "too many braces around initializer for %qT", totype);
 
-      for (; t; t = convs->u.next)
+      for (; t; t = t->u.next)
        {
-         if (t->kind == ck_user || !t->bad_p)
+         if (t->kind == ck_user && t->cand->reason)
+           {
+             permerror (input_location, "invalid user-defined conversion "
+                        "from %qT to %qT", TREE_TYPE (expr), totype);
+             print_z_candidate ("candidate is:", t->cand);
+             expr = convert_like_real (t, expr, fn, argnum, 1,
+                                       /*issue_conversion_warnings=*/false,
+                                       /*c_cast_p=*/false,
+                                       complain);
+             if (convs->kind == ck_ref_bind)
+               return convert_to_reference (totype, expr, CONV_IMPLICIT,
+                                            LOOKUP_NORMAL, NULL_TREE);
+             else
+               return cp_convert (totype, expr);
+           }
+         else if (t->kind == ck_user || !t->bad_p)
            {
              expr = convert_like_real (t, expr, fn, argnum, 1,
                                        /*issue_conversion_warnings=*/false,
@@ -5308,15 +5614,12 @@ convert_like_real (conversion *convs, tree expr, tree fn, int argnum,
          else if (t->kind == ck_identity)
            break;
        }
-      if (complain & tf_error)
-       {
-         permerror (input_location, "invalid conversion from %qT to %qT", TREE_TYPE (expr), totype);
-         if (fn)
-           permerror (DECL_SOURCE_LOCATION (fn),
-                      "  initializing argument %P of %qD", argnum, fn);
-       }
-      else
-       return error_mark_node;
+
+      permerror (input_location, "invalid conversion from %qT to %qT",
+                TREE_TYPE (expr), totype);
+      if (fn)
+       permerror (DECL_SOURCE_LOCATION (fn),
+                  "  initializing argument %P of %qD", argnum, fn);
 
       return cp_convert (totype, expr);
     }
@@ -5332,20 +5635,32 @@ convert_like_real (conversion *convs, tree expr, tree fn, int argnum,
        tree convfn = cand->fn;
        unsigned i;
 
+       /* If we're initializing from {}, it's value-initialization.  */
+       if (BRACE_ENCLOSED_INITIALIZER_P (expr)
+           && CONSTRUCTOR_NELTS (expr) == 0
+           && TYPE_HAS_DEFAULT_CONSTRUCTOR (totype))
+         {
+           expr = build_value_init (totype, complain);
+           expr = get_target_expr_sfinae (expr, complain);
+           if (expr != error_mark_node)
+             TARGET_EXPR_LIST_INIT_P (expr) = true;
+           return expr;
+         }
+
        expr = mark_rvalue_use (expr);
 
        /* When converting from an init list we consider explicit
           constructors, but actually trying to call one is an error.  */
        if (DECL_NONCONVERTING_P (convfn) && DECL_CONSTRUCTOR_P (convfn)
+           /* Unless this is for direct-list-initialization.  */
+           && !(BRACE_ENCLOSED_INITIALIZER_P (expr)
+                && CONSTRUCTOR_IS_DIRECT_INIT (expr))
            /* Unless we're calling it for value-initialization from an
               empty list, since that is handled separately in 8.5.4.  */
            && cand->num_convs > 0)
          {
-           if (complain & tf_error)
-             error ("converting to %qT from initializer list would use "
-                    "explicit constructor %qD", totype, convfn);
-           else
-             return error_mark_node;
+           error ("converting to %qT from initializer list would use "
+                  "explicit constructor %qD", totype, convfn);
          }
 
        /* Set user_conv_p on the argument conversions, so rvalue/base
@@ -5359,10 +5674,10 @@ convert_like_real (conversion *convs, tree expr, tree fn, int argnum,
           we need to build up a TARGET_EXPR.  */
        if (DECL_CONSTRUCTOR_P (convfn))
          {
-           expr = build_cplus_new (totype, expr);
+           expr = build_cplus_new (totype, expr, complain);
 
            /* Remember that this was list-initialization.  */
-           if (convs->check_narrowing)
+           if (convs->check_narrowing && expr != error_mark_node)
              TARGET_EXPR_LIST_INIT_P (expr) = true;
          }
 
@@ -5374,7 +5689,7 @@ convert_like_real (conversion *convs, tree expr, tree fn, int argnum,
        {
          int nelts = CONSTRUCTOR_NELTS (expr);
          if (nelts == 0)
-           expr = build_value_init (totype, tf_warning_or_error);
+           expr = build_value_init (totype, complain);
          else if (nelts == 1)
            expr = CONSTRUCTOR_ELT (expr, 0)->value;
          else
@@ -5397,6 +5712,9 @@ convert_like_real (conversion *convs, tree expr, tree fn, int argnum,
         }
       return expr;
     case ck_ambig:
+      /* We leave bad_p off ck_ambig because overload resolution considers
+        it valid, it just fails when we try to perform it.  So we need to
+         check complain here, too.  */
       if (complain & tf_error)
        {
          /* Call build_user_type_conversion again for the error.  */
@@ -5412,8 +5730,8 @@ convert_like_real (conversion *convs, tree expr, tree fn, int argnum,
        tree elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (totype), 0);
        tree new_ctor = build_constructor (init_list_type_node, NULL);
        unsigned len = CONSTRUCTOR_NELTS (expr);
-       tree array, val;
-       VEC(tree,gc) *parms;
+       tree array, val, field;
+       VEC(constructor_elt,gc) *vec = NULL;
        unsigned ix;
 
        /* Convert all the elements.  */
@@ -5426,27 +5744,38 @@ convert_like_real (conversion *convs, tree expr, tree fn, int argnum,
            if (!BRACE_ENCLOSED_INITIALIZER_P (val))
              check_narrowing (TREE_TYPE (sub), val);
            CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_ctor), NULL_TREE, sub);
+           if (!TREE_CONSTANT (sub))
+             TREE_CONSTANT (new_ctor) = false;
          }
        /* Build up the array.  */
        elttype = cp_build_qualified_type
          (elttype, cp_type_quals (elttype) | TYPE_QUAL_CONST);
        array = build_array_of_n_type (elttype, len);
-       array = finish_compound_literal (array, new_ctor);
-
-       parms = make_tree_vector ();
-       VEC_safe_push (tree, gc, parms, decay_conversion (array));
-       VEC_safe_push (tree, gc, parms, size_int (len));
-       /* Call the private constructor.  */
-       push_deferring_access_checks (dk_no_check);
-       new_ctor = build_special_member_call
-         (NULL_TREE, complete_ctor_identifier, &parms, totype, 0, complain);
-       release_tree_vector (parms);
-       pop_deferring_access_checks ();
-       return build_cplus_new (totype, new_ctor);
+       array = finish_compound_literal (array, new_ctor, complain);
+
+       /* Build up the initializer_list object.  */
+       totype = complete_type (totype);
+       field = next_initializable_field (TYPE_FIELDS (totype));
+       CONSTRUCTOR_APPEND_ELT (vec, field, decay_conversion (array));
+       field = next_initializable_field (DECL_CHAIN (field));
+       CONSTRUCTOR_APPEND_ELT (vec, field, size_int (len));
+       new_ctor = build_constructor (totype, vec);
+       return get_target_expr (new_ctor);
       }
 
     case ck_aggr:
-      return get_target_expr (digest_init (totype, expr));
+      if (TREE_CODE (totype) == COMPLEX_TYPE)
+       {
+         tree real = CONSTRUCTOR_ELT (expr, 0)->value;
+         tree imag = CONSTRUCTOR_ELT (expr, 1)->value;
+         real = perform_implicit_conversion (TREE_TYPE (totype),
+                                             real, complain);
+         imag = perform_implicit_conversion (TREE_TYPE (totype),
+                                             imag, complain);
+         expr = build2 (COMPLEX_EXPR, totype, real, imag);
+         return fold_if_not_in_template (expr);
+       }
+      return get_target_expr (digest_init (totype, expr, complain));
 
     default:
       break;
@@ -5489,35 +5818,31 @@ convert_like_real (conversion *convs, tree expr, tree fn, int argnum,
           conversion (i.e. the second step of copy-initialization), so
           don't allow any more.  */
        flags |= LOOKUP_NO_CONVERSION;
+      if (convs->rvaluedness_matches_p)
+       flags |= LOOKUP_PREFER_RVALUE;
       if (TREE_CODE (expr) == TARGET_EXPR
          && TARGET_EXPR_LIST_INIT_P (expr))
        /* Copy-list-initialization doesn't actually involve a copy.  */
        return expr;
       expr = build_temp (expr, totype, flags, &diag_kind, complain);
-      if (diag_kind && fn)
-       {
-         if ((complain & tf_error))
-           emit_diagnostic (diag_kind, DECL_SOURCE_LOCATION (fn), 0,
-                            "  initializing argument %P of %qD", argnum, fn);
-         else if (diag_kind == DK_ERROR)
-           return error_mark_node;
-       }
-      return build_cplus_new (totype, expr);
+      if (diag_kind && fn && complain)
+       emit_diagnostic (diag_kind, DECL_SOURCE_LOCATION (fn), 0,
+                        "  initializing argument %P of %qD", argnum, fn);
+      return build_cplus_new (totype, expr, complain);
 
     case ck_ref_bind:
       {
        tree ref_type = totype;
 
-       if (convs->bad_p && TYPE_REF_IS_RVALUE (ref_type)
-           && real_lvalue_p (expr))
+       if (convs->bad_p && !convs->u.next->bad_p)
          {
-           if (complain & tf_error)
-             {
-               error ("cannot bind %qT lvalue to %qT",
-                      TREE_TYPE (expr), totype);
-               if (fn)
-                 error ("  initializing argument %P of %q+D", argnum, fn);
-             }
+           gcc_assert (TYPE_REF_IS_RVALUE (ref_type)
+                       && real_lvalue_p (expr));
+
+           error ("cannot bind %qT lvalue to %qT",
+                  TREE_TYPE (expr), totype);
+           if (fn)
+             error ("  initializing argument %P of %q+D", argnum, fn);
            return error_mark_node;
          }
 
@@ -5543,19 +5868,16 @@ convert_like_real (conversion *convs, tree expr, tree fn, int argnum,
            if (!CP_TYPE_CONST_NON_VOLATILE_P (type)
                && !TYPE_REF_IS_RVALUE (ref_type))
              {
-               if (complain & tf_error)
-                 {
-                   /* If the reference is volatile or non-const, we
-                      cannot create a temporary.  */
-                   if (lvalue & clk_bitfield)
-                     error ("cannot bind bitfield %qE to %qT",
-                            expr, ref_type);
-                   else if (lvalue & clk_packed)
-                     error ("cannot bind packed field %qE to %qT",
-                            expr, ref_type);
-                   else
-                     error ("cannot bind rvalue %qE to %qT", expr, ref_type);
-                 }
+               /* If the reference is volatile or non-const, we
+                  cannot create a temporary.  */
+               if (lvalue & clk_bitfield)
+                 error ("cannot bind bitfield %qE to %qT",
+                        expr, ref_type);
+               else if (lvalue & clk_packed)
+                 error ("cannot bind packed field %qE to %qT",
+                        expr, ref_type);
+               else
+                 error ("cannot bind rvalue %qE to %qT", expr, ref_type);
                return error_mark_node;
              }
            /* If the source is a packed field, and we must use a copy
@@ -5568,9 +5890,8 @@ convert_like_real (conversion *convs, tree expr, tree fn, int argnum,
                && CLASS_TYPE_P (type)
                && type_has_nontrivial_copy_init (type))
              {
-               if (complain & tf_error)
-                 error ("cannot bind packed field %qE to %qT",
-                        expr, ref_type);
+               error ("cannot bind packed field %qE to %qT",
+                      expr, ref_type);
                return error_mark_node;
              }
            if (lvalue & clk_bitfield)
@@ -5578,7 +5899,7 @@ convert_like_real (conversion *convs, tree expr, tree fn, int argnum,
                expr = convert_bitfield_to_declared_type (expr);
                expr = fold_convert (type, expr);
              }
-           expr = build_target_expr_with_type (expr, type);
+           expr = build_target_expr_with_type (expr, type, complain);
          }
 
        /* Take the address of the thing to which we will bind the
@@ -5665,7 +5986,15 @@ convert_arg_to_ellipsis (tree arg)
   else if (NULLPTR_TYPE_P (arg_type))
     arg = null_pointer_node;
   else if (INTEGRAL_OR_ENUMERATION_TYPE_P (arg_type))
-    arg = perform_integral_promotions (arg);
+    {
+      if (SCOPED_ENUM_P (arg_type) && !abi_version_at_least (6))
+       {
+         warning (OPT_Wabi, "scoped enum %qT will not promote to an "
+                  "integral type in a future version of GCC", arg_type);
+         arg = cp_convert (ENUM_UNDERLYING_TYPE (arg_type), arg);
+       }
+      arg = perform_integral_promotions (arg);
+    }
 
   arg = require_complete_type (arg);
   arg_type = TREE_TYPE (arg);
@@ -5674,10 +6003,13 @@ convert_arg_to_ellipsis (tree arg)
       /* In a template (or ill-formed code), we can have an incomplete type
         even after require_complete_type, in which case we don't know
         whether it has trivial copy or not.  */
-      && COMPLETE_TYPE_P (arg_type)
-      && (type_has_nontrivial_copy_init (arg_type)
-         || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (arg_type)))
+      && COMPLETE_TYPE_P (arg_type))
     {
+      /* Build up a real lvalue-to-rvalue conversion in case the
+        copy constructor is trivial but not callable.  */
+      if (!cp_unevaluated_operand && CLASS_TYPE_P (arg_type))
+       force_rvalue (arg, tf_warning_or_error);
+
       /* [expr.call] 5.2.2/7:
         Passing a potentially-evaluated argument of class type (Clause 9)
         with a non-trivial copy constructor or a non-trivial destructor
@@ -5689,7 +6021,9 @@ convert_arg_to_ellipsis (tree arg)
 
         If the call appears in the context of a sizeof expression,
         it is not potentially-evaluated.  */
-      if (cp_unevaluated_operand == 0)
+      if (cp_unevaluated_operand == 0
+         && (type_has_nontrivial_copy_init (arg_type)
+             || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (arg_type)))
        error ("cannot pass objects of non-trivially-copyable "
               "type %q#T through %<...%>", arg_type);
     }
@@ -5803,24 +6137,18 @@ convert_default_arg (tree type, tree arg, tree fn, int parmnum)
 
      we must not perform access checks here.  */
   push_deferring_access_checks (dk_no_check);
+  /* We must make a copy of ARG, in case subsequent processing
+     alters any part of it.  */
   arg = break_out_target_exprs (arg);
   if (TREE_CODE (arg) == CONSTRUCTOR)
     {
-      arg = digest_init (type, arg);
+      arg = digest_init (type, arg, tf_warning_or_error);
       arg = convert_for_initialization (0, type, arg, LOOKUP_IMPLICIT,
                                        ICR_DEFAULT_ARGUMENT, fn, parmnum,
                                         tf_warning_or_error);
     }
   else
     {
-      /* We must make a copy of ARG, in case subsequent processing
-        alters any part of it.  For example, during gimplification a
-        cast of the form (T) &X::f (where "f" is a member function)
-        will lead to replacing the PTRMEM_CST for &X::f with a
-        VAR_DECL.  We can avoid the copy for constants, since they
-        are never modified in place.  */
-      if (!CONSTANT_CLASS_P (arg))
-       arg = unshare_expr (arg);
       arg = convert_for_initialization (0, type, arg, LOOKUP_IMPLICIT,
                                        ICR_DEFAULT_ARGUMENT, fn, parmnum,
                                         tf_warning_or_error);
@@ -5988,8 +6316,6 @@ build_over_call (struct z_candidate *cand, int flags, tsubst_flags_t complain)
                                   argarray);
       if (TREE_THIS_VOLATILE (fn) && cfun)
        current_function_returns_abnormally = 1;
-      if (!VOID_TYPE_P (return_type))
-       require_complete_type_sfinae (return_type, complain);
       return convert_from_reference (expr);
     }
 
@@ -6130,7 +6456,7 @@ build_over_call (struct z_candidate *cand, int flags, tsubst_flags_t complain)
       converted_arg = build_base_path (PLUS_EXPR,
                                       arg,
                                       cand->conversion_path,
-                                      1);
+                                      1, complain);
       /* Check that the base class is accessible.  */
       if (!accessible_base_p (TREE_TYPE (argtype),
                              BINFO_TYPE (cand->conversion_path), true))
@@ -6143,7 +6469,7 @@ build_over_call (struct z_candidate *cand, int flags, tsubst_flags_t complain)
       base_binfo = lookup_base (TREE_TYPE (TREE_TYPE (converted_arg)),
                                TREE_TYPE (parmtype), ba_unique, NULL);
       converted_arg = build_base_path (PLUS_EXPR, converted_arg,
-                                      base_binfo, 1);
+                                      base_binfo, 1, complain);
 
       argarray[j++] = converted_arg;
       parm = TREE_CHAIN (parm);
@@ -6161,14 +6487,35 @@ build_over_call (struct z_candidate *cand, int flags, tsubst_flags_t complain)
     {
       tree type = TREE_VALUE (parm);
       tree arg = VEC_index (tree, args, arg_index);
+      bool conversion_warning = true;
 
       conv = convs[i];
 
-      /* Don't make a copy here if build_call is going to.  */
-      if (conv->kind == ck_rvalue
-         && COMPLETE_TYPE_P (complete_type (type))
-         && !TREE_ADDRESSABLE (type))
-       conv = conv->u.next;
+      /* If the argument is NULL and used to (implicitly) instantiate a
+         template function (and bind one of the template arguments to
+         the type of 'long int'), we don't want to warn about passing NULL
+         to non-pointer argument.
+         For example, if we have this template function:
+
+           template<typename T> void func(T x) {}
+
+         we want to warn (when -Wconversion is enabled) in this case:
+
+           void foo() {
+             func<int>(NULL);
+           }
+
+         but not in this case:
+
+           void foo() {
+             func(NULL);
+           }
+      */
+      if (arg == null_node
+          && DECL_TEMPLATE_INFO (fn)
+          && cand->template_decl
+          && !(flags & LOOKUP_EXPLICIT_TMPL_ARGS))
+        conversion_warning = false;
 
       /* Warn about initializer_list deduction that isn't currently in the
         working draft.  */
@@ -6200,7 +6547,10 @@ build_over_call (struct z_candidate *cand, int flags, tsubst_flags_t complain)
            }
        }
 
-      val = convert_like_with_context (conv, arg, fn, i-is_method, complain);
+      val = convert_like_with_context (conv, arg, fn, i-is_method,
+                                      conversion_warning
+                                      ? complain
+                                      : complain & (~tf_warning));
 
       val = convert_for_arg_passing (type, val);
       if (val == error_mark_node)
@@ -6229,8 +6579,7 @@ build_over_call (struct z_candidate *cand, int flags, tsubst_flags_t complain)
   gcc_assert (j <= nargs);
   nargs = j;
 
-  check_function_arguments (TYPE_ATTRIBUTES (TREE_TYPE (fn)),
-                           nargs, argarray, TYPE_ARG_TYPES (TREE_TYPE (fn)));
+  check_function_arguments (TREE_TYPE (fn), nargs, argarray);
 
   /* Avoid actually calling copy constructors and copy assignment operators,
      if possible.  */
@@ -6285,7 +6634,7 @@ build_over_call (struct z_candidate *cand, int flags, tsubst_flags_t complain)
          if (TREE_CODE (arg) == TARGET_EXPR)
            return arg;
          else if (trivial)
-           return force_target_expr (DECL_CONTEXT (fn), arg);
+           return force_target_expr (DECL_CONTEXT (fn), arg, complain);
        }
       else if (TREE_CODE (arg) == TARGET_EXPR || trivial)
        {
@@ -6321,41 +6670,32 @@ build_over_call (struct z_candidate *cand, int flags, tsubst_flags_t complain)
        }
       else
        {
-         /* We must only copy the non-tail padding parts.
-            Use __builtin_memcpy for the bitwise copy.
-            FIXME fix 22488 so we can go back to using MODIFY_EXPR
-            instead of an explicit call to memcpy.  */
-       
-         tree arg0, arg1, arg2, t;
-         tree test = NULL_TREE;
+         /* We must only copy the non-tail padding parts.  */
+         tree arg0, arg2, t;
+         tree array_type, alias_set;
 
          arg2 = TYPE_SIZE_UNIT (as_base);
-         arg1 = arg;
          arg0 = cp_build_addr_expr (to, complain);
 
-         if (!can_trust_pointer_alignment ())
-           {
-             /* If we can't be sure about pointer alignment, a call
-                to __builtin_memcpy is expanded as a call to memcpy, which
-                is invalid with identical args.  Otherwise it is
-                expanded as a block move, which should be safe.  */
-             arg0 = save_expr (arg0);
-             arg1 = save_expr (arg1);
-             test = build2 (EQ_EXPR, boolean_type_node, arg0, arg1);
-           }
-         t = implicit_built_in_decls[BUILT_IN_MEMCPY];
-         t = build_call_n (t, 3, arg0, arg1, arg2);
-
-         t = convert (TREE_TYPE (arg0), t);
-         if (test)
-           t = build3 (COND_EXPR, TREE_TYPE (t), test, arg0, t);
-         val = cp_build_indirect_ref (t, RO_NULL, complain);
+         array_type = build_array_type (char_type_node,
+                                        build_index_type
+                                          (size_binop (MINUS_EXPR,
+                                                       arg2, size_int (1))));
+         alias_set = build_int_cst (build_pointer_type (type), 0);
+         t = build2 (MODIFY_EXPR, void_type_node,
+                     build2 (MEM_REF, array_type, arg0, alias_set),
+                     build2 (MEM_REF, array_type, arg, alias_set));
+         val = build2 (COMPOUND_EXPR, TREE_TYPE (to), t, to);
           TREE_NO_WARNING (val) = 1;
        }
 
       return val;
     }
-  /* FIXME handle trivial default constructor and destructor, too.  */
+  else if (DECL_DESTRUCTOR_P (fn)
+          && trivial_fn_p (fn)
+          && !DECL_DELETED_FN (fn))
+    return fold_convert (void_type_node, argarray[0]);
+  /* FIXME handle trivial default constructor, too.  */
 
   if (!already_used)
     mark_used (fn);
@@ -6373,7 +6713,8 @@ build_over_call (struct z_candidate *cand, int flags, tsubst_flags_t complain)
       if (TREE_DEPRECATED (fn))
        warn_deprecated_use (fn, NULL_TREE);
 
-      argarray[0] = build_base_path (PLUS_EXPR, argarray[0], binfo, 1);
+      argarray[0] = build_base_path (PLUS_EXPR, argarray[0], binfo, 1,
+                                    complain);
       if (TREE_SIDE_EFFECTS (argarray[0]))
        argarray[0] = save_expr (argarray[0]);
       t = build_pointer_type (TREE_TYPE (fn));
@@ -6398,15 +6739,12 @@ build_cxx_call (tree fn, int nargs, tree *argarray)
 {
   tree fndecl;
 
+  /* Remember roughly where this call is.  */
+  location_t loc = EXPR_LOC_OR_HERE (fn);
   fn = build_call_a (fn, nargs, argarray);
+  SET_EXPR_LOCATION (fn, loc);
 
-  /* If this call might throw an exception, note that fact.  */
   fndecl = get_callee_fndecl (fn);
-  if ((!fndecl || !TREE_NOTHROW (fndecl))
-      && at_function_scope_p ()
-      && cfun
-      && cp_function_chain)
-    cp_function_chain->can_throw = 1;
 
   /* Check that arguments to builtin functions match the expectations.  */
   if (fndecl
@@ -6427,7 +6765,7 @@ build_cxx_call (tree fn, int nargs, tree *argarray)
     return error_mark_node;
 
   if (MAYBE_CLASS_TYPE_P (TREE_TYPE (fn)))
-    fn = build_cplus_new (TREE_TYPE (fn), fn);
+    fn = build_cplus_new (TREE_TYPE (fn), fn, tf_warning_or_error);
   return convert_from_reference (fn);
 }
 
@@ -6586,7 +6924,7 @@ build_special_member_call (tree instance, tree name, VEC(tree,gc) **args,
            /* However, for assignment operators, we must convert
               dynamically if the base is virtual.  */
            instance = build_base_path (PLUS_EXPR, instance,
-                                       binfo, /*nonnull=*/1);
+                                       binfo, /*nonnull=*/1, complain);
        }
     }
 
@@ -6615,8 +6953,7 @@ build_special_member_call (tree instance, tree name, VEC(tree,gc) **args,
                    current_vtt_parm,
                    vtt);
       gcc_assert (BINFO_SUBVTT_INDEX (binfo));
-      sub_vtt = build2 (POINTER_PLUS_EXPR, TREE_TYPE (vtt), vtt,
-                       BINFO_SUBVTT_INDEX (binfo));
+      sub_vtt = fold_build_pointer_plus (vtt, BINFO_SUBVTT_INDEX (binfo));
 
       if (args == NULL)
        {
@@ -6687,10 +7024,10 @@ name_as_c_string (tree name, tree type, bool *free_p)
    be set, upon return, to the function called.  ARGS may be NULL.
    This may change ARGS.  */
 
-tree
-build_new_method_call (tree instance, tree fns, VEC(tree,gc) **args,
-                      tree conversion_path, int flags,
-                      tree *fn_p, tsubst_flags_t complain)
+static tree
+build_new_method_call_1 (tree instance, tree fns, VEC(tree,gc) **args,
+                        tree conversion_path, int flags,
+                        tree *fn_p, tsubst_flags_t complain)
 {
   struct z_candidate *candidates = 0, *cand;
   tree explicit_targs = NULL_TREE;
@@ -6801,7 +7138,7 @@ build_new_method_call (tree instance, tree fns, VEC(tree,gc) **args,
   /* Process the argument list.  */
   if (args != NULL && *args != NULL)
     {
-      *args = resolve_args (*args);
+      *args = resolve_args (*args, complain);
       if (*args == NULL)
        return error_mark_node;
     }
@@ -6832,10 +7169,29 @@ build_new_method_call (tree instance, tree fns, VEC(tree,gc) **args,
       && BRACE_ENCLOSED_INITIALIZER_P (VEC_index (tree, *args, 0))
       && CONSTRUCTOR_IS_DIRECT_INIT (VEC_index (tree, *args, 0)))
     {
+      tree init_list = VEC_index (tree, *args, 0);
+
       gcc_assert (VEC_length (tree, *args) == 1
                  && !(flags & LOOKUP_ONLYCONVERTING));
 
-      add_list_candidates (fns, first_mem_arg, VEC_index (tree, *args, 0),
+      /* If the initializer list has no elements and T is a class type with
+        a default constructor, the object is value-initialized.  Handle
+        this here so we don't need to handle it wherever we use
+        build_special_member_call.  */
+      if (CONSTRUCTOR_NELTS (init_list) == 0
+         && TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype)
+         && !processing_template_decl)
+       {
+         tree ob, init = build_value_init (basetype, complain);
+         if (integer_zerop (instance_ptr))
+           return get_target_expr_sfinae (init, complain);
+         ob = build_fold_indirect_ref (instance_ptr);
+         init = build2 (INIT_EXPR, TREE_TYPE (ob), ob, init);
+         TREE_SIDE_EFFECTS (init) = true;
+         return init;
+       }
+
+      add_list_candidates (fns, first_mem_arg, init_list,
                           basetype, explicit_targs, template_only,
                           conversion_path, access_binfo, flags, &candidates);
     }
@@ -6928,9 +7284,14 @@ build_new_method_call (tree instance, tree fns, VEC(tree,gc) **args,
            }
          else
            {
+             /* Optimize away vtable lookup if we know that this function
+                can't be overridden.  */
              if (DECL_VINDEX (fn) && ! (flags & LOOKUP_NONVIRTUAL)
-                 && resolves_to_fixed_type_p (instance, 0))
+                 && (resolves_to_fixed_type_p (instance, 0)
+                     || DECL_FINAL_P (fn) || CLASSTYPE_FINAL (basetype)))
                flags |= LOOKUP_NONVIRTUAL;
+              if (explicit_targs)
+                flags |= LOOKUP_EXPLICIT_TMPL_ARGS;
              /* Now we know what function is being called.  */
              if (fn_p)
                *fn_p = fn;
@@ -6994,6 +7355,21 @@ build_new_method_call (tree instance, tree fns, VEC(tree,gc) **args,
   return call;
 }
 
+/* Wrapper for above.  */
+
+tree
+build_new_method_call (tree instance, tree fns, VEC(tree,gc) **args,
+                      tree conversion_path, int flags,
+                      tree *fn_p, tsubst_flags_t complain)
+{
+  tree ret;
+  bool subtime = timevar_cond_start (TV_OVERLOAD);
+  ret = build_new_method_call_1 (instance, fns, args, conversion_path, flags,
+                                 fn_p, complain);
+  timevar_cond_stop (TV_OVERLOAD, subtime);
+  return ret;
+}
+
 /* Returns true iff standard conversion sequence ICS1 is a proper
    subsequence of ICS2.  */
 
@@ -7488,18 +7864,25 @@ compare_ics (conversion *ics1, conversion *ics2)
      types to which the references refer are the same type except for
      top-level cv-qualifiers, and the type to which the reference
      initialized by S2 refers is more cv-qualified than the type to
-     which the reference initialized by S1 refers */
+     which the reference initialized by S1 refers.
+
+     DR 1328 [over.match.best]: the context is an initialization by
+     conversion function for direct reference binding (13.3.1.6) of a
+     reference to function type, the return type of F1 is the same kind of
+     reference (i.e. lvalue or rvalue) as the reference being initialized,
+     and the return type of F2 is not.  */
 
   if (ref_conv1 && ref_conv2)
     {
       if (!ref_conv1->this_p && !ref_conv2->this_p
-         && (TYPE_REF_IS_RVALUE (ref_conv1->type)
-             != TYPE_REF_IS_RVALUE (ref_conv2->type)))
+         && (ref_conv1->rvaluedness_matches_p
+             != ref_conv2->rvaluedness_matches_p)
+         && (same_type_p (ref_conv1->type, ref_conv2->type)
+             || (TYPE_REF_IS_RVALUE (ref_conv1->type)
+                 != TYPE_REF_IS_RVALUE (ref_conv2->type))))
        {
-         if (ref_conv1->rvaluedness_matches_p)
-           return 1;
-         if (ref_conv2->rvaluedness_matches_p)
-           return -1;
+         return (ref_conv1->rvaluedness_matches_p
+                 - ref_conv2->rvaluedness_matches_p);
        }
 
       if (same_type_ignoring_top_level_qualifiers_p (to_type1, to_type2))
@@ -8010,7 +8393,8 @@ perform_implicit_conversion_flags (tree type, tree expr, tsubst_flags_t complain
          else if (invalid_nonstatic_memfn_p (expr, complain))
            /* We gave an error.  */;
          else
-           error ("could not convert %qE to %qT", expr, type);
+           error ("could not convert %qE from %qT to %qT", expr,
+                  TREE_TYPE (expr), type);
        }
       expr = error_mark_node;
     }
@@ -8041,7 +8425,7 @@ perform_implicit_conversion (tree type, tree expr, tsubst_flags_t complain)
    permitted.  If the conversion is valid, the converted expression is
    returned.  Otherwise, NULL_TREE is returned, except in the case
    that TYPE is a class type; in that case, an error is issued.  If
-   C_CAST_P is true, then this direction initialization is taking
+   C_CAST_P is true, then this direct-initialization is taking
    place as part of a static_cast being attempted as part of a C-style
    cast.  */
 
@@ -8070,7 +8454,7 @@ perform_direct_initialization_if_possible (tree type,
       expr = build_special_member_call (NULL_TREE, complete_ctor_identifier,
                                        &args, type, LOOKUP_NORMAL, complain);
       release_tree_vector (args);
-      return build_cplus_new (type, expr);
+      return build_cplus_new (type, expr, complain);
     }
 
   /* Get the high-water mark for the CONVERSION_OBSTACK.  */
@@ -8186,7 +8570,7 @@ set_up_extended_ref_temp (tree decl, tree expr, tree *cleanup, tree *initp)
       if (TREE_STATIC (var))
        init = add_stmt_to_compound (init, register_dtor_fn (var));
       else
-       *cleanup = cxx_maybe_build_cleanup (var);
+       *cleanup = cxx_maybe_build_cleanup (var, tf_warning_or_error);
 
       /* We must be careful to destroy the temporary only
         after its initialization has taken place.  If the
@@ -8232,7 +8616,7 @@ set_up_extended_ref_temp (tree decl, tree expr, tree *cleanup, tree *initp)
 
 tree
 initialize_reference (tree type, tree expr, tree decl, tree *cleanup,
-                     tsubst_flags_t complain)
+                     int flags, tsubst_flags_t complain)
 {
   conversion *conv;
   void *p;
@@ -8244,14 +8628,16 @@ initialize_reference (tree type, tree expr, tree decl, tree *cleanup,
   p = conversion_obstack_alloc (0);
 
   conv = reference_binding (type, TREE_TYPE (expr), expr, /*c_cast_p=*/false,
-                           LOOKUP_NORMAL);
+                           flags);
   if (!conv || conv->bad_p)
     {
       if (complain & tf_error)
        {
-         if (!CP_TYPE_CONST_P (TREE_TYPE (type))
-             && !TYPE_REF_IS_RVALUE (type)
-             && !real_lvalue_p (expr))
+         if (conv)
+           convert_like (conv, expr, complain);
+         else if (!CP_TYPE_CONST_P (TREE_TYPE (type))
+                  && !TYPE_REF_IS_RVALUE (type)
+                  && !real_lvalue_p (expr))
            error ("invalid initialization of non-const reference of "
                   "type %qT from an rvalue of type %qT",
                   type, TREE_TYPE (expr));
@@ -8303,6 +8689,8 @@ initialize_reference (tree type, tree expr, tree decl, tree *cleanup,
       tree var;
       tree base_conv_type;
 
+      gcc_assert (complain == tf_warning_or_error);
+
       /* Skip over the REF_BIND.  */
       conv = conv->u.next;
       /* If the next conversion is a BASE_CONV, skip that too -- but
@@ -8320,7 +8708,7 @@ initialize_reference (tree type, tree expr, tree decl, tree *cleanup,
                                /*inner=*/-1,
                                /*issue_conversion_warnings=*/true,
                                /*c_cast_p=*/false,
-                               tf_warning_or_error);
+                               complain);
       if (error_operand_p (expr))
        expr = error_mark_node;
       else
@@ -8341,18 +8729,18 @@ initialize_reference (tree type, tree expr, tree decl, tree *cleanup,
            }
          else
            /* Take the address of EXPR.  */
-           expr = cp_build_addr_expr (expr, tf_warning_or_error);
+           expr = cp_build_addr_expr (expr, complain);
          /* If a BASE_CONV was required, perform it now.  */
          if (base_conv_type)
            expr = (perform_implicit_conversion
                    (build_pointer_type (base_conv_type), expr,
-                    tf_warning_or_error));
+                    complain));
          expr = build_nop (type, expr);
        }
     }
   else
     /* Perform the conversion.  */
-    expr = convert_like (conv, expr, tf_warning_or_error);
+    expr = convert_like (conv, expr, complain);
 
   /* Free all the conversions we allocated.  */
   obstack_free (&conversion_obstack, p);