OSDN Git Service

PR c++/25811
[pf3gnuchains/gcc-fork.git] / gcc / cp / method.c
index d2d52bb..5ed98bc 100644 (file)
@@ -1,14 +1,15 @@
 /* Handle the hair of processing (but not expanding) inline functions.
    Also manage function and variable name overloading.
 /* Handle the hair of processing (but not expanding) inline functions.
    Also manage function and variable name overloading.
-   Copyright (C) 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 
-   1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
+   Copyright (C) 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
+   1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009
+   Free Software Foundation, Inc.
    Contributed by Michael Tiemann (tiemann@cygnus.com)
 
 This file is part of GCC.
    Contributed by Michael Tiemann (tiemann@cygnus.com)
 
 This file is part of GCC.
-   
+
 GCC is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 GCC is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
-the Free Software Foundation; either version 2, or (at your option)
+the Free Software Foundation; either version 3, or (at your option)
 any later version.
 
 GCC is distributed in the hope that it will be useful,
 any later version.
 
 GCC is distributed in the hope that it will be useful,
@@ -17,9 +18,8 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
-along with GCC; see the file COPYING.  If not, write to
-the Free Software Foundation, 59 Temple Place - Suite 330,
-Boston, MA 02111-1307, USA.  */
+along with GCC; see the file COPYING3.  If not see
+<http://www.gnu.org/licenses/>.  */
 
 
 /* Handle method declarations.  */
 
 
 /* Handle method declarations.  */
@@ -36,6 +36,10 @@ Boston, MA 02111-1307, USA.  */
 #include "toplev.h"
 #include "tm_p.h"
 #include "target.h"
 #include "toplev.h"
 #include "tm_p.h"
 #include "target.h"
+#include "tree-pass.h"
+#include "diagnostic.h"
+#include "cgraph.h"
+#include "gimple.h"
 
 /* Various flags to control the mangling process.  */
 
 
 /* Various flags to control the mangling process.  */
 
@@ -55,13 +59,9 @@ enum mangling_flags
 
 typedef enum mangling_flags mangling_flags;
 
 
 typedef enum mangling_flags mangling_flags;
 
-static tree thunk_adjust (tree, bool, HOST_WIDE_INT, tree);
 static void do_build_assign_ref (tree);
 static void do_build_copy_constructor (tree);
 static tree synthesize_exception_spec (tree, tree (*) (tree, void *), void *);
 static void do_build_assign_ref (tree);
 static void do_build_copy_constructor (tree);
 static tree synthesize_exception_spec (tree, tree (*) (tree, void *), void *);
-static tree locate_dtor (tree, void *);
-static tree locate_ctor (tree, void *);
-static tree locate_copy (tree, void *);
 static tree make_alias_for_thunk (tree);
 
 /* Called once to initialize method.c.  */
 static tree make_alias_for_thunk (tree);
 
 /* Called once to initialize method.c.  */
@@ -87,23 +87,22 @@ make_thunk (tree function, bool this_adjusting,
 {
   HOST_WIDE_INT d;
   tree thunk;
 {
   HOST_WIDE_INT d;
   tree thunk;
-  
-  my_friendly_assert (TREE_CODE (function) == FUNCTION_DECL, 20021025);
+
+  gcc_assert (TREE_CODE (function) == FUNCTION_DECL);
   /* We can have this thunks to covariant thunks, but not vice versa.  */
   /* We can have this thunks to covariant thunks, but not vice versa.  */
-  my_friendly_assert (!DECL_THIS_THUNK_P (function), 20021127);
-  my_friendly_assert (!DECL_RESULT_THUNK_P (function) || this_adjusting,
-                     20031123);
-  
+  gcc_assert (!DECL_THIS_THUNK_P (function));
+  gcc_assert (!DECL_RESULT_THUNK_P (function) || this_adjusting);
+
   /* Scale the VIRTUAL_OFFSET to be in terms of bytes.  */
   if (this_adjusting && virtual_offset)
   /* Scale the VIRTUAL_OFFSET to be in terms of bytes.  */
   if (this_adjusting && virtual_offset)
-    virtual_offset 
+    virtual_offset
       = size_binop (MULT_EXPR,
       = size_binop (MULT_EXPR,
-                   virtual_offset,
-                   convert (ssizetype,
-                            TYPE_SIZE_UNIT (vtable_entry_type)));
-  
+                   virtual_offset,
+                   convert (ssizetype,
+                            TYPE_SIZE_UNIT (vtable_entry_type)));
+
   d = tree_low_cst (fixed_offset, 0);
   d = tree_low_cst (fixed_offset, 0);
-  
+
   /* See if we already have the thunk in question.  For this_adjusting
      thunks VIRTUAL_OFFSET will be an INTEGER_CST, for covariant thunks it
      will be a BINFO.  */
   /* See if we already have the thunk in question.  For this_adjusting
      thunks VIRTUAL_OFFSET will be an INTEGER_CST, for covariant thunks it
      will be a BINFO.  */
@@ -117,34 +116,32 @@ make_thunk (tree function, bool this_adjusting,
                                      virtual_offset)
                : THUNK_VIRTUAL_OFFSET (thunk) == virtual_offset)))
       return thunk;
                                      virtual_offset)
                : THUNK_VIRTUAL_OFFSET (thunk) == virtual_offset)))
       return thunk;
-  
+
   /* All thunks must be created before FUNCTION is actually emitted;
      the ABI requires that all thunks be emitted together with the
      function to which they transfer control.  */
   /* All thunks must be created before FUNCTION is actually emitted;
      the ABI requires that all thunks be emitted together with the
      function to which they transfer control.  */
-  my_friendly_assert (!TREE_ASM_WRITTEN (function), 20021025);
+  gcc_assert (!TREE_ASM_WRITTEN (function));
   /* Likewise, we can only be adding thunks to a function declared in
      the class currently being laid out.  */
   /* Likewise, we can only be adding thunks to a function declared in
      the class currently being laid out.  */
-  my_friendly_assert (TYPE_SIZE (DECL_CONTEXT (function))
-                     && TYPE_BEING_DEFINED (DECL_CONTEXT (function)),
-                     20031211);
+  gcc_assert (TYPE_SIZE (DECL_CONTEXT (function))
+             && TYPE_BEING_DEFINED (DECL_CONTEXT (function)));
 
 
-  thunk = build_decl (FUNCTION_DECL, NULL_TREE, TREE_TYPE (function));
+  thunk = build_decl (DECL_SOURCE_LOCATION (function),
+                     FUNCTION_DECL, NULL_TREE, TREE_TYPE (function));
   DECL_LANG_SPECIFIC (thunk) = DECL_LANG_SPECIFIC (function);
   cxx_dup_lang_specific_decl (thunk);
   DECL_THUNKS (thunk) = NULL_TREE;
   DECL_LANG_SPECIFIC (thunk) = DECL_LANG_SPECIFIC (function);
   cxx_dup_lang_specific_decl (thunk);
   DECL_THUNKS (thunk) = NULL_TREE;
-  
+
   DECL_CONTEXT (thunk) = DECL_CONTEXT (function);
   TREE_READONLY (thunk) = TREE_READONLY (function);
   TREE_THIS_VOLATILE (thunk) = TREE_THIS_VOLATILE (function);
   TREE_PUBLIC (thunk) = TREE_PUBLIC (function);
   DECL_CONTEXT (thunk) = DECL_CONTEXT (function);
   TREE_READONLY (thunk) = TREE_READONLY (function);
   TREE_THIS_VOLATILE (thunk) = TREE_THIS_VOLATILE (function);
   TREE_PUBLIC (thunk) = TREE_PUBLIC (function);
-  if (flag_weak)
-    comdat_linkage (thunk);
   SET_DECL_THUNK_P (thunk, this_adjusting);
   THUNK_TARGET (thunk) = function;
   THUNK_FIXED_OFFSET (thunk) = d;
   THUNK_VIRTUAL_OFFSET (thunk) = virtual_offset;
   THUNK_ALIAS (thunk) = NULL_TREE;
   SET_DECL_THUNK_P (thunk, this_adjusting);
   THUNK_TARGET (thunk) = function;
   THUNK_FIXED_OFFSET (thunk) = d;
   THUNK_VIRTUAL_OFFSET (thunk) = virtual_offset;
   THUNK_ALIAS (thunk) = NULL_TREE;
-  
+
   /* The thunk itself is not a constructor or destructor, even if
      the thing it is thunking to is.  */
   DECL_INTERFACE_KNOWN (thunk) = 1;
   /* The thunk itself is not a constructor or destructor, even if
      the thing it is thunking to is.  */
   DECL_INTERFACE_KNOWN (thunk) = 1;
@@ -152,20 +149,15 @@ make_thunk (tree function, bool this_adjusting,
   DECL_SAVED_FUNCTION_DATA (thunk) = NULL;
   DECL_DESTRUCTOR_P (thunk) = 0;
   DECL_CONSTRUCTOR_P (thunk) = 0;
   DECL_SAVED_FUNCTION_DATA (thunk) = NULL;
   DECL_DESTRUCTOR_P (thunk) = 0;
   DECL_CONSTRUCTOR_P (thunk) = 0;
-  /* And neither is it a clone.  */
-  DECL_CLONED_FUNCTION (thunk) = NULL_TREE;
   DECL_EXTERNAL (thunk) = 1;
   DECL_ARTIFICIAL (thunk) = 1;
   DECL_EXTERNAL (thunk) = 1;
   DECL_ARTIFICIAL (thunk) = 1;
-  /* Even if this thunk is a member of a local class, we don't
-     need a static chain.  */
-  DECL_NO_STATIC_CHAIN (thunk) = 1;
   /* The THUNK is not a pending inline, even if the FUNCTION is.  */
   DECL_PENDING_INLINE_P (thunk) = 0;
   /* The THUNK is not a pending inline, even if the FUNCTION is.  */
   DECL_PENDING_INLINE_P (thunk) = 0;
-  DECL_INLINE (thunk) = 0;
   DECL_DECLARED_INLINE_P (thunk) = 0;
   DECL_DECLARED_INLINE_P (thunk) = 0;
-  /* Nor has it been deferred.  */
-  DECL_DEFERRED_FN (thunk) = 0;
-  
+  /* Nor is it a template instantiation.  */
+  DECL_USE_TEMPLATE (thunk) = 0;
+  DECL_TEMPLATE_INFO (thunk) = NULL;
+
   /* Add it to the list of thunks associated with FUNCTION.  */
   TREE_CHAIN (thunk) = DECL_THUNKS (function);
   DECL_THUNKS (function) = thunk;
   /* Add it to the list of thunks associated with FUNCTION.  */
   TREE_CHAIN (thunk) = DECL_THUNKS (function);
   DECL_THUNKS (function) = thunk;
@@ -182,7 +174,7 @@ finish_thunk (tree thunk)
   tree fixed_offset = ssize_int (THUNK_FIXED_OFFSET (thunk));
   tree virtual_offset = THUNK_VIRTUAL_OFFSET (thunk);
 
   tree fixed_offset = ssize_int (THUNK_FIXED_OFFSET (thunk));
   tree virtual_offset = THUNK_VIRTUAL_OFFSET (thunk);
 
-  my_friendly_assert (!DECL_NAME (thunk) && DECL_THUNK_P (thunk), 20021127);
+  gcc_assert (!DECL_NAME (thunk) && DECL_THUNK_P (thunk));
   if (virtual_offset && DECL_RESULT_THUNK_P (thunk))
     virtual_offset = BINFO_VPTR_FIELD (virtual_offset);
   function = THUNK_TARGET (thunk);
   if (virtual_offset && DECL_RESULT_THUNK_P (thunk))
     virtual_offset = BINFO_VPTR_FIELD (virtual_offset);
   function = THUNK_TARGET (thunk);
@@ -202,105 +194,79 @@ finish_thunk (tree thunk)
           cov_probe; cov_probe = TREE_CHAIN (cov_probe))
        if (DECL_NAME (cov_probe) == name)
          {
           cov_probe; cov_probe = TREE_CHAIN (cov_probe))
        if (DECL_NAME (cov_probe) == name)
          {
-           my_friendly_assert (!DECL_THUNKS (thunk), 20031023);
+           gcc_assert (!DECL_THUNKS (thunk));
            THUNK_ALIAS (thunk) = (THUNK_ALIAS (cov_probe)
                                   ? THUNK_ALIAS (cov_probe) : cov_probe);
            break;
          }
     }
            THUNK_ALIAS (thunk) = (THUNK_ALIAS (cov_probe)
                                   ? THUNK_ALIAS (cov_probe) : cov_probe);
            break;
          }
     }
-  
+
   DECL_NAME (thunk) = name;
   SET_DECL_ASSEMBLER_NAME (thunk, name);
 }
 
   DECL_NAME (thunk) = name;
   SET_DECL_ASSEMBLER_NAME (thunk, name);
 }
 
-/* Adjust PTR by the constant FIXED_OFFSET, and by the vtable
-   offset indicated by VIRTUAL_OFFSET, if that is
-   non-null. THIS_ADJUSTING is nonzero for a this adjusting thunk and
-   zero for a result adjusting thunk.  */
-
-static tree
-thunk_adjust (tree ptr, bool this_adjusting,
-             HOST_WIDE_INT fixed_offset, tree virtual_offset)
-{
-  if (this_adjusting)
-    /* Adjust the pointer by the constant.  */
-    ptr = fold (build (PLUS_EXPR, TREE_TYPE (ptr), ptr,
-                      ssize_int (fixed_offset)));
-
-  /* If there's a virtual offset, look up that value in the vtable and
-     adjust the pointer again.  */
-  if (virtual_offset)
-    {
-      tree vtable;
-
-      ptr = save_expr (ptr);
-      /* The vptr is always at offset zero in the object.  */
-      vtable = build1 (NOP_EXPR,
-                      build_pointer_type (build_pointer_type 
-                                          (vtable_entry_type)),
-                      ptr);
-      /* Form the vtable address.  */
-      vtable = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (vtable)), vtable);
-      /* Find the entry with the vcall offset.  */
-      vtable = build (PLUS_EXPR, TREE_TYPE (vtable), vtable, virtual_offset);
-      /* Get the offset itself.  */
-      vtable = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (vtable)), vtable);
-      /* Adjust the `this' pointer.  */
-      ptr = fold (build (PLUS_EXPR, TREE_TYPE (ptr), ptr, vtable));
-    }
-  
-  if (!this_adjusting)
-    /* Adjust the pointer by the constant.  */
-    ptr = fold (build (PLUS_EXPR, TREE_TYPE (ptr), ptr,
-                      ssize_int (fixed_offset)));
-
-  return ptr;
-}
-
 static GTY (()) int thunk_labelno;
 
 static GTY (()) int thunk_labelno;
 
-/* Create a static alias to function.  */
+/* Create a static alias to target.  */
 
 
-static tree
-make_alias_for_thunk (tree function)
+tree
+make_alias_for (tree target, tree newid)
 {
 {
-  tree alias;
-  char buf[256];
-
-  ASM_GENERATE_INTERNAL_LABEL (buf, "LTHUNK", thunk_labelno);
-  thunk_labelno++;
-  alias = build_decl (FUNCTION_DECL, get_identifier (buf),
-                     TREE_TYPE (function));
-  DECL_LANG_SPECIFIC (alias) = DECL_LANG_SPECIFIC (function);
+  tree alias = build_decl (DECL_SOURCE_LOCATION (target),
+                          TREE_CODE (target), newid, TREE_TYPE (target));
+  DECL_LANG_SPECIFIC (alias) = DECL_LANG_SPECIFIC (target);
   cxx_dup_lang_specific_decl (alias);
   DECL_CONTEXT (alias) = NULL;
   cxx_dup_lang_specific_decl (alias);
   DECL_CONTEXT (alias) = NULL;
-  TREE_READONLY (alias) = TREE_READONLY (function);
-  TREE_THIS_VOLATILE (alias) = TREE_THIS_VOLATILE (function);
+  TREE_READONLY (alias) = TREE_READONLY (target);
+  TREE_THIS_VOLATILE (alias) = TREE_THIS_VOLATILE (target);
   TREE_PUBLIC (alias) = 0;
   DECL_INTERFACE_KNOWN (alias) = 1;
   TREE_PUBLIC (alias) = 0;
   DECL_INTERFACE_KNOWN (alias) = 1;
-  DECL_NOT_REALLY_EXTERN (alias) = 1;
-  DECL_THIS_STATIC (alias) = 1;
-  DECL_SAVED_FUNCTION_DATA (alias) = NULL;
-  DECL_DESTRUCTOR_P (alias) = 0;
-  DECL_CONSTRUCTOR_P (alias) = 0;
-  DECL_CLONED_FUNCTION (alias) = NULL_TREE;
+  if (DECL_LANG_SPECIFIC (alias))
+    {
+      DECL_NOT_REALLY_EXTERN (alias) = 1;
+      DECL_USE_TEMPLATE (alias) = 0;
+      DECL_TEMPLATE_INFO (alias) = NULL;
+    }
   DECL_EXTERNAL (alias) = 0;
   DECL_ARTIFICIAL (alias) = 1;
   DECL_EXTERNAL (alias) = 0;
   DECL_ARTIFICIAL (alias) = 1;
-  DECL_NO_STATIC_CHAIN (alias) = 1;
-  DECL_PENDING_INLINE_P (alias) = 0;
-  DECL_INLINE (alias) = 0;
-  DECL_DECLARED_INLINE_P (alias) = 0;
-  DECL_DEFERRED_FN (alias) = 0;
-  DECL_USE_TEMPLATE (alias) = 0;
   DECL_TEMPLATE_INSTANTIATED (alias) = 0;
   DECL_TEMPLATE_INSTANTIATED (alias) = 0;
-  DECL_TEMPLATE_INFO (alias) = NULL;
-  DECL_INITIAL (alias) = error_mark_node;
+  if (TREE_CODE (alias) == FUNCTION_DECL)
+    {
+      DECL_SAVED_FUNCTION_DATA (alias) = NULL;
+      DECL_DESTRUCTOR_P (alias) = 0;
+      DECL_CONSTRUCTOR_P (alias) = 0;
+      DECL_PENDING_INLINE_P (alias) = 0;
+      DECL_DECLARED_INLINE_P (alias) = 0;
+      DECL_INITIAL (alias) = error_mark_node;
+      DECL_ARGUMENTS (alias) = copy_list (DECL_ARGUMENTS (target));
+    }
+  else
+    TREE_STATIC (alias) = 1;
   TREE_ADDRESSABLE (alias) = 1;
   TREE_USED (alias) = 1;
   SET_DECL_ASSEMBLER_NAME (alias, DECL_NAME (alias));
   TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (alias)) = 1;
   TREE_ADDRESSABLE (alias) = 1;
   TREE_USED (alias) = 1;
   SET_DECL_ASSEMBLER_NAME (alias, DECL_NAME (alias));
   TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (alias)) = 1;
