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.JsBlock;
020 import com.google.dart.compiler.backend.js.ast.JsCatch;
021 import com.google.dart.compiler.backend.js.ast.JsName;
022 import com.google.dart.compiler.backend.js.ast.JsTry;
023 import com.intellij.util.SmartList;
024 import org.jetbrains.annotations.NotNull;
025 import org.jetbrains.annotations.Nullable;
026 import org.jetbrains.jet.lang.psi.*;
027 import org.jetbrains.k2js.translate.context.TranslationContext;
028 import org.jetbrains.k2js.translate.general.AbstractTranslator;
029 import org.jetbrains.k2js.translate.general.Translation;
030
031 import java.util.List;
032
033 import static org.jetbrains.k2js.translate.utils.JsAstUtils.convertToBlock;
034
035 //TODO: not tested at all
036 //TODO: not implemented catch logic
037 public final class TryTranslator extends AbstractTranslator {
038
039 @NotNull
040 public static JsTry translate(@NotNull JetTryExpression expression,
041 @NotNull TranslationContext context) {
042 return (new TryTranslator(expression, context)).translate();
043 }
044
045 @NotNull
046 private final JetTryExpression expression;
047
048 private TryTranslator(@NotNull JetTryExpression expression,
049 @NotNull TranslationContext context) {
050 super(context);
051 this.expression = expression;
052 }
053
054 private JsTry translate() {
055 return new JsTry(translateTryBlock(), translateCatches(), translateFinallyBlock());
056 }
057
058 @Nullable
059 private JsBlock translateFinallyBlock() {
060 JetFinallySection finallyBlock = expression.getFinallyBlock();
061 if (finallyBlock == null) return null;
062
063 return convertToBlock(Translation.translateAsStatementAndMergeInBlockIfNeeded(finallyBlock.getFinalExpression(), context()));
064 }
065
066 @NotNull
067 private JsBlock translateTryBlock() {
068 return convertToBlock(Translation.translateAsStatementAndMergeInBlockIfNeeded(expression.getTryBlock(), context()));
069 }
070
071 @NotNull
072 private List<JsCatch> translateCatches() {
073 List<JsCatch> result = new SmartList<JsCatch>();
074 for (JetCatchClause catchClause : expression.getCatchClauses()) {
075 result.add(translateCatchClause(catchClause));
076 }
077 return result;
078 }
079
080 @NotNull
081 private JsCatch translateCatchClause(@NotNull JetCatchClause catchClause) {
082 JetParameter catchParameter = catchClause.getCatchParameter();
083 assert catchParameter != null : "Valid catch must have a parameter.";
084
085 JsName parameterName = context().getNameForElement(catchParameter);
086 JsCatch result = new JsCatch(context().scope(), parameterName.getIdent());
087 result.setBody(translateCatchBody(catchClause));
088 return result;
089 }
090
091 @NotNull
092 private JsBlock translateCatchBody(@NotNull JetCatchClause catchClause) {
093 JetExpression catchBody = catchClause.getCatchBody();
094 if (catchBody == null) {
095 return convertToBlock(context().getEmptyStatement());
096 }
097 return convertToBlock(Translation.translateAsStatementAndMergeInBlockIfNeeded(catchBody, context()));
098 }
099
100 }