FileDocCategorySizeDatePackage
VariableBase.javaAPI DocJava SE 6 API9032Tue Jun 10 00:22:30 BST 2008com.sun.org.apache.xalan.internal.xsltc.compiler

VariableBase

public class VariableBase extends TopLevelElement
author
Jacek Ambroziak
author
Santiago Pericas-Geertsen
author
Morten Jorgensen
author
Erwin Bolwidt
author
John Howard

Fields Summary
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
protected Vector
_refs
protected Vector
_dependencies
protected boolean
_ignore
Constructors Summary
Methods Summary
public voidaddReference(com.sun.org.apache.xalan.internal.xsltc.compiler.VariableRefBase vref)
Add a reference to this variable. Called by VariableRef when an expression contains a reference to this variable.

	_refs.addElement(vref);
    
public voiddisable()
Disable this variable/parameter


            
       
	_ignore = true;
    
public voiddisplay(int indent)
Display variable in a full AST dump

	indent(indent);
	System.out.println("Variable " + _name);
	if (_select != null) { 
	    indent(indent + IndentIncrement);
	    System.out.println("select " + _select.toString());
	}
	displayContents(indent + IndentIncrement);
    
public java.lang.StringgetEscapedName()
Returns the escaped qname of the variable or parameter

	return _escapedName;
    
public com.sun.org.apache.xalan.internal.xsltc.compiler.ExpressiongetExpression()
Returns the expression from this variable's select attribute (if any)

	return(_select);
    
public com.sun.org.apache.xalan.internal.xsltc.compiler.QNamegetName()
Returns the name of the variable or parameter as it will occur in the compiled translet.

	return _name;
    
public com.sun.org.apache.xalan.internal.xsltc.compiler.util.TypegetType()
Returns the type of the variable

	return _type;
    
public booleanisLocal()
Returns the true if the variable is local

	return _isLocal;
    
public com.sun.org.apache.bcel.internal.generic.InstructionloadInstruction()
Returns an instruction for loading the value of this variable onto the JVM stack.

	final Instruction instr = _loadInstruction;
	if (_loadInstruction == null) {
	    _loadInstruction = _type.LOAD(_local.getIndex());
        }
	return _loadInstruction;
    
public voidmapRegister(com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator methodGen)
Map this variable to a register

        if (_local == null) {
            final InstructionList il = methodGen.getInstructionList();
	    final String name = getEscapedName(); // TODO: namespace ?
	    final com.sun.org.apache.bcel.internal.generic.Type varType = _type.toJCType();
            _local = methodGen.addLocalVariable2(name, varType, il.getEnd());
        }
    
public voidparseContents(com.sun.org.apache.xalan.internal.xsltc.compiler.Parser parser)
Parse the contents of the element.

	// Get the 'name attribute
	String name = getAttribute("name");

        if (name.length() > 0) {
            if (!XML11Char.isXML11ValidQName(name)) {
                ErrorMsg err = new ErrorMsg(ErrorMsg.INVALID_QNAME_ERR, name, this);
                parser.reportError(Constants.ERROR, err);           
            }   
	    setName(parser.getQNameIgnoreDefaultNs(name));
        }
        else
	    reportError(this, parser, ErrorMsg.REQUIRED_ATTR_ERR, "name");

	// Check whether variable/param of the same name is already in scope
	VariableBase other = parser.lookupVariable(_name);
	if ((other != null) && (other.getParent() == getParent())) {
	    reportError(this, parser, ErrorMsg.VARIABLE_REDEF_ERR, name);
	}
	
	select = getAttribute("select");
	if (select.length() > 0) {
	    _select = getParser().parseExpression(this, "select", null);
	    if (_select.isDummy()) {
		reportError(this, parser, ErrorMsg.REQUIRED_ATTR_ERR, "select");
		return;
	    }
	}

	// Children must be parsed first -> static scoping
	parseChildren(parser);
    
public voidremoveReference(com.sun.org.apache.xalan.internal.xsltc.compiler.VariableRefBase vref)
Remove a reference to this variable. Called by VariableRef when this variable goes out of scope.

	_refs.remove(vref);
    
public voidsetName(com.sun.org.apache.xalan.internal.xsltc.compiler.QName name)
Set the name of the variable or paremeter. Escape all special chars.

	_name = name;
	_escapedName = Util.escape(name.getStringRep());
    
public com.sun.org.apache.bcel.internal.generic.InstructionstoreInstruction()
Returns an instruction for storing a value from the JVM stack into this variable.

	final Instruction instr = _storeInstruction;
	if (_storeInstruction == null) {
	    _storeInstruction = _type.STORE(_local.getIndex());
        }
	return _storeInstruction;
    
public java.lang.StringtoString()
Display variable as single string

	return("variable("+_name+")");
    
public voidtranslateValue(com.sun.org.apache.xalan.internal.xsltc.compiler.util.ClassGenerator classGen, com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator methodGen)
Compile the value of the variable, which is either in an expression in a 'select' attribute, or in the variable elements body

	// Compile expression is 'select' attribute if present
	if (_select != null) {
	    _select.translate(classGen, methodGen);
	    // Create a CachedNodeListIterator for select expressions
	    // in a variable or parameter.
	    if (_select.getType() instanceof NodeSetType) {
	        final ConstantPoolGen cpg = classGen.getConstantPool();
	        final InstructionList il = methodGen.getInstructionList();
	    	
	        final int initCNI = cpg.addMethodref(CACHED_NODE_LIST_ITERATOR_CLASS,
					    "<init>",
					    "("
					    +NODE_ITERATOR_SIG
					    +")V");
	        il.append(new NEW(cpg.addClass(CACHED_NODE_LIST_ITERATOR_CLASS)));
	        il.append(DUP_X1);
	        il.append(SWAP);

	        il.append(new INVOKESPECIAL(initCNI));
	    }
	    _select.startIterator(classGen, methodGen);
	}
	// If not, compile result tree from parameter body if present.
	else if (hasContents()) {
	    compileResultTree(classGen, methodGen);
	}
	// If neither are present then store empty string in variable
	else {
	    final ConstantPoolGen cpg = classGen.getConstantPool();
	    final InstructionList il = methodGen.getInstructionList();
	    il.append(new PUSH(cpg, Constants.EMPTYSTRING));
	}
    
public voidunmapRegister(com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator methodGen)
Remove the mapping of this variable to a register. Called when we leave the AST scope of the variable's declaration

	if (_refs.isEmpty() && (_local != null)) {
	    _local.setEnd(methodGen.getInstructionList().getEnd());
	    methodGen.removeLocalVariable(_local);
	    _refs = null;
	    _local = null;
	}