+  return alias;
+}
+
+static tree
+make_alias_for_thunk (tree function)
+{
+  tree alias;
+  char buf[256];
+
+  ASM_GENERATE_INTERNAL_LABEL (buf, "LTHUNK", thunk_labelno);
+  thunk_labelno++;
+
+  alias = make_alias_for (function, get_identifier (buf));
+
   if (!flag_syntax_only)
   if (!flag_syntax_only)
-    assemble_alias (alias, DECL_ASSEMBLER_NAME (function));
+    {
+      bool ok = cgraph_same_body_alias (alias, function);
+      DECL_ASSEMBLER_NAME (function);
+      gcc_assert (ok);
+    }
+
   return alias;
 }
 
   return alias;
 }
 
@@ -317,21 +283,25 @@ use_thunk (tree thunk_fndecl, bool emit_p)
   bool this_adjusting = DECL_THIS_THUNK_P (thunk_fndecl);
 
   /* We should have called finish_thunk to give it a name.  */
   bool this_adjusting = DECL_THIS_THUNK_P (thunk_fndecl);
 
   /* We should have called finish_thunk to give it a name.  */
-  my_friendly_assert (DECL_NAME (thunk_fndecl), 20021127);
+  gcc_assert (DECL_NAME (thunk_fndecl));
 
   /* We should never be using an alias, always refer to the
      aliased thunk.  */
 
   /* We should never be using an alias, always refer to the
      aliased thunk.  */
-  my_friendly_assert (!THUNK_ALIAS (thunk_fndecl), 20031023);
+  gcc_assert (!THUNK_ALIAS (thunk_fndecl));
 
   if (TREE_ASM_WRITTEN (thunk_fndecl))
     return;
 
   if (TREE_ASM_WRITTEN (thunk_fndecl))
     return;
-  
+
   function = THUNK_TARGET (thunk_fndecl);
   if (DECL_RESULT (thunk_fndecl))
     /* We already turned this thunk into an ordinary function.
        There's no need to process this thunk again.  */
     return;
 
   function = THUNK_TARGET (thunk_fndecl);
   if (DECL_RESULT (thunk_fndecl))
     /* We already turned this thunk into an ordinary function.
        There's no need to process this thunk again.  */
     return;
 
+  if (DECL_THUNK_P (function))
+    /* The target is itself a thunk, process it now.  */
+    use_thunk (function, emit_p);
+
   /* Thunks are always addressable; they only appear in vtables.  */
   TREE_ADDRESSABLE (thunk_fndecl) = 1;
 
   /* Thunks are always addressable; they only appear in vtables.  */
   TREE_ADDRESSABLE (thunk_fndecl) = 1;
 
@@ -355,11 +325,11 @@ use_thunk (tree thunk_fndecl, bool emit_p)
       if (!this_adjusting)
        virtual_offset = BINFO_VPTR_FIELD (virtual_offset);
       virtual_value = tree_low_cst (virtual_offset, /*pos=*/0);
       if (!this_adjusting)
        virtual_offset = BINFO_VPTR_FIELD (virtual_offset);
       virtual_value = tree_low_cst (virtual_offset, /*pos=*/0);
-      my_friendly_assert (virtual_value, 20021026);
+      gcc_assert (virtual_value);
     }
   else
     virtual_value = 0;
     }
   else
     virtual_value = 0;
-  
+
   /* And, if we need to emit the thunk, it's used.  */
   mark_used (thunk_fndecl);
   /* This thunk is actually defined.  */
   /* And, if we need to emit the thunk, it's used.  */
   mark_used (thunk_fndecl);
   /* This thunk is actually defined.  */
@@ -368,8 +338,10 @@ use_thunk (tree thunk_fndecl, bool emit_p)
      rewrite.  */
   TREE_PUBLIC (thunk_fndecl) = TREE_PUBLIC (function);
   DECL_VISIBILITY (thunk_fndecl) = DECL_VISIBILITY (function);
      rewrite.  */
   TREE_PUBLIC (thunk_fndecl) = TREE_PUBLIC (function);
   DECL_VISIBILITY (thunk_fndecl) = DECL_VISIBILITY (function);
-  if (flag_weak && TREE_PUBLIC (thunk_fndecl))
-    comdat_linkage (thunk_fndecl);
+  DECL_VISIBILITY_SPECIFIED (thunk_fndecl)
+    = DECL_VISIBILITY_SPECIFIED (function);
+  if (DECL_ONE_ONLY (function) || DECL_WEAK (function))
+    make_decl_one_only (thunk_fndecl, cxx_comdat_group (thunk_fndecl));
 
   if (flag_syntax_only)
     {
 
   if (flag_syntax_only)
     {
@@ -393,10 +365,6 @@ use_thunk (tree thunk_fndecl, bool emit_p)
        }
     }
 
        }
     }
 
-  /* The back-end expects DECL_INITIAL to contain a BLOCK, so we
-     create one.  */
-  DECL_INITIAL (thunk_fndecl) = make_node (BLOCK);
-
   /* Set up cloned argument trees for the thunk.  */
   t = NULL_TREE;
   for (a = DECL_ARGUMENTS (function); a; a = TREE_CHAIN (a))
   /* Set up cloned argument trees for the thunk.  */
   t = NULL_TREE;
   for (a = DECL_ARGUMENTS (function); a; a = TREE_CHAIN (a))
@@ -405,34 +373,19 @@ use_thunk (tree thunk_fndecl, bool emit_p)
       TREE_CHAIN (x) = t;
       DECL_CONTEXT (x) = thunk_fndecl;
       SET_DECL_RTL (x, NULL_RTX);
       TREE_CHAIN (x) = t;
       DECL_CONTEXT (x) = thunk_fndecl;
       SET_DECL_RTL (x, NULL_RTX);
+      DECL_HAS_VALUE_EXPR_P (x) = 0;
       t = x;
     }
   a = nreverse (t);
   DECL_ARGUMENTS (thunk_fndecl) = a;
       t = x;
     }
   a = nreverse (t);
   DECL_ARGUMENTS (thunk_fndecl) = a;
