Methods in KotlinBuiltIns made static where already possible

This commit is contained in:
Andrey Breslav
2014-12-02 15:50:16 +03:00
parent 75c887048e
commit ea4f0ab214
25 changed files with 68 additions and 72 deletions
@@ -232,7 +232,7 @@ public class DiagnosticsWithSuppression implements Diagnostics {
builder.addAll(suppressStringProvider.get(annotationDescriptor)); builder.addAll(suppressStringProvider.get(annotationDescriptor));
} }
if (!KotlinBuiltIns.getInstance().isSuppressAnnotation(annotationDescriptor)) continue; if (!KotlinBuiltIns.isSuppressAnnotation(annotationDescriptor)) continue;
// We only add strings and skip other values to facilitate recovery in presence of erroneous code // We only add strings and skip other values to facilitate recovery in presence of erroneous code
for (CompileTimeConstant<?> arrayValue : annotationDescriptor.getAllValueArguments().values()) { for (CompileTimeConstant<?> arrayValue : annotationDescriptor.getAllValueArguments().values()) {
@@ -98,12 +98,12 @@ public class FunctionDescriptorUtil {
@NotNull Visibility visibility @NotNull Visibility visibility
) { ) {
assert KotlinBuiltIns.getInstance().isFunctionOrExtensionFunctionType(functionType); assert KotlinBuiltIns.isFunctionOrExtensionFunctionType(functionType);
functionDescriptor.initialize(KotlinBuiltIns.getInstance().getReceiverType(functionType), functionDescriptor.initialize(KotlinBuiltIns.getReceiverType(functionType),
dispatchReceiverParameter, dispatchReceiverParameter,
Collections.<TypeParameterDescriptorImpl>emptyList(), Collections.<TypeParameterDescriptorImpl>emptyList(),
KotlinBuiltIns.getInstance().getValueParameters(functionDescriptor, functionType), KotlinBuiltIns.getValueParameters(functionDescriptor, functionType),
KotlinBuiltIns.getInstance().getReturnTypeFromFunctionType(functionType), KotlinBuiltIns.getReturnTypeFromFunctionType(functionType),
modality, modality,
visibility); visibility);
} }
@@ -78,7 +78,7 @@ public class ArgumentTypeResolver {
} }
private static boolean isFunctionOrErrorType(@NotNull JetType supertype) { private static boolean isFunctionOrErrorType(@NotNull JetType supertype) {
return KotlinBuiltIns.getInstance().isFunctionOrExtensionFunctionType(supertype) || supertype.isError(); return KotlinBuiltIns.isFunctionOrExtensionFunctionType(supertype) || supertype.isError();
} }
public void checkTypesWithNoCallee(@NotNull CallResolutionContext<?> context) { public void checkTypesWithNoCallee(@NotNull CallResolutionContext<?> context) {
@@ -42,7 +42,7 @@ public class CallResolverUtil {
public static boolean hasUnknownFunctionParameter(@NotNull JetType type) { public static boolean hasUnknownFunctionParameter(@NotNull JetType type) {
assert KotlinBuiltIns.getInstance().isFunctionOrExtensionFunctionType(type); assert KotlinBuiltIns.isFunctionOrExtensionFunctionType(type);
List<TypeProjection> arguments = type.getArguments(); List<TypeProjection> arguments = type.getArguments();
// last argument is return type of function type // last argument is return type of function type
List<TypeProjection> functionParameters = arguments.subList(0, arguments.size() - 1); List<TypeProjection> functionParameters = arguments.subList(0, arguments.size() - 1);
@@ -56,13 +56,13 @@ public class CallResolverUtil {
} }
public static boolean hasUnknownReturnType(@NotNull JetType type) { public static boolean hasUnknownReturnType(@NotNull JetType type) {
assert KotlinBuiltIns.getInstance().isFunctionOrExtensionFunctionType(type); assert KotlinBuiltIns.isFunctionOrExtensionFunctionType(type);
JetType returnTypeFromFunctionType = KotlinBuiltIns.getInstance().getReturnTypeFromFunctionType(type); JetType returnTypeFromFunctionType = KotlinBuiltIns.getReturnTypeFromFunctionType(type);
return ErrorUtils.containsErrorType(returnTypeFromFunctionType); return ErrorUtils.containsErrorType(returnTypeFromFunctionType);
} }
public static JetType replaceReturnTypeByUnknown(@NotNull JetType type) { public static JetType replaceReturnTypeByUnknown(@NotNull JetType type) {
assert KotlinBuiltIns.getInstance().isFunctionOrExtensionFunctionType(type); assert KotlinBuiltIns.isFunctionOrExtensionFunctionType(type);
List<TypeProjection> arguments = type.getArguments(); List<TypeProjection> arguments = type.getArguments();
List<TypeProjection> newArguments = Lists.newArrayList(); List<TypeProjection> newArguments = Lists.newArrayList();
newArguments.addAll(arguments.subList(0, arguments.size() - 1)); newArguments.addAll(arguments.subList(0, arguments.size() - 1));
@@ -250,7 +250,7 @@ public class CandidateResolver {
if (expectedType == null || TypeUtils.isDontCarePlaceholder(expectedType)) { if (expectedType == null || TypeUtils.isDontCarePlaceholder(expectedType)) {
expectedType = argumentTypeResolver.getShapeTypeOfFunctionLiteral(functionLiteralExpression, context.scope, context.trace, false); expectedType = argumentTypeResolver.getShapeTypeOfFunctionLiteral(functionLiteralExpression, context.scope, context.trace, false);
} }
if (expectedType == null || !KotlinBuiltIns.getInstance().isFunctionOrExtensionFunctionType(expectedType) if (expectedType == null || !KotlinBuiltIns.isFunctionOrExtensionFunctionType(expectedType)
|| CallResolverUtil.hasUnknownFunctionParameter(expectedType)) { || CallResolverUtil.hasUnknownFunctionParameter(expectedType)) {
return; return;
} }
@@ -221,7 +221,7 @@ public class InlineCallResolverExtension implements CallResolverExtension {
private static boolean isInlinableParameter(@NotNull CallableDescriptor descriptor) { private static boolean isInlinableParameter(@NotNull CallableDescriptor descriptor) {
JetType type = descriptor.getReturnType(); JetType type = descriptor.getReturnType();
return type != null && return type != null &&
KotlinBuiltIns.getInstance().isExactFunctionOrExtensionFunctionType(type) && KotlinBuiltIns.isExactFunctionOrExtensionFunctionType(type) &&
!type.isNullable() && !type.isNullable() &&
!InlineUtil.hasNoinlineAnnotation(descriptor); !InlineUtil.hasNoinlineAnnotation(descriptor);
} }
@@ -234,7 +234,7 @@ public class InlineCallResolverExtension implements CallResolverExtension {
DeclarationDescriptor containingDeclaration = descriptor.getContainingDeclaration(); DeclarationDescriptor containingDeclaration = descriptor.getContainingDeclaration();
boolean isInvoke = descriptor.getName().asString().equals("invoke") && boolean isInvoke = descriptor.getName().asString().equals("invoke") &&
containingDeclaration instanceof ClassDescriptor && containingDeclaration instanceof ClassDescriptor &&
KotlinBuiltIns.getInstance().isExactFunctionOrExtensionFunctionType(((ClassDescriptor) containingDeclaration).getDefaultType()); KotlinBuiltIns.isExactFunctionOrExtensionFunctionType(((ClassDescriptor) containingDeclaration).getDefaultType());
return isInvoke || return isInvoke ||
//or inline extension //or inline extension
@@ -54,7 +54,7 @@ private fun getSuperClassesReachableByClassInheritance(
): Set<ClassDescriptor> { ): Set<ClassDescriptor> {
val superClass = getSuperClass(descriptor) val superClass = getSuperClass(descriptor)
result.add(superClass) result.add(superClass)
if (!KotlinBuiltIns.getInstance().isAny(superClass)) { if (!KotlinBuiltIns.isAny(superClass)) {
getSuperClassesReachableByClassInheritance(superClass, result) getSuperClassesReachableByClassInheritance(superClass, result)
} }
return result return result
@@ -91,7 +91,7 @@ public class TracingStrategyForInvoke extends AbstractTracingStrategy {
} }
private void functionExpectedOrNoReceiverAllowed(BindingTrace trace) { private void functionExpectedOrNoReceiverAllowed(BindingTrace trace) {
if (KotlinBuiltIns.getInstance().isFunctionType(calleeType)) { if (KotlinBuiltIns.isFunctionType(calleeType)) {
LOG.assertTrue(call.getExplicitReceiver().exists(), LOG.assertTrue(call.getExplicitReceiver().exists(),
"'Invoke' is not found on expression of function type (" + calleeType + "): " "'Invoke' is not found on expression of function type (" + calleeType + "): "
+ PsiUtilPackage.getTextWithLocation(call.getCallElement())); + PsiUtilPackage.getTextWithLocation(call.getCallElement()));
@@ -149,9 +149,8 @@ public class InlineAnalyzerExtension implements FunctionAnalyzerExtension.Analyz
@NotNull CallableDescriptor functionDescriptor, @NotNull CallableDescriptor functionDescriptor,
@Nullable BindingTrace trace @Nullable BindingTrace trace
) { ) {
KotlinBuiltIns builtIns = KotlinBuiltIns.getInstance();
JetType type = parameter.getReturnType(); JetType type = parameter.getReturnType();
if (type != null && builtIns.isExactFunctionOrExtensionFunctionType(type)) { if (type != null && KotlinBuiltIns.isExactFunctionOrExtensionFunctionType(type)) {
if (!InlineUtil.hasNoinlineAnnotation(parameter)) { if (!InlineUtil.hasNoinlineAnnotation(parameter)) {
if (type.isNullable()) { if (type.isNullable()) {
if (trace != null) { if (trace != null) {
@@ -500,7 +500,7 @@ public class LazyClassDescriptor extends ClassDescriptorBase implements ClassDes
new Function0<Supertypes>() { new Function0<Supertypes>() {
@Override @Override
public Supertypes invoke() { public Supertypes invoke() {
if (KotlinBuiltIns.getInstance().isSpecialClassWithNoSupertypes(LazyClassDescriptor.this)) { if (KotlinBuiltIns.isSpecialClassWithNoSupertypes(LazyClassDescriptor.this)) {
return new Supertypes(Collections.<JetType>emptyList()); return new Supertypes(Collections.<JetType>emptyList());
} }
@@ -107,7 +107,7 @@ public class ClosureExpressionsTypingVisitor extends ExpressionTypingVisitor {
if (!expression.getFunctionLiteral().hasBody()) return null; if (!expression.getFunctionLiteral().hasBody()) return null;
JetType expectedType = context.expectedType; JetType expectedType = context.expectedType;
boolean functionTypeExpected = !noExpectedType(expectedType) && KotlinBuiltIns.getInstance().isFunctionOrExtensionFunctionType( boolean functionTypeExpected = !noExpectedType(expectedType) && KotlinBuiltIns.isFunctionOrExtensionFunctionType(
expectedType); expectedType);
AnonymousFunctionDescriptor functionDescriptor = createFunctionDescriptor(expression, context, functionTypeExpected); AnonymousFunctionDescriptor functionDescriptor = createFunctionDescriptor(expression, context, functionTypeExpected);
@@ -118,7 +118,7 @@ public class ClosureExpressionsTypingVisitor extends ExpressionTypingVisitor {
List<JetType> valueParametersTypes = ExpressionTypingUtils.getValueParametersTypes(functionDescriptor.getValueParameters()); List<JetType> valueParametersTypes = ExpressionTypingUtils.getValueParametersTypes(functionDescriptor.getValueParameters());
JetType resultType = KotlinBuiltIns.getInstance().getFunctionType( JetType resultType = KotlinBuiltIns.getInstance().getFunctionType(
Annotations.EMPTY, receiver, valueParametersTypes, safeReturnType); Annotations.EMPTY, receiver, valueParametersTypes, safeReturnType);
if (!noExpectedType(expectedType) && KotlinBuiltIns.getInstance().isFunctionOrExtensionFunctionType(expectedType)) { if (!noExpectedType(expectedType) && KotlinBuiltIns.isFunctionOrExtensionFunctionType(expectedType)) {
// all checks were done before // all checks were done before
return JetTypeInfo.create(resultType, context.dataFlowInfo); return JetTypeInfo.create(resultType, context.dataFlowInfo);
} }
@@ -145,7 +145,7 @@ public class ClosureExpressionsTypingVisitor extends ExpressionTypingVisitor {
JetType effectiveReceiverType; JetType effectiveReceiverType;
if (receiverTypeRef == null) { if (receiverTypeRef == null) {
if (functionTypeExpected) { if (functionTypeExpected) {
effectiveReceiverType = KotlinBuiltIns.getInstance().getReceiverType(context.expectedType); effectiveReceiverType = KotlinBuiltIns.getReceiverType(context.expectedType);
} }
else { else {
effectiveReceiverType = null; effectiveReceiverType = null;
@@ -177,7 +177,7 @@ public class ClosureExpressionsTypingVisitor extends ExpressionTypingVisitor {
List<JetParameter> declaredValueParameters = functionLiteral.getValueParameters(); List<JetParameter> declaredValueParameters = functionLiteral.getValueParameters();
List<ValueParameterDescriptor> expectedValueParameters = (functionTypeExpected) List<ValueParameterDescriptor> expectedValueParameters = (functionTypeExpected)
? KotlinBuiltIns.getInstance().getValueParameters(functionDescriptor, context.expectedType) ? KotlinBuiltIns.getValueParameters(functionDescriptor, context.expectedType)
: null; : null;
JetParameterList valueParameterList = functionLiteral.getValueParameterList(); JetParameterList valueParameterList = functionLiteral.getValueParameterList();
@@ -261,7 +261,7 @@ public class ClosureExpressionsTypingVisitor extends ExpressionTypingVisitor {
@NotNull SimpleFunctionDescriptorImpl functionDescriptor, @NotNull SimpleFunctionDescriptorImpl functionDescriptor,
boolean functionTypeExpected boolean functionTypeExpected
) { ) {
JetType expectedReturnType = functionTypeExpected ? KotlinBuiltIns.getInstance().getReturnTypeFromFunctionType(context.expectedType) : null; JetType expectedReturnType = functionTypeExpected ? KotlinBuiltIns.getReturnTypeFromFunctionType(context.expectedType) : null;
JetType returnType = computeUnsafeReturnType(expression, context, functionDescriptor, expectedReturnType); JetType returnType = computeUnsafeReturnType(expression, context, functionDescriptor, expectedReturnType);
if (!expression.getFunctionLiteral().hasDeclaredReturnType() && functionTypeExpected) { if (!expression.getFunctionLiteral().hasDeclaredReturnType() && functionTypeExpected) {
@@ -236,7 +236,7 @@ public class DescriptorValidator {
Collection<JetType> supertypes = descriptor.getTypeConstructor().getSupertypes(); Collection<JetType> supertypes = descriptor.getTypeConstructor().getSupertypes();
if (supertypes.isEmpty() && descriptor.getKind() != ClassKind.TRAIT if (supertypes.isEmpty() && descriptor.getKind() != ClassKind.TRAIT
&& !KotlinBuiltIns.getInstance().isSpecialClassWithNoSupertypes(descriptor)) { && !KotlinBuiltIns.isSpecialClassWithNoSupertypes(descriptor)) {
report(collector, descriptor, "No supertypes for non-trait"); report(collector, descriptor, "No supertypes for non-trait");
} }
validateTypes(descriptor, collector, supertypes); validateTypes(descriptor, collector, supertypes);
@@ -367,9 +367,8 @@ public class ConstraintSystemImpl implements ConstraintSystem {
assert superType != TypeUtils.PLACEHOLDER_FUNCTION_TYPE : "The type for " + constraintPosition + " shouldn't be a placeholder for function type"; assert superType != TypeUtils.PLACEHOLDER_FUNCTION_TYPE : "The type for " + constraintPosition + " shouldn't be a placeholder for function type";
KotlinBuiltIns kotlinBuiltIns = KotlinBuiltIns.getInstance();
if (subType == TypeUtils.PLACEHOLDER_FUNCTION_TYPE) { if (subType == TypeUtils.PLACEHOLDER_FUNCTION_TYPE) {
if (!kotlinBuiltIns.isFunctionOrExtensionFunctionType(superType)) { if (!KotlinBuiltIns.isFunctionOrExtensionFunctionType(superType)) {
if (isMyTypeVariable(superType)) { if (isMyTypeVariable(superType)) {
// a constraint binds type parameter and any function type, so there is no new info and no error // a constraint binds type parameter and any function type, so there is no new info and no error
return; return;
@@ -383,7 +382,7 @@ public class ConstraintSystemImpl implements ConstraintSystem {
// function literal without declaring receiver type { x -> ... } // function literal without declaring receiver type { x -> ... }
// can be considered as extension function if one is expected // can be considered as extension function if one is expected
// (special type constructor for function/ extension function should be introduced like PLACEHOLDER_FUNCTION_TYPE) // (special type constructor for function/ extension function should be introduced like PLACEHOLDER_FUNCTION_TYPE)
if (constraintKind == SUB_TYPE && kotlinBuiltIns.isFunctionType(subType) && kotlinBuiltIns.isExtensionFunctionType(superType)) { if (constraintKind == SUB_TYPE && KotlinBuiltIns.isFunctionType(subType) && KotlinBuiltIns.isExtensionFunctionType(superType)) {
subType = createCorrespondingExtensionFunctionType(subType, DONT_CARE); subType = createCorrespondingExtensionFunctionType(subType, DONT_CARE);
} }
@@ -522,7 +521,7 @@ public class ConstraintSystemImpl implements ConstraintSystem {
@NotNull @NotNull
public static JetType createCorrespondingExtensionFunctionType(@NotNull JetType functionType, @NotNull JetType receiverType) { public static JetType createCorrespondingExtensionFunctionType(@NotNull JetType functionType, @NotNull JetType receiverType) {
assert KotlinBuiltIns.getInstance().isFunctionType(functionType); assert KotlinBuiltIns.isFunctionType(functionType);
List<TypeProjection> typeArguments = functionType.getArguments(); List<TypeProjection> typeArguments = functionType.getArguments();
assert !typeArguments.isEmpty(); assert !typeArguments.isEmpty();
@@ -103,7 +103,7 @@ public class KotlinBuiltIns {
private final Map<JetType, JetType> primitiveJetTypeToJetArrayType; private final Map<JetType, JetType> primitiveJetTypeToJetArrayType;
private final Map<JetType, JetType> jetArrayTypeToPrimitiveJetType; private final Map<JetType, JetType> jetArrayTypeToPrimitiveJetType;
private final FqNames fqNames = new FqNames(); private static final FqNames FQ_NAMES = new FqNames();
private KotlinBuiltIns() { private KotlinBuiltIns() {
builtInsModule = new ModuleDescriptorImpl( builtInsModule = new ModuleDescriptorImpl(
@@ -734,11 +734,11 @@ public class KotlinBuiltIns {
// Functions // Functions
public boolean isFunctionOrExtensionFunctionType(@NotNull JetType type) { public static boolean isFunctionOrExtensionFunctionType(@NotNull JetType type) {
return isFunctionType(type) || isExtensionFunctionType(type); return isFunctionType(type) || isExtensionFunctionType(type);
} }
public boolean isFunctionType(@NotNull JetType type) { public static boolean isFunctionType(@NotNull JetType type) {
if (isExactFunctionType(type)) return true; if (isExactFunctionType(type)) return true;
for (JetType superType : type.getConstructor().getSupertypes()) { for (JetType superType : type.getConstructor().getSupertypes()) {
@@ -748,7 +748,7 @@ public class KotlinBuiltIns {
return false; return false;
} }
public boolean isExtensionFunctionType(@NotNull JetType type) { public static boolean isExtensionFunctionType(@NotNull JetType type) {
if (isExactExtensionFunctionType(type)) return true; if (isExactExtensionFunctionType(type)) return true;
for (JetType superType : type.getConstructor().getSupertypes()) { for (JetType superType : type.getConstructor().getSupertypes()) {
@@ -758,16 +758,16 @@ public class KotlinBuiltIns {
return false; return false;
} }
public boolean isExactFunctionOrExtensionFunctionType(@NotNull JetType type) { public static boolean isExactFunctionOrExtensionFunctionType(@NotNull JetType type) {
return isExactFunctionType(type) || isExactExtensionFunctionType(type); return isExactFunctionType(type) || isExactExtensionFunctionType(type);
} }
public boolean isExactFunctionType(@NotNull JetType type) { public static boolean isExactFunctionType(@NotNull JetType type) {
return isTypeConstructorFqNameInSet(type, fqNames.functionClasses); return isTypeConstructorFqNameInSet(type, FQ_NAMES.functionClasses);
} }
public boolean isExactExtensionFunctionType(@NotNull JetType type) { public static boolean isExactExtensionFunctionType(@NotNull JetType type) {
return isTypeConstructorFqNameInSet(type, fqNames.extensionFunctionClasses); return isTypeConstructorFqNameInSet(type, FQ_NAMES.extensionFunctionClasses);
} }
private static boolean isTypeConstructorFqNameInSet(@NotNull JetType type, @NotNull Set<FqNameUnsafe> classes) { private static boolean isTypeConstructorFqNameInSet(@NotNull JetType type, @NotNull Set<FqNameUnsafe> classes) {
@@ -780,7 +780,7 @@ public class KotlinBuiltIns {
} }
@Nullable @Nullable
public JetType getReceiverType(@NotNull JetType type) { public static JetType getReceiverType(@NotNull JetType type) {
assert isFunctionOrExtensionFunctionType(type) : type; assert isFunctionOrExtensionFunctionType(type) : type;
if (isExtensionFunctionType(type)) { if (isExtensionFunctionType(type)) {
return type.getArguments().get(0).getType(); return type.getArguments().get(0).getType();
@@ -789,7 +789,7 @@ public class KotlinBuiltIns {
} }
@NotNull @NotNull
public List<ValueParameterDescriptor> getValueParameters(@NotNull FunctionDescriptor functionDescriptor, @NotNull JetType type) { public static List<ValueParameterDescriptor> getValueParameters(@NotNull FunctionDescriptor functionDescriptor, @NotNull JetType type) {
assert isFunctionOrExtensionFunctionType(type); assert isFunctionOrExtensionFunctionType(type);
List<TypeProjection> parameterTypes = getParameterTypeProjectionsFromFunctionType(type); List<TypeProjection> parameterTypes = getParameterTypeProjectionsFromFunctionType(type);
List<ValueParameterDescriptor> valueParameters = new ArrayList<ValueParameterDescriptor>(parameterTypes.size()); List<ValueParameterDescriptor> valueParameters = new ArrayList<ValueParameterDescriptor>(parameterTypes.size());
@@ -805,14 +805,14 @@ public class KotlinBuiltIns {
} }
@NotNull @NotNull
public JetType getReturnTypeFromFunctionType(@NotNull JetType type) { public static JetType getReturnTypeFromFunctionType(@NotNull JetType type) {
assert isFunctionOrExtensionFunctionType(type); assert isFunctionOrExtensionFunctionType(type);
List<TypeProjection> arguments = type.getArguments(); List<TypeProjection> arguments = type.getArguments();
return arguments.get(arguments.size() - 1).getType(); return arguments.get(arguments.size() - 1).getType();
} }
@NotNull @NotNull
public List<TypeProjection> getParameterTypeProjectionsFromFunctionType(@NotNull JetType type) { public static List<TypeProjection> getParameterTypeProjectionsFromFunctionType(@NotNull JetType type) {
assert isFunctionOrExtensionFunctionType(type); assert isFunctionOrExtensionFunctionType(type);
List<TypeProjection> arguments = type.getArguments(); List<TypeProjection> arguments = type.getArguments();
int first = isExtensionFunctionType(type) ? 1 : 0; int first = isExtensionFunctionType(type) ? 1 : 0;
@@ -826,13 +826,13 @@ public class KotlinBuiltIns {
// Recognized & special // Recognized & special
public boolean isSpecialClassWithNoSupertypes(@NotNull ClassDescriptor descriptor) { public static boolean isSpecialClassWithNoSupertypes(@NotNull ClassDescriptor descriptor) {
FqNameUnsafe fqName = DescriptorUtils.getFqName(descriptor); FqNameUnsafe fqName = DescriptorUtils.getFqName(descriptor);
return fqNames.any.equals(fqName) || fqNames.nothing.equals(fqName); return FQ_NAMES.any.equals(fqName) || FQ_NAMES.nothing.equals(fqName);
} }
public boolean isAny(@NotNull ClassDescriptor descriptor) { public static boolean isAny(@NotNull ClassDescriptor descriptor) {
return fqNames.any.equals(DescriptorUtils.getFqName(descriptor)); return FQ_NAMES.any.equals(DescriptorUtils.getFqName(descriptor));
} }
public boolean isNothing(@NotNull JetType type) { public boolean isNothing(@NotNull JetType type) {
@@ -861,8 +861,8 @@ public class KotlinBuiltIns {
return getStringType().equals(type); return getStringType().equals(type);
} }
public boolean isCloneable(@NotNull ClassDescriptor descriptor) { public static boolean isCloneable(@NotNull ClassDescriptor descriptor) {
return fqNames.cloneable.equals(DescriptorUtils.getFqName(descriptor)); return FQ_NAMES.cloneable.equals(DescriptorUtils.getFqName(descriptor));
} }
public boolean isData(@NotNull ClassDescriptor classDescriptor) { public boolean isData(@NotNull ClassDescriptor classDescriptor) {
@@ -877,9 +877,9 @@ public class KotlinBuiltIns {
return containsAnnotation(declarationDescriptor, getTailRecursiveAnnotationClass()); return containsAnnotation(declarationDescriptor, getTailRecursiveAnnotationClass());
} }
public boolean isSuppressAnnotation(@NotNull AnnotationDescriptor annotationDescriptor) { public static boolean isSuppressAnnotation(@NotNull AnnotationDescriptor annotationDescriptor) {
ClassifierDescriptor classifier = annotationDescriptor.getType().getConstructor().getDeclarationDescriptor(); ClassifierDescriptor classifier = annotationDescriptor.getType().getConstructor().getDeclarationDescriptor();
return classifier != null && fqNames.suppress.equals(DescriptorUtils.getFqName(classifier)); return classifier != null && FQ_NAMES.suppress.equals(DescriptorUtils.getFqName(classifier));
} }
static boolean containsAnnotation(DeclarationDescriptor descriptor, ClassDescriptor annotationClass) { static boolean containsAnnotation(DeclarationDescriptor descriptor, ClassDescriptor annotationClass) {
@@ -43,7 +43,6 @@ import org.jetbrains.jet.utils.UtilsPackage;
import java.util.*; import java.util.*;
import static org.jetbrains.jet.lang.types.TypeUtils.CANT_INFER_LAMBDA_PARAM_TYPE; import static org.jetbrains.jet.lang.types.TypeUtils.CANT_INFER_LAMBDA_PARAM_TYPE;
import static org.jetbrains.jet.lang.types.TypeUtils.DONT_CARE;
public class DescriptorRendererImpl implements DescriptorRenderer { public class DescriptorRendererImpl implements DescriptorRenderer {
@@ -360,7 +359,7 @@ public class DescriptorRendererImpl implements DescriptorRenderer {
} }
private boolean shouldRenderAsPrettyFunctionType(@NotNull JetType type) { private boolean shouldRenderAsPrettyFunctionType(@NotNull JetType type) {
return KotlinBuiltIns.getInstance().isExactFunctionOrExtensionFunctionType(type) && prettyFunctionTypes; return KotlinBuiltIns.isExactFunctionOrExtensionFunctionType(type) && prettyFunctionTypes;
} }
@NotNull @NotNull
@@ -484,16 +483,16 @@ public class DescriptorRendererImpl implements DescriptorRenderer {
private String renderFunctionType(@NotNull JetType type) { private String renderFunctionType(@NotNull JetType type) {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
JetType receiverType = KotlinBuiltIns.getInstance().getReceiverType(type); JetType receiverType = KotlinBuiltIns.getReceiverType(type);
if (receiverType != null) { if (receiverType != null) {
sb.append(renderNormalizedType(receiverType)); sb.append(renderNormalizedType(receiverType));
sb.append("."); sb.append(".");
} }
sb.append("("); sb.append("(");
appendTypeProjections(KotlinBuiltIns.getInstance().getParameterTypeProjectionsFromFunctionType(type), sb); appendTypeProjections(KotlinBuiltIns.getParameterTypeProjectionsFromFunctionType(type), sb);
sb.append(") ").append(arrow()).append(" "); sb.append(") ").append(arrow()).append(" ");
sb.append(renderNormalizedType(KotlinBuiltIns.getInstance().getReturnTypeFromFunctionType(type))); sb.append(renderNormalizedType(KotlinBuiltIns.getReturnTypeFromFunctionType(type)));
if (type.isNullable()) { if (type.isNullable()) {
return "(" + sb + ")?"; return "(" + sb + ")?";
@@ -91,7 +91,7 @@ public class DescriptorSerializer {
builder.addTypeParameter(typeParameter(typeParameterDescriptor)); builder.addTypeParameter(typeParameter(typeParameterDescriptor));
} }
if (!KotlinBuiltIns.getInstance().isSpecialClassWithNoSupertypes(classDescriptor)) { if (!KotlinBuiltIns.isSpecialClassWithNoSupertypes(classDescriptor)) {
// Special classes (Any, Nothing) have no supertypes // Special classes (Any, Nothing) have no supertypes
for (JetType supertype : classDescriptor.getTypeConstructor().getSupertypes()) { for (JetType supertype : classDescriptor.getTypeConstructor().getSupertypes()) {
builder.addSupertype(type(supertype)); builder.addSupertype(type(supertype));
@@ -103,15 +103,14 @@ public class FunctionsHighlightingVisitor extends AfterAnalysisHighlightingVisit
return false; return false;
} }
KotlinBuiltIns builtIns = KotlinBuiltIns.getInstance();
JetType defaultType = ((ClassDescriptor) parent).getDefaultType(); JetType defaultType = ((ClassDescriptor) parent).getDefaultType();
if (builtIns.isFunctionOrExtensionFunctionType(defaultType)) { if (KotlinBuiltIns.isFunctionOrExtensionFunctionType(defaultType)) {
return true; return true;
} }
for (JetType supertype : TypeUtils.getAllSupertypes(defaultType)) { for (JetType supertype : TypeUtils.getAllSupertypes(defaultType)) {
if (builtIns.isFunctionOrExtensionFunctionType(supertype)) { if (KotlinBuiltIns.isFunctionOrExtensionFunctionType(supertype)) {
return true; return true;
} }
} }
@@ -135,7 +135,7 @@ public class KotlinSuppressIntentionAction(
val context = annotated.analyze() val context = annotated.analyze()
for (entry in annotated.getAnnotationEntries()) { for (entry in annotated.getAnnotationEntries()) {
val annotationDescriptor = context.get(BindingContext.ANNOTATION, entry) val annotationDescriptor = context.get(BindingContext.ANNOTATION, entry)
if (annotationDescriptor != null && KotlinBuiltIns.getInstance().isSuppressAnnotation(annotationDescriptor)) { if (annotationDescriptor != null && KotlinBuiltIns.isSuppressAnnotation(annotationDescriptor)) {
return entry return entry
} }
} }
@@ -130,7 +130,7 @@ public class FindImplicitNothingAction : AnAction() {
return when { return when {
builtIns.isNothing(this) -> true builtIns.isNothing(this) -> true
builtIns.isExactFunctionOrExtensionFunctionType(this) -> builtIns.getReturnTypeFromFunctionType(this).isNothingOrNothingFunctionType() KotlinBuiltIns.isExactFunctionOrExtensionFunctionType(this) -> KotlinBuiltIns.getReturnTypeFromFunctionType(this).isNothingOrNothingFunctionType()
else -> false else -> false
} }
@@ -133,8 +133,8 @@ public open class LookupElementFactory protected() {
1 -> { 1 -> {
val parameterType = parameters.single().getType() val parameterType = parameters.single().getType()
if (KotlinBuiltIns.getInstance().isFunctionOrExtensionFunctionType(parameterType)) { if (KotlinBuiltIns.isFunctionOrExtensionFunctionType(parameterType)) {
val parameterCount = KotlinBuiltIns.getInstance().getParameterTypeProjectionsFromFunctionType(parameterType).size() val parameterCount = KotlinBuiltIns.getParameterTypeProjectionsFromFunctionType(parameterType).size()
if (parameterCount <= 1) { if (parameterCount <= 1) {
// otherwise additional item with lambda template is to be added // otherwise additional item with lambda template is to be added
return KotlinFunctionInsertHandler(CaretPosition.IN_BRACKETS, GenerateLambdaInfo(parameterType, false)) return KotlinFunctionInsertHandler(CaretPosition.IN_BRACKETS, GenerateLambdaInfo(parameterType, false))
@@ -79,8 +79,8 @@ class LookupElementsCollector(
val parameters = descriptor.getValueParameters() val parameters = descriptor.getValueParameters()
if (parameters.size() == 1) { if (parameters.size() == 1) {
val parameterType = parameters.get(0).getType() val parameterType = parameters.get(0).getType()
if (KotlinBuiltIns.getInstance().isFunctionOrExtensionFunctionType(parameterType)) { if (KotlinBuiltIns.isFunctionOrExtensionFunctionType(parameterType)) {
val parameterCount = KotlinBuiltIns.getInstance().getParameterTypeProjectionsFromFunctionType(parameterType).size() val parameterCount = KotlinBuiltIns.getParameterTypeProjectionsFromFunctionType(parameterType).size()
if (parameterCount > 1) { if (parameterCount > 1) {
val lookupElement = boldImmediateLookupElementFactory.createLookupElement(resolutionFacade, descriptor) val lookupElement = boldImmediateLookupElementFactory.createLookupElement(resolutionFacade, descriptor)
addElement(object : LookupElementDecorator<LookupElement>(lookupElement) { addElement(object : LookupElementDecorator<LookupElement>(lookupElement) {
@@ -86,11 +86,11 @@ private fun needExplicitParameterTypes(context: InsertionContext, placeholderRan
val resolutionFacade = file.getResolutionFacade() val resolutionFacade = file.getResolutionFacade()
val bindingContext = resolutionFacade.analyzeWithPartialBodyResolve(expression) val bindingContext = resolutionFacade.analyzeWithPartialBodyResolve(expression)
val expectedInfos = ExpectedInfos(bindingContext, resolutionFacade).calculate(expression) ?: return false val expectedInfos = ExpectedInfos(bindingContext, resolutionFacade).calculate(expression) ?: return false
val functionTypes = expectedInfos.map { it.type }.filter { KotlinBuiltIns.getInstance().isExactFunctionOrExtensionFunctionType(it) }.toSet() val functionTypes = expectedInfos.map { it.type }.filter { KotlinBuiltIns.isExactFunctionOrExtensionFunctionType(it) }.toSet()
if (functionTypes.size <= 1) return false if (functionTypes.size <= 1) return false
val lambdaParameterCount = KotlinBuiltIns.getInstance().getParameterTypeProjectionsFromFunctionType(lambdaType).size val lambdaParameterCount = KotlinBuiltIns.getParameterTypeProjectionsFromFunctionType(lambdaType).size
return functionTypes.filter { KotlinBuiltIns.getInstance().getParameterTypeProjectionsFromFunctionType(it).size == lambdaParameterCount }.size > 1 return functionTypes.filter { KotlinBuiltIns.getParameterTypeProjectionsFromFunctionType(it).size == lambdaParameterCount }.size > 1
} }
private fun buildTemplate(lambdaType: JetType, explicitParameterTypes: Boolean, project: Project): Template { private fun buildTemplate(lambdaType: JetType, explicitParameterTypes: Boolean, project: Project): Template {
@@ -138,4 +138,4 @@ private class ParameterNameExpression(val nameSuggestions: Array<String>) : Expr
} }
fun functionParameterTypes(functionType: JetType): List<JetType> fun functionParameterTypes(functionType: JetType): List<JetType>
= KotlinBuiltIns.getInstance().getParameterTypeProjectionsFromFunctionType(functionType).map { it.getType() } = KotlinBuiltIns.getParameterTypeProjectionsFromFunctionType(functionType).map { it.getType() }
@@ -31,7 +31,7 @@ object LambdaItems {
val distinctTypes = functionExpectedInfos.map { it.type }.toSet() val distinctTypes = functionExpectedInfos.map { it.type }.toSet()
val singleType = if (distinctTypes.size == 1) distinctTypes.single() else null val singleType = if (distinctTypes.size == 1) distinctTypes.single() else null
val singleSignatureLength = singleType?.let { KotlinBuiltIns.getInstance().getParameterTypeProjectionsFromFunctionType(it).size } val singleSignatureLength = singleType?.let { KotlinBuiltIns.getParameterTypeProjectionsFromFunctionType(it).size }
val offerNoParametersLambda = singleSignatureLength == 0 || singleSignatureLength == 1 val offerNoParametersLambda = singleSignatureLength == 0 || singleSignatureLength == 1
if (offerNoParametersLambda) { if (offerNoParametersLambda) {
val lookupElement = LookupElementBuilder.create("{...}") val lookupElement = LookupElementBuilder.create("{...}")
@@ -119,7 +119,7 @@ class SmartCompletion(val expression: JetSimpleNameExpression,
val itemsToSkip = calcItemsToSkip(expressionWithType) val itemsToSkip = calcItemsToSkip(expressionWithType)
val functionExpectedInfos = expectedInfos.filter { KotlinBuiltIns.getInstance().isExactFunctionOrExtensionFunctionType(it.type) } val functionExpectedInfos = expectedInfos.filter { KotlinBuiltIns.isExactFunctionOrExtensionFunctionType(it.type) }
fun filterDeclaration(descriptor: DeclarationDescriptor): Collection<LookupElement> { fun filterDeclaration(descriptor: DeclarationDescriptor): Collection<LookupElement> {
val result = ArrayList<LookupElement>() val result = ArrayList<LookupElement>()
@@ -87,7 +87,7 @@ class TypeInstantiationItems(
type: JetType, type: JetType,
tail: Tail? tail: Tail?
) { ) {
if (KotlinBuiltIns.getInstance().isExactFunctionOrExtensionFunctionType(type)) return // do not show "object: ..." for function types if (KotlinBuiltIns.isExactFunctionOrExtensionFunctionType(type)) return // do not show "object: ..." for function types
val classifier = type.getConstructor().getDeclarationDescriptor() val classifier = type.getConstructor().getDeclarationDescriptor()
if (classifier !is ClassDescriptor) return if (classifier !is ClassDescriptor) return
@@ -97,7 +97,7 @@ class TypeInstantiationItems(
val typeArgs = type.getArguments() val typeArgs = type.getArguments()
items.addIfNotNull(createTypeInstantiationItem(classifier, typeArgs, tail)) items.addIfNotNull(createTypeInstantiationItem(classifier, typeArgs, tail))
if (!KotlinBuiltIns.getInstance().isAny(classifier)) { // do not search inheritors of Any if (!KotlinBuiltIns.isAny(classifier)) { // do not search inheritors of Any
inheritanceSearchers.addInheritorSearcher(classifier, classifier, typeArgs, tail) inheritanceSearchers.addInheritorSearcher(classifier, classifier, typeArgs, tail)
val javaAnalogFqName = KotlinToJavaTypesMap.getInstance().getKotlinToJavaFqName(DescriptorUtils.getFqNameSafe(classifier)) val javaAnalogFqName = KotlinToJavaTypesMap.getInstance().getKotlinToJavaFqName(DescriptorUtils.getFqNameSafe(classifier))