soot.dava.toolkits.base.AST.analysis.DepthFirstAdapter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of soot Show documentation
Show all versions of soot Show documentation
A Java Optimization Framework
package soot.dava.toolkits.base.AST.analysis;
/*-
* #%L
* Soot - a J*va Optimization Framework
* %%
* Copyright (C) 2005 Nomair A. Naeem
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 2.1 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* .
* #L%
*/
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import soot.Immediate;
import soot.Local;
import soot.SootClass;
import soot.Type;
import soot.Value;
import soot.ValueBox;
import soot.dava.internal.AST.ASTAndCondition;
import soot.dava.internal.AST.ASTBinaryCondition;
import soot.dava.internal.AST.ASTCondition;
import soot.dava.internal.AST.ASTDoWhileNode;
import soot.dava.internal.AST.ASTForLoopNode;
import soot.dava.internal.AST.ASTIfElseNode;
import soot.dava.internal.AST.ASTIfNode;
import soot.dava.internal.AST.ASTLabeledBlockNode;
import soot.dava.internal.AST.ASTMethodNode;
import soot.dava.internal.AST.ASTNode;
import soot.dava.internal.AST.ASTOrCondition;
import soot.dava.internal.AST.ASTStatementSequenceNode;
import soot.dava.internal.AST.ASTSwitchNode;
import soot.dava.internal.AST.ASTSynchronizedBlockNode;
import soot.dava.internal.AST.ASTTryNode;
import soot.dava.internal.AST.ASTUnaryCondition;
import soot.dava.internal.AST.ASTUnconditionalLoopNode;
import soot.dava.internal.AST.ASTWhileNode;
import soot.dava.internal.asg.AugmentedStmt;
import soot.dava.internal.javaRep.DInstanceFieldRef;
import soot.dava.internal.javaRep.DThisRef;
import soot.dava.internal.javaRep.DVariableDeclarationStmt;
import soot.jimple.ArrayRef;
import soot.jimple.BinopExpr;
import soot.jimple.CastExpr;
import soot.jimple.ConditionExpr;
import soot.jimple.Constant;
import soot.jimple.DefinitionStmt;
import soot.jimple.Expr;
import soot.jimple.InstanceFieldRef;
import soot.jimple.InstanceInvokeExpr;
import soot.jimple.InstanceOfExpr;
import soot.jimple.IntConstant;
import soot.jimple.InvokeExpr;
import soot.jimple.InvokeStmt;
import soot.jimple.NewArrayExpr;
import soot.jimple.NewMultiArrayExpr;
import soot.jimple.Ref;
import soot.jimple.ReturnStmt;
import soot.jimple.StaticFieldRef;
import soot.jimple.Stmt;
import soot.jimple.ThrowStmt;
import soot.jimple.UnopExpr;
/*
* CHANGE LOG: 18th MArch 2006: Need a reference to the ValueBox holding a BinOp for SimplifyExpressions
* Need to create a level of indirection i.e. instead of retrieving Values e.g. from stmts retrieve the valueBox
* and then apply on the value inside the valuebox
*/
public class DepthFirstAdapter extends AnalysisAdapter {
public boolean DEBUG = false;
boolean verbose = false;
public DepthFirstAdapter() {
}
public DepthFirstAdapter(boolean verbose) {
this.verbose = verbose;
}
public void inASTMethodNode(ASTMethodNode node) {
if (verbose) {
System.out.println("inASTMethodNode");
}
}
public void outASTMethodNode(ASTMethodNode node) {
if (verbose) {
System.out.println("outASTMethodNode");
}
}
public void caseASTMethodNode(ASTMethodNode node) {
inASTMethodNode(node);
normalRetrieving(node);
outASTMethodNode(node);
}
public void inASTSynchronizedBlockNode(ASTSynchronizedBlockNode node) {
if (verbose) {
System.out.println("inASTSynchronizedBlockNode");
}
}
public void outASTSynchronizedBlockNode(ASTSynchronizedBlockNode node) {
if (verbose) {
System.out.println("outASTSynchronizedBlockNode");
}
}
public void caseASTSynchronizedBlockNode(ASTSynchronizedBlockNode node) {
inASTSynchronizedBlockNode(node);
/*
* apply on the local on which synchronization is done MArch 18th, 2006: since getLocal returns a local always dont need
* a valuebox for this
*/
Value local = node.getLocal();
decideCaseExprOrRef(local);
/*
* apply on the body of the synch block
*/
normalRetrieving(node);
outASTSynchronizedBlockNode(node);
}
public void inASTLabeledBlockNode(ASTLabeledBlockNode node) {
if (verbose) {
System.out.println("inASTLabeledBlockNode");
}
}
public void outASTLabeledBlockNode(ASTLabeledBlockNode node) {
if (verbose) {
System.out.println("outASTLabeledBlockNode");
}
}
public void caseASTLabeledBlockNode(ASTLabeledBlockNode node) {
inASTLabeledBlockNode(node);
normalRetrieving(node);
outASTLabeledBlockNode(node);
}
public void inASTUnconditionalLoopNode(ASTUnconditionalLoopNode node) {
if (verbose) {
System.out.println("inASTUnconditionalWhileNode");
}
}
public void outASTUnconditionalLoopNode(ASTUnconditionalLoopNode node) {
if (verbose) {
System.out.println("outASTUnconditionalWhileNode");
}
}
public void caseASTUnconditionalLoopNode(ASTUnconditionalLoopNode node) {
inASTUnconditionalLoopNode(node);
normalRetrieving(node);
outASTUnconditionalLoopNode(node);
}
public void inASTSwitchNode(ASTSwitchNode node) {
if (verbose) {
System.out.println("inASTSwitchNode");
}
}
public void outASTSwitchNode(ASTSwitchNode node) {
if (verbose) {
System.out.println("outASTSwitchNode");
}
}
public void caseASTSwitchNode(ASTSwitchNode node) {
inASTSwitchNode(node);
/*
* apply on key of switchStatement
*/
/*
* March 18th 2006, added level of indirection to have access to value box Value key = node.get_Key();
* decideCaseExprOrRef(key);
*/
caseExprOrRefValueBox(node.getKeyBox());
/*
* Apply on bodies of switch cases
*/
normalRetrieving(node);
outASTSwitchNode(node);
}
public void inASTIfNode(ASTIfNode node) {
if (verbose) {
System.out.println("inASTIfNode");
}
}
public void outASTIfNode(ASTIfNode node) {
if (verbose) {
System.out.println("outASTIfNode");
}
}
public void caseASTIfNode(ASTIfNode node) {
inASTIfNode(node);
/*
* apply on the ASTCondition
*/
ASTCondition condition = node.get_Condition();
condition.apply(this);
/*
* Apply on the if body
*/
normalRetrieving(node);
outASTIfNode(node);
}
public void inASTIfElseNode(ASTIfElseNode node) {
if (verbose) {
System.out.println("inASTIfElseNode");
}
}
public void outASTIfElseNode(ASTIfElseNode node) {
if (verbose) {
System.out.println("outASTIfElseNode");
}
}
public void caseASTIfElseNode(ASTIfElseNode node) {
inASTIfElseNode(node);
/*
* apply on the ASTCondition
*/
ASTCondition condition = node.get_Condition();
condition.apply(this);
/*
* Apply on the if body followed by the else body
*/
normalRetrieving(node);
outASTIfElseNode(node);
}
public void inASTWhileNode(ASTWhileNode node) {
if (verbose) {
System.out.println("inASTWhileNode");
}
}
public void outASTWhileNode(ASTWhileNode node) {
if (verbose) {
System.out.println("outASTWhileNode");
}
}
public void caseASTWhileNode(ASTWhileNode node) {
inASTWhileNode(node);
/*
* apply on the ASTCondition
*/
ASTCondition condition = node.get_Condition();
condition.apply(this);
/*
* Apply on the while body
*/
normalRetrieving(node);
outASTWhileNode(node);
}
public void inASTForLoopNode(ASTForLoopNode node) {
if (verbose) {
System.out.println("inASTForLoopNode");
}
}
public void outASTForLoopNode(ASTForLoopNode node) {
if (verbose) {
System.out.println("outASTForLoopNode");
}
}
public void caseASTForLoopNode(ASTForLoopNode node) {
inASTForLoopNode(node);
/*
* Apply on init
*/
for (AugmentedStmt as : node.getInit()) {
Stmt s = as.get_Stmt();
if (s instanceof DefinitionStmt) {
caseDefinitionStmt((DefinitionStmt) s);
} else if (s instanceof ReturnStmt) {
caseReturnStmt((ReturnStmt) s);
} else if (s instanceof InvokeStmt) {
caseInvokeStmt((InvokeStmt) s);
} else if (s instanceof ThrowStmt) {
caseThrowStmt((ThrowStmt) s);
} else {
caseStmt(s);
}
}
/*
* apply on the ASTCondition
*/
ASTCondition condition = node.get_Condition();
condition.apply(this);
/*
* Apply on update
*/
for (AugmentedStmt as : node.getUpdate()) {
Stmt s = as.get_Stmt();
if (s instanceof DefinitionStmt) {
caseDefinitionStmt((DefinitionStmt) s);
} else if (s instanceof ReturnStmt) {
caseReturnStmt((ReturnStmt) s);
} else if (s instanceof InvokeStmt) {
caseInvokeStmt((InvokeStmt) s);
} else if (s instanceof ThrowStmt) {
caseThrowStmt((ThrowStmt) s);
} else {
caseStmt(s);
}
}
/*
* Apply on the for body
*/
normalRetrieving(node);
outASTForLoopNode(node);
}
public void inASTDoWhileNode(ASTDoWhileNode node) {
if (verbose) {
System.out.println("inASTDoWhileNode");
}
}
public void outASTDoWhileNode(ASTDoWhileNode node) {
if (verbose) {
System.out.println("outASTDoWhileNode");
}
}
public void caseASTDoWhileNode(ASTDoWhileNode node) {
inASTDoWhileNode(node);
/*
* apply on the ASTCondition
*/
ASTCondition condition = node.get_Condition();
condition.apply(this);
/*
* Apply on the while body
*/
normalRetrieving(node);
outASTDoWhileNode(node);
}
public void inASTTryNode(ASTTryNode node) {
if (verbose) {
System.out.println("inASTTryNode");
}
}
public void outASTTryNode(ASTTryNode node) {
if (verbose) {
System.out.println("outASTTryNode");
}
}
public void caseASTTryNode(ASTTryNode node) {
inASTTryNode(node);
// get try body
List