286N/A/*
286N/A * reserved comment block
286N/A * DO NOT REMOVE OR ALTER!
286N/A */
286N/A/*
286N/A * Copyright 2001-2004 The Apache Software Foundation.
286N/A *
286N/A * Licensed under the Apache License, Version 2.0 (the "License");
286N/A * you may not use this file except in compliance with the License.
286N/A * You may obtain a copy of the License at
286N/A *
286N/A * http://www.apache.org/licenses/LICENSE-2.0
286N/A *
286N/A * Unless required by applicable law or agreed to in writing, software
286N/A * distributed under the License is distributed on an "AS IS" BASIS,
286N/A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
286N/A * See the License for the specific language governing permissions and
286N/A * limitations under the License.
286N/A */
286N/A/*
286N/A * $Id: BinOpExpr.java,v 1.2.4.1 2005/09/01 11:42:27 pvedula Exp $
286N/A */
286N/A
286N/Apackage com.sun.org.apache.xalan.internal.xsltc.compiler;
286N/A
286N/Aimport com.sun.org.apache.bcel.internal.generic.InstructionList;
286N/Aimport com.sun.org.apache.xalan.internal.xsltc.compiler.util.ClassGenerator;
286N/Aimport com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg;
286N/Aimport com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator;
286N/Aimport com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodType;
286N/Aimport com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type;
286N/Aimport com.sun.org.apache.xalan.internal.xsltc.compiler.util.TypeCheckError;
286N/A
286N/A/**
286N/A * @author Jacek Ambroziak
286N/A * @author Santiago Pericas-Geertsen
286N/A */
286N/Afinal class BinOpExpr extends Expression {
286N/A public static final int PLUS = 0;
286N/A public static final int MINUS = 1;
286N/A public static final int TIMES = 2;
286N/A public static final int DIV = 3;
286N/A public static final int MOD = 4;
286N/A
286N/A private static final String[] Ops = {
286N/A "+", "-", "*", "/", "%"
286N/A };
286N/A
286N/A private int _op;
286N/A private Expression _left, _right;
286N/A
286N/A public BinOpExpr(int op, Expression left, Expression right) {
286N/A _op = op;
286N/A (_left = left).setParent(this);
286N/A (_right = right).setParent(this);
286N/A }
286N/A
286N/A /**
286N/A * Returns true if this expressions contains a call to position(). This is
286N/A * needed for context changes in node steps containing multiple predicates.
286N/A */
286N/A public boolean hasPositionCall() {
286N/A if (_left.hasPositionCall()) return true;
286N/A if (_right.hasPositionCall()) return true;
286N/A return false;
286N/A }
286N/A
286N/A /**
286N/A * Returns true if this expressions contains a call to last()
286N/A */
286N/A public boolean hasLastCall() {
286N/A return (_left.hasLastCall() || _right.hasLastCall());
286N/A }
286N/A
286N/A public void setParser(Parser parser) {
286N/A super.setParser(parser);
286N/A _left.setParser(parser);
286N/A _right.setParser(parser);
286N/A }
286N/A
286N/A public Type typeCheck(SymbolTable stable) throws TypeCheckError {
286N/A final Type tleft = _left.typeCheck(stable);
286N/A final Type tright = _right.typeCheck(stable);
286N/A final MethodType ptype = lookupPrimop(stable, Ops[_op],
286N/A new MethodType(Type.Void,
286N/A tleft, tright));
286N/A if (ptype != null) {
286N/A final Type arg1 = (Type) ptype.argsType().elementAt(0);
286N/A if (!arg1.identicalTo(tleft)) {
286N/A _left = new CastExpr(_left, arg1);
286N/A }
286N/A final Type arg2 = (Type) ptype.argsType().elementAt(1);
286N/A if (!arg2.identicalTo(tright)) {
286N/A _right = new CastExpr(_right, arg1);
286N/A }
286N/A return _type = ptype.resultType();
286N/A }
286N/A throw new TypeCheckError(this);
286N/A }
286N/A
286N/A public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
286N/A final InstructionList il = methodGen.getInstructionList();
286N/A
286N/A _left.translate(classGen, methodGen);
286N/A _right.translate(classGen, methodGen);
286N/A
286N/A switch (_op) {
286N/A case PLUS:
286N/A il.append(_type.ADD());
286N/A break;
286N/A case MINUS:
286N/A il.append(_type.SUB());
286N/A break;
286N/A case TIMES:
286N/A il.append(_type.MUL());
286N/A break;
286N/A case DIV:
286N/A il.append(_type.DIV());
286N/A break;
286N/A case MOD:
286N/A il.append(_type.REM());
286N/A break;
286N/A default:
286N/A ErrorMsg msg = new ErrorMsg(ErrorMsg.ILLEGAL_BINARY_OP_ERR, this);
286N/A getParser().reportError(Constants.ERROR, msg);
286N/A }
286N/A }
286N/A
286N/A public String toString() {
286N/A return Ops[_op] + '(' + _left + ", " + _right + ')';
286N/A }
286N/A}