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