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: AncestorPattern.java,v 1.2.4.1 2005/09/01 09:19:41 pvedula Exp $
22   */
23  
24  package com.sun.org.apache.xalan.internal.xsltc.compiler;
25  
26  import com.sun.org.apache.bcel.internal.generic.BranchHandle;
27  import com.sun.org.apache.bcel.internal.generic.ConstantPoolGen;
28  import com.sun.org.apache.bcel.internal.generic.GOTO;
29  import com.sun.org.apache.bcel.internal.generic.IFLT;
30  import com.sun.org.apache.bcel.internal.generic.ILOAD;
31  import com.sun.org.apache.bcel.internal.generic.INVOKEINTERFACE;
32  import com.sun.org.apache.bcel.internal.generic.ISTORE;
33  import com.sun.org.apache.bcel.internal.generic.InstructionHandle;
34  import com.sun.org.apache.bcel.internal.generic.InstructionList;
35  import com.sun.org.apache.bcel.internal.generic.LocalVariableGen;
36  import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ClassGenerator;
37  import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator;
38  import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type;
39  import com.sun.org.apache.xalan.internal.xsltc.compiler.util.TypeCheckError;
40  import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Util;
41  
42  /**
43   * @author Jacek Ambroziak
44   * @author Santiago Pericas-Geertsen
45   * @author Erwin Bolwidt <ejb@klomp.org>
46   */
47  final class AncestorPattern extends RelativePathPattern {
48  
49      private final Pattern _left;        // may be null
50      private final RelativePathPattern _right;
51      private InstructionHandle _loop;
52  
53      public AncestorPattern(RelativePathPattern right) {
54          this(null, right);
55      }
56  
57      public AncestorPattern(Pattern left, RelativePathPattern right) {
58          _left = left;
59          (_right = right).setParent(this);
60          if (left != null) {
61              left.setParent(this);
62          }
63      }
64  
65      public InstructionHandle getLoopHandle() {
66          return _loop;
67      }
68  
69      public void setParser(Parser parser) {
70          super.setParser(parser);
71          if (_left != null) {
72              _left.setParser(parser);
73          }
74          _right.setParser(parser);
75      }
76  
77      public boolean isWildcard() {
78          //!!! can be wildcard
79          return false;
80      }
81  
82      public StepPattern getKernelPattern() {
83          return _right.getKernelPattern();
84      }
85  
86      public void reduceKernelPattern() {
87          _right.reduceKernelPattern();
88      }
89  
90      public Type typeCheck(SymbolTable stable) throws TypeCheckError {
91          if (_left != null) {
92              _left.typeCheck(stable);
93          }
94          return _right.typeCheck(stable);
95      }
96  
97      public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
98          InstructionHandle parent;
99          final ConstantPoolGen cpg = classGen.getConstantPool();
100         final InstructionList il = methodGen.getInstructionList();
101 
102         /*
103          * The scope of this local var must be the entire method since
104          * a another pattern may decide to jump back into the loop
105          */
106         final LocalVariableGen local =
107             methodGen.addLocalVariable2("app", Util.getJCRefType(NODE_SIG),
108                                         il.getEnd());
109 
110         final com.sun.org.apache.bcel.internal.generic.Instruction loadLocal =
111             new ILOAD(local.getIndex());
112         final com.sun.org.apache.bcel.internal.generic.Instruction storeLocal =
113             new ISTORE(local.getIndex());
114 
115         if (_right instanceof StepPattern) {
116             il.append(DUP);
117             il.append(storeLocal);
118             _right.translate(classGen, methodGen);
119             il.append(methodGen.loadDOM());
120             il.append(loadLocal);
121         }
122         else {
123             _right.translate(classGen, methodGen);
124 
125             if (_right instanceof AncestorPattern) {
126                 il.append(methodGen.loadDOM());
127                 il.append(SWAP);
128             }
129         }
130 
131         if (_left != null) {
132             final int getParent = cpg.addInterfaceMethodref(DOM_INTF,
133                                                             GET_PARENT,
134                                                             GET_PARENT_SIG);
135             parent = il.append(new INVOKEINTERFACE(getParent, 2));
136 
137             il.append(DUP);
138             il.append(storeLocal);
139             _falseList.add(il.append(new IFLT(null)));
140             il.append(loadLocal);
141 
142             _left.translate(classGen, methodGen);
143 
144             final SyntaxTreeNode p = getParent();
145             if (p == null || p instanceof Instruction ||
146                 p instanceof TopLevelElement)
147             {
148                 // do nothing
149             }
150             else {
151                 il.append(loadLocal);
152             }
153 
154             final BranchHandle exit = il.append(new GOTO(null));
155             _loop = il.append(methodGen.loadDOM());
156             il.append(loadLocal);
157             local.setEnd(_loop);
158             il.append(new GOTO(parent));
159             exit.setTarget(il.append(NOP));
160             _left.backPatchFalseList(_loop);
161 
162             _trueList.append(_left._trueList);
163         }
164         else {
165             il.append(POP2);
166         }
167 
168         /*
169          * If _right is an ancestor pattern, backpatch this pattern's false
170          * list to the loop that searches for more ancestors.
171          */
172         if (_right instanceof AncestorPattern) {
173             final AncestorPattern ancestor = (AncestorPattern) _right;
174             _falseList.backPatch(ancestor.getLoopHandle());    // clears list
175         }
176 
177         _trueList.append(_right._trueList);
178         _falseList.append(_right._falseList);
179     }
180 
181     public String toString() {
182         return "AncestorPattern(" + _left + ", " + _right + ')';
183     }
184 }