@NotNull private static FrameMap createFrameMap( @NotNull GenerationState state, @NotNull FunctionDescriptor function, @NotNull JvmMethodSignature signature, boolean isStatic) { FrameMap frameMap = new FrameMap(); if (!isStatic) { frameMap.enterTemp(OBJECT_TYPE); } for (JvmMethodParameterSignature parameter : signature.getValueParameters()) { if (parameter.getKind() == JvmMethodParameterKind.RECEIVER) { ReceiverParameterDescriptor receiverParameter = function.getExtensionReceiverParameter(); if (receiverParameter != null) { frameMap.enter(receiverParameter, state.getTypeMapper().mapType(receiverParameter)); } } else if (parameter.getKind() != JvmMethodParameterKind.VALUE) { frameMap.enterTemp(parameter.getAsmType()); } } for (ValueParameterDescriptor parameter : function.getValueParameters()) { frameMap.enter(parameter, state.getTypeMapper().mapType(parameter)); } return frameMap; }
@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 public static FunctionDescriptor getErasedInvokeFunction( @NotNull FunctionDescriptor elementDescriptor) { int arity = elementDescriptor.getValueParameters().size(); ClassDescriptor elementClass = elementDescriptor.getExtensionReceiverParameter() == null ? getBuiltIns(elementDescriptor).getFunction(arity) : getBuiltIns(elementDescriptor).getExtensionFunction(arity); return elementClass .getDefaultType() .getMemberScope() .getFunctions(OperatorConventions.INVOKE) .iterator() .next(); }
private void generateBridge(@NotNull Method bridge, @NotNull Method delegate) { if (bridge.equals(delegate)) return; MethodVisitor mv = v.newMethod( OtherOrigin(element, funDescriptor), ACC_PUBLIC | ACC_BRIDGE, bridge.getName(), bridge.getDescriptor(), null, ArrayUtil.EMPTY_STRING_ARRAY); if (state.getClassBuilderMode() != ClassBuilderMode.FULL) return; mv.visitCode(); InstructionAdapter iv = new InstructionAdapter(mv); ImplementationBodyCodegen.markLineNumberForSyntheticFunction( DescriptorUtils.getParentOfType(funDescriptor, ClassDescriptor.class), iv); iv.load(0, asmType); ReceiverParameterDescriptor receiver = funDescriptor.getExtensionReceiverParameter(); int count = 1; if (receiver != null) { StackValue.local(count, bridge.getArgumentTypes()[count - 1]) .put(typeMapper.mapType(receiver.getType()), iv); count++; } List<ValueParameterDescriptor> params = funDescriptor.getValueParameters(); for (ValueParameterDescriptor param : params) { StackValue.local(count, bridge.getArgumentTypes()[count - 1]) .put(typeMapper.mapType(param.getType()), iv); count++; } iv.invokevirtual( asmType.getInternalName(), delegate.getName(), delegate.getDescriptor(), false); StackValue.onStack(delegate.getReturnType()).put(bridge.getReturnType(), iv); iv.areturn(bridge.getReturnType()); FunctionCodegen.endVisit(mv, "bridge", element); }
@NotNull private Method generateConstructor(@NotNull Type superClassAsmType) { List<FieldInfo> args = calculateConstructorParameters(typeMapper, closure, asmType); Type[] argTypes = fieldListToTypeArray(args); Method constructor = new Method("<init>", Type.VOID_TYPE, argTypes); MethodVisitor mv = v.newMethod( OtherOrigin(element, funDescriptor), visibilityFlag, "<init>", constructor.getDescriptor(), null, ArrayUtil.EMPTY_STRING_ARRAY); if (state.getClassBuilderMode() == ClassBuilderMode.FULL) { mv.visitCode(); InstructionAdapter iv = new InstructionAdapter(mv); int k = 1; for (FieldInfo fieldInfo : args) { k = genAssignInstanceFieldFromParam(fieldInfo, k, iv); } iv.load(0, superClassAsmType); if (superClassAsmType.equals(LAMBDA) || superClassAsmType.equals(FUNCTION_REFERENCE)) { int arity = funDescriptor.getValueParameters().size(); if (funDescriptor.getExtensionReceiverParameter() != null) arity++; if (funDescriptor.getDispatchReceiverParameter() != null) arity++; iv.iconst(arity); iv.invokespecial(superClassAsmType.getInternalName(), "<init>", "(I)V", false); } else { iv.invokespecial(superClassAsmType.getInternalName(), "<init>", "()V", false); } iv.visitInsn(RETURN); FunctionCodegen.endVisit(iv, "constructor", element); } return constructor; }
private static void equipFunctionReferenceWithReflection( @NotNull InstructionAdapter v, @NotNull FunctionDescriptor target) { DeclarationDescriptor container = target.getContainingDeclaration(); Type type; if (container instanceof PackageFragmentDescriptor) { type = target.getExtensionReceiverParameter() != null ? K_TOP_LEVEL_EXTENSION_FUNCTION : K_TOP_LEVEL_FUNCTION; } else if (container instanceof ClassDescriptor) { type = K_MEMBER_FUNCTION; } else { type = K_LOCAL_FUNCTION; } Method method = method("function", K_FUNCTION, FUNCTION_REFERENCE); v.invokestatic(REFLECTION, method.getName(), method.getDescriptor(), false); StackValue.coerce(K_FUNCTION, type, v); }
@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 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; }