OSDN Git Service

PR c++/34758
[pf3gnuchains/gcc-fork.git] / gcc / cp / call.c
index 1c64149..499ed03 100644 (file)
@@ -38,6 +38,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "target.h"
 #include "convert.h"
 #include "langhooks.h"
+#include "c-family/c-objc.h"
 
 /* The various kinds of conversion.  */
 
@@ -84,6 +85,9 @@ struct conversion {
   BOOL_BITFIELD user_conv_p : 1;
   BOOL_BITFIELD ellipsis_p : 1;
   BOOL_BITFIELD this_p : 1;
+  /* True if this conversion would be permitted with a bending of
+     language standards, e.g. disregarding pointer qualifiers or
+     converting integers to pointers.  */
   BOOL_BITFIELD bad_p : 1;
   /* If KIND is ck_ref_bind ck_base_conv, true to indicate that a
      temporary should be created to hold the result of the
@@ -129,6 +133,7 @@ struct conversion {
 
 static struct obstack conversion_obstack;
 static bool conversion_obstack_initialized;
+struct rejection_reason;
 
 static struct z_candidate * tourney (struct z_candidate *);
 static int equal_functions (tree, tree);
@@ -151,7 +156,7 @@ static void op_error (enum tree_code, enum tree_code, tree, tree,
 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 (struct z_candidate *);
+static void print_z_candidates (location_t, struct z_candidate *);
 static tree build_this (tree);
 static struct z_candidate *splice_viable (struct z_candidate *, bool, bool *);
 static bool any_strictly_viable (struct z_candidate *);
@@ -190,7 +195,7 @@ static conversion *maybe_handle_ref_bind (conversion **);
 static void maybe_handle_implicit_object (conversion **);
 static struct z_candidate *add_candidate
        (struct z_candidate **, tree, tree, const VEC(tree,gc) *, size_t,
-        conversion **, tree, tree, int);
+        conversion **, tree, tree, int, struct rejection_reason *);
 static tree source_type (conversion *);
 static void add_warning (struct z_candidate *, struct z_candidate *);
 static bool reference_compatible_p (tree, tree);
@@ -416,6 +421,43 @@ struct candidate_warning {
   candidate_warning *next;
 };
 
+/* Information for providing diagnostics about why overloading failed.  */
+
+enum rejection_reason_code {
+  rr_none,
+  rr_arity,
+  rr_arg_conversion,
+  rr_bad_arg_conversion
+};
+
+struct conversion_info {
+  /* The index of the argument, 0-based.  */
+  int n_arg;
+  /* The type of the actual argument.  */
+  tree from_type;
+  /* The type of the formal argument.  */
+  tree to_type;
+};
+  
+struct rejection_reason {
+  enum rejection_reason_code code;
+  union {
+    /* Information about an arity mismatch.  */
+    struct {
+      /* The expected number of arguments.  */
+      int expected;
+      /* The actual number of arguments in the call.  */
+      int actual;
+      /* Whether the call was a varargs call.  */
+      bool call_varargs_p;
+    } arity;
+    /* Information about an argument conversion mismatch.  */
+    struct conversion_info conversion;
+    /* Same, but for bad argument conversions.  */
+    struct conversion_info bad_conversion;
+  } u;
+};
+
 struct z_candidate {
   /* The FUNCTION_DECL that will be called if this candidate is
      selected by overload resolution.  */
@@ -437,6 +479,7 @@ struct z_candidate {
      type.  */
   conversion *second_conv;
   int viable;
+  struct rejection_reason *reason;
   /* If FN is a member function, the binfo indicating the path used to
      qualify the name of FN at the call site.  This path is used to
      determine whether or not FN is accessible if it is selected by
@@ -466,15 +509,24 @@ null_ptr_cst_p (tree t)
      A null pointer constant is an integral constant expression
      (_expr.const_) rvalue of integer type that evaluates to zero or
      an rvalue of type std::nullptr_t. */
-  t = integral_constant_value (t);
-  if (t == null_node
-      || NULLPTR_TYPE_P (TREE_TYPE (t)))
+  if (NULLPTR_TYPE_P (TREE_TYPE (t)))
     return true;
-  if (CP_INTEGRAL_TYPE_P (TREE_TYPE (t)) && integer_zerop (t))
+  if (CP_INTEGRAL_TYPE_P (TREE_TYPE (t)))
     {
-      STRIP_NOPS (t);
-      if (!TREE_OVERFLOW (t))
-       return true;
+      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
+       {
+         t = integral_constant_value (t);
+         STRIP_NOPS (t);
+         if (integer_zerop (t) && !TREE_OVERFLOW (t))
+           return true;
+       }
     }
   return false;
 }
@@ -509,6 +561,49 @@ conversion_obstack_alloc (size_t n)
   return p;
 }
 
+/* Allocate rejection reasons.  */
+
+static struct rejection_reason *
+alloc_rejection (enum rejection_reason_code code)
+{
+  struct rejection_reason *p;
+  p = (struct rejection_reason *) conversion_obstack_alloc (sizeof *p);
+  p->code = code;
+  return p;
+}
+
+static struct rejection_reason *
+arity_rejection (tree first_arg, int expected, int actual)
+{
+  struct rejection_reason *r = alloc_rejection (rr_arity);
+  int adjust = first_arg != NULL_TREE;
+  r->u.arity.expected = expected - adjust;
+  r->u.arity.actual = actual - adjust;
+  return r;
+}
+
+static struct rejection_reason *
+arg_conversion_rejection (tree first_arg, int n_arg, tree from, tree to)
+{
+  struct rejection_reason *r = alloc_rejection (rr_arg_conversion);
+  int adjust = first_arg != NULL_TREE;
+  r->u.conversion.n_arg = n_arg - adjust;
+  r->u.conversion.from_type = from;
+  r->u.conversion.to_type = to;
+  return r;
+}
+
+static struct rejection_reason *
+bad_arg_conversion_rejection (tree first_arg, int n_arg, tree from, tree to)
+{
+  struct rejection_reason *r = alloc_rejection (rr_bad_arg_conversion);
+  int adjust = first_arg != NULL_TREE;
+  r->u.bad_conversion.n_arg = n_arg - adjust;
+  r->u.bad_conversion.from_type = from;
+  r->u.bad_conversion.to_type = to;
+  return r;
+}
+
 /* Dynamically allocate a conversion.  */
 
 static conversion *
@@ -630,6 +725,29 @@ build_list_conv (tree type, tree ctor, int flags)
   return t;
 }
 
+/* Subroutine of build_aggr_conv: check whether CTOR, a braced-init-list,
+   is a valid aggregate initializer for array type ATYPE.  */
+
+static bool
+can_convert_array (tree atype, tree ctor, int flags)
+{
+  unsigned i;
+  tree elttype = TREE_TYPE (atype);
+  for (i = 0; i < CONSTRUCTOR_NELTS (ctor); ++i)
+    {
+      tree val = CONSTRUCTOR_ELT (ctor, i)->value;
+      bool ok;
+      if (TREE_CODE (elttype) == ARRAY_TYPE
+         && TREE_CODE (val) == CONSTRUCTOR)
+       ok = can_convert_array (elttype, val, flags);
+      else
+       ok = can_convert_arg (elttype, TREE_TYPE (val), val, flags);
+      if (!ok)
+       return false;
+    }
+  return true;
+}
+
 /* Represent a conversion from CTOR, a braced-init-list, to TYPE, an
    aggregate class, if such a conversion is possible.  */
 
@@ -641,26 +759,33 @@ build_aggr_conv (tree type, tree ctor, int flags)
   tree field = next_initializable_field (TYPE_FIELDS (type));
   tree empty_ctor = NULL_TREE;
 
