Debian Codegraph @

net/sf/saxon/style/XSLGlobalVariable.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.style;
9
10 import net.sf.saxon.expr.Component;
11 import net.sf.saxon.expr.Expression;
12 import net.sf.saxon.expr.Literal;
13 import net.sf.saxon.expr.instruct.Actor;
14 import net.sf.saxon.expr.instruct.GlobalParam;
15 import net.sf.saxon.expr.instruct.GlobalVariable;
16 import net.sf.saxon.expr.instruct.SlotManager;
17 import net.sf.saxon.expr.parser.ExpressionTool;
18 import net.sf.saxon.expr.parser.ExpressionVisitor;
19 import net.sf.saxon.expr.parser.Optimizer;
20 import net.sf.saxon.expr.parser.RetainedStaticContext;
21 import net.sf.saxon.om.GroundedValue;
22 import net.sf.saxon.om.StandardNames;
23 import net.sf.saxon.om.StructuredQName;
24 import net.sf.saxon.trace.LocationKind;
25 import net.sf.saxon.trans.SymbolicName;
26 import net.sf.saxon.trans.XPathException;
27 import net.sf.saxon.type.AnyItemType;
28 import net.sf.saxon.type.Type;
29 import net.sf.saxon.type.TypeHierarchy;
30 import net.sf.saxon.value.SequenceType;
31
32 /**
33 * Handler for xsl:variable elements appearing as a child of xsl:stylesheet.
34 * <p>The xsl:variable element has mandatory attribute {@code name} and
35 * optional attribute {@code select} (inter alia)</p>
36 */
37
38 public class XSLGlobalVariable extends StyleElement implements StylesheetComponent {
39
40 private SlotManager slotManager; // used to manage local variables declared inside this global variable
41 protected SourceBinding sourceBinding = new SourceBinding(this);
42 protected GlobalVariable compiledVariable = null;
43
44 /**
45 * Get the source binding object that holds information about the declared variable.
46 * @return the source binding
47 */
48
49 public SourceBinding getSourceBinding() {
50 return sourceBinding;
51 }
52
53 public StructuredQName getVariableQName() {
54 return sourceBinding.getVariableQName();
55 }
56
57 public StructuredQName getObjectName() {
58 return sourceBinding.getVariableQName();
59 }
60
61 /**
62 * Determine whether this type of element is allowed to contain a template-body
63 *
64 * @return true: yes, it may contain a template-body
65 */
66
67 public boolean mayContainSequenceConstructor() {
68 return true;
69 }
70
71 /**
72 * Test whether this is a global variable or parameter
73 *
74 * @return true if this is global
75 */
76
77 public boolean isGlobal() {
78 return isTopLevel();
79 // might be called before the "global" field is initialized
80 }
81
82 /**
83 * Hook to allow additional validation of a parent element immediately after its
84 * children have been validated.
85 */
86
87 public void postValidate() throws XPathException {
88 sourceBinding.postValidate();
89 }
90
91 /**
92 * Get the type of construct. This will be a constant in
93 * class {@link LocationKind}. This method is part of the
94 * {@link net.sf.saxon.trace.InstructionInfo} interface
95 */
96
97 public int getConstructType() {
98 return StandardNames.XSL_VARIABLE;
99 }
100
101 public GlobalVariable getCompiledVariable() {
102 return compiledVariable;
103 }
104
105
106
107 public XSLGlobalVariable() {
108 sourceBinding.setProperty(SourceBinding.GLOBAL, true);
109 }
110
111 protected int getPermittedAttributes() {
112 return
113 SourceBinding.ASSIGNABLE |
114 SourceBinding.SELECT |
115 SourceBinding.AS |
116 SourceBinding.STATIC |
117 SourceBinding.VISIBILITY;
118 }
119
120 private int state = 0;
121 // 0 = before prepareAttributes()
122 // 1 = during prepareAttributes()
123 // 2 = after prepareAttributes()
124
125 protected boolean redundant = false;
126
127 /**
128 * Get the corresponding Procedure object that results from the compilation of this
129 * StylesheetProcedure
130 */
131 public Actor getActor() throws XPathException {
132 GlobalVariable gv = getCompiledVariable();
133 if (gv == null) {
134 gv = this instanceof XSLGlobalParam ? new GlobalParam() : new GlobalVariable();
135 gv.setPackageData(getCompilation().getPackageData());
136 gv.obtainDeclaringComponent(this);
137 gv.setRequiredType(sourceBinding.getDeclaredType());
138 gv.setDeclaredVisibility(getDeclaredVisibility());
139 gv.setVariableQName(sourceBinding.getVariableQName());
140 gv.setSystemId(getSystemId());
141 gv.setLineNumber(getLineNumber());
142 RetainedStaticContext rsc = makeRetainedStaticContext();
143 gv.setRetainedStaticContext(rsc);
144 if (gv.getBody() != null) {
145 gv.getBody().setRetainedStaticContext(rsc);
146 }
147 compiledVariable = gv;
148 }
149 return gv;
150 }
151
152 public SymbolicName getSymbolicName() {
153 return new SymbolicName(StandardNames.XSL_VARIABLE, getObjectName());
154 }
155
156 public void checkCompatibility(Component component) {
157 SequenceType st1 = getSourceBinding().getDeclaredType();
158 if (st1 == null) {
159 st1 = SequenceType.ANY_SEQUENCE;
160 }
161 GlobalVariable other = (GlobalVariable) component.getActor();
162 TypeHierarchy th = component.getDeclaringPackage().getConfiguration().getTypeHierarchy();
163 int relation = th.sequenceTypeRelationship(st1, other.getRequiredType());
164 if (relation != TypeHierarchy.SAME_TYPE) {
165 compileError(
166 "The declared type of the overriding variable $" + getVariableQName().getDisplayName() +
167 " is different from that of the overridden variable",
168 "XTSE3070");
169 }
170 }
171
172 /**
173 * Ask whether this element contains a binding for a variable with a given name; and if it does,
174 * return the source binding information
175 *
176 * @param name the variable name
177 * @return the binding information if this element binds a variable of this name; otherwise null
178 */
179
180 public SourceBinding getBindingInformation(StructuredQName name) {
181 if (name.equals(sourceBinding.getVariableQName())) {
182 return sourceBinding;
183 } else {
184 return null;
185 }
186 }
187
188 public void prepareAttributes() throws XPathException {
189 if (state == 2) {
190 return;
191 }
192 if (state == 1) {
193 compileError("Circular reference to variable", "XTDE0640");
194 }
195 state = 1;
196 //System.err.println("Prepare attributes of $" + getVariableName());
197
198 sourceBinding.prepareAttributes(getPermittedAttributes());
199 state = 2;
200 }
201
202 @Override
203 public void index(ComponentDeclaration decl, PrincipalStylesheetModule top) throws XPathException {
204 top.indexVariableDeclaration(decl);
205 }
206
207 @Override
208 public void validate(ComponentDeclaration decl) throws XPathException {
209 slotManager = getConfiguration().makeSlotManager();
210 sourceBinding.validate();
211 }
212
213 /**
214 * Ask whether the global variable is declared with assignable="yes"
215 *
216 * @return true if assignabl="yes" was specified
217 */
218
219 public boolean isAssignable() {
220 return sourceBinding.hasProperty(SourceBinding.ASSIGNABLE);
221 }
222
223 /**
224 * Determine whether this node is a declaration.
225 *
226 * @return true - a global variable is a declaration
227 */
228
229 @Override
230 public boolean isDeclaration() {
231 return true;
232 }
233
234 /**
235 * Determine whether this node is an instruction.
236 *
237 * @return false - a global variable is not an instruction
238 */
239
240 public boolean isInstruction() {
241 return false;
242 }
243
244 /**
245 * Get the static type of the variable. This is the declared type, unless the value
246 * is statically known and constant, in which case it is the actual type of the value.
247 * @return the declared or inferred static type of the variable
248 */
249
250 public SequenceType getRequiredType() {
251 return sourceBinding.getInferredType(true);
252 }
253
254 @Override
255 public void fixupReferences() throws XPathException {
256 sourceBinding.fixupReferences(compiledVariable);
257 super.fixupReferences();
258 }
259
260 /**
261 * Compile.
262 * This method ensures space is available for local variables declared within
263 * this global variable
264 */
265
266 public void compileDeclaration(Compilation compilation, ComponentDeclaration decl) throws XPathException {
267
268 // Commented out: Can't eliminate unused variables at this stage because they might be xsl:expose'd as public
269 // boolean unused = sourceBinding.getReferences().isEmpty() && !isAssignable() &&
270 // getVisibility() == Visibility.PRIVATE;
271 // if (unused && !compilation.getCompilerInfo().isCompileWithTracing()) {
272 // redundant = true;
273 // // Remove the global variable from the package (otherwise a failure can occur
274 // // when pre-evaluating global variables)
275 // compilation.getPrincipalStylesheetModule().removeGlobalVariable(decl);
276 // }
277
278 if (!redundant) {
279 sourceBinding.handleSequenceConstructor(compilation, decl);
280 GlobalVariable inst = getCompiledVariable();
281 if (inst == null) {
282 inst = new GlobalVariable();
283 inst.setPackageData(getCompilation().getPackageData());
284 inst.obtainDeclaringComponent(this);
285 inst.setVariableQName(sourceBinding.getVariableQName());
286 }
287 if (sourceBinding.isStatic()) {
288 inst.setStatic(true);
289 GroundedValue<?> value =
290 compilation.getStaticVariable(sourceBinding.getVariableQName());
291 if (value == null) {
292 throw new AssertionError();
293 }
294 Expression select = Literal.makeLiteral(value);
295 select.setRetainedStaticContext(makeRetainedStaticContext());
296 inst.setSelectExpression(select);
297 } else {
298 Expression select = sourceBinding.getSelectExpression();
299 inst.setSelectExpression(select);
300 }
301 inst.setRetainedStaticContext(makeRetainedStaticContext());
302 initializeBinding(inst);
303 inst.setAssignable(isAssignable());
304 inst.setRequiredType(getRequiredType());
305 sourceBinding.fixupBinding(inst);
306 compiledVariable = inst;
307
308 Component overridden = getOverriddenComponent();
309 if (overridden != null) {
310 checkCompatibility(overridden);
311 }
312
313 // if (unused) {
314 // // Happens when there are no references, but compileWithTracing is set
315 // inst.setUnused(true);
316 // }
317 }
318
319 }
320
321 /**
322 * Initialize - common code called from the compile() method of all subclasses
323 *
324 * @param var the representation of the variable declaration in the compiled executable
325 */
326
327 protected void initializeBinding(GlobalVariable var) {
328
329 Expression select = var.getSelectExpression();
330 Expression exp2 = select;
331 if (exp2 != null) {
332 try {
333 ExpressionVisitor visitor = makeExpressionVisitor();
334 exp2 = select.simplify().typeCheck(visitor, getConfiguration().makeContextItemStaticInfo(Type.ITEM_TYPE, true));
335 } catch (XPathException err) {
336 compileError(err);
337 }
338
339 // Add trace wrapper code if required
340 exp2 = makeTraceInstruction(this, exp2);
341
342 allocateLocalSlots(exp2);
343 }
344 if (slotManager != null && slotManager.getNumberOfVariables() > 0) {
345 var.setContainsLocals(slotManager);
346 }
347 //StylesheetPackage pack = getCompilation().getStylesheetPackage();
348 //pack.registerGlobalVariable(gvar);
349
350 if (exp2 != select) {
351 var.setSelectExpression(exp2);
352 }
353
354 }
355
356
357 /**
358 * Get the SlotManager associated with this stylesheet construct. The SlotManager contains the
359 * information needed to manage the local stack frames used by run-time instances of the code.
360 *
361 * @return the associated SlotManager object
362 */
363
364 public SlotManager getSlotManager() {
365 return slotManager;
366 }
367
368 /**
369 * Optimize the stylesheet construct
370 *
371 * @param declaration the declaration of this variable
372 */
373
374 public void optimize(ComponentDeclaration declaration) throws XPathException {
375 if (!redundant && compiledVariable.getSelectExpression() != null) {
376 Expression exp2 = compiledVariable.getSelectExpression();
377 ExpressionVisitor visitor = makeExpressionVisitor();
378
379 exp2 = ExpressionTool.optimizeComponentBody(
380 exp2, getCompilation(), visitor,
381 getConfiguration().makeContextItemStaticInfo(AnyItemType.getInstance(), true), false);
382
383 allocateLocalSlots(exp2);
384 if (slotManager != null && slotManager.getNumberOfVariables() > 0) {
385 compiledVariable.setContainsLocals(slotManager);
386 }
387
388 if (exp2 != compiledVariable.getSelectExpression()) {
389 compiledVariable.setSelectExpression(exp2);
390 }
391 }
392 }
393
394
395 /**
396 * Mark this global variable as redundant, typically because it is overridden by another global
397 * variable of the same name, or because there are no references to it
398 *
399 * @param redundant true if this variable is redundant, otherwise false
400 */
401
402 public void setRedundant(boolean redundant) {
403 this.redundant = redundant;
404 }
405
406 /**
407 * Generate byte code if appropriate
408 *
409 * @param opt the optimizer
410 *
411 */
412 public void generateByteCode(Optimizer opt) {}
413
414
415 }
416