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
017package org.jetbrains.jet.codegen;
018
019import com.google.common.collect.ImmutableMap;
020import org.jetbrains.annotations.NotNull;
021import org.jetbrains.annotations.Nullable;
022import org.jetbrains.jet.lang.descriptors.CallableDescriptor;
023import org.jetbrains.jet.lang.descriptors.ClassifierDescriptor;
024import org.jetbrains.jet.lang.psi.*;
025import org.jetbrains.jet.lang.resolve.DescriptorUtils;
026import org.jetbrains.jet.lang.resolve.name.FqName;
027import org.jetbrains.jet.lang.types.JetType;
028import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns;
029import org.jetbrains.jet.lang.types.lang.PrimitiveType;
030
031import java.util.List;
032
033import static org.jetbrains.jet.codegen.AsmUtil.isPrimitiveNumberClassDescriptor;
034
035public class RangeCodegenUtil {
036    private static final ImmutableMap<FqName, PrimitiveType> RANGE_TO_ELEMENT_TYPE;
037    private static final ImmutableMap<FqName, PrimitiveType> PROGRESSION_TO_ELEMENT_TYPE;
038
039    static {
040        ImmutableMap.Builder<FqName, PrimitiveType> rangeBuilder = ImmutableMap.builder();
041        ImmutableMap.Builder<FqName, PrimitiveType> progressionBuilder = ImmutableMap.builder();
042        for (PrimitiveType primitiveType : PrimitiveType.values()) {
043            rangeBuilder.put(primitiveType.getRangeClassName(), primitiveType);
044            progressionBuilder.put(primitiveType.getProgressionClassName(), primitiveType);
045        }
046        RANGE_TO_ELEMENT_TYPE = rangeBuilder.build();
047        PROGRESSION_TO_ELEMENT_TYPE = progressionBuilder.build();
048    }
049
050    private RangeCodegenUtil() {}
051
052    public static boolean isRange(JetType rangeType) {
053        return !rangeType.isNullable() && getPrimitiveRangeElementType(rangeType) != null;
054    }
055
056    public static boolean isProgression(JetType rangeType) {
057        return !rangeType.isNullable() && getPrimitiveProgressionElementType(rangeType) != null;
058    }
059
060    @Nullable
061    public static BinaryCall getRangeAsBinaryCall(@NotNull JetForExpression forExpression) {
062        // We are looking for rangeTo() calls
063        // Other binary operations will succeed too, but will be filtered out later (by examining a resolvedCall)
064        JetExpression rangeExpression = forExpression.getLoopRange();
065        assert rangeExpression != null;
066        JetExpression loopRange = JetPsiUtil.deparenthesizeWithNoTypeResolution(rangeExpression);
067        if (loopRange instanceof JetQualifiedExpression) {
068            // a.rangeTo(b)
069            JetQualifiedExpression qualifiedExpression = (JetQualifiedExpression) loopRange;
070            JetExpression selector = qualifiedExpression.getSelectorExpression();
071            if (selector instanceof JetCallExpression) {
072                JetCallExpression callExpression = (JetCallExpression) selector;
073                List<? extends ValueArgument> arguments = callExpression.getValueArguments();
074                if (arguments.size() == 1) {
075                    return new BinaryCall(qualifiedExpression.getReceiverExpression(), callExpression.getCalleeExpression(),
076                                          arguments.get(0).getArgumentExpression());
077                }
078            }
079        }
080        else if (loopRange instanceof JetBinaryExpression) {
081            // a rangeTo b
082            // a .. b
083            JetBinaryExpression binaryExpression = (JetBinaryExpression) loopRange;
084            return new BinaryCall(binaryExpression.getLeft(), binaryExpression.getOperationReference(), binaryExpression.getRight());
085
086        }
087        return null;
088    }
089
090    @Nullable
091    private static PrimitiveType getPrimitiveRangeElementType(JetType rangeType) {
092        return getPrimitiveRangeOrProgressionElementType(rangeType, RANGE_TO_ELEMENT_TYPE);
093    }
094
095    @Nullable
096    private static PrimitiveType getPrimitiveProgressionElementType(JetType rangeType) {
097        return getPrimitiveRangeOrProgressionElementType(rangeType, PROGRESSION_TO_ELEMENT_TYPE);
098    }
099
100    @Nullable
101    private static PrimitiveType getPrimitiveRangeOrProgressionElementType(
102            @NotNull JetType rangeOrProgression,
103            @NotNull ImmutableMap<FqName, PrimitiveType> map
104    ) {
105        ClassifierDescriptor declarationDescriptor = rangeOrProgression.getConstructor().getDeclarationDescriptor();
106        assert declarationDescriptor != null;
107        if (declarationDescriptor != KotlinBuiltIns.getInstance().getBuiltInsScope().getClassifier(declarationDescriptor.getName())) {
108            // Must be a standard library class
109            return null;
110        }
111        return map.get(DescriptorUtils.getFQName(declarationDescriptor).toSafe());
112    }
113
114    public static boolean isOptimizableRangeTo(CallableDescriptor rangeTo) {
115        if ("rangeTo".equals(rangeTo.getName().asString())) {
116            if (isPrimitiveNumberClassDescriptor(rangeTo.getContainingDeclaration())) {
117                return true;
118            }
119        }
120        return false;
121    }
122
123    public static class BinaryCall {
124        public final JetExpression left;
125        public final JetExpression op;
126        public final JetExpression right;
127
128        private BinaryCall(JetExpression left, JetExpression op, JetExpression right) {
129            this.left = left;
130            this.op = op;
131            this.right = right;
132        }
133    }
134}