Exemple #1
0
  public Type childExpectedType(Expr child, AscriptionVisitor av) {
    TypeSystem ts = av.typeSystem();

    if (child == expr) {
      if (castType.type().isReference()) {
        return ts.Object();
      } else if (castType.type().isNumeric()) {
        return ts.Double();
      } else if (castType.type().isBoolean()) {
        return ts.Boolean();
      }
    }

    return child.type();
  }
Exemple #2
0
  /** Type check the expression. */
  public Node typeCheck(TypeChecker tc) throws SemanticException {
    TypeSystem ts = tc.typeSystem();

    if (!ts.isCastValid(expr.type(), castType.type())) {
      throw new SemanticException(
          "Cannot cast the expression of type \""
              + expr.type()
              + "\" to type \""
              + castType.type()
              + "\".",
          position());
    }

    return type(castType.type());
  }
Exemple #3
0
  public Object constantValue() {
    Object v = expr.constantValue();

    if (v == null) {
      return null;
    }

    if (v instanceof Boolean) {
      if (castType.type().isBoolean()) return v;
    }

    if (v instanceof String) {
      TypeSystem ts = castType.type().typeSystem();
      if (castType.type().equals(ts.String())) return v;
    }

    if (v instanceof Double) {
      double vv = ((Double) v).doubleValue();

      if (castType.type().isDouble()) return new Double((double) vv);
      if (castType.type().isFloat()) return new Float((float) vv);
      if (castType.type().isLong()) return new Long((long) vv);
      if (castType.type().isInt()) return new Integer((int) vv);
      if (castType.type().isChar()) return new Character((char) vv);
      if (castType.type().isShort()) return new Short((short) vv);
      if (castType.type().isByte()) return new Byte((byte) vv);
    }

    if (v instanceof Float) {
      float vv = ((Float) v).floatValue();

      if (castType.type().isDouble()) return new Double((double) vv);
      if (castType.type().isFloat()) return new Float((float) vv);
      if (castType.type().isLong()) return new Long((long) vv);
      if (castType.type().isInt()) return new Integer((int) vv);
      if (castType.type().isChar()) return new Character((char) vv);
      if (castType.type().isShort()) return new Short((short) vv);
      if (castType.type().isByte()) return new Byte((byte) vv);
    }

    if (v instanceof Number) {
      long vv = ((Number) v).longValue();

      if (castType.type().isDouble()) return new Double((double) vv);
      if (castType.type().isFloat()) return new Float((float) vv);
      if (castType.type().isLong()) return new Long((long) vv);
      if (castType.type().isInt()) return new Integer((int) vv);
      if (castType.type().isChar()) return new Character((char) vv);
      if (castType.type().isShort()) return new Short((short) vv);
      if (castType.type().isByte()) return new Byte((byte) vv);
    }

    if (v instanceof Character) {
      char vv = ((Character) v).charValue();

      if (castType.type().isDouble()) return new Double((double) vv);
      if (castType.type().isFloat()) return new Float((float) vv);
      if (castType.type().isLong()) return new Long((long) vv);
      if (castType.type().isInt()) return new Integer((int) vv);
      if (castType.type().isChar()) return new Character((char) vv);
      if (castType.type().isShort()) return new Short((short) vv);
      if (castType.type().isByte()) return new Byte((byte) vv);
    }

    // not a constant
    return null;
  }
Exemple #4
0
 public boolean isConstant() {
   return expr.isConstant() && castType.type().isPrimitive();
 }