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.initializer;
018    
019    import com.google.dart.compiler.backend.js.ast.*;
020    import com.intellij.util.SmartList;
021    import org.jetbrains.annotations.NotNull;
022    import org.jetbrains.annotations.Nullable;
023    import org.jetbrains.jet.lang.descriptors.CallableDescriptor;
024    import org.jetbrains.jet.lang.descriptors.ConstructorDescriptor;
025    import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
026    import org.jetbrains.jet.lang.descriptors.PropertyDescriptor;
027    import org.jetbrains.jet.lang.psi.JetClassOrObject;
028    import org.jetbrains.jet.lang.psi.JetDelegationSpecifier;
029    import org.jetbrains.jet.lang.psi.JetDelegatorToSuperCall;
030    import org.jetbrains.jet.lang.psi.JetParameter;
031    import org.jetbrains.jet.lang.resolve.BindingContext;
032    import org.jetbrains.jet.lang.resolve.calls.model.ResolvedCall;
033    import org.jetbrains.jet.lang.types.JetType;
034    import org.jetbrains.jet.lexer.JetTokens;
035    import org.jetbrains.k2js.translate.context.Namer;
036    import org.jetbrains.k2js.translate.context.TranslationContext;
037    import org.jetbrains.k2js.translate.general.AbstractTranslator;
038    import org.jetbrains.k2js.translate.reference.CallArgumentTranslator;
039    
040    import java.util.ArrayList;
041    import java.util.Collections;
042    import java.util.List;
043    
044    import static org.jetbrains.jet.lang.resolve.DescriptorUtils.getClassDescriptorForType;
045    import static org.jetbrains.k2js.translate.utils.BindingUtils.*;
046    import static org.jetbrains.k2js.translate.utils.FunctionBodyTranslator.setDefaultValueForArguments;
047    import static org.jetbrains.k2js.translate.utils.PsiUtils.getPrimaryConstructorParameters;
048    
049    public final class ClassInitializerTranslator extends AbstractTranslator {
050        @NotNull
051        private final JetClassOrObject classDeclaration;
052        @NotNull
053        private final List<JsStatement> initializerStatements = new SmartList<JsStatement>();
054    
055        public ClassInitializerTranslator(@NotNull JetClassOrObject classDeclaration, @NotNull TranslationContext context) {
056            // Note: it's important we use scope for class descriptor because anonymous function used in property initializers
057            // belong to the properties themselves
058            super(context.newDeclaration(getConstructor(context.bindingContext(), classDeclaration)));
059            this.classDeclaration = classDeclaration;
060        }
061    
062        @NotNull
063        public JsFunction generateInitializeMethod() {
064            //TODO: it's inconsistent that we have scope for class and function for constructor, currently have problems implementing better way
065            ConstructorDescriptor primaryConstructor = getConstructor(bindingContext(), classDeclaration);
066            JsFunction result = context().getFunctionObject(primaryConstructor);
067            //NOTE: while we translate constructor parameters we also add property initializer statements
068            // for properties declared as constructor parameters
069            result.getParameters().addAll(translatePrimaryConstructorParameters());
070            mayBeAddCallToSuperMethod(result);
071            new InitializerVisitor(initializerStatements).traverseContainer(classDeclaration, context());
072    
073            List<JsStatement> statements = result.getBody().getStatements();
074            statements.addAll(setDefaultValueForArguments(primaryConstructor, context()));
075            for (JsStatement statement : initializerStatements) {
076                if (statement instanceof JsBlock) {
077                    statements.addAll(((JsBlock) statement).getStatements());
078                }
079                else {
080                    statements.add(statement);
081                }
082            }
083    
084            return result;
085        }
086    
087        @NotNull
088        public JsExpression generateEnumEntryInstanceCreation(@NotNull JetType enumClassType) {
089            JetDelegatorToSuperCall superCall = getSuperCall();
090            List<JsExpression> arguments;
091            if (superCall != null) {
092                arguments = translateArguments(superCall);
093            } else {
094                arguments = Collections.emptyList();
095            }
096            JsNameRef reference = context().getQualifiedReference(getClassDescriptorForType(enumClassType));
097            return new JsNew(reference, arguments);
098        }
099    
100        private void mayBeAddCallToSuperMethod(JsFunction initializer) {
101            if (classDeclaration.hasModifier(JetTokens.ENUM_KEYWORD)) {
102                addCallToSuperMethod(Collections.<JsExpression>emptyList(), initializer);
103                return;
104            }
105            if (hasAncestorClass(bindingContext(), classDeclaration)) {
106                JetDelegatorToSuperCall superCall = getSuperCall();
107                if (superCall == null) {
108                    return;
109                }
110                addCallToSuperMethod(translateArguments(superCall), initializer);
111            }
112        }
113    
114        private void addCallToSuperMethod(@NotNull List<JsExpression> arguments, JsFunction initializer) {
115            JsName ref = context().scope().declareName(Namer.CALLEE_NAME);
116            initializer.setName(ref);
117            JsInvocation call = new JsInvocation(Namer.getFunctionCallRef(Namer.superMethodNameRef(ref)));
118            call.getArguments().add(JsLiteral.THIS);
119            call.getArguments().addAll(arguments);
120            initializerStatements.add(0, call.makeStmt());
121        }
122    
123        @NotNull
124        private List<JsExpression> translateArguments(@NotNull JetDelegatorToSuperCall superCall) {
125            ResolvedCall<? extends CallableDescriptor> call = context().bindingContext().get(BindingContext.RESOLVED_CALL, superCall.getCalleeExpression());
126            assert call != null : "ResolvedCall for superCall must be not null";
127            return CallArgumentTranslator.translate(call, null, context()).getTranslateArguments();
128        }
129    
130        @Nullable
131        private JetDelegatorToSuperCall getSuperCall() {
132            JetDelegatorToSuperCall result = null;
133            for (JetDelegationSpecifier specifier : classDeclaration.getDelegationSpecifiers()) {
134                if (specifier instanceof JetDelegatorToSuperCall) {
135                    result = (JetDelegatorToSuperCall) specifier;
136                }
137            }
138            return result;
139        }
140    
141        @NotNull
142        List<JsParameter> translatePrimaryConstructorParameters() {
143            List<JetParameter> parameterList = getPrimaryConstructorParameters(classDeclaration);
144            List<JsParameter> result = new ArrayList<JsParameter>();
145            for (JetParameter jetParameter : parameterList) {
146                result.add(translateParameter(jetParameter));
147            }
148            return result;
149        }
150    
151        @NotNull
152        private JsParameter translateParameter(@NotNull JetParameter jetParameter) {
153            DeclarationDescriptor parameterDescriptor =
154                    getDescriptorForElement(bindingContext(), jetParameter);
155            JsName parameterName = context().getNameForDescriptor(parameterDescriptor);
156            JsParameter jsParameter = new JsParameter(parameterName);
157            mayBeAddInitializerStatementForProperty(jsParameter, jetParameter);
158            return jsParameter;
159        }
160    
161        private void mayBeAddInitializerStatementForProperty(@NotNull JsParameter jsParameter,
162                @NotNull JetParameter jetParameter) {
163            PropertyDescriptor propertyDescriptor =
164                    getPropertyDescriptorForConstructorParameter(bindingContext(), jetParameter);
165            if (propertyDescriptor == null) {
166                return;
167            }
168            JsNameRef initialValueForProperty = jsParameter.getName().makeRef();
169            addInitializerOrPropertyDefinition(initialValueForProperty, propertyDescriptor);
170        }
171    
172        private void addInitializerOrPropertyDefinition(@NotNull JsNameRef initialValue, @NotNull PropertyDescriptor propertyDescriptor) {
173            initializerStatements.add(InitializerUtils.generateInitializerForProperty(context(), propertyDescriptor, initialValue));
174        }
175    }