// A ternary with matching true- and false-cases does not need to branch. if (Analysis::IsSameExpressionTree(*ifTrueExpr, *ifFalseExpr)) { // If `test` has no side-effects, we can eliminate it too, and just return `ifTrue`. if (!Analysis::HasSideEffects(*test)) {
ifTrue->fPosition = pos; return ifTrue;
} // Return a comma-expression containing `(test, ifTrue)`. return BinaryExpression::Make(context, pos, std::move(test), Operator::Kind::COMMA, std::move(ifTrue));
}
// A ternary of the form `test ? expr : false` can be simplified to `test && expr`. if (ifFalseExpr->isBoolLiteral() && !ifFalseExpr->as<Literal>().boolValue()) { return BinaryExpression::Make(context, pos, std::move(test), Operator::Kind::LOGICALAND, std::move(ifTrue));
}
// A ternary of the form `test ? true : expr` can be simplified to `test || expr`. if (ifTrueExpr->isBoolLiteral() && ifTrueExpr->as<Literal>().boolValue()) { return BinaryExpression::Make(context, pos, std::move(test), Operator::Kind::LOGICALOR, std::move(ifFalse));
}
// A ternary of the form `test ? false : true` can be simplified to `!test`. if (ifTrueExpr->isBoolLiteral() && !ifTrueExpr->as<Literal>().boolValue() &&
ifFalseExpr->isBoolLiteral() && ifFalseExpr->as<Literal>().boolValue()) { return PrefixExpression::Make(context, pos, Operator::Kind::LOGICALNOT,
std::move(test));
}
// A ternary of the form `test ? 1 : 0` can be simplified to `cast(test)`. if (ifTrueExpr->is<Literal>() && ifTrueExpr->as<Literal>().value() == 1.0 &&
ifFalseExpr->is<Literal>() && ifFalseExpr->as<Literal>().value() == 0.0) { return ConstructorScalarCast::Make(context, pos, ifTrue->type(), std::move(test));
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.