Esempio n. 1
0
  private void processPrimaryConstructor(MutableClassDescriptor classDescriptor, JetClass klass) {
    if (classDescriptor.getKind() == ClassKind.TRAIT) {
      JetParameterList primaryConstructorParameterList = klass.getPrimaryConstructorParameterList();
      if (primaryConstructorParameterList != null) {
        context.getTrace().report(CONSTRUCTOR_IN_TRAIT.on(primaryConstructorParameterList));
      }
      if (!klass.hasPrimaryConstructor()) return;
    }

    // TODO : not all the parameters are real properties
    JetScope memberScope = classDescriptor.getScopeForSupertypeResolution();
    ConstructorDescriptor constructorDescriptor =
        context
            .getDescriptorResolver()
            .resolvePrimaryConstructorDescriptor(memberScope, classDescriptor, klass);
    for (JetParameter parameter : klass.getPrimaryConstructorParameters()) {
      if (parameter.getValOrVarNode() != null) {
        PropertyDescriptor propertyDescriptor =
            context
                .getDescriptorResolver()
                .resolvePrimaryConstructorParameterToAProperty(
                    classDescriptor, memberScope, parameter);
        classDescriptor.addPropertyDescriptor(propertyDescriptor);
        context.getPrimaryConstructorParameterProperties().add(propertyDescriptor);
      }
    }
    if (constructorDescriptor != null) {
      classDescriptor.setPrimaryConstructor(constructorDescriptor, context.getTrace());
    }
  }
 private static String renderParameter(
     ValueParameterDescriptor descriptor, boolean named, BindingContext bindingContext) {
   StringBuilder builder = new StringBuilder();
   if (named) builder.append("[");
   if (descriptor.getVarargElementType() != null) {
     builder.append("vararg ");
   }
   builder
       .append(descriptor.getName())
       .append(": ")
       .append(DescriptorRenderer.TEXT.renderType(getActualParameterType(descriptor)));
   if (descriptor.hasDefaultValue()) {
     PsiElement element = BindingContextUtils.descriptorToDeclaration(bindingContext, descriptor);
     String defaultExpression = "?";
     if (element instanceof JetParameter) {
       JetParameter parameter = (JetParameter) element;
       JetExpression defaultValue = parameter.getDefaultValue();
       if (defaultValue != null) {
         if (defaultValue instanceof JetConstantExpression) {
           JetConstantExpression constantExpression = (JetConstantExpression) defaultValue;
           defaultExpression = constantExpression.getText();
           if (defaultExpression.length() > 10) {
             if (defaultExpression.startsWith("\"")) defaultExpression = "\"...\"";
             else if (defaultExpression.startsWith("\'")) defaultExpression = "\'...\'";
             else defaultExpression = defaultExpression.substring(0, 7) + "...";
           }
         }
       }
     }
     builder.append(" = ").append(defaultExpression);
   }
   if (named) builder.append("]");
   return builder.toString();
 }
