KickJava   Java API By Example, From Geeks To Geeks.

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


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: Type.java,v 1.15 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.BranchInstruction;
23 import com.sun.org.apache.bcel.internal.generic.Instruction;
24 import com.sun.org.apache.xalan.internal.xsltc.compiler.Constants;
25 import com.sun.org.apache.xalan.internal.xsltc.compiler.FlowList;
26 import com.sun.org.apache.xalan.internal.xsltc.compiler.NodeTest;
27
28 /**
29  * @author Jacek Ambroziak
30  * @author Santiago Pericas-Geertsen
31  * @author Morten Jorgensen
32  */

33 public abstract class Type implements Constants {
34     public static final Type Int = new IntType();
35     public static final Type Real = new RealType();
36     public static final Type Boolean = new BooleanType();
37     public static final Type NodeSet = new NodeSetType();
38     public static final Type String = new StringType();
39     public static final Type ResultTree = new ResultTreeType();
40     public static final Type Reference = new ReferenceType();
41     public static final Type Void = new VoidType();
42     
43     public static final Type Object = new ObjectType(java.lang.Object JavaDoc.class);
44     public static final Type ObjectString = new ObjectType(java.lang.String JavaDoc.class);
45
46     public static final Type Node = new NodeType(NodeTest.ANODE);
47     public static final Type Root = new NodeType(NodeTest.ROOT);
48     public static final Type Element = new NodeType(NodeTest.ELEMENT);
49     public static final Type Attribute = new NodeType(NodeTest.ATTRIBUTE);
50     public static final Type Text = new NodeType(NodeTest.TEXT);
51     public static final Type Comment = new NodeType(NodeTest.COMMENT);
52     public static final Type Processing_Instruction = new NodeType(NodeTest.PI);
53
54     /**
55      * Factory method to instantiate object types. Returns a pre-defined
56      * instance for "java.lang.Object" and "java.lang.String".
57      */

58     public static Type newObjectType(String JavaDoc javaClassName) {
59         if (javaClassName == "java.lang.Object") {
60             return Type.Object;
61         }
62         else if (javaClassName == "java.lang.String") {
63             return Type.ObjectString;
64         }
65         else {
66             //
67
java.security.AccessControlContext JavaDoc acc = java.security.AccessController.getContext();
68             acc.checkPermission(new RuntimePermission JavaDoc("getContextClassLoader"));
69
70             return new ObjectType(javaClassName);
71         }
72     }
73     
74    /**
75      * Factory method to instantiate object types. Returns a pre-defined
76      * instance for java.lang.Object.class and java.lang.String.class.
77      */

78     public static Type newObjectType(Class JavaDoc clazz) {
79         if (clazz == java.lang.Object JavaDoc.class) {
80             return Type.Object;
81         }
82         else if (clazz == java.lang.String JavaDoc.class) {
83             return Type.ObjectString;
84         }
85         else {
86             return new ObjectType(clazz);
87         }
88     }
89     
90     /**
91      * Returns a string representation of this type.
92      */

93     public abstract String JavaDoc toString();
94
95     /**
96      * Returns true if this and other are identical types.
97      */

98     public abstract boolean identicalTo(Type other);
99
100     /**
101      * Returns true if this type is a numeric type. Redefined in NumberType.
102      */

103     public boolean isNumber() {
104     return false;
105     }
106
107     /**
108      * Returns true if this type has no object representaion. Redefined in
109      * ResultTreeType.
110      */

111     public boolean implementedAsMethod() {
112     return false;
113     }
114
115     /**
116      * Returns true if this type is a simple type. Redefined in NumberType,
117      * BooleanType and StringType.
118      */

119     public boolean isSimple() {
120     return false;
121     }
122
123     public abstract com.sun.org.apache.bcel.internal.generic.Type toJCType();
124
125     /**
126      * Returns the distance between two types. This measure is used to select
127      * overloaded functions/operators. This method is typically redefined by
128      * the subclasses.
129      */

130     public int distanceTo(Type type) {
131     return type == this ? 0 : Integer.MAX_VALUE;
132     }
133
134     /**
135      * Returns the signature of an internal type's external representation.
136      */

137     public abstract String JavaDoc toSignature();
138
139     /**
140      * Translates an object of this type to an object of type
141      * <code>type</code>.
142      * Expects an object of the former type and pushes an object of the latter.
143      */

144     public void translateTo(ClassGenerator classGen, MethodGenerator methodGen,
145                 Type type) {
146     ErrorMsg err = new ErrorMsg(ErrorMsg.DATA_CONVERSION_ERR,
147                     toString(), type.toString());
148     classGen.getParser().reportError(Constants.FATAL, err);
149     }
150
151     /**
152      * Translates object of this type to an object of type <code>type</code>.
153      * Expects an object of the former type and pushes an object of the latter
154      * if not boolean. If type <code>type</code> is boolean then a branchhandle
155      * list (to be appended to the false list) is returned.
156      */

157     public FlowList translateToDesynthesized(ClassGenerator classGen,
158                          MethodGenerator methodGen,
159                          Type type) {
160     FlowList fl = null;
161     if (type == Type.Boolean) {
162         fl = translateToDesynthesized(classGen, methodGen,
163                       (BooleanType)type);
164     }
165     else {
166         translateTo(classGen, methodGen, type);
167     }
168     return fl;
169     }
170
171     /**
172      * Translates an object of this type to an non-synthesized boolean. It
173      * does not push a 0 or a 1 but instead returns branchhandle list to be
174      * appended to the false list.
175      */

176     public FlowList translateToDesynthesized(ClassGenerator classGen,
177                          MethodGenerator methodGen,
178                          BooleanType type) {
179     ErrorMsg err = new ErrorMsg(ErrorMsg.DATA_CONVERSION_ERR,
180                     toString(), type.toString());
181     classGen.getParser().reportError(Constants.FATAL, err);
182     return null;
183     }
184
185     /**
186      * Translates an object of this type to the external (Java) type denoted
187      * by <code>clazz</code>. This method is used to translate parameters
188      * when external functions are called.
189      */

190     public void translateTo(ClassGenerator classGen, MethodGenerator methodGen,
191                 Class JavaDoc clazz) {
192     ErrorMsg err = new ErrorMsg(ErrorMsg.DATA_CONVERSION_ERR,
193                     toString(), clazz.getClass().toString());
194     classGen.getParser().reportError(Constants.FATAL, err);
195     }
196
197     /**
198      * Translates an external (Java) type denoted by <code>clazz</code> to
199      * an object of this type. This method is used to translate return values
200      * when external functions are called.
201      */

202     public void translateFrom(ClassGenerator classGen, MethodGenerator methodGen,
203                   Class JavaDoc clazz) {
204     ErrorMsg err = new ErrorMsg(ErrorMsg.DATA_CONVERSION_ERR,
205                     clazz.getClass().toString(), toString());
206     classGen.getParser().reportError(Constants.FATAL, err);
207     }
208
209     /**
210      * Translates an object of this type to its boxed representation.
211      */

212     public void translateBox(ClassGenerator classGen,
213                  MethodGenerator methodGen) {
214     ErrorMsg err = new ErrorMsg(ErrorMsg.DATA_CONVERSION_ERR,
215                     toString(), "["+toString()+"]");
216     classGen.getParser().reportError(Constants.FATAL, err);
217     }
218
219     /**
220      * Translates an object of this type to its unboxed representation.
221      */

222     public void translateUnBox(ClassGenerator classGen,
223                    MethodGenerator methodGen) {
224     ErrorMsg err = new ErrorMsg(ErrorMsg.DATA_CONVERSION_ERR,
225                     "["+toString()+"]", toString());
226     classGen.getParser().reportError(Constants.FATAL, err);
227     }
228
229     /**
230      * Returns the class name of an internal type's external representation.
231      */

232     public String JavaDoc getClassName() {
233     return(EMPTYSTRING);
234     }
235
236     public Instruction ADD() {
237     return null; // should never be called
238
}
239
240     public Instruction SUB() {
241     return null; // should never be called
242
}
243
244     public Instruction MUL() {
245     return null; // should never be called
246
}
247
248     public Instruction DIV() {
249     return null; // should never be called
250
}
251
252     public Instruction REM() {
253     return null; // should never be called
254
}
255
256     public Instruction NEG() {
257     return null; // should never be called
258
}
259
260     public Instruction LOAD(int slot) {
261     return null; // should never be called
262
}
263     
264     public Instruction STORE(int slot) {
265     return null; // should never be called
266
}
267
268     public Instruction POP() {
269     return POP;
270     }
271
272     public BranchInstruction GT(boolean tozero) {
273     return null; // should never be called
274
}
275
276     public BranchInstruction GE(boolean tozero) {
277     return null; // should never be called
278
}
279
280     public BranchInstruction LT(boolean tozero) {
281     return null; // should never be called
282
}
283
284     public BranchInstruction LE(boolean tozero) {
285     return null; // should never be called
286
}
287
288     public Instruction CMP(boolean less) {
289     return null; // should never be called
290
}
291     
292     public Instruction DUP() {
293     return DUP; // default
294
}
295 }
296
Popular Tags