| 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.instruct; |
| 9 | |
| 10 | import net.sf.saxon.expr.XPathContext; |
| 11 | import net.sf.saxon.lib.Logger; |
| 12 | import net.sf.saxon.om.StructuredQName; |
| 13 | |
| 14 | import java.util.ArrayList; |
| 15 | import java.util.List; |
| 16 | |
| 17 | |
| 18 | /** |
| 19 | * A SlotManager supports functions, templates, etc: specifically, any executable code that |
| 20 | * requires a stack frame containing local variables. In XSLT a SlotManager underpins any |
| 21 | * top-level element that can contain local variable declarations, |
| 22 | * specifically, a top-level xsl:template, xsl:variable, xsl:param, or xsl:function element |
| 23 | * or an xsl:attribute-set element or xsl:key element. In XQuery it underpins functions and |
| 24 | * global variables. The purpose of the SlotManager is to allocate slot numbers to variables |
| 25 | * in the stack, and to record how many slots are needed. A Debugger may define a subclass |
| 26 | * with additional functionality. |
| 27 | */ |
| 28 | |
| 29 | public class SlotManager { |
| 30 | |
| 31 | /** |
| 32 | * An empty SlotManager |
| 33 | */ |
| 34 | |
| 35 | public static SlotManager EMPTY = new SlotManager(0); |
| 36 | |
| 37 | private ArrayList<StructuredQName> variableMap = new ArrayList<>(10); |
| 38 | // values are StructuredQName objects representing the variable names |
| 39 | private int numberOfVariables = 0; |
| 40 | |
| 41 | /** |
| 42 | * The constructor should not be called directly. A new SlotManager should be obtained using |
| 43 | * the factory method {@link net.sf.saxon.Configuration#makeSlotManager()}. |
| 44 | */ |
| 45 | |
| 46 | public SlotManager() { |
| 47 | numberOfVariables = 0; |
| 48 | variableMap = new ArrayList<>(); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Create a SlotManager with a given number of slots |
| 53 | * |
| 54 | * @param n the number of slots |
| 55 | */ |
| 56 | |
| 57 | public SlotManager(int n) { |
| 58 | numberOfVariables = n; |
| 59 | variableMap = new ArrayList<>(n); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Get number of variables (size of stack frame) |
| 64 | * |
| 65 | * @return the number of slots for variables |
| 66 | */ |
| 67 | |
| 68 | public int getNumberOfVariables() { |
| 69 | return numberOfVariables; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Set the number of variables |
| 74 | * |
| 75 | * @param numberOfVariables the space to be allocated for variables |
| 76 | */ |
| 77 | |
| 78 | public void setNumberOfVariables(int numberOfVariables) { |
| 79 | this.numberOfVariables = numberOfVariables; |
| 80 | variableMap.trimToSize(); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Allocate a slot number for a variable |
| 85 | * |
| 86 | * @param qName the name of the variable |
| 87 | * @return the allocated slot number (the next one available) |
| 88 | */ |
| 89 | |
| 90 | public int allocateSlotNumber(StructuredQName qName) { |
| 91 | variableMap.add(qName); |
| 92 | return numberOfVariables++; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Get the variable map (simply a list of variable names as structured QNames). Note that it |
| 97 | * is possible for several variables to have the same name. |
| 98 | * |
| 99 | * @return the list of variable names for this stack frame |
| 100 | */ |
| 101 | |
| 102 | /*@NotNull*/ |
| 103 | public List<StructuredQName> getVariableMap() { |
| 104 | return variableMap; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Display the values of the variables and parameters in an XPathContext stack frame |
| 109 | * to a Logger |
| 110 | * @param context the XPathContext holding the stack frame |
| 111 | * @param logger the destination for the output |
| 112 | */ |
| 113 | |
| 114 | public void showStackFrame(XPathContext context, Logger logger) { |
| 115 | // Saxon-HE version does nothing |
| 116 | } |
| 117 | |
| 118 | } |
| 119 |