protected boolean tryAssignTypeArguments(List<LightweightTypeReference> typeArguments, FunctionTypeReference result) { for(LightweightTypeReference typeArgument: typeArguments) { result.addTypeArgument(typeArgument); LightweightTypeReference lowerBound = typeArgument.getLowerBoundSubstitute(); if (lowerBound instanceof AnyTypeReference) { return false; } result.addParameterType(lowerBound); } return true; } }
protected FunctionTypeReference getAsFunctionOrNull(ParameterizedTypeReference typeReference) { if (typeReference.isRawType()) return null; FunctionTypeReference functionType = new FunctionTypeReference(typeReference.getOwner(), typeReference.getType()); List<LightweightTypeReference> allTypeArguments = typeReference.getTypeArguments(); if (!tryAssignTypeArguments(allTypeArguments.subList(0, allTypeArguments.size() - 1), functionType)) return null; LightweightTypeReference lastTypeArgument = allTypeArguments.get(allTypeArguments.size() - 1); functionType.addTypeArgument(lastTypeArgument); LightweightTypeReference returnType = lastTypeArgument.getUpperBoundSubstitute(); functionType.setReturnType(returnType); return functionType; }
protected void prepareResultType() { resultClosureType = expectedClosureType.getOwner().newFunctionTypeReference(expectedClosureType.getType()); for (LightweightTypeReference argument : expectedClosureType.getTypeArguments()) { resultClosureType.addTypeArgument(argument); } }
protected void initClosureType(FunctionTypeReference result, boolean isProcedure) { ITypeReferenceOwner owner = result.getOwner(); TypeParameterByUnboundSubstitutor substitutor = new TypeParameterByUnboundSubstitutor(Collections.<JvmTypeParameter, LightweightMergedBoundTypeArgument>emptyMap(), owner) { @Override protected UnboundTypeReference createUnboundTypeReference(JvmTypeParameter type) { UnboundTypeReference result = getExpectation().createUnboundTypeReference(getClosure(), type); return result; } }; JvmGenericType type = (JvmGenericType) result.getType(); List<JvmTypeParameter> typeParameters = type.getTypeParameters(); int max = type.getTypeParameters().size(); if (!isProcedure) max--; for(int i = 0; i < max; i++) { JvmTypeParameter typeParameter = typeParameters.get(i); LightweightTypeReference parameterType = owner.newParameterizedTypeReference(typeParameter); LightweightTypeReference substituted = substitutor.substitute(parameterType); result.addTypeArgument(substituted); } if (!isProcedure) { JvmTypeParameter typeParameter = typeParameters.get(max); LightweightTypeReference parameterType = owner.toLightweightTypeReference(typeParameter); LightweightTypeReference substituted = substitutor.substitute(parameterType); result.addTypeArgument(substituted); } }
protected void enhanceFunctionType(FunctionTypeReference reference, FunctionTypeReference result, Visiting visiting) { for(LightweightTypeReference parameterType: reference.getParameterTypes()) { result.addParameterType(visitTypeArgument(parameterType, visiting, true)); } for(LightweightTypeReference typeArgument: reference.getTypeArguments()) { result.addTypeArgument(visitTypeArgument(typeArgument, visiting)); } LightweightTypeReference returnType = reference.getReturnType(); if (returnType != null) { result.setReturnType(visitTypeArgument(returnType, visiting)); } }
private FunctionTypeReference createFunctionTypeRef( ITypeReferenceOwner owner, LightweightTypeReference functionType, List<LightweightTypeReference> parameterTypes, /* @Nullable */ LightweightTypeReference returnType, /* @Nullable */ LightweightTypeReference outer) { JvmType type = functionType.getType(); if (type == null) throw new IllegalArgumentException("type may not be null"); FunctionTypeReference result; if (outer == null) { result = owner.newFunctionTypeReference(type); } else { result = owner.newFunctionTypeReference(outer, type); } if (functionType instanceof ParameterizedTypeReference) { for(LightweightTypeReference typeArgument: ((ParameterizedTypeReference) functionType).getTypeArguments()) { result.addTypeArgument(typeArgument.copyInto(owner)); } } for(LightweightTypeReference parameterType: parameterTypes) { result.addParameterType(parameterType.copyInto(owner)); } if (returnType != null) { result.setReturnType(returnType.copyInto(owner)); } return result; }
protected LocalTypeSubstitutor.SubstitutionResult doSubstituteFunctionType(FunctionTypeReference reference, FunctionTypeReference result, boolean didSubstitute) { for(LightweightTypeReference parameterType: reference.getParameterTypes()) { SubstitutionResult visited = visitTypeArgument(parameterType, VarianceInfo.IN); didSubstitute = didSubstitute || visited.didSubstitute; result.addParameterType(visited.typeReference); } for(LightweightTypeReference typeArgument: reference.getTypeArguments()) { SubstitutionResult visited = visitTypeArgument(typeArgument, VarianceInfo.INVARIANT); didSubstitute = didSubstitute || visited.didSubstitute; result.addTypeArgument(visited.typeReference); } LightweightTypeReference returnType = reference.getReturnType(); if (returnType != null) { SubstitutionResult visited = visitTypeArgument(returnType, VarianceInfo.OUT); didSubstitute = didSubstitute || visited.didSubstitute; result.setReturnType(visited.typeReference); } return new SubstitutionResult(result, didSubstitute); }
typeArgument.setLowerBound(parameterType); typeArgument.addUpperBound(this.getJavaLangObjectTypeReference()); result.addTypeArgument(typeArgument); result.addTypeArgument(typeArgument);
LightweightTypeReference typeParameterReference = owner.newParameterizedTypeReference(typeParameter); LightweightTypeReference substituted = substitutor.substitute(typeParameterReference); result.addTypeArgument(substituted);
@Override public LightweightTypeReference doVisitFunctionTypeReference(XFunctionTypeRef reference) { JvmTypeReference equivalent = reference.getEquivalent(); if (equivalent == null) { return super.doVisitFunctionTypeReference(reference); } FunctionTypeReference result; // constructors used below to avoid subsequent checks for isInner which was supposed to be done by // the computation of the equivalent if (equivalent.eClass() == TypesPackage.Literals.JVM_INNER_TYPE_REFERENCE) { JvmParameterizedTypeReference outer = ((JvmInnerTypeReference) equivalent).getOuter(); LightweightTypeReference outerEquivalent = outer.accept(this); result = new InnerFunctionTypeReference(owner, outerEquivalent, reference.getType()); } else { result = new FunctionTypeReference(owner, reference.getType()); } if (equivalent instanceof JvmParameterizedTypeReference) { for(JvmTypeReference argument: ((JvmParameterizedTypeReference)equivalent).getArguments()) { result.addTypeArgument(visit(argument)); } } for(JvmTypeReference parameter: reference.getParamTypes()) { result.addParameterType(visit(parameter)); } if (reference.getReturnType() != null) { result.setReturnType(visit(reference.getReturnType())); } return result; }