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.utils;
018
019 import com.google.dart.compiler.backend.js.ast.*;
020 import org.jetbrains.annotations.NotNull;
021 import org.jetbrains.kotlin.builtins.KotlinBuiltIns;
022 import org.jetbrains.kotlin.descriptors.FunctionDescriptor;
023 import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor;
024 import org.jetbrains.kotlin.js.translate.context.TranslationContext;
025 import org.jetbrains.kotlin.js.translate.general.AbstractTranslator;
026 import org.jetbrains.kotlin.js.translate.general.Translation;
027 import org.jetbrains.kotlin.js.translate.utils.mutator.Mutator;
028 import org.jetbrains.kotlin.psi.JetDeclarationWithBody;
029 import org.jetbrains.kotlin.psi.JetExpression;
030 import org.jetbrains.kotlin.resolve.descriptorUtil.DescriptorUtilPackage;
031 import org.jetbrains.kotlin.types.JetType;
032
033 import java.util.ArrayList;
034 import java.util.List;
035
036 import static org.jetbrains.kotlin.js.translate.utils.BindingUtils.getDefaultArgument;
037 import static org.jetbrains.kotlin.js.translate.utils.JsAstUtils.*;
038 import static org.jetbrains.kotlin.js.translate.utils.mutator.LastExpressionMutator.mutateLastExpression;
039
040 public final class FunctionBodyTranslator extends AbstractTranslator {
041
042 @NotNull
043 public static JsBlock translateFunctionBody(@NotNull FunctionDescriptor descriptor,
044 @NotNull JetDeclarationWithBody declarationWithBody,
045 @NotNull TranslationContext functionBodyContext) {
046 return (new FunctionBodyTranslator(descriptor, declarationWithBody, functionBodyContext)).translate();
047 }
048
049 @NotNull
050 public static List<JsStatement> setDefaultValueForArguments(@NotNull FunctionDescriptor descriptor,
051 @NotNull TranslationContext functionBodyContext) {
052 List<ValueParameterDescriptor> valueParameters = descriptor.getValueParameters();
053
054 List<JsStatement> result = new ArrayList<JsStatement>(valueParameters.size());
055 for (ValueParameterDescriptor valueParameter : valueParameters) {
056 if (!DescriptorUtilPackage.hasDefaultValue(valueParameter)) continue;
057
058 JsNameRef jsNameRef = functionBodyContext.getNameForDescriptor(valueParameter).makeRef();
059 JetExpression defaultArgument = getDefaultArgument(valueParameter);
060 JsBlock defaultArgBlock = new JsBlock();
061 JsExpression defaultValue = Translation.translateAsExpression(defaultArgument, functionBodyContext, defaultArgBlock);
062 JsStatement assignStatement = assignment(jsNameRef, defaultValue).makeStmt();
063 JsStatement thenStatement = JsAstUtils.mergeStatementInBlockIfNeeded(assignStatement, defaultArgBlock);
064 JsBinaryOperation checkArgIsUndefined = equality(jsNameRef, functionBodyContext.namer().getUndefinedExpression());
065 JsIf jsIf = JsAstUtils.newJsIf(checkArgIsUndefined, thenStatement);
066 result.add(jsIf);
067 }
068
069 return result;
070 }
071
072 @NotNull
073 private final FunctionDescriptor descriptor;
074 @NotNull
075 private final JetDeclarationWithBody declaration;
076
077 private FunctionBodyTranslator(@NotNull FunctionDescriptor descriptor,
078 @NotNull JetDeclarationWithBody declaration,
079 @NotNull TranslationContext context) {
080 super(context);
081 this.descriptor = descriptor;
082 this.declaration = declaration;
083 }
084
085 @NotNull
086 private JsBlock translate() {
087 JetExpression jetBodyExpression = declaration.getBodyExpression();
088 assert jetBodyExpression != null : "Cannot translate a body of an abstract function.";
089 JsBlock jsBlock = new JsBlock(setDefaultValueForArguments(descriptor, context()));
090 jsBlock.getStatements().addAll(mayBeWrapWithReturn(Translation.translateExpression(jetBodyExpression, context(), jsBlock)).getStatements());
091 return jsBlock;
092 }
093
094 @NotNull
095 private JsBlock mayBeWrapWithReturn(@NotNull JsNode body) {
096 if (!mustAddReturnToGeneratedFunctionBody()) {
097 return convertToBlock(body);
098 }
099 return convertToBlock(lastExpressionReturned(body));
100 }
101
102 private boolean mustAddReturnToGeneratedFunctionBody() {
103 JetType functionReturnType = descriptor.getReturnType();
104 assert functionReturnType != null : "Function return typed type must be resolved.";
105 return (!declaration.hasBlockBody()) && (!KotlinBuiltIns.isUnit(functionReturnType));
106 }
107
108 @NotNull
109 private static JsNode lastExpressionReturned(@NotNull JsNode body) {
110 return mutateLastExpression(body, new Mutator() {
111 @Override
112 @NotNull
113 public JsNode mutate(@NotNull JsNode node) {
114 if (!(node instanceof JsExpression)) {
115 return node;
116 }
117 return new JsReturn((JsExpression)node);
118 }
119 });
120 }
121 }