Main Page   Packages   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members  

PushComplement.java

00001 package edu.ksu.cis.bandera.jjjc.optimizer;
00002 
00003 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
00004  * Bandera, a Java(TM) analysis and transformation toolkit           *
00005  * Copyright (C) 1999, 2000   Robby (robby@cis.ksu.edu)              *
00006  * All rights reserved.                                              *
00007  *                                                                   *
00008  * This work was done as a project in the SAnToS Laboratory,         *
00009  * Department of Computing and Information Sciences, Kansas State    *
00010  * University, USA (http://www.cis.ksu.edu/santos).                  *
00011  * It is understood that any modification not identified as such is  *
00012  * not covered by the preceding statement.                           *
00013  *                                                                   *
00014  * This work is free software; you can redistribute it and/or        *
00015  * modify it under the terms of the GNU Library General Public       *
00016  * License as published by the Free Software Foundation; either      *
00017  * version 2 of the License, or (at your option) any later version.  *
00018  *                                                                   *
00019  * This work is distributed in the hope that it will be useful,      *
00020  * but WITHOUT ANY WARRANTY; without even the implied warranty of    *
00021  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU *
00022  * Library General Public License for more details.                  *
00023  *                                                                   *
00024  * You should have received a copy of the GNU Library General Public *
00025  * License along with this toolkit; if not, write to the             *
00026  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,      *
00027  * Boston, MA  02111-1307, USA.                                      *
00028  *                                                                   *
00029  * Java is a trademark of Sun Microsystems, Inc.                     *
00030  *                                                                   *
00031  * To submit a bug report, send a comment, or get the latest news on *
00032  * this project and other SAnToS projects, please visit the web-site *
00033  *                http://www.cis.ksu.edu/santos                      *
00034  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00035 import java.io.*;
00036 import java.util.*;
00037 
00038 import edu.ksu.cis.bandera.jjjc.node.*;
00039 import edu.ksu.cis.bandera.jjjc.lexer.*;
00040 import edu.ksu.cis.bandera.jjjc.parser.*;
00041 import edu.ksu.cis.bandera.jjjc.analysis.*;
00042 import edu.ksu.cis.bandera.jjjc.unicodepreprocessor.*;
00043 
00044 import ca.mcgill.sable.laleh.java.astfix.*;
00045 
00046 public final class PushComplement extends DepthFirstAdapter {
00047     private boolean foundComplement;
00048     private static PushComplement p = new PushComplement();
00049 /**
00050  * 
00051  */
00052 private PushComplement() {
00053 }
00054 /**
00055  * 
00056  * @param node edu.ksu.cis.bandera.jjjc.node.AArrayAccessExp
00057  */
00058 public void caseAArrayAccessExp(AArrayAccessExp node) {
00059     if (foundComplement) {
00060         node.replaceBy(new AUnaryExp(new AComplementUnaryOperator(new TComplement()), (PExp) node.clone()));
00061     }
00062 }
00063 /**
00064  * 
00065  * @param node edu.ksu.cis.bandera.jjjc.node.AAssignmentExp
00066  */
00067 public void caseAAssignmentExp(AAssignmentExp node) {
00068     /*
00069     boolean prevFoundComplement = foundComplement;
00070     foundComplement = false;
00071     */
00072     node.getExp().apply(this);      
00073     /*
00074     foundComplement = prevFoundComplement;
00075     if (foundComplement) {
00076         node.replaceBy(new AUnaryExp(new AComplementUnaryOperator(new TComplement()), (PExp) node.clone()));
00077     }
00078     */
00079 }
00080 /**
00081  * 
00082  * @param node edu.ksu.cis.bandera.jjjc.node.ABinaryExp
00083  */
00084 public void caseABinaryExp(ABinaryExp node) {
00085     PBinaryOperator op = node.getBinaryOperator();
00086     boolean prevFoundComplement = foundComplement;
00087     if (foundComplement) {
00088         if (op instanceof AOrBinaryOperator) {
00089             node.setBinaryOperator(new AAndBinaryOperator(new TAnd()));
00090         } else if (op instanceof AAndBinaryOperator) {
00091             node.setBinaryOperator(new AOrBinaryOperator(new TOr()));
00092         } else if (op instanceof AGtBinaryOperator) {
00093             node.setBinaryOperator(new ALteqBinaryOperator(new TLteq()));
00094             foundComplement = false;
00095         } else if (op instanceof ALtBinaryOperator) {
00096             node.setBinaryOperator(new AGteqBinaryOperator(new TGteq()));
00097             foundComplement = false;
00098         } else if (op instanceof ALteqBinaryOperator) {
00099             node.setBinaryOperator(new AGtBinaryOperator(new TGt()));
00100             foundComplement = false;
00101         } else if (op instanceof AGteqBinaryOperator) {
00102             node.setBinaryOperator(new ALtBinaryOperator(new TLt()));
00103             foundComplement = false;
00104         } else if (op instanceof AEqBinaryOperator) {
00105             node.setBinaryOperator(new ANeqBinaryOperator(new TNeq()));
00106             foundComplement = false;
00107         } else if (op instanceof ANeqBinaryOperator) {
00108             node.setBinaryOperator(new AEqBinaryOperator(new TEq()));
00109             foundComplement = false;
00110         }
00111     }
00112     node.getFirst().apply(this);
00113     node.getSecond().apply(this);
00114     foundComplement = prevFoundComplement;
00115 }
00116 /**
00117  * 
00118  * @param node edu.ksu.cis.bandera.jjjc.node.AFalseBooleanLiteral
00119  */
00120 public void caseAFalseBooleanLiteral(AFalseBooleanLiteral node) {
00121     if (foundComplement)
00122         node.replaceBy(new ATrueBooleanLiteral(new TTrue()));
00123 }
00124 /**
00125  * 
00126  * @param node edu.ksu.cis.bandera.jjjc.node.AFieldAccessExp
00127  */
00128 public void caseAFieldAccessExp(AFieldAccessExp node) {
00129     if (foundComplement) {
00130         node.replaceBy(new AUnaryExp(new AComplementUnaryOperator(new TComplement()), (PExp) node.clone()));
00131     }
00132 }
00133 /**
00134  * 
00135  * @param node edu.ksu.cis.bandera.jjjc.node.AInstanceofExp
00136  */
00137 public void caseAInstanceofExp(AInstanceofExp node) {
00138     if (foundComplement) {
00139         node.replaceBy(new AUnaryExp(new AComplementUnaryOperator(new TComplement()), (PExp) node.clone()));
00140     }
00141 }
00142 /**
00143  * 
00144  * @param node edu.ksu.cis.bandera.jjjc.node.ANameExp
00145  */
00146 public void caseANameExp(ANameExp node) {
00147     if (foundComplement) {
00148         node.replaceBy(new AUnaryExp(new AComplementUnaryOperator(new TComplement()), (PExp) node.clone()));
00149     }
00150 }
00151 /**
00152  * 
00153  * @param node edu.ksu.cis.bandera.jjjc.node.ANameMethodInvocationExp
00154  */
00155 public void caseANameMethodInvocationExp(ANameMethodInvocationExp node) {
00156     boolean prevFoundComplement = foundComplement;
00157     foundComplement = false;
00158         
00159     {
00160         Object temp[] = node.getArgumentList().toArray();
00161         for (int i = 0; i < temp.length; i++) {
00162             ((PExp) temp[i]).apply(this);
00163         }
00164     }
00165 
00166     foundComplement = prevFoundComplement;
00167 
00168     if (foundComplement) {
00169         node.replaceBy(new AUnaryExp(new AComplementUnaryOperator(new TComplement()), (PExp) node.clone()));
00170     }
00171 }
00172 /**
00173  * 
00174  * @param node edu.ksu.cis.bandera.jjjc.node.APrimaryMethodInvocationExp
00175  */
00176 public void caseAPrimaryMethodInvocationExp(APrimaryMethodInvocationExp node) {
00177     boolean prevFoundComplement = foundComplement;
00178     foundComplement = false;
00179         
00180     {
00181         Object temp[] = node.getArgumentList().toArray();
00182         for (int i = 0; i < temp.length; i++) {
00183             ((PExp) temp[i]).apply(this);
00184         }
00185     }
00186 
00187     foundComplement = prevFoundComplement;
00188 
00189     if (foundComplement) {
00190         node.replaceBy(new AUnaryExp(new AComplementUnaryOperator(new TComplement()), (PExp) node.clone()));
00191     }
00192 }
00193 /**
00194  * 
00195  * @param node edu.ksu.cis.bandera.jjjc.node.AQuestionExp
00196  */
00197 public void caseAQuestionExp(AQuestionExp node) {
00198     boolean prevFoundComplement = foundComplement;
00199     
00200     foundComplement = false;
00201     node.getSecond().apply(this);
00202 
00203     foundComplement = false;
00204     node.getThird().apply(this);
00205         
00206     foundComplement = prevFoundComplement;
00207     node.getFirst().apply(this);
00208 }
00209 /**
00210  * 
00211  * @param node edu.ksu.cis.bandera.jjjc.node.ASuperMethodInvocationExp
00212  */
00213 public void caseASuperMethodInvocationExp(ASuperMethodInvocationExp node) {
00214     boolean prevFoundComplement = foundComplement;
00215     foundComplement = false;
00216         
00217     {
00218         Object temp[] = node.getArgumentList().toArray();
00219         for (int i = 0; i < temp.length; i++) {
00220             ((PExp) temp[i]).apply(this);
00221         }
00222     }
00223 
00224     foundComplement = prevFoundComplement;
00225 
00226     if (foundComplement) {
00227         node.replaceBy(new AUnaryExp(new AComplementUnaryOperator(new TComplement()), (PExp) node.clone()));
00228     }
00229 }
00230 /**
00231  * 
00232  * @param node edu.ksu.cis.bandera.jjjc.node.ATrueBooleanLiteral
00233  */
00234 public void caseATrueBooleanLiteral(ATrueBooleanLiteral node) {
00235     if (foundComplement)
00236         node.replaceBy(new AFalseBooleanLiteral(new TFalse()));
00237 }
00238 /**
00239  * 
00240  * @param node edu.ksu.cis.bandera.jjjc.node.AUnaryExp
00241  */
00242 public void caseAUnaryExp(AUnaryExp node) {
00243     PExp exp = node.getExp();
00244     if ("!".equals(node.getUnaryOperator().toString().trim())) {
00245         foundComplement = !foundComplement;
00246         node.replaceBy(exp);
00247         exp.apply(this);
00248         foundComplement = !foundComplement;
00249     } else {
00250         exp.apply(this);
00251     }
00252 }
00253 /**
00254  * 
00255  */
00256 private void init() {
00257     foundComplement = false;
00258 }
00259 /**
00260  * 
00261  * @param exp edu.ksu.cis.bandera.jjjc.node.Node
00262  */
00263 public static void push(Node exp) {
00264     p.init();
00265     exp.apply(p);
00266 }
00267 }

Generated at Thu Feb 7 06:53:08 2002 for Bandera by doxygen1.2.10 written by Dimitri van Heesch, © 1997-2001