Commit 8422942c by Richard Henderson Committed by Richard Henderson

typeck.c (c_sizeof): Be strict about casting result value back to c_size_type_node.

        * typeck.c (c_sizeof): Be strict about casting result value
        back to c_size_type_node.
        (expr_sizeof, c_sizeof_nowarn, c_alignof): Likewise.

From-SVN: r37446
parent 187e3bf0
2000-11-14 Richard Henderson <rth@redhat.com>
* typeck.c (c_sizeof): Be strict about casting result value
back to c_size_type_node.
(expr_sizeof, c_sizeof_nowarn, c_alignof): Likewise.
2000-11-13 Joseph S. Myers <jsm28@cam.ac.uk> 2000-11-13 Joseph S. Myers <jsm28@cam.ac.uk>
* typeck.c (build_unary_op): Use boolean_increment from * typeck.c (build_unary_op): Use boolean_increment from
......
...@@ -1557,44 +1557,46 @@ c_sizeof (type) ...@@ -1557,44 +1557,46 @@ c_sizeof (type)
{ {
if (pedantic || warn_pointer_arith) if (pedantic || warn_pointer_arith)
pedwarn ("ISO C++ forbids applying `sizeof' to a function type"); pedwarn ("ISO C++ forbids applying `sizeof' to a function type");
return size_one_node; size = size_one_node;
} }
if (code == METHOD_TYPE) else if (code == METHOD_TYPE)
{ {
if (pedantic || warn_pointer_arith) if (pedantic || warn_pointer_arith)
pedwarn ("ISO C++ forbids applying `sizeof' to a member function"); pedwarn ("ISO C++ forbids applying `sizeof' to a member function");
return size_one_node; size = size_one_node;
} }
if (code == VOID_TYPE) else if (code == VOID_TYPE)
{ {
if (pedantic || warn_pointer_arith) if (pedantic || warn_pointer_arith)
pedwarn ("ISO C++ forbids applying `sizeof' to type `void' which is an incomplete type"); pedwarn ("ISO C++ forbids applying `sizeof' to type `void' which is an incomplete type");
return size_one_node; size = size_one_node;
} }
if (code == ERROR_MARK) else if (code == ERROR_MARK)
return size_one_node; size = size_one_node;
else
/* ARM $5.3.2: ``When applied to a reference, the result is the size of the
referenced object.'' */
if (code == REFERENCE_TYPE)
type = TREE_TYPE (type);
if (code == OFFSET_TYPE)
{ {
cp_error ("`sizeof' applied to non-static member"); /* ARM $5.3.2: ``When applied to a reference, the result is the
return size_zero_node; size of the referenced object.'' */
} if (code == REFERENCE_TYPE)
type = TREE_TYPE (type);
if (!COMPLETE_TYPE_P (complete_type (type))) if (code == OFFSET_TYPE)
{ {
cp_error ("`sizeof' applied to incomplete type `%T'", type); cp_error ("`sizeof' applied to non-static member");
return size_zero_node; size = size_zero_node;
}
else if (!COMPLETE_TYPE_P (complete_type (type)))
{
cp_error ("`sizeof' applied to incomplete type `%T'", type);
size = size_zero_node;
}
else
/* Convert in case a char is more than one unit. */
size = size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
size_int (TYPE_PRECISION (char_type_node)
/ BITS_PER_UNIT));
} }
/* Convert in case a char is more than one unit. */
size = size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
size_int (TYPE_PRECISION (char_type_node)
/ BITS_PER_UNIT));
/* SIZE will have an integer type with TYPE_IS_SIZETYPE set. /* SIZE will have an integer type with TYPE_IS_SIZETYPE set.
TYPE_IS_SIZETYPE means that certain things (like overflow) will TYPE_IS_SIZETYPE means that certain things (like overflow) will
never happen. However, this node should really have type never happen. However, this node should really have type
...@@ -1619,12 +1621,12 @@ expr_sizeof (e) ...@@ -1619,12 +1621,12 @@ expr_sizeof (e)
if (is_overloaded_fn (e)) if (is_overloaded_fn (e))
{ {
pedwarn ("ISO C++ forbids applying `sizeof' to an expression of function type"); pedwarn ("ISO C++ forbids applying `sizeof' to an expression of function type");
return size_one_node; e = char_type_node;
} }
else if (type_unknown_p (e)) else if (type_unknown_p (e))
{ {
incomplete_type_error (e, TREE_TYPE (e)); incomplete_type_error (e, TREE_TYPE (e));
return size_one_node; e = char_type_node;
} }
/* It's illegal to say `sizeof (X::i)' for `i' a non-static data /* It's illegal to say `sizeof (X::i)' for `i' a non-static data
member unless you're in a non-static member of X. So hand off to member unless you're in a non-static member of X. So hand off to
...@@ -1643,23 +1645,35 @@ c_sizeof_nowarn (type) ...@@ -1643,23 +1645,35 @@ c_sizeof_nowarn (type)
tree type; tree type;
{ {
enum tree_code code = TREE_CODE (type); enum tree_code code = TREE_CODE (type);
tree size;
if (code == FUNCTION_TYPE if (code == FUNCTION_TYPE
|| code == METHOD_TYPE || code == METHOD_TYPE
|| code == VOID_TYPE || code == VOID_TYPE
|| code == ERROR_MARK) || code == ERROR_MARK)
return size_one_node; size = size_one_node;
else
if (code == REFERENCE_TYPE) {
type = TREE_TYPE (type); if (code == REFERENCE_TYPE)
type = TREE_TYPE (type);
if (!COMPLETE_TYPE_P (type)) if (!COMPLETE_TYPE_P (type))
return size_zero_node; size = size_zero_node;
else
/* Convert in case a char is more than one unit. */
size = size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
size_int (TYPE_PRECISION (char_type_node)
/ BITS_PER_UNIT));
}
/* Convert in case a char is more than one unit. */ /* SIZE will have an integer type with TYPE_IS_SIZETYPE set.
return size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type), TYPE_IS_SIZETYPE means that certain things (like overflow) will
size_int (TYPE_PRECISION (char_type_node) never happen. However, this node should really have type
/ BITS_PER_UNIT)); `size_t', which is just a typedef for an ordinary integer type. */
size = fold (build1 (NOP_EXPR, c_size_type_node, size));
my_friendly_assert (!TYPE_IS_SIZETYPE (TREE_TYPE (size)),
20001021);
return size;
} }
/* Implement the __alignof keyword: Return the minimum required /* Implement the __alignof keyword: Return the minimum required
...@@ -1676,18 +1690,19 @@ c_alignof (type) ...@@ -1676,18 +1690,19 @@ c_alignof (type)
return build_min (ALIGNOF_EXPR, sizetype, type); return build_min (ALIGNOF_EXPR, sizetype, type);
if (code == FUNCTION_TYPE || code == METHOD_TYPE) if (code == FUNCTION_TYPE || code == METHOD_TYPE)
return size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT); t = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
else if (code == VOID_TYPE || code == ERROR_MARK)
if (code == VOID_TYPE || code == ERROR_MARK) t = size_one_node;
return size_one_node; else
{
/* Similar to sizeof, __alignof applies to the referant. */
if (code == REFERENCE_TYPE)
type = TREE_TYPE (type);
/* C++: this is really correct! */ t = size_int (TYPE_ALIGN (type) / BITS_PER_UNIT);
if (code == REFERENCE_TYPE) }
type = TREE_TYPE (type);
t = size_int (TYPE_ALIGN (type) / BITS_PER_UNIT); return fold (build1 (NOP_EXPR, c_size_type_node, t));
force_fit_type (t, 0);
return t;
} }
/* Perform the array-to-pointer and function-to-pointer conversions /* Perform the array-to-pointer and function-to-pointer conversions
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment