01: /*
02: * Copyright 2007 Google Inc.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
05: * use this file except in compliance with the License. You may obtain a copy of
06: * the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13: * License for the specific language governing permissions and limitations under
14: * the License.
15: */
16: package com.google.gwt.dev.jjs.ast;
17:
18: import com.google.gwt.dev.jjs.SourceInfo;
19:
20: import java.util.List;
21:
22: /**
23: * New array expression.
24: */
25: public class JNewArray extends JExpression implements HasSettableType {
26:
27: public List<JExpression> dims = null;
28: public List<JExpression> initializers = null;
29: private JArrayType arrayType;
30:
31: public JNewArray(JProgram program, SourceInfo info,
32: JArrayType arrayType) {
33: super (program, info);
34: this .arrayType = arrayType;
35: }
36:
37: public JArrayType getArrayType() {
38: return arrayType;
39: }
40:
41: public JType getType() {
42: return arrayType;
43: }
44:
45: public boolean hasSideEffects() {
46: if (initializers != null) {
47: for (int i = 0, c = initializers.size(); i < c; ++i) {
48: if (initializers.get(i).hasSideEffects()) {
49: return true;
50: }
51: }
52: }
53: if (dims != null) {
54: for (int i = 0, c = dims.size(); i < c; ++i) {
55: if (dims.get(i).hasSideEffects()) {
56: return true;
57: }
58: }
59: }
60: // The new operation on an array does not actually cause side effects.
61: return false;
62: }
63:
64: public void setType(JType arrayType) {
65: this .arrayType = (JArrayType) arrayType;
66: }
67:
68: public void traverse(JVisitor visitor, Context ctx) {
69: if (visitor.visit(this , ctx)) {
70: assert ((dims != null) ^ (initializers != null));
71:
72: if (dims != null) {
73: visitor.accept(dims);
74:
75: // Visit all the class literals that will eventually get generated.
76: JArrayType it = arrayType;
77: for (JExpression dim : dims) {
78: if (dim instanceof JAbsentArrayDimension) {
79: break;
80: }
81: visitor.accept(program.getLiteralClass(it));
82: if (it.getElementType() instanceof JArrayType) {
83: it = (JArrayType) it.getElementType();
84: } else {
85: break;
86: }
87: }
88: }
89:
90: if (initializers != null) {
91: visitor.accept(initializers);
92: // Visit the class literals that will eventually get generated.
93: visitor.accept(program.getLiteralClass(arrayType));
94: }
95: }
96: visitor.endVisit(this, ctx);
97: }
98: }
|