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