Opened 8 years ago
Last modified 8 years ago
#34 new defect
Incorrect Support for Two Argument ?: Operator
| Reported by: | ajbeach | Owned by: | |
|---|---|---|---|
| Priority: | minor | Component: | cfa-cc |
| Version: | 1.0 | Keywords: | |
| Cc: |
Description
In parser.yy there is the following:
conditional_expression:
logical_OR_expression
| logical_OR_expression '?' comma_expression ':' conditional_expression
{ $$ = new ExpressionNode( build_cond( $1, $3, $5 ) ); }
// FIX ME: this hack computes $1 twice
| logical_OR_expression '?' /* empty */ ':' conditional_expression // GCC, $
{ $$ = new ExpressionNode( build_cond( $1, $1, $4 ) ); }
;
The two argument conditional operator should evaluate the first argument, the condition, once.
Note:
See TracTickets
for help on using tickets.
Some notes I have collected on a possible solution:
To fix this the resolver should type match conditional expression nodes so that they take in two expressions of type U and have a result of the same type, where type U has a 'to bool' conversion defined. Then on code generation we should be able to create the following:
({ U anon = $1; anon!=0 ? anon : $4; })That is, use the statement expression to store a temporary value, then do the cast to boolean. You could expand this earlier into a set of nodes, but I think doing it right a code generation is simpler in this case.