| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 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 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public class XSLGlobalVariable extends StyleElement implements StylesheetComponent { |
| 39 |
|
| 40 |
private SlotManager slotManager; |
| 41 |
protected SourceBinding sourceBinding = new SourceBinding(this); |
| 42 |
protected GlobalVariable compiledVariable = null; |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 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 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
public boolean mayContainSequenceConstructor() { |
| 68 |
return true; |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
public boolean isGlobal() { |
| 78 |
return isTopLevel(); |
| 79 |
|
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
public void postValidate() throws XPathException { |
| 88 |
sourceBinding.postValidate(); |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 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 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
protected boolean redundant = false; |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 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 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 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 |
|
| 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 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
public boolean isAssignable() { |
| 220 |
return sourceBinding.hasProperty(SourceBinding.ASSIGNABLE); |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
@Override |
| 230 |
public boolean isDeclaration() { |
| 231 |
return true; |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
public boolean isInstruction() { |
| 241 |
return false; |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 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 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
public void compileDeclaration(Compilation compilation, ComponentDeclaration decl) throws XPathException { |
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 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 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
} |
| 318 |
|
| 319 |
} |
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 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 |
|
| 340 |
exp2 = makeTraceInstruction(this, exp2); |
| 341 |
|
| 342 |
allocateLocalSlots(exp2); |
| 343 |
} |
| 344 |
if (slotManager != null && slotManager.getNumberOfVariables() > 0) { |
| 345 |
var.setContainsLocals(slotManager); |
| 346 |
} |
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
if (exp2 != select) { |
| 351 |
var.setSelectExpression(exp2); |
| 352 |
} |
| 353 |
|
| 354 |
} |
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
public SlotManager getSlotManager() { |
| 365 |
return slotManager; |
| 366 |
} |
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 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 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
public void setRedundant(boolean redundant) { |
| 403 |
this.redundant = redundant; |
| 404 |
} |
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
public void generateByteCode(Optimizer opt) {} |
| 413 |
|
| 414 |
|
| 415 |
} |
| 416 |
|