Exemplo n.º 1
0
  // convert the args for a call with signature callSig that is compatible
  // with this
  // to exactly the types this func expects (including filling in defaults if
  // possible)
  // throws on arg type or number mismatch
  public Object[] convertParameters(
      final FuncInfo callSig, Object[] args, final boolean externCall) {
    if (args == null) {
      args = new Object[0];
    }

    Debug.Assert(callSig != null);
    Debug.Assert(callSig.numArgs() == args.length, "supplied arg count must match callSig");

    // if (externCall) //!!!
    // Debug.WriteLine("Warning: FuncInfo.convertParameters() - externCall being ignored
    // (unimplemented)");

    if (numRequiredArgs() > callSig.numArgs()) {
      ScigolTreeParser.semanticError(
          callSig.getDefinitionLocation(),
          "not enough arguments for function " + this + " in call with signature " + callSig);
    }

    if (callSig.numArgs() > numArgs()) {
      ScigolTreeParser.semanticError(
          callSig.getDefinitionLocation(),
          "too many arguments for function " + this + " in call with signature " + callSig);
    }

    final Object[] convertedArgs = new Object[numArgs()];

    // for each formal param
    for (int a = 0; a < numArgs(); a++) {

      if (a < callSig.numArgs()) { // if param supplied
        Object arg =
            ((args[a] instanceof Value)
                ? (((Value) args[a]).getValue())
                : args[a]); // convert from Value if
        // necessary

        TypeSpec argType = TypeSpec.typeOf(arg);
        if (!argType.isAny()) { // implicit conversion from any always
          // exists
          // does an implicit conversion from the argType to the
          // parameter type exist?
          if (!TypeManager.existsImplicitConversion(argType, _paramTypes[a], new Value(arg))) {
            ScigolTreeParser.semanticError(
                callSig.getDefinitionLocation(),
                "argument "
                    + a
                    + " ("
                    + _paramNames[a]
                    + ") to func with signature "
                    + this
                    + " is of type '"
                    + argType
                    + "' which is incompatible with parameter type '"
                    + _paramTypes[a]
                    + "'");
          }
        } else {
          arg = TypeSpec.unwrapAny(arg);
          argType = TypeSpec.typeOf(arg);
        }

        // conversion
        final Value convertedArg =
            TypeManager.performImplicitConversion(argType, _paramTypes[a], new Value(arg));

        if (convertedArg == null) {
          ScigolTreeParser.semanticError(
              callSig.getDefinitionLocation(),
              "argument "
                  + a
                  + " ("
                  + _paramNames[a]
                  + ") to func with signature "
                  + this
                  + " is of type '"
                  + argType
                  + "' which cannot be converted into the required parameter type '"
                  + _paramTypes[a]
                  + "'");
        }

        convertedArgs[a] = convertedArg.getValue();
      } else { // not supplied
        // do we have a default value?
        if (!_paramHasDefault[a]) {
          ScigolTreeParser.semanticError(
              callSig.getDefinitionLocation(),
              "in call to function "
                  + this
                  + ", no argument was supplied for parameter "
                  + _paramNames[a]
                  + " (which has no default)");
        }

        // use default
        convertedArgs[a] = _paramDefaults[a];
      }
    } // for each param

    return convertedArgs;
  }