1 16 19 20 package com.sun.org.apache.xalan.internal.xsltc.compiler; 21 22 import java.util.Enumeration ; 23 import java.util.Vector ; 24 25 import com.sun.org.apache.bcel.internal.generic.BranchHandle; 26 import com.sun.org.apache.bcel.internal.generic.GOTO; 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.ClassGenerator; 31 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg; 32 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator; 33 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type; 34 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.TypeCheckError; 35 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Util; 36 37 42 final class Choose extends Instruction { 43 44 47 public void display(int indent) { 48 indent(indent); 49 Util.println("Choose"); 50 indent(indent + IndentIncrement); 51 displayContents(indent + IndentIncrement); 52 } 53 54 58 public void translate(ClassGenerator classGen, MethodGenerator methodGen) { 59 final Vector whenElements = new Vector (); 60 Otherwise otherwise = null; 61 Enumeration elements = elements(); 62 63 ErrorMsg error = null; 65 final int line = getLineNumber(); 66 67 while (elements.hasMoreElements()) { 69 Object element = elements.nextElement(); 70 if (element instanceof When) { 72 whenElements.addElement(element); 73 } 74 else if (element instanceof Otherwise) { 76 if (otherwise == null) { 77 otherwise = (Otherwise)element; 78 } 79 else { 80 error = new ErrorMsg(ErrorMsg.MULTIPLE_OTHERWISE_ERR, this); 81 getParser().reportError(Constants.ERROR, error); 82 } 83 } 84 else if (element instanceof Text) { 85 ((Text)element).ignore(); 86 } 87 else { 89 error = new ErrorMsg(ErrorMsg.WHEN_ELEMENT_ERR, this); 90 getParser().reportError(Constants.ERROR, error); 91 } 92 } 93 94 if (whenElements.size() == 0) { 96 error = new ErrorMsg(ErrorMsg.MISSING_WHEN_ERR, this); 97 getParser().reportError(Constants.ERROR, error); 98 return; 99 } 100 101 InstructionList il = methodGen.getInstructionList(); 102 103 BranchHandle nextElement = null; 106 Vector exitHandles = new Vector (); 107 InstructionHandle exit = null; 108 109 Enumeration whens = whenElements.elements(); 110 while (whens.hasMoreElements()) { 111 final When when = (When)whens.nextElement(); 112 final Expression test = when.getTest(); 113 114 InstructionHandle truec = il.getEnd(); 115 116 if (nextElement != null) 117 nextElement.setTarget(il.append(NOP)); 118 test.translateDesynthesized(classGen, methodGen); 119 120 if (test instanceof FunctionCall) { 121 FunctionCall call = (FunctionCall)test; 122 try { 123 Type type = call.typeCheck(getParser().getSymbolTable()); 124 if (type != Type.Boolean) { 125 test._falseList.add(il.append(new IFEQ(null))); 126 } 127 } 128 catch (TypeCheckError e) { 129 } 131 } 132 truec = il.getEnd(); 134 135 if (!when.ignore()) when.translateContents(classGen, methodGen); 138 139 exitHandles.addElement(il.append(new GOTO(null))); 141 if (whens.hasMoreElements() || otherwise != null) { 142 nextElement = il.append(new GOTO(null)); 143 test.backPatchFalseList(nextElement); 144 } 145 else 146 test.backPatchFalseList(exit = il.append(NOP)); 147 test.backPatchTrueList(truec.getNext()); 148 } 149 150 if (otherwise != null) { 152 nextElement.setTarget(il.append(NOP)); 153 otherwise.translateContents(classGen, methodGen); 154 exit = il.append(NOP); 155 } 156 157 Enumeration exitGotos = exitHandles.elements(); 159 while (exitGotos.hasMoreElements()) { 160 BranchHandle gotoExit = (BranchHandle)exitGotos.nextElement(); 161 gotoExit.setTarget(exit); 162 } 163 } 164 } 165 | Popular Tags |