package net.sourceforge.phpdt.internal.compiler.ast; import java.util.List; import java.util.ArrayList; /** * A For statement. * for(initializations;condition;increments) action * @author Matthieu Casanova */ public class ForStatement extends Statement { /** the initializations. */ public Expression[] initializations; /** the condition. */ public Expression condition; /** the increments. */ public Expression[] increments; public Statement action; public ForStatement(final Expression[] initializations, final Expression condition, final Expression[] increments, final Statement action, final int sourceStart, final int sourceEnd) { super(sourceStart, sourceEnd); this.initializations = initializations; this.condition = condition; this.increments = increments; this.action = action; } public String toString(final int tab) { final StringBuffer buff = new StringBuffer(tabString(tab)); buff.append("for ("); //$NON-NLS-1$ //inits if (initializations != null) { for (int i = 0; i < initializations.length; i++) { buff.append(initializations[i].toStringExpression()); if (i != (initializations.length - 1)) buff.append(" , "); //$NON-NLS-1$ } } buff.append("; "); //$NON-NLS-1$ //cond if (condition != null) { buff.append(condition.toStringExpression()); } buff.append("; "); //$NON-NLS-1$ //updates if (increments != null) { for (int i = 0; i < increments.length; i++) { //nice only with expressions buff.append(increments[i].toStringExpression()); if (i != (increments.length - 1)) buff.append(" , "); //$NON-NLS-1$ } } buff.append(") "); //$NON-NLS-1$ //block if (action == null) buff.append("{}"); //$NON-NLS-1$ else buff.append(action.toString(tab + 1)); //$NON-NLS-1$ return buff.toString(); } /** * Get the variables from outside (parameters, globals ...) * @return the variables from outside */ public List getOutsideVariable() { final ArrayList list = new ArrayList(); list.addAll(condition.getOutsideVariable()); list.addAll(action.getOutsideVariable()); for (int i = 0; i < initializations.length; i++) { list.addAll(initializations[i].getOutsideVariable()); } for (int i = 0; i < increments.length; i++) { list.addAll(increments[i].getOutsideVariable()); } return list; } /** * get the modified variables. * @return the variables from we change value */ public List getModifiedVariable() { final ArrayList list = new ArrayList(); list.addAll(condition.getModifiedVariable()); list.addAll(action.getModifiedVariable()); for (int i = 0; i < initializations.length; i++) { list.addAll(initializations[i].getModifiedVariable()); } for (int i = 0; i < increments.length; i++) { list.addAll(increments[i].getModifiedVariable()); } return list; } /** * Get the variables used. * @return the variables used */ public List getUsedVariable() { final ArrayList list = new ArrayList(); list.addAll(condition.getUsedVariable()); list.addAll(action.getUsedVariable()); for (int i = 0; i < initializations.length; i++) { list.addAll(initializations[i].getUsedVariable()); } for (int i = 0; i < increments.length; i++) { list.addAll(increments[i].getUsedVariable()); } return list; } }