Skip to content

Java: Add CharacterLiteral.getCodePointValue() #6614

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

Merged

Conversation

Marcono1234
Copy link
Contributor

@Marcono1234 Marcono1234 commented Sep 4, 2021

Resolves #3635

Adds CharacterLiteral.getCodePointValue() and adjusts CompileTimeConstantExpr.getIntValue() to work for CharacterLiteral.

Tests for CompileTimeConstantExpr have not been extended, hopefully the existing test code suffices.

@github-actions github-actions bot added the Java label Sep 4, 2021
@Marcono1234 Marcono1234 changed the title Marcono1234/char literal codepoint Java: Add CharacterLiteral.getCodePointValue() Sep 4, 2021
not this instanceof CharacterLiteral and
result = lit.toInt()
)
or
result = this.(CharacterLiteral).getCodePointValue()
Copy link
Contributor

Choose a reason for hiding this comment

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

We probably don't want to surprise users by exposing this change through an existing API

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 am not sure if this is really a surprise. If you perform an arithmetic operation with a char literal in Java then it actually uses its code point, e.g. 'a' + 1 is 98.
If a new predicate was added for this, then a lot of the logic for the arithmetic operations of CompileTimeConstantExpr would have to be duplicated.

Additionally getIntValue() already covers a cast to char, so there is not a big difference then.

* Gets a string which consists of the single character represented by
* this literal.
*/
string getStringValue() { result = getValue() }
Copy link
Contributor

Choose a reason for hiding this comment

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

Why add this alias?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Mostly because getValue() just happens to represent the same result (but not necessarily on purpose) and this separate predicate makes the intention and behavior clearer.
Though maybe it indeed does not add much value, should I remove it?

However, should it then (in a separate issue?) also be discussed if StringLiteral.getRepresentedString() is really needed? Because there getValue() would be even more obvious to use.

Copy link
Contributor

Choose a reason for hiding this comment

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

We should probably just get rid of getRepresentedString through a proper deprecation cycle. The reason for its existence dates back to before we had proper extraction of literal values, when this would simply remove the leading and trailing " chars in a naive attempt to calculate the value from the literal in QL.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Have removed getStringValue() and instead added an override for getValue() with that QLDoc.

Copy link
Contributor Author

@Marcono1234 Marcono1234 Oct 29, 2021

Choose a reason for hiding this comment

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

Have created #7004 for deprecating getRepresentedString().

The changed code previously also only covered IntegerLiteral:
- Restricted to Literal
- Integral type
- != "long"
- != "char"

So the only class left which matches all of these is IntegerLiteral.
@Marcono1234 Marcono1234 force-pushed the marcono1234/char-literal-codepoint branch from b104332 to 4f59886 Compare October 29, 2021 12:31
@Marcono1234 Marcono1234 marked this pull request as ready for review October 29, 2021 12:32
@Marcono1234 Marcono1234 requested a review from a team as a code owner October 29, 2021 12:32
aschackmull
aschackmull previously approved these changes Nov 1, 2021
@aschackmull aschackmull merged commit 64acd02 into github:main Nov 1, 2021
@Marcono1234 Marcono1234 deleted the marcono1234/char-literal-codepoint branch November 1, 2021 14:56
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.

Java: Add CharacterLiteral.getIntValue
3 participants