-  for (; field; field = next_initializable_field (TREE_CHAIN (field)))
+  for (; field; field = next_initializable_field (DECL_CHAIN (field)))
     {
+      tree ftype = TREE_TYPE (field);
+      tree val;
+      bool ok;
+
       if (i < CONSTRUCTOR_NELTS (ctor))
-       {
-         constructor_elt *ce = CONSTRUCTOR_ELT (ctor, i);
-         if (!can_convert_arg (TREE_TYPE (field), TREE_TYPE (ce->value),
-                               ce->value, flags))
-           return NULL;
-         ++i;
-         if (TREE_CODE (type) == UNION_TYPE)
-           break;
-       }
+       val = CONSTRUCTOR_ELT (ctor, i)->value;
       else
        {
          if (empty_ctor == NULL_TREE)
            empty_ctor = build_constructor (init_list_type_node, NULL);
-         if (!can_convert_arg (TREE_TYPE (field), TREE_TYPE (empty_ctor),
-                               empty_ctor, flags))
-           return NULL;
+         val = empty_ctor;
        }
+      ++i;
+
+      if (TREE_CODE (ftype) == ARRAY_TYPE
+         && TREE_CODE (val) == CONSTRUCTOR)
+       ok = can_convert_array (ftype, val, flags);
+      else
+       ok = can_convert_arg (ftype, TREE_TYPE (val), val, flags);
+
+      if (!ok)
+       return NULL;
+
+      if (TREE_CODE (type) == UNION_TYPE)
+       break;
     }
 
   if (i < CONSTRUCTOR_NELTS (ctor))
