1. /*
  2. * Copyright 2001-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /*
  17. * $Id: Param.java,v 1.28 2004/02/23 17:29:35 aruny Exp $
  18. */
  19. package com.sun.org.apache.xalan.internal.xsltc.compiler;
  20. import com.sun.org.apache.bcel.internal.classfile.Field;
  21. import com.sun.org.apache.bcel.internal.generic.BranchHandle;
  22. import com.sun.org.apache.bcel.internal.generic.CHECKCAST;
  23. import com.sun.org.apache.bcel.internal.generic.IFNONNULL;
  24. import com.sun.org.apache.bcel.internal.generic.ConstantPoolGen;
  25. import com.sun.org.apache.bcel.internal.generic.INVOKEVIRTUAL;
  26. import com.sun.org.apache.bcel.internal.generic.Instruction;
  27. import com.sun.org.apache.bcel.internal.generic.InstructionList;
  28. import com.sun.org.apache.bcel.internal.generic.PUSH;
  29. import com.sun.org.apache.bcel.internal.generic.PUTFIELD;
  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.ReferenceType;
  34. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type;
  35. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ObjectType;
  36. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.TypeCheckError;
  37. import com.sun.org.apache.xalan.internal.xsltc.runtime.BasisLibrary;
  38. /**
  39. * @author Jacek Ambroziak
  40. * @author Santiago Pericas-Geertsen
  41. * @author Morten Jorgensen
  42. * @author Erwin Bolwidt <ejb@klomp.org>
  43. * @author John Howard <JohnH@schemasoft.com>
  44. */
  45. final class Param extends VariableBase {
  46. /**
  47. * True if this Param is declared in a simple named template.
  48. * This is used to optimize codegen for parameter passing
  49. * in named templates.
  50. */
  51. private boolean _isInSimpleNamedTemplate = false;
  52. /**
  53. * Display variable as single string
  54. */
  55. public String toString() {
  56. return "param(" + _name + ")";
  57. }
  58. /**
  59. * Set the instruction for loading the value of this variable onto the
  60. * JVM stack and returns the old instruction.
  61. */
  62. public Instruction setLoadInstruction(Instruction instruction) {
  63. Instruction tmp = _loadInstruction;
  64. _loadInstruction = instruction;
  65. return tmp;
  66. }
  67. /**
  68. * Set the instruction for storing a value from the stack into this
  69. * variable and returns the old instruction.
  70. */
  71. public Instruction setStoreInstruction(Instruction instruction) {
  72. Instruction tmp = _storeInstruction;
  73. _storeInstruction = instruction;
  74. return tmp;
  75. }
  76. /**
  77. * Display variable in a full AST dump
  78. */
  79. public void display(int indent) {
  80. indent(indent);
  81. System.out.println("param " + _name);
  82. if (_select != null) {
  83. indent(indent + IndentIncrement);
  84. System.out.println("select " + _select.toString());
  85. }
  86. displayContents(indent + IndentIncrement);
  87. }
  88. /**
  89. * Parse the contents of the <xsl:param> element. This method must read
  90. * the 'name' (required) and 'select' (optional) attributes.
  91. */
  92. public void parseContents(Parser parser) {
  93. // Parse 'name' and 'select' attributes plus parameter contents
  94. super.parseContents(parser);
  95. // Add a ref to this param to its enclosing construct
  96. final SyntaxTreeNode parent = getParent();
  97. if (parent instanceof Stylesheet) {
  98. // Mark this as a global parameter
  99. _isLocal = false;
  100. // Check if a global variable with this name already exists...
  101. Param param = parser.getSymbolTable().lookupParam(_name);
  102. // ...and if it does we need to check import precedence
  103. if (param != null) {
  104. final int us = this.getImportPrecedence();
  105. final int them = param.getImportPrecedence();
  106. // It is an error if the two have the same import precedence
  107. if (us == them) {
  108. final String name = _name.toString();
  109. reportError(this, parser, ErrorMsg.VARIABLE_REDEF_ERR,name);
  110. }
  111. // Ignore this if previous definition has higher precedence
  112. else if (them > us) {
  113. _ignore = true;
  114. return;
  115. }
  116. else {
  117. param.disable();
  118. }
  119. }
  120. // Add this variable if we have higher precedence
  121. ((Stylesheet)parent).addParam(this);
  122. parser.getSymbolTable().addParam(this);
  123. }
  124. else if (parent instanceof Template) {
  125. Template template = (Template) parent;
  126. _isLocal = true;
  127. template.addParameter(this);
  128. if (template.isSimpleNamedTemplate()) {
  129. _isInSimpleNamedTemplate = true;
  130. }
  131. }
  132. }
  133. /**
  134. * Type-checks the parameter. The parameter type is determined by the
  135. * 'select' expression (if present) or is a result tree if the parameter
  136. * element has a body and no 'select' expression.
  137. */
  138. public Type typeCheck(SymbolTable stable) throws TypeCheckError {
  139. if (_select != null) {
  140. _type = _select.typeCheck(stable);
  141. if (_type instanceof ReferenceType == false && !(_type instanceof ObjectType)) {
  142. _select = new CastExpr(_select, Type.Reference);
  143. }
  144. }
  145. else if (hasContents()) {
  146. typeCheckContents(stable);
  147. }
  148. _type = Type.Reference;
  149. // This element has no type (the parameter does, but the parameter
  150. // element itself does not).
  151. return Type.Void;
  152. }
  153. public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
  154. final ConstantPoolGen cpg = classGen.getConstantPool();
  155. final InstructionList il = methodGen.getInstructionList();
  156. if (_ignore) return;
  157. _ignore = true;
  158. /*
  159. * To fix bug 24518 related to setting parameters of the form
  160. * {namespaceuri}localName which will get mapped to an instance
  161. * variable in the class.
  162. */
  163. final String name = BasisLibrary.mapQNameToJavaName(_name.toString());
  164. final String signature = _type.toSignature();
  165. final String className = _type.getClassName();
  166. if (isLocal()) {
  167. /*
  168. * If simple named template then generate a conditional init of the
  169. * param using its default value:
  170. * if (param == null) param = <default-value>
  171. */
  172. if (_isInSimpleNamedTemplate) {
  173. il.append(loadInstruction());
  174. BranchHandle ifBlock = il.append(new IFNONNULL(null));
  175. translateValue(classGen, methodGen);
  176. il.append(storeInstruction());
  177. ifBlock.setTarget(il.append(NOP));
  178. return;
  179. }
  180. il.append(classGen.loadTranslet());
  181. il.append(new PUSH(cpg, name));
  182. translateValue(classGen, methodGen);
  183. il.append(new PUSH(cpg, true));
  184. // Call addParameter() from this class
  185. il.append(new INVOKEVIRTUAL(cpg.addMethodref(TRANSLET_CLASS,
  186. ADD_PARAMETER,
  187. ADD_PARAMETER_SIG)));
  188. if (className != EMPTYSTRING) {
  189. il.append(new CHECKCAST(cpg.addClass(className)));
  190. }
  191. _type.translateUnBox(classGen, methodGen);
  192. if (_refs.isEmpty()) { // nobody uses the value
  193. il.append(_type.POP());
  194. _local = null;
  195. }
  196. else { // normal case
  197. _local = methodGen.addLocalVariable2(name,
  198. _type.toJCType(),
  199. il.getEnd());
  200. // Cache the result of addParameter() in a local variable
  201. il.append(_type.STORE(_local.getIndex()));
  202. }
  203. }
  204. else {
  205. if (classGen.containsField(name) == null) {
  206. classGen.addField(new Field(ACC_PUBLIC, cpg.addUtf8(name),
  207. cpg.addUtf8(signature),
  208. null, cpg.getConstantPool()));
  209. il.append(classGen.loadTranslet());
  210. il.append(DUP);
  211. il.append(new PUSH(cpg, name));
  212. translateValue(classGen, methodGen);
  213. il.append(new PUSH(cpg, true));
  214. // Call addParameter() from this class
  215. il.append(new INVOKEVIRTUAL(cpg.addMethodref(TRANSLET_CLASS,
  216. ADD_PARAMETER,
  217. ADD_PARAMETER_SIG)));
  218. _type.translateUnBox(classGen, methodGen);
  219. // Cache the result of addParameter() in a field
  220. if (className != EMPTYSTRING) {
  221. il.append(new CHECKCAST(cpg.addClass(className)));
  222. }
  223. il.append(new PUTFIELD(cpg.addFieldref(classGen.getClassName(),
  224. name, signature)));
  225. }
  226. }
  227. }
  228. }