// 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 static TypeSpec propertyTypeFromAccessor( final String accessorName, final FuncInfo accessorSig) { if (accessorName.equals("get")) { return accessorSig._returnType; } else if (accessorName.equals("set")) { return accessorSig._paramTypes[accessorSig.numArgs() - 1]; } else { Debug.Assert(false, "invalid accessor name"); } return null; }
// compare for equality, ignoring return types // NB: doens't consider param names or default values public boolean equalsParams(final FuncInfo f) { if (f == null) { return false; } if (f.numArgs() != numArgs()) { return false; } if (f.numRequiredArgs() != numRequiredArgs()) { return false; } for (int a = 0; a < numArgs(); a++) { if (!_paramTypes[a].equals(f._paramTypes[a])) { return false; } } return true; }
// 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; }