Exemplo n.º 1
0
  public void gen(@NotNull KtNamedFunction function) {
    SimpleFunctionDescriptor functionDescriptor =
        bindingContext.get(BindingContext.FUNCTION, function);
    assert functionDescriptor != null
        : "No descriptor for function "
            + function.getText()
            + "\n"
            + "in "
            + function.getContainingFile().getVirtualFile();

    if (owner.getContextKind() != OwnerKind.DEFAULT_IMPLS || function.hasBody()) {
      generateMethod(
          JvmDeclarationOriginKt.OtherOrigin(function, functionDescriptor),
          functionDescriptor,
          new FunctionGenerationStrategy.FunctionDefault(state, functionDescriptor, function));
    }

    generateDefaultIfNeeded(
        owner.intoFunction(functionDescriptor),
        functionDescriptor,
        owner.getContextKind(),
        DefaultParameterValueLoader.DEFAULT,
        function);

    generateOverloadsWithDefaultValues(function, functionDescriptor, functionDescriptor);
  }
Exemplo n.º 2
0
  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);
  }
Exemplo n.º 3
0
 public static String getInlineName(
     @NotNull CodegenContext codegenContext,
     @NotNull JetTypeMapper typeMapper,
     @NotNull JvmFileClassesProvider fileClassesManager) {
   return getInlineName(
       codegenContext, codegenContext.getContextDescriptor(), typeMapper, fileClassesManager);
 }
Exemplo n.º 4
0
 public void generateOverloadsWithDefaultValues(
     @Nullable KtNamedFunction function,
     @NotNull FunctionDescriptor functionDescriptor,
     @NotNull FunctionDescriptor delegateFunctionDescriptor) {
   new DefaultParameterValueSubstitutor(state)
       .generateOverloadsIfNeeded(
           function, functionDescriptor, delegateFunctionDescriptor, owner.getContextKind(), v);
 }
Exemplo n.º 5
0
 @Nullable
 public static PsiFile getContainingFile(CodegenContext codegenContext) {
   DeclarationDescriptor contextDescriptor = codegenContext.getContextDescriptor();
   PsiElement psiElement = DescriptorToSourceUtils.descriptorToDeclaration(contextDescriptor);
   if (psiElement != null) {
     return psiElement.getContainingFile();
   }
   return null;
 }
Exemplo n.º 6
0
  private static String getInlineName(
      @NotNull CodegenContext codegenContext,
      @NotNull DeclarationDescriptor currentDescriptor,
      @NotNull JetTypeMapper typeMapper,
      @NotNull JvmFileClassesProvider fileClassesProvider) {
    if (currentDescriptor instanceof PackageFragmentDescriptor) {
      PsiFile file = getContainingFile(codegenContext);

      Type implementationOwnerType;
      if (file == null) {
        implementationOwnerType =
            CodegenContextUtil.getImplementationOwnerClassType(codegenContext);
      } else {
        implementationOwnerType =
            FileClassesPackage.getFileClassType(fileClassesProvider, (JetFile) file);
      }

      if (implementationOwnerType == null) {
        DeclarationDescriptor contextDescriptor = codegenContext.getContextDescriptor();
        //noinspection ConstantConditions
        throw new RuntimeException(
            "Couldn't find declaration for "
                + contextDescriptor.getContainingDeclaration().getName()
                + "."
                + contextDescriptor.getName()
                + "; context: "
                + codegenContext);
      }

      return implementationOwnerType.getInternalName();
    } else if (currentDescriptor instanceof ClassifierDescriptor) {
      Type type = typeMapper.mapType((ClassifierDescriptor) currentDescriptor);
      return type.getInternalName();
    } else if (currentDescriptor instanceof FunctionDescriptor) {
      ClassDescriptor descriptor =
          typeMapper
              .getBindingContext()
              .get(CodegenBinding.CLASS_FOR_CALLABLE, (FunctionDescriptor) currentDescriptor);
      if (descriptor != null) {
        Type type = typeMapper.mapType(descriptor);
        return type.getInternalName();
      }
    }

    // TODO: add suffix for special case
    String suffix =
        currentDescriptor.getName().isSpecial() ? "" : currentDescriptor.getName().asString();

    //noinspection ConstantConditions
    return getInlineName(
            codegenContext,
            currentDescriptor.getContainingDeclaration(),
            typeMapper,
            fileClassesProvider)
        + "$"
        + suffix;
  }
Exemplo n.º 7
0
 public static void initDefaultSourceMappingIfNeeded(
     @NotNull CodegenContext context,
     @NotNull MemberCodegen codegen,
     @NotNull GenerationState state) {
   if (state.isInlineEnabled()) {
     CodegenContext<?> parentContext = context.getParentContext();
     while (parentContext != null) {
       if (parentContext instanceof MethodContext) {
         if (((MethodContext) parentContext).isInlineFunction()) {
           // just init default one to one mapping
           codegen.getOrCreateSourceMapper();
           break;
         }
       }
       parentContext = parentContext.getParentContext();
     }
   }
 }
Exemplo n.º 8
0
  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);
      }
    }
  }
Exemplo n.º 9
0
 public void generateMethod(
     @NotNull JvmDeclarationOrigin origin,
     @NotNull FunctionDescriptor descriptor,
     @NotNull FunctionGenerationStrategy strategy) {
   generateMethod(origin, descriptor, owner.intoFunction(descriptor), strategy);
 }