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

KE2: extract binary operators #17761

Merged
merged 6 commits into from
Nov 12, 2024
Merged

KE2: extract binary operators #17761

merged 6 commits into from
Nov 12, 2024

Conversation

tamasvajk
Copy link
Contributor

No description provided.

@tamasvajk tamasvajk changed the title WIP: KE2: extract binary ops KE2: extract binary operators Oct 16, 2024
@tamasvajk tamasvajk marked this pull request as ready for review October 16, 2024 12:35
@tamasvajk tamasvajk requested a review from a team as a code owner October 16, 2024 12:35
@@ -297,37 +298,83 @@ private fun KotlinFileExtractor.extractBinaryExpression(
val op = expression.operationToken
val target = expression.resolveCallTarget()?.symbol
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the plan for target == null?

If we're just going to emit an errorExpr, and perhaps add some more info in a supplementary table, then can we handle that case first, and make isNumericWithName and isBinaryPlus take a non-null KaFunctionSymbol and not have to handle the null case?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are cases below where target is null, for example in case of ===.

if (op == KtTokens.PLUS && target.isBinaryPlus()) {
extractBinaryExpression(expression, callable, parent, tw::writeExprs_addexpr)
} else if (op == KtTokens.MINUS && target.isNumericWithName("minus")) {
extractBinaryExpression(expression, callable, parent, tw::writeExprs_subexpr)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So far at least this is very regular; I wonder if it would be worth writing as

    val writeExprs_fun = if (op == KtTokens.PLUS && target.isBinaryPlus()) { tw::writeExprs_addexpr }
                         else if ...
                         else null
    if (writeExprs_fun != null) {
        extractBinaryExpression(expression, callable, parent, writeExprs_fun
    } else {
        something_else
    }

?

And either way, something like

when {
    op == KtTokens.PLUS && target.isBinaryPlus() -> tw::writeExprs_addexpr // or -> extractBinaryExpression(expression, callable, parent, tw::writeExprs_addexpr)
    ...
    else -> null
}

might be nicer than a big if/elseif

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer merging this PR, #17939, and #17874 first. I can do a followup refactoring afterwards.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's the followup refactoring PR: #17974

KtTokens.GT -> extractBinaryExpression(expression, callable, parent, tw::writeExprs_gtexpr)
KtTokens.LTEQ -> extractBinaryExpression(expression, callable, parent, tw::writeExprs_leexpr)
KtTokens.GTEQ -> extractBinaryExpression(expression, callable, parent, tw::writeExprs_geexpr)
else -> TODO("error")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we're changing this to just return tw::writeExprs_ltexpr as above, then we could drop the op in listOf check and just return null in the else case.

}
} else {
TODO("Extract lowered equivalent call, such as `a.compareTo(b) < 0`")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, but my previous suggestion won't work if we need to do something special in this case.
I think we should add a comment explaining what's going on with the AST in that case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need the comment. Instead we should merge this too: #17939

this.hasMatchingNames(
CallableId(FqName("kotlin"), null, Name.identifier("plus")),
ClassId(FqName("kotlin"), Name.identifier("String")),
nullability = KaTypeNullability.NULLABLE,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be worth adding a comment saying what this last case corresponds to

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can dig that up, but this bit was just refactored. It was originally introduced in #17752

Copy link
Contributor Author

@tamasvajk tamasvajk Nov 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment added in 53460d7.

@tamasvajk tamasvajk merged commit a9e45d8 into github:ke2 Nov 12, 2024
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants