Source code: javax/ide/model/java/source/tree/ExpressionT.java
1 /*
2 * @(#)ExpressionT.java
3 */
4
5 package javax.ide.model.java.source.tree;
6
7 import java.util.LinkedHashMap;
8 import java.util.List;
9 import java.util.Map;
10 import java.util.Set;
11
12 /**
13 * Common supertypes for all expressions. If it is a primary,
14 * selector, or method invocation, it has a name. Otherwise, it is an
15 * operator of some sort and has operands. With each enumerated
16 * constant is a description of which fields are non-null and
17 * useful. <p/>
18 *
19 * @author Andy Yu
20 * */
21 public interface ExpressionT
22 extends Tree
23 {
24 // ----------------------------------------------------------------------
25
26 public static final ExpressionT[] EMPTY_ARRAY =
27 new ExpressionT[ 0 ];
28
29
30 // ----------------------------------------------------------------------
31
32 /**
33 * Identifies the operation this expression is performing.
34 */
35 public ExpressionKind getExpressionKind();
36
37
38 // ----------------------------------------------------------------------
39
40 /**
41 * @return the ExpressionT representing the first operand.
42 * Non-null if this expression is an operator (and hence has
43 * operands).
44 */
45 public ExpressionT getFirstOperand();
46
47 /**
48 * @return the ExpressionT representing the second operand.
49 * Non-null if this operator has a second operand.
50 */
51 public ExpressionT getSecondOperand();
52
53 /**
54 * @return the ExpressionT representing the third operand.
55 * Non-null if this has a third operand, e.g. "x? x: x".
56 */
57 public ExpressionT getThirdOperand();
58
59 /**
60 * @return the array of ExpressionTs. Always non-null. May be
61 * zero-length. Returns a collection of ExpressionTs. <p/>
62 *
63 * List of ExpressionTs.
64 */
65 public List getOperands();
66
67 /**
68 * @return The size of the operand array.
69 */
70 public int getOperandCount();
71
72 /**
73 * Gets the operand at the specified index in the operand array.
74 *
75 * @param index 0-based.
76 *
77 * @return Null if the indicated operand doesn't exist.
78 */
79 public ExpressionT getOperandAt( int index );
80
81
82 // ----------------------------------------------------------------------
83
84 public static final class ExpressionKind
85 {
86 public static final int EXPR_invalid = 0;
87 public static final int EXPR_base = 1;
88
89 /** Arithmetic addition or string concatenation. 2+ operands. InfixExpressionT.class. */
90 public static final int EXPR_ADD = EXPR_base + 0;
91
92 /** Arithmetic addition or string concatenation assignment. 2
93 * operands. InfixExpressionT.class. */
94 public static final int EXPR_ADDASG = EXPR_base + 1;
95
96 /** Conditional (logical) AND. 2+ operands. InfixExpressionT.class. */
97 public static final int EXPR_AND = EXPR_base + 2;
98
99 /** Annotation expression. 0 operands. AnnotationExpressionT.class. */
100 public static final int EXPR_ANNOTATION = EXPR_base + 3;
101
102 /** An array constant. 0+ operands. This is LIST rather than
103 * LITERAL because this is not a lexer literal. ListExpressionT.class. */
104 public static final int EXPR_ARRAYCONST = EXPR_base + 4;
105
106 /** Assignment. 2 operands. InfixExpressionT.class. */
107 public static final int EXPR_ASG = EXPR_base + 5;
108
109 /** Bitwise AND. 2+ operands. InfixExpressionT.class. */
110 public static final int EXPR_BITAND = EXPR_base + 6;
111
112 /** Bitwise AND assignment. 2 operands. InfixExpressionT.class. */
113 public static final int EXPR_BITANDASG = EXPR_base + 7;
114
115 /** Bitwise NOT (negation or complement). 2 operands. UnaryExpressionT.class. */
116 public static final int EXPR_BITNOT = EXPR_base + 8;
117
118 /** Bitwise OR. 2+ operands. InfixExpressionT.class. */
119 public static final int EXPR_BITOR = EXPR_base + 9;
120
121 /** Bitwise OR assignment. 2 operands. InfixExpressionT.class. */
122 public static final int EXPR_BITORASG = EXPR_base + 10;
123
124 /** Bitwise XOR. 2+ operands. InfixExpressionT.class. */
125 public static final int EXPR_BITXOR = EXPR_base + 11;
126
127 /** Bitwise XOR assignment. 2 operands. InfixExpressionT.class. */
128 public static final int EXPR_BITXORASG = EXPR_base + 12;
129
130 /** A class literal. 1 operand. Operand is an TYPE. Despite the
131 * name, this is not a lexical literal. We call it a class literal
132 * because that's what the JLS calls it. You ask them. UnaryExpressionT.class. */
133 public static final int EXPR_CLASS_LITERAL = EXPR_base + 13;
134
135 /** Conditional operator (?:). 3 operands. QuestionExpressionT.class. */
136 public static final int EXPR_COND = EXPR_base + 14;
137
138 /** Array dereference. 2 operands. 1st is the expression lhs. 2nd
139 * is LIST with WRAPPER operand per array dereference. Operands
140 * are WRAPPER in order to record the offsets of the brackets. ArrayAccessExpressionT.class. */
141 public static final int EXPR_DEREF = EXPR_base + 15;
142
143 /** Arithmetic division. 2 operands. InfixExpressionT.class. */
144 public static final int EXPR_DIV = EXPR_base + 16;
145
146 /** Arithmetic division assignment. 2 operands. InfixExpressionT.class. */
147 public static final int EXPR_DIVASG = EXPR_base + 17;
148
149 /** Dot dereference. 1 operand. DotExpressionT.class. */
150 public static final int EXPR_DOT = EXPR_base + 18;
151
152 /** Equal. 2 operands. InfixExpressionT.class. */
153 public static final int EXPR_EQ = EXPR_base + 19;
154
155 /** Greater than or equal to. 2 operands InfixExpressionT.class. */
156 public static final int EXPR_GE = EXPR_base + 20;
157
158 /** Greater than. 2 operands. InfixExpressionT.class. */
159 public static final int EXPR_GT = EXPR_base + 21;
160
161 /** Simple name. 0 operands. IdentifierExpressionT.class. */
162 public static final int EXPR_IDENTIFIER = EXPR_base + 22;
163
164 /** Instanceof. 2 operands. 2nd is an TYPE. InfixExpressionT.class. */
165 public static final int EXPR_INSTANCEOF = EXPR_base + 23;
166
167 /** Method invocation. 1, 2, or 3 operands. If 1, the operand is
168 * the LIST arguments. If 2, the 1st operand is the lhs and the
169 * 2nd is the LIST arguments. If 3, the 1st operand is the lhs,
170 * the 2nd is the type arguments, and the 3rd is the LIST
171 * arguments. In * all cases, the last operand is the LIST
172 * arguments. MethodCallExpressionT.class. */
173 public static final int EXPR_INVOKE = EXPR_base + 24;
174
175 /** Less than. 2 operands. InfixExpressionT.class. */
176 public static final int EXPR_LE = EXPR_base + 25;
177
178 /** A list of operands. 0+ operands. ListExpressionT.class. */
179 public static final int EXPR_LIST = EXPR_base + 26;
180
181 /** Lexical literal. 0 operands. LiteralExpressionT.class. */
182 public static final int EXPR_LITERAL = EXPR_base + 27;
183
184 /** Bitwise left shift. 2 operands. InfixExpressionT.class. */
185 public static final int EXPR_LSH = EXPR_base + 28;
186
187 /** Bitwise left shift assignment. 2 operands. InfixExpressionT.class. */
188 public static final int EXPR_LSHASG = EXPR_base + 29;
189
190 /** Less than or equal to. 2 operands. InfixExpressionT.class. */
191 public static final int EXPR_LT = EXPR_base + 30;
192
193 /** Arithmetic subtraction. 2 operands. InfixExpressionT.class. */
194 public static final int EXPR_MINUS = EXPR_base + 31;
195
196 /** Arithmetic subtraction assignment. 2 operands. InfixExpressionT.class. */
197 public static final int EXPR_MINUSASG = EXPR_base + 32;
198
199 /** Arithmetic remainder (modulus). 2 operands. InfixExpressionT.class. */
200 public static final int EXPR_MOD = EXPR_base + 33;
201
202 /** Arithmetic remainder (modulus) assignment. 2 operands. InfixExpressionT.class. */
203 public static final int EXPR_MODASG = EXPR_base + 34;
204
205 /** Arithmetic multiplication. 2+ operands. InfixExpressionT.class. */
206 public static final int EXPR_MUL = EXPR_base + 35;
207
208 /** Arithmetic multiplication assignment. 2 operands. InfixExpressionT.class. */
209 public static final int EXPR_MULASG = EXPR_base + 36;
210
211 /** Arithmetic negative prefix. 1 operand. UnaryExpressionT.class. */
212 public static final int EXPR_NEGATIVE = EXPR_base + 37;
213
214 /** Array creator. 1 operand. If operand is an LIST, then this is
215 * an uninitialized array creator. Else the operand is an
216 * ARRAYCONST and this is an initialized array creator. The type
217 * involves a special getter. NewArrayExpressionT.class. */
218 public static final int EXPR_NEWARRAY = EXPR_base + 38;
219
220 /** Class creator. 1 or 2 operands. If 2, the 1st operand is the
221 * lhs of an inner class creator. The last operand is an LIST for
222 * the arguments. The type involves a special getter. The class
223 * body involves a special getter. NewClassExpressionT.class. */
224 public static final int EXPR_NEWOBJECT = EXPR_base + 39;
225
226 /** Logical NOT (negation or complement). 1 operand. UnaryExpressionT.class. */
227 public static final int EXPR_NOT = EXPR_base + 40;
228
229 /** Not-equal. 2 operands. InfixExpressionT.class. */
230 public static final int EXPR_NOTEQ = EXPR_base + 41;
231
232 /** Conditional (logical) OR. 2+ operands. InfixExpressionT.class. */
233 public static final int EXPR_OR = EXPR_base + 42;
234
235 /** Arithmetic positive prefix. 1 operand. UnaryExpressionT.class. */
236 public static final int EXPR_POSITIVE = EXPR_base + 43;
237
238 /** Decrement postfix. 1 operand. UnaryExpressionT.class. */
239 public static final int EXPR_POSTDEC = EXPR_base + 44;
240
241 /** Increment postfix. 1 operand. UnaryExpressionT.class. */
242 public static final int EXPR_POSTINC = EXPR_base + 45;
243
244 /** Decrement prefix. 1 operand. UnaryExpressionT.class. */
245 public static final int EXPR_PREDEC = EXPR_base + 46;
246
247 /** Increment prefix. 1 operand. UnaryExpressionT.class. */
248 public static final int EXPR_PREINC = EXPR_base + 47;
249
250 /** Qualified super. 1 operand. Operand is an TYPE. UnaryExpressionT.class. */
251 public static final int EXPR_QUALIFIED_SUPER = EXPR_base + 48;
252
253 /** Qualified this. 1 operand. Operand is an TYPE. UnaryExpressionT.class. */
254 public static final int EXPR_QUALIFIED_THIS = EXPR_base + 49;
255
256 /** Bitwise signed right shift. 2 operands. InfixExpressionT.class. */
257 public static final int EXPR_RSH = EXPR_base + 50;
258
259 /** Bitwise signed right shift assignment. 2 operands. InfixExpressionT.class. */
260 public static final int EXPR_RSHASG = EXPR_base + 51;
261
262 /** Type expression. 0 operands. TypeExpressionT.class. */
263 public static final int EXPR_TYPE = EXPR_base + 52;
264
265 /** Typecast. 2 operands. 1st is an TYPE. TypecastExpressionT.class. */
266 public static final int EXPR_TYPECAST = EXPR_base + 53;
267
268 /** Bitwise unsigned right shift. 2 operands. InfixExpressionT.class. */
269 public static final int EXPR_URSH = EXPR_base + 54;
270
271 /** Bitwise unsigned right shift assignment. 2 operands. InfixExpressionT.class. */
272 public static final int EXPR_URSHASG = EXPR_base + 55;
273
274 /** Nested expression (in parentheses or brackets). 1 operand. WrapperExpressionT.class. */
275 public static final int EXPR_WRAPPER = EXPR_base + 56;
276
277 ;
278
279 public static final int EXPR_max = EXPR_base + 57;
280
281 // ----------------------------------------------------------------------
282
283 private final int ordinal;
284
285 private final String name;
286
287 private final Class exprClass;
288
289 private ExpressionKind(int ordinal, String name, Class c)
290 {
291 this.ordinal = ordinal;
292 this.name = name;
293 this.exprClass = c;
294 }
295
296 public Class getExpressionClass()
297 {
298 return exprClass;
299 }
300
301
302 // ----------------------------------------------------------------------
303
304 // Begin enum compatibility section.
305
306 public String name()
307 {
308 return name;
309 }
310
311 public String toString()
312 {
313 return name();
314 }
315
316 public int ordinal()
317 {
318 return ordinal;
319 }
320
321 public int hashCode()
322 {
323 return ordinal();
324 }
325
326 public int compareTo(ExpressionKind other)
327 {
328 return ordinal() - other.ordinal();
329 }
330
331 public boolean equals(Object other)
332 {
333 if (other instanceof ExpressionKind)
334 {
335 final ExpressionKind tk = (ExpressionKind) other;
336 return ordinal() == tk.ordinal();
337 }
338
339 return false;
340 }
341
342 public Class getDeclaringClass()
343 {
344 return ExpressionKind.class;
345 }
346
347
348 // ----------------------------------------------------------------------
349
350 private static final Map values = new LinkedHashMap();
351
352 private static final String[] EXPR_names =
353 {
354 "EXPR_ADD",
355 "EXPR_ADDASG",
356 "EXPR_AND",
357 "EXPR_ANNOTATION",
358 "EXPR_ARRAYCONST",
359 "EXPR_ASG",
360 "EXPR_BITAND",
361 "EXPR_BITANDASG",
362 "EXPR_BITNOT",
363 "EXPR_BITOR",
364 "EXPR_BITORASG",
365 "EXPR_BITXOR",
366 "EXPR_BITXORASG",
367 "EXPR_CLASS_LITERAL",
368 "EXPR_COND",
369 "EXPR_DEREF",
370 "EXPR_DIV",
371 "EXPR_DIVASG",
372 "EXPR_DOT",
373 "EXPR_EQ",
374 "EXPR_GE",
375 "EXPR_GT",
376 "EXPR_IDENTIFIER",
377 "EXPR_INSTANCEOF",
378 "EXPR_INVOKE",
379 "EXPR_LE",
380 "EXPR_LIST",
381 "EXPR_LITERAL",
382 "EXPR_LSH",
383 "EXPR_LSHASG",
384 "EXPR_LT",
385 "EXPR_MINUS",
386 "EXPR_MINUSASG",
387 "EXPR_MOD",
388 "EXPR_MODASG",
389 "EXPR_MUL",
390 "EXPR_MULASG",
391 "EXPR_NEGATIVE",
392 "EXPR_NEWARRAY",
393 "EXPR_NEWOBJECT",
394 "EXPR_NOT",
395 "EXPR_NOTEQ",
396 "EXPR_OR",
397 "EXPR_POSITIVE",
398 "EXPR_POSTDEC",
399 "EXPR_POSTINC",
400 "EXPR_PREDEC",
401 "EXPR_PREINC",
402 "EXPR_QUALIFIED_SUPER",
403 "EXPR_QUALIFIED_THIS",
404 "EXPR_RSH",
405 "EXPR_RSHASG",
406 "EXPR_TYPE",
407 "EXPR_TYPECAST",
408 "EXPR_URSH",
409 "EXPR_URSHASG",
410 "EXPR_WRAPPER",
411 };
412
413 private static final Class[] EXPR_classes =
414 {
415 InfixExpressionT.class,
416 InfixExpressionT.class,
417 InfixExpressionT.class,
418 AnnotationExpressionT.class,
419 ListExpressionT.class,
420 InfixExpressionT.class,
421 InfixExpressionT.class,
422 InfixExpressionT.class,
423 UnaryExpressionT.class,
424 InfixExpressionT.class,
425 InfixExpressionT.class,
426 InfixExpressionT.class,
427 InfixExpressionT.class,
428 UnaryExpressionT.class,
429 QuestionExpressionT.class,
430 ArrayAccessExpressionT.class,
431 InfixExpressionT.class,
432 InfixExpressionT.class,
433 DotExpressionT.class,
434 InfixExpressionT.class,
435 InfixExpressionT.class,
436 InfixExpressionT.class,
437 IdentifierExpressionT.class,
438 InfixExpressionT.class,
439 MethodCallExpressionT.class,
440 InfixExpressionT.class,
441 ListExpressionT.class,
442 LiteralExpressionT.class,
443 InfixExpressionT.class,
444 InfixExpressionT.class,
445 InfixExpressionT.class,
446 InfixExpressionT.class,
447 InfixExpressionT.class,
448 InfixExpressionT.class,
449 InfixExpressionT.class,
450 InfixExpressionT.class,
451 InfixExpressionT.class,
452 UnaryExpressionT.class,
453 NewArrayExpressionT.class,
454 NewClassExpressionT.class,
455 UnaryExpressionT.class,
456 InfixExpressionT.class,
457 InfixExpressionT.class,
458 UnaryExpressionT.class,
459 UnaryExpressionT.class,
460 UnaryExpressionT.class,
461 UnaryExpressionT.class,
462 UnaryExpressionT.class,
463 UnaryExpressionT.class,
464 UnaryExpressionT.class,
465 InfixExpressionT.class,
466 InfixExpressionT.class,
467 TypeExpressionT.class,
468 TypecastExpressionT.class,
469 InfixExpressionT.class,
470 InfixExpressionT.class,
471 WrapperExpressionT.class,
472 };
473
474 static
475 {
476 for ( int i = EXPR_base; i < EXPR_max; i++ )
477 {
478 final String name = EXPR_names[i - EXPR_base];
479 values.put( name, new ExpressionKind(i, name, EXPR_classes[i-EXPR_base]));
480 }
481 }
482
483 public static ExpressionKind valueOf(int ordinal)
484 {
485 return valueOf(null, EXPR_names[ordinal]);
486 }
487
488 public static ExpressionKind valueOf(Class ignored, String name)
489 {
490 return (ExpressionKind) values.get(name);
491 }
492
493 public static ExpressionKind[] values()
494 {
495 final Set entries = values.entrySet();
496 return (ExpressionKind[])
497 entries.toArray(new ExpressionKind[entries.size()]);
498 }
499
500
501 // ----------------------------------------------------------------------
502 }
503 }