Handle erroneous code (initial drop, TBD).

This commit is contained in:
Dmitry Petrov
2016-09-02 17:11:15 +03:00
committed by Dmitry Petrov
parent bf03be97a1
commit fe397eddbe
14 changed files with 233 additions and 62 deletions
@@ -21,15 +21,17 @@ import org.jetbrains.kotlin.ir.IrElement
import org.jetbrains.kotlin.ir.throwNoSuchSlot
import org.jetbrains.kotlin.ir.visitors.IrElementVisitor
class IrDummyDeclaration(
interface IrErrorDeclaration : IrDeclaration
class IrErrorDeclarationImpl(
startOffset: Int,
endOffset: Int,
override val descriptor: DeclarationDescriptor
) : IrDeclarationBase(startOffset, endOffset, IrDeclarationOrigin.DEFINED) {
) : IrDeclarationBase(startOffset, endOffset, IrDeclarationOrigin.DEFINED), IrErrorDeclaration {
override val declarationKind: IrDeclarationKind get() = IrDeclarationKind.DUMMY
override fun <R, D> accept(visitor: IrElementVisitor<R, D>, data: D): R {
return visitor.visitDummyDeclaration(this, data)
return visitor.visitErrorDeclaration(this, data)
}
override fun <D> acceptChildren(visitor: IrElementVisitor<Unit, D>, data: D) {
@@ -1,37 +0,0 @@
/*
* 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.visitors.IrElementVisitor
import org.jetbrains.kotlin.types.KotlinType
class IrDummyExpression(
startOffset: Int,
endOffset: Int,
type: KotlinType,
val description: String
) : IrTerminalExpressionBase(startOffset, endOffset, type), IrExpressionWithCopy {
override fun <R, D> accept(visitor: IrElementVisitor<R, D>, data: D): R =
visitor.visitDummyExpression(this, data)
override fun <D> acceptChildren(visitor: IrElementVisitor<Unit, D>, data: D) {
// No children
}
override fun copy(): IrDummyExpression =
IrDummyExpression(startOffset, endOffset, type, description)
}
@@ -0,0 +1,90 @@
/*
* 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
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.utils.SmartList
interface IrErrorExpression : IrExpression {
val description: String
}
interface IrErrorCallExpression : IrErrorExpression {
var explicitReceiver: IrExpression?
val arguments: List<IrExpression>
}
class IrErrorExpressionImpl(
startOffset: Int,
endOffset: Int,
type: KotlinType,
override val description: String
) : IrTerminalExpressionBase(startOffset, endOffset, type), IrExpressionWithCopy, IrErrorExpression {
override fun <R, D> accept(visitor: IrElementVisitor<R, D>, data: D): R =
visitor.visitErrorExpression(this, data)
override fun <D> acceptChildren(visitor: IrElementVisitor<Unit, D>, data: D) {
// No children
}
override fun copy(): IrErrorExpressionImpl =
IrErrorExpressionImpl(startOffset, endOffset, type, description)
}
class IrErrorCallExpressionImpl(
startOffset: Int,
endOffset: Int,
type: KotlinType,
override val description: String
) : IrExpressionBase(startOffset, endOffset, type), IrErrorCallExpression {
override var explicitReceiver: IrExpression? = null
set(value) {
value?.assertDetached()
field?.detach()
field = value
value?.setTreeLocation(this, DISPATCH_RECEIVER_SLOT)
}
override val arguments: MutableList<IrExpression> = SmartList()
fun addArgument(argument: IrExpression) {
argument.assertDetached()
argument.setTreeLocation(this, arguments.size)
arguments.add(argument)
}
override fun <R, D> accept(visitor: IrElementVisitor<R, D>, data: D): R {
return visitor.visitErrorCallExpression(this, data)
}
override fun <D> acceptChildren(visitor: IrElementVisitor<Unit, D>, data: D) {
explicitReceiver?.accept(visitor, data)
arguments.forEach { it.accept(visitor, data) }
}
override fun getChild(slot: Int): IrElement? =
arguments.getOrNull(slot)
override fun replaceChild(slot: Int, newChild: IrElement) {
newChild.assertDetached()
arguments[slot].detach()
arguments[slot] = newChild.assertCast()
newChild.setTreeLocation(this, slot)
}
}
@@ -59,6 +59,13 @@ class DumpIrTreeVisitor(out: Appendable): IrElementVisitor<Unit, String> {
}
}
override fun visitErrorCallExpression(expression: IrErrorCallExpression, data: String) {
expression.dumpLabeledElementWith(data) {
expression.explicitReceiver?.accept(this, "receiver")
expression.arguments.forEach { it.accept(this, "") }
}
}
private fun visitFunctionWithParameters(declaration: IrFunction, data: String) {
declaration.dumpLabeledElementWith(data) {
declaration.descriptor.valueParameters.forEach { valueParameter ->
@@ -182,11 +182,14 @@ class RenderIrElementVisitor : IrElementVisitor<String, Nothing?> {
override fun visitTryCatch(tryCatch: IrTryCatch, data: Nothing?): String =
"TRY_CATCH type=${tryCatch.type.render()}"
override fun visitDummyDeclaration(declaration: IrDummyDeclaration, data: Nothing?): String =
"DUMMY ${declaration.descriptor.javaClass.simpleName} ${declaration.descriptor.name}"
override fun visitErrorDeclaration(declaration: IrErrorDeclaration, data: Nothing?): String =
"ERROR_DECL ${declaration.descriptor.javaClass.simpleName} ${declaration.descriptor.name}"
override fun visitDummyExpression(expression: IrDummyExpression, data: Nothing?): String =
"DUMMY ${expression.description} type=${expression.type.render()}"
override fun visitErrorExpression(expression: IrErrorExpression, data: Nothing?): String =
"ERROR_EXPR '${expression.description}' type=${expression.type.render()}"
override fun visitErrorCallExpression(expression: IrErrorCallExpression, data: Nothing?): String =
"ERROR_CALL '${expression.description}' type=${expression.type.render()}"
companion object {
private val DESCRIPTOR_RENDERER = DescriptorRenderer.withOptions {
@@ -91,7 +91,7 @@ interface IrElementVisitor<out R, in D> {
fun visitReturn(expression: IrReturn, data: D) = visitExpression(expression, data)
fun visitThrow(expression: IrThrow, data: D) = 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)
fun visitErrorDeclaration(declaration: IrErrorDeclaration, data: D) = visitDeclaration(declaration, data)
fun visitErrorExpression(expression: IrErrorExpression, data: D) = visitExpression(expression, data)
fun visitErrorCallExpression(expression: IrErrorCallExpression, data: D) = visitErrorExpression(expression, data)
}