@@ -725,6 +850,7 @@ standard_conversion (tree to, tree from, tree expr, bool c_cast_p,
   enum tree_code fcode, tcode;
   conversion *conv;
   bool fromref = false;
+  tree qualified_to;
 
   to = non_reference (to);
   if (TREE_CODE (from) == REFERENCE_TYPE)
@@ -732,6 +858,7 @@ standard_conversion (tree to, tree from, tree expr, bool c_cast_p,
       fromref = true;
       from = TREE_TYPE (from);
     }
+  qualified_to = to;
   to = strip_top_quals (to);
   from = strip_top_quals (from);
 
@@ -793,7 +920,11 @@ standard_conversion (tree to, tree from, tree expr, bool c_cast_p,
     }
 
   if (same_type_p (from, to))
-    return conv;
+    {
+      if (CLASS_TYPE_P (to) && conv->kind == ck_rvalue)
+       conv->type = qualified_to;
+      return conv;
+    }
 
   /* [conv.ptr]
      A null pointer constant can be converted to a pointer type; ... A
@@ -832,9 +963,10 @@ standard_conversion (tree to, tree from, tree expr, bool c_cast_p,
               && !TYPE_PTRMEM_P (from)
               && TREE_CODE (TREE_TYPE (from)) != FUNCTION_TYPE)
        {
+         tree nfrom = TREE_TYPE (from);
          from = build_pointer_type
-           (cp_build_qualified_type (void_type_node,
-                                     cp_type_quals (TREE_TYPE (from))));
+           (cp_build_qualified_type (void_type_node, 
+                                     cp_type_quals (nfrom)));
          conv = build_conv (ck_ptr, from, conv);
        }
       else if (TYPE_PTRMEM_P (from))
@@ -900,6 +1032,11 @@ standard_conversion (tree to, tree from, tree expr, bool c_cast_p,
       else if (expr && string_conv_p (to, expr, 0))
        /* converting from string constant to char *.  */
        conv = build_conv (ck_qual, to, conv);
+      /* Allow conversions among compatible ObjC pointer types (base
+        conversions have been already handled above).  */
+      else if (c_dialect_objc ()
+              && objc_compare_types (to, from, -4, NULL_TREE))
+       conv = build_conv (ck_ptr, to, conv);
       else if (ptr_reasonably_similar (to_pointee, from_pointee))
        {
          conv = build_conv (ck_ptr, to, conv);
@@ -999,6 +1136,9 @@ standard_conversion (tree to, tree from, tree expr, bool c_cast_p,
 bool
 reference_related_p (tree t1, tree t2)
 {
+  if (t1 == error_mark_node || t2 == error_mark_node)
+    return false;
+
   t1 = TYPE_MAIN_VARIANT (t1);
   t2 = TYPE_MAIN_VARIANT (t2);
 
@@ -1043,7 +1183,7 @@ convert_class_to_reference (tree reference_type, tree s, tree expr, int flags)
   if (!expr)
     return NULL;
 
-  conversions = lookup_conversions (s, /*lookup_template_convs_p=*/true);
+  conversions = lookup_conversions (s);
   if (!conversions)
     return NULL;
 
@@ -1096,9 +1236,15 @@ convert_class_to_reference (tree reference_type, tree s, tree expr, int flags)
             rvalue of the right type is good enough.  */
          tree f = cand->fn;
          tree t2 = TREE_TYPE (TREE_TYPE (f));
-         if (TREE_CODE (t2) != REFERENCE_TYPE
-             || !reference_compatible_p (t, TREE_TYPE (t2)))
+         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
@@ -1454,7 +1600,7 @@ implicit_conversion (tree to, tree from, tree expr, bool c_cast_p,
          tree elt;
 
          if (nelts == 0)
-           elt = integer_zero_node;
+           elt = build_value_init (to, tf_none);
          else if (nelts == 1)
            elt = CONSTRUCTOR_ELT (expr, 0)->value;
          else
@@ -1509,7 +1655,7 @@ add_candidate (struct z_candidate **candidates,
               tree fn, tree first_arg, const VEC(tree,gc) *args,
               size_t num_convs, conversion **convs,
               tree access_path, tree conversion_path,
-              int viable)
+              int viable, struct rejection_reason *reason)
 {
   struct z_candidate *cand = (struct z_candidate *)
     conversion_obstack_alloc (sizeof (struct z_candidate));
@@ -1522,12 +1668,28 @@ add_candidate (struct z_candidate **candidates,
   cand->access_path = access_path;
   cand->conversion_path = conversion_path;
   cand->viable = viable;
+  cand->reason = reason;
   cand->next = *candidates;
   *candidates = cand;
 
   return cand;
 }
 
+/* Return the number of remaining arguments in the parameter list
+   beginning with ARG.  */
+
+static int
+remaining_arguments (tree arg)
+{
+  int n;
+
+  for (n = 0; arg != NULL_TREE && arg != void_list_node;
+       arg = TREE_CHAIN (arg))
+    n++;
+
+  return n;
+}
+
 /* Create an overload candidate for the function or method FN called
    with the argument list FIRST_ARG/ARGS and add it to CANDIDATES.
    FLAGS is passed on to implicit_conversion.
@@ -1550,6 +1712,7 @@ add_function_candidate (struct z_candidate **candidates,
   tree orig_first_arg = first_arg;
   int skip;
   int viable = 1;
+  struct rejection_reason *reason = NULL;
 
   /* At this point we should not see any functions which haven't been
      explicitly declared, except for friend functions which will have
@@ -1589,12 +1752,36 @@ add_function_candidate (struct z_candidate **candidates,
       parmnode = TREE_CHAIN (parmnode);
     }
 
-  if (i < len && parmnode)
-    viable = 0;
+  if ((i < len && parmnode)
+      || !sufficient_parms_p (parmnode))
+    {
+      int remaining = remaining_arguments (parmnode);
+      viable = 0;
+      reason = arity_rejection (first_arg, i + remaining, len);
+    }
+  /* When looking for a function from a subobject from an implicit
+     copy/move constructor/operator=, don't consider anything that takes (a
+     reference to) an unrelated type.  See c++/44909 and core 1092.  */
+  else if (parmlist && (flags & LOOKUP_DEFAULTED))
+    {
+      if (DECL_CONSTRUCTOR_P (fn))
+       i = 1;
+      else if (DECL_ASSIGNMENT_OPERATOR_P (fn)
+              && DECL_OVERLOADED_OPERATOR_P (fn) == NOP_EXPR)
+       i = 2;
+      else
+       i = 0;
+      if (i && len == i)
+       {
+         parmnode = chain_index (i-1, parmlist);
+         if (!reference_related_p (non_reference (TREE_VALUE (parmnode)),
+                                   ctype))
+           viable = 0;
+       }
 
-  /* Make sure there are default args for the rest of the parms.  */
-  else if (!sufficient_parms_p (parmnode))
-    viable = 0;
+      /* This only applies at the top level.  */
+      flags &= ~LOOKUP_DEFAULTED;
+    }
 
   if (! viable)
     goto out;
@@ -1607,7 +1794,7 @@ add_function_candidate (struct z_candidate **candidates,
 
   for (i = 0; i < len; ++i)
     {
-      tree arg, argtype;
+      tree arg, argtype, to_type;
       conversion *t;
       int is_this;
 
@@ -1674,11 +1861,13 @@ add_function_candidate (struct z_candidate **candidates,
 
          t = implicit_conversion (parmtype, argtype, arg,
                                   /*c_cast_p=*/false, lflags);
+         to_type = parmtype;
        }
       else
        {
          t = build_identity_conv (argtype, arg);
          t->ellipsis_p = true;
+         to_type = argtype;
        }
 
       if (t && is_this)
@@ -1688,16 +1877,20 @@ add_function_candidate (struct z_candidate **candidates,
       if (! t)
        {
          viable = 0;
+         reason = arg_conversion_rejection (first_arg, i, argtype, to_type);
          break;
        }
 
       if (t->bad_p)
-       viable = -1;
+       {
+         viable = -1;
+         reason = bad_arg_conversion_rejection (first_arg, i, argtype, to_type);
+       }
     }
 
  out:
   return add_candidate (candidates, fn, orig_first_arg, args, len, convs,
-                       access_path, conversion_path, viable);
+                       access_path, conversion_path, viable, reason);
 }
 
 /* Create an overload candidate for the conversion function FN which will
@@ -1721,6 +1914,7 @@ add_conv_candidate (struct z_candidate **candidates, tree fn, tree obj,
   int i, len, viable, flags;
   tree parmlist, parmnode;
   conversion **convs;
+  struct rejection_reason *reason;
 
   for (parmlist = totype; TREE_CODE (parmlist) != FUNCTION_TYPE; )
     parmlist = TREE_TYPE (parmlist);
@@ -1731,6 +1925,7 @@ add_conv_candidate (struct z_candidate **candidates, tree fn, tree obj,
   parmnode = parmlist;
   viable = 1;
   flags = LOOKUP_IMPLICIT;
+  reason = NULL;
 
   /* Don't bother looking up the same type twice.  */
   if (*candidates && (*candidates)->fn == totype)
@@ -1738,7 +1933,7 @@ add_conv_candidate (struct z_candidate **candidates, tree fn, tree obj,
 
   for (i = 0; i < len; ++i)
     {
-      tree arg, argtype;
+      tree arg, argtype, convert_type = NULL_TREE;
       conversion *t;
 
       if (i == 0)
@@ -1751,17 +1946,24 @@ add_conv_candidate (struct z_candidate **candidates, tree fn, tree obj,
       argtype = lvalue_type (arg);
 
       if (i == 0)
-       t = implicit_conversion (totype, argtype, arg, /*c_cast_p=*/false,
-                                flags);
+       {
+         t = implicit_conversion (totype, argtype, arg, /*c_cast_p=*/false,
+                                  flags);
+         convert_type = totype;
+       }
       else if (parmnode == void_list_node)
        break;
       else if (parmnode)
-       t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg,
-                                /*c_cast_p=*/false, flags);
+       {
+         t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg,
+                                  /*c_cast_p=*/false, flags);
+         convert_type = TREE_VALUE (parmnode);
+       }
       else
        {
          t = build_identity_conv (argtype, arg);
          t->ellipsis_p = true;
+         convert_type = argtype;
        }
 
       convs[i] = t;
@@ -1769,7 +1971,10 @@ add_conv_candidate (struct z_candidate **candidates, tree fn, tree obj,
        break;
 
       if (t->bad_p)
-       viable = -1;
+       {
+         viable = -1;
+         reason = bad_arg_conversion_rejection (NULL_TREE, i, argtype, convert_type);
+       }
 
       if (i == 0)
        continue;
@@ -1778,14 +1983,16 @@ add_conv_candidate (struct z_candidate **candidates, tree fn, tree obj,
        parmnode = TREE_CHAIN (parmnode);
     }
 
-  if (i < len)
-    viable = 0;
-
-  if (!sufficient_parms_p (parmnode))
-    viable = 0;
+  if (i < len
+      || ! sufficient_parms_p (parmnode))
+    {
+      int remaining = remaining_arguments (parmnode);
+      viable = 0;
+      reason = arity_rejection (NULL_TREE, i + remaining, len);
+    }
 
   return add_candidate (candidates, totype, first_arg, arglist, len, convs,
-                       access_path, conversion_path, viable);
+                       access_path, conversion_path, viable, reason);
 }
 
 static void
@@ -1798,6 +2005,7 @@ build_builtin_candidate (struct z_candidate **candidates, tree fnname,
   size_t num_convs;
   int viable = 1, i;
   tree types[2];
+  struct rejection_reason *reason = NULL;
 
   types[0] = type1;
   types[1] = type2;
@@ -1825,9 +2033,13 @@ build_builtin_candidate (struct z_candidate **candidates, tree fnname,
          viable = 0;
          /* We need something for printing the candidate.  */
          t = build_identity_conv (types[i], NULL_TREE);
+         reason = arg_conversion_rejection (NULL_TREE, i, argtypes[i], types[i]);
        }
       else if (t->bad_p)
-       viable = 0;
+       {
+         viable = 0;
+         reason = bad_arg_conversion_rejection (NULL_TREE, i, argtypes[i], types[i]);
+       }
       convs[i] = t;
     }
 
@@ -1841,14 +2053,18 @@ build_builtin_candidate (struct z_candidate **candidates, tree fnname,
       if (t)
        convs[0] = t;
       else
-       viable = 0;
+       {
+         viable = 0;
+         reason = arg_conversion_rejection (NULL_TREE, 0, argtypes[2],
+                                            boolean_type_node);
+       }
     }
 
   add_candidate (candidates, fnname, /*first_arg=*/NULL_TREE, /*args=*/NULL,
                 num_convs, convs,
                 /*access_path=*/NULL_TREE,
                 /*conversion_path=*/NULL_TREE,
-                viable);
+                viable, reason);
 }
 
 static bool
@@ -1934,7 +2150,7 @@ add_builtin_candidate (struct z_candidate **candidates, enum tree_code code,
        }
       return;
 
-/* 7 For every cv-qualified or cv-unqualified complete object type T, there
+/* 7 For every cv-qualified or cv-unqualified object type T, there
      exist candidate operator functions of the form
 
             T&      operator*(T*);
@@ -1945,6 +2161,7 @@ add_builtin_candidate (struct z_candidate **candidates, enum tree_code code,
 
     case INDIRECT_REF:
       if (TREE_CODE (type1) == POINTER_TYPE
+         && !uses_template_parms (TREE_TYPE (type1))
          && (TYPE_PTROB_P (type1)
              || TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE))
        break;
@@ -2307,12 +2524,11 @@ add_builtin_candidates (struct z_candidate **candidates, enum tree_code code,
 {
   int ref1, i;
   int enum_p = 0;
-  tree type, argtypes[3];
+  tree type, argtypes[3], t;
   /* TYPES[i] is the set of possible builtin-operator parameter types
-     we will consider for the Ith argument.  These are represented as
-     a TREE_LIST; the TREE_VALUE of each node is the potential
-     parameter type.  */
-  tree types[2];
+     we will consider for the Ith argument.  */
+  VEC(tree,gc) *types[2];
+  unsigned ix;
 
   for (i = 0; i < 3; ++i)
     {
@@ -2374,7 +2590,8 @@ add_builtin_candidates (struct z_candidate **candidates, enum tree_code code,
       ref1 = 0;
     }
 
-  types[0] = types[1] = NULL_TREE;
+  types[0] = make_tree_vector ();
+  types[1] = make_tree_vector ();
 
   for (i = 0; i < 2; ++i)
     {
@@ -2387,17 +2604,16 @@ add_builtin_candidates (struct z_candidate **candidates, enum tree_code code,
          if (i == 0 && code == MODIFY_EXPR && code2 == NOP_EXPR)
            return;
 
-         convs = lookup_conversions (argtypes[i],
-                                     /*lookup_template_convs_p=*/false);
+         convs = lookup_conversions (argtypes[i]);
 
          if (code == COND_EXPR)
            {
              if (real_lvalue_p (args[i]))
-               types[i] = tree_cons
-                 (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
+               VEC_safe_push (tree, gc, types[i],
+                              build_reference_type (argtypes[i]));
 
-             types[i] = tree_cons
-               (NULL_TREE, TYPE_MAIN_VARIANT (argtypes[i]), types[i]);
+             VEC_safe_push (tree, gc, types[i],
+                            TYPE_MAIN_VARIANT (argtypes[i]));
            }
 
          else if (! convs)
@@ -2413,54 +2629,60 @@ add_builtin_candidates (struct z_candidate **candidates, enum tree_code code,
                continue;
 
              if (code == COND_EXPR && TREE_CODE (type) == REFERENCE_TYPE)
-               types[i] = tree_cons (NULL_TREE, type, types[i]);
+               VEC_safe_push (tree, gc, types[i], type);
 
              type = non_reference (type);
              if (i != 0 || ! ref1)
                {
                  type = TYPE_MAIN_VARIANT (type_decays_to (type));
                  if (enum_p && TREE_CODE (type) == ENUMERAL_TYPE)
-                   types[i] = tree_cons (NULL_TREE, type, types[i]);
+                   VEC_safe_push (tree, gc, types[i], type);
                  if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
                    type = type_promotes_to (type);
                }
 
-             if (! value_member (type, types[i]))
-               types[i] = tree_cons (NULL_TREE, type, types[i]);
+             if (! vec_member (type, types[i]))
+               VEC_safe_push (tree, gc, types[i], type);
            }
        }
       else
        {
          if (code == COND_EXPR && real_lvalue_p (args[i]))
-           types[i] = tree_cons
-             (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
+           VEC_safe_push (tree, gc, types[i],
+                          build_reference_type (argtypes[i]));
          type = non_reference (argtypes[i]);
          if (i != 0 || ! ref1)
            {
              type = TYPE_MAIN_VARIANT (type_decays_to (type));
              if (enum_p && UNSCOPED_ENUM_P (type))
-               types[i] = tree_cons (NULL_TREE, type, types[i]);
+               VEC_safe_push (tree, gc, types[i], type);
              if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
                type = type_promotes_to (type);
            }
-         types[i] = tree_cons (NULL_TREE, type, types[i]);
+         VEC_safe_push (tree, gc, types[i], type);
        }
     }
 
   /* Run through the possible parameter types of both arguments,
      creating candidates with those parameter types.  */
-  for (; types[0]; types[0] = TREE_CHAIN (types[0]))
+  FOR_EACH_VEC_ELT_REVERSE (tree, types[0], ix, t)
     {
-      if (types[1])
-       for (type = types[1]; type; type = TREE_CHAIN (type))
+      unsigned jx;
+      tree u;
+
+      if (!VEC_empty (tree, types[1]))
+       FOR_EACH_VEC_ELT_REVERSE (tree, types[1], jx, u)
          add_builtin_candidate
-           (candidates, code, code2, fnname, TREE_VALUE (types[0]),
-            TREE_VALUE (type), args, argtypes, flags);
+           (candidates, code, code2, fnname, t,
+            u, args, argtypes, flags);
       else
        add_builtin_candidate
-         (candidates, code, code2, fnname, TREE_VALUE (types[0]),
+         (candidates, code, code2, fnname, t,
           NULL_TREE, args, argtypes, flags);
     }
+
+  release_tree_vector (types[0]);
+  release_tree_vector (types[1]);
 }
 
 
@@ -2494,6 +2716,7 @@ add_template_candidate_real (struct z_candidate **candidates, tree tmpl,
   struct z_candidate *cand;
   int i;
   tree fn;
+  struct rejection_reason *reason = NULL;
 
   /* We don't do deduction on the in-charge parameter, the VTT
      parameter or 'this'.  */
@@ -2612,7 +2835,7 @@ add_template_candidate_real (struct z_candidate **candidates, tree tmpl,
   return cand;
  fail:
   return add_candidate (candidates, tmpl, first_arg, arglist, nargs, NULL,
-                       access_path, conversion_path, 0);
+                       access_path, conversion_path, 0, reason);
 }
 
 
@@ -2703,7 +2926,7 @@ build_this (tree obj)
   if (processing_template_decl)
     return build_address (obj);
 
-  return cp_build_unary_op (ADDR_EXPR, obj, 0, tf_warning_or_error);
+  return cp_build_addr_expr (obj, tf_warning_or_error);
 }
 
 /* Returns true iff functions are equivalent. Equivalent functions are
@@ -2723,6 +2946,21 @@ equal_functions (tree fn1, tree fn2)
   return fn1 == fn2;
 }
 
+/* Print information about a candidate being rejected due to INFO.  */
+
+static void
+print_conversion_rejection (location_t loc, struct conversion_info *info)
+{
+  if (info->n_arg == -1)
+    /* Conversion of implicit `this' argument failed.  */
+    inform (loc, "  no known conversion for implicit "
+           "%<this%> parameter from %qT to %qT",
+           info->from_type, info->to_type);
+  else
+    inform (loc, "  no known conversion for argument %d from %qT to %qT",
+           info->n_arg+1, info->from_type, info->to_type);
+}
+
 /* Print information about one overload candidate CANDIDATE.  MSGSTR
    is the text to print before the candidate itself.
 
@@ -2733,38 +2971,68 @@ equal_functions (tree fn1, tree fn2)
 static void
 print_z_candidate (const char *msgstr, struct z_candidate *candidate)
 {
+  const char *msg = (msgstr == NULL
+                    ? ""
+                    : ACONCAT ((msgstr, " ", NULL)));
+  location_t loc = location_of (candidate->fn);
+
   if (TREE_CODE (candidate->fn) == IDENTIFIER_NODE)
     {
       if (candidate->num_convs == 3)
-       inform (input_location, "%s %D(%T, %T, %T) <built-in>", msgstr, candidate->fn,
+       inform (input_location, "%s%D(%T, %T, %T) <built-in>", msg, candidate->fn,
                candidate->convs[0]->type,
                candidate->convs[1]->type,
                candidate->convs[2]->type);
       else if (candidate->num_convs == 2)
-       inform (input_location, "%s %D(%T, %T) <built-in>", msgstr, candidate->fn,
+       inform (input_location, "%s%D(%T, %T) <built-in>", msg, candidate->fn,
                candidate->convs[0]->type,
                candidate->convs[1]->type);
       else
-       inform (input_location, "%s %D(%T) <built-in>", msgstr, candidate->fn,
+       inform (input_location, "%s%D(%T) <built-in>", msg, candidate->fn,
                candidate->convs[0]->type);
     }
   else if (TYPE_P (candidate->fn))
-    inform (input_location, "%s %T <conversion>", msgstr, candidate->fn);
+    inform (input_location, "%s%T <conversion>", msg, candidate->fn);
   else if (candidate->viable == -1)
-    inform (input_location, "%s %+#D <near match>", msgstr, candidate->fn);
+    inform (loc, "%s%#D <near match>", msg, candidate->fn);
   else if (DECL_DELETED_FN (STRIP_TEMPLATE (candidate->fn)))
-    inform (input_location, "%s %+#D <deleted>", msgstr, candidate->fn);
+    inform (loc, "%s%#D <deleted>", msg, candidate->fn);
   else
-    inform (input_location, "%s %+#D", msgstr, candidate->fn);
+    inform (loc, "%s%#D", msg, candidate->fn);
+  /* Give the user some information about why this candidate failed.  */
+  if (candidate->reason != NULL)
+    {
+      struct rejection_reason *r = candidate->reason;
+
+      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);
+         break;
+       case rr_arg_conversion:
+         print_conversion_rejection (loc, &r->u.conversion);
+         break;
+       case rr_bad_arg_conversion:
+         print_conversion_rejection (loc, &r->u.bad_conversion);
+         break;
+       case rr_none:
+       default:
+         /* This candidate didn't have any issues or we failed to
+            handle a particular code.  Either way...  */
+         gcc_unreachable ();
+       }
+    }
 }
 
 static void
-print_z_candidates (struct z_candidate *candidates)
+print_z_candidates (location_t loc, struct z_candidate *candidates)
 {
-  const char *str;
   struct z_candidate *cand1;
   struct z_candidate **cand2;
-  char *spaces;
+  int n_candidates;
 
   if (!candidates)
     return;
@@ -2806,14 +3074,12 @@ print_z_candidates (struct z_candidate *candidates)
        }
     }
 
-  str = candidates->next ? _("candidates are:") :  _("candidate is:");
-  spaces = NULL;
+  for (n_candidates = 0, cand1 = candidates; cand1; cand1 = cand1->next)
+    n_candidates++;
+
+  inform_n (loc, n_candidates, "candidate is:", "candidates are:");
   for (; candidates; candidates = candidates->next)
-    {
-      print_z_candidate (spaces ? spaces : str, candidates);
-      spaces = spaces ? spaces : get_spaces (str);
-    }
-  free (spaces);
+    print_z_candidate (NULL, candidates);
 }
 
 /* USER_SEQ is a user-defined conversion sequence, beginning with a
@@ -2945,8 +3211,7 @@ build_user_type_conversion_1 (tree totype, tree expr, int flags)
             reference to it)...  */
        }
       else
