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

pascal.taie.analysis.graph.flowgraph.NodeManager Maven / Gradle / Ivy

The newest version!
/*
 * Tai-e: A Static Analysis Framework for Java
 *
 * Copyright (C) 2022 Tian Tan 
 * Copyright (C) 2022 Yue Li 
 *
 * This file is part of Tai-e.
 *
 * Tai-e is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation, either version 3
 * of the License, or (at your option) any later version.
 *
 * Tai-e is distributed in the hope that it will be useful,but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
 * Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with Tai-e. If not, see .
 */

package pascal.taie.analysis.graph.flowgraph;

import pascal.taie.analysis.pta.core.heap.Obj;
import pascal.taie.ir.exp.Var;
import pascal.taie.language.classes.JField;
import pascal.taie.util.Indexer;
import pascal.taie.util.collection.Maps;
import pascal.taie.util.collection.TwoKeyMap;
import pascal.taie.util.collection.Views;

import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

public abstract class NodeManager implements Indexer {

    private int nodeCounter;

    private final List nodes = new ArrayList<>(4096);

    private final Map var2Node = Maps.newMap(4096);

    private final TwoKeyMap iField2Node = Maps.newTwoKeyMap();

    private final Map array2Node = Maps.newMap(1024);

    private final Map sField2Node = Maps.newMap(1024);

    @Nullable
    public VarNode getVarNode(Var var) {
        return var2Node.get(var);
    }

    protected VarNode getOrCreateVarNode(Var var) {
        return var2Node.computeIfAbsent(var, v -> {
            VarNode node = new VarNode(v, nodeCounter++);
            nodes.add(node);
            return node;
        });
    }

    @Nullable
    public InstanceFieldNode getInstanceFieldNode(Obj base, JField field) {
        return iField2Node.get(base, field);
    }

    protected InstanceFieldNode getOrCreateInstanceFieldNode(Obj base, JField field) {
        return iField2Node.computeIfAbsent(base, field, (o, f) -> {
            InstanceFieldNode node = new InstanceFieldNode(o, f, nodeCounter++);
            nodes.add(node);
            return node;
        });
    }

    @Nullable
    public ArrayIndexNode getArrayIndexNode(Obj array) {
        return array2Node.get(array);
    }

    protected ArrayIndexNode getOrCreateArrayIndexNode(Obj array) {
        return array2Node.computeIfAbsent(array, a -> {
            ArrayIndexNode node = new ArrayIndexNode(a, nodeCounter++);
            nodes.add(node);
            return node;
        });
    }

    protected StaticFieldNode getOrCreateStaticFieldNode(JField field) {
        return sField2Node.computeIfAbsent(field, f -> {
            StaticFieldNode node = new StaticFieldNode(f, nodeCounter++);
            nodes.add(node);
            return node;
        });
    }

    public boolean hasNode(Node node) {
        return nodes.contains(node);
    }

    public Set getNodes() {
        return Views.toMappedSet(nodes, node -> node,
                o -> o instanceof Node node && hasNode(node));
    }

    @Override
    public int getIndex(Node node) {
        return node.getIndex();
    }

    @Override
    public Node getObject(int index) {
        return nodes.get(index);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy