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