many changes and fixes
[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 net.sourceforge.phpdt.internal.compiler.ast.declarations.VariableUsage;
4
5 import java.util.List;
6 import java.util.ArrayList;
7
8 /**
9  * A variable.
10  * It could be a simple variable, or contains another variable.
11  * @author Matthieu Casanova
12  */
13 public class Variable extends AbstractVariable {
14
15   /** The name of the variable. */
16   private String name;
17
18   /** A variable inside ($$varname). */
19   private AbstractVariable variable;
20
21   /**
22    * Create a new simple variable.
23    * @param name the name
24    * @param sourceStart the starting position
25    * @param sourceEnd the ending position
26    */
27   public Variable(final String name,
28                   final int sourceStart,
29                   final int sourceEnd) {
30     super(sourceStart, sourceEnd);
31     this.name = name;
32   }
33
34   /**
35    * Create a special variable ($$toto for example).
36    * @param variable the variable contained
37    * @param sourceStart the starting position
38    * @param sourceEnd the ending position
39    */
40   public Variable(final AbstractVariable variable,
41                   final int sourceStart,
42                   final int sourceEnd) {
43     super(sourceStart, sourceEnd);
44     this.variable = variable;
45   }
46
47   /**
48    * Return the expression as String.
49    * @return the expression
50    */
51   public String toStringExpression() {
52     return "$" + getName();
53   }
54
55   public String getName() {
56     if (variable == null) {
57       return name;
58     }
59     return variable.toStringExpression();
60   }
61
62   /**
63    * Get the variables from outside (parameters, globals ...)
64    * @return the variables from outside
65    */
66   public List getOutsideVariable() {
67     return new ArrayList(1);
68   }
69
70   /**
71    * get the modified variables.
72    * @return the variables modified
73    */
74   public List getModifiedVariable() {
75     return new ArrayList(1);
76   }
77
78   /**
79    * Get the variables used.
80    * @return the variables used
81    */
82   public List getUsedVariable() {
83     final ArrayList list = new ArrayList(1);
84     if (name == null) {
85       list.add(new VariableUsage(variable.getName(), sourceStart));
86     } else {
87       list.add(new VariableUsage(name, sourceStart));
88     }
89     return list;
90   }
91 }