OSDN Git Service

1999-11-25 Mark Mitchell <mark@codesourcery.com>
[pf3gnuchains/gcc-fork.git] / gcc / cp / cvt.c
index 388c7b8..d07c1d2 100644 (file)
@@ -339,11 +339,13 @@ build_up_reference (type, arg, flags)
   tree rval;
   tree argtype = TREE_TYPE (arg);
   tree target_type = TREE_TYPE (type);
+  tree stmt_expr = NULL_TREE;
 
   my_friendly_assert (TREE_CODE (type) == REFERENCE_TYPE, 187);
 
   if ((flags & DIRECT_BIND) && ! real_lvalue_p (arg))
     {
+      /* Create a new temporary variable.  */
       tree targ = arg;
       if (toplevel_bindings_p ())
        arg = get_temp_name (argtype, 1);
@@ -352,17 +354,14 @@ build_up_reference (type, arg, flags)
          arg = pushdecl (build_decl (VAR_DECL, NULL_TREE, argtype));
          DECL_ARTIFICIAL (arg) = 1;
        }
+
+      /* Process the initializer for the declaration.  */
       DECL_INITIAL (arg) = targ;
-      cp_finish_decl (arg, targ, NULL_TREE, 0,
+      cp_finish_decl (arg, targ, NULL_TREE, 
                      LOOKUP_ONLYCONVERTING|DIRECT_BIND);
     }
   else if (!(flags & DIRECT_BIND) && ! lvalue_p (arg))
-    {
-      tree slot = build_decl (VAR_DECL, NULL_TREE, argtype);
-      DECL_ARTIFICIAL (slot) = 1;
-      arg = build (TARGET_EXPR, argtype, slot, arg, NULL_TREE, NULL_TREE);
-      TREE_SIDE_EFFECTS (arg) = 1;
-    }
+    return get_target_expr (arg);
 
   /* If we had a way to wrap this up, and say, if we ever needed it's
      address, transform all occurrences of the register, into a memory
@@ -389,6 +388,13 @@ build_up_reference (type, arg, flags)
       = convert_to_pointer_force (build_pointer_type (target_type), rval);
   rval = build1 (NOP_EXPR, type, rval);
   TREE_CONSTANT (rval) = TREE_CONSTANT (TREE_OPERAND (rval, 0));
+
+  /* If we created and initialized a new temporary variable, add the
+     representation of that initialization to the RVAL.  */
+  if (stmt_expr)
+    rval = build (COMPOUND_EXPR, TREE_TYPE (rval), stmt_expr, rval);
+
+  /* And return the result.  */
   return rval;
 }
 
@@ -664,6 +670,9 @@ ocp_convert (type, expr, convtype, flags)
       || TREE_TYPE (e) == error_mark_node)
     return error_mark_node;
 
+  complete_type (type);
+  complete_type (TREE_TYPE (expr));
+
   if (TREE_READONLY_DECL_P (e))
     e = decl_constant_value (e);
 
@@ -689,20 +698,10 @@ ocp_convert (type, expr, convtype, flags)
 
   if (code == VOID_TYPE && (convtype & CONV_STATIC))
     {
-      e = require_complete_type_in_void (e);
-      if (e != error_mark_node)
-        e = build1 (CONVERT_EXPR, void_type_node, e);
-
+      e = convert_to_void (e, /*implicit=*/NULL);
       return e;
     }
 
-#if 0
-  /* This is incorrect.  A truncation can't be stripped this way.
-     Extensions will be stripped by the use of get_unwidened.  */
-  if (TREE_CODE (e) == NOP_EXPR)
-    return cp_convert (type, TREE_OPERAND (e, 0));
-#endif
-
   /* Just convert to the type of the member.  */
   if (code == OFFSET_TYPE)
     {
@@ -710,13 +709,6 @@ ocp_convert (type, expr, convtype, flags)
       code = TREE_CODE (type);
     }
 
-#if 0
-  if (code == REFERENCE_TYPE)
-    return fold (convert_to_reference (type, e, convtype, flags, NULL_TREE));
-  else if (TREE_CODE (TREE_TYPE (e)) == REFERENCE_TYPE)
-    e = convert_from_reference (e);
-#endif
-
   if (TREE_CODE (e) == OFFSET_REF)
     e = resolve_offset_ref (e);
 
@@ -748,10 +740,17 @@ ocp_convert (type, expr, convtype, flags)
        }
       if (code == BOOLEAN_TYPE)
        {
+         tree fn = NULL_TREE;
+
          /* Common Ada/Pascal programmer's mistake.  We always warn
              about this since it is so bad.  */
          if (TREE_CODE (expr) == FUNCTION_DECL)
-           cp_warning ("the address of `%D', will always be `true'", expr);
+           fn = expr;
+         else if (TREE_CODE (expr) == ADDR_EXPR 
+                  && TREE_CODE (TREE_OPERAND (expr, 0)) == FUNCTION_DECL)
+           fn = TREE_OPERAND (expr, 0);
+         if (fn)
+           cp_warning ("the address of `%D', will always be `true'", fn);
          return truthvalue_conversion (e);
        }
       return fold (convert_to_integer (type, e));
@@ -830,6 +829,140 @@ ocp_convert (type, expr, convtype, flags)
   return error_mark_node;
 }
 
