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.utils.dangerous;
018
019 import org.jetbrains.annotations.NotNull;
020 import org.jetbrains.jet.lang.psi.*;
021 import org.jetbrains.jet.lang.resolve.bindingContextUtil.BindingContextUtilPackage;
022 import org.jetbrains.k2js.translate.context.TranslationContext;
023 import org.jetbrains.k2js.translate.reference.InlinedCallExpressionTranslator;
024
025 public final class FindDangerousVisitor extends JetTreeVisitor<DangerousData> {
026
027 @NotNull
028 private final TranslationContext context;
029
030 public FindDangerousVisitor(@NotNull TranslationContext context) {
031 this.context = context;
032 }
033
034 @Override
035 public Void visitDeclaration(@NotNull JetDeclaration dcl, DangerousData data) {
036 return null;
037 }
038
039 @Override
040 public Void visitJetElement(@NotNull JetElement element, DangerousData data) {
041 if (data.exists()) {
042 return null;
043 }
044 return super.visitJetElement(element, data);
045 }
046
047 @Override
048 public Void visitWhenExpression(@NotNull JetWhenExpression expression, DangerousData data) {
049 if (expressionFound(expression, data)) {
050 return null;
051 }
052 return super.visitWhenExpression(expression, data);
053 }
054
055 @Override
056 public Void visitIfExpression(@NotNull JetIfExpression expression, DangerousData data) {
057 if (expressionFound(expression, data)) {
058 return null;
059 }
060 return super.visitIfExpression(expression, data);
061 }
062
063 @Override
064 public Void visitBlockExpression(@NotNull JetBlockExpression expression, DangerousData data) {
065 return BindingContextUtilPackage.isUsedAsStatement(expression, context.bindingContext())
066 ? null
067 : super.visitBlockExpression(expression, data);
068 }
069
070 @Override
071 public Void visitCallExpression(@NotNull JetCallExpression expression, DangerousData data) {
072 if (InlinedCallExpressionTranslator.shouldBeInlined(expression, context)) {
073 if (expressionFound(expression, data)) {
074 return null;
075 }
076 }
077 return super.visitCallExpression(expression, data);
078 }
079
080 private boolean expressionFound(@NotNull JetExpression expression, @NotNull DangerousData data) {
081 if (data.exists()) {
082 return true;
083 }
084 if (!BindingContextUtilPackage.isUsedAsStatement(expression, context.bindingContext())) {
085 data.setDangerousNode(expression);
086 return true;
087 }
088 return false;
089 }
090 }