Debian Codegraph @

net/sf/saxon/expr/UnaryExpression.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;
9
10 import net.sf.saxon.Configuration;
11 import net.sf.saxon.expr.parser.ContextItemStaticInfo;
12 import net.sf.saxon.expr.parser.ExpressionTool;
13 import net.sf.saxon.expr.parser.ExpressionVisitor;
14 import net.sf.saxon.trace.ExpressionPresenter;
15 import net.sf.saxon.trans.XPathException;
16 import net.sf.saxon.tree.jiter.MonoIterator;
17 import net.sf.saxon.type.ItemType;
18
19 /**
20 * Unary Expression: an expression taking a single operand expression
21 */
22
23 public abstract class UnaryExpression extends Expression {
24
25 private Operand operand;
26
27 public UnaryExpression(Expression p0) {
28 operand = new Operand(this, p0, getOperandRole());
29 // if (p0.getRetainedStaticContext() != null) {
30 // setRetainedStaticContext(p0.getRetainedStaticContext());
31 // }
32 ExpressionTool.copyLocationInfo(p0, this);
33 }
34
35 /*@NotNull*/
36 public Expression getBaseExpression() {
37 return operand.getChildExpression();
38 }
39
40 public void setBaseExpression(Expression child) {
41 operand.setChildExpression(child);
42 }
43
44 public Operand getOperand() {
45 return operand;
46 }
47
48 @Override
49 public Iterable<Operand> operands() {
50 return () -> new MonoIterator<>(operand);
51 }
52
53 /**
54 * Get the usage (in terms of streamability analysis) of the single operand
55 * @return the operand usage
56 */
57
58 protected abstract OperandRole getOperandRole();
59
60 /**
61 * Type-check the expression. Default implementation for unary operators that accept
62 * any kind of operand
63 */
64
65 /*@NotNull*/
66 public Expression typeCheck(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException {
67 operand.typeCheck(visitor, contextInfo);
68
69 // if the operand value is known, pre-evaluate the expression
70 try {
71 if (getBaseExpression() instanceof Literal) {
72 Expression e2 = Literal.makeLiteral(
73 iterate(visitor.getStaticContext().makeEarlyEvaluationContext()).materialize(), this);
74 ExpressionTool.copyLocationInfo(this, e2);
75 return e2;
76 }
77 //return (Value)ExpressionTool.eagerEvaluate(this, env.makeEarlyEvaluationContext());
78 } catch (Exception err) {
79 // if early evaluation fails, suppress the error: the value might
80 // not be needed at run-time
81 }
82 return this;
83 }
84
85 /**
86 * Perform optimisation of an expression and its subexpressions.
87 * <p>This method is called after all references to functions and variables have been resolved
88 * to the declaration of the function or variable, and after all type checking has been done.</p>
89 *
90 * @param visitor an expression visitor
91 * @param contextInfo the static type of "." at the point where this expression is invoked.
92 * The parameter is set to null if it is known statically that the context item will be undefined.
93 * If the type of the context item is not known statically, the argument is set to
94 * {@link net.sf.saxon.type.Type#ITEM_TYPE}
95 * @return the original expression, rewritten if appropriate to optimize execution
96 * @throws XPathException if an error is discovered during this phase
97 * (typically a type error)
98 */
99
100 /*@NotNull*/
101 public Expression optimize(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException {
102 operand.optimize(visitor, contextInfo);
103 // if the operand value is known, pre-evaluate the expression
104 Expression base = getBaseExpression();
105 try {
106 if (base instanceof Literal) {
107 return Literal.makeLiteral(
108 iterate(visitor.getStaticContext().makeEarlyEvaluationContext()).materialize(), this);
109 }
110 } catch (XPathException err) {
111 // if early evaluation fails, suppress the error: the value might
112 // not be needed at run-time
113 }
114 return this;
115 }
116
117 /**
118 * Get the static properties of this expression (other than its type). The result is
119 * bit-signficant. These properties are used for optimizations. In general, if
120 * property bit is set, it is true, but if it is unset, the value is unknown.
121 */
122
123 public int computeSpecialProperties() {
124 return getBaseExpression().getSpecialProperties();
125 }
126
127 /**
128 * Determine the static cardinality. Default implementation returns the cardinality of the operand
129 */
130
131 public int computeCardinality() {
132 return getBaseExpression().getCardinality();
133 }
134
135 /**
136 * Determine the data type of the expression, if possible. The default
137 * implementation for unary expressions returns the item type of the operand
138 *
139 * @return the item type of the items in the result sequence, insofar as this
140 * is known statically.
141 */
142
143 /*@NotNull*/
144 public ItemType getItemType() {
145 return getBaseExpression().getItemType();
146 }
147
148 /**
149 * Is this expression the same as another expression?
150 */
151
152 public boolean equals(/*@Nullable*/ Object other) {
153 return other != null && this.getClass().equals(other.getClass()) &&
154 this.getBaseExpression().isEqual(((UnaryExpression) other).getBaseExpression());
155 }
156
157 /**
158 * get HashCode for comparing two expressions. Note that this hashcode gives the same
159 * result for (A op B) and for (B op A), whether or not the operator is commutative.
160 */
161
162 public int computeHashCode() {
163 return ("UnaryExpression " + getClass()).hashCode() ^ getBaseExpression().hashCode();
164 }
165
166 /**
167 * The toString() method for an expression attempts to give a representation of the expression
168 * in an XPath-like form, but there is no guarantee that the syntax will actually be true XPath.
169 * In the case of XSLT instructions, the toString() method gives an abstracted view of the syntax
170 */
171
172 public String toString() {
173 return getExpressionName() + "(" + getBaseExpression() + ")";
174 }
175
176 @Override
177 public String toShortString() {
178 return getExpressionName() + "(" + getBaseExpression().toShortString() + ")";
179 }
180
181 /**
182 * Diagnostic print of expression structure. The abstract expression tree
183 * is written to the supplied output destination.
184 */
185
186 public void export(ExpressionPresenter out) throws XPathException {
187 String name = getExpressionName();
188 if (name == null) {
189 out.startElement("unaryOperator", this);
190 String op = displayOperator(out.getConfiguration());
191 if (op != null) {
192 out.emitAttribute("op", op);
193 }
194 } else {
195 out.startElement(name, this);
196 }
197 getBaseExpression().export(out);
198 out.endElement();
199 }
200
201 /**
202 * Give a string representation of the operator for use in diagnostics
203 *
204 * @param config the Saxon configuration
205 * @return the operator, as a string
206 */
207
208 /*@Nullable*/
209 protected String displayOperator(Configuration config) {
210 return null;
211 }
212
213
214
215 }