Esempio n. 1
0
  public void typeCheckBody() throws XPathException {
    Expression exp = compiledFunction.getBody();
    Expression exp2 = exp;
    ExpressionVisitor visitor = makeExpressionVisitor();
    try {
      // We've already done the typecheck of each XPath expression, but it's worth doing again at
      // this
      // level because we have more information now.

      exp2 = visitor.typeCheck(exp, null);
      if (resultType != null) {
        RoleLocator role = new RoleLocator(RoleLocator.FUNCTION_RESULT, functionName, 0);
        role.setErrorCode("XTTE0780");
        exp2 = TypeChecker.staticTypeCheck(exp2, resultType, false, role, visitor);
      }
    } catch (XPathException err) {
      err.maybeSetLocation(this);
      compileError(err);
    }
    if (exp2 != exp) {
      compiledFunction.setBody(exp2);
    }
  }
Esempio n. 2
0
  public void optimize(Declaration declaration) throws XPathException {
    Expression exp = compiledFunction.getBody();
    ExpressionVisitor visitor = makeExpressionVisitor();
    Expression exp2 = exp;
    Optimizer opt = getConfiguration().obtainOptimizer();
    try {
      if (opt.getOptimizationLevel() != Optimizer.NO_OPTIMIZATION) {
        exp2 = exp.optimize(visitor, null);
      }

    } catch (XPathException err) {
      err.maybeSetLocation(this);
      compileError(err);
    }

    // Try to extract new global variables from the body of the function
    if (opt.getOptimizationLevel() != Optimizer.NO_OPTIMIZATION) {
      Expression exp3 = opt.promoteExpressionsToGlobal(exp2, visitor);
      if (exp3 != null) {
        exp2 = visitor.optimize(exp3, null);
      }
    }

    // Add trace wrapper code if required
    exp2 = makeTraceInstruction(this, exp2);

    allocateSlots(exp2);
    if (exp2 != exp) {
      compiledFunction.setBody(exp2);
    }

    int tailCalls =
        ExpressionTool.markTailFunctionCalls(exp2, getObjectName(), getNumberOfArguments());
    if (tailCalls != 0) {
      compiledFunction.setTailRecursive(tailCalls > 0, tailCalls > 1);
      compiledFunction.setBody(new TailCallLoop(compiledFunction));
    }

    // Generate byte code if appropriate

    if (getConfiguration().isGenerateByteCode(Configuration.XSLT)) {
      try {
        Expression cbody =
            opt.compileToByteCode(
                compiledFunction.getBody(),
                nameAtt,
                Expression.PROCESS_METHOD | Expression.ITERATE_METHOD);
        if (cbody != null) {
          compiledFunction.setBody(cbody);
        }
      } catch (Exception e) {
        System.err.println("Failed while compiling function " + nameAtt);
        e.printStackTrace();
        throw new XPathException(e);
      }
    }

    compiledFunction.computeEvaluationMode();

    if (isExplaining()) {
      exp2.explain(getConfiguration().getStandardErrorOutput());
    }
  }