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