169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal/*
269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * Javassist, a Java-bytecode translator toolkit.
369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * Copyright (C) 1999-2007 Shigeru Chiba. All Rights Reserved.
469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal *
569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * The contents of this file are subject to the Mozilla Public License Version
669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * 1.1 (the "License"); you may not use this file except in compliance with
769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * the License.  Alternatively, the contents of this file may be used under
869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * the terms of the GNU Lesser General Public License Version 2.1 or later.
969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal *
1069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * Software distributed under the License is distributed on an "AS IS" basis,
1169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
1269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * for the specific language governing rights and limitations under the
1369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * License.
1469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal */
1569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
1669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalpackage javassist.compiler.ast;
1769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
1869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalimport javassist.compiler.TokenId;
1969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalimport javassist.compiler.CompileError;
2069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
2169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal/**
2269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * Expression.
2369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal */
2469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalpublic class Expr extends ASTList implements TokenId {
2569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /* operator must be either of:
2669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * (unary) +, (unary) -, ++, --, !, ~,
2769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * ARRAY, . (dot), MEMBER (static member access).
2869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Otherwise, the object should be an instance of a subclass.
2969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
3069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
3169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    protected int operatorId;
3269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
3369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    Expr(int op, ASTree _head, ASTList _tail) {
3469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        super(_head, _tail);
3569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        operatorId = op;
3669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
3769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
3869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    Expr(int op, ASTree _head) {
3969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        super(_head);
4069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        operatorId = op;
4169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
4269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
4369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public static Expr make(int op, ASTree oprand1, ASTree oprand2) {
4469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        return new Expr(op, oprand1, new ASTList(oprand2));
4569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
4669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
4769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public static Expr make(int op, ASTree oprand1) {
4869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        return new Expr(op, oprand1);
4969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
5069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
5169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public int getOperator() { return operatorId; }
5269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
5369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public void setOperator(int op) { operatorId = op; }
5469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
5569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public ASTree oprand1() { return getLeft(); }
5669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
5769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public void setOprand1(ASTree expr) {
5869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        setLeft(expr);
5969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
6069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
6169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public ASTree oprand2() { return getRight().getLeft(); }
6269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
6369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public void setOprand2(ASTree expr) {
6469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        getRight().setLeft(expr);
6569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
6669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
6769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public void accept(Visitor v) throws CompileError { v.atExpr(this); }
6869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
6969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public String getName() {
7069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        int id = operatorId;
7169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        if (id < 128)
7269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            return String.valueOf((char)id);
7369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        else if (NEQ <= id && id <= ARSHIFT_E)
7469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            return opNames[id - NEQ];
7569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        else if (id == INSTANCEOF)
7669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            return "instanceof";
7769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        else
7869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            return String.valueOf(id);
7969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
8069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
8169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    protected String getTag() {
8269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        return "op:" + getName();
8369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
8469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal}
85