ast: fix codegen of equals to not produce warnings

This commit is contained in:
2023-09-23 15:53:54 -07:00
parent c340cfb86d
commit a292449b6a
5 changed files with 23 additions and 25 deletions

View File

@ -12,10 +12,8 @@ class Break : Expression() {
override fun <T> visit(visitor: NodeVisitor<T>): T = override fun <T> visit(visitor: NodeVisitor<T>): T =
visitor.visitBreak(this) visitor.visitBreak(this)
override fun equals(other: Any?): Boolean { override fun equals(other: Any?): Boolean =
if (other !is Break) return false other is Break
return true
}
override fun hashCode(): Int = override fun hashCode(): Int =
31 * type.hashCode() 31 * type.hashCode()

View File

@ -12,10 +12,8 @@ class Continue : Expression() {
override fun <T> visit(visitor: NodeVisitor<T>): T = override fun <T> visit(visitor: NodeVisitor<T>): T =
visitor.visitContinue(this) visitor.visitContinue(this)
override fun equals(other: Any?): Boolean { override fun equals(other: Any?): Boolean =
if (other !is Continue) return false other is Continue
return true
}
override fun hashCode(): Int = override fun hashCode(): Int =
31 * type.hashCode() 31 * type.hashCode()

View File

@ -12,10 +12,8 @@ class NoneLiteral : Expression() {
override fun <T> visit(visitor: NodeVisitor<T>): T = override fun <T> visit(visitor: NodeVisitor<T>): T =
visitor.visitNoneLiteral(this) visitor.visitNoneLiteral(this)
override fun equals(other: Any?): Boolean { override fun equals(other: Any?): Boolean =
if (other !is NoneLiteral) return false other is NoneLiteral
return true
}
override fun hashCode(): Int = override fun hashCode(): Int =
31 * type.hashCode() 31 * type.hashCode()

View File

@ -421,9 +421,10 @@ class AstStandardCodegen(pkg: String, outputDirectory: Path, world: AstWorld) :
visitSelfFunction.body.add("visitor.visit${type.name}(this)") visitSelfFunction.body.add("visitor.visit${type.name}(this)")
kotlinClassLike.functions.add(visitSelfFunction) kotlinClassLike.functions.add(visitSelfFunction)
val equalsAndHashCodeMembers = kotlinClassLike.members.map { val hashCodeMembers = kotlinClassLike.members.map {
it.name it.name
}.sortedBy { it == "type" } }.sortedBy { it == "type" }
val equalsMembers = hashCodeMembers.filterNot { it == "type" }
val equalsFunction = KotlinFunction( val equalsFunction = KotlinFunction(
"equals", "equals",
returnType = "Boolean", returnType = "Boolean",
@ -433,14 +434,19 @@ class AstStandardCodegen(pkg: String, outputDirectory: Path, world: AstWorld) :
"other", "other",
"Any?" "Any?"
)) ))
equalsFunction.body.add("if (other !is ${type.name}) return false") if (equalsMembers.isNotEmpty()) {
var predicate = equalsAndHashCodeMembers.mapNotNull { equalsFunction.body.add("if (other !is ${type.name}) return false")
if (it == "type") null else "other.${it} == $it" var predicate = equalsMembers.joinToString(" && ") {
}.joinToString(" && ") "other.${it} == $it"
if (predicate.isEmpty()) { }
predicate = "true" if (predicate.isEmpty()) {
predicate = "true"
}
equalsFunction.body.add("return $predicate")
} else {
equalsFunction.isImmediateExpression = true
equalsFunction.body.add("other is ${type.name}")
} }
equalsFunction.body.add("return $predicate")
kotlinClassLike.functions.add(equalsFunction) kotlinClassLike.functions.add(equalsFunction)
val hashCodeFunction = KotlinFunction( val hashCodeFunction = KotlinFunction(
@ -449,12 +455,12 @@ class AstStandardCodegen(pkg: String, outputDirectory: Path, world: AstWorld) :
overridden = true overridden = true
) )
if (equalsAndHashCodeMembers.size == 1) { if (hashCodeMembers.size == 1) {
val member = equalsAndHashCodeMembers.single() val member = hashCodeMembers.single()
hashCodeFunction.isImmediateExpression = true hashCodeFunction.isImmediateExpression = true
hashCodeFunction.body.add("31 * ${member}.hashCode()") hashCodeFunction.body.add("31 * ${member}.hashCode()")
} else { } else {
for ((index, value) in equalsAndHashCodeMembers.withIndex()) { for ((index, value) in hashCodeMembers.withIndex()) {
if (index == 0) { if (index == 0) {
hashCodeFunction.body.add("var result = ${value}.hashCode()") hashCodeFunction.body.add("var result = ${value}.hashCode()")
} else { } else {

View File

@ -3,9 +3,7 @@ package gay.pizza.pork.tool
import com.github.ajalt.clikt.core.CliktCommand import com.github.ajalt.clikt.core.CliktCommand
import com.github.ajalt.clikt.parameters.arguments.argument import com.github.ajalt.clikt.parameters.arguments.argument
import gay.pizza.dough.fs.PlatformFsProvider import gay.pizza.dough.fs.PlatformFsProvider
import gay.pizza.pork.ast.Node
import gay.pizza.pork.ast.NodeCoalescer import gay.pizza.pork.ast.NodeCoalescer
import gay.pizza.pork.ast.data
import gay.pizza.pork.ast.visit import gay.pizza.pork.ast.visit
import gay.pizza.pork.minimal.FileTool import gay.pizza.pork.minimal.FileTool
import gay.pizza.pork.parser.ParserAttributes import gay.pizza.pork.parser.ParserAttributes