// can this func be called using a call with signature callSig? // if callSig.returnType is any, then ignore return type compatibility // (NB: this doesn't consider implicit argument conversions, use // TypeManager.isMatchingFunc() for that) public boolean callCompatible(final FuncInfo callSig) { Debug.Assert(callSig != null); if (callSig.numArgs() < numRequiredArgs()) { return false; // not enough args } if (callSig.numArgs() > numArgs()) { return false; // too many args } // check types for (int a = 0; a < callSig.numArgs(); a++) { if (!_paramTypes[a].equals(callSig._paramTypes[a])) { return false; } } if (callSig.getReturnType() != null) { if (!callSig.getReturnType().equals(TypeSpec.typeOf("any"))) { if (!_returnType.equals(callSig.getReturnType())) { return false; } } } return true; }
public FuncInfo() { // default to noarg, any return _paramNames = new String[0]; _paramTypes = new TypeSpec[0]; _paramDefaults = new Object[0]; _paramHasDefault = new boolean[0]; _returnType = TypeSpec.typeOf("any"); }
// construct from types of an argument list public FuncInfo(final ArrayList args) { if (args == null) { // default to noarg, any return _paramNames = new String[0]; _paramTypes = new TypeSpec[0]; _paramDefaults = new Object[0]; _paramHasDefault = new boolean[0]; _returnType = TypeSpec.typeOf("any"); } else { final int n = args.size(); _paramNames = new String[n]; _paramTypes = new TypeSpec[n]; _paramDefaults = new Object[n]; _paramHasDefault = new boolean[n]; _returnType = TypeSpec.typeOf("any"); for (int a = 0; a < n; a++) { _paramNames[a] = null; _paramTypes[a] = null; if (args.get(a) instanceof Value) { // if an arg is null and is an LValue, extract it's declared // type final Value v = (Value) args.get(a); final boolean isNull = (v.getValue() == null) || ((v.getValue() instanceof Any) && (((Any) v.getValue()).value == null)); if (isNull && v.isLValue()) { _paramTypes[a] = v.getLValue().getSymbol().getType(); } // if we have an Any value, try to deduce the type from the // actual value within else if (v.getValue() instanceof Any) { // if we have an Any if (v.getValue() != null) { _paramTypes[a] = TypeSpec.typeOf(((Any) v.getValue()).value); } } } if (_paramTypes[a] == null) { _paramTypes[a] = TypeSpec.typeOf(args.get(a)); } _paramDefaults[a] = null; _paramHasDefault[a] = false; } } }
// 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; }