OSDN Git Service

gcc/testsuite
[pf3gnuchains/gcc-fork.git] / gcc / cp / init.c
index 1909319..a6da19f 100644 (file)
@@ -1,6 +1,6 @@
 /* Handle initialization things in C++.
    Copyright (C) 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
-   1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
+   1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
    Free Software Foundation, Inc.
    Contributed by Michael Tiemann (tiemann@cygnus.com)
 
@@ -51,7 +51,6 @@ static tree initializing_context (tree);
 static void expand_cleanup_for_base (tree, tree);
 static tree get_temp_regvar (tree, tree);
 static tree dfs_initialize_vtbl_ptrs (tree, void *);
-static tree build_default_init (tree, tree);
 static tree build_dtor_call (tree, special_function_kind, int);
 static tree build_field_list (tree, tree, int *);
 static tree build_vtbl_address (tree);
@@ -137,11 +136,12 @@ initialize_vtbl_ptrs (tree addr)
 /* Return an expression for the zero-initialization of an object with
    type T.  This expression will either be a constant (in the case
    that T is a scalar), or a CONSTRUCTOR (in the case that T is an
-   aggregate).  In either case, the value can be used as DECL_INITIAL
-   for a decl of the indicated TYPE; it is a valid static initializer.
-   If NELTS is non-NULL, and TYPE is an ARRAY_TYPE, NELTS is the
-   number of elements in the array.  If STATIC_STORAGE_P is TRUE,
-   initializers are only generated for entities for which
+   aggregate), or NULL (in the case that T does not require
+   initialization).  In either case, the value can be used as
+   DECL_INITIAL for a decl of the indicated TYPE; it is a valid static
+   initializer. If NELTS is non-NULL, and TYPE is an ARRAY_TYPE, NELTS
+   is the number of elements in the array.  If STATIC_STORAGE_P is
+   TRUE, initializers are only generated for entities for which
    zero-initialization does not simply mean filling the storage with
    zero bytes.  */
 
@@ -200,7 +200,8 @@ build_zero_init (tree type, tree nelts, bool static_storage_p)
              tree value = build_zero_init (TREE_TYPE (field),
                                            /*nelts=*/NULL_TREE,
                                            static_storage_p);
-             CONSTRUCTOR_APPEND_ELT(v, field, value);
+             if (value)
+               CONSTRUCTOR_APPEND_ELT(v, field, value);
            }
 
          /* For unions, only the first field is initialized.  */
@@ -275,7 +276,7 @@ build_zero_init (tree type, tree nelts, bool static_storage_p)
    returns NULL_TREE; the caller is responsible for arranging for the
    constructors to be called.  */
 
-static tree
+tree
 build_default_init (tree type, tree nelts)
 {
   /* [dcl.init]:
@@ -1280,9 +1281,7 @@ is_aggr_type (tree type, int or_else)
   if (type == error_mark_node)
     return 0;
 
-  if (! IS_AGGR_TYPE (type)
-      && TREE_CODE (type) != TEMPLATE_TYPE_PARM
-      && TREE_CODE (type) != BOUND_TEMPLATE_TEMPLATE_PARM)
+  if (! IS_AGGR_TYPE (type))
     {
       if (or_else)
        error ("%qT is not an aggregate type", type);
@@ -1755,7 +1754,7 @@ build_new_1 (tree placement, tree type, tree nelts, tree init,
     placement_expr = NULL_TREE;
   else
     {
-      placement_expr = save_expr (TREE_VALUE (placement));
+      placement_expr = get_target_expr (TREE_VALUE (placement));
       placement = tree_cons (NULL_TREE, placement_expr, NULL_TREE);
     }
 
@@ -1787,6 +1786,11 @@ build_new_1 (tree placement, tree type, tree nelts, tree init,
                    (alloc_fn,
                     build_tree_list (NULL_TREE, class_addr)));
     }
+  else if (TYPE_FOR_JAVA (elt_type) && IS_AGGR_TYPE (elt_type))
+    {
+      error ("Java class %q#T object allocated using placement new", elt_type);
+      return error_mark_node;
+    }
   else
     {
       tree fnname;
@@ -1931,8 +1935,10 @@ build_new_1 (tree placement, tree type, tree nelts, tree init,
       if (targetm.cxx.cookie_has_size ())
        {
          /* Also store the element size.  */
-         cookie_ptr = build2 (MINUS_EXPR, build_pointer_type (sizetype),
-                              cookie_ptr, size_in_bytes (sizetype));
+         cookie_ptr = build2 (POINTER_PLUS_EXPR, size_ptr_type, cookie_ptr,
+                              fold_build1 (NEGATE_EXPR, sizetype,
+                                           size_in_bytes (sizetype)));
+
          cookie = build_indirect_ref (cookie_ptr, NULL);
          cookie = build2 (MODIFY_EXPR, sizetype, cookie,
                           size_in_bytes(elt_type));
@@ -2172,21 +2178,6 @@ build_new (tree placement, tree type, tree nelts, tree init,
       if (!build_expr_type_conversion (WANT_INT | WANT_ENUM, nelts, false))
        pedwarn ("size in array new must have integral type");
       nelts = cp_save_expr (cp_convert (sizetype, nelts));
-      /* It is valid to allocate a zero-element array:
-
-          [expr.new]
-
-          When the value of the expression in a direct-new-declarator
-          is zero, the allocation function is called to allocate an
-          array with no elements.  The pointer returned by the
-          new-expression is non-null.  [Note: If the library allocation
-          function is called, the pointer returned is distinct from the
-          pointer to any other object.]
-
-        However, that is not generally useful, so we issue a
-        warning.  */
-      if (integer_zerop (nelts))
-       warning (0, "allocating zero-element array");
     }
 
   /* ``A reference cannot be created by the new operator.  A reference
@@ -2867,6 +2858,7 @@ build_delete (tree type, tree addr, special_function_kind auto_delete,
     }
   else
     {
+      tree head = NULL_TREE;
       tree do_delete = NULL_TREE;
       tree ifexp;
 
@@ -2880,8 +2872,9 @@ build_delete (tree type, tree addr, special_function_kind auto_delete,
        {
          /* We will use ADDR multiple times so we must save it.  */
          addr = save_expr (addr);
+         head = get_target_expr (build_headof (addr));
          /* Delete the object.  */
-         do_delete = build_builtin_delete_call (addr);
+         do_delete = build_builtin_delete_call (head);
          /* Otherwise, treat this like a complete object destructor
             call.  */
          auto_delete = sfk_complete_destructor;
@@ -2920,6 +2913,10 @@ build_delete (tree type, tree addr, special_function_kind auto_delete,
       if (do_delete)
        expr = build2 (COMPOUND_EXPR, void_type_node, expr, do_delete);
 
+      /* We need to calculate this before the dtor changes the vptr.  */
+      if (head)
+       expr = build2 (COMPOUND_EXPR, void_type_node, head, expr);
+
       if (flags & LOOKUP_DESTRUCTOR)
        /* Explicit destructor call; don't check for null pointer.  */
        ifexp = integer_one_node;