@@ -370,17 +370,15 @@ private void inlineAliasesForName(Name name, GlobalNamespace namespace) {
370
370
List <Ref > refs = new ArrayList <>(name .getRefs ());
371
371
for (Ref ref : refs ) {
372
372
Scope hoistScope = ref .scope .getClosestHoistScope ();
373
- if (ref .type == Ref . Type . ALIASING_GET && !mayBeGlobalAlias (ref ) && ref .getTwin () == null ) {
373
+ if (ref .isAliasingGet () && !mayBeGlobalAlias (ref ) && ! ref .isTwin () ) {
374
374
// {@code name} meets condition (c). Try to inline it.
375
375
// TODO(johnlenz): consider picking up new aliases at the end
376
376
// of the pass instead of immediately like we do for global
377
377
// inlines.
378
378
inlineAliasIfPossible (name , ref , namespace );
379
- } else if (ref .type == Ref .Type .ALIASING_GET
380
- && hoistScope .isGlobal ()
381
- && ref .getTwin () == null ) { // ignore aliases in chained assignments
379
+ } else if (ref .isAliasingGet () && hoistScope .isGlobal () && !ref .isTwin ()) {
382
380
inlineGlobalAliasIfPossible (name , ref , namespace );
383
- } else if (name .isClass () && ref .type == Ref . Type . SUBCLASSING_GET && name .props != null ) {
381
+ } else if (name .isClass () && ref .isSubclassingGet () && name .props != null ) {
384
382
for (Name prop : name .props ) {
385
383
rewriteAllSubclassInheritedAccesses (name , ref , prop , namespace );
386
384
}
@@ -494,7 +492,7 @@ private boolean rewriteAllSubclassInheritedAccesses(
494
492
Name subclassNameObj = namespace .getOwnSlot (subclassName );
495
493
if (subclassNameObj != null && subclassNameObj .subclassingGetCount () > 0 ) {
496
494
for (Ref ref : subclassNameObj .getRefs ()) {
497
- if (ref .type == Ref . Type . SUBCLASSING_GET ) {
495
+ if (ref .isSubclassingGet () ) {
498
496
rewriteAllSubclassInheritedAccesses (superclassNameObj , ref , prop , namespace );
499
497
}
500
498
}
@@ -763,7 +761,7 @@ private void inlineGlobalAliasIfPossible(Name name, Ref alias, GlobalNamespace n
763
761
// Rewrite the initialization of the alias, unless this is an unsafe alias inline
764
762
// caused by an @constructor. In that case, we need to leave the initialization around.
765
763
Ref aliasDeclaration = aliasingName .getDeclaration ();
766
- if (aliasDeclaration .getTwin () != null ) {
764
+ if (aliasDeclaration .isTwin () ) {
767
765
// This is in a nested assign.
768
766
// Replace
769
767
// a.b = aliasing.name = aliased.name
@@ -797,42 +795,39 @@ private void rewriteAliasReferences(
797
795
Name aliasingName , Ref aliasingRef , Set <AstChange > newNodes ) {
798
796
List <Ref > refs = new ArrayList <>(aliasingName .getRefs ());
799
797
for (Ref ref : refs ) {
800
- switch (ref .type ) {
801
- case SET_FROM_GLOBAL :
802
- continue ;
803
- case DIRECT_GET :
804
- case ALIASING_GET :
805
- case PROTOTYPE_GET :
806
- case CALL_GET :
807
- case SUBCLASSING_GET :
808
- if (ref .getTwin () != null ) {
809
- // The reference is the left-hand side of a nested assignment. This means we store two
810
- // separate 'twin' Refs with the same node of types ALIASING_GET and SET_FROM_GLOBAL.
811
- // For example, the read of `c.d` has a twin reference in
812
- // a.b = c.d = e.f;
813
- // We handle this case later.
814
- checkState (ref .type == Ref .Type .ALIASING_GET , ref );
815
- break ;
816
- }
817
- if (ref .getNode ().isStringKey ()) {
818
- // e.g. `y` in `const {y} = x;`
819
- DestructuringGlobalNameExtractor .reassignDestructringLvalue (
820
- ref .getNode (), aliasingRef .getNode ().cloneTree (), newNodes , ref , compiler );
821
- } else {
822
- // e.g. `x.y`
823
- checkState (ref .getNode ().isGetProp () || ref .getNode ().isName ());
824
- Node newNode = aliasingRef .getNode ().cloneTree ();
825
- Node node = ref .getNode ();
826
- newNode .srcref (node );
827
- node .replaceWith (newNode );
828
- compiler .reportChangeToEnclosingScope (newNode );
829
- newNodes .add (new AstChange (ref .scope , newNode ));
830
- }
831
- aliasingName .removeRef (ref );
832
- break ;
833
- default :
834
- throw new IllegalStateException ();
798
+ if (ref .isSetFromGlobal ()) {
799
+ // Handled elsewhere
800
+ continue ;
801
+ }
802
+
803
+ checkState (ref .isGet (), ref ); // names with local sets should not be rewritten
804
+
805
+ // Twin refs that are both gets and sets are handled later, with the other sets.
806
+ if (ref .isTwin ()) {
807
+ // The reference is the left-hand side of a nested assignment. This means we store two
808
+ // separate 'twin' Refs with the same node of types ALIASING_GET and SET_FROM_GLOBAL.
809
+ // For example, the read of `c.d` has a twin reference in
810
+ // a.b = c.d = e.f;
811
+ // We handle this case later.
812
+ checkState (ref .isAliasingGet (), ref );
813
+ continue ;
814
+ }
815
+
816
+ if (ref .getNode ().isStringKey ()) {
817
+ // e.g. `y` in `const {y} = x;`
818
+ DestructuringGlobalNameExtractor .reassignDestructringLvalue (
819
+ ref .getNode (), aliasingRef .getNode ().cloneTree (), newNodes , ref , compiler );
820
+ } else {
821
+ // e.g. `x.y`
822
+ checkState (ref .getNode ().isGetProp () || ref .getNode ().isName ());
823
+ Node newNode = aliasingRef .getNode ().cloneTree ();
824
+ Node node = ref .getNode ();
825
+ newNode .srcref (node );
826
+ node .replaceWith (newNode );
827
+ compiler .reportChangeToEnclosingScope (newNode );
828
+ newNodes .add (new AstChange (ref .scope , newNode ));
835
829
}
830
+ aliasingName .removeRef (ref );
836
831
}
837
832
}
838
833
@@ -1340,18 +1335,17 @@ private ImmutableSet<Name> checkNamespaces() {
1340
1335
if (ref == name .getDeclaration ()) {
1341
1336
continue ;
1342
1337
}
1343
-
1344
- if (ref .type == Ref .Type .DELETE_PROP ) {
1338
+ if (ref .isDeleteProp ()) {
1345
1339
if (initialized ) {
1346
1340
warnAboutNamespaceRedefinition (name , ref );
1347
1341
}
1348
- } else if (ref .type == Ref . Type . SET_FROM_GLOBAL || ref . type == Ref . Type . SET_FROM_LOCAL ) {
1342
+ } else if (ref .isSet () ) {
1349
1343
if (initialized && !isSafeNamespaceReinit (ref )) {
1350
1344
warnAboutNamespaceRedefinition (name , ref );
1351
1345
}
1352
1346
1353
1347
initialized = true ;
1354
- } else if (ref .type == Ref . Type . ALIASING_GET ) {
1348
+ } else if (ref .isAliasingGet () ) {
1355
1349
warnAboutNamespaceAliasing (name , ref );
1356
1350
logDecisionForName (name , "escapes" );
1357
1351
escaped .add (name );
@@ -1475,7 +1469,7 @@ private void flattenReferencesTo(Name n, String alias) {
1475
1469
// 2) References inside a complex assign. (a = x.y = 0). These are
1476
1470
// called TWIN references, because they show up twice in the
1477
1471
// reference list. Only collapse the set, not the alias.
1478
- if (!NodeUtil .mayBeObjectLitKey (r .getNode ()) && (r . getTwin () == null || r .isSet ())) {
1472
+ if (!NodeUtil .mayBeObjectLitKey (r .getNode ()) && (! r . isTwin () || r .isSet ())) {
1479
1473
flattenNameRef (alias , r .getNode (), rParent , originalName );
1480
1474
} else if (r .getNode ().isStringKey () && r .getNode ().getParent ().isObjectPattern ()) {
1481
1475
Node newNode = IR .name (alias ).srcref (r .getNode ());
@@ -1521,7 +1515,7 @@ private void flattenPrefixes(String alias, String originalName, Name n, int dept
1521
1515
1522
1516
// References inside a complex assign (a = x.y = 0)
1523
1517
// have twins. We should only flatten one of the twins.
1524
- if (r . getTwin () == null || r .isSet ()) {
1518
+ if (! r . isTwin () || r .isSet ()) {
1525
1519
flattenNameRefAtDepth (alias , r .getNode (), depth , originalName );
1526
1520
}
1527
1521
}
@@ -1643,7 +1637,7 @@ private void collapseDeclarationOfNameAndDescendants(Name n, String alias, Set<N
1643
1637
* @param ref An object containing information about the assignment getting updated
1644
1638
*/
1645
1639
private void updateTwinnedDeclaration (String alias , Name refName , Ref ref ) {
1646
- checkNotNull (ref .getTwin () );
1640
+ checkState (ref .isTwin (), ref );
1647
1641
// Don't handle declarations of an already flat name, just qualified names.
1648
1642
if (!ref .getNode ().isGetProp ()) {
1649
1643
return ;
@@ -1760,7 +1754,7 @@ private void updateGlobalNameDeclarationAtAssignNode(
1760
1754
// we are only collapsing for global names.
1761
1755
Ref ref = n .getDeclaration ();
1762
1756
Node rvalue = ref .getNode ().getNext ();
1763
- if (ref .getTwin () != null ) {
1757
+ if (ref .isTwin () ) {
1764
1758
updateTwinnedDeclaration (alias , ref .name , ref );
1765
1759
return ;
1766
1760
}
0 commit comments