dummy implementation of method type parameters reader and refactoring

This commit is contained in:
Stepan Koltsov
2011-12-15 03:04:21 +04:00
parent 33967c32f7
commit cbe6dcc8c2
15 changed files with 178 additions and 60 deletions
@@ -200,6 +200,10 @@ public class BothSignatureWriter {
signatureVisitor().visitFormalTypeParameter(name); signatureVisitor().visitFormalTypeParameter(name);
jetSignatureWriter.visitFormalTypeParameter(name, translateVariance(variance)); jetSignatureWriter.visitFormalTypeParameter(name, translateVariance(variance));
} }
public void writeFormalTypeParameterEnd() {
jetSignatureWriter.visitFormalTypeParameterEnd();
}
public void writerFormalTypeParametersStart() { public void writerFormalTypeParametersStart() {
checkTopLevel(); checkTopLevel();
@@ -342,9 +346,9 @@ public class BothSignatureWriter {
// TODO: return null if not generic // TODO: return null if not generic
return signatureWriter.toString(); return signatureWriter.toString();
} }
@NotNull @NotNull
public List<String> makeKotlinSignatures() { public List<String> makeKotlinParameterTypes() {
checkState(State.METHOD_END); checkState(State.METHOD_END);
// TODO: return nulls if equal to #makeJavaString // TODO: return nulls if equal to #makeJavaString
return kotlinParameterTypes; return kotlinParameterTypes;
@@ -355,6 +359,11 @@ public class BothSignatureWriter {
checkState(State.METHOD_END); checkState(State.METHOD_END);
return kotlinReturnType; return kotlinReturnType;
} }
public String makeKotlinMethodTypeParameters() {
checkState(State.METHOD_END);
return kotlinClassParameters;
}
@Nullable @Nullable
public String makeKotlinClassSignature() { public String makeKotlinClassSignature() {
@@ -53,7 +53,7 @@ public class ClosureCodegen extends ObjectOrClosureCodegen {
public static CallableMethod asCallableMethod(FunctionDescriptor fd) { public static CallableMethod asCallableMethod(FunctionDescriptor fd) {
Method descriptor = erasedInvokeSignature(fd); Method descriptor = erasedInvokeSignature(fd);
String owner = getInternalClassName(fd); String owner = getInternalClassName(fd);
final CallableMethod result = new CallableMethod(owner, new JvmMethodSignature(descriptor, null, null, null), INVOKEVIRTUAL, Arrays.asList(descriptor.getArgumentTypes())); final CallableMethod result = new CallableMethod(owner, new JvmMethodSignature(descriptor, null, null, null, null), INVOKEVIRTUAL, Arrays.asList(descriptor.getArgumentTypes()));
if (fd.getReceiverParameter().exists()) { if (fd.getReceiverParameter().exists()) {
result.setNeedsReceiver(fd); result.setNeedsReceiver(fd);
} }
@@ -165,7 +165,7 @@ public class ClosureCodegen extends ObjectOrClosureCodegen {
final CodegenContext.ClosureContext closureContext = context.intoClosure(funDescriptor, function, name, this, state.getTypeMapper()); final CodegenContext.ClosureContext closureContext = context.intoClosure(funDescriptor, function, name, this, state.getTypeMapper());
FunctionCodegen fc = new FunctionCodegen(closureContext, cv, state); FunctionCodegen fc = new FunctionCodegen(closureContext, cv, state);
fc.generateMethod(body, new JvmMethodSignature(invokeSignature(funDescriptor), null, null, null), funDescriptor); fc.generateMethod(body, new JvmMethodSignature(invokeSignature(funDescriptor), null, null, null, null), funDescriptor);
return closureContext.outerWasUsed; return closureContext.outerWasUsed;
} }
@@ -93,6 +93,9 @@ public class FunctionCodegen {
if (jvmSignature.getKotlinReturnType() != null) { if (jvmSignature.getKotlinReturnType() != null) {
av.visit(StdlibNames.JET_METHOD_RETURN_TYPE_FIELD, jvmSignature.getKotlinReturnType()); av.visit(StdlibNames.JET_METHOD_RETURN_TYPE_FIELD, jvmSignature.getKotlinReturnType());
} }
if (jvmSignature.getKotlinTypeParameter() != null) {
av.visit(StdlibNames.JET_METHOD_TYPE_PARAMETERS_FIELD, jvmSignature.getKotlinTypeParameter());
}
av.visitEnd(); av.visitEnd();
} }
@@ -323,7 +323,7 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
} }
constructorMethod = new Method("<init>", Type.VOID_TYPE, parameterTypes.toArray(new Type[parameterTypes.size()])); constructorMethod = new Method("<init>", Type.VOID_TYPE, parameterTypes.toArray(new Type[parameterTypes.size()]));
callableMethod = new CallableMethod("", new JvmMethodSignature(constructorMethod, null, null, null) /* TODO */, Opcodes.INVOKESPECIAL, Collections.<Type>emptyList()); callableMethod = new CallableMethod("", new JvmMethodSignature(constructorMethod, null, null, null, null) /* TODO */, Opcodes.INVOKESPECIAL, Collections.<Type>emptyList());
} }
else { else {
callableMethod = typeMapper.mapToCallableMethod(constructorDescriptor, kind); callableMethod = typeMapper.mapToCallableMethod(constructorDescriptor, kind);
@@ -519,10 +519,12 @@ public class JetTypeMapper {
} }
Method method = new Method(f.getName(), returnType, parameterTypes.toArray(new Type[parameterTypes.size()])); Method method = new Method(f.getName(), returnType, parameterTypes.toArray(new Type[parameterTypes.size()]));
if (signatureVisitor == null) { if (signatureVisitor == null) {
return new JvmMethodSignature(method, null, null, null); return new JvmMethodSignature(method, null, null, null, null);
} else { } else {
return new JvmMethodSignature(method, signatureVisitor.makeJavaString(), return new JvmMethodSignature(method, signatureVisitor.makeJavaString(),
signatureVisitor.makeKotlinSignatures(), signatureVisitor.makeKotlinReturnTypeSignature()); signatureVisitor.makeKotlinMethodTypeParameters(),
signatureVisitor.makeKotlinParameterTypes(),
signatureVisitor.makeKotlinReturnTypeSignature());
} }
} }
@@ -568,6 +570,8 @@ public class JetTypeMapper {
} }
} }
} }
signatureVisitor.writeFormalTypeParameterEnd();
} }
@@ -583,7 +587,7 @@ public class JetTypeMapper {
} }
Type returnType = mapReturnType(f.getReturnType()); Type returnType = mapReturnType(f.getReturnType());
// TODO: proper generic signature // TODO: proper generic signature
return new JvmMethodSignature(new Method(name, returnType, parameterTypes.toArray(new Type[parameterTypes.size()])), null, null, null); return new JvmMethodSignature(new Method(name, returnType, parameterTypes.toArray(new Type[parameterTypes.size()])), null, null, null, null);
} }
@@ -608,7 +612,7 @@ public class JetTypeMapper {
} }
// TODO: proper generic signature // TODO: proper generic signature
return new JvmMethodSignature(new Method(name, returnType, params.toArray(new Type[params.size()])), null, null, null); return new JvmMethodSignature(new Method(name, returnType, params.toArray(new Type[params.size()])), null, null, null, null);
} }
@Nullable @Nullable
@@ -638,7 +642,7 @@ public class JetTypeMapper {
params.add(mapType(inType)); params.add(mapType(inType));
// TODO: proper generic signature // TODO: proper generic signature
return new JvmMethodSignature(new Method(name, Type.VOID_TYPE, params.toArray(new Type[params.size()])), null, null, null); return new JvmMethodSignature(new Method(name, Type.VOID_TYPE, params.toArray(new Type[params.size()])), null, null, null, null);
} }
private JvmMethodSignature mapConstructorSignature(ConstructorDescriptor descriptor, List<Type> valueParameterTypes) { private JvmMethodSignature mapConstructorSignature(ConstructorDescriptor descriptor, List<Type> valueParameterTypes) {
@@ -660,7 +664,7 @@ public class JetTypeMapper {
} }
Method method = new Method("<init>", Type.VOID_TYPE, parameterTypes.toArray(new Type[parameterTypes.size()])); Method method = new Method("<init>", Type.VOID_TYPE, parameterTypes.toArray(new Type[parameterTypes.size()]));
return new JvmMethodSignature(method, null, null, null); // TODO: generics signature return new JvmMethodSignature(method, null, null, null, null); // TODO: generics signature
} }
public CallableMethod mapToCallableMethod(ConstructorDescriptor descriptor, OwnerKind kind) { public CallableMethod mapToCallableMethod(ConstructorDescriptor descriptor, OwnerKind kind) {
@@ -14,14 +14,15 @@ public class JvmMethodSignature {
private final Method asmMethod; private final Method asmMethod;
/** Null when we don't care about type parameters */ /** Null when we don't care about type parameters */
private final String genericsSignature; private final String genericsSignature;
// TODO: type parameters private final String kotlinTypeParameter;
private final List<String> kotlinParameterTypes; private final List<String> kotlinParameterTypes;
private final String kotlinReturnType; private final String kotlinReturnType;
public JvmMethodSignature(@NotNull Method asmMethod, @Nullable String genericsSignature, public JvmMethodSignature(@NotNull Method asmMethod, @Nullable String genericsSignature,
@Nullable List<String> kotlinParameterTypes, @Nullable String kotlinReturnType) { @Nullable String kotlinTypeParameters, @Nullable List<String> kotlinParameterTypes, @Nullable String kotlinReturnType) {
this.asmMethod = asmMethod; this.asmMethod = asmMethod;
this.genericsSignature = genericsSignature; this.genericsSignature = genericsSignature;
this.kotlinTypeParameter = kotlinTypeParameters;
this.kotlinParameterTypes = kotlinParameterTypes; this.kotlinParameterTypes = kotlinParameterTypes;
this.kotlinReturnType = kotlinReturnType; this.kotlinReturnType = kotlinReturnType;
} }
@@ -34,6 +35,10 @@ public class JvmMethodSignature {
return genericsSignature; return genericsSignature;
} }
public String getKotlinTypeParameter() {
return kotlinTypeParameter;
}
public List<String> getKotlinParameterTypes() { public List<String> getKotlinParameterTypes() {
return kotlinParameterTypes; return kotlinParameterTypes;
} }
@@ -8,6 +8,7 @@ import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.*; import com.intellij.psi.*;
import com.intellij.psi.search.DelegatingGlobalSearchScope; import com.intellij.psi.search.DelegatingGlobalSearchScope;
import com.intellij.psi.search.GlobalSearchScope; import com.intellij.psi.search.GlobalSearchScope;
import jet.typeinfo.TypeInfoVariance;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.descriptors.*; import org.jetbrains.jet.lang.descriptors.*;
@@ -16,6 +17,9 @@ import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.lang.resolve.DescriptorUtils; import org.jetbrains.jet.lang.resolve.DescriptorUtils;
import org.jetbrains.jet.lang.types.*; import org.jetbrains.jet.lang.types.*;
import org.jetbrains.jet.plugin.JetFileType; import org.jetbrains.jet.plugin.JetFileType;
import org.jetbrains.jet.rt.signature.JetSignatureExceptionsAdapter;
import org.jetbrains.jet.rt.signature.JetSignatureReader;
import org.jetbrains.jet.rt.signature.JetSignatureVisitor;
import java.util.*; import java.util.*;
@@ -503,8 +507,7 @@ public class JavaDescriptorResolver {
method.getName() method.getName()
); );
methodDescriptorCache.put(method, functionDescriptorImpl); methodDescriptorCache.put(method, functionDescriptorImpl);
List<TypeParameterDescriptor> typeParameters = makeUninitializedTypeParameters(functionDescriptorImpl, method.getTypeParameters()); List<TypeParameterDescriptor> typeParameters = resolveMethodTypeParameters(method, functionDescriptorImpl);
initializeTypeParameters(method);
functionDescriptorImpl.initialize( functionDescriptorImpl.initialize(
null, null,
DescriptorUtils.getExpectedThisObjectIfNeeded(classDescriptor), DescriptorUtils.getExpectedThisObjectIfNeeded(classDescriptor),
@@ -522,6 +525,70 @@ public class JavaDescriptorResolver {
return substitutedFunctionDescriptor; return substitutedFunctionDescriptor;
} }
private List<TypeParameterDescriptor> resolveMethodTypeParameters(PsiMethod method, FunctionDescriptorImpl functionDescriptorImpl) {
for (PsiAnnotation annotation : method.getModifierList().getAnnotations()) {
if (annotation.getQualifiedName().equals(StdlibNames.JET_METHOD_CLASS)) {
PsiLiteralExpression attributeValue = (PsiLiteralExpression) annotation.findAttributeValue(StdlibNames.JET_METHOD_TYPE_PARAMETERS_FIELD);
if (attributeValue != null) {
String typeParametersString = (String) attributeValue.getValue();
if (typeParametersString != null) {
return resolveMethodTypeParametersFromJetSignature(typeParametersString, functionDescriptorImpl);
}
}
}
}
List<TypeParameterDescriptor> typeParameters = makeUninitializedTypeParameters(functionDescriptorImpl, method.getTypeParameters());
initializeTypeParameters(method);
return typeParameters;
}
private List<TypeParameterDescriptor> resolveMethodTypeParametersFromJetSignature(String jetSignature, final FunctionDescriptor functionDescriptor) {
final List<TypeParameterDescriptor> r = new ArrayList<TypeParameterDescriptor>();
new JetSignatureReader(jetSignature).acceptFormalTypeParametersOnly(new JetSignatureExceptionsAdapter() {
@Override
public JetSignatureVisitor visitFormalTypeParameter(final String name, TypeInfoVariance variance) {
return new JetSignatureExceptionsAdapter() {
int index = 0;
@Override
public JetSignatureVisitor visitClassBound() {
return new JetTypeJetSignatureReader(JavaDescriptorResolver.this, semanticServices.getJetSemanticServices().getStandardLibrary()) {
@Override
protected void done(@NotNull JetType jetType) {
// TODO
}
};
}
@Override
public JetSignatureVisitor visitInterfaceBound() {
return new JetTypeJetSignatureReader(JavaDescriptorResolver.this, semanticServices.getJetSemanticServices().getStandardLibrary()) {
@Override
protected void done(@NotNull JetType jetType) {
// TODO
}
};
}
@Override
public void visitFormalTypeParameterEnd() {
TypeParameterDescriptor typeParameter = TypeParameterDescriptor.createForFurtherModification(
functionDescriptor,
Collections.<AnnotationDescriptor>emptyList(), // TODO: wrong
true, // TODO: wrong
Variance.INVARIANT, // TOOD: wrong
name,
++index);
r.add(typeParameter);
}
};
}
});
return r;
}
private JetType makeReturnType(PsiType returnType, PsiMethod method) { private JetType makeReturnType(PsiType returnType, PsiMethod method) {
boolean changeNullable = false; boolean changeNullable = false;
boolean nullable = true; boolean nullable = true;
@@ -27,7 +27,8 @@ public class StdlibNames {
public static final String JET_METHOD_NULLABLE_RETURN_TYPE_FIELD = "nullableReturnType"; public static final String JET_METHOD_NULLABLE_RETURN_TYPE_FIELD = "nullableReturnType";
public static final String JET_METHOD_RETURN_TYPE_FIELD = "returnType"; public static final String JET_METHOD_RETURN_TYPE_FIELD = "returnType";
public static final String JET_METHOD_TYPE_PARAMETERS_FIELD = "typeParameters";
public static final String JET_OBJECT_INTERNAL = "jet/JetObject"; public static final String JET_OBJECT_INTERNAL = "jet/JetObject";
public static final String JET_OBJECT_CLASS = "jet.JetObject"; public static final String JET_OBJECT_CLASS = "jet.JetObject";
+6
View File
@@ -22,6 +22,12 @@ public @interface JetMethod {
*/ */
JetTypeProjection[] returnTypeProjections() default {}; JetTypeProjection[] returnTypeProjections() default {};
/**
* Serialized method type parameters.
* @return
*/
String typeParameters() default "";
/** /**
* @return is this type returnTypeNullable * @return is this type returnTypeNullable
*/ */
@@ -71,52 +71,56 @@ class TypeInfoParser {
signature.varNames = new HashMap<String, Integer>(); signature.varNames = new HashMap<String, Integer>();
signature.superTypes = new ArrayList<TypeInfo>(); signature.superTypes = new ArrayList<TypeInfo>();
new JetSignatureReader(annotationValue).accept(new JetSignatureExceptionsAdapter() { new JetSignatureReader(annotationValue).accept(new JetSignatureExceptionsAdapter() {
int varIndex = 0;
@Override @Override
public void visitFormalTypeParameter(String name, final TypeInfoVariance variance) { public JetSignatureVisitor visitFormalTypeParameter(final String name, final TypeInfoVariance variance) {
final TypeInfoVar typeInfoVar = new TypeInfoVar(signature, signature.variables.size());
signature.varNames.put(name, signature.variables.size());
TypeInfoProjection typeInfoProjection = new TypeInfoProjection() {
@Override
public TypeInfoVariance getVariance() {
return variance;
}
@Override
public TypeInfo getType() {
return typeInfoVar;
}
@Override
public String toString() {
return typeInfoVar.toString();
}
};
signature.variables.add(typeInfoProjection);
// TODO: supers
// TODO: nullability // TODO: nullability
}
@Override return new JetSignatureExceptionsAdapter() {
public JetSignatureVisitor visitClassBound() {
return new SignatureParserJetSignatureAdapter(signature.klazz.getClassLoader(), signature) {
@Override @Override
protected void done(TypeInfo typeInfo) { public JetSignatureVisitor visitClassBound() {
// TODO return new SignatureParserJetSignatureAdapter(signature.klazz.getClassLoader(), signature) {
@Override
protected void done(TypeInfo typeInfo) {
// TODO
}
};
} }
};
}
@Override
public JetSignatureVisitor visitInterfaceBound() {
return new SignatureParserJetSignatureAdapter(signature.klazz.getClassLoader(), signature) {
@Override @Override
protected void done(TypeInfo typeInfo) { public JetSignatureVisitor visitInterfaceBound() {
// TODO return new SignatureParserJetSignatureAdapter(signature.klazz.getClassLoader(), signature) {
@Override
protected void done(TypeInfo typeInfo) {
// TODO
}
};
}
@Override
public void visitFormalTypeParameterEnd() {
final TypeInfoVar typeInfoVar = new TypeInfoVar(signature, signature.variables.size());
signature.varNames.put(name, signature.variables.size());
TypeInfoProjection typeInfoProjection = new TypeInfoProjection() {
@Override
public TypeInfoVariance getVariance() {
return variance;
}
@Override
public TypeInfo getType() {
return typeInfoVar;
}
@Override
public String toString() {
return typeInfoVar.toString();
}
};
signature.variables.add(typeInfoProjection);
} }
}; };
} }
@@ -7,7 +7,12 @@ import jet.typeinfo.TypeInfoVariance;
*/ */
public class JetSignatureAdapter implements JetSignatureVisitor { public class JetSignatureAdapter implements JetSignatureVisitor {
@Override @Override
public void visitFormalTypeParameter(String name, TypeInfoVariance variance) { public JetSignatureVisitor visitFormalTypeParameter(String name, TypeInfoVariance variance) {
return this;
}
@Override
public void visitFormalTypeParameterEnd() {
} }
@Override @Override
@@ -7,7 +7,12 @@ import jet.typeinfo.TypeInfoVariance;
*/ */
public class JetSignatureExceptionsAdapter implements JetSignatureVisitor { public class JetSignatureExceptionsAdapter implements JetSignatureVisitor {
@Override @Override
public void visitFormalTypeParameter(String name, TypeInfoVariance variance) { public JetSignatureVisitor visitFormalTypeParameter(String name, TypeInfoVariance variance) {
throw new IllegalStateException();
}
@Override
public void visitFormalTypeParameterEnd() {
throw new IllegalStateException(); throw new IllegalStateException();
} }
@@ -59,17 +59,19 @@ public class JetSignatureReader {
if (end < 0) { if (end < 0) {
throw new IllegalStateException(); throw new IllegalStateException();
} }
v.visitFormalTypeParameter(signature.substring(pos, end), variance); JetSignatureVisitor parameterVisitor = v.visitFormalTypeParameter(signature.substring(pos, end), variance);
pos = end + 1; pos = end + 1;
c = signature.charAt(pos); c = signature.charAt(pos);
if (c == 'L' || c == '[' || c == 'T' || c == '?') { if (c == 'L' || c == '[' || c == 'T' || c == '?') {
pos = parseType(signature, pos, v.visitClassBound()); pos = parseType(signature, pos, parameterVisitor.visitClassBound());
} }
while ((c = signature.charAt(pos++)) == ':') { while ((c = signature.charAt(pos++)) == ':') {
pos = parseType(signature, pos, v.visitInterfaceBound()); pos = parseType(signature, pos, parameterVisitor.visitInterfaceBound());
} }
parameterVisitor.visitFormalTypeParameterEnd();
} while (c != '>'); } while (c != '>');
} else { } else {
pos = 0; pos = 0;
@@ -30,7 +30,9 @@ public interface JetSignatureVisitor {
* *
* @param name the name of the formal parameter. * @param name the name of the formal parameter.
*/ */
void visitFormalTypeParameter(String name, TypeInfoVariance variance); JetSignatureVisitor visitFormalTypeParameter(String name, TypeInfoVariance variance);
void visitFormalTypeParameterEnd();
/** /**
* Visits the class bound of the last visited formal type parameter. * Visits the class bound of the last visited formal type parameter.
@@ -43,7 +43,7 @@ public class JetSignatureWriter implements JetSignatureVisitor {
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
@Override @Override
public void visitFormalTypeParameter(final String name, TypeInfoVariance variance) { public JetSignatureVisitor visitFormalTypeParameter(final String name, TypeInfoVariance variance) {
if (!hasFormals) { if (!hasFormals) {
hasFormals = true; hasFormals = true;
buf.append('<'); buf.append('<');
@@ -62,6 +62,11 @@ public class JetSignatureWriter implements JetSignatureVisitor {
} }
buf.append(name); buf.append(name);
buf.append(':'); buf.append(':');
return this;
}
@Override
public void visitFormalTypeParameterEnd() {
} }
@Override @Override