Debian Codegraph @

net/sf/saxon/expr/instruct/Fork.java

raw symbols SCIP
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.Expression;
11 import net.sf.saxon.expr.Operand;
12 import net.sf.saxon.expr.OperandRole;
13 import net.sf.saxon.expr.XPathContext;
14 import net.sf.saxon.expr.parser.ExpressionTool;
15 import net.sf.saxon.expr.parser.RebindingMap;
16 import net.sf.saxon.om.StandardNames;
17 import net.sf.saxon.trace.ExpressionPresenter;
18 import net.sf.saxon.trans.XPathException;
19 import net.sf.saxon.type.AnyItemType;
20 import net.sf.saxon.type.ErrorType;
21 import net.sf.saxon.type.ItemType;
22 import net.sf.saxon.type.Type;
23
24 import java.util.Arrays;
25
26
27 /**
28 * This class implements an xsl:fork expression.
29 */
30
31 public class Fork extends Instruction {
32
33 Operand[] operanda;
34
35 /**
36 * Create a Fork instruction
37 * @param prongs the children of the xsl:fork instructions.
38 */
39
40 public Fork(Operand[] prongs) {
41 operanda = new Operand[prongs.length];
42 for (int i=0; i<prongs.length; i++) {
43 operanda[i] = new Operand(this, prongs[i].getChildExpression(), OperandRole.SAME_FOCUS_ACTION);
44 }
45 }
46
47 public Fork(Expression[] prongs) {
48 operanda = new Operand[prongs.length];
49 for (int i = 0; i < prongs.length; i++) {
50 operanda[i] = new Operand(this, prongs[i], OperandRole.SAME_FOCUS_ACTION);
51 }
52 }
53
54 @Override
55 public Iterable<Operand> operands() {
56 return Arrays.asList(operanda);
57 }
58
59 /**
60 * Get the namecode of the instruction for use in diagnostics
61 *
62 * @return a code identifying the instruction: typically but not always
63 * the fingerprint of a name in the XSLT namespace
64 */
65
66 public int getInstructionNameCode() {
67 return StandardNames.XSL_FORK;
68 }
69
70 /**
71 * Get the number of prongs in the fork
72 * @return the number of xsl:sequence children
73 */
74
75 public int getSize() {
76 return operanda.length;
77 }
78
79 /**
80 * Get the n'th child expression of the xsl:fork (zero-based)
81 * @param i the index of the required subexpression
82 * @return the i'th subexpression if it exists
83 * @throws IllegalArgumentException if i is out of range
84 */
85
86 public Expression getProng(int i) {
87 return operanda[i].getChildExpression();
88 }
89
90 /**
91 * Determine the item type of the value returned by the function
92 */
93
94 /*@NotNull*/
95 public ItemType getItemType() {
96 if (getSize() == 0) {
97 return ErrorType.getInstance();
98 }
99 ItemType t1 = null;
100 for (Operand o : operands()) {
101 ItemType t2 = o.getChildExpression().getItemType();
102 t1 = t1 == null ? t2 : Type.getCommonSuperType(t1, t2);
103 if (t1 instanceof AnyItemType) {
104 return t1; // no point going any further
105 }
106 }
107 return t1;
108 }
109
110 /**
111 * Get the (partial) name of a class that supports streaming of this kind of expression
112 *
113 * @return the partial name of a class that can be instantiated to provide streaming support in Saxon-EE,
114 * or null if there is no such class
115 */
116 @Override
117 public String getStreamerName() {
118 return "Fork";
119 }
120
121 /**
122 * Copy an expression. This makes a deep copy.
123 *
124 * @return the copy of the original expression
125 * @param rebindings
126 */
127
128 /*@NotNull*/
129 public Expression copy(RebindingMap rebindings) {
130 Expression[] e2 = new Expression[getSize()];
131 int i=0;
132 for (Operand o : operands()) {
133 e2[i++] = o.getChildExpression().copy(rebindings);
134 }
135 Fork f2 = new Fork(e2);
136 ExpressionTool.copyLocationInfo(this, f2);
137 return f2;
138 }
139
140 /**
141 * Process the instruction, without returning any tail calls. This is the
142 * non-streamed mode of operation, which processes the child expressions
143 * in turn exactly like a sequence constructor.
144 *
145 * @param context The dynamic context, giving access to the current node,
146 * the current variables, etc.
147 */
148 @Override
149 public TailCall processLeavingTail(XPathContext context) throws XPathException {
150 for (Operand o : operands()) {
151 o.getChildExpression().process(context);
152 }
153 return null;
154 }
155
156 /**
157 * Diagnostic print of expression structure. The abstract expression tree
158 * is written to the supplied output destination.
159 *
160 * @param out the expression presenter used to display the structure
161 */
162
163 public void export(ExpressionPresenter out) throws XPathException {
164 out.startElement("fork", this);
165 for (Operand o : operands()) {
166 o.getChildExpression().export(out);
167 }
168 out.endElement();
169 }
170
171
172 }