001 /*
002 * Copyright 2010-2015 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.kotlin.js.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.kotlin.js.translate.callTranslator.CallInfo;
023 import org.jetbrains.kotlin.js.translate.context.TranslationContext;
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 @Nullable
049 protected static JsExpression getThisOrReceiverOrNull(@NotNull CallInfo callInfo) {
050 if (callInfo.getDispatchReceiver() != null) {
051 return callInfo.getDispatchReceiver();
052 } else {
053 return callInfo.getExtensionReceiver();
054 }
055 }
056
057 @NotNull
058 public JsExpression apply(@NotNull CallInfo callInfo,
059 @NotNull List<JsExpression> arguments,
060 @NotNull TranslationContext context) {
061 return apply(getThisOrReceiverOrNull(callInfo), arguments, context);
062 }
063
064 @NotNull
065 public abstract JsExpression apply(@Nullable JsExpression receiver,
066 @NotNull List<JsExpression> arguments,
067 @NotNull TranslationContext context
068 );
069
070 public boolean exists() {
071 return true;
072 }
073
074 public abstract static class CallParametersAwareFunctionIntrinsic extends FunctionIntrinsic {
075 @NotNull
076 @Override
077 public JsExpression apply(@Nullable JsExpression receiver,
078 @NotNull List<JsExpression> arguments,
079 @NotNull TranslationContext context
080 ) {
081 throw new IllegalStateException();
082 }
083 }
084 }