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

WIP(general feedback appreciated): fix/refactor(control-flow): Switch rework and multi-node statement changes #5742

Open
wants to merge 24 commits into
base: master
Choose a base branch
from

Conversation

Mr-Pine
Copy link
Contributor

@Mr-Pine Mr-Pine commented Apr 6, 2024

In the process of handling switch expressions and enhanced switch statements I ended up rewriting the whole switch code.
Notable Changes to the results:

  • Switch expressions and enhanced switches are now properly supported and exhaustiveness for properly compiled java 21+ code is complete
  • Statements in the cases of a switch are no longer wrapped in separate blocks. Instead, the all expressions are now wrapped in one block and when appropriate a block case in an enhanced switch is of course also wrapped in a separate block.
  • default cases don't have a dummy statement any more.
  • Probably most important: New layout for statements that require multiple nodes:
    The construct starts with a node that is assigned the entire statement, then the subnodes follow and the construct is finished with a STATEMENT_END node that is tagged with the start node (See details below).
    I intend to adapt this to other multi-node constructs as well and some support for this is already implemented, but the rest is probably better placed in another PR. I think this change enables a more complete representation of multi-node constructs, especially of multi-node expressions like conditionals (currently conditionals in returns are not handled at all, for example) or maybe even nested method calls. The design of this was based on the way conditionals are handled when assigned to local variables. I added the end marker mainly as a jump point for skipping the entire statement and for easier support with return statements.

I'd appreciate some feedback for these changes, especially on the last point as it has a pretty big impact

image

for

public int switchTest(int a) {
	int b = 0;
	switch (a) {
		case 1:
			return 0;
		case 2:
			b = a * 2;
			break;
		case 3:
		case 4:
			b = a * 9;
			break;
		default:
			return 1;
	}
	return b;
}

As a side effect, fall throughs now go to the next block, not the next case expression
…anymore and handle return switch () {} expression
They now have a start and an end node. Should be ported to other multinode constructs like if, loops, conditionals, ...
@Mr-Pine Mr-Pine force-pushed the controlflow-switch-expression branch from 54630ae to 3c8e078 Compare April 10, 2024 21:23
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

Successfully merging this pull request may close these issues.

1 participant