/**
* Get the Nth child of a node per the BURM's semantics.
*/
public static IASNode getNthChild( IASNode node, int index)
{
IASNode result = null;
switch( node.getNodeID() )
{
case BindableVariableID:
case VariableID:
{
IVariableNode var = (IVariableNode) node;
switch( index )
{
case 0:
{
result = var.getNameExpressionNode();
break;
}
default:
{
// We have to go hunt among the children
// for the nodes that are relevant to the BURM.
int lastFoundChildPos = 0;
result = var.getVariableTypeNode();
if ( result != null )
{
lastFoundChildPos++;
if ( index == lastFoundChildPos)
break;
}
result = var.getAssignedValueNode();
if ( result != null )
{
lastFoundChildPos++;
if ( index == lastFoundChildPos)
break;
}
// Look for chained variable declarations.
int needle = 0;
while ( needle < node.getChildCount() && lastFoundChildPos < index )
{
if ( node.getChild(needle) instanceof IVariableNode )
lastFoundChildPos++;
if ( lastFoundChildPos < index )
needle++;
}
assert(lastFoundChildPos == index): "getNthChild() failed, should have been constrained by getChildCount(node)";
return ( node.getChild(needle));
}
}
break;
}
case FunctionID:
case GetterID:
case SetterID:
{
FunctionNode func = (FunctionNode) node;
switch( index )
{
case 0:
result = func.getNameExpressionNode();
break;
case 1:
result = func.getParametersContainerNode();
break;
case 2:
result = func.getReturnTypeNode();
if ( result != null )
break;
case 3:
assert (func.hasBeenParsed()) : "getScopedNode() called on a function before the body has been parsed";
result = func.getScopedNode();
break;
}
break;
}
case Op_CommaID:
{
switch( index )
{
case 0:
{
result = node.getChild(node.getChildCount()-1);
break;
}
default:
result = node.getChild(index - 1);
}
break;
}
case TryID:
{
ITryNode tryNode = (ITryNode) node;
switch( index )
{
case 0:
{
result = tryNode.getStatementContentsNode();
break;
}
case 1:
{
if ( tryNode.getFinallyNode() != null )
{
result = tryNode.getFinallyNode();
}
else
{
assert ( tryNode.getCatchNodeCount() > 0 );
result = tryNode.getCatchNode(0);
}
break;
}
default:
{
// Note: If the try has a contents and finally nodes,
// they are presented to the CG by getNthChild() as child
// nodes 0 and 1 before the n-ary tail of catch nodes.
if (tryNode.getStatementContentsNode() != null)
index--;
if (tryNode.getFinallyNode() != null)
index--;
result = tryNode.getCatchNode(index);
}
}
break;
}
case NamespaceID:
{
// Skip over MetaTagsNode and NamespaceIdentifierNode
final NamespaceNode nsNode = (NamespaceNode)node;
final IASNode nsName = nsNode.getNameExpressionNode();
final IASNode nsURI = nsNode.getNamespaceURINode();
switch(index)
{
case 0:
if(nsName != null)
result = nsName;