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: ConcatCall.java,v 1.7 2004/02/16 22:24:28 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.INVOKESPECIAL;
  23. import com.sun.org.apache.bcel.internal.generic.INVOKEVIRTUAL;
  24. import com.sun.org.apache.bcel.internal.generic.Instruction;
  25. import com.sun.org.apache.bcel.internal.generic.InstructionList;
  26. import com.sun.org.apache.bcel.internal.generic.NEW;
  27. import com.sun.org.apache.bcel.internal.generic.PUSH;
  28. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ClassGenerator;
  29. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator;
  30. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type;
  31. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.TypeCheckError;
  32. /**
  33. * @author Jacek Ambroziak
  34. * @author Santiago Pericas-Geertsen
  35. */
  36. final class ConcatCall extends FunctionCall {
  37. public ConcatCall(QName fname, Vector arguments) {
  38. super(fname, arguments);
  39. }
  40. public Type typeCheck(SymbolTable stable) throws TypeCheckError {
  41. for (int i = 0; i < argumentCount(); i++) {
  42. final Expression exp = argument(i);
  43. if (!exp.typeCheck(stable).identicalTo(Type.String)) {
  44. setArgument(i, new CastExpr(exp, Type.String));
  45. }
  46. }
  47. return _type = Type.String;
  48. }
  49. /** translate leaves a String on the stack */
  50. public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
  51. final ConstantPoolGen cpg = classGen.getConstantPool();
  52. final InstructionList il = methodGen.getInstructionList();
  53. final int nArgs = argumentCount();
  54. switch (nArgs) {
  55. case 0:
  56. il.append(new PUSH(cpg, EMPTYSTRING));
  57. break;
  58. case 1:
  59. argument().translate(classGen, methodGen);
  60. break;
  61. default:
  62. final int initBuffer = cpg.addMethodref(STRING_BUFFER_CLASS,
  63. "<init>", "()V");
  64. final Instruction append =
  65. new INVOKEVIRTUAL(cpg.addMethodref(STRING_BUFFER_CLASS,
  66. "append",
  67. "("+STRING_SIG+")"
  68. +STRING_BUFFER_SIG));
  69. final int toString = cpg.addMethodref(STRING_BUFFER_CLASS,
  70. "toString",
  71. "()"+STRING_SIG);
  72. il.append(new NEW(cpg.addClass(STRING_BUFFER_CLASS)));
  73. il.append(DUP);
  74. il.append(new INVOKESPECIAL(initBuffer));
  75. for (int i = 0; i < nArgs; i++) {
  76. argument(i).translate(classGen, methodGen);
  77. il.append(append);
  78. }
  79. il.append(new INVOKEVIRTUAL(toString));
  80. }
  81. }
  82. }