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