Skip to content

Commit

Permalink
[J2KT] Rename Source.inRoundBrackets() to Source.inParentheses().
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 564675707
  • Loading branch information
Googler authored and copybara-github committed Sep 12, 2023
1 parent 0165b85 commit c3daafa
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ import com.google.j2cl.transpiler.backend.kotlin.source.ifNotEmpty
import com.google.j2cl.transpiler.backend.kotlin.source.ifNotNullSource
import com.google.j2cl.transpiler.backend.kotlin.source.inAngleBrackets
import com.google.j2cl.transpiler.backend.kotlin.source.inInlineCurlyBrackets
import com.google.j2cl.transpiler.backend.kotlin.source.inRoundBrackets
import com.google.j2cl.transpiler.backend.kotlin.source.inParentheses
import com.google.j2cl.transpiler.backend.kotlin.source.inSquareBrackets
import com.google.j2cl.transpiler.backend.kotlin.source.infix
import com.google.j2cl.transpiler.backend.kotlin.source.join
Expand Down Expand Up @@ -147,7 +147,7 @@ private fun Renderer.arrayLiteralSource(arrayLiteral: ArrayLiteral): Source =
typeArgumentsSource(listOf(typeArgument))
)
},
inRoundBrackets(commaSeparated(arrayLiteral.valueExpressions.map(::expressionSource)))
inParentheses(commaSeparated(arrayLiteral.valueExpressions.map(::expressionSource)))
)
}

