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: Variable.java,v 1.27 2004/02/24 02:58:42 zongaro 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.ACONST_NULL;
  22. import com.sun.org.apache.bcel.internal.generic.ConstantPoolGen;
  23. import com.sun.org.apache.bcel.internal.generic.DCONST;
  24. import com.sun.org.apache.bcel.internal.generic.ICONST;
  25. import com.sun.org.apache.bcel.internal.generic.InstructionList;
  26. import com.sun.org.apache.bcel.internal.generic.PUTFIELD;
  27. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.BooleanType;
  28. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ClassGenerator;
  29. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg;
  30. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.IntType;
  31. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator;
  32. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.NodeType;
  33. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.RealType;
  34. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type;
  35. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.TypeCheckError;
  36. /**
  37. * @author Jacek Ambroziak
  38. * @author Santiago Pericas-Geertsen
  39. * @author Morten Jorgensen
  40. * @author Erwin Bolwidt <ejb@klomp.org>
  41. * @author John Howard <JohnH@schemasoft.com>
  42. */
  43. final class Variable extends VariableBase {
  44. public int getIndex() {
  45. return (_local != null) ? _local.getIndex() : -1;
  46. }
  47. /**
  48. * Parse the contents of the variable
  49. */
  50. public void parseContents(Parser parser) {
  51. // Parse 'name' and 'select' attributes plus parameter contents
  52. super.parseContents(parser);
  53. // Add a ref to this var to its enclosing construct
  54. SyntaxTreeNode parent = getParent();
  55. if (parent instanceof Stylesheet) {
  56. // Mark this as a global variable
  57. _isLocal = false;
  58. // Check if a global variable with this name already exists...
  59. Variable var = parser.getSymbolTable().lookupVariable(_name);
  60. // ...and if it does we need to check import precedence
  61. if (var != null) {
  62. final int us = this.getImportPrecedence();
  63. final int them = var.getImportPrecedence();
  64. // It is an error if the two have the same import precedence
  65. if (us == them) {
  66. final String name = _name.toString();
  67. reportError(this, parser, ErrorMsg.VARIABLE_REDEF_ERR,name);
  68. }
  69. // Ignore this if previous definition has higher precedence
  70. else if (them > us) {
  71. _ignore = true;
  72. return;
  73. }
  74. else {
  75. var.disable();
  76. }
  77. // Add this variable if we have higher precedence
  78. }
  79. ((Stylesheet)parent).addVariable(this);
  80. parser.getSymbolTable().addVariable(this);
  81. }
  82. else {
  83. _isLocal = true;
  84. }
  85. }
  86. /**
  87. * Runs a type check on either the variable element body or the
  88. * expression in the 'select' attribute
  89. */
  90. public Type typeCheck(SymbolTable stable) throws TypeCheckError {
  91. // Type check the 'select' expression if present
  92. if (_select != null) {
  93. _type = _select.typeCheck(stable);
  94. }
  95. // Type check the element contents otherwise
  96. else if (hasContents()) {
  97. typeCheckContents(stable);
  98. _type = Type.ResultTree;
  99. }
  100. else {
  101. _type = Type.Reference;
  102. }
  103. // The return type is void as the variable element does not leave
  104. // anything on the JVM's stack. The '_type' global will be returned
  105. // by the references to this variable, and not by the variable itself.
  106. return Type.Void;
  107. }
  108. /**
  109. * This method is part of a little trick that is needed to use local
  110. * variables inside nested for-each loops. See the initializeVariables()
  111. * method in the ForEach class for an explanation
  112. */
  113. public void initialize(ClassGenerator classGen, MethodGenerator methodGen) {
  114. final ConstantPoolGen cpg = classGen.getConstantPool();
  115. final InstructionList il = methodGen.getInstructionList();
  116. // This is only done for local variables that are actually used
  117. if (isLocal() && !_refs.isEmpty()) {
  118. // Create a variable slot if none is allocated
  119. if (_local == null) {
  120. _local = methodGen.addLocalVariable2(getEscapedName(),
  121. _type.toJCType(),
  122. il.getEnd());
  123. }
  124. // Push the default value on the JVM's stack
  125. if ((_type instanceof IntType) ||
  126. (_type instanceof NodeType) ||
  127. (_type instanceof BooleanType))
  128. il.append(new ICONST(0)); // 0 for node-id, integer and boolean
  129. else if (_type instanceof RealType)
  130. il.append(new DCONST(0)); // 0.0 for floating point numbers
  131. else
  132. il.append(new ACONST_NULL()); // and 'null' for anything else
  133. il.append(_type.STORE(_local.getIndex()));
  134. }
  135. }
  136. public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
  137. final ConstantPoolGen cpg = classGen.getConstantPool();
  138. final InstructionList il = methodGen.getInstructionList();
  139. final String name = getEscapedName();
  140. // Make sure that a variable instance is only compiled once
  141. if (_ignore) return;
  142. _ignore = true;
  143. if (isLocal()) {
  144. // Compile variable value computation
  145. translateValue(classGen, methodGen);
  146. // Add a new local variable and store value
  147. if (_refs.isEmpty()) { // Remove it if nobody uses the value
  148. il.append(_type.POP());
  149. _local = null;
  150. }
  151. else { // Store in local var slot if referenced
  152. if (_local == null) mapRegister(methodGen);
  153. il.append(_type.STORE(_local.getIndex()));
  154. }
  155. }
  156. else {
  157. String signature = _type.toSignature();
  158. // Global variables are store in class fields
  159. if (classGen.containsField(name) == null) {
  160. classGen.addField(new Field(ACC_PUBLIC,
  161. cpg.addUtf8(name),
  162. cpg.addUtf8(signature),
  163. null, cpg.getConstantPool()));
  164. // Push a reference to "this" for putfield
  165. il.append(classGen.loadTranslet());
  166. // Compile variable value computation
  167. translateValue(classGen, methodGen);
  168. // Store the variable in the allocated field
  169. il.append(new PUTFIELD(cpg.addFieldref(classGen.getClassName(),
  170. name, signature)));
  171. }
  172. }
  173. }
  174. }