KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > polyglot > ext > jl > ast > ArrayAccess_c


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 /**
11  * An <code>ArrayAccess</code> is an immutable representation of an
12  * access of an array member.
13  */

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     /** Get the precedence of the expression. */
26     public Precedence precedence() {
27     return Precedence.LITERAL;
28     }
29
30     /** Get the array of the expression. */
31     public Expr array() {
32     return this.array;
33     }
34
35     /** Set the array of the expression. */
36     public ArrayAccess array(Expr array) {
37     ArrayAccess_c n = (ArrayAccess_c) copy();
38     n.array = array;
39     return n;
40     }
41
42     /** Get the index of the expression. */
43     public Expr index() {
44     return this.index;
45     }
46
47     /** Set the index of the expression. */
48     public ArrayAccess index(Expr index) {
49     ArrayAccess_c n = (ArrayAccess_c) copy();
50     n.index = index;
51     return n;
52     }
53
54     /** Return the access flags of the variable. */
55     public Flags flags() {
56         return Flags.NONE;
57     }
58
59     /** Reconstruct the expression. */
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     /** Visit the children of the expression. */
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     /** Type check the expression. */
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 JavaDoc toString() {
110     return array + "[" + index + "]";
111     }
112
113     /** Write the expression to an output file. */
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