Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Luro02 opened this issue Jan 30, 2025 · 0 comments
Open

Comments

@Luro02
Copy link
Collaborator

Luro02 commented Jan 30, 2025

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant