Checks for primitive types and arrays made static in KotlinBuiltIns

This commit is contained in:
Andrey Breslav
2014-12-02 16:40:54 +03:00
parent b275b363c9
commit c21dd366e0
11 changed files with 41 additions and 27 deletions
@@ -851,7 +851,7 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
@Override
protected void assignToLoopParameter() {
Type arrayElParamType;
if (KotlinBuiltIns.getInstance().isArray(loopRangeType)) {
if (KotlinBuiltIns.isArray(loopRangeType)) {
arrayElParamType = boxType(asmElementType);
}
else {
@@ -3438,7 +3438,7 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
args.add(va.getArgumentExpression());
}
boolean isArray = KotlinBuiltIns.getInstance().isArray(arrayType);
boolean isArray = KotlinBuiltIns.isArray(arrayType);
if (!isArray && args.size() != 1) {
throw new CompilationException("primitive array constructor requires one argument", null, expression);
}
@@ -3492,7 +3492,7 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
}
public void newArrayInstruction(@NotNull JetType arrayType) {
if (KotlinBuiltIns.getInstance().isArray(arrayType)) {
if (KotlinBuiltIns.isArray(arrayType)) {
JetType elementJetType = arrayType.getArguments().get(0).getType();
putReifierMarkerIfTypeIsReifiedParameter(
elementJetType,
@@ -3519,7 +3519,7 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
operationDescriptor.getValueParameters().get(0).getType().equals(KotlinBuiltIns.getInstance().getIntType())) {
assert type != null;
Type elementType;
if (KotlinBuiltIns.getInstance().isArray(type)) {
if (KotlinBuiltIns.isArray(type)) {
JetType jetElementType = type.getArguments().get(0).getType();
elementType = boxType(asmType(jetElementType));
}
@@ -264,7 +264,7 @@ public class JetTypeMapper {
return asmType;
}
if (descriptor instanceof ClassDescriptor && KotlinBuiltIns.getInstance().isArray(jetType)) {
if (descriptor instanceof ClassDescriptor && KotlinBuiltIns.isArray(jetType)) {
if (jetType.getArguments().size() != 1) {
throw new UnsupportedOperationException("arrays must have one type argument");
}
@@ -683,11 +683,11 @@ public class JetTypeMapper {
*/
private static boolean forceBoxedReturnType(@NotNull FunctionDescriptor descriptor) {
//noinspection ConstantConditions
if (!KotlinBuiltIns.getInstance().isPrimitiveType(descriptor.getReturnType())) return false;
if (!KotlinBuiltIns.isPrimitiveType(descriptor.getReturnType())) return false;
for (FunctionDescriptor overridden : getAllOverriddenDescriptors(descriptor)) {
//noinspection ConstantConditions
if (!KotlinBuiltIns.getInstance().isPrimitiveType(overridden.getOriginal().getReturnType())) return true;
if (!KotlinBuiltIns.isPrimitiveType(overridden.getOriginal().getReturnType())) return true;
}
return false;
@@ -373,7 +373,7 @@ public class SignaturesPropagationData {
assert isArrayType(originalType);
if (builtIns.isPrimitiveArray(originalType)) {
if (KotlinBuiltIns.isPrimitiveArray(originalType)) {
// replace IntArray? with IntArray
return new VarargCheckResult(TypeUtils.makeNotNullable(originalType), true);
}
@@ -387,7 +387,7 @@ public class SignaturesPropagationData {
assert isArrayType(originalType);
if (builtIns.isPrimitiveArray(originalType)) {
if (KotlinBuiltIns.isPrimitiveArray(originalType)) {
// replace IntArray with IntArray?
return new VarargCheckResult(TypeUtils.makeNullable(originalType), false);
}
@@ -682,7 +682,7 @@ public class SignaturesPropagationData {
private static boolean isArrayType(@NotNull JetType type) {
KotlinBuiltIns builtIns = KotlinBuiltIns.getInstance();
return builtIns.isArray(type) || builtIns.isPrimitiveArray(type);
return KotlinBuiltIns.isArray(type) || KotlinBuiltIns.isPrimitiveArray(type);
}
private static class VarargCheckResult {
@@ -51,7 +51,7 @@ public class ConstantExpressionEvaluator private (val trace: BindingTrace) : Jet
DescriptorUtils.isClassObject(descriptor.getContainingDeclaration()) ||
DescriptorUtils.isStaticDeclaration(descriptor)) {
val returnType = descriptor.getType()
return KotlinBuiltIns.getInstance().isPrimitiveType(returnType) || KotlinBuiltIns.getInstance().getStringType() == returnType
return KotlinBuiltIns.isPrimitiveType(returnType) || KotlinBuiltIns.isString(returnType)
}
return false
}
@@ -72,13 +72,13 @@ public class CompileTimeConstantUtils {
if (isEnumClass(typeDescriptor) ||
isAnnotationClass(typeDescriptor) ||
isJavaLangClass(typeDescriptor) ||
builtIns.isPrimitiveArray(parameterType) ||
builtIns.isPrimitiveType(parameterType) ||
KotlinBuiltIns.isPrimitiveArray(parameterType) ||
KotlinBuiltIns.isPrimitiveType(parameterType) ||
builtIns.getStringType().equals(parameterType)) {
return true;
}
if (builtIns.isArray(parameterType)) {
if (KotlinBuiltIns.isArray(parameterType)) {
List<TypeProjection> arguments = parameterType.getArguments();
if (arguments.size() == 1) {
JetType arrayType = arguments.get(0).getType();
@@ -26,7 +26,6 @@ import org.jetbrains.jet.lang.psi.JetDeclaration;
import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.lang.psi.JetNamedFunction;
import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.lang.resolve.lazy.ResolveSession;
import org.jetbrains.jet.lang.types.JetType;
import org.jetbrains.jet.lang.types.TypeProjection;
import org.jetbrains.jet.lang.types.checker.JetTypeChecker;
@@ -69,7 +68,7 @@ public class MainFunctionDetector {
ValueParameterDescriptor parameter = parameters.get(0);
JetType parameterType = parameter.getType();
KotlinBuiltIns kotlinBuiltIns = KotlinBuiltIns.getInstance();
if (kotlinBuiltIns.isArray(parameterType)) {
if (KotlinBuiltIns.isArray(parameterType)) {
List<TypeProjection> typeArguments = parameterType.getArguments();
if (typeArguments.size() == 1) {
JetType typeArgument = typeArguments.get(0).getType();
@@ -308,7 +308,7 @@ class LazyJavaTypeResolver(
// foo(int) and foo(Integer)
// if we do not discriminate one of them, any call to foo(kotlin.Int) will result in overload resolution ambiguity
// so, for such cases, we discriminate Integer in favour of int
if (!KotlinBuiltIns.getInstance().isPrimitiveType(otherType) || !KotlinBuiltIns.getInstance().isPrimitiveType(lowerBound)) {
if (!KotlinBuiltIns.isPrimitiveType(otherType) || !KotlinBuiltIns.isPrimitiveType(lowerBound)) {
return Specificity.Relation.DONT_KNOW
}
// Int! >< Int?
@@ -410,7 +410,7 @@ public class DescriptorUtils {
if (type instanceof LazyType || type.isNullable()) return true;
KotlinBuiltIns builtIns = KotlinBuiltIns.getInstance();
return builtIns.isPrimitiveType(type) ||
return KotlinBuiltIns.isPrimitiveType(type) ||
JetTypeChecker.DEFAULT.equalTypes(builtIns.getStringType(), type) ||
JetTypeChecker.DEFAULT.equalTypes(builtIns.getNumber().getDefaultType(), type) ||
JetTypeChecker.DEFAULT.equalTypes(builtIns.getAnyType(), type);
@@ -155,11 +155,24 @@ public class KotlinBuiltIns {
public final FqNameUnsafe suppress = fqNameUnsafe("suppress");
public final FqNameUnsafe unit = fqNameUnsafe("Unit");
public final FqNameUnsafe string = fqNameUnsafe("String");
public final FqNameUnsafe array = fqNameUnsafe("Array");
public final FqName data = fqName("data");
public final FqName deprecated = fqName("deprecated");
public final FqName tailRecursive = fqName("tailRecursive");
public final FqName noinline = fqName("noinline");
public final Set<FqNameUnsafe> primitiveTypes;
public final Set<FqNameUnsafe> primitiveArrays;
{
primitiveTypes = new HashSet<FqNameUnsafe>(0);
primitiveArrays = new HashSet<FqNameUnsafe>(0);
for (PrimitiveType primitiveType : PrimitiveType.values()) {
primitiveTypes.add(fqNameUnsafe(primitiveType.getTypeName().asString()));
primitiveArrays.add(fqNameUnsafe(primitiveType.getArrayTypeName().asString()));
}
}
public final Set<FqNameUnsafe> functionClasses = computeIndexedFqNames("Function", FUNCTION_TRAIT_COUNT);
public final Set<FqNameUnsafe> extensionFunctionClasses = computeIndexedFqNames("ExtensionFunction", FUNCTION_TRAIT_COUNT);
@@ -612,7 +625,7 @@ public class KotlinBuiltIns {
@NotNull
public JetType getArrayElementType(@NotNull JetType arrayType) {
if (arrayType.getConstructor().getDeclarationDescriptor() == getArray()) {
if (isArray(arrayType)) {
if (arrayType.getArguments().size() != 1) {
throw new IllegalStateException();
}
@@ -725,16 +738,18 @@ public class KotlinBuiltIns {
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public boolean isArray(@NotNull JetType type) {
return getArray().equals(type.getConstructor().getDeclarationDescriptor());
public static boolean isArray(@NotNull JetType type) {
return isConstructedFromGivenClass(type, FQ_NAMES.array);
}
public boolean isPrimitiveArray(@NotNull JetType type) {
return jetArrayTypeToPrimitiveJetType.containsKey(TypeUtils.makeNotNullable(type));
public static boolean isPrimitiveArray(@NotNull JetType type) {
ClassifierDescriptor descriptor = type.getConstructor().getDeclarationDescriptor();
return descriptor != null && FQ_NAMES.primitiveArrays.contains(DescriptorUtils.getFqName(descriptor));
}
public boolean isPrimitiveType(@NotNull JetType type) {
return primitiveJetTypeToJetArrayType.containsKey(type);
public static boolean isPrimitiveType(@NotNull JetType type) {
ClassifierDescriptor descriptor = type.getConstructor().getDeclarationDescriptor();
return !type.isNullable() && descriptor != null && FQ_NAMES.primitiveTypes.contains(DescriptorUtils.getFqName(descriptor));
}
// Functions
@@ -51,7 +51,7 @@ fun generate(): String {
val builtIns = KotlinBuiltIns.getInstance()
[suppress("UNCHECKED_CAST")]
val allPrimitiveTypes = builtIns.getBuiltInsPackageScope().getDescriptors()
.filter { it is ClassDescriptor && builtIns.isPrimitiveType(it.getDefaultType()) } as List<ClassDescriptor>
.filter { it is ClassDescriptor && KotlinBuiltIns.isPrimitiveType(it.getDefaultType()) } as List<ClassDescriptor>
for (descriptor in allPrimitiveTypes + builtIns.getString()) {
[suppress("UNCHECKED_CAST")]
@@ -44,7 +44,7 @@ public class ReplaceWithOperatorAssignIntention : JetSelfTargetingIntention<JetB
fun checkExpressionRepeat(variableExpression: JetSimpleNameExpression, expression: JetBinaryExpression): Boolean {
val context = expression.analyze()
val descriptor = context[BindingContext.REFERENCE_TARGET, expression.getOperationReference()]?.getContainingDeclaration()
val isPrimitiveOperation = descriptor is ClassDescriptor && KotlinBuiltIns.getInstance().isPrimitiveType(descriptor.getDefaultType())
val isPrimitiveOperation = descriptor is ClassDescriptor && KotlinBuiltIns.isPrimitiveType(descriptor.getDefaultType())
return when {
variableExpression.matches(expression.getLeft()) -> {