View Javadoc
1   /*
2    * reserved comment block
3    * DO NOT REMOVE OR ALTER!
4    */
5   /*
6    * Copyright 2001-2004 The Apache Software Foundation.
7    *
8    * Licensed under the Apache License, Version 2.0 (the "License");
9    * you may not use this file except in compliance with the License.
10   * You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  /*
21   * $Id: Expression.java,v 1.2.4.1 2005/09/01 14:17:51 pvedula Exp $
22   */
23  
24  package com.sun.org.apache.xalan.internal.xsltc.compiler;
25  
26  import java.util.Vector;
27  
28  import com.sun.org.apache.bcel.internal.generic.BranchHandle;
29  import com.sun.org.apache.bcel.internal.generic.ConstantPoolGen;
30  import com.sun.org.apache.bcel.internal.generic.GOTO_W;
31  import com.sun.org.apache.bcel.internal.generic.IFEQ;
32  import com.sun.org.apache.bcel.internal.generic.InstructionHandle;
33  import com.sun.org.apache.bcel.internal.generic.InstructionList;
34  import com.sun.org.apache.xalan.internal.xsltc.compiler.util.BooleanType;
35  import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ClassGenerator;
36  import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg;
37  import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator;
38  import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodType;
39  import com.sun.org.apache.xalan.internal.xsltc.compiler.util.NodeSetType;
40  import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type;
41  import com.sun.org.apache.xalan.internal.xsltc.compiler.util.TypeCheckError;
42  
43  /**
44   * @author Jacek Ambroziak
45   * @author Santiago Pericas-Geertsen
46   * @author Morten Jorgensen
47   * @author Erwin Bolwidt <ejb@klomp.org>
48   */
49  abstract class Expression extends SyntaxTreeNode {
50      /**
51       * The type of this expression. It is set after calling
52       * <code>typeCheck()</code>.
53       */
54      protected Type _type;
55  
56      /**
57       * Instruction handles that comprise the true list.
58       */
59      protected FlowList _trueList = new FlowList();
60  
61      /**
62       * Instruction handles that comprise the false list.
63       */
64      protected FlowList _falseList = new FlowList();
65  
66      public Type getType() {
67          return _type;
68      }
69  
70      public abstract String toString();
71  
72      public boolean hasPositionCall() {
73          return false;           // default should be 'false' for StepPattern
74      }
75  
76      public boolean hasLastCall() {
77          return false;
78      }
79  
80      /**
81       * Returns an object representing the compile-time evaluation
82       * of an expression. We are only using this for function-available
83       * and element-available at this time.
84       */
85      public Object evaluateAtCompileTime() {
86          return null;
87      }
88  
89      /**
90       * Type check all the children of this node.
91       */
92      public Type typeCheck(SymbolTable stable) throws TypeCheckError {
93          return typeCheckContents(stable);
94      }
95  
96      /**
97       * Translate this node into JVM bytecodes.
98       */
99      public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
100         ErrorMsg msg = new ErrorMsg(ErrorMsg.NOT_IMPLEMENTED_ERR,
101                                     getClass(), this);
102         getParser().reportError(FATAL, msg);
103     }
104 
105     /**
106      * Translate this node into a fresh instruction list.
107      * The original instruction list is saved and restored.
108      */
109     public final InstructionList compile(ClassGenerator classGen,
110                                          MethodGenerator methodGen) {
111         final InstructionList result, save = methodGen.getInstructionList();
112         methodGen.setInstructionList(result = new InstructionList());
113         translate(classGen, methodGen);
114         methodGen.setInstructionList(save);
115         return result;
116     }
117 
118     /**
119      * Redefined by expressions of type boolean that use flow lists.
120      */
121     public void translateDesynthesized(ClassGenerator classGen,
122                                        MethodGenerator methodGen) {
123         translate(classGen, methodGen);
124         if (_type instanceof BooleanType) {
125             desynthesize(classGen, methodGen);
126         }
127     }
128 
129     /**
130      * If this expression is of type node-set and it is not a variable
131      * reference, then call setStartNode() passing the context node.
132      */
133     public void startIterator(ClassGenerator classGen,
134                                    MethodGenerator methodGen) {
135         // Ignore if type is not node-set
136         if (_type instanceof NodeSetType == false) {
137             return;
138         }
139 
140         // setStartNode() should not be called if expr is a variable ref
141         Expression expr = this;
142         if (expr instanceof CastExpr) {
143             expr = ((CastExpr) expr).getExpr();
144         }
145         if (expr instanceof VariableRefBase == false) {
146             final InstructionList il = methodGen.getInstructionList();
147             il.append(methodGen.loadContextNode());
148             il.append(methodGen.setStartNode());
149         }
150     }
151 
152     /**
153      * Synthesize a boolean expression, i.e., either push a 0 or 1 onto the
154      * operand stack for the next statement to succeed. Returns the handle
155      * of the instruction to be backpatched.
156      */
157     public void synthesize(ClassGenerator classGen, MethodGenerator methodGen) {
158         final ConstantPoolGen cpg = classGen.getConstantPool();
159         final InstructionList il = methodGen.getInstructionList();
160         _trueList.backPatch(il.append(ICONST_1));
161         final BranchHandle truec = il.append(new GOTO_W(null));
162         _falseList.backPatch(il.append(ICONST_0));
163         truec.setTarget(il.append(NOP));
164     }
165 
166     public void desynthesize(ClassGenerator classGen,
167                              MethodGenerator methodGen) {
168         final InstructionList il = methodGen.getInstructionList();
169         _falseList.add(il.append(new IFEQ(null)));
170     }
171 
172     public FlowList getFalseList() {
173         return _falseList;
174     }
175 
176     public FlowList getTrueList() {
177         return _trueList;
178     }
179 
180     public void backPatchFalseList(InstructionHandle ih) {
181         _falseList.backPatch(ih);
182     }
183 
184     public void backPatchTrueList(InstructionHandle ih) {
185         _trueList.backPatch(ih);
186     }
187 
188     /**
189      * Search for a primop in the symbol table that matches the method type
190      * <code>ctype</code>. Two methods match if they have the same arity.
191      * If a primop is overloaded then the "closest match" is returned. The
192      * first entry in the vector of primops that has the right arity is
193      * considered to be the default one.
194      */
195     public MethodType lookupPrimop(SymbolTable stable, String op,
196                                    MethodType ctype) {
197         MethodType result = null;
198         final Vector primop = stable.lookupPrimop(op);
199         if (primop != null) {
200             final int n = primop.size();
201             int minDistance = Integer.MAX_VALUE;
202             for (int i = 0; i < n; i++) {
203                 final MethodType ptype = (MethodType) primop.elementAt(i);
204                 // Skip if different arity
205                 if (ptype.argsCount() != ctype.argsCount()) {
206                     continue;
207                 }
208 
209                 // The first method with the right arity is the default
210                 if (result == null) {
211                     result = ptype;             // default method
212                 }
213 
214                 // Check if better than last one found
215                 final int distance = ctype.distanceTo(ptype);
216                 if (distance < minDistance) {
217                     minDistance = distance;
218                     result = ptype;
219                 }
220             }
221         }
222         return result;
223     }
224 }