-  BLOCK_VARS (DECL_INITIAL (thunk_fndecl)) = a;
-  
-  if (this_adjusting
-      && targetm.asm_out.can_output_mi_thunk (thunk_fndecl, fixed_offset,
-                                             virtual_value, alias))
-    {
-      const char *fnname;
-      current_function_decl = thunk_fndecl;
-      DECL_RESULT (thunk_fndecl)
-       = build_decl (RESULT_DECL, 0, integer_type_node);
-      fnname = XSTR (XEXP (DECL_RTL (thunk_fndecl), 0), 0);
-      init_function_start (thunk_fndecl);
-      current_function_is_thunk = 1;
-      assemble_start_function (thunk_fndecl, fnname);
-
-      targetm.asm_out.output_mi_thunk (asm_out_file, thunk_fndecl,
-                                      fixed_offset, virtual_value, alias);
-
-      assemble_end_function (thunk_fndecl, fnname);
-      current_function_decl = 0;
-      cfun = 0;
-      TREE_ASM_WRITTEN (thunk_fndecl) = 1;
-    }
-  else
+  TREE_ASM_WRITTEN (thunk_fndecl) = 1;
+  cgraph_add_thunk (thunk_fndecl, function,
+                   this_adjusting, fixed_offset, virtual_value,
+                   virtual_offset, alias);
+
+  if (!this_adjusting
+      || !targetm.asm_out.can_output_mi_thunk (thunk_fndecl, fixed_offset,
+                                              virtual_value, alias))
     {
       /* If this is a covariant thunk, or we don't have the necessary
         code for efficient thunks, generate a thunk function that
     {
       /* If this is a covariant thunk, or we don't have the necessary
         code for efficient thunks, generate a thunk function that
@@ -440,52 +393,8 @@ use_thunk (tree thunk_fndecl, bool emit_p)
         doesn't work for varargs.  */
 
       if (varargs_function_p (function))
         doesn't work for varargs.  */
 
       if (varargs_function_p (function))
-       error ("generic thunk code fails for method `%#D' which uses `...'",
+       error ("generic thunk code fails for method %q#D which uses %<...%>",
               function);
               function);
-
-      DECL_RESULT (thunk_fndecl) = NULL_TREE;
-
-      start_preparsed_function (thunk_fndecl, NULL_TREE, SF_PRE_PARSED);
-      /* We don't bother with a body block for thunks.  */
-
-      /* There's no need to check accessibility inside the thunk body.  */
-      push_deferring_access_checks (dk_no_check);
-
-      t = a;
-      if (this_adjusting)
-       t = thunk_adjust (t, /*this_adjusting=*/1,
-                         fixed_offset, virtual_offset);
-      
-      /* Build up the call to the real function.  */
-      t = tree_cons (NULL_TREE, t, NULL_TREE);
-      for (a = TREE_CHAIN (a); a; a = TREE_CHAIN (a))
-       t = tree_cons (NULL_TREE, a, t);
-      t = nreverse (t);
-      t = build_call (alias, t);
-      CALL_FROM_THUNK_P (t) = 1;
-      
-      if (VOID_TYPE_P (TREE_TYPE (t)))
-       finish_expr_stmt (t);
-      else
-       {
-         t = force_target_expr (TREE_TYPE (t), t);
-         if (!this_adjusting)
-           t = thunk_adjust (t, /*this_adjusting=*/0,
-                             fixed_offset, virtual_offset);
-         finish_return_stmt (t);
-       }
-
-      /* Since we want to emit the thunk, we explicitly mark its name as
-        referenced.  */
-      mark_decl_referenced (thunk_fndecl);
-
-      /* But we don't want debugging information about it.  */
-      DECL_IGNORED_P (thunk_fndecl) = 1;
-
-      /* Re-enable access control.  */
-      pop_deferring_access_checks ();
-
-      expand_body (finish_function (0));
     }
 
   pop_from_top_level ();
     }
 
   pop_from_top_level ();
@@ -493,13 +402,13 @@ use_thunk (tree thunk_fndecl, bool emit_p)
 \f
 /* Code for synthesizing methods which have default semantics defined.  */
 
 \f
 /* Code for synthesizing methods which have default semantics defined.  */
 
-/* Generate code for default X(X&) constructor.  */
+/* Generate code for default X(X&) or X(X&&) constructor.  */
 
 static void
 do_build_copy_constructor (tree fndecl)
 {
   tree parm = FUNCTION_FIRST_USER_PARM (fndecl);
 
 static void
 do_build_copy_constructor (tree fndecl)
 {
   tree parm = FUNCTION_FIRST_USER_PARM (fndecl);
-  tree t;
+  bool move_p = DECL_MOVE_CONSTRUCTOR_P (fndecl);
 
   parm = convert_from_reference (parm);
 
 
   parm = convert_from_reference (parm);
 
@@ -509,7 +418,7 @@ do_build_copy_constructor (tree fndecl)
        if *this is a base subobject.  */;
   else if (TYPE_HAS_TRIVIAL_INIT_REF (current_class_type))
     {
        if *this is a base subobject.  */;
   else if (TYPE_HAS_TRIVIAL_INIT_REF (current_class_type))
     {
-      t = build (INIT_EXPR, void_type_node, current_class_ref, parm);
+      tree t = build2 (INIT_EXPR, void_type_node, current_class_ref, parm);
       finish_expr_stmt (t);
     }
   else
       finish_expr_stmt (t);
     }
   else
@@ -519,7 +428,8 @@ do_build_copy_constructor (tree fndecl)
       int cvquals = cp_type_quals (TREE_TYPE (parm));
       int i;
       tree binfo, base_binfo;
       int cvquals = cp_type_quals (TREE_TYPE (parm));
       int i;
       tree binfo, base_binfo;
-      VEC (tree) *vbases;
+      tree init;
+      VEC(tree,gc) *vbases;
 
       /* Initialize all the base-classes with the parameter converted
         to their type so that we get their copy constructor and not
 
       /* Initialize all the base-classes with the parameter converted
         to their type so that we get their copy constructor and not
@@ -529,11 +439,12 @@ do_build_copy_constructor (tree fndecl)
       for (vbases = CLASSTYPE_VBASECLASSES (current_class_type), i = 0;
           VEC_iterate (tree, vbases, i, binfo); i++)
        {
       for (vbases = CLASSTYPE_VBASECLASSES (current_class_type), i = 0;
           VEC_iterate (tree, vbases, i, binfo); i++)
        {
-         member_init_list 
+         init = build_base_path (PLUS_EXPR, parm, binfo, 1);
+         if (move_p)
+           init = move (init);
+         member_init_list
            = tree_cons (binfo,
            = tree_cons (binfo,
-                        build_tree_list (NULL_TREE,
-                                         build_base_path (PLUS_EXPR, parm,
-                                                          binfo, 1)),
+                        build_tree_list (NULL_TREE, init),
                         member_init_list);
        }
 
                         member_init_list);
        }
 
@@ -541,34 +452,32 @@ do_build_copy_constructor (tree fndecl)
           BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
        {
          if (BINFO_VIRTUAL_P (base_binfo))
           BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
        {
          if (BINFO_VIRTUAL_P (base_binfo))
-           continue; 
+           continue;
 
 
-         member_init_list 
+         init = build_base_path (PLUS_EXPR, parm, base_binfo, 1);
+         if (move_p)
+           init = move (init);
+         member_init_list
            = tree_cons (base_binfo,
            = tree_cons (base_binfo,
-                        build_tree_list (NULL_TREE,
-                                         build_base_path (PLUS_EXPR, parm,
-                                                          base_binfo, 1)),
+                        build_tree_list (NULL_TREE, init),
                         member_init_list);
        }
 
       for (; fields; fields = TREE_CHAIN (fields))
        {
                         member_init_list);
        }
 
       for (; fields; fields = TREE_CHAIN (fields))
        {
-         tree init;
          tree field = fields;
          tree expr_type;
 
          if (TREE_CODE (field) != FIELD_DECL)
            continue;
 
          tree field = fields;
          tree expr_type;
 
          if (TREE_CODE (field) != FIELD_DECL)
            continue;
 
-         init = parm;
+         expr_type = TREE_TYPE (field);
          if (DECL_NAME (field))
            {
              if (VFIELD_NAME_P (DECL_NAME (field)))
                continue;
            }
          if (DECL_NAME (field))
            {
              if (VFIELD_NAME_P (DECL_NAME (field)))
                continue;
            }
-         else if ((t = TREE_TYPE (field)) != NULL_TREE
-                  && ANON_AGGR_TYPE_P (t)
-                  && TYPE_FIELDS (t) != NULL_TREE)
+         else if (ANON_AGGR_TYPE_P (expr_type) && TYPE_FIELDS (expr_type))
            /* Just use the field; anonymous types can't have
               nontrivial copy ctors or assignment ops.  */;
          else
            /* Just use the field; anonymous types can't have
               nontrivial copy ctors or assignment ops.  */;
          else
@@ -579,14 +488,22 @@ do_build_copy_constructor (tree fndecl)
             the field is "T", then the type will usually be "const
             T".  (There are no cv-qualified variants of reference
             types.)  */
             the field is "T", then the type will usually be "const
             T".  (There are no cv-qualified variants of reference
             types.)  */
-         expr_type = TREE_TYPE (field);
          if (TREE_CODE (expr_type) != REFERENCE_TYPE)
          if (TREE_CODE (expr_type) != REFERENCE_TYPE)
