forked from JetBrains/kotlin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a builder for
TypeEmbedding
s (#229)
Previously, we used the type embeddings directly in the code. This makes changing their representation quite hard; this PR introduces a builder that lets us hide the implementation details. This isn't quite perfect; we sometimes still check what subtype a particular embedding is. Getting rid of that entirely would be an even bigger change, though.
- Loading branch information
Showing
17 changed files
with
229 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
...l-verification/formver.core/src/org/jetbrains/kotlin/formver/embeddings/PretypeBuilder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* Copyright 2010-2024 JetBrains s.r.o. and Kotlin Programming Language contributors. | ||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. | ||
*/ | ||
|
||
package org.jetbrains.kotlin.formver.embeddings | ||
|
||
import org.jetbrains.kotlin.formver.embeddings.callables.CallableSignatureData | ||
import org.jetbrains.kotlin.formver.names.ScopedKotlinName | ||
|
||
/** | ||
* We use "pretype" to refer to types that do not contain information on nullability or | ||
* other flags. | ||
*/ | ||
interface PretypeBuilder { | ||
/** | ||
* Turn the builder into a `TypeEmbedding`. | ||
* | ||
* We allow this deferral so that the build can be done in any order. | ||
*/ | ||
fun complete(): TypeEmbedding | ||
} | ||
|
||
object UnitPretypeBuilder : PretypeBuilder { | ||
override fun complete(): TypeEmbedding = UnitTypeEmbedding | ||
} | ||
|
||
object NothingPretypeBuilder : PretypeBuilder { | ||
override fun complete(): TypeEmbedding = NothingTypeEmbedding | ||
} | ||
|
||
object AnyPretypeBuilder : PretypeBuilder { | ||
override fun complete(): TypeEmbedding = AnyTypeEmbedding | ||
} | ||
|
||
object IntPretypeBuilder : PretypeBuilder { | ||
override fun complete(): TypeEmbedding = IntTypeEmbedding | ||
} | ||
|
||
object BooleanPretypeBuilder : PretypeBuilder { | ||
override fun complete(): TypeEmbedding = BooleanTypeEmbedding | ||
} | ||
|
||
class FunctionPretypeBuilder : PretypeBuilder { | ||
private val paramTypes = mutableListOf<TypeEmbedding>() | ||
private var receiverType: TypeEmbedding? = null | ||
private var returnType: TypeEmbedding? = null | ||
|
||
fun withParam(paramInit: TypeBuilder.() -> PretypeBuilder) { | ||
paramTypes.add(buildType { paramInit() }) | ||
} | ||
|
||
fun withReceiver(receiverInit: TypeBuilder.() -> PretypeBuilder) { | ||
require(receiverType == null) { "Receiver already set" } | ||
receiverType = buildType { receiverInit() } | ||
} | ||
|
||
fun withReturnType(returnTypeInit: TypeBuilder.() -> PretypeBuilder) { | ||
require(returnType == null) { "Return type already set" } | ||
returnType = buildType { returnTypeInit() } | ||
} | ||
|
||
override fun complete(): TypeEmbedding { | ||
require(returnType != null) { "Return type not set" } | ||
return FunctionTypeEmbedding(CallableSignatureData(receiverType, paramTypes, returnType!!)) | ||
} | ||
} | ||
|
||
class ClassPretypeBuilder : PretypeBuilder { | ||
private var className: ScopedKotlinName? = null | ||
|
||
fun withName(name: ScopedKotlinName) { | ||
require(className == null) { "Class name already set" } | ||
className = name | ||
} | ||
|
||
override fun complete(): TypeEmbedding { | ||
require(className != null) { "Class name not set" } | ||
return ClassTypeEmbedding(className!!) | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
...rmal-verification/formver.core/src/org/jetbrains/kotlin/formver/embeddings/TypeBuilder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Copyright 2010-2024 JetBrains s.r.o. and Kotlin Programming Language contributors. | ||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. | ||
*/ | ||
|
||
package org.jetbrains.kotlin.formver.embeddings | ||
|
||
/** | ||
* Builder for a `TypeEmbedding`. | ||
* | ||
* We split most of the work into `PretypeBuilder`, which builds the type modulo nullability | ||
* (and potentially other flags). As a result, the builder does not contain the full building | ||
* state at any point, though a `TypeBuilder`, `PretypeBuilder` pair does. | ||
*/ | ||
class TypeBuilder { | ||
var isNullable = false | ||
|
||
fun complete(init: TypeBuilder.() -> PretypeBuilder): TypeEmbedding = completeWithPretypeBuilder(init()) | ||
|
||
private fun completeWithPretypeBuilder(subBuilder: PretypeBuilder): TypeEmbedding { | ||
val subResult = subBuilder.complete() | ||
return if (isNullable) NullableTypeEmbedding(subResult) else subResult | ||
} | ||
|
||
fun unit() = UnitPretypeBuilder | ||
fun nothing() = NothingPretypeBuilder | ||
fun any() = AnyPretypeBuilder | ||
fun int() = IntPretypeBuilder | ||
fun boolean() = BooleanPretypeBuilder | ||
fun function(init: FunctionPretypeBuilder.() -> Unit) = FunctionPretypeBuilder().also { it.init() } | ||
fun klass(init: ClassPretypeBuilder.() -> Unit) = ClassPretypeBuilder().also { it.init() } | ||
} | ||
|
||
fun TypeBuilder.nullableAny(): AnyPretypeBuilder { | ||
isNullable = true | ||
return any() | ||
} | ||
|
||
fun buildType(init: TypeBuilder.() -> PretypeBuilder): TypeEmbedding = TypeBuilder().complete(init) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.