Opened 7 years ago

Last modified 7 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:


In parser.yy there is the following:

    | 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.

Change History (1)

comment:1 Changed 7 years ago by ajbeach

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.

Note: See TracTickets for help on using tickets.