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: /**
21: * Conditional expression.
22: */
23: public class JConditional extends JExpression {
24:
25: private JExpression elseExpr;
26: private JExpression ifTest;
27: private JExpression thenExpr;
28: private final JType type;
29:
30: public JConditional(JProgram program, SourceInfo info, JType type,
31: JExpression ifTest, JExpression thenExpr,
32: JExpression elseExpr) {
33: super (program, info);
34: this .type = type;
35: this .ifTest = ifTest;
36: this .thenExpr = thenExpr;
37: this .elseExpr = elseExpr;
38: }
39:
40: public JExpression getElseExpr() {
41: return elseExpr;
42: }
43:
44: public JExpression getIfTest() {
45: return ifTest;
46: }
47:
48: public JExpression getThenExpr() {
49: return thenExpr;
50: }
51:
52: public JType getType() {
53: // TODO(later): allow multiple types for Type Flow?
54: if (type instanceof JReferenceType) {
55: return program.generalizeTypes((JReferenceType) thenExpr
56: .getType(), (JReferenceType) elseExpr.getType());
57: } else {
58: return type;
59: }
60: }
61:
62: public boolean hasSideEffects() {
63: return ifTest.hasSideEffects() || thenExpr.hasSideEffects()
64: || elseExpr.hasSideEffects();
65: }
66:
67: public void traverse(JVisitor visitor, Context ctx) {
68: if (visitor.visit(this, ctx)) {
69: ifTest = visitor.accept(ifTest);
70: thenExpr = visitor.accept(thenExpr);
71: elseExpr = visitor.accept(elseExpr);
72: }
73: visitor.endVisit(this, ctx);
74: }
75:
76: }
|