-           expr_type = cp_build_qualified_type (expr_type, cvquals);
-         init = build (COMPONENT_REF, expr_type, init, field, NULL_TREE);
+           {
+             int quals = cvquals;
+
+             if (DECL_MUTABLE_P (field))
+               quals &= ~TYPE_QUAL_CONST;
+             quals |= TYPE_QUALS (expr_type);
+             expr_type = cp_build_qualified_type (expr_type, quals);
+           }
+
+         init = build3 (COMPONENT_REF, expr_type, parm, field, NULL_TREE);
+         if (move_p && TREE_CODE (expr_type) != REFERENCE_TYPE)
+           init = move (init);
          init = build_tree_list (NULL_TREE, init);
 
          init = build_tree_list (NULL_TREE, init);
 
-         member_init_list
-           = tree_cons (field, init, member_init_list);
+         member_init_list = tree_cons (field, init, member_init_list);
        }
       finish_mem_initializers (member_init_list);
     }
        }
       finish_mem_initializers (member_init_list);
     }
@@ -607,7 +524,7 @@ do_build_assign_ref (tree fndecl)
        if *this is a base subobject.  */;
   else if (TYPE_HAS_TRIVIAL_ASSIGN_REF (current_class_type))
     {
        if *this is a base subobject.  */;
   else if (TYPE_HAS_TRIVIAL_ASSIGN_REF (current_class_type))
     {
-      tree t = build (MODIFY_EXPR, void_type_node, current_class_ref, parm);
+      tree t = build2 (MODIFY_EXPR, void_type_node, current_class_ref, parm);
       finish_expr_stmt (t);
     }
   else
       finish_expr_stmt (t);
     }
   else
@@ -622,75 +539,88 @@ do_build_assign_ref (tree fndecl)
           BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
        {
          tree converted_parm;
           BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
        {
          tree converted_parm;
+         VEC(tree,gc) *parmvec;
 
          /* We must convert PARM directly to the base class
             explicitly since the base class may be ambiguous.  */
          converted_parm = build_base_path (PLUS_EXPR, parm, base_binfo, 1);
          /* Call the base class assignment operator.  */
 
          /* We must convert PARM directly to the base class
             explicitly since the base class may be ambiguous.  */
          converted_parm = build_base_path (PLUS_EXPR, parm, base_binfo, 1);
          /* Call the base class assignment operator.  */
-         finish_expr_stmt 
-           (build_special_member_call (current_class_ref, 
+         parmvec = make_tree_vector_single (converted_parm);
+         finish_expr_stmt
+           (build_special_member_call (current_class_ref,
                                        ansi_assopname (NOP_EXPR),
                                        ansi_assopname (NOP_EXPR),
-                                       build_tree_list (NULL_TREE, 
-                                                        converted_parm),
+                                       &parmvec,
                                        base_binfo,
                                        base_binfo,
-                                       LOOKUP_NORMAL | LOOKUP_NONVIRTUAL));
+                                       LOOKUP_NORMAL | LOOKUP_NONVIRTUAL,
+                                        tf_warning_or_error));
+         release_tree_vector (parmvec);
        }
 
       /* Assign to each of the non-static data members.  */
        }
 
       /* Assign to each of the non-static data members.  */
-      for (fields = TYPE_FIELDS (current_class_type); 
-          fields; 
+      for (fields = TYPE_FIELDS (current_class_type);
+          fields;
           fields = TREE_CHAIN (fields))
        {
           fields = TREE_CHAIN (fields))
        {
-         tree comp, init, t;
+         tree comp = current_class_ref;
+         tree init = parm;
          tree field = fields;
          tree field = fields;
+         tree expr_type;
+         int quals;
 
          if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
            continue;
 
 
          if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
            continue;
 
-         if (CP_TYPE_CONST_P (TREE_TYPE (field)))
+         expr_type = TREE_TYPE (field);
+
+         if (CP_TYPE_CONST_P (expr_type))
            {
            {
-              error ("non-static const member `%#D', can't use default assignment operator", field);
+             error ("non-static const member %q#D, can't use default "
+                    "assignment operator", field);
              continue;
            }
              continue;
            }
-         else if (TREE_CODE (TREE_TYPE (field)) == REFERENCE_TYPE)
+         else if (TREE_CODE (expr_type) == REFERENCE_TYPE)
            {
            {
-             error ("non-static reference member `%#D', can't use default assignment operator", field);
+             error ("non-static reference member %q#D, can't use "
+                    "default assignment operator", field);
              continue;
            }
 
              continue;
            }
 
-         comp = current_class_ref;
-         init = parm;
-
          if (DECL_NAME (field))
            {
              if (VFIELD_NAME_P (DECL_NAME (field)))
                continue;
            }
          if (DECL_NAME (field))
            {
              if (VFIELD_NAME_P (DECL_NAME (field)))
                continue;
            }
-         else if ((t = TREE_TYPE (field)) != NULL_TREE
-                  && ANON_AGGR_TYPE_P (t)
-                  && TYPE_FIELDS (t) != NULL_TREE)
+         else if (ANON_AGGR_TYPE_P (expr_type)
+                  && TYPE_FIELDS (expr_type) != NULL_TREE)
            /* Just use the field; anonymous types can't have
               nontrivial copy ctors or assignment ops.  */;
          else
            continue;
 
            /* Just use the field; anonymous types can't have
               nontrivial copy ctors or assignment ops.  */;
          else
            continue;
 
-         comp = build (COMPONENT_REF, TREE_TYPE (field), comp, field,
-                       NULL_TREE);
-         init = build (COMPONENT_REF,
-                       cp_build_qualified_type (TREE_TYPE (field), cvquals),
-                       init, field, NULL_TREE);
+         comp = build3 (COMPONENT_REF, expr_type, comp, field, NULL_TREE);
+
+         /* Compute the type of init->field  */
+         quals = cvquals;
+         if (DECL_MUTABLE_P (field))
+           quals &= ~TYPE_QUAL_CONST;
+         expr_type = cp_build_qualified_type (expr_type, quals);
+
+         init = build3 (COMPONENT_REF, expr_type, init, field, NULL_TREE);
 
          if (DECL_NAME (field))
 
          if (DECL_NAME (field))
-           finish_expr_stmt (build_modify_expr (comp, NOP_EXPR, init));
+           init = cp_build_modify_expr (comp, NOP_EXPR, init, 
+                                        tf_warning_or_error);
          else
          else
-           finish_expr_stmt (build (MODIFY_EXPR, TREE_TYPE (comp), comp,
-                                    init));
+           init = build2 (MODIFY_EXPR, TREE_TYPE (comp), comp, init);
+         finish_expr_stmt (init);
        }
     }
   finish_return_stmt (current_class_ref);
   finish_compound_stmt (compound_stmt);
 }
 
        }
     }
   finish_return_stmt (current_class_ref);
   finish_compound_stmt (compound_stmt);
 }
 
+/* Synthesize FNDECL, a non-static member function.   */
+
 void
 synthesize_method (tree fndecl)
 {
 void
 synthesize_method (tree fndecl)
 {
@@ -698,18 +628,20 @@ synthesize_method (tree fndecl)
   tree context = decl_function_context (fndecl);
   bool need_body = true;
   tree stmt;
   tree context = decl_function_context (fndecl);
   bool need_body = true;
   tree stmt;
+  location_t save_input_location = input_location;
+  int error_count = errorcount;
+  int warning_count = warningcount;
 
 
-  if (at_eof)
-    import_export_decl (fndecl);
+  /* Reset the source location, we might have been previously
+     deferred, and thus have saved where we were first needed.  */
+  DECL_SOURCE_LOCATION (fndecl)
+    = DECL_SOURCE_LOCATION (TYPE_NAME (DECL_CONTEXT (fndecl)));
 
   /* If we've been asked to synthesize a clone, just synthesize the
      cloned function instead.  Doing so will automatically fill in the
      body for the clone.  */
   if (DECL_CLONED_FUNCTION_P (fndecl))
 
   /* If we've been asked to synthesize a clone, just synthesize the
      cloned function instead.  Doing so will automatically fill in the
      body for the clone.  */
   if (DECL_CLONED_FUNCTION_P (fndecl))
-    {
-      synthesize_method (DECL_CLONED_FUNCTION (fndecl));
-      return;
-    }
+    fndecl = DECL_CLONED_FUNCTION (fndecl);
 
   /* We may be in the middle of deferred access check.  Disable
      it now.  */
 
   /* We may be in the middle of deferred access check.  Disable
      it now.  */
@@ -718,17 +650,10 @@ synthesize_method (tree fndecl)
   if (! context)
     push_to_top_level ();
   else if (nested)
   if (! context)
     push_to_top_level ();
   else if (nested)
-    push_function_context_to (context);
+    push_function_context ();
 
 
-  /* Put the function definition at the position where it is needed,
-     rather than within the body of the class.  That way, an error
-     during the generation of the implicit body points at the place
-     where the attempt to generate the function occurs, giving the
-     user a hint as to why we are attempting to generate the
-     function.  */
-  DECL_SOURCE_LOCATION (fndecl) = input_location;
+  input_location = DECL_SOURCE_LOCATION (fndecl);
 
 
-  interface_unknown = 1;
   start_preparsed_function (fndecl, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED);
   stmt = begin_function_body ();
 
   start_preparsed_function (fndecl, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED);
   stmt = begin_function_body ();
 
@@ -742,7 +667,7 @@ synthesize_method (tree fndecl)
       tree arg_chain = FUNCTION_FIRST_USER_PARMTYPE (fndecl);
       if (arg_chain != void_list_node)
        do_build_copy_constructor (fndecl);
       tree arg_chain = FUNCTION_FIRST_USER_PARMTYPE (fndecl);
       if (arg_chain != void_list_node)
        do_build_copy_constructor (fndecl);
-      else if (TYPE_NEEDS_CONSTRUCTING (current_class_type))
+      else
        finish_mem_initializers (NULL_TREE);
     }
 
        finish_mem_initializers (NULL_TREE);
     }
 
