1 package net.sourceforge.phpdt.internal.compiler.ast;
3 import net.sourceforge.phpdt.internal.compiler.ast.declarations.VariableUsage;
6 import java.util.ArrayList;
10 * It could be a simple variable, or contains another variable.
11 * @author Matthieu Casanova
13 public class Variable extends AbstractVariable {
15 /** The name of the variable. */
18 /** A variable inside ($$varname). */
19 private AbstractVariable variable;
21 /** the variable is defined like this ${expression} */
22 private Expression expression;
24 public static final String _GET = "_GET";
25 public static final String _POST = "_POST";
26 public static final String _REQUEST = "_REQUEST";
27 public static final String _SERVER = "_SERVER";
28 public static final String _SESSION = "_SESSION";
29 public static final String _this = "this";
30 public static final String GLOBALS = "GLOBALS";
31 public static final String _COOKIE = "_COOKIE";
32 public static final String _FILES = "_FILES";
33 public static final String _ENV = "_ENV";
35 /** Here is an array of all superglobals variables and the special "this". */
36 public static final String[] SPECIAL_VARS = {_GET,
48 * Create a new simple variable.
49 * @param name the name
50 * @param sourceStart the starting position
51 * @param sourceEnd the ending position
53 public Variable(final String name,
54 final int sourceStart,
55 final int sourceEnd) {
56 super(sourceStart, sourceEnd);
61 * Create a special variable ($$toto for example).
62 * @param variable the variable contained
63 * @param sourceStart the starting position
64 * @param sourceEnd the ending position
66 public Variable(final AbstractVariable variable,
67 final int sourceStart,
68 final int sourceEnd) {
69 super(sourceStart, sourceEnd);
70 this.variable = variable;
74 * Create a special variable ($$toto for example).
75 * @param expression the variable contained
76 * @param sourceStart the starting position
77 * @param sourceEnd the ending position
79 public Variable(final Expression expression,
80 final int sourceStart,
81 final int sourceEnd) {
82 super(sourceStart, sourceEnd);
83 this.expression = expression;
87 * Return the expression as String.
88 * @return the expression
90 public String toStringExpression() {
91 return "$" + getName();
94 public String getName() {
98 if (variable != null) {
99 return variable.toStringExpression();
101 return "{" + expression.toStringExpression() + "}";
105 * Get the variables from outside (parameters, globals ...)
106 * @return the variables from outside
108 public List getOutsideVariable() {
109 return new ArrayList(1);
113 * get the modified variables.
114 * @return the variables modified
116 public List getModifiedVariable() {
117 return new ArrayList(1);
121 * Get the variables used.
122 * @return the variables used
124 public List getUsedVariable() {
125 final String varName;
128 } else if (variable != null) {
129 varName = variable.getName();
131 varName = expression.toStringExpression();//todo : do a better thing like evaluate this ??
133 if (arrayContains(SPECIAL_VARS, name)) {
134 return new ArrayList(1);
136 final ArrayList list = new ArrayList(1);
137 list.add(new VariableUsage(varName, sourceStart));