-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathIR.java
967 lines (825 loc) · 26.5 KB
/
IR.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
/*
*
* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Rhino code, released
* May 6, 1999.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 1997-1999
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* John Lenz
*
* Alternatively, the contents of this file may be used under the terms of
* the GNU General Public License Version 2 or later (the "GPL"), in which
* case the provisions of the GPL are applicable instead of those above. If
* you wish to allow use of your version of this file only under the terms of
* the GPL and not to allow others to use your version of this file under the
* MPL, indicate your decision by deleting the provisions above and replacing
* them with the notice and other provisions required by the GPL. If you do
* not delete the provisions above, a recipient may use your version of this
* file under either the MPL or the GPL.
*
* ***** END LICENSE BLOCK ***** */
package com.google.javascript.rhino;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
import static com.google.javascript.jscomp.base.JSCompDoubles.isPositive;
import com.google.common.base.Preconditions;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.List;
import org.jspecify.annotations.Nullable;
/**
* An AST construction helper class
*
* @author [email protected] (John Lenz)
*/
public class IR {
private IR() {}
public static Node empty() {
return new Node(Token.EMPTY);
}
public static Node export(Node declaration) {
return new Node(Token.EXPORT, declaration);
}
public static Node importNode(Node name, Node importSpecs, Node moduleIdentifier) {
checkState(name.isName() || name.isEmpty(), name);
checkState(
importSpecs.isImportSpec() || importSpecs.isImportStar() || importSpecs.isEmpty(),
importSpecs);
checkState(moduleIdentifier.isStringLit(), moduleIdentifier);
return new Node(Token.IMPORT, name, importSpecs, moduleIdentifier);
}
public static Node importStar(String name) {
return Node.newString(Token.IMPORT_STAR, name);
}
public static Node function(Node name, Node params, Node body) {
checkState(name.isName());
checkState(params.isParamList());
checkState(body.isBlock());
return new Node(Token.FUNCTION, name, params, body);
}
public static Node arrowFunction(Node name, Node params, Node body) {
checkState(name.isName());
checkState(params.isParamList());
checkState(body.isBlock() || mayBeExpression(body));
Node func = new Node(Token.FUNCTION, name, params, body);
func.setIsArrowFunction(true);
return func;
}
public static Node paramList(Node... params) {
Node paramList = new Node(Token.PARAM_LIST);
for (Node param : params) {
checkState(param.isName() || param.isRest() || param.isDefaultValue());
paramList.addChildToBack(param);
}
return paramList;
}
public static Node root(Node... rootChildren) {
Node root = new Node(Token.ROOT);
for (Node child : rootChildren) {
checkState(child.getToken() == Token.ROOT || child.getToken() == Token.SCRIPT);
root.addChildToBack(child);
}
return root;
}
public static Node block() {
return new Node(Token.BLOCK);
}
public static Node block(Node stmt) {
checkState(mayBeStatement(stmt), "Block node cannot contain %s", stmt.getToken());
return new Node(Token.BLOCK, stmt);
}
public static Node block(Node... stmts) {
Node block = block();
for (Node stmt : stmts) {
checkState(mayBeStatement(stmt));
block.addChildToBack(stmt);
}
return block;
}
public static Node block(List<Node> stmts) {
Node paramList = block();
for (Node stmt : stmts) {
checkState(mayBeStatement(stmt));
paramList.addChildToBack(stmt);
}
return paramList;
}
private static Node blockUnchecked(Node stmt) {
return new Node(Token.BLOCK, stmt);
}
public static Node script() {
// TODO(johnlenz): finish setting up the SCRIPT node
return new Node(Token.SCRIPT);
}
public static Node script(Node... stmts) {
Node block = script();
for (Node stmt : stmts) {
checkState(mayBeStatementNoReturn(stmt));
block.addChildToBack(stmt);
}
return block;
}
public static Node script(List<Node> stmts) {
Node paramList = script();
for (Node stmt : stmts) {
checkState(mayBeStatementNoReturn(stmt));
paramList.addChildToBack(stmt);
}
return paramList;
}
public static Node var(Node lhs, Node value) {
return declaration(lhs, value, Token.VAR);
}
public static Node var(Node lhs) {
return declaration(lhs, Token.VAR);
}
public static Node let(Node lhs, Node value) {
return declaration(lhs, value, Token.LET);
}
public static Node let(Node lhs) {
return declaration(lhs, Token.LET);
}
public static Node constNode(Node lhs, Node value) {
return declaration(lhs, value, Token.CONST);
}
public static Node declaration(Node lhs, Token type) {
checkState(lhs.isName() || lhs.isDestructuringPattern() || lhs.isDestructuringLhs(), lhs);
if (lhs.isDestructuringPattern()) {
lhs = new Node(Token.DESTRUCTURING_LHS, lhs);
}
return new Node(type, lhs);
}
public static Node declaration(Node lhs, Node value, Token type) {
if (lhs.isName()) {
checkState(!lhs.hasChildren());
} else {
checkState(lhs.isArrayPattern() || lhs.isObjectPattern());
lhs = new Node(Token.DESTRUCTURING_LHS, lhs);
}
Preconditions.checkState(mayBeExpression(value), "%s can't be an expression", value);
lhs.addChildToBack(value);
return new Node(type, lhs);
}
public static Node returnNode() {
return new Node(Token.RETURN);
}
public static Node returnNode(Node expr) {
checkState(mayBeExpression(expr));
return new Node(Token.RETURN, expr);
}
public static Node yieldNode(Node expr) {
checkState(mayBeExpression(expr));
return new Node(Token.YIELD, expr);
}
public static Node await(Node expr) {
checkState(mayBeExpression(expr));
return new Node(Token.AWAIT, expr);
}
public static Node throwNode(Node expr) {
checkState(mayBeExpression(expr));
return new Node(Token.THROW, expr);
}
public static Node exprResult(Node expr) {
checkState(mayBeExpression(expr), expr);
return new Node(Token.EXPR_RESULT, expr);
}
public static Node ifNode(Node cond, Node then) {
checkState(mayBeExpression(cond));
checkState(then.isBlock());
return new Node(Token.IF, cond, then);
}
public static Node ifNode(Node cond, Node then, Node elseNode) {
checkState(mayBeExpression(cond));
checkState(then.isBlock());
checkState(elseNode.isBlock());
return new Node(Token.IF, cond, then, elseNode);
}
public static Node doNode(Node body, Node cond) {
checkState(body.isBlock());
checkState(mayBeExpression(cond));
return new Node(Token.DO, body, cond);
}
public static Node whileNode(Node cond, Node body) {
checkState(body.isBlock());
checkState(mayBeExpression(cond));
return new Node(Token.WHILE, cond, body);
}
public static Node forIn(Node target, Node cond, Node body) {
checkState(target.isVar() || mayBeExpression(target));
checkState(mayBeExpression(cond));
checkState(body.isBlock());
return new Node(Token.FOR_IN, target, cond, body);
}
public static Node forNode(Node init, Node cond, Node incr, Node body) {
checkState(init.isVar() || init.isLet() || init.isConst() || mayBeExpressionOrEmpty(init));
checkState(mayBeExpressionOrEmpty(cond));
checkState(mayBeExpressionOrEmpty(incr));
checkState(body.isBlock());
Node r = new Node(Token.FOR, init, cond, incr);
r.addChildToBack(body);
return r;
}
public static Node switchNode(Node cond, Node... cases) {
checkState(mayBeExpression(cond));
Node switchNode = new Node(Token.SWITCH, cond);
Node switchBody = new Node(Token.SWITCH_BODY);
switchNode.addChildToBack(switchBody);
for (Node caseNode : cases) {
checkState(caseNode.isCase() || caseNode.isDefaultCase());
switchBody.addChildToBack(caseNode);
}
return switchNode;
}
public static Node caseNode(Node expr, Node body) {
checkState(mayBeExpression(expr));
checkState(body.isBlock());
body.setIsAddedBlock(true);
return new Node(Token.CASE, expr, body);
}
public static Node defaultCase(Node body) {
checkState(body.isBlock());
body.setIsAddedBlock(true);
return new Node(Token.DEFAULT_CASE, body);
}
public static Node label(Node name, Node stmt) {
// TODO(johnlenz): additional validation here.
checkState(name.isLabelName());
checkState(mayBeStatement(stmt));
return new Node(Token.LABEL, name, stmt);
}
public static Node labelName(String name) {
checkState(!name.isEmpty());
return Node.newString(Token.LABEL_NAME, name);
}
public static Node tryFinally(Node tryBody, Node finallyBody) {
checkState(tryBody.isBlock());
checkState(finallyBody.isBlock());
Node catchBody = block().srcrefIfMissing(tryBody);
return new Node(Token.TRY, tryBody, catchBody, finallyBody);
}
public static Node tryCatch(Node tryBody, Node catchNode) {
checkState(tryBody.isBlock());
checkState(catchNode.isCatch());
Node catchBody = blockUnchecked(catchNode).srcrefIfMissing(catchNode);
return new Node(Token.TRY, tryBody, catchBody);
}
public static Node tryCatchFinally(Node tryBody, Node catchNode, Node finallyBody) {
checkState(finallyBody.isBlock());
Node tryNode = tryCatch(tryBody, catchNode);
tryNode.addChildToBack(finallyBody);
return tryNode;
}
public static Node catchNode(Node expr, Node body) {
checkState(expr.isName());
checkState(body.isBlock());
return new Node(Token.CATCH, expr, body);
}
public static Node breakNode() {
return new Node(Token.BREAK);
}
public static Node breakNode(Node name) {
// TODO(johnlenz): additional validation here.
checkState(name.isLabelName());
return new Node(Token.BREAK, name);
}
public static Node continueNode() {
return new Node(Token.CONTINUE);
}
public static Node continueNode(Node name) {
// TODO(johnlenz): additional validation here.
checkState(name.isLabelName());
return new Node(Token.CONTINUE, name);
}
public static Node call(Node target, Node... args) {
Node call = new Node(Token.CALL, target);
for (Node arg : args) {
checkState(mayBeExpression(arg) || arg.isSpread(), arg);
call.addChildToBack(arg);
}
return call;
}
public static Node startOptChainCall(Node target, Node... args) {
Node call = new Node(Token.OPTCHAIN_CALL, target);
for (Node arg : args) {
checkState(mayBeExpression(arg) || arg.isSpread(), arg);
call.addChildToBack(arg);
}
call.setIsOptionalChainStart(true);
return call;
}
public static Node continueOptChainCall(Node target, Node... args) {
Node call = new Node(Token.OPTCHAIN_CALL, target);
for (Node arg : args) {
checkState(mayBeExpression(arg) || arg.isSpread(), arg);
call.addChildToBack(arg);
}
call.setIsOptionalChainStart(false);
return call;
}
public static Node newNode(Node target, Node... args) {
Node newcall = new Node(Token.NEW, target);
for (Node arg : args) {
checkState(mayBeExpression(arg) || arg.isSpread(), arg);
newcall.addChildToBack(arg);
}
return newcall;
}
public static Node name(String name) {
Preconditions.checkState(
name.indexOf('.') == -1, "Invalid name '%s'. Did you mean to use NodeUtil.newQName?", name);
return Node.newString(Token.NAME, name);
}
public static Node startOptChainGetprop(Node target, String prop) {
checkState(mayBeExpression(target), target);
Node optChainGetProp = Node.newString(Token.OPTCHAIN_GETPROP, prop);
optChainGetProp.addChildToBack(target);
optChainGetProp.setIsOptionalChainStart(true);
return optChainGetProp;
}
public static Node continueOptChainGetprop(Node target, String prop) {
checkState(mayBeExpression(target), target);
Node optChainGetProp = Node.newString(Token.OPTCHAIN_GETPROP, prop);
optChainGetProp.addChildToBack(target);
optChainGetProp.setIsOptionalChainStart(false);
return optChainGetProp;
}
public static Node getprop(Node target, String prop) {
checkState(mayBeExpression(target));
Node getprop = Node.newString(Token.GETPROP, prop);
getprop.addChildToBack(target);
return getprop;
}
public static Node getprop(Node target, String prop, String... moreProps) {
checkState(mayBeExpression(target));
Node result = IR.getprop(target, prop);
for (String moreProp : moreProps) {
result = IR.getprop(result, moreProp);
}
return result;
}
public static Node startOptChainGetelem(Node target, Node elem) {
checkState(mayBeExpression(target), target);
checkState(mayBeExpression(elem), elem);
Node optChainGetElem = new Node(Token.OPTCHAIN_GETELEM, target, elem);
optChainGetElem.setIsOptionalChainStart(true);
return optChainGetElem;
}
public static Node continueOptChainGetelem(Node target, Node elem) {
checkState(mayBeExpression(target), target);
checkState(mayBeExpression(elem), elem);
Node optChainGetElem = new Node(Token.OPTCHAIN_GETELEM, target, elem);
optChainGetElem.setIsOptionalChainStart(false);
return optChainGetElem;
}
public static Node getelem(Node target, Node elem) {
checkState(mayBeExpression(target));
checkState(mayBeExpression(elem));
return new Node(Token.GETELEM, target, elem);
}
public static Node delprop(Node target) {
checkState(mayBeExpression(target));
return new Node(Token.DELPROP, target);
}
public static Node assign(Node target, Node expr) {
checkState(target.isValidAssignmentTarget(), target);
checkState(mayBeExpression(expr), expr);
return new Node(Token.ASSIGN, target, expr);
}
public static Node hook(Node cond, Node trueval, Node falseval) {
checkState(mayBeExpression(cond));
checkState(mayBeExpression(trueval));
checkState(mayBeExpression(falseval));
return new Node(Token.HOOK, cond, trueval, falseval);
}
public static Node in(Node expr1, Node expr2) {
return binaryOp(Token.IN, expr1, expr2);
}
public static Node comma(Node expr1, Node expr2) {
return binaryOp(Token.COMMA, expr1, expr2);
}
public static Node and(Node expr1, Node expr2) {
return binaryOp(Token.AND, expr1, expr2);
}
public static Node or(Node expr1, Node expr2) {
return binaryOp(Token.OR, expr1, expr2);
}
public static Node coalesce(Node expr1, Node expr2) {
return binaryOp(Token.COALESCE, expr1, expr2);
}
public static Node not(Node expr1) {
return unaryOp(Token.NOT, expr1);
}
/** "<" */
public static Node lt(Node expr1, Node expr2) {
return binaryOp(Token.LT, expr1, expr2);
}
/** ">=" */
public static Node ge(Node expr1, Node expr2) {
return binaryOp(Token.GE, expr1, expr2);
}
/** "==" */
public static Node eq(Node expr1, Node expr2) {
return binaryOp(Token.EQ, expr1, expr2);
}
/** "!=" */
public static Node ne(Node expr1, Node expr2) {
return binaryOp(Token.NE, expr1, expr2);
}
/** "===" */
public static Node sheq(Node expr1, Node expr2) {
return binaryOp(Token.SHEQ, expr1, expr2);
}
/** "!==" */
public static Node shne(Node expr1, Node expr2) {
return binaryOp(Token.SHNE, expr1, expr2);
}
public static Node voidNode(Node expr1) {
return unaryOp(Token.VOID, expr1);
}
public static Node neg(Node expr1) {
return unaryOp(Token.NEG, expr1);
}
public static Node pos(Node expr1) {
return unaryOp(Token.POS, expr1);
}
public static Node cast(Node expr1, JSDocInfo jsdoc) {
Node op = unaryOp(Token.CAST, expr1);
op.setJSDocInfo(jsdoc);
return op;
}
public static Node inc(Node exp, boolean isPost) {
Node op = unaryOp(Token.INC, exp);
op.putBooleanProp(Node.INCRDECR_PROP, isPost);
return op;
}
public static Node dec(Node exp, boolean isPost) {
Node op = unaryOp(Token.DEC, exp);
op.putBooleanProp(Node.INCRDECR_PROP, isPost);
return op;
}
public static Node add(Node expr1, Node expr2) {
return binaryOp(Token.ADD, expr1, expr2);
}
public static Node sub(Node expr1, Node expr2) {
return binaryOp(Token.SUB, expr1, expr2);
}
/** "||=" */
public static Node assignOr(Node expr1, Node expr2) {
return binaryOp(Token.ASSIGN_OR, expr1, expr2);
}
/** "&&=" */
public static Node assignAnd(Node expr1, Node expr2) {
return binaryOp(Token.ASSIGN_AND, expr1, expr2);
}
/** "??=" */
public static Node assignCoalesce(Node expr1, Node expr2) {
return binaryOp(Token.ASSIGN_COALESCE, expr1, expr2);
}
/** "&" */
public static Node bitwiseAnd(Node expr1, Node expr2) {
return binaryOp(Token.BITAND, expr1, expr2);
}
/** ">>" */
public static Node rightShift(Node expr1, Node expr2) {
return binaryOp(Token.RSH, expr1, expr2);
}
// TODO(johnlenz): the rest of the ops
// literals
public static Node objectlit(Node... propdefs) {
Node objectlit = new Node(Token.OBJECTLIT);
for (Node propdef : propdefs) {
switch (propdef.getToken()) {
case STRING_KEY:
case MEMBER_FUNCTION_DEF:
case GETTER_DEF:
case SETTER_DEF:
case OBJECT_SPREAD:
case COMPUTED_PROP:
break;
default:
throw new IllegalStateException("Unexpected OBJECTLIT child: " + propdef);
}
objectlit.addChildToBack(propdef);
}
return objectlit;
}
public static Node objectPattern(Node... keys) {
Node objectPattern = new Node(Token.OBJECT_PATTERN);
for (Node key : keys) {
checkState(key.isStringKey() || key.isComputedProp() || key.isRest());
objectPattern.addChildToBack(key);
}
return objectPattern;
}
public static Node arrayPattern(Node... keys) {
Node arrayPattern = new Node(Token.ARRAY_PATTERN);
for (Node key : keys) {
checkState(key.isRest() || key.isValidAssignmentTarget());
arrayPattern.addChildToBack(key);
}
return arrayPattern;
}
public static Node computedProp(Node key, Node value) {
checkState(mayBeExpression(key), key);
checkState(mayBeExpression(value), value);
return new Node(Token.COMPUTED_PROP, key, value);
}
public static Node propdef(Node string, Node value) {
checkState(string.isStringKey());
checkState(!string.hasChildren());
checkState(mayBeExpression(value));
string.addChildToFront(value);
return string;
}
public static Node arraylit(Node... exprs) {
return arraylit(Arrays.asList(exprs));
}
public static Node arraylit(Iterable<Node> exprs) {
Node arraylit = new Node(Token.ARRAYLIT);
for (Node expr : exprs) {
checkState(mayBeExpressionOrEmpty(expr) || expr.isSpread(), expr);
arraylit.addChildToBack(expr);
}
return arraylit;
}
public static Node regexp(Node expr) {
checkState(expr.isStringLit());
return new Node(Token.REGEXP, expr);
}
public static Node regexp(Node expr, Node flags) {
checkState(expr.isStringLit());
checkState(flags.isStringLit());
return new Node(Token.REGEXP, expr, flags);
}
public static Node string(String s) {
return Node.newString(s);
}
public static Node stringKey(String s) {
return Node.newString(Token.STRING_KEY, s);
}
public static Node stringKey(String s, Node value) {
checkState(mayBeExpression(value) || value.isDefaultValue() || value.isObjectPattern());
Node stringKey = stringKey(s);
stringKey.addChildToFront(value);
return stringKey;
}
public static Node quotedStringKey(String s, Node value) {
Node k = stringKey(s, value);
k.putBooleanProp(Node.QUOTED_PROP, true);
return k;
}
public static Node templateLiteral() {
return new Node(Token.TEMPLATELIT);
}
public static Node templateLiteralString(@Nullable String cooked, String raw) {
return Node.newTemplateLitString(cooked, raw);
}
public static Node templateLiteralSubstitution(Node child) {
var sub = new Node(Token.TEMPLATELIT_SUB);
sub.addChildToBack(child);
return sub;
}
public static Node iterRest(Node target) {
checkState(target.isValidAssignmentTarget(), target);
return new Node(Token.ITER_REST, target);
}
public static Node objectRest(Node target) {
checkState(target.isValidAssignmentTarget(), target);
return new Node(Token.OBJECT_REST, target);
}
public static Node iterSpread(Node expr) {
checkState(mayBeExpression(expr));
return new Node(Token.ITER_SPREAD, expr);
}
public static Node objectSpread(Node expr) {
checkState(mayBeExpression(expr));
return new Node(Token.OBJECT_SPREAD, expr);
}
public static Node superNode() {
return new Node(Token.SUPER);
}
public static Node getterDef(String name, Node value) {
checkState(value.isFunction());
Node member = Node.newString(Token.GETTER_DEF, name);
member.addChildToFront(value);
return member;
}
public static Node setterDef(String name, Node value) {
checkState(value.isFunction());
Node member = Node.newString(Token.SETTER_DEF, name);
member.addChildToFront(value);
return member;
}
public static Node memberFieldDef(String name, Node value) {
checkState(mayBeExpression(value));
Node member = Node.newString(Token.MEMBER_FIELD_DEF, name);
member.addChildToFront(value);
return member;
}
public static Node memberFunctionDef(String name, Node function) {
checkState(function.isFunction());
Node member = Node.newString(Token.MEMBER_FUNCTION_DEF, name);
member.addChildToBack(function);
return member;
}
public static Node number(double d) {
checkState(!Double.isNaN(d), d);
checkState(isPositive(d), d);
return Node.newNumber(d);
}
public static Node bigint(BigInteger b) {
checkNotNull(b);
checkState(b.signum() >= 0, b);
return Node.newBigInt(b);
}
public static Node thisNode() {
return new Node(Token.THIS);
}
public static Node trueNode() {
return new Node(Token.TRUE);
}
public static Node falseNode() {
return new Node(Token.FALSE);
}
public static Node nullNode() {
return new Node(Token.NULL);
}
public static Node typeof(Node expr) {
return unaryOp(Token.TYPEOF, expr);
}
public static Node importMeta() {
return new Node(Token.IMPORT_META);
}
// helper methods
private static Node binaryOp(Token token, Node expr1, Node expr2) {
checkState(mayBeExpression(expr1), expr1);
checkState(mayBeExpression(expr2), expr2);
return new Node(token, expr1, expr2);
}
private static Node unaryOp(Token token, Node expr) {
checkState(mayBeExpression(expr));
return new Node(token, expr);
}
private static boolean mayBeExpressionOrEmpty(Node n) {
return n.isEmpty() || mayBeExpression(n);
}
// NOTE: some nodes are neither statements nor expression nodes:
// SCRIPT, LABEL_NAME, PARAM_LIST, CASE, DEFAULT_CASE, CATCH
// GETTER_DEF, SETTER_DEF
/**
* It isn't possible to always determine if a detached node is a expression, so make a best guess.
*/
private static boolean mayBeStatementNoReturn(Node n) {
switch (n.getToken()) {
case EMPTY:
case FUNCTION:
// EMPTY and FUNCTION are used both in expression and statement
// contexts
return true;
case BLOCK:
case BREAK:
case CLASS:
case CONST:
case CONTINUE:
case DEBUGGER:
case DO:
case EXPR_RESULT:
case FOR:
case FOR_IN:
case FOR_OF:
case FOR_AWAIT_OF:
case IF:
case LABEL:
case LET:
case SWITCH:
case THROW:
case TRY:
case VAR:
case WHILE:
case WITH:
return true;
default:
return false;
}
}
/**
* It isn't possible to always determine if a detached node is a expression, so make a best guess.
*/
public static boolean mayBeStatement(Node n) {
if (!mayBeStatementNoReturn(n)) {
return n.isReturn();
}
return true;
}
/**
* It isn't possible to always determine if a detached node is a expression, so make a best guess.
*/
public static boolean mayBeExpression(Node n) {
switch (n.getToken()) {
case FUNCTION:
case CLASS:
// FUNCTION and CLASS are used both in expression and statement
// contexts.
return true;
case ADD:
case AND:
case ARRAYLIT:
case ASSIGN:
case ASSIGN_BITOR:
case ASSIGN_BITXOR:
case ASSIGN_BITAND:
case ASSIGN_LSH:
case ASSIGN_RSH:
case ASSIGN_URSH:
case ASSIGN_ADD:
case ASSIGN_SUB:
case ASSIGN_MUL:
case ASSIGN_EXPONENT:
case ASSIGN_DIV:
case ASSIGN_MOD:
case ASSIGN_OR:
case ASSIGN_AND:
case ASSIGN_COALESCE:
case AWAIT:
case BIGINT:
case BITAND:
case BITOR:
case BITNOT:
case BITXOR:
case CALL:
case CAST:
case COALESCE:
case COMMA:
case DEC:
case DELPROP:
case DIV:
case DYNAMIC_IMPORT:
case EQ:
case EXPONENT:
case FALSE:
case GE:
case GETPROP:
case GETELEM:
case GT:
case HOOK:
case IMPORT_META:
case IN:
case INC:
case INSTANCEOF:
case LE:
case LSH:
case LT:
case MOD:
case MUL:
case NAME:
case NE:
case NEG:
case NEW:
case NEW_TARGET:
case NOT:
case NUMBER:
case NULL:
case OBJECTLIT:
case OPTCHAIN_CALL:
case OPTCHAIN_GETELEM:
case OPTCHAIN_GETPROP:
case OR:
case POS:
case REGEXP:
case RSH:
case SHEQ:
case SHNE:
case STRINGLIT:
case SUB:
case SUPER:
case TEMPLATELIT:
case TAGGED_TEMPLATELIT:
case THIS:
case TYPEOF:
case TRUE:
case URSH:
case VOID:
case YIELD:
return true;
default:
return false;
}
}
}