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.JsBlock;
020    import com.google.dart.compiler.backend.js.ast.JsExpression;
021    import com.google.dart.compiler.backend.js.ast.JsNode;
022    import com.google.dart.compiler.backend.js.ast.JsReturn;
023    import org.jetbrains.annotations.NotNull;
024    import org.jetbrains.jet.lang.descriptors.FunctionDescriptor;
025    import org.jetbrains.jet.lang.psi.JetDeclarationWithBody;
026    import org.jetbrains.jet.lang.psi.JetExpression;
027    import org.jetbrains.jet.lang.types.JetType;
028    import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns;
029    import org.jetbrains.k2js.translate.context.TranslationContext;
030    import org.jetbrains.k2js.translate.general.AbstractTranslator;
031    import org.jetbrains.k2js.translate.general.Translation;
032    import org.jetbrains.k2js.translate.utils.mutator.Mutator;
033    
034    import static org.jetbrains.k2js.translate.utils.JsAstUtils.convertToBlock;
035    import static org.jetbrains.k2js.translate.utils.mutator.LastExpressionMutator.mutateLastExpression;
036    
037    public final class FunctionBodyTranslator extends AbstractTranslator {
038    
039        @NotNull
040        public static JsBlock translateFunctionBody(@NotNull FunctionDescriptor descriptor,
041                                                    @NotNull JetDeclarationWithBody declarationWithBody,
042                                                    @NotNull TranslationContext functionBodyContext) {
043            return (new FunctionBodyTranslator(descriptor, declarationWithBody, functionBodyContext)).translate();
044        }
045    
046    
047        @NotNull
048        private final FunctionDescriptor descriptor;
049        @NotNull
050        private final JetDeclarationWithBody declaration;
051    
052        private FunctionBodyTranslator(@NotNull FunctionDescriptor descriptor,
053                                       @NotNull JetDeclarationWithBody declaration,
054                                       @NotNull TranslationContext context) {
055            super(context);
056            this.descriptor = descriptor;
057            this.declaration = declaration;
058        }
059    
060        @NotNull
061        private JsBlock translate() {
062            JetExpression jetBodyExpression = declaration.getBodyExpression();
063            assert jetBodyExpression != null : "Cannot translate a body of an abstract function.";
064            return mayBeWrapWithReturn(Translation.translateExpression(jetBodyExpression, context()));
065        }
066    
067        @NotNull
068        private JsBlock mayBeWrapWithReturn(@NotNull JsNode body) {
069            if (!mustAddReturnToGeneratedFunctionBody()) {
070                return convertToBlock(body);
071            }
072            return convertToBlock(lastExpressionReturned(body));
073        }
074    
075        private boolean mustAddReturnToGeneratedFunctionBody() {
076            JetType functionReturnType = descriptor.getReturnType();
077            assert functionReturnType != null : "Function return typed type must be resolved.";
078            return (!declaration.hasBlockBody()) && (!KotlinBuiltIns.getInstance().isUnit(functionReturnType));
079        }
080    
081        @NotNull
082        private static JsNode lastExpressionReturned(@NotNull JsNode body) {
083            return mutateLastExpression(body, new Mutator() {
084                @Override
085                @NotNull
086                public JsNode mutate(@NotNull JsNode node) {
087                    if (!(node instanceof JsExpression)) {
088                        return node;
089                    }
090                    return new JsReturn((JsExpression)node);
091                }
092            });
093        }
094    }