1/*
2 * reserved comment block
3 * DO NOT REMOVE OR ALTER!
4 */
5/*
6 * Licensed to the Apache Software Foundation (ASF) under one or more
7 * contributor license agreements.  See the NOTICE file distributed with
8 * this work for additional information regarding copyright ownership.
9 * The ASF licenses this file to You under the Apache License, Version 2.0
10 * (the "License"); you may not use this file except in compliance with
11 * the License.  You may obtain a copy of the License at
12 *
13 *      http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 */
21
22package com.sun.org.apache.xalan.internal.xsltc.compiler;
23
24import com.sun.org.apache.bcel.internal.generic.CHECKCAST;
25import com.sun.org.apache.bcel.internal.generic.ConstantPoolGen;
26import com.sun.org.apache.bcel.internal.generic.GETFIELD;
27import com.sun.org.apache.bcel.internal.generic.INVOKEINTERFACE;
28import com.sun.org.apache.bcel.internal.generic.InstructionList;
29import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ClassGenerator;
30import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator;
31import com.sun.org.apache.xalan.internal.xsltc.compiler.util.NodeSetType;
32import com.sun.org.apache.xalan.internal.xsltc.runtime.BasisLibrary;
33
34/**
35 * @author Jacek Ambroziak
36 * @author Santiago Pericas-Geertsen
37 * @author Morten Jorgensen
38 * @author Erwin Bolwidt <ejb@klomp.org>
39 */
40final class ParameterRef extends VariableRefBase {
41
42    /**
43     * Name of param being referenced.
44     */
45    QName _name = null;
46
47    public ParameterRef(Param param) {
48        super(param);
49        _name = param._name;
50
51    }
52
53    public String toString() {
54        return "parameter-ref("+_variable.getName()+'/'+_variable.getType()+')';
55    }
56
57    public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
58        final ConstantPoolGen cpg = classGen.getConstantPool();
59        final InstructionList il = methodGen.getInstructionList();
60
61        /*
62         * To fix bug 24518 related to setting parameters of the form
63         * {namespaceuri}localName, which will get mapped to an instance
64         * variable in the class.
65         */
66        final String name = BasisLibrary.mapQNameToJavaName (_name.toString());
67        final String signature = _type.toSignature();
68
69        if (_variable.isLocal()) {
70            if (classGen.isExternal()) {
71                Closure variableClosure = _closure;
72                while (variableClosure != null) {
73                    if (variableClosure.inInnerClass()) break;
74                    variableClosure = variableClosure.getParentClosure();
75                }
76
77                if (variableClosure != null) {
78                    il.append(ALOAD_0);
79                    il.append(new GETFIELD(
80                        cpg.addFieldref(variableClosure.getInnerClassName(),
81                            name, signature)));
82                }
83                else {
84                    il.append(_variable.loadInstruction());
85                }
86            }
87            else {
88                il.append(_variable.loadInstruction());
89            }
90        }
91        else {
92            final String className = classGen.getClassName();
93            il.append(classGen.loadTranslet());
94            if (classGen.isExternal()) {
95                il.append(new CHECKCAST(cpg.addClass(className)));
96            }
97            il.append(new GETFIELD(cpg.addFieldref(className,name,signature)));
98        }
99
100        if (_variable.getType() instanceof NodeSetType) {
101            // The method cloneIterator() also does resetting
102            final int clone = cpg.addInterfaceMethodref(NODE_ITERATOR,
103                                                       "cloneIterator",
104                                                       "()" +
105                                                        NODE_ITERATOR_SIG);
106            il.append(new INVOKEINTERFACE(clone, 1));
107        }
108    }
109}
110