Initial upgrade to Platform/JDT 3.4.1
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / core / dom / ReturnStatement.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.List;
16
17 /**
18  * Return statement AST node type.
19  *
20  * <pre>
21  * ReturnStatement:
22  *    <b>return</b> [ Expression ] <b>;</b>
23  * </pre>
24  * 
25  * @since 2.0
26  * @noinstantiate This class is not intended to be instantiated by clients.
27  */
28 public class ReturnStatement extends Statement {
29                         
30         /**
31          * The "expression" structural property of this node type.
32          * @since 3.0
33          */
34         public static final ChildPropertyDescriptor EXPRESSION_PROPERTY = 
35                 new ChildPropertyDescriptor(ReturnStatement.class, "expression", Expression.class, OPTIONAL, CYCLE_RISK); //$NON-NLS-1$
36
37         /**
38          * A list of property descriptors (element type: 
39          * {@link StructuralPropertyDescriptor}),
40          * or null if uninitialized.
41          */
42         private static final List PROPERTY_DESCRIPTORS;
43         
44         static {
45                 List propertyList = new ArrayList(2);
46                 createPropertyList(ReturnStatement.class, propertyList);
47                 addProperty(EXPRESSION_PROPERTY, propertyList);
48                 PROPERTY_DESCRIPTORS = reapPropertyList(propertyList);
49         }
50
51         /**
52          * Returns a list of structural property descriptors for this node type.
53          * Clients must not modify the result.
54          * 
55          * @param apiLevel the API level; one of the
56          * <code>AST.JLS*</code> constants
57          * @return a list of property descriptors (element type: 
58          * {@link StructuralPropertyDescriptor})
59          * @since 3.0
60          */
61         public static List propertyDescriptors(int apiLevel) {
62                 return PROPERTY_DESCRIPTORS;
63         }
64                         
65         /**
66          * The expression; <code>null</code> for none; defaults to none.
67          */
68         private Expression optionalExpression = null;
69         
70         /**
71          * Creates a new AST node for a return statement owned by the 
72          * given AST. By default, the statement has no expression.
73          * 
74          * @param ast the AST that is to own this node
75          */
76         ReturnStatement(AST ast) {
77                 super(ast);
78         }
79
80         /* (omit javadoc for this method)
81          * Method declared on ASTNode.
82          */
83         final List internalStructuralPropertiesForType(int apiLevel) {
84                 return propertyDescriptors(apiLevel);
85         }
86         
87         /* (omit javadoc for this method)
88          * Method declared on ASTNode.
89          */
90         final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) {
91                 if (property == EXPRESSION_PROPERTY) {
92                         if (get) {
93                                 return getExpression();
94                         } else {
95                                 setExpression((Expression) child);
96                                 return null;
97                         }
98                 }
99                 // allow default implementation to flag the error
100                 return super.internalGetSetChildProperty(property, get, child);
101         }
102         
103         /* (omit javadoc for this method)
104          * Method declared on ASTNode.
105          */
106         final int getNodeType0() {
107                 return RETURN_STATEMENT;
108         }
109
110         /* (omit javadoc for this method)
111          * Method declared on ASTNode.
112          */
113         ASTNode clone0(AST target) {
114                 ReturnStatement result = new ReturnStatement(target);
115                 result.setSourceRange(this.getStartPosition(), this.getLength());
116                 result.copyLeadingComment(this);
117                 result.setExpression(
118                         (Expression) ASTNode.copySubtree(target, getExpression()));
119                 return result;
120         }
121
122         /* (omit javadoc for this method)
123          * Method declared on ASTNode.
124          */
125         final boolean subtreeMatch0(ASTMatcher matcher, Object other) {
126                 // dispatch to correct overloaded match method
127                 return matcher.match(this, other);
128         }
129
130         /* (omit javadoc for this method)
131          * Method declared on ASTNode.
132          */
133         void accept0(ASTVisitor visitor) {
134                 boolean visitChildren = visitor.visit(this);
135                 if (visitChildren) {
136                         acceptChild(visitor, getExpression());
137                 }
138                 visitor.endVisit(this);
139         }
140         
141         /**
142          * Returns the expression of this return statement, or 
143          * <code>null</code> if there is none.
144          * 
145          * @return the expression node, or <code>null</code> if there is none
146          */ 
147         public Expression getExpression() {
148                 return this.optionalExpression;
149         }
150         
151         /**
152          * Sets or clears the expression of this return statement.
153          * 
154          * @param expression the expression node, or <code>null</code> if 
155          *    there is none
156          * @exception IllegalArgumentException if:
157          * <ul>
158          * <li>the node belongs to a different AST</li>
159          * <li>the node already has a parent</li>
160          * <li>a cycle in would be created</li>
161          * </ul>
162          */ 
163         public void setExpression(Expression expression) {
164                 ASTNode oldChild = this.optionalExpression;
165                 preReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
166                 this.optionalExpression = expression;
167                 postReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
168         }
169         
170         /* (omit javadoc for this method)
171          * Method declared on ASTNode.
172          */
173         int memSize() {
174                 return super.memSize() + 1 * 4;
175         }
176         
177         /* (omit javadoc for this method)
178          * Method declared on ASTNode.
179          */
180         int treeSize() {
181                 return
182                         memSize()
183                         + (this.optionalExpression == null ? 0 : getExpression().treeSize());
184         }
185 }
186