@@ -758,13 +683,18 @@ synthesize_method (tree fndecl)
   finish_function_body (stmt);
   expand_or_defer_fn (finish_function (0));
 
   finish_function_body (stmt);
   expand_or_defer_fn (finish_function (0));
 
-  extract_interface_info ();
+  input_location = save_input_location;
+
   if (! context)
     pop_from_top_level ();
   else if (nested)
   if (! context)
     pop_from_top_level ();
   else if (nested)
-    pop_function_context_from (context);
+    pop_function_context ();
 
   pop_deferring_access_checks ();
 
   pop_deferring_access_checks ();
+
+  if (error_count != errorcount || warning_count != warningcount)
+    inform (input_location, "synthesized method %qD first required here ",
+           fndecl);
 }
 
 /* Use EXTRACTOR to locate the relevant function called for each base &
 }
 
 /* Use EXTRACTOR to locate the relevant function called for each base &
@@ -775,7 +705,7 @@ synthesize_method (tree fndecl)
 
 static tree
 synthesize_exception_spec (tree type, tree (*extractor) (tree, void*),
 
 static tree
 synthesize_exception_spec (tree type, tree (*extractor) (tree, void*),
-                           void *client)
+                          void *client)
 {
   tree raises = empty_except_spec;
   tree fields = TYPE_FIELDS (type);
 {
   tree raises = empty_except_spec;
   tree fields = TYPE_FIELDS (type);
@@ -787,52 +717,50 @@ synthesize_exception_spec (tree type, tree (*extractor) (tree, void*),
     {
       tree fn = (*extractor) (BINFO_TYPE (base_binfo), client);
       if (fn)
     {
       tree fn = (*extractor) (BINFO_TYPE (base_binfo), client);
       if (fn)
-        {
-          tree fn_raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
-          
-          raises = merge_exception_specifiers (raises, fn_raises);
-        }
+       {
+         tree fn_raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
+
+         raises = merge_exception_specifiers (raises, fn_raises);
+       }
     }
   for (; fields; fields = TREE_CHAIN (fields))
     {
       tree type = TREE_TYPE (fields);
       tree fn;
     }
   for (; fields; fields = TREE_CHAIN (fields))
     {
       tree type = TREE_TYPE (fields);
       tree fn;
-      
+
       if (TREE_CODE (fields) != FIELD_DECL || DECL_ARTIFICIAL (fields))
       if (TREE_CODE (fields) != FIELD_DECL || DECL_ARTIFICIAL (fields))
-        continue;
+       continue;
       while (TREE_CODE (type) == ARRAY_TYPE)
       while (TREE_CODE (type) == ARRAY_TYPE)
-       type = TREE_TYPE (type);
-      if (TREE_CODE (type) != RECORD_TYPE)
-        continue;
-      
+       type = TREE_TYPE (type);
+      if (!CLASS_TYPE_P (type))
+       continue;
+
       fn = (*extractor) (type, client);
       if (fn)
       fn = (*extractor) (type, client);
       if (fn)
-        {
-          tree fn_raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
-          
-          raises = merge_exception_specifiers (raises, fn_raises);
-        }
+       {
+         tree fn_raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
+
+         raises = merge_exception_specifiers (raises, fn_raises);
+       }
     }
   return raises;
 }
 
 /* Locate the dtor of TYPE.  */
 
     }
   return raises;
 }
 
 /* Locate the dtor of TYPE.  */
 
-static tree
+tree
 locate_dtor (tree type, void *client ATTRIBUTE_UNUSED)
 {
 locate_dtor (tree type, void *client ATTRIBUTE_UNUSED)
 {
-  return (CLASSTYPE_METHOD_VEC (type) 
-         ? CLASSTYPE_DESTRUCTORS (type) 
-         : NULL_TREE);
+  return CLASSTYPE_DESTRUCTORS (type);
 }
 
 /* Locate the default ctor of TYPE.  */
 
 }
 
 /* Locate the default ctor of TYPE.  */
 
-static tree
+tree
 locate_ctor (tree type, void *client ATTRIBUTE_UNUSED)
 {
   tree fns;
 locate_ctor (tree type, void *client ATTRIBUTE_UNUSED)
 {
   tree fns;
-  
+
   if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
     return NULL_TREE;
 
   if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
     return NULL_TREE;
 
@@ -845,11 +773,13 @@ locate_ctor (tree type, void *client ATTRIBUTE_UNUSED)
     {
       tree fn = OVL_CURRENT (fns);
       tree parms = TYPE_ARG_TYPES (TREE_TYPE (fn));
     {
       tree fn = OVL_CURRENT (fns);
       tree parms = TYPE_ARG_TYPES (TREE_TYPE (fn));
-      
-      if (sufficient_parms_p (TREE_CHAIN (parms)))
-        return fn;
+
+      parms = skip_artificial_parms_for (fn, parms);
+
+      if (sufficient_parms_p (parms))
+       return fn;
     }
     }
-  return NULL_TREE;
+  gcc_unreachable ();
 }
 
 struct copy_data
 }
 
 struct copy_data
@@ -862,14 +792,14 @@ struct copy_data
    points to a COPY_DATA holding the name (NULL for the ctor)
    and desired qualifiers of the source operand.  */
 
    points to a COPY_DATA holding the name (NULL for the ctor)
    and desired qualifiers of the source operand.  */
 
-static tree
+tree
 locate_copy (tree type, void *client_)
 {
   struct copy_data *client = (struct copy_data *)client_;
   tree fns;
   tree best = NULL_TREE;
   bool excess_p = false;
 locate_copy (tree type, void *client_)
 {
   struct copy_data *client = (struct copy_data *)client_;
   tree fns;
   tree best = NULL_TREE;
   bool excess_p = false;
-  
+
   if (client->name)
     {
       int ix;
   if (client->name)
     {
       int ix;
@@ -884,6 +814,8 @@ locate_copy (tree type, void *client_)
         it now.  */
       if (CLASSTYPE_LAZY_COPY_CTOR (type))
        lazily_declare_fn (sfk_copy_constructor, type);
         it now.  */
       if (CLASSTYPE_LAZY_COPY_CTOR (type))
        lazily_declare_fn (sfk_copy_constructor, type);
+      if (CLASSTYPE_LAZY_MOVE_CTOR (type))
+       lazily_declare_fn (sfk_move_constructor, type);
       fns = CLASSTYPE_CONSTRUCTORS (type);
     }
   else
       fns = CLASSTYPE_CONSTRUCTORS (type);
     }
   else
@@ -895,27 +827,31 @@ locate_copy (tree type, void *client_)
       tree src_type;
       int excess;
       int quals;
       tree src_type;
       int excess;
       int quals;
-      
-      parms = TREE_CHAIN (parms);
+
+      parms = skip_artificial_parms_for (fn, parms);
       if (!parms)
       if (!parms)
-        continue;
+       continue;
       src_type = non_reference (TREE_VALUE (parms));
       src_type = non_reference (TREE_VALUE (parms));
+
+      if (src_type == error_mark_node)
+        return NULL_TREE;
+
       if (!same_type_ignoring_top_level_qualifiers_p (src_type, type))
       if (!same_type_ignoring_top_level_qualifiers_p (src_type, type))
-        continue;
+       continue;
       if (!sufficient_parms_p (TREE_CHAIN (parms)))
       if (!sufficient_parms_p (TREE_CHAIN (parms)))
-        continue;
+       continue;
       quals = cp_type_quals (src_type);
       if (client->quals & ~quals)
       quals = cp_type_quals (src_type);
       if (client->quals & ~quals)
-        continue;
+       continue;
       excess = quals & ~client->quals;
       if (!best || (excess_p && !excess))
       excess = quals & ~client->quals;
       if (!best || (excess_p && !excess))
-        {
-          best = fn;
-          excess_p = excess;
-        }
+       {
+         best = fn;
+         excess_p = excess;
+       }
       else
       else
-        /* Ambiguous */
-        return NULL_TREE;
+       /* Ambiguous */
+       return NULL_TREE;
     }
   return best;
 }
     }
   return best;
 }
