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