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: VoidType.java,v 1.7 2004/02/16 22:26:44 minchau Exp $
  18. */
  19. package com.sun.org.apache.xalan.internal.xsltc.compiler.util;
  20. import com.sun.org.apache.bcel.internal.generic.Instruction;
  21. import com.sun.org.apache.bcel.internal.generic.InstructionList;
  22. import com.sun.org.apache.bcel.internal.generic.PUSH;
  23. import com.sun.org.apache.xalan.internal.xsltc.compiler.Constants;
  24. /**
  25. * @author Jacek Ambroziak
  26. * @author Santiago Pericas-Geertsen
  27. */
  28. public final class VoidType extends Type {
  29. protected VoidType() {}
  30. public String toString() {
  31. return "void";
  32. }
  33. public boolean identicalTo(Type other) {
  34. return this == other;
  35. }
  36. public String toSignature() {
  37. return "V";
  38. }
  39. public com.sun.org.apache.bcel.internal.generic.Type toJCType() {
  40. return null; // should never be called
  41. }
  42. public Instruction POP() {
  43. return NOP;
  44. }
  45. /**
  46. * Translates a void into an object of internal type <code>type</code>.
  47. * This translation is needed when calling external functions
  48. * that return void.
  49. *
  50. * @see com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type#translateTo
  51. */
  52. public void translateTo(ClassGenerator classGen, MethodGenerator methodGen,
  53. Type type) {
  54. if (type == Type.String) {
  55. translateTo(classGen, methodGen, (StringType) type);
  56. }
  57. else {
  58. ErrorMsg err = new ErrorMsg(ErrorMsg.DATA_CONVERSION_ERR,
  59. toString(), type.toString());
  60. classGen.getParser().reportError(Constants.FATAL, err);
  61. }
  62. }
  63. /**
  64. * Translates a void into a string by pushing the empty string ''.
  65. *
  66. * @see com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type#translateTo
  67. */
  68. public void translateTo(ClassGenerator classGen, MethodGenerator methodGen,
  69. StringType type) {
  70. final InstructionList il = methodGen.getInstructionList();
  71. il.append(new PUSH(classGen.getConstantPool(), ""));
  72. }
  73. /**
  74. * Translates an external (primitive) Java type into a void.
  75. * Only an external "void" can be converted to this class.
  76. */
  77. public void translateFrom(ClassGenerator classGen, MethodGenerator methodGen,
  78. Class clazz) {
  79. if (!clazz.getName().equals("void")) {
  80. ErrorMsg err = new ErrorMsg(ErrorMsg.DATA_CONVERSION_ERR,
  81. toString(), clazz.getName());
  82. classGen.getParser().reportError(Constants.FATAL, err);
  83. }
  84. }
  85. }