1. /*
  2. * Copyright 2001-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /*
  17. * $Id: VariableRefBase.java,v 1.13 2004/02/16 22:25:10 minchau Exp $
  18. */
  19. package com.sun.org.apache.xalan.internal.xsltc.compiler;
  20. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type;
  21. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.TypeCheckError;
  22. /**
  23. * @author Morten Jorgensen
  24. * @author Santiago Pericas-Geertsen
  25. */
  26. class VariableRefBase extends Expression {
  27. /**
  28. * A reference to the associated variable.
  29. */
  30. protected final VariableBase _variable;
  31. /**
  32. * A reference to the enclosing expression/instruction for which a
  33. * closure is needed (Predicate, Number or Sort).
  34. */
  35. protected Closure _closure = null;
  36. public VariableRefBase(VariableBase variable) {
  37. _variable = variable;
  38. variable.addReference(this);
  39. }
  40. public VariableRefBase() {
  41. _variable = null;
  42. }
  43. /**
  44. * Returns a reference to the associated variable
  45. */
  46. public VariableBase getVariable() {
  47. return _variable;
  48. }
  49. /**
  50. * Returns a reference to any parent variable
  51. */
  52. public VariableBase findParentVariable() {
  53. SyntaxTreeNode node = this;
  54. while (node != null && !(node instanceof VariableBase)) {
  55. node = node.getParent();
  56. }
  57. return (VariableBase) node;
  58. }
  59. /**
  60. * Two variable references are deemed equal if they refer to the
  61. * same variable.
  62. */
  63. public boolean equals(Object obj) {
  64. try {
  65. return (_variable == ((VariableRefBase) obj)._variable);
  66. }
  67. catch (ClassCastException e) {
  68. return false;
  69. }
  70. }
  71. /**
  72. * Returns a string representation of this variable reference on the
  73. * format 'variable-ref(<var-name>)'.
  74. * @return Variable reference description
  75. */
  76. public String toString() {
  77. return "variable-ref("+_variable.getName()+'/'+_variable.getType()+')';
  78. }
  79. public Type typeCheck(SymbolTable stable)
  80. throws TypeCheckError
  81. {
  82. // Returned cached type if available
  83. if (_type != null) return _type;
  84. // Find nearest closure to add a variable reference
  85. if (_variable.isLocal()) {
  86. SyntaxTreeNode node = getParent();
  87. do {
  88. if (node instanceof Closure) {
  89. _closure = (Closure) node;
  90. break;
  91. }
  92. if (node instanceof TopLevelElement) {
  93. break; // way up in the tree
  94. }
  95. node = node.getParent();
  96. } while (node != null);
  97. if (_closure != null) {
  98. _closure.addVariable(this);
  99. }
  100. }
  101. // Insert a dependency link from one variable to another
  102. VariableBase parent = findParentVariable();
  103. if (parent != null) {
  104. VariableBase var = _variable;
  105. if (_variable._ignore) {
  106. if (_variable instanceof Variable) {
  107. var = parent.getSymbolTable()
  108. .lookupVariable(_variable._name);
  109. } else if (_variable instanceof Param) {
  110. var = parent.getSymbolTable().lookupParam(_variable._name);
  111. }
  112. }
  113. parent.addDependency(var);
  114. }
  115. // Attempt to get the cached variable type
  116. _type = _variable.getType();
  117. // If that does not work we must force a type-check (this is normally
  118. // only needed for globals in included/imported stylesheets
  119. if (_type == null) {
  120. _variable.typeCheck(stable);
  121. _type = _variable.getType();
  122. }
  123. // Return the type of the referenced variable
  124. return _type;
  125. }
  126. }