*** empty log message ***
[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     buff.append(statement.toString(tab + 1));
53     for (int i = 0; i < elseifs.length; i++) {
54       buff.append(elseifs[i].toString(tab + 1));
55       buff.append("\n");//$NON-NLS-1$
56     }
57     if (els != null) {
58       buff.append(els.toString(tab + 1));
59       buff.append("\n");//$NON-NLS-1$
60     }
61     return buff.toString();
62   }
63
64   /**
65    * Get the variables from outside (parameters, globals ...)
66    * @return the variables from outside
67    */
68   public List getOutsideVariable() {
69     final ArrayList list = new ArrayList();
70     list.addAll(condition.getOutsideVariable()); // todo: check if unuseful
71     list.addAll(statement.getOutsideVariable());
72     for (int i = 0; i < elseifs.length; i++) {
73       list.addAll(elseifs[i].getOutsideVariable());
74     }
75     if (els != null) {
76       list.addAll(els.getOutsideVariable());
77     }
78     return list;
79   }
80
81   /**
82    * get the modified variables.
83    * @return the variables from we change value
84    */
85   public List getModifiedVariable() {
86     final ArrayList list = new ArrayList();
87     list.addAll(condition.getModifiedVariable());
88     list.addAll(statement.getModifiedVariable());
89     for (int i = 0; i < elseifs.length; i++) {
90       list.addAll(elseifs[i].getModifiedVariable());
91     }
92     if (els != null) {
93       list.addAll(els.getModifiedVariable());
94     }
95     return list;
96   }
97
98   /**
99    * Get the variables used.
100    * @return the variables used
101    */
102   public List getUsedVariable() {
103     final ArrayList list = new ArrayList();
104     list.addAll(condition.getUsedVariable());
105     list.addAll(statement.getUsedVariable());
106     for (int i = 0; i < elseifs.length; i++) {
107       list.addAll(elseifs[i].getUsedVariable());
108     }
109     if (els != null) {
110       list.addAll(els.getUsedVariable());
111     }
112     return list;
113   }
114 }