-       conv_fns = lookup_conversions (fromtype,
-                                      /*lookup_template_convs_p=*/true);
+       conv_fns = lookup_conversions (fromtype);
     }
 
   candidates = 0;
@@ -3051,9 +3316,20 @@ build_user_type_conversion_1 (tree totype, tree expr, int flags)
          cand->second_conv = ics;
 
          if (!ics)
-           cand->viable = 0;
+           {
+             tree rettype = TREE_TYPE (TREE_TYPE (cand->fn));
+             cand->viable = 0;
+             cand->reason = arg_conversion_rejection (NULL_TREE, -1,
+                                                      rettype, totype);
+           }
          else if (cand->viable == 1 && ics->bad_p)
-           cand->viable = -1;
+           {
+             tree rettype = TREE_TYPE (TREE_TYPE (cand->fn));
+             cand->viable = -1;
+             cand->reason
+               = bad_arg_conversion_rejection (NULL_TREE, -1,
+                                               rettype, totype);
+           }
        }
     }
 
@@ -3068,7 +3344,7 @@ build_user_type_conversion_1 (tree totype, tree expr, int flags)
        {
          error ("conversion from %qT to %qT is ambiguous",
                    fromtype, totype);
-         print_z_candidates (candidates);
+         print_z_candidates (location_of (expr), candidates);
        }
 
       cand = candidates;       /* any one will do */
