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