KickJava   Java API By Example, From Geeks To Geeks.

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


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: BooleanType.java,v 1.8 2004/02/16 22:26:44 minchau Exp $
18  */

19
20 package com.sun.org.apache.xalan.internal.xsltc.compiler.util;
21
22 import com.sun.org.apache.bcel.internal.generic.BranchHandle;
23 import com.sun.org.apache.bcel.internal.generic.BranchInstruction;
24 import com.sun.org.apache.bcel.internal.generic.CHECKCAST;
25 import com.sun.org.apache.bcel.internal.generic.ConstantPoolGen;
26 import com.sun.org.apache.bcel.internal.generic.GOTO;
27 import com.sun.org.apache.bcel.internal.generic.IFEQ;
28 import com.sun.org.apache.bcel.internal.generic.IFGE;
29 import com.sun.org.apache.bcel.internal.generic.IFGT;
30 import com.sun.org.apache.bcel.internal.generic.IFLE;
31 import com.sun.org.apache.bcel.internal.generic.IFLT;
32 import com.sun.org.apache.bcel.internal.generic.IF_ICMPGE;
33 import com.sun.org.apache.bcel.internal.generic.IF_ICMPGT;
34 import com.sun.org.apache.bcel.internal.generic.IF_ICMPLE;
35 import com.sun.org.apache.bcel.internal.generic.IF_ICMPLT;
36 import com.sun.org.apache.bcel.internal.generic.ILOAD;
37 import com.sun.org.apache.bcel.internal.generic.INVOKESPECIAL;
38 import com.sun.org.apache.bcel.internal.generic.INVOKEVIRTUAL;
39 import com.sun.org.apache.bcel.internal.generic.ISTORE;
40 import com.sun.org.apache.bcel.internal.generic.Instruction;
41 import com.sun.org.apache.bcel.internal.generic.InstructionList;
42 import com.sun.org.apache.bcel.internal.generic.NEW;
43 import com.sun.org.apache.bcel.internal.generic.PUSH;
44 import com.sun.org.apache.xalan.internal.xsltc.compiler.Constants;
45
46 /**
47  * @author Jacek Ambroziak
48  * @author Santiago Pericas-Geertsen
49  */

