KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > dev > jjs > ast > JConditional


1 /*
2  * Copyright 2007 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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, JExpression elseExpr) {
32     super(program, info);
33     this.type = type;
34     this.ifTest = ifTest;
35     this.thenExpr = thenExpr;
36     this.elseExpr = elseExpr;
37   }
38
39   public JExpression getElseExpr() {
40     return elseExpr;
41   }
42
43   public JExpression getIfTest() {
44     return ifTest;
45   }
46
47   public JExpression getThenExpr() {
48     return thenExpr;
49   }
50
51   public JType getType() {
52     // TODO(later): allow multiple types for Type Flow?
53
if (type instanceof JReferenceType) {
54       return program.generalizeTypes((JReferenceType) thenExpr.getType(),
55           (JReferenceType) elseExpr.getType());
56     } else {
57       return type;
58     }
59   }
60
61   public boolean hasSideEffects() {
62     return ifTest.hasSideEffects() || thenExpr.hasSideEffects()
63         || elseExpr.hasSideEffects();
64   }
65
66   public void traverse(JVisitor visitor, Context ctx) {
67     if (visitor.visit(this, ctx)) {
68       ifTest = visitor.accept(ifTest);
69       thenExpr = visitor.accept(thenExpr);
70       elseExpr = visitor.accept(elseExpr);
71     }
72     visitor.endVisit(this, ctx);
73   }
74
75 }
76
Popular Tags