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.expression;
018
019 import com.google.dart.compiler.backend.js.ast.JsExpression;
020 import com.google.dart.compiler.backend.js.ast.JsName;
021 import com.google.dart.compiler.backend.js.ast.JsNameRef;
022 import com.google.dart.compiler.backend.js.ast.JsVars;
023 import org.jetbrains.annotations.NotNull;
024 import org.jetbrains.annotations.Nullable;
025 import org.jetbrains.kotlin.descriptors.FunctionDescriptor;
026 import org.jetbrains.kotlin.descriptors.VariableDescriptor;
027 import org.jetbrains.kotlin.js.translate.callTranslator.CallTranslator;
028 import org.jetbrains.kotlin.js.translate.context.TranslationContext;
029 import org.jetbrains.kotlin.js.translate.general.AbstractTranslator;
030 import org.jetbrains.kotlin.js.translate.reference.CallExpressionTranslator;
031 import org.jetbrains.kotlin.psi.KtMultiDeclaration;
032 import org.jetbrains.kotlin.psi.KtMultiDeclarationEntry;
033 import org.jetbrains.kotlin.resolve.BindingContext;
034 import org.jetbrains.kotlin.resolve.BindingContextUtils;
035 import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall;
036
037 import java.util.ArrayList;
038 import java.util.List;
039
040 import static org.jetbrains.kotlin.js.translate.utils.InlineUtils.setInlineCallMetadata;
041
042 public class MultiDeclarationTranslator extends AbstractTranslator {
043
044 // if multiObjectName was init, initializer must be null
045 @NotNull
046 public static JsVars translate(
047 @NotNull KtMultiDeclaration multiDeclaration,
048 @NotNull JsName multiObjectName,
049 @Nullable JsExpression initializer,
050 @NotNull TranslationContext context
051 ) {
052 return new MultiDeclarationTranslator(multiDeclaration, multiObjectName, initializer, context).translate();
053 }
054
055 @NotNull
056 private final KtMultiDeclaration multiDeclaration;
057 @NotNull
058 private final JsName multiObjectName;
059 @Nullable
060 private final JsExpression initializer;
061
062 private MultiDeclarationTranslator(
063 @NotNull KtMultiDeclaration multiDeclaration,
064 @NotNull JsName multiObjectName,
065 @Nullable JsExpression initializer,
066 @NotNull TranslationContext context
067 ) {
068 super(context);
069 this.multiDeclaration = multiDeclaration;
070 this.multiObjectName = multiObjectName;
071 this.initializer = initializer;
072 }
073
074 private JsVars translate() {
075 List<JsVars.JsVar> jsVars = new ArrayList<JsVars.JsVar>();
076 if (initializer != null) {
077 jsVars.add(new JsVars.JsVar(multiObjectName, initializer));
078 }
079
080 JsNameRef multiObjNameRef = multiObjectName.makeRef();
081 for (KtMultiDeclarationEntry entry : multiDeclaration.getEntries()) {
082 ResolvedCall<FunctionDescriptor> entryInitCall = context().bindingContext().get(BindingContext.COMPONENT_RESOLVED_CALL, entry);
083 assert entryInitCall != null : "Entry init call must be not null";
084 JsExpression entryInitializer = CallTranslator.translate(context(), entryInitCall, multiObjNameRef);
085 FunctionDescriptor candidateDescriptor = entryInitCall.getCandidateDescriptor();
086 if (CallExpressionTranslator.shouldBeInlined(candidateDescriptor)) {
087 setInlineCallMetadata(entryInitializer, entry, entryInitCall, context());
088 }
089 VariableDescriptor descriptor = BindingContextUtils.getNotNull( context().bindingContext(), BindingContext.VARIABLE, entry);
090 JsName name = context().getNameForDescriptor(descriptor);
091 jsVars.add(new JsVars.JsVar(name, entryInitializer));
092 }
093 return new JsVars(jsVars, true);
094 }
095 }