1 cananian 1.1.2.6 // ArrayPointer.java, created Sat Mar 27 17:05:07 1999 by duncan
  2 cananian 1.1.2.5 // Copyright (C) 1998 Duncan Bryce <duncan@lcs.mit.edu>
  3 cananian 1.1.2.5 // Licensed under the terms of the GNU GPL; see COPYING for details.
  4 duncan   1.1.2.1 package harpoon.Interpret.Tree;
  5 duncan   1.1.2.1 
  6 duncan   1.1.2.1 import harpoon.ClassFile.HClass;
  7 duncan   1.1.2.1 import harpoon.Util.Tuple;
  8 duncan   1.1.2.1 
  9 duncan   1.1.2.1 /**
 10 duncan   1.1.2.1  * The <code>ArrayPointer</code> class represents a pointer to an 
 11 duncan   1.1.2.1  * <code>ArrayRef</code> plus some offset.  This pointer can be dereferenced 
 12 duncan   1.1.2.1  * with <code>getValue()</code>, and the value at this location can be 
 13 duncan   1.1.2.1  * modified with <code>updateValue()</code>.
 14 duncan   1.1.2.1  * 
 15 duncan   1.1.2.1  * @author  Duncan Bryce <duncan@lcs.mit.edu>
 16 cananian 1.2      * @version $Id: ArrayPointer.java,v 1.2 2002/02/25 21:05:50 cananian Exp $
 17 duncan   1.1.2.1  */
 18 duncan   1.1.2.1 class ArrayPointer extends Pointer {
 19 duncan   1.1.2.1     private boolean isDerived;
 20 duncan   1.1.2.1     
 21 duncan   1.1.2.1     // Constructor used to add two ArrayPointers
 22 duncan   1.1.2.1     private ArrayPointer(ArrayPointer base, long offset) {
 23 duncan   1.1.2.1         this((ArrayRef)base.getBase(), base.getOffset() + offset, true);
 24 duncan   1.1.2.1     }
 25 duncan   1.1.2.1     
 26 duncan   1.1.2.1     // Copy constructor
 27 duncan   1.1.2.1     private ArrayPointer(ArrayRef base, long offset, boolean isDerived) {
 28 duncan   1.1.2.1         super(new Object[] { base, new Long(offset) });
 29 duncan   1.1.2.1         this.isDerived = isDerived;
 30 duncan   1.1.2.1     }
 31 duncan   1.1.2.1 
 32 duncan   1.1.2.1     /** Class constructor.  
 33 duncan   1.1.2.1      */
 34 duncan   1.1.2.1     ArrayPointer(ArrayRef base, long offset) {
 35 duncan   1.1.2.1         this(base, offset, offset==0);
 36 duncan   1.1.2.1     }
 37 duncan   1.1.2.1 
 38 duncan   1.1.2.1     /** Adds the specified parameter to this <code>ArrayPointer</code>'s
 39 duncan   1.1.2.1      *  offset */
 40 duncan   1.1.2.1     public Pointer add(long offset) {
 41 duncan   1.1.2.1         return new ArrayPointer(this, offset);
 42 duncan   1.1.2.1     }
 43 duncan   1.1.2.1 
 44 duncan   1.1.2.1     /** Returns true if <code>obj</code> is an <code>ArrayPointer</code> 
 45 duncan   1.1.2.1      *  which points to the same location as this <code>ArrayPointer</code>.
 46 duncan   1.1.2.1      */
 47 duncan   1.1.2.1     public boolean equals(Object obj) {
 48 cananian 1.1.2.3         ArrayPointer ptr;
 49 cananian 1.1.2.3         if (this==obj) return true;
 50 cananian 1.1.2.3         if (null==obj) return false;
 51 cananian 1.1.2.3         try { ptr = (ArrayPointer)obj; }
 52 cananian 1.1.2.3         catch (ClassCastException e) { return false; }
 53 cananian 1.1.2.3         return (getBase()==ptr.getBase()) &&
 54 cananian 1.1.2.3             (getOffset()==ptr.getOffset());
 55 duncan   1.1.2.1     }
 56 duncan   1.1.2.1 
 57 duncan   1.1.2.1     /** Returns an ArrayRef representing the base of this ArrayPointer */
 58 duncan   1.1.2.1     public Object getBase()   { return proj(0); }
 59 duncan   1.1.2.1 
 60 duncan   1.1.2.1     /** Returns the offset of this ArrayPointer */
 61 duncan   1.1.2.1     public long   getOffset() { return ((Long)proj(1)).longValue(); }
 62 duncan   1.1.2.1     
 63 duncan   1.1.2.1     /** Returns the value obtained by dereferencing this 
 64 duncan   1.1.2.1      *  <code>ArrayPointer</code>.  This value is in non-nataive format.
 65 duncan   1.1.2.1      */
 66 duncan   1.1.2.1     Object getValue() {
 67 duncan   1.1.2.1         return Method.toNonNativeFormat(ArrayRef.get(this));
 68 duncan   1.1.2.1     }
 69 duncan   1.1.2.4 
 70 duncan   1.1.2.4     /** Returns an integer enumeration of the kind of this Pointer.  The 
 71 duncan   1.1.2.4         enumerated values are public fields of the <code>Pointer</code> class.
 72 duncan   1.1.2.4     */
 73 duncan   1.1.2.4     public int kind() { return Pointer.ARRAY_PTR; }
 74 duncan   1.1.2.4 
 75 duncan   1.1.2.1     /** Sets the value at the memory location specified by this 
 76 duncan   1.1.2.1      *  <code>ArrayPointer</code> to the specified parameter. 
 77 duncan   1.1.2.1      *  This value should be in non-native format.
 78 duncan   1.1.2.1      */
 79 duncan   1.1.2.1     void updateValue(Object value) {
 80 duncan   1.1.2.1         HClass type = ((ArrayRef)getBase()).type.getComponentType();
 81 duncan   1.1.2.1         ArrayRef.update(this, Method.toNativeFormat(value, type));
 82 duncan   1.1.2.1     }
 83 duncan   1.1.2.1 
 84 duncan   1.1.2.1     /** Always returns false. */
 85 duncan   1.1.2.1     public boolean isConst()         { return false; }
 86 duncan   1.1.2.1 
 87 duncan   1.1.2.1     /** Returns true if this ArrayPointer is derived */
 88 duncan   1.1.2.2   public boolean isDerived()       { return isDerived; }
 89 duncan   1.1.2.1 
 90 duncan   1.1.2.1     /** Returns a human-readable representation of this 
 91 duncan   1.1.2.1      *  <code>ArrayPointer</code>
 92 duncan   1.1.2.1      */
 93 duncan   1.1.2.1     public String toString() { 
 94 duncan   1.1.2.1         StringBuffer sb = new StringBuffer("ArrayPtr: < ");
 95 duncan   1.1.2.1         sb.append(getBase().toString());
 96 duncan   1.1.2.1         sb.append(" , ");
 97 duncan   1.1.2.1         sb.append(getOffset());
 98 duncan   1.1.2.1         sb.append(" > ---> ");
 99 duncan   1.1.2.2         //sb.append(getValue());
100 duncan   1.1.2.1         return sb.toString();
101 duncan   1.1.2.1     }
102 duncan   1.1.2.1 
103 duncan   1.1.2.1 }
104 duncan   1.1.2.1 
105 duncan   1.1.2.1 
106 cananian 1.2