1 package polyglot.ext.jl.ast; 2 3 import polyglot.ast.*; 4 5 import polyglot.util.*; 6 import polyglot.types.*; 7 import polyglot.visit.*; 8 import java.util.*; 9 10 14 public class ArrayAccess_c extends Expr_c implements ArrayAccess 15 { 16 protected Expr array; 17 protected Expr index; 18 19 public ArrayAccess_c(Position pos, Expr array, Expr index) { 20 super(pos); 21 this.array = array; 22 this.index = index; 23 } 24 25 26 public Precedence precedence() { 27 return Precedence.LITERAL; 28 } 29 30 31 public Expr array() { 32 return this.array; 33 } 34 35 36 public ArrayAccess array(Expr array) { 37 ArrayAccess_c n = (ArrayAccess_c) copy(); 38 n.array = array; 39 return n; 40 } 41 42 43 public Expr index() { 44 return this.index; 45 } 46 47 48 public ArrayAccess index(Expr index) { 49 ArrayAccess_c n = (ArrayAccess_c) copy(); 50 n.index = index; 51 return n; 52 } 53 54 55 public Flags flags() { 56 return Flags.NONE; 57 } 58 59 60 protected ArrayAccess_c reconstruct(Expr array, Expr index) { 61 if (array != this.array || index != this.index) { 62 ArrayAccess_c n = (ArrayAccess_c) copy(); 63 n.array = array; 64 n.index = index; 65 return n; 66 } 67 68 return this; 69 } 70 71 72 public Node visitChildren(NodeVisitor v) { 73 Expr array = (Expr) visitChild(this.array, v); 74 Expr index = (Expr) visitChild(this.index, v); 75 return reconstruct(array, index); 76 } 77 78 79 public Node typeCheck(TypeChecker tc) throws SemanticException { 80 TypeSystem ts = tc.typeSystem(); 81 82 if (! array.type().isArray()) { 83 throw new SemanticException( 84 "Subscript can only follow an array type.", position()); 85 } 86 87 if (! ts.isImplicitCastValid(index.type(), ts.Int())) { 88 throw new SemanticException( 89 "Array subscript must be an integer.", position()); 90 } 91 92 return type(array.type().toArray().base()); 93 } 94 95 public Type childExpectedType(Expr child, AscriptionVisitor av) { 96 TypeSystem ts = av.typeSystem(); 97 98 if (child == index) { 99 return ts.Int(); 100 } 101 102 if (child == array) { 103 return ts.arrayOf(this.type); 104 } 105 106 return child.type(); 107 } 108 109 public String toString() { 110 return array + "[" + index + "]"; 111 } 112 113 114 public void prettyPrint(CodeWriter w, PrettyPrinter tr) { 115 printSubExpr(array, w, tr); 116 w.write ("["); 117 printBlock(index, w, tr); 118 w.write ("]"); 119 } 120 121 public Term entry() { 122 return array.entry(); 123 } 124 125 public List acceptCFG(CFGBuilder v, List succs) { 126 v.visitCFG(array, index.entry()); 127 v.visitCFG(index, this); 128 return succs; 129 } 130 131 public List throwTypes(TypeSystem ts) { 132 return CollectionUtil.list(ts.OutOfBoundsException(), 133 ts.NullPointerException()); 134 } 135 } 136 | Popular Tags |