/**
   * Type check a call to a standard function. Insert CastExprs when needed. If as a result of the
   * insertion of a CastExpr a type check error is thrown, then catch it and re-throw it with a new
   * "this".
   */
  public Type typeCheckStandard(SymbolTable stable) throws TypeCheckError {
    _fname.clearNamespace(); // HACK!!!

    final int n = _arguments.size();
    final Vector argsType = typeCheckArgs(stable);
    final MethodType args = new MethodType(Type.Void, argsType);
    final MethodType ptype = lookupPrimop(stable, _fname.getLocalPart(), args);

    if (ptype != null) {
      for (int i = 0; i < n; i++) {
        final Type argType = (Type) ptype.argsType().elementAt(i);
        final Expression exp = (Expression) _arguments.elementAt(i);
        if (!argType.identicalTo(exp.getType())) {
          try {
            _arguments.setElementAt(new CastExpr(exp, argType), i);
          } catch (TypeCheckError e) {
            throw new TypeCheckError(this); // invalid conversion
          }
        }
      }
      _chosenMethodType = ptype;
      return _type = ptype.resultType();
    }
    throw new TypeCheckError(this);
  }
Esempio n. 2
0
 public Type typeCheck(SymbolTable stable) throws TypeCheckError {
   final Type tleft = _left.typeCheck(stable);
   final Type tright = _right.typeCheck(stable);
   final MethodType ptype =
       lookupPrimop(stable, Ops[_op], new MethodType(Type.Void, tleft, tright));
   if (ptype != null) {
     final Type arg1 = (Type) ptype.argsType().elementAt(0);
     if (!arg1.identicalTo(tleft)) {
       _left = new CastExpr(_left, arg1);
     }
     final Type arg2 = (Type) ptype.argsType().elementAt(1);
     if (!arg2.identicalTo(tright)) {
       _right = new CastExpr(_right, arg1);
     }
     return _type = ptype.resultType();
   }
   throw new TypeCheckError(this);
 }