Exemple #1
0
 /**
  * Set the definitions of the parameters in the compiled function, as an array.
  *
  * @param fn the compiled object representing the user-written function
  */
 public void setParameterDefinitions(UserFunction fn) {
   UserFunctionParameter[] params = new UserFunctionParameter[getNumberOfArguments()];
   fn.setParameterDefinitions(params);
   int count = 0;
   AxisIterator kids = iterateAxis(Axis.CHILD);
   while (true) {
     NodeInfo node = kids.next();
     if (node == null) {
       return;
     }
     if (node instanceof XSLParam) {
       UserFunctionParameter param = new UserFunctionParameter();
       params[count++] = param;
       param.setRequiredType(((XSLParam) node).getRequiredType());
       param.setVariableQName(((XSLParam) node).getVariableQName());
       param.setSlotNumber(((XSLParam) node).getSlotNumber());
       ((XSLParam) node).fixupBinding(param);
       int refs = ExpressionTool.getReferenceCount(fn.getBody(), param, false);
       param.setReferenceCount(refs);
     }
   }
 }
Exemple #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());
    }
  }