Changes:
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / compiler / ast / VariableDeclaration.java
1 package net.sourceforge.phpdt.internal.compiler.ast;
2
3 import java.util.List;
4
5 import net.sourceforge.phpdt.internal.compiler.parser.Outlineable;
6 import net.sourceforge.phpdt.internal.ui.PHPUiImages;
7
8 import org.eclipse.jface.resource.ImageDescriptor;
9 import org.eclipse.jface.text.Position;
10
11 /**
12  * A variable declaration.
13  * @author Matthieu Casanova
14  */
15 public class VariableDeclaration extends Expression implements Outlineable {
16
17   public static final int EQUAL = 0;
18   public static final int PLUS_EQUAL = 1;
19   public static final int MINUS_EQUAL = 2;
20   public static final int STAR_EQUAL = 3;
21   public static final int SLASH_EQUAL = 4;
22   public static final int AND_EQUAL = 5;
23   public static final int OR_EQUAL = 6;
24   public static final int XOR_EQUAL = 7;
25   public static final int DOT_EQUAL = 8;
26   public static final int REM_EQUAL = 9;
27   public static final int TILDE_EQUAL = 10;
28   public static final int LSHIFT_EQUAL = 11;
29   public static final int RSIGNEDSHIFT_EQUAL = 12;
30
31   protected AbstractVariable variable;
32
33   /** The value for variable initialization. */
34   public Expression initialization;
35
36   private Object parent;
37   private boolean reference;
38
39   private Position position;
40
41   private int operator;
42
43   /**
44    * Create a variable.
45    * @param variable the name of the variable
46    * @param initialization the initialization
47    * @param operator the assign operator
48    * @param sourceStart the start point
49    */
50   public VariableDeclaration(final Object parent,
51                              final AbstractVariable variable,
52                              final Expression initialization,
53                              final int operator,
54                              final int sourceStart) {
55     super(sourceStart, initialization.sourceEnd);
56     this.initialization = initialization;
57     this.variable = variable;
58     this.operator = operator;
59     this.parent = parent;
60     position = new Position(sourceStart, sourceEnd);
61   }
62
63   /**
64    * Create a variable.
65    * @param variable a variable (in case of $$variablename)
66    * @param sourceStart the start point
67    */
68   public VariableDeclaration(final Object parent,
69                              final AbstractVariable variable,
70                              final int sourceStart,
71                              final int sourceEnd) {
72     super(sourceStart, sourceEnd);
73     this.variable = variable;
74     this.parent = parent;
75     position = new Position(sourceStart, sourceEnd);
76   }
77
78   public void setReference(final boolean reference) {
79     this.reference = reference;
80   }
81
82   /**
83    * Create a variable.
84    * @param initialization the initialization
85    * @param variable a variable (in case of $$variablename)
86    * @param sourceStart the start point
87    */
88   public VariableDeclaration(final AbstractVariable variable,
89                              final Expression initialization,
90                              final int operator,
91                              final int sourceStart) {
92     super(sourceStart, initialization.sourceEnd);
93     this.variable = variable;
94     this.initialization = initialization;
95     this.operator = operator;
96   }
97
98   /**
99    * Create a variable.
100    * @param variable a variable (in case of $$variablename)
101    * @param sourceStart the start point
102    */
103   public VariableDeclaration(final AbstractVariable variable,
104                              final int sourceStart) {
105     super(sourceStart, variable.sourceEnd);
106     this.variable = variable;
107   }
108
109   /**
110    * Return the operator as String.
111    * @return the operator
112    */
113   public final String operatorToString() {
114     switch (operator) {
115       case EQUAL:
116         return "="; //$NON-NLS-1$
117       case PLUS_EQUAL:
118         return "+=";   //$NON-NLS-1$
119       case MINUS_EQUAL:
120         return "-=";   //$NON-NLS-1$
121       case STAR_EQUAL:
122         return "*="; //$NON-NLS-1$
123       case SLASH_EQUAL:
124         return "/="; //$NON-NLS-1$
125       case AND_EQUAL:
126         return "<="; //$NON-NLS-1$
127       case OR_EQUAL:
128         return "|=";//$NON-NLS-1$
129       case XOR_EQUAL:
130         return "^=";//$NON-NLS-1$
131       case DOT_EQUAL:
132         return ".="; //$NON-NLS-1$
133       case REM_EQUAL:
134         return "%="; //$NON-NLS-1$
135       case TILDE_EQUAL:
136         return "~="; //$NON-NLS-1$
137       case LSHIFT_EQUAL:
138         return "<<="; //$NON-NLS-1$
139       case RSIGNEDSHIFT_EQUAL:
140         return ">>="; //$NON-NLS-1$
141     }
142     return " unknown operator ";//$NON-NLS-1$
143   }
144
145   /**
146    * Return the variable into String.
147    * @return a String
148    */
149   public String toStringExpression() {
150     final StringBuffer buff;
151     if (reference) {
152       buff = new StringBuffer("&"); //$NON-NLS-1$
153     } else {
154       buff = new StringBuffer();//$NON-NLS-1$
155     }
156     buff.append(variable.toStringExpression());
157     if (initialization != null) {
158       buff.append(operatorToString()); //$NON-NLS-1$
159       buff.append(initialization.toStringExpression());
160     }
161     return buff.toString();
162   }
163
164   public Object getParent() {
165     return parent;
166   }
167
168   public String toString() {
169     return toStringExpression();
170   }
171
172   /**
173    * Get the image of a variable.
174    * @return the image that represents a php variable
175    */
176   public ImageDescriptor getImage() {
177     return PHPUiImages.DESC_VAR;
178   }
179
180   public Position getPosition() {
181     return position;
182   }
183
184   /**
185    * Get the name of the field as String.
186    * @return the name of the String
187    */
188   public String name() {
189     return variable.getName();
190   }
191
192   /**
193    * Get the variables from outside (parameters, globals ...)
194    */
195   public void getOutsideVariable(final List list) {
196   }
197
198   /**
199    * get the modified variables.
200    */
201   public void getModifiedVariable(final List list) {
202     variable.getUsedVariable(list);
203     if (initialization != null) {
204       initialization.getModifiedVariable(list);
205     }
206   }
207
208   /**
209    * Get the variables used.
210    */
211   public void getUsedVariable(final List list) {
212     if (initialization != null) {
213       initialization.getUsedVariable(list);
214     }
215   }
216 }