diff options
author | Gabriel Dos Reis <gdr@nerim.net> | 2002-07-23 13:54:06 +0000 |
---|---|---|
committer | Gabriel Dos Reis <gdr@gcc.gnu.org> | 2002-07-23 13:54:06 +0000 |
commit | fa72b06489d3674c61ce6156ac72cd12572d5fc0 (patch) | |
tree | 5a3cc29ddefe2d01107bd8557ff47770b29e7bc4 /gcc/c-common.c | |
parent | 2b773ee2c1954b982aac0ee71fd6e5ae148e4691 (diff) | |
download | gcc-fa72b06489d3674c61ce6156ac72cd12572d5fc0.zip gcc-fa72b06489d3674c61ce6156ac72cd12572d5fc0.tar.gz gcc-fa72b06489d3674c61ce6156ac72cd12572d5fc0.tar.bz2 |
Fix PR/7363:
2002-07-21 Gabriel Dos Reis <gdr@nerim.net>
Fix PR/7363:
* c-common.c (c_sizeof_or_alignof_type): New function.
(c_alignof): Remove definition.
* c-common.h (c_sizeof, c_alignof): Define as macros.
(c_sizeof_or_alignof_type): Declare.
(my_friendly_assert): Moved from cp/cp-tree.h
* c-typeck.c (c_sizeof): Remove definition.
cp/
2002-07-21 Gabriel Dos Reis <gdr@nerim.net>
Fix PR/7363:
* typeck.c (cxx_sizeof_or_alignof_type): New function.
(c_sizeof): Remove definition.
(expr_sizeof): Use cxx_sizeof.
* decl2.c (build_expr_from_tree): Use cxx_sizeof_or_alignof_type.
* decl.c (finish_destructor_body): Use cxx_sizeof.
* semantics.c (finish_alignof): Likewise.
(finish_alignof): Use cxx_alignof.
* cp-tree.h (cxx_sizeof, cxx_alignof): New macros.
(cxx_sizeof_or_alignof_type): Declare.
(my_friendly_assert): Move to ../c-common.h.
From-SVN: r55678
Diffstat (limited to 'gcc/c-common.c')
-rw-r--r-- | gcc/c-common.c | 65 |
1 files changed, 45 insertions, 20 deletions
diff --git a/gcc/c-common.c b/gcc/c-common.c index a948959..d7be6c8 100644 --- a/gcc/c-common.c +++ b/gcc/c-common.c @@ -2601,36 +2601,61 @@ c_common_get_alias_set (t) return -1; } -/* Implement the __alignof keyword: Return the minimum required - alignment of TYPE, measured in bytes. */ - +/* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where the + second parameter indicates which OPERATOR is being applied. */ tree -c_alignof (type) +c_sizeof_or_alignof_type (type, op) tree type; + enum tree_code op; { - enum tree_code code = TREE_CODE (type); - tree t; - - /* In C++, sizeof applies to the referent. Handle alignof the same way. */ - if (code == REFERENCE_TYPE) + const char *op_name; + tree value = NULL; + enum tree_code type_code = TREE_CODE (type); + + my_friendly_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR, 20020720); + op_name = op == SIZEOF_EXPR ? "sizeof" : "__alignof__"; + + if (type_code == FUNCTION_TYPE) + { + if (op == SIZEOF_EXPR) + { + if (pedantic || warn_pointer_arith) + pedwarn ("invalid application of `sizeof' to a function type"); + value = size_one_node; + } + else + value = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT); + } + else if (type_code == VOID_TYPE || type_code == ERROR_MARK) { - type = TREE_TYPE (type); - code = TREE_CODE (type); + if (type_code == VOID_TYPE && (pedantic || warn_pointer_arith)) + pedwarn ("invalid application of `%s' to a void type", op_name); + value = size_one_node; } - - if (code == FUNCTION_TYPE) - t = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT); - else if (code == VOID_TYPE || code == ERROR_MARK) - t = size_one_node; else if (!COMPLETE_TYPE_P (type)) { - error ("__alignof__ applied to an incomplete type"); - t = size_zero_node; + error ("invalid application of `%s' to an incomplete type", op_name); + value = size_zero_node; } else - t = size_int (TYPE_ALIGN (type) / BITS_PER_UNIT); + { + if (op == SIZEOF_EXPR) + /* Convert in case a char is more than one unit. */ + value = size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type), + size_int (TYPE_PRECISION (char_type_node) + / BITS_PER_UNIT)); + else + value = size_int (TYPE_ALIGN (type) / BITS_PER_UNIT); + } - return fold (build1 (NOP_EXPR, c_size_type_node, t)); + /* VALUE will have an integer type with TYPE_IS_SIZETYPE set. + TYPE_IS_SIZETYPE means that certain things (like overflow) will + never happen. However, this node should really have type + `size_t', which is just a typedef for an ordinary integer type. */ + value = fold (build1 (NOP_EXPR, c_size_type_node, value)); + my_friendly_assert (!TYPE_IS_SIZETYPE (TREE_TYPE (value)), 20001021); + + return value; } /* Implement the __alignof keyword: Return the minimum required |