conditionalexpr.java

来自「JDK1.4编译器后端」· Java 代码 · 共 476 行 · 第 1/2 页

JAVA
476
字号

package AST;
import java.util.HashSet;import java.util.LinkedHashSet;import java.io.FileNotFoundException;import java.io.File;import java.util.*;import beaver.*;import java.util.ArrayList;import java.util.zip.*;import java.io.*;public class ConditionalExpr extends Expr implements Cloneable {
    public void flushCache() {        super.flushCache();        constant_computed = false;        constant_value = null;        isConstant_computed = false;        booleanOperator_computed = false;        type_computed = false;        type_value = null;        else_branch_label_computed = false;        then_branch_label_computed = false;    }     @SuppressWarnings({"unchecked", "cast"})  public ConditionalExpr clone() throws CloneNotSupportedException {        ConditionalExpr node = (ConditionalExpr)super.clone();        node.constant_computed = false;        node.constant_value = null;        node.isConstant_computed = false;        node.booleanOperator_computed = false;        node.type_computed = false;        node.type_value = null;        node.else_branch_label_computed = false;        node.then_branch_label_computed = false;        node.in$Circle(false);        node.is$Final(false);    return node;    }     @SuppressWarnings({"unchecked", "cast"})  public ConditionalExpr copy() {      try {          ConditionalExpr node = (ConditionalExpr)clone();          if(children != null) node.children = (ASTNode[])children.clone();          return node;      } catch (CloneNotSupportedException e) {      }      System.err.println("Error: Could not clone node of type " + getClass().getName() + "!");      return null;    }     @SuppressWarnings({"unchecked", "cast"})  public ConditionalExpr fullCopy() {        ConditionalExpr res = (ConditionalExpr)copy();        for(int i = 0; i < getNumChildNoTransform(); i++) {          ASTNode node = getChildNoTransform(i);          if(node != null) node = node.fullCopy();          res.setChild(node, i);        }        return res;    }    // Declared in PrettyPrint.jadd at line 443  public void toString(StringBuffer s) {    getCondition().toString(s);    s.append(" ? ");    getTrueExpr().toString(s);    s.append(" : ");    getFalseExpr().toString(s);  }    // Declared in TypeCheck.jrag at line 562  // 15.25  public void typeCheck() {    if(!getCondition().type().isBoolean())      error("*** First expression must be a boolean in conditional operator");    if(type().isUnknown() && !getTrueExpr().type().isUnknown() && !getFalseExpr().type().isUnknown()) {      error("*** Operands in conditional operator does not match");     }  }    // Declared in CreateBCode.jrag at line 1022  public void createBCode(CodeGeneration gen) {    if(type().isBoolean())      emitBooleanCondition(gen);    else {      int endBranch = hostType().constantPool().newLabel();      getCondition().emitEvalBranch(gen);      if(getCondition().canBeTrue()) {        gen.addLabel(then_branch_label());        getTrueExpr().createBCode(gen);        getTrueExpr().type().emitCastTo(gen, type());        if(getCondition().canBeFalse()) {          gen.changeStackDepth(-type().variableSize());          gen.emitGoto(endBranch);        }      }      if(getCondition().canBeFalse()) {        gen.addLabel(else_branch_label());        getFalseExpr().createBCode(gen);        getFalseExpr().type().emitCastTo(gen, type());      }      gen.addLabel(endBranch);    }  }    // Declared in CreateBCode.jrag at line 1088    public void emitEvalBranch(CodeGeneration gen) {    int endBranch = hostType().constantPool().newLabel();    getCondition().emitEvalBranch(gen);    gen.addLabel(then_branch_label());    if(getCondition().canBeTrue()) {      getTrueExpr().emitEvalBranch(gen);      gen.emitGoto(true_label());    }      gen.addLabel(else_branch_label());    if(getCondition().canBeFalse()) {      getFalseExpr().emitEvalBranch(gen);      gen.emitGoto(true_label());    }  }    // Declared in java.ast at line 3    // Declared in java.ast line 191
    public ConditionalExpr() {        super();

    }    // Declared in java.ast at line 10
    // Declared in java.ast line 191    public ConditionalExpr(Expr p0, Expr p1, Expr p2) {        setChild(p0, 0);        setChild(p1, 1);        setChild(p2, 2);    }    // Declared in java.ast at line 16  protected int numChildren() {
    return 3;
  }    // Declared in java.ast at line 19
  public boolean mayHaveRewrite() { return false; }    // Declared in java.ast at line 2    // Declared in java.ast line 191    public void setCondition(Expr node) {        setChild(node, 0);    }    // Declared in java.ast at line 5    public Expr getCondition() {        return (Expr)getChild(0);    }    // Declared in java.ast at line 9    public Expr getConditionNoTransform() {        return (Expr)getChildNoTransform(0);    }    // Declared in java.ast at line 2    // Declared in java.ast line 191    public void setTrueExpr(Expr node) {        setChild(node, 1);    }    // Declared in java.ast at line 5    public Expr getTrueExpr() {        return (Expr)getChild(1);    }    // Declared in java.ast at line 9    public Expr getTrueExprNoTransform() {        return (Expr)getChildNoTransform(1);    }    // Declared in java.ast at line 2    // Declared in java.ast line 191    public void setFalseExpr(Expr node) {        setChild(node, 2);    }    // Declared in java.ast at line 5    public Expr getFalseExpr() {        return (Expr)getChild(2);    }    // Declared in java.ast at line 9    public Expr getFalseExprNoTransform() {        return (Expr)getChildNoTransform(2);    }    protected boolean constant_computed = false;    protected Constant constant_value;    // Declared in ConstantExpression.jrag at line 132 @SuppressWarnings({"unchecked", "cast"})     public Constant constant() {        if(constant_computed)            return constant_value;        int num = boundariesCrossed;        boolean isFinal = this.is$Final();        constant_value = constant_compute();        if(isFinal && num == boundariesCrossed)            constant_computed = true;        return constant_value;    }    private Constant constant_compute() {  return type().questionColon(getCondition().constant(), getTrueExpr().constant(),getFalseExpr().constant());  }    protected boolean isConstant_computed = false;    protected boolean isConstant_value;    // Declared in ConstantExpression.jrag at line 477 @SuppressWarnings({"unchecked", "cast"})     public boolean isConstant() {        if(isConstant_computed)            return isConstant_value;        int num = boundariesCrossed;        boolean isFinal = this.is$Final();        isConstant_value = isConstant_compute();        if(isFinal && num == boundariesCrossed)            isConstant_computed = true;        return isConstant_value;    }    private boolean isConstant_compute() {  return getCondition().isConstant() && getTrueExpr().isConstant() && getFalseExpr().isConstant();  }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?