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: * Binary operator expression.
22: */
23: public class JBinaryOperation extends JExpression implements
24: HasSettableType {
25:
26: private JExpression lhs;
27: private final JBinaryOperator op;
28: private JExpression rhs;
29: private JType type;
30:
31: public JBinaryOperation(JProgram program, SourceInfo info,
32: JType type, JBinaryOperator op, JExpression lhs,
33: JExpression rhs) {
34: super (program, info);
35: this .op = op;
36: this .type = type;
37: this .lhs = lhs;
38: this .rhs = rhs;
39: }
40:
41: public JExpression getLhs() {
42: return lhs;
43: }
44:
45: public JBinaryOperator getOp() {
46: return op;
47: }
48:
49: public JExpression getRhs() {
50: return rhs;
51: }
52:
53: public JType getType() {
54: if (op == JBinaryOperator.ASG) {
55: // Use rhs because (generality lhs >= generality rhs)
56: return getRhs().getType();
57: } else if (isAssignment()) {
58: // Use lhs because this is really a write-then-read
59: return getLhs().getType();
60: } else {
61: // Most binary operators never change type
62: return type;
63: }
64: }
65:
66: public boolean hasSideEffects() {
67: return op.isAssignment() || getLhs().hasSideEffects()
68: || getRhs().hasSideEffects();
69: }
70:
71: public boolean isAssignment() {
72: return op.isAssignment();
73: }
74:
75: public void setType(JType newType) {
76: type = newType;
77: }
78:
79: public void traverse(JVisitor visitor, Context ctx) {
80: if (visitor.visit(this, ctx)) {
81: lhs = visitor.accept(lhs);
82: rhs = visitor.accept(rhs);
83: }
84: visitor.endVisit(this, ctx);
85: }
86:
87: }
|