Debian Codegraph @

net/sf/saxon/expr/Operand.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.expr.parser.ContextItemStaticInfo;
11 import net.sf.saxon.expr.parser.ExpressionTool;
12 import net.sf.saxon.expr.parser.ExpressionVisitor;
13 import net.sf.saxon.pattern.NodeTest;
14 import net.sf.saxon.trans.XPathException;
15 import net.sf.saxon.tree.jiter.MonoIterator;
16 import net.sf.saxon.type.AnyItemType;
17 import net.sf.saxon.value.SequenceType;
18
19 import java.io.PrintWriter;
20 import java.io.StringWriter;
21 import java.util.Iterator;
22
23 /**
24 * Information about a sub-expression and its relationship to the parent expression
25 */
26
27 public final class Operand implements Iterable<Operand>, ExpressionOwner {
28
29 private Expression parentExpression;
30 private Expression childExpression;
31 private OperandRole role;
32
33 /**
34 * Create an operand object
35 * @param parentExpression the expression of which this is an operand
36 * @param childExpression the actual expression used as the operand
37 * @param role information about the role this operand plays within the parent expression
38 */
39
40 public Operand(Expression parentExpression, Expression childExpression, OperandRole role) {
41 this.parentExpression = parentExpression;
42 this.role = role;
43 setChildExpression(childExpression);
44 }
45
46 /**
47 * Get the parent expression of this operand
48 *
49 * @return the parent expression
50 */
51 public Expression getParentExpression() {
52 return parentExpression;
53 }
54
55 /**
56 * Get the expression used as the actual operand
57 *
58 * @return the child expression
59 */
60 public Expression getChildExpression() {
61 return childExpression;
62 }
63
64 /**
65 * Change the child expression used for this operand. This is the only approved way of
66 * making structural changes to the expression tree, and the implementation of this
67 * method is responsible for maintaining the consistency of the tree
68 * @param childExpression the new child expression
69 */
70
71 public void setChildExpression(Expression childExpression) {
72 if (childExpression != this.childExpression) {
73 if (role.isConstrainedClass() && this.childExpression != null && childExpression.getClass() != this.childExpression.getClass()) {
74 throw new AssertionError();
75 }
76 this.childExpression = childExpression;
77 parentExpression.adoptChildExpression(childExpression);
78 parentExpression.resetLocalStaticProperties();
79 //childExpression.verifyParentPointers();
80 }
81 }
82
83 private static boolean DEBUG = false;
84 public void detachChild() {
85 if (DEBUG) {
86 childExpression.setParentExpression(null);
87 StringWriter sw = new StringWriter();
88 new XPathException("dummy").printStackTrace(new PrintWriter(sw));
89 childExpression = new ErrorExpression("child expression has been detached: " + sw.toString(), "ZZZ", false);
90 ExpressionTool.copyLocationInfo(parentExpression, childExpression);
91 }
92 }
93
94 /**
95 * Get the operand role
96 * @return the operand role
97 */
98
99 public OperandRole getOperandRole() {
100 return role;
101 }
102
103 /**
104 * Set the operand role
105 * @param role the operand role
106 */
107
108 public void setOperandRole(OperandRole role) {
109 this.role = role;
110 }
111
112 /**
113 * Ask whether the child expression sets a new focus for evaluation of other operands
114 *
115 * @return true if the child expression is evaluated with the same focus as its parent expression
116 */
117 public boolean setsNewFocus() {
118 return role.setsNewFocus();
119 }
120
121 public boolean hasSpecialFocusRules() {
122 return role.hasSpecialFocusRules();
123 }
124
125 /**
126 * Ask whether the child expression is evaluated with the same focus as its parent expression
127 *
128 * @return true if the child expression is evaluated with the same focus as its parent expression
129 */
130 public boolean hasSameFocus() {
131 return role.hasSameFocus();
132 }
133
134 /**
135 * Ask whether the operand is a higher-order operand, in the sense that the child expression
136 * is evaluated repeatedly during a single evaluation of the parent expression
137 *
138 * @return true if the operand is higher-order
139 */
140 public boolean isEvaluatedRepeatedly() {
141 return role.isEvaluatedRepeatedly();
142 }
143
144 /**
145 * Get the usage of the operand
146 *
147 * @return the usage
148 */
149 public OperandUsage getUsage() {
150 return role.getUsage();
151 }
152
153 /**
154 * Set the usage of the operand
155 * @param usage the operand usage
156 */
157
158 public void setUsage(OperandUsage usage) {
159 role = new OperandRole(role.properties, usage, role.getRequiredType());
160 }
161
162 /**
163 * Get the required type of the operand
164 *
165 * @return the required type
166 */
167 public SequenceType getRequiredType() {
168 return role.getRequiredType();
169 }
170
171 /**
172 * Ask whether the operand is a member of a choice group
173 * @return true if the operand is in a choice group
174 */
175
176 public boolean isInChoiceGroup() {
177 return role.isInChoiceGroup();
178 }
179
180 /**
181 * Get a singleton iterator that returns this operand only
182 * @return a singleton iterator
183 */
184
185 public Iterator<Operand> iterator() {
186 return new MonoIterator<Operand>(this);
187 }
188
189 public void typeCheck(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException {
190 try {
191 setChildExpression(getChildExpression().typeCheck(visitor, contextInfo));
192 } catch (XPathException e) {
193 e.maybeSetLocation(getChildExpression().getLocation());
194 if (!e.isReportableStatically()) {
195 visitor.getStaticContext().issueWarning(
196 "Evaluation will always throw a dynamic error: " + e.getMessage(), getChildExpression().getLocation());
197 setChildExpression(new ErrorExpression(e));
198 } else {
199 throw e;
200 }
201 }
202 }
203
204 public void optimize(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException {
205 try {
206 setChildExpression(getChildExpression().optimize(visitor, contextInfo));
207 } catch (XPathException e) {
208 e.maybeSetLocation(getChildExpression().getLocation());
209 if (!e.isReportableStatically()) {
210 visitor.getStaticContext().issueWarning(
211 "Evaluation will always throw a dynamic error: " + e.getMessage(), getChildExpression().getLocation());
212 setChildExpression(new ErrorExpression(e));
213 } else {
214 throw e;
215 }
216 }
217 }
218
219 public static OperandUsage typeDeterminedUsage(net.sf.saxon.type.ItemType type) {
220 if (type.isPlainType()) {
221 return OperandUsage.ABSORPTION;
222 } else if (type instanceof NodeTest || type == AnyItemType.getInstance()) {
223 return OperandUsage.NAVIGATION;
224 } else {
225 return OperandUsage.INSPECTION;
226 }
227 }
228
229 }
230