OSDN Git Service

2012-06-04 Paolo Carlini <paolo.carlini@oracle.com>
[pf3gnuchains/gcc-fork.git] / gcc / cp / call.c
index bdbede7..5e74f56 100644 (file)
@@ -1,7 +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, 2011
+   2010, 2011, 2012
    Free Software Foundation, Inc.
    Contributed by Michael Tiemann (tiemann@cygnus.com) and
    modified by Brendan Kehoe (brendan@cygnus.com).
@@ -111,12 +111,15 @@ struct conversion {
     /* The next conversion in the chain.  Since the conversions are
        arranged from outermost to innermost, the NEXT conversion will
        actually be performed before this conversion.  This variant is
-       used only when KIND is neither ck_identity nor ck_ambig.  */
+       used only when KIND is neither ck_identity, ck_ambig nor
+       ck_list.  Please use the next_conversion function instead
+       of using this field directly.  */
     conversion *next;
     /* The expression at the beginning of the conversion chain.  This
        variant is used only if KIND is ck_identity or ck_ambig.  */
     tree expr;
-    /* The array of conversions for an initializer_list.  */
+    /* The array of conversions for an initializer_list, so this
+       variant is used only when KIN D is ck_list.  */
     conversion **list;
   } u;
   /* The function candidate corresponding to this conversion
@@ -193,6 +196,7 @@ static conversion *standard_conversion (tree, tree, tree, bool, int);
 static conversion *reference_binding (tree, tree, tree, bool, int);
 static conversion *build_conv (conversion_kind, tree, conversion *);
 static conversion *build_list_conv (tree, tree, int);
+static conversion *next_conversion (conversion *);
 static bool is_subseq (conversion *, conversion *);
 static conversion *maybe_handle_ref_bind (conversion **);
 static void maybe_handle_implicit_object (conversion **);
@@ -522,7 +526,7 @@ struct z_candidate {
   tree access_path;
   /* If FN is a non-static member function, the binfo indicating the
      subobject to which the `this' pointer should be converted if FN
-     is selected by overload resolution.  The type pointed to the by
+     is selected by overload resolution.  The type pointed to by
      the `this' pointer must correspond to the most derived class
      indicated by the CONVERSION_PATH.  */
   tree conversion_path;
@@ -549,10 +553,8 @@ null_ptr_cst_p (tree t)
     {
       /* Core issue 903 says only literal 0 is a null pointer constant.  */
       if (cxx_dialect < cxx0x)
-       {
-         t = integral_constant_value (t);
-         STRIP_NOPS (t);
-       }
+       t = integral_constant_value (t);
+      STRIP_NOPS (t);
       if (integer_zerop (t) && !TREE_OVERFLOW (t))
        return true;
     }
@@ -835,6 +837,21 @@ build_list_conv (tree type, tree ctor, int flags)
   return t;
 }
 
+/* Return the next conversion of the conversion chain (if applicable),
+   or NULL otherwise.  Please use this function instead of directly
+   accessing fields of struct conversion.  */
+
+static conversion *
+next_conversion (conversion *conv)
+{
+  if (conv == NULL
+      || conv->kind == ck_identity
+      || conv->kind == ck_ambig
+      || conv->kind == ck_list)
+    return NULL;
+  return conv->u.next;
+}
+
 /* Subroutine of build_aggr_conv: check whether CTOR, a braced-init-list,
    is a valid aggregate initializer for array type ATYPE.  */
 
@@ -1257,10 +1274,8 @@ standard_conversion (tree to, tree from, tree expr, bool c_cast_p,
       tree tbase = class_of_this_parm (tofn);
 
       if (!DERIVED_FROM_P (fbase, tbase)
-         || !same_type_p (TREE_TYPE (fromfn), TREE_TYPE (tofn))
-         || !compparms (TREE_CHAIN (TYPE_ARG_TYPES (fromfn)),
-                        TREE_CHAIN (TYPE_ARG_TYPES (tofn)))
-         || cp_type_quals (fbase) != cp_type_quals (tbase))
+         || !same_type_p (static_fn_type (fromfn),
+                          static_fn_type (tofn)))
        return NULL;
 
       from = build_memfn_type (fromfn, tbase, cp_type_quals (tbase));
@@ -1660,6 +1675,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
@@ -1716,15 +1737,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)
          && 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;
 
