Example #1
0
  @NotNull
  public static List<FieldInfo> calculateConstructorParameters(
      @NotNull JetTypeMapper typeMapper,
      @NotNull CalculatedClosure closure,
      @NotNull Type ownerType) {
    BindingContext bindingContext = typeMapper.getBindingContext();
    List<FieldInfo> args = Lists.newArrayList();
    ClassDescriptor captureThis = closure.getCaptureThis();
    if (captureThis != null) {
      Type type = typeMapper.mapType(captureThis);
      args.add(FieldInfo.createForHiddenField(ownerType, type, CAPTURED_THIS_FIELD));
    }
    JetType captureReceiverType = closure.getCaptureReceiverType();
    if (captureReceiverType != null) {
      args.add(
          FieldInfo.createForHiddenField(
              ownerType, typeMapper.mapType(captureReceiverType), CAPTURED_RECEIVER_FIELD));
    }

    for (DeclarationDescriptor descriptor : closure.getCaptureVariables().keySet()) {
      if (descriptor instanceof VariableDescriptor && !(descriptor instanceof PropertyDescriptor)) {
        Type sharedVarType = typeMapper.getSharedVarType(descriptor);

        Type type =
            sharedVarType != null
                ? sharedVarType
                : typeMapper.mapType((VariableDescriptor) descriptor);
        args.add(
            FieldInfo.createForHiddenField(ownerType, type, "$" + descriptor.getName().asString()));
      } else if (DescriptorUtils.isLocalFunction(descriptor)) {
        Type classType = asmTypeForAnonymousClass(bindingContext, (FunctionDescriptor) descriptor);
        args.add(
            FieldInfo.createForHiddenField(
                ownerType, classType, "$" + descriptor.getName().asString()));
      } else if (descriptor instanceof FunctionDescriptor) {
        assert captureReceiverType != null;
      }
    }
    return args;
  }
Example #2
0
  @NotNull
  public List<CapturedParamDesc> getCapturedVars() {
    // lazy initialization cause it would be calculated after object creation
    if (capturedVars == null) {
      capturedVars = new ArrayList<CapturedParamDesc>();

      if (closure.getCaptureThis() != null) {
        Type type = typeMapper.mapType(closure.getCaptureThis());
        EnclosedValueDescriptor descriptor =
            new EnclosedValueDescriptor(
                AsmUtil.CAPTURED_THIS_FIELD,
                /* descriptor = */ null,
                StackValue.field(
                    type, closureClassType, AsmUtil.CAPTURED_THIS_FIELD, false, StackValue.LOCAL_0),
                type);
        capturedVars.add(getCapturedParamInfo(descriptor));
      }

      if (closure.getCaptureReceiverType() != null) {
        Type type = typeMapper.mapType(closure.getCaptureReceiverType());
        EnclosedValueDescriptor descriptor =
            new EnclosedValueDescriptor(
                AsmUtil.CAPTURED_RECEIVER_FIELD,
                /* descriptor = */ null,
                StackValue.field(
                    type,
                    closureClassType,
                    AsmUtil.CAPTURED_RECEIVER_FIELD,
                    false,
                    StackValue.LOCAL_0),
                type);
        capturedVars.add(getCapturedParamInfo(descriptor));
      }

      for (EnclosedValueDescriptor descriptor : closure.getCaptureVariables().values()) {
        capturedVars.add(getCapturedParamInfo(descriptor));
      }
    }
    return capturedVars;
  }