/// /// <summary> /// Determines if it is possible to coerce the given list of arguments /// so a method signature can be matched. /// </summary> /// /// <param name="args">List of arguments to check</param> /// /// <returns> /// True if exactly one argument is of an integral type, false otherwise. /// Idea is to only coerce integral arguments since floating point arguments /// would lose precision when converted to a narrower type and objects don't /// need it due to inheritance (i.e. they will match the method signature without /// it). If there is more than one integral argument, then the chance that the /// wrong overloaded method is called is too great, and so this method returns /// false meaning it can't safely coerce the integral args. If only one is found, /// and a method can't be matched without coercion, then we can safely narrow /// down that one integral arg without worry about matching the wrong method. /// </returns> /// private static bool CanCoerce(List <CseObject> args) { if (args == null) { return(false); } bool foundIntType = false; bool canCoerce = false; for (int i = 0; i < args.Count; i++) { CseObject arg = args[i]; if (LiteralExp.IsIntType(arg)) { if (!foundIntType) { canCoerce = true; foundIntType = true; } else { canCoerce = false; break; } } } return(canCoerce); }
/// /// <summary> /// Inspects all CseObjects in args and for the literal integral values, it /// converts their type to the most narrow type that can contain their value. /// E.g. if the value is a literal 300 (not the value of an identifier as those /// types aren't changed), then the smallest integral data type that can store /// that would be a short. This increases the chance that the given args will /// match a wider method signature. /// </summary> /// /// <param name="args">Arguments to inspect and convert</param> /// private static void NarrowAllIntTypes(ref List <CseObject> args) { if (args == null) { return; } for (int i = 0; i < args.Count; i++) { CseObject arg = args[i]; if (LiteralExp.IsIntType(arg) && arg.IsLiteral) { args[i] = LiteralExp.NarrowIntType(arg); args[i].CompileTimeType = args[i].Value.GetType(); } } }