IR: '!!' is generated as 'CHECK_NOT_NULL' intrinsic
``` fun <T : Any> CHECK_NOT_NULL(x: T?): x = if (x != null) x else throw NullPointerException(...) ``` This allows to compile both Kotlin/JVM and Kotlin/JS effectively.
This commit is contained in:
+1
-1
@@ -19,7 +19,7 @@ class ExceptionHelperCallsTransformer(private val context: JsIrBackendContext) :
|
||||
}
|
||||
|
||||
private val helperMapping = mapOf(
|
||||
context.irBuiltIns.throwNpeSymbol to referenceFunction(kotlinPackageFqn.child(Name.identifier("THROW_NPE"))),
|
||||
// context.irBuiltIns.throwNpeSymbol to referenceFunction(kotlinPackageFqn.child(Name.identifier("THROW_NPE"))), -- TODO checkNotNullSymbol
|
||||
context.irBuiltIns.throwCceSymbol to referenceFunction(kotlinPackageFqn.child(Name.identifier("THROW_CCE"))),
|
||||
context.irBuiltIns.throwIseSymbol to referenceFunction(kotlinPackageFqn.child(Name.identifier("THROW_ISE"))),
|
||||
context.irBuiltIns.noWhenBranchMatchedExceptionSymbol to referenceFunction(kotlinPackageFqn.child(Name.identifier("noWhenBranchMatchedException")))
|
||||
|
||||
+3
-3
@@ -45,7 +45,7 @@ class IrIntrinsicMethods(val irBuiltIns: IrBuiltIns, val symbols: JvmSymbols) {
|
||||
|
||||
private val intrinsicsMap = (
|
||||
listOf(
|
||||
Key(KOTLIN_JVM, FqName("T"),"<get-javaClass>", emptyList()) to JavaClassProperty,
|
||||
Key(KOTLIN_JVM, FqName("T"), "<get-javaClass>", emptyList()) to JavaClassProperty,
|
||||
Key(
|
||||
KOTLIN_JVM,
|
||||
KotlinBuiltIns.FQ_NAMES.kClass.toSafe(),
|
||||
@@ -96,7 +96,7 @@ class IrIntrinsicMethods(val irBuiltIns: IrBuiltIns, val symbols: JvmSymbols) {
|
||||
irBuiltIns.enumValueOfSymbol.toKey()!! to IrEnumValueOf,
|
||||
irBuiltIns.noWhenBranchMatchedExceptionSymbol.toKey()!! to IrNoWhenBranchMatchedException,
|
||||
irBuiltIns.illegalArgumentExceptionSymbol.toKey()!! to IrIllegalArgumentException,
|
||||
irBuiltIns.throwNpeSymbol.toKey()!! to ThrowNPE,
|
||||
// irBuiltIns.throwNpeSymbol.toKey()!! to ThrowNPE, -- TODO checkNotNullSymbol
|
||||
irBuiltIns.andandSymbol.toKey()!! to AndAnd,
|
||||
irBuiltIns.ororSymbol.toKey()!! to OrOr,
|
||||
symbols.unsafeCoerceIntrinsicSymbol.toKey()!! to UnsafeCoerce
|
||||
@@ -136,7 +136,7 @@ class IrIntrinsicMethods(val irBuiltIns: IrBuiltIns, val symbols: JvmSymbols) {
|
||||
primitiveComparisonIntrinsics(irBuiltIns.lessOrEqualFunByOperandType, KtTokens.LTEQ) +
|
||||
primitiveComparisonIntrinsics(irBuiltIns.greaterFunByOperandType, KtTokens.GT) +
|
||||
primitiveComparisonIntrinsics(irBuiltIns.greaterOrEqualFunByOperandType, KtTokens.GTEQ)
|
||||
).toMap()
|
||||
).toMap()
|
||||
|
||||
private val PrimitiveType.symbol
|
||||
get() = irBuiltIns.primitiveTypeToIrType[this]!!.classOrNull!!
|
||||
|
||||
+21
-12
@@ -40,10 +40,8 @@ import org.jetbrains.kotlin.resolve.calls.NewCommonSuperTypeCalculator
|
||||
import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall
|
||||
import org.jetbrains.kotlin.resolve.checkers.PrimitiveNumericComparisonInfo
|
||||
import org.jetbrains.kotlin.resolve.constants.evaluate.ConstantExpressionEvaluator
|
||||
import org.jetbrains.kotlin.types.KotlinType
|
||||
import org.jetbrains.kotlin.types.*
|
||||
import org.jetbrains.kotlin.types.checker.intersectTypes
|
||||
import org.jetbrains.kotlin.types.isDynamic
|
||||
import org.jetbrains.kotlin.types.isError
|
||||
import org.jetbrains.kotlin.types.typeUtil.isPrimitiveNumberType
|
||||
import org.jetbrains.kotlin.types.typeUtil.makeNotNullable
|
||||
import org.jetbrains.kotlin.types.typeUtil.makeNullable
|
||||
@@ -488,16 +486,27 @@ class OperatorExpressionGenerator(statementGenerator: StatementGenerator) : Stat
|
||||
val irArgument = ktArgument.genExpr()
|
||||
val ktOperator = expression.operationReference
|
||||
|
||||
val resultType = irArgument.type.makeNotNull()
|
||||
val argumentType = context.bindingContext.getType(ktArgument)
|
||||
?: throw AssertionError("No type for !! argument")
|
||||
val expressionType = argumentType.makeNotNullable()
|
||||
|
||||
return irBlock(ktOperator.startOffsetSkippingComments, ktOperator.endOffset, origin, resultType) {
|
||||
val temporary = irTemporary(irArgument, "notnull")
|
||||
+irIfNull(
|
||||
resultType,
|
||||
irGet(temporary.type, temporary.symbol),
|
||||
irThrowNpe(origin),
|
||||
irGet(temporary.type, temporary.symbol)
|
||||
)
|
||||
val checkNotNull = context.irBuiltIns.checkNotNull
|
||||
val checkNotNullSubstituted =
|
||||
checkNotNull.substitute(
|
||||
TypeSubstitutor.create(
|
||||
mapOf(checkNotNull.typeParameters[0].typeConstructor to TypeProjectionImpl(argumentType))
|
||||
)
|
||||
) ?: throw AssertionError("Substitution failed for $checkNotNull: T=$argumentType")
|
||||
|
||||
return IrCallImpl(
|
||||
ktOperator.startOffsetSkippingComments, ktOperator.endOffset,
|
||||
expressionType.toIrType(),
|
||||
context.irBuiltIns.checkNotNullSymbol,
|
||||
checkNotNullSubstituted,
|
||||
origin
|
||||
).apply {
|
||||
putTypeArgument(0, argumentType.toIrType().makeNotNull())
|
||||
putValueArgument(0, irArgument)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -13,7 +13,6 @@ import org.jetbrains.kotlin.ir.expressions.*
|
||||
import org.jetbrains.kotlin.ir.expressions.impl.*
|
||||
import org.jetbrains.kotlin.ir.symbols.*
|
||||
import org.jetbrains.kotlin.ir.types.IrType
|
||||
import org.jetbrains.kotlin.ir.types.classifierOrFail
|
||||
import org.jetbrains.kotlin.ir.util.parentAsClass
|
||||
import org.jetbrains.kotlin.types.KotlinType
|
||||
import org.jetbrains.kotlin.utils.addToStdlib.assertedCast
|
||||
@@ -159,17 +158,6 @@ fun IrBuilderWithScope.irIfThenMaybeElse(
|
||||
fun IrBuilderWithScope.irIfNull(type: IrType, subject: IrExpression, thenPart: IrExpression, elsePart: IrExpression) =
|
||||
irIfThenElse(type, irEqualsNull(subject), thenPart, elsePart)
|
||||
|
||||
fun IrBuilderWithScope.irThrowNpe(origin: IrStatementOrigin? = null) =
|
||||
IrCallImpl(
|
||||
startOffset, endOffset,
|
||||
context.irBuiltIns.nothingType,
|
||||
context.irBuiltIns.throwNpeSymbol,
|
||||
context.irBuiltIns.throwNpeSymbol.descriptor,
|
||||
typeArgumentsCount = 0,
|
||||
valueArgumentsCount = 0,
|
||||
origin = origin
|
||||
)
|
||||
|
||||
fun IrBuilderWithScope.irIfThenReturnTrue(condition: IrExpression) =
|
||||
irIfThen(context.irBuiltIns.unitType, condition, irReturnTrue())
|
||||
|
||||
|
||||
@@ -68,17 +68,6 @@ fun IrGeneratorContext.equalsNull(startOffset: Int, endOffset: Int, argument: Ir
|
||||
fun IrGeneratorContext.eqeqeq(startOffset: Int, endOffset: Int, argument1: IrExpression, argument2: IrExpression): IrExpression =
|
||||
primitiveOp2(startOffset, endOffset, irBuiltIns.eqeqeqSymbol, irBuiltIns.booleanType, IrStatementOrigin.EQEQEQ, argument1, argument2)
|
||||
|
||||
fun IrGeneratorContext.throwNpe(startOffset: Int, endOffset: Int, origin: IrStatementOrigin): IrExpression =
|
||||
IrCallImpl(
|
||||
startOffset, endOffset,
|
||||
irBuiltIns.nothingType,
|
||||
irBuiltIns.throwNpeSymbol,
|
||||
irBuiltIns.throwNpeSymbol.descriptor,
|
||||
typeArgumentsCount = 0,
|
||||
valueArgumentsCount = 0,
|
||||
origin = origin
|
||||
)
|
||||
|
||||
fun IrGeneratorContext.constTrue(startOffset: Int, endOffset: Int) =
|
||||
IrConstImpl.constTrue(startOffset, endOffset, irBuiltIns.booleanType)
|
||||
|
||||
|
||||
@@ -23,10 +23,7 @@ import org.jetbrains.kotlin.ir.util.SymbolTable
|
||||
import org.jetbrains.kotlin.ir.util.TypeTranslator
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
import org.jetbrains.kotlin.name.Name
|
||||
import org.jetbrains.kotlin.types.KotlinType
|
||||
import org.jetbrains.kotlin.types.KotlinTypeFactory
|
||||
import org.jetbrains.kotlin.types.SimpleType
|
||||
import org.jetbrains.kotlin.types.Variance
|
||||
import org.jetbrains.kotlin.types.*
|
||||
import org.jetbrains.kotlin.types.typeUtil.makeNullable
|
||||
|
||||
class IrBuiltIns(
|
||||
@@ -204,8 +201,8 @@ class IrBuiltIns(
|
||||
val greaterFunByOperandType = primitiveTypesWithComparisons.defineComparisonOperatorForEachType(OperatorNames.GREATER)
|
||||
|
||||
val ieee754equalsFunByOperandType =
|
||||
primitiveFloatingPointTypes.associate {
|
||||
it to defineOperator(OperatorNames.IEEE754_EQUALS, bool, listOf(it.makeNullable(), it.makeNullable()))
|
||||
primitiveFloatingPointTypes.associateWith {
|
||||
defineOperator(OperatorNames.IEEE754_EQUALS, bool, listOf(it.makeNullable(), it.makeNullable()))
|
||||
}
|
||||
|
||||
val booleanNot = builtIns.boolean.unsubstitutedMemberScope.getContributedFunctions(Name.identifier("not"), NoLookupLocation.FROM_BACKEND).single()
|
||||
@@ -213,7 +210,6 @@ class IrBuiltIns(
|
||||
|
||||
val eqeqeqSymbol = defineOperator(OperatorNames.EQEQEQ, bool, listOf(anyN, anyN))
|
||||
val eqeqSymbol = defineOperator(OperatorNames.EQEQ, bool, listOf(anyN, anyN))
|
||||
val throwNpeSymbol = defineOperator(OperatorNames.THROW_NPE, nothing, listOf())
|
||||
val throwCceSymbol = defineOperator(OperatorNames.THROW_CCE, nothing, listOf())
|
||||
val throwIseSymbol = defineOperator(OperatorNames.THROW_ISE, nothing, listOf())
|
||||
val andandSymbol = defineOperator(OperatorNames.ANDAND, bool, listOf(bool, bool))
|
||||
@@ -223,15 +219,11 @@ class IrBuiltIns(
|
||||
|
||||
val eqeqeq = eqeqeqSymbol.descriptor
|
||||
val eqeq = eqeqSymbol.descriptor
|
||||
val throwNpe = throwNpeSymbol.descriptor
|
||||
val throwCce = throwCceSymbol.descriptor
|
||||
val noWhenBranchMatchedException = noWhenBranchMatchedExceptionSymbol.descriptor
|
||||
val illegalArgumentException = illegalArgumentExceptionSymbol.descriptor
|
||||
|
||||
val enumValueOfSymbol = createEnumValueOfFun()
|
||||
val enumValueOf = enumValueOfSymbol.descriptor
|
||||
|
||||
private fun createEnumValueOfFun(): IrSimpleFunctionSymbol =
|
||||
val enumValueOfSymbol =
|
||||
SimpleFunctionDescriptorImpl.create(
|
||||
packageFragment,
|
||||
Annotations.EMPTY,
|
||||
@@ -248,10 +240,42 @@ class IrBuiltIns(
|
||||
false, false, false, null, SourceElement.NO_SOURCE
|
||||
)
|
||||
|
||||
val returnType = KotlinTypeFactory.simpleType(Annotations.EMPTY, typeParameterT.typeConstructor, listOf(), false)
|
||||
val returnType = typeParameterT.typeConstructor.makeNonNullType()
|
||||
|
||||
initialize(null, null, listOf(typeParameterT), listOf(valueParameterName), returnType, Modality.FINAL, Visibilities.PUBLIC)
|
||||
}.addStub()
|
||||
val enumValueOf = enumValueOfSymbol.descriptor
|
||||
|
||||
val checkNotNullSymbol =
|
||||
SimpleFunctionDescriptorImpl.create(
|
||||
packageFragment,
|
||||
Annotations.EMPTY,
|
||||
Name.identifier("CHECK_NOT_NULL"),
|
||||
CallableMemberDescriptor.Kind.SYNTHESIZED,
|
||||
SourceElement.NO_SOURCE
|
||||
).apply {
|
||||
val typeParameterT = TypeParameterDescriptorImpl.createForFurtherModification(
|
||||
this, Annotations.EMPTY, false, Variance.INVARIANT, Name.identifier("T"), 0, SourceElement.NO_SOURCE
|
||||
).apply {
|
||||
addUpperBound(builtIns.anyType)
|
||||
setInitialized()
|
||||
}
|
||||
|
||||
val valueParameterX = ValueParameterDescriptorImpl(
|
||||
this, null, 0, Annotations.EMPTY, Name.identifier("x"), typeParameterT.typeConstructor.makeNullableType(),
|
||||
false, false, false, null, SourceElement.NO_SOURCE
|
||||
)
|
||||
|
||||
initialize(
|
||||
null, null,
|
||||
listOf(typeParameterT), listOf(valueParameterX), typeParameterT.typeConstructor.makeNonNullType(),
|
||||
Modality.FINAL, Visibilities.PUBLIC
|
||||
)
|
||||
}.addStub()
|
||||
val checkNotNull = checkNotNullSymbol.descriptor
|
||||
|
||||
private fun TypeConstructor.makeNonNullType() = KotlinTypeFactory.simpleType(Annotations.EMPTY, this, listOf(), false)
|
||||
private fun TypeConstructor.makeNullableType() = KotlinTypeFactory.simpleType(Annotations.EMPTY, this, listOf(), true)
|
||||
|
||||
val dataClassArrayMemberHashCodeSymbol = defineOperator("dataClassArrayMemberHashCode", int, listOf(any))
|
||||
val dataClassArrayMemberHashCode = dataClassArrayMemberHashCodeSymbol.descriptor
|
||||
|
||||
Reference in New Issue
Block a user