Extract static helpers to FunctionTypesUtil

Also move 'getInternalClassName' method from CodegenUtil to FunctionTypesUtil,
since it's related to function types
This commit is contained in:
Alexander Udalov
2013-04-15 16:04:04 +04:00
parent 121cb13883
commit f509407852
5 changed files with 102 additions and 67 deletions
@@ -35,7 +35,6 @@ import org.jetbrains.jet.lang.psi.JetClassObject;
import org.jetbrains.jet.lang.psi.JetClassOrObject;
import org.jetbrains.jet.lang.psi.JetObjectDeclaration;
import org.jetbrains.jet.lang.resolve.DescriptorUtils;
import org.jetbrains.jet.lang.resolve.java.JvmClassName;
import org.jetbrains.jet.lang.resolve.java.JvmStdlibNames;
import org.jetbrains.jet.lang.resolve.name.Name;
import org.jetbrains.jet.lang.types.JetType;
@@ -122,17 +121,6 @@ public class CodegenUtil {
return descriptor.getKind() == ClassKind.OBJECT ? JvmStdlibNames.FLAG_CLASS_KIND_OBJECT : JvmStdlibNames.FLAG_CLASS_KIND_DEFAULT;
}
@NotNull
public static JvmClassName getInternalClassName(FunctionDescriptor descriptor) {
int paramCount = descriptor.getValueParameters().size();
if (descriptor.getReceiverParameter() != null) {
return JvmClassName.byInternalName("jet/ExtensionFunctionImpl" + paramCount);
}
else {
return JvmClassName.byInternalName("jet/FunctionImpl" + paramCount);
}
}
public static JvmMethodSignature erasedInvokeSignature(FunctionDescriptor fd) {
BothSignatureWriter signatureWriter = new BothSignatureWriter(BothSignatureWriter.Mode.METHOD, false);
@@ -70,7 +70,7 @@ import java.util.*;
import static org.jetbrains.asm4.Opcodes.*;
import static org.jetbrains.jet.codegen.AsmUtil.*;
import static org.jetbrains.jet.codegen.CodegenUtil.*;
import static org.jetbrains.jet.codegen.CodegenUtil.isInterface;
import static org.jetbrains.jet.codegen.FunctionTypesUtil.getFunctionImplClassName;
import static org.jetbrains.jet.codegen.binding.CodegenBinding.*;
import static org.jetbrains.jet.lang.resolve.BindingContext.*;
import static org.jetbrains.jet.lang.resolve.BindingContextUtils.descriptorToDeclaration;
@@ -2786,7 +2786,7 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
private StackValue invokeOperation(JetOperationExpression expression, FunctionDescriptor op, CallableMethod callable) {
int functionLocalIndex = lookupLocalIndex(op);
if (functionLocalIndex >= 0) {
stackValueForLocal(op, functionLocalIndex).put(getInternalClassName(op).getAsmType(), v);
stackValueForLocal(op, functionLocalIndex).put(getFunctionImplClassName(op).getAsmType(), v);
}
ResolvedCall<? extends CallableDescriptor> resolvedCall =
bindingContext.get(BindingContext.RESOLVED_CALL, expression.getOperationReference());
@@ -0,0 +1,93 @@
/*
* Copyright 2010-2013 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.jet.codegen;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.descriptors.*;
import org.jetbrains.jet.lang.descriptors.impl.MutableClassDescriptor;
import org.jetbrains.jet.lang.resolve.java.JvmClassName;
import org.jetbrains.jet.lang.resolve.name.Name;
import org.jetbrains.jet.lang.resolve.scopes.JetScope;
import org.jetbrains.jet.lang.types.JetType;
import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns;
import java.util.ArrayList;
import java.util.List;
public class FunctionTypesUtil {
private static final List<ClassDescriptor> FUNCTIONS;
private static final List<ClassDescriptor> EXTENSION_FUNCTIONS;
static {
int n = KotlinBuiltIns.FUNCTION_TRAIT_COUNT;
FUNCTIONS = new ArrayList<ClassDescriptor>(n);
EXTENSION_FUNCTIONS = new ArrayList<ClassDescriptor>(n);
KotlinBuiltIns builtIns = KotlinBuiltIns.getInstance();
for (int i = 0; i < n; i++) {
Name functionImpl = Name.identifier("FunctionImpl" + i);
FUNCTIONS.add(createFunctionImplDescriptor(functionImpl, builtIns.getFunction(i)));
Name extensionFunctionImpl = Name.identifier("ExtensionFunctionImpl" + i);
EXTENSION_FUNCTIONS.add(createFunctionImplDescriptor(extensionFunctionImpl, builtIns.getExtensionFunction(i)));
}
}
private FunctionTypesUtil() {
}
@NotNull
public static JetType getSuperTypeForClosure(@NotNull FunctionDescriptor funDescriptor, int arity) {
if (funDescriptor.getReceiverParameter() != null) {
return EXTENSION_FUNCTIONS.get(arity).getDefaultType();
}
else {
return FUNCTIONS.get(arity).getDefaultType();
}
}
@NotNull
private static ClassDescriptor createFunctionImplDescriptor(@NotNull Name name, @NotNull ClassDescriptor functionInterface) {
JetScope builtInsScope = KotlinBuiltIns.getInstance().getBuiltInsScope();
MutableClassDescriptor functionImpl = new MutableClassDescriptor(
builtInsScope.getContainingDeclaration(),
builtInsScope,
ClassKind.CLASS,
false,
name
);
functionImpl.setModality(Modality.FINAL);
functionImpl.setVisibility(Visibilities.PUBLIC);
functionImpl.setTypeParameterDescriptors(functionInterface.getDefaultType().getConstructor().getParameters());
functionImpl.createTypeConstructor();
return functionImpl;
}
@NotNull
public static JvmClassName getFunctionImplClassName(@NotNull FunctionDescriptor descriptor) {
int paramCount = descriptor.getValueParameters().size();
if (descriptor.getReceiverParameter() != null) {
return JvmClassName.byInternalName("jet/ExtensionFunctionImpl" + paramCount);
}
else {
return JvmClassName.byInternalName("jet/FunctionImpl" + paramCount);
}
}
}
@@ -22,7 +22,6 @@ import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.descriptors.*;
import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor;
import org.jetbrains.jet.lang.descriptors.impl.ClassDescriptorImpl;
import org.jetbrains.jet.lang.descriptors.impl.MutableClassDescriptor;
import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.lang.resolve.BindingTrace;
@@ -34,12 +33,13 @@ import org.jetbrains.jet.lang.resolve.java.PackageClassUtils;
import org.jetbrains.jet.lang.resolve.name.FqName;
import org.jetbrains.jet.lang.resolve.name.Name;
import org.jetbrains.jet.lang.resolve.scopes.JetScope;
import org.jetbrains.jet.lang.types.JetType;
import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns;
import java.util.*;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import static org.jetbrains.jet.codegen.CodegenUtil.peekFromStack;
import static org.jetbrains.jet.codegen.FunctionTypesUtil.getSuperTypeForClosure;
import static org.jetbrains.jet.codegen.binding.CodegenBinding.*;
import static org.jetbrains.jet.lang.resolve.BindingContext.*;
@@ -56,42 +56,6 @@ class CodegenAnnotatingVisitor extends JetVisitorVoid {
this.bindingContext = bindingTrace.getBindingContext();
}
private static final List<ClassDescriptor> FUNCTIONS;
private static final List<ClassDescriptor> EXTENSION_FUNCTIONS;
static {
int n = KotlinBuiltIns.FUNCTION_TRAIT_COUNT;
FUNCTIONS = new ArrayList<ClassDescriptor>(n);
EXTENSION_FUNCTIONS = new ArrayList<ClassDescriptor>(n);
KotlinBuiltIns builtIns = KotlinBuiltIns.getInstance();
for (int i = 0; i < n; i++) {
Name functionImpl = Name.identifier("FunctionImpl" + i);
FUNCTIONS.add(createFunctionImplDescriptor(functionImpl, builtIns.getFunction(i)));
Name extensionFunctionImpl = Name.identifier("ExtensionFunctionImpl" + i);
EXTENSION_FUNCTIONS.add(createFunctionImplDescriptor(extensionFunctionImpl, builtIns.getExtensionFunction(i)));
}
}
private static ClassDescriptor createFunctionImplDescriptor(Name name, ClassDescriptor functionInterface) {
JetScope builtInsScope = KotlinBuiltIns.getInstance().getBuiltInsScope();
MutableClassDescriptor functionImpl = new MutableClassDescriptor(
builtInsScope.getContainingDeclaration(),
builtInsScope,
ClassKind.CLASS,
false,
name
);
functionImpl.setModality(Modality.FINAL);
functionImpl.setVisibility(Visibilities.PUBLIC);
functionImpl.setTypeParameterDescriptors(functionInterface.getDefaultType().getConstructor().getParameters());
functionImpl.createTypeConstructor();
return functionImpl;
}
@Override
public void visitCallExpression(JetCallExpression expression) {
super.visitCallExpression(expression);
@@ -139,15 +103,6 @@ class CodegenAnnotatingVisitor extends JetVisitorVoid {
return classDescriptor;
}
private JetType getSuperTypeForClosure(FunctionDescriptor funDescriptor, int arity) {
if (funDescriptor.getReceiverParameter() != null) {
return EXTENSION_FUNCTIONS.get(arity).getDefaultType();
}
else {
return FUNCTIONS.get(arity).getDefaultType();
}
}
private String inventAnonymousClassName(JetElement declaration) {
String top = peekFromStack(nameStack);
Integer cnt = anonymousSubclassesCount.get(top);
@@ -47,6 +47,7 @@ import static org.jetbrains.asm4.Opcodes.*;
import static org.jetbrains.jet.codegen.AsmUtil.boxType;
import static org.jetbrains.jet.codegen.AsmUtil.getTraitImplThisParameterType;
import static org.jetbrains.jet.codegen.CodegenUtil.*;
import static org.jetbrains.jet.codegen.FunctionTypesUtil.getFunctionImplClassName;
import static org.jetbrains.jet.codegen.binding.CodegenBinding.*;
public class JetTypeMapper extends BindingTraceAware {
@@ -937,7 +938,7 @@ public class JetTypeMapper extends BindingTraceAware {
public CallableMethod asCallableMethod(FunctionDescriptor fd) {
JvmMethodSignature descriptor = erasedInvokeSignature(fd);
JvmClassName owner = getInternalClassName(fd);
JvmClassName owner = getFunctionImplClassName(fd);
Type receiverParameterType;
ReceiverParameterDescriptor receiverParameter = fd.getOriginal().getReceiverParameter();
if (receiverParameter != null) {
@@ -946,8 +947,6 @@ public class JetTypeMapper extends BindingTraceAware {
else {
receiverParameterType = null;
}
return new CallableMethod(
owner, null, null, descriptor, INVOKEVIRTUAL,
getInternalClassName(fd), receiverParameterType, getInternalClassName(fd).getAsmType());
return new CallableMethod(owner, null, null, descriptor, INVOKEVIRTUAL, owner, receiverParameterType, owner.getAsmType());
}
}