Essential Type of an Expression Clarification

Moderators: misra-c, david ward

gs
Posts: 87
Joined: Thu Nov 18, 2004 1:39 am

Essential Type of an Expression Clarification

Suppose I have:

Code: Select all

``((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?

misra-c
Posts: 572
Joined: Thu Jan 05, 2006 1:11 pm

Re: Essential Type of an Expression Clarification

Appendix D.7 for "+" states in paragraph 2.1 that
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
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: Select all

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

Code: Select all

``(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: Select all

``(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
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: Select all

``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.
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: Select all

``(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: Select all

``(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: Select all

``(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: Select all

``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: Select all

``(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
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: Select all

``(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