Changes:
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / compiler / ast / Variable.java
1 package net.sourceforge.phpdt.internal.compiler.ast;
2
3 import java.util.List;
4
5 import net.sourceforge.phpdt.internal.compiler.ast.declarations.VariableUsage;
6
7 /**
8  * A variable.
9  * It could be a simple variable, or contains another variable.
10  * @author Matthieu Casanova
11  */
12 public class Variable extends AbstractVariable {
13
14   /** The name of the variable. */
15   private String name;
16
17   /** A variable inside ($$varname). */
18   private AbstractVariable variable;
19
20   /** the variable is defined like this ${expression} */
21   private Expression expression;
22
23   public static final String _GET = "_GET";
24   public static final String _POST = "_POST";
25   public static final String _REQUEST = "_REQUEST";
26   public static final String _SERVER = "_SERVER";
27   public static final String _SESSION = "_SESSION";
28   public static final String _this = "this";
29   public static final String GLOBALS = "GLOBALS";
30   public static final String _COOKIE = "_COOKIE";
31   public static final String _FILES = "_FILES";
32   public static final String _ENV = "_ENV";
33
34   /** Here is an array of all superglobals variables and the special "this". */
35   public static final String[] SPECIAL_VARS = {_GET,
36                                                _POST,
37                                                _REQUEST,
38                                                _SERVER,
39                                                _SESSION,
40                                                _this,
41                                                GLOBALS,
42                                                _COOKIE,
43                                                _FILES,
44                                                _ENV};
45
46   /**
47    * Create a new simple variable.
48    * @param name the name
49    * @param sourceStart the starting position
50    * @param sourceEnd the ending position
51    */
52   public Variable(final String name,
53                   final int sourceStart,
54                   final int sourceEnd) {
55     super(sourceStart, sourceEnd);
56     this.name = name;
57   }
58
59   /**
60    * Create a special variable ($$toto for example).
61    * @param variable the variable contained
62    * @param sourceStart the starting position
63    * @param sourceEnd the ending position
64    */
65   public Variable(final AbstractVariable variable,
66                   final int sourceStart,
67                   final int sourceEnd) {
68     super(sourceStart, sourceEnd);
69     this.variable = variable;
70   }
71
72   /**
73    * Create a special variable ($$toto for example).
74    * @param expression the variable contained
75    * @param sourceStart the starting position
76    * @param sourceEnd the ending position
77    */
78   public Variable(final Expression expression,
79                   final int sourceStart,
80                   final int sourceEnd) {
81     super(sourceStart, sourceEnd);
82     this.expression = expression;
83   }
84
85   /**
86    * Return the expression as String.
87    * @return the expression
88    */
89   public String toStringExpression() {
90     return "$" + getName();
91   }
92
93   public String getName() {
94     if (name != null) {
95       return name;
96     }
97     if (variable != null) {
98       return variable.toStringExpression();
99     }
100     return "{" + expression.toStringExpression() + "}";
101   }
102
103   /**
104    * Get the variables from outside (parameters, globals ...)
105    */
106   public void getOutsideVariable(final List list) {
107   }
108
109   /**
110    * get the modified variables.
111    */
112   public void getModifiedVariable(final List list) {
113   }
114
115   /**
116    * Get the variables used.
117    */
118   public void getUsedVariable(final List list) {
119     final String varName;
120     if (name != null) {
121       varName = name;
122     } else if (variable != null) {
123       varName = variable.getName();
124     } else {
125       varName = expression.toStringExpression();//todo : do a better thing like evaluate this ??
126     }
127     if (!arrayContains(SPECIAL_VARS, name)) {
128       list.add(new VariableUsage(varName, sourceStart));
129     }
130   }
131 }