+/* When an expression is used in a void context, its value is discarded and
+   no lvalue-rvalue and similar conversions happen [expr.static.cast/4,
+   stmt.expr/1, expr.comma/1].  This permits dereferencing an incomplete type
+   in a void context. The C++ standard does not define what an `access' to an
+   object is, but there is reason to beleive that it is the lvalue to rvalue
+   conversion -- if it were not, `*&*p = 1' would violate [expr]/4 in that it
+   accesses `*p' not to calculate the value to be stored. But, dcl.type.cv/8
+   indicates that volatile semantics should be the same between C and C++
+   where ever possible. C leaves it implementation defined as to what
+   constitutes an access to a volatile. So, we interpret `*vp' as a read of
+   the volatile object `vp' points to, unless that is an incomplete type. For
+   volatile references we do not do this interpretation, because that would
+   make it impossible to ignore the reference return value from functions. We
+   issue warnings in the confusing cases.
+   
+   IMPLICIT is tells us the context of an implicit void conversion.  */
+
+tree
+convert_to_void (expr, implicit)
+     tree expr;
+     const char *implicit;
+{
+  if (expr == error_mark_node)
+    return expr;
+  if (!TREE_TYPE (expr))
+    return expr;
+  if (same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (expr)), void_type_node))
+    return expr;
+  switch (TREE_CODE (expr))
+    {
+    case COND_EXPR:
+      {
+        /* The two parts of a cond expr might be separate lvalues.  */
+        tree op1 = TREE_OPERAND (expr,1);
+        tree op2 = TREE_OPERAND (expr,2);
+        tree new_op1 = convert_to_void (op1, implicit);
+        tree new_op2 = convert_to_void (op2, implicit);
+        
+        if (new_op1 != op1 || new_op2 != op2)
+          expr = build (COND_EXPR,
+                        implicit ? TREE_TYPE (expr) : void_type_node,
+                        TREE_OPERAND (expr, 0), new_op1, new_op2);
+        break;
+      }
+    
+    case COMPOUND_EXPR:
+      {
+        /* The second part of a compound expr contains the value.  */
+        tree op1 = TREE_OPERAND (expr,1);
+        tree new_op1 = convert_to_void (op1, implicit);
+        
+        if (new_op1 != op1)
+          expr = build (COMPOUND_EXPR, TREE_TYPE (new_op1),
+                        TREE_OPERAND (expr, 0), new_op1);
+        break;
+      }
+    
+    case NON_LVALUE_EXPR:
+    case NOP_EXPR:
+      /* These have already decayed to rvalue. */
+      break;
+    
+    case CALL_EXPR:   /* we have a special meaning for volatile void fn() */
+      break;
+    
+    case INDIRECT_REF:
+      {
+        tree type = TREE_TYPE (expr);
+        int is_reference = TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0)))
+                           == REFERENCE_TYPE;
+        int is_volatile = TYPE_VOLATILE (type);
+        int is_complete = TYPE_SIZE (complete_type (type)) != NULL_TREE;
+        
+        if (is_volatile && !is_complete)
+          cp_warning ("object of incomplete type `%T' will not be accessed in %s",
+                      type, implicit ? implicit : "void context");
+        else if (is_reference && is_volatile)
+          cp_warning ("object of type `%T' will not be accessed in %s",
+                      TREE_TYPE (TREE_OPERAND (expr, 0)),
+                      implicit ? implicit : "void context");
+        if (is_reference || !is_volatile || !is_complete)
+          expr = TREE_OPERAND (expr, 0);
+      
+        break;
+      }
+    
+    case VAR_DECL:
+      {
+        /* External variables might be incomplete.  */
+        tree type = TREE_TYPE (expr);
+        int is_complete = TYPE_SIZE (complete_type (type)) != NULL_TREE;
+        
+        if (TYPE_VOLATILE (type) && !is_complete)
+          cp_warning ("object `%E' of incomplete type `%T' will not be accessed in %s",
+                      expr, type, implicit ? implicit : "void context");
+        break;
+      }
+    
+    default:;
+    }
+  {
+    tree probe = expr;
+  
+    if (TREE_CODE (probe) == ADDR_EXPR)
+      probe = TREE_OPERAND (expr, 0);
+    if (!is_overloaded_fn (probe))
+      ;/* OK */
+    else if (really_overloaded_fn (probe))
+        {
+          /* [over.over] enumerates the places where we can take the address
+             of an overloaded function, and this is not one of them.  */
+          cp_pedwarn ("%s has no context for overloaded function name `%E'",
+                      implicit ? implicit : "void cast", expr);
+        }
+    else if (implicit && probe == expr)
+      /* Only warn when there is no &.  */
+      cp_warning ("%s is a reference, not call, to function `%E'",
+                    implicit, expr);
+  }
+  
+  if (expr != error_mark_node
+      && !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (expr)), void_type_node))
+    {
+      /* FIXME: This is where we should check for expressions with no
+         effects.  At the moment we do that in both build_x_component_expr
+         and expand_expr_stmt -- inconsistently too.  For the moment
+         leave implicit void conversions unadorned so that expand_expr_stmt
+         has a chance of detecting some of the cases.  */
+      if (!implicit)
+        expr = build1 (CONVERT_EXPR, void_type_node, expr);
+    }
+  return expr;
+}
+
 /* Create an expression whose value is that of EXPR,
    converted to type TYPE.  The TREE_TYPE of the value
    is always TYPE.  This function implements all reasonable