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.kotlin.js.translate.reference;
018
019 import com.google.dart.compiler.backend.js.ast.*;
020 import com.google.gwt.dev.js.ThrowExceptionOnErrorReporter;
021 import org.jetbrains.annotations.NotNull;
022 import org.jetbrains.annotations.Nullable;
023 import org.jetbrains.kotlin.descriptors.*;
024 import org.jetbrains.kotlin.js.parser.ParserUtilsKt;
025 import org.jetbrains.kotlin.js.resolve.diagnostics.JsCallChecker;
026 import org.jetbrains.kotlin.js.translate.callTranslator.CallTranslator;
027 import org.jetbrains.kotlin.js.translate.context.TranslationContext;
028 import org.jetbrains.kotlin.psi.KtCallExpression;
029 import org.jetbrains.kotlin.psi.KtExpression;
030 import org.jetbrains.kotlin.psi.ValueArgument;
031 import org.jetbrains.kotlin.resolve.calls.callUtil.CallUtilKt;
032 import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall;
033 import org.jetbrains.kotlin.resolve.inline.InlineUtil;
034
035 import java.util.List;
036
037 import static org.jetbrains.kotlin.js.resolve.diagnostics.JsCallChecker.isJsCall;
038 import static org.jetbrains.kotlin.js.translate.utils.PsiUtils.getFunctionDescriptor;
039 import static org.jetbrains.kotlin.js.translate.utils.InlineUtils.setInlineCallMetadata;
040 import static org.jetbrains.kotlin.resolve.constants.evaluate.ConstantExpressionEvaluator.getConstant;
041
042 public final class CallExpressionTranslator extends AbstractCallExpressionTranslator {
043
044 @NotNull
045 public static JsNode translate(
046 @NotNull KtCallExpression expression,
047 @Nullable JsExpression receiver,
048 @NotNull TranslationContext context
049 ) {
050 ResolvedCall<? extends FunctionDescriptor> resolvedCall = CallUtilKt.getFunctionResolvedCallWithAssert(expression, context.bindingContext());
051
052 if (isJsCall(resolvedCall)) {
053 return (new CallExpressionTranslator(expression, receiver, context)).translateJsCode();
054 }
055
056 JsExpression callExpression = (new CallExpressionTranslator(expression, receiver, context)).translate();
057
058 if (!resolvedCall.isSafeCall() && shouldBeInlined(expression, context)) {
059 setInlineCallMetadata(callExpression, expression, resolvedCall, context);
060 }
061
062 return callExpression;
063 }
064
065 public static boolean shouldBeInlined(@NotNull KtCallExpression expression, @NotNull TranslationContext context) {
066 if (!context.getConfig().isInlineEnabled()) return false;
067
068 CallableDescriptor descriptor = getFunctionDescriptor(expression, context);
069 return shouldBeInlined(descriptor);
070 }
071
072 public static boolean shouldBeInlined(@NotNull CallableDescriptor descriptor) {
073 if (descriptor instanceof SimpleFunctionDescriptor) {
074 return InlineUtil.isInline(descriptor);
075 }
076
077 if (descriptor instanceof ValueParameterDescriptor) {
078 return InlineUtil.isInline(descriptor.getContainingDeclaration()) &&
079 InlineUtil.isInlineLambdaParameter((ParameterDescriptor) descriptor);
080 }
081
082 return false;
083 }
084
085 private CallExpressionTranslator(
086 @NotNull KtCallExpression expression,
087 @Nullable JsExpression receiver,
088 @NotNull TranslationContext context
089 ) {
090 super(expression, receiver, context);
091 }
092
093 @NotNull
094 private JsExpression translate() {
095 return CallTranslator.translate(context(), resolvedCall, receiver);
096 }
097
098 @NotNull
099 private JsNode translateJsCode() {
100 List<? extends ValueArgument> arguments = expression.getValueArguments();
101 KtExpression argumentExpression = arguments.get(0).getArgumentExpression();
102 assert argumentExpression != null;
103
104 List<JsStatement> statements = parseJsCode(argumentExpression);
105 int size = statements.size();
106
107 if (size == 0) {
108 return program().getEmptyExpression();
109 } else if (size > 1) {
110 return new JsBlock(statements);
111 } else {
112 JsStatement resultStatement = statements.get(0);
113 if (resultStatement instanceof JsExpressionStatement) {
114 return ((JsExpressionStatement) resultStatement).getExpression();
115 }
116
117 return resultStatement;
118 }
119 }
120
121 @NotNull
122 private List<JsStatement> parseJsCode(@NotNull KtExpression jsCodeExpression) {
123 String jsCode = JsCallChecker.extractStringValue(getConstant(jsCodeExpression, context().bindingContext()));
124
125 assert jsCode != null : "jsCode must be compile time string " + jsCodeExpression.getText();
126
127 // Parser can change local or global scope.
128 // In case of js we want to keep new local names,
129 // but no new global ones.
130 JsScope currentScope = context().scope();
131 assert currentScope instanceof JsFunctionScope : "Usage of js outside of function is unexpected";
132 JsScope temporaryRootScope = new JsRootScope(new JsProgram("<js code>"));
133 JsScope scope = new DelegatingJsFunctionScopeWithTemporaryParent((JsFunctionScope) currentScope, temporaryRootScope);
134 return ParserUtilsKt.parse(jsCode, ThrowExceptionOnErrorReporter.INSTANCE$, scope);
135 }
136 }