use of org.exist.xquery.value.SequenceType in project exist by eXist-db.
the class PartialFunctionApplication method createPartial.
private FunctionReference createPartial(Sequence contextSequence, Item contextItem, FunctionCall staticCall) throws XPathException {
final FunctionSignature signature = staticCall.getSignature();
final SequenceType[] paramTypes = signature.getArgumentTypes();
// the parameters of the newly created inline function:
// old params except the fixed ones
final List<SequenceType> newParamTypes = new ArrayList<>();
// the arguments to pass to the inner call
final List<Expression> callArgs = new ArrayList<>();
// parameter variables of the new inline function
final List<QName> variables = new ArrayList<>();
// the inline function
final int argCount = staticCall.getArgumentCount();
for (int i = 0; i < argCount; i++) {
final Expression param = staticCall.getArgument(i);
if (param instanceof Function.Placeholder) {
// overloaded functions like concat may have an arbitrary number of arguments
if (i < paramTypes.length) {
newParamTypes.add(paramTypes[i]);
} else // overloaded function: add last sequence type
{
newParamTypes.add(paramTypes[paramTypes.length - 1]);
}
// create local parameter variables
final QName varName = new QName("vp" + i, XMLConstants.NULL_NS_URI);
variables.add(varName);
// the argument to the inner call is a variable ref
final VariableReference ref = new VariableReference(context, varName);
callArgs.add(ref);
} else {
// fixed argument: just compute the argument value
try {
param.analyze(cachedContextInfo);
final Sequence seq = param.eval(contextSequence, contextItem);
callArgs.add(new PrecomputedValue(context, seq));
} catch (final XPathException e) {
if (e.getLine() <= 0) {
e.setLocation(line, column, getSource());
}
// append location of the function call to the exception message:
e.addFunctionCall(function.functionDef, this);
throw e;
}
}
}
final SequenceType[] newParamArray = newParamTypes.toArray(new SequenceType[0]);
final QName name = new QName(PARTIAL_FUN_PREFIX + hashCode(), XMLConstants.NULL_NS_URI);
final FunctionSignature newSignature = new FunctionSignature(name, newParamArray, signature.getReturnType());
final UserDefinedFunction func = new UserDefinedFunction(context, newSignature);
func.setLocation(staticCall.getLine(), staticCall.getColumn());
// add the created parameter variables to the function
for (final QName varName : variables) {
func.addVariable(varName);
}
final FunctionCall innerCall = new FunctionCall(staticCall);
innerCall.setArguments(callArgs);
func.setFunctionBody(innerCall);
final FunctionCall newCall = new FunctionCall(context, func);
newCall.setLocation(staticCall.getLine(), staticCall.getColumn());
return new FunctionReference(newCall);
}
Aggregations