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