a lot of fixes
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / compiler / ast / IfStatement.java
1 package net.sourceforge.phpdt.internal.compiler.ast;
2
3 import java.util.List;
4 import java.util.ArrayList;
5
6 /**
7  * This is a if statement.
8  * if (condition)
9  *  statement
10  * (elseif statement)*
11  * else statement
12  * @author Matthieu Casanova
13  */
14 public class IfStatement extends Statement {
15
16   public Expression condition;
17   public Statement statement;
18   public ElseIf[] elseifs;
19   public Else els;
20
21   /**
22    * Create a new If statement.
23    * @param condition the condition
24    * @param statement a statement or a block of statements
25    * @param elseifs the elseifs
26    * @param els the else (or null)
27    * @param sourceStart the starting position
28    * @param sourceEnd the ending offset
29    */
30   public IfStatement(final Expression condition,
31                      final Statement statement,
32                      final ElseIf[] elseifs,
33                      final Else els,
34                      final int sourceStart,
35                      final int sourceEnd) {
36     super(sourceStart, sourceEnd);
37     this.condition = condition;
38     this.statement = statement;
39     this.elseifs = elseifs;
40     this.els = els;
41   }
42
43   /**
44    * Return the object into String.
45    * @param tab how many tabs (not used here
46    * @return a String
47    */
48   public String toString(final int tab) {
49     final StringBuffer buff = new StringBuffer(tabString(tab));
50     buff.append("if (");//$NON-NLS-1$
51     buff.append(condition.toStringExpression()).append(") ");//$NON-NLS-1$
52     if (statement != null) {
53       buff.append(statement.toString(tab + 1));
54     }
55     for (int i = 0; i < elseifs.length; i++) {
56       buff.append(elseifs[i].toString(tab + 1));
57       buff.append("\n");//$NON-NLS-1$
58     }
59     if (els != null) {
60       buff.append(els.toString(tab + 1));
61       buff.append("\n");//$NON-NLS-1$
62     }
63     return buff.toString();
64   }
65
66   /**
67    * Get the variables from outside (parameters, globals ...)
68    * @return the variables from outside
69    */
70   public List getOutsideVariable() {
71     final ArrayList list = new ArrayList();
72     list.addAll(condition.getOutsideVariable()); // todo: check if unuseful
73     if (statement != null) {
74       list.addAll(statement.getOutsideVariable());
75     }
76     for (int i = 0; i < elseifs.length; i++) {
77       list.addAll(elseifs[i].getOutsideVariable());
78     }
79     if (els != null) {
80       list.addAll(els.getOutsideVariable());
81     }
82     return list;
83   }
84
85   /**
86    * get the modified variables.
87    * @return the variables from we change value
88    */
89   public List getModifiedVariable() {
90     final ArrayList list = new ArrayList();
91     list.addAll(condition.getModifiedVariable());
92     if (statement != null) {
93       list.addAll(statement.getModifiedVariable());
94     }
95     for (int i = 0; i < elseifs.length; i++) {
96       list.addAll(elseifs[i].getModifiedVariable());
97     }
98     if (els != null) {
99       list.addAll(els.getModifiedVariable());
100     }
101     return list;
102   }
103
104   /**
105    * Get the variables used.
106    * @return the variables used
107    */
108   public List getUsedVariable() {
109     final ArrayList list = new ArrayList();
110     list.addAll(condition.getUsedVariable());
111     if (statement != null) {
112       list.addAll(statement.getUsedVariable());
113     }
114     for (int i = 0; i < elseifs.length; i++) {
115       list.addAll(elseifs[i].getUsedVariable());
116     }
117     if (els != null) {
118       list.addAll(els.getUsedVariable());
119     }
120     return list;
121   }
122 }