mirror of
https://github.com/GayPizzaSpecifications/pork.git
synced 2025-08-03 13:11:32 +00:00
ast: fix codegen of equals to not produce warnings
This commit is contained in:
@ -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()
|
||||||
|
@ -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()
|
||||||
|
@ -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()
|
||||||
|
@ -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?"
|
||||||
))
|
))
|
||||||
|
if (equalsMembers.isNotEmpty()) {
|
||||||
equalsFunction.body.add("if (other !is ${type.name}) return false")
|
equalsFunction.body.add("if (other !is ${type.name}) return false")
|
||||||
var predicate = equalsAndHashCodeMembers.mapNotNull {
|
var predicate = equalsMembers.joinToString(" && ") {
|
||||||
if (it == "type") null else "other.${it} == $it"
|
"other.${it} == $it"
|
||||||
}.joinToString(" && ")
|
}
|
||||||
if (predicate.isEmpty()) {
|
if (predicate.isEmpty()) {
|
||||||
predicate = "true"
|
predicate = "true"
|
||||||
}
|
}
|
||||||
equalsFunction.body.add("return $predicate")
|
equalsFunction.body.add("return $predicate")
|
||||||
|
} else {
|
||||||
|
equalsFunction.isImmediateExpression = true
|
||||||
|
equalsFunction.body.add("other is ${type.name}")
|
||||||
|
}
|
||||||
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 {
|
||||||
|
@ -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
|
||||||
|
Reference in New Issue
Block a user