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.JsInvocation;
021    import com.google.dart.compiler.backend.js.ast.JsNameRef;
022    import org.jetbrains.annotations.NotNull;
023    import org.jetbrains.annotations.Nullable;
024    import org.jetbrains.jet.lang.descriptors.ClassDescriptor;
025    import org.jetbrains.jet.lang.psi.JetExpression;
026    import org.jetbrains.jet.lang.psi.JetIsExpression;
027    import org.jetbrains.jet.lang.psi.JetTypeReference;
028    import org.jetbrains.jet.lang.resolve.name.Name;
029    import org.jetbrains.k2js.translate.context.TranslationContext;
030    import org.jetbrains.k2js.translate.general.AbstractTranslator;
031    import org.jetbrains.k2js.translate.general.Translation;
032    import org.jetbrains.k2js.translate.intrinsic.functions.patterns.NamePredicate;
033    import org.jetbrains.k2js.translate.utils.BindingUtils;
034    import org.jetbrains.k2js.translate.utils.TranslationUtils;
035    
036    import static org.jetbrains.k2js.translate.utils.BindingUtils.getTypeByReference;
037    import static org.jetbrains.k2js.translate.utils.JsAstUtils.*;
038    import static org.jetbrains.k2js.translate.utils.JsDescriptorUtils.getNameIfStandardType;
039    
040    public final class PatternTranslator extends AbstractTranslator {
041    
042        @NotNull
043        public static PatternTranslator newInstance(@NotNull TranslationContext context) {
044            return new PatternTranslator(context);
045        }
046    
047        private PatternTranslator(@NotNull TranslationContext context) {
048            super(context);
049        }
050    
051        @NotNull
052        public JsExpression translateIsExpression(@NotNull JetIsExpression expression) {
053            JsExpression left = Translation.translateAsExpression(expression.getLeftHandSide(), context());
054            JetTypeReference typeReference = expression.getTypeRef();
055            assert typeReference != null;
056            JsExpression result = translateIsCheck(left, typeReference);
057            if (expression.isNegated()) {
058                return negated(result);
059            }
060            return result;
061        }
062    
063        @NotNull
064        public JsExpression translateIsCheck(@NotNull JsExpression subject, @NotNull JetTypeReference typeReference) {
065            JsExpression result = translateAsIntrinsicTypeCheck(subject, typeReference);
066            if (result != null) {
067                return result;
068            }
069            return translateAsIsCheck(subject, typeReference);
070        }
071    
072        @NotNull
073        private JsExpression translateAsIsCheck(@NotNull JsExpression expressionToMatch,
074                                                @NotNull JetTypeReference typeReference) {
075            JsInvocation isCheck = new JsInvocation(context().namer().isOperationReference(),
076                                                         expressionToMatch, getClassNameReference(typeReference));
077            if (isNullable(typeReference)) {
078                return addNullCheck(expressionToMatch, isCheck);
079            }
080            return isCheck;
081        }
082    
083        @Nullable
084        private JsExpression translateAsIntrinsicTypeCheck(@NotNull JsExpression expressionToMatch,
085                                                           @NotNull JetTypeReference typeReference) {
086            Name typeName = getNameIfStandardType(getTypeByReference(bindingContext(), typeReference));
087            if (typeName == null) {
088                return null;
089            }
090    
091            String jsSTypeName;
092            if (NamePredicate.STRING.apply(typeName)) {
093                jsSTypeName = "string";
094            }
095            else if (NamePredicate.PRIMITIVE_NUMBERS.apply(typeName)) {
096                jsSTypeName = "number";
097            }
098            else {
099                return null;
100            }
101            return typeof(expressionToMatch, program().getStringLiteral(jsSTypeName));
102        }
103    
104        @NotNull
105        private static JsExpression addNullCheck(@NotNull JsExpression expressionToMatch, @NotNull JsInvocation isCheck) {
106            return or(TranslationUtils.isNullCheck(expressionToMatch), isCheck);
107        }
108    
109        private boolean isNullable(JetTypeReference typeReference) {
110            return getTypeByReference(bindingContext(), typeReference).isNullable();
111        }
112    
113        @NotNull
114        private JsNameRef getClassNameReference(@NotNull JetTypeReference typeReference) {
115            ClassDescriptor referencedClass = BindingUtils.getClassDescriptorForTypeReference
116                (bindingContext(), typeReference);
117            return context().getQualifiedReference(referencedClass);
118        }
119    
120        @NotNull
121        public JsExpression translateExpressionPattern(@NotNull JsExpression expressionToMatch, @NotNull JetExpression patternExpression) {
122            JsExpression expressionToMatchAgainst = translateExpressionForExpressionPattern(patternExpression);
123            return equality(expressionToMatch, expressionToMatchAgainst);
124        }
125    
126        @NotNull
127        public JsExpression translateExpressionForExpressionPattern(@NotNull JetExpression patternExpression) {
128            return Translation.translateAsExpression(patternExpression, context());
129        }
130    }