@@ -3121,6 +3397,76 @@ build_user_type_conversion (tree totype, tree expr, int flags)
   return NULL_TREE;
 }
 
+/* Subroutine of convert_nontype_argument.
+
+   EXPR is an argument for a template non-type parameter of integral or
+   enumeration type.  Do any necessary conversions (that are permitted for
+   non-type arguments) to convert it to the parameter type.
+
+   If conversion is successful, returns the converted expression;
+   otherwise, returns error_mark_node.  */
+
+tree
+build_integral_nontype_arg_conv (tree type, tree expr, tsubst_flags_t complain)
+{
+  conversion *conv;
+  void *p;
+  tree t;
+
+  if (error_operand_p (expr))
+    return error_mark_node;
+
+  gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
+
+  /* Get the high-water mark for the CONVERSION_OBSTACK.  */
+  p = conversion_obstack_alloc (0);
+
+  conv = implicit_conversion (type, TREE_TYPE (expr), expr,
+                             /*c_cast_p=*/false,
+                             LOOKUP_IMPLICIT);
+
+  /* for a non-type template-parameter of integral or
+     enumeration type, integral promotions (4.5) and integral
+     conversions (4.7) are applied.  */
+  /* It should be sufficient to check the outermost conversion step, since
+     there are no qualification conversions to integer type.  */
+  if (conv)
+    switch (conv->kind)
+      {
+       /* A conversion function is OK.  If it isn't constexpr, we'll
+          complain later that the argument isn't constant.  */
+      case ck_user:
+       /* The lvalue-to-rvalue conversion is OK.  */
+      case ck_rvalue:
+      case ck_identity:
+       break;
+
+      case ck_std:
+       t = conv->u.next->type;
+       if (INTEGRAL_OR_ENUMERATION_TYPE_P (t))
+         break;
+
+       if (complain & tf_error)
+         error ("conversion from %qT to %qT not considered for "
+                "non-type template argument", t, type);
+       /* and fall through.  */
+
+      default:
+       conv = NULL;
+       break;
+      }
+
+  if (conv)
+    expr = convert_like (conv, expr, complain);
+  else
+    expr = error_mark_node;
+
+  /* Free all the conversions we allocated.  */
+  obstack_free (&conversion_obstack, p);
+
+  return expr;
+}
+
 /* Do any initial processing on the arguments to a function call.  */
 
 static VEC(tree,gc) *
@@ -3129,7 +3475,7 @@ resolve_args (VEC(tree,gc) *args)
   unsigned int ix;
   tree arg;
 
-  for (ix = 0; VEC_iterate (tree, args, ix, arg); ++ix)
+  FOR_EACH_VEC_ELT (tree, args, ix, arg)
     {
       if (error_operand_p (arg))
        return NULL;
@@ -3198,6 +3544,28 @@ perform_overload_resolution (tree fn,
   return cand;
 }
 
+/* Print an error message about being unable to build a call to FN with
+   ARGS.  ANY_VIABLE_P indicates whether any candidate functions could
+   be located; CANDIDATES is a possibly empty list of such
+   functions.  */
+
+static void
+print_error_for_call_failure (tree fn, VEC(tree,gc) *args, bool any_viable_p,
+                             struct z_candidate *candidates)
+{
+  tree name = DECL_NAME (OVL_CURRENT (fn));
+  location_t loc = location_of (name);
+
+  if (!any_viable_p)
+    error_at (loc, "no matching function for call to %<%D(%A)%>",
+             name, build_tree_list_vec (args));
+  else
+    error_at (loc, "call of overloaded %<%D(%A)%> is ambiguous",
+             name, build_tree_list_vec (args));
+  if (candidates)
+    print_z_candidates (loc, candidates);
+}
+
 /* Return an expression for a call to FN (a namespace-scope function,
    or a static member function) with the ARGS.  This may change
    ARGS.  */
@@ -3229,9 +3597,7 @@ build_new_function_call (tree fn, VEC(tree,gc) **args, bool koenig_p,
       if (!fn)
        {
          if (complain & tf_error)
-           error ("no matching function for call to %<%D(%A)%>",
-                  DECL_NAME (OVL_CURRENT (orig_fn)),
-                  build_tree_list_vec (*args));
+           print_error_for_call_failure (orig_fn, *args, false, NULL);
          return error_mark_node;
        }
     }
@@ -3250,14 +3616,7 @@ build_new_function_call (tree fn, VEC(tree,gc) **args, bool koenig_p,
            return cp_build_function_call_vec (candidates->fn, args, complain);
          if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
            fn = TREE_OPERAND (fn, 0);
-         if (!any_viable_p)
-           error ("no matching function for call to %<%D(%A)%>",
-                  DECL_NAME (OVL_CURRENT (fn)), build_tree_list_vec (*args));
-         else
-           error ("call of overloaded %<%D(%A)%> is ambiguous",
-                  DECL_NAME (OVL_CURRENT (fn)), build_tree_list_vec (*args));
-         if (candidates)
-           print_z_candidates (candidates);
+         print_error_for_call_failure (fn, *args, any_viable_p, candidates);
        }
       result = error_mark_node;
     }
