class SideEffectsAnalysis {
/**
* Returns true if the number of times the child executes depends on the
* parent.
*
* For example, the guard of an IF is not control dependent on the
* IF, but its two THEN/ELSE blocks are.
*
* Also, the guard of WHILE and DO are control dependent on the parent
* since the number of times it executes depends on the parent.
*/
private static boolean isControlDependentChild(Node child) {
Node parent = child.getParent();
if (parent == null) {
return false;
}
ArrayList<Node> siblings = Lists.newArrayList(parent.children());
int indexOfChildInParent = siblings.indexOf(child);
switch(parent.getType()) {
case Token.IF:
case Token.HOOK:
return (indexOfChildInParent == 1 || indexOfChildInParent == 2);
case Token.WHILE:
case Token.DO:
return true;
case Token.FOR:
// Only initializer is not control dependent
return indexOfChildInParent != 0;
case Token.SWITCH:
return indexOfChildInParent > 0;
case Token.AND:
return true;
case Token.OR:
return true;
case Token.FUNCTION:
return true;
default:
return false;
}
}
}