| 1 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 2 | // Copyright (c) 2018 Saxonica Limited. |
| 3 | // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. |
| 4 | // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 5 | // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0. |
| 6 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 7 | |
| 8 | package net.sf.saxon.expr; |
| 9 | |
| 10 | import net.sf.saxon.expr.parser.RebindingMap; |
| 11 | import net.sf.saxon.om.Item; |
| 12 | import net.sf.saxon.om.Sequence; |
| 13 | import net.sf.saxon.om.StructuredQName; |
| 14 | import net.sf.saxon.trans.XPathException; |
| 15 | |
| 16 | /** |
| 17 | * Variable reference: a reference to a local variable. This subclass of VariableReference |
| 18 | * bypasses the Binding object to get the value directly from the relevant slot in the local |
| 19 | * stackframe. |
| 20 | */ |
| 21 | |
| 22 | public class LocalVariableReference extends VariableReference { |
| 23 | |
| 24 | int slotNumber = -999; |
| 25 | |
| 26 | /** |
| 27 | * Create a local variable reference. The binding and slot number will be supplied later |
| 28 | */ |
| 29 | |
| 30 | public LocalVariableReference(StructuredQName name) { |
| 31 | super(name); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Create a LocalVariableReference bound to a given Binding |
| 36 | * |
| 37 | * @param binding the binding (that is, the declaration of this local variable) |
| 38 | */ |
| 39 | |
| 40 | public LocalVariableReference(LocalBinding binding) { |
| 41 | super(binding); |
| 42 | } |
| 43 | |
| 44 | |
| 45 | /** |
| 46 | * Create a clone copy of this VariableReference |
| 47 | * |
| 48 | * @return the cloned copy |
| 49 | * @param rebindings a mutable list of (old binding, new binding) pairs |
| 50 | * that is used to update the bindings held in any |
| 51 | * local variable references that are copied. |
| 52 | */ |
| 53 | |
| 54 | /*@NotNull*/ |
| 55 | public Expression copy(RebindingMap rebindings) { |
| 56 | if (binding == null) { |
| 57 | throw new UnsupportedOperationException("Cannot copy a variable reference whose binding is unknown"); |
| 58 | } |
| 59 | // if (slotNumber < 0) { |
| 60 | // // slot numbers have not yet been allocated. This is messy. For the time being, the safest |
| 61 | // // thing is to reuse the existing variable reference, which usually works, rather than copying |
| 62 | // // it, which will almost certainly fail. See XSLT streaming test case si-attribute-053 |
| 63 | // return this; |
| 64 | // } |
| 65 | LocalVariableReference ref = new LocalVariableReference(getVariableName()); |
| 66 | ref.copyFrom(this); |
| 67 | ref.slotNumber = slotNumber; |
| 68 | Binding newBinding = rebindings.get(binding); |
| 69 | if (newBinding != null) { |
| 70 | ref.binding = newBinding; |
| 71 | } |
| 72 | ref.binding.addReference(ref, isInLoop()); |
| 73 | return ref; |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public LocalBinding getBinding() { |
| 78 | return (LocalBinding)super.getBinding(); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Set the slot number for this local variable, that is, its position in the local stack frame |
| 83 | * |
| 84 | * @param slotNumber the slot number to be used |
| 85 | */ |
| 86 | |
| 87 | public void setSlotNumber(int slotNumber) { |
| 88 | this.slotNumber = slotNumber; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Get the slot number allocated to this local variable |
| 93 | * |
| 94 | * @return the slot number |
| 95 | */ |
| 96 | |
| 97 | public int getSlotNumber() { |
| 98 | return slotNumber; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Return the value of the variable |
| 103 | * |
| 104 | * @param c the XPath dynamic context |
| 105 | * @return the value of the variable |
| 106 | * @throws XPathException if any dynamic error occurs while evaluating the variable |
| 107 | */ |
| 108 | |
| 109 | /*@NotNull*/ |
| 110 | public Sequence<?> evaluateVariable(XPathContext c) throws XPathException { |
| 111 | try { |
| 112 | return c.getStackFrame().slots[slotNumber]; |
| 113 | } catch (ArrayIndexOutOfBoundsException err) { |
| 114 | if (slotNumber == -999) { |
| 115 | if (binding != null) { |
| 116 | try { |
| 117 | slotNumber = getBinding().getLocalSlotNumber(); |
| 118 | return c.getStackFrame().slots[slotNumber]; |
| 119 | } catch (ArrayIndexOutOfBoundsException err2) { |
| 120 | // fall through |
| 121 | } |
| 122 | } |
| 123 | throw new ArrayIndexOutOfBoundsException("Local variable $" + getDisplayName() + " has not been allocated a stack frame slot"); |
| 124 | } else { |
| 125 | int actual = c.getStackFrame().slots.length; |
| 126 | throw new ArrayIndexOutOfBoundsException("Local variable $" + getDisplayName() + " uses slot " |
| 127 | + slotNumber + " but " + (actual == 0 ? "no" : "only " + c.getStackFrame().slots.length) + " slots" + |
| 128 | " are allocated on the stack frame"); |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Get a name identifying the kind of expression, in terms meaningful to a user. |
| 135 | * |
| 136 | * @return a name identifying the kind of expression, in terms meaningful to a user. |
| 137 | * The name will always be in the form of a lexical XML QName, and should match the name used |
| 138 | * in export() output displaying the expression. |
| 139 | */ |
| 140 | @Override |
| 141 | public String getExpressionName() { |
| 142 | return "locVarRef"; |
| 143 | } |
| 144 | |
| 145 | |
| 146 | /** |
| 147 | * Replace this VariableReference where appropriate by a more efficient implementation. This |
| 148 | * can only be done after all slot numbers are allocated. The efficiency is gained by binding the |
| 149 | * VariableReference directly to a local or global slot, rather than going via the Binding object |
| 150 | * |
| 151 | * @param parent the parent expression of this variable reference |
| 152 | */ |
| 153 | |
| 154 | // public void refineVariableReference(Expression parent) { |
| 155 | // // no-op |
| 156 | // } |
| 157 | } |