@@ -820,6 +820,82 @@ else if (passigns instanceof SequenceStatement)
820820 return st ;
821821 } // Other cases, for all other forms of statement.
822822
823+ public static Statement replaceLocalDeclarations (Statement st , Vector vars )
824+ { // replace each var v : T := e statement in st by v := e
825+ // if v : vars
826+
827+ if (st == null )
828+ { return st ; }
829+
830+ if (st instanceof SequenceStatement )
831+ { SequenceStatement sq = (SequenceStatement ) st ;
832+ Vector stats = sq .getStatements ();
833+
834+ Vector res = new Vector ();
835+ for (int i = 0 ; i < stats .size (); i ++)
836+ { if (stats .get (i ) instanceof Statement )
837+ { Statement stat = (Statement ) stats .get (i );
838+ res .add (
839+ Statement .replaceLocalDeclarations (stat ,vars ));
840+ }
841+ }
842+ return new SequenceStatement (res );
843+ }
844+
845+ if (st instanceof CreationStatement )
846+ { CreationStatement cs = (CreationStatement ) st ;
847+ String lhs = cs .assignsTo ;
848+ if (vars .contains (lhs ) &&
849+ cs .initialExpression != null )
850+ { Expression lhsexpr = new BasicExpression (lhs );
851+ lhsexpr .type = cs .getType ();
852+ lhsexpr .elementType = cs .getElementType ();
853+ AssignStatement newst =
854+ new AssignStatement (lhsexpr ,
855+ cs .initialExpression );
856+ return newst ;
857+ }
858+ return st ;
859+ }
860+
861+ if (st instanceof ConditionalStatement )
862+ { ConditionalStatement cs = (ConditionalStatement ) st ;
863+ Statement newif =
864+ Statement .replaceLocalDeclarations (cs .ifPart (), vars );
865+ Statement newelse =
866+ Statement .replaceLocalDeclarations (cs .elsePart (), vars );
867+ ConditionalStatement res =
868+ new ConditionalStatement (cs .test , newif , newelse );
869+ return res ;
870+ }
871+
872+ if (st instanceof WhileStatement )
873+ { WhileStatement ws = (WhileStatement ) st ;
874+ Statement newbody =
875+ Statement .replaceLocalDeclarations (
876+ ws .getLoopBody (),vars );
877+ WhileStatement res =
878+ new WhileStatement (ws .getLoopTest (),newbody );
879+ res .loopKind = ws .loopKind ;
880+ return res ;
881+ }
882+
883+ /* if (st instanceof TryStatement)
884+ { TryStatement ts = (TryStatement) st;
885+ res.addAll(getLocalDeclarations(ts.getBody()));
886+ Vector stats = ts.getClauses();
887+ for (int i = 0; i < stats.size(); i++)
888+ { if (stats.get(i) instanceof Statement)
889+ { Statement stat = (Statement) stats.get(i);
890+ res.addAll(getLocalDeclarations(stat));
891+ }
892+ }
893+ res.addAll(getLocalDeclarations(ts.getEndStatement()));
894+ } */
895+
896+ return st ;
897+ } // Other cases, for all other forms of statement.
898+
823899 public static Statement unfoldCall (
824900 Statement stat , String nme , Statement defn )
825901 { // self.nme() replaced by defn in stat.
0 commit comments