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.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 org.jetbrains.annotations.NotNull;
023 import org.jetbrains.jet.lang.descriptors.FunctionDescriptor;
024 import org.jetbrains.jet.lang.psi.JetBinaryExpression;
025 import org.jetbrains.jet.lang.types.expressions.OperatorConventions;
026 import org.jetbrains.k2js.translate.context.TranslationContext;
027 import org.jetbrains.k2js.translate.general.AbstractTranslator;
028
029 import static org.jetbrains.k2js.translate.utils.BindingUtils.getFunctionDescriptorForOperationExpression;
030 import static org.jetbrains.k2js.translate.utils.JsDescriptorUtils.isCompareTo;
031 import static org.jetbrains.k2js.translate.utils.PsiUtils.getOperationToken;
032
033 public final class CompareToTranslator extends AbstractTranslator {
034
035 public static boolean isCompareToCall(@NotNull JetBinaryExpression expression,
036 @NotNull TranslationContext context) {
037 FunctionDescriptor operationDescriptor =
038 getFunctionDescriptorForOperationExpression(context.bindingContext(), expression);
039
040 if (operationDescriptor == null) return false;
041
042 return (isCompareTo(operationDescriptor));
043 }
044
045 @NotNull
046 public static JsExpression translate(@NotNull JetBinaryExpression expression,
047 @NotNull TranslationContext context) {
048 return (new CompareToTranslator(expression, context)).translate();
049 }
050
051 @NotNull
052 private final JetBinaryExpression expression;
053
054 private CompareToTranslator(@NotNull JetBinaryExpression expression,
055 @NotNull TranslationContext context) {
056 super(context);
057 this.expression = expression;
058 FunctionDescriptor functionDescriptor =
059 getFunctionDescriptorForOperationExpression(context.bindingContext(), expression);
060 assert functionDescriptor != null : "CompareTo should always have a descriptor";
061 assert (OperatorConventions.COMPARISON_OPERATIONS.contains(getOperationToken(expression)));
062 }
063
064 @NotNull
065 private JsExpression translate() {
066 JsBinaryOperator correspondingOperator = OperatorTable.getBinaryOperator(getOperationToken(expression));
067 JsExpression methodCall = BinaryOperationTranslator.translateAsOverloadedCall(expression, context());
068 return new JsBinaryOperation(correspondingOperator, methodCall, context().program().getNumberLiteral(0));
069 }
070 }