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.basic;
018
019 import com.google.dart.compiler.backend.js.ast.JsExpression;
020 import org.jetbrains.annotations.NotNull;
021 import org.jetbrains.annotations.Nullable;
022 import org.jetbrains.k2js.translate.context.TranslationContext;
023 import org.jetbrains.k2js.translate.reference.CallTranslator;
024
025 import java.util.List;
026
027 /**
028 * Base for intrinsics that substitute standard function calls like Int#plus, Float#minus ... etc
029 */
030 public abstract class FunctionIntrinsic {
031
032 @NotNull
033 public static final FunctionIntrinsic NO_INTRINSIC = new FunctionIntrinsic() {
034 @Override
035 public boolean exists() {
036 return false;
037 }
038
039 @NotNull
040 @Override
041 public JsExpression apply(@Nullable JsExpression receiver,
042 @NotNull List<JsExpression> arguments,
043 @NotNull TranslationContext context) {
044 throw new UnsupportedOperationException("FunctionIntrinsic#NO_INTRINSIC_#apply");
045 }
046 };
047
048 @NotNull
049 public JsExpression apply(@NotNull CallTranslator callTranslator,
050 @NotNull List<JsExpression> arguments,
051 @NotNull TranslationContext context) {
052 return apply(callTranslator.getCallParameters().getThisOrReceiverOrNull(), arguments, context);
053 }
054
055 @NotNull
056 public abstract JsExpression apply(@Nullable JsExpression receiver,
057 @NotNull List<JsExpression> arguments,
058 @NotNull TranslationContext context
059 );
060
061 public boolean exists() {
062 return true;
063 }
064
065 public abstract static class CallParametersAwareFunctionIntrinsic extends FunctionIntrinsic {
066 @NotNull
067 @Override
068 public JsExpression apply(@Nullable JsExpression receiver,
069 @NotNull List<JsExpression> arguments,
070 @NotNull TranslationContext context
071 ) {
072 throw new IllegalStateException();
073 }
074 }
075 }