KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > compiler > base > ast > BinopExpr


1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  *
3  * This file is part of the source and core tools for the AspectJ(tm)
4  * programming language; see http://aspectj.org
5  *
6  * The contents of this file are subject to the Mozilla Public License
7  * Version 1.1 (the "License"); you may not use this file except in
8  * compliance with the License. You may obtain a copy of the License at
9  * either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/.
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is AspectJ.
17  *
18  * The Initial Developer of the Original Code is Xerox Corporation. Portions
19  * created by Xerox Corporation are Copyright (C) 1999-2002 Xerox Corporation.
20  * All Rights Reserved.
21  *
22  * Contributor(s):
23  */

24
25 package org.aspectj.compiler.base.ast;
26
27 import org.aspectj.compiler.base.*;
28 import org.aspectj.compiler.base.cst.*;
29
30 import java.util.*;
31
32 import org.aspectj.compiler.base.bcg.CodeBuilder;
33 import org.aspectj.compiler.base.bcg.Label;
34
35 /**
36  * @grammar rand1 <op> rand2
37  * @child Expr rand1
38  * @property String op
39  * @child Expr rand2
40  */

41 public abstract class BinopExpr extends Expr {
42     /** Only called from {@link AssignExpr}, overrided in {@link
43         PlusOpExpr}, since += is the only operator with weird typing
44         behaviour. */

45     void checkAssignmentOperatorType() {
46         discoverType();
47     }
48
49 // // ------------------------------
50
// // INTRO from FlowCheckerPass
51

52 // public void walkFlow(FlowCheckerPass w) {
53
// super.walkFlow(w);
54
// // this needs to be _MUCH_ faster
55
// w.setVars(w.getVars().getTrue().join(w.getVars().getFalse()));
56
// }
57

58     // ------------------------------
59
// INTRO from AssignmentCheckerPass (defined in AspectJCompiler)
60

61     public ASTObject postAssignmentCheck(AssignmentCheckerPass walker) {
62         return tryToFold();
63     }
64
65     private ASTObject tryToFold() {
66         if (getLiftType().isAnyType()) return this;
67         if (rand1 instanceof LiteralExpr && rand2 instanceof LiteralExpr) {
68             try {
69                 return
70                     halfFold(getLiftType(), (LiteralExpr)rand1, (LiteralExpr)rand2)
71                     .setSource(this);
72             } catch (ArithmeticException JavaDoc e) {
73                 //this.showError(e.getMessage());
74
return this;
75             }
76         } else {
77             return this;
78         }
79     }
80
81     abstract protected Type getLiftType();
82
83     /** returns a new unfinished ASTObject, that still needs to have
84         {@link ASTObject#setSource} called on it. */

85     abstract protected LiteralExpr halfFold(Type type, LiteralExpr lit1, LiteralExpr lit2);
86
87     public static BinopExpr build(SourceLocation source, String JavaDoc op,
88                                   Expr rand1, Expr rand2) {
89         op = op.intern();
90         if (op == "+") {
91             return new AddOpExpr(source, rand1, op, rand2);
92         } else if (op == "&&") {
93             return new AndAndOpExpr(source, rand1, op, rand2);
94         } else if (op == "||") {
95             return new OrOrOpExpr(source, rand1, op, rand2);
96         } else if (op == "*" || op == "/" || op == "%" || op == "-") {
97             return new NumericOpExpr(source, rand1, op, rand2);
98         } else if (op == "&" || op == "|" || op == "^") {
99             return new BitwiseOpExpr(source, rand1, op, rand2);
100         } else if (op == "<<" || op == ">>" || op == ">>>") {
101             return new ShiftOpExpr(source, rand1, op, rand2);
102         } else if (op == "<" || op == "<=" || op == ">=" || op == ">") {
103             return new NumericTestOpExpr(source, rand1, op, rand2);
104         } else if (op == "==" || op == "!=") {
105             return new EqualityTestOpExpr(source, rand1, op, rand2);
106         } else {
107             throw new RuntimeException JavaDoc("bad op " + op);
108         }
109     }
110
111     public void unparse(CodeWriter writer) {
112         writer.write(rand1);
113         writer.writeOp(op);
114         writer.write(rand2);
115     }
116
117     // ------------------------------
118
// bcg
119
/** Generate code that takes the top two values on the stack and
120         replaces it with the one value that is appropriate. This is
121         only used by non-effect operators, and is sometimes called
122         from {@link AssignExpr}. */

123     abstract protected void cgOp(CodeBuilder cb, Type ty);
124
125     // XXX this turns off effect optimization of + just for the bad case of
126
// ("foo" + new Blah()).staticField
127
// there must be a better way to do this.
128
protected void cgEffect(CodeBuilder cb) {
129         if (op == "/" || this instanceof ConditionalOpExpr || op == "+") {
130             super.cgEffect(cb);
131         } else {
132             getRand1().cgEffect(cb);
133             getRand2().cgEffect(cb);
134         }
135     }
136
137     //BEGIN: Generated from @child and @property
138
protected Expr rand1;
139     public Expr getRand1() { return rand1; }
140     public void setRand1(Expr _rand1) {
141         if (_rand1 != null) _rand1.setParent(this);
142         rand1 = _rand1;
143     }
144
145     protected String JavaDoc op;
146     public String JavaDoc getOp() { return op; }
147     public void setOp(String JavaDoc _op) { op = _op; }
148
149     protected Expr rand2;
150     public Expr getRand2() { return rand2; }
151     public void setRand2(Expr _rand2) {
152         if (_rand2 != null) _rand2.setParent(this);
153         rand2 = _rand2;
154     }
155
156     public BinopExpr(SourceLocation location, Expr _rand1, String JavaDoc _op, Expr _rand2) {
157         super(location);
158         setRand1(_rand1);
159         setOp(_op);
160         setRand2(_rand2);
161     }
162     protected BinopExpr(SourceLocation source) {
163         super(source);
164     }
165
166     public ASTObject getChildAt(int childIndex) {
167         switch(childIndex) {
168         case 0: return rand1;
169         case 1: return rand2;
170         default: return super.getChildAt(childIndex);
171         }
172     }
173      public String JavaDoc getChildNameAt(int childIndex) {
174         switch(childIndex) {
175         case 0: return "rand1";
176         case 1: return "rand2";
177         default: return super.getChildNameAt(childIndex);
178         }
179     }
180      public void setChildAt(int childIndex, ASTObject child) {
181         switch(childIndex) {
182         case 0: setRand1((Expr)child); return;
183         case 1: setRand2((Expr)child); return;
184         default: super.setChildAt(childIndex, child); return;
185         }
186     }
187      public int getChildCount() {
188         return 2;
189     }
190
191     public String JavaDoc getDefaultDisplayName() {
192         return "BinopExpr(op: "+op+")";
193     }
194
195     //END: Generated from @child and @property
196
}
197
Popular Tags