mirror of
https://github.com/GayPizzaSpecifications/pork.git
synced 2025-08-02 12:50:55 +00:00
ast: fix codegen of equals to not produce warnings
This commit is contained in:
parent
c340cfb86d
commit
a292449b6a
@ -12,10 +12,8 @@ class Break : Expression() {
|
||||
override fun <T> visit(visitor: NodeVisitor<T>): T =
|
||||
visitor.visitBreak(this)
|
||||
|
||||
override fun equals(other: Any?): Boolean {
|
||||
if (other !is Break) return false
|
||||
return true
|
||||
}
|
||||
override fun equals(other: Any?): Boolean =
|
||||
other is Break
|
||||
|
||||
override fun hashCode(): Int =
|
||||
31 * type.hashCode()
|
||||
|
@ -12,10 +12,8 @@ class Continue : Expression() {
|
||||
override fun <T> visit(visitor: NodeVisitor<T>): T =
|
||||
visitor.visitContinue(this)
|
||||
|
||||
override fun equals(other: Any?): Boolean {
|
||||
if (other !is Continue) return false
|
||||
return true
|
||||
}
|
||||
override fun equals(other: Any?): Boolean =
|
||||
other is Continue
|
||||
|
||||
override fun hashCode(): Int =
|
||||
31 * type.hashCode()
|
||||
|
@ -12,10 +12,8 @@ class NoneLiteral : Expression() {
|
||||
override fun <T> visit(visitor: NodeVisitor<T>): T =
|
||||
visitor.visitNoneLiteral(this)
|
||||
|
||||
override fun equals(other: Any?): Boolean {
|
||||
if (other !is NoneLiteral) return false
|
||||
return true
|
||||
}
|
||||
override fun equals(other: Any?): Boolean =
|
||||
other is NoneLiteral
|
||||
|
||||
override fun hashCode(): Int =
|
||||
31 * type.hashCode()
|
||||
|
@ -421,9 +421,10 @@ class AstStandardCodegen(pkg: String, outputDirectory: Path, world: AstWorld) :
|
||||
visitSelfFunction.body.add("visitor.visit${type.name}(this)")
|
||||
kotlinClassLike.functions.add(visitSelfFunction)
|
||||
|
||||
val equalsAndHashCodeMembers = kotlinClassLike.members.map {
|
||||
val hashCodeMembers = kotlinClassLike.members.map {
|
||||
it.name
|
||||
}.sortedBy { it == "type" }
|
||||
val equalsMembers = hashCodeMembers.filterNot { it == "type" }
|
||||
val equalsFunction = KotlinFunction(
|
||||
"equals",
|
||||
returnType = "Boolean",
|
||||
@ -433,14 +434,19 @@ class AstStandardCodegen(pkg: String, outputDirectory: Path, world: AstWorld) :
|
||||
"other",
|
||||
"Any?"
|
||||
))
|
||||
equalsFunction.body.add("if (other !is ${type.name}) return false")
|
||||
var predicate = equalsAndHashCodeMembers.mapNotNull {
|
||||
if (it == "type") null else "other.${it} == $it"
|
||||
}.joinToString(" && ")
|
||||
if (predicate.isEmpty()) {
|
||||
predicate = "true"
|
||||
if (equalsMembers.isNotEmpty()) {
|
||||
equalsFunction.body.add("if (other !is ${type.name}) return false")
|
||||
var predicate = equalsMembers.joinToString(" && ") {
|
||||
"other.${it} == $it"
|
||||
}
|
||||
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)
|
||||
|
||||
val hashCodeFunction = KotlinFunction(
|
||||
@ -449,12 +455,12 @@ class AstStandardCodegen(pkg: String, outputDirectory: Path, world: AstWorld) :
|
||||
overridden = true
|
||||
)
|
||||
|
||||
if (equalsAndHashCodeMembers.size == 1) {
|
||||
val member = equalsAndHashCodeMembers.single()
|
||||
if (hashCodeMembers.size == 1) {
|
||||
val member = hashCodeMembers.single()
|
||||
hashCodeFunction.isImmediateExpression = true
|
||||
hashCodeFunction.body.add("31 * ${member}.hashCode()")
|
||||
} else {
|
||||
for ((index, value) in equalsAndHashCodeMembers.withIndex()) {
|
||||
for ((index, value) in hashCodeMembers.withIndex()) {
|
||||
if (index == 0) {
|
||||
hashCodeFunction.body.add("var result = ${value}.hashCode()")
|
||||
} else {
|
||||
|
@ -3,9 +3,7 @@ package gay.pizza.pork.tool
|
||||
import com.github.ajalt.clikt.core.CliktCommand
|
||||
import com.github.ajalt.clikt.parameters.arguments.argument
|
||||
import gay.pizza.dough.fs.PlatformFsProvider
|
||||
import gay.pizza.pork.ast.Node
|
||||
import gay.pizza.pork.ast.NodeCoalescer
|
||||
import gay.pizza.pork.ast.data
|
||||
import gay.pizza.pork.ast.visit
|
||||
import gay.pizza.pork.minimal.FileTool
|
||||
import gay.pizza.pork.parser.ParserAttributes
|
||||
|
Loading…
Reference in New Issue
Block a user