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