Debian Codegraph @

net/sf/saxon/expr/NegateExpression.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.*;
12 import net.sf.saxon.om.GroundedValue;
13 import net.sf.saxon.trace.ExpressionPresenter;
14 import net.sf.saxon.trans.XPathException;
15 import net.sf.saxon.type.ItemType;
16 import net.sf.saxon.value.DoubleValue;
17 import net.sf.saxon.value.NumericValue;
18 import net.sf.saxon.value.SequenceType;
19
20 /**
21 * Negate Expression: implements the unary minus operator.
22 * This expression is initially created as an ArithmeticExpression (or in backwards
23 * compatibility mode, an ArithmeticExpression10) to take advantage of the type checking code.
24 * So we don't need to worry about type checking or argument conversion.
25 */
26
27 public class NegateExpression extends UnaryExpression {
28
29 private boolean backwardsCompatible;
30
31 /**
32 * Create a NegateExpression
33 *
34 * @param base the expression that computes the value whose sign is to be reversed
35 */
36
37 public NegateExpression(Expression base) {
38 super(base);
39 }
40
41 /**
42 * Set whether the expression is to be evaluated in XPath 1.0 compatibility mode
43 *
44 * @param compatible true if XPath 1.0 compatibility mode is enabled
45 */
46
47 public void setBackwardsCompatible(boolean compatible) {
48 backwardsCompatible = compatible;
49 }
50
51 /**
52 * Ask whether the expression is to be evaluated in XPath 1.0 compatibility mode
53 *
54 * @return true if XPath 1.0 compatibility mode is enabled
55 */
56
57 public boolean isBackwardsCompatible() {
58 return backwardsCompatible;
59 }
60
61 protected OperandRole getOperandRole() {
62 return OperandRole.SINGLE_ATOMIC;
63 }
64
65 /*@NotNull*/
66 public Expression typeCheck(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException {
67 getOperand().typeCheck(visitor, contextInfo);
68 RoleDiagnostic role = new RoleDiagnostic(RoleDiagnostic.UNARY_EXPR, "-", 0);
69 Expression operand = visitor.getConfiguration().getTypeChecker(backwardsCompatible).staticTypeCheck(
70 getBaseExpression(), SequenceType.OPTIONAL_NUMERIC,
71 role, visitor);
72 setBaseExpression(operand);
73 if (operand instanceof Literal) {
74 GroundedValue v = ((Literal) operand).getValue();
75 if (v instanceof NumericValue) {
76 return Literal.makeLiteral(((NumericValue) v).negate(), this);
77 }
78 }
79 return this;
80 }
81
82 /**
83 * Determine the data type of the expression, if this is known statically
84 */
85
86 /*@NotNull*/
87 public ItemType getItemType() {
88 return getBaseExpression().getItemType();
89 }
90
91 /**
92 * An implementation of Expression must provide at least one of the methods evaluateItem(), iterate(), or process().
93 * This method indicates which of these methods is provided directly. The other methods will always be available
94 * indirectly, using an implementation that relies on one of the other methods.
95 *
96 * @return the implementation method, for example {@link #ITERATE_METHOD} or {@link #EVALUATE_METHOD} or
97 * {@link #PROCESS_METHOD}
98 */
99 @Override
100 public int getImplementationMethod() {
101 return EVALUATE_METHOD;
102 }
103
104 /**
105 * Evaluate the expression.
106 */
107
108 public NumericValue evaluateItem(XPathContext context) throws XPathException {
109
110 NumericValue v1 = (NumericValue) getBaseExpression().evaluateItem(context);
111 if (v1 == null) {
112 return backwardsCompatible ? DoubleValue.NaN : null;
113 }
114 return v1.negate();
115 }
116
117
118 /**
119 * Copy an expression. This makes a deep copy.
120 *
121 * @return the copy of the original expression
122 * @param rebindings
123 */
124
125 /*@NotNull*/
126 public Expression copy(RebindingMap rebindings) {
127 NegateExpression exp = new NegateExpression(getBaseExpression().copy(rebindings));
128 ExpressionTool.copyLocationInfo(this, exp);
129 return exp;
130 }
131
132 protected String displayOperator(Configuration config) {
133 return "-";
134 }
135
136 /**
137 * Get a name identifying the kind of expression, in terms meaningful to a user.
138 *
139 * @return a name identifying the kind of expression, in terms meaningful to a user.
140 * The name will always be in the form of a lexical XML QName, and should match the name used
141 * in explain() output displaying the expression.
142 */
143 @Override
144 public String getExpressionName() {
145 return "minus";
146 }
147
148 public void export(ExpressionPresenter out) throws XPathException {
149 out.startElement("minus", this);
150 if (backwardsCompatible) {
151 out.emitAttribute("vn", "1");
152 }
153 getBaseExpression().export(out);
154 out.endElement();
155 }
156 }
157