Initial upgrade to Platform/JDT 3.4.1
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / core / dom / PrimitiveType.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2008 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11
12 package net.sourceforge.phpdt.core.dom;
13
14 import java.util.ArrayList;
15 import java.util.HashMap;
16 import java.util.List;
17 import java.util.Map;
18
19 /**
20  * Primitive type nodes.
21  * <pre>
22  * PrimitiveType:
23  *    <b>byte</b>
24  *    <b>short</b>
25  *    <b>char</b>
26  *    <b>int</b>
27  *    <b>long</b>
28  *    <b>float</b>
29  *    <b>double</b>
30  *    <b>boolean</b>
31  *    <b>void</b>
32  * </pre>
33  * <p>
34  * Note that due to the fact that AST nodes belong to a specific AST and
35  * have a specific parent, there needs to multiple instances of these
36  * nodes.
37  * </p>
38  * 
39  * @since 2.0
40  * @noinstantiate This class is not intended to be instantiated by clients.
41  */
42 public class PrimitiveType extends Type {
43         
44         /**
45          * Primitive type codes (typesafe enumeration).
46          * <pre>
47          *    <b>byte</b>       BYTE
48          *    <b>short</b>      SHORT
49          *    <b>char</b>       CHAR
50          *    <b>int</b>        INT
51          *    <b>long</b>       LONG
52          *    <b>float</b>      FLOAT
53          *    <b>double</b>     DOUBLE
54          *    <b>boolean</b>    BOOLEAN
55          *    <b>void</b>       VOID
56          * </pre>
57          */
58         public static class Code {
59         
60                 /**
61                  * The name of the type.
62                  */
63                 private String name;
64                 
65                 /**
66                  * Creates a new primitive type code with the given name.
67                  * <p>
68                  * Note: this constructor is package-private. The only instances
69                  * ever created are the ones for the standard primitive types.
70                  * </p>
71                  * 
72                  * @param name the standard name of the primitive type
73                  */
74                 Code(String name) {
75                         this.name = name;
76                 }
77                 
78                 /**
79                  * Returns the standard name of the primitive type.
80                  * 
81                  * @return the standard name of the primitive type
82                  */
83                 public String toString() {
84                         return name;
85                 }
86         }               
87         
88         /** Type code for the primitive type "int". */
89         public static final Code INT = new Code("int");//$NON-NLS-1$
90         /** Type code for the primitive type "char". */
91         public static final Code CHAR = new Code("char");//$NON-NLS-1$
92         /** Type code for the primitive type "boolean". */
93         public static final Code BOOLEAN = new Code("boolean");//$NON-NLS-1$
94         /** Type code for the primitive type "short". */
95         public static final Code SHORT = new Code("short");//$NON-NLS-1$
96         /** Type code for the primitive type "long". */
97         public static final Code LONG = new Code("long");//$NON-NLS-1$
98         /** Type code for the primitive type "float". */
99         public static final Code FLOAT = new Code("float");//$NON-NLS-1$
100         /** Type code for the primitive type "double". */
101         public static final Code DOUBLE = new Code("double");//$NON-NLS-1$
102         /** Type code for the primitive type "byte". */
103         public static final Code BYTE = new Code("byte");//$NON-NLS-1$
104
105         /** Type code for the primitive type "void". Note that "void" is
106          * special in that its only legitimate uses are as a method return
107          * type and as a type literal.
108          */
109         public static final Code VOID = new Code("void");//$NON-NLS-1$
110                 
111         /** 
112          * The primitive type code; one of the PrimitiveType constants; default
113          * is int.
114          */
115         private PrimitiveType.Code typeCode = INT;
116         
117         /**
118          * Map from token to primitive type code (key type: <code>String</code>;
119          * value type: <code>PrimitiveType.Code</code>).
120          */
121         private static final Map CODES;
122         static {
123                 CODES = new HashMap(20);
124                 Code[] ops = {
125                                 INT,
126                                 BYTE,
127                                 CHAR,
128                                 BOOLEAN,
129                                 SHORT,
130                                 LONG,
131                                 FLOAT,
132                                 DOUBLE,
133                                 VOID,
134                         };
135                 for (int i = 0; i < ops.length; i++) {
136                         CODES.put(ops[i].toString(), ops[i]);
137                 }
138         }
139         
140         /**
141          * Returns the primitive type code corresponding to the given string,
142          * or <code>null</code> if none.
143          * <p>
144          * <code>toCode</code> is the converse of <code>toString</code>:
145          * that is, 
146          * <code>PrimitiveType.Code.toCode(code.toString()) == code</code>
147          * for all type code <code>code</code>.
148          * </p>
149          * 
150          * @param token the standard name of the primitive type
151          * @return the primitive type code, or <code>null</code> if none
152          */
153         public static PrimitiveType.Code toCode(String token) {
154                 return (PrimitiveType.Code) CODES.get(token);
155         }
156         
157         /**
158          * The "primitiveTypeCode" structural property of this node type.
159          * @since 3.0
160          */
161         public static final SimplePropertyDescriptor PRIMITIVE_TYPE_CODE_PROPERTY = 
162                 new SimplePropertyDescriptor(PrimitiveType.class, "primitiveTypeCode", PrimitiveType.Code.class, MANDATORY); //$NON-NLS-1$
163         
164         /**
165          * A list of property descriptors (element type: 
166          * {@link StructuralPropertyDescriptor}),
167          * or null if uninitialized.
168          */
169         private static final List PROPERTY_DESCRIPTORS;
170         
171         static {
172                 List propertyList = new ArrayList(2);
173                 createPropertyList(PrimitiveType.class, propertyList);
174                 addProperty(PRIMITIVE_TYPE_CODE_PROPERTY, propertyList);
175                 PROPERTY_DESCRIPTORS = reapPropertyList(propertyList);
176         }
177
178         /**
179          * Returns a list of structural property descriptors for this node type.
180          * Clients must not modify the result.
181          * 
182          * @param apiLevel the API level; one of the
183          * <code>AST.JLS*</code> constants
184
185          * @return a list of property descriptors (element type: 
186          * {@link StructuralPropertyDescriptor})
187          * @since 3.0
188          */
189         public static List propertyDescriptors(int apiLevel) {
190                 return PROPERTY_DESCRIPTORS;
191         }
192                         
193         /**
194          * Creates a new unparented node for a primitive type owned by the given
195          * AST. By default, the node has type "int".
196          * <p>
197          * N.B. This constructor is package-private.
198          * </p>
199          * 
200          * @param ast the AST that is to own this node
201          */
202         PrimitiveType(AST ast) {
203                 super(ast);
204         }
205         
206         /* (omit javadoc for this method)
207          * Method declared on ASTNode.
208          */
209         final List internalStructuralPropertiesForType(int apiLevel) {
210                 return propertyDescriptors(apiLevel);
211         }
212         
213         /* (omit javadoc for this method)
214          * Method declared on ASTNode.
215          */
216         final Object internalGetSetObjectProperty(SimplePropertyDescriptor property, boolean get, Object value) {
217                 if (property == PRIMITIVE_TYPE_CODE_PROPERTY) {
218                         if (get) {
219                                 return getPrimitiveTypeCode();
220                         } else {
221                                 setPrimitiveTypeCode((Code) value);
222                                 return null;
223                         }
224                 }
225                 // allow default implementation to flag the error
226                 return super.internalGetSetObjectProperty(property, get, value);
227         }
228
229         /* (omit javadoc for this method)
230          * Method declared on ASTNode.
231          */
232         final int getNodeType0() {
233                 return PRIMITIVE_TYPE;
234         }
235
236         /* (omit javadoc for this method)
237          * Method declared on ASTNode.
238          */
239         ASTNode clone0(AST target) {
240                 PrimitiveType result = new PrimitiveType(target);
241                 result.setSourceRange(this.getStartPosition(), this.getLength());
242                 result.setPrimitiveTypeCode(getPrimitiveTypeCode());
243                 return result;
244         }
245
246         /* (omit javadoc for this method)
247          * Method declared on ASTNode.
248          */
249         final boolean subtreeMatch0(ASTMatcher matcher, Object other) {
250                 // dispatch to correct overloaded match method
251                 return matcher.match(this, other);
252         }
253
254         /* (omit javadoc for this method)
255          * Method declared on ASTNode.
256          */
257         void accept0(ASTVisitor visitor) {
258                 visitor.visit(this);
259                 visitor.endVisit(this);
260         }
261         
262         /**
263          * Returns the primitive type code.
264          * 
265          * @return one of the primitive type code constants declared in this
266          *    class
267          */
268         public PrimitiveType.Code getPrimitiveTypeCode() {
269                 return this.typeCode;
270         }
271         
272         /**
273          * Sets the primitive type code.
274          * 
275          * @param typeCode one of the primitive type code constants declared in 
276          *    this class
277          * @exception IllegalArgumentException if the argument is incorrect
278          */
279         public void setPrimitiveTypeCode(PrimitiveType.Code typeCode) {
280                 if (typeCode == null) {
281                         throw new IllegalArgumentException();
282                 }
283                 preValueChange(PRIMITIVE_TYPE_CODE_PROPERTY);
284                 this.typeCode = typeCode;
285                 postValueChange(PRIMITIVE_TYPE_CODE_PROPERTY);
286         }
287
288         /* (omit javadoc for this method)
289          * Method declared on ASTNode.
290          */
291         int memSize() {
292                 // treat Code as free
293                 return BASE_NODE_SIZE + 1 * 4;
294         }
295         
296         /* (omit javadoc for this method)
297          * Method declared on ASTNode.
298          */
299         int treeSize() {
300                 return memSize();
301         }
302 }