@@ -1935,16 +1954,18 @@ add_function_candidate (struct z_candidate **candidates,
             to handle move constructors and template constructors as well;
             the standardese should soon be updated similarly.  */
          if (ctype && i == 0 && (len-skip == 1)
-             && !(flags & LOOKUP_ONLYCONVERTING)
              && DECL_CONSTRUCTOR_P (fn)
              && parmtype != error_mark_node
              && (same_type_ignoring_top_level_qualifiers_p
                  (non_reference (parmtype), ctype)))
            {
-             lflags |= LOOKUP_COPY_PARM;
+             if (!(flags & LOOKUP_ONLYCONVERTING))
+               lflags |= LOOKUP_COPY_PARM;
              /* We allow user-defined conversions within init-lists, but
-                not for the copy constructor.  */
-             if (flags & LOOKUP_NO_COPY_CTOR_CONVERSION)
+                don't list-initialize the copy parm, as that would mean
+                using two levels of braces for the same type.  */
+             if ((flags & LOOKUP_LIST_INIT_CTOR)
+                 && BRACE_ENCLOSED_INITIALIZER_P (arg))
                lflags |= LOOKUP_NO_CONVERSION;
            }
          else
@@ -2578,6 +2599,21 @@ add_builtin_candidate (struct z_candidate **candidates, enum tree_code code,
          || MAYBE_CLASS_TYPE_P (type1)
          || TREE_CODE (type1) == ENUMERAL_TYPE))
     {
+      if (TYPE_PTR_P (type1) || TYPE_PTR_TO_MEMBER_P (type1))
+       {
+         tree cptype = composite_pointer_type (type1, type2,
+                                               error_mark_node,
+                                               error_mark_node,
+                                               CPO_CONVERSION,
+                                               tf_none);
+         if (cptype != error_mark_node)
+           {
+             build_builtin_candidate
+               (candidates, fnname, cptype, cptype, args, argtypes, flags);
+             return;
+           }
+       }
+
       build_builtin_candidate
        (candidates, fnname, type1, type1, args, argtypes, flags);
       build_builtin_candidate
@@ -3306,9 +3342,8 @@ add_list_candidates (tree fns, tree first_arg,
 
   gcc_assert (*candidates == NULL);
 
-  /* For list-initialization we consider explicit constructors, but
-     give an error if one is selected.  */
-  flags &= ~LOOKUP_ONLYCONVERTING;
+  /* We're looking for a ctor for list-initialization.  */
+  flags |= LOOKUP_LIST_INIT_CTOR;
   /* And we don't allow narrowing conversions.  We also use this flag to
      avoid the copy constructor call for copy-list-initialization.  */
   flags |= LOOKUP_NO_NARROWING;
@@ -3336,8 +3371,6 @@ add_list_candidates (tree fns, tree first_arg,
   flags &= ~LOOKUP_LIST_ONLY;
   /* We allow more user-defined conversions within an init-list.  */
   flags &= ~LOOKUP_NO_CONVERSION;
-  /* But not for the copy ctor.  */
-  flags |= LOOKUP_NO_COPY_CTOR_CONVERSION;
 
   add_candidates (fns, first_arg, args, NULL_TREE,
                  explicit_targs, template_only, conversion_path,
@@ -3354,7 +3387,7 @@ static struct z_candidate *
 build_user_type_conversion_1 (tree totype, tree expr, int flags)
 {
   struct z_candidate *candidates, *cand;
-  tree fromtype = TREE_TYPE (expr);
+  tree fromtype;
   tree ctors = NULL_TREE;
   tree conv_fns = NULL_TREE;
   conversion *conv = NULL;
@@ -3363,6 +3396,11 @@ build_user_type_conversion_1 (tree totype, tree expr, int flags)
   bool any_viable_p;
   int convflags;
 
+  if (!expr)
+    return NULL;
+
+  fromtype = TREE_TYPE (expr);
+
   /* We represent conversion within a hierarchy using RVALUE_CONV and
      BASE_CONV, as specified by [over.best.ics]; these become plain
      constructor calls, as specified in [dcl.init].  */
@@ -3807,6 +3845,9 @@ build_new_function_call (tree fn, VEC(tree,gc) **args, bool koenig_p,
        return error_mark_node;
     }
 
+  if (flag_tm)
+    tm_malloc_replacement (fn);
+
   /* If this function was found without using argument dependent
      lookup, then we want to ignore any undeclared friend
      functions.  */
@@ -4589,7 +4630,7 @@ build_conditional_expr_1 (tree arg1, tree arg2, tree arg3,
          && TREE_CODE (arg3_type) == ENUMERAL_TYPE)
         {
           if (complain & tf_warning)
-            warning (0
+            warning (OPT_Wenum_compare
                      "enumeral mismatch in conditional expression: %qT vs %qT",
                      arg2_type, arg3_type);
         }
@@ -4755,7 +4796,11 @@ add_candidates (tree fns, tree first_arg, const VEC(tree,gc) *args,
       if (DECL_CONSTRUCTOR_P (fn))
        {
          check_list_ctor = !!(flags & LOOKUP_LIST_ONLY);
-         check_converting = !!(flags & LOOKUP_ONLYCONVERTING);
+         /* For list-initialization we consider explicit constructors
+            and complain if one is chosen.  */
+         check_converting
+           = ((flags & (LOOKUP_ONLYCONVERTING|LOOKUP_LIST_INIT_CTOR))
+              == LOOKUP_ONLYCONVERTING);
        }
       else
        {
@@ -4835,32 +4880,10 @@ add_candidates (tree fns, tree first_arg, const VEC(tree,gc) *args,
     }
 }
 
-/* Even unsigned enum types promote to signed int.  We don't want to
-   issue -Wsign-compare warnings for this case.  Here ORIG_ARG is the
-   original argument and ARG is the argument after any conversions
-   have been applied.  We set TREE_NO_WARNING if we have added a cast
-   from an unsigned enum type to a signed integer type.  */
-
-static void
-avoid_sign_compare_warnings (tree orig_arg, tree arg)
-{
-  if (orig_arg != NULL_TREE
-      && arg != NULL_TREE
-      && orig_arg != arg
-      && TREE_CODE (TREE_TYPE (orig_arg)) == ENUMERAL_TYPE
-      && TYPE_UNSIGNED (TREE_TYPE (orig_arg))
-      && INTEGRAL_TYPE_P (TREE_TYPE (arg))
-      && !TYPE_UNSIGNED (TREE_TYPE (arg)))
-    TREE_NO_WARNING (arg) = 1;
-}
-
 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;
-  tree orig_arg3 = arg3;
   struct z_candidate *candidates = 0, *cand;
   VEC(tree,gc) *arglist;
   tree fnname;
@@ -5155,10 +5178,6 @@ build_new_op_1 (enum tree_code code, int flags, tree arg1, tree arg2, tree arg3,
     return result;
 
  builtin:
-  avoid_sign_compare_warnings (orig_arg1, arg1);
-  avoid_sign_compare_warnings (orig_arg2, arg2);
-  avoid_sign_compare_warnings (orig_arg3, arg3);
-
   switch (code)
     {
     case MODIFY_EXPR:
@@ -5204,6 +5223,7 @@ build_new_op_1 (enum tree_code code, int flags, tree arg1, tree arg2, tree arg3,
     case POSTDECREMENT_EXPR:
     case REALPART_EXPR:
     case IMAGPART_EXPR:
+    case ABS_EXPR:
       return cp_build_unary_op (code, arg1, candidates != 0, complain);
 
     case ARRAY_REF:
@@ -5510,10 +5530,9 @@ build_temp (tree expr, tree type, int flags,
 static void
 conversion_null_warnings (tree totype, tree expr, tree fn, int argnum)
 {
-  tree t = non_reference (totype);
-
   /* Issue warnings about peculiar, but valid, uses of NULL.  */
-  if (expr == null_node && TREE_CODE (t) != BOOLEAN_TYPE && ARITHMETIC_TYPE_P (t))
+  if (expr == null_node && TREE_CODE (totype) != BOOLEAN_TYPE
+      && ARITHMETIC_TYPE_P (totype))
     {
       if (fn)
        warning_at (input_location, OPT_Wconversion_null,
@@ -5521,11 +5540,12 @@ conversion_null_warnings (tree totype, tree expr, tree fn, int argnum)
                    argnum, fn);
       else
        warning_at (input_location, OPT_Wconversion_null,
-                   "converting to non-pointer type %qT from NULL", t);
+                   "converting to non-pointer type %qT from NULL", totype);
     }
 
   /* Issue warnings if "false" is converted to a NULL pointer */
-  else if (expr == boolean_false_node && POINTER_TYPE_P (t))
+  else if (TREE_CODE (TREE_TYPE (expr)) == BOOLEAN_TYPE
+          && TYPE_PTR_P (totype))
     {
       if (fn)
        warning_at (input_location, OPT_Wconversion_null,
@@ -5533,7 +5553,7 @@ conversion_null_warnings (tree totype, tree expr, tree fn, int argnum)
                    "of %qD", argnum, fn);
       else
        warning_at (input_location, OPT_Wconversion_null,
-                   "converting %<false%> to pointer type %qT", t);
+                   "converting %<false%> to pointer type %qT", totype);
     }
 }
 
@@ -5577,7 +5597,7 @@ 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 = t->u.next)
+      for (; t ; t = next_conversion (t))
        {
          if (t->kind == ck_user && t->cand->reason)
            {
@@ -5636,10 +5656,14 @@ convert_like_real (conversion *convs, tree expr, tree fn, int argnum,
            && CONSTRUCTOR_NELTS (expr) == 0
            && TYPE_HAS_DEFAULT_CONSTRUCTOR (totype))
          {
+           bool direct = CONSTRUCTOR_IS_DIRECT_INIT (expr);
            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;
+             {
+               TARGET_EXPR_LIST_INIT_P (expr) = true;
+               TARGET_EXPR_DIRECT_INIT_P (expr) = direct;
+             }
            return expr;
          }
 
@@ -5648,6 +5672,9 @@ convert_like_real (conversion *convs, tree expr, tree fn, int argnum,
        /* 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)
@@ -5696,7 +5723,7 @@ convert_like_real (conversion *convs, tree expr, tree fn, int argnum,
         leave it as an lvalue.  */
       if (inner >= 0)
         {   
-          expr = decl_constant_value (expr);
+          expr = decl_constant_value_safe (expr);
           if (expr == null_node && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (totype))
             /* If __null has been converted to an integer type, we do not
                want to warn about uses of EXPR as an integer, rather than
@@ -5745,11 +5772,15 @@ convert_like_real (conversion *convs, tree expr, tree fn, int argnum,
          (elttype, cp_type_quals (elttype) | TYPE_QUAL_CONST);
        array = build_array_of_n_type (elttype, len);
        array = finish_compound_literal (array, new_ctor, complain);
+       /* Take the address explicitly rather than via decay_conversion
+          to avoid the error about taking the address of a temporary.  */
+       array = cp_build_addr_expr (array, complain);
+       array = cp_convert (build_pointer_type (elttype), array);
 
        /* 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));
+       CONSTRUCTOR_APPEND_ELT (vec, field, array);
        field = next_initializable_field (DECL_CHAIN (field));
        CONSTRUCTOR_APPEND_ELT (vec, field, size_int (len));
        new_ctor = build_constructor (totype, vec);
@@ -6130,6 +6161,8 @@ 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)
     {
@@ -6140,14 +6173,6 @@ convert_default_arg (tree type, tree arg, tree fn, int parmnum)
     }
   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);
@@ -6443,6 +6468,12 @@ build_over_call (struct z_candidate *cand, int flags, tsubst_flags_t complain)
            return error_mark_node;
        }
 
+      /* See if the function member or the whole class type is declared
+        final and the call can be devirtualized.  */
+      if (DECL_FINAL_P (fn)
+         || CLASSTYPE_FINAL (TYPE_METHOD_BASETYPE (TREE_TYPE (fn))))
+       flags |= LOOKUP_NONVIRTUAL;
+
       /* [class.mfct.nonstatic]: If a nonstatic member function of a class
         X is called for an object that is not of type X, or of a type
         derived from X, the behavior is undefined.
@@ -6455,7 +6486,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))
@@ -6468,7 +6499,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);
@@ -6560,9 +6591,14 @@ build_over_call (struct z_candidate *cand, int flags, tsubst_flags_t complain)
 
   /* Default arguments */
   for (; parm && parm != void_list_node; parm = TREE_CHAIN (parm), i++)
-    argarray[j++] = convert_default_arg (TREE_VALUE (parm),
-                                        TREE_PURPOSE (parm),
-                                        fn, i - is_method);
+    {
+      if (TREE_VALUE (parm) == error_mark_node)
+       return error_mark_node;
+      argarray[j++] = convert_default_arg (TREE_VALUE (parm),
+                                          TREE_PURPOSE (parm),
+                                          fn, i - is_method);
+    }
+
   /* Ellipsis */
   for (; arg_index < VEC_length (tree, args); ++arg_index)
     {
@@ -6712,7 +6748,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));
@@ -6922,7 +6959,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);
        }
     }
 
@@ -6950,8 +6987,10 @@ build_special_member_call (tree instance, tree name, VEC(tree,gc) **args,
                            current_in_charge_parm, integer_zero_node),
                    current_vtt_parm,
                    vtt);
-      gcc_assert (BINFO_SUBVTT_INDEX (binfo));
-      sub_vtt = fold_build_pointer_plus (vtt, BINFO_SUBVTT_INDEX (binfo));
+      if (BINFO_SUBVTT_INDEX (binfo))
+       sub_vtt = fold_build_pointer_plus (vtt, BINFO_SUBVTT_INDEX (binfo));
+      else
+       sub_vtt = vtt;
 
       if (args == NULL)
        {
@@ -7168,6 +7207,7 @@ build_new_method_call_1 (tree instance, tree fns, VEC(tree,gc) **args,
       && CONSTRUCTOR_IS_DIRECT_INIT (VEC_index (tree, *args, 0)))
     {
       tree init_list = VEC_index (tree, *args, 0);
+      tree init = NULL_TREE;
 
       gcc_assert (VEC_length (tree, *args) == 1
                  && !(flags & LOOKUP_ONLYCONVERTING));
@@ -7179,8 +7219,16 @@ build_new_method_call_1 (tree instance, tree fns, VEC(tree,gc) **args,
       if (CONSTRUCTOR_NELTS (init_list) == 0
          && TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype)
          && !processing_template_decl)
+       init = build_value_init (basetype, complain);
+
+      /* If BASETYPE is an aggregate, we need to do aggregate
+        initialization.  */
+      else if (CP_AGGREGATE_TYPE_P (basetype))
+       init = digest_init (basetype, init_list, complain);
+
+      if (init)
        {
-         tree ob, init = build_value_init (basetype, complain);
+         tree ob;
          if (integer_zerop (instance_ptr))
            return get_target_expr_sfinae (init, complain);
          ob = build_fold_indirect_ref (instance_ptr);
@@ -7189,6 +7237,7 @@ build_new_method_call_1 (tree instance, tree fns, VEC(tree,gc) **args,
          return init;
        }
 
+      /* Otherwise go ahead with overload resolution.  */
       add_list_candidates (fns, first_mem_arg, init_list,
                           basetype, explicit_targs, template_only,
                           conversion_path, access_binfo, flags, &candidates);
@@ -7282,6 +7331,8 @@ build_new_method_call_1 (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))
                flags |= LOOKUP_NONVIRTUAL;
@@ -7573,7 +7624,7 @@ compare_ics (conversion *ics1, conversion *ics2)
      Specifically, we need to do the reference binding comparison at the
      end of this function.  */
 
-  if (ics1->user_conv_p || ics1->kind == ck_list)
+  if (ics1->user_conv_p || ics1->kind == ck_list || ics1->kind == ck_aggr)
     {
       conversion *t1;
       conversion *t2;
@@ -7859,18 +7910,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)
+      if (!ref_conv1->this_p && !ref_conv2->this_p
+         && (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
-             > ref_conv2->rvaluedness_matches_p)
-           return 1;
-         if (ref_conv2->rvaluedness_matches_p
-             > ref_conv1->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))
@@ -7957,6 +8015,12 @@ joust (struct z_candidate *cand1, struct z_candidate *cand2, bool warn)
       int static_1 = DECL_STATIC_FUNCTION_P (cand1->fn);
       int static_2 = DECL_STATIC_FUNCTION_P (cand2->fn);
 
+      if (DECL_CONSTRUCTOR_P (cand1->fn)
+         && is_list_ctor (cand1->fn) != is_list_ctor (cand2->fn))
+       /* We're comparing a near-match list constructor and a near-match
+          non-list constructor.  Just treat them as unordered.  */
+       return 0;
+
       gcc_assert (static_1 != static_2);
 
       if (static_1)
@@ -8074,6 +8138,22 @@ joust (struct z_candidate *cand1, struct z_candidate *cand2, bool warn)
   if (winner)
     return winner;
 
+  /* DR 495 moved this tiebreaker above the template ones.  */
+  /* or, if not that,
+     the  context  is  an  initialization by user-defined conversion (see
+     _dcl.init_  and  _over.match.user_)  and  the  standard   conversion
+     sequence  from  the return type of F1 to the destination type (i.e.,
+     the type of the entity being initialized)  is  a  better  conversion
+     sequence  than the standard conversion sequence from the return type
+     of F2 to the destination type.  */
+
+  if (cand1->second_conv)
+    {
+      winner = compare_ics (cand1->second_conv, cand2->second_conv);
+      if (winner)
+       return winner;
+    }
+
   /* or, if not that,
      F1 is a non-template function and F2 is a template function
      specialization.  */
@@ -8102,21 +8182,6 @@ joust (struct z_candidate *cand1, struct z_candidate *cand2, bool warn)
        return winner;
     }
 
-  /* or, if not that,
-     the  context  is  an  initialization by user-defined conversion (see
-     _dcl.init_  and  _over.match.user_)  and  the  standard   conversion
-     sequence  from  the return type of F1 to the destination type (i.e.,
-     the type of the entity being initialized)  is  a  better  conversion
-     sequence  than the standard conversion sequence from the return type
-     of F2 to the destination type.  */
-
-  if (cand1->second_conv)
-    {
-      winner = compare_ics (cand1->second_conv, cand2->second_conv);
-      if (winner)
-       return winner;
-    }
-
   /* Check whether we can discard a builtin candidate, either because we
      have two identical ones or matching builtin and non-builtin candidates.
 
@@ -8386,13 +8451,16 @@ perform_implicit_conversion_flags (tree type, tree expr, tsubst_flags_t complain
        }
       expr = error_mark_node;
     }
-  else if (processing_template_decl)
+  else if (processing_template_decl && conv->kind != ck_identity)
     {
       /* In a template, we are only concerned about determining the
         type of non-dependent expressions, so we do not have to
-        perform the actual conversion.  */
-      if (TREE_TYPE (expr) != type)
-       expr = build_nop (type, expr);
+        perform the actual conversion.  But for initializers, we
+        need to be able to perform it at instantiation
+        (or fold_non_dependent_expr) time.  */
+      expr = build1 (IMPLICIT_CONV_EXPR, type, expr);
+      if (!(flags & LOOKUP_ONLYCONVERTING))
+       IMPLICIT_CONV_EXPR_DIRECT_INIT (expr) = true;
     }
   else
     expr = convert_like (conv, expr, complain);
@@ -8465,6 +8533,44 @@ perform_direct_initialization_if_possible (tree type,
   return expr;
 }
 
+/* When initializing a reference that lasts longer than a full-expression,
+   this special rule applies:
+
+     [class.temporary]
+
+     The temporary to which the reference is bound or the temporary
+     that is the complete object to which the reference is bound
+     persists for the lifetime of the reference.
+
+     The temporaries created during the evaluation of the expression
+     initializing the reference, except the temporary to which the
+     reference is bound, are destroyed at the end of the
+     full-expression in which they are created.
+
+   In that case, we store the converted expression into a new
+   VAR_DECL in a new scope.
+
+   However, we want to be careful not to create temporaries when
+   they are not required.  For example, given:
+
+     struct B {};
+     struct D : public B {};
+     D f();
+     const B& b = f();
+
+   there is no need to copy the return value from "f"; we can just
+   extend its lifetime.  Similarly, given:
+
+     struct S {};
+     struct T { operator S(); };
+     T t;
+     const S& s = t;
+
+  we can extend the lifetime of the return value of the conversion
+  operator.
+
+  The next several functions are involved in this lifetime extension.  */
+
 /* DECL is a VAR_DECL whose type is a REFERENCE_TYPE.  The reference
    is being bound to a temporary.  Create and return a new VAR_DECL
    with the indicated TYPE; this variable will store the value to
@@ -8482,6 +8588,7 @@ make_temporary_var_for_ref_to_temp (tree decl, tree type)
   if (TREE_STATIC (decl))
     {
       /* Namespace-scope or local static; give it a mangled name.  */
+      /* FIXME share comdat with decl?  */
       tree name;
 
       TREE_STATIC (var) = 1;
@@ -8503,8 +8610,9 @@ make_temporary_var_for_ref_to_temp (tree decl, tree type)
    cleanup for the new variable is returned through CLEANUP, and the
    code to initialize the new variable is returned through INITP.  */
 
-tree
-set_up_extended_ref_temp (tree decl, tree expr, tree *cleanup, tree *initp)
+static tree
+set_up_extended_ref_temp (tree decl, tree expr, VEC(tree,gc) **cleanups,
+                         tree *initp)
 {
   tree init;
   tree type;
@@ -8525,6 +8633,18 @@ set_up_extended_ref_temp (tree decl, tree expr, tree *cleanup, tree *initp)
   if (TREE_CODE (expr) != TARGET_EXPR)
     expr = get_target_expr (expr);
 
+  if (TREE_CODE (decl) == FIELD_DECL
+      && extra_warnings && !TREE_NO_WARNING (decl))
+    {
+      warning (OPT_Wextra, "a temporary bound to %qD only persists "
+              "until the constructor exits", decl);
+      TREE_NO_WARNING (decl) = true;
+    }
+
+  /* Recursively extend temps in this initializer.  */
+  TARGET_EXPR_INITIAL (expr)
+    = extend_ref_init_temps (decl, TARGET_EXPR_INITIAL (expr), cleanups);
+
   /* 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);
@@ -8558,7 +8678,11 @@ 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, tf_warning_or_error);
+       {
+         tree cleanup = cxx_maybe_build_cleanup (var, tf_warning_or_error);
+         if (cleanup)
+           VEC_safe_push (tree, gc, *cleanups, cleanup);
+       }
 
       /* We must be careful to destroy the temporary only
         after its initialization has taken place.  If the
@@ -8592,18 +8716,10 @@ set_up_extended_ref_temp (tree decl, tree expr, tree *cleanup, tree *initp)
 }
 
 /* Convert EXPR to the indicated reference TYPE, in a way suitable for
-   initializing a variable of that TYPE.  If DECL is non-NULL, it is
-   the VAR_DECL being initialized with the EXPR.  (In that case, the
-   type of DECL will be TYPE.)  If DECL is non-NULL, then CLEANUP must
-   also be non-NULL, and with *CLEANUP initialized to NULL.  Upon
-   return, if *CLEANUP is no longer NULL, it will be an expression
-   that should be pushed as a cleanup after the returned expression
-   is used to initialize DECL.
-
-   Return the converted expression.  */
+   initializing a variable of that TYPE.  */
 
 tree
-initialize_reference (tree type, tree expr, tree decl, tree *cleanup,
+initialize_reference (tree type, tree expr,
                      int flags, tsubst_flags_t complain)
 {
   conversion *conv;
@@ -8637,103 +8753,77 @@ initialize_reference (tree type, tree expr, tree decl, tree *cleanup,
       return error_mark_node;
     }
 
-  /* If DECL is non-NULL, then this special rule applies:
-
-       [class.temporary]
-
-       The temporary to which the reference is bound or the temporary
-       that is the complete object to which the reference is bound
-       persists for the lifetime of the reference.
+  gcc_assert (conv->kind == ck_ref_bind);
 
-       The temporaries created during the evaluation of the expression
-       initializing the reference, except the temporary to which the
-       reference is bound, are destroyed at the end of the
-       full-expression in which they are created.
+  /* Perform the conversion.  */
+  expr = convert_like (conv, expr, complain);
 
-     In that case, we store the converted expression into a new
-     VAR_DECL in a new scope.
+  /* Free all the conversions we allocated.  */
+  obstack_free (&conversion_obstack, p);
 
-     However, we want to be careful not to create temporaries when
-     they are not required.  For example, given:
+  return expr;
+}
 
-       struct B {};
-       struct D : public B {};
-       D f();
-       const B& b = f();
+/* Subroutine of extend_ref_init_temps.  Possibly extend one initializer,
+   which is bound either to a reference or a std::initializer_list.  */
 
-     there is no need to copy the return value from "f"; we can just
-     extend its lifetime.  Similarly, given:
+static tree
+extend_ref_init_temps_1 (tree decl, tree init, VEC(tree,gc) **cleanups)
+{
+  tree sub = init;
+  tree *p;
+  STRIP_NOPS (sub);
+  if (TREE_CODE (sub) != ADDR_EXPR)
+    return init;
+  /* Deal with binding to a subobject.  */
+  for (p = &TREE_OPERAND (sub, 0); TREE_CODE (*p) == COMPONENT_REF; )
+    p = &TREE_OPERAND (*p, 0);
+  if (TREE_CODE (*p) == TARGET_EXPR)
+    {
+      tree subinit = NULL_TREE;
+      *p = set_up_extended_ref_temp (decl, *p, cleanups, &subinit);
+      if (subinit)
+       init = build2 (COMPOUND_EXPR, TREE_TYPE (init), subinit, init);
+    }
+  return init;
+}
 
-       struct S {};
-       struct T { operator S(); };
-       T t;
-       const S& s = t;
+/* INIT is part of the initializer for DECL.  If there are any
+   reference or initializer lists being initialized, extend their
+   lifetime to match that of DECL.  */
 
-    we can extend the lifetime of the return value of the conversion
-    operator.  */
-  gcc_assert (conv->kind == ck_ref_bind);
-  if (decl)
+tree
+extend_ref_init_temps (tree decl, tree init, VEC(tree,gc) **cleanups)
+{
+  tree type = TREE_TYPE (init);
+  if (processing_template_decl)
+    return init;
+  if (TREE_CODE (type) == REFERENCE_TYPE)
+    init = extend_ref_init_temps_1 (decl, init, cleanups);
+  else if (is_std_init_list (type))
     {
-      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
-        remember that the conversion was required.  */
-      if (conv->kind == ck_base)
+      /* The temporary array underlying a std::initializer_list
+        is handled like a reference temporary.  */
+      tree ctor = init;
+      if (TREE_CODE (ctor) == TARGET_EXPR)
+       ctor = TARGET_EXPR_INITIAL (ctor);
+      if (TREE_CODE (ctor) == CONSTRUCTOR)
        {
-         base_conv_type = conv->type;
-         conv = conv->u.next;
-       }
-      else
-       base_conv_type = NULL_TREE;
-      /* Perform the remainder of the conversion.  */
-      expr = convert_like_real (conv, expr,
-                               /*fn=*/NULL_TREE, /*argnum=*/0,
-                               /*inner=*/-1,
-                               /*issue_conversion_warnings=*/true,
-                               /*c_cast_p=*/false,
-                               complain);
-      if (error_operand_p (expr))
-       expr = error_mark_node;
-      else
-       {
-         if (!lvalue_or_rvalue_with_address_p (expr))
-           {
-             tree init;
-             var = set_up_extended_ref_temp (decl, expr, cleanup, &init);
-             /* Use its address to initialize the reference variable.  */
-             expr = build_address (var);
-             if (base_conv_type)
-               expr = convert_to_base (expr,
-                                       build_pointer_type (base_conv_type),
-                                       /*check_access=*/true,
-                                       /*nonnull=*/true, complain);
-             if (init)
-               expr = build2 (COMPOUND_EXPR, TREE_TYPE (expr), init, expr);
-           }
-         else
-           /* Take the address of EXPR.  */
-           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,
-                    complain));
-         expr = build_nop (type, expr);
+         tree array = CONSTRUCTOR_ELT (ctor, 0)->value;
+         array = extend_ref_init_temps_1 (decl, array, cleanups);
+         CONSTRUCTOR_ELT (ctor, 0)->value = array;
        }
     }
-  else
-    /* Perform the conversion.  */
-    expr = convert_like (conv, expr, complain);
-
-  /* Free all the conversions we allocated.  */
-  obstack_free (&conversion_obstack, p);
+  else if (TREE_CODE (init) == CONSTRUCTOR)
+    {
+      unsigned i;
+      constructor_elt *p;
+      VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
+      FOR_EACH_VEC_ELT (constructor_elt, elts, i, p)
+       p->value = extend_ref_init_temps (decl, p->value, cleanups);
+    }
 
-  return expr;
+  return init;
 }
 
 /* Returns true iff TYPE is some variant of std::initializer_list.  */