Esempio n. 3
0
  private void processPrimaryConstructor(
      @NotNull TopDownAnalysisContext c,
      @NotNull MutableClassDescriptor classDescriptor,
      @NotNull JetClass klass) {
    // TODO : not all the parameters are real properties
    JetScope memberScope = classDescriptor.getScopeForClassHeaderResolution();
    ConstructorDescriptor constructorDescriptor =
        descriptorResolver.resolvePrimaryConstructorDescriptor(
            memberScope, classDescriptor, klass, trace);
    if (constructorDescriptor != null) {
      List<ValueParameterDescriptor> valueParameterDescriptors =
          constructorDescriptor.getValueParameters();
      List<JetParameter> primaryConstructorParameters = klass.getPrimaryConstructorParameters();
      assert valueParameterDescriptors.size() == primaryConstructorParameters.size();
      List<ValueParameterDescriptor> notProperties = new ArrayList<ValueParameterDescriptor>();
      for (ValueParameterDescriptor valueParameterDescriptor : valueParameterDescriptors) {
        JetParameter parameter =
            primaryConstructorParameters.get(valueParameterDescriptor.getIndex());
        if (parameter.getValOrVarNode() != null) {
          PropertyDescriptor propertyDescriptor =
              descriptorResolver.resolvePrimaryConstructorParameterToAProperty(
                  classDescriptor, valueParameterDescriptor, memberScope, parameter, trace);
          classDescriptor.getBuilder().addPropertyDescriptor(propertyDescriptor);
          c.getPrimaryConstructorParameterProperties().put(parameter, propertyDescriptor);
        } else {
          notProperties.add(valueParameterDescriptor);
        }
      }

      if (classDescriptor.getKind() != ClassKind.TRAIT) {
        classDescriptor.setPrimaryConstructor(constructorDescriptor);
        classDescriptor.addConstructorParametersToInitializersScope(notProperties);
      }
    }
  }
  private List<ValueParameterDescriptor> createValueParameterDescriptors(
      ExpressionTypingContext context,
      JetFunctionLiteral functionLiteral,
      FunctionDescriptorImpl functionDescriptor,
      boolean functionTypeExpected) {
    List<ValueParameterDescriptor> valueParameterDescriptors = Lists.newArrayList();
    List<JetParameter> declaredValueParameters = functionLiteral.getValueParameters();

    List<ValueParameterDescriptor> expectedValueParameters =
        (functionTypeExpected)
            ? JetStandardClasses.getValueParameters(functionDescriptor, context.expectedType)
            : null;

    boolean hasDeclaredValueParameters = functionLiteral.getValueParameterList() != null;
    if (functionTypeExpected
        && !hasDeclaredValueParameters
        && expectedValueParameters.size() == 1) {
      ValueParameterDescriptor valueParameterDescriptor = expectedValueParameters.get(0);
      ValueParameterDescriptor it =
          new ValueParameterDescriptorImpl(
              functionDescriptor,
              0,
              Collections.<AnnotationDescriptor>emptyList(),
              "it",
              false,
              valueParameterDescriptor.getOutType(),
              valueParameterDescriptor.hasDefaultValue(),
              valueParameterDescriptor.getVarargElementType());
      valueParameterDescriptors.add(it);
      context.trace.record(AUTO_CREATED_IT, it);
    } else {
      for (int i = 0; i < declaredValueParameters.size(); i++) {
        JetParameter declaredParameter = declaredValueParameters.get(i);
        JetTypeReference typeReference = declaredParameter.getTypeReference();

        JetType type;
        if (typeReference != null) {
          type = context.getTypeResolver().resolveType(context.scope, typeReference);
        } else {
          if (expectedValueParameters != null && i < expectedValueParameters.size()) {
            type = expectedValueParameters.get(i).getOutType();
          } else {
            context.trace.report(CANNOT_INFER_PARAMETER_TYPE.on(declaredParameter));
            type = ErrorUtils.createErrorType("Cannot be inferred");
          }
        }
        ValueParameterDescriptor valueParameterDescriptor =
            context
                .getDescriptorResolver()
                .resolveValueParameterDescriptor(functionDescriptor, declaredParameter, i, type);
        valueParameterDescriptors.add(valueParameterDescriptor);
      }
    }
    return valueParameterDescriptors;
  }
