/// <summary>Finds the best methods for the specified arguments given a candidate method enumeration.</summary> /// <param name="signatures">The candidate function signatures.</param> /// <param name="argumentNodes">The argument nodes, can be new {null,null}.</param> /// <param name="argumentTypes">The argument type references to match.</param> /// <returns>The number of "best match" methods.</returns> private static int FindBestSignature(FunctionSignature[] signatures, SingleValueNode[] argumentNodes, IEdmTypeReference[] argumentTypes) { Debug.Assert(signatures != null, "signatures != null"); Debug.Assert(argumentTypes != null, "argumentTypes != null"); Debug.Assert(signatures.All(s => s.ArgumentTypes != null && s.ArgumentTypes.All(t => t.IsODataPrimitiveTypeKind())), "All signatures must have only primitive argument types."); List<FunctionSignature> applicableSignatures = signatures.Where(signature => IsApplicable(signature, argumentNodes, argumentTypes)).ToList(); if (applicableSignatures.Count > 1) { applicableSignatures = FindBestApplicableSignatures(applicableSignatures, argumentTypes); } int result = applicableSignatures.Count; if (result == 1) { FunctionSignature signature = applicableSignatures[0]; for (int i = 0; i < argumentTypes.Length; i++) { argumentTypes[i] = signature.ArgumentTypes[i]; } result = 1; } else if (result > 1) { // We may have the case for operators (which C# doesn't) in which we have a nullable operand // and a non-nullable operand. We choose to convert the one non-null operand to nullable in that // case (the binary expression will lift to null). if (argumentTypes.Length == 2 && result == 2) { if (applicableSignatures[0].ArgumentTypes[0].Definition.IsEquivalentTo(applicableSignatures[1].ArgumentTypes[0].Definition)) { FunctionSignature nullableMethod = applicableSignatures[0].ArgumentTypes[0].IsNullable ? applicableSignatures[0] : applicableSignatures[1]; argumentTypes[0] = nullableMethod.ArgumentTypes[0]; argumentTypes[1] = nullableMethod.ArgumentTypes[1]; // TODO: why is this necessary? We keep it here for now since the product has it but assert // that nothing new was found. int signatureCount = FindBestSignature(signatures, argumentNodes, argumentTypes); Debug.Assert(signatureCount == 1, "signatureCount == 1"); Debug.Assert(argumentTypes[0] == nullableMethod.ArgumentTypes[0], "argumentTypes[0] == nullableMethod.ArgumentTypes[0]"); Debug.Assert(argumentTypes[1] == nullableMethod.ArgumentTypes[1], "argumentTypes[1] == nullableMethod.ArgumentTypes[1]"); return signatureCount; } } } return result; }
/// <summary>Checks whether the specified method is applicable given the argument expressions.</summary> /// <param name="signature">The candidate function signature to check.</param> /// <param name="argumentNodes">The argument nodes, can be new {null,null}.</param> /// <param name="argumentTypes">The argument types to match.</param> /// <returns>An applicable function signature if all argument types can be promoted; 'null' otherwise.</returns> private static bool IsApplicable(FunctionSignature signature, SingleValueNode[] argumentNodes, IEdmTypeReference[] argumentTypes) { Debug.Assert(signature != null, "signature != null"); Debug.Assert(argumentTypes != null, "argumentTypes != null"); if (signature.ArgumentTypes.Length != argumentTypes.Length) { return false; } for (int i = 0; i < argumentTypes.Length; ++i) { if (!CanPromoteNodeTo(argumentNodes[i], argumentTypes[i], signature.ArgumentTypes[i])) { return false; } } return true; }
/// <summary>Finds the exact fitting function for the specified arguments.</summary> /// <param name="functions">Functions to consider.</param> /// <param name="argumentTypes">Types of the arguments for the function.</param> /// <returns>The exact fitting function; null if no exact match was found.</returns> internal static FunctionSignature FindExactFunctionSignature(FunctionSignature[] functions, IEdmTypeReference[] argumentTypes) { Debug.Assert(functions != null, "functions != null"); Debug.Assert(argumentTypes != null, "argumentTypes != null"); for (int functionIndex = 0; functionIndex < functions.Length; functionIndex++) { FunctionSignature functionSignature = functions[functionIndex]; bool matchFound = true; if (functionSignature.ArgumentTypes.Length != argumentTypes.Length) { continue; } for (int argumentIndex = 0; argumentIndex < argumentTypes.Length; argumentIndex++) { IEdmTypeReference functionSignatureArgumentType = functionSignature.ArgumentTypes[argumentIndex]; IEdmTypeReference argumentType = argumentTypes[argumentIndex]; Debug.Assert(functionSignatureArgumentType.IsODataPrimitiveTypeKind(), "Only primitive arguments are supported for functions."); if (!argumentType.IsODataPrimitiveTypeKind()) { matchFound = false; break; } if (!argumentType.IsEquivalentTo(functionSignatureArgumentType)) { matchFound = false; break; } } if (matchFound) { return functionSignature; } } return null; }