Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Essential Type of an Expression Clarification
#1
Suppose I have:
Code:
((unsigned short) 1u) + ((unsigned short) 2u);
The essential type of each constant, '1u' and '2u', is unsigned char and the casts make the essential type of each operand unsigned short. Now, if shorts are 16 bits wide and chars are 8 bits wide, what is the essential type of the expression; unsigned char because of the value, '3u', or unsigned short because of the operands?
#2
Appendix D.7 for "+" states in paragraph 2.1 that
Quote:if both operands are essentially unsigned, then if the expression is an integer constant expression then the essential type of the result is the UTLR of the result.
Casts from arithmetic types to integer types are permitted in integer constant expressions and so the expression satisfies the conditions of 2.1. Therefore the essential type of the result will be "unsigned char".

The MISRA Essential Types model produces an internally consistent view of C types, but a side-effect of keeping the model simple has been that a few integer constant expressions (where integer promotions are involved) may produce results that at first sight are not obvious.

For those interested in the detailed calculation of the Essential Type for an expression, there are two basic situations that are useful to remember.
  • 1. Is it an operator whose operands might undergo an "integer promotion" to unsigned int or signed int.
  • 2. Would the operation have taken place in unsigned int or signed int?

This second situation is particularly important since Appendix D.1 states that
Quote: The essential type of an expression only differs from the standard C type in expressions where the standard type is either signed int or unsigned int.

The following examples aim to clarify the application of the essential type rules to expressions containing either casts or literals with suffices.

unsigned int u1, u2; // assuming 16 bit short and 32 bit int

Code:
5U;   : Essential Type (ET) = unsigned char
See Appendix D.6

Code:
(unsigned short)5U;   : ET = unsigned short
The ET which results from a cast is the same as the "standard type"(ST), which in this case is unsigned short.
The standard type is "unsigned short", and so according to Appendix D.1, the essential type is the same as the standard type. (Appendix D.7 is not applied)

Code:
(unsigned int)5U;   : ET = unsigned int
The standard type is "unsigned int", which means according to Appendix D.1 that the other sections in Appendix D need to be considered. Appendix D.7 states
Quote:The essential type of any expression not listed in this section is the same as its standard type
Casts are not mentioned in Appendix D.7, and so the essential type is the same as the standard type.

Code:
2U + 3U;  : ET = unsigned char
The standard type for the result of the "+" operation is "unsigned int", which means Appendix D.7 on "+" is applicable.
The ET is determined by Appendix D.7 on "+" 2.1.
Quote: If the expression is an integer constant expression then the essential type of the result is the UTLR of the result.
The unsigned type of lowest rank (UTLR) for the value 5 is unsigned char.

Code:
(unsigned short)(2U + 3U);  : ET = unsigned short
The ET is the result of a cast and is therefore the same as the standard type.

Code:
(unsigned short)2U + (unsigned short)3U;  : ET = unsigned char
The standard type for the result of the "+" operation is "signed int", which means Appendix D.7 on "+" is applicable.
The ET is determined by Appendix D.7 on "+" 2.1, The UTLR for the value 5 is unsigned char.

Code:
(unsigned long)2U+(unsigned long)3U;  : ET = unsigned long
The standard type is "unsigned long", so the ET is the same as the standard type. (Appendix D.7 is not applied)

Code:
2UL + 3UL;  : ET = unsigned long
The standard type is "unsigned long", so the ET is the same as the standard type. (Appendix D.7 is not applied)

Code:
(unsigned short)u1 + (unsigned short)u2;   : ET = unsigned short
The standard type for the result of the "+" operation is "signed int", which means Appendix D.7 on "+" is applicable.
The ET is determined by Appendix D.7 on "+" 2.2, which states that
Quote:the essential type of the result is the essential type of the operand with the highest rank.
In this case both operands have an ET of unsigned short.

Code:
(unsigned short)50000U + (unsigned short)50000U;  : ET = unsigned int
The standard type for the result of the "+" operation is "signed int", which means Appendix D7 on "+" is applicable.
The ET is determined by Appendix D.7 on "+" 2.1, The UTLR for the value 100,000 is unsigned int.
Posted by and on behalf of the MISRA C Working Group


Forum Jump:


Users browsing this thread: 3 Guest(s)