21-04-2005, 03:44 PM
1. Explicit casts can change the underlying type of their enclosing expression. For example using the types and names in 10.2:
is compliant. Without the explicit cast the + expression would have an underlying type of int16_t, but with the explicit cast the underlying type is int32_t. The underlying type of s16b is not altered, but the underlying type of (int32_t)s16b is int32_t.
2. It is correct that the guidelines is not concerned with an expression’s underlying type if any of its sub-expressions violate an underlying type-related rule. However the resultant underlying type can be deduced by extending the usual arithmetic conversion rules, as described in section 6.2.1.5 of ISO 9899:1990, to underlying types.
3. In the example for question 1 there is an implicit conversion on s16a from int16_t to int32_t. The underlying type of s16a it-self is not altered, but the underlying type of the left hand expression to + after the implicit cast is int32_t.
4. It is correct that effectively Boolean expressions have underlying type of int. There was discussion, whilst the guidelines were being drawn up, as to whether there should be an underlying type of Boolean, but it was felt that other rules sufficiently restricted the use of Boolean expressions
5. The MISRA C guidelines treat objects of type enum as having an underlying type of int. Enumeration constants follow the same rules as other integer constants. In other words their underlying type is a function of their magnitude. See 6.10.4 in the MISRA C guidelines.
There was much discussion whilst the guidelines was being written as to whether enumerated types should be forced to be strongly typed. It was felt that in an ideal world no integral operations would be allowed on enumerated constants. However it was decided in the end not to add such rules to MISRA-C:2004.
Code:
s32a = s16a + (int32_t)s16b;
2. It is correct that the guidelines is not concerned with an expression’s underlying type if any of its sub-expressions violate an underlying type-related rule. However the resultant underlying type can be deduced by extending the usual arithmetic conversion rules, as described in section 6.2.1.5 of ISO 9899:1990, to underlying types.
3. In the example for question 1 there is an implicit conversion on s16a from int16_t to int32_t. The underlying type of s16a it-self is not altered, but the underlying type of the left hand expression to + after the implicit cast is int32_t.
4. It is correct that effectively Boolean expressions have underlying type of int. There was discussion, whilst the guidelines were being drawn up, as to whether there should be an underlying type of Boolean, but it was felt that other rules sufficiently restricted the use of Boolean expressions
5. The MISRA C guidelines treat objects of type enum as having an underlying type of int. Enumeration constants follow the same rules as other integer constants. In other words their underlying type is a function of their magnitude. See 6.10.4 in the MISRA C guidelines.
There was much discussion whilst the guidelines was being written as to whether enumerated types should be forced to be strongly typed. It was felt that in an ideal world no integral operations would be allowed on enumerated constants. However it was decided in the end not to add such rules to MISRA-C:2004.