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.k2js.translate.utils;
018
019 import com.google.dart.compiler.backend.js.ast.*;
020 import org.jetbrains.annotations.NotNull;
021 import org.jetbrains.jet.lang.descriptors.FunctionDescriptor;
022 import org.jetbrains.jet.lang.descriptors.ValueParameterDescriptor;
023 import org.jetbrains.jet.lang.psi.JetDeclarationWithBody;
024 import org.jetbrains.jet.lang.psi.JetExpression;
025 import org.jetbrains.jet.lang.types.JetType;
026 import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns;
027 import org.jetbrains.k2js.translate.context.TranslationContext;
028 import org.jetbrains.k2js.translate.general.AbstractTranslator;
029 import org.jetbrains.k2js.translate.general.Translation;
030 import org.jetbrains.k2js.translate.utils.mutator.Mutator;
031
032 import java.util.ArrayList;
033 import java.util.List;
034
035 import static org.jetbrains.k2js.translate.utils.BindingUtils.getDefaultArgument;
036 import static org.jetbrains.k2js.translate.utils.JsAstUtils.assignment;
037 import static org.jetbrains.k2js.translate.utils.JsAstUtils.convertToBlock;
038 import static org.jetbrains.k2js.translate.utils.JsAstUtils.equality;
039 import static org.jetbrains.k2js.translate.utils.mutator.LastExpressionMutator.mutateLastExpression;
040
041 public final class FunctionBodyTranslator extends AbstractTranslator {
042
043 @NotNull
044 public static JsBlock translateFunctionBody(@NotNull FunctionDescriptor descriptor,
045 @NotNull JetDeclarationWithBody declarationWithBody,
046 @NotNull TranslationContext functionBodyContext) {
047 return (new FunctionBodyTranslator(descriptor, declarationWithBody, functionBodyContext)).translate();
048 }
049
050 @NotNull
051 public static List<JsStatement> setDefaultValueForArguments(@NotNull FunctionDescriptor descriptor,
052 @NotNull TranslationContext functionBodyContext) {
053 List<JsStatement> result = new ArrayList<JsStatement>();
054 for (ValueParameterDescriptor valueParameter : descriptor.getValueParameters()) {
055 if (valueParameter.hasDefaultValue()) {
056 JsNameRef jsNameRef = functionBodyContext.getNameForDescriptor(valueParameter).makeRef();
057 JetExpression defaultArgument = getDefaultArgument(functionBodyContext.bindingContext(), valueParameter);
058 JsExpression defaultValue = Translation.translateAsExpression(defaultArgument, functionBodyContext);
059
060 JsBinaryOperation checkArgIsUndefined = equality(jsNameRef, functionBodyContext.namer().getUndefinedExpression());
061 JsIf jsIf = new JsIf(checkArgIsUndefined, assignment(jsNameRef, defaultValue).makeStmt());
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())).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.getInstance().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 }