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.operation;
018
019 import com.google.dart.compiler.backend.js.ast.JsBinaryOperation;
020 import com.google.dart.compiler.backend.js.ast.JsBinaryOperator;
021 import com.google.dart.compiler.backend.js.ast.JsExpression;
022 import com.google.dart.compiler.backend.js.ast.JsLiteral;
023 import org.jetbrains.annotations.NotNull;
024 import org.jetbrains.jet.lang.descriptors.CallableDescriptor;
025 import org.jetbrains.jet.lang.descriptors.FunctionDescriptor;
026 import org.jetbrains.jet.lang.psi.JetBinaryExpression;
027 import org.jetbrains.jet.lang.psi.JetExpression;
028 import org.jetbrains.jet.lang.resolve.name.Name;
029 import org.jetbrains.jet.lang.types.expressions.OperatorConventions;
030 import org.jetbrains.jet.lexer.JetTokens;
031 import org.jetbrains.k2js.translate.context.TranslationContext;
032 import org.jetbrains.k2js.translate.intrinsic.functions.factories.TopLevelFIF;
033 import org.jetbrains.k2js.translate.intrinsic.functions.patterns.NamePredicate;
034 import org.jetbrains.k2js.translate.utils.JsAstUtils;
035 import org.jetbrains.k2js.translate.utils.JsDescriptorUtils;
036 import org.jetbrains.k2js.translate.utils.TranslationUtils;
037
038 import java.util.Arrays;
039
040 import static org.jetbrains.k2js.translate.utils.BindingUtils.getCallableDescriptorForOperationExpression;
041 import static org.jetbrains.k2js.translate.utils.PsiUtils.getOperationToken;
042
043 public final class EqualsIntrinsic implements BinaryOperationIntrinsic {
044
045 @Override
046 public boolean isApplicable(@NotNull JetBinaryExpression expression, @NotNull TranslationContext context) {
047 if (!OperatorConventions.EQUALS_OPERATIONS.contains(getOperationToken(expression))) {
048 return false;
049 }
050 CallableDescriptor functionDescriptor = getCallableDescriptorForOperationExpression(context.bindingContext(), expression);
051 if (!(functionDescriptor instanceof FunctionDescriptor)) return false;
052
053 return JsDescriptorUtils.isBuiltin(functionDescriptor) || TopLevelFIF.EQUALS_IN_ANY.apply((FunctionDescriptor) functionDescriptor);
054 }
055
056 @Override
057 @NotNull
058 public JsExpression apply(@NotNull JetBinaryExpression expression,
059 @NotNull JsExpression left,
060 @NotNull JsExpression right,
061 @NotNull TranslationContext context) {
062 boolean isNegated = getOperationToken(expression).equals(JetTokens.EXCLEQ);
063 if (right == JsLiteral.NULL || left == JsLiteral.NULL) {
064 return TranslationUtils.nullCheck(right == JsLiteral.NULL ? left : right, isNegated);
065 }
066 else if (canUseSimpleEquals(expression, context)) {
067 return new JsBinaryOperation(isNegated ? JsBinaryOperator.REF_NEQ : JsBinaryOperator.REF_EQ, left, right);
068 }
069
070 JsExpression result = TopLevelFIF.KOTLIN_EQUALS.apply(left, Arrays.asList(right), context);
071 return isNegated ? JsAstUtils.negated(result) : result;
072 }
073
074 private static boolean canUseSimpleEquals(@NotNull JetBinaryExpression expression, @NotNull TranslationContext context) {
075 JetExpression left = expression.getLeft();
076 assert left != null : "No left-hand side: " + expression.getText();
077 Name typeName = JsDescriptorUtils.getNameIfStandardType(left, context);
078 return typeName != null && NamePredicate.PRIMITIVE_NUMBERS.apply(typeName);
079 }
080 }