Debian Codegraph @

net/sf/saxon/expr/CastingExpression.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.XPathParser;
11 import net.sf.saxon.om.NamespaceResolver;
12 import net.sf.saxon.trans.XPathException;
13 import net.sf.saxon.type.AtomicType;
14 import net.sf.saxon.type.BuiltInAtomicType;
15 import net.sf.saxon.type.Converter;
16
17 /**
18 * Casting Expression: abstract superclass for "cast as X" and "castable as X", which share a good deal of logic
19 */
20
21 public abstract class CastingExpression extends UnaryExpression {
22
23 private AtomicType targetType;
24 private AtomicType targetPrimitiveType;
25 private boolean allowEmpty = false;
26 protected Converter converter;
27 private boolean operandIsStringLiteral = false;
28
29 /**
30 * Create a cast expression
31 *
32 * @param source expression giving the value to be converted
33 * @param target the type to which the value is to be converted
34 * @param allowEmpty true if the expression allows an empty sequence as input, producing
35 * an empty sequence as output. If false, an empty sequence is a type error.
36 */
37
38 public CastingExpression(Expression source, AtomicType target, boolean allowEmpty) {
39 super(source);
40 this.allowEmpty = allowEmpty;
41 targetType = target;
42 targetPrimitiveType = (AtomicType) target.getPrimitiveItemType();
43 //derived = (targetType.getFingerprint() != targetPrimitiveType.getFingerprint());
44 }
45
46 /**
47 * Get the primitive base type of the target type of the cast
48 *
49 * @return the primitive type of the target type
50 */
51
52 public AtomicType getTargetPrimitiveType() {
53 return targetPrimitiveType;
54 }
55
56 /**
57 * Set the target type
58 *
59 * @param type the target type for the cast
60 */
61
62 public void setTargetType(AtomicType type) {
63 targetType = type;
64 }
65
66 /**
67 * Get the target type (the result type)
68 *
69 * @return the target type
70 */
71
72 public AtomicType getTargetType() {
73 return targetType;
74 }
75
76 protected OperandRole getOperandRole() {
77 return OperandRole.SINGLE_ATOMIC;
78 }
79
80 /**
81 * Say whether the expression accepts an empty sequence as input (producing an empty sequence as output)
82 *
83 * @param allow true if an empty sequence is accepted
84 */
85
86 public void setAllowEmpty(boolean allow) {
87 allowEmpty = allow;
88 }
89
90 /**
91 * Ask whether the expression accepts an empty sequence as input (producing an empty sequence as output)
92 *
93 * @return true if an empty sequence is accepted
94 */
95
96 public boolean allowsEmpty() {
97 return allowEmpty;
98 }
99
100 /**
101 * Say whether the operand to the cast expression was supplied in the form of a string literal. This is
102 * relevant only for XPath 2.0 / XQuery 1.0, and then only when the target type is a QName or NOTATION.
103 *
104 * @param option true if the operand was supplied as a string literal
105 */
106
107 public void setOperandIsStringLiteral(boolean option) {
108 operandIsStringLiteral = option;
109 }
110
111 /**
112 * Ask whether the operand to the cast expression was supplied in the form of a string literal. This is
113 * relevant only for XPath 2.0 / XQuery 1.0, and then only when the target type is a QName or NOTATION.
114 *
115 * @return true if the operand was supplied as a string literal
116 */
117
118 public boolean isOperandIsStringLiteral() {
119 return operandIsStringLiteral;
120 }
121
122 /**
123 * Get the Converter allocated to implement this cast expression, if any
124 *
125 * @return the Converter if one has been statically allocated, or null if not
126 */
127
128 public Converter getConverter() {
129 return converter;
130 }
131
132 /**
133 * Get the namespace resolver, if any
134 *
135 * @return the namespace resolver that was statically allocated if the target type is namespace-sensitive
136 */
137
138 public NamespaceResolver getNamespaceResolver() {
139 return getRetainedStaticContext();
140 }
141
142 /**
143 * Get the expression's dependencies. If the target type is namespace-sensitive, then the expression
144 * has a dependency on the namespace bindings in the static context
145 *
146 * @return the expression's dependencies.
147 */
148 @Override
149 public int getIntrinsicDependencies() {
150 return getTargetType().isNamespaceSensitive() ? StaticProperty.DEPENDS_ON_STATIC_CONTEXT : 0;
151 }
152
153 /**
154 * Simplify the expression
155 */
156
157 /*@NotNull*/
158 public Expression simplify() throws XPathException {
159 if (targetType instanceof BuiltInAtomicType) {
160 String s = XPathParser.whyDisallowedType(getPackageData(), (BuiltInAtomicType)targetType);
161 if (s != null) {
162 // this is checked here because the ConstructorFunctionLibrary doesn't have access to the static
163 // context at bind time
164 XPathException err = new XPathException(s, "XPST0080", this.getLocation());
165 err.setIsStaticError(true);
166 throw err;
167 }
168 }
169 setBaseExpression(getBaseExpression().simplify());
170 return this;
171 }
172
173
174 /**
175 * Determine the special properties of this expression
176 *
177 * @return {@link net.sf.saxon.expr.StaticProperty#NO_NODES_NEWLY_CREATED}.
178 */
179
180 public int computeSpecialProperties() {
181 int p = super.computeSpecialProperties();
182 return p | StaticProperty.NO_NODES_NEWLY_CREATED;
183 }
184
185
186 }
187