KickJava   Java API By Example, From Geeks To Geeks.

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


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: Expression.java,v 1.20 2004/02/16 22:24:28 minchau Exp $
18  */

19
20 package com.sun.org.apache.xalan.internal.xsltc.compiler;
21
22 import java.util.Vector JavaDoc;
23
24 import com.sun.org.apache.bcel.internal.generic.BranchHandle;
25 import com.sun.org.apache.bcel.internal.generic.ConstantPoolGen;
26 import com.sun.org.apache.bcel.internal.generic.GOTO_W;
27 import com.sun.org.apache.bcel.internal.generic.IFEQ;
28 import com.sun.org.apache.bcel.internal.generic.InstructionHandle;
29 import com.sun.org.apache.bcel.internal.generic.InstructionList;
30 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.BooleanType;
31 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ClassGenerator;
32 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg;
33 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator;
34 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodType;
35 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.NodeSetType;
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  */

45 abstract class Expression extends SyntaxTreeNode {
46     /**
47      * The type of this expression. It is set after calling
48      * <code>typeCheck()</code>.
49      */

50     protected Type _type;
51
52     /**
53      * Instruction handles that comprise the true list.
54      */

55     protected FlowList _trueList = new FlowList();
56
57     /**
58      * Instruction handles that comprise the false list.
59      */

60     protected FlowList _falseList = new FlowList();
61
62     public Type getType() {
63     return _type;
64     }
65
66     public abstract String JavaDoc toString();
67
68     public boolean hasPositionCall() {
69     return false; // default should be 'false' for StepPattern
70
}
71
72     public boolean hasLastCall() {
73     return false;
74     }
75         
76     /**
77      * Returns an object representing the compile-time evaluation
78      * of an expression. We are only using this for function-available
79      * and element-available at this time.
80      */

81     public Object JavaDoc evaluateAtCompileTime() {
82     return null;
83     }
84
85     /**
86      * Type check all the children of this node.
87      */

88     public Type typeCheck(SymbolTable stable) throws TypeCheckError {
89     return typeCheckContents(stable);
90     }
91
92     /**
93      * Translate this node into JVM bytecodes.
94      */

95     public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
96     ErrorMsg msg = new ErrorMsg(ErrorMsg.NOT_IMPLEMENTED_ERR,
97                     getClass(), this);
98     getParser().reportError(FATAL, msg);
99     }
100     
101     /**
102      * Translate this node into a fresh instruction list.
103      * The original instruction list is saved and restored.
104      */

105     public final InstructionList compile(ClassGenerator classGen,
106                      MethodGenerator methodGen) {
107     final InstructionList result, save = methodGen.getInstructionList();
108     methodGen.setInstructionList(result = new InstructionList());
109     translate(classGen, methodGen);
110     methodGen.setInstructionList(save);
111     return result;
112     }
113
114     /**
115      * Redefined by expressions of type boolean that use flow lists.
116      */

117     public void translateDesynthesized(ClassGenerator classGen,
118                        MethodGenerator methodGen) {
119     translate(classGen, methodGen);
120     if (_type instanceof BooleanType) {
121         desynthesize(classGen, methodGen);
122     }
123     }
124
125     /**
126      * If this expression is of type node-set and it is not a variable
127      * reference, then call setStartNode() passing the context node.
128      */

129     public void startIterator(ClassGenerator classGen,
130                    MethodGenerator methodGen) {
131     // Ignore if type is not node-set
132
if (_type instanceof NodeSetType == false) {
133         return;
134     }
135
136     // setStartNode() should not be called if expr is a variable ref
137
Expression expr = this;
138     if (expr instanceof CastExpr) {
139         expr = ((CastExpr) expr).getExpr();
140     }
141     if (expr instanceof VariableRefBase == false) {
142         final InstructionList il = methodGen.getInstructionList();
143         il.append(methodGen.loadContextNode());
144         il.append(methodGen.setStartNode());
145     }
146     }
147
148     /**
149      * Synthesize a boolean expression, i.e., either push a 0 or 1 onto the
150      * operand stack for the next statement to succeed. Returns the handle
151      * of the instruction to be backpatched.
152      */

153     public void synthesize(ClassGenerator classGen, MethodGenerator methodGen) {
154     final ConstantPoolGen cpg = classGen.getConstantPool();
155     final InstructionList il = methodGen.getInstructionList();
156     _trueList.backPatch(il.append(ICONST_1));
157     final BranchHandle truec = il.append(new GOTO_W(null));
158     _falseList.backPatch(il.append(ICONST_0));
159     truec.setTarget(il.append(NOP));
160     }
161
162     public void desynthesize(ClassGenerator classGen,
163                  MethodGenerator methodGen) {
164     final InstructionList il = methodGen.getInstructionList();
165     _falseList.add(il.append(new IFEQ(null)));
166     }
167
168     public FlowList getFalseList() {
169     return _falseList;
170     }
171
172     public FlowList getTrueList() {
173     return _trueList;
174     }
175
176     public void backPatchFalseList(InstructionHandle ih) {
177     _falseList.backPatch(ih);
178     }
179
180     public void backPatchTrueList(InstructionHandle ih) {
181     _trueList.backPatch(ih);
182     }
183
184     /**
185      * Search for a primop in the symbol table that matches the method type
186      * <code>ctype</code>. Two methods match if they have the same arity.
187      * If a primop is overloaded then the "closest match" is returned. The
188      * first entry in the vector of primops that has the right arity is
189      * considered to be the default one.
190      */

191     public MethodType lookupPrimop(SymbolTable stable, String JavaDoc op,
192                    MethodType ctype) {
193     MethodType result = null;
194     final Vector JavaDoc primop = stable.lookupPrimop(op);
195     if (primop != null) {
196         final int n = primop.size();
197         int minDistance = Integer.MAX_VALUE;
198         for (int i = 0; i < n; i++) {
199         final MethodType ptype = (MethodType) primop.elementAt(i);
200         // Skip if different arity
201
if (ptype.argsCount() != ctype.argsCount()) {
202             continue;
203         }
204                 
205         // The first method with the right arity is the default
206
if (result == null) {
207             result = ptype; // default method
208
}
209
210         // Check if better than last one found
211
final int distance = ctype.distanceTo(ptype);
212         if (distance < minDistance) {
213             minDistance = distance;
214             result = ptype;
215         }
216         }
217     }
218     return result;
219     }
220 }
221
Popular Tags