Package net.sf.saxon.expr.parser

## Class TypeChecker

- java.lang.Object
- - net.sf.saxon.expr.parser.TypeChecker

- Direct Known Subclasses:
    `TypeChecker10`

      public class TypeChecker
      extends java.lang.Object

  This class provides Saxon's type checking capability. It contains a method, staticTypeCheck, which is called at compile time to perform type checking of an expression.

- - ### Constructor Summary

    Constructors
    Constructor | Description
    --- | ---
    `TypeChecker()` |

  - ### Method Summary

    All Methods Static Methods Instance Methods Concrete Methods
    Modifier and Type | Method | Description
    --- | --- | ---
    `static XPathException` | `ebvError​(Expression exp, TypeHierarchy th)` | Test whether a given expression is capable of returning a value that has an effective boolean value.
    `Expression` | `makeArithmeticExpression​(Expression lhs, int operator, Expression rhs)` |
    `Expression` | `makeGeneralComparison​(Expression lhs, int operator, Expression rhs)` |
    `Expression` | `processValueOf​(Expression select, Configuration config)` |
    `Expression` | `staticTypeCheck​(Expression supplied, SequenceType req, RoleDiagnostic role, ExpressionVisitor visitor)` | Check an expression against a required type, modifying it if necessary.
    `static Expression` | `strictTypeCheck​(Expression supplied, SequenceType req, RoleDiagnostic role, StaticContext env)` | Check an expression against a required type, modifying it if necessary.
    `static XPathException` | `testConformance​(Sequence val, SequenceType requiredType, XPathContext context)` | Test whether a given value conforms to a given type

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### TypeChecker

          public TypeChecker()

  - ### Method Detail

    - #### staticTypeCheck

          public Expression staticTypeCheck​(Expression supplied,
                                            SequenceType req,
                                            RoleDiagnostic role,
                                            ExpressionVisitor visitor)
                                     throws XPathException

      Check an expression against a required type, modifying it if necessary.

      This method takes the supplied expression and checks to see whether it is known statically to conform to the specified type. There are three possible outcomes. If the static type of the expression is a subtype of the required type, the method returns the expression unchanged. If the static type of the expression is incompatible with the required type (for example, if the supplied type is integer and the required type is string) the method throws an exception (this results in a compile-time type error being reported). If the static type is a supertype of the required type, then a new expression is constructed that evaluates the original expression and checks the dynamic type of the result; this new expression is returned as the result of the method.

      The rules applied are those for function calling in XPath, that is, the rules that the argument of a function call must obey in relation to the signature of the function. Some contexts require slightly different rules (for example, operands of polymorphic operators such as "+"). In such cases this method cannot be used.

      Note that this method does not do recursive type-checking of the sub-expressions.

      Parameters:
        `supplied` - The expression to be type-checked
        `req` - The required type for the context in which the expression is used
        `role` - Information about the role of the subexpression within the containing expression, used to provide useful error messages
        `visitor` - An expression visitor
      Returns:
        The original expression if it is type-safe, or the expression wrapped in a run-time type checking expression if not.
      Throws:
        `XPathException` - if the supplied type is statically inconsistent with the required type (that is, if they have no common subtype)

    - #### makeArithmeticExpression

          public Expression makeArithmeticExpression​(Expression lhs,
                                                     int operator,
                                                     Expression rhs)

    - #### makeGeneralComparison

          public Expression makeGeneralComparison​(Expression lhs,
                                                  int operator,
                                                  Expression rhs)

    - #### processValueOf

          public Expression processValueOf​(Expression select,
                                           Configuration config)

    - #### strictTypeCheck

          public static Expression strictTypeCheck​(Expression supplied,
                                                   SequenceType req,
                                                   RoleDiagnostic role,
                                                   StaticContext env)
                                            throws XPathException

      Check an expression against a required type, modifying it if necessary. This is a variant of the method `staticTypeCheck(net.sf.saxon.expr.Expression, net.sf.saxon.value.SequenceType, net.sf.saxon.expr.parser.RoleDiagnostic, net.sf.saxon.expr.parser.ExpressionVisitor)` used for expressions that declare variables in XQuery. In these contexts, conversions such as numeric type promotion and atomization are not allowed.
      Parameters:
        `supplied` - The expression to be type-checked
        `req` - The required type for the context in which the expression is used
        `role` - Information about the role of the subexpression within the containing expression, used to provide useful error messages
        `env` - The static context containing the types being checked. At present this is used only to locate a NamePool
      Returns:
        The original expression if it is type-safe, or the expression wrapped in a run-time type checking expression if not.
      Throws:
        `XPathException` - if the supplied type is statically inconsistent with the required type (that is, if they have no common subtype)

    - #### testConformance

          public static XPathException testConformance​(Sequence val,
                                                       SequenceType requiredType,
                                                       XPathContext context)
                                                throws XPathException

      Test whether a given value conforms to a given type
      Parameters:
        `val` - the value
        `requiredType` - the required type
        `context` - XPath dynamic context
      Returns:
        an XPathException describing the error condition if the value doesn't conform; or null if it does.
      Throws:
        `XPathException` - if a failure occurs reading the value

    - #### ebvError

          public static XPathException ebvError​(Expression exp,
                                                TypeHierarchy th)

      Test whether a given expression is capable of returning a value that has an effective boolean value.
      Parameters:
        `exp` - the given expression
        `th` - the type hierarchy cache
      Returns:
        null if the expression is OK (optimistically), an exception object if not