Esempio n. 5
0
  @Override
  public Icon getIcon(@NotNull PsiElement psiElement, int flags) {
    if (psiElement instanceof JetFile) {
      JetFile file = (JetFile) psiElement;
      JetClassOrObject mainClass = getMainClass(file);
      return mainClass != null ? getIcon(mainClass, flags) : JetIcons.FILE;
    }
    if (psiElement instanceof JetNamespaceHeader) {
      return (flags & Iconable.ICON_FLAG_OPEN) != 0
          ? PlatformIcons.PACKAGE_OPEN_ICON
          : PlatformIcons.PACKAGE_ICON;
    }
    if (psiElement instanceof JetNamedFunction) {
      return PsiTreeUtil.getParentOfType(psiElement, JetNamedDeclaration.class) instanceof JetClass
          ? PlatformIcons.METHOD_ICON
          : JetIcons.FUNCTION;
    }
    if (psiElement instanceof JetClass) {
      JetClass jetClass = (JetClass) psiElement;
      if (jetClass.isTrait()) {
        return JetIcons.TRAIT;
      }

      Icon icon =
          jetClass.hasModifier(JetTokens.ENUM_KEYWORD) ? PlatformIcons.ENUM_ICON : JetIcons.CLASS;
      if (jetClass instanceof JetEnumEntry) {
        JetEnumEntry enumEntry = (JetEnumEntry) jetClass;
        if (enumEntry.getPrimaryConstructorParameterList() == null) {
          icon = PlatformIcons.ENUM_ICON;
        }
      }
      return icon;
    }
    if (psiElement instanceof JetObjectDeclaration || psiElement instanceof JetClassObject) {
      return JetIcons.OBJECT;
    }
    if (psiElement instanceof JetParameter) {
      JetParameter parameter = (JetParameter) psiElement;
      if (parameter.getValOrVarNode() != null) {
        JetParameterList parameterList =
            PsiTreeUtil.getParentOfType(psiElement, JetParameterList.class);
        if (parameterList != null && parameterList.getParent() instanceof JetClass) {
          return parameter.isMutable() ? JetIcons.FIELD_VAR : JetIcons.FIELD_VAL;
        }
      }
      return JetIcons.PARAMETER;
    }
    if (psiElement instanceof JetProperty) {
      JetProperty property = (JetProperty) psiElement;
      return property.isVar() ? JetIcons.FIELD_VAR : JetIcons.FIELD_VAL;
    }
    return null;
  }
Esempio n. 6
0
 private void checkDefaultParameterValues(
     List<JetParameter> valueParameters,
     List<ValueParameterDescriptor> valueParameterDescriptors,
     JetScope declaringScope) {
   for (int i = 0; i < valueParameters.size(); i++) {
     ValueParameterDescriptor valueParameterDescriptor = valueParameterDescriptors.get(i);
     if (valueParameterDescriptor.hasDefaultValue()) {
       JetParameter jetParameter = valueParameters.get(i);
       JetExpression defaultValue = jetParameter.getDefaultValue();
       if (defaultValue != null) {
         expressionTypingServices.getType(
             declaringScope, defaultValue, valueParameterDescriptor.getType(), trace);
       }
     }
   }
 }
  @NotNull
  private static ValueParameterDescriptor createValueParameterDescriptor(
      @NotNull ExpressionTypingContext context,
      @NotNull FunctionDescriptorImpl functionDescriptor,
      @NotNull List<JetParameter> declaredValueParameters,
      @Nullable List<ValueParameterDescriptor> expectedValueParameters,
      int index) {
    JetParameter declaredParameter = declaredValueParameters.get(index);
    JetTypeReference typeReference = declaredParameter.getTypeReference();

    JetType expectedType;
    if (expectedValueParameters != null && index < expectedValueParameters.size()) {
      expectedType = expectedValueParameters.get(index).getType();
    } else {
      expectedType = null;
    }
    JetType type;
    if (typeReference != null) {
      type =
          context
              .expressionTypingServices
              .getTypeResolver()
              .resolveType(context.scope, typeReference, context.trace, true);
      if (expectedType != null) {
        if (!JetTypeChecker.INSTANCE.isSubtypeOf(expectedType, type)) {
          context.trace.report(
              EXPECTED_PARAMETER_TYPE_MISMATCH.on(declaredParameter, expectedType));
        }
      }
    } else {
      if (expectedType == null
          || expectedType == DONT_CARE
          || expectedType == CANT_INFER_TYPE_PARAMETER) {
        context.trace.report(CANNOT_INFER_PARAMETER_TYPE.on(declaredParameter));
      }
      if (expectedType != null) {
        type = expectedType;
      } else {
        type = CANT_INFER_LAMBDA_PARAM_TYPE;
      }
    }
    return context
        .expressionTypingServices
        .getDescriptorResolver()
        .resolveValueParameterDescriptorWithAnnotationArguments(
            context.scope, functionDescriptor, declaredParameter, index, type, context.trace);
  }
Esempio n. 8
0
 private void generatePrimaryConstructorProperties(
     PropertyCodegen propertyCodegen, JetClassOrObject origin) {
   boolean isAnnotation = origin instanceof JetClass && ((JetClass) origin).isAnnotation();
   for (JetParameter p : getPrimaryConstructorParameters()) {
     if (p.getValOrVarNode() != null) {
       PropertyDescriptor propertyDescriptor =
           state.getBindingContext().get(BindingContext.PRIMARY_CONSTRUCTOR_PARAMETER, p);
       if (propertyDescriptor != null) {
         if (!isAnnotation) {
           propertyCodegen.generatePrimaryConstructorProperty(p, propertyDescriptor);
         } else {
           Type type = state.getTypeMapper().mapType(propertyDescriptor);
           v.newMethod(
               p, ACC_PUBLIC | ACC_ABSTRACT, p.getName(), "()" + type.getDescriptor(), null, null);
         }
       }
     }
   }
 }
Esempio n. 9
0
 @Override
 public Boolean computeValue(
     SlicedMap map,
     PropertyDescriptor propertyDescriptor,
     Boolean backingFieldRequired,
     boolean valueNotFound) {
   if (propertyDescriptor.getKind() != CallableMemberDescriptor.Kind.DECLARATION) {
     return false;
   }
   backingFieldRequired = valueNotFound ? false : backingFieldRequired;
   assert backingFieldRequired != null;
   // TODO: user BindingContextAccessors
   PsiElement declarationPsiElement =
       map.get(BindingContextUtils.DESCRIPTOR_TO_DECLARATION, propertyDescriptor);
   if (declarationPsiElement instanceof JetParameter) {
     JetParameter jetParameter = (JetParameter) declarationPsiElement;
     return jetParameter.getValOrVarNode() != null
         || backingFieldRequired; // this part is unused because we do not allow access to
                                  // constructor parameters in member bodies
   }
   if (propertyDescriptor.getModality() == Modality.ABSTRACT) return false;
   PropertyGetterDescriptor getter = propertyDescriptor.getGetter();
   PropertySetterDescriptor setter = propertyDescriptor.getSetter();
   if (getter == null) {
     return true;
   } else if (propertyDescriptor.isVar() && setter == null) {
     return true;
   } else if (setter != null
       && !setter.hasBody()
       && setter.getModality() != Modality.ABSTRACT) {
     return true;
   } else if (!getter.hasBody() && getter.getModality() != Modality.ABSTRACT) {
     return true;
   }
   return backingFieldRequired;
 }
Esempio n. 10
0
  @Override
  public PsiJetParameterStub createStub(@NotNull JetParameter psi, StubElement parentStub) {
    JetTypeReference typeReference = psi.getTypeReference();
    JetExpression defaultValue = psi.getDefaultValue();

    return new PsiJetParameterStubImpl(
        parentStub,
        psi.getFqName(),
        psi.getName(),
        psi.isMutable(),
        psi.isVarArg(),
        typeReference != null ? typeReference.getText() : null,
        defaultValue != null ? defaultValue.getText() : null);
  }
  private void computeValueParameters(
      @NotNull List<ValueParameterDescriptor> parameterDescriptors) {
    if (parameterDescriptors.size() != altFunDeclaration.getValueParameters().size()) {
      throw new AlternativeSignatureMismatchException(
          "Method signature has %d value parameters, but alternative signature has %d",
          parameterDescriptors.size(), altFunDeclaration.getValueParameters().size());
    }

    List<ValueParameterDescriptor> altParamDescriptors = new ArrayList<ValueParameterDescriptor>();
    for (int i = 0, size = parameterDescriptors.size(); i < size; i++) {
      ValueParameterDescriptor originalParameterDescriptor = parameterDescriptors.get(i);
      JetParameter annotationValueParameter = altFunDeclaration.getValueParameters().get(i);

      //noinspection ConstantConditions
      JetTypeElement alternativeTypeElement =
          annotationValueParameter.getTypeReference().getTypeElement();
      assert alternativeTypeElement != null;

      JetType alternativeType;
      JetType alternativeVarargElementType;

      JetType originalParamVarargElementType = originalParameterDescriptor.getVarargElementType();
      if (originalParamVarargElementType == null) {
        if (annotationValueParameter.isVarArg()) {
          throw new AlternativeSignatureMismatchException(
              "Parameter in method signature is not vararg, but in alternative signature it is vararg");
        }

        alternativeType =
            TypeTransformingVisitor.computeType(
                alternativeTypeElement,
                originalParameterDescriptor.getType(),
                originalToAltTypeParameters,
                MEMBER_SIGNATURE_CONTRAVARIANT);
        alternativeVarargElementType = null;
      } else {
        if (!annotationValueParameter.isVarArg()) {
          throw new AlternativeSignatureMismatchException(
              "Parameter in method signature is vararg, but in alternative signature it is not");
        }

        alternativeVarargElementType =
            TypeTransformingVisitor.computeType(
                alternativeTypeElement, originalParamVarargElementType,
                originalToAltTypeParameters, MEMBER_SIGNATURE_CONTRAVARIANT);
        alternativeType = KotlinBuiltIns.getInstance().getArrayType(alternativeVarargElementType);
      }

      Name altName = annotationValueParameter.getNameAsName();

      altParamDescriptors.add(
          new ValueParameterDescriptorImpl(
              originalParameterDescriptor.getContainingDeclaration(),
              null,
              originalParameterDescriptor.getIndex(),
              originalParameterDescriptor.getAnnotations(),
              altName != null ? altName : originalParameterDescriptor.getName(),
              alternativeType,
              originalParameterDescriptor.declaresDefaultValue(),
              alternativeVarargElementType));
    }

    altValueParameters = altParamDescriptors;
  }
