From c3daafa1535662ae389ed89988768c254c26896f Mon Sep 17 00:00:00 2001 From: Googler Date: Tue, 12 Sep 2023 04:15:10 -0700 Subject: [PATCH] [J2KT] Rename Source.inRoundBrackets() to Source.inParentheses(). PiperOrigin-RevId: 564675707 --- .../backend/kotlin/ExpressionRenderer.kt | 20 ++++++------- .../backend/kotlin/J2ObjCCompatRenderer.kt | 6 ++-- .../backend/kotlin/MemberRenderer.kt | 4 +-- .../j2cl/transpiler/backend/kotlin/Source.kt | 28 +++++++++---------- .../backend/kotlin/StatementRenderer.kt | 20 ++++++------- .../transpiler/backend/kotlin/TypeRenderer.kt | 6 ++-- .../backend/kotlin/objc/ObjCRenderer.kt | 6 ++-- .../backend/kotlin/source/Source.kt | 4 +-- 8 files changed, 45 insertions(+), 49 deletions(-) diff --git a/transpiler/java/com/google/j2cl/transpiler/backend/kotlin/ExpressionRenderer.kt b/transpiler/java/com/google/j2cl/transpiler/backend/kotlin/ExpressionRenderer.kt index 580e251e21..d2c4e788d2 100644 --- a/transpiler/java/com/google/j2cl/transpiler/backend/kotlin/ExpressionRenderer.kt +++ b/transpiler/java/com/google/j2cl/transpiler/backend/kotlin/ExpressionRenderer.kt @@ -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 @@ -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))) ) } @@ -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( @@ -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 = @@ -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) @@ -424,7 +424,7 @@ private fun Renderer.invocationTypeArgumentsSource(typeArguments: List throw InternalCompilerError("renderPrimitiveArrayOf($componentTypeDescriptor)") } ), - inRoundBrackets(expressionSource(dimension)) + inParentheses(expressionSource(dimension)) ) private fun Renderer.arrayOfNullsSource(typeArgument: TypeArgument, dimension: Expression): Source = @@ -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 = @@ -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) diff --git a/transpiler/java/com/google/j2cl/transpiler/backend/kotlin/J2ObjCCompatRenderer.kt b/transpiler/java/com/google/j2cl/transpiler/backend/kotlin/J2ObjCCompatRenderer.kt index 5bd67ebc75..c3f417bd8f 100644 --- a/transpiler/java/com/google/j2cl/transpiler/backend/kotlin/J2ObjCCompatRenderer.kt +++ b/transpiler/java/com/google/j2cl/transpiler/backend/kotlin/J2ObjCCompatRenderer.kt @@ -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 @@ -502,7 +502,7 @@ private fun literalRenderer(int: Int): Renderer = when (int) { Int.MIN_VALUE -> jintTypeRenderer.map { - inRoundBrackets(spaceSeparated(inRoundBrackets(it), source("0x80000000"))) + inParentheses(spaceSeparated(inParentheses(it), source("0x80000000"))) } else -> rendererOf(source("$int")) } @@ -511,7 +511,7 @@ private fun literalRenderer(long: Long): Renderer = when (long) { Long.MIN_VALUE -> jlongTypeRenderer.map { - inRoundBrackets(spaceSeparated(inRoundBrackets(it), source("0x8000000000000000LL"))) + inParentheses(spaceSeparated(inParentheses(it), source("0x8000000000000000LL"))) } else -> rendererOf(source("${long}LL")) } diff --git a/transpiler/java/com/google/j2cl/transpiler/backend/kotlin/MemberRenderer.kt b/transpiler/java/com/google/j2cl/transpiler/backend/kotlin/MemberRenderer.kt index 2f86757ffa..f89d2fe106 100644 --- a/transpiler/java/com/google/j2cl/transpiler/backend/kotlin/MemberRenderer.kt +++ b/transpiler/java/com/google/j2cl/transpiler/backend/kotlin/MemberRenderer.kt @@ -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 @@ -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, diff --git a/transpiler/java/com/google/j2cl/transpiler/backend/kotlin/Source.kt b/transpiler/java/com/google/j2cl/transpiler/backend/kotlin/Source.kt index 8a43534808..3b327c331e 100644 --- a/transpiler/java/com/google/j2cl/transpiler/backend/kotlin/Source.kt +++ b/transpiler/java/com/google/j2cl/transpiler/backend/kotlin/Source.kt @@ -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 @@ -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") } @@ -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")) @@ -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) = join( at(name), - inRoundBrackets( + inParentheses( if (parameters.size <= 2) commaSeparated(parameters) else indented(inNewLine(commaAndNewLineSeparated(parameters))) ) diff --git a/transpiler/java/com/google/j2cl/transpiler/backend/kotlin/StatementRenderer.kt b/transpiler/java/com/google/j2cl/transpiler/backend/kotlin/StatementRenderer.kt index d837fd6af0..cc075e65fe 100644 --- a/transpiler/java/com/google/j2cl/transpiler/backend/kotlin/StatementRenderer.kt +++ b/transpiler/java/com/google/j2cl/transpiler/backend/kotlin/StatementRenderer.kt @@ -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 @@ -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)) } ) @@ -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 = @@ -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", @@ -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)) @@ -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 @@ -209,7 +209,7 @@ private fun Renderer.synchronizedStatementSource( spaceSeparated( join( extensionMemberQualifiedNameSource("kotlin.synchronized"), - inRoundBrackets(expressionSource(synchronizedStatement.expression)) + inParentheses(expressionSource(synchronizedStatement.expression)) ), statementSource(synchronizedStatement.body) ) @@ -217,7 +217,7 @@ private fun Renderer.synchronizedStatementSource( private fun Renderer.whileStatementSource(whileStatement: WhileStatement): Source = spaceSeparated( source("while"), - inRoundBrackets(expressionSource(whileStatement.conditionExpression)), + inParentheses(expressionSource(whileStatement.conditionExpression)), statementSource(whileStatement.body) ) @@ -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) ) diff --git a/transpiler/java/com/google/j2cl/transpiler/backend/kotlin/TypeRenderer.kt b/transpiler/java/com/google/j2cl/transpiler/backend/kotlin/TypeRenderer.kt index a9dd638e82..a60b5175c1 100644 --- a/transpiler/java/com/google/j2cl/transpiler/backend/kotlin/TypeRenderer.kt +++ b/transpiler/java/com/google/j2cl/transpiler/backend/kotlin/TypeRenderer.kt @@ -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 @@ -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) } } } diff --git a/transpiler/java/com/google/j2cl/transpiler/backend/kotlin/objc/ObjCRenderer.kt b/transpiler/java/com/google/j2cl/transpiler/backend/kotlin/objc/ObjCRenderer.kt index 214e6c0b37..4982443e0b 100644 --- a/transpiler/java/com/google/j2cl/transpiler/backend/kotlin/objc/ObjCRenderer.kt +++ b/transpiler/java/com/google/j2cl/transpiler/backend/kotlin/objc/ObjCRenderer.kt @@ -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 @@ -72,7 +72,7 @@ fun nsEnumTypedef(name: String, type: Renderer, values: List): 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 -> @@ -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)) ) diff --git a/transpiler/java/com/google/j2cl/transpiler/backend/kotlin/source/Source.kt b/transpiler/java/com/google/j2cl/transpiler/backend/kotlin/source/Source.kt index d4101ad894..95b864348b 100644 --- a/transpiler/java/com/google/j2cl/transpiler/backend/kotlin/source/Source.kt +++ b/transpiler/java/com/google/j2cl/transpiler/backend/kotlin/source/Source.kt @@ -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(">"))