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: CastCall.java,v 1.3 2004/02/16 22:24:29 minchau Exp $
  18. */
  19. package com.sun.org.apache.xalan.internal.xsltc.compiler;
  20. import java.util.Vector;
  21. import com.sun.org.apache.bcel.internal.generic.ConstantPoolGen;
  22. import com.sun.org.apache.bcel.internal.generic.CHECKCAST;
  23. import com.sun.org.apache.bcel.internal.generic.InstructionList;
  24. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ClassGenerator;
  25. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator;
  26. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg;
  27. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type;
  28. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ObjectType;
  29. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.TypeCheckError;
  30. /**
  31. * @author Santiago Pericas-Geertsen
  32. */
  33. final class CastCall extends FunctionCall {
  34. /**
  35. * Name of the class that is the target of the cast. Must be a
  36. * fully-qualified Java class Name.
  37. */
  38. private String _className;
  39. /**
  40. * A reference to the expression being casted.
  41. */
  42. private Expression _right;
  43. /**
  44. * Constructor.
  45. */
  46. public CastCall(QName fname, Vector arguments) {
  47. super(fname, arguments);
  48. }
  49. /**
  50. * Type check the two parameters for this function
  51. */
  52. public Type typeCheck(SymbolTable stable) throws TypeCheckError {
  53. // Check that the function was passed exactly two arguments
  54. if (argumentCount() != 2) {
  55. throw new TypeCheckError(new ErrorMsg(ErrorMsg.ILLEGAL_ARG_ERR,
  56. getName(), this));
  57. }
  58. // The first argument must be a literal String
  59. Expression exp = argument(0);
  60. if (exp instanceof LiteralExpr) {
  61. _className = ((LiteralExpr) exp).getValue();
  62. _type = Type.newObjectType(_className);
  63. }
  64. else {
  65. throw new TypeCheckError(new ErrorMsg(ErrorMsg.NEED_LITERAL_ERR,
  66. getName(), this));
  67. }
  68. // Second argument must be of type reference or object
  69. _right = argument(1);
  70. Type tright = _right.typeCheck(stable);
  71. if (tright != Type.Reference &&
  72. tright instanceof ObjectType == false)
  73. {
  74. throw new TypeCheckError(new ErrorMsg(ErrorMsg.DATA_CONVERSION_ERR,
  75. tright, _type, this));
  76. }
  77. return _type;
  78. }
  79. public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
  80. final ConstantPoolGen cpg = classGen.getConstantPool();
  81. final InstructionList il = methodGen.getInstructionList();
  82. _right.translate(classGen, methodGen);
  83. il.append(new CHECKCAST(cpg.addClass(_className)));
  84. }
  85. }