1 /*
2  * reserved comment block
3  * DO NOT REMOVE OR ALTER!
4  */
5 /*
6  * Copyright 2001-2004 The Apache Software Foundation.
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *     http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 /*
21  * $Id: BinOpExpr.java,v 1.2.4.1 2005/09/01 11:42:27 pvedula Exp $
22  */
23 
24 package com.sun.org.apache.xalan.internal.xsltc.compiler;
25 
26 import com.sun.org.apache.bcel.internal.generic.InstructionList;
27 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ClassGenerator;
28 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg;
29 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator;
30 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodType;
31 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type;
32 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.TypeCheckError;
33 
34 /**
35  * @author Jacek Ambroziak
36  * @author Santiago Pericas-Geertsen
37  */
38 final class BinOpExpr extends Expression {
39     public static final int PLUS  = 0;
40     public static final int MINUS = 1;
41     public static final int TIMES = 2;
42     public static final int DIV   = 3;
43     public static final int MOD   = 4;
44 
45     private static final String[] Ops = {
46         "+", "-", "*", "/", "%"
47     };
48 
49     private int _op;
50     private Expression _left, _right;
51 
BinOpExpr(int op, Expression left, Expression right)52     public BinOpExpr(int op, Expression left, Expression right) {
53         _op = op;
54         (_left = left).setParent(this);
55         (_right = right).setParent(this);
56     }
57 
58     /**
59      * Returns true if this expressions contains a call to position(). This is
60      * needed for context changes in node steps containing multiple predicates.
61      */
hasPositionCall()62     public boolean hasPositionCall() {
63         if (_left.hasPositionCall()) return true;
64         if (_right.hasPositionCall()) return true;
65         return false;
66     }
67 
68     /**
69      * Returns true if this expressions contains a call to last()
70      */
hasLastCall()71     public boolean hasLastCall() {
72             return (_left.hasLastCall() || _right.hasLastCall());
73     }
74 
setParser(Parser parser)75     public void setParser(Parser parser) {
76         super.setParser(parser);
77         _left.setParser(parser);
78         _right.setParser(parser);
79     }
80 
typeCheck(SymbolTable stable)81     public Type typeCheck(SymbolTable stable) throws TypeCheckError {
82         final Type tleft = _left.typeCheck(stable);
83         final Type tright = _right.typeCheck(stable);
84         final MethodType ptype = lookupPrimop(stable, Ops[_op],
85                                               new MethodType(Type.Void,
86                                                              tleft, tright));
87         if (ptype != null) {
88             final Type arg1 = (Type) ptype.argsType().elementAt(0);
89             if (!arg1.identicalTo(tleft)) {
90                 _left = new CastExpr(_left, arg1);
91             }
92             final Type arg2 = (Type) ptype.argsType().elementAt(1);
93             if (!arg2.identicalTo(tright)) {
94                 _right = new CastExpr(_right, arg1);
95             }
96             return _type = ptype.resultType();
97         }
98         throw new TypeCheckError(this);
99     }
100 
translate(ClassGenerator classGen, MethodGenerator methodGen)101     public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
102         final InstructionList il = methodGen.getInstructionList();
103 
104         _left.translate(classGen, methodGen);
105         _right.translate(classGen, methodGen);
106 
107         switch (_op) {
108         case PLUS:
109             il.append(_type.ADD());
110             break;
111         case MINUS:
112             il.append(_type.SUB());
113             break;
114         case TIMES:
115             il.append(_type.MUL());
116             break;
117         case DIV:
118             il.append(_type.DIV());
119             break;
120         case MOD:
121             il.append(_type.REM());
122             break;
123         default:
124             ErrorMsg msg = new ErrorMsg(ErrorMsg.ILLEGAL_BINARY_OP_ERR, this);
125             getParser().reportError(Constants.ERROR, msg);
126         }
127     }
128 
toString()129     public String toString() {
130         return Ops[_op] + '(' + _left + ", " + _right + ')';
131     }
132 }
133