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