Skip to content

Provide some code to deal with operators that can be swapped like a + b + c #685

@Luro02

Description

@Luro02

Description

There are some issues like

that would benefit from this. The core problem is that (a + b) + c and c + b + a are represented differently in the model, even though they are effectively the same.

There are some cases where this is not true, like for a != null && a.getValue() == 5 && b == 3, where the a != null must be before the other uses, because of short-circuiting and potential side-effects.

The idea is to introduce a class like this

record BinaryOperatorView(List<CtExpression<?>> operands, BinaryOperatorKind operator) {
    // TODO: Maybe save the original expression internally in the class?
}

That can then be used to group operands, when they match certain criteria and join the remainder.

The class would be constructed from a given CtBinaryOperator. It should ideally support translating a - b + c to a + (-b) + c, which can then be optimized if necessary.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions