Exemple #1
0
  private static boolean isMoreSpecific(
      @NotNull CallableMemberDescriptor a, @NotNull CallableMemberDescriptor b) {
    if (a instanceof SimpleFunctionDescriptor) {
      assert b instanceof SimpleFunctionDescriptor : "b is " + b.getClass();

      KotlinType aReturnType = a.getReturnType();
      assert aReturnType != null;
      KotlinType bReturnType = b.getReturnType();
      assert bReturnType != null;

      return KotlinTypeChecker.DEFAULT.isSubtypeOf(aReturnType, bReturnType);
    }
    if (a instanceof PropertyDescriptor) {
      assert b instanceof PropertyDescriptor : "b is " + b.getClass();

      if (((PropertyDescriptor) a).isVar() || ((PropertyDescriptor) b).isVar()) {
        return ((PropertyDescriptor) a).isVar();
      }

      // both vals
      return KotlinTypeChecker.DEFAULT.isSubtypeOf(
          ((PropertyDescriptor) a).getType(), ((PropertyDescriptor) b).getType());
    }
    throw new IllegalArgumentException("Unexpected callable: " + a.getClass());
  }
  private VariableDescriptor createLoopParameterDescriptor(
      KtParameter loopParameter,
      KotlinType expectedParameterType,
      ExpressionTypingContext context) {
    components
        .modifiersChecker
        .withTrace(context.trace)
        .checkParameterHasNoValOrVar(loopParameter, VAL_OR_VAR_ON_LOOP_PARAMETER);

    KtTypeReference typeReference = loopParameter.getTypeReference();
    VariableDescriptor variableDescriptor;
    if (typeReference != null) {
      variableDescriptor =
          components.descriptorResolver.resolveLocalVariableDescriptor(
              context.scope, loopParameter, context.trace);
      KotlinType actualParameterType = variableDescriptor.getType();
      if (expectedParameterType != null
          && !KotlinTypeChecker.DEFAULT.isSubtypeOf(expectedParameterType, actualParameterType)) {
        context.trace.report(
            TYPE_MISMATCH_IN_FOR_LOOP.on(
                typeReference, expectedParameterType, actualParameterType));
      }
    } else {
      if (expectedParameterType == null) {
        expectedParameterType = ErrorUtils.createErrorType("Error");
      }
      variableDescriptor =
          components.descriptorResolver.resolveLocalVariableDescriptor(
              loopParameter, expectedParameterType, context.trace, context.scope);
    }

    checkVariableShadowing(context.scope, context.trace, variableDescriptor);

    return variableDescriptor;
  }
  /**
   * Two types are related, roughly, when one is a subtype or supertype of the other.
   *
   * <p>Note that some types have platform-specific counterparts, i.e. kotlin.String is mapped to
   * java.lang.String, such types (and all their sub- and supertypes) are related too.
   *
   * <p>Due to limitations in PlatformToKotlinClassMap, we only consider mapping of platform classes
   * to Kotlin classed (i.e. java.lang.String -> kotlin.String) and ignore mappings that go the
   * other way.
   */
  private static boolean isRelated(
      @NotNull KotlinType a,
      @NotNull KotlinType b,
      @NotNull PlatformToKotlinClassMap platformToKotlinClassMap) {
    List<KotlinType> aTypes =
        mapToPlatformIndependentTypes(TypeUtils.makeNotNullable(a), platformToKotlinClassMap);
    List<KotlinType> bTypes =
        mapToPlatformIndependentTypes(TypeUtils.makeNotNullable(b), platformToKotlinClassMap);

    for (KotlinType aType : aTypes) {
      for (KotlinType bType : bTypes) {
        if (KotlinTypeChecker.DEFAULT.isSubtypeOf(aType, bType)) return true;
        if (KotlinTypeChecker.DEFAULT.isSubtypeOf(bType, aType)) return true;
      }
    }

    return false;
  }
Exemple #4
0
 @NotNull
 public static KotlinType getPrimitiveNumberType(
     @NotNull IntegerValueTypeConstructor numberValueTypeConstructor,
     @NotNull KotlinType expectedType) {
   if (noExpectedType(expectedType) || expectedType.isError()) {
     return getDefaultPrimitiveNumberType(numberValueTypeConstructor);
   }
   for (KotlinType primitiveNumberType : numberValueTypeConstructor.getSupertypes()) {
     if (KotlinTypeChecker.DEFAULT.isSubtypeOf(primitiveNumberType, expectedType)) {
       return primitiveNumberType;
     }
   }
   return getDefaultPrimitiveNumberType(numberValueTypeConstructor);
 }
 public boolean isBooleanOrSubtype(@NotNull KotlinType type) {
   return KotlinTypeChecker.DEFAULT.isSubtypeOf(type, getBooleanType());
 }
Exemple #6
0
 public static boolean equalTypes(@NotNull KotlinType a, @NotNull KotlinType b) {
   return KotlinTypeChecker.DEFAULT.isSubtypeOf(a, b)
       && KotlinTypeChecker.DEFAULT.isSubtypeOf(b, a);
 }