001 /*
002 * Copyright 2010-2015 JetBrains s.r.o.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017 package org.jetbrains.kotlin.resolve;
018
019 import kotlin.KotlinPackage;
020 import org.jetbrains.annotations.NotNull;
021 import org.jetbrains.annotations.Nullable;
022 import org.jetbrains.kotlin.builtins.KotlinBuiltIns;
023 import org.jetbrains.kotlin.descriptors.ClassDescriptor;
024 import org.jetbrains.kotlin.descriptors.VariableDescriptor;
025 import org.jetbrains.kotlin.descriptors.annotations.Annotated;
026 import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor;
027 import org.jetbrains.kotlin.name.FqName;
028 import org.jetbrains.kotlin.psi.JetExpression;
029 import org.jetbrains.kotlin.psi.JetParameter;
030 import org.jetbrains.kotlin.psi.JetPsiUtil;
031 import org.jetbrains.kotlin.psi.JetTypeReference;
032 import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall;
033 import org.jetbrains.kotlin.resolve.constants.BooleanValue;
034 import org.jetbrains.kotlin.resolve.constants.CompileTimeConstant;
035 import org.jetbrains.kotlin.resolve.constants.ConstantValue;
036 import org.jetbrains.kotlin.resolve.constants.TypedCompileTimeConstant;
037 import org.jetbrains.kotlin.resolve.constants.evaluate.ConstantExpressionEvaluator;
038 import org.jetbrains.kotlin.types.JetType;
039 import org.jetbrains.kotlin.types.TypeProjection;
040 import org.jetbrains.kotlin.types.TypeUtils;
041
042 import java.util.Collection;
043 import java.util.List;
044 import java.util.Set;
045
046 import static org.jetbrains.kotlin.diagnostics.Errors.INVALID_TYPE_OF_ANNOTATION_MEMBER;
047 import static org.jetbrains.kotlin.diagnostics.Errors.NULLABLE_TYPE_OF_ANNOTATION_MEMBER;
048 import static org.jetbrains.kotlin.resolve.BindingContext.VALUE_PARAMETER;
049 import static org.jetbrains.kotlin.resolve.DescriptorUtils.isAnnotationClass;
050 import static org.jetbrains.kotlin.resolve.DescriptorUtils.isEnumClass;
051
052 public class CompileTimeConstantUtils {
053
054 private final static Set<String> ARRAY_CALL_NAMES = KotlinPackage.hashSetOf(
055 "kotlin.array", "kotlin.doubleArray", "kotlin.floatArray", "kotlin.longArray", "kotlin.intArray", "kotlin.charArray",
056 "kotlin.shortArray", "kotlin.byteArray", "kotlin.booleanArray",
057 "kotlin.arrayOf", "kotlin.doubleArrayOf", "kotlin.floatArrayOf", "kotlin.longArrayOf", "kotlin.intArrayOf", "kotlin.charArrayOf",
058 "kotlin.shortArrayOf", "kotlin.byteArrayOf", "kotlin.booleanArrayOf"
059 );
060
061 public static void checkConstructorParametersType(@NotNull List<JetParameter> parameters, @NotNull BindingTrace trace) {
062 for (JetParameter parameter : parameters) {
063 VariableDescriptor parameterDescriptor = trace.getBindingContext().get(VALUE_PARAMETER, parameter);
064 if (parameterDescriptor == null) continue;
065 JetType parameterType = parameterDescriptor.getType();
066 JetTypeReference typeReference = parameter.getTypeReference();
067 if (typeReference != null) {
068 if (parameterType.isMarkedNullable()) {
069 trace.report(NULLABLE_TYPE_OF_ANNOTATION_MEMBER.on(typeReference));
070 }
071 else if (!isAcceptableTypeForAnnotationParameter(parameterType)) {
072 trace.report(INVALID_TYPE_OF_ANNOTATION_MEMBER.on(typeReference));
073 }
074 }
075 }
076 }
077
078 private static boolean isAcceptableTypeForAnnotationParameter(@NotNull JetType parameterType) {
079 ClassDescriptor typeDescriptor = TypeUtils.getClassDescriptor(parameterType);
080 if (typeDescriptor == null) {
081 return false;
082 }
083
084 if (isEnumClass(typeDescriptor) ||
085 isAnnotationClass(typeDescriptor) ||
086 KotlinBuiltIns.isKClass(typeDescriptor) ||
087 KotlinBuiltIns.isPrimitiveArray(parameterType) ||
088 KotlinBuiltIns.isPrimitiveType(parameterType) ||
089 KotlinBuiltIns.isString(parameterType)) {
090 return true;
091 }
092
093 if (KotlinBuiltIns.isArray(parameterType)) {
094 List<TypeProjection> arguments = parameterType.getArguments();
095 if (arguments.size() == 1) {
096 JetType arrayType = arguments.get(0).getType();
097 if (arrayType.isMarkedNullable()) {
098 return false;
099 }
100 ClassDescriptor arrayTypeDescriptor = TypeUtils.getClassDescriptor(arrayType);
101 if (arrayTypeDescriptor != null) {
102 return isEnumClass(arrayTypeDescriptor) ||
103 isAnnotationClass(arrayTypeDescriptor) ||
104 KotlinBuiltIns.isKClass(arrayTypeDescriptor) ||
105 KotlinBuiltIns.isString(arrayType);
106 }
107 }
108 }
109 return false;
110 }
111
112 @Nullable
113 public static String getIntrinsicAnnotationArgument(@NotNull Annotated annotatedDescriptor) {
114 AnnotationDescriptor intrinsicAnnotation =
115 annotatedDescriptor.getAnnotations().findAnnotation(new FqName("kotlin.jvm.internal.Intrinsic"));
116 if (intrinsicAnnotation == null) return null;
117
118 Collection<ConstantValue<?>> values = intrinsicAnnotation.getAllValueArguments().values();
119 if (values.isEmpty()) return null;
120
121 Object value = values.iterator().next().getValue();
122 return value instanceof String ? (String) value : null;
123 }
124
125 public static boolean isArrayMethodCall(@NotNull ResolvedCall<?> resolvedCall) {
126 return ARRAY_CALL_NAMES.contains(DescriptorUtils.getFqName(resolvedCall.getCandidateDescriptor()).asString());
127 }
128
129 public static boolean canBeReducedToBooleanConstant(
130 @Nullable JetExpression expression,
131 @NotNull BindingTrace trace,
132 @Nullable Boolean expectedValue
133 ) {
134 JetExpression effectiveExpression = JetPsiUtil.deparenthesize(expression);
135
136 if (effectiveExpression == null) return false;
137
138 CompileTimeConstant<?> compileTimeConstant = ConstantExpressionEvaluator.getConstant(effectiveExpression, trace.getBindingContext());
139 if (!(compileTimeConstant instanceof TypedCompileTimeConstant) || compileTimeConstant.getUsesVariableAsConstant()) return false;
140
141 ConstantValue constantValue = ((TypedCompileTimeConstant) compileTimeConstant).getConstantValue();
142
143 if (!(constantValue instanceof BooleanValue)) return false;
144
145 Boolean value = ((BooleanValue) constantValue).getValue();
146 return expectedValue == null || expectedValue.equals(value);
147 }
148
149 private CompileTimeConstantUtils() {
150 }
151 }