eef7d947fa232d4226db56b5a6fbf1e9389a9db2
[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 net.sourceforge.phpdt.internal.compiler.ast.Expression;
4 import net.sourceforge.phpdt.internal.compiler.ast.AbstractVariableDeclaration;
5 import net.sourceforge.phpdt.internal.compiler.parser.Outlineable;
6 import net.sourceforge.phpdt.internal.ui.PHPUiImages;
7 import org.eclipse.jface.resource.ImageDescriptor;
8
9 /**
10  * A variable declaration.
11  * @author Matthieu Casanova
12  */
13 public class VariableDeclaration extends AbstractVariableDeclaration implements Outlineable {
14
15   /** The value for variable initialization. */
16   public Expression initialization;
17
18   private Object parent;
19   private boolean reference;
20
21   /**
22    * Create a variable.
23    * @param initialization the initialization
24    * @param name the name of the variable
25    * @param sourceStart the start point
26    */
27   public VariableDeclaration(Object parent,
28                              char[] name,
29                              Expression initialization,
30                              int sourceStart) {
31     super(name, sourceStart, initialization.sourceEnd);
32     this.initialization = initialization;
33     this.parent = parent;
34   }
35
36   /**
37    * Create a variable.
38    * @param initialization the initialization
39    * @param name the name of the variable
40    * @param sourceStart the start point
41    */
42   public VariableDeclaration(Object parent,
43                              char[] name,
44                              int sourceStart,
45                              int sourceEnd) {
46     super(name, sourceStart, sourceEnd);
47     this.initialization = initialization;
48     this.parent = parent;
49   }
50
51   public void setReference(boolean reference) {
52     this.reference = reference;
53   }
54
55   /**
56    * Create a variable.
57    * @param initialization the initialization
58    * @param name the name of the variable
59    * @param sourceStart the start point
60    */
61   public VariableDeclaration(char[] name,
62                              Expression initialization,
63                              int sourceStart) {
64     super(name, sourceStart, initialization.sourceEnd);
65     this.initialization = initialization;
66   }
67
68   /**
69    * Create a variable.
70    * @param name the name of the variable
71    * @param sourceStart the start point
72    */
73   public VariableDeclaration(char[] name,
74                              int sourceStart) {
75     super(name, sourceStart, sourceStart + name.length);
76     this.initialization = initialization;
77   }
78
79   /**
80    * Return the variable into String.
81    * @return a String
82    */
83   public String toStringExpression() {
84     final StringBuffer buff;
85     if (reference) {
86       buff = new StringBuffer('&');
87     } else {
88       buff = new StringBuffer();
89     }
90     buff.append(name);
91     if (initialization != null) {
92       buff.append(" = "); //$NON-NLS-1$
93       buff.append(initialization.toStringExpression());
94     }
95     return buff.toString();
96   }
97
98   public Object getParent() {
99     return parent;
100   }
101
102     /**
103      * Get the image of a variable.
104      * @return the image that represents a php variable
105      */
106     public ImageDescriptor getImage() {
107         return PHPUiImages.DESC_VAR;
108     }
109 }