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

org.qbicc.pointer.ElementPointer Maven / Gradle / Ivy

There is a newer version: 0.77.0
Show newest version
package org.qbicc.pointer;

import org.qbicc.interpreter.Memory;
import org.qbicc.type.ArrayType;
import org.qbicc.type.ValueType;

/**
 * A pointer to an array element.
 */
public final class ElementPointer extends Pointer {
    private final Pointer arrayPointer;
    private final long index;

    public ElementPointer(Pointer arrayPointer, long index) {
        super(((ArrayType)arrayPointer.getType().getPointeeType()).getElementType().getPointer());
        this.arrayPointer = arrayPointer;
        this.index = index;
    }

    public Pointer getArrayPointer() {
        return arrayPointer;
    }

    public long getIndex() {
        return index;
    }

    public ArrayType getPointerArrayType() {
        return (ArrayType) arrayPointer.getType().getPointeeType();
    }

    public ValueType getArrayElementType() {
        return getPointerArrayType().getElementType();
    }

    @Override
    public Pointer offsetByElements(long count) {
        if (count == 0) {
            return this;
        }
        return new ElementPointer(arrayPointer, index + count);
    }

    @Override
    public RootPointer getRootPointer() {
        return arrayPointer.getRootPointer();
    }

    @Override
    public long getRootByteOffset() {
        return arrayPointer.getRootByteOffset() + index * getArrayElementType().getSize();
    }

    @Override
    public Memory getRootMemoryIfExists() {
        return arrayPointer.getRootMemoryIfExists();
    }

    @Override
    public String getRootSymbolIfExists() {
        return arrayPointer.getRootSymbolIfExists();
    }

    @Override
    public StringBuilder toString(StringBuilder b) {
        return arrayPointer.toString(b).append('[').append(index).append(']');
    }

    public  R accept(final Visitor visitor, final T t) {
        return visitor.visit(t, this);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy