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