View Javadoc
1   /*
2    * reserved comment block
3    * DO NOT REMOVE OR ALTER!
4    */
5   /*
6    * Copyright 2001-2004 The Apache Software Foundation.
7    *
8    * Licensed under the Apache License, Version 2.0 (the "License");
9    * you may not use this file except in compliance with the License.
10   * You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  /*
21   * $Id: ConcatCall.java,v 1.2.4.1 2005/09/01 12:07:31 pvedula Exp $
22   */
23  
24  package com.sun.org.apache.xalan.internal.xsltc.compiler;
25  
26  import java.util.Vector;
27  
28  import com.sun.org.apache.bcel.internal.generic.ConstantPoolGen;
29  import com.sun.org.apache.bcel.internal.generic.INVOKESPECIAL;
30  import com.sun.org.apache.bcel.internal.generic.INVOKEVIRTUAL;
31  import com.sun.org.apache.bcel.internal.generic.Instruction;
32  import com.sun.org.apache.bcel.internal.generic.InstructionList;
33  import com.sun.org.apache.bcel.internal.generic.NEW;
34  import com.sun.org.apache.bcel.internal.generic.PUSH;
35  import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ClassGenerator;
36  import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator;
37  import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type;
38  import com.sun.org.apache.xalan.internal.xsltc.compiler.util.TypeCheckError;
39  
40  /**
41   * @author Jacek Ambroziak
42   * @author Santiago Pericas-Geertsen
43   */
44  final class ConcatCall extends FunctionCall {
45      public ConcatCall(QName fname, Vector arguments) {
46          super(fname, arguments);
47      }
48  
49      public Type typeCheck(SymbolTable stable) throws TypeCheckError {
50          for (int i = 0; i < argumentCount(); i++) {
51              final Expression exp = argument(i);
52              if (!exp.typeCheck(stable).identicalTo(Type.String)) {
53                  setArgument(i, new CastExpr(exp, Type.String));
54              }
55          }
56          return _type = Type.String;
57      }
58  
59      /** translate leaves a String on the stack */
60      public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
61          final ConstantPoolGen cpg = classGen.getConstantPool();
62          final InstructionList il = methodGen.getInstructionList();
63          final int nArgs = argumentCount();
64  
65          switch (nArgs) {
66          case 0:
67              il.append(new PUSH(cpg, EMPTYSTRING));
68              break;
69  
70          case 1:
71              argument().translate(classGen, methodGen);
72              break;
73  
74          default:
75              final int initBuffer = cpg.addMethodref(STRING_BUFFER_CLASS,
76                                                      "<init>", "()V");
77              final Instruction append =
78                  new INVOKEVIRTUAL(cpg.addMethodref(STRING_BUFFER_CLASS,
79                                                     "append",
80                                                     "("+STRING_SIG+")"
81                                                     +STRING_BUFFER_SIG));
82  
83              final int toString = cpg.addMethodref(STRING_BUFFER_CLASS,
84                                                    "toString",
85                                                    "()"+STRING_SIG);
86  
87              il.append(new NEW(cpg.addClass(STRING_BUFFER_CLASS)));
88              il.append(DUP);
89              il.append(new INVOKESPECIAL(initBuffer));
90              for (int i = 0; i < nArgs; i++) {
91                  argument(i).translate(classGen, methodGen);
92                  il.append(append);
93              }
94              il.append(new INVOKEVIRTUAL(toString));
95          }
96      }
97  }