Debian Codegraph @

net/sf/saxon/expr/instruct/Actor.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.instruct;
9
10 import net.sf.saxon.Configuration;
11 import net.sf.saxon.expr.*;
12 import net.sf.saxon.expr.parser.Location;
13 import net.sf.saxon.expr.parser.RetainedStaticContext;
14 import net.sf.saxon.lib.NamespaceConstant;
15 import net.sf.saxon.style.StyleElement;
16 import net.sf.saxon.style.StylesheetPackage;
17 import net.sf.saxon.style.XSLGlobalParam;
18 import net.sf.saxon.trace.ExpressionPresenter;
19 import net.sf.saxon.trace.InstructionInfo;
20 import net.sf.saxon.trans.SymbolicName;
21 import net.sf.saxon.trans.Visibility;
22 import net.sf.saxon.trans.VisibilityProvenance;
23 import net.sf.saxon.trans.XPathException;
24
25 import java.util.Collections;
26 import java.util.Iterator;
27 import java.util.List;
28
29 /**
30 * This object represents the compiled form of a user-written function, template, attribute-set, etc
31 * (the source can be either an XSLT stylesheet function or an XQuery function).
32 * <p>It is assumed that type-checking, of both the arguments and the results,
33 * has been handled at compile time. That is, the expression supplied as the body
34 * of the function must be wrapped in code to check or convert the result to the
35 * required type, and calls on the function must be wrapped at compile time to check or
36 * convert the supplied arguments.</p>
37 */
38
39 public abstract class Actor implements InstructionInfo, ExpressionOwner {
40
41 protected Expression body;
42 private String systemId;
43 private int lineNumber;
44 private SlotManager stackFrameMap;
45 private PackageData packageData;
46 private Component declaringComponent;
47 private Visibility declaredVisibility;
48 private RetainedStaticContext retainedStaticContext;
49
50 public Actor() {
51 }
52
53 /**
54 * Get the symbolic name of the component
55 *
56 * @return the symbolic name
57 */
58
59 public abstract SymbolicName getSymbolicName();
60
61 /**
62 * Set basic data about the unit of compilation (XQuery module, XSLT package) to which this
63 * procedure belongs
64 *
65 * @param packageData information about the containing package
66 */
67
68 public void setPackageData(PackageData packageData) {
69 this.packageData = packageData;
70 }
71
72 /**
73 * Get basic data about the unit of compilation (XQuery module, XSLT package) to which this
74 * container belongs
75 */
76 public PackageData getPackageData() {
77 return packageData;
78 }
79
80 public Component makeDeclaringComponent(Visibility visibility, StylesheetPackage declaringPackage) {
81 if (declaringComponent == null) {
82 declaringComponent = Component.makeComponent(this, visibility, VisibilityProvenance.DEFAULTED, declaringPackage, declaringPackage);
83 }
84 return declaringComponent;
85 }
86
87 /**
88 * Return the declaring component, creating it if it does not already exist.
89 * @param declaration The source element in the stylesheet corresponding to the component
90 * @return the component corresponding to this declaration.
91 */
92
93 public Component obtainDeclaringComponent(StyleElement declaration) {
94 if (declaringComponent == null) {
95 StylesheetPackage declaringPackage = declaration.getContainingPackage();
96 Visibility defaultVisibility = declaration instanceof XSLGlobalParam ? Visibility.PUBLIC : Visibility.PRIVATE;
97 Visibility declaredVisibility = declaration.getDeclaredVisibility();
98 Visibility actualVisibility = declaredVisibility == null ? defaultVisibility : declaredVisibility;
99 VisibilityProvenance provenance = declaredVisibility == null ? VisibilityProvenance.DEFAULTED : VisibilityProvenance.EXPLICIT;
100 declaringComponent = Component.makeComponent(this, actualVisibility, provenance, declaringPackage, declaringPackage);
101 }
102 return declaringComponent;
103 }
104
105 public Component getDeclaringComponent() {
106 return declaringComponent;
107 }
108
109 public void setDeclaringComponent(Component comp) {
110 declaringComponent = comp;
111 }
112
113 /**
114 * Allocate slot numbers to all the external component references in this component
115 *
116 * @param pack the containing package
117 */
118
119 public void allocateAllBindingSlots(StylesheetPackage pack) {
120 if (getBody() != null && getDeclaringComponent().getDeclaringPackage() == pack && packageData.getHostLanguage() == Configuration.XSLT) {
121 allocateBindingSlotsRecursive(pack, this, getBody(), getDeclaringComponent().getComponentBindings());
122 }
123 }
124
125 public static void allocateBindingSlotsRecursive(StylesheetPackage pack, Actor p, Expression exp, List<ComponentBinding> bindings) {
126 if (exp instanceof ComponentInvocation) {
127 p.processComponentReference(pack, (ComponentInvocation) exp, bindings);
128 }
129 for (Operand o : exp.operands()) {
130 allocateBindingSlotsRecursive(pack, p, o.getChildExpression(), bindings);
131 }
132 }
133
134 /**
135 * Process a component reference from this component to some other component. Specifically, allocate
136 * a slot number to the component reference, and add the reference to the supplied binding vector
137 *
138 * @param pack the containing package
139 * @param invocation the instruction or expression representing the external reference, for example
140 * a call-template instruction
141 * @param bindings the supplied binding vector, containing all outward references
142 * from this component; this list is modified by the call
143 */
144
145 private void processComponentReference(StylesheetPackage pack, ComponentInvocation invocation, List<ComponentBinding> bindings) {
146 SymbolicName name = invocation.getSymbolicName();
147 if (name == null) {
148 // there is no target component, e.g. with apply-templates mode="#current"
149 return;
150 }
151
152 Component target = pack.getComponent(name);
153 if (target == null && name.getComponentName().hasURI(NamespaceConstant.XSLT) &&
154 name.getComponentName().getLocalPart().equals("original")) {
155 target = pack.getOverriddenComponent(getSymbolicName());
156 }
157 if (target == null) {
158 throw new AssertionError("Target of component reference " + name + " is undefined");
159 }
160 if (invocation.getBindingSlot() >= 0) {
161 throw new AssertionError("**** Component reference " + name + " is already bound");
162 }
163 int slot = bindings.size();
164 ComponentBinding cb = new ComponentBinding(name, target);
165 bindings.add(cb);
166 invocation.setBindingSlot(slot);
167 }
168
169 public void setBody(Expression body) {
170 this.body = body;
171 if (body != null) {
172 body.setParentExpression(null);
173 }
174 }
175
176 public final Expression getBody() {
177 return body;
178 }
179
180 public void setStackFrameMap(SlotManager map) {
181 stackFrameMap = map;
182 }
183
184 public SlotManager getStackFrameMap() {
185 return stackFrameMap;
186 }
187
188 public void setLineNumber(int lineNumber) {
189 this.lineNumber = lineNumber;
190 }
191
192 public void setSystemId(String systemId) {
193 this.systemId = systemId;
194 }
195
196 public Location getLocation() {
197 return this;
198 }
199
200 public int getLineNumber() {
201 return lineNumber;
202 }
203
204 public String getSystemId() {
205 return systemId;
206 }
207
208 public int getColumnNumber() {
209 return -1;
210 }
211
212 /*@Nullable*/
213 public String getPublicId() {
214 return null;
215 }
216
217 public Location saveLocation() {
218 return this;
219 }
220
221 public void setRetainedStaticContext(RetainedStaticContext rsc) {
222 this.retainedStaticContext = rsc;
223 }
224
225 public RetainedStaticContext getRetainedStaticContext() {
226 return retainedStaticContext;
227 }
228
229 public Object getProperty(String name) {
230 return null;
231 }
232
233 /**
234 * Set the visibility of the component as defined using its actual @visibility attribute
235 *
236 * @param visibility the actual declared visibility; null if the visibility attribute is absent
237 */
238
239 public void setDeclaredVisibility(Visibility visibility) {
240 this.declaredVisibility = visibility;
241 }
242
243 /**
244 * Get the visibility of the component as defined using its actual @visibility attribute
245 *
246 * @return the actual declared visibility; null if the visibility attribute is absent
247 */
248
249 public Visibility getDeclaredVisibility() {
250 return declaredVisibility;
251 }
252
253
254 /**
255 * Get an iterator over all the properties available. The values returned by the iterator
256 * will be of type String, and each string can be supplied as input to the getProperty()
257 * method to retrieve the value of the property. The iterator may return properties whose
258 * value is null.
259 */
260
261 public Iterator<String> getProperties() {
262 final List<String> list = Collections.emptyList();
263 return list.iterator();
264 }
265
266 /**
267 * Get the kind of component that this represents, using integer constants such as
268 * {@link net.sf.saxon.om.StandardNames#XSL_FUNCTION}
269 */
270
271 public abstract int getComponentKind();
272
273 /**
274 * Export expression structure. The abstract expression tree
275 * is written to the supplied outputstream.
276 *
277 * @param presenter the expression presenter used to generate the XML representation of the structure
278 */
279
280 public abstract void export(ExpressionPresenter presenter) throws XPathException;
281
282 public boolean isExportable() {
283 return true;
284 }
285
286 public void setChildExpression(Expression expr) {
287 setBody(expr);
288 }
289 }
290
291