50 public final class BooleanType extends Type {
51     protected BooleanType() {}
52
53     public String JavaDoc toString() {
54     return "boolean";
55     }
56
57     public boolean identicalTo(Type other) {
58     return this == other;
59     }
60
61     public String JavaDoc toSignature() {
62     return "Z";
63     }
64
65     public boolean isSimple() {
66     return true;
67     }
68
69     public com.sun.org.apache.bcel.internal.generic.Type toJCType() {
70     return com.sun.org.apache.bcel.internal.generic.Type.BOOLEAN;
71     }
72
73     /**
74      * Translates a real into an object of internal type <code>type</code>. The
75      * translation to int is undefined since booleans are always converted to
76      * reals in arithmetic expressions.
77      *
78      * @see com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type#translateTo
79      */

80     public void translateTo(ClassGenerator classGen, MethodGenerator methodGen,
81                 Type type) {
82     if (type == Type.String) {
83         translateTo(classGen, methodGen, (StringType) type);
84     }
85     else if (type == Type.Real) {
86         translateTo(classGen, methodGen, (RealType) type);
87     }
88     else if (type == Type.Reference) {
89         translateTo(classGen, methodGen, (ReferenceType) type);
90     }
91     else {
92         ErrorMsg err = new ErrorMsg(ErrorMsg.DATA_CONVERSION_ERR,
93                     toString(), type.toString());
94         classGen.getParser().reportError(Constants.FATAL, err);
95     }
96     }
97
98     /**
99      * Expects a boolean on the stack and pushes a string. If the value on the
100      * stack is zero, then the string 'false' is pushed. Otherwise, the string
101      * 'true' is pushed.
102      *
103      * @see com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type#translateTo
104      */

105     public void translateTo(ClassGenerator classGen, MethodGenerator methodGen,
106                 StringType type) {
107     final ConstantPoolGen cpg = classGen.getConstantPool();
108     final InstructionList il = methodGen.getInstructionList();
109     final BranchHandle falsec = il.append(new IFEQ(null));
110     il.append(new PUSH(cpg, "true"));
111     final BranchHandle truec = il.append(new GOTO(null));
112     falsec.setTarget(il.append(new PUSH(cpg, "false")));
113     truec.setTarget(il.append(NOP));
114     }
115
116     /**
117      * Expects a boolean on the stack and pushes a real. The value "true" is
118      * converted to 1.0 and the value "false" to 0.0.
119      *
120      * @see com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type#translateTo
121      */

122     public void translateTo(ClassGenerator classGen, MethodGenerator methodGen,
123                 RealType type) {
124     methodGen.getInstructionList().append(I2D);
125     }
126
127     /**
128      * Expects a boolean on the stack and pushes a boxed boolean.
129      * Boxed booleans are represented by an instance of
130      * <code>java.lang.Boolean</code>.
131      *
132      * @see com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type#translateTo
133      */

134     public void translateTo(ClassGenerator classGen, MethodGenerator methodGen,
135                 ReferenceType type) {
136     final ConstantPoolGen cpg = classGen.getConstantPool();
137     final InstructionList il = methodGen.getInstructionList();
138     il.append(new NEW(cpg.addClass(BOOLEAN_CLASS)));
139     il.append(DUP_X1);
140     il.append(SWAP);
141     il.append(new INVOKESPECIAL(cpg.addMethodref(BOOLEAN_CLASS,
142                              "<init>",
143                              "(Z)V")));
144     }
145
146     /**
147      * Translates an internal boolean into an external (Java) boolean.
148      */

149     public void translateTo(ClassGenerator classGen, MethodGenerator methodGen,
150                 Class JavaDoc clazz) {
151     if (clazz == java.lang.Boolean.TYPE) {
152         methodGen.getInstructionList().append(NOP);
153     }
154         // Is Boolean <: clazz? I.e. clazz in { Boolean, Object }
155
else if (clazz.isAssignableFrom(java.lang.Boolean JavaDoc.class)) {
156             translateTo(classGen, methodGen, Type.Reference);
157         }
158     else {
159         ErrorMsg err = new ErrorMsg(ErrorMsg.DATA_CONVERSION_ERR,
160                     toString(), clazz.getName());
161         classGen.getParser().reportError(Constants.FATAL, err);
162     }
163     }
164
165     /**
166      * Translates an external (Java) boolean into internal boolean.
167      */

168     public void translateFrom(ClassGenerator classGen, MethodGenerator methodGen,
169                   Class JavaDoc clazz) {
170     translateTo(classGen, methodGen, clazz);
171     }
172
173     /**
174      * Translates an object of this type to its boxed representation.
175      */

176     public void translateBox(ClassGenerator classGen,
177                  MethodGenerator methodGen) {
178     translateTo(classGen, methodGen, Type.Reference);
179     }
180
181     /**
182      * Translates an object of this type to its unboxed representation.
183      */

184     public void translateUnBox(ClassGenerator classGen,
185                    MethodGenerator methodGen) {
186     final ConstantPoolGen cpg = classGen.getConstantPool();
187     final InstructionList il = methodGen.getInstructionList();
188     il.append(new CHECKCAST(cpg.addClass(BOOLEAN_CLASS)));
189     il.append(new INVOKEVIRTUAL(cpg.addMethodref(BOOLEAN_CLASS,
190                              BOOLEAN_VALUE,
191                              BOOLEAN_VALUE_SIG)));
192     }
193
194     public Instruction LOAD(int slot) {
195     return new ILOAD(slot);
196     }
197     
198     public Instruction STORE(int slot) {
199     return new ISTORE(slot);
200     }
201
202     public BranchInstruction GT(boolean tozero) {
203     return tozero ? (BranchInstruction) new IFGT(null) :
204         (BranchInstruction) new IF_ICMPGT(null);
205     }
206
207     public BranchInstruction GE(boolean tozero) {
208     return tozero ? (BranchInstruction) new IFGE(null) :
209         (BranchInstruction) new IF_ICMPGE(null);
210     }
211
212     public BranchInstruction LT(boolean tozero) {
213     return tozero ? (BranchInstruction) new IFLT(null) :
214         (BranchInstruction) new IF_ICMPLT(null);
215     }
216
217     public BranchInstruction LE(boolean tozero) {
218     return tozero ? (BranchInstruction) new IFLE(null) :
219         (BranchInstruction) new IF_ICMPLE(null);
220     }
221 }
222
Popular Tags