content: /*******************************************************************************
   * Copyright (c) 2009 IBM Corporation and others.
   *
   * This program and the accompanying materials are made
   * available under the terms of the Eclipse Public License 2.0
   * which is available at https://www.eclipse.org/legal/epl-2.0/
   *
   * SPDX-License-Identifier: EPL-2.0
   * 
   * Contributors:
   *     IBM Corporation - initial API and implementation
   *     Zend Technologies
   *******************************************************************************/
  package org.eclipse.php.core.ast.nodes;
  
  import java.util.ArrayList;
  import java.util.Collections;
  import java.util.List;
  
  import org.eclipse.php.core.PHPVersion;
  import org.eclipse.php.core.ast.match.ASTMatcher;
  import org.eclipse.php.core.ast.visitor.Visitor;
  
  /**
   * Represent a return statement
   * 
   * <pre>
   * e.g.
   * 
   * return; return $a;
   * </pre>
   */
  public class ReturnStatement extends Statement {
  
  	private Expression expression;
  
  	/**
  	 * The "expression" structural property of this node type.
  	 */
  	public static final ChildPropertyDescriptor EXPRESSION_PROPERTY = new ChildPropertyDescriptor(ReturnStatement.class,
  			"expression", Expression.class, OPTIONAL, CYCLE_RISK); //$NON-NLS-1$
  
  	/**
  	 * A list of property descriptors (element type:
  	 * {@link StructuralPropertyDescriptor}), or null if uninitialized.
  	 */
  	private static final List<StructuralPropertyDescriptor> PROPERTY_DESCRIPTORS;
  
  	static {
  		List<StructuralPropertyDescriptor> propertyList = new ArrayList<>(1);
  		propertyList.add(EXPRESSION_PROPERTY);
  		PROPERTY_DESCRIPTORS = Collections.unmodifiableList(propertyList);
  	}
  
  	public ReturnStatement(int start, int end, AST ast) {
  		this(start, end, ast, null);
  	}
  
  	public ReturnStatement(AST ast) {
  		super(ast);
  	}
  
  	public ReturnStatement(int start, int end, AST ast, Expression expr) {
  		super(start, end, ast);
  
  		if (expr != null) {
  			setExpression(expr);
  		}
  	}
  
  	@Override
  	public void accept0(Visitor visitor) {
  		final boolean visit = visitor.visit(this);
  		if (visit) {
  			childrenAccept(visitor);
  		}
  		visitor.endVisit(this);
  	}
  
  	@Override
  	public void childrenAccept(Visitor visitor) {
  		if (expression != null) {
  			expression.accept(visitor);
  		}
  	}
  
  	@Override
  	public void traverseTopDown(Visitor visitor) {
  		accept(visitor);
  		if (expression != null) {
  			expression.traverseTopDown(visitor);
  		}
  	}
  
  	@Override
  	public void traverseBottomUp(Visitor visitor) {
  		if (expression != null) {
  			expression.traverseBottomUp(visitor);
  		}
  		accept(visitor);
  	}
  
  	@Override
  	public void toString(StringBuilder buffer, String tab) {
  		buffer.append(tab).append("<ReturnStatement"); //$NON-NLS-1$
  		appendInterval(buffer);
  		buffer.append(">\n"); //$NON-NLS-1$
  		if (expression != null) {
  			expression.toString(buffer, TAB + tab);
  			buffer.append("\n"); //$NON-NLS-1$
  		}
  		buffer.append(tab).append("</ReturnStatement>"); //$NON-NLS-1$
  	}
  
  	@Override
  	public int getType() {
  		return ASTNode.RETURN_STATEMENT;
  	}
  
  	/**
  	 * Returns the expression of this return statement.
  	 * 
  	 * @return the expression node
  	 */
  	public Expression getExpression() {
  		return expression;
  	}
  
  	/**
  	 * Sets the expression of this expression statement.
  	 * 
  	 * @param expression
  	 *            the new expression node
  	 * @exception IllegalArgumentException
  	 *                if:
  	 *                <ul>
  	 *                <li>the node belongs to a different AST</li>
  	 *                <li>the node already has a parent</li>
  	 *                <li>a cycle in would be created</li>
  	 *                </ul>
  	 */
  	public void setExpression(Expression expression) {
  		ASTNode oldChild = this.expression;
  		preReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
  		this.expression = expression;
  		postReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
  	}
  
  	@Override
  	final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) {
  		if (property == EXPRESSION_PROPERTY) {
  			if (get) {
  				return getExpression();
  			} else {
  				setExpression((Expression) child);
  				return null;
  			}
  		}
  		// allow default implementation to flag the error
  		return super.internalGetSetChildProperty(property, get, child);
  	}
  
  	/*
  	 * Method declared on ASTNode.
  	 */
  	@Override
  	public boolean subtreeMatch(ASTMatcher matcher, Object other) {
  		// dispatch to correct overloaded match method
  		return matcher.match(this, other);
  	}
  
  	@Override
  	ASTNode clone0(AST target) {
  		final Expression expr = ASTNode.copySubtree(target, getExpression());
  		final ReturnStatement result = new ReturnStatement(this.getStart(), this.getEnd(), target, expr);
  		return result;
  	}
  
  	@Override
  	List<StructuralPropertyDescriptor> internalStructuralPropertiesForType(PHPVersion apiLevel) {
  		return PROPERTY_DESCRIPTORS;
  	}
  }
  