Debian Codegraph @

src/com/google/javascript/rhino/IR.java

raw symbols SCIP
1 /*
2 *
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0
5 *
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
10 *
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
15 *
16 * The Original Code is Rhino code, released
17 * May 6, 1999.
18 *
19 * The Initial Developer of the Original Code is
20 * Netscape Communications Corporation.
21 * Portions created by the Initial Developer are Copyright (C) 1997-1999
22 * the Initial Developer. All Rights Reserved.
23 *
24 * Contributor(s):
25 * John Lenz
26 *
27 * Alternatively, the contents of this file may be used under the terms of
28 * the GNU General Public License Version 2 or later (the "GPL"), in which
29 * case the provisions of the GPL are applicable instead of those above. If
30 * you wish to allow use of your version of this file only under the terms of
31 * the GPL and not to allow others to use your version of this file under the
32 * MPL, indicate your decision by deleting the provisions above and replacing
33 * them with the notice and other provisions required by the GPL. If you do
34 * not delete the provisions above, a recipient may use your version of this
35 * file under either the MPL or the GPL.
36 *
37 * ***** END LICENSE BLOCK ***** */
38
39 package com.google.javascript.rhino;
40
41 import com.google.common.base.Preconditions;
42
43 import java.util.List;
44
45 /**
46 * An AST construction helper class
47 * @author [email protected] (John Lenz)
48 */
49 public class IR {
50
51 private IR() {}
52
53 public static Node empty() {
54 return new Node(Token.EMPTY);
55 }
56
57 public static Node function(Node name, Node params, Node body) {
58 Preconditions.checkState(name.isName());
59 Preconditions.checkState(params.isParamList());
60 Preconditions.checkState(body.isBlock());
61 return new Node(Token.FUNCTION, name, params, body);
62 }
63
64 public static Node paramList() {
65 return new Node(Token.PARAM_LIST);
66 }
67
68 public static Node paramList(Node param) {
69 Preconditions.checkState(param.isName());
70 return new Node(Token.PARAM_LIST, param);
71 }
72
73 public static Node paramList(Node ... params) {
74 Node paramList = paramList();
75 for (Node param : params) {
76 Preconditions.checkState(param.isName());
77 paramList.addChildToBack(param);
78 }
79 return paramList;
80 }
81
82 public static Node paramList(List<Node> params) {
83 Node paramList = paramList();
84 for (Node param : params) {
85 Preconditions.checkState(param.isName());
86 paramList.addChildToBack(param);
87 }
88 return paramList;
89 }
90
91 public static Node block() {
92 Node block = new Node(Token.BLOCK);
93 return block;
94 }
95
96 public static Node block(Node stmt) {
97 Preconditions.checkState(mayBeStatement(stmt));
98 Node block = new Node(Token.BLOCK, stmt);
99 return block;
100 }
101
102 public static Node block(Node ... stmts) {
103 Node block = block();
104 for (Node stmt : stmts) {
105 Preconditions.checkState(mayBeStatement(stmt));
106 block.addChildToBack(stmt);
107 }
108 return block;
109 }
110
111 public static Node block(List<Node> stmts) {
112 Node paramList = block();
113 for (Node stmt : stmts) {
114 Preconditions.checkState(mayBeStatement(stmt));
115 paramList.addChildToBack(stmt);
116 }
117 return paramList;
118 }
119
120 private static Node blockUnchecked(Node stmt) {
121 return new Node(Token.BLOCK, stmt);
122 }
123
124 public static Node script() {
125 // TODO(johnlenz): finish setting up the SCRIPT node
126 Node block = new Node(Token.SCRIPT);
127 return block;
128 }
129
130 public static Node script(Node ... stmts) {
131 Node block = script();
132 for (Node stmt : stmts) {
133 Preconditions.checkState(mayBeStatementNoReturn(stmt));
134 block.addChildToBack(stmt);
135 }
136 return block;
137 }
138
139 public static Node script(List<Node> stmts) {
140 Node paramList = script();
141 for (Node stmt : stmts) {
142 Preconditions.checkState(mayBeStatementNoReturn(stmt));
143 paramList.addChildToBack(stmt);
144 }
145 return paramList;
146 }
147
148 public static Node var(Node name, Node value) {
149 Preconditions.checkState(name.isName() && !name.hasChildren());
150 Preconditions.checkState(mayBeExpression(value));
151 name.addChildToFront(value);
152 return var(name);
153 }
154
155 public static Node var(Node name) {
156 Preconditions.checkState(name.isName());
157 return new Node(Token.VAR, name);
158 }
159
160 public static Node returnNode() {
161 return new Node(Token.RETURN);
162 }
163
164 public static Node returnNode(Node expr) {
165 Preconditions.checkState(mayBeExpression(expr));
166 return new Node(Token.RETURN, expr);
167 }
168
169 public static Node throwNode(Node expr) {
170 Preconditions.checkState(mayBeExpression(expr));
171 return new Node(Token.THROW, expr);
172 }
173
174 public static Node exprResult(Node expr) {
175 Preconditions.checkState(mayBeExpression(expr));
176 return new Node(Token.EXPR_RESULT, expr);
177 }
178
179 public static Node ifNode(Node cond, Node then) {
180 Preconditions.checkState(mayBeExpression(cond));
181 Preconditions.checkState(then.isBlock());
182 return new Node(Token.IF, cond, then);
183 }
184
185 public static Node ifNode(Node cond, Node then, Node elseNode) {
186 Preconditions.checkState(mayBeExpression(cond));
187 Preconditions.checkState(then.isBlock());
188 Preconditions.checkState(elseNode.isBlock());
189 return new Node(Token.IF, cond, then, elseNode);
190 }
191
192 public static Node doNode(Node body, Node cond) {
193 Preconditions.checkState(body.isBlock());
194 Preconditions.checkState(mayBeExpression(cond));
195 return new Node(Token.DO, body, cond);
196 }
197
198 public static Node forIn(Node target, Node cond, Node body) {
199 Preconditions.checkState(target.isVar() || mayBeExpression(target));
200 Preconditions.checkState(mayBeExpression(cond));
201 Preconditions.checkState(body.isBlock());
202 return new Node(Token.FOR, target, cond, body);
203 }
204
205 public static Node forNode(Node init, Node cond, Node incr, Node body) {
206 Preconditions.checkState(init.isVar() || mayBeExpressionOrEmpty(init));
207 Preconditions.checkState(mayBeExpressionOrEmpty(cond));
208 Preconditions.checkState(mayBeExpressionOrEmpty(incr));
209 Preconditions.checkState(body.isBlock());
210 return new Node(Token.FOR, init, cond, incr, body);
211 }
212
213 public static Node switchNode(Node cond, Node ... cases) {
214 Preconditions.checkState(mayBeExpression(cond));
215 Node switchNode = new Node(Token.SWITCH, cond);
216 for (Node caseNode : cases) {
217 Preconditions.checkState(caseNode.isCase() || caseNode.isDefaultCase());
218 switchNode.addChildToBack(caseNode);
219 }
220 return switchNode;
221 }
222
223 public static Node caseNode(Node expr, Node body) {
224 Preconditions.checkState(mayBeExpression(expr));
225 Preconditions.checkState(body.isBlock());
226 body.putBooleanProp(Node.SYNTHETIC_BLOCK_PROP, true);
227 return new Node(Token.CASE, expr, body);
228 }
229
230 public static Node defaultCase(Node body) {
231 Preconditions.checkState(body.isBlock());
232 body.putBooleanProp(Node.SYNTHETIC_BLOCK_PROP, true);
233 return new Node(Token.DEFAULT_CASE, body);
234 }
235
236 public static Node label(Node name, Node stmt) {
237 // TODO(johnlenz): additional validation here.
238 Preconditions.checkState(name.isLabelName());
239 Preconditions.checkState(mayBeStatement(stmt));
240 Node block = new Node(Token.LABEL, name, stmt);
241 return block;
242 }
243
244 public static Node labelName(String name) {
245 Preconditions.checkState(!name.isEmpty());
246 return Node.newString(Token.LABEL_NAME, name);
247 }
248
249 public static Node tryFinally(Node tryBody, Node finallyBody) {
250 Preconditions.checkState(tryBody.isBlock());
251 Preconditions.checkState(finallyBody.isBlock());
252 Node catchBody = block().copyInformationFrom(tryBody);
253 return new Node(Token.TRY, tryBody, catchBody, finallyBody);
254 }
255
256 public static Node tryCatch(Node tryBody, Node catchNode) {
257 Preconditions.checkState(tryBody.isBlock());
258 Preconditions.checkState(catchNode.isCatch());
259 Node catchBody = blockUnchecked(catchNode).copyInformationFrom(catchNode);
260 return new Node(Token.TRY, tryBody, catchBody);
261 }
262
263 public static Node tryCatchFinally(
264 Node tryBody, Node catchNode, Node finallyBody) {
265 Preconditions.checkState(finallyBody.isBlock());
266 Node tryNode = tryCatch(tryBody, catchNode);
267 tryNode.addChildToBack(finallyBody);
268 return tryNode;
269 }
270
271 public static Node catchNode(Node expr, Node body) {
272 Preconditions.checkState(expr.isName());
273 Preconditions.checkState(body.isBlock());
274 return new Node(Token.CATCH, expr, body);
275 }
276
277 public static Node breakNode() {
278 return new Node(Token.BREAK);
279 }
280
281 public static Node breakNode(Node name) {
282 // TODO(johnlenz): additional validation here.
283 Preconditions.checkState(name.isLabelName());
284 return new Node(Token.BREAK, name);
285 }
286
287 public static Node continueNode() {
288 return new Node(Token.CONTINUE);
289 }
290
291 public static Node continueNode(Node name) {
292 // TODO(johnlenz): additional validation here.
293 Preconditions.checkState(name.isLabelName());
294 return new Node(Token.CONTINUE, name);
295 }
296
297
298 //
299
300 public static Node call(Node target, Node ... args) {
301 Node call = new Node(Token.CALL, target);
302 for (Node arg : args) {
303 Preconditions.checkState(mayBeExpression(arg));
304 call.addChildToBack(arg);
305 }
306 return call;
307 }
308
309 public static Node newNode(Node target, Node ... args) {
310 Node newcall = new Node(Token.NEW, target);
311 for (Node arg : args) {
312 Preconditions.checkState(mayBeExpression(arg));
313 newcall.addChildToBack(arg);
314 }
315 return newcall;
316 }
317
318 public static Node name(String name) {
319 return Node.newString(Token.NAME, name);
320 }
321
322 public static Node getprop(Node target, Node prop) {
323 Preconditions.checkState(mayBeExpression(target));
324 Preconditions.checkState(prop.isString());
325 return new Node(Token.GETPROP, target, prop);
326 }
327
328 public static Node getelem(Node target, Node elem) {
329 Preconditions.checkState(mayBeExpression(target));
330 Preconditions.checkState(mayBeExpression(elem));
331 return new Node(Token.GETELEM, target, elem);
332 }
333
334 public static Node assign(Node target, Node expr) {
335 Preconditions.checkState(isAssignmentTarget(target));
336 Preconditions.checkState(mayBeExpression(expr));
337 return new Node(Token.ASSIGN, target, expr);
338 }
339
340 public static Node hook(Node cond, Node trueval, Node falseval) {
341 Preconditions.checkState(mayBeExpression(cond));
342 Preconditions.checkState(mayBeExpression(trueval));
343 Preconditions.checkState(mayBeExpression(falseval));
344 return new Node(Token.HOOK, cond, trueval, falseval);
345 }
346
347 public static Node comma(Node expr1, Node expr2) {
348 return binaryOp(Token.COMMA, expr1, expr2);
349 }
350
351 public static Node and(Node expr1, Node expr2) {
352 return binaryOp(Token.AND, expr1, expr2);
353 }
354
355 public static Node or(Node expr1, Node expr2) {
356 return binaryOp(Token.OR, expr1, expr2);
357 }
358
359 public static Node not(Node expr1) {
360 return unaryOp(Token.NOT, expr1);
361 }
362
363 /**
364 * "=="
365 */
366 public static Node eq(Node expr1, Node expr2) {
367 return binaryOp(Token.EQ, expr1, expr2);
368 }
369
370 /**
371 * "==="
372 */
373 public static Node sheq(Node expr1, Node expr2) {
374 return binaryOp(Token.SHEQ, expr1, expr2);
375 }
376
377 public static Node voidNode(Node expr1) {
378 return unaryOp(Token.VOID, expr1);
379 }
380
381 public static Node neg(Node expr1) {
382 return unaryOp(Token.NEG, expr1);
383 }
384
385 public static Node pos(Node expr1) {
386 return unaryOp(Token.POS, expr1);
387 }
388
389 public static Node cast(Node expr1) {
390 return unaryOp(Token.CAST, expr1);
391 }
392
393 public static Node add(Node expr1, Node expr2) {
394 return binaryOp(Token.ADD, expr1, expr2);
395 }
396
397 public static Node sub(Node expr1, Node expr2) {
398 return binaryOp(Token.SUB, expr1, expr2);
399 }
400
401 // TODO(johnlenz): the rest of the ops
402
403 // literals
404 public static Node objectlit(Node ... propdefs) {
405 Node objectlit = new Node(Token.OBJECTLIT);
406 for (Node propdef : propdefs) {
407 Preconditions.checkState(
408 propdef.isStringKey() ||
409 propdef.isGetterDef() || propdef.isSetterDef());
410 Preconditions.checkState(propdef.hasOneChild());
411 objectlit.addChildToBack(propdef);
412 }
413 return objectlit;
414 }
415
416 // TODO(johnlenz): quoted props
417
418 public static Node propdef(Node string, Node value) {
419 Preconditions.checkState(string.isStringKey());
420 Preconditions.checkState(!string.hasChildren());
421 Preconditions.checkState(mayBeExpression(value));
422 string.addChildToFront(value);
423 return string;
424 }
425
426 public static Node arraylit(Node ... exprs) {
427 Node arraylit = new Node(Token.ARRAYLIT);
428 for (Node expr : exprs) {
429 Preconditions.checkState(mayBeExpressionOrEmpty(expr));
430 arraylit.addChildToBack(expr);
431 }
432 return arraylit;
433 }
434
435 public static Node regexp(Node expr) {
436 Preconditions.checkState(expr.isString());
437 return new Node(Token.REGEXP, expr);
438 }
439
440 public static Node regexp(Node expr, Node flags) {
441 Preconditions.checkState(expr.isString());
442 Preconditions.checkState(flags.isString());
443 return new Node(Token.REGEXP, expr, flags);
444 }
445
446 public static Node string(String s) {
447 return Node.newString(s);
448 }
449
450 public static Node stringKey(String s) {
451 return Node.newString(Token.STRING_KEY, s);
452 }
453
454 public static Node number(double d) {
455 return Node.newNumber(d);
456 }
457
458 public static Node thisNode() {
459 return new Node(Token.THIS);
460 }
461
462 public static Node trueNode() {
463 return new Node(Token.TRUE);
464 }
465
466 public static Node falseNode() {
467 return new Node(Token.FALSE);
468 }
469
470 public static Node nullNode() {
471 return new Node(Token.NULL);
472 }
473
474 // helper methods
475
476 private static Node binaryOp(int token, Node expr1, Node expr2) {
477 Preconditions.checkState(mayBeExpression(expr1));
478 Preconditions.checkState(mayBeExpression(expr2));
479 return new Node(token, expr1, expr2);
480 }
481
482 private static Node unaryOp(int token, Node expr) {
483 Preconditions.checkState(mayBeExpression(expr));
484 return new Node(token, expr);
485 }
486
487 private static boolean mayBeExpressionOrEmpty(Node n) {
488 return n.isEmpty() || mayBeExpression(n);
489 }
490
491 private static boolean isAssignmentTarget(Node n) {
492 return n.isName() || n.isGetProp() || n.isGetElem();
493 }
494
495 // NOTE: some nodes are neither statements nor expression nodes:
496 // SCRIPT, LABEL_NAME, PARAM_LIST, CASE, DEFAULT_CASE, CATCH
497 // GETTER_DEF, SETTER_DEF
498
499 /**
500 * It isn't possible to always determine if a detached node is a expression,
501 * so make a best guess.
502 */
503 private static boolean mayBeStatementNoReturn(Node n) {
504 switch (n.getType()) {
505 case Token.EMPTY:
506 case Token.FUNCTION:
507 // EMPTY and FUNCTION are used both in expression and statement
508 // contexts
509 return true;
510
511 case Token.BLOCK:
512 case Token.BREAK:
513 case Token.CONST:
514 case Token.CONTINUE:
515 case Token.DEBUGGER:
516 case Token.DO:
517 case Token.EXPR_RESULT:
518 case Token.FOR:
519 case Token.IF:
520 case Token.LABEL:
521 case Token.SWITCH:
522 case Token.THROW:
523 case Token.TRY:
524 case Token.VAR:
525 case Token.WHILE:
526 case Token.WITH:
527 return true;
528
529 default:
530 return false;
531 }
532 }
533
534 /**
535 * It isn't possible to always determine if a detached node is a expression,
536 * so make a best guess.
537 */
538 private static boolean mayBeStatement(Node n) {
539 if (!mayBeStatementNoReturn(n)) {
540 return n.isReturn();
541 }
542 return true;
543 }
544
545 /**
546 * It isn't possible to always determine if a detached node is a expression,
547 * so make a best guess.
548 */
549 private static boolean mayBeExpression(Node n) {
550 switch (n.getType()) {
551 case Token.FUNCTION:
552 // FUNCTION is used both in expression and statement
553 // contexts.
554 return true;
555
556 case Token.ADD:
557 case Token.AND:
558 case Token.ARRAYLIT:
559 case Token.ASSIGN:
560 case Token.ASSIGN_BITOR:
561 case Token.ASSIGN_BITXOR:
562 case Token.ASSIGN_BITAND:
563 case Token.ASSIGN_LSH:
564 case Token.ASSIGN_RSH:
565 case Token.ASSIGN_URSH:
566 case Token.ASSIGN_ADD:
567 case Token.ASSIGN_SUB:
568 case Token.ASSIGN_MUL:
569 case Token.ASSIGN_DIV:
570 case Token.ASSIGN_MOD:
571 case Token.BITAND:
572 case Token.BITOR:
573 case Token.BITNOT:
574 case Token.BITXOR:
575 case Token.CALL:
576 case Token.CAST:
577 case Token.COMMA:
578 case Token.DEC:
579 case Token.DELPROP:
580 case Token.DIV:
581 case Token.EQ:
582 case Token.FALSE:
583 case Token.GE:
584 case Token.GETPROP:
585 case Token.GETELEM:
586 case Token.GT:
587 case Token.HOOK:
588 case Token.IN:
589 case Token.INC:
590 case Token.INSTANCEOF:
591 case Token.LE:
592 case Token.LSH:
593 case Token.LT:
594 case Token.MOD:
595 case Token.MUL:
596 case Token.NAME:
597 case Token.NE:
598 case Token.NEG:
599 case Token.NEW:
600 case Token.NOT:
601 case Token.NUMBER:
602 case Token.NULL:
603 case Token.OBJECTLIT:
604 case Token.OR:
605 case Token.POS:
606 case Token.REGEXP:
607 case Token.RSH:
608 case Token.SHEQ:
609 case Token.SHNE:
610 case Token.STRING:
611 case Token.SUB:
612 case Token.THIS:
613 case Token.TYPEOF:
614 case Token.TRUE:
615 case Token.URSH:
616 case Token.VOID:
617 return true;
618
619 default:
620 return false;
621 }
622 }
623 }