private LogicalVariable translateExprNode(TranslationContext tCtx, ExprNode node) throws SystemException {
return createConcatenation(translateExpressionList(node.getExpressions(), tCtx), tCtx);
}
private LogicalVariable translatePathExpr(PathExprNode pe, TranslationContext tCtx) throws SystemException {
ILogicalExpression ctxExpr = null;
PathType type = pe.getPathType();
if (type != null) {
XQueryVariable dotVar = tCtx.varScope.lookupVariable(XMLQueryCompilerConstants.DOT_VAR_NAME);
ILogicalExpression root = sfce(BuiltinFunctions.FN_ROOT_1, vre(dotVar.getLogicalVariable()));
if (PathType.SLASH.equals(type)) {
ctxExpr = root;
} else {
ctxExpr = sfce(BuiltinOperators.DESCENDANT_OR_SELF,
treat(root, SequenceType.create(AnyNodeType.INSTANCE, Quantifier.QUANT_STAR)));
}
}
if (pe.getPaths() != null) {
for (RelativePathExprNode rpen : pe.getPaths()) {
boolean asc = true;
if (PathType.SLASH_SLASH.equals(rpen.getPathType())) {
ctxExpr = sfce(BuiltinOperators.DESCENDANT_OR_SELF,
treat(ctxExpr, SequenceType.create(AnyNodeType.INSTANCE, Quantifier.QUANT_STAR)));
}
boolean popScope = false;
if (ctxExpr != null) {
popScope = true;
tCtx = tCtx.pushContext();
tCtx.pushVariableScope();
iterateOver(ctxExpr, tCtx);
ctxExpr = null;
}
List<ASTNode> predicates = null;
ASTNode pathNode = rpen.getPath();
if (ASTTag.AXIS_STEP.equals(pathNode.getTag())) {
AxisStepNode axisNode = (AxisStepNode) pathNode;
predicates = axisNode.getPredicates();
AxisStepNode.Axis axis = axisNode.getAxis();
if (ctxExpr == null) {
ctxExpr = vre(tCtx.varScope.lookupVariable(XMLQueryCompilerConstants.DOT_VAR_NAME)
.getLogicalVariable());
}
Function axisFn = translateAxis(axis);
NodeType nt = translateNodeTest(axis, axisNode.getNodeTest());
int ntCode = currCtx.encodeSequenceType(SequenceType.create(nt, Quantifier.QUANT_ONE));
ctxExpr = sfce(axisFn,
treat(ctxExpr, SequenceType.create(AnyNodeType.INSTANCE, Quantifier.QUANT_STAR)),
ce(SequenceType.create(BuiltinTypeRegistry.XS_INT, Quantifier.QUANT_ONE), ntCode));
asc = isForwardAxis(axis);
} else if (ASTTag.FILTER_EXPRESSION.equals(pathNode.getTag())) {
FilterExprNode filterNode = (FilterExprNode) pathNode;
predicates = filterNode.getPredicates();
ctxExpr = vre(translateExpression(filterNode.getExpr(), tCtx));
} else {
throw new IllegalStateException("Unknown path node: " + pathNode.getTag());
}
if (predicates != null && !predicates.isEmpty()) {
ctxExpr = sfce(asc ? BuiltinOperators.SORT_DISTINCT_NODES_ASC_OR_ATOMICS
: BuiltinOperators.SORT_DISTINCT_NODES_DESC_OR_ATOMICS, ctxExpr);
for (ASTNode pn : predicates) {
tCtx = tCtx.pushContext();
tCtx.pushVariableScope();
iterateOver(ctxExpr, tCtx);
LogicalVariable pLVar = translateExpression(pn, tCtx);
ILogicalExpression tTest = instanceOf(vre(pLVar),
SequenceType.create(BuiltinTypeRegistry.XSEXT_NUMERIC, Quantifier.QUANT_ONE));
ILogicalExpression posTest = sfce(BuiltinOperators.VALUE_EQ, vre(pLVar), vre(tCtx.varScope
.lookupVariable(XMLQueryCompilerConstants.POS_VAR_NAME).getLogicalVariable()));
ILogicalExpression boolTest = sfce(BuiltinFunctions.FN_BOOLEAN_1, vre(pLVar));
SelectOperator select = new SelectOperator(mutable(sfce(BuiltinOperators.IF_THEN_ELSE, tTest,
posTest, boolTest)), false, null);
select.getInputs().add(mutable(tCtx.op));
tCtx.op = select;