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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
12 package net.sourceforge.phpdt.core.dom;
14 import java.util.ArrayList;
15 import java.util.HashMap;
16 import java.util.List;
20 * Infix expression AST node type.
23 * Expression InfixOperator Expression { InfixOperator Expression }
27 * @noinstantiate This class is not intended to be instantiated by clients.
29 public class InfixExpression extends Expression {
32 * Infix operators (typesafe enumeration).
34 * InfixOperator:<code>
40 * <b><<</b> LEFT_SHIFT
41 * <b>>></b> RIGHT_SHIFT_SIGNED
42 * <b>>>></b> RIGHT_SHIFT_UNSIGNED
45 * <b><=</b> LESS_EQUALS
46 * <b>>=</b> GREATER_EQUALS
48 * <b>!=</b> NOT_EQUALS
52 * <b>&&</b> CONDITIONAL_AND
53 * <b>||</b> CONDITIONAL_OR</code>
56 public static class Operator {
59 * The token for the operator.
64 * Creates a new infix operator with the given token.
66 * Note: this constructor is private. The only instances
67 * ever created are the ones for the standard operators.
70 * @param token the character sequence for the operator
72 private Operator(String token) {
77 * Returns the character sequence for the operator.
79 * @return the character sequence for the operator
81 public String toString() {
85 /** Multiplication "*" operator. */
86 public static final Operator TIMES = new Operator("*");//$NON-NLS-1$
87 /** Division "/" operator. */
88 public static final Operator DIVIDE = new Operator("/");//$NON-NLS-1$
89 /** Remainder "%" operator. */
90 public static final Operator REMAINDER = new Operator("%");//$NON-NLS-1$
91 /** Addition (or string concatenation) "+" operator. */
92 public static final Operator PLUS = new Operator("+");//$NON-NLS-1$
93 /** Subtraction "-" operator. */
94 public static final Operator MINUS = new Operator("-");//$NON-NLS-1$
95 /** Left shift "<<" operator. */
96 public static final Operator LEFT_SHIFT = new Operator("<<");//$NON-NLS-1$
97 /** Signed right shift ">>" operator. */
98 public static final Operator RIGHT_SHIFT_SIGNED = new Operator(">>");//$NON-NLS-1$
99 /** Unsigned right shift ">>>" operator. */
100 public static final Operator RIGHT_SHIFT_UNSIGNED =
101 new Operator(">>>");//$NON-NLS-1$
102 /** Less than "<" operator. */
103 public static final Operator LESS = new Operator("<");//$NON-NLS-1$
104 /** Greater than ">" operator. */
105 public static final Operator GREATER = new Operator(">");//$NON-NLS-1$
106 /** Less than or equals "<=" operator. */
107 public static final Operator LESS_EQUALS = new Operator("<=");//$NON-NLS-1$
108 /** Greater than or equals ">=;" operator. */
109 public static final Operator GREATER_EQUALS = new Operator(">=");//$NON-NLS-1$
110 /** Equals "==" operator. */
111 public static final Operator EQUALS = new Operator("==");//$NON-NLS-1$
112 /** Not equals "!=" operator. */
113 public static final Operator NOT_EQUALS = new Operator("!=");//$NON-NLS-1$
114 /** Exclusive OR "^" operator. */
115 public static final Operator XOR = new Operator("^");//$NON-NLS-1$
116 /** Inclusive OR "|" operator. */
117 public static final Operator OR = new Operator("|");//$NON-NLS-1$
118 /** AND "&" operator. */
119 public static final Operator AND = new Operator("&");//$NON-NLS-1$
120 /** Conditional OR "||" operator. */
121 public static final Operator CONDITIONAL_OR = new Operator("||");//$NON-NLS-1$
122 /** Conditional AND "&&" operator. */
123 public static final Operator CONDITIONAL_AND = new Operator("&&");//$NON-NLS-1$
126 * Map from token to operator (key type: <code>String</code>;
127 * value type: <code>Operator</code>).
129 private static final Map CODES;
131 CODES = new HashMap(20);
140 RIGHT_SHIFT_UNSIGNED,
153 for (int i = 0; i < ops.length; i++) {
154 CODES.put(ops[i].toString(), ops[i]);
159 * Returns the infix operator corresponding to the given string,
160 * or <code>null</code> if none.
162 * <code>toOperator</code> is the converse of <code>toString</code>:
163 * that is, <code>Operator.toOperator(op.toString()) == op</code> for
164 * all operators <code>op</code>.
167 * @param token the character sequence for the operator
168 * @return the infix operator, or <code>null</code> if none
170 public static Operator toOperator(String token) {
171 return (Operator) CODES.get(token);
177 * The "leftOperand" structural property of this node type.
180 public static final ChildPropertyDescriptor LEFT_OPERAND_PROPERTY =
181 new ChildPropertyDescriptor(InfixExpression.class, "leftOperand", Expression.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$
184 * The "operator" structural property of this node type.
187 public static final SimplePropertyDescriptor OPERATOR_PROPERTY =
188 new SimplePropertyDescriptor(InfixExpression.class, "operator", InfixExpression.Operator.class, MANDATORY); //$NON-NLS-1$
191 * The "rightOperand" structural property of this node type.
194 public static final ChildPropertyDescriptor RIGHT_OPERAND_PROPERTY =
195 new ChildPropertyDescriptor(InfixExpression.class, "rightOperand", Expression.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$
198 * The "extendedOperands" structural property of this node type.
201 public static final ChildListPropertyDescriptor EXTENDED_OPERANDS_PROPERTY =
202 new ChildListPropertyDescriptor(InfixExpression.class, "extendedOperands", Expression.class, CYCLE_RISK); //$NON-NLS-1$
205 * A list of property descriptors (element type:
206 * {@link StructuralPropertyDescriptor}),
207 * or null if uninitialized.
209 private static final List PROPERTY_DESCRIPTORS;
212 List properyList = new ArrayList(5);
213 createPropertyList(InfixExpression.class, properyList);
214 addProperty(LEFT_OPERAND_PROPERTY, properyList);
215 addProperty(OPERATOR_PROPERTY, properyList);
216 addProperty(RIGHT_OPERAND_PROPERTY, properyList);
217 addProperty(EXTENDED_OPERANDS_PROPERTY, properyList);
218 PROPERTY_DESCRIPTORS = reapPropertyList(properyList);
222 * Returns a list of structural property descriptors for this node type.
223 * Clients must not modify the result.
225 * @param apiLevel the API level; one of the
226 * <code>AST.JLS*</code> constants
228 * @return a list of property descriptors (element type:
229 * {@link StructuralPropertyDescriptor})
232 public static List propertyDescriptors(int apiLevel) {
233 return PROPERTY_DESCRIPTORS;
237 * The infix operator; defaults to InfixExpression.Operator.PLUS.
239 private InfixExpression.Operator operator = InfixExpression.Operator.PLUS;
242 * The left operand; lazily initialized; defaults to an unspecified,
243 * but legal, simple name.
245 private Expression leftOperand = null;
248 * The right operand; lazily initialized; defaults to an unspecified,
249 * but legal, simple name.
251 private Expression rightOperand = null;
254 * The list of extended operand expressions (element type:
255 * <code>Expression</code>). Lazily initialized; defaults to an empty list.
257 private ASTNode.NodeList extendedOperands = null;
260 * Creates a new AST node for an infix expression owned by the given
261 * AST. By default, the node has unspecified (but legal) operator,
262 * left and right operands, and an empty list of additional operands.
264 * @param ast the AST that is to own this node
266 InfixExpression(AST ast) {
270 /* (omit javadoc for this method)
271 * Method declared on ASTNode.
273 final List internalStructuralPropertiesForType(int apiLevel) {
274 return propertyDescriptors(apiLevel);
277 /* (omit javadoc for this method)
278 * Method declared on ASTNode.
280 final Object internalGetSetObjectProperty(SimplePropertyDescriptor property, boolean get, Object value) {
281 if (property == OPERATOR_PROPERTY) {
283 return getOperator();
285 setOperator((Operator) value);
289 // allow default implementation to flag the error
290 return super.internalGetSetObjectProperty(property, get, value);
293 /* (omit javadoc for this method)
294 * Method declared on ASTNode.
296 final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) {
297 if (property == LEFT_OPERAND_PROPERTY) {
299 return getLeftOperand();
301 setLeftOperand((Expression) child);
305 if (property == RIGHT_OPERAND_PROPERTY) {
307 return getRightOperand();
309 setRightOperand((Expression) child);
313 // allow default implementation to flag the error
314 return super.internalGetSetChildProperty(property, get, child);
317 /* (omit javadoc for this method)
318 * Method declared on ASTNode.
320 final List internalGetChildListProperty(ChildListPropertyDescriptor property) {
321 if (property == EXTENDED_OPERANDS_PROPERTY) {
322 return extendedOperands();
324 // allow default implementation to flag the error
325 return super.internalGetChildListProperty(property);
328 /* (omit javadoc for this method)
329 * Method declared on ASTNode.
331 final int getNodeType0() {
332 return INFIX_EXPRESSION;
335 /* (omit javadoc for this method)
336 * Method declared on ASTNode.
338 ASTNode clone0(AST target) {
339 InfixExpression result = new InfixExpression(target);
340 result.setSourceRange(this.getStartPosition(), this.getLength());
341 result.setOperator(getOperator());
342 result.setLeftOperand((Expression) getLeftOperand().clone(target));
343 result.setRightOperand((Expression) getRightOperand().clone(target));
344 if (this.extendedOperands != null) {
345 // be careful not to trigger lazy creation of list
346 result.extendedOperands().addAll(
347 ASTNode.copySubtrees(target, this.extendedOperands()));
352 /* (omit javadoc for this method)
353 * Method declared on ASTNode.
355 final boolean subtreeMatch0(ASTMatcher matcher, Object other) {
356 // dispatch to correct overloaded match method
357 return matcher.match(this, other);
360 /* (omit javadoc for this method)
361 * Method declared on ASTNode.
363 void accept0(ASTVisitor visitor) {
364 boolean visitChildren = visitor.visit(this);
366 // visit children in normal left to right reading order
367 acceptChild(visitor, getLeftOperand());
368 acceptChild(visitor, getRightOperand());
369 if (this.extendedOperands != null) {
370 // be careful not to trigger lazy creation of list
371 acceptChildren(visitor, this.extendedOperands);
374 visitor.endVisit(this);
378 * Returns the operator of this infix expression.
380 * @return the infix operator
382 public InfixExpression.Operator getOperator() {
383 return this.operator;
387 * Sets the operator of this infix expression.
389 * @param operator the infix operator
390 * @exception IllegalArgumentException if the argument is incorrect
392 public void setOperator(InfixExpression.Operator operator) {
393 if (operator == null) {
394 throw new IllegalArgumentException();
396 preValueChange(OPERATOR_PROPERTY);
397 this.operator = operator;
398 postValueChange(OPERATOR_PROPERTY);
402 * Returns the left operand of this infix expression.
404 * @return the left operand node
406 public Expression getLeftOperand() {
407 if (this.leftOperand == null) {
408 // lazy init must be thread-safe for readers
409 synchronized (this) {
410 if (this.leftOperand == null) {
412 this.leftOperand= new SimpleName(this.ast);
413 postLazyInit(this.leftOperand, LEFT_OPERAND_PROPERTY);
417 return this.leftOperand;
421 * Sets the left operand of this infix expression.
423 * @param expression the left operand node
424 * @exception IllegalArgumentException if:
426 * <li>the node belongs to a different AST</li>
427 * <li>the node already has a parent</li>
428 * <li>a cycle in would be created</li>
431 public void setLeftOperand(Expression expression) {
432 if (expression == null) {
433 throw new IllegalArgumentException();
435 ASTNode oldChild = this.leftOperand;
436 preReplaceChild(oldChild, expression, LEFT_OPERAND_PROPERTY);
437 this.leftOperand = expression;
438 postReplaceChild(oldChild, expression, LEFT_OPERAND_PROPERTY);
442 * Returns the right operand of this infix expression.
444 * @return the right operand node
446 public Expression getRightOperand() {
447 if (this.rightOperand == null) {
448 // lazy init must be thread-safe for readers
449 synchronized (this) {
450 if (this.rightOperand == null) {
452 this.rightOperand= new SimpleName(this.ast);
453 postLazyInit(this.rightOperand, RIGHT_OPERAND_PROPERTY);
457 return this.rightOperand;
461 * Sets the right operand of this infix expression.
463 * @param expression the right operand node
464 * @exception IllegalArgumentException if:
466 * <li>the node belongs to a different AST</li>
467 * <li>the node already has a parent</li>
468 * <li>a cycle in would be created</li>
471 public void setRightOperand(Expression expression) {
472 if (expression == null) {
473 throw new IllegalArgumentException();
475 ASTNode oldChild = this.rightOperand;
476 preReplaceChild(oldChild, expression, RIGHT_OPERAND_PROPERTY);
477 this.rightOperand = expression;
478 postReplaceChild(oldChild, expression, RIGHT_OPERAND_PROPERTY);
482 * Returns where there are any extended operands.
484 * @return <code>true</code> if there are one or more extended operands,
485 * and <code>false</code> if there are no extended operands
487 public boolean hasExtendedOperands() {
489 (this.extendedOperands != null) && this.extendedOperands.size() > 0;
493 * Returns the live list of extended operands.
495 * The extended operands is the preferred way of representing deeply nested
496 * expressions of the form <code>L op R op R2 op R3...</code> where
497 * the same operator appears between all the operands (the most
498 * common case being lengthy string concatenation expressions). Using
499 * the extended operands keeps the trees from getting too deep; this
500 * decreases the risk is running out of thread stack space at runtime
501 * when traversing such trees.
502 * ((a + b) + c) + d would be translated to:
505 * extendedOperands: {c, d}
509 * @return the live list of extended operands
510 * (element type: <code>Expression</code>)
512 public List extendedOperands() {
513 if (this.extendedOperands == null) {
515 this.extendedOperands = new ASTNode.NodeList(EXTENDED_OPERANDS_PROPERTY);
517 return this.extendedOperands;
520 /* (omit javadoc for this method)
521 * Method declared on ASTNode.
524 // treat Operator as free
525 return BASE_NODE_SIZE + 4 * 4;
528 /* (omit javadoc for this method)
529 * Method declared on ASTNode.
534 + (this.leftOperand == null ? 0 : getLeftOperand().treeSize())
535 + (this.rightOperand == null ? 0 : getRightOperand().treeSize())
536 + (this.extendedOperands == null ? 0 : extendedOperands.listSize());