All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.teavm.javascript.AsyncCallsFinder Maven / Gradle / Ivy

There is a newer version: 0.10.2
Show newest version
/*
 *  Copyright 2015 Alexey Andreev.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.teavm.javascript;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.teavm.javascript.ast.*;
import org.teavm.model.MethodReference;

/**
 *
 * @author Alexey Andreev
 */
class AsyncCallsFinder implements StatementVisitor, ExprVisitor {
    Set asyncCalls = new HashSet<>();
    Set allCalls = new HashSet<>();

    private void visitList(List statements) {
        for (Statement stmt : statements) {
            stmt.acceptVisitor(this);
        }
    }

    @Override
    public void visit(AssignmentStatement statement) {
        if (statement.getLeftValue() != null) {
            statement.getLeftValue().acceptVisitor(this);
        }
        statement.getRightValue().acceptVisitor(this);
        if (!statement.isAsync()) {
            statement.getRightValue().acceptVisitor(this);
            return;
        }
        if (!(statement.getRightValue() instanceof InvocationExpr)) {
            statement.getRightValue().acceptVisitor(this);
            return;
        }
        InvocationExpr invocation = (InvocationExpr) statement.getRightValue();
        asyncCalls.add(invocation.getMethod());
    }

    @Override
    public void visit(SequentialStatement statement) {
        visitList(statement.getSequence());
    }

    @Override
    public void visit(ConditionalStatement statement) {
        visitList(statement.getConsequent());
        visitList(statement.getAlternative());
        statement.getCondition().acceptVisitor(this);
    }

    @Override
    public void visit(SwitchStatement statement) {
        for (SwitchClause clause : statement.getClauses()) {
            visitList(clause.getBody());
        }
        visitList(statement.getDefaultClause());
        statement.getValue().acceptVisitor(this);
    }

    @Override
    public void visit(WhileStatement statement) {
        visitList(statement.getBody());
        if (statement.getCondition() != null) {
            statement.getCondition().acceptVisitor(this);
        }
    }

    @Override
    public void visit(BlockStatement statement) {
        visitList(statement.getBody());
    }

    @Override
    public void visit(BreakStatement statement) {
    }

    @Override
    public void visit(ContinueStatement statement) {
    }

    @Override
    public void visit(ReturnStatement statement) {
        if (statement.getResult() != null) {
            statement.getResult().acceptVisitor(this);
        }
    }

    @Override
    public void visit(ThrowStatement statement) {
        statement.getException().acceptVisitor(this);
    }

    @Override
    public void visit(InitClassStatement statement) {
    }

    @Override
    public void visit(TryCatchStatement statement) {
        visitList(statement.getProtectedBody());
        visitList(statement.getHandler());
    }

    @Override
    public void visit(GotoPartStatement statement) {
    }

    @Override
    public void visit(MonitorEnterStatement statement) {
        statement.getObjectRef().acceptVisitor(this);
    }

    @Override
    public void visit(MonitorExitStatement statement) {
        statement.getObjectRef().acceptVisitor(this);
    }

    @Override
    public void visit(BinaryExpr expr) {
        expr.getFirstOperand().acceptVisitor(this);
        expr.getSecondOperand().acceptVisitor(this);
    }

    @Override
    public void visit(UnaryExpr expr) {
        expr.getOperand().acceptVisitor(this);
    }

    @Override
    public void visit(ConditionalExpr expr) {
        expr.getCondition().acceptVisitor(this);
        expr.getConsequent().acceptVisitor(this);
        expr.getAlternative().acceptVisitor(this);
    }

    @Override
    public void visit(ConstantExpr expr) {
    }

    @Override
    public void visit(VariableExpr expr) {
    }

    @Override
    public void visit(SubscriptExpr expr) {
        expr.getArray().acceptVisitor(this);
        expr.getIndex().acceptVisitor(this);
    }

    @Override
    public void visit(UnwrapArrayExpr expr) {
        expr.getArray().acceptVisitor(this);
    }

    @Override
    public void visit(InvocationExpr expr) {
        allCalls.add(expr.getMethod());
        for (Expr arg : expr.getArguments()) {
            arg.acceptVisitor(this);
        }
    }

    @Override
    public void visit(QualificationExpr expr) {
        expr.getQualified().acceptVisitor(this);
    }

    @Override
    public void visit(NewExpr expr) {
    }

    @Override
    public void visit(NewArrayExpr expr) {
        expr.getLength().acceptVisitor(this);
    }

    @Override
    public void visit(NewMultiArrayExpr expr) {
        for (Expr dim : expr.getDimensions()) {
            dim.acceptVisitor(this);
        }
    }

    @Override
    public void visit(InstanceOfExpr expr) {
        expr.getExpr().acceptVisitor(this);
    }

    @Override
    public void visit(StaticClassExpr expr) {
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy