1 16 19 20 package org.apache.xalan.xsltc.compiler; 21 22 import java.util.Vector ; 23 24 import org.apache.bcel.generic.ConstantPoolGen; 25 import org.apache.bcel.generic.Instruction; 26 import org.apache.bcel.generic.InstructionList; 27 import org.apache.bcel.generic.INVOKESPECIAL; 28 import org.apache.bcel.generic.LocalVariableGen; 29 import org.apache.bcel.generic.NEW; 30 import org.apache.bcel.generic.PUSH; 31 import org.apache.xalan.xsltc.compiler.util.ClassGenerator; 32 import org.apache.xalan.xsltc.compiler.util.ErrorMsg; 33 import org.apache.xalan.xsltc.compiler.util.MethodGenerator; 34 import org.apache.xalan.xsltc.compiler.util.NodeSetType; 35 import org.apache.xalan.xsltc.compiler.util.Type; 36 import org.apache.xalan.xsltc.compiler.util.Util; 37 import org.apache.xml.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 protected int _weight = 0; 69 70 73 public void disable() { 74 _ignore = true; 75 } 76 77 81 public void addReference(VariableRefBase vref) { 82 _refs.addElement(vref); 83 } 84 85 89 public void removeReference(VariableRefBase vref) { 90 _refs.remove(vref); 91 } 92 93 96 public void addDependency(VariableBase other) { 97 if (_dependencies == null) { 98 _dependencies = new Vector (); 99 } 100 if (!_dependencies.contains(other)) { 101 _dependencies.addElement(other); 102 } 103 } 104 105 108 public Vector getDependencies() { 109 return _dependencies; 110 } 111 112 115 public void mapRegister(MethodGenerator methodGen) { 116 if (_local == null) { 117 final InstructionList il = methodGen.getInstructionList(); 118 final String name = getEscapedName(); final org.apache.bcel.generic.Type varType = _type.toJCType(); 120 _local = methodGen.addLocalVariable2(name, varType, il.getEnd()); 121 } 122 } 123 124 128 public void unmapRegister(MethodGenerator methodGen) { 129 if (_refs.isEmpty() && (_local != null)) { 130 _local.setEnd(methodGen.getInstructionList().getEnd()); 131 methodGen.removeLocalVariable(_local); 132 _refs = null; 133 _local = null; 134 } 135 } 136 137 141 public Instruction loadInstruction() { 142 final Instruction instr = _loadInstruction; 143 if (_loadInstruction == null) { 144 _loadInstruction = _type.LOAD(_local.getIndex()); 145 } 146 return _loadInstruction; 147 } 148 149 153 public Instruction storeInstruction() { 154 final Instruction instr = _storeInstruction; 155 if (_storeInstruction == null) { 156 _storeInstruction = _type.STORE(_local.getIndex()); 157 } 158 return _storeInstruction; 159 } 160 161 164 public Expression getExpression() { 165 return(_select); 166 } 167 168 171 public String toString() { 172 return("variable("+_name+")"); 173 } 174 175 178 public void display(int indent) { 179 indent(indent); 180 System.out.println("Variable " + _name); 181 if (_select != null) { 182 indent(indent + IndentIncrement); 183 System.out.println("select " + _select.toString()); 184 } 185 displayContents(indent + IndentIncrement); 186 } 187 188 191 public Type getType() { 192 return _type; 193 } 194 195 199 public QName getName() { 200 return _name; 201 } 202 203 206 public String getEscapedName() { 207 return _escapedName; 208 } 209 210 213 public void setName(QName name) { 214 _name = name; 215 _escapedName = Util.escape(name.getStringRep()); 216 } 217 218 221 public boolean isLocal() { 222 return _isLocal; 223 } 224 225 228 public void parseContents(Parser parser) { 229 String name = getAttribute("name"); 231 232 if (name.length() > 0) { 233 if (!XMLChar.isValidQName(name)) { 234 ErrorMsg err = new ErrorMsg(ErrorMsg.INVALID_QNAME_ERR, name, this); 235 parser.reportError(Constants.ERROR, err); 236 } 237 setName(parser.getQNameIgnoreDefaultNs(name)); 238 } 239 else 240 reportError(this, parser, ErrorMsg.REQUIRED_ATTR_ERR, "name"); 241 242 VariableBase other = parser.lookupVariable(_name); 244 if ((other != null) && (other.getParent() == getParent())) { 245 reportError(this, parser, ErrorMsg.VARIABLE_REDEF_ERR, name); 246 } 247 248 select = getAttribute("select"); 249 if (select.length() > 0) { 250 _select = getParser().parseExpression(this, "select", null); 251 if (_select.isDummy()) { 252 reportError(this, parser, ErrorMsg.REQUIRED_ATTR_ERR, "select"); 253 return; 254 } 255 } 256 257 parseChildren(parser); 259 } 260 261 265 public void translateValue(ClassGenerator classGen, 266 MethodGenerator methodGen) { 267 if (_select != null) { 269 _select.translate(classGen, methodGen); 270 if (_select.getType() instanceof NodeSetType) { 273 final ConstantPoolGen cpg = classGen.getConstantPool(); 274 final InstructionList il = methodGen.getInstructionList(); 275 276 final int initCNI = cpg.addMethodref(CACHED_NODE_LIST_ITERATOR_CLASS, 277 "<init>", 278 "(" 279 +NODE_ITERATOR_SIG 280 +")V"); 281 il.append(new NEW(cpg.addClass(CACHED_NODE_LIST_ITERATOR_CLASS))); 282 il.append(DUP_X1); 283 il.append(SWAP); 284 285 il.append(new INVOKESPECIAL(initCNI)); 286 } 287 _select.startIterator(classGen, methodGen); 288 } 289 else if (hasContents()) { 291 compileResultTree(classGen, methodGen); 292 } 293 else { 295 final ConstantPoolGen cpg = classGen.getConstantPool(); 296 final InstructionList il = methodGen.getInstructionList(); 297 il.append(new PUSH(cpg, Constants.EMPTYSTRING)); 298 } 299 } 300 301 } 302 | Popular Tags |