KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xalan > internal > xsltc > compiler > Param


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: Param.java,v 1.28 2004/02/23 17:29:35 aruny Exp $
18  */

19
20 package com.sun.org.apache.xalan.internal.xsltc.compiler;
21
22 import com.sun.org.apache.bcel.internal.classfile.Field;
23 import com.sun.org.apache.bcel.internal.generic.BranchHandle;
24 import com.sun.org.apache.bcel.internal.generic.CHECKCAST;
25 import com.sun.org.apache.bcel.internal.generic.IFNONNULL;
26 import com.sun.org.apache.bcel.internal.generic.ConstantPoolGen;
27 import com.sun.org.apache.bcel.internal.generic.INVOKEVIRTUAL;
28 import com.sun.org.apache.bcel.internal.generic.Instruction;
29 import com.sun.org.apache.bcel.internal.generic.InstructionList;
30 import com.sun.org.apache.bcel.internal.generic.PUSH;
31 import com.sun.org.apache.bcel.internal.generic.PUTFIELD;
32 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ClassGenerator;
33 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg;
34 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator;
35 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ReferenceType;
36 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type;
37 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ObjectType;
38 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.TypeCheckError;
39 import com.sun.org.apache.xalan.internal.xsltc.runtime.BasisLibrary;
40
41 /**
42  * @author Jacek Ambroziak
43  * @author Santiago Pericas-Geertsen
44  * @author Morten Jorgensen
45  * @author Erwin Bolwidt <ejb@klomp.org>
46  * @author John Howard <JohnH@schemasoft.com>
47  */

