Skip to content

Commit c7e2cd8

Browse files
broadwaylambSpace Cloud
authored and
Space Cloud
committed
[IR] Get rid of IrTypeBase
Every IrType subclass also inherited from IrTypeBase, which complicated the class hierarchy and prevented us from making IrType a sealed class.
1 parent a5530c7 commit c7e2cd8

File tree

15 files changed

+51
-92
lines changed

15 files changed

+51
-92
lines changed

compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/utils/VariousUtils.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ fun IrType.toArrayOrPrimitiveArrayType(builtins: Fir2IrBuiltinSymbolsContainer):
161161

162162
val IrClassSymbol.defaultTypeWithoutArguments: IrSimpleType
163163
get() = IrSimpleTypeImpl(
164-
kotlinType = null,
164+
originalKotlinType = null,
165165
classifier = this,
166166
nullability = SimpleTypeNullability.DEFINITELY_NOT_NULL,
167167
arguments = emptyList(),

compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/CheckIrElementVisitor.kt

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
2+
* Copyright 2010-2024 JetBrains s.r.o. and Kotlin Programming Language contributors.
33
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
44
*/
55

@@ -325,11 +325,9 @@ internal class CheckIrElementVisitor(
325325
}
326326

327327
private fun checkType(type: IrType, element: IrElement) {
328-
when (type) {
329-
is IrSimpleType -> {
330-
if (!type.classifier.isBound) {
331-
reportError(element, "Type: ${type.render()} has unbound classifier")
332-
}
328+
if (type is IrSimpleType) {
329+
if (!type.classifier.isBound) {
330+
reportError(element, "Type: ${type.render()} has unbound classifier")
333331
}
334332
}
335333
}

compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/lower/JsInteropFunctionsLowering.kt

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
2+
* Copyright 2010-2024 JetBrains s.r.o. and Kotlin Programming Language contributors.
33
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
44
*/
55

@@ -283,7 +283,7 @@ class JsInteropFunctionsLowering(val context: WasmBackendContext) : DeclarationT
283283
builtIns.doubleType,
284284
context.wasmSymbols.voidType ->
285285
return null
286-
286+
else -> {}
287287
}
288288

289289
if (isExternalType(this))
@@ -424,6 +424,7 @@ class JsInteropFunctionsLowering(val context: WasmBackendContext) : DeclarationT
424424
builtIns.doubleType,
425425
symbols.voidType ->
426426
return null
427+
else -> {}
427428
}
428429

429430
if (isExternalType(this))

compiler/ir/ir.actualization/src/main/kotlin/org/jetbrains/kotlin/backend/common/actualizer/IrExpectActualMatchingContext.kt

+5-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
2+
* Copyright 2010-2024 JetBrains s.r.o. and Kotlin Programming Language contributors.
33
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
44
*/
55

@@ -16,8 +16,6 @@ import org.jetbrains.kotlin.ir.expressions.IrConstructorCall
1616
import org.jetbrains.kotlin.ir.symbols.*
1717
import org.jetbrains.kotlin.ir.types.*
1818
import org.jetbrains.kotlin.ir.types.impl.IrSimpleTypeImpl
19-
import org.jetbrains.kotlin.ir.types.impl.IrTypeBase
20-
import org.jetbrains.kotlin.ir.types.impl.IrTypeProjectionImpl
2119
import org.jetbrains.kotlin.ir.types.impl.makeTypeProjection
2220
import org.jetbrains.kotlin.ir.util.*
2321
import org.jetbrains.kotlin.mpp.*
@@ -450,13 +448,10 @@ internal abstract class IrExpectActualMatchingContext(
450448
private fun substituteArgumentOrNull(argument: IrTypeArgument): IrTypeArgument? {
451449
return when (argument) {
452450
is IrStarProjection -> null
453-
is IrTypeProjection -> when (argument) {
454-
is IrTypeProjectionImpl -> {
455-
val newType = substituteOrNull(argument.type) ?: return null
456-
makeTypeProjection(newType, argument.variance)
457-
}
458-
is IrTypeBase -> substituteOrNull(argument) as IrTypeBase?
459-
else -> shouldNotBeCalled()
451+
is IrType -> substituteOrNull(argument)
452+
is IrTypeProjection -> {
453+
val newType = substituteOrNull(argument.type) ?: return null
454+
makeTypeProjection(newType, argument.variance)
460455
}
461456
}
462457
}

compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/descriptors/IrBuiltInsOverDescriptors.kt

+1-6
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,7 @@ import org.jetbrains.kotlin.ir.BuiltInOperatorNames
1919
import org.jetbrains.kotlin.ir.IrBuiltIns
2020
import org.jetbrains.kotlin.ir.ObsoleteDescriptorBasedAPI
2121
import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET
22-
import org.jetbrains.kotlin.ir.declarations.IrClass
23-
import org.jetbrains.kotlin.ir.declarations.IrConstructor
24-
import org.jetbrains.kotlin.ir.declarations.IrExternalPackageFragment
25-
import org.jetbrains.kotlin.ir.declarations.IrFactory
26-
import org.jetbrains.kotlin.ir.declarations.createEmptyExternalPackageFragment
22+
import org.jetbrains.kotlin.ir.declarations.*
2723
import org.jetbrains.kotlin.ir.descriptors.*
2824
import org.jetbrains.kotlin.ir.expressions.impl.IrConstructorCallImpl
2925
import org.jetbrains.kotlin.ir.symbols.IrClassSymbol
@@ -34,7 +30,6 @@ import org.jetbrains.kotlin.ir.symbols.impl.IrValueParameterSymbolImpl
3430
import org.jetbrains.kotlin.ir.types.*
3531
import org.jetbrains.kotlin.ir.types.impl.IrSimpleTypeBuilder
3632
import org.jetbrains.kotlin.ir.types.impl.buildSimpleType
37-
import org.jetbrains.kotlin.ir.types.impl.originalKotlinType
3833
import org.jetbrains.kotlin.ir.util.*
3934
import org.jetbrains.kotlin.name.FqName
4035
import org.jetbrains.kotlin.name.Name

compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/OperatorExpressionGenerator.kt

+2-14
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,6 @@
11
/*
2-
* Copyright 2010-2016 JetBrains s.r.o.
3-
*
4-
* Licensed under the Apache License, Version 2.0 (the "License");
5-
* you may not use this file except in compliance with the License.
6-
* You may obtain a copy of the License at
7-
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
10-
* Unless required by applicable law or agreed to in writing, software
11-
* distributed under the License is distributed on an "AS IS" BASIS,
12-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
* See the License for the specific language governing permissions and
14-
* limitations under the License.
2+
* Copyright 2010-2024 JetBrains s.r.o. and Kotlin Programming Language contributors.
3+
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
154
*/
165

176
package org.jetbrains.kotlin.psi2ir.generators
@@ -27,7 +16,6 @@ import org.jetbrains.kotlin.ir.symbols.IrSimpleFunctionSymbol
2716
import org.jetbrains.kotlin.ir.types.IrType
2817
import org.jetbrains.kotlin.ir.types.classifierOrFail
2918
import org.jetbrains.kotlin.ir.types.classifierOrNull
30-
import org.jetbrains.kotlin.ir.types.impl.originalKotlinType
3119
import org.jetbrains.kotlin.lexer.KtTokens
3220
import org.jetbrains.kotlin.name.Name
3321
import org.jetbrains.kotlin.psi.*

compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/intermediate/ArrayAccessAssignmentReceiver.kt

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import org.jetbrains.kotlin.ir.expressions.*
1212
import org.jetbrains.kotlin.ir.expressions.impl.IrBlockImpl
1313
import org.jetbrains.kotlin.ir.expressions.impl.IrGetValueImpl
1414
import org.jetbrains.kotlin.ir.expressions.impl.IrTypeOperatorCallImpl
15-
import org.jetbrains.kotlin.ir.types.impl.originalKotlinType
1615
import org.jetbrains.kotlin.ir.util.inlineStatement
1716
import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid
1817
import org.jetbrains.kotlin.ir.visitors.IrElementVisitorVoid

compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/transformations/InsertImplicitCasts.kt

+2-14
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,6 @@
11
/*
2-
* Copyright 2010-2016 JetBrains s.r.o.
3-
*
4-
* Licensed under the Apache License, Version 2.0 (the "License");
5-
* you may not use this file except in compliance with the License.
6-
* You may obtain a copy of the License at
7-
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
10-
* Unless required by applicable law or agreed to in writing, software
11-
* distributed under the License is distributed on an "AS IS" BASIS,
12-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
* See the License for the specific language governing permissions and
14-
* limitations under the License.
2+
* Copyright 2010-2024 JetBrains s.r.o. and Kotlin Programming Language contributors.
3+
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
154
*/
165

176
package org.jetbrains.kotlin.psi2ir.transformations
@@ -34,7 +23,6 @@ import org.jetbrains.kotlin.ir.expressions.impl.IrConstImpl
3423
import org.jetbrains.kotlin.ir.expressions.impl.IrTypeOperatorCallImpl
3524
import org.jetbrains.kotlin.ir.symbols.IrConstructorSymbol
3625
import org.jetbrains.kotlin.ir.types.IrType
37-
import org.jetbrains.kotlin.ir.types.impl.originalKotlinType
3826
import org.jetbrains.kotlin.ir.types.toKotlinType
3927
import org.jetbrains.kotlin.ir.util.SymbolTable
4028
import org.jetbrains.kotlin.ir.util.TypeTranslator

compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/descriptors/IrBasedDescriptors.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
2+
* Copyright 2010-2024 JetBrains s.r.o. and Kotlin Programming Language contributors.
33
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
44
*/
55

@@ -1151,8 +1151,8 @@ fun IrType.toIrBasedKotlinType(builtins: KotlinBuiltIns? = null): KotlinType = w
11511151
it
11521152
}
11531153
}
1154-
is IrDynamicType -> kotlinType ?: builtins?.let(::createDynamicType) ?: error("Couldn't instantiate DynamicType")
1155-
is IrErrorType -> kotlinType ?: error("Can't find KotlinType in IrErrorType: " + (this as IrType).render())
1154+
is IrDynamicType -> originalKotlinType ?: builtins?.let(::createDynamicType) ?: error("Couldn't instantiate DynamicType")
1155+
is IrErrorType -> originalKotlinType ?: error("Can't find KotlinType in IrErrorType: " + (this as IrType).render())
11561156
else ->
11571157
throw AssertionError("Unexpected type: $this = ${this.render()}")
11581158
}

compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/types/IrType.kt

+10-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors.
2+
* Copyright 2010-2024 JetBrains s.r.o. and Kotlin Programming Language contributors.
33
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
44
*/
55

@@ -9,13 +9,17 @@ import org.jetbrains.kotlin.ir.declarations.IrAnnotationContainer
99
import org.jetbrains.kotlin.ir.symbols.IrClassSymbol
1010
import org.jetbrains.kotlin.ir.symbols.IrClassifierSymbol
1111
import org.jetbrains.kotlin.ir.symbols.IrTypeAliasSymbol
12-
import org.jetbrains.kotlin.ir.types.impl.IrTypeBase
1312
import org.jetbrains.kotlin.mpp.TypeRefMarker
1413
import org.jetbrains.kotlin.types.KotlinType
1514
import org.jetbrains.kotlin.types.Variance
1615
import org.jetbrains.kotlin.types.model.*
1716

18-
abstract class IrType : KotlinTypeMarker, TypeRefMarker, IrAnnotationContainer {
17+
sealed class IrType : IrTypeProjection, KotlinTypeMarker, TypeRefMarker, IrAnnotationContainer {
18+
final override val type: IrType
19+
get() = this
20+
21+
open val originalKotlinType: KotlinType?
22+
get() = null
1923

2024
/**
2125
* @return true if this type is equal to [other] symbolically. Note that this is NOT EQUIVALENT to the full type checking algorithm
@@ -31,15 +35,14 @@ abstract class IrType : KotlinTypeMarker, TypeRefMarker, IrAnnotationContainer {
3135
}
3236

3337
abstract class IrErrorType(
34-
kotlinType: KotlinType?,
3538
private val errorClassStubSymbol: IrClassSymbol,
3639
val isMarkedNullable: Boolean = false
37-
) : IrTypeBase(kotlinType), SimpleTypeMarker {
40+
) : IrType(), SimpleTypeMarker {
3841
val symbol: IrClassSymbol
3942
get() = errorClassStubSymbol
4043
}
4144

42-
abstract class IrDynamicType(kotlinType: KotlinType?) : IrTypeBase(kotlinType), DynamicTypeMarker
45+
abstract class IrDynamicType : IrType(), DynamicTypeMarker
4346

4447
enum class SimpleTypeNullability {
4548
MARKED_NULLABLE,
@@ -51,7 +54,7 @@ enum class SimpleTypeNullability {
5154
}
5255
}
5356

54-
abstract class IrSimpleType(kotlinType: KotlinType?) : IrTypeBase(kotlinType), SimpleTypeMarker, TypeArgumentListMarker {
57+
abstract class IrSimpleType : IrType(), SimpleTypeMarker, TypeArgumentListMarker {
5558
abstract val classifier: IrClassifierSymbol
5659

5760
/**

compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/types/impl/IrSimpleTypeImpl.kt

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors.
2+
* Copyright 2010-2024 JetBrains s.r.o. and Kotlin Programming Language contributors.
33
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
44
*/
55

@@ -16,7 +16,7 @@ import org.jetbrains.kotlin.types.Variance
1616
import org.jetbrains.kotlin.types.model.CaptureStatus
1717
import org.jetbrains.kotlin.utils.compactIfPossible
1818

19-
abstract class IrAbstractSimpleType(kotlinType: KotlinType?) : IrSimpleType(kotlinType) {
19+
abstract class IrAbstractSimpleType : IrSimpleType() {
2020
abstract override val classifier: IrClassifierSymbol
2121
abstract override val nullability: SimpleTypeNullability
2222
abstract override val arguments: List<IrTypeArgument>
@@ -35,7 +35,7 @@ abstract class IrAbstractSimpleType(kotlinType: KotlinType?) : IrSimpleType(kotl
3535
arguments.hashCode()
3636
}
3737

38-
abstract class IrDelegatedSimpleType(kotlinType: KotlinType? = null) : IrAbstractSimpleType(kotlinType) {
38+
abstract class IrDelegatedSimpleType : IrAbstractSimpleType() {
3939

4040
protected abstract val delegate: IrSimpleType
4141

@@ -52,13 +52,13 @@ abstract class IrDelegatedSimpleType(kotlinType: KotlinType? = null) : IrAbstrac
5252
}
5353

5454
class IrSimpleTypeImpl(
55-
kotlinType: KotlinType?,
55+
override val originalKotlinType: KotlinType?,
5656
override val classifier: IrClassifierSymbol,
5757
nullability: SimpleTypeNullability,
5858
override val arguments: List<IrTypeArgument>,
5959
override val annotations: List<IrConstructorCall>,
6060
override val abbreviation: IrTypeAbbreviation? = null
61-
) : IrAbstractSimpleType(kotlinType) {
61+
) : IrAbstractSimpleType() {
6262

6363
override val nullability =
6464
if (classifier !is IrTypeParameterSymbol && nullability == SimpleTypeNullability.NOT_SPECIFIED)
@@ -169,7 +169,7 @@ class IrTypeProjectionImpl internal constructor(
169169
fun makeTypeProjection(type: IrType, variance: Variance): IrTypeProjection =
170170
when {
171171
type is IrCapturedType -> IrTypeProjectionImpl(type, variance)
172-
type is IrTypeProjection && type.variance == variance -> type
172+
type.variance == variance -> type
173173
type is IrSimpleType -> type.toBuilder().buildTypeProjection(variance)
174174
type is IrDynamicType -> IrDynamicTypeImpl(null, type.annotations, variance)
175175
type is IrErrorType -> IrErrorTypeImpl(null, type.annotations, variance)

compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/types/impl/IrTypeBase.kt

+5-12
Original file line numberDiff line numberDiff line change
@@ -16,34 +16,27 @@ import org.jetbrains.kotlin.types.model.CaptureStatus
1616
import org.jetbrains.kotlin.types.model.CapturedTypeConstructorMarker
1717
import org.jetbrains.kotlin.types.model.CapturedTypeMarker
1818

19-
abstract class IrTypeBase(val kotlinType: KotlinType?) : IrType(), IrTypeProjection {
20-
override val type: IrType get() = this
21-
}
22-
2319
class IrErrorTypeImpl(
24-
kotlinType: KotlinType?,
20+
override val originalKotlinType: KotlinType?,
2521
override val annotations: List<IrConstructorCall>,
2622
override val variance: Variance,
2723
isMarkedNullable: Boolean = false
28-
) : IrErrorType(kotlinType, IrErrorClassImpl.symbol, isMarkedNullable) {
24+
) : IrErrorType(IrErrorClassImpl.symbol, isMarkedNullable) {
2925
override fun equals(other: Any?): Boolean = other is IrErrorTypeImpl
3026

3127
override fun hashCode(): Int = IrErrorTypeImpl::class.java.hashCode()
3228
}
3329

3430
class IrDynamicTypeImpl(
35-
kotlinType: KotlinType?,
31+
override val originalKotlinType: KotlinType?,
3632
override val annotations: List<IrConstructorCall>,
3733
override val variance: Variance,
38-
) : IrDynamicType(kotlinType) {
34+
) : IrDynamicType() {
3935
override fun equals(other: Any?): Boolean = other is IrDynamicTypeImpl
4036

4137
override fun hashCode(): Int = IrDynamicTypeImpl::class.java.hashCode()
4238
}
4339

44-
val IrType.originalKotlinType: KotlinType?
45-
get() = (this as? IrTypeBase)?.kotlinType
46-
4740
data object IrStarProjectionImpl : IrStarProjection
4841

4942
// Please note this type is not denotable which means it could only exist inside type system
@@ -55,7 +48,7 @@ class IrCapturedType(
5548
override val nullability: SimpleTypeNullability,
5649
override val annotations: List<IrConstructorCall>,
5750
override val abbreviation: IrTypeAbbreviation?,
58-
) : IrSimpleType(null), CapturedTypeMarker {
51+
) : IrSimpleType(), CapturedTypeMarker {
5952
class Constructor(val argument: IrTypeArgument, val typeParameter: IrTypeParameter) : CapturedTypeConstructorMarker {
6053
var superTypes: List<IrType> = emptyList()
6154
private set

compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/RenderIrElement.kt

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import org.jetbrains.kotlin.ir.expressions.*
1515
import org.jetbrains.kotlin.ir.symbols.*
1616
import org.jetbrains.kotlin.ir.types.*
1717
import org.jetbrains.kotlin.ir.types.impl.IrCapturedType
18-
import org.jetbrains.kotlin.ir.types.impl.originalKotlinType
1918
import org.jetbrains.kotlin.ir.visitors.IrElementVisitor
2019
import org.jetbrains.kotlin.renderer.DescriptorRenderer
2120
import org.jetbrains.kotlin.types.Variance

compiler/ir/serialization.common/src/org/jetbrains/kotlin/backend/common/linkage/partial/PartiallyLinkedMarkerType.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
2+
* Copyright 2010-2024 JetBrains s.r.o. and Kotlin Programming Language contributors.
33
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
44
*/
55

@@ -21,7 +21,7 @@ import org.jetbrains.kotlin.ir.types.SimpleTypeNullability
2121
internal class PartiallyLinkedMarkerType(
2222
builtIns: IrBuiltIns,
2323
val unusableClassifier: ExploredClassifier.Unusable,
24-
) : IrSimpleType(null) {
24+
) : IrSimpleType() {
2525
override val annotations: List<IrConstructorCall> get() = emptyList()
2626
override val classifier: IrClassSymbol = builtIns.anyClass
2727
override val nullability: SimpleTypeNullability get() = SimpleTypeNullability.MARKED_NULLABLE

0 commit comments

Comments
 (0)