コード例 #1
0
ファイル: FunctionCodegen.java プロジェクト: AlexJuca/kotlin
  private static void generateTypeCheckBarrierIfNeeded(
      @NotNull InstructionAdapter iv,
      @NotNull FunctionDescriptor descriptor,
      @NotNull Type returnType,
      @Nullable final Type delegateParameterType) {
    BuiltinMethodsWithSpecialGenericSignature.DefaultValue defaultValue =
        BuiltinMethodsWithSpecialGenericSignature.getDefaultValueForOverriddenBuiltinFunction(
            descriptor);
    if (defaultValue == null) return;

    assert descriptor.getValueParameters().size() == 1
        : "Should be descriptor with one value parameter, but found: " + descriptor;

    boolean isCheckForAny =
        delegateParameterType == null || OBJECT_TYPE.equals(delegateParameterType);

    final KotlinType kotlinType = descriptor.getValueParameters().get(0).getType();

    if (isCheckForAny && TypeUtils.isNullableType(kotlinType)) return;

    iv.load(1, OBJECT_TYPE);

    Label afterBarrier = new Label();

    if (isCheckForAny) {
      assert !TypeUtils.isNullableType(kotlinType)
          : "Only bridges for not-nullable types are necessary";
      iv.ifnonnull(afterBarrier);
    } else {
      CodegenUtilKt.generateIsCheck(iv, kotlinType, boxType(delegateParameterType));
      iv.ifne(afterBarrier);
    }

    StackValue.constant(defaultValue.getValue(), returnType).put(returnType, iv);
    iv.areturn(returnType);

    iv.visitLabel(afterBarrier);
  }
コード例 #2
0
ファイル: FunctionCodegen.java プロジェクト: AlexJuca/kotlin
  public void generateBridges(@NotNull FunctionDescriptor descriptor) {
    if (descriptor instanceof ConstructorDescriptor) return;
    if (owner.getContextKind() == OwnerKind.DEFAULT_IMPLS) return;
    if (isInterface(descriptor.getContainingDeclaration())) return;

    // equals(Any?), hashCode(), toString() never need bridges
    if (isMethodOfAny(descriptor)) return;

    // If the function doesn't have a physical declaration among super-functions, it's a SAM adapter
    // or alike and doesn't need bridges
    if (CallResolverUtilKt.isOrOverridesSynthesized(descriptor)) return;

    boolean isSpecial =
        SpecialBuiltinMembers.getOverriddenBuiltinReflectingJvmDescriptor(descriptor) != null;

    Set<Bridge<Method>> bridgesToGenerate;
    if (!isSpecial) {
      bridgesToGenerate =
          ImplKt.generateBridgesForFunctionDescriptor(descriptor, getSignatureMapper(typeMapper));
      if (!bridgesToGenerate.isEmpty()) {
        PsiElement origin =
            descriptor.getKind() == DECLARATION ? getSourceFromDescriptor(descriptor) : null;
        boolean isSpecialBridge =
            BuiltinMethodsWithSpecialGenericSignature
                    .getOverriddenBuiltinFunctionWithErasedValueParametersInJava(descriptor)
                != null;

        for (Bridge<Method> bridge : bridgesToGenerate) {
          generateBridge(
              origin, descriptor, bridge.getFrom(), bridge.getTo(), isSpecialBridge, false);
        }
      }
    } else {
      Set<BridgeForBuiltinSpecial<Method>> specials =
          BuiltinSpecialBridgesUtil.generateBridgesForBuiltinSpecial(
              descriptor, getSignatureMapper(typeMapper));

      if (!specials.isEmpty()) {
        PsiElement origin =
            descriptor.getKind() == DECLARATION ? getSourceFromDescriptor(descriptor) : null;
        for (BridgeForBuiltinSpecial<Method> bridge : specials) {
          generateBridge(
              origin,
              descriptor,
              bridge.getFrom(),
              bridge.getTo(),
              bridge.isSpecial(),
              bridge.isDelegateToSuper());
        }
      }

      if (!descriptor.getKind().isReal()
          && isAbstractMethod(descriptor, OwnerKind.IMPLEMENTATION)) {
        CallableDescriptor overridden =
            SpecialBuiltinMembers.getOverriddenBuiltinReflectingJvmDescriptor(descriptor);
        assert overridden != null;

        Method method = typeMapper.mapSignature(descriptor).getAsmMethod();
        int flags = ACC_ABSTRACT | getVisibilityAccessFlag(descriptor);
        v.newMethod(
            JvmDeclarationOriginKt.OtherOrigin(overridden),
            flags,
            method.getName(),
            method.getDescriptor(),
            null,
            null);
      }
    }
  }