KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xalan > internal > xsltc > compiler > Variable


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: Variable.java,v 1.27 2004/02/24 02:58:42 zongaro Exp $
18  */

19
20 package com.sun.org.apache.xalan.internal.xsltc.compiler;
21
22 import com.sun.org.apache.bcel.internal.classfile.Field;
23 import com.sun.org.apache.bcel.internal.generic.ACONST_NULL;
24 import com.sun.org.apache.bcel.internal.generic.ConstantPoolGen;
25 import com.sun.org.apache.bcel.internal.generic.DCONST;
26 import com.sun.org.apache.bcel.internal.generic.ICONST;
27 import com.sun.org.apache.bcel.internal.generic.InstructionList;
28 import com.sun.org.apache.bcel.internal.generic.PUTFIELD;
29 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.BooleanType;
30 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ClassGenerator;
31 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg;
32 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.IntType;
33 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator;
34 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.NodeType;
35 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.RealType;
36 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type;
37 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.TypeCheckError;
38
39 /**
40  * @author Jacek Ambroziak
41  * @author Santiago Pericas-Geertsen
42  * @author Morten Jorgensen
43  * @author Erwin Bolwidt <ejb@klomp.org>
44  * @author John Howard <JohnH@schemasoft.com>
45  */

46 final class Variable extends VariableBase {
47
48     public int getIndex() {
49     return (_local != null) ? _local.getIndex() : -1;
50     }
51
52     /**
53      * Parse the contents of the variable
54      */

55     public void parseContents(Parser parser) {
56     // Parse 'name' and 'select' attributes plus parameter contents
57
super.parseContents(parser);
58
59     // Add a ref to this var to its enclosing construct
60
SyntaxTreeNode parent = getParent();
61     if (parent instanceof Stylesheet) {
62         // Mark this as a global variable
63
_isLocal = false;
64         // Check if a global variable with this name already exists...
65
Variable var = parser.getSymbolTable().lookupVariable(_name);
66         // ...and if it does we need to check import precedence
67
if (var != null) {
68         final int us = this.getImportPrecedence();
69         final int them = var.getImportPrecedence();
70         // It is an error if the two have the same import precedence
71
if (us == them) {
72             final String JavaDoc name = _name.toString();
73             reportError(this, parser, ErrorMsg.VARIABLE_REDEF_ERR,name);
74         }
75         // Ignore this if previous definition has higher precedence
76
else if (them > us) {
77             _ignore = true;
78             return;
79         }
80         else {
81             var.disable();
82         }
83         // Add this variable if we have higher precedence
84
}
85         ((Stylesheet)parent).addVariable(this);
86         parser.getSymbolTable().addVariable(this);
87     }
88     else {
89         _isLocal = true;
90     }
91     }
92
93     /**
94      * Runs a type check on either the variable element body or the
95      * expression in the 'select' attribute
96      */

97     public Type typeCheck(SymbolTable stable) throws TypeCheckError {
98
99     // Type check the 'select' expression if present
100
if (_select != null) {
101         _type = _select.typeCheck(stable);
102     }
103     // Type check the element contents otherwise
104
else if (hasContents()) {
105         typeCheckContents(stable);
106         _type = Type.ResultTree;
107     }
108     else {
109         _type = Type.Reference;
110     }
111     // The return type is void as the variable element does not leave
112
// anything on the JVM's stack. The '_type' global will be returned
113
// by the references to this variable, and not by the variable itself.
114
return Type.Void;
115     }
116
117     /**
118      * This method is part of a little trick that is needed to use local
119      * variables inside nested for-each loops. See the initializeVariables()
120      * method in the ForEach class for an explanation
121      */

122     public void initialize(ClassGenerator classGen, MethodGenerator methodGen) {
123     final ConstantPoolGen cpg = classGen.getConstantPool();
124     final InstructionList il = methodGen.getInstructionList();
125
126     // This is only done for local variables that are actually used
127
if (isLocal() && !_refs.isEmpty()) {
128         // Create a variable slot if none is allocated
129
if (_local == null) {
130         _local = methodGen.addLocalVariable2(getEscapedName(),
131                              _type.toJCType(),
132                              il.getEnd());
133         }
134         // Push the default value on the JVM's stack
135
if ((_type instanceof IntType) ||
136         (_type instanceof NodeType) ||
137         (_type instanceof BooleanType))
138         il.append(new ICONST(0)); // 0 for node-id, integer and boolean
139
else if (_type instanceof RealType)
140         il.append(new DCONST(0)); // 0.0 for floating point numbers
141
else
142         il.append(new ACONST_NULL()); // and 'null' for anything else
143
il.append(_type.STORE(_local.getIndex()));
144     }
145     }
146
147     public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
148     final ConstantPoolGen cpg = classGen.getConstantPool();
149     final InstructionList il = methodGen.getInstructionList();
150
151     final String JavaDoc name = getEscapedName();
152
153     // Make sure that a variable instance is only compiled once
154
if (_ignore) return;
155     _ignore = true;
156
157     if (isLocal()) {
158         // Compile variable value computation
159
translateValue(classGen, methodGen);
160
161         // Add a new local variable and store value
162
if (_refs.isEmpty()) { // Remove it if nobody uses the value
163
il.append(_type.POP());
164         _local = null;
165         }
166         else { // Store in local var slot if referenced
167
if (_local == null) mapRegister(methodGen);
168         il.append(_type.STORE(_local.getIndex()));
169         }
170     }
171     else {
172         String JavaDoc signature = _type.toSignature();
173
174         // Global variables are store in class fields
175
if (classGen.containsField(name) == null) {
176         classGen.addField(new Field(ACC_PUBLIC,
177                         cpg.addUtf8(name),
178                         cpg.addUtf8(signature),
179                         null, cpg.getConstantPool()));
180
181         // Push a reference to "this" for putfield
182
il.append(classGen.loadTranslet());
183         // Compile variable value computation
184
translateValue(classGen, methodGen);
185         // Store the variable in the allocated field
186
il.append(new PUTFIELD(cpg.addFieldref(classGen.getClassName(),
187                                name, signature)));
188         }
189     }
190     }
191 }
192
Popular Tags