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 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 com.google.dart.compiler.backend.js.ast.JsNumberLiteral;
023    import org.jetbrains.annotations.NotNull;
024    import org.jetbrains.annotations.Nullable;
025    import org.jetbrains.kotlin.js.patterns.NamePredicate;
026    import org.jetbrains.kotlin.js.translate.context.TranslationContext;
027    import org.jetbrains.kotlin.js.translate.general.AbstractTranslator;
028    import org.jetbrains.kotlin.js.translate.general.Translation;
029    import org.jetbrains.kotlin.js.translate.intrinsic.functions.factories.TopLevelFIF;
030    import org.jetbrains.kotlin.name.Name;
031    import org.jetbrains.kotlin.psi.*;
032    import org.jetbrains.kotlin.resolve.BindingContext;
033    import org.jetbrains.kotlin.types.JetType;
034    
035    import java.util.Collections;
036    
037    import static org.jetbrains.kotlin.js.translate.utils.ErrorReportingUtils.message;
038    import static org.jetbrains.kotlin.js.translate.utils.JsAstUtils.sum;
039    import static org.jetbrains.kotlin.js.descriptorUtils.DescriptorUtilsPackage.getNameIfStandardType;
040    
041    public final class StringTemplateTranslator extends AbstractTranslator {
042        private final JetStringTemplateEntry[] expressionEntries;
043    
044        @NotNull
045        public static JsExpression translate(@NotNull JetStringTemplateExpression expression,
046                                             @NotNull TranslationContext context) {
047            return (new StringTemplateTranslator(expression, context).translate());
048        }
049    
050        private StringTemplateTranslator(@NotNull JetStringTemplateExpression 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 (JetStringTemplateEntry entry : expressionEntries) {
062                entry.accept(entryVisitor);
063            }
064            return entryVisitor.getResultingExpression();
065        }
066    
067        private final class EntryVisitor extends JetVisitorVoid {
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 JetStringTemplateEntryWithExpression entry) {
083                JetExpression entryExpression = entry.getExpression();
084                assert entryExpression != null :
085                        "JetStringTemplateEntryWithExpression must have not null entry expression.";
086                JsExpression translatedExpression = Translation.translateAsExpression(entryExpression, context());
087                if (translatedExpression instanceof JsNumberLiteral) {
088                    append(context().program().getStringLiteral(translatedExpression.toString()));
089                    return;
090                }
091    
092                JetType type = context().bindingContext().getType(entryExpression);
093                if (type == null || type.isMarkedNullable()) {
094                    append(TopLevelFIF.TO_STRING.apply((JsExpression) null, Collections.singletonList(translatedExpression), context()));
095                }
096                else if (mustCallToString(type)) {
097                    append(new JsInvocation(new JsNameRef("toString", translatedExpression)));
098                }
099                else {
100                    append(translatedExpression);
101                }
102            }
103    
104            private boolean mustCallToString(@NotNull JetType type) {
105                Name typeName = getNameIfStandardType(type);
106                if (typeName != null) {
107                    //TODO: this is a hacky optimization, should use some generic approach
108                    if (NamePredicate.STRING.apply(typeName)) {
109                        return false;
110                    }
111                    else if (NamePredicate.PRIMITIVE_NUMBERS.apply(typeName)) {
112                        return resultingExpression == null;
113                    }
114                }
115                return expressionEntries.length == 1;
116            }
117    
118            @Override
119            public void visitLiteralStringTemplateEntry(@NotNull JetLiteralStringTemplateEntry entry) {
120                appendText(entry.getText());
121            }
122    
123            @Override
124            public void visitEscapeStringTemplateEntry(@NotNull JetEscapeStringTemplateEntry entry) {
125                appendText(entry.getUnescapedValue());
126            }
127    
128            private void appendText(@NotNull String text) {
129                append(program().getStringLiteral(text));
130            }
131    
132            @NotNull
133            public JsExpression getResultingExpression() {
134                assert resultingExpression != null;
135                return resultingExpression;
136            }
137        }
138    }