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. package com.sun.org.apache.xalan.internal.xsltc.compiler.util;
  20. import com.sun.org.apache.bcel.internal.generic.BranchInstruction;
  21. import com.sun.org.apache.bcel.internal.generic.Instruction;
  22. import com.sun.org.apache.xalan.internal.xsltc.compiler.Constants;
  23. import com.sun.org.apache.xalan.internal.xsltc.compiler.FlowList;
  24. import com.sun.org.apache.xalan.internal.xsltc.compiler.NodeTest;
  25. /**
  26. * @author Jacek Ambroziak
  27. * @author Santiago Pericas-Geertsen
  28. * @author Morten Jorgensen
  29. */
  30. public abstract class Type implements Constants {
  31. public static final Type Int = new IntType();
  32. public static final Type Real = new RealType();
  33. public static final Type Boolean = new BooleanType();
  34. public static final Type NodeSet = new NodeSetType();
  35. public static final Type String = new StringType();
  36. public static final Type ResultTree = new ResultTreeType();
  37. public static final Type Reference = new ReferenceType();
  38. public static final Type Void = new VoidType();
  39. public static final Type Object = new ObjectType(java.lang.Object.class);
  40. public static final Type ObjectString = new ObjectType(java.lang.String.class);
  41. public static final Type Node = new NodeType(NodeTest.ANODE);
  42. public static final Type Root = new NodeType(NodeTest.ROOT);
  43. public static final Type Element = new NodeType(NodeTest.ELEMENT);
  44. public static final Type Attribute = new NodeType(NodeTest.ATTRIBUTE);
  45. public static final Type Text = new NodeType(NodeTest.TEXT);
  46. public static final Type Comment = new NodeType(NodeTest.COMMENT);
  47. public static final Type Processing_Instruction = new NodeType(NodeTest.PI);
  48. /**
  49. * Factory method to instantiate object types. Returns a pre-defined
  50. * instance for "java.lang.Object" and "java.lang.String".
  51. */
  52. public static Type newObjectType(String javaClassName) {
  53. if (javaClassName == "java.lang.Object") {
  54. return Type.Object;
  55. }
  56. else if (javaClassName == "java.lang.String") {
  57. return Type.ObjectString;
  58. }
  59. else {
  60. return new ObjectType(javaClassName);
  61. }
  62. }
  63. /**
  64. * Factory method to instantiate object types. Returns a pre-defined
  65. * instance for java.lang.Object.class and java.lang.String.class.
  66. */
  67. public static Type newObjectType(Class clazz) {
  68. if (clazz == java.lang.Object.class) {
  69. return Type.Object;
  70. }
  71. else if (clazz == java.lang.String.class) {
  72. return Type.ObjectString;
  73. }
  74. else {
  75. return new ObjectType(clazz);
  76. }
  77. }
  78. /**
  79. * Returns a string representation of this type.
  80. */
  81. public abstract String toString();
  82. /**
  83. * Returns true if this and other are identical types.
  84. */
  85. public abstract boolean identicalTo(Type other);
  86. /**
  87. * Returns true if this type is a numeric type. Redefined in NumberType.
  88. */
  89. public boolean isNumber() {
  90. return false;
  91. }
  92. /**
  93. * Returns true if this type has no object representaion. Redefined in
  94. * ResultTreeType.
  95. */
  96. public boolean implementedAsMethod() {
  97. return false;
  98. }
  99. /**
  100. * Returns true if this type is a simple type. Redefined in NumberType,
  101. * BooleanType and StringType.
  102. */
  103. public boolean isSimple() {
  104. return false;
  105. }
  106. public abstract com.sun.org.apache.bcel.internal.generic.Type toJCType();
  107. /**
  108. * Returns the distance between two types. This measure is used to select
  109. * overloaded functions/operators. This method is typically redefined by
  110. * the subclasses.
  111. */
  112. public int distanceTo(Type type) {
  113. return type == this ? 0 : Integer.MAX_VALUE;
  114. }
  115. /**
  116. * Returns the signature of an internal type's external representation.
  117. */
  118. public abstract String toSignature();
  119. /**
  120. * Translates an object of this type to an object of type
  121. * <code>type</code>.
  122. * Expects an object of the former type and pushes an object of the latter.
  123. */
  124. public void translateTo(ClassGenerator classGen, MethodGenerator methodGen,
  125. Type type) {
  126. ErrorMsg err = new ErrorMsg(ErrorMsg.DATA_CONVERSION_ERR,
  127. toString(), type.toString());
  128. classGen.getParser().reportError(Constants.FATAL, err);
  129. }
  130. /**
  131. * Translates object of this type to an object of type <code>type</code>.
  132. * Expects an object of the former type and pushes an object of the latter
  133. * if not boolean. If type <code>type</code> is boolean then a branchhandle
  134. * list (to be appended to the false list) is returned.
  135. */
  136. public FlowList translateToDesynthesized(ClassGenerator classGen,
  137. MethodGenerator methodGen,
  138. Type type) {
  139. FlowList fl = null;
  140. if (type == Type.Boolean) {
  141. fl = translateToDesynthesized(classGen, methodGen,
  142. (BooleanType)type);
  143. }
  144. else {
  145. translateTo(classGen, methodGen, type);
  146. }
  147. return fl;
  148. }
  149. /**
  150. * Translates an object of this type to an non-synthesized boolean. It
  151. * does not push a 0 or a 1 but instead returns branchhandle list to be
  152. * appended to the false list.
  153. */
  154. public FlowList translateToDesynthesized(ClassGenerator classGen,
  155. MethodGenerator methodGen,
  156. BooleanType type) {
  157. ErrorMsg err = new ErrorMsg(ErrorMsg.DATA_CONVERSION_ERR,
  158. toString(), type.toString());
  159. classGen.getParser().reportError(Constants.FATAL, err);
  160. return null;
  161. }
  162. /**
  163. * Translates an object of this type to the external (Java) type denoted
  164. * by <code>clazz</code>. This method is used to translate parameters
  165. * when external functions are called.
  166. */
  167. public void translateTo(ClassGenerator classGen, MethodGenerator methodGen,
  168. Class clazz) {
  169. ErrorMsg err = new ErrorMsg(ErrorMsg.DATA_CONVERSION_ERR,
  170. toString(), clazz.getClass().toString());
  171. classGen.getParser().reportError(Constants.FATAL, err);
  172. }
  173. /**
  174. * Translates an external (Java) type denoted by <code>clazz</code> to
  175. * an object of this type. This method is used to translate return values
  176. * when external functions are called.
  177. */
  178. public void translateFrom(ClassGenerator classGen, MethodGenerator methodGen,
  179. Class clazz) {
  180. ErrorMsg err = new ErrorMsg(ErrorMsg.DATA_CONVERSION_ERR,
  181. clazz.getClass().toString(), toString());
  182. classGen.getParser().reportError(Constants.FATAL, err);
  183. }
  184. /**
  185. * Translates an object of this type to its boxed representation.
  186. */
  187. public void translateBox(ClassGenerator classGen,
  188. MethodGenerator methodGen) {
  189. ErrorMsg err = new ErrorMsg(ErrorMsg.DATA_CONVERSION_ERR,
  190. toString(), "["+toString()+"]");
  191. classGen.getParser().reportError(Constants.FATAL, err);
  192. }
  193. /**
  194. * Translates an object of this type to its unboxed representation.
  195. */
  196. public void translateUnBox(ClassGenerator classGen,
  197. MethodGenerator methodGen) {
  198. ErrorMsg err = new ErrorMsg(ErrorMsg.DATA_CONVERSION_ERR,
  199. "["+toString()+"]", toString());
  200. classGen.getParser().reportError(Constants.FATAL, err);
  201. }
  202. /**
  203. * Returns the class name of an internal type's external representation.
  204. */
  205. public String getClassName() {
  206. return(EMPTYSTRING);
  207. }
  208. public Instruction ADD() {
  209. return null; // should never be called
  210. }
  211. public Instruction SUB() {
  212. return null; // should never be called
  213. }
  214. public Instruction MUL() {
  215. return null; // should never be called
  216. }
  217. public Instruction DIV() {
  218. return null; // should never be called
  219. }
  220. public Instruction REM() {
  221. return null; // should never be called
  222. }
  223. public Instruction NEG() {
  224. return null; // should never be called
  225. }
  226. public Instruction LOAD(int slot) {
  227. return null; // should never be called
  228. }
  229. public Instruction STORE(int slot) {
  230. return null; // should never be called
  231. }
  232. public Instruction POP() {
  233. return POP;
  234. }
  235. public BranchInstruction GT(boolean tozero) {
  236. return null; // should never be called
  237. }
  238. public BranchInstruction GE(boolean tozero) {
  239. return null; // should never be called
  240. }
  241. public BranchInstruction LT(boolean tozero) {
  242. return null; // should never be called
  243. }
  244. public BranchInstruction LE(boolean tozero) {
  245. return null; // should never be called
  246. }
  247. public Instruction CMP(boolean less) {
  248. return null; // should never be called
  249. }
  250. public Instruction DUP() {
  251. return DUP; // default
  252. }
  253. }