Debian Codegraph @

net/sf/saxon/ma/arrays/SquareArrayConstructor.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.ma.arrays;
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.GroundedValue;
17 import net.sf.saxon.om.Item;
18 import net.sf.saxon.trace.ExpressionPresenter;
19 import net.sf.saxon.trans.XPathException;
20 import net.sf.saxon.type.*;
21 import net.sf.saxon.value.Cardinality;
22 import net.sf.saxon.value.SequenceType;
23
24 import java.util.ArrayList;
25 import java.util.List;
26
27
28 /**
29 * An expression that delivers a fixed size array whose members are the result of evaluating
30 * corresponding expressions: [a,b,c,d]
31 */
32
33 public class SquareArrayConstructor extends Expression {
34
35
36 private OperandArray operanda;
37
38
39 /**
40 * Create an array constructor with defined members
41 * @param children expressions defining the members (comma-separated subexpressions)
42 */
43
44 public SquareArrayConstructor(List<Expression> children) {
45 Expression[] kids = children.toArray(new Expression[0]);
46 for (Expression e : children) {
47 adoptChildExpression(e);
48 }
49 setOperanda(new OperandArray(this, kids, OperandRole.NAVIGATE));
50 }
51
52 /**
53 * Set the data structure for the operands of this expression. This must be created during initialisation of the
54 * expression and must not be subsequently changed
55 *
56 * @param operanda the data structure for expression operands
57 */
58
59 protected void setOperanda(OperandArray operanda) {
60 this.operanda = operanda;
61 }
62
63 /**
64 * Get the data structure holding the operands of this expression.
65 *
66 * @return the data structure holding expression operands
67 */
68
69 public OperandArray getOperanda() {
70 return operanda;
71 }
72
73 public Operand getOperand(int i) {
74 return operanda.getOperand(i);
75 }
76
77 @Override
78 public Iterable<Operand> operands() {
79 return operanda.operands();
80 }
81
82
83 public String getExpressionName() {
84 return "SquareArrayConstructor";
85 }
86
87 public String getStreamerName() {
88 return "ArrayBlock";
89 }
90
91
92 public int computeSpecialProperties() {
93 return 0;
94 }
95
96 /**
97 * Is this expression the same as another expression?
98 *
99 * @param other the expression to be compared with this one
100 * @return true if the two expressions are statically equivalent
101 */
102 @Override
103 public boolean equals(Object other) {
104 if (!(other instanceof SquareArrayConstructor)) {
105 return false;
106 } else {
107 SquareArrayConstructor ab2 = (SquareArrayConstructor) other;
108 if (ab2.getOperanda().getNumberOfOperands() != getOperanda().getNumberOfOperands()) {
109 return false;
110 }
111 for (int i = 0; i < getOperanda().getNumberOfOperands(); i++) {
112 if (!getOperanda().getOperand(i).equals(ab2.getOperanda().getOperand(i))) {
113 return false;
114 }
115 }
116 return true;
117 }
118 }
119
120 /**
121 * Hashcode supporting equals()
122 */
123
124 public int computeHashCode() {
125 int h = 0x878b92a0;
126 for (Operand o : operands()) {
127 h ^= o.getChildExpression().hashCode();
128 }
129 return h;
130 }
131
132 @Override
133 public Expression typeCheck(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException {
134 Expression e = super.typeCheck(visitor, contextInfo);
135 if (e != this) {
136 return e;
137 }
138 return preEvaluate(visitor);
139 }
140
141 @Override
142 public Expression optimize(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException {
143 Expression e = super.optimize(visitor, contextInfo);
144 if (e != this) {
145 return e;
146 }
147 return preEvaluate(visitor);
148 }
149
150 private Expression preEvaluate(ExpressionVisitor visitor) {
151 boolean allFixed = false;
152 for (Operand o : operands()) {
153 if (!(o.getChildExpression() instanceof Literal)) {
154 return this;
155 }
156 }
157 try {
158 return Literal.makeLiteral(evaluateItem(visitor.makeDynamicContext()), this);
159 } catch (XPathException e) {
160 return this;
161 }
162 }
163
164 /**
165 * Copy an expression. This makes a deep copy.
166 *
167 * @return the copy of the original expression
168 * @param rebindings variables that need to be re-bound
169 */
170
171 /*@NotNull*/
172 public Expression copy(RebindingMap rebindings) {
173 List<Expression> m2 = new ArrayList<>(getOperanda().getNumberOfOperands());
174 for (Operand o : operands()) {
175 m2.add(o.getChildExpression().copy(rebindings));
176 }
177 SquareArrayConstructor b2 = new SquareArrayConstructor(m2);
178 ExpressionTool.copyLocationInfo(this, b2);
179 return b2;
180 }
181
182 /**
183 * Determine the data type of the items returned by this expression
184 *
185 * @return the data type
186 */
187
188 /*@NotNull*/
189 public final ItemType getItemType() {
190 ItemType contentType = null;
191 int contentCardinality = StaticProperty.EXACTLY_ONE;
192 TypeHierarchy th = getConfiguration().getTypeHierarchy();
193 for (Expression e : getOperanda().operandExpressions()) {
194 if (contentType == null) {
195 contentType = e.getItemType();
196 contentCardinality = e.getCardinality();
197 } else {
198 contentType = Type.getCommonSuperType(contentType, e.getItemType(), th);
199 contentCardinality = Cardinality.union(contentCardinality, e.getCardinality());
200 }
201 }
202 if (contentType == null) {
203 contentType = ErrorType.getInstance();
204 }
205 return new ArrayItemType(SequenceType.makeSequenceType(contentType, contentCardinality));
206 }
207
208 /**
209 * Get the static type of the expression as a UType, following precisely the type
210 * inference rules defined in the XSLT 3.0 specification.
211 *
212 * @param contextItemType the static type of the context item
213 * @return the static item type of the expression according to the XSLT 3.0 defined rules
214 */
215 @Override
216 public UType getStaticUType(UType contextItemType) {
217 return UType.FUNCTION;
218 }
219
220 /**
221 * Determine the cardinality of the expression
222 */
223
224 public final int computeCardinality() {
225 // An array is an item!
226 return StaticProperty.EXACTLY_ONE;
227 }
228
229 /**
230 * Diagnostic print of expression structure. The abstract expression tree
231 * is written to the supplied output destination.
232 */
233
234 public void export(ExpressionPresenter out) throws XPathException {
235 out.startElement("arrayBlock", this);
236 for (Operand o : operands()) {
237 o.getChildExpression().export(out);
238 }
239 out.endElement();
240 }
241
242 @Override
243 public String toShortString() {
244 int n = getOperanda().getNumberOfOperands();
245 switch (n) {
246 case 0:
247 return "[]";
248 case 1:
249 return "[" + getOperanda().getOperand(0).getChildExpression().toShortString() + "]";
250 case 2:
251 return "[" + getOperanda().getOperand(0).getChildExpression().toShortString() + ", " +
252 getOperanda().getOperand(1).getChildExpression().toShortString() + "]";
253 default:
254 return "[" + getOperanda().getOperand(0).getChildExpression().toShortString() + ", ...]";
255 }
256 }
257
258 /**
259 * An implementation of Expression must provide at least one of the methods evaluateItem(), iterate(), or process().
260 * This method indicates which of these methods is provided. This implementation provides both iterate() and
261 * process() methods natively.
262 */
263
264 public int getImplementationMethod() {
265 return EVALUATE_METHOD;
266 }
267
268 /**
269 * Evaluate an expression as a single item. This always returns either a single Item or
270 * null (denoting the empty sequence). No conversion is done. This method should not be
271 * used unless the static type of the expression is a subtype of "item" or "item?": that is,
272 * it should not be called if the expression may return a sequence. There is no guarantee that
273 * this condition will be detected.
274 *
275 * @param context The context in which the expression is to be evaluated
276 * @return the node or atomic value that results from evaluating the
277 * expression; or null to indicate that the result is an empty
278 * sequence
279 * @throws net.sf.saxon.trans.XPathException if any dynamic error occurs evaluating the
280 * expression
281 */
282 @Override
283 public Item evaluateItem(XPathContext context) throws XPathException {
284 List<GroundedValue<?>> value = new ArrayList<>(getOperanda().getNumberOfOperands());
285 for (Operand o : operands()) {
286 GroundedValue<?> s = ExpressionTool.eagerEvaluate(o.getChildExpression(), context);
287 value.add(s);
288 }
289 return new SimpleArrayItem(value);
290 }
291
292 }