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.codegen.optimization.boxing;
018    
019    import com.google.common.collect.ImmutableMap;
020    import org.jetbrains.annotations.NotNull;
021    import org.jetbrains.kotlin.builtins.PrimitiveType;
022    import org.jetbrains.kotlin.codegen.RangeCodegenUtil;
023    import org.jetbrains.kotlin.codegen.intrinsics.IteratorNext;
024    import org.jetbrains.kotlin.codegen.optimization.common.StrictBasicValue;
025    import org.jetbrains.kotlin.name.Name;
026    import org.jetbrains.kotlin.resolve.jvm.JvmPrimitiveType;
027    import org.jetbrains.org.objectweb.asm.Type;
028    
029    public class ProgressionIteratorBasicValue extends StrictBasicValue {
030        private final static ImmutableMap<String, Type> VALUES_TYPENAME_TO_TYPE;
031    
032        static {
033            ImmutableMap.Builder<String, Type> builder = ImmutableMap.builder();
034            for (PrimitiveType primitiveType : RangeCodegenUtil.supportedRangeTypes()) {
035                builder.put(primitiveType.getTypeName().asString(), Type.getType(JvmPrimitiveType.get(primitiveType).getDesc()));
036            }
037            VALUES_TYPENAME_TO_TYPE = builder.build();
038        }
039    
040        @NotNull
041        private static Type getValuesType(@NotNull String valuesTypeName) {
042            Type type = VALUES_TYPENAME_TO_TYPE.get(valuesTypeName);
043            assert type != null : "Unexpected type " + valuesTypeName;
044            return type;
045        }
046    
047        private final Type valuesPrimitiveType;
048        private final String valuesPrimitiveTypeName;
049    
050        public ProgressionIteratorBasicValue(@NotNull String valuesPrimitiveTypeName) {
051            super(IteratorNext.Companion.getPrimitiveIteratorType(Name.identifier(valuesPrimitiveTypeName)));
052            this.valuesPrimitiveType = getValuesType(valuesPrimitiveTypeName);
053            this.valuesPrimitiveTypeName = valuesPrimitiveTypeName;
054        }
055    
056        @NotNull
057        public Type getValuesPrimitiveType() {
058            return valuesPrimitiveType;
059        }
060    
061        @Override
062        public boolean equals(Object o) {
063            if (this == o) return true;
064            if (o == null || getClass() != o.getClass()) return false;
065            if (!super.equals(o)) return false;
066    
067            ProgressionIteratorBasicValue value = (ProgressionIteratorBasicValue) o;
068    
069            if (!valuesPrimitiveType.equals(value.valuesPrimitiveType)) return false;
070    
071            return true;
072        }
073    
074        @NotNull
075        public String getNextMethodName() {
076            return "next" + valuesPrimitiveTypeName;
077        }
078    
079        @NotNull
080        public String getNextMethodDesc() {
081            return "()" + getValuesPrimitiveType().getDescriptor();
082        }
083    }