Debian Codegraph @

net/sf/saxon/expr/GlobalVariableReference.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.instruct.GlobalVariable;
11 import net.sf.saxon.expr.parser.RebindingMap;
12 import net.sf.saxon.om.Sequence;
13 import net.sf.saxon.om.StandardNames;
14 import net.sf.saxon.om.StructuredQName;
15 import net.sf.saxon.trace.ExpressionPresenter;
16 import net.sf.saxon.trans.SymbolicName;
17 import net.sf.saxon.trans.Visibility;
18 import net.sf.saxon.trans.XPathException;
19
20 import java.util.HashSet;
21 import java.util.Set;
22
23 /**
24 * A reference to a global variable
25 */
26 public class GlobalVariableReference extends VariableReference implements ComponentInvocation {
27
28 int bindingSlot = -1;
29
30 public GlobalVariableReference(StructuredQName name) {
31 super(name);
32 }
33
34 public GlobalVariableReference(GlobalVariable var) {
35 super(var);
36 }
37
38 /*@NotNull*/
39 public Expression copy(RebindingMap rebindings) {
40 if (binding == null) {
41 //System.err.println("copy unbound variable " + this);
42 throw new UnsupportedOperationException("Cannot copy a variable reference whose binding is unknown");
43 }
44 GlobalVariableReference ref = new GlobalVariableReference(getVariableName());
45 ref.copyFrom(this);
46 return ref;
47 }
48
49 /**
50 * Set the binding slot to be used. This is the offset within the binding vector of the containing
51 * component where the actual target component is to be found. The target template is not held directly
52 * in the invocation instruction/expression itself because it can be overridden in a using package.
53 *
54 * @param slot the offset in the binding vector of the containing package where the target component
55 * can be found.
56 */
57 public void setBindingSlot(int slot) {
58 if (bindingSlot != -1) {
59 throw new AssertionError("Duplicate binding slot assignment");
60 }
61 bindingSlot = slot;
62 }
63
64 /**
65 * Get the binding slot to be used. This is the offset within the binding vector of the containing
66 * component where the actual target component is to be found.
67 *
68 * @return the offset in the binding vector of the containing package where the target component
69 * can be found.
70 */
71 public int getBindingSlot() {
72 return bindingSlot;
73 }
74
75 /**
76 * Get the symbolic name of the component that this invocation references
77 *
78 * @return the symbolic name of the target component
79 */
80 public SymbolicName getSymbolicName() {
81 return new SymbolicName(StandardNames.XSL_VARIABLE, getVariableName());
82 }
83
84 public void setTarget(Component target) {
85 binding = (GlobalVariable) target.getActor();
86 }
87
88 public Component getTarget() {
89 return ((GlobalVariable) binding).getDeclaringComponent();
90 }
91
92 public Component getFixedTarget() {
93 Component c = getTarget();
94 Visibility v = c.getVisibility();
95 if (v == Visibility.PRIVATE || v == Visibility.FINAL) {
96 return c;
97 } else {
98 return null;
99 }
100 }
101
102 /**
103 * Evaluate this variable
104 *
105 * @param c the XPath dynamic context
106 * @return the value of the variable
107 * @throws net.sf.saxon.trans.XPathException if any error occurs
108 */
109 @Override
110 public Sequence<?> evaluateVariable(XPathContext c) throws XPathException {
111
112 if (bindingSlot >= 0) {
113 if (c.getCurrentComponent() == null) {
114 throw new AssertionError("No current component");
115 }
116 Component target = c.getTargetComponent(bindingSlot);
117 if (target.isHiddenAbstractComponent()) {
118 XPathException err = new XPathException("Cannot evaluate an abstract variable ("
119 + getVariableName().getDisplayName()
120 + ") with no overriding declaration", "XTDE3052");
121 err.setLocation(getLocation());
122 throw err;
123 }
124 GlobalVariable p = (GlobalVariable) target.getActor();
125 return p.evaluateVariable(c, target);
126 } else {
127 // code for references to final/private variables, also used in XQuery
128 GlobalVariable b = (GlobalVariable) binding;
129 return b.evaluateVariable(c, b.getDeclaringComponent());
130 }
131
132 }
133
134 /**
135 * Diagnostic print of expression structure. The abstract expression tree
136 * is written to the supplied output destination.
137 */
138
139 public void export(ExpressionPresenter out) throws XPathException {
140 out.startElement("gVarRef", this);
141 out.emitAttribute("name", getVariableName());
142 out.emitAttribute("bSlot", "" + getBindingSlot());
143 out.endElement();
144 }
145
146 public Set<Expression> getPreconditions() {
147 Set<Expression> pre = new HashSet<Expression>();
148 //pre.add(this.copy());
149 return pre;
150 }
151 }