001: /* Soot - a J*va Optimization Framework
002: * Copyright (C) 1997-1999 Raja Vallee-Rai
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the
016: * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
017: * Boston, MA 02111-1307, USA.
018: */
019:
020: /*
021: * Modified by the Sable Research Group and others 1997-1999.
022: * See the 'credits' file distributed with Soot for the complete list of
023: * contributors. (Soot is distributed at http://www.sable.mcgill.ca/soot)
024: */
025:
026: package soot.jimple;
027:
028: import soot.*;
029: import java.util.*;
030:
031: /** Implementation of the Body class for the Jimple IR. */
032: public class JimpleBody extends StmtBody {
033: /**
034: Construct an empty JimpleBody
035: **/
036:
037: JimpleBody(SootMethod m) {
038: super (m);
039: }
040:
041: /**
042: Construct an extremely empty JimpleBody, for parsing into.
043: **/
044:
045: JimpleBody() {
046: }
047:
048: /** Clones the current body, making deep copies of the contents. */
049: public Object clone() {
050: Body b = new JimpleBody(getMethod());
051: b.importBodyContentsFrom(this );
052: return b;
053: }
054:
055: /** Make sure that the JimpleBody is well formed. If not, throw
056: * an exception. Right now, performs only a handful of checks.
057: */
058: public void validate() {
059: super .validate();
060:
061: // Check validity of traps.
062: /* this check may not hold when jop.bcm is enabled.
063: * disabled by Feng Qian, May 2002
064: */
065: /*
066: {
067: Iterator it = getTraps().iterator();
068:
069: while(it.hasNext())
070: {
071: Trap t = (Trap) it.next();
072:
073: Stmt s = (Stmt) t.getHandlerUnit();
074:
075: if(!(s instanceof IdentityStmt)
076: || !(((IdentityStmt) s).getRightOp() instanceof CaughtExceptionRef)){
077: G.v().out.println(s);
078: throw new RuntimeException("Trap handler is not of the form x := caughtexceptionref");
079: }
080: }
081: }
082: */
083: }
084:
085: /** Inserts usual statements for handling this & parameters into body. */
086: public void insertIdentityStmts() {
087: int i = 0;
088:
089: if (!getMethod().isStatic()) {
090: Local l = Jimple.v().newLocal("this",
091: RefType.v(getMethod().getDeclaringClass()));
092: getLocals().add(l);
093: getUnits()
094: .addFirst(
095: Jimple.v().newIdentityStmt(
096: l,
097: Jimple.v().newThisRef(
098: (RefType) l.getType())));
099: }
100:
101: Iterator parIt = getMethod().getParameterTypes().iterator();
102: while (parIt.hasNext()) {
103: Type t = (Type) parIt.next();
104: Local l = Jimple.v().newLocal("parameter" + i, t);
105: getLocals().add(l);
106: getUnits()
107: .addFirst(
108: Jimple.v().newIdentityStmt(
109: l,
110: Jimple.v().newParameterRef(
111: l.getType(), i)));
112: i++;
113: }
114: }
115:
116: /** Returns the first non-identity stmt in this body. */
117: public Stmt getFirstNonIdentityStmt() {
118: Iterator it = getUnits().iterator();
119: Object o = null;
120: while (it.hasNext())
121: if (!((o = it.next()) instanceof IdentityStmt))
122: break;
123: if (o == null)
124: throw new RuntimeException("no non-id statements!");
125: return (Stmt) o;
126: }
127: }
|