Expand Down Expand Up @@ -187,7 +187,7 @@ private fun Renderer.castExpressionSource(castExpression: CastExpression): Sourc
// Render cast to intersection type descriptor: (A & B & C) x
// using smart casts: (x).let { it as A; it as B; it as C; it }
dotSeparated(
inRoundBrackets(expressionSource(castExpression.expression)),
inParentheses(expressionSource(castExpression.expression)),
spaceSeparated(
extensionMemberQualifiedNameSource("kotlin.let"),
inInlineCurlyBrackets(
Expand All @@ -208,7 +208,7 @@ private fun Renderer.castExpressionSource(castExpression: CastExpression): Sourc

private fun Renderer.castTypeDescriptorSource(typeDescriptor: TypeDescriptor): Source =
typeDescriptorSource(typeDescriptor).letIf(typeDescriptor.variableHasAmpersandAny) {
inRoundBrackets(it)
inParentheses(it)
}

private fun BinaryOperator.ktSymbol(useEquality: Boolean): String =
Expand Down Expand Up @@ -271,7 +271,7 @@ private fun Renderer.conditionalExpressionSource(
): Source =
spaceSeparated(
source("if"),
inRoundBrackets(expressionSource(conditionalExpression.conditionExpression)),
inParentheses(expressionSource(conditionalExpression.conditionExpression)),
expressionSource(conditionalExpression.trueExpression),
source("else"),
expressionSource(conditionalExpression.falseExpression)
Expand Down Expand Up @@ -424,7 +424,7 @@ private fun Renderer.invocationTypeArgumentsSource(typeArguments: List<TypeArgum
}

internal fun Renderer.invocationSource(invocation: Invocation) =
inRoundBrackets(commaSeparated(invocation.arguments.map(::expressionSource)))
inParentheses(commaSeparated(invocation.arguments.map(::expressionSource)))

private fun Renderer.multiExpressionSource(multiExpression: MultiExpression): Source =
spaceSeparated(
Expand Down Expand Up @@ -458,7 +458,7 @@ private fun Renderer.newArraySource(
join(
topLevelQualifiedNameSource("kotlin.Array"),
typeArgumentsSource(listOf(typeArgument)),
inRoundBrackets(expressionSource(firstDimension))
inParentheses(expressionSource(firstDimension))
),
block(
newArraySource(
Expand Down Expand Up @@ -490,7 +490,7 @@ private fun Renderer.primitiveArrayOfSource(
else -> throw InternalCompilerError("renderPrimitiveArrayOf($componentTypeDescriptor)")
}
),
inRoundBrackets(expressionSource(dimension))
inParentheses(expressionSource(dimension))
)

private fun Renderer.arrayOfNullsSource(typeArgument: TypeArgument, dimension: Expression): Source =
Expand All @@ -505,7 +505,7 @@ private fun Renderer.arrayOfNullsSource(typeArgument: TypeArgument, dimension: E
extensionMemberQualifiedNameSource("javaemul.lang.uninitializedArrayOf"),
typeArgumentsSource(listOf(typeArgument))
),
inRoundBrackets(expressionSource(dimension))
inParentheses(expressionSource(dimension))
)

private fun Renderer.newInstanceSource(expression: NewInstance): Source =
Expand Down Expand Up @@ -675,7 +675,7 @@ private fun Renderer.rightSubExpressionSource(precedence: Precedence, operand: E
expressionInParensSource(operand, precedence.requiresParensOnRight(operand.precedence))

private fun Renderer.expressionInParensSource(expression: Expression, needsParentheses: Boolean) =
expressionSource(expression).letIf(needsParentheses, ::inRoundBrackets)
expressionSource(expression).letIf(needsParentheses, ::inParentheses)

private val Expression.isNonQualifiedThisReference: Boolean
get() = this is ThisReference && (!isQualified || this.typeDescriptor.typeDeclaration.isAnonymous)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ import com.google.j2cl.transpiler.backend.kotlin.source.dotSeparated
import com.google.j2cl.transpiler.backend.kotlin.source.emptyLineSeparated
import com.google.j2cl.transpiler.backend.kotlin.source.ifNotEmpty
import com.google.j2cl.transpiler.backend.kotlin.source.inAngleBrackets
import com.google.j2cl.transpiler.backend.kotlin.source.inRoundBrackets
import com.google.j2cl.transpiler.backend.kotlin.source.inParentheses
import com.google.j2cl.transpiler.backend.kotlin.source.inSquareBrackets
import com.google.j2cl.transpiler.backend.kotlin.source.join
import com.google.j2cl.transpiler.backend.kotlin.source.plusNewLine
Expand Down Expand Up @@ -502,7 +502,7 @@ private fun literalRenderer(int: Int): Renderer<Source> =
when (int) {
Int.MIN_VALUE ->
jintTypeRenderer.map {
inRoundBrackets(spaceSeparated(inRoundBrackets(it), source("0x80000000")))
inParentheses(spaceSeparated(inParentheses(it), source("0x80000000")))
}
else -> rendererOf(source("$int"))
}
Expand All @@ -511,7 +511,7 @@ private fun literalRenderer(long: Long): Renderer<Source> =
when (long) {
Long.MIN_VALUE ->
jlongTypeRenderer.map {
inRoundBrackets(spaceSeparated(inRoundBrackets(it), source("0x8000000000000000LL")))
inParentheses(spaceSeparated(inParentheses(it), source("0x8000000000000000LL")))
}
else -> rendererOf(source("${long}LL"))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import com.google.j2cl.transpiler.backend.kotlin.source.commaSeparated
import com.google.j2cl.transpiler.backend.kotlin.source.emptyLineSeparated
import com.google.j2cl.transpiler.backend.kotlin.source.emptySource
import com.google.j2cl.transpiler.backend.kotlin.source.ifNotNullSource
import com.google.j2cl.transpiler.backend.kotlin.source.inRoundBrackets
import com.google.j2cl.transpiler.backend.kotlin.source.inParentheses
import com.google.j2cl.transpiler.backend.kotlin.source.indentedIf
import com.google.j2cl.transpiler.backend.kotlin.source.join
import com.google.j2cl.transpiler.backend.kotlin.source.newLineSeparated
Expand Down Expand Up @@ -228,7 +228,7 @@ internal fun Renderer.methodParametersSource(
val renderWithNewLines = objCParameterNames != null && parameters.isNotEmpty()
val optionalNewLineSource = sourceIf(renderWithNewLines) { source("\n") }
return sourceIf(!methodDescriptor.isKtProperty) {
inRoundBrackets(
inParentheses(
join(
indentedIf(
renderWithNewLines,
Expand Down
28 changes: 13 additions & 15 deletions transpiler/java/com/google/j2cl/transpiler/backend/kotlin/Source.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import com.google.j2cl.transpiler.backend.kotlin.source.commaAndNewLineSeparated
import com.google.j2cl.transpiler.backend.kotlin.source.commaSeparated
import com.google.j2cl.transpiler.backend.kotlin.source.dotSeparated
import com.google.j2cl.transpiler.backend.kotlin.source.inNewLine
import com.google.j2cl.transpiler.backend.kotlin.source.inRoundBrackets
import com.google.j2cl.transpiler.backend.kotlin.source.inRoundBracketsIfNotEmpty
import com.google.j2cl.transpiler.backend.kotlin.source.inParentheses
import com.google.j2cl.transpiler.backend.kotlin.source.inParenthesesIfNotEmpty
import com.google.j2cl.transpiler.backend.kotlin.source.indented
import com.google.j2cl.transpiler.backend.kotlin.source.infix
import com.google.j2cl.transpiler.backend.kotlin.source.join
Expand All @@ -38,27 +38,25 @@ fun literalSource(it: Int): Source = source("$it")
fun literalSource(it: Long): Source =
when (it) {
// Long.MIN_VALUE can not be represented as a literal in Kotlin.
Long.MIN_VALUE -> inRoundBrackets(infix(literalSource(Long.MAX_VALUE), "+", literalSource(1L)))
Long.MIN_VALUE -> inParentheses(infix(literalSource(Long.MAX_VALUE), "+", literalSource(1L)))
else -> source("${it}L")
}

fun literalSource(it: Float): Source =
if (it.isNaN()) inRoundBrackets(infix(literalSource(0f), "/", literalSource(0f)))
if (it.isNaN()) inParentheses(infix(literalSource(0f), "/", literalSource(0f)))
else
when (it) {
Float.NEGATIVE_INFINITY -> inRoundBrackets(infix(literalSource(-1f), "/", literalSource(0f)))
Float.POSITIVE_INFINITY -> inRoundBrackets(infix(literalSource(1f), "/", literalSource(0f)))
Float.NEGATIVE_INFINITY -> inParentheses(infix(literalSource(-1f), "/", literalSource(0f)))
Float.POSITIVE_INFINITY -> inParentheses(infix(literalSource(1f), "/", literalSource(0f)))
else -> source("${it}f")
}

fun literalSource(it: Double): Source =
if (it.isNaN()) inRoundBrackets(infix(literalSource(0.0), "/", literalSource(0.0)))
if (it.isNaN()) inParentheses(infix(literalSource(0.0), "/", literalSource(0.0)))
else
when (it) {
Double.NEGATIVE_INFINITY ->
inRoundBrackets(infix(literalSource(-1.0), "/", literalSource(0.0)))
Double.POSITIVE_INFINITY ->
inRoundBrackets(infix(literalSource(1.0), "/", literalSource(0.0)))
Double.NEGATIVE_INFINITY -> inParentheses(infix(literalSource(-1.0), "/", literalSource(0.0)))
Double.POSITIVE_INFINITY -> inParentheses(infix(literalSource(1.0), "/", literalSource(0.0)))
else -> source("$it")
}

Expand All @@ -69,7 +67,7 @@ fun at(source: Source) = join(source("@"), source)
fun labelReference(name: String) = at(identifierSource(name))

fun Source.functionCall(name: String, vararg args: Source) =
dotSeparated(this, join(source(name), inRoundBrackets(commaSeparated(args.toList()))))
dotSeparated(this, join(source(name), inParentheses(commaSeparated(args.toList()))))

fun classLiteral(type: Source) = join(type, source("::class"))

Expand All @@ -81,17 +79,17 @@ fun isExpression(lhs: Source, rhs: Source) = infix(lhs, "is", rhs)

fun itSource() = source("it")

fun todo(source: Source) = join(source("TODO"), inRoundBrackets(source))
fun todo(source: Source) = join(source("TODO"), inParentheses(source))

fun annotation(name: Source) = join(at(name))

fun annotation(name: Source, parameter: Source, vararg parameters: Source) =
join(at(name), inRoundBracketsIfNotEmpty(commaSeparated(parameter, *parameters)))
join(at(name), inParenthesesIfNotEmpty(commaSeparated(parameter, *parameters)))

fun annotation(name: Source, parameters: List<Source>) =
join(
at(name),
inRoundBrackets(
inParentheses(
if (parameters.size <= 2) commaSeparated(parameters)
else indented(inNewLine(commaAndNewLineSeparated(parameters)))
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ import com.google.j2cl.transpiler.backend.kotlin.source.colonSeparated
import com.google.j2cl.transpiler.backend.kotlin.source.commaSeparated
import com.google.j2cl.transpiler.backend.kotlin.source.emptySource
import com.google.j2cl.transpiler.backend.kotlin.source.ifNotNullSource
import com.google.j2cl.transpiler.backend.kotlin.source.inRoundBrackets
import com.google.j2cl.transpiler.backend.kotlin.source.inParentheses
import com.google.j2cl.transpiler.backend.kotlin.source.infix
import com.google.j2cl.transpiler.backend.kotlin.source.join
import com.google.j2cl.transpiler.backend.kotlin.source.newLineSeparated
Expand Down Expand Up @@ -83,7 +83,7 @@ private fun Renderer.assertStatementSource(assertStatement: AssertStatement): So
spaceSeparated(
join(
extensionMemberQualifiedNameSource("kotlin.assert"),
inRoundBrackets(expressionSource(assertStatement.expression))
inParentheses(expressionSource(assertStatement.expression))
),
assertStatement.message.ifNotNullSource { block(expressionSource(it)) }
)
Expand All @@ -104,7 +104,7 @@ private fun Renderer.doWhileStatementSource(doWhileStatement: DoWhileStatement):
source("do"),
statementSource(doWhileStatement.body),
source("while"),
inRoundBrackets(expressionSource(doWhileStatement.conditionExpression))
inParentheses(expressionSource(doWhileStatement.conditionExpression))
)

private fun Renderer.expressionStatementSource(expressionStatement: ExpressionStatement): Source =
Expand All @@ -113,7 +113,7 @@ private fun Renderer.expressionStatementSource(expressionStatement: ExpressionSt
private fun Renderer.forEachStatementSource(forEachStatement: ForEachStatement): Source =
spaceSeparated(
source("for"),
inRoundBrackets(
inParentheses(
infix(
nameSource(forEachStatement.loopVariable),
"in",
Expand All @@ -126,7 +126,7 @@ private fun Renderer.forEachStatementSource(forEachStatement: ForEachStatement):
private fun Renderer.ifStatementSource(ifStatement: IfStatement): Source =
spaceSeparated(
source("if"),
inRoundBrackets(expressionSource(ifStatement.conditionExpression)),
inParentheses(expressionSource(ifStatement.conditionExpression)),
statementSource(ifStatement.thenStatement),
ifStatement.elseStatement.ifNotNullSource {
spaceSeparated(source("else"), statementSource(it))
Expand Down Expand Up @@ -168,7 +168,7 @@ private fun Renderer.returnStatementSource(returnStatement: ReturnStatement): So
private fun Renderer.switchStatementSource(switchStatement: SwitchStatement): Source =
spaceSeparated(
source("when"),
inRoundBrackets(expressionSource(switchStatement.switchExpression)),
inParentheses(expressionSource(switchStatement.switchExpression)),
block(
newLineSeparated(
// TODO(b/263161219): Represent WhenStatement as a data class, convert from SwitchStatement
Expand Down Expand Up @@ -209,15 +209,15 @@ private fun Renderer.synchronizedStatementSource(
spaceSeparated(
join(
extensionMemberQualifiedNameSource("kotlin.synchronized"),
inRoundBrackets(expressionSource(synchronizedStatement.expression))
inParentheses(expressionSource(synchronizedStatement.expression))
),
statementSource(synchronizedStatement.body)
)

private fun Renderer.whileStatementSource(whileStatement: WhileStatement): Source =
spaceSeparated(
source("while"),
inRoundBrackets(expressionSource(whileStatement.conditionExpression)),
inParentheses(expressionSource(whileStatement.conditionExpression)),
statementSource(whileStatement.body)
)

Expand Down Expand Up @@ -252,8 +252,6 @@ private fun Renderer.catchClauseSource(
): Source =
spaceSeparated(
source("catch"),
inRoundBrackets(
colonSeparated(nameSource(variable), typeDescriptorSource(type.toNonNullable()))
),
inParentheses(colonSeparated(nameSource(variable), typeDescriptorSource(type.toNonNullable()))),
blockSource(body)
)
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import com.google.j2cl.transpiler.backend.kotlin.source.commaSeparated
import com.google.j2cl.transpiler.backend.kotlin.source.emptyLineSeparated
import com.google.j2cl.transpiler.backend.kotlin.source.emptySource
import com.google.j2cl.transpiler.backend.kotlin.source.ifNotNullSource
import com.google.j2cl.transpiler.backend.kotlin.source.inRoundBrackets
import com.google.j2cl.transpiler.backend.kotlin.source.inParentheses
import com.google.j2cl.transpiler.backend.kotlin.source.join
import com.google.j2cl.transpiler.backend.kotlin.source.newLineSeparated
import com.google.j2cl.transpiler.backend.kotlin.source.plusSemicolon
Expand Down Expand Up @@ -122,12 +122,12 @@ private fun Renderer.superTypeInvocationSource(
superTypeDescriptor: TypeDescriptor
): Source =
sourceIf(superTypeDescriptor.isClass) {
if (!type.hasConstructors) inRoundBrackets(emptySource)
if (!type.hasConstructors) inParentheses(emptySource)
else
type.ktPrimaryConstructor.let { ktPrimaryConstructor ->
sourceIf(ktPrimaryConstructor != null) {
getConstructorInvocation(ktPrimaryConstructor).let {
if (it == null) inRoundBrackets(emptySource) else invocationSource(it)
if (it == null) inParentheses(emptySource) else invocationSource(it)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import com.google.j2cl.transpiler.backend.kotlin.source.commaSeparated
import com.google.j2cl.transpiler.backend.kotlin.source.dotSeparated
import com.google.j2cl.transpiler.backend.kotlin.source.emptyLineSeparated
import com.google.j2cl.transpiler.backend.kotlin.source.ifEmpty
import com.google.j2cl.transpiler.backend.kotlin.source.inRoundBrackets
import com.google.j2cl.transpiler.backend.kotlin.source.inParentheses
import com.google.j2cl.transpiler.backend.kotlin.source.inSquareBrackets
import com.google.j2cl.transpiler.backend.kotlin.source.join
import com.google.j2cl.transpiler.backend.kotlin.source.newLineSeparated
Expand Down Expand Up @@ -72,7 +72,7 @@ fun nsEnumTypedef(name: String, type: Renderer<Source>, values: List<String>): R
semicolonEnded(
spaceSeparated(
source("typedef"),
join(nsEnumSource, inRoundBrackets(commaSeparated(typeSource, source(name)))),
join(nsEnumSource, inParentheses(commaSeparated(typeSource, source(name)))),
block(
newLineSeparated(
values.mapIndexed { index, name ->
Expand Down Expand Up @@ -101,7 +101,7 @@ fun functionDeclaration(
returnTypeSource,
join(
source(name),
inRoundBrackets(commaSeparated(parameterSources).ifEmpty { source("void") })
inParentheses(commaSeparated(parameterSources).ifEmpty { source("void") })
),
block(newLineSeparated(statementSources))
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ fun afterSpace(source: Source) = join(source(" "), source)

fun inNewLine(source: Source) = source("\n") + source

fun inRoundBracketsIfNotEmpty(source: Source) = source.ifNotEmpty { inRoundBrackets(it) }
fun inParenthesesIfNotEmpty(source: Source) = source.ifNotEmpty { inParentheses(it) }

fun inRoundBrackets(source: Source) = join(source("("), source, source(")"))
fun inParentheses(source: Source) = join(source("("), source, source(")"))

fun inAngleBrackets(source: Source) = join(source("<"), source, source(">"))

Expand Down

0 comments on commit c3daafa

Please sign in to comment.