Debian Codegraph @

net/sf/saxon/expr/flwor/TupleExpression.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.flwor;
9
10 import net.sf.saxon.expr.*;
11 import net.sf.saxon.expr.oper.OperandArray;
12 import net.sf.saxon.expr.parser.ContextItemStaticInfo;
13 import net.sf.saxon.expr.parser.ExpressionTool;
14 import net.sf.saxon.expr.parser.ExpressionVisitor;
15 import net.sf.saxon.expr.parser.RebindingMap;
16 import net.sf.saxon.om.Sequence;
17 import net.sf.saxon.trace.ExpressionPresenter;
18 import net.sf.saxon.trans.XPathException;
19 import net.sf.saxon.type.ItemType;
20
21 import java.util.ArrayList;
22 import java.util.List;
23
24 /**
25 * A tuple expression is an expression that returns a tuple. Specifically,
26 * it is a list local variables references; it returns a Tuple item
27 * containing the current value of these variables.
28 */
29 public class TupleExpression extends Expression {
30
31 private OperandArray operanda;
32
33 public TupleExpression() {
34 }
35
36 /**
37 * Set the data structure for the operands of this expression. This must be created during initialisation of the
38 * expression and must not be subsequently changed
39 * @param operanda the data structure for expression operands
40 */
41
42 protected void setOperanda(OperandArray operanda) {
43 this.operanda = operanda;
44 }
45
46 /**
47 * Get the data structure holding the operands of this expression.
48 * @return the data structure holding expression operands
49 */
50
51 protected OperandArray getOperanda() {
52 return operanda;
53 }
54
55 @Override
56 public Iterable<Operand> operands() {
57 return operanda.operands();
58 }
59
60
61 public void setVariables(List<LocalVariableReference> refs) {
62 Expression[] e = new Expression[refs.size()];
63 for (int i=0; i<refs.size(); i++) {
64 e[i] = refs.get(i);
65 }
66 setOperanda(new OperandArray(this, e, OperandRole.SAME_FOCUS_ACTION));
67 }
68
69 public int getSize() {
70 return getOperanda().getNumberOfOperands();
71 }
72
73 public LocalVariableReference getSlot(int i) {
74 return (LocalVariableReference)getOperanda().getOperandExpression(i);
75 }
76
77 public void setSlot(int i, LocalVariableReference ref) {
78 getOperanda().setOperand(i, ref);
79 }
80
81 public boolean includesBinding(Binding binding) {
82 for (Operand o : operands()) {
83 if (((LocalVariableReference)o.getChildExpression()).getBinding() == binding) {
84 return true;
85 }
86 }
87 return false;
88 }
89
90 /*@NotNull*/
91 public ItemType getItemType() {
92 return getConfiguration().getJavaExternalObjectType(Object.class);
93 }
94
95 /*@NotNull*/
96 public Expression typeCheck(ExpressionVisitor visitor,
97 /*@Nullable*/ ContextItemStaticInfo contextInfo)
98 throws XPathException {
99 for (int i=0; i<getSize(); i++) {
100 operanda.getOperand(i).typeCheck(visitor, contextInfo);
101 }
102 return this;
103 }
104
105 /**
106 * An implementation of Expression must provide at least one of the methods evaluateItem(), iterate(), or process().
107 * This method indicates which of these methods is provided directly. The other methods will always be available
108 * indirectly, using an implementation that relies on one of the other methods.
109 *
110 * @return the implementation method, for example {@link #ITERATE_METHOD} or {@link #EVALUATE_METHOD} or
111 * {@link #PROCESS_METHOD}
112 */
113 @Override
114 public int getImplementationMethod() {
115 return EVALUATE_METHOD;
116 }
117
118 /**
119 * Is this expression the same as another expression?
120 *
121 * @param other the expression to be compared with this one
122 * @return true if the two expressions are statically equivalent
123 */
124 @Override
125 public boolean equals(Object other) {
126 if (!(other instanceof TupleExpression)) {
127 return false;
128 } else {
129 TupleExpression t2 = (TupleExpression)other;
130 if (getOperanda().getNumberOfOperands() != t2.getOperanda().getNumberOfOperands()) {
131 return false;
132 }
133 for (int i=0; i<getSize(); i++) {
134 if (!getSlot(i).isEqual(t2.getSlot(i))) {
135 return false;
136 }
137 }
138 return true;
139 }
140 }
141
142 /**
143 * Hashcode supporting equals()
144 */
145
146 public int computeHashCode() {
147 int h = 77;
148 for (Operand o : operands()) {
149 h ^= o.getChildExpression().hashCode();
150 }
151 return h;
152 }
153
154 /**
155 * Copy an expression. This makes a deep copy.
156 *
157 * @return the copy of the original expression
158 * @param rebindings
159 */
160
161 /*@NotNull*/
162 public Expression copy(RebindingMap rebindings) {
163 int n = getOperanda().getNumberOfOperands();
164 List<LocalVariableReference> refs2 = new ArrayList<LocalVariableReference>(n);
165 for (int i = 0; i < n; i++) {
166 refs2.add ((LocalVariableReference) getSlot(i).copy(rebindings));
167 }
168 TupleExpression t2 = new TupleExpression();
169 ExpressionTool.copyLocationInfo(this, t2);
170 t2.setVariables(refs2);
171 return t2;
172 }
173
174 /**
175 * Diagnostic print of expression structure. The abstract expression tree
176 * is written to the supplied output destination.
177 */
178
179 public void export(ExpressionPresenter out) throws XPathException {
180 out.startElement("tuple", this);
181 for (Operand o : operands()) {
182 o.getChildExpression().export(out);
183 }
184 out.endElement();
185 }
186
187
188 public Tuple evaluateItem(XPathContext context) throws XPathException {
189 final int n = getSize();
190 Sequence[] tuple = new Sequence[n];
191 for (int i=0; i<n; i++) {
192 tuple[i] = getSlot(i).evaluateVariable(context);
193 }
194 return new Tuple(tuple);
195 }
196
197 /**
198 * Get a name identifying the kind of expression, in terms meaningful to a user.
199 *
200 * @return a name identifying the kind of expression, in terms meaningful to a user.
201 * The name will always be in the form of a lexical XML QName, and should match the name used
202 * in export() output displaying the expression.
203 */
204 @Override
205 public String getExpressionName() {
206 return "tuple";
207 }
208
209
210 /**
211 * Set the local variables in the current stack frame to values corresponding to a supplied tuple
212 *
213 * @param context identifies the stack frame to be modified
214 * @param tuple the tuple containing the current values
215 */
216
217 public void setCurrentTuple(XPathContext context, Tuple tuple) throws XPathException {
218 Sequence[] members = tuple.getMembers();
219 int n = getSize();
220 for (int i = 0; i < n; i++) {
221 context.setLocalVariable(getSlot(i).getBinding().getLocalSlotNumber(), members[i]);
222 }
223 }
224
225 /**
226 * Get the cardinality of the expression. This is exactly one, in the sense
227 * that evaluating the TupleExpression returns a single tuple.
228 *
229 * @return the static cardinality - EXACTLY_ONE
230 */
231
232 public int computeCardinality() {
233 return StaticProperty.EXACTLY_ONE;
234 }
235
236 public int getIntrinsicDependencies() {
237 return 0;
238 }
239
240 }
241