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: ParentPattern.java,v 1.8 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.ConstantPoolGen;
  21. import com.sun.org.apache.bcel.internal.generic.ILOAD;
  22. import com.sun.org.apache.bcel.internal.generic.INVOKEINTERFACE;
  23. import com.sun.org.apache.bcel.internal.generic.ISTORE;
  24. import com.sun.org.apache.bcel.internal.generic.InstructionList;
  25. import com.sun.org.apache.bcel.internal.generic.LocalVariableGen;
  26. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ClassGenerator;
  27. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator;
  28. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type;
  29. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.TypeCheckError;
  30. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Util;
  31. /**
  32. * @author Jacek Ambroziak
  33. * @author Santiago Pericas-Geertsen
  34. */
  35. final class ParentPattern extends RelativePathPattern {
  36. private final Pattern _left;
  37. private final RelativePathPattern _right;
  38. public ParentPattern(Pattern left, RelativePathPattern right) {
  39. (_left = left).setParent(this);
  40. (_right = right).setParent(this);
  41. }
  42. public void setParser(Parser parser) {
  43. super.setParser(parser);
  44. _left.setParser(parser);
  45. _right.setParser(parser);
  46. }
  47. public boolean isWildcard() {
  48. return false;
  49. }
  50. public StepPattern getKernelPattern() {
  51. return _right.getKernelPattern();
  52. }
  53. public void reduceKernelPattern() {
  54. _right.reduceKernelPattern();
  55. }
  56. public Type typeCheck(SymbolTable stable) throws TypeCheckError {
  57. _left.typeCheck(stable);
  58. return _right.typeCheck(stable);
  59. }
  60. public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
  61. final ConstantPoolGen cpg = classGen.getConstantPool();
  62. final InstructionList il = methodGen.getInstructionList();
  63. final LocalVariableGen local =
  64. methodGen.addLocalVariable2("ppt",
  65. Util.getJCRefType(NODE_SIG),
  66. il.getEnd());
  67. final com.sun.org.apache.bcel.internal.generic.Instruction loadLocal =
  68. new ILOAD(local.getIndex());
  69. final com.sun.org.apache.bcel.internal.generic.Instruction storeLocal =
  70. new ISTORE(local.getIndex());
  71. if (_right.isWildcard()) {
  72. il.append(methodGen.loadDOM());
  73. il.append(SWAP);
  74. }
  75. else if (_right instanceof StepPattern) {
  76. il.append(DUP);
  77. il.append(storeLocal);
  78. _right.translate(classGen, methodGen);
  79. il.append(methodGen.loadDOM());
  80. local.setEnd(il.append(loadLocal));
  81. }
  82. else {
  83. _right.translate(classGen, methodGen);
  84. if (_right instanceof AncestorPattern) {
  85. il.append(methodGen.loadDOM());
  86. il.append(SWAP);
  87. }
  88. }
  89. final int getParent = cpg.addInterfaceMethodref(DOM_INTF,
  90. GET_PARENT,
  91. GET_PARENT_SIG);
  92. il.append(new INVOKEINTERFACE(getParent, 2));
  93. final SyntaxTreeNode p = getParent();
  94. if (p == null || p instanceof Instruction ||
  95. p instanceof TopLevelElement)
  96. {
  97. _left.translate(classGen, methodGen);
  98. }
  99. else {
  100. il.append(DUP);
  101. il.append(storeLocal);
  102. _left.translate(classGen, methodGen);
  103. il.append(methodGen.loadDOM());
  104. local.setEnd(il.append(loadLocal));
  105. }
  106. methodGen.removeLocalVariable(local);
  107. /*
  108. * If _right is an ancestor pattern, backpatch _left false
  109. * list to the loop that searches for more ancestors.
  110. */
  111. if (_right instanceof AncestorPattern) {
  112. final AncestorPattern ancestor = (AncestorPattern) _right;
  113. _left.backPatchFalseList(ancestor.getLoopHandle()); // clears list
  114. }
  115. _trueList.append(_right._trueList.append(_left._trueList));
  116. _falseList.append(_right._falseList.append(_left._falseList));
  117. }
  118. public String toString() {
  119. return "Parent(" + _left + ", " + _right + ')';
  120. }
  121. }