KickJava   Java API By Example, From Geeks To Geeks.

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


1 package polyglot.ext.jl.ast;
2
3 import polyglot.ast.*;
4 import polyglot.types.*;
5 import polyglot.visit.*;
6 import polyglot.util.*;
7 import java.util.*;
8
9 /**
10  * A <code>NewArray</code> represents a new array expression such as <code>new
11  * File[8][] { null }</code>. It consists of an element type (e.g.,
12  * <code>File</code>), a list of dimension expressions (e.g., 8), 0 or more
13  * additional dimensions (e.g., 1 for []), and an array initializer. The
14  * dimensions of the array initializer must equal the number of additional "[]"
15  * dimensions.
16  */

17 public class NewArray_c extends Expr_c implements NewArray
18 {
19     protected TypeNode baseType;
20     protected List dims;
21     protected int addDims;
22     protected ArrayInit init;
23
24     public NewArray_c(Position pos, TypeNode baseType, List dims, int addDims, ArrayInit init) {
25     super(pos);
26     this.baseType = baseType;
27     this.dims = TypedList.copyAndCheck(dims, Expr.class, true);
28     this.addDims = addDims;
29     this.init = init;
30     }
31
32     /** Get the base type node of the expression. */
33     public TypeNode baseType() {
34     return this.baseType;
35     }
36
37     /** Set the base type node of the expression. */
38     public NewArray baseType(TypeNode baseType) {
39     NewArray_c n = (NewArray_c) copy();
40     n.baseType = baseType;
41     return n;
42     }
43
44     /** Get the dimension expressions of the expression. */
45     public List dims() {
46     return Collections.unmodifiableList(this.dims);
47     }
48
49     /** Set the dimension expressions of the expression. */
50     public NewArray dims(List dims) {
51     NewArray_c n = (NewArray_c) copy();
52     n.dims = TypedList.copyAndCheck(dims, Expr.class, true);
53     return n;
54     }
55
56     /** Get the number of dimensions of the expression. */
57     public int numDims() {
58         return dims.size() + addDims;
59     }
60
61     /** Get the number of additional dimensions of the expression. */
62     public int additionalDims() {
63     return this.addDims;
64     }
65
66     /** Set the number of additional dimensions of the expression. */
67     public NewArray additionalDims(int addDims) {
68     NewArray_c n = (NewArray_c) copy();
69     n.addDims = addDims;
70     return n;
71     }
72
73     /** Get the initializer of the expression. */
74     public ArrayInit init() {
75     return this.init;
76     }
77
78     /** Set the initializer of the expression. */
79     public NewArray init(ArrayInit init) {
80     NewArray_c n = (NewArray_c) copy();
81     n.init = init;
82     return n;
83     }
84
85     /** Reconstruct the expression. */
86     protected NewArray_c reconstruct(TypeNode baseType, List dims, ArrayInit init) {
87     if (baseType != this.baseType || ! CollectionUtil.equals(dims, this.dims) || init != this.init) {
88         NewArray_c n = (NewArray_c) copy();
89         n.baseType = baseType;
90         n.dims = TypedList.copyAndCheck(dims, Expr.class, true);
91         n.init = init;
92         return n;
93     }
94
95     return this;
96     }
97
98     /** Visit the children of the expression. */
99     public Node visitChildren(NodeVisitor v) {
100     TypeNode baseType = (TypeNode) visitChild(this.baseType, v);
101     List dims = visitList(this.dims, v);
102     ArrayInit init = (ArrayInit) visitChild(this.init, v);
103     return reconstruct(baseType, dims, init);
104     }
105
106     /** Type check the expression. */
107     public Node typeCheck(TypeChecker tc) throws SemanticException {
108         TypeSystem ts = tc.typeSystem();
109
110         for (Iterator i = dims.iterator(); i.hasNext(); ) {
111             Expr expr = (Expr) i.next();
112             if (! ts.isImplicitCastValid(expr.type(), ts.Int())) {
113                 throw new SemanticException("Array dimension must be an integer.",
114                         expr.position());
115             }
116         }
117
118         ArrayType type = ts.arrayOf(baseType.type(), dims.size() + addDims);
119
120     if (init != null) {
121             init.typeCheckElements(type);
122     }
123
124     return type(type);
125     }
126
127     public Type childExpectedType(Expr child, AscriptionVisitor av) {
128         if (child == init) {
129             return this.type;
130         }
131
132         return child.type();
133     }
134
135     public String JavaDoc toString() {
136     return "new " + baseType + "[...]";
137     }
138
139     /** Write the expression to an output file. */
140     public void prettyPrint(CodeWriter w, PrettyPrinter tr) {
141     w.write("new ");
142     print(baseType, w, tr);
143
144     for (Iterator i = dims.iterator(); i.hasNext();) {
145       Expr e = (Expr) i.next();
146       w.write("[");
147       printBlock(e, w, tr);
148       w.write("]");
149     }
150
151     for (int i = 0; i < addDims; i++) {
152         w.write("[]");
153     }
154
155     if (init != null) {
156         w.write(" ");
157         print(init, w, tr);
158     }
159     }
160
161     public Term entry() {
162         return listEntry(dims, (init != null ? init.entry() : this));
163     }
164
165     public List acceptCFG(CFGBuilder v, List succs) {
166         v.visitCFGList(dims, (init != null ? init.entry() : this));
167         if (init != null) {
168             v.visitCFG(init, this);
169         }
170         return succs;
171     }
172 }
173
Popular Tags