@@ -923,21 +859,47 @@ locate_copy (tree type, void *client_)
 /* Implicitly declare the special function indicated by KIND, as a
    member of TYPE.  For copy constructors and assignment operators,
    CONST_P indicates whether these functions should take a const
 /* Implicitly declare the special function indicated by KIND, as a
    member of TYPE.  For copy constructors and assignment operators,
    CONST_P indicates whether these functions should take a const
-   reference argument or a non-const reference.  */
+   reference argument or a non-const reference.  Returns the
+   FUNCTION_DECL for the implicitly declared function.  */
 
 
-tree
+static tree
 implicitly_declare_fn (special_function_kind kind, tree type, bool const_p)
 {
   tree fn;
   tree parameter_types = void_list_node;
 implicitly_declare_fn (special_function_kind kind, tree type, bool const_p)
 {
   tree fn;
   tree parameter_types = void_list_node;
-  tree return_type = void_type_node;
+  tree return_type;
   tree fn_type;
   tree raises = empty_except_spec;
   tree rhs_parm_type = NULL_TREE;
   tree fn_type;
   tree raises = empty_except_spec;
   tree rhs_parm_type = NULL_TREE;
+  tree this_parm;
   tree name;
   tree name;
+  HOST_WIDE_INT saved_processing_template_decl;
+
+  /* Because we create declarations for implicitly declared functions
+     lazily, we may be creating the declaration for a member of TYPE
+     while in some completely different context.  However, TYPE will
+     never be a dependent class (because we never want to do lookups
+     for implicitly defined functions in a dependent class).
+     Furthermore, we must set PROCESSING_TEMPLATE_DECL to zero here
+     because we only create clones for constructors and destructors
+     when not in a template.  */
+  gcc_assert (!dependent_type_p (type));
+  saved_processing_template_decl = processing_template_decl;
+  processing_template_decl = 0;
 
   type = TYPE_MAIN_VARIANT (type);
 
 
   type = TYPE_MAIN_VARIANT (type);
 
+  if (targetm.cxx.cdtor_returns_this () && !TYPE_FOR_JAVA (type))
+    {
+      if (kind == sfk_destructor)
+       /* See comment in check_special_function_return_type.  */
+       return_type = build_pointer_type (void_type_node);
+      else
+       return_type = build_pointer_type (type);
+    }
+  else
+    return_type = void_type_node;
+
   switch (kind)
     {
     case sfk_destructor:
   switch (kind)
     {
     case sfk_destructor:
@@ -954,34 +916,37 @@ implicitly_declare_fn (special_function_kind kind, tree type, bool const_p)
 
     case sfk_copy_constructor:
     case sfk_assignment_operator:
 
     case sfk_copy_constructor:
     case sfk_assignment_operator:
+    case sfk_move_constructor:
     {
       struct copy_data data;
     {
       struct copy_data data;
-      
+
       data.name = NULL;
       data.quals = 0;
       if (kind == sfk_assignment_operator)
       data.name = NULL;
       data.quals = 0;
       if (kind == sfk_assignment_operator)
-        {
+       {
          return_type = build_reference_type (type);
          return_type = build_reference_type (type);
-          name = ansi_assopname (NOP_EXPR);
-          data.name = name;
-        }
+         name = ansi_assopname (NOP_EXPR);
+         data.name = name;
+       }
       else
        name = constructor_name (type);
 
       if (const_p)
       else
        name = constructor_name (type);
 
       if (const_p)
-        {
-          data.quals = TYPE_QUAL_CONST;
+       {
+         data.quals = TYPE_QUAL_CONST;
          rhs_parm_type = build_qualified_type (type, TYPE_QUAL_CONST);
          rhs_parm_type = build_qualified_type (type, TYPE_QUAL_CONST);
-        }
+       }
       else
        rhs_parm_type = type;
       else
        rhs_parm_type = type;
-      rhs_parm_type = build_reference_type (rhs_parm_type);
+      rhs_parm_type
+       = cp_build_reference_type (rhs_parm_type,
+                                  kind == sfk_move_constructor);
       parameter_types = tree_cons (NULL_TREE, rhs_parm_type, parameter_types);
       raises = synthesize_exception_spec (type, &locate_copy, &data);
       break;
     }
     default:
       parameter_types = tree_cons (NULL_TREE, rhs_parm_type, parameter_types);
       raises = synthesize_exception_spec (type, &locate_copy, &data);
       break;
     }
     default:
-      abort ();
+      gcc_unreachable ();
     }
 
   /* Create the function.  */
     }
 
   /* Create the function.  */
@@ -990,7 +955,8 @@ implicitly_declare_fn (special_function_kind kind, tree type, bool const_p)
     fn_type = build_exception_variant (fn_type, raises);
   fn = build_lang_decl (FUNCTION_DECL, name, fn_type);
   DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (TYPE_NAME (type));
     fn_type = build_exception_variant (fn_type, raises);
   fn = build_lang_decl (FUNCTION_DECL, name, fn_type);
   DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (TYPE_NAME (type));
-  if (kind == sfk_constructor || kind == sfk_copy_constructor)
+  if (kind == sfk_constructor || kind == sfk_copy_constructor
+      || kind == sfk_move_constructor)
     DECL_CONSTRUCTOR_P (fn) = 1;
   else if (kind == sfk_destructor)
     DECL_DESTRUCTOR_P (fn) = 1;
     DECL_CONSTRUCTOR_P (fn) = 1;
   else if (kind == sfk_destructor)
     DECL_DESTRUCTOR_P (fn) = 1;
@@ -999,8 +965,15 @@ implicitly_declare_fn (special_function_kind kind, tree type, bool const_p)
       DECL_ASSIGNMENT_OPERATOR_P (fn) = 1;
       SET_OVERLOADED_OPERATOR_CODE (fn, NOP_EXPR);
     }
       DECL_ASSIGNMENT_OPERATOR_P (fn) = 1;
       SET_OVERLOADED_OPERATOR_CODE (fn, NOP_EXPR);
     }
-  /* Create the argument list.  The call to "grokclassfn" will add the
-     "this" parameter and any other implicit parameters.  */
+  
+  /* If pointers to member functions use the least significant bit to
+     indicate whether a function is virtual, ensure a pointer
+     to this function will have that bit clear.  */
+  if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn
+      && DECL_ALIGN (fn) < 2 * BITS_PER_UNIT)
+    DECL_ALIGN (fn) = 2 * BITS_PER_UNIT;
+
+  /* Create the explicit arguments.  */
   if (rhs_parm_type)
     {
       /* Note that this parameter is *not* marked DECL_ARTIFICIAL; we
   if (rhs_parm_type)
     {
       /* Note that this parameter is *not* marked DECL_ARTIFICIAL; we
@@ -1009,24 +982,112 @@ implicitly_declare_fn (special_function_kind kind, tree type, bool const_p)
       DECL_ARGUMENTS (fn) = cp_build_parm_decl (NULL_TREE, rhs_parm_type);
       TREE_READONLY (DECL_ARGUMENTS (fn)) = 1;
     }
       DECL_ARGUMENTS (fn) = cp_build_parm_decl (NULL_TREE, rhs_parm_type);
       TREE_READONLY (DECL_ARGUMENTS (fn)) = 1;
     }
-
-  grokclassfn (type, fn, kind == sfk_destructor ? DTOR_FLAG : NO_SPECIAL,
-              TYPE_UNQUALIFIED);
-  grok_special_member_properties (fn);
-  TREE_PUBLIC (fn) = !decl_function_context (TYPE_MAIN_DECL (type));
-  rest_of_decl_compilation (fn, /*asmspec=*/NULL,
-                           toplevel_bindings_p (), at_eof);
+  /* Add the "this" parameter.  */
+  this_parm = build_this_parm (fn_type, TYPE_UNQUALIFIED);
+  TREE_CHAIN (this_parm) = DECL_ARGUMENTS (fn);
+  DECL_ARGUMENTS (fn) = this_parm;
+
+  grokclassfn (type, fn, kind == sfk_destructor ? DTOR_FLAG : NO_SPECIAL);
+  set_linkage_according_to_type (type, fn);
+  rest_of_decl_compilation (fn, toplevel_bindings_p (), at_eof);
   DECL_IN_AGGR_P (fn) = 1;
   DECL_ARTIFICIAL (fn) = 1;
   DECL_IN_AGGR_P (fn) = 1;
   DECL_ARTIFICIAL (fn) = 1;
