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.functions.factories;
018
019 import com.google.common.base.Predicate;
020 import com.google.common.base.Predicates;
021 import com.google.dart.compiler.backend.js.ast.JsExpression;
022 import com.google.dart.compiler.backend.js.ast.JsPrefixOperation;
023 import com.google.dart.compiler.backend.js.ast.JsUnaryOperator;
024 import org.jetbrains.annotations.NotNull;
025 import org.jetbrains.annotations.Nullable;
026 import org.jetbrains.jet.lang.descriptors.FunctionDescriptor;
027 import org.jetbrains.jet.lang.resolve.name.Name;
028 import org.jetbrains.jet.lang.types.expressions.OperatorConventions;
029 import org.jetbrains.jet.lexer.JetToken;
030 import org.jetbrains.k2js.translate.context.TranslationContext;
031 import org.jetbrains.k2js.translate.intrinsic.functions.basic.FunctionIntrinsic;
032 import org.jetbrains.k2js.translate.intrinsic.functions.patterns.DescriptorPredicate;
033 import org.jetbrains.k2js.translate.intrinsic.functions.patterns.NamePredicate;
034 import org.jetbrains.k2js.translate.operation.OperatorTable;
035 import org.jetbrains.k2js.translate.utils.JsDescriptorUtils;
036
037 import java.util.List;
038
039 import static org.jetbrains.k2js.translate.intrinsic.functions.patterns.NamePredicate.PRIMITIVE_NUMBERS;
040 import static org.jetbrains.k2js.translate.intrinsic.functions.patterns.PatternBuilder.pattern;
041
042 public enum PrimitiveUnaryOperationFIF implements FunctionIntrinsicFactory {
043
044 INSTANCE;
045
046 private static final NamePredicate UNARY_OPERATIONS = new NamePredicate(OperatorConventions.UNARY_OPERATION_NAMES.values());
047 @NotNull
048 private static final DescriptorPredicate UNARY_OPERATION_FOR_PRIMITIVE_NUMBER =
049 pattern(PRIMITIVE_NUMBERS, UNARY_OPERATIONS);
050 @NotNull
051 private static final Predicate<FunctionDescriptor> PRIMITIVE_UNARY_OPERATION_NAMES =
052 Predicates.or(UNARY_OPERATION_FOR_PRIMITIVE_NUMBER, pattern("Boolean.not"), pattern("Int.inv"));
053 @NotNull
054 private static final DescriptorPredicate NO_PARAMETERS = new DescriptorPredicate() {
055 @Override
056 public boolean apply(@NotNull FunctionDescriptor descriptor) {
057 return !JsDescriptorUtils.hasParameters(descriptor);
058 }
059 };
060 @NotNull
061 private static final Predicate<FunctionDescriptor> PATTERN = Predicates.and(PRIMITIVE_UNARY_OPERATION_NAMES, NO_PARAMETERS);
062
063 @Nullable
064 @Override
065 public FunctionIntrinsic getIntrinsic(@NotNull FunctionDescriptor descriptor) {
066 if (!PATTERN.apply(descriptor)) {
067 return null;
068 }
069
070 Name name = descriptor.getName();
071
072 JsUnaryOperator jsOperator;
073 if ("inv".equals(name.asString())) {
074 jsOperator = JsUnaryOperator.BIT_NOT;
075 }
076 else {
077 JetToken jetToken = OperatorConventions.UNARY_OPERATION_NAMES.inverse().get(name);
078 jsOperator = OperatorTable.getUnaryOperator(jetToken);
079 }
080
081 final JsUnaryOperator finalJsOperator = jsOperator;
082 return new FunctionIntrinsic() {
083 @NotNull
084 @Override
085 public JsExpression apply(@Nullable JsExpression receiver,
086 @NotNull List<JsExpression> arguments,
087 @NotNull TranslationContext context) {
088 assert receiver != null;
089 assert arguments.size() == 0 : "Unary operator should not have arguments.";
090 //NOTE: cannot use this for increment/decrement
091 return new JsPrefixOperation(finalJsOperator, receiver);
092 }
093 };
094 }
095
096 }