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.operation;
018    
019    import com.google.dart.compiler.backend.js.ast.JsConditional;
020    import com.google.dart.compiler.backend.js.ast.JsExpression;
021    import org.jetbrains.annotations.NotNull;
022    import org.jetbrains.jet.lang.psi.JetUnaryExpression;
023    import org.jetbrains.jet.lexer.JetTokens;
024    import org.jetbrains.k2js.translate.context.TemporaryConstVariable;
025    import org.jetbrains.k2js.translate.context.TranslationContext;
026    import org.jetbrains.k2js.translate.reference.CallBuilder;
027    import org.jetbrains.k2js.translate.reference.CallType;
028    import org.jetbrains.k2js.translate.utils.TranslationUtils;
029    
030    import java.util.Collections;
031    
032    import static org.jetbrains.k2js.translate.general.Translation.translateAsExpression;
033    import static org.jetbrains.k2js.translate.utils.BindingUtils.getResolvedCall;
034    import static org.jetbrains.k2js.translate.utils.PsiUtils.getBaseExpression;
035    import static org.jetbrains.k2js.translate.utils.PsiUtils.getOperationToken;
036    import static org.jetbrains.k2js.translate.utils.TranslationUtils.isNotNullCheck;
037    
038    
039    public final class UnaryOperationTranslator {
040        private UnaryOperationTranslator() {
041        }
042    
043        @NotNull
044        public static JsExpression translate(@NotNull JetUnaryExpression expression,
045                                             @NotNull TranslationContext context) {
046            if (isExclExcl(expression)) {
047                return translateExclExclOperator(expression, context);
048            }
049            if (IncrementTranslator.isIncrement(expression)) {
050                return IncrementTranslator.translate(expression, context);
051            }
052            return translateAsCall(expression, context);
053        }
054    
055        private static boolean isExclExcl(@NotNull JetUnaryExpression expression) {
056            return getOperationToken(expression).equals(JetTokens.EXCLEXCL);
057        }
058    
059        @NotNull
060        private static JsExpression translateExclExclOperator(@NotNull JetUnaryExpression expression, @NotNull TranslationContext context) {
061            JsExpression translatedExpression = translateAsExpression(getBaseExpression(expression), context);
062            TemporaryConstVariable tempVar = context.getOrDeclareTemporaryConstVariable(translatedExpression);
063    
064            JsConditional ensureNotNull = new JsConditional(isNotNullCheck(tempVar.value()), tempVar.value(), context.namer().throwNPEFunctionCall());
065    
066            // associate (cache) ensureNotNull expression to new LazyValue with same name.
067            context.associateExpressionToLazyValue(ensureNotNull, new TemporaryConstVariable(tempVar.name(), ensureNotNull));
068            return ensureNotNull;
069        }
070    
071        @NotNull
072        private static JsExpression translateAsCall(@NotNull JetUnaryExpression expression,
073                                                    @NotNull TranslationContext context) {
074            return CallBuilder.build(context)
075                    .receiver(TranslationUtils.translateBaseExpression(context, expression))
076                    .args(Collections.<JsExpression>emptyList())
077                    .resolvedCall(getResolvedCall(context.bindingContext(), expression.getOperationReference()))
078                    .type(CallType.NORMAL).translate();
079        }
080    }