+  DECL_DEFAULTED_FN (fn) = 1;
   DECL_NOT_REALLY_EXTERN (fn) = 1;
   DECL_DECLARED_INLINE_P (fn) = 1;
   DECL_NOT_REALLY_EXTERN (fn) = 1;
   DECL_DECLARED_INLINE_P (fn) = 1;
-  DECL_INLINE (fn) = 1;
-  if (TREE_USED (fn))
-    abort ();
-  
+  gcc_assert (!TREE_USED (fn));
+
+  /* Restore PROCESSING_TEMPLATE_DECL.  */
+  processing_template_decl = saved_processing_template_decl;
+
   return fn;
 }
 
   return fn;
 }
 
+/* Gives any errors about defaulted functions which need to be deferred
+   until the containing class is complete.  */
+
+void
+defaulted_late_check (tree fn)
+{
+  /* Complain about invalid signature for defaulted fn.  */
+  tree ctx = DECL_CONTEXT (fn);
+  special_function_kind kind = special_function_p (fn);
+  bool fn_const_p = (copy_fn_p (fn) == 2);
+  tree implicit_fn = implicitly_declare_fn (kind, ctx, fn_const_p);
+
+  if (!same_type_p (TREE_TYPE (TREE_TYPE (fn)),
+                   TREE_TYPE (TREE_TYPE (implicit_fn)))
+      || !compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
+                    TYPE_ARG_TYPES (TREE_TYPE (implicit_fn))))
+    {
+      error ("defaulted declaration %q+D", fn);
+      error_at (DECL_SOURCE_LOCATION (fn),
+               "does not match expected signature %qD", implicit_fn);
+    }
+}
+
+/* Returns true iff FN can be explicitly defaulted, and gives any
+   errors if defaulting FN is ill-formed.  */
+
+bool
+defaultable_fn_check (tree fn)
+{
+  special_function_kind kind = sfk_none;
+
+  if (DECL_CONSTRUCTOR_P (fn))
+    {
+      if (FUNCTION_FIRST_USER_PARMTYPE (fn) == void_list_node)
+       kind = sfk_constructor;
+      else if (copy_fn_p (fn) > 0
+              && (TREE_CHAIN (FUNCTION_FIRST_USER_PARMTYPE (fn))
+                  == void_list_node))
+       kind = sfk_copy_constructor;
+      else if (move_fn_p (fn))
+       kind = sfk_move_constructor;
+    }
+  else if (DECL_DESTRUCTOR_P (fn))
+    kind = sfk_destructor;
+  else if (DECL_ASSIGNMENT_OPERATOR_P (fn)
+          && DECL_OVERLOADED_OPERATOR_P (fn) == NOP_EXPR
+          && copy_fn_p (fn))
+    kind = sfk_assignment_operator;
+
+  if (kind == sfk_none)
+    {
+      error ("%qD cannot be defaulted", fn);
+      return false;
+    }
+  else
+    {
+      tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
+      for (; t && t != void_list_node; t = TREE_CHAIN (t))
+       if (TREE_PURPOSE (t))
+         {
+           error ("defaulted function %q+D with default argument", fn);
+           break;
+         }
+      if (TYPE_BEING_DEFINED (DECL_CONTEXT (fn)))
+       {
+         if (DECL_NONCONVERTING_P (fn))
+           error ("%qD declared explicit cannot be defaulted in the class "
+                  "body", fn);
+         if (current_access_specifier != access_public_node)
+           error ("%qD declared with non-public access cannot be defaulted "
+                  "in the class body", fn);
+         if (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)))
+           error ("function %q+D defaulted on its first declaration "
+                  "must not have an exception-specification", fn);
+         if (DECL_VIRTUAL_P (fn))
+           error ("%qD declared virtual cannot be defaulted in the class "
+                  "body", fn);
+       }
+      else if (!processing_template_decl)
+       defaulted_late_check (fn);
+
+      return true;
+    }
+}
+
 /* Add an implicit declaration to TYPE for the kind of function
    indicated by SFK.  Return the FUNCTION_DECL for the new implicit
    declaration.  */
 /* Add an implicit declaration to TYPE for the kind of function
    indicated by SFK.  Return the FUNCTION_DECL for the new implicit
    declaration.  */
@@ -1048,24 +1109,48 @@ lazily_declare_fn (special_function_kind sfk, tree type)
     const_p = false;
   /* Declare the function.  */
   fn = implicitly_declare_fn (sfk, type, const_p);
     const_p = false;
   /* Declare the function.  */
   fn = implicitly_declare_fn (sfk, type, const_p);
+  /* A destructor may be virtual.  */
+  if (sfk == sfk_destructor)
+    check_for_override (fn, type);
   /* Add it to CLASSTYPE_METHOD_VEC.  */
   /* Add it to CLASSTYPE_METHOD_VEC.  */
-  add_method (type, fn);
+  add_method (type, fn, NULL_TREE);
   /* Add it to TYPE_METHODS.  */
   /* Add it to TYPE_METHODS.  */
-  TREE_CHAIN (fn) = TYPE_METHODS (type);
-  TYPE_METHODS (type) = fn;
+  if (sfk == sfk_destructor
+      && DECL_VIRTUAL_P (fn)
+      && abi_version_at_least (2))
+    /* The ABI requires that a virtual destructor go at the end of the
+       vtable.  */
+    TYPE_METHODS (type) = chainon (TYPE_METHODS (type), fn);
+  else
+    {
+      /* G++ 3.2 put the implicit destructor at the *beginning* of the
+        TYPE_METHODS list, which cause the destructor to be emitted
+        in an incorrect location in the vtable.  */
+      if (warn_abi && sfk == sfk_destructor && DECL_VIRTUAL_P (fn))
+       warning (OPT_Wabi, "vtable layout for class %qT may not be ABI-compliant"
+                "and may change in a future version of GCC due to "
+                "implicit virtual destructor",
+                type);
+      TREE_CHAIN (fn) = TYPE_METHODS (type);
+      TYPE_METHODS (type) = fn;
+    }
   maybe_add_class_template_decl_list (type, fn, /*friend_p=*/0);
   maybe_add_class_template_decl_list (type, fn, /*friend_p=*/0);
-  if (sfk == sfk_constructor || sfk == sfk_copy_constructor)
+  if (sfk == sfk_assignment_operator)
+    CLASSTYPE_LAZY_ASSIGNMENT_OP (type) = 0;
+  else
     {
       /* Remember that the function has been created.  */
       if (sfk == sfk_constructor)
        CLASSTYPE_LAZY_DEFAULT_CTOR (type) = 0;
     {
       /* Remember that the function has been created.  */
       if (sfk == sfk_constructor)
        CLASSTYPE_LAZY_DEFAULT_CTOR (type) = 0;
-      else
+      else if (sfk == sfk_copy_constructor)
        CLASSTYPE_LAZY_COPY_CTOR (type) = 0;
        CLASSTYPE_LAZY_COPY_CTOR (type) = 0;
+      else if (sfk == sfk_move_constructor)
+       CLASSTYPE_LAZY_MOVE_CTOR (type) = 0;
+      else if (sfk == sfk_destructor)
+       CLASSTYPE_LAZY_DESTRUCTOR (type) = 0;
       /* Create appropriate clones.  */
       clone_function_decl (fn, /*update_method_vec=*/true);
     }
       /* Create appropriate clones.  */
       clone_function_decl (fn, /*update_method_vec=*/true);
     }
-  else if (sfk == sfk_assignment_operator)
-    CLASSTYPE_LAZY_ASSIGNMENT_OP (type) = 0;
 
   return fn;
 }
 
   return fn;
 }
@@ -1074,7 +1159,7 @@ lazily_declare_fn (special_function_kind sfk, tree type)
    as there are artificial parms in FN.  */
 
 tree
    as there are artificial parms in FN.  */
 
 tree
-skip_artificial_parms_for (tree fn, tree list)
+skip_artificial_parms_for (const_tree fn, tree list)
 {
   if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
     list = TREE_CHAIN (list);
 {
   if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
     list = TREE_CHAIN (list);
@@ -1088,4 +1173,25 @@ skip_artificial_parms_for (tree fn, tree list)
   return list;
 }
 
   return list;
 }
 
+/* Given a FUNCTION_DECL FN and a chain LIST, return the number of
+   artificial parms in FN.  */
+
+int
+num_artificial_parms_for (const_tree fn)
+{
+  int count = 0;
+
+  if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
+    count++;
+  else
+    return 0;
+
+  if (DECL_HAS_IN_CHARGE_PARM_P (fn))
+    count++;
+  if (DECL_HAS_VTT_PARM_P (fn))
+    count++;
+  return count;
+}
+
+
 #include "gt-cp-method.h"
 #include "gt-cp-method.h"