KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xalan > xsltc > compiler > VariableRefBase


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
20 package org.apache.xalan.xsltc.compiler;
21
22 import org.apache.xalan.xsltc.compiler.util.Type;
23 import org.apache.xalan.xsltc.compiler.util.TypeCheckError;
24
25 /**
26  * @author Morten Jorgensen
27  * @author Santiago Pericas-Geertsen
28  */

29 class VariableRefBase extends Expression {
30
31     /**
32      * A reference to the associated variable.
33      */

34     protected final VariableBase _variable;
35
36     /**
37      * A reference to the enclosing expression/instruction for which a
38      * closure is needed (Predicate, Number or Sort).
39      */

40     protected Closure _closure = null;
41
42     public VariableRefBase(VariableBase variable) {
43     _variable = variable;
44     variable.addReference(this);
45     }
46
47     public VariableRefBase() {
48     _variable = null;
49     }
50
51     /**
52      * Returns a reference to the associated variable
53      */

54     public VariableBase getVariable() {
55     return _variable;
56     }
57
58     /**
59      * Returns a reference to any parent variable
60      */

61     public VariableBase findParentVariable() {
62     SyntaxTreeNode node = this;
63     while (node != null && !(node instanceof VariableBase)) {
64         node = node.getParent();
65     }
66     return (VariableBase) node;
67     }
68
69     /**
70      * Two variable references are deemed equal if they refer to the
71      * same variable.
72      */

73     public boolean equals(Object JavaDoc obj) {
74     try {
75         return (_variable == ((VariableRefBase) obj)._variable);
76     }
77     catch (ClassCastException JavaDoc e) {
78         return false;
79     }
80     }
81
82     /**
83      * Returns a string representation of this variable reference on the
84      * format 'variable-ref(<var-name>)'.
85      * @return Variable reference description
86      */

87     public String JavaDoc toString() {
88     return "variable-ref("+_variable.getName()+'/'+_variable.getType()+')';
89     }
90
91     public Type typeCheck(SymbolTable stable)
92     throws TypeCheckError
93     {
94     // Returned cached type if available
95
if (_type != null) return _type;
96
97     // Find nearest closure to add a variable reference
98
if (_variable.isLocal()) {
99         SyntaxTreeNode node = getParent();
100         do {
101         if (node instanceof Closure) {
102             _closure = (Closure) node;
103             break;
104         }
105         if (node instanceof TopLevelElement) {
106             break; // way up in the tree
107
}
108         node = node.getParent();
109         } while (node != null);
110
111         if (_closure != null) {
112         _closure.addVariable(this);
113         }
114     }
115
116     // Insert a dependency link from one variable to another
117
VariableBase parent = findParentVariable();
118         if (parent != null) {
119             VariableBase var = _variable;
120             if (_variable._ignore) {
121                 if (_variable instanceof Variable) {
122                     var = parent.getSymbolTable()
123                                 .lookupVariable(_variable._name);
124                 } else if (_variable instanceof Param) {
125                     var = parent.getSymbolTable().lookupParam(_variable._name);
126                 }
127             }
128             parent.addDependency(var);
129         }
130
131         // Attempt to get the cached variable type
132
_type = _variable.getType();
133
134         // If that does not work we must force a type-check (this is normally
135
// only needed for globals in included/imported stylesheets
136
if (_type == null) {
137             _variable.typeCheck(stable);
138             _type = _variable.getType();
139         }
140
141         // Return the type of the referenced variable
142
return _type;
143     }
144
145 }
146
Popular Tags