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.k2js.translate.intrinsic.functions.factories;
018
019import com.google.dart.compiler.backend.js.ast.JsExpression;
020import com.google.dart.compiler.backend.js.ast.JsNameRef;
021import org.jetbrains.annotations.NotNull;
022import org.jetbrains.annotations.Nullable;
023import org.jetbrains.k2js.translate.context.TranslationContext;
024import org.jetbrains.k2js.translate.intrinsic.functions.basic.BuiltInFunctionIntrinsic;
025import org.jetbrains.k2js.translate.intrinsic.functions.basic.CallStandardMethodIntrinsic;
026import org.jetbrains.k2js.translate.intrinsic.functions.basic.FunctionIntrinsic;
027import org.jetbrains.k2js.translate.intrinsic.functions.patterns.NamePredicate;
028
029import java.util.List;
030
031import static org.jetbrains.k2js.translate.intrinsic.functions.patterns.PatternBuilder.pattern;
032
033public final class TopLevelFIF extends CompositeFIF {
034    @NotNull
035    public static final CallStandardMethodIntrinsic EQUALS = new CallStandardMethodIntrinsic(new JsNameRef("equals", "Kotlin"), true, 1);
036    @NotNull
037    public static final FunctionIntrinsic RETURN_RECEIVER_INTRINSIC = new FunctionIntrinsic() {
038        @NotNull
039        @Override
040        public JsExpression apply(
041                @Nullable JsExpression receiver,
042                @NotNull List<JsExpression> arguments,
043                @NotNull TranslationContext context
044        ) {
045            assert receiver != null;
046            return receiver;
047        }
048    };
049    @NotNull
050    public static final FunctionIntrinsicFactory INSTANCE = new TopLevelFIF();
051
052    private TopLevelFIF() {
053        add(pattern("toString"), new BuiltInFunctionIntrinsic("toString"));
054        add(pattern("equals"), EQUALS);
055        add(pattern(NamePredicate.PRIMITIVE_NUMBERS, "equals"), EQUALS);
056        add(pattern("String|Boolean|Char|Number.equals"), EQUALS);
057        add(pattern("arrayOfNulls"), new CallStandardMethodIntrinsic(new JsNameRef("nullArray", "Kotlin"), false, 1));
058        add(pattern("iterator"), RETURN_RECEIVER_INTRINSIC);
059    }
060}