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