This commit is contained in:
Dmitry Petrov
2016-08-24 17:54:14 +03:00
committed by Dmitry Petrov
parent 4709aaafaa
commit c79b9c12d6
8 changed files with 111 additions and 2 deletions
@@ -135,6 +135,10 @@ class StatementGenerator(
}
}
override fun visitThrowExpression(expression: KtThrowExpression, data: Nothing?): IrStatement {
return IrThrowImpl(expression.startOffset, expression.endOffset, expression.thrownExpression!!.genExpr())
}
override fun visitConstantExpression(expression: KtConstantExpression, data: Nothing?): IrExpression {
val compileTimeConstant = ConstantExpressionEvaluator.getConstant(expression, context.bindingContext)
?: error("KtConstantExpression was not evaluated: ${expression.text}")
@@ -97,6 +97,12 @@ class InsertImplicitCasts(val builtIns: KotlinBuiltIns): IrElementVisitor<Unit,
loop.condition.replaceWithCast(builtIns.booleanType)
}
override fun visitThrow(expression: IrThrow, data: Nothing?) {
expression.acceptChildren(this, null)
expression.value.replaceWithCast(builtIns.throwable.defaultType)
}
private fun IrExpression.replaceWithCast(expectedType: KotlinType?) {
replaceWith { it.wrapWithImplicitCast(expectedType) }
}
@@ -0,0 +1,63 @@
/*
* Copyright 2010-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.ir.expressions
import org.jetbrains.kotlin.ir.*
import org.jetbrains.kotlin.ir.visitors.IrElementVisitor
interface IrThrow : IrExpression {
var value: IrExpression
}
class IrThrowImpl(startOffset: Int, endOffset: Int) : IrExpressionBase(startOffset, endOffset, null), IrThrow {
constructor(startOffset: Int, endOffset: Int, value: IrExpression) : this(startOffset, endOffset) {
this.value = value
}
private var valueImpl: IrExpression? = null
override var value: IrExpression
get() = valueImpl!!
set(newValue) {
newValue.assertDetached()
valueImpl?.detach()
valueImpl = newValue
newValue.setTreeLocation(this, CHILD_EXPRESSION_SLOT)
}
override fun getChild(slot: Int): IrElement? =
when (slot) {
CHILD_EXPRESSION_SLOT -> value
else -> null
}
override fun replaceChild(slot: Int, newChild: IrElement) {
when (slot) {
CHILD_EXPRESSION_SLOT -> value = newChild.assertCast()
else -> throwNoSuchSlot(slot)
}
}
override fun <R, D> accept(visitor: IrElementVisitor<R, D>, data: D): R =
visitor.visitThrow(this, data)
override fun <D> acceptChildren(visitor: IrElementVisitor<Unit, D>, data: D) {
value.accept(visitor, data)
}
}
@@ -112,6 +112,9 @@ class RenderIrElementVisitor : IrElementVisitor<String, Nothing?> {
override fun visitContinue(jump: IrContinue, data: Nothing?): String =
"CONTINUE loop.operator=${jump.loop.operator} depth=${jump.getDepth()}"
override fun visitThrow(expression: IrThrow, data: Nothing?): String =
"THROW type=${expression.renderType()}"
override fun visitDummyDeclaration(declaration: IrDummyDeclaration, data: Nothing?): String =
"DUMMY ${declaration.descriptor.name}"
@@ -37,10 +37,9 @@ interface IrElementVisitor<out R, in D> {
fun visitBody(body: IrBody, data: D): R = visitElement(body, data)
fun visitExpressionBody(body: IrExpressionBody, data: D): R = visitBody(body, data)
fun visitExpression(expression: IrExpression, data: D): R = visitElement(expression, data)
fun <T> visitConst(expression: IrConst<T>, data: D): R = visitExpression(expression, data)
fun visitReturn(expression: IrReturn, data: D): R = visitExpression(expression, data)
fun visitBlock(expression: IrBlock, data: D): R = visitExpression(expression, data)
fun visitStringConcatenation(expression: IrStringConcatenation, data: D) = visitExpression(expression, data)
fun visitThisReference(expression: IrThisReference, data: D) = visitExpression(expression, data)
@@ -65,6 +64,9 @@ interface IrElementVisitor<out R, in D> {
fun visitBreak(jump: IrBreak, data: D) = visitBreakContinue(jump, data)
fun visitContinue(jump: IrContinue, data: D) = visitBreakContinue(jump, data)
fun visitReturn(expression: IrReturn, data: D): R = visitExpression(expression, data)
fun visitThrow(expression: IrThrow, data: D): R = visitExpression(expression, data)
// NB Use it only for testing purposes; will be removed as soon as all Kotlin expression types are covered
fun visitDummyDeclaration(declaration: IrDummyDeclaration, data: D) = visitDeclaration(declaration, data)
fun visitDummyExpression(expression: IrDummyExpression, data: D) = visitExpression(expression, data)
+9
View File
@@ -0,0 +1,9 @@
fun test1() {
throw Throwable()
}
fun testImplicitCast(a: Any) {
if (a is Throwable) {
throw a
}
}
+16
View File
@@ -0,0 +1,16 @@
IrFile /throw.kt
IrFunction public fun test1(): kotlin.Unit
IrExpressionBody
BLOCK type=<no-type> hasResult=false operator=null
THROW type=<no-type>
CALL .<init> type=kotlin.Throwable operator=null
IrFunction public fun testImplicitCast(/*0*/ a: kotlin.Any): kotlin.Unit
IrExpressionBody
BLOCK type=<no-type> hasResult=false operator=null
WHEN type=kotlin.Unit operator=IF
if: TYPE_OP operator=INSTANCEOF typeOperand=kotlin.Throwable
GET_VAR a type=kotlin.Any operator=null
then: BLOCK type=<no-type> hasResult=false operator=null
THROW type=<no-type>
TYPE_OP operator=IMPLICIT_CAST typeOperand=kotlin.Throwable
GET_VAR a type=kotlin.Any operator=null
@@ -257,6 +257,12 @@ public class IrTextTestCaseGenerated extends AbstractIrTextTestCase {
doTest(fileName);
}
@TestMetadata("throw.kt")
public void testThrow() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/ir/irText/throw.kt");
doTest(fileName);
}
@TestMetadata("typeOperators.kt")
public void testTypeOperators() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/ir/irText/typeOperators.kt");