Debian Codegraph @

net/sf/saxon/expr/flwor/LetClause.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.parser.*;
12 import net.sf.saxon.trace.ExpressionPresenter;
13 import net.sf.saxon.trans.XPathException;
14 import net.sf.saxon.tree.util.FastStringBuffer;
15 import net.sf.saxon.type.ItemType;
16
17 import java.util.List;
18
19 /**
20 * A "let" clause in a FLWOR expression
21 */
22 public class LetClause extends Clause {
23
24 private LocalVariableBinding rangeVariable;
25 private Operand sequenceOp;
26 private Evaluator evaluator;
27
28 @Override
29 public int getClauseKey() {
30 return LET;
31 }
32
33 public Evaluator getEvaluator() {
34 if (evaluator == null) {
35 evaluator = ExpressionTool.lazyEvaluator(getSequence(), true);
36 }
37 return evaluator;
38 }
39
40 public LetClause copy(FLWORExpression flwor, RebindingMap rebindings) {
41 LetClause let2 = new LetClause();
42 let2.setLocation(getLocation());
43 let2.setPackageData(getPackageData());
44 let2.rangeVariable = rangeVariable.copy();
45 let2.initSequence(flwor, getSequence().copy(rebindings));
46 return let2;
47 }
48
49 public void initSequence(FLWORExpression flwor, Expression sequence) {
50 sequenceOp = new Operand(flwor, sequence, isRepeated() ? OperandRole.REPEAT_NAVIGATE : OperandRole.NAVIGATE);
51 }
52
53 public void setSequence(Expression sequence) {
54 sequenceOp.setChildExpression(sequence);
55 }
56
57 public Expression getSequence() {
58 return sequenceOp.getChildExpression();
59 }
60
61
62 public void setRangeVariable(LocalVariableBinding binding) {
63 this.rangeVariable = binding;
64 }
65
66 public LocalVariableBinding getRangeVariable() {
67 return rangeVariable;
68 }
69
70 /**
71 * Get the number of variables bound by this clause
72 *
73 * @return the number of variable bindings
74 */
75 @Override
76 public LocalVariableBinding[] getRangeVariables() {
77 return new LocalVariableBinding[]{rangeVariable};
78 }
79
80 /**
81 * Get a tuple stream that implements the functionality of this clause, taking its
82 * input from another tuple stream which this clause modifies
83 *
84 * @param base the input tuple stream
85 * @param context
86 * @return the output tuple stream
87 */
88
89 @Override
90 public TuplePull getPullStream(TuplePull base, XPathContext context) {
91 return new LetClausePull(base, this);
92 }
93
94 /**
95 * Get a push-mode tuple stream that implements the functionality of this clause, supplying its
96 * output to another tuple stream
97 *
98 * @param destination the output tuple stream
99 * @param context
100 * @return the push tuple stream that implements the functionality of this clause of the FLWOR
101 * expression
102 */
103 @Override
104 public TuplePush getPushStream(TuplePush destination, XPathContext context) {
105 return new LetClausePush(destination, this);
106 }
107
108 /**
109 * Process the subexpressions of this clause
110 *
111 * @param processor the expression processor used to process the subexpressions
112 */
113 @Override
114 public void processOperands(OperandProcessor processor) throws XPathException {
115 processor.processOperand(sequenceOp);
116 }
117
118 /**
119 * Type-check the expression
120 */
121
122 public void typeCheck(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException {
123 RoleDiagnostic role = new RoleDiagnostic(RoleDiagnostic.VARIABLE, rangeVariable.getVariableQName().getDisplayName(), 0);
124 setSequence(TypeChecker.strictTypeCheck(
125 getSequence(), rangeVariable.getRequiredType(), role, visitor.getStaticContext()));
126 evaluator = ExpressionTool.lazyEvaluator(getSequence(), true);
127 }
128
129 @Override
130 public void gatherVariableReferences(final ExpressionVisitor visitor, Binding binding, List<VariableReference> references) {
131 ExpressionTool.gatherVariableReferences(getSequence(), binding, references);
132 }
133
134 @Override
135 public void refineVariableType(ExpressionVisitor visitor, List<VariableReference> references, Expression returnExpr) {
136 final Expression seq = getSequence();
137 final ItemType actualItemType = seq.getItemType();
138 for (VariableReference ref : references) {
139 ref.refineVariableType(actualItemType, getSequence().getCardinality(),
140 seq instanceof Literal ? ((Literal) seq).getValue() : null,
141 seq.getSpecialProperties());
142 ExpressionTool.resetStaticProperties(returnExpr);
143 }
144 }
145
146 // /**
147 // * Provide additional information about the type of the variable, typically derived by analyzing
148 // * the initializer of the variable binding
149 // * @param type the item type of the variable
150 // * @param cardinality the cardinality of the variable
151 // * @param constantValue the actual value of the variable, if this is known statically, otherwise null
152 // * @param properties additional static properties of the variable's initializer
153 // * @param visitor an ExpressionVisitor
154 // */
155
156 // public void refineVariableType(
157 // ItemType type, int cardinality, Value constantValue, int properties, ExpressionVisitor visitor) {
158 // Executable exec = visitor.getExecutable();
159 // if (exec == null) {
160 // // happens during use-when evaluation
161 // return;
162 // }
163 // TypeHierarchy th = exec.getConfiguration().getTypeHierarchy();
164 // ItemType oldItemType = rangeVariable.getRequiredType().getPrimaryType();
165 // ItemType newItemType = oldItemType;
166 // if (th.isSubType(type, oldItemType)) {
167 // newItemType = type;
168 // }
169 // if (oldItemType instanceof NodeTest && type instanceof AtomicType) {
170 // // happens when all references are flattened
171 // newItemType = type;
172 // }
173 // int newcard = cardinality & rangeVariable.getRequiredType().getCardinality();
174 // if (newcard==0) {
175 // // this will probably lead to a type error later
176 // newcard = rangeVariable.getRequiredType().getCardinality();
177 // }
178 // SequenceType seqType = SequenceType.makeSequenceType(newItemType, newcard);
179 // //setStaticType(seqType, constantValue, properties);
180 // }
181 @Override
182 public void addToPathMap(PathMap pathMap, PathMap.PathMapNodeSet pathMapNodeSet) {
183 PathMap.PathMapNodeSet varPath = getSequence().addToPathMap(pathMap, pathMapNodeSet);
184 pathMap.registerPathForVariable(rangeVariable, varPath);
185 }
186
187 /**
188 * Diagnostic print of expression structure. The abstract expression tree
189 * is written to the supplied output destination.
190 *
191 * @param out the expression presenter used to display the structure
192 */
193 @Override
194 public void explain(ExpressionPresenter out) throws XPathException {
195 out.startElement("let");
196 out.emitAttribute("var", getRangeVariable().getVariableQName());
197 out.emitAttribute("slot", getRangeVariable().getLocalSlotNumber() + "");
198 getSequence().export(out);
199 out.endElement();
200 }
201
202 public String toShortString() {
203 FastStringBuffer fsb = new FastStringBuffer(FastStringBuffer.C64);
204 fsb.append("let $");
205 fsb.append(rangeVariable.getVariableQName().getDisplayName());
206 fsb.append(" := ");
207 fsb.append(getSequence().toShortString());
208 return fsb.toString();
209 }
210
211 public String toString() {
212 FastStringBuffer fsb = new FastStringBuffer(FastStringBuffer.C64);
213 fsb.append("let $");
214 fsb.append(rangeVariable.getVariableQName().getDisplayName());
215 fsb.append(" := ");
216 fsb.append(getSequence().toString());
217 return fsb.toString();
218 }
219 }
220