Represents a field in a struct. This is not a Type in the type system.
| 8 | * Represents a field in a struct. This is not a Type in the type system. |
| 9 | */ |
| 10 | public class Field extends Type { |
| 11 | |
| 12 | // The pair {fieldName,type} uniquely identifies a field. |
| 13 | |
| 14 | // Field name |
| 15 | public final String _fname; |
| 16 | // Type of the field |
| 17 | public final Type _type; |
| 18 | // Unique memory alias, not sensibly part of a "type" but very convenient here. |
| 19 | public final int _alias; |
| 20 | // Field must be written to exactly once, no more, no less |
| 21 | public final boolean _final; |
| 22 | |
| 23 | private Field(String fname, Type type, int alias, boolean xfinal ) { |
| 24 | super(TFLD); |
| 25 | _fname = fname; |
| 26 | _type = type; |
| 27 | _alias = alias; |
| 28 | _final = xfinal; |
| 29 | } |
| 30 | // Make with existing alias |
| 31 | public static Field make( String fname, Type type, int alias, boolean xfinal ) { |
| 32 | return new Field(fname,type,alias,xfinal).intern(); |
| 33 | } |
| 34 | public Field makeFrom( Type type ) { |
| 35 | return type == _type ? this : new Field(_fname,type,_alias,_final).intern(); |
| 36 | } |
| 37 | @Override public Field makeRO() { return isFinal() ? this : make(_fname,_type.makeRO(),_alias,true); } |
| 38 | @Override public boolean isFinal() { return _final && _type.isFinal(); } |
| 39 | |
| 40 | public static final Field TEST = make("test",Type.NIL,-2,false); |
| 41 | public static final Field TEST2= make("test",Type.NIL,-2,true); |
| 42 | public static void gather(ArrayList<Type> ts) { ts.add(TEST); ts.add(TEST2); } |
| 43 | |
| 44 | @Override Field xmeet( Type that ) { |
| 45 | Field fld = (Field)that; // Invariant |
| 46 | assert _fname.equals(fld._fname); |
| 47 | assert _alias==fld._alias; |
| 48 | return make(_fname,_type.meet(fld._type),_alias,_final | fld._final); |
| 49 | } |
| 50 | |
| 51 | @Override |
| 52 | public Field dual() { return make(_fname,_type.dual(),_alias,!_final); } |
| 53 | |
| 54 | @Override public Field glb() { |
| 55 | Type glb = _type.glb(); |
| 56 | return (glb==_type && _final) ? this : make(_fname,glb,_alias,true); |
| 57 | } |
| 58 | |
| 59 | // Override in subclasses |
| 60 | int hash() { return _fname.hashCode() ^ _type.hashCode() ^ _alias ^ (_final ? 1024 : 0); } |
| 61 | |
| 62 | boolean eq(Type t) { |
| 63 | Field f = (Field)t; |
| 64 | return _fname.equals(f._fname) && _type==f._type && _alias==f._alias && _final==f._final; |
| 65 | } |
| 66 | |
| 67 |