@@ -3315,14 +3674,7 @@ build_operator_new_call (tree fnname, VEC(tree,gc) **args,
      and give up.  */
   if (!cand)
     {
-      if (!any_viable_p)
-       error ("no matching function for call to %<%D(%A)%>",
-              DECL_NAME (OVL_CURRENT (fns)), build_tree_list_vec (*args));
-      else
-       error ("call of overloaded %<%D(%A)%> is ambiguous",
-              DECL_NAME (OVL_CURRENT (fns)), build_tree_list_vec (*args));
-      if (candidates)
-       print_z_candidates (candidates);
+      print_error_for_call_failure (fns, *args, any_viable_p, candidates);
       return error_mark_node;
     }
 
@@ -3432,7 +3784,7 @@ build_op_call (tree obj, VEC(tree,gc) **args, tsubst_flags_t complain)
                      LOOKUP_NORMAL, &candidates);
     }
 
-  convs = lookup_conversions (type, /*lookup_template_convs_p=*/true);
+  convs = lookup_conversions (type);
 
   for (; convs; convs = TREE_CHAIN (convs))
     {
@@ -3472,7 +3824,7 @@ build_op_call (tree obj, VEC(tree,gc) **args, tsubst_flags_t complain)
         {
           error ("no match for call to %<(%T) (%A)%>", TREE_TYPE (obj),
                 build_tree_list_vec (*args));
-          print_z_candidates (candidates);
+          print_z_candidates (location_of (TREE_TYPE (obj)), candidates);
         }
       result = error_mark_node;
     }
@@ -3485,7 +3837,7 @@ build_op_call (tree obj, VEC(tree,gc) **args, tsubst_flags_t complain)
             {
               error ("call of %<(%T) (%A)%> is ambiguous", 
                      TREE_TYPE (obj), build_tree_list_vec (*args));
-              print_z_candidates (candidates);
+              print_z_candidates (location_of (TREE_TYPE (obj)), candidates);
             }
          result = error_mark_node;
        }
@@ -3904,7 +4256,7 @@ build_conditional_expr (tree arg1, tree arg2, tree arg3,
           if (complain & tf_error)
             {
               op_error (COND_EXPR, NOP_EXPR, arg1, arg2, arg3, FALSE);
-              print_z_candidates (candidates);
+              print_z_candidates (location_of (arg1), candidates);
             }
          return error_mark_node;
        }
@@ -3914,7 +4266,7 @@ build_conditional_expr (tree arg1, tree arg2, tree arg3,
           if (complain & tf_error)
             {
               op_error (COND_EXPR, NOP_EXPR, arg1, arg2, arg3, FALSE);
-              print_z_candidates (candidates);
+              print_z_candidates (location_of (arg1), candidates);
             }
          return error_mark_node;
        }
@@ -3977,6 +4329,10 @@ build_conditional_expr (tree arg1, tree arg2, tree arg3,
       /* In this case, there is always a common type.  */
       result_type = type_after_usual_arithmetic_conversions (arg2_type,
                                                             arg3_type);
+      do_warn_double_promotion (result_type, arg2_type, arg3_type,
+                               "implicit conversion from %qT to %qT to "
+                               "match other result of conditional",
+                               input_location);
 
       if (TREE_CODE (arg2_type) == ENUMERAL_TYPE
          && TREE_CODE (arg3_type) == ENUMERAL_TYPE)
@@ -4429,7 +4785,7 @@ build_new_op (enum tree_code code, int flags, tree arg1, tree arg2, tree arg3,
                    /* ... Otherwise, report the more generic
                       "no matching operator found" error */
                    op_error (code, code2, arg1, arg2, arg3, FALSE);
-                   print_z_candidates (candidates);
+                   print_z_candidates (input_location, candidates);
                  }
            }
          result = error_mark_node;
@@ -4444,7 +4800,7 @@ build_new_op (enum tree_code code, int flags, tree arg1, tree arg2, tree arg3,
          if ((flags & LOOKUP_COMPLAIN) && (complain & tf_error))
            {
              op_error (code, code2, arg1, arg2, arg3, TRUE);
-             print_z_candidates (candidates);
+             print_z_candidates (input_location, candidates);
            }
          result = error_mark_node;
        }
@@ -5018,7 +5374,7 @@ convert_like_real (conversion *convs, tree expr, tree fn, int argnum,
        {
          int nelts = CONSTRUCTOR_NELTS (expr);
          if (nelts == 0)
-           expr = integer_zero_node;
+           expr = build_value_init (totype, tf_warning_or_error);
          else if (nelts == 1)
            expr = CONSTRUCTOR_ELT (expr, 0)->value;
          else
@@ -5067,7 +5423,8 @@ convert_like_real (conversion *convs, tree expr, tree fn, int argnum,
                                          1, false, false, complain);
            if (sub == error_mark_node)
              return sub;
-           check_narrowing (TREE_TYPE (sub), val);
+           if (!BRACE_ENCLOSED_INITIALIZER_P (val))
+             check_narrowing (TREE_TYPE (sub), val);
            CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_ctor), NULL_TREE, sub);
          }
        /* Build up the array.  */
