1 /*
   2  * reserved comment block
   3  * DO NOT REMOVE OR ALTER!
   4  */
   5 /*
   6  * Copyright 2001-2004 The Apache Software Foundation.
   7  *
   8  * Licensed under the Apache License, Version 2.0 (the "License");
   9  * you may not use this file except in compliance with the License.
  10  * You may obtain a copy of the License at
  11  *
  12  *     http://www.apache.org/licenses/LICENSE-2.0
  13  *
  14  * Unless required by applicable law or agreed to in writing, software
  15  * distributed under the License is distributed on an "AS IS" BASIS,
  16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17  * See the License for the specific language governing permissions and
  18  * limitations under the License.
  19  */
  20 /*
  21  * $Id: UnresolvedRef.java,v 1.5 2005/09/28 13:48:17 pvedula Exp $
  22  */
  23 
  24 package com.sun.org.apache.xalan.internal.xsltc.compiler;
  25 
  26 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ClassGenerator;
  27 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg;
  28 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator;
  29 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type;
  30 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.TypeCheckError;
  31 
  32 /**
  33  * @author Morten Jorgensen
  34  */
  35 final class UnresolvedRef extends VariableRefBase {
  36 
  37     private QName _variableName = null;
  38     private VariableRefBase _ref = null;
  39 
  40     public UnresolvedRef(QName name) {
  41         super();
  42         _variableName = name;
  43     }
  44 
  45     public QName getName() {
  46         return(_variableName);
  47     }
  48 
  49     private ErrorMsg reportError() {
  50         ErrorMsg err = new ErrorMsg(ErrorMsg.VARIABLE_UNDEF_ERR,
  51                                     _variableName, this);
  52         getParser().reportError(Constants.ERROR, err);
  53         return(err);
  54     }
  55 
  56     private VariableRefBase resolve(Parser parser, SymbolTable stable) {
  57         // At this point the AST is already built and we should be able to
  58         // find any declared global variable or parameter
  59         VariableBase ref = parser.lookupVariable(_variableName);
  60         if (ref == null) {
  61             ref = (VariableBase)stable.lookupName(_variableName);
  62         }
  63         if (ref == null) {
  64             reportError();
  65             return null;
  66         }
  67 
  68         // If in a top-level element, create dependency to the referenced var
  69         _variable = ref;
  70         addParentDependency();
  71 
  72         if (ref instanceof Variable) {
  73             return new VariableRef((Variable) ref);
  74         }
  75         else if (ref instanceof Param) {
  76             return new ParameterRef((Param)ref);
  77         }
  78         return null;
  79     }
  80 
  81     public Type typeCheck(SymbolTable stable) throws TypeCheckError {
  82         if (_ref != null) {
  83             final String name = _variableName.toString();
  84             ErrorMsg err = new ErrorMsg(ErrorMsg.CIRCULAR_VARIABLE_ERR,
  85                                         name, this);
  86         }
  87         if ((_ref = resolve(getParser(), stable)) != null) {
  88             return (_type = _ref.typeCheck(stable));
  89         }
  90         throw new TypeCheckError(reportError());
  91     }
  92 
  93     public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
  94         if (_ref != null)
  95             _ref.translate(classGen, methodGen);
  96         else
  97             reportError();
  98     }
  99 
 100     public String toString() {
 101         return "unresolved-ref()";
 102     }
 103 
 104 }