001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements. See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership. The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the  "License");
007     * you may not use this file except in compliance with the License.
008     * You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    /*
019     * $Id: UnresolvedRef.java 476471 2006-11-18 08:36:27Z minchau $
020     */
021    
022    package org.apache.xalan.xsltc.compiler;
023    
024    import org.apache.xalan.xsltc.compiler.util.ClassGenerator;
025    import org.apache.xalan.xsltc.compiler.util.ErrorMsg;
026    import org.apache.xalan.xsltc.compiler.util.MethodGenerator;
027    import org.apache.xalan.xsltc.compiler.util.Type;
028    import org.apache.xalan.xsltc.compiler.util.TypeCheckError;
029    
030    /**
031     * @author Morten Jorgensen
032     */
033    final class UnresolvedRef extends VariableRefBase {
034    
035        private QName _variableName = null;
036        private VariableRefBase _ref = null;
037    
038        public UnresolvedRef(QName name) {
039            super();
040            _variableName = name;
041        }
042    
043        public QName getName() {
044            return(_variableName);
045        }
046    
047        private ErrorMsg reportError() {
048            ErrorMsg err = new ErrorMsg(ErrorMsg.VARIABLE_UNDEF_ERR,
049                                        _variableName, this);
050            getParser().reportError(Constants.ERROR, err);
051            return(err);
052        }
053    
054        private VariableRefBase resolve(Parser parser, SymbolTable stable) {
055            // At this point the AST is already built and we should be able to
056            // find any declared global variable or parameter
057            VariableBase ref = parser.lookupVariable(_variableName);
058            if (ref == null) {
059                ref = (VariableBase)stable.lookupName(_variableName);
060            }
061            if (ref == null) {
062                reportError();
063                return null;
064            }
065            
066            // If in a top-level element, create dependency to the referenced var
067            _variable = ref;
068            addParentDependency();
069            
070            if (ref instanceof Variable) {
071                return new VariableRef((Variable) ref);
072            }
073            else if (ref instanceof Param) {
074                return new ParameterRef((Param)ref);
075            }        
076            return null;
077        }
078    
079        public Type typeCheck(SymbolTable stable) throws TypeCheckError {
080            if (_ref != null) {
081                final String name = _variableName.toString();
082                ErrorMsg err = new ErrorMsg(ErrorMsg.CIRCULAR_VARIABLE_ERR,
083                                            name, this);
084            }
085            if ((_ref = resolve(getParser(), stable)) != null) {
086                return (_type = _ref.typeCheck(stable));
087            }
088            throw new TypeCheckError(reportError());
089        }
090    
091        public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
092            if (_ref != null)
093                _ref.translate(classGen, methodGen);
094            else
095                reportError();
096        }
097    
098        public String toString() {
099            return "unresolved-ref()";
100        }
101    
102    }