Skip to content

Commit f95d803

Browse files
authored
declaration hoisting
1 parent d6cc181 commit f95d803

File tree

2 files changed

+126
-0
lines changed

2 files changed

+126
-0
lines changed

BehaviouralFeature.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11136,6 +11136,56 @@ else if (selfcalls == 2)
1113611136

1113711137
return act;
1113811138
}
11139+
11140+
public void hoistLocalDeclarations()
11141+
{ // Find all the local declarations.
11142+
// Give warnings if
11143+
// (i) multiple declarations with same variable name
11144+
// (ii) declaration with same name as a parameter
11145+
// Apart from case (ii) replace declarations by assigns
11146+
// in the code and put all declarations at the start
11147+
11148+
if (activity == null)
11149+
{ return; }
11150+
11151+
Vector localdecs =
11152+
Statement.getLocalDeclarations(activity);
11153+
11154+
Vector initialDecs = new Vector();
11155+
Vector varnames = new Vector();
11156+
11157+
for (int i = 0; i < localdecs.size(); i++)
11158+
{ CreationStatement cs = (CreationStatement) localdecs.get(i);
11159+
String vname = cs.assignsTo;
11160+
11161+
ModelElement par =
11162+
ModelElement.lookupByName(vname, parameters);
11163+
11164+
if (par != null)
11165+
{ System.err.println("!! Warning: " + vname + " is both a parameter and local variable!");
11166+
continue;
11167+
}
11168+
11169+
if (varnames.contains(vname))
11170+
{ System.err.println("!! Warning: multiple declarations for variable " + vname);
11171+
continue;
11172+
}
11173+
11174+
varnames.add(vname);
11175+
initialDecs.add(cs);
11176+
}
11177+
11178+
if (varnames.size() == 0)
11179+
{ return; }
11180+
11181+
Statement newactivity =
11182+
Statement.replaceLocalDeclarations(activity,varnames);
11183+
SequenceStatement ss = new SequenceStatement(initialDecs);
11184+
ss.addStatement(newactivity);
11185+
11186+
System.out.println(">>> New activity: " + ss);
11187+
activity = ss;
11188+
}
1113911189
}
1114011190

1114111191

Statement.java

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)