@@ -5116,7 +5473,7 @@ convert_like_real (conversion *convs, tree expr, tree fn, int argnum,
          /* We are going to bind a reference directly to a base-class
             subobject of EXPR.  */
          /* Build an expression for `*((base*) &expr)'.  */
-         expr = cp_build_unary_op (ADDR_EXPR, expr, 0, complain);
+         expr = cp_build_addr_expr (expr, complain);
          expr = convert_to_base (expr, build_pointer_type (totype),
                                  !c_cast_p, /*nonnull=*/true, complain);
          expr = cp_build_indirect_ref (expr, RO_IMPLICIT_CONVERSION, complain);
@@ -5132,6 +5489,10 @@ 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 (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)
        {
@@ -5165,16 +5526,21 @@ convert_like_real (conversion *convs, tree expr, tree fn, int argnum,
            VA_ARG_EXPR and CONSTRUCTOR expressions are special cases
            that need temporaries, even when their types are reference
            compatible with the type of reference being bound, so the
-           upcoming call to cp_build_unary_op (ADDR_EXPR, expr, ...)
-           doesn't fail.  */
+           upcoming call to cp_build_addr_expr doesn't fail.  */
        if (convs->need_temporary_p
            || TREE_CODE (expr) == CONSTRUCTOR
            || TREE_CODE (expr) == VA_ARG_EXPR)
          {
-           tree type = convs->u.next->type;
+           /* Otherwise, a temporary of type "cv1 T1" is created and
+              initialized from the initializer expression using the rules
+              for a non-reference copy-initialization (8.5).  */
+
+           tree type = TREE_TYPE (ref_type);
            cp_lvalue_kind lvalue = real_lvalue_p (expr);
 
-           if (!CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (ref_type))
+           gcc_assert (same_type_ignoring_top_level_qualifiers_p
+                       (type, convs->u.next->type));
+           if (!CP_TYPE_CONST_NON_VOLATILE_P (type)
                && !TYPE_REF_IS_RVALUE (ref_type))
              {
                if (complain & tf_error)
@@ -5217,7 +5583,7 @@ convert_like_real (conversion *convs, tree expr, tree fn, int argnum,
 
        /* Take the address of the thing to which we will bind the
           reference.  */
-       expr = cp_build_unary_op (ADDR_EXPR, expr, 1, complain);
+       expr = cp_build_addr_expr (expr, complain);
        if (expr == error_mark_node)
          return error_mark_node;
 
@@ -5269,11 +5635,14 @@ convert_like_real (conversion *convs, tree expr, tree fn, int argnum,
 tree
 convert_arg_to_ellipsis (tree arg)
 {
+  tree arg_type;
+
   /* [expr.call]
 
      The lvalue-to-rvalue, array-to-pointer, and function-to-pointer
      standard conversions are performed.  */
   arg = decay_conversion (arg);
+  arg_type = TREE_TYPE (arg);
   /* [expr.call]
 
      If the argument has integral or enumeration type that is subject
@@ -5281,21 +5650,29 @@ convert_arg_to_ellipsis (tree arg)
      type that is subject to the floating point promotion
      (_conv.fpprom_), the value of the argument is converted to the
      promoted type before the call.  */
-  if (TREE_CODE (TREE_TYPE (arg)) == REAL_TYPE
-      && (TYPE_PRECISION (TREE_TYPE (arg))
+  if (TREE_CODE (arg_type) == REAL_TYPE
+      && (TYPE_PRECISION (arg_type)
          < TYPE_PRECISION (double_type_node))
-      && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (arg))))
-    arg = convert_to_real (double_type_node, arg);
-  else if (NULLPTR_TYPE_P (TREE_TYPE (arg)))
+      && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (arg_type)))
+    {
+      if (warn_double_promotion && !c_inhibit_evaluation_warnings)
+       warning (OPT_Wdouble_promotion,
+                "implicit conversion from %qT to %qT when passing "
+                "argument to function",
+                arg_type, double_type_node);
+      arg = convert_to_real (double_type_node, arg);
+    }
+  else if (NULLPTR_TYPE_P (arg_type))
     arg = null_pointer_node;
-  else if (INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (arg)))
+  else if (INTEGRAL_OR_ENUMERATION_TYPE_P (arg_type))
     arg = perform_integral_promotions (arg);
 
   arg = require_complete_type (arg);
+  arg_type = TREE_TYPE (arg);
 
   if (arg != error_mark_node
-      && (type_has_nontrivial_copy_init (TREE_TYPE (arg))
-         || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (arg))))
+      && (type_has_nontrivial_copy_init (arg_type)
+         || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (arg_type)))
     {
       /* [expr.call] 5.2.2/7:
         Passing a potentially-evaluated argument of class type (Clause 9)
@@ -5310,7 +5687,7 @@ convert_arg_to_ellipsis (tree arg)
         it is not potentially-evaluated.  */
       if (cp_unevaluated_operand == 0)
        error ("cannot pass objects of non-trivially-copyable "
-              "type %q#T through %<...%>", TREE_TYPE (arg));
+              "type %q#T through %<...%>", arg_type);
     }
 
   return arg;
@@ -5369,10 +5746,17 @@ cxx_type_promotes_to (tree type)
 }
 
 /* ARG is a default argument expression being passed to a parameter of
-   the indicated TYPE, which is a parameter to FN.  Do any required
-   conversions.  Return the converted value.  */
+   the indicated TYPE, which is a parameter to FN.  PARMNUM is the
+   zero-based argument number.  Do any required conversions.  Return
+   the converted value.  */
 
 static GTY(()) VEC(tree,gc) *default_arg_context;
+void
+push_defarg_context (tree fn)
+{ VEC_safe_push (tree, gc, default_arg_context, fn); }
+void
+pop_defarg_context (void)
+{ VEC_pop (tree, default_arg_context); }
 
 tree
 convert_default_arg (tree type, tree arg, tree fn, int parmnum)
@@ -5380,24 +5764,27 @@ convert_default_arg (tree type, tree arg, tree fn, int parmnum)
   int i;
   tree t;
 
-  /* If the ARG is an unparsed default argument expression, the
-     conversion cannot be performed.  */
-  if (TREE_CODE (arg) == DEFAULT_ARG)
-    {
-      error ("the default argument for parameter %d of %qD has "
-            "not yet been parsed",
-            parmnum, fn);
-      return error_mark_node;
-    }
+  /* See through clones.  */
+  fn = DECL_ORIGIN (fn);
 
   /* Detect recursion.  */
-  for (i = 0; VEC_iterate (tree, default_arg_context, i, t); ++i)
+  FOR_EACH_VEC_ELT (tree, default_arg_context, i, t)
     if (t == fn)
       {
        error ("recursive evaluation of default argument for %q#D", fn);
        return error_mark_node;
       }
-  VEC_safe_push (tree, gc, default_arg_context, fn);
+
+  /* If the ARG is an unparsed default argument expression, the
+     conversion cannot be performed.  */
+  if (TREE_CODE (arg) == DEFAULT_ARG)
+    {
+      error ("call to %qD uses the default argument for parameter %P, which "
+            "is not yet defined", fn, parmnum);
+      return error_mark_node;
+    }
+
+  push_defarg_context (fn);
 
   if (fn && DECL_TEMPLATE_INFO (fn))
     arg = tsubst_default_argument (fn, type, arg);
@@ -5437,7 +5824,7 @@ convert_default_arg (tree type, tree arg, tree fn, int parmnum)
     }
   pop_deferring_access_checks();
 
-  VEC_pop (tree, default_arg_context);
+  pop_defarg_context ();
 
   return arg;
 }
@@ -5588,7 +5975,7 @@ build_over_call (struct z_candidate *cand, int flags, tsubst_flags_t complain)
          ++nargs;
          alcarray = XALLOCAVEC (tree, nargs);
          alcarray[0] = first_arg;
-         for (ix = 0; VEC_iterate (tree, args, ix, arg); ++ix)
+         FOR_EACH_VEC_ELT (tree, args, ix, arg)
            alcarray[ix + 1] = arg;
          argarray = alcarray;
        }
@@ -5598,7 +5985,7 @@ build_over_call (struct z_candidate *cand, int flags, tsubst_flags_t complain)
       if (TREE_THIS_VOLATILE (fn) && cfun)
        current_function_returns_abnormally = 1;
       if (!VOID_TYPE_P (return_type))
-       require_complete_type (return_type);
+       require_complete_type_sfinae (return_type, complain);
       return convert_from_reference (expr);
     }
 
@@ -5649,15 +6036,9 @@ build_over_call (struct z_candidate *cand, int flags, tsubst_flags_t complain)
        access_fn = fn;
       if (flags & LOOKUP_SPECULATIVE)
        {
-         /* If we're checking for implicit delete, we don't want access
-            control errors.  */
-         if (!accessible_p (cand->access_path, access_fn, true))
-           {
-             /* Unless we're under maybe_explain_implicit_delete.  */
-             if (flags & LOOKUP_COMPLAIN)
-               enforce_access (cand->access_path, access_fn, fn);
-             return error_mark_node;
-           }
+         if (!speculative_access_check (cand->access_path, access_fn, fn,
+                                        !!(flags & LOOKUP_COMPLAIN)))
+           return error_mark_node;
        }
       else
        perform_or_defer_access_check (cand->access_path, access_fn, fn);
@@ -5881,15 +6262,9 @@ build_over_call (struct z_candidate *cand, int flags, tsubst_flags_t complain)
       else
        arg = cp_build_indirect_ref (arg, RO_NULL, complain);
 
-      if (TREE_CODE (arg) == TARGET_EXPR
-         && TARGET_EXPR_LIST_INIT_P (arg))
-       {
-         /* Copy-list-initialization doesn't require the constructor
-            to be defined.  */
-       }
       /* [class.copy]: the copy constructor is implicitly defined even if
         the implementation elided its use.  */
