/* 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 static boolean isLocalNamedFun(DeclarationDescriptor fd) { if (fd instanceof FunctionDescriptor) { FunctionDescriptor descriptor = (FunctionDescriptor) fd; return descriptor.getVisibility() == Visibilities.LOCAL && !descriptor.getName().isSpecial(); } return false; }
@Override public StackValue innerValue( DeclarationDescriptor d, LocalLookup localLookup, GenerationState state, MutableClosure closure, Type classType) { FunctionDescriptor vd = (FunctionDescriptor) d; boolean idx = localLookup != null && localLookup.lookupLocal(vd); if (!idx) return null; BindingContext bindingContext = state.getBindingContext(); Type localType = asmTypeForAnonymousClass(bindingContext, vd); MutableClosure localFunClosure = bindingContext.get(CLOSURE, bindingContext.get(CLASS_FOR_FUNCTION, vd)); if (localFunClosure != null && JvmCodegenUtil.isConst(localFunClosure)) { // This is an optimization: we can obtain an instance of a const closure simply by // GETSTATIC ...$instance // (instead of passing this instance to the constructor and storing as a field) return StackValue.field(localType, localType, JvmAbi.INSTANCE_FIELD, true); } String fieldName = "$" + vd.getName(); StackValue innerValue = StackValue.field(localType, classType, fieldName, false); closure.recordField(fieldName, localType); closure.captureVariable(new EnclosedValueDescriptor(fieldName, d, innerValue, localType)); return innerValue; }
private ClassDescriptor recordClassForFunction(FunctionDescriptor funDescriptor) { ClassDescriptor classDescriptor; int arity = funDescriptor.getValueParameters().size(); classDescriptor = new ClassDescriptorImpl( funDescriptor.getContainingDeclaration(), Collections.<AnnotationDescriptor>emptyList(), Modality.FINAL, Name.special("<closure>")); ((ClassDescriptorImpl) classDescriptor) .initialize( false, Collections.<TypeParameterDescriptor>emptyList(), Collections.singleton( (funDescriptor.getReceiverParameter().exists() ? JetStandardClasses.getReceiverFunction(arity) : JetStandardClasses.getFunction(arity)) .getDefaultType()), JetScope.EMPTY, Collections.<ConstructorDescriptor>emptySet(), null); assert PsiCodegenPredictor.checkPredictedClassNameForFun( bindingContext, funDescriptor, classDescriptor); bindingTrace.record(CLASS_FOR_FUNCTION, funDescriptor, classDescriptor); return classDescriptor; }
@NotNull public static String[] getThrownExceptions( @NotNull FunctionDescriptor function, @NotNull final JetTypeMapper mapper) { AnnotationDescriptor annotation = function.getAnnotations().findAnnotation(new FqName("kotlin.throws")); if (annotation == null) { annotation = function.getAnnotations().findAnnotation(new FqName("kotlin.jvm.Throws")); } if (annotation == null) return ArrayUtil.EMPTY_STRING_ARRAY; Collection<ConstantValue<?>> values = annotation.getAllValueArguments().values(); if (values.isEmpty()) return ArrayUtil.EMPTY_STRING_ARRAY; Object value = values.iterator().next(); if (!(value instanceof ArrayValue)) return ArrayUtil.EMPTY_STRING_ARRAY; ArrayValue arrayValue = (ArrayValue) value; List<String> strings = ContainerUtil.mapNotNull( arrayValue.getValue(), new Function<ConstantValue<?>, String>() { @Override public String fun(ConstantValue<?> constant) { if (constant instanceof KClassValue) { KClassValue classValue = (KClassValue) constant; ClassDescriptor classDescriptor = DescriptorUtils.getClassDescriptorForType(classValue.getValue()); return mapper.mapClass(classDescriptor).getInternalName(); } return null; } }); return ArrayUtil.toStringArray(strings); }
public static void genNotNullAssertionsForParameters( @NotNull InstructionAdapter v, @NotNull GenerationState state, @NotNull FunctionDescriptor descriptor, @NotNull FrameMap frameMap) { if (!state.isGenerateNotNullParamAssertions()) return; // Private method is not accessible from other classes, no assertions needed if (getVisibilityAccessFlag(descriptor) == ACC_PRIVATE) return; for (ValueParameterDescriptor parameter : descriptor.getValueParameters()) { JetType type = parameter.getReturnType(); if (type == null || isNullableType(type)) continue; int index = frameMap.getIndex(parameter); Type asmType = state.getTypeMapper().mapReturnType(type); if (asmType.getSort() == Type.OBJECT || asmType.getSort() == Type.ARRAY) { v.load(index, asmType); v.visitLdcInsn(descriptor.getName().asString()); v.invokestatic( "jet/runtime/Intrinsics", "checkParameterIsNotNull", "(Ljava/lang/Object;Ljava/lang/String;)V"); } } }
@Nullable private static InsertHandler<LookupElement> getInsertHandler( @NotNull DeclarationDescriptor descriptor) { if (descriptor instanceof FunctionDescriptor) { FunctionDescriptor functionDescriptor = (FunctionDescriptor) descriptor; if (functionDescriptor.getValueParameters().isEmpty()) { return EMPTY_FUNCTION_HANDLER; } if (functionDescriptor.getValueParameters().size() == 1 && KotlinBuiltIns.getInstance() .isFunctionOrExtensionFunctionType( functionDescriptor.getValueParameters().get(0).getType())) { return PARAMS_BRACES_FUNCTION_HANDLER; } return PARAMS_PARENTHESIS_FUNCTION_HANDLER; } if (descriptor instanceof ClassDescriptor) { return JetClassInsertHandler.INSTANCE; } return null; }
public static int getMethodAsmFlags(FunctionDescriptor functionDescriptor, OwnerKind kind) { int flags = getCommonCallableFlags(functionDescriptor); if (functionDescriptor.getModality() == Modality.FINAL && !(functionDescriptor instanceof ConstructorDescriptor)) { DeclarationDescriptor containingDeclaration = functionDescriptor.getContainingDeclaration(); if (!(containingDeclaration instanceof ClassDescriptor) || ((ClassDescriptor) containingDeclaration).getKind() != ClassKind.TRAIT) { flags |= ACC_FINAL; } } if (isStaticMethod(kind, functionDescriptor)) { flags |= ACC_STATIC; } if (isAbstractMethod(functionDescriptor, kind)) { flags |= ACC_ABSTRACT; } if (JetTypeMapper.isAccessor(functionDescriptor)) { flags |= ACC_SYNTHETIC; } return flags; }
@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 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; }
@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; }
public boolean isMain(@NotNull JetNamedFunction function) { if (!"main".equals(function.getName())) return false; FunctionDescriptor functionDescriptor = getFunctionDescriptor.fun(function); List<ValueParameterDescriptor> parameters = functionDescriptor.getValueParameters(); if (parameters.size() != 1) return false; ValueParameterDescriptor parameter = parameters.get(0); JetType parameterType = parameter.getType(); if (!KotlinBuiltIns.isArray(parameterType)) return false; List<TypeProjection> typeArguments = parameterType.getArguments(); if (typeArguments.size() != 1) return false; JetType typeArgument = typeArguments.get(0).getType(); if (!JetTypeChecker.DEFAULT.equalTypes( typeArgument, getBuiltIns(functionDescriptor).getStringType())) return false; if (DescriptorUtils.isTopLevelDeclaration(functionDescriptor)) return true; DeclarationDescriptor containingDeclaration = functionDescriptor.getContainingDeclaration(); return containingDeclaration instanceof ClassDescriptor && ((ClassDescriptor) containingDeclaration).getKind().isSingleton() && AnnotationsPackage.hasPlatformStaticAnnotation(functionDescriptor); }
@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 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); }
protected void renderValueParameters(FunctionDescriptor descriptor, StringBuilder builder) { if (descriptor.getValueParameters().isEmpty()) { renderEmptyValueParameters(builder); } for (Iterator<ValueParameterDescriptor> iterator = descriptor.getValueParameters().iterator(); iterator.hasNext(); ) { renderValueParameter(iterator.next(), !iterator.hasNext(), builder); } }
private static boolean isMethodOfAny(@NotNull FunctionDescriptor descriptor) { String name = descriptor.getName().asString(); List<ValueParameterDescriptor> parameters = descriptor.getValueParameters(); if (parameters.isEmpty()) { return name.equals("hashCode") || name.equals("toString"); } else if (parameters.size() == 1 && name.equals("equals")) { return isNullableAny(parameters.get(0).getType()); } return false; }
public void genDelegate( @NotNull FunctionDescriptor functionDescriptor, FunctionDescriptor overriddenDescriptor, StackValue field) { genDelegate( functionDescriptor, overriddenDescriptor.getOriginal(), (ClassDescriptor) overriddenDescriptor.getContainingDeclaration(), field); }
@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); }
private static int getVarargsFlag(FunctionDescriptor functionDescriptor) { if (!functionDescriptor.getValueParameters().isEmpty() && functionDescriptor .getValueParameters() .get(functionDescriptor.getValueParameters().size() - 1) .getVarargElementType() != null) { return ACC_VARARGS; } return 0; }
@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 @Override public String getText() { if (functionsToAdd.size() == 1) { FunctionDescriptor newFunction = functionsToAdd.get(0); ClassDescriptor supertype = (ClassDescriptor) newFunction.getContainingDeclaration(); return JetBundle.message( "add.function.to.type.action.single", IdeDescriptorRenderers.SOURCE_CODE_SHORT_NAMES_IN_TYPES.render(newFunction), supertype.getName().toString()); } else { return JetBundle.message("add.function.to.supertype.action.multiple"); } }
private static FunctionDescriptor generateFunctionSignatureForType( FunctionDescriptor functionDescriptor, ClassDescriptor typeDescriptor) { // TODO: support for generics. Modality modality = typeDescriptor.getModality(); if (typeDescriptor.getKind() == ClassKind.INTERFACE) { modality = Modality.OPEN; } return functionDescriptor.copy( typeDescriptor, modality, functionDescriptor.getVisibility(), CallableMemberDescriptor.Kind.DECLARATION, /* copyOverrides = */ false); }
@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()); }
@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; }
@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(); }
@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)); }
@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; }
private void generateBridge( @Nullable PsiElement origin, @NotNull FunctionDescriptor descriptor, @NotNull Method bridge, @NotNull Method delegateTo, boolean isSpecialBridge, boolean isStubDeclarationWithDelegationToSuper) { boolean isSpecialOrDelegationToSuper = isSpecialBridge || isStubDeclarationWithDelegationToSuper; int flags = ACC_PUBLIC | ACC_BRIDGE | (!isSpecialOrDelegationToSuper ? ACC_SYNTHETIC : 0) | (isSpecialBridge ? ACC_FINAL : 0); // TODO. MethodVisitor mv = v.newMethod( JvmDeclarationOriginKt.Bridge(descriptor, origin), flags, bridge.getName(), bridge.getDescriptor(), null, null); if (state.getClassBuilderMode() != ClassBuilderMode.FULL) return; mv.visitCode(); Type[] argTypes = bridge.getArgumentTypes(); Type[] originalArgTypes = delegateTo.getArgumentTypes(); InstructionAdapter iv = new InstructionAdapter(mv); MemberCodegen.markLineNumberForDescriptor(owner.getThisDescriptor(), iv); if (delegateTo.getArgumentTypes().length == 1 && isSpecialBridge) { generateTypeCheckBarrierIfNeeded( iv, descriptor, bridge.getReturnType(), delegateTo.getArgumentTypes()[0]); } iv.load(0, OBJECT_TYPE); for (int i = 0, reg = 1; i < argTypes.length; i++) { StackValue.local(reg, argTypes[i]).put(originalArgTypes[i], iv); //noinspection AssignmentToForLoopParameter reg += argTypes[i].getSize(); } if (isStubDeclarationWithDelegationToSuper) { ClassDescriptor parentClass = getSuperClassDescriptor((ClassDescriptor) descriptor.getContainingDeclaration()); assert parentClass != null; String parentInternalName = typeMapper.mapClass(parentClass).getInternalName(); iv.invokespecial(parentInternalName, delegateTo.getName(), delegateTo.getDescriptor()); } else { iv.invokevirtual(v.getThisName(), delegateTo.getName(), delegateTo.getDescriptor()); } StackValue.coerce(delegateTo.getReturnType(), bridge.getReturnType(), iv); iv.areturn(bridge.getReturnType()); endVisit(mv, "bridge method", origin); }