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.js.translate.intrinsic.functions.factories;
018    
019    import com.google.common.collect.Lists;
020    import org.jetbrains.annotations.NotNull;
021    import org.jetbrains.annotations.Nullable;
022    import org.jetbrains.kotlin.builtins.KotlinBuiltIns;
023    import org.jetbrains.kotlin.builtins.PrimitiveType;
024    import org.jetbrains.kotlin.js.backend.ast.*;
025    import org.jetbrains.kotlin.js.patterns.DescriptorPredicate;
026    import org.jetbrains.kotlin.js.patterns.NamePredicate;
027    import org.jetbrains.kotlin.js.translate.context.Namer;
028    import org.jetbrains.kotlin.js.translate.context.TranslationContext;
029    import org.jetbrains.kotlin.js.translate.intrinsic.functions.basic.FunctionIntrinsic;
030    import org.jetbrains.kotlin.js.translate.intrinsic.functions.basic.FunctionIntrinsicWithReceiverComputed;
031    import org.jetbrains.kotlin.name.Name;
032    
033    import java.util.List;
034    
035    import static com.intellij.openapi.util.text.StringUtil.decapitalize;
036    import static org.jetbrains.kotlin.js.patterns.PatternBuilder.pattern;
037    import static org.jetbrains.kotlin.js.translate.utils.JsAstUtils.assignment;
038    
039    public final class ArrayFIF extends CompositeFIF {
040        private static final NamePredicate NUMBER_ARRAY;
041        private static final NamePredicate CHAR_ARRAY;
042        private static final NamePredicate BOOLEAN_ARRAY;
043        private static final NamePredicate LONG_ARRAY;
044        private static final NamePredicate ARRAYS;
045        private static final DescriptorPredicate ARRAY_FACTORY_METHODS;
046    
047        static {
048            List<Name> arrayTypeNames = Lists.newArrayList();
049            List<Name> arrayFactoryMethodNames = Lists.newArrayList(Name.identifier("arrayOf"));
050            for (PrimitiveType type : PrimitiveType.values()) {
051                Name arrayTypeName = type.getArrayTypeName();
052                if (type != PrimitiveType.CHAR && type != PrimitiveType.BOOLEAN && type != PrimitiveType.LONG) {
053                    arrayTypeNames.add(arrayTypeName);
054                }
055                arrayFactoryMethodNames.add(Name.identifier(decapitalize(arrayTypeName.asString() + "Of")));
056            }
057    
058            Name arrayName = KotlinBuiltIns.FQ_NAMES.array.shortName();
059            Name booleanArrayName = PrimitiveType.BOOLEAN.getArrayTypeName();
060            Name charArrayName = PrimitiveType.CHAR.getArrayTypeName();
061            Name longArrayName = PrimitiveType.LONG.getArrayTypeName();
062    
063            NUMBER_ARRAY = new NamePredicate(arrayTypeNames);
064            CHAR_ARRAY = new NamePredicate(charArrayName);
065            BOOLEAN_ARRAY = new NamePredicate(booleanArrayName);
066            LONG_ARRAY = new NamePredicate(longArrayName);
067    
068            arrayTypeNames.add(charArrayName);
069            arrayTypeNames.add(booleanArrayName);
070            arrayTypeNames.add(longArrayName);
071            arrayTypeNames.add(arrayName);
072            ARRAYS = new NamePredicate(arrayTypeNames);
073            ARRAY_FACTORY_METHODS = pattern(Namer.KOTLIN_LOWER_NAME, new NamePredicate(arrayFactoryMethodNames));
074        }
075    
076        private static final FunctionIntrinsic ARRAY_INTRINSIC = new FunctionIntrinsicWithReceiverComputed() {
077            @NotNull
078            @Override
079            public JsExpression apply(
080                    @Nullable JsExpression receiver,
081                    @NotNull List<? extends JsExpression> arguments,
082                    @NotNull TranslationContext context
083            ) {
084                assert arguments.size() == 1;
085                return arguments.get(0);
086            }
087        };
088    
089        @NotNull
090        public static final FunctionIntrinsic GET_INTRINSIC = new FunctionIntrinsicWithReceiverComputed() {
091            @NotNull
092            @Override
093            public JsExpression apply(@Nullable JsExpression receiver,
094                    @NotNull List<? extends JsExpression> arguments,
095                    @NotNull TranslationContext context) {
096                assert receiver != null;
097                assert arguments.size() == 1 : "Array get expression must have one argument.";
098                JsExpression indexExpression = arguments.get(0);
099                return new JsArrayAccess(receiver, indexExpression);
100            }
101        };
102    
103        @NotNull
104        public static final FunctionIntrinsic SET_INTRINSIC = new FunctionIntrinsicWithReceiverComputed() {
105            @NotNull
106            @Override
107            public JsExpression apply(@Nullable JsExpression receiver,
108                    @NotNull List<? extends JsExpression> arguments,
109                    @NotNull TranslationContext context) {
110                assert receiver != null;
111                assert arguments.size() == 2 : "Array set expression must have two arguments.";
112                JsExpression indexExpression = arguments.get(0);
113                JsExpression value = arguments.get(1);
114                JsArrayAccess arrayAccess = new JsArrayAccess(receiver, indexExpression);
115                return assignment(arrayAccess, value);
116            }
117        };
118    
119        @NotNull
120        public static final FunctionIntrinsicFactory INSTANCE = new ArrayFIF();
121    
122        private ArrayFIF() {
123            add(pattern(ARRAYS, "get"), GET_INTRINSIC);
124            add(pattern(ARRAYS, "set"), SET_INTRINSIC);
125            add(pattern(ARRAYS, "<get-size>"), LENGTH_PROPERTY_INTRINSIC);
126            add(pattern(ARRAYS, "iterator"), new KotlinFunctionIntrinsic("arrayIterator"));
127    
128            add(pattern(NUMBER_ARRAY, "<init>(Int)"), new KotlinFunctionIntrinsic("newArray", JsNumberLiteral.ZERO));
129            add(pattern(CHAR_ARRAY, "<init>(Int)"), new KotlinFunctionIntrinsic("newArray", JsNumberLiteral.ZERO));
130            add(pattern(BOOLEAN_ARRAY, "<init>(Int)"), new KotlinFunctionIntrinsic("newArray", JsLiteral.FALSE));
131            add(pattern(LONG_ARRAY, "<init>(Int)"), new KotlinFunctionIntrinsic("newArray", new JsNameRef(Namer.LONG_ZERO, Namer.kotlinLong())));
132    
133            add(pattern(ARRAYS, "<init>(Int,Function1)"), new KotlinFunctionIntrinsic("newArrayF"));
134    
135            add(pattern("kotlin", "arrayOfNulls"), new KotlinFunctionIntrinsic("newArray", JsLiteral.NULL));
136    
137            add(ARRAY_FACTORY_METHODS, ARRAY_INTRINSIC);
138        }
139    }