-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Node.java
2974 lines (2557 loc) · 82.8 KB
/
Node.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
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
*
* ***** 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):
* Norris Boyd
* Roger Lawrence
* Mike McCabe
*
* 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 com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Objects;
import com.google.common.base.Preconditions;
import com.google.javascript.rhino.jstype.JSType;
import java.io.IOException;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Set;
import javax.annotation.Nullable;
/**
* This class implements the root of the intermediate representation.
*
*/
public class Node implements Serializable {
private static final long serialVersionUID = 1L;
public static final int
JSDOC_INFO_PROP = 29, // contains a JSDocInfo object
VAR_ARGS_NAME = 30, // the name node is a variable length
// argument placeholder.
INCRDECR_PROP = 32, // whether incrdecr is pre (false) or post (true)
QUOTED_PROP = 36, // set to indicate a quoted object lit key
OPT_ARG_NAME = 37, // The name node is an optional argument.
SYNTHETIC_BLOCK_PROP = 38, // A synthetic block. Used to make
// processing simpler, and does not
// represent a real block in the source.
ADDED_BLOCK = 39, // Used to indicate BLOCK that is added
ORIGINALNAME_PROP = 40, // The original name of the node, before
// renaming.
SIDE_EFFECT_FLAGS = 42, // Function or constructor call side effect
// flags
// Coding convention props
IS_CONSTANT_NAME = 43, // The variable or property is constant.
IS_NAMESPACE = 46, // The variable creates a namespace.
DIRECTIVES = 48, // The ES5 directives on this node.
DIRECT_EVAL = 49, // ES5 distinguishes between direct and
// indirect calls to eval.
FREE_CALL = 50, // A CALL without an explicit "this" value.
STATIC_SOURCE_FILE = 51, // A StaticSourceFile indicating the file
// where this node lives.
LENGTH = 52, // The length of the code represented by
// this node.
INPUT_ID = 53, // The id of the input associated with this
// node.
SLASH_V = 54, // Whether a STRING node contains a \v
// vertical tab escape. This is a total hack.
// See comments in IRFactory about this.
INFERRED_FUNCTION = 55, // Marks a function whose parameter types
// have been inferred.
CHANGE_TIME = 56, // For passes that work only on changed funs.
REFLECTED_OBJECT = 57, // An object that's used for goog.object.reflect-style reflection.
STATIC_MEMBER = 58, // Set if class member definition is static
GENERATOR_FN = 59, // Set if the node is a Generator function or
// member method.
ARROW_FN = 60,
ASYNC_FN = 61, // http://tc39.github.io/ecmascript-asyncawait/
YIELD_FOR = 62, // Set if a yield is a "yield all"
EXPORT_DEFAULT = 63, // Set if a export is a "default" export
EXPORT_ALL_FROM = 64, // Set if an export is a "*"
IS_CONSTANT_VAR = 65, // A lexical variable is inferred const
GENERATOR_MARKER = 66, // Used by the ES6-to-ES3 translator.
GENERATOR_SAFE = 67, // Used by the ES6-to-ES3 translator.
RAW_STRING_VALUE = 71, // Used to support ES6 tagged template literal.
COMPUTED_PROP_METHOD = 72, // A computed property that has the method
// syntax ( [prop]() {...} ) rather than the
// property definition syntax ( [prop]: value ).
COMPUTED_PROP_GETTER = 73, // A computed property in a getter, e.g.
// var obj = { get [prop]() {...} };
COMPUTED_PROP_SETTER = 74, // A computed property in a setter, e.g.
// var obj = { set [prop](val) {...} };
COMPUTED_PROP_VARIABLE = 75, // A computed property that's a variable, e.g. [prop]: string;
ANALYZED_DURING_GTI = 76, // In GlobalTypeInfo, we mark some AST nodes
// to avoid analyzing them during
// NewTypeInference. We remove this attribute
// in the fwd direction of NewTypeInference.
CONSTANT_PROPERTY_DEF = 77, // Used to communicate information between
// GlobalTypeInfo and NewTypeInference.
// We use this to tag getprop nodes that
// declare properties.
DECLARED_TYPE_EXPR = 78, // Used to attach TypeDeclarationNode ASTs to
// Nodes which represent a typed NAME or
// FUNCTION.
//
TYPE_BEFORE_CAST = 79, // The type of an expression before the cast.
// This will be present only if the expression is casted.
OPT_ES6_TYPED = 80, // The node is an optional parameter or property
// in ES6 Typed syntax.
GENERIC_TYPE_LIST = 81, // Generic type list in ES6 typed syntax.
IMPLEMENTS = 82, // "implements" clause in ES6 typed syntax.
CONSTRUCT_SIGNATURE = 83, // This node is a TypeScript ConstructSignature
ACCESS_MODIFIER = 84, // TypeScript accessibility modifiers (public, protected, private)
NON_INDEXABLE = 85, // Indicates the node should not be indexed by analysis tools.
PARSE_RESULTS = 86, // Parse results stored on SCRIPT nodes to allow replaying
// parse warnings/errors when cloning cached ASTs.
GOOG_MODULE = 87, // Indicates that a SCRIPT node is a goog.module. Remains set
// after the goog.module is desugared.
GOOG_MODULE_REQUIRE = 88, // Node is a goog.require() as desugared by goog.module()
FEATURE_SET = 89, // Attaches a FeatureSet to SCRIPT nodes.
IS_MODULE_NAME = 90; // Indicates that a STRING node represents a namespace from
// goog.module() or goog.require() call.
private static final String propToString(int propType) {
switch (propType) {
case VAR_ARGS_NAME: return "var_args_name";
case JSDOC_INFO_PROP: return "jsdoc_info";
case INCRDECR_PROP: return "incrdecr";
case QUOTED_PROP: return "quoted";
case OPT_ARG_NAME: return "opt_arg";
case SYNTHETIC_BLOCK_PROP: return "synthetic";
case ADDED_BLOCK: return "added_block";
case ORIGINALNAME_PROP: return "originalname";
case SIDE_EFFECT_FLAGS: return "side_effect_flags";
case IS_CONSTANT_NAME: return "is_constant_name";
case IS_NAMESPACE: return "is_namespace";
case DIRECTIVES: return "directives";
case DIRECT_EVAL: return "direct_eval";
case FREE_CALL: return "free_call";
case STATIC_SOURCE_FILE: return "source_file";
case INPUT_ID: return "input_id";
case LENGTH: return "length";
case SLASH_V: return "slash_v";
case INFERRED_FUNCTION: return "inferred";
case CHANGE_TIME: return "change_time";
case REFLECTED_OBJECT: return "reflected_object";
case STATIC_MEMBER: return "static_member";
case GENERATOR_FN: return "generator_fn";
case ARROW_FN: return "arrow_fn";
case ASYNC_FN: return "async_fn";
case YIELD_FOR: return "yield_for";
case EXPORT_DEFAULT: return "export_default";
case EXPORT_ALL_FROM: return "export_all_from";
case IS_CONSTANT_VAR: return "is_constant_var";
case GENERATOR_MARKER: return "is_generator_marker";
case GENERATOR_SAFE: return "is_generator_safe";
case RAW_STRING_VALUE: return "raw_string_value";
case COMPUTED_PROP_METHOD: return "computed_prop_method";
case COMPUTED_PROP_GETTER: return "computed_prop_getter";
case COMPUTED_PROP_SETTER: return "computed_prop_setter";
case COMPUTED_PROP_VARIABLE: return "computed_prop_variable";
case ANALYZED_DURING_GTI: return "analyzed_during_gti";
case CONSTANT_PROPERTY_DEF: return "constant_property_def";
case DECLARED_TYPE_EXPR: return "declared_type_expr";
case TYPE_BEFORE_CAST: return "type_before_cast";
case OPT_ES6_TYPED: return "opt_es6_typed";
case GENERIC_TYPE_LIST: return "generic_type";
case IMPLEMENTS: return "implements";
case CONSTRUCT_SIGNATURE: return "construct_signature";
case ACCESS_MODIFIER: return "access_modifier";
case NON_INDEXABLE: return "non_indexable";
case PARSE_RESULTS: return "parse_results";
case GOOG_MODULE: return "goog_module";
case GOOG_MODULE_REQUIRE: return "goog_module_require";
case FEATURE_SET: return "feature_set";
case IS_MODULE_NAME: return "is_module_name";
default:
throw new IllegalStateException("unexpected prop id " + propType);
}
}
/**
* Represents a node in the type declaration AST.
*/
public static class TypeDeclarationNode extends Node {
private static final long serialVersionUID = 1L;
private String str; // This is used for specialized signatures.
public TypeDeclarationNode(Token nodeType, String str) {
super(nodeType);
this.str = str;
}
public TypeDeclarationNode(Token nodeType) {
super(nodeType);
}
public TypeDeclarationNode(Token nodeType, Node child) {
super(nodeType, child);
}
public TypeDeclarationNode(Token nodeType, Node left, Node right) {
super(nodeType, left, right);
}
public TypeDeclarationNode(Token nodeType, Node left, Node mid, Node right) {
super(nodeType, left, mid, right);
}
/**
* returns the string content.
* @return non null.
*/
@Override
public String getString() {
return str;
}
@Override
public TypeDeclarationNode cloneNode(boolean cloneTypeExprs) {
return copyNodeFields(new TypeDeclarationNode(token, str), cloneTypeExprs);
}
}
private static class NumberNode extends Node {
private static final long serialVersionUID = 1L;
NumberNode(double number) {
super(Token.NUMBER);
this.number = number;
}
public NumberNode(double number, int lineno, int charno) {
super(Token.NUMBER, lineno, charno);
this.number = number;
}
@Override
public double getDouble() {
return this.number;
}
@Override
public void setDouble(double d) {
this.number = d;
}
@Override
boolean isEquivalentTo(
Node node, boolean compareType, boolean recur, boolean jsDoc) {
boolean equiv = super.isEquivalentTo(node, compareType, recur, jsDoc);
if (equiv) {
double thisValue = getDouble();
double thatValue = ((NumberNode) node).getDouble();
if (thisValue == thatValue) {
// detect the difference between 0.0 and -0.0.
return (thisValue != 0.0) || (1 / thisValue == 1 / thatValue);
}
}
return false;
}
private double number;
@Override
public NumberNode cloneNode(boolean cloneTypeExprs) {
return copyNodeFields(new NumberNode(number), cloneTypeExprs);
}
}
private static class StringNode extends Node {
private static final long serialVersionUID = 1L;
StringNode(Token token, String str) {
super(token);
if (null == str) {
throw new IllegalArgumentException("StringNode: str is null");
}
this.str = str;
}
StringNode(Token token, String str, int lineno, int charno) {
super(token, lineno, charno);
if (null == str) {
throw new IllegalArgumentException("StringNode: str is null");
}
this.str = str;
}
/**
* returns the string content.
* @return non null.
*/
@Override
public String getString() {
return this.str;
}
/**
* sets the string content.
* @param str the new value. Non null.
*/
@Override
public void setString(String str) {
if (null == str) {
throw new IllegalArgumentException("StringNode: str is null");
}
this.str = str;
}
@Override
boolean isEquivalentTo(
Node node, boolean compareType, boolean recur, boolean jsDoc) {
return (super.isEquivalentTo(node, compareType, recur, jsDoc)
&& this.str.equals(((StringNode) node).str));
}
/**
* If the property is not defined, this was not a quoted key. The
* QUOTED_PROP int property is only assigned to STRING tokens used as
* object lit keys.
* @return true if this was a quoted string key in an object literal.
*/
@Override
public boolean isQuotedString() {
return getBooleanProp(QUOTED_PROP);
}
/**
* This should only be called for STRING nodes created in object lits.
*/
@Override
public void setQuotedString() {
putBooleanProp(QUOTED_PROP, true);
}
private String str;
@Override
public StringNode cloneNode(boolean cloneTypeExprs) {
return copyNodeFields(new StringNode(token, str), cloneTypeExprs);
}
}
// PropListItems must be immutable so that they can be shared.
private interface PropListItem {
int getType();
PropListItem getNext();
PropListItem chain(PropListItem next);
Object getObjectValue();
int getIntValue();
}
private abstract static class AbstractPropListItem
implements PropListItem, Serializable {
private static final long serialVersionUID = 1L;
private final PropListItem next;
private final int propType;
AbstractPropListItem(int propType, PropListItem next) {
this.propType = propType;
this.next = next;
}
@Override
public int getType() {
return propType;
}
@Override
public PropListItem getNext() {
return next;
}
@Override
public abstract PropListItem chain(PropListItem next);
}
// A base class for Object storing props
private static class ObjectPropListItem
extends AbstractPropListItem {
private static final long serialVersionUID = 1L;
private final Object objectValue;
ObjectPropListItem(int propType, Object objectValue, PropListItem next) {
super(propType, next);
this.objectValue = objectValue;
}
@Override
public int getIntValue() {
throw new UnsupportedOperationException();
}
@Override
public Object getObjectValue() {
return objectValue;
}
@Override
public String toString() {
return String.valueOf(objectValue);
}
@Override
public PropListItem chain(PropListItem next) {
return new ObjectPropListItem(getType(), objectValue, next);
}
}
// A base class for int storing props
private static class IntPropListItem extends AbstractPropListItem {
private static final long serialVersionUID = 1L;
final int intValue;
IntPropListItem(int propType, int intValue, PropListItem next) {
super(propType, next);
this.intValue = intValue;
}
@Override
public int getIntValue() {
return intValue;
}
@Override
public Object getObjectValue() {
throw new UnsupportedOperationException();
}
@Override
public String toString() {
return String.valueOf(intValue);
}
@Override
public PropListItem chain(PropListItem next) {
return new IntPropListItem(getType(), intValue, next);
}
}
public Node(Token nodeType) {
token = nodeType;
parent = null;
sourcePosition = -1;
}
public Node(Token nodeType, Node child) {
Preconditions.checkArgument(child.parent == null,
"new child has existing parent");
Preconditions.checkArgument(child.next == null,
"new child has existing sibling");
token = nodeType;
parent = null;
first = last = child;
child.next = null;
child.parent = this;
sourcePosition = -1;
}
public Node(Token nodeType, Node left, Node right) {
Preconditions.checkArgument(left.parent == null,
"first new child has existing parent");
Preconditions.checkArgument(left.next == null,
"first new child has existing sibling");
Preconditions.checkArgument(right.parent == null,
"second new child has existing parent");
Preconditions.checkArgument(right.next == null,
"second new child has existing sibling");
token = nodeType;
parent = null;
first = left;
last = right;
left.next = right;
left.parent = this;
right.next = null;
right.parent = this;
sourcePosition = -1;
}
public Node(Token nodeType, Node left, Node mid, Node right) {
Preconditions.checkArgument(left.parent == null);
Preconditions.checkArgument(left.next == null);
Preconditions.checkArgument(mid.parent == null);
Preconditions.checkArgument(mid.next == null);
Preconditions.checkArgument(right.parent == null);
Preconditions.checkArgument(right.next == null);
token = nodeType;
parent = null;
first = left;
last = right;
left.next = mid;
left.parent = this;
mid.next = right;
mid.parent = this;
right.next = null;
right.parent = this;
sourcePosition = -1;
}
Node(Token nodeType, Node left, Node mid, Node mid2, Node right) {
Preconditions.checkArgument(left.parent == null);
Preconditions.checkArgument(left.next == null);
Preconditions.checkArgument(mid.parent == null);
Preconditions.checkArgument(mid.next == null);
Preconditions.checkArgument(mid2.parent == null);
Preconditions.checkArgument(mid2.next == null);
Preconditions.checkArgument(right.parent == null);
Preconditions.checkArgument(right.next == null);
token = nodeType;
parent = null;
first = left;
last = right;
left.next = mid;
left.parent = this;
mid.next = mid2;
mid.parent = this;
mid2.next = right;
mid2.parent = this;
right.next = null;
right.parent = this;
sourcePosition = -1;
}
public Node(Token nodeType, int lineno, int charno) {
token = nodeType;
parent = null;
sourcePosition = mergeLineCharNo(lineno, charno);
}
public Node(Token nodeType, Node child, int lineno, int charno) {
this(nodeType, child);
sourcePosition = mergeLineCharNo(lineno, charno);
}
public static Node newNumber(double number) {
return new NumberNode(number);
}
public static Node newNumber(double number, int lineno, int charno) {
return new NumberNode(number, lineno, charno);
}
public static Node newString(String str) {
return new StringNode(Token.STRING, str);
}
public static Node newString(Token token, String str) {
return new StringNode(token, str);
}
public static Node newString(String str, int lineno, int charno) {
return new StringNode(Token.STRING, str, lineno, charno);
}
public static Node newString(Token token, String str, int lineno, int charno) {
return new StringNode(token, str, lineno, charno);
}
public Token getToken() {
return token;
}
public Token getType() {
return getToken();
}
public void setToken(Token token) {
this.token = token;
}
public void setType(Token token) {
setToken(token);
}
public boolean hasChildren() {
return first != null;
}
public Node getFirstChild() {
return first;
}
public Node getFirstFirstChild() {
return first == null ? null : first.first;
}
public Node getSecondChild() {
return first.next;
}
public Node getLastChild() {
return last;
}
public Node getNext() {
return next;
}
public Node getChildBefore(Node child) {
if (child == first) {
return null;
}
Node n = first;
if (n == null) {
throw new RuntimeException("node is not a child");
}
while (n.next != child) {
n = n.next;
if (n == null) {
throw new RuntimeException("node is not a child");
}
}
return n;
}
public Node getChildAtIndex(int i) {
Node n = first;
while (i > 0) {
n = n.next;
i--;
}
return n;
}
public int getIndexOfChild(Node child) {
Node n = first;
int i = 0;
while (n != null) {
if (child == n) {
return i;
}
n = n.next;
i++;
}
return -1;
}
public Node getLastSibling() {
Node n = this;
while (n.next != null) {
n = n.next;
}
return n;
}
public void addChildToFront(Node child) {
Preconditions.checkArgument(child.parent == null);
Preconditions.checkArgument(child.next == null);
child.parent = this;
child.next = first;
first = child;
if (last == null) {
last = child;
}
}
public void addChildToBack(Node child) {
Preconditions.checkArgument(child.parent == null);
Preconditions.checkArgument(child.next == null);
child.parent = this;
child.next = null;
if (last == null) {
first = last = child;
return;
}
last.next = child;
last = child;
}
public void addChildrenToFront(Node children) {
for (Node child = children; child != null; child = child.next) {
Preconditions.checkArgument(child.parent == null);
child.parent = this;
}
Node lastSib = children.getLastSibling();
lastSib.next = first;
first = children;
if (last == null) {
last = lastSib;
}
}
public void addChildrenToBack(Node children) {
addChildrenAfter(children, getLastChild());
}
/**
* Add 'child' before 'node'.
*/
public void addChildBefore(Node newChild, Node node) {
Preconditions.checkArgument(node != null && node.parent == this,
"The existing child node of the parent should not be null.");
Preconditions.checkArgument(newChild.next == null,
"The new child node has siblings.");
Preconditions.checkArgument(newChild.parent == null,
"The new child node already has a parent.");
if (first == node) {
newChild.parent = this;
newChild.next = first;
first = newChild;
return;
}
Node prev = getChildBefore(node);
addChildAfter(newChild, prev);
}
/**
* Add 'child' after 'node'.
*/
public void addChildAfter(Node newChild, Node node) {
Preconditions.checkArgument(newChild.next == null,
"The new child node has siblings.");
addChildrenAfter(newChild, node);
}
/**
* Add all children after 'node'.
*/
public void addChildrenAfter(Node children, Node node) {
Preconditions.checkArgument(node == null || node.parent == this);
for (Node child = children; child != null; child = child.next) {
Preconditions.checkArgument(child.parent == null);
child.parent = this;
}
Node lastSibling = children.getLastSibling();
if (node != null) {
Node oldNext = node.next;
node.next = children;
lastSibling.next = oldNext;
if (node == last) {
last = lastSibling;
}
} else {
// Append to the beginning.
if (first != null) {
lastSibling.next = first;
} else {
last = lastSibling;
}
first = children;
}
}
/**
* Detach a child from its parent and siblings.
*/
public void removeChild(Node child) {
Node prev = getChildBefore(child);
if (prev == null) {
first = first.next;
} else {
prev.next = child.next;
}
if (child == last) {
last = prev;
}
child.next = null;
child.parent = null;
}
/**
* Detaches child from Node and replaces it with newChild.
*/
public void replaceChild(Node child, Node newChild) {
Preconditions.checkArgument(newChild.next == null,
"The new child node has siblings.");
Preconditions.checkArgument(newChild.parent == null,
"The new child node already has a parent.");
// Copy over important information.
newChild.copyInformationFrom(child);
newChild.next = child.next;
newChild.parent = this;
if (child == first) {
first = newChild;
} else {
Node prev = getChildBefore(child);
prev.next = newChild;
}
if (child == last) {
last = newChild;
}
child.next = null;
child.parent = null;
}
public void replaceChildAfter(Node prevChild, Node newChild) {
Preconditions.checkArgument(prevChild.parent == this,
"prev is not a child of this node.");
Preconditions.checkArgument(newChild.next == null,
"The new child node has siblings.");
Preconditions.checkArgument(newChild.parent == null,
"The new child node already has a parent.");
// Copy over important information.
newChild.copyInformationFrom(prevChild);
Node child = prevChild.next;
newChild.next = child.next;
newChild.parent = this;
prevChild.next = newChild;
if (child == last) {
last = newChild;
}
child.next = null;
child.parent = null;
}
@VisibleForTesting
PropListItem lookupProperty(int propType) {
PropListItem x = propListHead;
while (x != null && propType != x.getType()) {
x = x.getNext();
}
return x;
}
/**
* Clone the properties from the provided node without copying
* the property object. The receiving node may not have any
* existing properties.
* @param other The node to clone properties from.
* @return this node.
*/
public Node clonePropsFrom(Node other) {
Preconditions.checkState(this.propListHead == null,
"Node has existing properties.");
this.propListHead = other.propListHead;
return this;
}
public void removeProp(int propType) {
PropListItem result = removeProp(propListHead, propType);
if (result != propListHead) {
propListHead = result;
}
}
public boolean hasProps() {
return propListHead != null;
}
/**
* @param item The item to inspect
* @param propType The property to look for
* @return The replacement list if the property was removed, or
* 'item' otherwise.
*/
private PropListItem removeProp(PropListItem item, int propType) {
if (item == null) {
return null;
} else if (item.getType() == propType) {
return item.getNext();
} else {
PropListItem result = removeProp(item.getNext(), propType);
if (result != item.getNext()) {
return item.chain(result);
} else {
return item;
}
}
}
public Object getProp(int propType) {
PropListItem item = lookupProperty(propType);
if (item == null) {
return null;
}
return item.getObjectValue();
}
public boolean getBooleanProp(int propType) {
return getIntProp(propType) != 0;
}
/**
* Returns the integer value for the property, or 0 if the property
* is not defined.
*/
public int getIntProp(int propType) {
PropListItem item = lookupProperty(propType);
if (item == null) {
return 0;
}
return item.getIntValue();
}
public int getExistingIntProp(int propType) {
PropListItem item = lookupProperty(propType);
if (item == null) {
throw new IllegalStateException("missing prop: " + propType);
}
return item.getIntValue();
}
public void putProp(int propType, Object value) {
removeProp(propType);
if (value != null) {
propListHead = createProp(propType, value, propListHead);
}
}
public void putBooleanProp(int propType, boolean value) {
putIntProp(propType, value ? 1 : 0);
}
public void putIntProp(int propType, int value) {
removeProp(propType);
if (value != 0) {
propListHead = createProp(propType, value, propListHead);
}
}
/**
* Sets the syntactical type specified on this node.
* @param typeExpression
*/
public void setDeclaredTypeExpression(TypeDeclarationNode typeExpression) {
putProp(DECLARED_TYPE_EXPR, typeExpression);
}
/**
* Returns the syntactical type specified on this node. Not to be confused
* with {@link #getJSType()} which returns the compiler-inferred type.
*/
public TypeDeclarationNode getDeclaredTypeExpression() {
return (TypeDeclarationNode) getProp(DECLARED_TYPE_EXPR);
}
PropListItem createProp(int propType, Object value, PropListItem next) {
return new ObjectPropListItem(propType, value, next);
}
PropListItem createProp(int propType, int value, PropListItem next) {
return new IntPropListItem(propType, value, next);
}
/**
* Returns the type of this node before casting. This annotation will only exist on the first
* child of a CAST node after type checking.
*/
public JSType getJSTypeBeforeCast() {
return (JSType) getProp(TYPE_BEFORE_CAST);
}
// Gets all the property types, in sorted order.
private int[] getSortedPropTypes() {
int count = 0;
for (PropListItem x = propListHead; x != null; x = x.getNext()) {
count++;
}
int[] keys = new int[count];
for (PropListItem x = propListHead; x != null; x = x.getNext()) {