Messages.getMessage(Error.FUNC_EMPTY_SEQ_DISALLOWED,
Integer.valueOf(argPosition), ExpressionDumper.dump(expr)));}
}
}
expr = new DynamicCardinalityCheck(context, type.getCardinality(), expr,
new Error(Error.FUNC_PARAM_CARDINALITY, argPosition, mySignature));
// check return type if both types are not Type.ITEM
int returnType = expr.returnsType();
if (returnType == Type.ANY_TYPE || returnType == Type.EMPTY)
{returnType = Type.ITEM;}
boolean typeMatches = type.getPrimaryType() == Type.ITEM;
typeMatches = Type.subTypeOf(returnType, type.getPrimaryType());
if (typeMatches && cardinalityMatches) {
if (type.getNodeName() != null)
{expr = new DynamicNameCheck(context,
new NameTest(type.getPrimaryType(), type.getNodeName()), expr);}
return expr;
}
//Loose argument check : we may move this, or a part hereof, to UntypedValueCheck
if (context.isBackwardsCompatible()) {
if (Type.subTypeOf(type.getPrimaryType(), Type.STRING)) {
if (!Type.subTypeOf(returnType, Type.ATOMIC)) {
expr = new Atomize(context, expr);
returnType = Type.ATOMIC;
}
expr = new AtomicToString(context, expr);
returnType = Type.STRING;
} else if (type.getPrimaryType() == Type.NUMBER
|| Type.subTypeOf(type.getPrimaryType(), Type.DOUBLE)) {
if (!Type.subTypeOf(returnType, Type.ATOMIC)) {
expr = new Atomize(context, expr);
returnType = Type.ATOMIC;
}
expr = new UntypedValueCheck(context, type.getPrimaryType(), expr,
new Error(Error.FUNC_PARAM_TYPE, String.valueOf(argPosition), mySignature));
returnType = type.getPrimaryType();
}
//If the required type is an atomic type, convert the argument to an atomic
if (Type.subTypeOf(type.getPrimaryType(), Type.ATOMIC)) {
if(!Type.subTypeOf(returnType, Type.ATOMIC))
{expr = new Atomize(context, expr);}
if (!(type.getPrimaryType() == Type.ATOMIC))
{expr = new UntypedValueCheck(context, type.getPrimaryType(),
expr, new Error(Error.FUNC_PARAM_TYPE, String.valueOf(argPosition), mySignature));}
returnType = expr.returnsType();
}
//Strict argument check : we may move this, or a part hereof, to UntypedValueCheck
} else {
//If the required type is an atomic type, convert the argument to an atomic
if (Type.subTypeOf(type.getPrimaryType(), Type.ATOMIC)) {
if(!Type.subTypeOf(returnType, Type.ATOMIC))
{expr = new Atomize(context, expr);}
expr = new UntypedValueCheck(context, type.getPrimaryType(),
expr, new Error(Error.FUNC_PARAM_TYPE, String.valueOf(argPosition), mySignature));
returnType = expr.returnsType();
}
}
if (returnType != Type.ITEM && !Type.subTypeOf(returnType, type.getPrimaryType())) {
if (!(Type.subTypeOf(type.getPrimaryType(), returnType) ||