001    /*
002     * Copyright 2010-2015 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.kotlin.js.translate.expression;
018    
019    import org.jetbrains.kotlin.js.backend.ast.JsExpression;
020    import org.jetbrains.kotlin.js.backend.ast.JsInvocation;
021    import org.jetbrains.kotlin.js.backend.ast.JsNameRef;
022    import org.jetbrains.kotlin.js.backend.ast.JsNumberLiteral;
023    import com.intellij.util.SmartList;
024    import org.jetbrains.annotations.NotNull;
025    import org.jetbrains.annotations.Nullable;
026    import org.jetbrains.kotlin.builtins.KotlinBuiltIns;
027    import org.jetbrains.kotlin.js.descriptorUtils.DescriptorUtilsKt;
028    import org.jetbrains.kotlin.js.patterns.NamePredicate;
029    import org.jetbrains.kotlin.js.translate.context.TranslationContext;
030    import org.jetbrains.kotlin.js.translate.general.AbstractTranslator;
031    import org.jetbrains.kotlin.js.translate.general.Translation;
032    import org.jetbrains.kotlin.js.translate.intrinsic.functions.factories.TopLevelFIF;
033    import org.jetbrains.kotlin.js.translate.utils.JsAstUtils;
034    import org.jetbrains.kotlin.name.Name;
035    import org.jetbrains.kotlin.psi.*;
036    import org.jetbrains.kotlin.types.KotlinType;
037    
038    import static org.jetbrains.kotlin.js.translate.utils.ErrorReportingUtils.message;
039    import static org.jetbrains.kotlin.js.translate.utils.JsAstUtils.sum;
040    
041    public final class StringTemplateTranslator extends AbstractTranslator {
042        private final KtStringTemplateEntry[] expressionEntries;
043    
044        @NotNull
045        public static JsExpression translate(@NotNull KtStringTemplateExpression expression,
046                                             @NotNull TranslationContext context) {
047            return (new StringTemplateTranslator(expression, context).translate());
048        }
049    
050        private StringTemplateTranslator(@NotNull KtStringTemplateExpression expression,
051                                         @NotNull TranslationContext context) {
052            super(context);
053    
054            expressionEntries = expression.getEntries();
055            assert expressionEntries.length != 0 : message(expression, "String template must have one or more entries.");
056        }
057    
058        @NotNull
059        private JsExpression translate() {
060            EntryVisitor entryVisitor = new EntryVisitor();
061            for (KtStringTemplateEntry entry : expressionEntries) {
062                entry.accept(entryVisitor);
063            }
064            return entryVisitor.getResultingExpression();
065        }
066    
067        private final class EntryVisitor extends KtVisitorVoid {
068    
069            @Nullable
070            private JsExpression resultingExpression = null;
071    
072            void append(@NotNull JsExpression expression) {
073                if (resultingExpression == null) {
074                    resultingExpression = expression;
075                }
076                else {
077                    resultingExpression = sum(resultingExpression, expression);
078                }
079            }
080    
081            @Override
082            public void visitStringTemplateEntryWithExpression(@NotNull KtStringTemplateEntryWithExpression entry) {
083                KtExpression entryExpression = entry.getExpression();
084                assert entryExpression != null :
085                        "JetStringTemplateEntryWithExpression must have not null entry expression.";
086                JsExpression translatedExpression = Translation.translateAsExpression(entryExpression, context());
087    
088                KotlinType type = context().bindingContext().getType(entryExpression);
089    
090                if (translatedExpression instanceof JsNumberLiteral) {
091                    append(context().program().getStringLiteral(translatedExpression.toString()));
092                    return;
093                }
094    
095                if (type == null || type.isMarkedNullable()) {
096                    append(TopLevelFIF.TO_STRING.apply((JsExpression) null, new SmartList<JsExpression>(translatedExpression), context()));
097                }
098                else if (KotlinBuiltIns.isChar(type)) {
099                    append(JsAstUtils.charToString(translatedExpression));
100                }
101                else if (mustCallToString(type)) {
102                    append(new JsInvocation(new JsNameRef("toString", translatedExpression)));
103                }
104                else {
105                    append(translatedExpression);
106                }
107            }
108    
109            private boolean mustCallToString(@NotNull KotlinType type) {
110                Name typeName = DescriptorUtilsKt.getNameIfStandardType(type);
111                if (typeName != null) {
112                    //TODO: this is a hacky optimization, should use some generic approach
113                    if (NamePredicate.STRING.apply(typeName)) {
114                        return false;
115                    }
116                    else if (NamePredicate.PRIMITIVE_NUMBERS.apply(typeName)) {
117                        return resultingExpression == null;
118                    }
119                }
120                return expressionEntries.length == 1;
121            }
122    
123            @Override
124            public void visitLiteralStringTemplateEntry(@NotNull KtLiteralStringTemplateEntry entry) {
125                appendText(entry.getText());
126            }
127    
128            @Override
129            public void visitEscapeStringTemplateEntry(@NotNull KtEscapeStringTemplateEntry entry) {
130                appendText(entry.getUnescapedValue());
131            }
132    
133            private void appendText(@NotNull String text) {
134                append(program().getStringLiteral(text));
135            }
136    
137            @NotNull
138            public JsExpression getResultingExpression() {
139                assert resultingExpression != null;
140                return resultingExpression;
141            }
142        }
143    }