SchemaType type = moduleCtx.lookupSchemaType(name);
if (type != null && args.size() < 2) {
if (!type.isAtomicType()) {
throw new SystemException(ErrorCode.XPST0051, fnNode.getName().getSourceLocation());
}
LogicalVariable var = args.isEmpty() ? tCtx.varScope.lookupVariable(XMLQueryCompilerConstants.DOT_VAR_NAME)
.getLogicalVariable() : args.get(0);
ILogicalExpression expr = cast(vre(var), SequenceType.create((ItemType) type, Quantifier.QUANT_QUESTION));
return createAssignment(expr, tCtx);
}
QName fName = createQName(fnNode.getName(), moduleCtx.getDefaultFunctionNamespaceUri());
if (BuiltinFunctions.FN_POSITION_QNAME.equals(fName)) {
XQueryVariable var = tCtx.varScope.lookupVariable(XMLQueryCompilerConstants.POS_VAR_NAME);
return var.getLogicalVariable();
}
if (BuiltinFunctions.FN_LAST_QNAME.equals(fName)) {
XQueryVariable var = tCtx.varScope.lookupVariable(XMLQueryCompilerConstants.LAST_VAR_NAME);
return var.getLogicalVariable();
}
int nArgs = fnNode.getArguments().size();
Function fn = moduleCtx.lookupFunction(fName, nArgs);
if (fn != null && fn.useContextImplicitly()) {
args.add(tCtx.varScope.lookupVariable(XMLQueryCompilerConstants.DOT_VAR_NAME).getLogicalVariable());
fn = moduleCtx.lookupFunction(fName, nArgs + 1);
}
if (fn == null) {
Function[] fns = moduleCtx.lookupFunctions(fName);
if (fns != null) {
for (int i = 0; i < fns.length && i <= nArgs; ++i) {
if (fns[i] != null && fns[i].getSignature().isVarArgs()) {
fn = fns[i];
break;
}
}
}
}
if (fn == null) {
throw new SystemException(ErrorCode.XPST0017, fnNode.getName().getSourceLocation());
}
Signature sign = fn.getSignature();
List<Mutable<ILogicalExpression>> argExprs = new ArrayList<Mutable<ILogicalExpression>>();
for (int i = 0; i < args.size(); ++i) {
SequenceType argType = sign.getParameterType(i);
argExprs.add(mutable(normalize(vre(args.get(i)), argType)));
}
LogicalVariable lVar = createAssignment(new ScalarFunctionCallExpression(fn, argExprs), tCtx);
return lVar;
}