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.k2js.translate.intrinsic.functions.factories;
018    
019    import com.google.common.collect.Sets;
020    import com.google.dart.compiler.backend.js.ast.JsBinaryOperation;
021    import com.google.dart.compiler.backend.js.ast.JsBinaryOperator;
022    import com.google.dart.compiler.backend.js.ast.JsExpression;
023    import com.google.dart.compiler.backend.js.ast.JsNameRef;
024    import org.jetbrains.annotations.NotNull;
025    import org.jetbrains.annotations.Nullable;
026    import org.jetbrains.jet.lang.resolve.name.Name;
027    import org.jetbrains.jet.lang.types.expressions.OperatorConventions;
028    import org.jetbrains.k2js.translate.context.TranslationContext;
029    import org.jetbrains.k2js.translate.intrinsic.functions.basic.FunctionIntrinsic;
030    import org.jetbrains.k2js.translate.intrinsic.functions.patterns.NamePredicate;
031    
032    import java.util.List;
033    import java.util.Set;
034    
035    import static org.jetbrains.jet.lang.types.expressions.OperatorConventions.*;
036    import static org.jetbrains.k2js.translate.intrinsic.functions.patterns.PatternBuilder.pattern;
037    import static org.jetbrains.k2js.translate.utils.JsAstUtils.assignment;
038    import static org.jetbrains.k2js.translate.utils.JsAstUtils.subtract;
039    
040    public final class NumberConversionFIF extends CompositeFIF {
041        @NotNull
042        private static final NamePredicate SUPPORTED_CONVERSIONS;
043    
044        static {
045            Set<Name> supportedConversions = Sets.newHashSet(NUMBER_CONVERSIONS);
046            //TODO: support longs and chars
047            supportedConversions.remove(CHAR);
048            supportedConversions.remove(LONG);
049            SUPPORTED_CONVERSIONS = new NamePredicate(supportedConversions);
050        }
051    
052        @NotNull
053        private static final NamePredicate FLOATING_POINT_CONVERSIONS = new NamePredicate(OperatorConventions.FLOAT, OperatorConventions.DOUBLE);
054    
055        @NotNull
056        private static final NamePredicate INTEGER_CONVERSIONS = new NamePredicate(OperatorConventions.INT, OperatorConventions.SHORT,
057                                                                               OperatorConventions.BYTE);
058    
059        @NotNull
060        private static final FunctionIntrinsic RETURN_RECEIVER = new FunctionIntrinsic() {
061            @NotNull
062            @Override
063            public JsExpression apply(@Nullable JsExpression receiver,
064                    @NotNull List<JsExpression> arguments,
065                    @NotNull TranslationContext context) {
066                assert receiver != null;
067                assert arguments.isEmpty();
068                return receiver;
069            }
070        };
071    
072        @NotNull
073        public static final String INTEGER_NUMBER_TYPES = "Int|Byte|Short";
074        //NOTE: treat Number as if it is floating point type
075        @NotNull
076        private static final String FLOATING_POINT_NUMBER_TYPES = "Float|Double|Number";
077        @NotNull
078        private static final FunctionIntrinsic GET_INTEGER_PART = new FunctionIntrinsic() {
079            @NotNull
080            @Override
081            public JsExpression apply(@Nullable JsExpression receiver,
082                    @NotNull List<JsExpression> arguments,
083                    @NotNull TranslationContext context) {
084                assert receiver != null;
085                assert arguments.isEmpty();
086                JsNameRef toConvertReference = context.declareTemporary(null).reference();
087                JsBinaryOperation fractional =
088                        new JsBinaryOperation(JsBinaryOperator.MOD, toConvertReference, context.program().getNumberLiteral(1));
089                return subtract(assignment(toConvertReference, receiver), fractional);
090            }
091        };
092        @NotNull
093        public static final FunctionIntrinsicFactory INSTANCE = new NumberConversionFIF();
094    
095        private NumberConversionFIF() {
096            add(pattern(INTEGER_NUMBER_TYPES, SUPPORTED_CONVERSIONS), RETURN_RECEIVER);
097            add(pattern(FLOATING_POINT_NUMBER_TYPES, INTEGER_CONVERSIONS), GET_INTEGER_PART);
098            add(pattern(FLOATING_POINT_NUMBER_TYPES, FLOATING_POINT_CONVERSIONS), RETURN_RECEIVER);
099        }
100    }