1 16 19 20 package com.sun.org.apache.xalan.internal.xsltc.compiler; 21 22 import java.util.Vector ; 23 24 import com.sun.org.apache.bcel.internal.generic.ConstantPoolGen; 25 import com.sun.org.apache.bcel.internal.generic.Instruction; 26 import com.sun.org.apache.bcel.internal.generic.InstructionList; 27 import com.sun.org.apache.bcel.internal.generic.INVOKESPECIAL; 28 import com.sun.org.apache.bcel.internal.generic.LocalVariableGen; 29 import com.sun.org.apache.bcel.internal.generic.NEW; 30 import com.sun.org.apache.bcel.internal.generic.PUSH; 31 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ClassGenerator; 32 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg; 33 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator; 34 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.NodeSetType; 35 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type; 36 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Util; 37 import com.sun.org.apache.xml.internal.utils.XMLChar; 38 39 46 class VariableBase extends TopLevelElement { 47 48 protected QName _name; protected String _escapedName; protected Type _type; protected boolean _isLocal; protected LocalVariableGen _local; protected Instruction _loadInstruction; protected Instruction _storeInstruction; protected Expression _select; protected String select; 58 protected Vector _refs = new Vector (2); 60 61 protected Vector _dependencies = null; 63 64 protected boolean _ignore = false; 66 67 70 public void disable() { 71 _ignore = true; 72 } 73 74 78 public void addReference(VariableRefBase vref) { 79 _refs.addElement(vref); 80 } 81 82 86 public void removeReference(VariableRefBase vref) { 87 _refs.remove(vref); 88 } 89 90 93 public void mapRegister(MethodGenerator methodGen) { 94 if (_local == null) { 95 final InstructionList il = methodGen.getInstructionList(); 96 final String name = getEscapedName(); final com.sun.org.apache.bcel.internal.generic.Type varType = _type.toJCType(); 98 _local = methodGen.addLocalVariable2(name, varType, il.getEnd()); 99 } 100 } 101 102 106 public void unmapRegister(MethodGenerator methodGen) { 107 if (_refs.isEmpty() && (_local != null)) { 108 _local.setEnd(methodGen.getInstructionList().getEnd()); 109 methodGen.removeLocalVariable(_local); 110 _refs = null; 111 _local = null; 112 } 113 } 114 115 119 public Instruction loadInstruction() { 120 final Instruction instr = _loadInstruction; 121 if (_loadInstruction == null) { 122 _loadInstruction = _type.LOAD(_local.getIndex()); 123 } 124 return _loadInstruction; 125 } 126 127 131 public Instruction storeInstruction() { 132 final Instruction instr = _storeInstruction; 133 if (_storeInstruction == null) { 134 _storeInstruction = _type.STORE(_local.getIndex()); 135 } 136 return _storeInstruction; 137 } 138 139 142 public Expression getExpression() { 143 return(_select); 144 } 145 146 149 public String toString() { 150 return("variable("+_name+")"); 151 } 152 153 156 public void display(int indent) { 157 indent(indent); 158 System.out.println("Variable " + _name); 159 if (_select != null) { 160 indent(indent + IndentIncrement); 161 System.out.println("select " + _select.toString()); 162 } 163 displayContents(indent + IndentIncrement); 164 } 165 166 169 public Type getType() { 170 return _type; 171 } 172 173 177 public QName getName() { 178 return _name; 179 } 180 181 184 public String getEscapedName() { 185 return _escapedName; 186 } 187 188 191 public void setName(QName name) { 192 _name = name; 193 _escapedName = Util.escape(name.getStringRep()); 194 } 195 196 199 public boolean isLocal() { 200 return _isLocal; 201 } 202 203 206 public void parseContents(Parser parser) { 207 String name = getAttribute("name"); 209 210 if (name.length() > 0) { 211 if (!XMLChar.isValidQName(name)) { 212 ErrorMsg err = new ErrorMsg(ErrorMsg.INVALID_QNAME_ERR, name, this); 213 parser.reportError(Constants.ERROR, err); 214 } 215 setName(parser.getQNameIgnoreDefaultNs(name)); 216 } 217 else 218 reportError(this, parser, ErrorMsg.REQUIRED_ATTR_ERR, "name"); 219 220 VariableBase other = parser.lookupVariable(_name); 222 if ((other != null) && (other.getParent() == getParent())) { 223 reportError(this, parser, ErrorMsg.VARIABLE_REDEF_ERR, name); 224 } 225 226 select = getAttribute("select"); 227 if (select.length() > 0) { 228 _select = getParser().parseExpression(this, "select", null); 229 if (_select.isDummy()) { 230 reportError(this, parser, ErrorMsg.REQUIRED_ATTR_ERR, "select"); 231 return; 232 } 233 } 234 235 parseChildren(parser); 237 } 238 239 243 public void translateValue(ClassGenerator classGen, 244 MethodGenerator methodGen) { 245 if (_select != null) { 247 _select.translate(classGen, methodGen); 248 if (_select.getType() instanceof NodeSetType) { 251 final ConstantPoolGen cpg = classGen.getConstantPool(); 252 final InstructionList il = methodGen.getInstructionList(); 253 254 final int initCNI = cpg.addMethodref(CACHED_NODE_LIST_ITERATOR_CLASS, 255 "<init>", 256 "(" 257 +NODE_ITERATOR_SIG 258 +")V"); 259 il.append(new NEW(cpg.addClass(CACHED_NODE_LIST_ITERATOR_CLASS))); 260 il.append(DUP_X1); 261 il.append(SWAP); 262 263 il.append(new INVOKESPECIAL(initCNI)); 264 } 265 _select.startIterator(classGen, methodGen); 266 } 267 else if (hasContents()) { 269 compileResultTree(classGen, methodGen); 270 } 271 else { 273 final ConstantPoolGen cpg = classGen.getConstantPool(); 274 final InstructionList il = methodGen.getInstructionList(); 275 il.append(new PUSH(cpg, Constants.EMPTYSTRING)); 276 } 277 } 278 279 } 280 | Popular Tags |