Move some callable method impl to trait

This commit is contained in:
Michael Bogdanov
2015-04-04 12:24:59 +03:00
parent 05aa80e7f1
commit 47c838e62b
4 changed files with 18 additions and 56 deletions
@@ -46,8 +46,15 @@ public trait Callable {
public fun isStaticCall(): Boolean
public fun beforeParameterGeneration(v: InstructionAdapter, value: StackValue?)
public fun beforeParameterGeneration(v: InstructionAdapter, value: StackValue?) {
public fun invokeMethodWithArguments(resolvedCall: ResolvedCall<*>, receiver: StackValue, returnType: Type, codegen: ExpressionCodegen): StackValue
}
public fun invokeMethodWithArguments(resolvedCall: ResolvedCall<*>, receiver: StackValue, returnType: Type, codegen: ExpressionCodegen): StackValue {
return StackValue.functionCall(returnType) {
codegen.invokeMethodWithArguments(this, resolvedCall, receiver)
StackValue.coerce(this.returnType, returnType, it)
}
}
}
@@ -40,33 +40,19 @@ import java.util.ArrayList;
public class CallableMethod(override val owner: Type, private val defaultImplOwner: Type?, private val defaultImplParam: Type?, private val signature: JvmMethodSignature, private val invokeOpcode: Int, override val thisType: Type?, override val receiverType: Type?, override val generateCalleeType: Type?) : Callable {
public fun getValueParameters(): List<JvmMethodParameterSignature> {
return signature.getValueParameters()
}
override val valueParameterTypes: List<Type>
get() {
val valueParameters = signature.getValueParameters()
val result = ArrayList<Type>(valueParameters.size())
for (parameter in valueParameters) {
if (parameter.getKind() == JvmMethodParameterKind.VALUE) {
result.add(parameter.getAsmType())
}
}
return result
}
get() = signature.getValueParameters().filter { it.getKind() == JvmMethodParameterKind.VALUE }.map { it.getAsmType() }
public fun getAsmMethod(): Method {
return signature.getAsmMethod()
}
override val argumentTypes: Array<Type>
get() =signature.getAsmMethod().getArgumentTypes()
private fun invoke(v: InstructionAdapter) {
v.visitMethodInsn(invokeOpcode, owner.getInternalName(), getAsmMethod().getName(), getAsmMethod().getDescriptor())
}
get() = getAsmMethod().getArgumentTypes()
public override fun invokeWithNotNullAssertion(v: InstructionAdapter, state: GenerationState, resolvedCall: ResolvedCall<*>) {
@@ -75,7 +61,7 @@ public class CallableMethod(override val owner: Type, private val defaultImplOwn
}
public override fun invokeWithoutAssertions(v: InstructionAdapter) {
invoke(v)
v.visitMethodInsn(invokeOpcode, owner.getInternalName(), getAsmMethod().getName(), getAsmMethod().getDescriptor())
}
private fun invokeDefault(v: InstructionAdapter) {
@@ -104,25 +90,13 @@ public class CallableMethod(override val owner: Type, private val defaultImplOwn
}
override val returnType: Type
get() {
return signature.getReturnType()
}
override fun toString(): String {
return Printer.OPCODES[invokeOpcode] + " " + owner.getInternalName() + "." + signature
}
get() = signature.getReturnType()
override fun isStaticCall(): Boolean {
return invokeOpcode == INVOKESTATIC
}
public override fun beforeParameterGeneration(v: InstructionAdapter, value: StackValue?) {
}
override fun invokeMethodWithArguments(resolvedCall: ResolvedCall<*>, receiver: StackValue, returnType: Type, codegen: ExpressionCodegen): StackValue {
return StackValue.functionCall(returnType) {
codegen.invokeMethodWithArguments(this@CallableMethod, resolvedCall, receiver, returnType)
}
override fun toString(): String {
return "${Printer.OPCODES[invokeOpcode]} ${owner.getInternalName()}.$signature"
}
}
@@ -2255,17 +2255,7 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
Callable callable = resolveToCallable(accessibleFunctionDescriptor, superCall, resolvedCall);
Type returnType = typeMapper.mapReturnType(accessibleFunctionDescriptor);
return ((Callable) callable).invokeMethodWithArguments(resolvedCall, receiver, returnType, this);
}
public void invokeMethodWithArguments(
@NotNull Callable callable,
@NotNull ResolvedCall resolvedCall,
@NotNull StackValue receiver,
@NotNull Type returnType
) {
invokeMethodWithArguments(callable, resolvedCall, receiver);
StackValue.coerce(callable.getReturnType(), returnType, v);
return callable.invokeMethodWithArguments(resolvedCall, receiver, returnType, this);
}
@Nullable
@@ -97,26 +97,17 @@ public abstract class IntrinsicCallable(override val returnType: Type,
return receiverType ?: thisType
}
override fun beforeParameterGeneration(v: InstructionAdapter, value: StackValue?) {
}
override fun invokeMethodWithArguments(resolvedCall: ResolvedCall<*>, receiver: StackValue, returnType: Type, codegen: ExpressionCodegen): StackValue {
return StackValue.functionCall(returnType) {
codegen.invokeMethodWithArguments(this, resolvedCall, receiver, returnType)
}
}
}
public class UnaryIntrinsic(val callable: CallableMethod, val newReturnType: Type? = null, needPrimitiveCheck: Boolean = false, val newThisType: Type? = null, val invoke: UnaryIntrinsic.(v: InstructionAdapter) -> Unit) :
IntrinsicCallable(newReturnType ?: callable.returnType, callable.valueParameterTypes, newThisType ?: callable.thisType, callable.receiverType) {
{
init {
if (needPrimitiveCheck) {
assert(AsmUtil.isPrimitive(returnType)) { "Return type of UnaryPlus intrinsic should be of primitive type : " + returnType }
}
assert(valueParameterTypes.size == 0, "Unary operation should not have any parameters")
assert(valueParameterTypes.isEmpty(), "Unary operation should not have any parameters")
}
override fun invokeIntrinsic(v: InstructionAdapter) {