Esempio n. 12
0
  public static Icon getBaseIcon(PsiElement psiElement) {
    if (psiElement instanceof JetNamespaceHeader) {
      return PlatformIcons.PACKAGE_ICON;
    }

    if (psiElement instanceof KotlinLightClassForPackage) {
      return JetIcons.FILE;
    }

    if (psiElement instanceof KotlinLightClassForExplicitDeclaration) {
      psiElement = psiElement.getNavigationElement();
    }

    if (psiElement instanceof JetNamedFunction) {
      if (((JetFunction) psiElement).getReceiverTypeRef() != null) {
        return JetIcons.EXTENSION_FUNCTION;
      }

      if (PsiTreeUtil.getParentOfType(psiElement, JetNamedDeclaration.class) instanceof JetClass) {
        if (JetPsiUtil.isAbstract((JetFunction) psiElement)) {
          return PlatformIcons.ABSTRACT_METHOD_ICON;
        } else {
          return PlatformIcons.METHOD_ICON;
        }
      } else {
        return JetIcons.FUNCTION;
      }
    }
    if (psiElement instanceof JetClass) {
      JetClass jetClass = (JetClass) psiElement;
      if (jetClass.isTrait()) {
        return JetIcons.TRAIT;
      }

      Icon icon = jetClass.isEnum() ? PlatformIcons.ENUM_ICON : JetIcons.CLASS;
      if (jetClass instanceof JetEnumEntry) {
        JetEnumEntry enumEntry = (JetEnumEntry) jetClass;
        if (enumEntry.getPrimaryConstructorParameterList() == null) {
          icon = PlatformIcons.ENUM_ICON;
        }
      }
      return icon;
    }
    if (psiElement instanceof JetObjectDeclaration || psiElement instanceof JetClassObject) {
      return JetIcons.OBJECT;
    }
    if (psiElement instanceof JetParameter) {
      JetParameter parameter = (JetParameter) psiElement;
      if (parameter.getValOrVarNode() != null) {
        JetParameterList parameterList =
            PsiTreeUtil.getParentOfType(psiElement, JetParameterList.class);
        if (parameterList != null && parameterList.getParent() instanceof JetClass) {
          return parameter.isMutable() ? JetIcons.FIELD_VAR : JetIcons.FIELD_VAL;
        }
      }
      return JetIcons.PARAMETER;
    }
    if (psiElement instanceof JetProperty) {
      JetProperty property = (JetProperty) psiElement;
      return property.isVar() ? JetIcons.FIELD_VAR : JetIcons.FIELD_VAL;
    }

    return null;
  }