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.reference;
018    
019    import com.google.dart.compiler.backend.js.ast.JsConditional;
020    import com.google.dart.compiler.backend.js.ast.JsExpression;
021    import com.google.dart.compiler.backend.js.ast.JsLiteral;
022    import org.jetbrains.annotations.NotNull;
023    import org.jetbrains.annotations.Nullable;
024    import org.jetbrains.jet.lang.psi.JetDotQualifiedExpression;
025    import org.jetbrains.jet.lang.psi.JetQualifiedExpression;
026    import org.jetbrains.jet.lang.psi.JetSafeQualifiedExpression;
027    import org.jetbrains.k2js.translate.context.TranslationContext;
028    
029    import static org.jetbrains.k2js.translate.utils.TranslationUtils.notNullConditional;
030    
031    public enum CallType {
032        SAFE {
033            @NotNull
034            @Override
035            JsExpression constructCall(@Nullable JsExpression receiver, @NotNull CallConstructor constructor,
036                                       @NotNull TranslationContext context) {
037                assert receiver != null;
038                JsConditional expression = notNullConditional(receiver, JsLiteral.NULL, context);
039                expression.setThenExpression(constructor.construct(expression.getThenExpression()));
040                return expression;
041            }
042        },
043        //TODO: bang qualifier is not implemented in frontend for now
044        // BANG,
045        NORMAL {
046            @NotNull
047            @Override
048            JsExpression constructCall(@Nullable JsExpression receiver, @NotNull CallConstructor constructor,
049                                       @NotNull TranslationContext context) {
050                return constructor.construct(receiver);
051            }
052        };
053    
054        @NotNull
055        abstract JsExpression constructCall(@Nullable JsExpression receiver, @NotNull CallConstructor constructor,
056                                            @NotNull TranslationContext context);
057    
058        @NotNull
059        public static CallType getCallTypeForQualifiedExpression(@NotNull JetQualifiedExpression expression) {
060            if (expression instanceof JetSafeQualifiedExpression) {
061                return SAFE;
062            }
063            assert expression instanceof JetDotQualifiedExpression;
064            return NORMAL;
065        }
066    
067        public interface CallConstructor {
068            @NotNull
069            JsExpression construct(@Nullable JsExpression receiver);
070        }
071    
072    }