@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); }
/* 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); }
public void serialize(FunctionDescriptor fun) { serialize(fun.getModality()); sb.append(" "); if (!fun.getAnnotations().isEmpty()) { new Serializer(sb).serializeSeparated(fun.getAnnotations(), " "); sb.append(" "); } if (!fun.getOverriddenDescriptors().isEmpty()) { sb.append("override /*" + fun.getOverriddenDescriptors().size() + "*/ "); } if (fun instanceof ConstructorDescriptor) { sb.append("/*constructor*/ "); } sb.append("fun "); if (!fun.getTypeParameters().isEmpty()) { sb.append("<"); new Serializer(sb).serializeCommaSeparated(fun.getTypeParameters()); sb.append(">"); } if (fun.getReceiverParameter().exists()) { new TypeSerializer(sb).serialize(fun.getReceiverParameter()); sb.append("."); } sb.append(fun.getName()); sb.append("("); new TypeSerializer(sb).serializeCommaSeparated(fun.getValueParameters()); sb.append("): "); new TypeSerializer(sb).serialize(fun.getReturnType()); }
@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 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); }
public void resolveFunctionBody( @NotNull DataFlowInfo outerDataFlowInfo, @NotNull BindingTrace trace, @NotNull JetDeclarationWithBody function, @NotNull FunctionDescriptor functionDescriptor, @NotNull LexicalScope declaringScope) { computeDeferredType(functionDescriptor.getReturnType()); resolveFunctionBody( outerDataFlowInfo, trace, function, functionDescriptor, declaringScope, null, CallChecker.DoNothing.INSTANCE$); assert functionDescriptor.getReturnType() != null; }
@NotNull private static KotlinType getFunctionExpectedReturnType( @NotNull FunctionDescriptor descriptor, @NotNull KtElement function, @NotNull ExpressionTypingContext context) { KotlinType expectedType; if (function instanceof KtSecondaryConstructor) { expectedType = DescriptorUtilsKt.getBuiltIns(descriptor).getUnitType(); } else if (function instanceof KtFunction) { KtFunction ktFunction = (KtFunction) function; expectedType = context.trace.get(EXPECTED_RETURN_TYPE, ktFunction); if ((expectedType == null) && (ktFunction.getTypeReference() != null || ktFunction.hasBlockBody())) { expectedType = descriptor.getReturnType(); } } else { expectedType = descriptor.getReturnType(); } return expectedType != null ? expectedType : TypeUtils.NO_EXPECTED_TYPE; }
@Override protected void generateBody() { FunctionDescriptor erasedInterfaceFunction; if (samType == null) { erasedInterfaceFunction = getErasedInvokeFunction(funDescriptor); } else { erasedInterfaceFunction = samType.getAbstractMethod().getOriginal(); } generateBridge( typeMapper.mapSignature(erasedInterfaceFunction).getAsmMethod(), typeMapper.mapSignature(funDescriptor).getAsmMethod()); functionCodegen.generateMethod(OtherOrigin(element, funDescriptor), funDescriptor, strategy); // TODO: rewrite cause ugly hack if (samType != null) { SimpleFunctionDescriptorImpl descriptorForBridges = SimpleFunctionDescriptorImpl.create( funDescriptor.getContainingDeclaration(), funDescriptor.getAnnotations(), erasedInterfaceFunction.getName(), CallableMemberDescriptor.Kind.DECLARATION, funDescriptor.getSource()); descriptorForBridges.initialize( null, erasedInterfaceFunction.getDispatchReceiverParameter(), erasedInterfaceFunction.getTypeParameters(), erasedInterfaceFunction.getValueParameters(), erasedInterfaceFunction.getReturnType(), Modality.OPEN, erasedInterfaceFunction.getVisibility()); descriptorForBridges.addOverriddenDescriptor(erasedInterfaceFunction); functionCodegen.generateBridges(descriptorForBridges); } this.constructor = generateConstructor(superClassAsmType); if (isConst(closure)) { generateConstInstance(); } genClosureFields(closure, v, typeMapper); functionCodegen.generateDefaultIfNeeded( context.intoFunction(funDescriptor), funDescriptor, context.getContextKind(), DefaultParameterValueLoader.DEFAULT, null); }
@NotNull public Collection<KotlinType> getSupertypesForClosure(@NotNull FunctionDescriptor descriptor) { ReceiverParameterDescriptor receiverParameter = descriptor.getExtensionReceiverParameter(); //noinspection ConstantConditions KotlinType functionType = DescriptorUtilsKt.getBuiltIns(descriptor) .getFunctionType( Annotations.Companion.getEMPTY(), receiverParameter == null ? null : receiverParameter.getType(), ExpressionTypingUtils.getValueParametersTypes(descriptor.getValueParameters()), descriptor.getReturnType()); return Arrays.asList(lambda.getDefaultType(), functionType); }
@NotNull private static JetType getFunctionTypeForSamType(@NotNull JetType samType) { FunctionDescriptor function = getAbstractMethodOfSamType(samType); JetType returnType = function.getReturnType(); assert returnType != null : "function is not initialized: " + function; List<JetType> parameterTypes = Lists.newArrayList(); for (ValueParameterDescriptor parameter : function.getValueParameters()) { parameterTypes.add(parameter.getType()); } JetType functionType = KotlinBuiltIns.getInstance() .getFunctionType( Collections.<AnnotationDescriptor>emptyList(), null, parameterTypes, returnType); return TypeUtils.makeNullableAsSpecified(functionType, samType.isNullable()); }
@Override public Void visitFunctionDescriptor(FunctionDescriptor descriptor, StringBuilder builder) { renderVisibility(descriptor.getVisibility(), builder); renderModality(descriptor.getModality(), builder); builder.append(renderKeyword("fun")).append(" "); if (renderTypeParameters(descriptor.getTypeParameters(), builder)) { builder.append(" "); } ReceiverDescriptor receiver = descriptor.getReceiverParameter(); if (receiver.exists()) { builder.append(escape(renderType(receiver.getType()))).append("."); } renderName(descriptor, builder); renderValueParameters(descriptor, builder); builder.append(" : ").append(escape(renderType(descriptor.getReturnType()))); renderWhereSuffix(descriptor, builder); return null; }
@NotNull public Collection<KotlinType> getSupertypesForFunctionReference( @NotNull FunctionDescriptor descriptor) { ReceiverParameterDescriptor extensionReceiver = descriptor.getExtensionReceiverParameter(); ReceiverParameterDescriptor dispatchReceiver = descriptor.getDispatchReceiverParameter(); KotlinType receiverType = extensionReceiver != null ? extensionReceiver.getType() : dispatchReceiver != null ? dispatchReceiver.getType() : null; //noinspection ConstantConditions KotlinType functionType = DescriptorUtilsKt.getBuiltIns(descriptor) .getFunctionType( Annotations.Companion.getEMPTY(), receiverType, ExpressionTypingUtils.getValueParametersTypes(descriptor.getValueParameters()), descriptor.getReturnType()); return Arrays.asList(functionReference.getDefaultType(), functionType); }
public static SimpleFunctionDescriptor createInvoke(FunctionDescriptor fd) { int arity = fd.getValueParameters().size(); SimpleFunctionDescriptorImpl invokeDescriptor = new SimpleFunctionDescriptorImpl( fd.getExpectedThisObject().exists() ? JetStandardClasses.getReceiverFunction(arity) : JetStandardClasses.getFunction(arity), Collections.<AnnotationDescriptor>emptyList(), Name.identifier("invoke"), CallableMemberDescriptor.Kind.DECLARATION); invokeDescriptor.initialize( fd.getReceiverParameter().exists() ? fd.getReceiverParameter().getType() : null, fd.getExpectedThisObject(), Collections.<TypeParameterDescriptorImpl>emptyList(), fd.getValueParameters(), fd.getReturnType(), Modality.FINAL, Visibilities.PUBLIC, /*isInline = */ false); return invokeDescriptor; }
public static boolean containsErrorType(@NotNull FunctionDescriptor function) { if (containsErrorType(function.getReturnType())) { return true; } ReceiverParameterDescriptor receiverParameter = function.getReceiverParameter(); if (receiverParameter != null && containsErrorType(receiverParameter.getType())) { return true; } for (ValueParameterDescriptor parameter : function.getValueParameters()) { if (containsErrorType(parameter.getType())) { return true; } } for (TypeParameterDescriptor parameter : function.getTypeParameters()) { for (JetType upperBound : parameter.getUpperBounds()) { if (containsErrorType(upperBound)) { return true; } } } return false; }
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; }
public AccessorForFunctionDescriptor( @NotNull FunctionDescriptor descriptor, @NotNull DeclarationDescriptor containingDeclaration, int index) { super( containingDeclaration, Annotations.EMPTY, Name.identifier( (descriptor instanceof ConstructorDescriptor ? "$init" : descriptor.getName()) + "$b$" + index), Kind.DECLARATION); initialize( DescriptorUtils.getReceiverParameterType(descriptor.getReceiverParameter()), descriptor instanceof ConstructorDescriptor ? NO_RECEIVER_PARAMETER : descriptor.getExpectedThisObject(), Collections.<TypeParameterDescriptor>emptyList(), copyValueParameters(descriptor), descriptor.getReturnType(), Modality.FINAL, Visibilities.INTERNAL); }
@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); }
@NotNull public static LookupElement createLookupElement( @NotNull KotlinCodeAnalyzer analyzer, @NotNull DeclarationDescriptor descriptor, @Nullable PsiElement declaration) { if (declaration != null) { MutableLookupElement javaLookupElement = createJavaLookupElementIfPossible(declaration); if (javaLookupElement != null) { InsertHandler<LookupElement> customHandler = getInsertHandler(descriptor); if (customHandler != null) { return javaLookupElement.setInsertHandler(getInsertHandler(descriptor)); } else { return javaLookupElement; } } } LookupElementBuilder element = LookupElementBuilder.create( new JetLookupObject(descriptor, analyzer, declaration), descriptor.getName().getName()); String presentableText = descriptor.getName().getName(); String typeText = ""; String tailText = ""; boolean tailTextGrayed = true; if (descriptor instanceof FunctionDescriptor) { FunctionDescriptor functionDescriptor = (FunctionDescriptor) descriptor; JetType returnType = functionDescriptor.getReturnType(); typeText = DescriptorRenderer.TEXT.renderType(returnType); presentableText += DescriptorRenderer.TEXT.renderFunctionParameters(functionDescriptor); boolean extensionFunction = functionDescriptor.getReceiverParameter() != null; DeclarationDescriptor containingDeclaration = descriptor.getContainingDeclaration(); if (containingDeclaration != null && extensionFunction) { tailText += " for " + DescriptorRenderer.TEXT.renderType( functionDescriptor.getReceiverParameter().getType()); tailText += " in " + DescriptorUtils.getFQName(containingDeclaration); } } else if (descriptor instanceof VariableDescriptor) { JetType outType = ((VariableDescriptor) descriptor).getType(); typeText = DescriptorRenderer.TEXT.renderType(outType); } else if (descriptor instanceof ClassDescriptor) { DeclarationDescriptor declaredIn = descriptor.getContainingDeclaration(); assert declaredIn != null; tailText = " (" + DescriptorUtils.getFQName(declaredIn) + ")"; tailTextGrayed = true; } else { typeText = DescriptorRenderer.TEXT.render(descriptor); } element = element.withInsertHandler(getInsertHandler(descriptor)); element = element .withTailText(tailText, tailTextGrayed) .withTypeText(typeText) .withPresentableText(presentableText); element = element.withIcon( JetDescriptorIconProvider.getIcon(descriptor, Iconable.ICON_FLAG_VISIBILITY)); element = element.withStrikeoutness(KotlinBuiltIns.getInstance().isDeprecated(descriptor)); return element; }
@Nullable public static JetType getSubstitutedReturnType( @NotNull FunctionDescriptor functionDescriptor, TypeSubstitutor substitutor) { return substitutor.substitute(functionDescriptor.getReturnType(), Variance.OUT_VARIANCE); }
public void resolveFunctionBody( @NotNull DataFlowInfo outerDataFlowInfo, @NotNull BindingTrace trace, @NotNull JetDeclarationWithBody function, @NotNull FunctionDescriptor functionDescriptor, @NotNull LexicalScope scope, @Nullable Function1<LexicalScope, DataFlowInfo> beforeBlockBody, @NotNull CallChecker callChecker) { LexicalScope innerScope = FunctionDescriptorUtil.getFunctionInnerScope(scope, functionDescriptor, trace); List<JetParameter> valueParameters = function.getValueParameters(); List<ValueParameterDescriptor> valueParameterDescriptors = functionDescriptor.getValueParameters(); valueParameterResolver.resolveValueParameters( valueParameters, valueParameterDescriptors, ExpressionTypingContext.newContext( trace, innerScope, outerDataFlowInfo, NO_EXPECTED_TYPE, callChecker)); // Synthetic "field" creation if (functionDescriptor instanceof PropertyAccessorDescriptor) { PropertyAccessorDescriptor accessorDescriptor = (PropertyAccessorDescriptor) functionDescriptor; JetProperty property = (JetProperty) function.getParent(); final SyntheticFieldDescriptor fieldDescriptor = new SyntheticFieldDescriptor(accessorDescriptor, property); innerScope = new LexicalScopeImpl( innerScope, functionDescriptor, true, functionDescriptor.getExtensionReceiverParameter(), "Accessor inner scope with synthetic field", RedeclarationHandler.DO_NOTHING, new Function1<LexicalScopeImpl.InitializeHandler, Unit>() { @Override public Unit invoke(LexicalScopeImpl.InitializeHandler handler) { handler.addVariableOrClassDescriptor(fieldDescriptor); return Unit.INSTANCE$; } }); // Check parameter name shadowing for (JetParameter parameter : function.getValueParameters()) { if (SyntheticFieldDescriptor.NAME.equals(parameter.getNameAsName())) { trace.report(Errors.ACCESSOR_PARAMETER_NAME_SHADOWING.on(parameter)); } } } DataFlowInfo dataFlowInfo = null; if (beforeBlockBody != null) { dataFlowInfo = beforeBlockBody.invoke(innerScope); } if (function.hasBody()) { expressionTypingServices.checkFunctionReturnType( innerScope, function, functionDescriptor, dataFlowInfo != null ? dataFlowInfo : outerDataFlowInfo, null, trace); } assert functionDescriptor.getReturnType() != null; }