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.operation; 018 019import com.google.dart.compiler.backend.js.ast.JsBinaryOperation; 020import com.google.dart.compiler.backend.js.ast.JsBinaryOperator; 021import com.google.dart.compiler.backend.js.ast.JsExpression; 022import com.google.dart.compiler.backend.js.ast.JsLiteral; 023import org.jetbrains.annotations.NotNull; 024import org.jetbrains.jet.lang.descriptors.FunctionDescriptor; 025import org.jetbrains.jet.lang.psi.JetBinaryExpression; 026import org.jetbrains.jet.lang.psi.JetExpression; 027import org.jetbrains.jet.lang.resolve.name.Name; 028import org.jetbrains.jet.lang.types.expressions.OperatorConventions; 029import org.jetbrains.jet.lexer.JetTokens; 030import org.jetbrains.k2js.translate.context.TranslationContext; 031import org.jetbrains.k2js.translate.intrinsic.functions.factories.TopLevelFIF; 032import org.jetbrains.k2js.translate.intrinsic.functions.patterns.NamePredicate; 033import org.jetbrains.k2js.translate.utils.JsAstUtils; 034import org.jetbrains.k2js.translate.utils.JsDescriptorUtils; 035import org.jetbrains.k2js.translate.utils.TranslationUtils; 036 037import java.util.Arrays; 038 039import static org.jetbrains.k2js.translate.utils.BindingUtils.getFunctionDescriptorForOperationExpression; 040import static org.jetbrains.k2js.translate.utils.PsiUtils.getOperationToken; 041 042public 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}