286N/A/*
286N/A * reserved comment block
286N/A * DO NOT REMOVE OR ALTER!
286N/A */
286N/A/*
286N/A * Copyright 2001-2004 The Apache Software Foundation.
286N/A *
286N/A * Licensed under the Apache License, Version 2.0 (the "License");
286N/A * you may not use this file except in compliance with the License.
286N/A * You may obtain a copy of the License at
286N/A *
286N/A * http://www.apache.org/licenses/LICENSE-2.0
286N/A *
286N/A * Unless required by applicable law or agreed to in writing, software
286N/A * distributed under the License is distributed on an "AS IS" BASIS,
286N/A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
286N/A * See the License for the specific language governing permissions and
286N/A * limitations under the License.
286N/A */
286N/A/*
286N/A * $Id: Param.java,v 1.2.4.1 2005/09/02 11:03:42 pvedula Exp $
286N/A */
286N/A
286N/Apackage com.sun.org.apache.xalan.internal.xsltc.compiler;
286N/A
286N/Aimport com.sun.org.apache.bcel.internal.classfile.Field;
286N/Aimport com.sun.org.apache.bcel.internal.generic.BranchHandle;
286N/Aimport com.sun.org.apache.bcel.internal.generic.CHECKCAST;
286N/Aimport com.sun.org.apache.bcel.internal.generic.IFNONNULL;
286N/Aimport com.sun.org.apache.bcel.internal.generic.ConstantPoolGen;
286N/Aimport com.sun.org.apache.bcel.internal.generic.INVOKEVIRTUAL;
286N/Aimport com.sun.org.apache.bcel.internal.generic.Instruction;
286N/Aimport com.sun.org.apache.bcel.internal.generic.InstructionList;
286N/Aimport com.sun.org.apache.bcel.internal.generic.PUSH;
286N/Aimport com.sun.org.apache.bcel.internal.generic.PUTFIELD;
286N/Aimport com.sun.org.apache.xalan.internal.xsltc.compiler.util.ClassGenerator;
286N/Aimport com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg;
286N/Aimport com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator;
286N/Aimport com.sun.org.apache.xalan.internal.xsltc.compiler.util.ReferenceType;
286N/Aimport com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type;
286N/Aimport com.sun.org.apache.xalan.internal.xsltc.compiler.util.ObjectType;
286N/Aimport com.sun.org.apache.xalan.internal.xsltc.compiler.util.TypeCheckError;
286N/Aimport com.sun.org.apache.xalan.internal.xsltc.runtime.BasisLibrary;
286N/A
286N/A/**
286N/A * @author Jacek Ambroziak
286N/A * @author Santiago Pericas-Geertsen
286N/A * @author Morten Jorgensen
286N/A * @author Erwin Bolwidt <ejb@klomp.org>
286N/A * @author John Howard <JohnH@schemasoft.com>
286N/A */
286N/Afinal class Param extends VariableBase {
286N/A
286N/A /**
286N/A * True if this Param is declared in a simple named template.
286N/A * This is used to optimize codegen for parameter passing
286N/A * in named templates.
286N/A */
286N/A private boolean _isInSimpleNamedTemplate = false;
286N/A
286N/A /**
286N/A * Display variable as single string
286N/A */
286N/A public String toString() {
286N/A return "param(" + _name + ")";
286N/A }
286N/A
286N/A /**
286N/A * Set the instruction for loading the value of this variable onto the
286N/A * JVM stack and returns the old instruction.
286N/A */
286N/A public Instruction setLoadInstruction(Instruction instruction) {
286N/A Instruction tmp = _loadInstruction;
286N/A _loadInstruction = instruction;
286N/A return tmp;
286N/A }
286N/A
286N/A /**
286N/A * Set the instruction for storing a value from the stack into this
286N/A * variable and returns the old instruction.
286N/A */
286N/A public Instruction setStoreInstruction(Instruction instruction) {
286N/A Instruction tmp = _storeInstruction;
286N/A _storeInstruction = instruction;
286N/A return tmp;
286N/A }
286N/A
286N/A /**
286N/A * Display variable in a full AST dump
286N/A */
286N/A public void display(int indent) {
286N/A indent(indent);
286N/A System.out.println("param " + _name);
286N/A if (_select != null) {
286N/A indent(indent + IndentIncrement);
286N/A System.out.println("select " + _select.toString());
286N/A }
286N/A displayContents(indent + IndentIncrement);
286N/A }
286N/A
286N/A /**
286N/A * Parse the contents of the <xsl:param> element. This method must read
286N/A * the 'name' (required) and 'select' (optional) attributes.
286N/A */
286N/A public void parseContents(Parser parser) {
286N/A
286N/A // Parse 'name' and 'select' attributes plus parameter contents
286N/A super.parseContents(parser);
286N/A
286N/A // Add a ref to this param to its enclosing construct
286N/A final SyntaxTreeNode parent = getParent();
286N/A if (parent instanceof Stylesheet) {
286N/A // Mark this as a global parameter
286N/A _isLocal = false;
286N/A // Check if a global variable with this name already exists...
286N/A Param param = parser.getSymbolTable().lookupParam(_name);
286N/A // ...and if it does we need to check import precedence
286N/A if (param != null) {
286N/A final int us = this.getImportPrecedence();
286N/A final int them = param.getImportPrecedence();
286N/A // It is an error if the two have the same import precedence
286N/A if (us == them) {
286N/A final String name = _name.toString();
286N/A reportError(this, parser, ErrorMsg.VARIABLE_REDEF_ERR,name);
286N/A }
286N/A // Ignore this if previous definition has higher precedence
286N/A else if (them > us) {
286N/A _ignore = true;
286N/A copyReferences(param);
286N/A return;
286N/A }
286N/A else {
286N/A param.copyReferences(this);
286N/A param.disable();
286N/A }
286N/A }
286N/A // Add this variable if we have higher precedence
286N/A ((Stylesheet)parent).addParam(this);
286N/A parser.getSymbolTable().addParam(this);
286N/A }
286N/A else if (parent instanceof Template) {
286N/A Template template = (Template) parent;
286N/A _isLocal = true;
286N/A template.addParameter(this);
286N/A if (template.isSimpleNamedTemplate()) {
286N/A _isInSimpleNamedTemplate = true;
286N/A }
286N/A }
286N/A }
286N/A
286N/A /**
286N/A * Type-checks the parameter. The parameter type is determined by the
286N/A * 'select' expression (if present) or is a result tree if the parameter
286N/A * element has a body and no 'select' expression.
286N/A */
286N/A public Type typeCheck(SymbolTable stable) throws TypeCheckError {
286N/A if (_select != null) {
286N/A _type = _select.typeCheck(stable);
286N/A if (_type instanceof ReferenceType == false && !(_type instanceof ObjectType)) {
286N/A _select = new CastExpr(_select, Type.Reference);
286N/A }
286N/A }
286N/A else if (hasContents()) {
286N/A typeCheckContents(stable);
286N/A }
286N/A _type = Type.Reference;
286N/A
286N/A // This element has no type (the parameter does, but the parameter
286N/A // element itself does not).
286N/A return Type.Void;
286N/A }
286N/A
286N/A public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
286N/A final ConstantPoolGen cpg = classGen.getConstantPool();
286N/A final InstructionList il = methodGen.getInstructionList();
286N/A
286N/A if (_ignore) return;
286N/A _ignore = true;
286N/A
286N/A /*
286N/A * To fix bug 24518 related to setting parameters of the form
286N/A * {namespaceuri}localName which will get mapped to an instance
286N/A * variable in the class.
286N/A */
286N/A final String name = BasisLibrary.mapQNameToJavaName(_name.toString());
286N/A final String signature = _type.toSignature();
286N/A final String className = _type.getClassName();
286N/A
286N/A if (isLocal()) {
286N/A /*
286N/A * If simple named template then generate a conditional init of the
286N/A * param using its default value:
286N/A * if (param == null) param = <default-value>
286N/A */
286N/A if (_isInSimpleNamedTemplate) {
286N/A il.append(loadInstruction());
286N/A BranchHandle ifBlock = il.append(new IFNONNULL(null));
286N/A translateValue(classGen, methodGen);
286N/A il.append(storeInstruction());
286N/A ifBlock.setTarget(il.append(NOP));
286N/A return;
286N/A }
286N/A
286N/A il.append(classGen.loadTranslet());
286N/A il.append(new PUSH(cpg, name));
286N/A translateValue(classGen, methodGen);
286N/A il.append(new PUSH(cpg, true));
286N/A
286N/A // Call addParameter() from this class
286N/A il.append(new INVOKEVIRTUAL(cpg.addMethodref(TRANSLET_CLASS,
286N/A ADD_PARAMETER,
286N/A ADD_PARAMETER_SIG)));
286N/A if (className != EMPTYSTRING) {
286N/A il.append(new CHECKCAST(cpg.addClass(className)));
286N/A }
286N/A
286N/A _type.translateUnBox(classGen, methodGen);
286N/A
286N/A if (_refs.isEmpty()) { // nobody uses the value
286N/A il.append(_type.POP());
286N/A _local = null;
286N/A }
286N/A else { // normal case
286N/A _local = methodGen.addLocalVariable2(name,
286N/A _type.toJCType(),
286N/A il.getEnd());
286N/A // Cache the result of addParameter() in a local variable
286N/A il.append(_type.STORE(_local.getIndex()));
286N/A }
286N/A }
286N/A else {
286N/A if (classGen.containsField(name) == null) {
286N/A classGen.addField(new Field(ACC_PUBLIC, cpg.addUtf8(name),
286N/A cpg.addUtf8(signature),
286N/A null, cpg.getConstantPool()));
286N/A il.append(classGen.loadTranslet());
286N/A il.append(DUP);
286N/A il.append(new PUSH(cpg, name));
286N/A translateValue(classGen, methodGen);
286N/A il.append(new PUSH(cpg, true));
286N/A
286N/A // Call addParameter() from this class
286N/A il.append(new INVOKEVIRTUAL(cpg.addMethodref(TRANSLET_CLASS,
286N/A ADD_PARAMETER,
286N/A ADD_PARAMETER_SIG)));
286N/A
286N/A _type.translateUnBox(classGen, methodGen);
286N/A
286N/A // Cache the result of addParameter() in a field
286N/A if (className != EMPTYSTRING) {
286N/A il.append(new CHECKCAST(cpg.addClass(className)));
286N/A }
286N/A il.append(new PUTFIELD(cpg.addFieldref(classGen.getClassName(),
286N/A name, signature)));
286N/A }
286N/A }
286N/A }
286N/A}