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 =
051 CallUtilKt.getFunctionResolvedCallWithAssert(expression, context.bindingContext());
052
053 if (isJsCall(resolvedCall)) {
054 return (new CallExpressionTranslator(expression, receiver, context)).translateJsCode();
055 }
056
057 JsExpression callExpression = (new CallExpressionTranslator(expression, receiver, context)).translate();
058
059 if (shouldBeInlined(expression, context)) {
060 setInlineCallMetadata(callExpression, expression, resolvedCall, context);
061 }
062
063 return callExpression;
064 }
065
066 public static boolean shouldBeInlined(@NotNull KtCallExpression expression, @NotNull TranslationContext context) {
067 if (!context.getConfig().isInlineEnabled()) return false;
068
069 CallableDescriptor descriptor = getFunctionDescriptor(expression, context);
070 return shouldBeInlined(descriptor);
071 }
072
073 public static boolean shouldBeInlined(@NotNull CallableDescriptor descriptor) {
074 if (descriptor instanceof SimpleFunctionDescriptor) {
075 return InlineUtil.isInline(descriptor);
076 }
077
078 if (descriptor instanceof ValueParameterDescriptor) {
079 return InlineUtil.isInline(descriptor.getContainingDeclaration()) &&
080 InlineUtil.isInlineLambdaParameter((ParameterDescriptor) descriptor);
081 }
082
083 return false;
084 }
085
086 private CallExpressionTranslator(
087 @NotNull KtCallExpression expression,
088 @Nullable JsExpression receiver,
089 @NotNull TranslationContext context
090 ) {
091 super(expression, receiver, context);
092 }
093
094 @NotNull
095 private JsExpression translate() {
096 return CallTranslator.translate(context(), resolvedCall, receiver);
097 }
098
099 @NotNull
100 private JsNode translateJsCode() {
101 List<? extends ValueArgument> arguments = expression.getValueArguments();
102 KtExpression argumentExpression = arguments.get(0).getArgumentExpression();
103 assert argumentExpression != null;
104
105 List<JsStatement> statements = parseJsCode(argumentExpression);
106 int size = statements.size();
107
108 if (size == 0) {
109 return JsLiteral.NULL;
110 } else if (size > 1) {
111 return new JsBlock(statements);
112 } else {
113 JsStatement resultStatement = statements.get(0);
114 if (resultStatement instanceof JsExpressionStatement) {
115 return ((JsExpressionStatement) resultStatement).getExpression();
116 }
117
118 return resultStatement;
119 }
120 }
121
122 @NotNull
123 private List<JsStatement> parseJsCode(@NotNull KtExpression jsCodeExpression) {
124 String jsCode = JsCallChecker.extractStringValue(getConstant(jsCodeExpression, context().bindingContext()));
125
126 assert jsCode != null : "jsCode must be compile time string " + jsCodeExpression.getText();
127
128 // Parser can change local or global scope.
129 // In case of js we want to keep new local names,
130 // but no new global ones.
131 JsScope currentScope = context().scope();
132 assert currentScope instanceof JsFunctionScope : "Usage of js outside of function is unexpected";
133 JsScope temporaryRootScope = new JsRootScope(new JsProgram("<js code>"));
134 JsScope scope = new DelegatingJsFunctionScopeWithTemporaryParent((JsFunctionScope) currentScope, temporaryRootScope);
135 return ParserUtilsKt.parse(jsCode, ThrowExceptionOnErrorReporter.INSTANCE, scope);
136 }
137 }