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 String _fname; |
| 16 | // Type of the field |
| 17 | public Type _t; |
| 18 | // Unique memory alias, not sensibly part of a "type" but very convenient here. |
| 19 | public int _alias; |
| 20 | // Field must be written to exactly once, no more, no less |
| 21 | public boolean _final; |
| 22 | // Field is final in the declaration; value is the same for all instances |
| 23 | // and will be moved to the class object |
| 24 | public boolean _one; |
| 25 | |
| 26 | private static final Ary<Field> FREE = new Ary<>(Field.class); |
| 27 | private Field(String fname, Type type, int alias, boolean xfinal, boolean one ) { super(TFLD); init(fname,type,alias,xfinal,one); } |
| 28 | private Field init(String fname, Type type, int alias, boolean xfinal, boolean one ) { |
| 29 | _fname = fname; |
| 30 | _t = type; |
| 31 | _alias = alias; |
| 32 | _final = xfinal; |
| 33 | _one = one; |
| 34 | return this; |
| 35 | } |
| 36 | |
| 37 | // Return a filled-in Field; either from free list or alloc new. |
| 38 | private static Field malloc(String fname, Type type, int alias, boolean xfinal, boolean one ) { |
| 39 | return FREE.isEmpty() ? new Field(fname,type,alias,xfinal,one) : FREE.pop().init(fname,type,alias,xfinal,one); |
| 40 | } |
| 41 | // Malloc-from |
| 42 | public Field malloc( ) { return malloc(_fname,null,_alias,_final,_one); } |
| 43 | @Override Field free(Type t) { |
| 44 | Field f = (Field)t; |
| 45 | assert !f.isFree() && !f._terned; |
| 46 | f._t = null; |
| 47 | f._dual = null; |
| 48 | f._hash = 0; |
| 49 | FREE.push(f); |
| 50 | return this; |
| 51 | } |
| 52 | @Override boolean isFree() { return _t ==null; } |
| 53 | |
| 54 | // Make and intern with listed fields |
| 55 | public static Field make( String fname, Type type, int alias, boolean xfinal, boolean one ) { |
| 56 | Field f = malloc(fname,type,alias,xfinal,one); |
| 57 | Field f2 = f.intern(); |
| 58 | if( f2==f ) return f; |
| 59 | return VISIT.isEmpty() ? f2.free(f) : f2.delayFree(f); |
| 60 | } |
| 61 | public Field makeFrom( Type type ) { |
| 62 | return type == _t ? this : make(_fname,type,_alias,_final,_one); |
| 63 | } |
| 64 | public Field makeFrom( boolean xfinal ) { |
| 65 | return xfinal == _final ? this : make(_fname,_t,_alias,xfinal,_one); |
| 66 | } |
| 67 |