001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 *  Unless required by applicable law or agreed to in writing, software
012 *  distributed under the License is distributed on an "AS IS" BASIS,
013 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 *  See the License for the specific language governing permissions and
015 *  limitations under the License.
016 *
017 */
018package org.apache.bcel.verifier.statics;
019
020
021import org.apache.bcel.generic.Type;
022import org.apache.bcel.verifier.exc.AssertionViolatedException;
023import org.apache.bcel.verifier.exc.LocalVariableInfoInconsistentException;
024
025/**
026 * A utility class holding the information about
027 * the names and the types of the local variables in
028 * a given method.
029 */
030public class LocalVariablesInfo{
031
032    /** The information about the local variables is stored here. */
033    private final LocalVariableInfo[] localVariableInfos;
034
035    /** The constructor. */
036    LocalVariablesInfo(final int max_locals) {
037        localVariableInfos = new LocalVariableInfo[max_locals];
038        for (int i=0; i<max_locals; i++) {
039            localVariableInfos[i] = new LocalVariableInfo();
040        }
041    }
042
043    /**
044     * Returns the LocalVariableInfo for the given slot.
045     *
046     * @param slot Slot to query.
047     * @return The LocalVariableInfo for the given slot.
048     */
049    public LocalVariableInfo getLocalVariableInfo(final int slot) {
050        if (slot < 0 || slot >= localVariableInfos.length) {
051            throw new AssertionViolatedException("Slot number for local variable information out of range.");
052        }
053        return localVariableInfos[slot];
054    }
055
056    /**
057     * Adds information about the local variable in slot 'slot'. Automatically
058     * adds information for slot+1 if 't' is Type.LONG or Type.DOUBLE.
059     *
060     * @param name variable name
061     * @param startPc Range in which the variable is valid.
062     * @param length length of ...
063     * @param type variable type
064     * @throws LocalVariableInfoInconsistentException if the new information conflicts
065     *         with already gathered information.
066     */
067    public void add(final int slot, final String name, final int startPc, final int length, final Type type) throws LocalVariableInfoInconsistentException{
068        // The add operation on LocalVariableInfo may throw the '...Inconsistent...' exception, we don't throw it explicitely here.
069
070        if (slot < 0 || slot >= localVariableInfos.length) {
071            throw new AssertionViolatedException("Slot number for local variable information out of range.");
072        }
073
074        localVariableInfos[slot].add(name, startPc, length, type);
075        if (type == Type.LONG) {
076            localVariableInfos[slot+1].add(name, startPc, length, LONG_Upper.theInstance());
077        }
078        if (type == Type.DOUBLE) {
079            localVariableInfos[slot+1].add(name, startPc, length, DOUBLE_Upper.theInstance());
080        }
081    }
082}