@NotNull private static Multimap<FqName, Pair<FunctionDescriptor, PsiMethod>> getSuperclassToFunctionsMultimap( @NotNull PsiMethodWrapper method, @NotNull BindingContext bindingContext, @NotNull ClassDescriptor containingClass) { Multimap<FqName, Pair<FunctionDescriptor, PsiMethod>> result = HashMultimap.create(); Name functionName = Name.identifier(method.getName()); int parameterCount = method.getParameters().size(); for (JetType supertype : TypeUtils.getAllSupertypes(containingClass.getDefaultType())) { ClassifierDescriptor klass = supertype.getConstructor().getDeclarationDescriptor(); assert klass != null; FqName fqName = DescriptorUtils.getFQName(klass).toSafe(); for (FunctionDescriptor fun : klass.getDefaultType().getMemberScope().getFunctions(functionName)) { if (fun.getKind().isReal() && fun.getValueParameters().size() == parameterCount) { PsiElement declaration = BindingContextUtils.descriptorToDeclaration(bindingContext, fun); if (declaration instanceof PsiMethod) { result.put(fqName, Pair.create(fun, (PsiMethod) declaration)); } // else declaration is null or JetNamedFunction: both cases are processed later } } } return result; }
@NotNull public Collection<JetType> getSupertypesForClosure(@NotNull FunctionDescriptor descriptor) { ReceiverParameterDescriptor receiverParameter = descriptor.getReceiverParameter(); List<TypeProjection> typeArguments = new ArrayList<TypeProjection>(2); ClassDescriptor classDescriptor; if (receiverParameter != null) { classDescriptor = extensionFunctionImpl; typeArguments.add(new TypeProjectionImpl(receiverParameter.getType())); } else { classDescriptor = functionImpl; } //noinspection ConstantConditions typeArguments.add(new TypeProjectionImpl(descriptor.getReturnType())); JetType functionImplType = new JetTypeImpl( classDescriptor.getDefaultType().getAnnotations(), classDescriptor.getTypeConstructor(), false, typeArguments, classDescriptor.getMemberScope(typeArguments)); JetType functionType = KotlinBuiltIns.getInstance() .getFunctionType( Annotations.EMPTY, receiverParameter == null ? null : receiverParameter.getType(), DescriptorUtils.getValueParametersTypes(descriptor.getValueParameters()), descriptor.getReturnType()); return Arrays.asList(functionImplType, functionType); }
@NotNull public static JetScope getFunctionInnerScope( @NotNull JetScope outerScope, @NotNull FunctionDescriptor descriptor, @NotNull BindingTrace trace) { WritableScope parameterScope = new WritableScopeImpl( outerScope, descriptor, new TraceBasedRedeclarationHandler(trace), "Function inner scope"); ReceiverParameterDescriptor receiver = descriptor.getReceiverParameter(); if (receiver != null) { parameterScope.setImplicitReceiver(receiver); } for (TypeParameterDescriptor typeParameter : descriptor.getTypeParameters()) { parameterScope.addTypeParameterDescriptor(typeParameter); } for (ValueParameterDescriptor valueParameterDescriptor : descriptor.getValueParameters()) { parameterScope.addVariableDescriptor(valueParameterDescriptor); } parameterScope.addLabeledDeclaration(descriptor); parameterScope.changeLockLevel(WritableScope.LockLevel.READING); return parameterScope; }
/* FUNCTIONS */ private void renderFunction( @NotNull FunctionDescriptor function, @NotNull StringBuilder builder) { if (!startFromName) { renderAnnotations(function, builder); renderVisibility(function.getVisibility(), builder); renderModalityForCallable(function, builder); renderOverride(function, builder); renderMemberKind(function, builder); builder.append(renderKeyword("fun")).append(" "); renderTypeParameters(function.getTypeParameters(), builder, true); ReceiverParameterDescriptor receiver = function.getReceiverParameter(); if (receiver != null) { builder.append(escape(renderType(receiver.getType()))).append("."); } } renderName(function, builder); renderValueParameters(function, builder); JetType returnType = function.getReturnType(); if (unitReturnType || !KotlinBuiltIns.getInstance().isUnit(returnType)) { builder.append(": ").append(returnType == null ? "[NULL]" : escape(renderType(returnType))); } renderWhereSuffix(function.getTypeParameters(), builder); }
@NotNull public JetTypeInfo getSimpleNameExpressionTypeInfo( @NotNull JetSimpleNameExpression nameExpression, @NotNull ReceiverValue receiver, @Nullable ASTNode callOperationNode, @NotNull ResolutionContext context) { boolean[] result = new boolean[1]; TemporaryBindingTrace traceForVariable = TemporaryBindingTrace.create(context.trace, "trace to resolve as variable", nameExpression); JetType type = getVariableType( nameExpression, receiver, callOperationNode, context.replaceBindingTrace(traceForVariable), result); if (result[0]) { traceForVariable.commit(); if (type instanceof NamespaceType && context.expressionPosition == ExpressionPosition.FREE) { type = null; } return JetTypeInfo.create(type, context.dataFlowInfo); } Call call = CallMaker.makeCall( nameExpression, receiver, callOperationNode, nameExpression, Collections.<ValueArgument>emptyList()); TemporaryBindingTrace traceForFunction = TemporaryBindingTrace.create(context.trace, "trace to resolve as function", nameExpression); ResolvedCall<FunctionDescriptor> resolvedCall = getResolvedCallForFunction( call, nameExpression, receiver, context, ResolveMode.TOP_LEVEL_CALL, ResolutionResultsCache.create(), result); if (result[0]) { FunctionDescriptor functionDescriptor = resolvedCall != null ? resolvedCall.getResultingDescriptor() : null; traceForFunction.commit(); boolean hasValueParameters = functionDescriptor == null || functionDescriptor.getValueParameters().size() > 0; context.trace.report( FUNCTION_CALL_EXPECTED.on(nameExpression, nameExpression, hasValueParameters)); type = functionDescriptor != null ? functionDescriptor.getReturnType() : null; return JetTypeInfo.create(type, context.dataFlowInfo); } traceForVariable.commit(); return JetTypeInfo.create(null, context.dataFlowInfo); }
@NotNull private VarargCheckResult checkVarargInSuperFunctions( @NotNull ValueParameterDescriptor originalParam) { boolean someSupersVararg = false; boolean someSupersNotVararg = false; for (FunctionDescriptor superFunction : superFunctions) { if (superFunction.getValueParameters().get(originalParam.getIndex()).getVarargElementType() != null) { someSupersVararg = true; } else { someSupersNotVararg = true; } } JetType originalVarargElementType = originalParam.getVarargElementType(); JetType originalType = originalParam.getType(); if (someSupersVararg && someSupersNotVararg) { reportError("Incompatible super methods: some have vararg parameter, some have not"); return new VarargCheckResult(originalType, originalVarargElementType != null); } KotlinBuiltIns builtIns = KotlinBuiltIns.getInstance(); if (someSupersVararg && originalVarargElementType == null) { // convert to vararg assert isArrayType(originalType); if (builtIns.isPrimitiveArray(originalType)) { // replace IntArray? with IntArray return new VarargCheckResult(TypeUtils.makeNotNullable(originalType), true); } // replace Array<out Foo>? with Array<Foo> JetType varargElementType = builtIns.getArrayElementType(originalType); return new VarargCheckResult(builtIns.getArrayType(INVARIANT, varargElementType), true); } else if (someSupersNotVararg && originalVarargElementType != null) { // convert to non-vararg assert isArrayType(originalType); if (builtIns.isPrimitiveArray(originalType)) { // replace IntArray with IntArray? return new VarargCheckResult(TypeUtils.makeNullable(originalType), false); } // replace Array<Foo> with Array<out Foo>? return new VarargCheckResult( TypeUtils.makeNullable( builtIns.getArrayType(Variance.OUT_VARIANCE, originalVarargElementType)), false); } return new VarargCheckResult(originalType, originalVarargElementType != null); }
@NotNull private static FunctionDescriptor substituteSuperFunction( @NotNull Map<ClassDescriptor, JetType> superclassToSupertype, @NotNull FunctionDescriptor superFun) { DeclarationDescriptor superFunContainer = superFun.getContainingDeclaration(); assert superFunContainer instanceof ClassDescriptor : superFunContainer; JetType supertype = superclassToSupertype.get(superFunContainer); assert supertype != null : "Couldn't find super type for super function: " + superFun; TypeSubstitutor supertypeSubstitutor = TypeSubstitutor.create(supertype); FunctionDescriptor substitutedSuperFun = superFun.substitute(supertypeSubstitutor); assert substitutedSuperFun != null; return substitutedSuperFun; }
@Nullable public static List<ValueParameterDescriptor> getSubstitutedValueParameters( FunctionDescriptor substitutedDescriptor, @NotNull FunctionDescriptor functionDescriptor, @NotNull TypeSubstitutor substitutor) { List<ValueParameterDescriptor> result = new ArrayList<ValueParameterDescriptor>(); List<ValueParameterDescriptor> unsubstitutedValueParameters = functionDescriptor.getValueParameters(); for (int i = 0, unsubstitutedValueParametersSize = unsubstitutedValueParameters.size(); i < unsubstitutedValueParametersSize; i++) { ValueParameterDescriptor unsubstitutedValueParameter = unsubstitutedValueParameters.get(i); // TODO : Lazy? JetType substitutedType = substitutor.substitute(unsubstitutedValueParameter.getType(), Variance.IN_VARIANCE); JetType varargElementType = unsubstitutedValueParameter.getVarargElementType(); JetType substituteVarargElementType = varargElementType == null ? null : substitutor.substitute(varargElementType, Variance.IN_VARIANCE); if (substitutedType == null) return null; result.add( new ValueParameterDescriptorImpl( substitutedDescriptor, unsubstitutedValueParameter, unsubstitutedValueParameter.getAnnotations(), unsubstitutedValueParameter.isVar(), substitutedType, substituteVarargElementType)); } return result; }
public static Map<TypeConstructor, TypeProjection> createSubstitutionContext( @NotNull FunctionDescriptor functionDescriptor, List<JetType> typeArguments) { if (functionDescriptor.getTypeParameters().isEmpty()) return Collections.emptyMap(); Map<TypeConstructor, TypeProjection> result = new HashMap<TypeConstructor, TypeProjection>(); int typeArgumentsSize = typeArguments.size(); List<TypeParameterDescriptor> typeParameters = functionDescriptor.getTypeParameters(); assert typeArgumentsSize == typeParameters.size(); for (int i = 0; i < typeArgumentsSize; i++) { TypeParameterDescriptor typeParameterDescriptor = typeParameters.get(i); JetType typeArgument = typeArguments.get(i); result.put(typeParameterDescriptor.getTypeConstructor(), new TypeProjection(typeArgument)); } return result; }
@Nullable public static FunctionDescriptor substituteFunctionDescriptor( @NotNull List<JetType> typeArguments, @NotNull FunctionDescriptor functionDescriptor) { Map<TypeConstructor, TypeProjection> substitutionContext = createSubstitutionContext(functionDescriptor, typeArguments); return functionDescriptor.substitute(TypeSubstitutor.create(substitutionContext)); }
@NotNull public Collection<JetType> getSupertypesForCallableReference( @NotNull FunctionDescriptor descriptor) { ReceiverParameterDescriptor receiverParameter = descriptor.getReceiverParameter(); ReceiverParameterDescriptor expectedThisObject = descriptor.getExpectedThisObject(); List<TypeProjection> typeArguments = new ArrayList<TypeProjection>(2); ClassDescriptor classDescriptor; JetType receiverType; if (receiverParameter != null) { classDescriptor = kExtensionFunctionImpl; receiverType = receiverParameter.getType(); typeArguments.add(new TypeProjectionImpl(receiverType)); } else if (expectedThisObject != null) { classDescriptor = kMemberFunctionImpl; receiverType = expectedThisObject.getType(); typeArguments.add(new TypeProjectionImpl(receiverType)); } else { classDescriptor = kFunctionImpl; receiverType = null; } //noinspection ConstantConditions typeArguments.add(new TypeProjectionImpl(descriptor.getReturnType())); JetType kFunctionImplType = new JetTypeImpl( classDescriptor.getDefaultType().getAnnotations(), classDescriptor.getTypeConstructor(), false, typeArguments, classDescriptor.getMemberScope(typeArguments)); JetType kFunctionType = reflectionTypes.getKFunctionType( Annotations.EMPTY, receiverType, DescriptorUtils.getValueParametersTypes(descriptor.getValueParameters()), descriptor.getReturnType(), receiverParameter != null); return Arrays.asList(kFunctionImplType, kFunctionType); }
private void renderValueParameters( @NotNull FunctionDescriptor function, @NotNull StringBuilder builder) { handler.appendBeforeValueParameters(function, builder); for (ValueParameterDescriptor parameter : function.getValueParameters()) { handler.appendBeforeValueParameter(parameter, builder); renderValueParameter(parameter, builder, false); handler.appendAfterValueParameter(parameter, builder); } handler.appendAfterValueParameters(function, builder); }
private List<TypeParameterDescriptor> modifyTypeParametersAccordingToSuperMethods( List<TypeParameterDescriptor> autoTypeParameters) { List<TypeParameterDescriptor> result = Lists.newArrayList(); for (TypeParameterDescriptor autoParameter : autoTypeParameters) { int index = autoParameter.getIndex(); TypeParameterDescriptorImpl modifiedTypeParameter = autoTypeParameterToModified.get(autoParameter); List<Iterator<JetType>> upperBoundFromSuperFunctionsIterators = Lists.newArrayList(); for (FunctionDescriptor superFunction : superFunctions) { upperBoundFromSuperFunctionsIterators.add( superFunction.getTypeParameters().get(index).getUpperBounds().iterator()); } for (JetType autoUpperBound : autoParameter.getUpperBounds()) { List<TypeAndVariance> upperBoundsFromSuperFunctions = Lists.newArrayList(); for (Iterator<JetType> iterator : upperBoundFromSuperFunctionsIterators) { assert iterator.hasNext(); upperBoundsFromSuperFunctions.add(new TypeAndVariance(iterator.next(), INVARIANT)); } JetType modifiedUpperBound = modifyTypeAccordingToSuperMethods( autoUpperBound, upperBoundsFromSuperFunctions, UPPER_BOUND); modifiedTypeParameter.addUpperBound(modifiedUpperBound); } for (Iterator<JetType> iterator : upperBoundFromSuperFunctionsIterators) { assert !iterator.hasNext(); } modifiedTypeParameter.setInitialized(); result.add(modifiedTypeParameter); } return result; }
private void resolveFunctionBody( @NotNull BindingTrace trace, @NotNull JetDeclarationWithBody function, @NotNull FunctionDescriptor functionDescriptor, @NotNull JetScope declaringScope) { if (!context.completeAnalysisNeeded(function)) return; JetExpression bodyExpression = function.getBodyExpression(); JetScope functionInnerScope = FunctionDescriptorUtil.getFunctionInnerScope(declaringScope, functionDescriptor, trace); if (bodyExpression != null) { expressionTypingServices.checkFunctionReturnType( functionInnerScope, function, functionDescriptor, trace); } List<JetParameter> valueParameters = function.getValueParameters(); List<ValueParameterDescriptor> valueParameterDescriptors = functionDescriptor.getValueParameters(); checkDefaultParameterValues(valueParameters, valueParameterDescriptors, functionInnerScope); assert functionDescriptor.getReturnType() != null; }
@NotNull public TypeInfoForCall getCallExpressionTypeInfoForCallWithoutFinalTypeCheck( @NotNull JetCallExpression callExpression, @NotNull ReceiverValue receiver, @Nullable ASTNode callOperationNode, @NotNull ResolutionContext context, @NotNull ResolveMode resolveMode) { boolean[] result = new boolean[1]; Call call = CallMaker.makeCall(receiver, callOperationNode, callExpression); TemporaryBindingTrace traceForFunction = TemporaryBindingTrace.create( context.trace, "trace to resolve as function call", callExpression); ResolvedCall<FunctionDescriptor> resolvedCall = getResolvedCallForFunction( call, callExpression, receiver, context.replaceBindingTrace(traceForFunction), resolveMode, result); if (result[0]) { FunctionDescriptor functionDescriptor = resolvedCall != null ? resolvedCall.getResultingDescriptor() : null; traceForFunction.commit(); if (callExpression.getValueArgumentList() == null && callExpression.getFunctionLiteralArguments().isEmpty()) { // there are only type arguments boolean hasValueParameters = functionDescriptor == null || functionDescriptor.getValueParameters().size() > 0; context.trace.report( FUNCTION_CALL_EXPECTED.on(callExpression, callExpression, hasValueParameters)); } if (functionDescriptor == null) { return TypeInfoForCall.create(null, context.dataFlowInfo); } JetType type = functionDescriptor.getReturnType(); return TypeInfoForCall.create( type, resolvedCall.getDataFlowInfo(), resolvedCall, call, context, resolveMode); } JetExpression calleeExpression = callExpression.getCalleeExpression(); if (calleeExpression instanceof JetSimpleNameExpression && callExpression.getTypeArgumentList() == null) { TemporaryBindingTrace traceForVariable = TemporaryBindingTrace.create( context.trace, "trace to resolve as variable with 'invoke' call", callExpression); JetType type = getVariableType( (JetSimpleNameExpression) calleeExpression, receiver, callOperationNode, context.replaceBindingTrace(traceForVariable), result); if (result[0]) { traceForVariable.commit(); context.trace.report( FUNCTION_EXPECTED.on( (JetReferenceExpression) calleeExpression, calleeExpression, type != null ? type : ErrorUtils.createErrorType(""))); return TypeInfoForCall.create(null, context.dataFlowInfo); } } traceForFunction.commit(); return TypeInfoForCall.create(null, context.dataFlowInfo); }
@Nullable public static JetType getSubstitutedReturnType( @NotNull FunctionDescriptor functionDescriptor, TypeSubstitutor substitutor) { return substitutor.substitute(functionDescriptor.getReturnType(), Variance.OUT_VARIANCE); }