Debian Codegraph @

net/sf/saxon/expr/OperandRole.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
11 import net.sf.saxon.type.FunctionItemType;
12 import net.sf.saxon.type.ItemType;
13 import net.sf.saxon.type.PlainType;
14 import net.sf.saxon.value.SequenceType;
15
16 /**
17 * Defines the role of a child expression relative to its parent expression. The properties
18 * of an operand role depend only on the kind of expression and not on the actual arguments
19 * supplied to a specific instance of the kind of operand.
20 */
21 public class OperandRole {
22
23 // Bit settings in properties field
24
25 public final static int SETS_NEW_FOCUS = 1;
26 public final static int USES_NEW_FOCUS = 2;
27 public final static int HIGHER_ORDER = 4;
28 public final static int IN_CHOICE_GROUP = 8;
29 public final static int CONSTRAINED_CLASS = 16;
30 public final static int SINGLETON = 32; // set only where HIGHER_ORDER would otherwise imply repeated evaluation
31 public final static int HAS_SPECIAL_FOCUS_RULES = 64;
32
33 public final static OperandRole SAME_FOCUS_ACTION =
34 new OperandRole(0, OperandUsage.TRANSMISSION, SequenceType.ANY_SEQUENCE);
35 public final static OperandRole FOCUS_CONTROLLING_SELECT =
36 new OperandRole(OperandRole.SETS_NEW_FOCUS, OperandUsage.INSPECTION, SequenceType.ANY_SEQUENCE);
37 public final static OperandRole FOCUS_CONTROLLED_ACTION =
38 new OperandRole(OperandRole.USES_NEW_FOCUS | OperandRole.HIGHER_ORDER, OperandUsage.TRANSMISSION, SequenceType.ANY_SEQUENCE);
39 public final static OperandRole INSPECT =
40 new OperandRole(0, OperandUsage.INSPECTION, SequenceType.ANY_SEQUENCE);
41 public final static OperandRole ABSORB =
42 new OperandRole(0, OperandUsage.ABSORPTION, SequenceType.ANY_SEQUENCE);
43 public final static OperandRole REPEAT_INSPECT =
44 new OperandRole(OperandRole.HIGHER_ORDER, OperandUsage.INSPECTION, SequenceType.ANY_SEQUENCE);
45 public final static OperandRole NAVIGATE =
46 new OperandRole(0, OperandUsage.NAVIGATION, SequenceType.ANY_SEQUENCE);
47 public final static OperandRole REPEAT_NAVIGATE =
48 new OperandRole(OperandRole.HIGHER_ORDER, OperandUsage.NAVIGATION, SequenceType.ANY_SEQUENCE);
49 public final static OperandRole REPEAT_NAVIGATE_CONSTRAINED =
50 new OperandRole(OperandRole.HIGHER_ORDER | OperandRole.CONSTRAINED_CLASS, OperandUsage.NAVIGATION, SequenceType.ANY_SEQUENCE);
51 public final static OperandRole SINGLE_ATOMIC =
52 new OperandRole(0, OperandUsage.ABSORPTION, SequenceType.SINGLE_ATOMIC);
53 public final static OperandRole ATOMIC_SEQUENCE =
54 new OperandRole(0, OperandUsage.ABSORPTION, SequenceType.ATOMIC_SEQUENCE);
55 public final static OperandRole NEW_FOCUS_ATOMIC =
56 new OperandRole(OperandRole.USES_NEW_FOCUS | OperandRole.HIGHER_ORDER, OperandUsage.ABSORPTION, SequenceType.ATOMIC_SEQUENCE);
57 public final static OperandRole PATTERN =
58 new OperandRole(OperandRole.USES_NEW_FOCUS | OperandRole.HIGHER_ORDER | OperandRole.CONSTRAINED_CLASS, OperandUsage.ABSORPTION, SequenceType.ATOMIC_SEQUENCE);
59
60 int properties;
61 private OperandUsage usage;
62 private SequenceType requiredType = SequenceType.ANY_SEQUENCE;
63
64 public OperandRole(int properties, OperandUsage usage) {
65 this.properties = properties;
66 this.usage = usage;
67 }
68
69 public OperandRole(int properties, OperandUsage usage, SequenceType requiredType) {
70 this.properties = properties;
71 this.usage = usage;
72 this.requiredType = requiredType;
73 }
74
75 /**
76 * Ask whether the child expression sets the focus for evaluation of other child expressions
77 * @return true if the child expression is evaluated with the same focus as its parent expression
78 */
79
80 public boolean setsNewFocus() {
81 return (properties & SETS_NEW_FOCUS) != 0;
82 }
83
84 /**
85 * Ask whether the child expression is evaluated with the same focus as its parent expression
86 * @return true if the child expression is evaluated with the same focus as its parent expression
87 */
88
89 public boolean hasSameFocus() {
90 return (properties & (USES_NEW_FOCUS | HAS_SPECIAL_FOCUS_RULES)) == 0;
91 }
92
93 public boolean hasSpecialFocusRules() {
94 return (properties & HAS_SPECIAL_FOCUS_RULES) != 0;
95 }
96
97 /**
98 * Ask whether the operand is a higher-order operand, in the sense that the child expression
99 * is evaluated repeatedly during a single evaluation of the parent expression
100 * @return true if the operand is higher-order
101 */
102
103 public boolean isEvaluatedRepeatedly() {
104 return ((properties & HIGHER_ORDER) != 0) && ((properties & SINGLETON) == 0);
105 }
106
107 /**
108 * Ask whether the operand is constrained to be of a particular class (preventing
109 * substitution of a variable binding)
110 * @return true if the operand expression is constrained to be of a particular class
111 */
112
113 public boolean isConstrainedClass() { return (properties & CONSTRAINED_CLASS) != 0; }
114
115 /**
116 * Get the usage of the operand
117 * @return the usage
118 */
119
120 public OperandUsage getUsage() {
121 return usage;
122 }
123
124 /**
125 * Get the required type of the operand
126 * @return the required type
127 */
128
129 public SequenceType getRequiredType() {
130 return requiredType;
131 }
132
133 /**
134 * Ask whether the operand is a member of a choice group
135 * @return true if the operand is in a choice group
136 */
137
138 public boolean isInChoiceGroup() {
139 return (properties & IN_CHOICE_GROUP) != 0;
140 }
141
142 /**
143 * Static method to get the type-determined usage for a particular ItemType
144 * @param type the required type
145 * @return the type-determined operand usage
146 */
147
148 public static OperandUsage getTypeDeterminedUsage(ItemType type) {
149 if (type instanceof FunctionItemType) {
150 return OperandUsage.INSPECTION;
151 } else if (type instanceof PlainType) {
152 return OperandUsage.ABSORPTION;
153 } else {
154 return OperandUsage.NAVIGATION;
155 }
156 }
157
158 public OperandRole modifyProperty(int property, boolean on) {
159 int newProp = on ? (properties | property) : (properties & ~property);
160 return new OperandRole(newProp, usage, requiredType);
161 }
162
163 public int getProperties() {
164 return properties;
165 }
166
167 }
168