48 final class Param extends VariableBase {
49
50     /**
51      * True if this Param is declared in a simple named template.
52      * This is used to optimize codegen for parameter passing
53      * in named templates.
54      */

55     private boolean _isInSimpleNamedTemplate = false;
56
57     /**
58      * Display variable as single string
59      */

60     public String JavaDoc toString() {
61     return "param(" + _name + ")";
62     }
63
64     /**
65      * Set the instruction for loading the value of this variable onto the
66      * JVM stack and returns the old instruction.
67      */

68     public Instruction setLoadInstruction(Instruction instruction) {
69         Instruction tmp = _loadInstruction;
70         _loadInstruction = instruction;
71         return tmp;
72     }
73
74     /**
75      * Set the instruction for storing a value from the stack into this
76      * variable and returns the old instruction.
77      */

78     public Instruction setStoreInstruction(Instruction instruction) {
79         Instruction tmp = _storeInstruction;
80         _storeInstruction = instruction;
81         return tmp;
82     }
83
84     /**
85      * Display variable in a full AST dump
86      */

87     public void display(int indent) {
88     indent(indent);
89     System.out.println("param " + _name);
90     if (_select != null) {
91         indent(indent + IndentIncrement);
92         System.out.println("select " + _select.toString());
93     }
94     displayContents(indent + IndentIncrement);
95     }
96
97     /**
98      * Parse the contents of the <xsl:param> element. This method must read
99      * the 'name' (required) and 'select' (optional) attributes.
100      */

101     public void parseContents(Parser parser) {
102
103     // Parse 'name' and 'select' attributes plus parameter contents
104
super.parseContents(parser);
105
106     // Add a ref to this param to its enclosing construct
107
final SyntaxTreeNode parent = getParent();
108     if (parent instanceof Stylesheet) {
109         // Mark this as a global parameter
110
_isLocal = false;
111         // Check if a global variable with this name already exists...
112
Param param = parser.getSymbolTable().lookupParam(_name);
113         // ...and if it does we need to check import precedence
114
if (param != null) {
115         final int us = this.getImportPrecedence();
116         final int them = param.getImportPrecedence();
117         // It is an error if the two have the same import precedence
118
if (us == them) {
119             final String JavaDoc name = _name.toString();
120             reportError(this, parser, ErrorMsg.VARIABLE_REDEF_ERR,name);
121         }
122         // Ignore this if previous definition has higher precedence
123
else if (them > us) {
124             _ignore = true;
125             return;
126         }
127         else {
128             param.disable();
129         }
130         }
131         // Add this variable if we have higher precedence
132
((Stylesheet)parent).addParam(this);
133         parser.getSymbolTable().addParam(this);
134     }
135     else if (parent instanceof Template) {
136             Template template = (Template) parent;
137         _isLocal = true;
138             template.addParameter(this);
139             if (template.isSimpleNamedTemplate()) {
140                 _isInSimpleNamedTemplate = true;
141             }
142     }
143     }
144
145     /**
146      * Type-checks the parameter. The parameter type is determined by the
147      * 'select' expression (if present) or is a result tree if the parameter
148      * element has a body and no 'select' expression.
149      */

150     public Type typeCheck(SymbolTable stable) throws TypeCheckError {
151     if (_select != null) {
152         _type = _select.typeCheck(stable);
153         if (_type instanceof ReferenceType == false && !(_type instanceof ObjectType)) {
154         _select = new CastExpr(_select, Type.Reference);
155         }
156     }
157     else if (hasContents()) {
158         typeCheckContents(stable);
159     }
160     _type = Type.Reference;
161
162     // This element has no type (the parameter does, but the parameter
163
// element itself does not).
164
return Type.Void;
165     }
166
167     public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
168     final ConstantPoolGen cpg = classGen.getConstantPool();
169     final InstructionList il = methodGen.getInstructionList();
170
171     if (_ignore) return;
172     _ignore = true;
173
174         /*
175          * To fix bug 24518 related to setting parameters of the form
176          * {namespaceuri}localName which will get mapped to an instance
177          * variable in the class.
178          */

179     final String JavaDoc name = BasisLibrary.mapQNameToJavaName(_name.toString());
180     final String JavaDoc signature = _type.toSignature();
181     final String JavaDoc className = _type.getClassName();
182
183     if (isLocal()) {
184             /*
185               * If simple named template then generate a conditional init of the
186               * param using its default value:
187               * if (param == null) param = <default-value>
188               */

189             if (_isInSimpleNamedTemplate) {
190         il.append(loadInstruction());
191                 BranchHandle ifBlock = il.append(new IFNONNULL(null));
192                 translateValue(classGen, methodGen);
193                 il.append(storeInstruction());
194                 ifBlock.setTarget(il.append(NOP));
195                 return;
196             }
197             
198         il.append(classGen.loadTranslet());
199         il.append(new PUSH(cpg, name));
200         translateValue(classGen, methodGen);
201         il.append(new PUSH(cpg, true));
202
203         // Call addParameter() from this class
204
il.append(new INVOKEVIRTUAL(cpg.addMethodref(TRANSLET_CLASS,
205                              ADD_PARAMETER,
206                              ADD_PARAMETER_SIG)));
207         if (className != EMPTYSTRING) {
208         il.append(new CHECKCAST(cpg.addClass(className)));
209         }
210
211         _type.translateUnBox(classGen, methodGen);
212
213         if (_refs.isEmpty()) { // nobody uses the value
214
il.append(_type.POP());
215         _local = null;
216         }
217         else { // normal case
218
_local = methodGen.addLocalVariable2(name,
219                              _type.toJCType(),
220                              il.getEnd());
221         // Cache the result of addParameter() in a local variable
222
il.append(_type.STORE(_local.getIndex()));
223         }
224     }
225     else {
226         if (classGen.containsField(name) == null) {
227         classGen.addField(new Field(ACC_PUBLIC, cpg.addUtf8(name),
228                         cpg.addUtf8(signature),
229                         null, cpg.getConstantPool()));
230         il.append(classGen.loadTranslet());
231         il.append(DUP);
232         il.append(new PUSH(cpg, name));
233         translateValue(classGen, methodGen);
234         il.append(new PUSH(cpg, true));
235
236         // Call addParameter() from this class
237
il.append(new INVOKEVIRTUAL(cpg.addMethodref(TRANSLET_CLASS,
238                              ADD_PARAMETER,
239                              ADD_PARAMETER_SIG)));
240
241         _type.translateUnBox(classGen, methodGen);
242
243         // Cache the result of addParameter() in a field
244
if (className != EMPTYSTRING) {
245             il.append(new CHECKCAST(cpg.addClass(className)));
246         }
247         il.append(new PUTFIELD(cpg.addFieldref(classGen.getClassName(),
248                                name, signature)));
249         }
250     }
251     }
252 }
253
Popular Tags