-      else if (!trivial)
+      if (!trivial || DECL_DELETED_FN (fn))
        {
          mark_used (fn);
          already_used = true;
@@ -5900,9 +6275,7 @@ build_over_call (struct z_candidate *cand, int flags, tsubst_flags_t complain)
         INIT_EXPR to collapse the temp into our target.  Otherwise, if the
         ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a
         temp or an INIT_EXPR otherwise.  */
-      fa = (cand->first_arg != NULL_TREE
-           ? cand->first_arg
-           : VEC_index (tree, args, 0));
+      fa = argarray[0];
       if (integer_zerop (fa))
        {
          if (TREE_CODE (arg) == TARGET_EXPR)
@@ -5920,7 +6293,8 @@ build_over_call (struct z_candidate *cand, int flags, tsubst_flags_t complain)
        }
     }
   else if (DECL_OVERLOADED_OPERATOR_P (fn) == NOP_EXPR
-          && trivial_fn_p (fn))
+          && trivial_fn_p (fn)
+          && !DECL_DELETED_FN (fn))
     {
       tree to = stabilize_reference
        (cp_build_indirect_ref (argarray[0], RO_NULL, complain));
@@ -5953,7 +6327,7 @@ build_over_call (struct z_candidate *cand, int flags, tsubst_flags_t complain)
 
          arg2 = TYPE_SIZE_UNIT (as_base);
          arg1 = arg;
-         arg0 = cp_build_unary_op (ADDR_EXPR, to, 0, complain);
+         arg0 = cp_build_addr_expr (to, complain);
 
          if (!can_trust_pointer_alignment ())
            {
@@ -5977,6 +6351,7 @@ build_over_call (struct z_candidate *cand, int flags, tsubst_flags_t complain)
 
       return val;
     }
+  /* FIXME handle trivial default constructor and destructor, too.  */
 
   if (!already_used)
     mark_used (fn);
@@ -6025,7 +6400,8 @@ build_cxx_call (tree fn, int nargs, tree *argarray)
   fndecl = get_callee_fndecl (fn);
   if ((!fndecl || !TREE_NOTHROW (fndecl))
       && at_function_scope_p ()
-      && cfun)
+      && cfun
+      && cp_function_chain)
     cp_function_chain->can_throw = 1;
 
   /* Check that arguments to builtin functions match the expectations.  */
@@ -6095,7 +6471,7 @@ build_java_interface_fn_ref (tree fn, tree instance)
 
   /* Determine the itable index of FN.  */
   i = 1;
-  for (method = TYPE_METHODS (iface); method; method = TREE_CHAIN (method))
+  for (method = TYPE_METHODS (iface); method; method = DECL_CHAIN (method))
     {
       if (!DECL_VIRTUAL_P (method))
        continue;
@@ -6168,7 +6544,7 @@ build_special_member_call (tree instance, tree name, VEC(tree,gc) **args,
   if (TYPE_P (binfo))
     {
       /* Resolve the name.  */
-      if (!complete_type_or_else (binfo, NULL_TREE))
+      if (!complete_type_or_maybe_complain (binfo, NULL_TREE, complain))
        return error_mark_node;
 
       binfo = TYPE_BINFO (binfo);
@@ -6227,7 +6603,7 @@ build_special_member_call (tree instance, tree name, VEC(tree,gc) **args,
       /* If the current function is a complete object constructor
         or destructor, then we fetch the VTT directly.
         Otherwise, we look it up using the VTT we were given.  */
-      vtt = TREE_CHAIN (CLASSTYPE_VTABLES (current_class_type));
+      vtt = DECL_CHAIN (CLASSTYPE_VTABLES (current_class_type));
       vtt = decay_conversion (vtt);
       vtt = build3 (COND_EXPR, TREE_TYPE (vtt),
                    build2 (EQ_EXPR, boolean_type_node,
@@ -6472,7 +6848,7 @@ build_new_method_call (tree instance, tree fns, VEC(tree,gc) **args,
     {
       if (complain & tf_error)
        {
-         if (!COMPLETE_TYPE_P (basetype))
+         if (!COMPLETE_OR_OPEN_TYPE_P (basetype))
            cxx_incomplete_type_error (instance_ptr, basetype);
          else if (optype)
            error ("no matching function for call to %<%T::operator %T(%A)%#V%>",
@@ -6494,7 +6870,7 @@ build_new_method_call (tree instance, tree fns, VEC(tree,gc) **args,
              if (free_p)
                free (pretty_name);
            }
-         print_z_candidates (candidates);
+         print_z_candidates (location_of (name), candidates);
        }
       call = error_mark_node;
     }
@@ -6515,7 +6891,7 @@ build_new_method_call (tree instance, tree fns, VEC(tree,gc) **args,
                arglist = TREE_CHAIN (arglist);
              error ("call of overloaded %<%s(%A)%> is ambiguous", pretty_name,
                     arglist);
-             print_z_candidates (candidates);
+             print_z_candidates (location_of (name), candidates);
              if (free_p)
                free (pretty_name);
            }
@@ -6806,9 +7182,8 @@ compare_ics (conversion *ics1, conversion *ics2)
       /* We couldn't make up our minds; try to figure it out below.  */
     }
 
-  if (ics1->ellipsis_p || ics1->kind == ck_list)
-    /* Both conversions are ellipsis conversions or both are building a
-       std::initializer_list.  */
+  if (ics1->ellipsis_p)
+    /* Both conversions are ellipsis conversions.  */
     return 0;
 
   /* User-defined  conversion sequence U1 is a better conversion sequence
@@ -6817,16 +7192,24 @@ compare_ics (conversion *ics1, conversion *ics2)
      ond standard conversion sequence of U1 is  better  than  the  second
      standard conversion sequence of U2.  */
 
-  if (ics1->user_conv_p)
+  /* Handle list-conversion with the same code even though it isn't always
+     ranked as a user-defined conversion and it doesn't have a second
+     standard conversion sequence; it will still have the desired effect.
+     Specifically, we need to do the reference binding comparison at the
+     end of this function.  */
+
+  if (ics1->user_conv_p || ics1->kind == ck_list)
     {
       conversion *t1;
       conversion *t2;
 
       for (t1 = ics1; t1->kind != ck_user; t1 = t1->u.next)
-       if (t1->kind == ck_ambig || t1->kind == ck_aggr)
+       if (t1->kind == ck_ambig || t1->kind == ck_aggr
+           || t1->kind == ck_list)
          break;
       for (t2 = ics2; t2->kind != ck_user; t2 = t2->u.next)
-       if (t2->kind == ck_ambig || t2->kind == ck_aggr)
+       if (t2->kind == ck_ambig || t2->kind == ck_aggr
+           || t2->kind == ck_list)
          break;
 
       if (t1->kind != t2->kind)
@@ -7765,9 +8148,33 @@ set_up_extended_ref_temp (tree decl, tree expr, tree *cleanup, tree *initp)
      VAR.  */
   if (TREE_CODE (expr) != TARGET_EXPR)
     expr = get_target_expr (expr);
-  /* Create the INIT_EXPR that will initialize the temporary
-     variable.  */
-  init = build2 (INIT_EXPR, type, var, expr);
+
+  /* If the initializer is constant, put it in DECL_INITIAL so we get
+     static initialization and use in constant expressions.  */
+  init = maybe_constant_init (expr);
+  if (TREE_CONSTANT (init))
+    {
+      if (literal_type_p (type) && CP_TYPE_CONST_NON_VOLATILE_P (type))
+       {
+         /* 5.19 says that a constant expression can include an
+            lvalue-rvalue conversion applied to "a glvalue of literal type
+            that refers to a non-volatile temporary object initialized
+            with a constant expression".  Rather than try to communicate
+            that this VAR_DECL is a temporary, just mark it constexpr.
+
+            Currently this is only useful for initializer_list temporaries,
+            since reference vars can't appear in constant expressions.  */
+         DECL_DECLARED_CONSTEXPR_P (var) = true;
+         DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var) = true;
+         TREE_CONSTANT (var) = true;
+       }
+      DECL_INITIAL (var) = init;
+      init = NULL_TREE;
+    }
+  else
+    /* Create the INIT_EXPR that will initialize the temporary
+       variable.  */
+    init = build2 (INIT_EXPR, type, var, expr);
   if (at_function_scope_p ())
     {
       add_decl_expr (var);
@@ -7925,11 +8332,12 @@ initialize_reference (tree type, tree expr, tree decl, tree *cleanup,
                                        build_pointer_type (base_conv_type),
                                        /*check_access=*/true,
                                        /*nonnull=*/true, complain);
-             expr = build2 (COMPOUND_EXPR, TREE_TYPE (expr), init, expr);
+             if (init)
+               expr = build2 (COMPOUND_EXPR, TREE_TYPE (expr), init, expr);
            }
          else
            /* Take the address of EXPR.  */
-           expr = cp_build_unary_op (ADDR_EXPR, expr, 0, tf_warning_or_error);
+           expr = cp_build_addr_expr (expr, tf_warning_or_error);
          /* If a BASE_CONV was required, perform it now.  */
          if (base_conv_type)
            expr = (perform_implicit_conversion