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: UnresolvedRef.java,v 1.6 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.ClassGenerator;
  21. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg;
  22. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator;
  23. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type;
  24. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.TypeCheckError;
  25. /**
  26. * @author Morten Jorgensen
  27. */
  28. final class UnresolvedRef extends VariableRefBase {
  29. private QName _variableName = null;
  30. private VariableRefBase _ref = null;
  31. private VariableBase _var = null;
  32. private Stylesheet _sheet = null;
  33. public UnresolvedRef(QName name) {
  34. super();
  35. _variableName = name;
  36. _sheet = getStylesheet();
  37. }
  38. public QName getName() {
  39. return(_variableName);
  40. }
  41. private ErrorMsg reportError() {
  42. ErrorMsg err = new ErrorMsg(ErrorMsg.VARIABLE_UNDEF_ERR,
  43. _variableName, this);
  44. getParser().reportError(Constants.ERROR, err);
  45. return(err);
  46. }
  47. private VariableRefBase resolve(Parser parser, SymbolTable stable) {
  48. // At this point the AST is already built and we should be able to
  49. // find any declared global variable or parameter
  50. VariableBase ref = parser.lookupVariable(_variableName);
  51. if (ref == null) ref = (VariableBase)stable.lookupName(_variableName);
  52. if (ref == null) {
  53. reportError();
  54. return null;
  55. }
  56. // Insert the referenced variable as something the parent variable
  57. // is dependent of (this class should only be used under variables)
  58. if ((_var = findParentVariable()) != null) _var.addDependency(ref);
  59. // Instanciate a true variable/parameter ref
  60. if (ref instanceof Variable)
  61. return(new VariableRef((Variable)ref));
  62. else if (ref instanceof Param)
  63. return(new ParameterRef((Param)ref));
  64. else
  65. return null;
  66. }
  67. public Type typeCheck(SymbolTable stable) throws TypeCheckError {
  68. if (_ref != null) {
  69. final String name = _variableName.toString();
  70. ErrorMsg err = new ErrorMsg(ErrorMsg.CIRCULAR_VARIABLE_ERR,
  71. name, this);
  72. }
  73. if ((_ref = resolve(getParser(), stable)) != null) {
  74. return (_type = _ref.typeCheck(stable));
  75. }
  76. throw new TypeCheckError(reportError());
  77. }
  78. public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
  79. if (_ref != null)
  80. _ref.translate(classGen, methodGen);
  81. else
  82. reportError();
  83. }
  84. public String toString() {
  85. return "unresolved-ref()";
  86. }
  87. }