001 /*
002 * Copyright 2010-2013 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.jet.lang.resolve.constants;
018
019 import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
020 import org.jetbrains.jet.lang.psi.JetExpression;
021 import org.jetbrains.jet.lang.psi.JetQualifiedExpression;
022 import org.jetbrains.jet.lang.psi.JetSimpleNameExpression;
023 import org.jetbrains.jet.lang.resolve.BindingContext;
024 import org.jetbrains.jet.lang.resolve.BindingTrace;
025 import org.jetbrains.jet.lang.resolve.name.Name;
026 import org.jetbrains.jet.lang.types.expressions.OperatorConventions;
027 import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns;
028
029 import static org.jetbrains.jet.lang.types.expressions.OperatorConventions.*;
030 import static org.jetbrains.jet.lang.types.expressions.OperatorConventions.BYTE;
031 import static org.jetbrains.jet.lang.types.expressions.OperatorConventions.INT;
032
033 public class ConstantUtils {
034
035 public static void propagateConstantValues(JetQualifiedExpression expression, BindingTrace trace, JetSimpleNameExpression selectorExpression) {
036 JetExpression receiverExpression = expression.getReceiverExpression();
037 CompileTimeConstant<?> receiverValue = trace.getBindingContext().get(BindingContext.COMPILE_TIME_VALUE, receiverExpression);
038 CompileTimeConstant<?> wholeExpressionValue = trace.getBindingContext().get(BindingContext.COMPILE_TIME_VALUE, expression);
039 DeclarationDescriptor declarationDescriptor = trace.getBindingContext().get(BindingContext.REFERENCE_TARGET, selectorExpression);
040 if (wholeExpressionValue == null && receiverValue != null && !(receiverValue instanceof ErrorValue) && receiverValue.getValue() instanceof Number
041 && KotlinBuiltIns.getInstance().getNumber() == declarationDescriptor) {
042 Number value = (Number) receiverValue.getValue();
043 Name referencedName = selectorExpression.getReferencedNameAsName();
044 if (OperatorConventions.NUMBER_CONVERSIONS.contains(referencedName)) {
045 if (DOUBLE.equals(referencedName)) {
046 trace.record(BindingContext.COMPILE_TIME_VALUE, expression, new DoubleValue(value.doubleValue()));
047 }
048 else if (FLOAT.equals(referencedName)) {
049 trace.record(BindingContext.COMPILE_TIME_VALUE, expression, new FloatValue(value.floatValue()));
050 }
051 else if (LONG.equals(referencedName)) {
052 trace.record(BindingContext.COMPILE_TIME_VALUE, expression, new LongValue(value.longValue()));
053 }
054 else if (SHORT.equals(referencedName)) {
055 trace.record(BindingContext.COMPILE_TIME_VALUE, expression, new ShortValue(value.shortValue()));
056 }
057 else if (BYTE.equals(referencedName)) {
058 trace.record(BindingContext.COMPILE_TIME_VALUE, expression, new ByteValue(value.byteValue()));
059 }
060 else if (INT.equals(referencedName)) {
061 trace.record(BindingContext.COMPILE_TIME_VALUE, expression, new IntValue(value.intValue()));
062 }
063 }
064 }
065 }
066 }